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 $
4 .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5 .\" Copyright (C) 2000, 2001 Internet Software Consortium.
7 .\" Permission to use, copy, modify, and distribute this software for any
8 .\" purpose with or without fee is hereby granted, provided that the above
9 .\" copyright notice and this permission notice appear in all copies.
11 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 .\" PERFORMANCE OF THIS SOFTWARE.
25 .Nd socket address structure to host and service name
32 .Fa "const char *hostname" "const char *servname"
33 .Fa "const struct addrinfo *hints" "struct addrinfo **res"
36 .Fn freeaddrinfo "struct addrinfo *ai"
40 function is used to get a list of
41 addresses and port numbers for host
45 It is a replacement for and provides more flexibility than the
55 arguments are either pointers to NUL-terminated strings or the null pointer.
56 An acceptable value for
58 is either a valid host name or a numeric host address string consisting
59 of a dotted decimal IPv4 address,
61 or a UNIX-domain address.
64 is either a decimal port number or a service name listed in
73 is an optional pointer to a
79 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, .. */
80 int ai_family; /* AF_xxx */
81 int ai_socktype; /* SOCK_xxx */
82 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
83 socklen_t ai_addrlen; /* length of ai_addr */
84 char *ai_canonname; /* canonical name for hostname */
85 struct sockaddr *ai_addr; /* binary address */
86 struct addrinfo *ai_next; /* next structure in linked list */
90 This structure can be used to provide hints concerning the type of socket
91 that the caller supports or wishes to use.
92 The caller can supply the following structure elements in
94 .Bl -tag -width "ai_socktypeXX"
96 The address family that should be used.
101 it means the caller will accept any address family supported by the
104 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,
119 .Dv IPPROTO_UDPLITE .
122 is zero the caller will accept any protocol.
128 parameter points shall be set to zero
129 or be the bitwise-inclusive OR of one or more of the values
138 For a UNIX-domain address,
141 .Bl -tag -width "AI_CANONNAMEXX"
145 bit is set, IPv4 addresses shall be returned only if
146 an IPv4 address is configured on the local system,
147 and IPv6 addresses shall be returned only if
148 an IPv6 address is configured on the local system.
152 flag is used with the
156 shall return all matching IPv6 and IPv4 addresses.
158 For example, when using the DNS, queries are made for both AAAA records and A records, and
160 returns the combined results of both queries.
161 Any IPv4 addresses found are returned as IPv4-mapped IPv6 addresses.
171 bit is set, a successful call to
173 will return a NUL-terminated string containing the canonical name
174 of the specified hostname in the
179 .It Dv AI_NUMERICHOST
182 bit is set, it indicates that
184 should be treated as a numeric string defining an IPv4 or IPv6 address
185 and no name resolution should be attempted.
186 .It Dv AI_NUMERICSERV
192 string supplied shall be a numeric port string.
195 error shall be returned.
196 This bit shall prevent any type of name resolution service
197 (for example, NIS+) from being invoked.
201 bit is set it indicates that the returned socket address structure
202 is intended for use in a call to
206 argument is the null pointer, then the IP address portion of the
207 socket address structure will be set to
209 for an IPv4 address or
215 bit is not set, the returned socket address structure will be ready
218 for a connection-oriented protocol or
223 if a connectionless protocol was chosen.
226 address portion of the socket address structure will be set to the
229 is the null pointer and
235 flag is specified along with an ai_family of
239 shall return IPv4-mapped IPv6 addresses on finding no matching IPv6 addresses (
243 For example, when using the DNS, if no AAAA records are found then a query is made for A records and any found are returned as IPv4-mapped IPv6 addresses.
247 flag shall be ignored unless
254 All other elements of the
258 must be zero or the null pointer.
264 behaves as if the caller provided a
270 and all other elements set to zero or
273 After a successful call to
276 is a pointer to a linked list of one or more
279 The list can be traversed by following the
283 structure until a null pointer is encountered.
286 structure contains three members that are suitable for a call to
294 structure in the list, the
296 member points to a filled-in socket address structure of length
299 This implementation of
301 allows numeric IPv6 address notation with scope identifier,
302 as documented in chapter 11 of RFC 4007.
303 By appending the percent character and scope identifier to addresses,
307 This would make management of scoped addresses easier
308 and allows cut-and-paste input of scoped addresses.
310 At this moment the code supports only link-local addresses with the format.
311 The scope identifier is hardcoded to the name of the hardware interface
323 on the link associated with the
328 The current implementation assumes a one-to-one relationship between
329 the interface and link, which is not necessarily true from the specification.
331 All of the information returned by
333 is dynamically allocated: the
335 structures themselves as well as the socket address structures and
336 the canonical host name strings included in the
340 Memory allocated for the dynamically allocated structures created by
350 structure created by a call to
352 .Sh IMPLEMENTATION NOTES
354 .Li freeaddrinfo(NULL)
355 is left unspecified by both
359 The current implementation ignores a
361 argument for compatibility with programs that rely on the implementation
362 details of other operating systems.
365 returns zero on success or one of the error codes listed in
369 The following code tries to connect to
374 It loops through all the addresses available, regardless of address family.
375 If the destination resolves to an IPv4 address, it will use an
378 Similarly, if it resolves to IPv6, an
381 Observe that there is no hardcoded reference to a particular address family.
382 The code works even if
384 returns addresses that are not IPv4/v6.
385 .Bd -literal -offset indent
386 struct addrinfo hints, *res, *res0;
389 const char *cause = NULL;
391 memset(&hints, 0, sizeof(hints));
392 hints.ai_family = AF_UNSPEC;
393 hints.ai_socktype = SOCK_STREAM;
394 error = getaddrinfo("www.kame.net", "http", &hints, &res0);
396 errx(1, "%s", gai_strerror(error));
400 for (res = res0; res; res = res->ai_next) {
401 s = socket(res->ai_family, res->ai_socktype,
408 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
415 break; /* okay we got one */
424 The following example tries to open a wildcard listening socket onto service
426 for all the address families available.
427 .Bd -literal -offset indent
428 struct addrinfo hints, *res, *res0;
432 const char *cause = NULL;
434 memset(&hints, 0, sizeof(hints));
435 hints.ai_family = AF_UNSPEC;
436 hints.ai_socktype = SOCK_STREAM;
437 hints.ai_flags = AI_PASSIVE;
438 error = getaddrinfo(NULL, "http", &hints, &res0);
440 errx(1, "%s", gai_strerror(error));
444 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
445 s[nsock] = socket(res->ai_family, res->ai_socktype,
452 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
457 (void) listen(s[nsock], 5);
473 .Xr gethostbyname 3 ,
475 .Xr getservbyname 3 ,
490 .%T Basic Socket Interface Extensions for IPv6
500 .%T "IPv6 Scoped Address Architecture"
506 .%T Protocol Independence Using the Sockets API
507 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
513 function is defined by the
515 specification and documented in
517 .Dq Basic Socket Interface Extensions for IPv6 .