@Test public void testSerialize() { UDP udp = new UDP(); udp.setSourcePort(0x50); udp.setDestinationPort(0x60); udp.setParent(ipv4); assertArrayEquals(bytePacketUDP4, udp.serialize()); udp.resetChecksum(); udp.setParent(ipv6); assertArrayEquals(bytePacketUDP6, udp.serialize()); }
@Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } this.length = (short) (8 + (payloadData == null ? 0 : payloadData.length)); final byte[] data = new byte[this.length]; final ByteBuffer bb = ByteBuffer.wrap(data); bb.putShort((short) (this.sourcePort & 0xffff)); bb.putShort((short) (this.destinationPort & 0xffff)); bb.putShort(this.length); bb.putShort(this.checksum); if (payloadData != null) { bb.put(payloadData); } if (this.parent != null && this.parent instanceof IPv4) { ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); } if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null) { if (this.parent instanceof IPv4) { final IPv4 ipv4 = (IPv4) this.parent; accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) + (ipv4.getSourceAddress() & 0xffff); accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) + (ipv4.getDestinationAddress() & 0xffff); accumulation += ipv4.getProtocol() & 0xff; accumulation += length & 0xffff; } else if (this.parent instanceof IPv6) { final IPv6 ipv6 = (IPv6) this.parent; final int bbLength = Ip6Address.BYTE_LENGTH * 2 + 2 + 4; final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength); bbChecksum.put(ipv6.getSourceAddress()); bbChecksum.put(ipv6.getDestinationAddress()); bbChecksum.put((byte) 0); bbChecksum.put(ipv6.getNextHeader()); bbChecksum.putInt(length); bbChecksum.rewind(); for (int i = 0; i < bbLength / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } } } for (int i = 0; i < this.length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (this.length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(6, this.checksum); } return data; }
UDP extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } this.length = (short) (8 + (payloadData == null ? 0 : payloadData.length)); final byte[] data = new byte[this.length]; final ByteBuffer bb = ByteBuffer.wrap(data); bb.putShort((short) (this.sourcePort & 0xffff)); bb.putShort((short) (this.destinationPort & 0xffff)); bb.putShort(this.length); bb.putShort(this.checksum); if (payloadData != null) { bb.put(payloadData); } if (this.parent != null && this.parent instanceof IPv4) { ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); } if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null) { if (this.parent instanceof IPv4) { final IPv4 ipv4 = (IPv4) this.parent; accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) + (ipv4.getSourceAddress() & 0xffff); accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) + (ipv4.getDestinationAddress() & 0xffff); accumulation += ipv4.getProtocol() & 0xff; accumulation += length & 0xffff; } else if (this.parent instanceof IPv6) { final IPv6 ipv6 = (IPv6) this.parent; final int bbLength = Ip6Address.BYTE_LENGTH * 2 + 2 + 4; final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength); bbChecksum.put(ipv6.getSourceAddress()); bbChecksum.put(ipv6.getDestinationAddress()); bbChecksum.put((byte) 0); bbChecksum.put(ipv6.getNextHeader()); bbChecksum.putInt(length); bbChecksum.rewind(); for (int i = 0; i < bbLength / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } } } for (int i = 0; i < this.length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (this.length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(6, this.checksum); } return data; } }
UDP extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } this.length = (short) (8 + (payloadData == null ? 0 : payloadData.length)); final byte[] data = new byte[this.length]; final ByteBuffer bb = ByteBuffer.wrap(data); bb.putShort((short) (this.sourcePort & 0xffff)); bb.putShort((short) (this.destinationPort & 0xffff)); bb.putShort(this.length); bb.putShort(this.checksum); if (payloadData != null) { bb.put(payloadData); } if (this.parent != null && this.parent instanceof IPv4) { ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); } if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null) { if (this.parent instanceof IPv4) { final IPv4 ipv4 = (IPv4) this.parent; accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) + (ipv4.getSourceAddress() & 0xffff); accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) + (ipv4.getDestinationAddress() & 0xffff); accumulation += ipv4.getProtocol() & 0xff; accumulation += length & 0xffff; } else if (this.parent instanceof IPv6) { final IPv6 ipv6 = (IPv6) this.parent; final int bbLength = Ip6Address.BYTE_LENGTH * 2 + 2 + 4; final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength); bbChecksum.put(ipv6.getSourceAddress()); bbChecksum.put(ipv6.getDestinationAddress()); bbChecksum.put((byte) 0); bbChecksum.put(ipv6.getNextHeader()); bbChecksum.putInt(length); bbChecksum.rewind(); for (int i = 0; i < bbLength / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } } } for (int i = 0; i < this.length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (this.length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(6, this.checksum); } return data; }  }
UDP extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } this.length = (short) (8 + (payloadData == null ? 0 : payloadData.length)); final byte[] data = new byte[this.length]; final ByteBuffer bb = ByteBuffer.wrap(data); bb.putShort((short) (this.sourcePort & 0xffff)); bb.putShort((short) (this.destinationPort & 0xffff)); bb.putShort(this.length); bb.putShort(this.checksum); if (payloadData != null) { bb.put(payloadData); } if (this.parent != null && this.parent instanceof IPv4) { ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); } if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null) { if (this.parent instanceof IPv4) { final IPv4 ipv4 = (IPv4) this.parent; accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) + (ipv4.getSourceAddress() & 0xffff); accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) + (ipv4.getDestinationAddress() & 0xffff); accumulation += ipv4.getProtocol() & 0xff; accumulation += length & 0xffff; } else if (this.parent instanceof IPv6) { final IPv6 ipv6 = (IPv6) this.parent; final int bbLength = Ip6Address.BYTE_LENGTH * 2 + 2 + 4; final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength); bbChecksum.put(ipv6.getSourceAddress()); bbChecksum.put(ipv6.getDestinationAddress()); bbChecksum.put((byte) 0); bbChecksum.put(ipv6.getNextHeader()); bbChecksum.putInt(length); bbChecksum.rewind(); for (int i = 0; i < bbLength / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } } } for (int i = 0; i < this.length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (this.length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(6, this.checksum); } return data; }  int getSourcePort(); UDP setSourcePort(final int sourcePort); int getDestinationPort(); UDP setDestinationPort(final int destinationPort); short getLength(); short getChecksum(); UDP setChecksum(final short checksum); @Override void resetChecksum(); @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<UDP> deserializer(); @Override String toString(); }
UDP extends BasePacket { @Override public byte[] serialize() { byte[] payloadData = null; if (this.payload != null) { this.payload.setParent(this); payloadData = this.payload.serialize(); } this.length = (short) (8 + (payloadData == null ? 0 : payloadData.length)); final byte[] data = new byte[this.length]; final ByteBuffer bb = ByteBuffer.wrap(data); bb.putShort((short) (this.sourcePort & 0xffff)); bb.putShort((short) (this.destinationPort & 0xffff)); bb.putShort(this.length); bb.putShort(this.checksum); if (payloadData != null) { bb.put(payloadData); } if (this.parent != null && this.parent instanceof IPv4) { ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); } if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null) { if (this.parent instanceof IPv4) { final IPv4 ipv4 = (IPv4) this.parent; accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) + (ipv4.getSourceAddress() & 0xffff); accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) + (ipv4.getDestinationAddress() & 0xffff); accumulation += ipv4.getProtocol() & 0xff; accumulation += length & 0xffff; } else if (this.parent instanceof IPv6) { final IPv6 ipv6 = (IPv6) this.parent; final int bbLength = Ip6Address.BYTE_LENGTH * 2 + 2 + 4; final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength); bbChecksum.put(ipv6.getSourceAddress()); bbChecksum.put(ipv6.getDestinationAddress()); bbChecksum.put((byte) 0); bbChecksum.put(ipv6.getNextHeader()); bbChecksum.putInt(length); bbChecksum.rewind(); for (int i = 0; i < bbLength / 2; ++i) { accumulation += 0xffff & bbChecksum.getShort(); } } } for (int i = 0; i < this.length / 2; ++i) { accumulation += 0xffff & bb.getShort(); } if (this.length % 2 > 0) { accumulation += (bb.get() & 0xff) << 8; } accumulation = (accumulation >> 16 & 0xffff) + (accumulation & 0xffff); this.checksum = (short) (~accumulation & 0xffff); bb.putShort(6, this.checksum); } return data; }  int getSourcePort(); UDP setSourcePort(final int sourcePort); int getDestinationPort(); UDP setDestinationPort(final int destinationPort); short getLength(); short getChecksum(); UDP setChecksum(final short checksum); @Override void resetChecksum(); @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<UDP> deserializer(); @Override String toString(); static final Map<Integer, Deserializer<? extends IPacket>> PORT_DESERIALIZER_MAP; static final int DHCP_SERVER_PORT; static final int DHCP_CLIENT_PORT; }