Home > Internet > Principles and sample programming codes of IPv6 domain resolve

Principles and sample programming codes of IPv6 domain resolve

11:12:18 September 24th, 2009 Leave a comment Go to comments

While the IPv4 addresses are being exhausted, the deployment of the new IPv6 infrastructure has been started, and you may have been enjoying the new technology now. The most improvement brought by IPv6 is the address space of 128 bit other than the 32 bit address in v4, making the number of available address extremely increased. However, at the same time, the use of IPv6 network, especially programming, is not completely compatible with the v4 case. This article discusses the domain name resolve problem and the programming implementation under IPv6 environment.

Thanks to the ingenious design of the DNS protocol, the old DNS system will support IPv6 with almost no modification. Interestingly, a IPv6 DNS client is not even required to have IPv6 access or even IPv6 network components installed (IPv6 support is not enabled on Windows XP by default, for example), but just access to a working DNS server (such as the one automatically configured by DHCP) either through IPv6 or IPv4 connection. Actually the only difference of IPv4 and IPv6 domain resolve is that v4 requests the A record of a domain while v6 requests the AAAA record.

The following example illustrates how to resolve the IPv6 domain gipv6.prdomain.net using nslookup command shipped with Windows and the dig dommand of BIND. (Note that the OpenDNS server used for the resolve were just connected through IPv6)

>nslookup -querytype=AAAA gipv6.prdomain.net
Server:  resolver
Address:1.opendns.com  208.67.222.222

Non-authoritative answer:
Name:    gipv6.prdomain.net
Address:  2001:4860:b004::68
>dig AAAA gipv6.prdomain.net

; <<>> DiG 9.4.2 <<>> AAAA gipv6.prdomain.net
; (1 server found)
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 943
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;gipv6.prdomain.net. IN AAAA

;; ANSWER SECTION:
gipv6.prdomain.net. 1800 IN AAAA 2001:4860:b004::68

;; Query time: 360 msec
;; SERVER: 208.67.222.222#53(208.67.222.222)
;; WHEN: Sat Sep 19 16:13:18 2009
;; MSG SIZE rcvd: 64

Now lets have a look at how to programmingly resolve an IPv6 domain. In IPv4 applications, domain names are resolved using the gethostbyname() function. It is possible to force the function to work in v6 mode, but loses compatibility at the same time. A new getaddrinfo() function was introduced to support domain resolve for both v4 and v6 addresses. Here is some sample code for Visual C++:

#include "stdio.h"
#include "Ws2tcpip.h"

#pragma comment(lib, "Ws2_32.lib")  // lib required by getaddrinfo


int main(int argc, char * argv[])
{
  const char *strDomain2Resolve = "gipv6.aulddays.com";

  // Initialize Winsock
  WSADATA wsaData;
  int nStatus = WSAStartup(MAKEWORD(2,2), &wsaData);
  if (NO_ERROR != nStatus)
  {
    printf("Error at WSAStartup()\n");
    return -1;
  }

  addrinfo Hints, *AddrList;
  memset(&Hints, 0, sizeof(Hints));
  Hints.ai_family = PF_INET6; // The IPv6 address family

  // Resolve the domain name
  nStatus = getaddrinfo(strDomain2Resolve, NULL, &Hints, &AddrList);
  if (NO_ERROR != nStatus)
  {
    // In case of resolve failed, including the target
    // domain name does not have an AAAA record
    printf("getaddrinfo() failed with error %d: %s\n",
      nStatus, gai_strerror(nStatus));
    return -1;
  }

  // print all addresses found
  printf("The following address(es) were found:\n");
  char pBuf[64];  // buffer for printing, 40 byte should be sufficient
  for(addrinfo *i = AddrList; i; i = i->ai_next)
  {
    // retrieve one address found
    in6_addr DnsAddr = ((sockaddr_in6 *)i->ai_addr)->sin6_addr;
    // prepare the printable version
    inet_ntop(AF_INET6, &DnsAddr, pBuf, 64);
    printf("%s\n", pBuf);
  }

  // For clients, it is appropriate to just use the
  // first address in the list (if there are multiple)
  in6_addr AddrToUse = ((sockaddr_in6 *)AddrList->ai_addr)->sin6_addr;

  freeaddrinfo(AddrList);

  WSACleanup();
}
  1. No comments yet.
  1. No trackbacks yet.
Please leave these two fields as-is: