The socket has not joined the multicast group, but can receive data.

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

+4
source share
2 answers

The Linux kernel does not monitor the status of IGMP connections. IGMP is a router protocol. Sending an IGMP connection to a multicast group simply tells the router that it should forward packets to the specified address and port. Note that routers must be able to speak IGMP.

This means that although you used setsockopt() to join a multicast group, membership is not tracked by the kernel in the kernel, as you might expect. The kernel simply sends an IGMP connection packet to the router. You can verify this using wirehark or something else.

Since the kernel does not monitor the IGMP socket state, inbound traffic at this address and port is β€œnormal” traffic for the kernel.

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

By the way: why do you need two sockets tied to the same address and port?

UPDATE: After explaining @Ambroz Bizjak (thanks), it is not true that the Linux kernel does not monitor the status of IGMP connections. It does. But he does not use this information to decide which packets should be sent to which socket if several sockets are bound to the same address and port.

+3
source

The kernel simply does not filter incoming multicast packets, on the basis of which multicast groups are members of the socket. If you do not add a socket to the group, it can still receive group messages in the group if other sockets on the same system are members. (I'm not sure what will happen if multicast is received, but no sockets will be members. You can check if you want to.)

Note that the core actually tracks ownership of the group, even for each socket. It should or could not properly implement the IGMP client side. For example, the kernel must respond to various IGMP requests from the router (where it is asked which groups the node joins to), and also so that it knows to send the message "Leave group" when there are no more sockets connected to a specific group.

+2
source

All Articles