1 .\" $NetBSD: getaddrinfo.3,v 1.39 2005/01/11 06:01:41 itojun 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.
26 .Nd socket address structure to host and service name
28 .Fd #include <sys/types.h>
29 .Fd #include <sys/socket.h>
30 .Fd #include <netdb.h>
32 .Fn getaddrinfo "const char *hostname" "const char *servname" \
33 "const struct addrinfo *hints" "struct addrinfo **res"
35 .Fn freeaddrinfo "struct addrinfo *ai"
39 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 or an IPv6 address.
62 is either a decimal port number or a service name listed in
71 is an optional pointer to a
77 int ai_flags; /* input flags */
78 int ai_family; /* protocol family for socket */
79 int ai_socktype; /* socket type */
80 int ai_protocol; /* protocol for socket */
81 socklen_t ai_addrlen; /* length of socket-address */
82 struct sockaddr *ai_addr; /* socket-address for socket */
83 char *ai_canonname; /* canonical name for service location */
84 struct addrinfo *ai_next; /* pointer to next in list */
88 This structure can be used to provide hints concerning the type of socket
89 that the caller supports or wishes to use.
90 The caller can supply the following structure elements in
92 .Bl -tag -width "ai_socktypeXX"
94 The protocol family that should be used.
99 it means the caller will accept any protocol family supported by the
102 Denotes the type of socket that is wanted:
109 is zero the caller will accept any socket type.
111 Indicates which transport protocol is desired,
117 is zero the caller will accept any protocol.
122 the following values:
123 .Bl -tag -width "AI_CANONNAMEXX"
127 bit is set, a successful call to
129 will return a NUL-terminated string containing the canonical name
130 of the specified hostname in the
135 .It Dv AI_NUMERICHOST
138 bit is set, it indicates that
140 should be treated as a numeric string defining an IPv4 or IPv6 address
141 and no name resolution should be attempted.
145 bit is set it indicates that the returned socket address structure
146 is intended for use in a call to
150 argument is the null pointer, then the IP address portion of the
151 socket address structure will be set to
153 for an IPv4 address or
159 bit is not set, the returned socket address structure will be ready
162 for a connection-oriented protocol or
167 if a connectionless protocol was chosen.
170 address portion of the socket address structure will be set to the
173 is the null pointer and
179 All other elements of the
183 must be zero or the null pointer.
189 behaves as if the caller provided a
195 and all other elements set to zero or
198 After a successful call to
201 is a pointer to a linked list of one or more
204 The list can be traversed by following the
208 structure until a null pointer is encountered.
216 structure are suitable for a call to
220 structure in the list, the
222 member points to a filled-in socket address structure of length
225 This implementation of
227 allows numeric IPv6 address notation with scope identifier,
228 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
229 By appending the percent character and scope identifier to addresses,
233 This would make management of scoped addresses easier
234 and allows cut-and-paste input of scoped addresses.
236 At this moment the code supports only link-local addresses with the format.
237 The scope identifier is hardcoded to the name of the hardware interface
249 on the link associated with the
254 The current implementation assumes a one-to-one relationship between
255 the interface and link, which is not necessarily true from the specification.
257 All of the information returned by
259 is dynamically allocated: the
261 structures themselves as well as the socket address structures and
262 the canonical host name strings included in the
266 Memory allocated for the dynamically allocated structures created by
276 structure created by a call to
280 returns zero on success or one of the error codes listed in
284 The following code tries to connect to
289 It loops through all the addresses available, regardless of address family.
290 If the destination resolves to an IPv4 address, it will use an
293 Similarly, if it resolves to IPv6, an
296 Observe that there is no hardcoded reference to a particular address family.
297 The code works even if
299 returns addresses that are not IPv4/v6.
300 .Bd -literal -offset indent
301 struct addrinfo hints, *res, *res0;
304 const char *cause = NULL;
306 memset(&hints, 0, sizeof(hints));
307 hints.ai_family = PF_UNSPEC;
308 hints.ai_socktype = SOCK_STREAM;
309 error = getaddrinfo("www.kame.net", "http", &hints, &res0);
311 errx(1, "%s", gai_strerror(error));
315 for (res = res0; res; res = res->ai_next) {
316 s = socket(res->ai_family, res->ai_socktype,
323 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
330 break; /* okay we got one */
339 The following example tries to open a wildcard listening socket onto service
341 for all the address families available.
342 .Bd -literal -offset indent
343 struct addrinfo hints, *res, *res0;
347 const char *cause = NULL;
349 memset(&hints, 0, sizeof(hints));
350 hints.ai_family = PF_UNSPEC;
351 hints.ai_socktype = SOCK_STREAM;
352 hints.ai_flags = AI_PASSIVE;
353 error = getaddrinfo(NULL, "http", &hints, &res0);
355 errx(1, "%s", gai_strerror(error));
359 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
360 s[nsock] = socket(res->ai_family, res->ai_socktype,
367 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
372 (void) listen(s[nsock], 5);
388 .Xr gethostbyname 3 ,
390 .Xr getservbyname 3 ,
403 .%T Basic Socket Interface Extensions for IPv6
413 .%T "IPv6 Scoped Address Architecture"
415 .%N draft-ietf-ipv6-scoping-arch-02.txt
416 .%O work in progress material
420 .%T Protocol Independence Using the Sockets API
421 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
427 function is defined by the
429 draft specification and documented in
431 .Dq Basic Socket Interface Extensions for IPv6 .
433 The implementation of