How to filter multicast receive socket by interface?

I need to create two sockets listening on the same IP port, but on different interfaces:

  • socket0 receives UDP traffic sent 224.2.2.2ORE000 to eth0
  • socket1 receives UDP traffic sent to 224.2.2.2Point000 on eth1

It seemed pretty straight forward, until I realized that Linux combines all this into the same traffic. For example, say only traffic on eth1, and there is no activity on eth0. When I first create socket0, it will not receive any data, but as soon as I create socket1 (and join the multicast group), socket0 will also start receiving the same data. I found this link that explains this.

Now this makes sense to me, because the only time I specify the network interface is to join the multicast group setsockopt(socket,IPPROTO_IP,IP_ADD_MEMBERSHIP,...)with ip_mreq.imr_interface.s_addr. I believe this indicates which interface connects to the group, but has nothing to do with which interface your socket will receive from.

What I have tried so far is binding sockets to a multicast address and port, which behaves as described above. I tried to bind to the interface, but this does not work on Linux (it seems to work on Windows), you are not getting any traffic on the socket. And finally, I tried binding to INADDR_ANY, but that’s not what I want, since I get any other data sent to the port regardless of the destination IP address, for example, unicast, and it still won’t stop the multicast data from other interfaces.

I can not use SO_BINDTODEVICE, since it requires root privileges.

, . , , , . , C, , , , .

, , , . ( ) - , , . , , .


:

, , , . , (224.2.2.2:5000 ), . , ( , ), .

, , . - , .

+4
1

, , Linux, root:

INADDR_ANY IP_PKTINFO. recvmsg() UDP- IP_PKTINFO. UDP:

struct in_pktinfo {
    unsigned int   ipi_ifindex;  /* Interface index */
    struct in_addr ipi_spec_dst; /* Local address */
    struct in_addr ipi_addr;     /* Header Destination address */
};

ipi_ifindex - , . ( if_indextoname() : if_nametoindex().

Windows, , UDP .

Linux bind() IP- UDP . , , . INADDR_ANY UDP, , , (, ).

+2

Source: https://habr.com/ru/post/1684617/


All Articles