How to write custom data to the TCP packet header options field with Java?
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 c开发者_StackOverflow社区an 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.)
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());
}
精彩评论