Unleashed v1.4
[unleashed.git] / share / man / man3socket / getaddrinfo.3socket
blobedad340f2f05e0575fe3a953f89fa63a7131abbf
1 '\" te
2 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
4 .\"  See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with
5 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH GETADDRINFO 3SOCKET "Feb 25, 2017"
7 .SH NAME
8 getaddrinfo, getnameinfo, freeaddrinfo, gai_strerror \- translate between node
9 name and address
10 .SH SYNOPSIS
11 .LP
12 .nf
13 \fBcc\fR [ \fIflag\fR\&.\|.\|. ] \fIfile\fR \&.\|.\|. [ \fIlibrary\fR \&.\|.\|. ]
14 #include <sys/socket.h>
15 #include <netdb.h>
17 \fBint\fR \fBgetaddrinfo\fR(\fBconst char *\fR\fInodename\fR, \fBconst char *\fR\fIservname\fR,
18      \fBconst struct addrinfo *\fR\fIhints\fR, \fBstruct addrinfo **\fR\fIres\fR);
19 .fi
21 .LP
22 .nf
23 \fBint\fR \fBgetnameinfo\fR(\fBconst struct sockaddr *\fR\fIsa\fR, \fBsocklen_t\fR \fIsalen\fR,
24      \fBchar *\fR\fIhost\fR, \fBsize_t\fR \fIhostlen\fR, \fBchar *\fR\fIserv\fR, \fBsize_t\fR \fIservlen\fR,
25      \fBint\fR \fIflags\fR);
26 .fi
28 .LP
29 .nf
30 \fBvoid\fR \fBfreeaddrinfo\fR(\fBstruct addrinfo *\fR\fIai\fR);
31 .fi
33 .LP
34 .nf
35 \fBchar *\fR\fBgai_strerror\fR(\fBint\fR \fIerrcode\fR);
36 .fi
38 .SH DESCRIPTION
39 .LP
40 These functions perform translations from node name to address and from address
41 to node name in a protocol-independent manner.
42 .sp
43 .LP
44 The \fBgetaddrinfo()\fR function performs the node name to address translation.
45 The \fInodename\fR and \fIservname\fR arguments are pointers to null-terminated
46 strings or \fINULL\fR. One or both of these arguments must be a non-null
47 pointer. In the normal client scenario, both the \fInodename\fR and
48 \fIservname\fR are specified. In the normal server scenario, only the
49 \fIservname\fR is specified.
50 .sp
51 .LP
52 A non-null \fInodename\fR string can be a node name or a numeric host address
53 string. The \fInodename\fR can also be an IPv6 zone-id in the form:
54 .sp
55 .in +2
56 .nf
57 <address>%<zone-id>
58 .fi
59 .in -2
61 .sp
62 .LP
63 The address is the literal IPv6 link-local address or host name of the
64 destination. The zone-id is the interface ID of the IPv6 link used to send the
65 packet. The zone-id can either be a numeric value, indicating a literal zone
66 value, or an interface name such as \fBhme0\fR. If the zone-id is an interface
67 name, the interface's index will be stored in the \fIsin6_scope_id\fR field of
68 the \fBstruct sockaddr_in6\fR. If the interface does not exist, the error
69 \fBEAI_NONAME\fR will be returned. If the zone-id is a numeric value, it will
70 be placed in \fIsin6_scope_id\fR.
71 .sp
72 .LP
73 A non-null \fIservname\fR string can be either a service name or a decimal port
74 number.
75 .sp
76 .LP
77 The caller can optionally pass an \fBaddrinfo\fR structure, pointed to by the
78 \fIhints\fR argument, to provide hints concerning the type of socket that the
79 caller supports.
80 .sp
81 .LP
82 The \fBaddrinfo\fR structure is defined as:
83 .sp
84 .in +2
85 .nf
86 struct addrinfo {
87 int              ai_flags;      /* AI_PASSIVE, AI_CANONNAME,
88                                    AI_NUMERICHOST, AI_NUMERICSERV
89                                    AI_V4MAPPED, AI_ALL,
90                                    AI_ADDRCONFIG */
91 int              ai_family;     /* PF_xxx */
92 int              ai_socktype;   /* SOCK_xxx */
93 int              ai_protocol;   /* 0 or IPPROTO_xxx for IPv4 & IPv6 */
94 socklen_t        ai_addrlen;    /* length of ai_addr */
95 char             *ai_canonname; /* canonical name for nodename */
96 struct sockaddr  *ai_addr;      /* binary address */
97 struct addrinfo  *ai_next;      /* next structure in linked list */
99 .fi
100 .in -2
104 In this \fIhints\fR structure, all members other than \fBai_flags\fR,
105 \fBai_family\fR, \fBai_socktype\fR, and \fBai_protocol\fR must be 0 or a null
106 pointer. A value of \fBPF_UNSPEC\fR for \fBai_family\fR indicates that the
107 caller will accept any protocol family. A value of 0 for \fBai_socktype\fR
108 indicates that the caller will accept any socket type.  A value of 0 for
109 \fBai_protocol\fR indicates that the caller will accept any protocol. For
110 example, if the caller handles only TCP and not UDP, then the \fBai_socktype\fR
111 member of the \fIhints\fR structure should be set to \fBSOCK_STREAM\fR when
112 \fBgetaddrinfo()\fR is called. If the caller handles only IPv4 and not IPv6,
113 then the \fBai_family\fR member of the \fIhints\fR structure should be set to
114 \fBPF_INET\fR when \fBgetaddrinfo()\fR is called. If the third argument to
115 \fBgetaddrinfo()\fR is a null pointer, it is as if the caller had filled in an
116 \fBaddrinfo\fR structure initialized to 0 with \fBai_family\fR set to
117 \fBPF_UNSPEC\fR.
120 Upon success, a pointer to a linked list of one or more \fBaddrinfo\fR
121 structures is returned through the final argument.  The caller can process each
122 \fBaddrinfo\fR structure in this list by following the \fBai_next\fR pointer,
123 until a null pointer is encountered. In each returned \fBaddrinfo\fR structure
124 the three members \fBai_family\fR, \fBai_socktype\fR, and \fBai_protocol\fR are
125 the corresponding arguments for a call to the \fBsocket\fR(3SOCKET) function.
126 In each \fBaddrinfo\fR structure the \fBai_addr\fR member points to a filled-in
127 socket address structure whose length is specified by the \fBai_addrlen\fR
128 member.
131 If the \fBAI_PASSIVE\fR bit is set in the \fBai_flags\fR member of the
132 \fIhints\fR structure, the caller plans to use the returned socket address
133 structure in a call to \fBbind\fR(3SOCKET). In this case, if the \fInodename\fR
134 argument is a null pointer, the IP address portion of the socket address
135 structure will be set to \fBINADDR_ANY\fR for an IPv4 address or
136 \fBIN6ADDR_ANY_INIT\fR for an IPv6 address.
139 If the \fBAI_PASSIVE\fR bit is not set in the \fBai_flags\fR member of the
140 \fIhints\fR structure, then the returned socket address structure will be ready
141 for a call to \fBconnect\fR(3SOCKET) (for a connection-oriented protocol) or
142 either \fBconnect\fR(3SOCKET), \fBsendto\fR(3SOCKET), or \fBsendmsg\fR(3SOCKET)
143 (for a connectionless protocol). If the \fInodename\fR argument is a null
144 pointer, the IP address portion of the socket address structure will be set to
145 the loopback address.
148 If the \fBAI_CANONNAME\fR bit is set in the \fBai_flags\fR member of the
149 \fIhints\fR structure, then upon successful return the \fBai_canonname\fR
150 member of the first \fBaddrinfo\fR structure in the linked list will point to a
151 null-terminated string containing the canonical name of the specified
152 \fInodename\fR. A numeric host address string is not a name, and thus does not
153 have a canonical name form; no address to host name translation is performed.
156 If the \fBAI_NUMERICHOST\fR bit is set in the \fBai_flags\fR member of the
157 \fIhints\fR structure, then a non-null \fInodename\fR string must be a numeric
158 host address string. Otherwise an error of \fBEAI_NONAME\fR is returned. This
159 flag prevents any type of name resolution service (such as DNS) from being
160 called.
163 If the \fBAI_NUMERICSERV\fR flag is specified, then a non-null servname string
164 supplied will be a numeric port string.  Otherwise, an [\fBEAI_NONAME\fR] error
165 is returned. This flag prevents any type of name resolution service
166 from being invoked.
169 If the \fBAI_V4MAPPED\fR flag is specified along with an \fBai_family\fR of
170 \fBAF_INET6\fR, then \fBgetaddrinfo()\fR returns IPv4-mapped IPv6 addresses on
171 finding no matching IPv6 addresses (\fBai_addrlen\fR shall be 16). For example,
172 if no AAAA records are found when using DNS, a query is made for A records. Any
173 found records are returned as IPv4-mapped IPv6 addresses.
176 The \fBAI_V4MAPPED\fR flag is ignored unless \fBai_family\fR equals
177 \fBAF_INET6\fR.
180 If the \fBAI_ALL\fR flag is used with the AI_V4MAPPED flag, then
181 \fBgetaddrinfo()\fR returns all matching IPv6 and IPv4 addresses. For example,
182 when using the DNS, queries are made for both AAAA records and A records, and
183 \fBgetaddrinfo()\fR returns the combined results of both queries. Any IPv4
184 addresses found are returned as IPv4-mapped IPv6 addresses.
187 The \fBAI_ALL\fR flag without the \fBAI_V4MAPPED\fR flag is ignored.
190 When \fBai_family\fR is not specified (\fBAF_UNSPEC\fR), \fBAI_V4MAPPED\fR and
191 \fBAI_ALL\fR flags are used only if \fBAF_INET6\fR is supported.
194 If the \fBAI_ADDRCONFIG\fR flag is specified, IPv4 addresses are returned only
195 if an IPv4 address is configured on the local system, and IPv6 addresses are
196 returned only if an IPv6 address is configured on the local system. For this
197 case, the loopback address is not considered to be as valid as a configured
198 address. For example, when using the DNS, a query for AAAA records should occur
199 only if the node has at least one IPv6 address configured (other than IPv6
200 loopback) and a query for A records should occur only if the node has at least
201 one IPv4 address configured (other than the IPv4 loopback).
204 All of the information returned by \fBgetaddrinfo()\fR is dynamically
205 allocated: the \fBaddrinfo\fR structures as well as the socket address
206 structures and canonical node name strings pointed to by the \fBaddrinfo\fR
207 structures. The \fBfreeaddrinfo()\fR function is called to return this
208 information to the system. For \fBfreeaddrinfo()\fR, the \fBaddrinfo\fR
209 structure pointed to by the \fIai\fR argument is freed, along with any dynamic
210 storage pointed to by the structure. This operation is repeated until a null
211 \fBai_next\fR pointer is encountered.
214 To aid applications in printing error messages based on the \fBEAI_\fR* codes
215 returned by \fBgetaddrinfo()\fR, the \fBgai_strerror()\fR is defined. The
216 argument is one of the \fBEAI_\fR* values defined below and the return value
217 points to a string describing the error. If the argument is not one of the
218 \fBEAI_\fR* values, the function still returns a pointer to a string whose
219 contents indicate an unknown error.
222 The \fBgetnameinfo()\fR function looks up an IP address and port number
223 provided by the caller in the name service database and system-specific
224 database, and returns text strings for both in buffers provided by the caller.
225 The function indicates successful completion by a 0 return value; a non-zero
226 return value indicates failure.
229 The first argument, \fIsa\fR, points to either a \fBsockaddr_in\fR structure
230 (for IPv4) or a \fBsockaddr_in6\fR structure (for IPv6) that holds the IP
231 address and port number. The \fIsalen\fR argument gives the length of the
232 \fBsockaddr_in\fR or \fBsockaddr_in6\fR structure.
235 The function returns the node name associated with the IP address in the buffer
236 pointed to by the \fIhost\fR argument.
239 The function can also return the IPv6 zone-id in the form:
241 .in +2
243 <address>%<zone-id>
245 .in -2
249 The caller provides the size of this buffer with the \fIhostlen\fR argument.
250 The service name associated with the port number is returned in the buffer
251 pointed to by \fIserv\fR, and the \fIservlen\fR argument gives the length of
252 this buffer. The caller specifies not to return either string by providing a 0
253 value for the \fIhostlen\fR or \fIservlen\fR arguments. Otherwise, the caller
254 must provide buffers large enough to hold the node name and the service name,
255 including the terminating null characters.
258 To aid the application in allocating buffers for these two returned strings,
259 the following constants are defined in <\fBnetdb.h\fR>:
261 .in +2
263 #define NI_MAXHOST  1025
264 #define NI_MAXSERV    32
266 .in -2
270 The final argument is a flag that changes the default actions of this function.
271 By default, the fully-qualified domain name (\fBFQDN\fR) for the host is looked
272 up in the name service database and returned. If the flag bit \fBNI_NOFQDN\fR
273 is set, only the node name portion of the \fBFQDN\fR is returned for local
274 hosts.
277 If the flag bit \fBNI_NUMERICHOST\fR is set, or if the host's name cannot be
278 located in the name service, the numeric form of the host's address is returned
279 instead of its name, for example, by calling \fBinet_ntop()\fR (see
280 \fBinet\fR(3SOCKET)) instead of \fBgetipnodebyname\fR(3SOCKET). If the flag bit
281 \fBNI_NAMEREQD\fR is set, an error is returned if the host's name cannot be
282 located in the name service database.
285 If the flag bit \fBNI_NUMERICSERV\fR is set, the numeric form of the service
286 address is returned (for example, its port number) instead of its name. The two
287 \fBNI_NUMERIC\fR* flags are required to support the \fB-n\fR flag that many
288 commands provide.
291 A fifth flag bit, \fBNI_DGRAM\fR, specifies that the service is a datagram
292 service, and causes \fBgetservbyport\fR(3SOCKET) to be called with a second
293 argument of \fBudp\fR instead of the default \fBtcp\fR. This is required for
294 the few ports (for example, 512-514) that have different services for UDP and
295 TCP.
298 These \fBNI_\fR* flags are defined in <\fBnetdb.h\fR> along with the \fBAI_\fR*
299 flags already defined for \fBgetaddrinfo()\fR.
300 .SH RETURN VALUES
302 For \fBgetaddrinfo()\fR, if the query is successful, a pointer to a linked list
303 of one or more \fBaddrinfo\fR structures is returned by the fourth argument and
304 the function returns \fB0\fR. The order of the addresses returned in the fourth
305 argument is discussed in the ADDRESS ORDERING section. If the query fails, a
306 non-zero error code will be returned. For \fBgetnameinfo()\fR, if successful,
307 the strings hostname and service are copied into \fIhost\fR and \fIserv\fR,
308 respectively. If unsuccessful, zero values for either \fIhostlen\fR or
309 \fIservlen\fR will suppress the associated lookup; in this case no data is
310 copied into the applicable buffer. If \fBgai_strerror()\fR is successful, a
311 pointer to a string containing an error message appropriate for the \fBEAI_\fR*
312 errors is returned. If \fIerrcode\fR is not one of the \fBEAI_\fR* values, a
313 pointer to a string indicating an unknown error is returned.
314 .SS "Address Ordering"
316 AF_INET6 addresses returned by the fourth argument of \fBgetaddrinfo()\fR are
317 ordered according to the algorithm described in \fIRFC 3484, Default Address
318 Selection for Internet Protocol version 6 (IPv6)\fR. The addresses are ordered
319 using a list of pair-wise comparison rules which are applied in order. If a
320 rule determines that one address is better than another, the remaining rules
321 are irrelevant to the comparison of those two addresses. If two addresses are
322 equivalent according to one rule, the remaining rules act as a tie-breaker. The
323 address ordering list of pair-wise comparison rules follow below:
328 box;
329 l | l
330 l | l .
331 Avoid unusable destinations.    T{
332 Prefer a destination that is reachable through the IP routing table.
335 Prefer matching scope.  T{
336 Prefer a destination whose scope is equal to the scope of its source address. See \fBinet6\fR(7P) for the definition of scope used by this rule.
339 Avoid link-local source.        T{
340 Avoid selecting a link-local source address when the destination address is not a link-local address.
343 Avoid deprecated addresses.     T{
344 Prefer a destination that is not deprecated (\fBIFF_DEPRECATED\fR).
348 Prefer matching label. This rule uses labels that are obtained through the IPv6 default address selection policy table. See \fBipaddrsel\fR(8) for a description of the default contents of the table and how the table is configured.
349 T}      T{
350 Prefer a destination whose label is equal to the label of its source address.
354 Prefer higher precedence. This rule uses precedence values that are obtained through the IPv6 default address selection policy table. See \fBipaddrsel\fR(8) for a description of the default contents of the table and how the table is configured.
355 T}      T{
356 Prefer the destination whose precedence is higher than the other destination.
359 Prefer native transport.        T{
360 Prefer a destination if the interface that is used for sending packets to that destination is not an IP over IP tunnel.
364 Prefer smaller scope. See \fBinet6\fR(7P) for the definition of this rule.
365 T}      T{
366 Prefer the destination whose scope is smaller than the other destination.
369 Use longest matching prefix.    T{
370 When the two destinations belong to the same address family, prefer the destination that has the longer matching prefix with its source address.
374 .SH ERRORS
376 The following names are the error values returned by \fBgetaddrinfo()\fR and
377 are defined in <\fBnetdb.h\fR>:
379 .ne 2
381 \fB\fBEAI_ADDRFAMILY\fR\fR
383 .RS 18n
384 Address family for nodename is not supported.
388 .ne 2
390 \fB\fBEAI_AGAIN\fR\fR
392 .RS 18n
393 Temporary failure in name resolution has occurred .
397 .ne 2
399 \fB\fBEAI_BADFLAGS\fR\fR
401 .RS 18n
402 Invalid value specified for \fBai_flags\fR.
406 .ne 2
408 \fB\fBEAI_FAIL\fR\fR
410 .RS 18n
411 Non-recoverable failure in name resolution has occurred.
415 .ne 2
417 \fB\fBEAI_FAMILY\fR\fR
419 .RS 18n
420 The \fBai_family\fR is not supported.
424 .ne 2
426 \fB\fBEAI_MEMORY\fR\fR
428 .RS 18n
429 Memory allocation failure has occurred.
433 .ne 2
435 \fB\fBEAI_NODATA\fR\fR
437 .RS 18n
438 No address is associated with \fInodename\fR.
442 .ne 2
444 \fB\fBEAI_NONAME\fR\fR
446 .RS 18n
447 Neither \fInodename\fR nor \fIservname\fR is provided or known.
451 .ne 2
453 \fB\fBEAI_SERVICE\fR\fR
455 .RS 18n
456 The \fIservname\fR is not supported for \fBai_socktype\fR.
460 .ne 2
462 \fB\fBEAI_SOCKTYPE\fR\fR
464 .RS 18n
465 The \fBai_socktype\fR is not supported.
469 .ne 2
471 \fB\fBEAI_OVERFLOW\fR\fR
473 .RS 18n
474 Argument buffer has overflowed.
478 .ne 2
480 \fB\fBEAI_SYSTEM\fR\fR
482 .RS 18n
483 System error was returned in \fIerrno\fR.
486 .SH FILES
487 .ne 2
489 \fB\fB/etc/inet/hosts\fR\fR
491 .RS 22n
492 local database that associates names of nodes with IP addresses
496 .ne 2
498 \fB\fB/etc/netconfig\fR\fR
500 .RS 22n
501 network configuration database
505 .ne 2
507 \fB\fB/etc/nsswitch.conf\fR\fR
509 .RS 22n
510 configuration file for the name service switch
513 .SH ATTRIBUTES
515 See \fBattributes\fR(5) for description of the following attributes:
520 box;
521 c | c
522 l | l .
523 ATTRIBUTE TYPE  ATTRIBUTE VALUE
525 Interface Stability     Committed
527 MT-Level        MT-Safe
529 Standard        See \fBstandards\fR(5).
532 .SH SEE ALSO
534 \fBipaddrsel\fR(8), \fBgethostbyname\fR(3NSL), \fBgetipnodebyname\fR(3SOCKET),
535 \fBhtonl\fR(3), \fBinet\fR(3SOCKET), \fBsockaddr\fR(3SOCKET),
536 \fBnetdb.h\fR(3HEAD), \fBsocket\fR(3SOCKET), \fBhosts\fR(4),
537 \fBnsswitch.conf\fR(4), \fBattributes\fR(5), \fBstandards\fR(5), \fBinet6\fR(7P)
540 Draves, R. \fIRFC 3484, Default Address Selection for Internet Protocol version
541 6 (IPv6)\fR. Network Working Group. February 2003.
542 .SH NOTES
544 IPv4-mapped addresses are not recommended.