@Test public void testSerialize() { IPacket packet = new IPv4() .setIdentification((short) 0) .setFlags((byte) 0x02) .setTtl((byte) 64) .setSourceAddress("192.168.10.231") .setDestinationAddress("192.168.10.219") .setPayload(new ICMP() .setIcmpType((byte) 8) .setIcmpCode((byte) 0) .setPayload(new Data(new byte[] {0x76, (byte) 0xf2, 0x0, 0x2, 0x1, 0x1, 0x1})) ); byte[] actual = packet.serialize(); assertTrue(Arrays.equals(pktSerialized, actual)); }
@Override public byte[] serialize() { short padding = 0; if (paddingMap.containsKey(this.icmpType)) padding = paddingMap.get(this.icmpType); int length = 4 + padding; byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); length += payloadData.length; } byte[] data = new byte[length]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put(this.icmpType); bb.put(this.icmpCode); bb.putShort(this.checksum); for (int i = 0; i < padding; i++) bb.put((byte) 0); if (payloadData != null) bb.put(payloadData); if (this.parent != null && this.parent instanceof IPv4) ((IPv4)this.parent).setProtocol(IPv4.PROTOCOL_ICMP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(2, this.checksum); } return data; }
ICMP extends BasePacket { @Override public byte[] serialize() { short padding = 0; if (paddingMap.containsKey(this.icmpType)) padding = paddingMap.get(this.icmpType); int length = 4 + padding; byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); length += payloadData.length; } byte[] data = new byte[length]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put(this.icmpType); bb.put(this.icmpCode); bb.putShort(this.checksum); for (int i = 0; i < padding; i++) bb.put((byte) 0); if (payloadData != null) bb.put(payloadData); if (this.parent != null && this.parent instanceof IPv4) ((IPv4)this.parent).setProtocol(IPv4.PROTOCOL_ICMP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(2, this.checksum); } return data; } }
ICMP extends BasePacket { @Override public byte[] serialize() { short padding = 0; if (paddingMap.containsKey(this.icmpType)) padding = paddingMap.get(this.icmpType); int length = 4 + padding; byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); length += payloadData.length; } byte[] data = new byte[length]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put(this.icmpType); bb.put(this.icmpCode); bb.putShort(this.checksum); for (int i = 0; i < padding; i++) bb.put((byte) 0); if (payloadData != null) bb.put(payloadData); if (this.parent != null && this.parent instanceof IPv4) ((IPv4)this.parent).setProtocol(IPv4.PROTOCOL_ICMP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(2, this.checksum); } return data; }  }
ICMP extends BasePacket { @Override public byte[] serialize() { short padding = 0; if (paddingMap.containsKey(this.icmpType)) padding = paddingMap.get(this.icmpType); int length = 4 + padding; byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); length += payloadData.length; } byte[] data = new byte[length]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put(this.icmpType); bb.put(this.icmpCode); bb.putShort(this.checksum); for (int i = 0; i < padding; i++) bb.put((byte) 0); if (payloadData != null) bb.put(payloadData); if (this.parent != null && this.parent instanceof IPv4) ((IPv4)this.parent).setProtocol(IPv4.PROTOCOL_ICMP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(2, this.checksum); } return data; }  byte getIcmpType(); ICMP setIcmpType(byte icmpType); byte getIcmpCode(); ICMP setIcmpCode(byte icmpCode); short getChecksum(); ICMP setChecksum(short checksum); @Override byte[] serialize(); @Override int hashCode(); @Override boolean equals(Object obj); @Override IPacket deserialize(byte[] data, int offset, int length); }
ICMP extends BasePacket { @Override public byte[] serialize() { short padding = 0; if (paddingMap.containsKey(this.icmpType)) padding = paddingMap.get(this.icmpType); int length = 4 + padding; byte[] payloadData = null; if (payload != null) { payload.setParent(this); payloadData = payload.serialize(); length += payloadData.length; } byte[] data = new byte[length]; ByteBuffer bb = ByteBuffer.wrap(data); bb.put(this.icmpType); bb.put(this.icmpCode); bb.putShort(this.checksum); for (int i = 0; i < padding; i++) bb.put((byte) 0); if (payloadData != null) bb.put(payloadData); if (this.parent != null && this.parent instanceof IPv4) ((IPv4)this.parent).setProtocol(IPv4.PROTOCOL_ICMP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; for (int i = 0; i < length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = ((accumulation >> 16) & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(2, this.checksum); } return data; }  byte getIcmpType(); ICMP setIcmpType(byte icmpType); byte getIcmpCode(); ICMP setIcmpCode(byte icmpCode); short getChecksum(); ICMP setChecksum(short checksum); @Override byte[] serialize(); @Override int hashCode(); @Override boolean equals(Object obj); @Override IPacket deserialize(byte[] data, int offset, int length); static final Map<Byte, Short> paddingMap; static final byte ECHO_REPLY; static final byte ECHO_REQUEST; static final byte TIME_EXCEEDED; static final byte DESTINATION_UNREACHABLE; static final byte CODE_PORT_UNREACHABLE; }