1 .\" $NetBSD: getaddrinfo.3,v 1.57 2013/05/03 21:13:34 wiz Exp $
2 .\" $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $
3 .\" $OpenBSD: getaddrinfo.3,v 1.35 2004/12/21 03:40:31 jaredy Exp $
5 .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
6 .\" Copyright (C) 2000, 2001 Internet Software Consortium.
8 .\" Permission to use, copy, modify, and distribute this software for any
9 .\" purpose with or without fee is hereby granted, provided that the above
10 .\" copyright notice and this permission notice appear in all copies.
12 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
13 .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14 .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15 .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16 .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
17 .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18 .\" PERFORMANCE OF THIS SOFTWARE.
27 .Nd host and service name to socket address structure
31 .Fn getaddrinfo "const char * restrict hostname" \
32 "const char * restrict servname" \
33 "const struct addrinfo * restrict hints" "struct addrinfo ** restrict res"
35 .Fn freeaddrinfo "struct addrinfo *ai"
37 .Fn allocaddrinfo "socklen_t len"
41 function is used to get a list of
43 addresses and port numbers for host
47 It is a replacement for and provides more flexibility than the
57 arguments are either pointers to NUL-terminated strings or the null pointer.
58 An acceptable value for
60 is either a valid host name or a numeric host address string consisting
61 of a dotted decimal IPv4 address or an IPv6 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; /* input flags */
80 int ai_family; /* address family for socket */
81 int ai_socktype; /* socket type */
82 int ai_protocol; /* protocol for socket */
83 socklen_t ai_addrlen; /* length of socket-address */
84 struct sockaddr *ai_addr; /* socket-address for socket */
85 char *ai_canonname; /* canonical name for service location */
86 struct addrinfo *ai_next; /* pointer to next in 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"
98 family that should be used.
103 it means the caller will accept any address family supported by the
105 Note that while address families
107 and protocol families
109 are theoretically distinct, in practice the distinction has been lost.
110 .\" (.Dv !? Consistent with usage below though...)
114 in terms of the address family constants
118 is to be passed as a protocol family to
121 Denotes the type of socket that is wanted:
128 is zero the caller will accept any socket type.
130 Indicates which transport protocol is desired,
136 is zero the caller will accept any protocol.
141 the following values:
142 .Bl -tag -width "AI_CANONNAMEXX"
146 bit is set, a successful call to
148 will return a NUL-terminated string containing the canonical name
149 of the specified hostname in the
154 .It Dv AI_NUMERICHOST
157 bit is set, it indicates that
159 should be treated as a numeric string defining an IPv4 or IPv6 address
160 and no name resolution should be attempted.
161 .It Dv AI_NUMERICSERV
164 bit is set, it indicates that the
166 string contains a numeric port number.
167 This is used to prevent service name resolution.
171 bit is set it indicates that the returned socket address structure
172 is intended for use in a call to
176 argument is the null pointer, then the IP address portion of the
177 socket address structure will be set to
179 for an IPv4 address or
185 bit is not set, the returned socket address structure will be ready
188 for a connection-oriented protocol or
193 if a connectionless protocol was chosen.
196 address portion of the socket address structure will be set to the
199 is the null pointer and
203 Perform SRV (RFC 2782) record lookups first.
204 This is a non-portable extension.
208 All other elements of the
212 must be zero or the null pointer.
218 behaves as if the caller provided a
224 and all other elements set to zero or
227 After a successful call to
230 is a pointer to a linked list of one or more
233 The list can be traversed by following the
237 structure until a null pointer is encountered.
245 structure are suitable for a call to
249 structure in the list, the
251 member points to a filled-in socket address structure of length
254 This implementation of
256 allows numeric IPv6 address notation with scope identifier,
257 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
258 By appending the percent character and scope identifier to addresses,
262 This would make management of scoped addresses easier
263 and allows cut-and-paste input of scoped addresses.
265 At this moment the code supports only link-local addresses with the format.
266 The scope identifier is hardcoded to the name of the hardware interface
278 on the link associated with the
283 The current implementation assumes a one-to-one relationship between
284 the interface and link, which is not necessarily true from the specification.
286 All of the information returned by
288 is dynamically allocated: the
290 structures themselves as well as the socket address structures and
291 the canonical host name strings included in the
295 Memory allocated for the dynamically allocated structures created by
305 structure created by a call to
311 function is intended primarily for authors of
319 in a way that is compatible with being returned from
321 and being ultimately freed by
323 The returned structure is zeroed, except for the
328 bytes of memory for storage of a socket address.
329 It is safe to allocate memory separately for
333 or in any other way that is compatible with deallocation by
337 returns zero on success or one of the error codes listed in
341 The following code tries to connect to
346 It loops through all the addresses available, regardless of address family.
347 If the destination resolves to an IPv4 address, it will use an
350 Similarly, if it resolves to IPv6, an
353 Observe that there is no hardcoded reference to a particular address family.
354 The code works even if
356 returns addresses that are not IPv4/v6.
357 .Bd -literal -offset indent
358 struct addrinfo hints, *res, *res0;
361 const char *cause = NULL;
363 memset(\*[Am]hints, 0, sizeof(hints));
364 hints.ai_family = AF_UNSPEC;
365 hints.ai_socktype = SOCK_STREAM;
366 error = getaddrinfo("www.kame.net", "http", \*[Am]hints, \*[Am]res0);
368 errx(1, "%s", gai_strerror(error));
372 for (res = res0; res; res = res-\*[Gt]ai_next) {
373 s = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype,
374 res-\*[Gt]ai_protocol);
380 if (connect(s, res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 0) {
387 break; /* okay we got one */
396 The following example tries to open a wildcard listening socket onto service
398 for all the address families available.
399 .Bd -literal -offset indent
400 struct addrinfo hints, *res, *res0;
404 const char *cause = NULL;
406 memset(\*[Am]hints, 0, sizeof(hints));
407 hints.ai_family = AF_UNSPEC;
408 hints.ai_socktype = SOCK_STREAM;
409 hints.ai_flags = AI_PASSIVE;
410 error = getaddrinfo(NULL, "http", \*[Am]hints, \*[Am]res0);
412 errx(1, "%s", gai_strerror(error));
416 for (res = res0; res \*[Am]\*[Am] nsock \*[Lt] MAXSOCK; res = res-\*[Gt]ai_next) {
417 s[nsock] = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype,
418 res-\*[Gt]ai_protocol);
419 if (s[nsock] \*[Lt] 0) {
424 if (bind(s[nsock], res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 0) {
429 (void) listen(s[nsock], 5);
445 .Xr gethostbyname 3 ,
447 .Xr getservbyname 3 ,
460 .%T Basic Socket Interface Extensions for IPv6
470 .%T "IPv6 Scoped Address Architecture"
472 .%N draft-ietf-ipv6-scoping-arch-02.txt
473 .%O work in progress material
477 .%T "Protocol Independence Using the Sockets API"
478 .%I USENIX Association
479 .%B Proceedings of the FREENIX Track: 2000 USENIX Annual Technical Conference
482 .%U http://www.usenix.org/events/usenix2000/freenix/metzprotocol/metzprotocol.pdf
487 function is defined by the
489 draft specification and documented in
491 .Dq Basic Socket Interface Extensions for IPv6 .