IPv4 extends BasePacket { @Override public IPacket deserialize(byte[] data, int offset, int length) { ByteBuffer bb = ByteBuffer.wrap(data, offset, length); short sscratch; this.version = bb.get(); this.headerLength = (byte) (this.version & 0xf); this.version = (byte) ((this.version >> 4) & 0xf); this.diffServ = bb.get(); this.totalLength = bb.getShort(); this.identification = bb.getShort(); sscratch = bb.getShort(); this.flags = (byte) ((sscratch >> 13) & 0x7); this.fragmentOffset = (short) (sscratch & 0x1fff); this.ttl = bb.get(); this.protocol = bb.get(); this.checksum = bb.getShort(); this.sourceAddress = bb.getInt(); this.destinationAddress = bb.getInt(); if (this.headerLength > 5) { int optionsLength = (this.headerLength - 5) * 4; this.options = new byte[optionsLength]; bb.get(this.options); } IPacket payload; if (IPv4.protocolClassMap.containsKey(this.protocol)) { Class<? extends IPacket> clazz = IPv4.protocolClassMap.get(this.protocol); try { payload = clazz.newInstance(); } catch (Exception e) { throw new RuntimeException("Error parsing payload for IPv4 packet", e); } } else { payload = new Data(); } this.payload = payload.deserialize(data, bb.position(), bb.limit()-bb.position()); this.payload.setParent(this); if (this.totalLength != length) this.isTruncated = true; else this.isTruncated = false; return this; } IPv4(); byte getVersion(); IPv4 setVersion(byte version); byte getHeaderLength(); byte getDiffServ(); IPv4 setDiffServ(byte diffServ); short getTotalLength(); short getIdentification(); boolean isTruncated(); void setTruncated(boolean isTruncated); IPv4 setIdentification(short identification); byte getFlags(); IPv4 setFlags(byte flags); short getFragmentOffset(); IPv4 setFragmentOffset(short fragmentOffset); byte getTtl(); IPv4 setTtl(byte ttl); byte getProtocol(); IPv4 setProtocol(byte protocol); short getChecksum(); IPv4 setChecksum(short checksum); @Override void resetChecksum(); int getSourceAddress(); IPv4 setSourceAddress(int sourceAddress); IPv4 setSourceAddress(String sourceAddress); int getDestinationAddress(); IPv4 setDestinationAddress(int destinationAddress); IPv4 setDestinationAddress(String destinationAddress); byte[] getOptions(); IPv4 setOptions(byte[] options); byte[] serialize(); @Override IPacket deserialize(byte[] data, int offset, int length); static int toIPv4Address(String ipAddress); static int toIPv4Address(byte[] ipAddress); static String fromIPv4Address(int ipAddress); static String fromIPv4AddressCollection(Collection<Integer> ipAddresses); static byte[] toIPv4AddressBytes(String ipAddress); static byte[] toIPv4AddressBytes(int ipAddress); @Override int hashCode(); @Override boolean equals(Object obj); static final byte PROTOCOL_ICMP; static final byte PROTOCOL_TCP; static final byte PROTOCOL_UDP; static Map<Byte, Class<? extends IPacket>> protocolClassMap; }
@Test public void testDeserialize() { byte[] pktSerialized = new byte[] { 0x45, 0x00, 0x00, 0x2e, 0x41, (byte) 0xbe, 0x40, 0x00, 0x40, 0x06, (byte) 0xd4, (byte) 0xf0, (byte) 0xc0, (byte) 0xa8, 0x02, (byte) 0xdb, (byte) 0xd0, 0x55, (byte) 0x90, 0x42, (byte) 0xd5, 0x48, 0x01, (byte) 0xbb, (byte) 0xe3, 0x50, (byte) 0xb2, 0x2f, (byte) 0xfc, (byte) 0xf8, (byte) 0xa8, 0x2c, 0x50, 0x18, (byte) 0xff, (byte) 0xff, 0x24, 0x3c, 0x00, 0x00, 0x14, 0x03, 0x01, 0x00, 0x01, 0x01 }; IPv4 packet = new IPv4(); packet.deserialize(pktSerialized, 0, pktSerialized.length); byte[] pktSerialized1 = packet.serialize(); assertTrue(Arrays.equals(pktSerialized, pktSerialized1)); }