Search code examples
network-programmingudpmulticastmdns

Joining to multicast groups on newly created interfaces with IP_ADD_MEMBERSHIP


I have an mDNS responder which must respond to multicast queries on all interfaces.

For example, on startup it adds its socket to multicast groups using an ethernet interface.

Then I connect to a VPN and a new ppp interface emerge. How to add my socket to the newly created interfaces in order to be able to respond on them as well?

Example:

  1. I start my mDNS responder application. It creates a UDP socket and binds it to INADDR_ANY (0.0.0.0). Then the socket joins the mDNS multicast group on ethernet interface - e.g. address 224.0.0.251, interface 192.168.0.102.

  2. I run dig @224.0.0.251 hedgaron-debian-test.local -p 5353 and receive an answer.

  3. I connect to a VPN and a new tun0 interface is being created.

  4. I run dig @224.0.0.251 hedgaron-debian-test.local -p 5353 once more and my query does not make it through, because (I guess) the responder`s socket is not joined to the multicast group on the newly created interface. Ofc, this time dig makes a query from the new interface.

NOTE: I heard l2tp/ipsec VPN doesn't work alongside with ethernet. Maybe it is an issue as well.

How to make the socket accept multicast packets on all interfaces?


Solution

  • I have found the solution. l2tp/ipsec is not an issue.

    The socket must be joined to a multigroup on every interface, not just on 0.0.0.0. To join newly created interfaces in background, the application must start a thread which rejoins on all interfaces every N seconds.

    It allows the socket to accept multicast packets from all networks.