‘gai_strerror’ Function
The nonzero error return values from getaddrinfo have the names and meanings shown in Figure 11.7. The function gai_strerror takes one of these values as an argument and returns a pointer to the corresponding error string.
#include
const char *gai_strerror (int error);
‘freeaddrinfo’ Function
All the storage returned by getaddrinfo, the addrinfo structures, the ai_addr structures, and the ai_canonname string are obtained dynamically (e.g., from malloc). This storage is returned by calling freeaddrinfo.
#include
void freeaddrinfo (struct addrinfo *ai);
ai should point to the first addrinfo structure returned by getaddrinfo. All the structures in the linked list are freed, along with any dynamic storage pointed to by those structures (e.g., socket address structures and canonical hostnames).
‘getaddrinfo’ Function: IPv6
The POSIX specification defines the getaddrinfo function and the information it returns for both IPv4 and IPv6.
- getaddrinfo is dealing with two different inputs: the type of socket address structure the caller wants back and the type of records that should be searched for in the DNS or other database.
- The address family in the hints structure provided by the caller specifies the type of socket address structure that the caller expects to be returned. If the caller specifies AF_INET, the function must not return any sockaddr_in6 structures; if the caller specifies AF_INET6, the function must not return any sockaddr_in structures.
- POSIX says that specifying AF_UNSPEC will return addresses that can be used with any protocol family that can be used with the hostname and service name. This implies that if a host has both AAAA records and A records, the AAAA records are returned as sockaddr_in6 structures and the A records are returned as sockaddr_in structures. It makes no sense to also return the A records as IPv4-mapped IPv6 addresses in sockaddr_in6 structures because no additional information is being returned: These addresses are already being returned in sockaddr_in structures.
- This statement in the POSIX specification also implies that if the AI_PASSIVE flag is specified without a hostname, then the IPv6 wildcard address (IN6ADDR_ANY_INIT or 0::0) should be returned as a sockaddr_in6 structure, along with the IPv4 wildcard address (INADDR_ANY or 0.0.0.0), which is returned as a sockaddr_in structure. It also makes sense to return the IPv6 wildcard address first because we will see in Section 12.2(See 9.1.2) that an IPv6 server socket can handle both IPv6 and IPv4 clients on a dual-stack host.
- The address family specified in the hint structure’s ai_family member, along with the flags such as AI_V4MAPPED and AI_ALL specified in the ai_flags member, dictate the type of records that are searched for in the DNS (A and/or AAAA) and what type of addresses are returned (IPv4, IPv6, and/or IPv4-mapped IPv6). We summarize this in Figure 11.8.
- The hostname can also be either an IPv6 hex string or an IPv4 dotted-decimal string. The validity of this string depends on the address family specified by the caller. An IPv6 hex string is not acceptable if AF_INET is specified, and an IPv4 dotted-decimal string is not acceptable if AF_INET6 is specified. But, if AF_UNSPEC is specified, either is acceptable and the appropriate type of socket address structure is returned.
‘getaddrinfo’ Function: Examples
We will now show some examples of getaddrinfo using a test program that lets us enter all the parameters: the hostname, service name, address family, socket type, and AI_CANONNAME and AI_PASSIVE flags. (We do not show this test program, as it is about 350 lines of uninteresting code. It is provided with the source code for the book, as described in the Preface.) The test program outputs information on the variable number of addrinfo structures that are returned, showing the arguments for a call to socket and the address in each socket address structure.
‘host_serv’ Function
Our first interface to getaddrinfo does not require the caller to allocate a hints structure and fill it in. Instead, the two fields of interest, the address family and the socket type, are arguments to our host_serv function.
#include "unp.h"
struct addrinfo *host_serv (const char *hostname, const char *service, int family, ints socktype);
#include "unp.h"
struct addrinfo* host_serv(const char *host, const char *serv, int family, int socktype)
{
int n;
struct addrinfo hints, *res;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_flags=AI_CANONNAME;
hints.ai_family=family;
hints.ai_socktype=socktype;
if((n=getaddrinfo(host, serv, &hints, &res))!=0)
{
return NULL;
}
return res;
}
host_serv function