title: How to listen on all IPV6 addresses using C sockets API
body: <p>I maintain GPSD, a widely-deployed open-source service daemon that monitors GPSes and other geodetic sensors.  It listens for client-application connections on port 2947 on both IPv4 and IPv6. For security and privacy it normally listens only on the loopback address, but there is a -G option to the daemon that is intended to cause it to listen on any address.</p>
  
  <p>The problem: the -G option works in IPv4, but I can't figure out how to make it work with IPv6. The method that should work based on various tutorial examples does not, producing instead an error suggesting the address is already in use. I'm seeking help to fix this from people experienced with IPv6 network programming.</p>
  
  <p>Relevant code is at <a href="http://git.berlios.de/cgi-bin/gitweb.cgi?p=gpsd;a=blob;f=gpsd.c;h=ee2156caf03ca23405f57f3e04e9ef306a75686f;hb=HEAD" rel="noreferrer">http://git.berlios.de/cgi-bin/gitweb.cgi?p=gpsd;a=blob;f=gpsd.c;h=ee2156caf03ca23405f57f3e04e9ef306a75686f;hb=HEAD</a></p>
  
  <p>This code operates correctly in both the -G and non -G cases under IPv4, as is easily verified with netstat -l.</p>
  
  <p>Now look around line 398 after "case AF_INET6:".  The listen_global option is set by -G; when false, the code succeeds.  There is presently a following comment, inherited from an unknown contributor, that reads like this:</p>
  
  <pre><code>/* else */
          /* BAD:  sat.sa_in6.sin6_addr = in6addr_any;
       * the simple assignment will not work (except as an initializer)
       * because sin6_addr is an array not a simple type
       * we could do something like this:
       * memcpy(sat.sa_in6.sin6_addr, in6addr_any, sizeof(sin6_addr));
       * BUT, all zeros is IPv6 wildcard, and we just zeroed the array
       * so really nothing to do here
       */
  </code></pre>
  
  <p>According to various tutorial examples I have looked up, the assignment "sat.sa_in6.sin6_addr = in6addr_any;" is (despite the comment) correct, and it does compile.  However, startup with -G fails claiming the listen address is already in use.</p>
  
  <p>Is the assignment "sat.sa_in6.sin6_addr = in6addr_any;" nominally correct here? What else, if anything, am I missing?</p>