This example demonstrates how you can create a new packet from an in-memory buffer, modify few fields within various headers and recalculate necessary checksums for the updated values.
Download Source from SVN:
JPacket packet =
new JMemoryPacket(JProtocol.ETHERNET_ID,
" 001801bf 6adc0025 4bb7afec 08004500 "
+ " 0041a983 40004006 d69ac0a8 00342f8c "
+ " ca30c3ef 008f2e80 11f52ea8 4b578018 "
+ " ffffa6ea 00000101 080a152e ef03002a "
+ " 2c943538 322e3430 204e4f4f 500d0a");
Ip4 ip = packet.getHeader(new Ip4());
Tcp tcp = packet.getHeader(new Tcp());
tcp.destination(80);
ip.checksum(ip.calculateChecksum());
tcp.checksum(tcp.calculateChecksum());
packet.scan(Ethernet.ID);
System.out.println(packet);
The packet template is created based upon a different packet that was previously captured. We supply the packet contents as a string hexdump. The hexdump is parsed and turned into a byte[] which servers as the basis for the packet. JMemoryPacket allocates storage for the new packet and scans it.
At this point we can peer our protocol headers which allow us, using their convenient getters and setter methods, to manipulate their contents more precisely.
After our modifications, we scan the packet one more time to make sure that any structural changes we made, which we didn't in our simple example, are recalculated and the packet state appropriately updated.
Lastly we print out our newly modified packet:
Frame: Frame: number = 1 Frame: timestamp = 2010-08-23 10:39:23.803 Frame: wire length = 79 bytes Frame: captured length = 79 bytes Frame: Eth: ******* Ethernet - "Ethernet" - offset=0 (0x0) length=14 Eth: Eth: destination = 0:18:1:bf:6a:dc Eth: .... ..0. .... .... = [0] LG bit Eth: .... ...0 .... .... = [0] IG bit Eth: source = 0:25:4b:b7:af:ec Eth: .... ..0. .... .... = [0] LG bit Eth: .... ...0 .... .... = [0] IG bit Eth: type = 0x800 (2048) [ip version 4] Eth: Ip: ******* Ip4 - "ip version 4" - offset=14 (0xE) length=20 protocol suite=NETWORK Ip: Ip: version = 4 Ip: hlen = 5 [5 * 4 = 20 bytes, No Ip Options] Ip: diffserv = 0x0 (0) Ip: 0000 00.. = [0] code point: not set Ip: .... ..0. = [0] ECN bit: not set Ip: .... ...0 = [0] ECE bit: not set Ip: length = 65 Ip: id = 0xA983 (43395) Ip: flags = 0x2 (2) Ip: 0.. = [0] reserved Ip: .1. = [1] DF: do not fragment: set Ip: ..0 = [0] MF: more fragments: not set Ip: offset = 0 Ip: ttl = 64 [time to live] Ip: type = 6 [next: Transmission Control] Ip: checksum = 0xD69A (54938) [correct] Ip: source = 192.168.0.52 Ip: destination = 47.140.202.48 Ip: Tcp: ******* Tcp offset=34 (0x22) length=32 Tcp: Tcp: source = 50159 Tcp: destination = 80 Tcp: seq = 0x2E8011F5 (780145141) Tcp: ack = 0x2EA84B57 (782781271) Tcp: hlen = 8 Tcp: reserved = 0 Tcp: flags = 0x18 (24) Tcp: 0... .... = [0] cwr: reduced (cwr) Tcp: .0.. .... = [0] ece: ECN echo flag Tcp: ..0. .... = [0] ack: urgent, out-of-band data Tcp: ...1 .... = [1] ack: acknowledgment Tcp: .... 1... = [1] ack: push current segment of data Tcp: .... .0.. = [0] ack: reset connection Tcp: .... ..0. = [0] ack: synchronize connection, startup Tcp: .... ...0 = [0] fin: closing down connection Tcp: window = 65535 Tcp: checksum = 0xA729 (42793) [correct] Tcp: urgent = 0 Tcp: Data: ******* Payload offset=66 (0x42) length=13 Data: 0042: 35 38 32 2e 34 30 20 4e 4f 4f 50 0d 0a 582.40 NOOP..