@Test public void testReadreadLsaHeader() throws Exception { byte[] header = {0, 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58, -100, 0, 48}; channelBuffer = ChannelBuffers.copiedBuffer(header); lsaHeader = OspfUtil.readLsaHeader(channelBuffer); assertThat(lsaHeader, is(notNullValue())); }
public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception { LsaHeader lsaHeader = null; if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) { byte[] byteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(byteArray, 0, OspfUtil.FOUR_BYTES); ChannelBuffer tempBuffer = ChannelBuffers.copiedBuffer(byteArray); int lsType = byteArray[3]; if (lsType == OspfParameters.AREA_LOCAL_OPAQUE_LSA || lsType == OspfParameters.LINK_LOCAL_OPAQUE_LSA || lsType == OspfParameters.AS_OPAQUE_LSA) { OpaqueLsaHeader header = new OpaqueLsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); header.setOpaqueType(channelBuffer.readByte()); header.setOpaqueId(channelBuffer.readUnsignedMedium()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); byte[] opaqueIdBytes = OspfUtil.convertToTwoBytes(header.opaqueId()); header.setLinkStateId(header.opaqueType() + "." + "0" + "." + opaqueIdBytes[0] + "." + opaqueIdBytes[1]); lsaHeader = header; } else { LsaHeader header = new LsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName()); tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); lsaHeader = header; } } return lsaHeader; }
OspfUtil { public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception { LsaHeader lsaHeader = null; if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) { byte[] byteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(byteArray, 0, OspfUtil.FOUR_BYTES); ChannelBuffer tempBuffer = ChannelBuffers.copiedBuffer(byteArray); int lsType = byteArray[3]; if (lsType == OspfParameters.AREA_LOCAL_OPAQUE_LSA || lsType == OspfParameters.LINK_LOCAL_OPAQUE_LSA || lsType == OspfParameters.AS_OPAQUE_LSA) { OpaqueLsaHeader header = new OpaqueLsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); header.setOpaqueType(channelBuffer.readByte()); header.setOpaqueId(channelBuffer.readUnsignedMedium()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); byte[] opaqueIdBytes = OspfUtil.convertToTwoBytes(header.opaqueId()); header.setLinkStateId(header.opaqueType() + "." + "0" + "." + opaqueIdBytes[0] + "." + opaqueIdBytes[1]); lsaHeader = header; } else { LsaHeader header = new LsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName()); tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); lsaHeader = header; } } return lsaHeader; } }
OspfUtil { public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception { LsaHeader lsaHeader = null; if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) { byte[] byteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(byteArray, 0, OspfUtil.FOUR_BYTES); ChannelBuffer tempBuffer = ChannelBuffers.copiedBuffer(byteArray); int lsType = byteArray[3]; if (lsType == OspfParameters.AREA_LOCAL_OPAQUE_LSA || lsType == OspfParameters.LINK_LOCAL_OPAQUE_LSA || lsType == OspfParameters.AS_OPAQUE_LSA) { OpaqueLsaHeader header = new OpaqueLsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); header.setOpaqueType(channelBuffer.readByte()); header.setOpaqueId(channelBuffer.readUnsignedMedium()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); byte[] opaqueIdBytes = OspfUtil.convertToTwoBytes(header.opaqueId()); header.setLinkStateId(header.opaqueType() + "." + "0" + "." + opaqueIdBytes[0] + "." + opaqueIdBytes[1]); lsaHeader = header; } else { LsaHeader header = new LsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName()); tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); lsaHeader = header; } } return lsaHeader; } private  OspfUtil(); }
OspfUtil { public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception { LsaHeader lsaHeader = null; if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) { byte[] byteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(byteArray, 0, OspfUtil.FOUR_BYTES); ChannelBuffer tempBuffer = ChannelBuffers.copiedBuffer(byteArray); int lsType = byteArray[3]; if (lsType == OspfParameters.AREA_LOCAL_OPAQUE_LSA || lsType == OspfParameters.LINK_LOCAL_OPAQUE_LSA || lsType == OspfParameters.AS_OPAQUE_LSA) { OpaqueLsaHeader header = new OpaqueLsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); header.setOpaqueType(channelBuffer.readByte()); header.setOpaqueId(channelBuffer.readUnsignedMedium()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); byte[] opaqueIdBytes = OspfUtil.convertToTwoBytes(header.opaqueId()); header.setLinkStateId(header.opaqueType() + "." + "0" + "." + opaqueIdBytes[0] + "." + opaqueIdBytes[1]); lsaHeader = header; } else { LsaHeader header = new LsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName()); tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); lsaHeader = header; } } return lsaHeader; } private  OspfUtil(); static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, Ip4Address mask); static long ipAddressToLong(String ipAddress); static boolean isOpaqueEnabled(int options); static int byteToInteger(byte[] bytesToConvert); static long byteToLong(byte[] bytesToConvert); static int createRandomNumber(); static LsaHeader readLsaHeader(ChannelBuffer channelBuffer); static byte[] convertToTwoBytes(int numberToConvert); static byte[] convertToThreeBytes(int numberToConvert); static byte[] convertToFourBytes(int numberToConvert); static byte[] convertToFourBytes(long numberToConvert); static byte[] addLengthAndCheckSum(byte[] ospfPacket, int lengthBytePos1, int lengthBytePos2,
                                              int checksumBytePos1, int checksumBytePos2); static byte[] addMetadata(int interfaceIndex, byte[] ospfPacket, int allDroutersValue,
                                     Ip4Address destinationIp); }
