views:

1869

answers:

7

What would be the easiest way to be able to send and receive raw network packets. Do I have to write my own JNI wrapping of some c API, and in that case what API am I looking for?

EDIT: I want to be able to do what wireshark does, i.e. record all incomming packets on an interface, and in addition be able to send back my own created packets. And I want to do it on a mac.

A: 

How low level do you need? Usually when I am doing networking with Java, I use a ServerSocket if I need to accept connections, and Socket when I need to connect to another machine. Socket will use a TCP connection... if you need a UDP connection, I think you can use a DatagramSocket.

These classes abstract away the cross platform details, so unless you really need to see the packet level information, these are really useful.

Update: It looks like if you use UDP with DatagramSocket, you can send and receive packets using DatagramPacket, but it looks like that still abstracts away all but the address/port and data being sent.

EDIT: Whoever keeps downvoting me, please look at his initial question before you downvote this... it wasn't clear that he NEEDED actual packet access, and I was trying to see if he REALLY needed it... sockets in Java are more than sufficient if you are not building something like ethereal/wireshark.

Mike Stone
A: 

TINI is a java ethernet controller, which may have libraries and classes for directly accessing data from ethernet frames to TCP streams. You may be able to find something in there that implements your needed classes. If not, there should be pointers or user groups that will give you a head start.

Adam Davis
+1  A: 

If you start with the idea that you need something like a packet sniffer, you'll want to look at http://netresearch.ics.uci.edu/kfujii/jpcap/doc/.

Joe Liversedge
A: 

Raw Socket for Java is a request for JDK for a looong long time. See the request here. There's a long discussion there where you can look for workarounds and solutions. I once needed this for a simple PING operation, but I can't remember how I resolved this. Sorry :)

Marcio Aguiar
Why did I receive a downvote? The enhancement request link shows a lot of solutions to this problem and seems that Raw Socket is going to be part of JDK. ¬¬
Marcio Aguiar
A: 

My best bet so far seems to be the BPF api and to write a thin JNI wrapper

Johan Lübcke
A: 

You can't access raw sockets from pure Java, so you will need some sort of layer between your Java code and the network interfaces.

Also note that access to raw sockets is normally only available to "root" processes, since otherwise any user could both a) sniff all traffic, and b) generate spoofed packets.

Rather than write your whole program so that it needs to run as "root", you might consider having the packet capture and generation done in a standalone program with some sort of IPC (RMI, named pipe, TCP socket, etc) to exchange the data with your Java app.

Alnitak
A: 

commet utilser les raw socket pour capturer les packet ip sous linux