0

I have a Java project where I am required to both send and receive network packets with only the Ethernet header present. The header contains custom MAC addresses as well, which do not match the physical hardware address of the receiving/sending interface.

I have selected jNetPcap 1.3 to accomplish this task, but I am concerned about the thread safety of my Java application and am in need of some help with the particularities of libpcap.

I have two threads, where the first thread executes a

org.jnetpcap.Pcap.loop(Pcap.LOOP_INFINITE, handler, outputQueue)

loop to capture packets from a previously opened org.jnetpcap.Pcap Object (representing a pcap_t) passed to the thread by the caller.

The second thread is supposed to pick payload/header pairs from an input queue and send them using

org.jnetpcap.Pcap.sendPacket(packetByteBuffer)

using the SAME org.jnetpcap.Pcap Object as the thread executing the reception loop.


Problem:

From using google I concluded that this approach is not working because libpcap is not threadsafe when accessing the same pcap_t object from different threads.

Theoretical Solution:

I think the solution to my problem is to create two separate instances of org.jnetpcap.Pcap, open them separately using org.jnetpcap.Pcap.openLive() and passing one instance to the transmission thread and one to the reception thread.

Before I run off changing a lot of code, I hope someone can confirm that this is indeed the right approach to solving this problem.

Thanks in advance for your answers.

aMpeX
  • 57
  • 7

1 Answers1

0

You must in some way synchronize access between the threads, e.g. you could use

  • Pcap.breakloop() and break the loop that receives packages to send some and continue the loop afterwards.
  • Pcap.dispatch() and a short timeout for Pcap.openLive() and switch between queued packages to be send and receiving packages.

From the jNetPcap Documentation: It is however safe to interact with various Pcap objects from multiple threads, as long as access is externally synchronized.

flob
  • 3,760
  • 2
  • 34
  • 57
  • Thank you for the clarification. I missed that bit of the documentation completely. Maybe I will go back to jNetPcap as for now I have decided to build my own library with JNI access to raw sockets. – aMpeX Jul 01 '14 at 09:41