1 .\" $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $
2 .\" $OpenBSD: getaddrinfo.3,v 1.35 2004/12/21 03:40:31 jaredy Exp $
3 .\" $FreeBSD: src/lib/libc/net/getaddrinfo.3,v 1.29 2005/01/23 16:02:48 gnn Exp $
4 .\" $DragonFly: src/lib/libc/net/getaddrinfo.3,v 1.7 2008/05/22 06:50:14 hasso Exp $
6 .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
7 .\" Copyright (C) 2000, 2001 Internet Software Consortium.
9 .\" Permission to use, copy, modify, and distribute this software for any
10 .\" purpose with or without fee is hereby granted, provided that the above
11 .\" copyright notice and this permission notice appear in all copies.
13 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
14 .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15 .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
16 .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
18 .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 .\" PERFORMANCE OF THIS SOFTWARE.
27 .Nd socket address structure to host and service name
35 .Fn getaddrinfo "const char *hostname" "const char *servname" \
36 "const struct addrinfo *hints" "struct addrinfo **res"
38 .Fn freeaddrinfo "struct addrinfo *ai"
42 function is used to get a list of
44 addresses and port numbers for host
48 It is a replacement for and provides more flexibility than the
58 arguments are either pointers to NUL-terminated strings or the null pointer.
59 An acceptable value for
61 is either a valid host name or a numeric host address string consisting
62 of a dotted decimal IPv4 address or an IPv6 address.
65 is either a decimal port number or a service name listed in
74 is an optional pointer to a
80 int ai_flags; /* input flags */
81 int ai_family; /* protocol family for socket */
82 int ai_socktype; /* socket type */
83 int ai_protocol; /* protocol for socket */
84 socklen_t ai_addrlen; /* length of socket-address */
85 struct sockaddr *ai_addr; /* socket-address for socket */
86 char *ai_canonname; /* canonical name for service location */
87 struct addrinfo *ai_next; /* pointer to next in list */
91 This structure can be used to provide hints concerning the type of socket
92 that the caller supports or wishes to use.
93 The caller can supply the following structure elements in
95 .Bl -tag -width "ai_socktypeXX"
97 The protocol family that should be used.
102 it means the caller will accept any protocol family supported by the
105 Denotes the type of socket that is wanted:
112 is zero the caller will accept any socket type.
114 Indicates which transport protocol is desired,
120 is zero the caller will accept any protocol.
125 the following values:
126 .Bl -tag -width "AI_CANONNAMEXX"
130 bit is set, IPv4 addresses shall be returned only if
131 an IPv4 address is configured on the local system,
132 and IPv6 addresses shall be returned only if
133 an IPv6 address is configured on the local system.
141 shall return all matching IPv6 and IPv4 addresses.
150 bit is set, a successful call to
152 will return a NUL-terminated string containing the canonical name
153 of the specified hostname in the
158 .It Dv AI_NUMERICHOST
161 bit is set, it indicates that
163 should be treated as a numeric string defining an IPv4 or IPv6 address
164 and no name resolution should be attempted.
165 .It Dv AI_NUMERICSERV
171 string supplied shall be a numeric port string.
174 error shall be returned.
175 This bit shall prevent any type of name resolution service
176 (for example, NIS+) from being invoked.
180 bit is set it indicates that the returned socket address structure
181 is intended for use in a call to
185 argument is the null pointer, then the IP address portion of the
186 socket address structure will be set to
188 for an IPv4 address or
194 bit is not set, the returned socket address structure will be ready
197 for a connection-oriented protocol or
202 if a connectionless protocol was chosen.
205 address portion of the socket address structure will be set to the
208 is the null pointer and
214 flag is specified along with an
220 shall return IPv4-mapped IPv6 addresses
221 on finding no matching IPv6 addresses (
226 flag shall be ignored unless
233 All other elements of the
237 must be zero or the null pointer.
243 behaves as if the caller provided a
249 and all other elements set to zero or
252 After a successful call to
255 is a pointer to a linked list of one or more
258 The list can be traversed by following the
262 structure until a null pointer is encountered.
270 structure are suitable for a call to
274 structure in the list, the
276 member points to a filled-in socket address structure of length
279 This implementation of
281 allows numeric IPv6 address notation with scope identifier,
282 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
283 By appending the percent character and scope identifier to addresses,
287 This would make management of scoped addresses easier
288 and allows cut-and-paste input of scoped addresses.
290 At this moment the code supports only link-local addresses with the format.
291 The scope identifier is hardcoded to the name of the hardware interface
303 on the link associated with the
308 The current implementation assumes a one-to-one relationship between
309 the interface and link, which is not necessarily true from the specification.
311 All of the information returned by
313 is dynamically allocated: the
315 structures themselves as well as the socket address structures and
316 the canonical host name strings included in the
320 Memory allocated for the dynamically allocated structures created by
330 structure created by a call to
334 returns zero on success or one of the error codes listed in
338 The following code tries to connect to
343 It loops through all the addresses available, regardless of address family.
344 If the destination resolves to an IPv4 address, it will use an
347 Similarly, if it resolves to IPv6, an
350 Observe that there is no hardcoded reference to a particular address family.
351 The code works even if
353 returns addresses that are not IPv4/v6.
354 .Bd -literal -offset indent
355 struct addrinfo hints, *res, *res0;
358 const char *cause = NULL;
360 memset(&hints, 0, sizeof(hints));
361 hints.ai_family = PF_UNSPEC;
362 hints.ai_socktype = SOCK_STREAM;
363 error = getaddrinfo("www.kame.net", "http", &hints, &res0);
365 errx(1, "%s", gai_strerror(error));
369 for (res = res0; res; res = res->ai_next) {
370 s = socket(res->ai_family, res->ai_socktype,
377 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
384 break; /* okay we got one */
393 The following example tries to open a wildcard listening socket onto service
395 for all the address families available.
396 .Bd -literal -offset indent
397 struct addrinfo hints, *res, *res0;
401 const char *cause = NULL;
403 memset(&hints, 0, sizeof(hints));
404 hints.ai_family = PF_UNSPEC;
405 hints.ai_socktype = SOCK_STREAM;
406 hints.ai_flags = AI_PASSIVE;
407 error = getaddrinfo(NULL, "http", &hints, &res0);
409 errx(1, "%s", gai_strerror(error));
413 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
414 s[nsock] = socket(res->ai_family, res->ai_socktype,
421 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
426 (void) listen(s[nsock], 5);
442 .Xr gethostbyname 3 ,
444 .Xr getservbyname 3 ,
457 .%T Basic Socket Interface Extensions for IPv6
467 .%T "IPv6 Scoped Address Architecture"
469 .%N draft-ietf-ipv6-scoping-arch-02.txt
470 .%O work in progress material
474 .%T Protocol Independence Using the Sockets API
475 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
481 function is defined by the
483 draft specification and documented in
485 .Dq Basic Socket Interface Extensions for IPv6 .
487 The implementation of