rinn2883
rinn2883

Reputation: 366

Ubuntu C++ multicast double leave group message

I'm working on ubuntu 16.04. C++ code to create a UDP socket that joins/leaves a multicast group.

The code for joining/leaving the group works, but i was playing around with it and noticed something in wireshark and I can't find information about it.

When the code exits before I call leave group. Wireshark picks up 2 leave group messages. I think that when the code exits the kernel will close the socket and send a leave group message? But why 2? It's always 2.

Edit: Also when i call leave group wireshark picks up 2 leave group messages. I am sure the function is only executed once (because of print). Is the 2 messages normal behavior?

Also i added a print to the join function and it is called only once but sometimes wireshark picks up 2 join messages? This happens sometimes. I have no idea about this one.

But the code works, so I think the behavior is more related to how the kernel handles these things?

In the code i do something like this for join

ip_mreq multicastRequest;
std::cout << "joinGroup called" << std::endl;
multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
multicastRequest.imr_interface.s_addr = inet_addr(interfaceAddress(interface).c_str());
setsockopt(sockDesc_, IPPROTO_IP, IP_ADD_MEMBERSHIP,&multicastRequest, sizeof(multicastRequest))

and for leave:

ip_mreq multicastRequest;
std::cout << "leaveGroup called" << std::endl;
multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
multicastRequest.imr_interface.s_addr = inet_addr(interfaceAddress(interface).c_str());
setsockopt(sockDesc_, IPPROTO_IP, IP_DROP_MEMBERSHIP,&multicastRequest, sizeof(multicastRequest)) 

Upvotes: 2

Views: 656

Answers (1)

jch
jch

Reputation: 5651

This is described in RFC 3376 Section 8.1. All IGMP queries are repeated a number of times, known as the robustness variable. The value of the robustness variable defaults to 2, and can be tuned with the sysctl net.ipv4.igmp_qrv:

$ sysctl net.ipv4.igmp_qrv
net.ipv4.igmp_qrv = 2
$ sudo sysctl net.ipv4.igmp_qrv=1
net.ipv4.igmp_qrv = 1

Upvotes: 3

Related Questions