I could not find a working example for this, so I am going to post a question (and let's see if I can reduce this to an MVP code example). So, I need to do mdns queries, I can use two sockets (one for sending / second for receiving) but so far I have not been able to make it work with a single socket.
So the steps that I am performing are:
Enumerate all the interface addresses on the host. Then for each host:
- Create a non blocking UDP socket
- bind() to either the interface address:5353 or the multicast group address (either of these cause either read or write to fail)
- Set IP_MULTICAST_IF to the local interface
- IP_ADD_MEMBERSHIP to the multicast group
- Add the sockets for read/write in kqueue
So depending on what I do in step 2 either read or write fails:
- If I bind the socket to the multicast group I get read notifications from the kqueue and can read packets, but when I get a write notification from the kqueue and try to write it fails with errno 49
- If I bind the socket to the interface address I can send the packets but no read notifications ever arrive from the kqueue
So which address I need to bind to then? Also, since the IP_ADD_MEMBERSHIP
parameter ip_mreq
has the interface field, do I really need the IP_MULTICAST_IF
?