title: IPv6 Network Programming without IPv6?
body: <p>OK, so I have started doing some socket programming, I am trying to simultaneously learn how to do things using the standard BSD implementation and also how that translates into the Winsock API on Windows. I am setting my self up a Winsock utility library header that can be included in future projects that incorporate networking. I want my library to be IPv4/6 agnostic in that it has functions that work with both IPv4 and IPv6.</p>
  <p>The problem I am having is that I do not have access to internet service anywhere apparently cannot connect to any of the IPv6 internet. I know that the World IPv6 day is coming up in June, so maybe this will change. I doubt it though.. Anyway, I am unable to test any of the functions I write that work particularly on IPv6 addresses or hosts.</p>
  <p>Here is an example:</p>
  <p>I have this function that is supposed to take a hostname as a string, resolve it and then return a presentable IP address as a string. I have a version [ip_string()] that returns the first good IP address it can get in the address family AF_INET or AF_INET6. The second version [ip4_string()] will only return an IPv6 address and the same with [ip6_string()] returning an IPv6 address.</p>
  <pre><code>char *ip_string(const char hostname[])
  {
      char *buffer = new char[INET6_ADDRSTRLEN];
      struct addrinfo hints, *res, *ptr;
      void *addr = NULL;  // void pointer to be set to sin_addr or sin6_addr
  
      /** Initialize hints structure */
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_UNSPEC; // ipv4 or ipv6 address family
      hints.ai_socktype = SOCK_STREAM;
  
      /** Function getaddrinfo returns non-zero if there's a problem */
      if (getaddrinfo(hostname, &quot;http&quot;, &amp;hints, &amp;res) != 0)
          throw WinsockException(&quot;getaddrinfo()&quot;);
  
      /** Loop through list of address structures till we find a valid IP */
      const int MAX_ATTEMPTS = 15;
      int i = 0;
      for (ptr = res; (ptr != NULL); ptr = ptr-&gt;ai_next)
      {
          if (i++ &lt;= MAX_ATTEMPTS)
          {
              if (ptr-&gt;ai_family == AF_INET)
              {       // Cast appropriately for ipv4
                  addr = &amp;(((struct sockaddr_in *)ptr-&gt;ai_addr)-&gt;sin_addr);
                  inet_ntop(ptr-&gt;ai_family, (in_addr*) addr, buffer, INET_ADDRSTRLEN);
              }
              else if (ptr-&gt;ai_family == AF_INET6)
              {       // Cast appropraitely for ipv6
                  addr = &amp;(((struct sockaddr_in6 *)ptr-&gt;ai_addr)-&gt;sin6_addr);
                  inet_ntop(ptr-&gt;ai_family, (in6_addr*) addr, buffer, INET6_ADDRSTRLEN);
              }
          }
      }
  
      freeaddrinfo(res);  // free list of address structures
      if (addr == NULL)
      {
          delete [] buffer;
          return NULL;
      }
  
      return buffer;
  }
  </code></pre>
  <p>The problem I'm having (I'm assuming), is because I do not have a usable IPv6 connection, (which I confirmed using <a href="http://test-ipv6.com" rel="nofollow noreferrer">test-ipv6.com</a>), anytime any of these functions calls getaddrinfo() on a ipv6 host the function seems to not return at all.</p>
  <p>Is this normal? Is there anyway to check the connection to see if ipv6 is enabled through the Winsock API? Is there any good way for me to test ipv6 enabled code on a non-ipv6 connection?</p>