Few network applications written for IPv4 can dispense with the use of the Domain Name System (DNS) and, for that matter, function gethostbyname(). In the IPv6 context, though, this function is useless. Every IPv6-enabled host is likely to have, apart from one or more IPv6 addresses, also an IPv4 address and both A and AAAA records may belong to the same domain name. The gethostbyname() function provides no option for specifying which record we are interested in, or whether we want both. In 1999, RFC 2553 [RFC2553] thus came up with new functions for DNS look-ups, namely getipnodebyname and getipnodebyaddr. Unfortunately, they turned out to have other shortcomings (lack of support for address scopes). As a result, in RFC 3493 (which obsoletes RFC 2553) these two functions were deprecated. All in all, we have been left with just one function for DNS look-ups, getaddrinfo(). This function has its origins in the IEEE POSIX standard 1003.1g and is protocol independent. Along with gethostbyname(), it is also a replacement for the getservbyname() function. The prototype looks as follows:
int *getaddrinfo(const char *nodename, const char *servname,
const struct addrinfo *hints, struct addrinfo **res);
The function returns either zero, which indicates success, or various error codes. The result of the DNS query is passed to the calling program via the res argument, a pointer to a unidirectional list of structures of the addrinfo type defined in <netdb.h> as

The ai_next member points to the next item in the list (the last one has NULL here). Memory must be allocated dynamically for this list and so we have to keep in mind (at least in C) to release this memory when it is no more needed. This is done using the freeaddrinfo() function.
