title: Get IPv6 DNS Server Addresses using GetAdaptersAddresses()
body: <p>I am trying to get the DNS Server IPv6 addresses in C++, and I found this article showing how to get the information using GetAdaptersAddresses(): <a href="https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses#:%7E:text=The%20GetAdaptersAddresses%20function%20retrieves%20the%20addresses%20associated%20with,associated%20with%20adapters%20with%20IPv4%20or%20IPv6%20enabled." rel="nofollow noreferrer">GetAdaptersAddresses function (iphlpapi.h)</a>. This article also provides example code. Going through the instructions, it says the FirstDnsServerAddress member of the IP_ADAPTER_ADDRESSES structure is a pointer to a linked list of IP_ADAPTER_DNS_SERVER_ADDRESS structures. The SOCKET_ADDRESS structure is used in the IP_ADAPTER_DNS_SERVER_ADDRESS structure. The SOCKADDR structure pointed to by the lpSockaddr member varies depending on the protocol or address family selected. For example, the sockaddr_in6 structure is used for an IPv6 socket address while the sockaddr_in4 structure is used for an IPv4 socket address. Based on those instructions I modified the example code to get the IP addresses as follows:</p>
  <pre class="lang-c prettyprint-override"><code>pDnServer = pCurrAddresses-&gt;FirstDnsServerAddress;
  if (pDnServer) {
      for (i = 0; pDnServer != NULL; i++) {
          switch (pDnServer-&gt;Address.lpSockaddr-&gt;sa_family) {
              case AF_INET:
                  if (pDnServer-&gt;Address.lpSockaddr) {
                      char* ipv4_addrstr = (char*)malloc(INET_ADDRSTRLEN * sizeof(char));
                      inet_ntop(AF_INET, (struct sockaddr_in*)pDnServer-&gt;Address.lpSockaddr-&gt;sa_data, ipv4_addrstr, INET_ADDRSTRLEN);
                      printf(&quot;IPv4 Address: %s\n&quot;, ipv4_addrstr);
                      free(ipv4_addrstr);
                  }
                  break;
              case AF_INET6: 
                  if (pDnServer-&gt;Address.lpSockaddr) {
                      char* ipv6_addrstr = (char*)malloc(INET6_ADDRSTRLEN * sizeof(char));
                      memset(ipv6_addrstr, 0, sizeof(char) * INET6_ADDRSTRLEN);
                      inet_ntop(AF_INET6, (struct sockaddr_in6*)pDnServer-&gt;Address.lpSockaddr-&gt;sa_data, ipv6_addrstr, INET6_ADDRSTRLEN);
                      printf(&quot;IPv6 Address: %s\n&quot;, ipv6_addrstr);
                      free(ipv6_addrstr);
                  }
                  break;
  
              default:
                  printf(&quot;\tAddress Family Name: Unknown\n&quot;);
                  break;
          } // End switch
          pDnServer = pDnServer-&gt;Next;
      } // End for loop
          
      printf(&quot;\tNumber of DNS Server Addresses: %d\n&quot;, i);
  }
  </code></pre>
  <p>The code gets the IPv4 DNS Server addresses correctly. However, the code is returning incorrect IPv6 addresses. For example, the Google DNS Server addresses are <strong>2001:4860:4860::8888</strong> and <strong>2001:4860:4860::8844</strong>. If I enter these addresses as my IPv4 DNS Server addresses, the code returns <strong>::2001:4860:4860:0:0</strong>.</p>
  <p>Does anyone know what I might have wrong in this code?</p>