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 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References: RFC 2553
28 .\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED,
29 .\" and AI_NUMERICSERV.
30 .\" 2006-11-25, Ulrich Drepper <drepper@redhat.com>
31 .\" Add text describing Internationalized Domain Name extensions.
32 .\" 2007-06-08, mtk: added example programs
33 .\" 2008-02-26, mtk; clarify discussion of NULL 'hints' argument; other
35 .\" 2008-06-18, mtk: many parts rewritten
36 .\" 2008-12-04, Petr Baudis <pasky@suse.cz>
37 .\" Describe results ordering and reference /etc/gai.conf.
38 .\" FIXME . glibc's 2.9 NEWS file documents DCCP and UDP-lite support
39 .\" and is SCTP support now also there?
41 .TH GETADDRINFO 3 2013-01-15 "GNU" "Linux Programmer's Manual"
43 getaddrinfo, freeaddrinfo, gai_strerror \- network address and
47 .B #include <sys/types.h>
48 .B #include <sys/socket.h>
51 .BI "int getaddrinfo(const char *" "node" ", const char *" "service" ,
52 .BI " const struct addrinfo *" "hints" ,
53 .BI " struct addrinfo **" "res" );
55 .BI "void freeaddrinfo(struct addrinfo *" "res" );
57 .BI "const char *gai_strerror(int " "errcode" );
61 Feature Test Macro Requirements for glibc (see
62 .BR feature_test_macros (7)):
70 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
78 which identify an Internet host and a service,
82 structures, each of which contains an Internet address
83 that can be specified in a call to
89 function combines the functionality provided by the
90 .\" .BR getipnodebyname (3),
91 .\" .BR getipnodebyaddr (3),
95 functions into a single interface, but unlike the latter functions,
97 is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
103 contains the following fields:
112 socklen_t ai_addrlen;
113 struct sockaddr *ai_addr;
115 struct addrinfo *ai_next;
122 argument points to an
124 structure that specifies criteria for selecting the socket address
125 structures returned in the list pointed to by
129 is not NULL it points to an
136 specify criteria that limit the set of socket addresses returned by
141 This field specifies the desired address family for the returned addresses.
142 Valid values for this field include
150 should return socket addresses for any address family
151 (either IPv4 or IPv6, for example) that can be used with
157 This field specifies the preferred socket type, for example
161 Specifying 0 in this field indicates that socket addresses of any type
166 This field specifies the protocol for the returned socket addresses.
167 Specifying 0 in this field indicates that socket addresses with
168 any protocol can be returned by
172 This field specifies additional options, described below.
173 Multiple flags are specified by bitwise OR-ing them together.
175 All the other fields in the structure pointed to by
177 must contain either 0 or a NULL pointer, as appropriate.
180 as NULL is equivalent to setting
191 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
194 specifies either a numerical network address
195 (for IPv4, numbers-and-dots notation as supported by
197 for IPv6, hexadecimal string format as supported by
199 or a network hostname, whose network addresses are looked up and resolved.
206 must be a numerical network address.
209 flag suppresses any potentially lengthy network host address lookups.
218 then the returned socket addresses will be suitable for
223 The returned socket address will contain the "wildcard address"
228 The wildcard address is used by applications (typically servers)
229 that intend to accept connections on any of the hosts's network addresses.
232 is not NULL, then the
240 then the returned socket addresses will be suitable for use with
248 then the network address will be set to the loopback interface address
249 .RB ( INADDR_LOOPBACK
251 .BR IN6ADDR_LOOPBACK_INIT
253 this is used by applications that intend to communicate
254 with peers running on the same host.
257 sets the port in each returned address structure.
258 If this argument is a service name (see
260 it is translated to the corresponding port number.
261 This argument can also be specified as a decimal number,
262 which is simply converted to binary.
265 is NULL, then the port number of the returned socket addresses
266 will be left uninitialized.
275 must point to a string containing a numeric port number.
276 This flag is used to inhibit the invocation of a name resolution service
277 in cases where it is known not to be required.
283 but not both, may be NULL.
287 function allocates and initializes a linked list of
289 structures, one for each network address that matches
293 subject to any restrictions imposed by
295 and returns a pointer to the start of the list in
297 The items in the linked list are linked by the
301 There are several reasons why
302 the linked list may have more than one
304 structure, including: the network host is multihomed, accessible
305 over multiple protocols (e.g., both
309 or the same service is available from multiple socket types (one
313 address, for example).
314 Normally, the application should try
315 using the addresses in the order in which they are returned.
316 The sorting function used within
318 is defined in RFC\ 3484; the order can be tweaked for a particular
321 (available since glibc 2.5).
329 field of the first of the
331 structures in the returned list is set to point to the
332 official name of the host.
333 .\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
334 .\" structure was set pointing to the canonical name; that was
335 .\" more than POSIX.1-2001 specified, or other implementations provided.
338 The remaining fields of each returned
340 structure are initialized as follows:
347 fields return the socket creation parameters (i.e., these fields have
348 the same meaning as the corresponding arguments of
363 returns the protocol for the socket.
365 A pointer to the socket address is placed in the
367 field, and the length of the socket address, in bytes,
376 flag, then IPv4 addresses are returned in the list pointed to by
378 only if the local system has at least one
379 IPv4 address configured, and IPv6 addresses are only returned
380 if the local system has at least one IPv6 address configured.
381 The loopback address is not considered for this case as valid
382 as a configured address.
392 and no matching IPv6 addresses could be found,
393 then return IPv4-mapped IPv6 addresses in the list pointed to by
401 then return both IPv6 and IPv4-mapped IPv6 addresses
402 in the list pointed to by
407 is not also specified.
411 function frees the memory that was allocated
412 for the dynamically allocated linked list
414 .SS "Extensions to getaddrinfo() for Internationalized Domain Names"
416 Starting with glibc 2.3.4,
418 has been extended to selectively allow the incoming and outgoing
419 hostnames to be transparently converted to and from the
420 Internationalized Domain Name (IDN) format (see RFC 3490,
421 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
422 Four new flags are defined:
425 If this flag is specified, then the node name given in
427 is converted to IDN format if necessary.
428 The source encoding is that of the current locale.
430 If the input name contains non-ASCII characters, then the IDN encoding
432 Those parts of the node name (delimited by dots) that contain
433 non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
434 before being passed to the name resolution functions.
435 .\" Implementation Detail:
436 .\" To minimize effects on system performance the implementation might
437 .\" want to check whether the input string contains any non-ASCII
438 .\" characters. If there are none the IDN step can be skipped completely.
439 .\" On systems which allow not-ASCII safe encodings for a locale this
440 .\" might be a problem.
443 After a successful name lookup, and if the
447 will return the canonical name of the
448 node corresponding to the
450 structure value passed back.
451 The return value is an exact copy of the value returned by the name
454 If the name is encoded using ACE, then it will contain the
456 prefix for one or more components of the name.
457 To convert these components into a readable form the
459 flag can be passed in addition to
461 The resulting string is encoded using the current locale's encoding.
463 .\"Implementation Detail:
464 .\"If no component of the returned name starts with xn\-\- the IDN
465 .\"step can be skipped, therefore avoiding unnecessary slowdowns.
467 .BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
468 Setting these flags will enable the
469 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
470 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
472 flags respectively to be used in the IDNA handling.
474 .\" FIXME glibc defines the following additional errors, some which
475 .\" can probably be returned by getaddrinfo(); they need to
478 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
479 .\" #define EAI_CANCELED -101 /* Request canceled. */
480 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
481 .\" #define EAI_ALLDONE -103 /* All requests done. */
482 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
483 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
486 returns 0 if it succeeds, or one of the following nonzero error codes:
490 The specified network host does not have any network addresses in the
491 requested address family.
494 The name server returned a temporary failure indication.
499 contains invalid flags; or,
508 The name server returned a permanent failure indication.
511 The requested address family is not supported.
518 The specified network host exists, but does not have any
519 network addresses defined.
526 is not known; or both
536 was not a numeric port-number string.
539 The requested service is not available for the requested socket type.
540 It may be available through another socket type.
541 For example, this error could occur if
543 was "shell" (a service only available on stream sockets), and either
551 or the error could occur if
557 (a socket type that does not support the concept of services).
560 The requested socket type is not supported.
561 This could occur, for example, if
565 are inconsistent (e.g.,
572 Other system error, check
578 function translates these error codes to a human readable string,
579 suitable for error reporting.
586 function is documented in RFC\ 2553.
590 .IB address % scope-id
591 notation for specifying the IPv6 scope-ID.
597 are available since glibc 2.3.3.
599 is available since glibc 2.3.4.
601 According to POSIX.1-2001, specifying
606 The GNU C library instead assumes a value of
607 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
609 since this value is considered an improvement on the specification.
611 .\" getnameinfo.3 refers to this example
612 .\" socket.2 refers to this example
613 .\" bind.2 refers to this example
614 .\" connect.2 refers to this example
615 .\" recvfrom.2 refers to this example
616 .\" sendto.2 refers to this example
617 The following programs demonstrate the use of
623 The programs are an echo server and client for UDP datagrams.
627 #include <sys/types.h>
632 #include <sys/socket.h>
638 main(int argc, char *argv[])
640 struct addrinfo hints;
641 struct addrinfo *result, *rp;
643 struct sockaddr_storage peer_addr;
644 socklen_t peer_addr_len;
649 fprintf(stderr, "Usage: %s port\\n", argv[0]);
653 memset(&hints, 0, sizeof(struct addrinfo));
654 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
655 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
656 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
657 hints.ai_protocol = 0; /* Any protocol */
658 hints.ai_canonname = NULL;
659 hints.ai_addr = NULL;
660 hints.ai_next = NULL;
662 s = getaddrinfo(NULL, argv[1], &hints, &result);
664 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
668 /* getaddrinfo() returns a list of address structures.
669 Try each address until we successfully bind(2).
670 If socket(2) (or bind(2)) fails, we (close the socket
671 and) try the next address. */
673 for (rp = result; rp != NULL; rp = rp\->ai_next) {
674 sfd = socket(rp\->ai_family, rp\->ai_socktype,
679 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
685 if (rp == NULL) { /* No address succeeded */
686 fprintf(stderr, "Could not bind\\n");
690 freeaddrinfo(result); /* No longer needed */
692 /* Read datagrams and echo them back to sender */
695 peer_addr_len = sizeof(struct sockaddr_storage);
696 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
697 (struct sockaddr *) &peer_addr, &peer_addr_len);
699 continue; /* Ignore failed request */
701 char host[NI_MAXHOST], service[NI_MAXSERV];
703 s = getnameinfo((struct sockaddr *) &peer_addr,
704 peer_addr_len, host, NI_MAXHOST,
705 service, NI_MAXSERV, NI_NUMERICSERV);
707 printf("Received %ld bytes from %s:%s\\n",
708 (long) nread, host, service);
710 fprintf(stderr, "getnameinfo: %s\\n", gai_strerror(s));
712 if (sendto(sfd, buf, nread, 0,
713 (struct sockaddr *) &peer_addr,
714 peer_addr_len) != nread)
715 fprintf(stderr, "Error sending response\\n");
722 #include <sys/types.h>
723 #include <sys/socket.h>
733 main(int argc, char *argv[])
735 struct addrinfo hints;
736 struct addrinfo *result, *rp;
743 fprintf(stderr, "Usage: %s host port msg...\\n", argv[0]);
747 /* Obtain address(es) matching host/port */
749 memset(&hints, 0, sizeof(struct addrinfo));
750 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
751 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
753 hints.ai_protocol = 0; /* Any protocol */
755 s = getaddrinfo(argv[1], argv[2], &hints, &result);
757 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
761 /* getaddrinfo() returns a list of address structures.
762 Try each address until we successfully connect(2).
763 If socket(2) (or connect(2)) fails, we (close the socket
764 and) try the next address. */
766 for (rp = result; rp != NULL; rp = rp\->ai_next) {
767 sfd = socket(rp\->ai_family, rp\->ai_socktype,
772 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
778 if (rp == NULL) { /* No address succeeded */
779 fprintf(stderr, "Could not connect\\n");
783 freeaddrinfo(result); /* No longer needed */
785 /* Send remaining command\-line arguments as separate
786 datagrams, and read responses from server */
788 for (j = 3; j < argc; j++) {
789 len = strlen(argv[j]) + 1;
790 /* +1 for terminating null byte */
792 if (len + 1 > BUF_SIZE) {
794 "Ignoring long message in argument %d\\n", j);
798 if (write(sfd, argv[j], len) != len) {
799 fprintf(stderr, "partial/failed write\\n");
803 nread = read(sfd, buf, BUF_SIZE);
809 printf("Received %ld bytes: %s\\n", (long) nread, buf);
816 .\" .BR getipnodebyaddr (3),
817 .\" .BR getipnodebyname (3),
818 .BR getaddrinfo_a (3),
819 .BR gethostbyname (3),