p The .Li addrinfo structure is defined as a result of including the .Aq Pa netdb.h header: d -literal -offset struct addrinfo { int ai_flags; /* AI_xxx */ int ai_family; /* PF_xxx */ int ai_socktype; /* SOCK_xxx */ int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ socklen_t ai_addrlen; /* length of ai_addr */ char * ai_canonname; /* canonical name for nodename */ struct sockaddr *ai_addr; /* binary address */ struct addrinfo *ai_next; /* next structure in linked list */ }; .Ed
p The .Fa nodename and .Fa servname arguments are pointers to null-terminated strings or .Dv NULL . One or both of these two arguments must be a
f non Dv -NULL pointer. In the normal client scenario, both the .Fa nodename and .Fa servname are specified. In the normal server scenario, only the .Fa servname is specified. A
f non Dv -NULL .Fa nodename string can be either a node name or a numeric host address string
o i.e., a dotted-decimal IPv4 address or an IPv6 hex address
c . A
f non Dv -NULL .Fa servname string can be either a service name or a decimal port number.
p The caller can optionally pass an .Li addrinfo structure, pointed to by the third argument, to provide hints concerning the type of socket that the caller supports. In this .Fa hints structure all members other than .Fa ai_flags , .Fa ai_family , .Fa ai_socktype , and .Fa ai_protocol must be zero or a .Dv NULL pointer. A value of .Dv PF_UNSPEC for .Fa ai_family means the caller will accept any protocol family. A value of 0 for .Fa ai_socktype means the caller will accept any socket type. A value of 0 for .Fa ai_protocol means the caller will accept any protocol. For example, if the caller handles only TCP and not UDP, then the .Fa ai_socktype member of the hints structure should be set to .Dv SOCK_STREAM when .Fn getaddrinfo is called. If the caller handles only IPv4 and not IPv6, then the .Fa ai_family member of the .Fa hints structure should be set to .Dv PF_INET when .Fn getaddrinfo is called. If the third argument to .Fn getaddrinfo is a .Dv NULL pointer, this is the same as if the caller had filled in an .Li addrinfo structure initialized to zero with .Fa ai_family set to .Dv PF_UNSPEC .
p Upon successful return a pointer to a linked list of one or more .Li addrinfo structures is returned through the final argument. The caller can process each .Li addrinfo structure in this list by following the .Fa ai_next pointer, until a .Dv NULL pointer is encountered. In each returned .Li addrinfo structure the three members .Fa ai_family , .Fa ai_socktype , and .Fa ai_protocol are the corresponding arguments for a call to the .Fn socket function. In each .Li addrinfo structure the .Fa ai_addr member points to a filled-in socket address structure whose length is specified by the .Fa ai_addrlen member.
p If the .Dv AI_PASSIVE bit is set in the .Fa ai_flags member of the .Fa hints structure, then the caller plans to use the returned socket address structure in a call to .Fn bind . In this case, if the .Fa nodename argument is a .Dv NULL pointer, then the IP address portion of the socket address structure will be set to .Dv INADDR_ANY for an IPv4 address or .Dv IN6ADDR_ANY_INIT for an IPv6 address.
p If the .Dv AI_PASSIVE bit is not set in the .Fa ai_flags member of the .Fa hints structure, then the returned socket address structure will be ready for a call to .Fn connect
q for a connection-oriented protocol or either .Fn connect , .Fn sendto , or .Fn sendmsg
q for a connectionless protocol . In this case, if the .Fa nodename argument is a .Dv NULL pointer, then the IP address portion of the socket address structure will be set to the loopback address.
p If the .Dv AI_CANONNAME bit is set in the .Fa ai_flags member of the .Fa hints structure, then upon successful return the .Fa ai_canonname member of the first .Li addrinfo structure in the linked list will point to a null-terminated string containing the canonical name of the specified .Fa nodename .
p If the .Dv AI_NUMERICHOST bit is set in the .Fa ai_flags member of the .Fa hints structure, then a
f non Dv -NULL .Fa nodename string must be a numeric host address string. Otherwise an error of .Dv EAI_NONAME is returned. This flag prevents any type of name resolution service (e.g., the DNS) from being called.
p If the .Dv AI_NUMERICSERV bit is set in the .Fa ai_flags member of the .Fa hints structure, then a
f non Dv NULL .Fa servname string supplied shall be a numeric port string. Otherwise, an .Dv EAI_NONAME error shall be returned. This flag shall prevent any type of name resolution service (for example, NIS) from being invoked.
p The arguments to .Fn getaddrinfo must sufficiently be consistent and unambiguous. Here are pitfall cases you may encounter: l -bullet t .Fn getaddrinfo will raise an error if members of the .Fa hints structure are not consistent. For example, for internet address families, .Fn getaddrinfo will raise an error if you specify .Dv SOCK_STREAM to .Fa ai_socktype while you specify .Dv IPPROTO_UDP to .Fa ai_protocol . t If you specify a .Fa servname which is defined only for certain .Fa ai_socktype , .Fn getaddrinfo will raise an error because the arguments are not consistent. For example, .Fn getaddrinfo will raise an error if you ask for .Dq Li tftp service on .Dv SOCK_STREAM . t For internet address families, if you specify .Fa servname while you set .Fa ai_socktype to .Dv SOCK_RAW , .Fn getaddrinfo will raise an error, because service names are not defined for the internet .Dv SOCK_RAW space. t If you specify a numeric .Fa servname , while leaving .Fa ai_socktype and .Fa ai_protocol unspecified, .Fn getaddrinfo will raise an error. This is because the numeric .Fa servname does not identify any socket type, and .Fn getaddrinfo is not allowed to glob the argument in such case. .El
p All of the information returned by .Fn getaddrinfo is dynamically allocated: the .Li addrinfo structures, the socket address structures, and canonical node name strings pointed to by the addrinfo structures. To return this information to the system the function .Fn freeaddrinfo is called. The .Fa addrinfo structure pointed to by the .Fa ai argument is freed, along with any dynamic storage pointed to by the structure. This operation is repeated until a .Dv NULL .Fa ai_next pointer is encountered.
p
To aid applications in printing error messages based on the
.Dv EAI_xxx
codes returned by
.Fn getaddrinfo ,
.Fn gai_strerror
is defined.
The argument is one of the
.Dv EAI_xxx
values defined earlier and the return value points to a string describing
the error.
If the argument is not one of the
.Dv EAI_xxx
values, the function still returns a pointer to a string whose contents
indicate an unknown error.
.Ss Extension for scoped IPv6 address
The implementation allows experimental numeric IPv6 address notation with
scope identifier.
By appending the percent character and scope identifier to addresses,
you can fill the
.Li sin6_scope_id
field for addresses.
This would make management of scoped address easier,
and allows cut-and-paste input of scoped address.
p At this moment the code supports only link-local addresses with the format. Scope identifier is hardcoded to the name of the hardware interface associated with the link.
o such as .Li ne0
c . An example is .Dq Li fe80::1%ne0 , which means .Do .Li fe80::1 on the link associated with the .Li ne0 interface .Dc .
p
The implementation is still very experimental and non-standard.
The current implementation assumes a one-to-one relationship between
the interface and link, which is not necessarily true from the specification.
.Sh EXAMPLES
The following code tries to connect to
.Dq Li www.kame.net
service
.Dq Li http .
via stream socket.
It loops through all the addresses available, regardless of address family.
If the destination resolves to an IPv4 address, it will use
.Dv AF_INET
socket.
Similarly, if it resolves to IPv6,
.Dv AF_INET6
socket is used.
Observe that there is no hardcoded reference to a particular address family.
The code works even if
.Nm getaddrinfo
returns addresses that are not IPv4/v6.
d -literal -offset indent struct addrinfo hints, *res, *res0;
int error;
int s;
const char *cause = NULL;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
error = getaddrinfo("www.kame.net", "http", \*[Am]hints, \*[Am]res0);
if (error) {
errx(1, "%s", gai_strerror(error));
/*NOTREACHED*/
}
s = -1;
for (res = res0; res; res = res-\*[Gt]ai_next) {
s = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype,
res-\*[Gt]ai_protocol);
if (s \*[Lt] 0) {
cause = "socket";
continue;
}
if (connect(s, res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 0) {
cause = "connect";
close(s);
s = -1;
continue;
}
break; /* okay we got one */
}
if (s \*[Lt] 0) {
err(1, "%s", cause);
/*NOTREACHED*/
}
freeaddrinfo(res0);
.Ed
p
The following example tries to open a wildcard listening socket onto service
.Dq Li http ,
for all the address families available.
d -literal -offset indent struct addrinfo hints, *res, *res0;
int error;
int s[MAXSOCK];
int nsock;
const char *cause = NULL;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
error = getaddrinfo(NULL, "http", \*[Am]hints, \*[Am]res0);
if (error) {
errx(1, "%s", gai_strerror(error));
/*NOTREACHED*/
}
nsock = 0;
for (res = res0; res \*[Am]\*[Am] nsock \*[Lt] MAXSOCK; res = res-\*[Gt]ai_next) {
s[nsock] = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype,
res-\*[Gt]ai_protocol);
if (s[nsock] \*[Lt] 0) {
cause = "socket";
continue;
}
if (bind(s[nsock], res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 0) {
cause = "bind";
close(s[nsock]);
continue;
}
(void) listen(s[nsock], 5);
nsock++;
}
if (nsock == 0) {
err(1, "%s", cause);
/*NOTREACHED*/
}
freeaddrinfo(res0);
.Ed
.Sh DIAGNOSTICS
Error return status from
.Fn getaddrinfo
is zero on success and non-zero on errors.
Non-zero error codes are defined in
.Aq Pa netdb.h ,
and as follows:
p l -tag -width EAI_ADDRFAMILY -compact t Dv EAI_ADDRFAMILY Address family for .Fa nodename not supported. t Dv EAI_AGAIN Temporary failure in name resolution. t Dv EAI_BADFLAGS Invalid value for .Fa ai_flags . t Dv EAI_FAIL Non-recoverable failure in name resolution. t Dv EAI_FAMILY .Fa ai_family not supported. t Dv EAI_MEMORY Memory allocation failure. t Dv EAI_NODATA No address associated with .Fa nodename . t Dv EAI_NONAME .Fa nodename nor .Fa servname provided, or not known. t Dv EAI_SERVICE .Fa servname not supported for .Fa ai_socktype . t Dv EAI_SOCKTYPE .Fa ai_socktype not supported. t Dv EAI_SYSTEM System error returned in .Va errno . .El
p
If called with proper argument,
.Fn gai_strerror
returns a pointer to a string describing the given error code.
If the argument is not one of the
.Dv EAI_xxx
values, the function still returns a pointer to a string whose contents
indicate an unknown error.
.Sh SEE ALSO
.Xr gethostbyname 3 ,
.Xr getnameinfo 3 ,
.Xr getservbyname 3 ,
.Xr hosts 5 ,
.Xr nsswitch.conf 5 ,
.Xr resolv.conf 5 ,
.Xr services 5 ,
.Xr hostname 7 ,
.Xr named 8
p
.Rs
.%A R. Gilligan
.%A S. Thomson
.%A J. Bound
.%A W. Stevens
.%T Basic Socket Interface Extensions for IPv6
.%R RFC 2553
.%D March 1999
.Re
.Rs
.%A R. Gilligan
.%A S. Thomson
.%A J. Bound
.%A J. McCann
.%A W. Stevens
.%T Basic Socket Interface Extensions for IPv6
.%R RFC 3493
.%D February 2003
.Re
.Rs
.%A Tatsuya Jinmei
.%A Atsushi Onoe
.%T "An Extension of Format for IPv6 Scoped Addresses"
.%R internet draft
.%N draft-ietf-ipngwg-scopedaddr-format-02.txt
.%O work in progress material
.Re
.Rs
.%A Craig Metz
.%T Protocol Independence Using the Sockets API
.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
.%D June 2000
.Re
.Sh STANDARDS
The
.Fn getaddrinfo
function is defined in IEEE POSIX 1003.1g draft specification,
and documented in
.Dq Basic Socket Interface Extensions for IPv6
q RFC 2553 .
.Sh HISTORY
The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
.Sh BUGS
The current implementation is not thread-safe.
p The text was shamelessly copied from RFC 2553.