1 .\" Copyright (c) 2007, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 2006 Ulrich Drepper <drepper@redhat.com>
3 .\" A few pieces of an earlier version remain:
4 .\" Copyright 2000, Sam Varshavchik <mrsam@courier-mta.com>
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
8 .\" References: RFC 2553
10 .\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED,
11 .\" and AI_NUMERICSERV.
12 .\" 2006-11-25, Ulrich Drepper <drepper@redhat.com>
13 .\" Add text describing Internationalized Domain Name extensions.
14 .\" 2007-06-08, mtk: added example programs
15 .\" 2008-02-26, mtk; clarify discussion of NULL 'hints' argument; other
17 .\" 2008-06-18, mtk: many parts rewritten
18 .\" 2008-12-04, Petr Baudis <pasky@suse.cz>
19 .\" Describe results ordering and reference /etc/gai.conf.
21 .\" FIXME . glibc's 2.9 NEWS file documents DCCP and UDP-lite support
22 .\" and is SCTP support now also there?
24 .TH GETADDRINFO 3 2021-08-27 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
26 getaddrinfo, freeaddrinfo, gai_strerror \- network address and
30 .RI ( libc ", " \-lc )
33 .B #include <sys/types.h>
34 .B #include <sys/socket.h>
37 .BI "int getaddrinfo(const char *restrict " node ,
38 .BI " const char *restrict " service ,
39 .BI " const struct addrinfo *restrict " hints ,
40 .BI " struct addrinfo **restrict " res );
42 .BI "void freeaddrinfo(struct addrinfo *" res );
44 .BI "const char *gai_strerror(int " errcode );
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
57 _POSIX_C_SOURCE >= 200112L
58 Glibc 2.21 and earlier:
66 which identify an Internet host and a service,
70 structures, each of which contains an Internet address
71 that can be specified in a call to
77 function combines the functionality provided by the
78 .\" .BR getipnodebyname (3),
79 .\" .BR getipnodebyaddr (3),
83 functions into a single interface, but unlike the latter functions,
85 is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
91 contains the following fields:
100 socklen_t ai_addrlen;
101 struct sockaddr *ai_addr;
103 struct addrinfo *ai_next;
110 argument points to an
112 structure that specifies criteria for selecting the socket address
113 structures returned in the list pointed to by
117 is not NULL it points to an
124 specify criteria that limit the set of socket addresses returned by
129 This field specifies the desired address family for the returned addresses.
130 Valid values for this field include
138 should return socket addresses for any address family
139 (either IPv4 or IPv6, for example) that can be used with
145 This field specifies the preferred socket type, for example
149 Specifying 0 in this field indicates that socket addresses of any type
154 This field specifies the protocol for the returned socket addresses.
155 Specifying 0 in this field indicates that socket addresses with
156 any protocol can be returned by
160 This field specifies additional options, described below.
161 Multiple flags are specified by bitwise OR-ing them together.
163 All the other fields in the structure pointed to by
165 must contain either 0 or a null pointer, as appropriate.
169 as NULL is equivalent to setting
180 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
181 (POSIX specifies different defaults for
185 specifies either a numerical network address
186 (for IPv4, numbers-and-dots notation as supported by
188 for IPv6, hexadecimal string format as supported by
190 or a network hostname, whose network addresses are looked up and resolved.
197 must be a numerical network address.
200 flag suppresses any potentially lengthy network host address lookups.
209 then the returned socket addresses will be suitable for
214 The returned socket address will contain the "wildcard address"
219 The wildcard address is used by applications (typically servers)
220 that intend to accept connections on any of the host's network addresses.
223 is not NULL, then the
231 then the returned socket addresses will be suitable for use with
239 then the network address will be set to the loopback interface address
240 .RB ( INADDR_LOOPBACK
242 .B IN6ADDR_LOOPBACK_INIT
244 this is used by applications that intend to communicate
245 with peers running on the same host.
248 sets the port in each returned address structure.
249 If this argument is a service name (see
251 it is translated to the corresponding port number.
252 This argument can also be specified as a decimal number,
253 which is simply converted to binary.
256 is NULL, then the port number of the returned socket addresses
257 will be left uninitialized.
266 must point to a string containing a numeric port number.
267 This flag is used to inhibit the invocation of a name resolution service
268 in cases where it is known not to be required.
274 but not both, may be NULL.
278 function allocates and initializes a linked list of
280 structures, one for each network address that matches
284 subject to any restrictions imposed by
286 and returns a pointer to the start of the list in
288 The items in the linked list are linked by the
292 There are several reasons why
293 the linked list may have more than one
295 structure, including: the network host is multihomed, accessible
296 over multiple protocols (e.g., both
300 or the same service is available from multiple socket types (one
304 address, for example).
305 Normally, the application should try
306 using the addresses in the order in which they are returned.
307 The sorting function used within
309 is defined in RFC\ 3484; the order can be tweaked for a particular
312 (available since glibc 2.5).
320 field of the first of the
322 structures in the returned list is set to point to the
323 official name of the host.
324 .\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
325 .\" structure was set pointing to the canonical name; that was
326 .\" more than POSIX.1-2001 specified, or other implementations provided.
329 The remaining fields of each returned
331 structure are initialized as follows:
338 fields return the socket creation parameters (i.e., these fields have
339 the same meaning as the corresponding arguments of
354 returns the protocol for the socket.
356 A pointer to the socket address is placed in the
358 field, and the length of the socket address, in bytes,
367 flag, then IPv4 addresses are returned in the list pointed to by
369 only if the local system has at least one
370 IPv4 address configured, and IPv6 addresses are returned
371 only if the local system has at least one IPv6 address configured.
372 The loopback address is not considered for this case as valid
373 as a configured address.
374 This flag is useful on, for example,
375 IPv4-only systems, to ensure that
377 does not return IPv6 socket addresses that would always fail in
390 and no matching IPv6 addresses could be found,
391 then return IPv4-mapped IPv6 addresses in the list pointed to by
399 then return both IPv6 and IPv4-mapped IPv6 addresses
400 in the list pointed to by
405 is not also specified.
409 function frees the memory that was allocated
410 for the dynamically allocated linked list
412 .SS Extensions to getaddrinfo() for Internationalized Domain Names
413 Starting with glibc 2.3.4,
415 has been extended to selectively allow the incoming and outgoing
416 hostnames to be transparently converted to and from the
417 Internationalized Domain Name (IDN) format (see RFC 3490,
418 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
419 Four new flags are defined:
422 If this flag is specified, then the node name given in
424 is converted to IDN format if necessary.
425 The source encoding is that of the current locale.
427 If the input name contains non-ASCII characters, then the IDN encoding
429 Those parts of the node name (delimited by dots) that contain
430 non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
431 before being passed to the name resolution functions.
432 .\" Implementation Detail:
433 .\" To minimize effects on system performance the implementation might
434 .\" want to check whether the input string contains any non-ASCII
435 .\" characters. If there are none the IDN step can be skipped completely.
436 .\" On systems which allow not-ASCII safe encodings for a locale this
437 .\" might be a problem.
440 After a successful name lookup, and if the
444 will return the canonical name of the
445 node corresponding to the
447 structure value passed back.
448 The return value is an exact copy of the value returned by the name
451 If the name is encoded using ACE, then it will contain the
453 prefix for one or more components of the name.
454 To convert these components into a readable form the
456 flag can be passed in addition to
458 The resulting string is encoded using the current locale's encoding.
460 .\"Implementation Detail:
461 .\"If no component of the returned name starts with xn\-\- the IDN
462 .\"step can be skipped, therefore avoiding unnecessary slowdowns.
464 .BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
465 Setting these flags will enable the
466 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
467 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
469 flags respectively to be used in the IDNA handling.
471 .\" FIXME glibc defines the following additional errors, some which
472 .\" can probably be returned by getaddrinfo(); they need to
475 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
476 .\" #define EAI_CANCELED -101 /* Request canceled. */
477 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
478 .\" #define EAI_ALLDONE -103 /* All requests done. */
479 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
480 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
483 returns 0 if it succeeds, or one of the following nonzero error codes:
487 The specified network host does not have any network addresses in the
488 requested address family.
491 The name server returned a temporary failure indication.
496 contains invalid flags; or,
505 The name server returned a permanent failure indication.
508 The requested address family is not supported.
515 The specified network host exists, but does not have any
516 network addresses defined.
523 is not known; or both
533 was not a numeric port-number string.
536 The requested service is not available for the requested socket type.
537 It may be available through another socket type.
538 For example, this error could occur if
540 was "shell" (a service available only on stream sockets), and either
548 or the error could occur if
554 (a socket type that does not support the concept of services).
557 The requested socket type is not supported.
558 This could occur, for example, if
562 are inconsistent (e.g.,
571 is set to indicate the error.
575 function translates these error codes to a human readable string,
576 suitable for error reporting.
580 For an explanation of the terms used in this section, see
588 Interface Attribute Value
591 T} Thread safety MT-Safe env locale
595 T} Thread safety MT-Safe
601 POSIX.1-2001, POSIX.1-2008.
604 function is documented in RFC\ 2553.
608 .IB address % scope-id
609 notation for specifying the IPv6 scope-ID.
615 are available since glibc 2.3.3.
617 is available since glibc 2.3.4.
619 According to POSIX.1, specifying
620 .\" POSIX.1-2001, POSIX.1-2008
625 The GNU C library instead assumes a value of
626 .B (AI_V4MAPPED\~|\~AI_ADDRCONFIG)
628 since this value is considered an improvement on the specification.
630 .\" getnameinfo.3 refers to this example
631 .\" socket.2 refers to this example
632 .\" bind.2 refers to this example
633 .\" connect.2 refers to this example
634 .\" recvfrom.2 refers to this example
635 .\" sendto.2 refers to this example
636 The following programs demonstrate the use of
642 The programs are an echo server and client for UDP datagrams.
646 #include <sys/types.h>
651 #include <sys/socket.h>
657 main(int argc, char *argv[])
659 struct addrinfo hints;
660 struct addrinfo *result, *rp;
662 struct sockaddr_storage peer_addr;
663 socklen_t peer_addr_len;
668 fprintf(stderr, "Usage: %s port\en", argv[0]);
672 memset(&hints, 0, sizeof(hints));
673 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
674 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
675 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
676 hints.ai_protocol = 0; /* Any protocol */
677 hints.ai_canonname = NULL;
678 hints.ai_addr = NULL;
679 hints.ai_next = NULL;
681 s = getaddrinfo(NULL, argv[1], &hints, &result);
683 fprintf(stderr, "getaddrinfo: %s\en", gai_strerror(s));
687 /* getaddrinfo() returns a list of address structures.
688 Try each address until we successfully bind(2).
689 If socket(2) (or bind(2)) fails, we (close the socket
690 and) try the next address. */
692 for (rp = result; rp != NULL; rp = rp\->ai_next) {
693 sfd = socket(rp\->ai_family, rp\->ai_socktype,
698 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
704 freeaddrinfo(result); /* No longer needed */
706 if (rp == NULL) { /* No address succeeded */
707 fprintf(stderr, "Could not bind\en");
711 /* Read datagrams and echo them back to sender. */
714 peer_addr_len = sizeof(peer_addr);
715 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
716 (struct sockaddr *) &peer_addr, &peer_addr_len);
718 continue; /* Ignore failed request */
720 char host[NI_MAXHOST], service[NI_MAXSERV];
722 s = getnameinfo((struct sockaddr *) &peer_addr,
723 peer_addr_len, host, NI_MAXHOST,
724 service, NI_MAXSERV, NI_NUMERICSERV);
726 printf("Received %zd bytes from %s:%s\en",
727 nread, host, service);
729 fprintf(stderr, "getnameinfo: %s\en", gai_strerror(s));
731 if (sendto(sfd, buf, nread, 0,
732 (struct sockaddr *) &peer_addr,
733 peer_addr_len) != nread)
734 fprintf(stderr, "Error sending response\en");
741 #include <sys/types.h>
742 #include <sys/socket.h>
752 main(int argc, char *argv[])
754 struct addrinfo hints;
755 struct addrinfo *result, *rp;
762 fprintf(stderr, "Usage: %s host port msg...\en", argv[0]);
766 /* Obtain address(es) matching host/port. */
768 memset(&hints, 0, sizeof(hints));
769 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
770 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
772 hints.ai_protocol = 0; /* Any protocol */
774 s = getaddrinfo(argv[1], argv[2], &hints, &result);
776 fprintf(stderr, "getaddrinfo: %s\en", gai_strerror(s));
780 /* getaddrinfo() returns a list of address structures.
781 Try each address until we successfully connect(2).
782 If socket(2) (or connect(2)) fails, we (close the socket
783 and) try the next address. */
785 for (rp = result; rp != NULL; rp = rp\->ai_next) {
786 sfd = socket(rp\->ai_family, rp\->ai_socktype,
791 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
797 freeaddrinfo(result); /* No longer needed */
799 if (rp == NULL) { /* No address succeeded */
800 fprintf(stderr, "Could not connect\en");
804 /* Send remaining command\-line arguments as separate
805 datagrams, and read responses from server. */
807 for (int j = 3; j < argc; j++) {
808 len = strlen(argv[j]) + 1;
809 /* +1 for terminating null byte */
811 if (len > BUF_SIZE) {
813 "Ignoring long message in argument %d\en", j);
817 if (write(sfd, argv[j], len) != len) {
818 fprintf(stderr, "partial/failed write\en");
822 nread = read(sfd, buf, BUF_SIZE);
828 printf("Received %zd bytes: %s\en", nread, buf);
835 .\" .BR getipnodebyaddr (3),
836 .\" .BR getipnodebyname (3),
837 .BR getaddrinfo_a (3),
838 .BR gethostbyname (3),