@Test public void testProcessDdMessage() throws Exception { ospfInterface.setOspfArea(ospfArea); ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11")); ospfInterface.setInterfaceType(2); ospfInterface.setIpNetworkMask(Ip4Address.valueOf("255.255.255.255")); ospfInterface.setHelloIntervalTime(10); ospfInterface.setRouterDeadIntervalTime(10); ospfArea.setAreaId(Ip4Address.valueOf("12.12.12.12")); channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class); OspfMessage message; ddPacket = new DdPacket(); ddPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1")); ddPacket.setOspfVer(2); ddPacket.setAreaId(Ip4Address.valueOf("12.12.12.12")); ddPacket.setRouterId(Ip4Address.valueOf("2.2.2.2")); ddPacket.setIsOpaqueCapable(true); ddPacket.setIsMore(1); ddPacket.setIsInitialize(1); ddPacket.setIsMaster(1); ddPacket.setSequenceNo(123); message = ddPacket; ospfNbrHashMap = new HashMap(); ospfNbr = new OspfNbrImpl(ospfArea, ospfInterface, Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("2.2.2.2"), 2, topologyForDeviceAndLink); ospfNbr.setLastDdPacket(createDdPacket()); ospfNbr.setNeighborId(Ip4Address.valueOf("2.2.2.2")); ospfNbr.setState(OspfNeighborState.EXSTART); ospfNbr.setRouterPriority(0); ospfNbr.setNeighborDr(Ip4Address.valueOf("13.13.13.13")); ospfNbr.setDdSeqNum(123); ospfInterface.addNeighbouringRouter(ospfNbr); channelHandlerContext = null; channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class); ospfInterface.processDdMessage(message, channelHandlerContext); assertThat(ospfInterfaceChannelHandler, is(notNullValue())); }
void processDdMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processDdMessage...!!!"); Channel channel = ctx.getChannel(); DdPacket ddPacket = (DdPacket) ospfMessage; log.debug("Got DD packet from {}", ddPacket.sourceIp()); Ip4Address neighbourId = ddPacket.routerId(); OspfNbr nbr = neighbouringRouter(neighbourId.toString()); if (nbr != null) { log.debug("OspfChannelHandler::processDdMessage:: OSPFNeighborState {}", nbr.getState()); nbr.setIsOpaqueCapable(ddPacket.isOpaqueCapable()); if (ddPacket.imtu() > mtu()) { log.debug("the MTU size is greater than the interface MTU"); return; } if (nbr.getState() == OspfNeighborState.DOWN) { return; } if (nbr.getState() == OspfNeighborState.ATTEMPT) { return; } if (nbr.getState() == OspfNeighborState.TWOWAY) { nbr.adjOk(channel); return; } if (nbr.getState() == OspfNeighborState.INIT) { ((OspfNbrImpl) nbr).twoWayReceived(ddPacket, ctx.getChannel()); } else if (nbr.getState() == OspfNeighborState.EXSTART) { int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); nbr.setOptions(options); if (initialize == OspfUtil.INITIALIZE_SET && more == OspfUtil.MORE_SET && masterOrSlave == OspfUtil.IS_MASTER) { if (ddPacket.getLsaHeaderList().isEmpty()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) > OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { nbr.setIsMaster(OspfUtil.IS_MASTER); ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setDdSeqNum(ddPacket.sequenceNo()); nbr.setOptions(ddPacket.options()); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, true, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } if (initialize == OspfUtil.INITIALIZE_NOTSET && masterOrSlave == OspfUtil.NOT_MASTER) { if (nbr.ddSeqNum() == ddPacket.sequenceNo()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) < OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setOptions(ddPacket.options()); nbr.setDdSeqNum(nbr.ddSeqNum() + 1); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, false, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } } else if (nbr.getState() == OspfNeighborState.EXCHANGE) { log.debug("Neighbor state:: EXCHANGE"); boolean isDuplicateDDPacket = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); if (!isDuplicateDDPacket) { if (nbr.isMaster() != masterOrSlave) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Master/Slave Inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (initialize == 1) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { if (ddPacket.sequenceNo() == nbr.ddSeqNum()) { ((OspfNbrImpl) nbr).processDdPacket(false, ddPacket, ctx.getChannel()); log.debug("Received DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Sequence Number Mismatch"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } else { if (ddPacket.sequenceNo() == (nbr.ddSeqNum() + 1)) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); ((OspfNbrImpl) nbr).processDdPacket(true, ddPacket, ctx.getChannel()); log.debug("Process DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("options inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { return; } else { DdPacket newResPacket = ((OspfNbrImpl) nbr).lastSentDdPacket(); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } else if (nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { int options = ddPacket.options(); if (nbr.options() != options) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (ddPacket.isInitialize() == OspfUtil.INITIALIZE_SET) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } boolean isDuplicate = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); if (nbr.isMaster() != OspfUtil.IS_MASTER) { if (isDuplicate) { log.debug("received a duplicate DD packet"); } } else { if (isDuplicate) { ddPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(((OspfNbrImpl) nbr).lastSentDdPacket()); ctx.getChannel().write(messageToWrite); log.debug("Sending back the duplicate packet "); } } } } }
OspfInterfaceImpl implements OspfInterface { void processDdMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processDdMessage...!!!"); Channel channel = ctx.getChannel(); DdPacket ddPacket = (DdPacket) ospfMessage; log.debug("Got DD packet from {}", ddPacket.sourceIp()); Ip4Address neighbourId = ddPacket.routerId(); OspfNbr nbr = neighbouringRouter(neighbourId.toString()); if (nbr != null) { log.debug("OspfChannelHandler::processDdMessage:: OSPFNeighborState {}", nbr.getState()); nbr.setIsOpaqueCapable(ddPacket.isOpaqueCapable()); if (ddPacket.imtu() > mtu()) { log.debug("the MTU size is greater than the interface MTU"); return; } if (nbr.getState() == OspfNeighborState.DOWN) { return; } if (nbr.getState() == OspfNeighborState.ATTEMPT) { return; } if (nbr.getState() == OspfNeighborState.TWOWAY) { nbr.adjOk(channel); return; } if (nbr.getState() == OspfNeighborState.INIT) { ((OspfNbrImpl) nbr).twoWayReceived(ddPacket, ctx.getChannel()); } else if (nbr.getState() == OspfNeighborState.EXSTART) { int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); nbr.setOptions(options); if (initialize == OspfUtil.INITIALIZE_SET && more == OspfUtil.MORE_SET && masterOrSlave == OspfUtil.IS_MASTER) { if (ddPacket.getLsaHeaderList().isEmpty()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) > OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { nbr.setIsMaster(OspfUtil.IS_MASTER); ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setDdSeqNum(ddPacket.sequenceNo()); nbr.setOptions(ddPacket.options()); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, true, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } if (initialize == OspfUtil.INITIALIZE_NOTSET && masterOrSlave == OspfUtil.NOT_MASTER) { if (nbr.ddSeqNum() == ddPacket.sequenceNo()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) < OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setOptions(ddPacket.options()); nbr.setDdSeqNum(nbr.ddSeqNum() + 1); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, false, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } } else if (nbr.getState() == OspfNeighborState.EXCHANGE) { log.debug("Neighbor state:: EXCHANGE"); boolean isDuplicateDDPacket = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); if (!isDuplicateDDPacket) { if (nbr.isMaster() != masterOrSlave) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Master/Slave Inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (initialize == 1) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { if (ddPacket.sequenceNo() == nbr.ddSeqNum()) { ((OspfNbrImpl) nbr).processDdPacket(false, ddPacket, ctx.getChannel()); log.debug("Received DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Sequence Number Mismatch"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } else { if (ddPacket.sequenceNo() == (nbr.ddSeqNum() + 1)) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); ((OspfNbrImpl) nbr).processDdPacket(true, ddPacket, ctx.getChannel()); log.debug("Process DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("options inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { return; } else { DdPacket newResPacket = ((OspfNbrImpl) nbr).lastSentDdPacket(); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } else if (nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { int options = ddPacket.options(); if (nbr.options() != options) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (ddPacket.isInitialize() == OspfUtil.INITIALIZE_SET) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } boolean isDuplicate = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); if (nbr.isMaster() != OspfUtil.IS_MASTER) { if (isDuplicate) { log.debug("received a duplicate DD packet"); } } else { if (isDuplicate) { ddPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(((OspfNbrImpl) nbr).lastSentDdPacket()); ctx.getChannel().write(messageToWrite); log.debug("Sending back the duplicate packet "); } } } } } }
OspfInterfaceImpl implements OspfInterface { void processDdMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processDdMessage...!!!"); Channel channel = ctx.getChannel(); DdPacket ddPacket = (DdPacket) ospfMessage; log.debug("Got DD packet from {}", ddPacket.sourceIp()); Ip4Address neighbourId = ddPacket.routerId(); OspfNbr nbr = neighbouringRouter(neighbourId.toString()); if (nbr != null) { log.debug("OspfChannelHandler::processDdMessage:: OSPFNeighborState {}", nbr.getState()); nbr.setIsOpaqueCapable(ddPacket.isOpaqueCapable()); if (ddPacket.imtu() > mtu()) { log.debug("the MTU size is greater than the interface MTU"); return; } if (nbr.getState() == OspfNeighborState.DOWN) { return; } if (nbr.getState() == OspfNeighborState.ATTEMPT) { return; } if (nbr.getState() == OspfNeighborState.TWOWAY) { nbr.adjOk(channel); return; } if (nbr.getState() == OspfNeighborState.INIT) { ((OspfNbrImpl) nbr).twoWayReceived(ddPacket, ctx.getChannel()); } else if (nbr.getState() == OspfNeighborState.EXSTART) { int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); nbr.setOptions(options); if (initialize == OspfUtil.INITIALIZE_SET && more == OspfUtil.MORE_SET && masterOrSlave == OspfUtil.IS_MASTER) { if (ddPacket.getLsaHeaderList().isEmpty()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) > OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { nbr.setIsMaster(OspfUtil.IS_MASTER); ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setDdSeqNum(ddPacket.sequenceNo()); nbr.setOptions(ddPacket.options()); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, true, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } if (initialize == OspfUtil.INITIALIZE_NOTSET && masterOrSlave == OspfUtil.NOT_MASTER) { if (nbr.ddSeqNum() == ddPacket.sequenceNo()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) < OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setOptions(ddPacket.options()); nbr.setDdSeqNum(nbr.ddSeqNum() + 1); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, false, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } } else if (nbr.getState() == OspfNeighborState.EXCHANGE) { log.debug("Neighbor state:: EXCHANGE"); boolean isDuplicateDDPacket = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); if (!isDuplicateDDPacket) { if (nbr.isMaster() != masterOrSlave) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Master/Slave Inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (initialize == 1) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { if (ddPacket.sequenceNo() == nbr.ddSeqNum()) { ((OspfNbrImpl) nbr).processDdPacket(false, ddPacket, ctx.getChannel()); log.debug("Received DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Sequence Number Mismatch"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } else { if (ddPacket.sequenceNo() == (nbr.ddSeqNum() + 1)) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); ((OspfNbrImpl) nbr).processDdPacket(true, ddPacket, ctx.getChannel()); log.debug("Process DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("options inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { return; } else { DdPacket newResPacket = ((OspfNbrImpl) nbr).lastSentDdPacket(); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } else if (nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { int options = ddPacket.options(); if (nbr.options() != options) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (ddPacket.isInitialize() == OspfUtil.INITIALIZE_SET) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } boolean isDuplicate = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); if (nbr.isMaster() != OspfUtil.IS_MASTER) { if (isDuplicate) { log.debug("received a duplicate DD packet"); } } else { if (isDuplicate) { ddPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(((OspfNbrImpl) nbr).lastSentDdPacket()); ctx.getChannel().write(messageToWrite); log.debug("Sending back the duplicate packet "); } } } } }  }
OspfInterfaceImpl implements OspfInterface { void processDdMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processDdMessage...!!!"); Channel channel = ctx.getChannel(); DdPacket ddPacket = (DdPacket) ospfMessage; log.debug("Got DD packet from {}", ddPacket.sourceIp()); Ip4Address neighbourId = ddPacket.routerId(); OspfNbr nbr = neighbouringRouter(neighbourId.toString()); if (nbr != null) { log.debug("OspfChannelHandler::processDdMessage:: OSPFNeighborState {}", nbr.getState()); nbr.setIsOpaqueCapable(ddPacket.isOpaqueCapable()); if (ddPacket.imtu() > mtu()) { log.debug("the MTU size is greater than the interface MTU"); return; } if (nbr.getState() == OspfNeighborState.DOWN) { return; } if (nbr.getState() == OspfNeighborState.ATTEMPT) { return; } if (nbr.getState() == OspfNeighborState.TWOWAY) { nbr.adjOk(channel); return; } if (nbr.getState() == OspfNeighborState.INIT) { ((OspfNbrImpl) nbr).twoWayReceived(ddPacket, ctx.getChannel()); } else if (nbr.getState() == OspfNeighborState.EXSTART) { int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); nbr.setOptions(options); if (initialize == OspfUtil.INITIALIZE_SET && more == OspfUtil.MORE_SET && masterOrSlave == OspfUtil.IS_MASTER) { if (ddPacket.getLsaHeaderList().isEmpty()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) > OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { nbr.setIsMaster(OspfUtil.IS_MASTER); ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setDdSeqNum(ddPacket.sequenceNo()); nbr.setOptions(ddPacket.options()); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, true, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } if (initialize == OspfUtil.INITIALIZE_NOTSET && masterOrSlave == OspfUtil.NOT_MASTER) { if (nbr.ddSeqNum() == ddPacket.sequenceNo()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) < OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setOptions(ddPacket.options()); nbr.setDdSeqNum(nbr.ddSeqNum() + 1); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, false, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } } else if (nbr.getState() == OspfNeighborState.EXCHANGE) { log.debug("Neighbor state:: EXCHANGE"); boolean isDuplicateDDPacket = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); if (!isDuplicateDDPacket) { if (nbr.isMaster() != masterOrSlave) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Master/Slave Inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (initialize == 1) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { if (ddPacket.sequenceNo() == nbr.ddSeqNum()) { ((OspfNbrImpl) nbr).processDdPacket(false, ddPacket, ctx.getChannel()); log.debug("Received DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Sequence Number Mismatch"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } else { if (ddPacket.sequenceNo() == (nbr.ddSeqNum() + 1)) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); ((OspfNbrImpl) nbr).processDdPacket(true, ddPacket, ctx.getChannel()); log.debug("Process DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("options inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { return; } else { DdPacket newResPacket = ((OspfNbrImpl) nbr).lastSentDdPacket(); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } else if (nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { int options = ddPacket.options(); if (nbr.options() != options) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (ddPacket.isInitialize() == OspfUtil.INITIALIZE_SET) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } boolean isDuplicate = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); if (nbr.isMaster() != OspfUtil.IS_MASTER) { if (isDuplicate) { log.debug("received a duplicate DD packet"); } } else { if (isDuplicate) { ddPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(((OspfNbrImpl) nbr).lastSentDdPacket()); ctx.getChannel().write(messageToWrite); log.debug("Sending back the duplicate packet "); } } } } }  OspfInterfaceState state(); void setState(OspfInterfaceState ospfInterfaceState); void setChannel(Channel channel); OspfArea ospfArea(); void setController(Controller controller); void setOspfArea(OspfArea ospfArea); String interfaceState(); List<LsaHeader> linkStateHeaders(); Ip4Address ipNetworkMask(); @Override void setIpNetworkMask(Ip4Address ipNetworkMask); void addNeighbouringRouter(OspfNbr ospfNbr); OspfNbr neighbouringRouter(String neighborId); void removeNeighbors(); void removeNeighbor(OspfNbr ospfNeighbor); void addLsaHeaderForDelayAck(LsaHeader lsaHeader); void removeLsaFromNeighborMap(String lsaKey); boolean isNeighborInList(String neighborId); Map<String, OspfNbr> listOfNeighbors(); void setListOfNeighbors(HashMap<String, OspfNbr> listOfNeighbors); int interfaceIndex(); void setInterfaceIndex(int interfaceIndex); Ip4Address ipAddress(); void setIpAddress(Ip4Address ipAddress); int routerPriority(); void setRouterPriority(int routerPriority); int helloIntervalTime(); void setHelloIntervalTime(int helloIntervalTime); int routerDeadIntervalTime(); void setRouterDeadIntervalTime(int routerDeadIntervalTime); int interfaceType(); void setInterfaceType(int interfaceType); int mtu(); void setMtu(int mtu); int reTransmitInterval(); void setReTransmitInterval(int reTransmitInterval); Ip4Address dr(); void setDr(Ip4Address dr); Ip4Address bdr(); void setBdr(Ip4Address bdr); void interfaceUp(); void backupSeen(Channel ch); void waitTimer(Channel ch); void callDrElection(Channel ch); void neighborChange(); void interfaceDown(); void processOspfMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx); boolean compareDdPackets(DdPacket receivedDPacket, DdPacket lastDdPacket); void startHelloTimer(); void stopHelloTimer(); void startWaitTimer(); void stopWaitTimer(); void startDelayedAckTimer(); void stopDelayedAckTimer(); void electRouter(Channel ch); List<OspfEligibleRouter> calculateListOfEligibleRouters(OspfEligibleRouter electedDr); OspfEligibleRouter electBdr(List<OspfEligibleRouter> eligibleRouters); OspfEligibleRouter electDr(List<OspfEligibleRouter> eligibleRouters,
                                      OspfEligibleRouter electedBdr); OspfEligibleRouter selectRouterBasedOnPriority(List<OspfEligibleRouter> routersList); void addDeviceInformation(OspfRouter ospfRouter); void removeDeviceInformation(OspfRouter ospfRouter); void addLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); void removeLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); }
OspfInterfaceImpl implements OspfInterface { void processDdMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx) throws Exception { log.debug("OspfChannelHandler::processDdMessage...!!!"); Channel channel = ctx.getChannel(); DdPacket ddPacket = (DdPacket) ospfMessage; log.debug("Got DD packet from {}", ddPacket.sourceIp()); Ip4Address neighbourId = ddPacket.routerId(); OspfNbr nbr = neighbouringRouter(neighbourId.toString()); if (nbr != null) { log.debug("OspfChannelHandler::processDdMessage:: OSPFNeighborState {}", nbr.getState()); nbr.setIsOpaqueCapable(ddPacket.isOpaqueCapable()); if (ddPacket.imtu() > mtu()) { log.debug("the MTU size is greater than the interface MTU"); return; } if (nbr.getState() == OspfNeighborState.DOWN) { return; } if (nbr.getState() == OspfNeighborState.ATTEMPT) { return; } if (nbr.getState() == OspfNeighborState.TWOWAY) { nbr.adjOk(channel); return; } if (nbr.getState() == OspfNeighborState.INIT) { ((OspfNbrImpl) nbr).twoWayReceived(ddPacket, ctx.getChannel()); } else if (nbr.getState() == OspfNeighborState.EXSTART) { int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); nbr.setOptions(options); if (initialize == OspfUtil.INITIALIZE_SET && more == OspfUtil.MORE_SET && masterOrSlave == OspfUtil.IS_MASTER) { if (ddPacket.getLsaHeaderList().isEmpty()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) > OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { nbr.setIsMaster(OspfUtil.IS_MASTER); ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setDdSeqNum(ddPacket.sequenceNo()); nbr.setOptions(ddPacket.options()); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, true, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } if (initialize == OspfUtil.INITIALIZE_NOTSET && masterOrSlave == OspfUtil.NOT_MASTER) { if (nbr.ddSeqNum() == ddPacket.sequenceNo()) { if (OspfUtil.ipAddressToLong(ddPacket.routerId().toString()) < OspfUtil.ipAddressToLong(ospfArea.routerId().toString())) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); nbr.setOptions(ddPacket.options()); nbr.setDdSeqNum(nbr.ddSeqNum() + 1); ((OspfNbrImpl) nbr).negotiationDone(ddPacket, false, ddPacket.getLsaHeaderList(), ctx.getChannel()); } } } } else if (nbr.getState() == OspfNeighborState.EXCHANGE) { log.debug("Neighbor state:: EXCHANGE"); boolean isDuplicateDDPacket = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); int initialize = ddPacket.isInitialize(); int more = ddPacket.isMore(); int masterOrSlave = ddPacket.isMaster(); int options = ddPacket.options(); if (!isDuplicateDDPacket) { if (nbr.isMaster() != masterOrSlave) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Master/Slave Inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (initialize == 1) { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { if (ddPacket.sequenceNo() == nbr.ddSeqNum()) { ((OspfNbrImpl) nbr).processDdPacket(false, ddPacket, ctx.getChannel()); log.debug("Received DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("Sequence Number Mismatch"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } else { if (ddPacket.sequenceNo() == (nbr.ddSeqNum() + 1)) { ((OspfNbrImpl) nbr).setLastDdPacket(ddPacket); ((OspfNbrImpl) nbr).processDdPacket(true, ddPacket, ctx.getChannel()); log.debug("Process DD Packet"); } else { DdPacket newResPacket = (DdPacket) ((OspfNbrImpl) nbr).seqNumMismatch("options inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } } else { if (masterOrSlave == OspfUtil.NOT_MASTER) { return; } else { DdPacket newResPacket = ((OspfNbrImpl) nbr).lastSentDdPacket(); log.debug("Sending back DDPacket to {}", ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } } } else if (nbr.getState() == OspfNeighborState.LOADING || nbr.getState() == OspfNeighborState.FULL) { int options = ddPacket.options(); if (nbr.options() != options) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } else if (ddPacket.isInitialize() == OspfUtil.INITIALIZE_SET) { OspfMessage newResPacket = ((OspfNbrImpl) nbr).seqNumMismatch("Initialize bit inconsistency"); newResPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(newResPacket); ctx.getChannel().write(messageToWrite); } boolean isDuplicate = compareDdPackets(ddPacket, ((OspfNbrImpl) nbr).lastDdPacket()); if (nbr.isMaster() != OspfUtil.IS_MASTER) { if (isDuplicate) { log.debug("received a duplicate DD packet"); } } else { if (isDuplicate) { ddPacket.setDestinationIp(ddPacket.sourceIp()); byte[] messageToWrite = getMessage(((OspfNbrImpl) nbr).lastSentDdPacket()); ctx.getChannel().write(messageToWrite); log.debug("Sending back the duplicate packet "); } } } } }  OspfInterfaceState state(); void setState(OspfInterfaceState ospfInterfaceState); void setChannel(Channel channel); OspfArea ospfArea(); void setController(Controller controller); void setOspfArea(OspfArea ospfArea); String interfaceState(); List<LsaHeader> linkStateHeaders(); Ip4Address ipNetworkMask(); @Override void setIpNetworkMask(Ip4Address ipNetworkMask); void addNeighbouringRouter(OspfNbr ospfNbr); OspfNbr neighbouringRouter(String neighborId); void removeNeighbors(); void removeNeighbor(OspfNbr ospfNeighbor); void addLsaHeaderForDelayAck(LsaHeader lsaHeader); void removeLsaFromNeighborMap(String lsaKey); boolean isNeighborInList(String neighborId); Map<String, OspfNbr> listOfNeighbors(); void setListOfNeighbors(HashMap<String, OspfNbr> listOfNeighbors); int interfaceIndex(); void setInterfaceIndex(int interfaceIndex); Ip4Address ipAddress(); void setIpAddress(Ip4Address ipAddress); int routerPriority(); void setRouterPriority(int routerPriority); int helloIntervalTime(); void setHelloIntervalTime(int helloIntervalTime); int routerDeadIntervalTime(); void setRouterDeadIntervalTime(int routerDeadIntervalTime); int interfaceType(); void setInterfaceType(int interfaceType); int mtu(); void setMtu(int mtu); int reTransmitInterval(); void setReTransmitInterval(int reTransmitInterval); Ip4Address dr(); void setDr(Ip4Address dr); Ip4Address bdr(); void setBdr(Ip4Address bdr); void interfaceUp(); void backupSeen(Channel ch); void waitTimer(Channel ch); void callDrElection(Channel ch); void neighborChange(); void interfaceDown(); void processOspfMessage(OspfMessage ospfMessage, ChannelHandlerContext ctx); boolean compareDdPackets(DdPacket receivedDPacket, DdPacket lastDdPacket); void startHelloTimer(); void stopHelloTimer(); void startWaitTimer(); void stopWaitTimer(); void startDelayedAckTimer(); void stopDelayedAckTimer(); void electRouter(Channel ch); List<OspfEligibleRouter> calculateListOfEligibleRouters(OspfEligibleRouter electedDr); OspfEligibleRouter electBdr(List<OspfEligibleRouter> eligibleRouters); OspfEligibleRouter electDr(List<OspfEligibleRouter> eligibleRouters,
                                      OspfEligibleRouter electedBdr); OspfEligibleRouter selectRouterBasedOnPriority(List<OspfEligibleRouter> routersList); void addDeviceInformation(OspfRouter ospfRouter); void removeDeviceInformation(OspfRouter ospfRouter); void addLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); void removeLinkInformation(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }