@Test public void testSerialize() { ICMP6 icmp6 = new ICMP6(); icmp6.setIcmpType(ICMP6.ECHO_REQUEST); icmp6.setIcmpCode((byte) 0); icmp6.setParent(ipv6); assertArrayEquals(bytePacket, icmp6.serialize()); }
@Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } int payloadLength = 0; if (payloadData != null) { payloadLength = payloadData.length; } final byte[] data = new byte[HEADER_LENGTH + payloadLength]; final ByteBuffer bbData = ByteBuffer.wrap(data); final byte[] checksumData = new byte[IPv6.FIXED_HEADER_LENGTH + HEADER_LENGTH + payloadLength]; final ByteBuffer bbChecksum = ByteBuffer.wrap(checksumData); IPv6 ipv6Parent = null; for (IPacket p = this.parent; p != null; p = p.getParent()) { if (p instanceof IPv6) { ipv6Parent = (IPv6) p; break; } } if (ipv6Parent != null) { bbChecksum.put(ipv6Parent.getSourceAddress()); bbChecksum.put(ipv6Parent.getDestinationAddress()); } else { bbChecksum.put(ZERO_ADDRESS); bbChecksum.put(ZERO_ADDRESS); } bbChecksum.putInt(HEADER_LENGTH + payloadLength); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put(IPv6.PROTOCOL_ICMP6); bbChecksum.put(this.icmpType); bbChecksum.put(this.icmpCode); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbData.put(this.icmpType); bbData.put(this.icmpCode); bbData.putShort(this.checksum); if (payloadData != null) { bbData.put(payloadData); bbChecksum.put(payloadData); } if (this.parent != null) { if (this.parent instanceof IPv6) { ((IPv6) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } else if (this.parent instanceof IExtensionHeader) { ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } } if (this.checksum == 0) { bbData.rewind(); bbChecksum.rewind(); int accumulation = 0; for (int i = 0; i < checksumData.length / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } if (checksumData.length % 2 > 0) { accumulation += (bbChecksum.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bbData.putShort(2, this.checksum); } return data; }
ICMP6 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } int payloadLength = 0; if (payloadData != null) { payloadLength = payloadData.length; } final byte[] data = new byte[HEADER_LENGTH + payloadLength]; final ByteBuffer bbData = ByteBuffer.wrap(data); final byte[] checksumData = new byte[IPv6.FIXED_HEADER_LENGTH + HEADER_LENGTH + payloadLength]; final ByteBuffer bbChecksum = ByteBuffer.wrap(checksumData); IPv6 ipv6Parent = null; for (IPacket p = this.parent; p != null; p = p.getParent()) { if (p instanceof IPv6) { ipv6Parent = (IPv6) p; break; } } if (ipv6Parent != null) { bbChecksum.put(ipv6Parent.getSourceAddress()); bbChecksum.put(ipv6Parent.getDestinationAddress()); } else { bbChecksum.put(ZERO_ADDRESS); bbChecksum.put(ZERO_ADDRESS); } bbChecksum.putInt(HEADER_LENGTH + payloadLength); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put(IPv6.PROTOCOL_ICMP6); bbChecksum.put(this.icmpType); bbChecksum.put(this.icmpCode); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbData.put(this.icmpType); bbData.put(this.icmpCode); bbData.putShort(this.checksum); if (payloadData != null) { bbData.put(payloadData); bbChecksum.put(payloadData); } if (this.parent != null) { if (this.parent instanceof IPv6) { ((IPv6) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } else if (this.parent instanceof IExtensionHeader) { ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } } if (this.checksum == 0) { bbData.rewind(); bbChecksum.rewind(); int accumulation = 0; for (int i = 0; i < checksumData.length / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } if (checksumData.length % 2 > 0) { accumulation += (bbChecksum.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bbData.putShort(2, this.checksum); } return data; } }
ICMP6 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } int payloadLength = 0; if (payloadData != null) { payloadLength = payloadData.length; } final byte[] data = new byte[HEADER_LENGTH + payloadLength]; final ByteBuffer bbData = ByteBuffer.wrap(data); final byte[] checksumData = new byte[IPv6.FIXED_HEADER_LENGTH + HEADER_LENGTH + payloadLength]; final ByteBuffer bbChecksum = ByteBuffer.wrap(checksumData); IPv6 ipv6Parent = null; for (IPacket p = this.parent; p != null; p = p.getParent()) { if (p instanceof IPv6) { ipv6Parent = (IPv6) p; break; } } if (ipv6Parent != null) { bbChecksum.put(ipv6Parent.getSourceAddress()); bbChecksum.put(ipv6Parent.getDestinationAddress()); } else { bbChecksum.put(ZERO_ADDRESS); bbChecksum.put(ZERO_ADDRESS); } bbChecksum.putInt(HEADER_LENGTH + payloadLength); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put(IPv6.PROTOCOL_ICMP6); bbChecksum.put(this.icmpType); bbChecksum.put(this.icmpCode); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbData.put(this.icmpType); bbData.put(this.icmpCode); bbData.putShort(this.checksum); if (payloadData != null) { bbData.put(payloadData); bbChecksum.put(payloadData); } if (this.parent != null) { if (this.parent instanceof IPv6) { ((IPv6) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } else if (this.parent instanceof IExtensionHeader) { ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } } if (this.checksum == 0) { bbData.rewind(); bbChecksum.rewind(); int accumulation = 0; for (int i = 0; i < checksumData.length / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } if (checksumData.length % 2 > 0) { accumulation += (bbChecksum.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bbData.putShort(2, this.checksum); } return data; }  }
ICMP6 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } int payloadLength = 0; if (payloadData != null) { payloadLength = payloadData.length; } final byte[] data = new byte[HEADER_LENGTH + payloadLength]; final ByteBuffer bbData = ByteBuffer.wrap(data); final byte[] checksumData = new byte[IPv6.FIXED_HEADER_LENGTH + HEADER_LENGTH + payloadLength]; final ByteBuffer bbChecksum = ByteBuffer.wrap(checksumData); IPv6 ipv6Parent = null; for (IPacket p = this.parent; p != null; p = p.getParent()) { if (p instanceof IPv6) { ipv6Parent = (IPv6) p; break; } } if (ipv6Parent != null) { bbChecksum.put(ipv6Parent.getSourceAddress()); bbChecksum.put(ipv6Parent.getDestinationAddress()); } else { bbChecksum.put(ZERO_ADDRESS); bbChecksum.put(ZERO_ADDRESS); } bbChecksum.putInt(HEADER_LENGTH + payloadLength); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put(IPv6.PROTOCOL_ICMP6); bbChecksum.put(this.icmpType); bbChecksum.put(this.icmpCode); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbData.put(this.icmpType); bbData.put(this.icmpCode); bbData.putShort(this.checksum); if (payloadData != null) { bbData.put(payloadData); bbChecksum.put(payloadData); } if (this.parent != null) { if (this.parent instanceof IPv6) { ((IPv6) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } else if (this.parent instanceof IExtensionHeader) { ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } } if (this.checksum == 0) { bbData.rewind(); bbChecksum.rewind(); int accumulation = 0; for (int i = 0; i < checksumData.length / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } if (checksumData.length % 2 > 0) { accumulation += (bbChecksum.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bbData.putShort(2, this.checksum); } return data; }  byte getIcmpType(); ICMP6 setIcmpType(final byte icmpType); byte getIcmpCode(); ICMP6 setIcmpCode(final byte icmpCode); short getChecksum(); ICMP6 setChecksum(final short checksum); @Override byte[] serialize(); @Override IPacket deserialize(final byte[] data, final int offset,
                               final int length); @Override int hashCode(); @Override boolean equals(final Object obj); static Deserializer<ICMP6> deserializer(); @Override String toString(); }
ICMP6 extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } int payloadLength = 0; if (payloadData != null) { payloadLength = payloadData.length; } final byte[] data = new byte[HEADER_LENGTH + payloadLength]; final ByteBuffer bbData = ByteBuffer.wrap(data); final byte[] checksumData = new byte[IPv6.FIXED_HEADER_LENGTH + HEADER_LENGTH + payloadLength]; final ByteBuffer bbChecksum = ByteBuffer.wrap(checksumData); IPv6 ipv6Parent = null; for (IPacket p = this.parent; p != null; p = p.getParent()) { if (p instanceof IPv6) { ipv6Parent = (IPv6) p; break; } } if (ipv6Parent != null) { bbChecksum.put(ipv6Parent.getSourceAddress()); bbChecksum.put(ipv6Parent.getDestinationAddress()); } else { bbChecksum.put(ZERO_ADDRESS); bbChecksum.put(ZERO_ADDRESS); } bbChecksum.putInt(HEADER_LENGTH + payloadLength); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbChecksum.put(IPv6.PROTOCOL_ICMP6); bbChecksum.put(this.icmpType); bbChecksum.put(this.icmpCode); bbChecksum.put((byte) 0); bbChecksum.put((byte) 0); bbData.put(this.icmpType); bbData.put(this.icmpCode); bbData.putShort(this.checksum); if (payloadData != null) { bbData.put(payloadData); bbChecksum.put(payloadData); } if (this.parent != null) { if (this.parent instanceof IPv6) { ((IPv6) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } else if (this.parent instanceof IExtensionHeader) { ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ICMP6); } } if (this.checksum == 0) { bbData.rewind(); bbChecksum.rewind(); int accumulation = 0; for (int i = 0; i < checksumData.length / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } if (checksumData.length % 2 > 0) { accumulation += (bbChecksum.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bbData.putShort(2, this.checksum); } return data; }  byte getIcmpType(); ICMP6 setIcmpType(final byte icmpType); byte getIcmpCode(); ICMP6 setIcmpCode(final byte icmpCode); short getChecksum(); ICMP6 setChecksum(final short checksum); @Override byte[] serialize(); @Override IPacket deserialize(final byte[] data, final int offset,
                               final int length); @Override int hashCode(); @Override boolean equals(final Object obj); static Deserializer<ICMP6> deserializer(); @Override String toString(); static final byte HEADER_LENGTH; static final byte DEST_UNREACH; static final byte PKT_TOO_BIG; static final byte TIME_EXCEED; static final byte PARAM_ERR; static final byte ECHO_REQUEST; static final byte ECHO_REPLY; static final byte MCAST_QUERY; static final byte MCAST_REPORT; static final byte MCAST_DONE; static final byte ROUTER_SOLICITATION; static final byte ROUTER_ADVERTISEMENT; static final byte NEIGHBOR_SOLICITATION; static final byte NEIGHBOR_ADVERTISEMENT; static final byte REDIRECT; static final byte NO_ROUTE; static final byte COMM_PROHIBIT; static final byte BEYOND_SCOPE; static final byte ADDR_UNREACH; static final byte PORT_UNREACH; static final byte FAIL_POLICY; static final byte REJECT_ROUTE; static final byte SRC_ROUTING_HEADER_ERR; static final byte HOP_LIMIT_EXCEED; static final byte DEFRAG_TIME_EXCEED; static final byte HDR_FIELD_ERR; static final byte NEXT_HEADER_ERR; static final byte IPV6_OPT_ERR; static final Map<Byte, Deserializer<? extends IPacket>> TYPE_DESERIALIZER_MAP; }