1 .\" $NetBSD: getaddrinfo.3,v 1.55 2010/04/17 20:28:47 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
205 All other elements of the
209 must be zero or the null pointer.
215 behaves as if the caller provided a
221 and all other elements set to zero or
224 After a successful call to
227 is a pointer to a linked list of one or more
230 The list can be traversed by following the
234 structure until a null pointer is encountered.
242 structure are suitable for a call to
246 structure in the list, the
248 member points to a filled-in socket address structure of length
251 This implementation of
253 allows numeric IPv6 address notation with scope identifier,
254 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
255 By appending the percent character and scope identifier to addresses,
259 This would make management of scoped addresses easier
260 and allows cut-and-paste input of scoped addresses.
262 At this moment the code supports only link-local addresses with the format.
263 The scope identifier is hardcoded to the name of the hardware interface
275 on the link associated with the
280 The current implementation assumes a one-to-one relationship between
281 the interface and link, which is not necessarily true from the specification.
283 All of the information returned by
285 is dynamically allocated: the
287 structures themselves as well as the socket address structures and
288 the canonical host name strings included in the
292 Memory allocated for the dynamically allocated structures created by
302 structure created by a call to
308 function is intended primarily for authors of
316 in a way that is compatible with being returned from
318 and being ultimately freed by
320 The returned structure is zeroed, except for the
325 bytes of memory for storage of a socket address.
326 It is safe to allocate memory separately for
330 or in any other way that is compatible with deallocation by
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(\*[Am]hints, 0, sizeof(hints));
361 hints.ai_family = AF_UNSPEC;
362 hints.ai_socktype = SOCK_STREAM;
363 error = getaddrinfo("www.kame.net", "http", \*[Am]hints, \*[Am]res0);
365 errx(1, "%s", gai_strerror(error));
369 for (res = res0; res; res = res-\*[Gt]ai_next) {
370 s = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype,
371 res-\*[Gt]ai_protocol);
377 if (connect(s, res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 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(\*[Am]hints, 0, sizeof(hints));
404 hints.ai_family = AF_UNSPEC;
405 hints.ai_socktype = SOCK_STREAM;
406 hints.ai_flags = AI_PASSIVE;
407 error = getaddrinfo(NULL, "http", \*[Am]hints, \*[Am]res0);
409 errx(1, "%s", gai_strerror(error));
413 for (res = res0; res \*[Am]\*[Am] nsock \*[Lt] MAXSOCK; res = res-\*[Gt]ai_next) {
414 s[nsock] = socket(res-\*[Gt]ai_family, res-\*[Gt]ai_socktype,
415 res-\*[Gt]ai_protocol);
416 if (s[nsock] \*[Lt] 0) {
421 if (bind(s[nsock], res-\*[Gt]ai_addr, res-\*[Gt]ai_addrlen) \*[Lt] 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 .%I USENIX Association
476 .%B Proceedings of the FREENIX Track: 2000 USENIX Annual Technical Conference
479 .%U http://www.usenix.org/events/usenix2000/freenix/metzprotocol/metzprotocol.pdf
484 function is defined by the
486 draft specification and documented in
488 .Dq Basic Socket Interface Extensions for IPv6 .