4
votes

Purpose: to receive UDP unicast packets sent to a single port in two different processes.

System: Linux, Language: C

I am able to bind two sockets in two different processes to the same port using SO_REUSEADDR. But, as expected, the packets are received in only one(the one bound later).

Is it possible to receive packets in both the processes? If not, how is tcpdump able to read but not consume packets.

3
What do you want? To inspect datagrams? → use an AF_PACKET socket or libpcap. To do some processing and send responses? → use multicast. To do some load balancing? → use some load-balancing proxy/router.ninjalj

3 Answers

1
votes

It is not possible with sockets API, and tcpdump picks packets right from network interface, before any TCP/IP processing.

Your only chance is to receive packets in one process and resend them to another one.

0
votes

Why unicast? This is exactly what multicast is for.

To answer you second question - tcpdump gets a copy of each packet it listens to via something called bpf, and that has to be explicitly supported by the network card driver.

0
votes

If you open a socket, bind, listen, then fork() your process, incoming connection requests will be handed, each time, to a randomly selected, single one of those processes (I've never bothered to figure out the strategy that Linux uses because it balances the load well).

I am currently testing that same behavior with a UDP server.