As it is defined (see: http://www.freesoft.org/CIE/Course/Section4/8.htm) the TCP header has an 'Options' field. There are a couple of options already defined (see: www.iana.org/assignments/tcp-parameters/) but I want to come up with my own. (For experimenting/research.)
How can I get Java to write (and then read) some custom data to the options field?
Bonus question: if it cannot be done with Java. what kind of application can do this? (No, I don't really feel like messing with some kernel-level TCP/IP stack implementation, I want to keep it app level.)
This option field is used in the SYN packet when the client establishes a connection with the server. “Kind=2” takes 1 bytes space, “length=4” takes 1-byte space and “MSS” takes 2-byte space. Thus, the total space consumed (length = 4) by the MSS option is 4 bytes.
The TCP Options (MSS, Window Scaling, Selective Acknowledgements, Timestamps, Nop) are located at the end of the TCP Header which is also why they are covered last.
The TCP header padding is used to ensure that the TCP header ends, and data begins, on a 32-bit boundary. The padding is composed of zeros.
Maximum Segment Size(MSS) It should be seen only 3-way handshake in SYN and SYN/ACK packets. MSS option fields is 4 bytes long.
JNetPcap is a library that will allow you to change headers from low level layers including TCP. http://jnetpcap.com/node/29
Here is a quick example:
byte[] pktBytes = FormatUtils.toByteArray("0015c672234c90e6ba92661608004500002d358c4000800600000a000b050a090028c26e270fb8b256e3a2009f785018faf01f550000746573740a");
JMemoryPacket packet = new JMemoryPacket(pktBytes);
packet.scan(Ethernet.ID); //Need to be done before doing any edits
//Editing Ip layer
Ip4 ip = packet.getHeader(new Ip4());
ip.source(new byte[] {2,6,0,0}); //Source Ip 2.6.0.0
ip.destination(new byte[] {1,2,3,4}); //Dest Ip 1.2.3.4
//Editing Tcp layer
Tcp tcp = packet.getHeader(new Tcp());
tcp.destination(5555); //Port destination 5555
if (pcap.sendPacket(packet) != Pcap.OK) {
System.err.println(pcap.getErr());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With