title: Multicast UDP packets are received at multiple sockets
body: <p>I am running Ubuntu 14.04. I have a process which contains 2 threads. The first thread sends 5 UDP packets through lo interface, each with a different mcast dst address: 224.0.24.10, 239.255.1.1, 239.255.1.2, 239.255.1.3, 239.255.1.4. These packets have the same dport = 60000.</p>
  
  <p>The 2nd thread creates 5 sockets and tries to receive those mcast packets. It looks like this:</p>
  
  <pre><code>int sd;
  struct sockaddr_in sock;
  struct ip_mreq group;
  int reuse = 1;
  
  sd = socket(AF_INET, SOCK_DGRAM, 0);
  setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&amp;reuse, sizeof(reuse));
  
  bzero((char *)&amp;sock, sizeof(sock));
  sock.sin_family = AF_INET;
  sock.sin_port = htons(60000);
  sock.sin_addr.s_addr = INADDR_ANY;
  
  bind(sd, (struct sockaddr*)&amp;sock, sizeof(sock));
  
  group.imr_multiaddr.s_addr = dip;
  group.imr_interface.s_addr = ((struct sockaddr_in *)(&amp;(ifr.ifr_addr)))-&gt;sin_addr.s_addr;   //This is the IP of lo: 127.0.0.1
  
  setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&amp;group, sizeof(group));
  </code></pre>
  
  <p>I left out all the error checking in the code sample above. I have them in my real code.</p>
  
  <p>Basically, the code above is in a function which is called 5 times, each call has a different dip. The sd(s) created are then set into a fd_set where select() is used to receive from all 5 sockets. </p>
  
  <p>What I saw is that socket associated with 224.0.24.10 receives the correct packet. </p>
  
  <p>But the other 4 sockets receive ALL packets to 239.255.1.*. For example, socket associated with 239.255.1.1 shall receives packets with dip = 239.255.1.1 only. But it receives packets with dip = 239.255.1.1 - 239.255.1.4.</p>
  
  <p>I turned on IP_PKTINFO and used in_pktinfo to dump dip in the received packets and confirm that these dip are correct. But why are the packets received by the wrong sockets?</p>
  
  <p>Any solution?</p>
  
  <p>Thanks,
  Yao</p>