3 .\" Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
5 .\" Permission to use, copy, modify, and/or distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 .\" PERFORMANCE OF THIS SOFTWARE.
17 .\" Id: getaddrinfo.3,v 1.3 2009/01/22 23:49:23 tbox Exp
20 .Dt GETADDRINFO @LIB_NETWORK_EXT@
26 .Nd nodename-to-address translation in protocol-independent manner
28 .Fd #include <sys/socket.h>
29 .Fd #include <netdb.h>
31 .Fn getaddrinfo "const char *nodename" "const char *servname" \
32 "const struct addrinfo *hints" "struct addrinfo **res"
34 .Fn freeaddrinfo "struct addrinfo *ai"
36 .Fn gai_strerror "int ecode"
40 function is defined for protocol-independent nodename-to-address translation.
41 It performs functionality of
42 .Xr gethostbyname @LIB_NETWORK_EXT@
44 .Xr getservbyname @LIB_NETWORK_EXT@ ,
45 in more sophisticated manner.
47 The addrinfo structure is defined as a result of including the
52 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
53 int ai_family; /* PF_xxx */
54 int ai_socktype; /* SOCK_xxx */
55 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
56 size_t ai_addrlen; /* length of ai_addr */
57 char *ai_canonname; /* canonical name for nodename */
58 struct sockaddr *ai_addr; /* binary address */
59 struct addrinfo *ai_next; /* next structure in linked list */
67 arguments are pointers to null-terminated strings or
69 One or both of these two arguments must be a
72 In the normal client scenario, both the
77 In the normal server scenario, only the
83 string can be either a node name or a numeric host address string
84 (i.e., a dotted-decimal IPv4 address or an IPv6 hex address).
88 string can be either a service name or a decimal port number.
90 The caller can optionally pass an
92 structure, pointed to by the third argument,
93 to provide hints concerning the type of socket that the caller supports.
96 structure all members other than
109 means the caller will accept any protocol family.
112 means the caller will accept any socket type.
115 means the caller will accept any protocol.
116 For example, if the caller handles only TCP and not UDP, then the
118 member of the hints structure should be set to
123 If the caller handles only IPv4 and not IPv6, then the
127 structure should be set to
132 If the third argument to
136 pointer, this is the same as if the caller had filled in an
138 structure initialized to zero with
142 Upon successful return a pointer to a linked list of one or more
144 structures is returned through the final argument.
145 The caller can process each
147 structure in this list by following the
151 pointer is encountered.
154 structure the three members
159 are the corresponding arguments for a call to the
166 member points to a filled-in socket address structure whose length is
177 structure, then the caller plans to use the returned socket address
178 structure in a call to
184 pointer, then the IP address portion of the socket
185 address structure will be set to
187 for an IPv4 address or
193 bit is not set in the
197 structure, then the returned socket address structure will be ready for a
200 .Pq for a connection-oriented protocol
206 .Pq for a connectionless protocol .
211 pointer, then the IP address portion of the
212 socket address structure will be set to the loopback address.
220 structure, then upon successful return the
224 structure in the linked list will point to a null-terminated string
225 containing the canonical name of the specified
237 string must be a numeric host address string.
238 Otherwise an error of
241 This flag prevents any type of name resolution service (e.g., the DNS)
244 All of the information returned by
246 is dynamically allocated:
249 structures, and the socket address structures and canonical node name
250 strings pointed to by the addrinfo structures.
251 To return this information to the system the function
256 structure pointed to by the
258 is freed, along with any dynamic storage pointed to by the structure.
259 This operation is repeated until a
262 pointer is encountered.
264 To aid applications in printing error messages based on the
270 The argument is one of the
272 values defined earlier and the return value points to a string describing
274 If the argument is not one of the
276 values, the function still returns a pointer to a string whose contents
277 indicate an unknown error.
279 .Bl -tag -width /etc/resolv.conf -compact
281 .It Pa /etc/host.conf
282 .It Pa /etc/resolv.conf
285 Error return status from
287 is zero on success and non-zero on errors.
288 Non-zero error codes are defined in
292 .Bl -tag -width EAI_ADDRFAMILY -compact
293 .It Dv EAI_ADDRFAMILY
294 address family for nodename not supported
296 temporary failure in name resolution
298 invalid value for ai_flags
300 non-recoverable failure in name resolution
302 ai_family not supported
304 memory allocation failure
306 no address associated with nodename
308 nodename nor servname provided, or not known
310 servname not supported for ai_socktype
312 ai_socktype not supported
314 system error returned in errno
317 If called with proper argument,
319 returns a pointer to a string describing the given error code.
320 If the argument is not one of the
322 values, the function still returns a pointer to a string whose contents
323 indicate an unknown error.
325 .Xr getnameinfo @LIB_NETWORK_EXT@ ,
326 .Xr gethostbyname @LIB_NETWORK_EXT@ ,
327 .Xr getservbyname @LIB_NETWORK_EXT@ ,
328 .Xr hosts @FORMAT_EXT@ ,
329 .Xr services @FORMAT_EXT@ ,
330 .Xr hostname @DESC_EXT@
332 R. Gilligan, S. Thomson, J. Bound, and W. Stevens,
333 ``Basic Socket Interface Extensions for IPv6,'' RFC2133, April 1997.
335 The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
339 function is defined IEEE POSIX 1003.1g draft specification,
340 and documented in ``Basic Socket Interface Extensions for IPv6''
343 The text was shamelessly copied from RFC2133.