OspfUtil { public static LsaHeader readLsaHeader(ChannelBuffer channelBuffer) throws Exception { LsaHeader lsaHeader = null; if (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) { byte[] byteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(byteArray, 0, OspfUtil.FOUR_BYTES); ChannelBuffer tempBuffer = ChannelBuffers.copiedBuffer(byteArray); int lsType = byteArray[3]; if (lsType == OspfParameters.AREA_LOCAL_OPAQUE_LSA || lsType == OspfParameters.LINK_LOCAL_OPAQUE_LSA || lsType == OspfParameters.AS_OPAQUE_LSA) { OpaqueLsaHeader header = new OpaqueLsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); header.setOpaqueType(channelBuffer.readByte()); header.setOpaqueId(channelBuffer.readUnsignedMedium()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); byte[] opaqueIdBytes = OspfUtil.convertToTwoBytes(header.opaqueId()); header.setLinkStateId(header.opaqueType() + "." + "0" + "." + opaqueIdBytes[0] + "." + opaqueIdBytes[1]); lsaHeader = header; } else { LsaHeader header = new LsaHeader(); header.setAge(tempBuffer.readShort()); header.setOptions(tempBuffer.readByte()); header.setLsType(tempBuffer.readByte()); byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setLinkStateId(InetAddress.getByAddress(tempByteArray).getHostName()); tempByteArray = new byte[OspfUtil.FOUR_BYTES]; channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); header.setAdvertisingRouter(Ip4Address.valueOf(tempByteArray)); header.setLsSequenceNo(channelBuffer.readInt()); header.setLsCheckSum(channelBuffer.readUnsignedShort()); header.setLsPacketLen(channelBuffer.readShort()); lsaHeader = header; } } return lsaHeader; } private  OspfUtil(); static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, Ip4Address mask); static long ipAddressToLong(String ipAddress); static boolean isOpaqueEnabled(int options); static int byteToInteger(byte[] bytesToConvert); static long byteToLong(byte[] bytesToConvert); static int createRandomNumber(); static LsaHeader readLsaHeader(ChannelBuffer channelBuffer); static byte[] convertToTwoBytes(int numberToConvert); static byte[] convertToThreeBytes(int numberToConvert); static byte[] convertToFourBytes(int numberToConvert); static byte[] convertToFourBytes(long numberToConvert); static byte[] addLengthAndCheckSum(byte[] ospfPacket, int lengthBytePos1, int lengthBytePos2,
                                              int checksumBytePos1, int checksumBytePos2); static byte[] addMetadata(int interfaceIndex, byte[] ospfPacket, int allDroutersValue,
                                     Ip4Address destinationIp); static final int OSPF_VERSION_2; static final int OSPF_VERSION; static final int PACKET_MINIMUM_LENGTH; static final int METADATA_LEN; static final int MINIMUM_FRAME_LEN; static final int OSPF_HEADER_LENGTH; static final int LSA_HEADER_LENGTH; static final int DD_HEADER_LENGTH; static final int LSREQUEST_LENGTH; static final int OSPFPACKET_LENGTH_POS1; static final int OSPFPACKET_LENGTH_POS2; static final int OSPFPACKET_CHECKSUM_POS1; static final int OSPFPACKET_CHECKSUM_POS2; static final int LSAPACKET_CHECKSUM_POS1; static final int LSAPACKET_CHECKSUM_POS2; static final Ip4Address ALL_SPF_ROUTERS; static final Ip4Address ALL_DROUTERS; static final Ip4Address DEFAULTIP; static final int RETRANSMITINTERVAL; static final int ONLY_ALL_SPF_ROUTERS; static final int JOIN_ALL_DROUTERS; static final int INITIALIZE_SET; static final int INITIALIZE_NOTSET; static final int MORE_SET; static final int MORE_NOTSET; static final int IS_MASTER; static final int NOT_MASTER; static final int NOT_ASSIGNED; static final int FOUR_BYTES; static final int FIVE_BYTES; static final int EIGHT_BYTES; static final int TWELVE_BYTES; static final int EXTERNAL_DESTINATION_LENGTH; static final String SHOST; static final int SPORT; static final int MTU; static final char CONFIG_LENGTH; static final char ROUTER_PRIORITY; static final int HELLO_PACKET_OPTIONS; }