title: C listen to multicast on all interfaces, respond on same as recieved
body: <p>I am trying to listen to multicast on all interfaces in system, but responds only on this on which I've received multicast packet.</p>
  
  <p>What I've did is to create a socket for each of the interfaces and here the problems starts.</p>
  
  <p>When I bind interface to <code>INADDR_ANY</code> it will receive packets for all interfaces and send on default one. If I bind port to specific interface it will not receive multicast packet (but it will be able to send it on correct interface).</p>
  
  <p>I've tried setting options like <code>IP_ADD_MEMBERSHIP</code> or <code>IP_MULTICAST_IF</code> but without success. </p>
  
  <p>I think the other options whould be to create one socket to receive on all ifs and senders sockets for all interfaces, but on this approach I have no idea on which ifs packet entered...</p>
  
  <p>Code samples (simplified, without error handling and stuff):</p>
  
  <p>Creating socket:</p>
  
  <pre><code>//here i am looping over all interfaces from getifaddrs
  struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp-&gt;ifa_addr;
  
  sockets[i] = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  setsockopt(sockets[i], SOL_SOCKET, SO_REUSEADDR, &amp;optval, sizeof optval);
  
  mreq.imr_multiaddr.s_addr=inet_addr(MDNS_ADDRESS);
  mreq.imr_interface.s_addr=pAddr-&gt;sin_addr.s_addr;
  
  setsockopt(sockets[i], IPPROTO_IP, IP_ADD_MEMBERSHIP, &amp;mreq, sizeof(mreq));
  setsockopt(sockets[i], IPPROTO_IP, IP_MULTICAST_IF, &amp;pAddr, sizeof(struct in_addr));
  
  memset(&amp;my_addr, 0, sizeof(struct sockaddr_in));
  my_addr.sin_family = AF_INET;
  my_addr.sin_addr.s_addr = INADDR_ANY; //or pAddr-&gt;sin_addr.s_addr;
  my_addr.sin_port = htons(port);
  
  bind(sockets[i], (struct sockaddr *)&amp;my_addr, sizeof(struct sockaddr)) == -1);
  </code></pre>
  
  <p>Receiving and sending:</p>
  
  <pre><code>recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&amp;their_addr, &amp;addr_len);
  
  //do some magic and response (response should be a multicast too)
  
  destination.sin_addr.s_addr = inet_addr(MULTICAST_ADDRESS);
  destination.sin_family = AF_INET;
  destination.sin_port = htons( port );
  sendto(sockfd, buffer, len, 0, (struct sockaddr *)&amp;destination, sizeof destination);
  </code></pre>
  
  <p>I would like to create something similar in work to mDNS so when packet entered on specific interface program should answer on the same if with some data about this if. It should not send this on other ifs as it may not be relevant for them, but it should send it as multicast so any other host in same network will receive the respond.</p>