Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packet Encapsulation in Java

I have been given an assignment for my data communications class where I am required to encapsulate a IP packet over UDP. I then need to encapsulate this manually made packet into an actual UDP packet and send it over IP. This will yield the following protocol stack (blue denotes the headers I will create manually):

enter image description here

Is there an easy way to do this in Java? I understand in C I could use the struct defined for UDP and IP headers in the Linux kernel libraries, manually populate all the fields, and concatenate the information into the packet. However, in Java I will not have access to Linux networking header files. I am also trying to do this without the use of external APIs.

For clarification I plan to develop the application Linux using Eclipse.

like image 884
Adam Jakiela Avatar asked Nov 28 '25 16:11

Adam Jakiela


1 Answers

Actually, you may try to access Linux kernel libraries through Java Native Access.

like this: (http://jnaexamples.blogspot.tw/2012/03/java-native-access-is-easy-way-to.html)

You can wrap the TCP packet structure in native c code, and then send it from java socket.

Meanwhile, the jnetpcap library may be your coding reference if you want to assemble the TCP/IP data structure in java. (http://jnetpcap.com/node/29)

like image 196
Yuan Zhang Avatar answered Nov 30 '25 05:11

Yuan Zhang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!