Listening to two devices at once with libpcap

I am trying to listen on two devices using libpcap, but I still cannot figure out how to do this. I tried to set the device to "any", but it does not work. I am trying to write a dhcp relay, so I need to listen to eth0 and eth1.

I tried to create two pcap_loops, each with a different device and handler, but only the first pcap_loop works, the second is ignored.

Is there a way to do this, or should I leave libpcap and try to do this using raw sockets?

+5
source share
1 answer

You will need to run pcap_loop () in separate threads, one for each interface, we do this and it works.

Some parts of libpcap are not thread safe, though, atleast pcap_setfilter (), so provide your own lock around this.

If you do not want to use streams, you will need to provide an event loop on your own, where you track the file descriptors of each device using select / poll or similar. You can get the file descriptor for the device descriptor using pcap_get_selectable_fd ().

+3
source

All Articles