title: Multiple Broadcast Reception on Same port but different interfaces
body: <p>I'm trying to build a little personal DHCP  server to serve a specific scope if the broadcast is received on eth0 and another if received on wlan0 but I can't bind more than a single interface on the same address:port combination (255.255.255.255:67)
  I heard about SO_REUSABLE but I have no idea about how to implement it and if of course it's the good way to do it</p>
  
  <p>Actually this is my code : </p>
  
  <pre><code>#include &lt;stdio.h&gt;   
  #include &lt;string.h&gt; 
  #include &lt;stdlib.h&gt; 
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;net/if.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  #define BUFLEN 1024
  #define PORT 67
  using namespace std;
  
  char *ipAddrFromInterface(char *apInterfaceName) //this function is not from me
  {
      return "255.255.255.255";
  
      /*char *if_name = (char *) apInterfaceName;
      struct ifreq ifr;
      size_t if_name_len = strlen(if_name);
  
      if(if_name_len &lt; sizeof(ifr.ifr_name))
      {
          memcpy(ifr.ifr_name, if_name, if_name_len);
          ifr.ifr_name[if_name_len] = 0;
      }
      else
          printf("interface name is too long\n");
  
      int fd = socket(AF_INET, SOCK_DGRAM, 0);
  
      if(fd == -1)
          printf("A =&gt; %s\n", strerror(errno));
  
      if(ioctl(fd, SIOCGIFADDR, &amp;ifr) == -1)
      {
          int temp_errno = errno;
          close(fd);
          printf("B =&gt; %s\n", strerror(temp_errno));
      }
  
      if(ioctl(fd, SIOCGIFADDR, &amp;ifr) == -1)
      {
          int temp_errno = errno;
          close(fd);
          printf("C =&gt; %s\n", strerror(temp_errno));
      }
  
      close(fd);
  
      struct sockaddr_in* ipaddr = (struct sockaddr_in*) &amp;ifr.ifr_addr;
      return inet_ntoa(ipaddr-&gt;sin_addr);*/
  }
  
  struct socketData
  {
      int sock;
      sockaddr_in  socket;
      char *interfaceName;
  };
  
  void print(int i)
  {
      printf("%d\n", i);
      fflush(stdout);
  }
  
  void server_receive_thread(vector&lt;char*&gt; aInterfaceList)
  {
      int socketIndex = 0;
      struct sockaddr_in  localSock;
      int socketDescriptor; int socketLength;
      vector&lt;socketData&gt; aSockets;
  
      for( ; socketIndex &lt; aInterfaceList.size(); socketIndex++)
      {
          socketData socketD;
          char *apInterfaceName = aInterfaceList.at(socketIndex);
  
          if((socketDescriptor = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
          {
              printf("can't listen on interface %s... sleeping\n", apInterfaceName);
          }
          else
          {
              memset(&amp;localSock, 0, sizeof(localSock));
              localSock.sin_family = AF_INET;
              localSock.sin_port = htons(PORT);
  
              inet_aton(ipAddrFromInterface(apInterfaceName), &amp;localSock.sin_addr);
              setsockopt(socketDescriptor, SOL_SOCKET, SO_BINDTODEVICE, apInterfaceName, sizeof(apInterfaceName));
  
              if(bind(socketDescriptor, (struct sockaddr *) &amp;localSock, sizeof(localSock)) == -1)
              {
                  printf("can't bind interface %s to listen on port %d... sleeping\n", apInterfaceName, PORT);
              }
              else
              {
                  printf("bound to interface %s on port %d\n", apInterfaceName, PORT);
                  socketD.sock = socketDescriptor;
                  socketD.socket = localSock;
                  socketD.interfaceName = apInterfaceName;
                  aSockets.push_back(socketD);
              }
          }
      }
  
      fd_set master;
      int fdMax = -1;
      while(1)
      {
          FD_ZERO(&amp;master);
  
          for(int iSock = 0; iSock &lt; aSockets.size(); iSock++)
          {
              socketData d = aSockets.at(iSock);
              FD_SET(d.sock, &amp;master);
  
              if(d.sock &gt; fdMax)
                  fdMax = d.sock;
          }
  
          printf("fdmax is : ");
          print(fdMax);
  
          if(select(fdMax + 1, &amp;master, NULL, NULL, NULL) == -1)
              print(2);
  
          print(200);
  
          for(int iSock = 0; iSock &lt; aSockets.size(); iSock++)
          {
              socketData d = aSockets.at(iSock);
  
              if(FD_ISSET(d.sock, &amp;master))
                  print(3);
          }
          print(1);
      }
  }
  
  int main()
  {
      std::vector&lt;char*&gt;  interfaceList;
      interfaceList.push_back("wlan0");
      interfaceList.push_back("eth0");
      server_receive_thread(interfaceList);
      return 0;
  }
  </code></pre>