How to write custom data to the TCP packet header options field with Java?

你。 提交于 2019-11-27 05:41:07

问题


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 very 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.)

Thanks!


回答1:


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());
}


来源:https://stackoverflow.com/questions/2653262/how-to-write-custom-data-to-the-tcp-packet-header-options-field-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!