5331 want sockaddr(3SOCKET)
[illumos-gate.git] / usr / src / man / man3socket / getaddrinfo.3socket
blob1ab1f78d612365cc940e925ae5761eb0e927f587
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 "May 22, 2014"
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 \&.\|.\|. \fB-lsocket\fR \fB -lnsl \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.
67 .sp
68 .LP
69 A non-null \fIservname\fR string can be either a service name or a decimal port
70 number.
71 .sp
72 .LP
73 The caller can optionally pass an \fBaddrinfo\fR structure, pointed to by the
74 \fIhints\fR argument, to provide hints concerning the type of socket that the
75 caller supports.
76 .sp
77 .LP
78 The \fBaddrinfo\fR structure is defined as:
79 .sp
80 .in +2
81 .nf
82 struct addrinfo {
83 int              ai_flags;      /* AI_PASSIVE, AI_CANONNAME,
84                                    AI_NUMERICHOST, AI_NUMERICSERV
85                                    AI_V4MAPPED, AI_ALL,
86                                    AI_ADDRCONFIG */
87 int              ai_family;     /* PF_xxx */
88 int              ai_socktype;   /* SOCK_xxx */
89 int              ai_protocol;   /* 0 or IPPROTO_xxx for IPv4 & IPv6 */
90 socklen_t        ai_addrlen;    /* length of ai_addr */
91 char             *ai_canonname; /* canonical name for nodename */
92 struct sockaddr  *ai_addr;      /* binary address */
93 struct addrinfo  *ai_next;      /* next structure in linked list */
95 .fi
96 .in -2
98 .sp
99 .LP
100 In this \fIhints\fR structure, all members other than \fBai_flags\fR,
101 \fBai_family\fR, \fBai_socktype\fR, and \fBai_protocol\fR must be 0 or a null
102 pointer. A value of \fBPF_UNSPEC\fR for \fBai_family\fR indicates that the
103 caller will accept any protocol family. A value of 0 for \fBai_socktype\fR
104 indicates that the caller will accept any socket type.  A value of 0 for
105 \fBai_protocol\fR indicates that the caller will accept any protocol. For
106 example, if the caller handles only TCP and not UDP, then the \fBai_socktype\fR
107 member of the \fIhints\fR structure should be set to \fBSOCK_STREAM\fR when
108 \fBgetaddrinfo()\fR is called. If the caller handles only IPv4 and not IPv6,
109 then the \fBai_family\fR member of the \fIhints\fR structure should be set to
110 \fBPF_INET\fR when \fBgetaddrinfo()\fR is called. If the third argument to
111 \fBgetaddrinfo()\fR is a null pointer, it is as if the caller had filled in an
112 \fBaddrinfo\fR structure initialized to 0 with \fBai_family\fR set to
113 \fBPF_UNSPEC\fR.
116 Upon success, a pointer to a linked list of one or more \fBaddrinfo\fR
117 structures is returned through the final argument.  The caller can process each
118 \fBaddrinfo\fR structure in this list by following the \fBai_next\fR pointer,
119 until a null pointer is encountered. In each returned \fBaddrinfo\fR structure
120 the three members \fBai_family\fR, \fBai_socktype\fR, and \fBai_protocol\fR are
121 the corresponding arguments for a call to the \fBsocket\fR(3SOCKET) function.
122 In each \fBaddrinfo\fR structure the \fBai_addr\fR member points to a filled-in
123 socket address structure whose length is specified by the \fBai_addrlen\fR
124 member.
127 If the \fBAI_PASSIVE\fR bit is set in the \fBai_flags\fR member of the
128 \fIhints\fR structure, the caller plans to use the returned socket address
129 structure in a call to \fBbind\fR(3SOCKET). In this case, if the \fInodename\fR
130 argument is a null pointer, the IP address portion of the socket address
131 structure will be set to \fBINADDR_ANY\fR for an IPv4 address or
132 \fBIN6ADDR_ANY_INIT\fR for an IPv6 address.
135 If the \fBAI_PASSIVE\fR bit is not set in the \fBai_flags\fR member of the
136 \fIhints\fR structure, then the returned socket address structure will be ready
137 for a call to \fBconnect\fR(3SOCKET) (for a connection-oriented protocol) or
138 either \fBconnect\fR(3SOCKET), \fBsendto\fR(3SOCKET), or \fBsendmsg\fR(3SOCKET)
139 (for a connectionless protocol). If the \fInodename\fR argument is a null
140 pointer, the IP address portion of the socket address structure will be set to
141 the loopback address.
144 If the \fBAI_CANONNAME\fR bit is set in the \fBai_flags\fR member of the
145 \fIhints\fR structure, then upon successful return the \fBai_canonname\fR
146 member of the first \fBaddrinfo\fR structure in the linked list will point to a
147 null-terminated string containing the canonical name of the specified
148 \fInodename\fR. A numeric host address string is not a name, and thus does not
149 have a canonical name form; no address to host name translation is performed.
152 If the \fBAI_NUMERICHOST\fR bit is set in the \fBai_flags\fR member of the
153 \fIhints\fR structure, then a non-null \fInodename\fR string must be a numeric
154 host address string. Otherwise an error of \fBEAI_NONAME\fR is returned. This
155 flag prevents any type of name resolution service (such as DNS) from being
156 called.
159 If the \fBAI_NUMERICSERV\fR flag is specified, then a non-null servname string
160 supplied will be a numeric port string.  Otherwise, an [\fBEAI_NONAME\fR] error
161 is returned. This flag prevents any type of name resolution service (for
162 example, NIS+) from being invoked.
165 If the \fBAI_V4MAPPED\fR flag is specified along with an \fBai_family\fR of
166 \fBAF_INET6\fR, then \fBgetaddrinfo()\fR returns IPv4-mapped IPv6 addresses on
167 finding no matching IPv6 addresses (\fBai_addrlen\fR shall be 16). For example,
168 if no AAAA records are found when using DNS, a query is made for A records. Any
169 found records are returned as IPv4-mapped IPv6 addresses.
172 The \fBAI_V4MAPPED\fR flag is ignored unless \fBai_family\fR equals
173 \fBAF_INET6\fR.
176 If the \fBAI_ALL\fR flag is used with the AI_V4MAPPED flag, then
177 \fBgetaddrinfo()\fR returns all matching IPv6 and IPv4 addresses. For example,
178 when using the DNS, queries are made for both AAAA records and A records, and
179 \fBgetaddrinfo()\fR returns the combined results of both queries. Any IPv4
180 addresses found are returned as IPv4-mapped IPv6 addresses.
183 The \fBAI_ALL\fR flag without the \fBAI_V4MAPPED\fR flag is ignored.
186 When \fBai_family\fR is not specified (\fBAF_UNSPEC\fR), \fBAI_V4MAPPED\fR and
187 \fBAI_ALL\fR flags are used only if \fBAF_INET6\fR is supported.
190 If the \fBAI_ADDRCONFIG\fR flag is specified, IPv4 addresses are returned only
191 if an IPv4 address is configured on the local system, and IPv6 addresses are
192 returned only if an IPv6 address is configured on the local system. For this
193 case, the loopback address is not considered to be as valid as a configured
194 address. For example, when using the DNS, a query for AAAA records should occur
195 only if the node has at least one IPv6 address configured (other than IPv6
196 loopback) and a query for A records should occur only if the node has at least
197 one IPv4 address configured (other than the IPv4 loopback).
200 All of the information returned by \fBgetaddrinfo()\fR is dynamically
201 allocated: the \fBaddrinfo\fR structures as well as the socket address
202 structures and canonical node name strings pointed to by the \fBaddrinfo\fR
203 structures. The \fBfreeaddrinfo()\fR function is called to return this
204 information to the system. For \fBfreeaddrinfo()\fR, the \fBaddrinfo\fR
205 structure pointed to by the \fIai\fR argument is freed, along with any dynamic
206 storage pointed to by the structure. This operation is repeated until a null
207 \fBai_next\fR pointer is encountered.
210 To aid applications in printing error messages based on the \fBEAI_\fR* codes
211 returned by \fBgetaddrinfo()\fR, the \fBgai_strerror()\fR is defined. The
212 argument is one of the \fBEAI_\fR* values defined below and the return value
213 points to a string describing the error. If the argument is not one of the
214 \fBEAI_\fR* values, the function still returns a pointer to a string whose
215 contents indicate an unknown error.
218 The \fBgetnameinfo()\fR function looks up an IP address and port number
219 provided by the caller in the name service database and system-specific
220 database, and returns text strings for both in buffers provided by the caller.
221 The function indicates successful completion by a 0 return value; a non-zero
222 return value indicates failure.
225 The first argument, \fIsa\fR, points to either a \fBsockaddr_in\fR structure
226 (for IPv4) or a \fBsockaddr_in6\fR structure (for IPv6) that holds the IP
227 address and port number. The \fIsalen\fR argument gives the length of the
228 \fBsockaddr_in\fR or \fBsockaddr_in6\fR structure.
231 The function returns the node name associated with the IP address in the buffer
232 pointed to by the \fIhost\fR argument.
235 The function can also return the IPv6 zone-id in the form:
237 .in +2
239 <address>%<zone-id>
241 .in -2
245 The caller provides the size of this buffer with the \fIhostlen\fR argument.
246 The service name associated with the port number is returned in the buffer
247 pointed to by \fIserv\fR, and the \fIservlen\fR argument gives the length of
248 this buffer. The caller specifies not to return either string by providing a 0
249 value for the \fIhostlen\fR or \fIservlen\fR arguments. Otherwise, the caller
250 must provide buffers large enough to hold the node name and the service name,
251 including the terminating null characters.
254 To aid the application in allocating buffers for these two returned strings,
255 the following constants are defined in <\fBnetdb.h\fR>:
257 .in +2
259 #define NI_MAXHOST  1025
260 #define NI_MAXSERV    32
262 .in -2
266 The final argument is a flag that changes the default actions of this function.
267 By default, the fully-qualified domain name (\fBFQDN\fR) for the host is looked
268 up in the name service database and returned. If the flag bit \fBNI_NOFQDN\fR
269 is set, only the node name portion of the \fBFQDN\fR is returned for local
270 hosts.
273 If the flag bit \fBNI_NUMERICHOST\fR is set, or if the host's name cannot be
274 located in the name service, the numeric form of the host's address is returned
275 instead of its name, for example, by calling \fBinet_ntop()\fR (see
276 \fBinet\fR(3SOCKET)) instead of \fBgetipnodebyname\fR(3SOCKET). If the flag bit
277 \fBNI_NAMEREQD\fR is set, an error is returned if the host's name cannot be
278 located in the name service database.
281 If the flag bit \fBNI_NUMERICSERV\fR is set, the numeric form of the service
282 address is returned (for example, its port number) instead of its name. The two
283 \fBNI_NUMERIC\fR* flags are required to support the \fB-n\fR flag that many
284 commands provide.
287 A fifth flag bit, \fBNI_DGRAM\fR, specifies that the service is a datagram
288 service, and causes \fBgetservbyport\fR(3SOCKET) to be called with a second
289 argument of \fBudp\fR instead of the default \fBtcp\fR. This is required for
290 the few ports (for example, 512-514) that have different services for UDP and
291 TCP.
294 These \fBNI_\fR* flags are defined in <\fBnetdb.h\fR> along with the \fBAI_\fR*
295 flags already defined for \fBgetaddrinfo()\fR.
296 .SH RETURN VALUES
298 For \fBgetaddrinfo()\fR, if the query is successful, a pointer to a linked list
299 of one or more \fBaddrinfo\fR structures is returned by the fourth argument and
300 the function returns \fB0\fR. The order of the addresses returned in the fourth
301 argument is discussed in the ADDRESS ORDERING section. If the query fails, a
302 non-zero error code will be returned. For \fBgetnameinfo()\fR, if successful,
303 the strings hostname and service are copied into \fIhost\fR and \fIserv\fR,
304 respectively. If unsuccessful, zero values for either \fIhostlen\fR or
305 \fIservlen\fR will suppress the associated lookup; in this case no data is
306 copied into the applicable buffer. If \fBgai_strerror()\fR is successful, a
307 pointer to a string containing an error message appropriate for the \fBEAI_\fR*
308 errors is returned. If \fIerrcode\fR is not one of the \fBEAI_\fR* values, a
309 pointer to a string indicating an unknown error is returned.
310 .SS "Address Ordering"
312 AF_INET6 addresses returned by the fourth argument of \fBgetaddrinfo()\fR are
313 ordered according to the algorithm described in \fIRFC 3484, Default Address
314 Selection for Internet Protocol version 6 (IPv6)\fR. The addresses are ordered
315 using a list of pair-wise comparison rules which are applied in order. If a
316 rule determines that one address is better than another, the remaining rules
317 are irrelevant to the comparison of those two addresses. If two addresses are
318 equivalent according to one rule, the remaining rules act as a tie-breaker. The
319 address ordering list of pair-wise comparison rules follow below:
324 box;
325 l | l
326 l | l .
327 Avoid unusable destinations.    T{
328 Prefer a destination that is reachable through the IP routing table.
331 Prefer matching scope.  T{
332 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.
335 Avoid link-local source.        T{
336 Avoid selecting a link-local source address when the destination address is not a link-local address.
339 Avoid deprecated addresses.     T{
340 Prefer a destination that is not deprecated (\fBIFF_DEPRECATED\fR).
344 Prefer matching label. This rule uses labels that are obtained through the IPv6 default address selection policy table. See \fBipaddrsel\fR(1M) for a description of the default contents of the table and how the table is configured.
345 T}      T{
346 Prefer a destination whose label is equal to the label of its source address.
350 Prefer higher precedence. This rule uses precedence values that are obtained through the IPv6 default address selection policy table. See \fBipaddrsel\fR(1M) for a description of the default contents of the table and how the table is configured.
351 T}      T{
352 Prefer the destination whose precedence is higher than the other destination.
355 Prefer native transport.        T{
356 Prefer a destination if the interface that is used for sending packets to that destination is not an IP over IP tunnel.
360 Prefer smaller scope. See \fBinet6\fR(7P) for the definition of this rule.
361 T}      T{
362 Prefer the destination whose scope is smaller than the other destination.
365 Use longest matching prefix.    T{
366 When the two destinations belong to the same address family, prefer the destination that has the longer matching prefix with its source address.
370 .SH ERRORS
372 The following names are the error values returned by \fBgetaddrinfo()\fR and
373 are defined in <\fBnetdb.h\fR>:
375 .ne 2
377 \fB\fBEAI_ADDRFAMILY\fR\fR
379 .RS 18n
380 Address family for nodename is not supported.
384 .ne 2
386 \fB\fBEAI_AGAIN\fR\fR
388 .RS 18n
389 Temporary failure in name resolution has occurred .
393 .ne 2
395 \fB\fBEAI_BADFLAGS\fR\fR
397 .RS 18n
398 Invalid value specified for \fBai_flags\fR.
402 .ne 2
404 \fB\fBEAI_FAIL\fR\fR
406 .RS 18n
407 Non-recoverable failure in name resolution has occurred.
411 .ne 2
413 \fB\fBEAI_FAMILY\fR\fR
415 .RS 18n
416 The \fBai_family\fR is not supported.
420 .ne 2
422 \fB\fBEAI_MEMORY\fR\fR
424 .RS 18n
425 Memory allocation failure has occurred.
429 .ne 2
431 \fB\fBEAI_NODATA\fR\fR
433 .RS 18n
434 No address is associated with \fInodename\fR.
438 .ne 2
440 \fB\fBEAI_NONAME\fR\fR
442 .RS 18n
443 Neither \fInodename\fR nor \fIservname\fR is provided or known.
447 .ne 2
449 \fB\fBEAI_SERVICE\fR\fR
451 .RS 18n
452 The \fIservname\fR is not supported for \fBai_socktype\fR.
456 .ne 2
458 \fB\fBEAI_SOCKTYPE\fR\fR
460 .RS 18n
461 The \fBai_socktype\fR is not supported.
465 .ne 2
467 \fB\fBEAI_OVERFLOW\fR\fR
469 .RS 18n
470 Argument buffer has overflowed.
474 .ne 2
476 \fB\fBEAI_SYSTEM\fR\fR
478 .RS 18n
479 System error was returned in \fIerrno\fR.
482 .SH FILES
483 .ne 2
485 \fB\fB/etc/inet/hosts\fR\fR
487 .RS 22n
488 local database that associates names of nodes with IP addresses
492 .ne 2
494 \fB\fB/etc/netconfig\fR\fR
496 .RS 22n
497 network configuration database
501 .ne 2
503 \fB\fB/etc/nsswitch.conf\fR\fR
505 .RS 22n
506 configuration file for the name service switch
509 .SH ATTRIBUTES
511 See \fBattributes\fR(5) for description of the following attributes:
516 box;
517 c | c
518 l | l .
519 ATTRIBUTE TYPE  ATTRIBUTE VALUE
521 Interface Stability     Committed
523 MT-Level        MT-Safe
525 Standard        See \fBstandards\fR(5).
528 .SH SEE ALSO
530 \fBipaddrsel\fR(1M), \fBgethostbyname\fR(3NSL), \fBgetipnodebyname\fR(3SOCKET),
531 \fBhtonl\fR(3SOCKET), \fBinet\fR(3SOCKET), \fBsockaddr\fR(3SOCKET),
532 \fBnetdb.h\fR(3HEAD), \fBsocket\fR(3SOCKET), \fBhosts\fR(4),
533 \fBnsswitch.conf\fR(4), \fBattributes\fR(5), \fBstandards\fR(5), \fBinet6\fR(7P)
536 Draves, R. \fIRFC 3484, Default Address Selection for Internet Protocol version
537 6 (IPv6)\fR. Network Working Group. February 2003.
538 .SH NOTES
540 IPv4-mapped addresses are not recommended.