Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to bind a multicast (UDP) socket?

I am using multicast UDP between hosts that have multiple network interfaces. I am using boost::asio, and am confused by the 2 operations receivers have to make: bind, then join-group.

Why do you need to specify the local address of an interface, during bind, when you do that with every multicast group that you join?

The sister-question regards the multicast port: Since during sending, you send to a multicast address & port, why, during subscription to a multicast group, you only specify the address, not the port - the port being specified in the confusing call to bind.

Note: the "join-group" is a wrapper over setsockopt(IP_ADD_MEMBERSHIP), which as documented, may be called multiple times on the same socket to subscribe to different groups (over different networks?). It would therefore make perfect sense to ditch the bind call and specify the port every time I subscribe to a group.

From what I see, always binding to "0.0.0.0" and specifying the interface address when joining the group, works very well. Confused.

like image 889
haelix Avatar asked May 21 '12 21:05

haelix


People also ask

What is multicast in UDP?

What is IP Multicasting? -IP multicasting allows a host to send a single packet to thousands of hosts across a routed network i.e. The Internet. It is used mainly for audio (radio) and video distribution. In Networking a packet can be sent to: A single host –Unicast = (TCP and UDP) All hosts -Broadcast – (UDP only)

What is UDP bind?

UDP bind. The bind() system call is used to associate a local address with a socket. Addresses in the AF_IPV4 family are specified using struct sockaddr_in. The sys_bind() function is defined in net/socket.

Do I need to bind a UDP socket?

As far as I remember bind is not required for a UDP socket because a bind call is made for you by the stack.

What is a multicast socket?

The multicast datagram socket class is useful for sending and receiving IP multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining "groups" of other multicast hosts on the internet. A multicast group is specified by a class D IP address and by a standard UDP port number.


1 Answers

To bind a UDP socket when receiving multicast means to specify an address and port from which to receive data (NOT a local interface, as is the case for TCP acceptor bind). The address specified in this case has a filtering role, i.e. the socket will only receive datagrams sent to that multicast address & port, no matter what groups are subsequently joined by the socket. This explains why when binding to INADDR_ANY (0.0.0.0) I received datagrams sent to my multicast group, whereas when binding to any of the local interfaces I did not receive anything, even though the datagrams were being sent on the network to which that interface corresponded.

Quoting from UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API by W.R Stevens. 21.10. Sending and Receiving

[...] We want the receiving socket to bind the multicast group and port, say 239.255.1.2 port 8888. (Recall that we could just bind the wildcard IP address and port 8888, but binding the multicast address prevents the socket from receiving any other datagrams that might arrive destined for port 8888.) We then want the receiving socket to join the multicast group. The sending socket will send datagrams to this same multicast address and port, say 239.255.1.2 port 8888.

like image 186
haelix Avatar answered Oct 07 '22 20:10

haelix