user1680544
user1680544

Reputation:

A socket didn't join a multicast group but it can receive data.

when I created two udp sockets and I bind them to INADDR_ANY and same port number. but one of them joined a multicast group. but both of them can receive data from the same multicast group, even one of sockets didn't join to the multicast group.

Upvotes: 9

Views: 3609

Answers (2)

hek2mgl
hek2mgl

Reputation: 157967

The linux kernel doesn't track the state of IGMP joins. IGMP is a router protocol. Sending an IGMP join to a multicast group just tells the router that it should forward packets to a given address and port. Note that routers must be capable of talking IGMP.

This means although you used setsockopt() to join the multicast group, the membership isn't tracked per socket by the kernel as you may expect. The kernel just sends an IGMP join packet to the router. You can verify this using wireshark or whatever.

As the kernel doesn't track the IGMP state of a socket, incoming traffic on that address and port is just 'regular' traffic for the kernel.

So if you bound both sockets to the same address and port and then have sent an IGMP join using that address and port, it is the expected behaviour that packets will be available on both sockets.

BTW: Why do you need two sockets bound to the same address and port?

UPDATE: Following the explanation of @Ambroz Bizjak (thanks) it isn't true that the Linux kernel doesn't track the state of IGMP joins. It does. But it don't uses this information to decide which packets should be forwarded to which socket, if multiple sockets are bound to the same address and port.

Upvotes: 4

Ambroz Bizjak
Ambroz Bizjak

Reputation: 8095

The kernel simply doesn't filter incoming multicast packets based on which multicast groups a socket is a member of. If you don't add a socket to a group, it may still receive multicasts to a group if other sockets on the same system are members. (I'm not sure what happens if a multicast arrives but no sockets are members. You can test if you like.)

Note that the kernel in fact does track group ownership, per-socket even. It has to, or it couldn't properly implement the client side of the IGMP protocol. For instance, the kernel needs to reply to various kinds of IGMP queries from the router (where it is asked what groups the host is joined to), and also so it knows to send a Leave Group message when there are no more sockets joined to a specific group.

Upvotes: 6

Related Questions