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 2010-09-27 "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:
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.
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"
414 Starting with glibc 2.3.4,
416 has been extended to selectively allow the incoming and outgoing
417 hostnames to be transparently converted to and from the
418 Internationalized Domain Name (IDN) format (see RFC 3490,
419 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
420 Four new flags are defined:
423 If this flag is specified, then the node name given in
425 is converted to IDN format if necessary.
426 The source encoding is that of the current locale.
428 If the input name contains non-ASCII characters, then the IDN encoding
430 Those parts of the node name (delimited by dots) that contain
431 non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
432 before being passed to the name resolution functions.
433 .\" Implementation Detail:
434 .\" To minimize effects on system performance the implementation might
435 .\" want to check whether the input string contains any non-ASCII
436 .\" characters. If there are none the IDN step can be skipped completely.
437 .\" On systems which allow not-ASCII safe encodings for a locale this
438 .\" might be a problem.
441 After a successful name lookup, and if the
445 will return the canonical name of the
446 node corresponding to the
448 structure value passed back.
449 The return value is an exact copy of the value returned by the name
452 If the name is encoded using ACE, then it will contain the
454 prefix for one or more components of the name.
455 To convert these components into a readable form the
457 flag can be passed in addition to
459 The resulting string is encoded using the current locale's encoding.
461 .\"Implementation Detail:
462 .\"If no component of the returned name starts with xn\-\- the IDN
463 .\"step can be skipped, therefore avoiding unnecessary slowdowns.
465 .BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
466 Setting these flags will enable the
467 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
468 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
470 flags respectively to be used in the IDNA handling.
472 .\" FIXME glibc defines the following additional errors, some which
473 .\" can probably be returned by getaddrinfo(); they need to
476 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
477 .\" #define EAI_CANCELED -101 /* Request canceled. */
478 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
479 .\" #define EAI_ALLDONE -103 /* All requests done. */
480 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
481 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
484 returns 0 if it succeeds, or one of the following nonzero error codes:
488 The specified network host does not have any network addresses in the
489 requested address family.
492 The name server returned a temporary failure indication.
497 contains invalid flags; or,
506 The name server returned a permanent failure indication.
509 The requested address family is not supported.
516 The specified network host exists, but does not have any
517 network addresses defined.
524 is not known; or both
534 was not a numeric port-number string.
537 The requested service is not available for the requested socket type.
538 It may be available through another socket type.
539 For example, this error could occur if
541 was "shell" (a service only available on stream sockets), and either
549 or the error could occur if
555 (a socket type that does not support the concept of services).
558 The requested socket type is not supported.
559 This could occur, for example, if
563 are inconsistent (e.g.,
570 Other system error, check
576 function translates these error codes to a human readable string,
577 suitable for error reporting.
584 function is documented in RFC\ 2553.
588 .IB address % scope-id
589 notation for specifying the IPv6 scope-ID.
595 are available since glibc 2.3.3.
597 is available since glibc 2.3.4.
599 According to POSIX.1-2001, specifying
604 The GNU C library instead assumes a value of
605 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)"
607 since this value is considered an improvement on the specification.
609 .\" getnameinfo.3 refers to this example
610 .\" socket.2 refers to this example
611 .\" bind.2 refers to this example
612 .\" connect.2 refers to this example
613 .\" recvfrom.2 refers to this example
614 .\" sendto.2 refers to this example
615 The following programs demonstrate the use of
621 The programs are an echo server and client for UDP datagrams.
625 #include <sys/types.h>
630 #include <sys/socket.h>
636 main(int argc, char *argv[])
638 struct addrinfo hints;
639 struct addrinfo *result, *rp;
641 struct sockaddr_storage peer_addr;
642 socklen_t peer_addr_len;
647 fprintf(stderr, "Usage: %s port\\n", argv[0]);
651 memset(&hints, 0, sizeof(struct addrinfo));
652 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
653 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
654 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
655 hints.ai_protocol = 0; /* Any protocol */
656 hints.ai_canonname = NULL;
657 hints.ai_addr = NULL;
658 hints.ai_next = NULL;
660 s = getaddrinfo(NULL, argv[1], &hints, &result);
662 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
666 /* getaddrinfo() returns a list of address structures.
667 Try each address until we successfully bind(2).
668 If socket(2) (or bind(2)) fails, we (close the socket
669 and) try the next address. */
671 for (rp = result; rp != NULL; rp = rp\->ai_next) {
672 sfd = socket(rp\->ai_family, rp\->ai_socktype,
677 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
683 if (rp == NULL) { /* No address succeeded */
684 fprintf(stderr, "Could not bind\\n");
688 freeaddrinfo(result); /* No longer needed */
690 /* Read datagrams and echo them back to sender */
693 peer_addr_len = sizeof(struct sockaddr_storage);
694 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
695 (struct sockaddr *) &peer_addr, &peer_addr_len);
697 continue; /* Ignore failed request */
699 char host[NI_MAXHOST], service[NI_MAXSERV];
701 s = getnameinfo((struct sockaddr *) &peer_addr,
702 peer_addr_len, host, NI_MAXHOST,
703 service, NI_MAXSERV, NI_NUMERICSERV);
705 printf("Received %ld bytes from %s:%s\\n",
706 (long) nread, host, service);
708 fprintf(stderr, "getnameinfo: %s\\n", gai_strerror(s));
710 if (sendto(sfd, buf, nread, 0,
711 (struct sockaddr *) &peer_addr,
712 peer_addr_len) != nread)
713 fprintf(stderr, "Error sending response\\n");
720 #include <sys/types.h>
721 #include <sys/socket.h>
731 main(int argc, char *argv[])
733 struct addrinfo hints;
734 struct addrinfo *result, *rp;
741 fprintf(stderr, "Usage: %s host port msg...\\n", argv[0]);
745 /* Obtain address(es) matching host/port */
747 memset(&hints, 0, sizeof(struct addrinfo));
748 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
749 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
751 hints.ai_protocol = 0; /* Any protocol */
753 s = getaddrinfo(argv[1], argv[2], &hints, &result);
755 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
759 /* getaddrinfo() returns a list of address structures.
760 Try each address until we successfully connect(2).
761 If socket(2) (or connect(2)) fails, we (close the socket
762 and) try the next address. */
764 for (rp = result; rp != NULL; rp = rp\->ai_next) {
765 sfd = socket(rp\->ai_family, rp\->ai_socktype,
770 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
776 if (rp == NULL) { /* No address succeeded */
777 fprintf(stderr, "Could not connect\\n");
781 freeaddrinfo(result); /* No longer needed */
783 /* Send remaining command\-line arguments as separate
784 datagrams, and read responses from server */
786 for (j = 3; j < argc; j++) {
787 len = strlen(argv[j]) + 1;
788 /* +1 for terminating null byte */
790 if (len + 1 > BUF_SIZE) {
792 "Ignoring long message in argument %d\\n", j);
796 if (write(sfd, argv[j], len) != len) {
797 fprintf(stderr, "partial/failed write\\n");
801 nread = read(sfd, buf, BUF_SIZE);
807 printf("Received %ld bytes: %s\\n", (long) nread, buf);
814 .\" .BR getipnodebyaddr (3),
815 .\" .BR getipnodebyname (3),
816 .BR getaddrinfo_a (3),
817 .BR gethostbyname (3),