]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getaddrinfo.3
wspace in SYNOPSIS
[thirdparty/man-pages.git] / man3 / getaddrinfo.3
CommitLineData
fea681da 1.\" Copyright 2000 Sam Varshavchik <mrsam@courier-mta.com>
c11b1abf 2.\" and Copyright (c) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
3.\"
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\"
24.\" References: RFC 2553
8d18d6ed 25.\"
c13182ef 26.\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED,
8d18d6ed 27.\" and AI_NUMERICSERV.
35bf3cc8 28.\" 2007-06-08, mtk: added example programs
8d18d6ed 29.\"
a5b9e015 30.TH GETADDRINFO 3 2007-11-15 "GNU" "Linux Programmer's Manual"
fea681da
MK
31.SH NAME
32getaddrinfo, freeaddrinfo, gai_strerror \- network address and service translation
33.SH SYNOPSIS
34.nf
35.B #include <sys/types.h>
36.B #include <sys/socket.h>
37.B #include <netdb.h>
38.sp
39.BI "int getaddrinfo(const char *" "node" ", const char *" "service" ,
40.BI " const struct addrinfo *" "hints" ,
41.BI " struct addrinfo **" "res" );
42.sp
43.BI "void freeaddrinfo(struct addrinfo *" "res" );
44.sp
45.BI "const char *gai_strerror(int " "errcode" );
46.fi
47.SH DESCRIPTION
48The
2777b1ca 49.BR getaddrinfo ()
fea681da 50function combines the functionality provided by the
823ecd0c
MK
51.\" .BR getipnodebyname (3),
52.\" .BR getipnodebyaddr (3),
38f76cd2 53.BR getservbyname (3)
fea681da
MK
54and
55.BR getservbyport (3)
56functions into a single interface.
57The thread-safe
2777b1ca 58.BR getaddrinfo ()
35bf3cc8
MK
59function creates one or more socket address structures
60that can be used by the
fea681da
MK
61.BR bind (2)
62and
63.BR connect (2)
64system calls to create a client or a server socket.
65.PP
66The
2777b1ca 67.BR getaddrinfo ()
fea681da
MK
68function is not limited to creating IPv4 socket address structures;
69IPv6 socket address structures can be created if IPv6 support is available.
70These socket address structures can be used directly by
71.BR bind (2)
72or
73.BR connect (2),
74to prepare a client or a server socket.
75.PP
76The
8478ee02 77.I addrinfo
fea681da
MK
78structure used by this function contains the following members:
79.sp
80.nf
89f9f5b2
MK
81struct addrinfo {
82 int ai_flags;
83 int ai_family;
84 int ai_socktype;
85 int ai_protocol;
86 size_t ai_addrlen;
87 struct sockaddr *ai_addr;
88 char *ai_canonname;
89 struct addrinfo *ai_next;
90};
fea681da
MK
91.fi
92.PP
2777b1ca 93.BR getaddrinfo ()
fea681da
MK
94sets
95.I res
3f624b93 96to point to a dynamically allocated linked list of
8478ee02 97.I addrinfo
fea681da
MK
98structures, linked by the
99.I ai_next
100member.
101There are several reasons why
8194de33 102the linked list may have more than one
8478ee02 103.I addrinfo
fea681da
MK
104structure, including: if the network host is
105multi-homed; or if the same service
106is available from multiple socket protocols (one
107.B SOCK_STREAM
c13182ef 108address and another
fea681da
MK
109.B SOCK_DGRAM
110address, for example).
111.PP
112The members
113.IR ai_family ,
114.IR ai_socktype ,
115and
116.I ai_protocol
117have the same meaning as the corresponding parameters in the
118.BR socket (2)
119system call.
120The
2777b1ca 121.BR getaddrinfo ()
fea681da
MK
122function returns socket addresses in either IPv4 or IPv6
123address family,
124.RI "(" "ai_family"
125will be set to either
8d18d6ed 126.B AF_INET
fea681da 127or
8d18d6ed 128.BR AF_INET6 ).
fea681da
MK
129.PP
130The
131.I hints
132parameter specifies
133the preferred socket type, or protocol.
134A NULL
135.I hints
136specifies that any network address or protocol is acceptable.
8d18d6ed 137If this parameter is not NULL it points to an
8478ee02 138.I addrinfo
fea681da
MK
139structure
140whose
141.IR ai_family ,
142.IR ai_socktype ,
143and
144.I ai_protocol
145members specify the preferred socket type.
8d18d6ed 146.B AF_UNSPEC
fea681da
MK
147in
148.I ai_family
149specifies any protocol family (either IPv4 or IPv6, for example).
1500 in
151.I ai_socktype
152or
153.I ai_protocol
154specifies that any socket type or protocol is acceptable as well.
155The
156.I ai_flags
157member
158specifies additional options, defined below.
159Multiple flags are specified by logically OR-ing them together.
160All the other members in the
161.I hints
162parameter must contain either 0, or a null pointer.
163.PP
164The
165.I node
166or
167.I service
168parameter, but not both, may be NULL.
169.I node
170specifies either a numerical network address
171(dotted-decimal format for IPv4, hexadecimal format for IPv6)
172or a network hostname, whose network addresses are looked up and resolved.
8d18d6ed
MK
173If
174.I hints.ai_flags
175contains the
fea681da
MK
176.B AI_NUMERICHOST
177flag then the
178.I node
179parameter must be a numerical network address.
180The
181.B AI_NUMERICHOST
182flag suppresses any potentially lengthy network host address lookups.
183.PP
184The
2777b1ca 185.BR getaddrinfo ()
8194de33 186function creates a linked list of
8478ee02 187.I addrinfo
fea681da
MK
188structures, one for each network address subject to any restrictions
189imposed by the
190.I hints
191parameter.
8194de33 192The
fea681da 193.I ai_canonname
c13182ef
MK
194field of the first of these
195.I addrinfo
8194de33 196structures is set to point to the official name of the host, if
8d18d6ed 197.I hints.ai_flags
fea681da
MK
198includes the
199.B AI_CANONNAME
200flag.
c13182ef 201.\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
8194de33 202.\" structure was set pointing to the canonical name; that was
c13182ef 203.\" more than POSIX.1-2001 specified, or other implementations provided.
8194de33 204.\" MTK, Aug 05
fea681da
MK
205.IR ai_family ,
206.IR ai_socktype ,
207and
208.I ai_protocol
209specify the socket creation parameters.
210A pointer to the socket address is placed in the
211.I ai_addr
212member, and the length of the socket address, in bytes,
213is placed in the
214.I ai_addrlen
215member.
216.PP
217If
218.I node
219is NULL,
220the
221network address in each socket structure is initialized according to the
222.B AI_PASSIVE
8d18d6ed
MK
223flag, which is set in
224.IR hints.ai_flags .
fea681da
MK
225The network address in each socket structure will be left unspecified
226if
227.B AI_PASSIVE
228flag is set.
229This is used by server applications, which intend to accept
230client connections on any network address.
231The network address will be set to the loopback interface address
232if the
233.B AI_PASSIVE
234flag is not set.
235This is used by client applications, which intend to connect
236to a server running on the same network host.
237.PP
8d18d6ed
MK
238If
239.I hints.ai_flags
240includes the
241.B AI_ADDRCONFIG
242flag, then IPv4 addresses are returned in the list pointed to by
243.I result
c13182ef
MK
244only if the local system has at least one
245IPv4 address configured, and IPv6 addresses are only returned
8d18d6ed
MK
246if the local system has at least one IPv6 address configured.
247.PP
248If
249.I hint.ai_flags
250specifies the
251.B AI_V4MAPPED
252flag, and
253.I hints.ai_family
254was specified as
255.BR AF_INET6 ,
256and no matching IPv6 addresses could be found,
257then return IPv4-mapped IPv6 addresses in the list pointed to by
258.IR result .
259If both
260.B AI_V4MAPPED
261and
262.B AI_ALL
263are specified in
264.IR hints.ai_family ,
c13182ef 265then return both IPv6 and IPv4-mapped IPv6 addresses
8d18d6ed
MK
266in the list pointed to by
267.IR result .
268.B AI_ALL
269is ignored if
270.B AI_V4MAPPED
271is not also specified.
272.PP
fea681da
MK
273.I service
274sets the port number in the network address of each socket structure.
275If
276.I service
277is NULL the port number will be left uninitialized.
8d18d6ed
MK
278If
279.B AI_NUMERICSERV
280is specified in
0daa9e92 281.I hints.ai_flags
8d18d6ed
MK
282and
283.I service
c13182ef 284is not NULL, then
8d18d6ed
MK
285.I service
286must point to a string containing a numeric port number.
287This flag is used to inhibit the invocation of a name resolution service
288in cases where it is known not to be required.
fea681da
MK
289.PP
290The
2777b1ca 291.BR freeaddrinfo ()
fea681da 292function frees the memory that was allocated
8194de33 293for the dynamically allocated linked list
fea681da 294.IR res .
22135cad
MK
295.SS "Extensions to getaddrinfo() for Internationalized Domain Names"
296.PP
297Starting with glibc 2.3.4,
c13182ef
MK
298.BR getaddrinfo ()
299has been extended to selectively allow the incoming and outgoing
300host names to be transparently converted to and from the
22135cad
MK
301Internationalized Domain Name (IDN) format (see RFC 3490,
302.IR "Internationalizing Domain Names in Applications (IDNA)" ).
303Four new flags are defined:
304.TP
305.B AI_IDN
306If this flag is specified, then the node name given in
307.I node
308is converted to IDN format if necessary.
309The source encoding is that of the current locale.
310
c13182ef
MK
311If the input name contains non-ASCII characters, then the IDN encoding
312is used.
22135cad 313Those parts of the node name (delimited by dots) that contain
c13182ef 314non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
22135cad
MK
315before being passed to the name resolution functions.
316.\" Implementation Detail:
317.\" To minimize effects on system performance the implementation might
318.\" want to check whether the input string contains any non-ASCII
319.\" characters. If there are none the IDN step can be skipped completely.
320.\" On systems which allow not-ASCII safe encodings for a locale this
321.\" might be a problem.
322.TP
323.B AI_CANONIDN
c13182ef
MK
324After a successful name lookup, and if the
325.B AI_CANONNAME
22135cad
MK
326flag was specified,
327.BR getaddrinfo ()
328will return the canonical name of the
c13182ef
MK
329node corresponding to the
330.I addrinfo
22135cad
MK
331structure value passed back.
332The return value is an exact copy of the value returned by the name
333resolution function.
334
c13182ef 335If the name is encoded using ACE, then it will contain the
94e9d9fe 336.I xn\-\-
c13182ef
MK
337prefix for one or more components of the name.
338To convert these components into a readable form the
339.B AI_CANONIDN
340flag can be passed in addition to
341.BR AI_CANONNAME .
22135cad
MK
342The resulting string is encoded using the current locale's encoding.
343.\"
344.\"Implementation Detail:
94e9d9fe 345.\"If no component of the returned name starts with xn\-\- the IDN
22135cad
MK
346.\"step can be skipped, therefore avoiding unnecessary slowdowns.
347.TP
348.BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
c13182ef 349Setting these flags will enable the
22135cad 350IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
c13182ef
MK
351IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
352conforming host name)
22135cad 353flags respectively to be used in the IDNA handling.
fea681da 354.SH "RETURN VALUE"
d5da3c8d 355.\" FIXME glibc defines the following additional errors, some which
c13182ef 356.\" can probably be returned by getaddrinfo(); they need to
22135cad 357.\" be documented.
3d32fee8
MK
358.\" #ifdef __USE_GNU
359.\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
360.\" #define EAI_CANCELED -101 /* Request canceled. */
361.\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
362.\" #define EAI_ALLDONE -103 /* All requests done. */
363.\" #define EAI_INTR -104 /* Interrupted by a signal. */
364.\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
365.\" #endif
2777b1ca 366.BR getaddrinfo ()
fea681da
MK
367returns 0 if it succeeds, or one of the following non-zero error codes:
368.TP
a0aa388d
MK
369.B EAI_ADDRFAMILY
370The specified network host does not have any network addresses in the
371requested address family.
fea681da 372.TP
a0aa388d
MK
373.B EAI_AGAIN
374The name server returned a temporary failure indication.
375Try again later.
fea681da
MK
376.TP
377.B EAI_BADFLAGS
378.I ai_flags
379contains invalid flags.
380.TP
a0aa388d
MK
381.B EAI_FAIL
382The name server returned a permanent failure indication.
383.TP
384.B EAI_FAMILY
385The requested address family is not supported at all.
386.TP
387.B EAI_MEMORY
388Out of memory.
389.TP
390.B EAI_NODATA
391The specified network host exists, but does not have any
392network addresses defined.
393.TP
fea681da
MK
394.B EAI_NONAME
395The
396.I node
397or
398.I service
8d18d6ed 399is not known; or both
fea681da
MK
400.I node
401and
402.I service
8d18d6ed
MK
403are NULL; or
404.B AI_NUMERICSERV
405was specified in
406.I hints.ai_flags
c13182ef 407and
8d18d6ed
MK
408.I service
409was not a numeric port-number string.
fea681da
MK
410.TP
411.B EAI_SERVICE
412The requested service is not available for the requested socket type.
413It may be available through another socket type.
414.TP
a0aa388d
MK
415.B EAI_SOCKTYPE
416The requested socket type is not supported at all.
fea681da
MK
417.TP
418.B EAI_SYSTEM
419Other system error, check
420.I errno
421for details.
422.PP
423The
2777b1ca 424.BR gai_strerror ()
fea681da
MK
425function translates these error codes to a human readable string,
426suitable for error reporting.
427.SH "CONFORMING TO"
68e1685c 428POSIX.1-2001.
fea681da 429The
63aa9df0 430.BR getaddrinfo ()
331da7c3 431function is documented in RFC\ 2553.
8d18d6ed
MK
432.SH "NOTES"
433.BR AI_ADDRCONFIG ,
d63f46e1 434.BR AI_ALL ,
8d18d6ed 435and
0daa9e92 436.B AI_V4MAPPED
8d18d6ed 437are available since glibc 2.3.3.
0daa9e92 438.B AI_NUMERICSERV
8d18d6ed 439is available since glibc 2.3.4.
35bf3cc8 440.SH EXAMPLE
88a180b7
MK
441.\" getnameinfo.3 refers to this example
442.\" socket.2 refers to this example
443.\" bind.2 refers to this example
444.\" connect.2 refers to this example
445.\" recvfrom.2 refers to this example
446.\" sendto.2 refers to this example
35bf3cc8
MK
447The following programs demonstrate the use of
448.BR getaddrinfo (),
449.BR gai_strerror (),
450.BR freeaddrinfo (),
451and
452.BR getnameinfo (3).
453The programs are an echo server and client for UDP datagrams.
454
455This is the server:
456.in +0.25i
457.nf
458
988db661 459#include <sys/types.h>
35bf3cc8 460#include <stdio.h>
988db661
MK
461#include <stdlib.h>
462#include <unistd.h>
463#include <string.h>
464#include <sys/socket.h>
35bf3cc8
MK
465#include <netdb.h>
466
467#define BUF_SIZE 500
468
469int
470main(int argc, char *argv[])
471{
472 struct addrinfo hints;
473 struct addrinfo *result, *rp;
474 int sfd, s;
475 struct sockaddr_storage peer_addr;
476 socklen_t peer_addr_len;
477 ssize_t nread;
478 char buf[BUF_SIZE];
479
480 if (argc != 2) {
481 fprintf(stderr, "Usage: %s port\\n", argv[0]);
482 exit(EXIT_FAILURE);
483 }
484
485 memset(&hints, 0, sizeof(struct addrinfo));
486 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
487 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
488 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
489 hints.ai_protocol = 0; /* Any protocol */
59cef123
MK
490 hints.ai_canonname = NULL;
491 hints.ai_addr = NULL;
492 hints.ai_next = NULL;
35bf3cc8
MK
493
494 s = getaddrinfo(NULL, argv[1], &hints, &result);
495 if (s != 0) {
496 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
497 exit(EXIT_FAILURE);
498 }
499
500 /* getaddrinfo() returns a list of address structures.
7a056410 501 Try each address until we successfully bind(2).
988db661 502 If socket(2) (or bind(2)) fails, we (close the socket
35bf3cc8
MK
503 and) try the next address. */
504
29059a65 505 for (rp = result; rp != NULL; rp = rp\->ai_next) {
988db661 506 sfd = socket(rp\->ai_family, rp\->ai_socktype,
29059a65
MK
507 rp\->ai_protocol);
508 if (sfd == \-1)
35bf3cc8
MK
509 continue;
510
29059a65 511 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
35bf3cc8
MK
512 break; /* Success */
513
514 close(sfd);
515 }
516
517 if (rp == NULL) { /* No address succeeded */
518 fprintf(stderr, "Could not bind\\n");
519 exit(EXIT_FAILURE);
520 }
521
522 freeaddrinfo(result); /* No longer needed */
523
524 /* Read datagrams and echo them back to sender */
525
526 for (;;) {
527 peer_addr_len = sizeof(struct sockaddr_storage);
988db661 528 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
35bf3cc8 529 (struct sockaddr *) &peer_addr, &peer_addr_len);
29059a65 530 if (nread == \-1)
35bf3cc8
MK
531 continue; /* Ignore failed request */
532
533 char host[NI_MAXHOST], service[NI_MAXSERV];
988db661
MK
534
535 s = getnameinfo((struct sockaddr *) &peer_addr,
35bf3cc8
MK
536 peer_addr_len, host, NI_MAXHOST,
537 service, NI_MAXSERV, NI_NUMERICSERV);
988db661 538 if (s == 0)
35bf3cc8
MK
539 printf("Received %ld bytes from %s:%s\\n",
540 (long) nread, host, service);
541 else
542 fprintf(stderr, "getnameinfo: %s\\n", gai_strerror(s));
543
988db661 544 if (sendto(sfd, buf, nread, 0,
35bf3cc8 545 (struct sockaddr *) &peer_addr,
988db661 546 peer_addr_len) != nread)
35bf3cc8
MK
547 fprintf(stderr, "Error sending response\\n");
548 }
549}
550.fi
551.in
552
553This is the client:
554.in +0.25i
555.nf
556
988db661 557#include <sys/types.h>
35bf3cc8
MK
558#include <sys/socket.h>
559#include <netdb.h>
560#include <stdio.h>
988db661
MK
561#include <stdlib.h>
562#include <unistd.h>
563#include <string.h>
35bf3cc8
MK
564
565#define BUF_SIZE 500
566
567int
568main(int argc, char *argv[])
569{
570 struct addrinfo hints;
571 struct addrinfo *result, *rp;
572 int sfd, s, j;
573 size_t len;
574 ssize_t nread;
575 char buf[BUF_SIZE];
576
577 if (argc < 3) {
578 fprintf(stderr, "Usage: %s host port msg...\\n", argv[0]);
579 exit(EXIT_FAILURE);
580 }
581
582 /* Obtain address(es) matching host/port */
583
584 memset(&hints, 0, sizeof(struct addrinfo));
585 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
586 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
587 hints.ai_flags = 0;
588 hints.ai_protocol = 0; /* Any protocol */
589
590 s = getaddrinfo(argv[1], argv[2], &hints, &result);
591 if (s != 0) {
592 fprintf(stderr, "getaddrinfo: %s\\n", gai_strerror(s));
593 exit(EXIT_FAILURE);
594 }
595
596 /* getaddrinfo() returns a list of address structures.
7a056410
MK
597 Try each address until we successfully connect(2).
598 If socket(2) (or connect(2)) fails, we (close the socket
35bf3cc8
MK
599 and) try the next address. */
600
29059a65 601 for (rp = result; rp != NULL; rp = rp\->ai_next) {
988db661 602 sfd = socket(rp\->ai_family, rp\->ai_socktype,
29059a65
MK
603 rp\->ai_protocol);
604 if (sfd == \-1)
35bf3cc8 605 continue;
988db661 606
29059a65 607 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
35bf3cc8
MK
608 break; /* Success */
609
610 close(sfd);
611 }
612
613 if (rp == NULL) { /* No address succeeded */
614 fprintf(stderr, "Could not connect\\n");
615 exit(EXIT_FAILURE);
616 }
617
618 freeaddrinfo(result); /* No longer needed */
619
988db661 620 /* Send remaining command\-line arguments as separate
35bf3cc8
MK
621 datagrams, and read responses from server */
622
623 for (j = 3; j < argc; j++) {
988db661 624 len = strlen(argv[j]) + 1;
35bf3cc8
MK
625 /* +1 for terminating null byte */
626
627 if (len + 1 > BUF_SIZE) {
988db661 628 fprintf(stderr,
35bf3cc8
MK
629 "Ignoring long message in argument %d\\n", j);
630 continue;
631 }
632
633 if (write(sfd, argv[j], len) != len) {
634 fprintf(stderr, "partial/failed write\\n");
635 exit(EXIT_FAILURE);
636 }
988db661 637
35bf3cc8 638 nread = read(sfd, buf, BUF_SIZE);
29059a65 639 if (nread == \-1) {
35bf3cc8
MK
640 perror("read");
641 exit(EXIT_FAILURE);
642 }
643
644 printf("Received %ld bytes: %s\\n", (long) nread, buf);
645 }
646
647 exit(EXIT_SUCCESS);
648}
649.fi
650.in
fea681da 651.SH "SEE ALSO"
823ecd0c 652.\" .BR getipnodebyaddr (3),
ac5e6a36 653.\" .BR getipnodebyname (3),
31363d8f 654.BR getnameinfo (3)