Example sending tcp packet
Belippo opened this issue · 1 comments
Hi, where can i find an example that sends tcp packet without Ethernet,ip and tcp value hardcoded in hex-string format as in this example?
`
final String ETHERNET = "0026622f4787 001d60b30184 0800";
final String IPv4 = "4500003ccb5b4000400628e4 c0a8FD05 c0a8FD06";
final String TCP = "e14e00508e50190100000000a00216d08f470000020405b40402080a0021d25a0000000001030307";
final byte[] packetBytes = PcapUtils.parseHexString(ETHERNET + IPv4 + TCP);
List<PcapIf> devices = Pcap.findAllDevs();
try (Pcap pcap = Pcap.create(devices.get(0))) {
pcap.activate();
/* Transmit our packet */
pcap.sendPacket(packetBytes);
}`
Thank u!
Currently, there is no specific API way to send packets except to create them in a buffer from scratch. You can take the HEXSTRING approach to build a skeleton/template of a packet and then just modify the appropriate fields before transmitting the packet in a buffer or just from scratch in a buffer/array and trasnsmit.
Second approach would be overlay protocol headers over the skeleton packet and use the protocol header's setter methods to change the header fields. There are a couple of ways to do that such as "dissect" the skeleton packet which will bind each protocol header at the correct position within the packet buffer or manually bind each header to a specific part of the buffer.
So that is currently, today with jNetPcap and/or jNetPcap Pro.
If you are just looking to send TCP packets, the most efficient way would be to use Java's socket API, but I'm assuming you want to send custom packets, created and controlled by your application.
Future plans
So in the near future, the plans for protocol packet modules is to provide a very good packet creation and protocol stack emulation support.
I have already started work on the system-tables module (currently private repo until a bit more developed) which will provide access to all related networking system tables such as ARP and ROUTING tables, and also lower level control over DNS resolution and other useful tables. All available on all platforms.
Second, the protocol modules will provide APIs for packet creation support to jNetPcap Pro and jNetWorks libraries. These 2 libraries is where most of the actual implementation for packets is defined and is different on a per type of hardware used (Pcap libraries vs. hardware accelerated ones.)
If you can be patient a little bit, you will see work begin on this within next few weeks. I always start a new GIT branch for new features and pull requests which track detailed progress of the work so you can always get access to the code early.
If you have any feedback and feature requests please, feel free to open up a discussion or an issue. The baseline for features, will be a traffic generation tool which will be developed for tutorial and example purposes. All the neccessary features to make a typically trafic gen tool work will be developed from the library API perspective. How much protocol stack emulation to perform, will be upto a discussion.
Current thinking is to provide a good "template" support where you can use packet builders to build packet templates and then use the templates to stamp out real packets to be transmitted. This way you create as many templates as different packet types you have once and then just change its properties before transmission at runtime and loops.