]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getaddrinfo.3
sync
[thirdparty/man-pages.git] / man3 / getaddrinfo.3
CommitLineData
fea681da
MK
1.\" Copyright 2000 Sam Varshavchik <mrsam@courier-mta.com>
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
11.\"
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
19.\"
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" References: RFC 2553
8d18d6ed
MK
24.\"
25.\" 2005-08-09, mtk, added AI_ALL, AI_ADDRCONFIG, AI_V4MAPPED,
26.\" and AI_NUMERICSERV.
27.\"
fea681da
MK
28.TH getaddrinfo 3 2000-12-18 "Linux Man Page" "Linux Programmer's Manual"
29.SH NAME
30getaddrinfo, freeaddrinfo, gai_strerror \- network address and service translation
31.SH SYNOPSIS
32.nf
33.B #include <sys/types.h>
34.B #include <sys/socket.h>
35.B #include <netdb.h>
36.sp
37.BI "int getaddrinfo(const char *" "node" ", const char *" "service" ,
38.BI " const struct addrinfo *" "hints" ,
39.BI " struct addrinfo **" "res" );
40.sp
41.BI "void freeaddrinfo(struct addrinfo *" "res" );
42.sp
43.BI "const char *gai_strerror(int " "errcode" );
44.fi
45.SH DESCRIPTION
46The
47.BR getaddrinfo (3)
48function combines the functionality provided by the
49.BR getipnodebyname (3),
50.BR getipnodebyaddr (3),
51.BR getservbyname (3),
52and
53.BR getservbyport (3)
54functions into a single interface.
55The thread-safe
56.BR getaddrinfo (3)
57function creates one or more socket address structures that can be used by the
58.BR bind (2)
59and
60.BR connect (2)
61system calls to create a client or a server socket.
62.PP
63The
64.BR getaddrinfo (3)
65function is not limited to creating IPv4 socket address structures;
66IPv6 socket address structures can be created if IPv6 support is available.
67These socket address structures can be used directly by
68.BR bind (2)
69or
70.BR connect (2),
71to prepare a client or a server socket.
72.PP
73The
8478ee02 74.I addrinfo
fea681da
MK
75structure used by this function contains the following members:
76.sp
77.nf
78.B struct addrinfo {
79.BI " int " "ai_flags" ";"
80.BI " int " "ai_family" ";"
81.BI " int " "ai_socktype" ";"
82.BI " int " "ai_protocol" ";"
83.BI " size_t " "ai_addrlen" ";"
84.BI " struct sockaddr *" "ai_addr" ";"
85.BI " char *" "ai_canonname" ";"
86.BI " struct addrinfo *" "ai_next" ";"
87.B };
88.fi
89.PP
90.BR getaddrinfo (3)
91sets
92.I res
8194de33 93to point to a dynamically-allocated linked list of
8478ee02 94.I addrinfo
fea681da
MK
95structures, linked by the
96.I ai_next
97member.
98There are several reasons why
8194de33 99the linked list may have more than one
8478ee02 100.I addrinfo
fea681da
MK
101structure, including: if the network host is
102multi-homed; or if the same service
103is available from multiple socket protocols (one
104.B SOCK_STREAM
105address and another
106.B SOCK_DGRAM
107address, for example).
108.PP
109The members
110.IR ai_family ,
111.IR ai_socktype ,
112and
113.I ai_protocol
114have the same meaning as the corresponding parameters in the
115.BR socket (2)
116system call.
117The
118.BR getaddrinfo (3)
119function returns socket addresses in either IPv4 or IPv6
120address family,
121.RI "(" "ai_family"
122will be set to either
8d18d6ed 123.B AF_INET
fea681da 124or
8d18d6ed 125.BR AF_INET6 ).
fea681da
MK
126.PP
127The
128.I hints
129parameter specifies
130the preferred socket type, or protocol.
131A NULL
132.I hints
133specifies that any network address or protocol is acceptable.
8d18d6ed 134If this parameter is not NULL it points to an
8478ee02 135.I addrinfo
fea681da
MK
136structure
137whose
138.IR ai_family ,
139.IR ai_socktype ,
140and
141.I ai_protocol
142members specify the preferred socket type.
8d18d6ed 143.B AF_UNSPEC
fea681da
MK
144in
145.I ai_family
146specifies any protocol family (either IPv4 or IPv6, for example).
1470 in
148.I ai_socktype
149or
150.I ai_protocol
151specifies that any socket type or protocol is acceptable as well.
152The
153.I ai_flags
154member
155specifies additional options, defined below.
156Multiple flags are specified by logically OR-ing them together.
157All the other members in the
158.I hints
159parameter must contain either 0, or a null pointer.
160.PP
161The
162.I node
163or
164.I service
165parameter, but not both, may be NULL.
166.I node
167specifies either a numerical network address
168(dotted-decimal format for IPv4, hexadecimal format for IPv6)
169or a network hostname, whose network addresses are looked up and resolved.
8d18d6ed
MK
170If
171.I hints.ai_flags
172contains the
fea681da
MK
173.B AI_NUMERICHOST
174flag then the
175.I node
176parameter must be a numerical network address.
177The
178.B AI_NUMERICHOST
179flag suppresses any potentially lengthy network host address lookups.
180.PP
181The
182.BR getaddrinfo (3)
8194de33 183function creates a linked list of
8478ee02 184.I addrinfo
fea681da
MK
185structures, one for each network address subject to any restrictions
186imposed by the
187.I hints
188parameter.
8194de33 189The
fea681da 190.I ai_canonname
8194de33 191field of the first of these
8478ee02 192.I addrinfo
8194de33 193structures is set to point to the official name of the host, if
8d18d6ed 194.I hints.ai_flags
fea681da
MK
195includes the
196.B AI_CANONNAME
197flag.
8194de33
MK
198.\" In glibc prior to 2.3.4, the ai_canonname of each addrinfo
199.\" structure was set pointing to the canonical name; that was
68e1685c 200.\" more than POSIX.1-2001 specified, or other implementations provided.
8194de33 201.\" MTK, Aug 05
fea681da
MK
202.IR ai_family ,
203.IR ai_socktype ,
204and
205.I ai_protocol
206specify the socket creation parameters.
207A pointer to the socket address is placed in the
208.I ai_addr
209member, and the length of the socket address, in bytes,
210is placed in the
211.I ai_addrlen
212member.
213.PP
214If
215.I node
216is NULL,
217the
218network address in each socket structure is initialized according to the
219.B AI_PASSIVE
8d18d6ed
MK
220flag, which is set in
221.IR hints.ai_flags .
fea681da
MK
222The network address in each socket structure will be left unspecified
223if
224.B AI_PASSIVE
225flag is set.
226This is used by server applications, which intend to accept
227client connections on any network address.
228The network address will be set to the loopback interface address
229if the
230.B AI_PASSIVE
231flag is not set.
232This is used by client applications, which intend to connect
233to a server running on the same network host.
234.PP
8d18d6ed
MK
235If
236.I hints.ai_flags
237includes the
238.B AI_ADDRCONFIG
239flag, then IPv4 addresses are returned in the list pointed to by
240.I result
fe8931d2 241only if the local system has at least one
8d18d6ed
MK
242IPv4 address configured, and IPv6 addresses are only returned
243if the local system has at least one IPv6 address configured.
244.PP
245If
246.I hint.ai_flags
247specifies the
248.B AI_V4MAPPED
249flag, and
250.I hints.ai_family
251was specified as
252.BR AF_INET6 ,
253and no matching IPv6 addresses could be found,
254then return IPv4-mapped IPv6 addresses in the list pointed to by
255.IR result .
256If both
257.B AI_V4MAPPED
258and
259.B AI_ALL
260are specified in
261.IR hints.ai_family ,
262then return both IPv6 and IPv4-mapped IPv6 addresses
263in the list pointed to by
264.IR result .
265.B AI_ALL
266is ignored if
267.B AI_V4MAPPED
268is not also specified.
269.PP
fea681da
MK
270.I service
271sets the port number in the network address of each socket structure.
272If
273.I service
274is NULL the port number will be left uninitialized.
8d18d6ed
MK
275If
276.B AI_NUMERICSERV
277is specified in
278.IR hints.ai_flags
279and
280.I service
281is not NULL, then
282.I service
283must point to a string containing a numeric port number.
284This flag is used to inhibit the invocation of a name resolution service
285in cases where it is known not to be required.
fea681da
MK
286.PP
287The
288.BR freeaddrinfo (3)
289function frees the memory that was allocated
8194de33 290for the dynamically allocated linked list
fea681da 291.IR res .
22135cad
MK
292.SS "Extensions to getaddrinfo() for Internationalized Domain Names"
293.PP
294Starting with glibc 2.3.4,
295.BR getaddrinfo ()
296has been extended to selectively allow the incoming and outgoing
297host names to be transparently converted to and from the
298Internationalized Domain Name (IDN) format (see RFC 3490,
299.IR "Internationalizing Domain Names in Applications (IDNA)" ).
300Four new flags are defined:
301.TP
302.B AI_IDN
303If this flag is specified, then the node name given in
304.I node
305is converted to IDN format if necessary.
306The source encoding is that of the current locale.
307
308If the input name contains non-ASCII characters, then the IDN encoding
309is used.
310Those parts of the node name (delimited by dots) that contain
311non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
312before being passed to the name resolution functions.
313.\" Implementation Detail:
314.\" To minimize effects on system performance the implementation might
315.\" want to check whether the input string contains any non-ASCII
316.\" characters. If there are none the IDN step can be skipped completely.
317.\" On systems which allow not-ASCII safe encodings for a locale this
318.\" might be a problem.
319.TP
320.B AI_CANONIDN
321After a successful name lookup, and if the
322.B AI_CANONNAME
323flag was specified,
324.BR getaddrinfo ()
325will return the canonical name of the
326node corresponding to the
327.I addrinfo
328structure value passed back.
329The return value is an exact copy of the value returned by the name
330resolution function.
331
332If the name is encoded using ACE, then it will contain the
333.I xn--
334prefix for one or more components of the name.
335To convert these components into a readable form the
336.B AI_CANONIDN
337flag can be passed in addition to
338.BR AI_CANONNAME .
339The resulting string is encoded using the current locale's encoding.
340.\"
341.\"Implementation Detail:
342.\"If no component of the returned name starts with xn-- the IDN
343.\"step can be skipped, therefore avoiding unnecessary slowdowns.
344.TP
345.BR AI_IDN_ALLOW_UNASSIGNED ", " AI_IDN_USE_STD3_ASCII_RULES
346Setting these flags will enable the
347IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
348IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
349conforming host name)
350flags respectively to be used in the IDNA handling.
fea681da 351.SH "RETURN VALUE"
d5da3c8d 352.\" FIXME glibc defines the following additional errors, some which
22135cad
MK
353.\" can probably be returned by getaddrinfo(); they need to
354.\" be documented.
355.\" # ifdef __USE_GNU
356.\" # define EAI_INPROGRESS -100 /* Processing request in progress. */
357.\" # define EAI_CANCELED -101 /* Request canceled. */
358.\" # define EAI_NOTCANCELED -102 /* Request not canceled. */
359.\" # define EAI_ALLDONE -103 /* All requests done. */
360.\" # define EAI_INTR -104 /* Interrupted by a signal. */
361.\" # define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
362.\" # endif
fea681da
MK
363.BR getaddrinfo (3)
364returns 0 if it succeeds, or one of the following non-zero error codes:
365.TP
a0aa388d
MK
366.B EAI_ADDRFAMILY
367The specified network host does not have any network addresses in the
368requested address family.
fea681da 369.TP
a0aa388d
MK
370.B EAI_AGAIN
371The name server returned a temporary failure indication.
372Try again later.
fea681da
MK
373.TP
374.B EAI_BADFLAGS
375.I ai_flags
376contains invalid flags.
377.TP
a0aa388d
MK
378.B EAI_FAIL
379The name server returned a permanent failure indication.
380.TP
381.B EAI_FAMILY
382The requested address family is not supported at all.
383.TP
384.B EAI_MEMORY
385Out of memory.
386.TP
387.B EAI_NODATA
388The specified network host exists, but does not have any
389network addresses defined.
390.TP
fea681da
MK
391.B EAI_NONAME
392The
393.I node
394or
395.I service
8d18d6ed 396is not known; or both
fea681da
MK
397.I node
398and
399.I service
8d18d6ed
MK
400are NULL; or
401.B AI_NUMERICSERV
402was specified in
403.I hints.ai_flags
404and
405.I service
406was not a numeric port-number string.
fea681da
MK
407.TP
408.B EAI_SERVICE
409The requested service is not available for the requested socket type.
410It may be available through another socket type.
411.TP
a0aa388d
MK
412.B EAI_SOCKTYPE
413The requested socket type is not supported at all.
fea681da
MK
414.TP
415.B EAI_SYSTEM
416Other system error, check
417.I errno
418for details.
419.PP
420The
421.BR gai_strerror (3)
422function translates these error codes to a human readable string,
423suitable for error reporting.
424.SH "CONFORMING TO"
68e1685c 425POSIX.1-2001.
fea681da 426The
63aa9df0 427.BR getaddrinfo ()
331da7c3 428function is documented in RFC\ 2553.
8d18d6ed
MK
429.SH "NOTES"
430.BR AI_ADDRCONFIG ,
d63f46e1 431.BR AI_ALL ,
8d18d6ed
MK
432and
433.BR AI_V4MAPPED
434are available since glibc 2.3.3.
435.BR AI_NUMERICSERV
436is available since glibc 2.3.4.
fea681da
MK
437.SH "SEE ALSO"
438.BR getipnodebyaddr (3),
439.BR getipnodebyname (3)