1 .\" $FreeBSD: src/lib/libc/net/getaddrinfo.3,v 1.2.2.8 2001/08/17 15:42:38 ru Exp $
2 .\" $KAME: getaddrinfo.3,v 1.22 2000/08/09 21:16:17 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
50 .Fd #include <sys/types.h>
51 .Fd #include <sys/socket.h>
52 .Fd #include <netdb.h>
54 .Fn getaddrinfo "const char *nodename" "const char *servname" \
55 "const struct addrinfo *hints" "struct addrinfo **res"
57 .Fn freeaddrinfo "struct addrinfo *ai"
59 .Fn gai_strerror "int ecode"
64 function is defined for protocol-independent nodename-to-address translation.
65 It performs the functionality of
69 but in a more sophisticated manner.
73 structure is defined as a result of including the
78 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
79 int ai_family; /* PF_xxx */
80 int ai_socktype; /* SOCK_xxx */
81 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
82 size_t ai_addrlen; /* length of ai_addr */
83 char *ai_canonname; /* canonical name for nodename */
84 struct sockaddr *ai_addr; /* binary address */
85 struct addrinfo *ai_next; /* next structure in linked list */
93 arguments are pointers to null-terminated strings or
95 One or both of these two arguments must be a
98 In the normal client scenario, both the
103 In the normal server scenario, only the
109 string can be either a node name or a numeric host address string
110 (i.e., a dotted-decimal IPv4 address or an IPv6 hex address).
114 string can be either a service name or a decimal port number.
116 The caller can optionally pass an
118 structure, pointed to by the third argument,
119 to provide hints concerning the type of socket that the caller supports.
122 structure all members other than
135 means the caller will accept any protocol family.
138 means the caller will accept any socket type.
141 means the caller will accept any protocol.
142 For example, if the caller handles only TCP and not UDP, then the
144 member of the hints structure should be set to
149 If the caller handles only IPv4 and not IPv6, then the
153 structure should be set to
158 If the third argument to
162 pointer, this is the same as if the caller had filled in an
164 structure initialized to zero with
169 Upon successful return a pointer to a linked list of one or more
171 structures is returned through the final argument.
172 The caller can process each
174 structure in this list by following the
178 pointer is encountered.
181 structure the three members
186 are the corresponding arguments for a call to the
193 member points to a filled-in socket address structure whose length is
204 structure, then the caller plans to use the returned socket address
205 structure in a call to
211 pointer, then the IP address portion of the socket
212 address structure will be set to
214 for an IPv4 address or
220 bit is not set in the
224 structure, then the returned socket address structure will be ready for a
227 (for a connection-oriented protocol)
233 (for a connectionless protocol).
238 pointer, then the IP address portion of the
239 socket address structure will be set to the loopback address.
247 structure, then upon successful return the
251 structure in the linked list will point to a null-terminated string
252 containing the canonical name of the specified
264 string must be a numeric host address string.
265 Otherwise an error of
268 This flag prevents any type of name resolution service (e.g., the DNS)
273 must be sufficiently consistent and unambiguous.
274 Here are some problem cases you may encounter:
278 will fail if the members in the
280 structure are not consistent.
281 For example, for internet address families,
283 will fail if you specify
294 which is defined only for certain
297 will fail because the arguments are not consistent.
300 will return an error if you ask for
305 For internet address families, if you specify
312 will fail, because service names are not defined for the internet
316 If you specify numeric
325 This is because the numeric
327 does not identify any socket type, and
329 is not allowed to glob the argument in such case.
332 All of the information returned by
334 is dynamically allocated:
337 structures, the socket address structures, and canonical node name
338 strings pointed to by the addrinfo structures.
339 To return this information to the system the function
344 structure pointed to by the
346 is freed, along with any dynamic storage pointed to by the structure.
347 This operation is repeated until a
350 pointer is encountered.
352 To aid applications in printing error messages based on the
358 The argument is one of the
360 values defined earlier and the return value points to a string describing
362 If the argument is not one of the
364 values, the function still returns a pointer to a string whose contents
365 indicate an unknown error.
368 This implementation supports numeric IPv6 address notation with the
369 experimental scope identifier.
370 By appending a percent sign and scope identifier to the address, you
371 can specify the value of the
373 field of the socket address.
374 This makes management of scoped address easier,
375 and allows cut-and-paste input of scoped addresses.
377 At the moment the code supports only link-local addresses in this format.
378 The scope identifier is hardcoded to name of hardware interface associated
387 on the link associated with the
392 This implementation is still very experimental and non-standard.
393 The current implementation assumes a one-to-one relationship between
394 interfaces and links, which is not necessarily true according to the
398 The following code tries to connect to
403 It loops through all the addresses available, regardless of the address family.
404 If the destination resolves to an IPv4 address, it will use an
407 Similarly, if it resolves to IPv6, an
410 Observe that there is no hardcoded reference to particular address family.
411 The code works even if
413 returns addresses that are not IPv4/v6.
414 .Bd -literal -offset indent
415 struct addrinfo hints, *res, *res0;
418 const char *cause = NULL;
420 memset(&hints, 0, sizeof(hints));
421 hints.ai_family = PF_UNSPEC;
422 hints.ai_socktype = SOCK_STREAM;
423 error = getaddrinfo("www.kame.net", "http", &hints, &res0);
425 errx(1, "%s", gai_strerror(error));
429 cause = "no addresses";
430 errno = EADDRNOTAVAIL;
431 for (res = res0; res; res = res->ai_next) {
432 s = socket(res->ai_family, res->ai_socktype,
439 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
446 break; /* okay we got one */
455 The following example tries to open a wildcard listening socket onto service
457 for all the address families available.
458 .Bd -literal -offset indent
459 struct addrinfo hints, *res, *res0;
463 const char *cause = NULL;
465 memset(&hints, 0, sizeof(hints));
466 hints.ai_family = PF_UNSPEC;
467 hints.ai_socktype = SOCK_STREAM;
468 hints.ai_flags = AI_PASSIVE;
469 error = getaddrinfo(NULL, "http", &hints, &res0);
471 errx(1, "%s", gai_strerror(error));
475 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
476 s[nsock] = socket(res->ai_family, res->ai_socktype,
483 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
489 if (listen(s[nsock], SOMAXCONN) < 0) {
505 .Bl -tag -width /etc/resolv.conf -compact
507 .It Pa /etc/host.conf
508 .It Pa /etc/resolv.conf
512 Error return status from
514 is zero on success and non-zero on errors.
515 Non-zero error codes are defined in
519 .Bl -tag -width EAI_ADDRFAMILY -compact
520 .It Dv EAI_ADDRFAMILY
525 Temporary failure in name resolution.
530 Non-recoverable failure in name resolution.
535 Memory allocation failure.
537 No address associated with
543 provided, or not known.
552 System error returned in
556 If called with an appropriate argument,
558 returns a pointer to a string describing the given error code.
559 If the argument is not one of the
561 values, the function still returns a pointer to a string whose contents
562 indicate an unknown error.
565 .Xr gethostbyname 3 ,
567 .Xr getservbyname 3 ,
578 .%T Basic Socket Interface Extensions for IPv6
585 .%T "An Extension of Format for IPv6 Scoped Addresses"
587 .%N draft-ietf-ipngwg-scopedaddr-format-02.txt
588 .%O work in progress material
592 .%T Protocol Independence Using the Sockets API
593 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
598 The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
603 function is defined in
606 .Dq Basic Socket Interface Extensions for IPv6
610 The current implementation is not thread-safe.
612 The text was shamelessly copied from RFC2553.