@Test public void testSerialize() { IPacket packet = new IPv4() .setDiffServ((byte) 0x20) .setIdentification((short) 0x1d85) .setFlags((byte) 0x00) .setTtl((byte) 50) .setSourceAddress("74.125.45.109") .setDestinationAddress("192.168.1.111") .setPayload(new TCP() .setSourcePort(993) .setDestinationPort(49202) .setSequence(0xe3adee88) .setAcknowledge(0xb7dad824) .setDataOffset((byte) 8) .setFlags((short) 0x10) .setWindowSize((short) 267) .setOptions(new byte[] {0x01, 0x01, 0x08, 0x0a, 0x20, (byte) 0x9a, 0x41, 0x04, 0x07, 0x76, 0x53, 0x1f}) .setPayload(null) ); byte[] actual = packet.serialize(); assertTrue(Arrays.equals(pktSerialized, actual)); }
public byte[] serialize() { int length; if (dataOffset == 0) dataOffset = 5; length = dataOffset << 2; 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.putShort((short)this.sourcePort.getPort()); bb.putShort((short)this.destinationPort.getPort()); bb.putInt(this.sequence); bb.putInt(this.acknowledge); bb.putShort((short) (this.flags | (dataOffset << 12))); bb.putShort(this.windowSize); bb.putShort(this.checksum); bb.putShort(this.urgentPointer); if (dataOffset > 5) { int padding; bb.put(options); padding = (dataOffset << 2) - 20 - options.length; 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(IpProtocol.TCP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null && this.parent instanceof IPv4) { IPv4 ipv4 = (IPv4) this.parent; accumulation += ((ipv4.getSourceAddress().getInt() >> 16) & 0xffff) + (ipv4.getSourceAddress().getInt() & 0xffff); accumulation += ((ipv4.getDestinationAddress().getInt() >> 16) & 0xffff) + (ipv4.getDestinationAddress().getInt() & 0xffff); accumulation += ipv4.getProtocol().getIpProtocolNumber() & 0xff; accumulation += length & 0xffff; } 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(16, this.checksum); } return data; }
TCP extends BasePacket { public byte[] serialize() { int length; if (dataOffset == 0) dataOffset = 5; length = dataOffset << 2; 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.putShort((short)this.sourcePort.getPort()); bb.putShort((short)this.destinationPort.getPort()); bb.putInt(this.sequence); bb.putInt(this.acknowledge); bb.putShort((short) (this.flags | (dataOffset << 12))); bb.putShort(this.windowSize); bb.putShort(this.checksum); bb.putShort(this.urgentPointer); if (dataOffset > 5) { int padding; bb.put(options); padding = (dataOffset << 2) - 20 - options.length; 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(IpProtocol.TCP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null && this.parent instanceof IPv4) { IPv4 ipv4 = (IPv4) this.parent; accumulation += ((ipv4.getSourceAddress().getInt() >> 16) & 0xffff) + (ipv4.getSourceAddress().getInt() & 0xffff); accumulation += ((ipv4.getDestinationAddress().getInt() >> 16) & 0xffff) + (ipv4.getDestinationAddress().getInt() & 0xffff); accumulation += ipv4.getProtocol().getIpProtocolNumber() & 0xff; accumulation += length & 0xffff; } 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(16, this.checksum); } return data; } }
TCP extends BasePacket { public byte[] serialize() { int length; if (dataOffset == 0) dataOffset = 5; length = dataOffset << 2; 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.putShort((short)this.sourcePort.getPort()); bb.putShort((short)this.destinationPort.getPort()); bb.putInt(this.sequence); bb.putInt(this.acknowledge); bb.putShort((short) (this.flags | (dataOffset << 12))); bb.putShort(this.windowSize); bb.putShort(this.checksum); bb.putShort(this.urgentPointer); if (dataOffset > 5) { int padding; bb.put(options); padding = (dataOffset << 2) - 20 - options.length; 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(IpProtocol.TCP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null && this.parent instanceof IPv4) { IPv4 ipv4 = (IPv4) this.parent; accumulation += ((ipv4.getSourceAddress().getInt() >> 16) & 0xffff) + (ipv4.getSourceAddress().getInt() & 0xffff); accumulation += ((ipv4.getDestinationAddress().getInt() >> 16) & 0xffff) + (ipv4.getDestinationAddress().getInt() & 0xffff); accumulation += ipv4.getProtocol().getIpProtocolNumber() & 0xff; accumulation += length & 0xffff; } 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(16, this.checksum); } return data; }  }
TCP extends BasePacket { public byte[] serialize() { int length; if (dataOffset == 0) dataOffset = 5; length = dataOffset << 2; 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.putShort((short)this.sourcePort.getPort()); bb.putShort((short)this.destinationPort.getPort()); bb.putInt(this.sequence); bb.putInt(this.acknowledge); bb.putShort((short) (this.flags | (dataOffset << 12))); bb.putShort(this.windowSize); bb.putShort(this.checksum); bb.putShort(this.urgentPointer); if (dataOffset > 5) { int padding; bb.put(options); padding = (dataOffset << 2) - 20 - options.length; 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(IpProtocol.TCP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null && this.parent instanceof IPv4) { IPv4 ipv4 = (IPv4) this.parent; accumulation += ((ipv4.getSourceAddress().getInt() >> 16) & 0xffff) + (ipv4.getSourceAddress().getInt() & 0xffff); accumulation += ((ipv4.getDestinationAddress().getInt() >> 16) & 0xffff) + (ipv4.getDestinationAddress().getInt() & 0xffff); accumulation += ipv4.getProtocol().getIpProtocolNumber() & 0xff; accumulation += length & 0xffff; } 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(16, this.checksum); } return data; }  TransportPort getSourcePort(); TCP setSourcePort(TransportPort sourcePort); TCP setSourcePort(int sourcePort); TransportPort getDestinationPort(); TCP setDestinationPort(TransportPort destinationPort); TCP setDestinationPort(int destinationPort); short getChecksum(); int getSequence(); TCP setSequence(int seq); int getAcknowledge(); TCP setAcknowledge(int ack); byte getDataOffset(); TCP setDataOffset(byte offset); short getFlags(); TCP setFlags(short flags); short getWindowSize(); TCP setWindowSize(short windowSize); short getTcpChecksum(); TCP setTcpChecksum(short checksum); @Override void resetChecksum(); short getUrgentPointer(short urgentPointer); TCP setUrgentPointer(short urgentPointer); byte[] getOptions(); TCP setOptions(byte[] options); TCP setChecksum(short checksum); byte[] serialize(); @Override int hashCode(); @Override boolean equals(Object obj); @Override IPacket deserialize(byte[] data, int offset, int length); }
TCP extends BasePacket { public byte[] serialize() { int length; if (dataOffset == 0) dataOffset = 5; length = dataOffset << 2; 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.putShort((short)this.sourcePort.getPort()); bb.putShort((short)this.destinationPort.getPort()); bb.putInt(this.sequence); bb.putInt(this.acknowledge); bb.putShort((short) (this.flags | (dataOffset << 12))); bb.putShort(this.windowSize); bb.putShort(this.checksum); bb.putShort(this.urgentPointer); if (dataOffset > 5) { int padding; bb.put(options); padding = (dataOffset << 2) - 20 - options.length; 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(IpProtocol.TCP); if (this.checksum == 0) { bb.rewind(); int accumulation = 0; if (this.parent != null && this.parent instanceof IPv4) { IPv4 ipv4 = (IPv4) this.parent; accumulation += ((ipv4.getSourceAddress().getInt() >> 16) & 0xffff) + (ipv4.getSourceAddress().getInt() & 0xffff); accumulation += ((ipv4.getDestinationAddress().getInt() >> 16) & 0xffff) + (ipv4.getDestinationAddress().getInt() & 0xffff); accumulation += ipv4.getProtocol().getIpProtocolNumber() & 0xff; accumulation += length & 0xffff; } 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(16, this.checksum); } return data; }  TransportPort getSourcePort(); TCP setSourcePort(TransportPort sourcePort); TCP setSourcePort(int sourcePort); TransportPort getDestinationPort(); TCP setDestinationPort(TransportPort destinationPort); TCP setDestinationPort(int destinationPort); short getChecksum(); int getSequence(); TCP setSequence(int seq); int getAcknowledge(); TCP setAcknowledge(int ack); byte getDataOffset(); TCP setDataOffset(byte offset); short getFlags(); TCP setFlags(short flags); short getWindowSize(); TCP setWindowSize(short windowSize); short getTcpChecksum(); TCP setTcpChecksum(short checksum); @Override void resetChecksum(); short getUrgentPointer(short urgentPointer); TCP setUrgentPointer(short urgentPointer); byte[] getOptions(); TCP setOptions(byte[] options); TCP setChecksum(short checksum); byte[] serialize(); @Override int hashCode(); @Override boolean equals(Object obj); @Override IPacket deserialize(byte[] data, int offset, int length);  }