So my task this week was to create customized TCP packets for the NetSerV experiment. I would have had to look for a user-level TCP stack ... or use the popular pcap implementation, which can generate packets on the network.
And since this has to run on Java, we have to use jpcap (there is an older version on Sourceforge, which I did not try.)
I was able to successfully create a program that can send TCP packets, both on Windows and Linux. And was able to compile and run SendTCP.java, which can send packets
To get the TCP packets to send properly in a local network and be received at the remote node, you need to know a few things:
- Make sure to correctly specify source and destination MAC addresses. You can get the source MAC address by doing:
ether.src_mac = device.mac_address;
You will have to manually set the receiver's MAC address, or look at ARP.java to figure how to do it over the network.
- Make sure to correctly specify source and destination IP address. Can be done through InetAddress.getByName() as in the example.
- For unknown TCP ports that don't have any daemons running on them, you may not get a ACK response for a SYN packet, though you will get a TCP RST packet.
- If you send it to a port that has a daemon, you will get a nice ACK packet, which you can handle properly.
Windows ran succesfully, but I learnt a few things for the Linux install since I had to run it on a AMD 64-bit processor:
- There seems to be no version of jpcap download on the jpcap site. So you will have to build it yourself.
- Download the source from the jpcap site.
- Follow instructions for source install here.
- You need libpcap and libpcap-dev installed. libpcap is most likely already installed; if you want libpcap-dev, do "sudo apt-get install libpcap-dev"
- You can install the native library and the JAR by following the instructions in #3.