title: Receiving different multicast with same source address on different interfaces
body: <p>On a system with several network interfaces, I can have the same multicast address and port combination used on different networks, with different data on them. I want to be able to connect to them with several network cards and receive different data on each interface.</p>
  
  <p>To do so, I bind to the interface I want to receive on using the <code>IP_MULTICAST_IF</code> option:</p>
  
  <pre><code>ip_mreqn mreqn;
  memset(&amp;mreqn, 0, sizeof (ip_mreqn));
  mreqn.imr_multiaddr.s_addr = inet_addr(mc);
  mreqn.imr_address.s_addr = INADDR_ANY;
  mreqn.imr_ifindex = if_nametoindex(device);
  if (setsockopt(mct-&gt;fd, IPPROTO_IP, IP_MULTICAST_IF, &amp;mreqn, sizeof(mreqn)) &lt; 0) {
    perror("setsockopt multicast if");
    return 1;
  }
  </code></pre>
  
  <p>and make sure the join request is only sent on that interface by setting <code>IP_ADD_MEMBERSHIP</code> with the same structure:</p>
  
  <pre><code>if (setsockopt(mct-&gt;fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &amp;mreqn, sizeof(mreqn)) &lt; 0) {
    perror("setsockopt add membership");
    return 1;
  }
  </code></pre>
  
  <p>While the <code>IP_ADD_MEMBERSHIP</code> code works (the join request is only sent on the interface specified), the <code>IP_MULTICAST_IF</code> does not. Instead once it is able to join the multicast on any of the interfaces, I receive the same data through all sockets, even if they have different <code>imr_ifindex</code> set.</p>