View Antoni Milton's profile on LinkedIn
Showing posts with label networking. Show all posts
Showing posts with label networking. Show all posts

Tuesday, August 9, 2011

How to resolve the IPv6 Address from domain name in C++ ?

In general, we are using gethostbyname or gethostbyname_r for resolving IPAddress from domain name. These system calls are not supporting IPv6 format.

For resolve IPv6Address from domain name, we need to use gethostbyname2_r function.

struct in6_addr aadrv6;
struct hostent *host;
char* pszHostName = “localhost6.localdomain6”;
char strIPAddr[50];
char buffer[2048];
int iError = 0,hrr;

iError = gethostbyname2_r( pszHostName, AF_INET6, &hostbuf,buffer, 2048,&host,&hrr );

if( iError == 0 && host != NULL )
{
    memcpy( &aadrv6 , host->h_addr_list[0], sizeof( in6_addr ) );
   if( inet_ntop ( AF_INET6, &aadrv6, strIPAddr, INET6_ADDRSTRLEN ) != NULL )
   {
        printf("IPv6Address: %s\n", strIPAddr);
    }
}

output:
#./a.out
IPv6Address: ::1