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 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" References: RFC 2553
30 .\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED,
31 .\" and AI_NUMERICSERV.
32 .\" 2006-11-25, Ulrich Drepper <drepper@redhat.com>
33 .\" Add text describing Internationalized Domain Name extensions.
34 .\" 2007-06-08, mtk: added example programs
35 .\" 2008-02-26, mtk; clarify discussion of NULL 'hints' argument; other
37 .\" 2008-06-18, mtk: many parts rewritten
38 .\" 2008-12-04, Petr Baudis <pasky@suse.cz>
39 .\" Describe results ordering and reference /etc/gai.conf.
41 .\" FIXME . glibc's 2.9 NEWS file documents DCCP and UDP-lite support
42 .\" and is SCTP support now also there?
44 .TH GETADDRINFO 3 2021-03-22 "GNU" "Linux Programmer's Manual"
46 getaddrinfo, freeaddrinfo, gai_strerror \- network address and
50 .B #include <sys/types.h>
51 .B #include <sys/socket.h>
54 .BI "int getaddrinfo(const char *restrict " node ,
55 .BI " const char *restrict " service ,
56 .BI " const struct addrinfo *restrict " hints ,
57 .BI " struct addrinfo **restrict " res );
59 .BI "void freeaddrinfo(struct addrinfo *" res );
61 .BI "const char *gai_strerror(int " errcode );
65 Feature Test Macro Requirements for glibc (see
66 .BR feature_test_macros (7)):
74 _POSIX_C_SOURCE >= 200112L
75 Glibc 2.21 and earlier:
83 which identify an Internet host and a service,
87 structures, each of which contains an Internet address
88 that can be specified in a call to
94 function combines the functionality provided by the
95 .\" .BR getipnodebyname (3),
96 .\" .BR getipnodebyaddr (3),
100 functions into a single interface, but unlike the latter functions,
102 is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
108 contains the following fields:
117 socklen_t ai_addrlen;
118 struct sockaddr *ai_addr;
120 struct addrinfo *ai_next;
127 argument points to an
129 structure that specifies criteria for selecting the socket address
130 structures returned in the list pointed to by
134 is not NULL it points to an
141 specify criteria that limit the set of socket addresses returned by
146 This field specifies the desired address family for the returned addresses.
147 Valid values for this field include
155 should return socket addresses for any address family
156 (either IPv4 or IPv6, for example) that can be used with
162 This field specifies the preferred socket type, for example
166 Specifying 0 in this field indicates that socket addresses of any type
171 This field specifies the protocol for the returned socket addresses.
172 Specifying 0 in this field indicates that socket addresses with
173 any protocol can be returned by
177 This field specifies additional options, described below.
178 Multiple flags are specified by bitwise OR-ing them together.
180 All the other fields in the structure pointed to by
182 must contain either 0 or a null pointer, as appropriate.
186 as NULL is equivalent to setting
197 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
198 (POSIX specifies different defaults for
202 specifies either a numerical network address
203 (for IPv4, numbers-and-dots notation as supported by
205 for IPv6, hexadecimal string format as supported by
207 or a network hostname, whose network addresses are looked up and resolved.
214 must be a numerical network address.
217 flag suppresses any potentially lengthy network host address lookups.
226 then the returned socket addresses will be suitable for
231 The returned socket address will contain the "wildcard address"
236 The wildcard address is used by applications (typically servers)
237 that intend to accept connections on any of the host's network addresses.
240 is not NULL, then the
248 then the returned socket addresses will be suitable for use with
256 then the network address will be set to the loopback interface address
257 .RB ( INADDR_LOOPBACK
259 .BR IN6ADDR_LOOPBACK_INIT
261 this is used by applications that intend to communicate
262 with peers running on the same host.
265 sets the port in each returned address structure.
266 If this argument is a service name (see
268 it is translated to the corresponding port number.
269 This argument can also be specified as a decimal number,
270 which is simply converted to binary.
273 is NULL, then the port number of the returned socket addresses
274 will be left uninitialized.
283 must point to a string containing a numeric port number.
284 This flag is used to inhibit the invocation of a name resolution service
285 in cases where it is known not to be required.
291 but not both, may be NULL.
295 function allocates and initializes a linked list of
297 structures, one for each network address that matches
301 subject to any restrictions imposed by
303 and returns a pointer to the start of the list in
305 The items in the linked list are linked by the
309 There are several reasons why
310 the linked list may have more than one
312 structure, including: the network host is multihomed, accessible
313 over multiple protocols (e.g., both
317 or the same service is available from multiple socket types (one
321 address, for example).
322 Normally, the application should try
323 using the addresses in the order in which they are returned.
324 The sorting function used within
326 is defined in RFC\ 3484; the order can be tweaked for a particular
329 (available since glibc 2.5).
337 field of the first of the
339 structures in the returned list is set to point to the
340 official name of the host.
341 .\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
342 .\" structure was set pointing to the canonical name; that was
343 .\" more than POSIX.1-2001 specified, or other implementations provided.
346 The remaining fields of each returned
348 structure are initialized as follows:
355 fields return the socket creation parameters (i.e., these fields have
356 the same meaning as the corresponding arguments of
371 returns the protocol for the socket.
373 A pointer to the socket address is placed in the
375 field, and the length of the socket address, in bytes,
384 flag, then IPv4 addresses are returned in the list pointed to by
386 only if the local system has at least one
387 IPv4 address configured, and IPv6 addresses are returned
388 only if the local system has at least one IPv6 address configured.
389 The loopback address is not considered for this case as valid
390 as a configured address.
391 This flag is useful on, for example,
392 IPv4-only systems, to ensure that
394 does not return IPv6 socket addresses that would always fail in
407 and no matching IPv6 addresses could be found,
408 then return IPv4-mapped IPv6 addresses in the list pointed to by
416 then return both IPv6 and IPv4-mapped IPv6 addresses
417 in the list pointed to by
422 is not also specified.
426 function frees the memory that was allocated
427 for the dynamically allocated linked list
429 .SS Extensions to getaddrinfo() for Internationalized Domain Names
430 Starting with glibc 2.3.4,
432 has been extended to selectively allow the incoming and outgoing
433 hostnames to be transparently converted to and from the
434 Internationalized Domain Name (IDN) format (see RFC 3490,
435 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
436 Four new flags are defined:
439 If this flag is specified, then the node name given in
441 is converted to IDN format if necessary.
442 The source encoding is that of the current locale.
444 If the input name contains non-ASCII characters, then the IDN encoding
446 Those parts of the node name (delimited by dots) that contain
447 non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
448 before being passed to the name resolution functions.
449 .\" Implementation Detail:
450 .\" To minimize effects on system performance the implementation might
451 .\" want to check whether the input string contains any non-ASCII
452 .\" characters. If there are none the IDN step can be skipped completely.
453 .\" On systems which allow not-ASCII safe encodings for a locale this
454 .\" might be a problem.
457 After a successful name lookup, and if the
461 will return the canonical name of the
462 node corresponding to the
464 structure value passed back.
465 The return value is an exact copy of the value returned by the name
468 If the name is encoded using ACE, then it will contain the
470 prefix for one or more components of the name.
471 To convert these components into a readable form the
473 flag can be passed in addition to
475 The resulting string is encoded using the current locale's encoding.
477 .\"Implementation Detail:
478 .\"If no component of the returned name starts with xn\-\- the IDN
479 .\"step can be skipped, therefore avoiding unnecessary slowdowns.
481 .BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
482 Setting these flags will enable the
483 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
484 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
486 flags respectively to be used in the IDNA handling.
488 .\" FIXME glibc defines the following additional errors, some which
489 .\" can probably be returned by getaddrinfo(); they need to
492 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
493 .\" #define EAI_CANCELED -101 /* Request canceled. */
494 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
495 .\" #define EAI_ALLDONE -103 /* All requests done. */
496 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
497 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
500 returns 0 if it succeeds, or one of the following nonzero error codes:
504 The specified network host does not have any network addresses in the
505 requested address family.
508 The name server returned a temporary failure indication.
513 contains invalid flags; or,
522 The name server returned a permanent failure indication.
525 The requested address family is not supported.
532 The specified network host exists, but does not have any
533 network addresses defined.
540 is not known; or both
550 was not a numeric port-number string.
553 The requested service is not available for the requested socket type.
554 It may be available through another socket type.
555 For example, this error could occur if
557 was "shell" (a service available only on stream sockets), and either
565 or the error could occur if
571 (a socket type that does not support the concept of services).
574 The requested socket type is not supported.
575 This could occur, for example, if
579 are inconsistent (e.g.,
586 Other system error, check
592 function translates these error codes to a human readable string,
593 suitable for error reporting.
597 For an explanation of the terms used in this section, see
605 Interface Attribute Value
608 T} Thread safety MT-Safe env locale
612 T} Thread safety MT-Safe
618 POSIX.1-2001, POSIX.1-2008.
621 function is documented in RFC\ 2553.
625 .IB address % scope-id
626 notation for specifying the IPv6 scope-ID.
632 are available since glibc 2.3.3.
634 is available since glibc 2.3.4.
636 According to POSIX.1, specifying
637 .\" POSIX.1-2001, POSIX.1-2008
642 The GNU C library instead assumes a value of
643 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
645 since this value is considered an improvement on the specification.
647 .\" getnameinfo.3 refers to this example
648 .\" socket.2 refers to this example
649 .\" bind.2 refers to this example
650 .\" connect.2 refers to this example
651 .\" recvfrom.2 refers to this example
652 .\" sendto.2 refers to this example
653 The following programs demonstrate the use of
659 The programs are an echo server and client for UDP datagrams.
663 #include <sys/types.h>
668 #include <sys/socket.h>
674 main(int argc, char *argv[])
676 struct addrinfo hints;
677 struct addrinfo *result, *rp;
679 struct sockaddr_storage peer_addr;
680 socklen_t peer_addr_len;
685 fprintf(stderr, "Usage: %s port\en", argv[0]);
689 memset(&hints, 0, sizeof(hints));
690 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
691 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
692 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
693 hints.ai_protocol = 0; /* Any protocol */
694 hints.ai_canonname = NULL;
695 hints.ai_addr = NULL;
696 hints.ai_next = NULL;
698 s = getaddrinfo(NULL, argv[1], &hints, &result);
700 fprintf(stderr, "getaddrinfo: %s\en", gai_strerror(s));
704 /* getaddrinfo() returns a list of address structures.
705 Try each address until we successfully bind(2).
706 If socket(2) (or bind(2)) fails, we (close the socket
707 and) try the next address. */
709 for (rp = result; rp != NULL; rp = rp\->ai_next) {
710 sfd = socket(rp\->ai_family, rp\->ai_socktype,
715 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
721 freeaddrinfo(result); /* No longer needed */
723 if (rp == NULL) { /* No address succeeded */
724 fprintf(stderr, "Could not bind\en");
728 /* Read datagrams and echo them back to sender. */
731 peer_addr_len = sizeof(peer_addr);
732 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
733 (struct sockaddr *) &peer_addr, &peer_addr_len);
735 continue; /* Ignore failed request */
737 char host[NI_MAXHOST], service[NI_MAXSERV];
739 s = getnameinfo((struct sockaddr *) &peer_addr,
740 peer_addr_len, host, NI_MAXHOST,
741 service, NI_MAXSERV, NI_NUMERICSERV);
743 printf("Received %zd bytes from %s:%s\en",
744 nread, host, service);
746 fprintf(stderr, "getnameinfo: %s\en", gai_strerror(s));
748 if (sendto(sfd, buf, nread, 0,
749 (struct sockaddr *) &peer_addr,
750 peer_addr_len) != nread)
751 fprintf(stderr, "Error sending response\en");
758 #include <sys/types.h>
759 #include <sys/socket.h>
769 main(int argc, char *argv[])
771 struct addrinfo hints;
772 struct addrinfo *result, *rp;
779 fprintf(stderr, "Usage: %s host port msg...\en", argv[0]);
783 /* Obtain address(es) matching host/port. */
785 memset(&hints, 0, sizeof(hints));
786 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
787 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
789 hints.ai_protocol = 0; /* Any protocol */
791 s = getaddrinfo(argv[1], argv[2], &hints, &result);
793 fprintf(stderr, "getaddrinfo: %s\en", gai_strerror(s));
797 /* getaddrinfo() returns a list of address structures.
798 Try each address until we successfully connect(2).
799 If socket(2) (or connect(2)) fails, we (close the socket
800 and) try the next address. */
802 for (rp = result; rp != NULL; rp = rp\->ai_next) {
803 sfd = socket(rp\->ai_family, rp\->ai_socktype,
808 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
814 freeaddrinfo(result); /* No longer needed */
816 if (rp == NULL) { /* No address succeeded */
817 fprintf(stderr, "Could not connect\en");
821 /* Send remaining command\-line arguments as separate
822 datagrams, and read responses from server. */
824 for (int j = 3; j < argc; j++) {
825 len = strlen(argv[j]) + 1;
826 /* +1 for terminating null byte */
828 if (len > BUF_SIZE) {
830 "Ignoring long message in argument %d\en", j);
834 if (write(sfd, argv[j], len) != len) {
835 fprintf(stderr, "partial/failed write\en");
839 nread = read(sfd, buf, BUF_SIZE);
845 printf("Received %zd bytes: %s\en", nread, buf);
852 .\" .BR getipnodebyaddr (3),
853 .\" .BR getipnodebyname (3),
854 .BR getaddrinfo_a (3),
855 .BR gethostbyname (3),