@Test public void testSerialize() { byte[] expected = new byte[] { 0x45, 0x00, 0x00, 0x14, 0x5e, 0x4e, 0x00, 0x00, 0x3f, 0x06, 0x31, 0x2e, (byte) 0xac, 0x18, 0x4a, (byte) 0xdf, (byte) 0xab, 0x40, 0x4a, 0x30 }; IPv4 packet = new IPv4() .setIdentification((short) 24142) .setTtl((byte) 63) .setProtocol((byte) 0x06) .setSourceAddress("172.24.74.223") .setDestinationAddress("171.64.74.48"); byte[] actual = packet.serialize(); assertTrue(Arrays.equals(expected, actual)); }
@Override public byte[] serialize() { byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); } int optionsLength = 0; if (this.options != null) optionsLength = this.options.length / 4; this.headerLength = (byte) (5 + optionsLength); this.totalLength = (short) (this.headerLength * 4 + ((payloadData == null) ? 0 : payloadData.length)); byte[] data = new byte[this.totalLength]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put((byte) (((this.version & 0xf) << 4) | (this.headerLength & 0xf))); bb.put(this.diffServ); bb.putShort(this.totalLength); bb.putShort(this.identification); bb.putShort((short)(((this.flags & IPV4_FLAGS_MASK) << IPV4_FLAGS_SHIFT) | (this.fragmentOffset & IPV4_OFFSET_MASK))); bb.put(this.ttl); bb.put(this.protocol); bb.putShort(this.checksum); bb.putInt(this.sourceAddress); bb.putInt(this.destinationAddress); if (this.options != null) bb.put(this.options); if (payloadData != null) bb.put(payloadData); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < this.headerLength * 2; ++i) { accumulation += 0xffff & bb.getShort(); } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(10, this.checksum); } return data; }
IPv4 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); } int optionsLength = 0; if (this.options != null) optionsLength = this.options.length / 4; this.headerLength = (byte) (5 + optionsLength); this.totalLength = (short) (this.headerLength * 4 + ((payloadData == null) ? 0 : payloadData.length)); byte[] data = new byte[this.totalLength]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put((byte) (((this.version & 0xf) << 4) | (this.headerLength & 0xf))); bb.put(this.diffServ); bb.putShort(this.totalLength); bb.putShort(this.identification); bb.putShort((short)(((this.flags & IPV4_FLAGS_MASK) << IPV4_FLAGS_SHIFT) | (this.fragmentOffset & IPV4_OFFSET_MASK))); bb.put(this.ttl); bb.put(this.protocol); bb.putShort(this.checksum); bb.putInt(this.sourceAddress); bb.putInt(this.destinationAddress); if (this.options != null) bb.put(this.options); if (payloadData != null) bb.put(payloadData); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < this.headerLength * 2; ++i) { accumulation += 0xffff & bb.getShort(); } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(10, this.checksum); } return data; } }
IPv4 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); } int optionsLength = 0; if (this.options != null) optionsLength = this.options.length / 4; this.headerLength = (byte) (5 + optionsLength); this.totalLength = (short) (this.headerLength * 4 + ((payloadData == null) ? 0 : payloadData.length)); byte[] data = new byte[this.totalLength]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put((byte) (((this.version & 0xf) << 4) | (this.headerLength & 0xf))); bb.put(this.diffServ); bb.putShort(this.totalLength); bb.putShort(this.identification); bb.putShort((short)(((this.flags & IPV4_FLAGS_MASK) << IPV4_FLAGS_SHIFT) | (this.fragmentOffset & IPV4_OFFSET_MASK))); bb.put(this.ttl); bb.put(this.protocol); bb.putShort(this.checksum); bb.putInt(this.sourceAddress); bb.putInt(this.destinationAddress); if (this.options != null) bb.put(this.options); if (payloadData != null) bb.put(payloadData); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < this.headerLength * 2; ++i) { accumulation += 0xffff & bb.getShort(); } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(10, this.checksum); } return data; } IPv4(); }
IPv4 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); } int optionsLength = 0; if (this.options != null) optionsLength = this.options.length / 4; this.headerLength = (byte) (5 + optionsLength); this.totalLength = (short) (this.headerLength * 4 + ((payloadData == null) ? 0 : payloadData.length)); byte[] data = new byte[this.totalLength]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put((byte) (((this.version & 0xf) << 4) | (this.headerLength & 0xf))); bb.put(this.diffServ); bb.putShort(this.totalLength); bb.putShort(this.identification); bb.putShort((short)(((this.flags & IPV4_FLAGS_MASK) << IPV4_FLAGS_SHIFT) | (this.fragmentOffset & IPV4_OFFSET_MASK))); bb.put(this.ttl); bb.put(this.protocol); bb.putShort(this.checksum); bb.putInt(this.sourceAddress); bb.putInt(this.destinationAddress); if (this.options != null) bb.put(this.options); if (payloadData != null) bb.put(payloadData); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < this.headerLength * 2; ++i) { accumulation += 0xffff & bb.getShort(); } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(10, this.checksum); } return data; } 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); boolean isFragment(); void setFragment(boolean isFrag); 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); @Override 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); }
IPv4 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); } int optionsLength = 0; if (this.options != null) optionsLength = this.options.length / 4; this.headerLength = (byte) (5 + optionsLength); this.totalLength = (short) (this.headerLength * 4 + ((payloadData == null) ? 0 : payloadData.length)); byte[] data = new byte[this.totalLength]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put((byte) (((this.version & 0xf) << 4) | (this.headerLength & 0xf))); bb.put(this.diffServ); bb.putShort(this.totalLength); bb.putShort(this.identification); bb.putShort((short)(((this.flags & IPV4_FLAGS_MASK) << IPV4_FLAGS_SHIFT) | (this.fragmentOffset & IPV4_OFFSET_MASK))); bb.put(this.ttl); bb.put(this.protocol); bb.putShort(this.checksum); bb.putInt(this.sourceAddress); bb.putInt(this.destinationAddress); if (this.options != null) bb.put(this.options); if (payloadData != null) bb.put(payloadData); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < this.headerLength * 2; ++i) { accumulation += 0xffff & bb.getShort(); } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(10, this.checksum); } return data; } 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); boolean isFragment(); void setFragment(boolean isFrag); 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); @Override 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; static final byte IPV4_FLAGS_MOREFRAG; static final byte IPV4_FLAGS_DONTFRAG; static final byte IPV4_FLAGS_MASK; static final byte IPV4_FLAGS_SHIFT; static final short IPV4_OFFSET_MASK; }