Sockets: Internet Domains 1215
Figure 59-3: Structures allocated and returned by getaddrinfo()
The hints argument
The hints argument specifies further criteria for selecting the socket address struc-
tures returned by getaddrinfo(). When used as the hints argument, only the ai_flags,
ai_family, ai_socktype, and ai_protocol fields of the addrinfo structure can be set. The
other fields are unused, and should be initialized to 0 or NULL, as appropriate.
The hints.ai_family field selects the domain for the returned socket address
structures. It may be specified as AF_INET or AFINET6 (or some other AF* constant, if
the implementation supports it). If we are interested in getting back all types of
socket address structures, we can specify the value AF_UNSPEC for this field.
The hints.ai_socktype field specifies the type of socket for which the returned
address structure is to be used. If we specify this field as SOCK_DGRAM, then a lookup
is performed for the UDP service, and a corresponding socket address structure is
returned via result. If we specify SOCK_STREAM, a lookup for the TCP service is performed.
If hints.ai_socktype is specified as 0, any socket type is acceptable.
The hints.ai_protocol field selects the socket protocol for the returned address
structures. For our purposes, this field is always specified as 0, meaning that the
caller will accept any protocol.
ai_addr
result
addrinfo
structures
ai_next
socket address
ai_canonname structures
canonical
hostname
ai_addr
ai_next
ai_addr
ai_next