1 .\" $OpenBSD: getaddrinfo.3,v 1.19 2001/08/06 10:42:26 mpech Exp $
2 .\" $KAME: getaddrinfo.3,v 1.29 2001/02/12 09:24:45 itojun Exp $
4 .\" Copyright (c) 1983, 1987, 1991, 1993
5 .\" The Regents of the University of California. All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed by the University of
18 .\" California, Berkeley and its contributors.
19 .\" 4. Neither the name of the University nor the names of its contributors
20 .\" may be used to endorse or promote products derived from this software
21 .\" without specific prior written permission.
23 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
45 .Nd nodename-to-address translation in protocol-independent manner
48 .Fd #include <sys/types.h>
49 .Fd #include <sys/socket.h>
50 .Fd #include <netdb.h>
52 .Fn getaddrinfo "const char *nodename" "const char *servname" \
53 "const struct addrinfo *hints" "struct addrinfo **res"
55 .Fn freeaddrinfo "struct addrinfo *ai"
57 .Fn gai_strerror "int ecode"
62 function is defined for protocol-independent nodename-to-address translation.
63 It performs the functionality of
67 but in a more sophisticated manner.
71 structure is defined as a result of including the
76 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
77 int ai_family; /* PF_xxx */
78 int ai_socktype; /* SOCK_xxx */
79 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
80 size_t ai_addrlen; /* length of ai_addr */
81 char *ai_canonname; /* canonical name for nodename */
82 struct sockaddr *ai_addr; /* binary address */
83 struct addrinfo *ai_next; /* next structure in linked list */
91 arguments are pointers to NUL-terminated strings or
93 One or both of these two arguments must be a non-null pointer.
94 In the normal client scenario, both the
99 In the normal server scenario, only the
104 string can be either a node name or a numeric host address string
105 (i.e., a dotted-decimal IPv4 address or an IPv6 hex address).
108 string can be either a service name or a decimal port number.
110 The caller can optionally pass an
112 structure, pointed to by the third argument,
113 to provide hints concerning the type of socket that the caller supports.
116 structure all members other than
122 must be zero or a null pointer.
127 means the caller will accept any protocol family.
130 means the caller will accept any socket type.
133 means the caller will accept any protocol.
134 For example, if the caller handles only TCP and not UDP, then the
136 member of the hints structure should be set to
141 If the caller handles only IPv4 and not IPv6, then the
145 structure should be set to
150 If the third argument to
152 is a null pointer, this is the same as if the caller had filled in an
154 structure initialized to zero with
159 Upon successful return a pointer to a linked list of one or more
161 structures is returned through the final argument.
162 The caller can process each
164 structure in this list by following the
166 pointer, until a null pointer is encountered.
169 structure the three members
174 are the corresponding arguments for a call to the
181 member points to a filled-in socket address structure whose length is
192 structure, then the caller plans to use the returned socket address
193 structure in a call to
197 argument is a null pointer, then the IP address portion of the socket
198 address structure will be set to
200 for an IPv4 address or
206 bit is not set in the
210 structure, then the returned socket address structure will be ready for a
213 .Pq for a connection-oriented protocol
219 .Pq for a connectionless protocol .
222 argument is a null pointer, then the IP address portion of the
223 socket address structure will be set to the loopback address.
231 structure, then upon successful return the
235 structure in the linked list will point to a NUL-terminated string
236 containing the canonical name of the specified
245 structure, then a non-null
247 string must be a numeric host address string.
248 Otherwise an error of
251 This flag prevents any type of name resolution service (e.g., the DNS)
256 must sufficiently be consistent and unambiguous.
257 Here are pitfall cases you may encounter:
261 will raise an error if members of the
263 structure are not consistent.
264 For example, for internet address families,
266 will raise an error if you specify
277 which is defined only for certain
280 will raise an error because the arguments are not consistent.
283 will raise an error if you ask for
288 For internet address families, if you specify
295 will raise an error, because service names are not defined for the internet
299 If you specify a numeric
308 This is because the numeric
310 does not identify any socket type, and
312 is not allowed to glob the argument in such case.
315 All of the information returned by
317 is dynamically allocated:
320 structures, the socket address structures, and canonical node name
321 strings pointed to by the addrinfo structures.
322 To return this information to the system the function
327 structure pointed to by the
329 is freed, along with any dynamic storage pointed to by the structure.
330 This operation is repeated until a
333 pointer is encountered.
335 To aid applications in printing error messages based on the
341 The argument is one of the
343 values defined earlier and the return value points to a string describing
345 If the argument is not one of the
347 values, the function still returns a pointer to a string whose contents
348 indicate an unknown error.
350 .Ss Extension for scoped IPv6 address
351 The implementation allows experimental numeric IPv6 address notation with
353 By appending the percent character and scope identifier to addresses,
357 This would make management of scoped address easier,
358 and allows cut-and-paste input of scoped address.
360 At this moment the code supports only link-local addresses with the format.
361 Scope identifier is hardcoded to name of hardware interface associated
367 Example would be like
372 on the link associated with
377 The implementation is still very experimental and non-standard.
378 The current implementation assumes one-by-one relationship between
379 interface and link, which is not necessarily true from the specification.
382 The following code tries to connect to
387 It loops through all the addresses available, regardless from address family.
388 If the destination resolves to IPv4 address, it will use
391 Similarly, if it resolves to IPv6,
394 Observe that there is no hardcoded reference to particular address family.
395 The code works even if
397 returns addresses that are not IPv4/v6.
398 .Bd -literal -offset indent
399 struct addrinfo hints, *res, *res0;
402 const char *cause = NULL;
404 memset(&hints, 0, sizeof(hints));
405 hints.ai_family = PF_UNSPEC;
406 hints.ai_socktype = SOCK_STREAM;
407 error = getaddrinfo("www.kame.net", "http", &hints, &res0);
409 errx(1, "%s", gai_strerror(error));
413 for (res = res0; res; res = res->ai_next) {
414 s = socket(res->ai_family, res->ai_socktype,
421 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
428 break; /* okay we got one */
437 The following example tries to open a wildcard listening socket onto service
439 for all the address families available.
440 .Bd -literal -offset indent
441 struct addrinfo hints, *res, *res0;
445 const char *cause = NULL;
447 memset(&hints, 0, sizeof(hints));
448 hints.ai_family = PF_UNSPEC;
449 hints.ai_socktype = SOCK_STREAM;
450 hints.ai_flags = AI_PASSIVE;
451 error = getaddrinfo(NULL, "http", &hints, &res0);
453 errx(1, "%s", gai_strerror(error));
457 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
458 s[nsock] = socket(res->ai_family, res->ai_socktype,
465 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
470 (void) listen(s[nsock], 5);
482 Error return status from
484 is zero on success and non-zero on errors.
485 Non-zero error codes are defined in
489 .Bl -tag -width EAI_ADDRFAMILY -compact
490 .It Dv EAI_ADDRFAMILY
495 Temporary failure in name resolution.
500 Non-recoverable failure in name resolution.
505 Memory allocation failure.
507 No address associated with
513 provided, or not known.
522 System error returned in
526 If called with proper argument,
528 returns a pointer to a string describing the given error code.
529 If the argument is not one of the
531 values, the function still returns a pointer to a string whose contents
532 indicate an unknown error.
536 .Xr gethostbyname 3 ,
537 .Xr getservbyname 3 ,
548 .%T Basic Socket Interface Extensions for IPv6
555 .%T "An Extension of Format for IPv6 Scoped Addresses"
557 .%N draft-ietf-ipngwg-scopedaddr-format-02.txt
558 .%O work in progress material
562 .%T Protocol Independence Using the Sockets API
563 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
568 The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
573 function is defined in IEEE POSIX 1003.1g draft specification,
575 .Dq Basic Socket Interface Extensions for IPv6
579 The current implementation is not thread-safe.
581 The text was shamelessly copied from RFC2553.