xref: /netbsd-src/external/bsd/libbind/dist/doc/getaddrinfo.3 (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos.\"	$NetBSD: getaddrinfo.3,v 1.1.1.2 2012/09/09 16:07:46 christos Exp $
2b5677b36Schristos.\"
3b5677b36Schristos.\" Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
4b5677b36Schristos.\"
5b5677b36Schristos.\" Permission to use, copy, modify, and/or distribute this software for any
6b5677b36Schristos.\" purpose with or without fee is hereby granted, provided that the above
7b5677b36Schristos.\" copyright notice and this permission notice appear in all copies.
8b5677b36Schristos.\"
9b5677b36Schristos.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10b5677b36Schristos.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11b5677b36Schristos.\" AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12b5677b36Schristos.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13b5677b36Schristos.\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14b5677b36Schristos.\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15b5677b36Schristos.\" PERFORMANCE OF THIS SOFTWARE.
16b5677b36Schristos.\"
17b5677b36Schristos.\"     Id: getaddrinfo.3,v 1.3 2009/01/22 23:49:23 tbox Exp
18b5677b36Schristos.\"
19b5677b36Schristos.Dd May 25, 1995
20b5677b36Schristos.Dt GETADDRINFO @LIB_NETWORK_EXT@
21b5677b36Schristos.Os KAME
22b5677b36Schristos.Sh NAME
23b5677b36Schristos.Nm getaddrinfo
24b5677b36Schristos.Nm freeaddrinfo ,
25b5677b36Schristos.Nm gai_strerror
26b5677b36Schristos.Nd nodename-to-address translation in protocol-independent manner
27b5677b36Schristos.Sh SYNOPSIS
28b5677b36Schristos.Fd #include <sys/socket.h>
29b5677b36Schristos.Fd #include <netdb.h>
30b5677b36Schristos.Ft int
31b5677b36Schristos.Fn getaddrinfo "const char *nodename" "const char *servname" \
32b5677b36Schristos"const struct addrinfo *hints" "struct addrinfo **res"
33b5677b36Schristos.Ft void
34b5677b36Schristos.Fn freeaddrinfo "struct addrinfo *ai"
35b5677b36Schristos.Ft "char *"
36b5677b36Schristos.Fn gai_strerror "int ecode"
37b5677b36Schristos.Sh DESCRIPTION
38b5677b36SchristosThe
39b5677b36Schristos.Fn getaddrinfo
40b5677b36Schristosfunction is defined for protocol-independent nodename-to-address translation.
41b5677b36SchristosIt performs functionality of
42b5677b36Schristos.Xr gethostbyname @LIB_NETWORK_EXT@
43b5677b36Schristosand
44b5677b36Schristos.Xr getservbyname @LIB_NETWORK_EXT@ ,
45b5677b36Schristosin more sophisticated manner.
46b5677b36Schristos.Pp
47b5677b36SchristosThe addrinfo structure is defined as a result of including the
48b5677b36Schristos.Li <netdb.h>
49b5677b36Schristosheader:
50b5677b36Schristos.Bd -literal -offset
51b5677b36Schristosstruct addrinfo {                                                  *
52b5677b36Schristos     int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
53b5677b36Schristos     int     ai_family;    /* PF_xxx */
54b5677b36Schristos     int     ai_socktype;  /* SOCK_xxx */
55b5677b36Schristos     int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
56b5677b36Schristos     size_t  ai_addrlen;   /* length of ai_addr */
57b5677b36Schristos     char   *ai_canonname; /* canonical name for nodename */
58b5677b36Schristos     struct sockaddr  *ai_addr; /* binary address */
59b5677b36Schristos     struct addrinfo  *ai_next; /* next structure in linked list */
60b5677b36Schristos};
61b5677b36Schristos.Ed
62b5677b36Schristos.Pp
63b5677b36SchristosThe
64b5677b36Schristos.Fa nodename
65b5677b36Schristosand
66b5677b36Schristos.Fa servname
67b5677b36Schristosarguments are pointers to null-terminated strings or
68b5677b36Schristos.Dv NULL .
69b5677b36SchristosOne or both of these two arguments must be a
70b5677b36Schristos.Pf non Dv -NULL
71b5677b36Schristospointer.
72b5677b36SchristosIn the normal client scenario, both the
73b5677b36Schristos.Fa nodename
74b5677b36Schristosand
75b5677b36Schristos.Fa servname
76b5677b36Schristosare specified.
77b5677b36SchristosIn the normal server scenario, only the
78b5677b36Schristos.Fa servname
79b5677b36Schristosis specified.
80b5677b36SchristosA
81b5677b36Schristos.Pf non Dv -NULL
82b5677b36Schristos.Fa nodename
83b5677b36Schristosstring can be either a node name or a numeric host address string
84b5677b36Schristos(i.e., a dotted-decimal IPv4 address or an IPv6 hex address).
85b5677b36SchristosA
86b5677b36Schristos.Pf non Dv -NULL
87b5677b36Schristos.Fa servname
88b5677b36Schristosstring can be either a service name or a decimal port number.
89b5677b36Schristos.Pp
90b5677b36SchristosThe caller can optionally pass an
91b5677b36Schristos.Li addrinfo
92b5677b36Schristosstructure, pointed to by the third argument,
93b5677b36Schristosto provide hints concerning the type of socket that the caller supports.
94b5677b36SchristosIn this
95b5677b36Schristos.Fa hints
96b5677b36Schristosstructure all members other than
97b5677b36Schristos.Fa ai_flags ,
98b5677b36Schristos.Fa ai_family ,
99b5677b36Schristos.Fa ai_socktype ,
100b5677b36Schristosand
101b5677b36Schristos.Fa ai_protocol
102b5677b36Schristosmust be zero or a
103b5677b36Schristos.Dv NULL
104b5677b36Schristospointer.
105b5677b36SchristosA value of
106b5677b36Schristos.Dv PF_UNSPEC
107b5677b36Schristosfor
108b5677b36Schristos.Fa ai_family
109b5677b36Schristosmeans the caller will accept any protocol family.
110b5677b36SchristosA value of 0 for
111b5677b36Schristos.Fa ai_socktype
112b5677b36Schristosmeans the caller will accept any socket type.
113b5677b36SchristosA value of 0 for
114b5677b36Schristos.Fa ai_protocol
115b5677b36Schristosmeans the caller will accept any protocol.
116b5677b36SchristosFor example, if the caller handles only TCP and not UDP, then the
117b5677b36Schristos.Fa ai_socktype
118b5677b36Schristosmember of the hints structure should be set to
119b5677b36Schristos.Dv SOCK_STREAM
120b5677b36Schristoswhen
121b5677b36Schristos.Fn getaddrinfo
122b5677b36Schristosis called.
123b5677b36SchristosIf the caller handles only IPv4 and not IPv6, then the
124b5677b36Schristos.Fa ai_family
125b5677b36Schristosmember of the
126b5677b36Schristos.Fa hints
127b5677b36Schristosstructure should be set to
128b5677b36Schristos.Dv PF_INET
129b5677b36Schristoswhen
130b5677b36Schristos.Fn getaddrinfo
131b5677b36Schristosis called.
132b5677b36SchristosIf the third argument to
133b5677b36Schristos.Fn getaddrinfo
134b5677b36Schristosis a
135b5677b36Schristos.Dv NULL
136b5677b36Schristospointer, this is the same as if the caller had filled in an
137b5677b36Schristos.Li addrinfo
138b5677b36Schristosstructure initialized to zero with
139b5677b36Schristos.Fa ai_family
140b5677b36Schristosset to PF_UNSPEC.
141b5677b36Schristos.Pp
142b5677b36SchristosUpon successful return a pointer to a linked list of one or more
143b5677b36Schristos.Li addrinfo
144b5677b36Schristosstructures is returned through the final argument.
145b5677b36SchristosThe caller can process each
146b5677b36Schristos.Li addrinfo
147b5677b36Schristosstructure in this list by following the
148b5677b36Schristos.Fa ai_next
149b5677b36Schristospointer, until a
150b5677b36Schristos.Dv NULL
151b5677b36Schristospointer is encountered.
152b5677b36SchristosIn each returned
153b5677b36Schristos.Li addrinfo
154b5677b36Schristosstructure the three members
155b5677b36Schristos.Fa ai_family ,
156b5677b36Schristos.Fa ai_socktype ,
157b5677b36Schristosand
158b5677b36Schristos.Fa ai_protocol
159b5677b36Schristosare the corresponding arguments for a call to the
160b5677b36Schristos.Fn socket
161b5677b36Schristosfunction.
162b5677b36SchristosIn each
163b5677b36Schristos.Li addrinfo
164b5677b36Schristosstructure the
165b5677b36Schristos.Fa ai_addr
166b5677b36Schristosmember points to a filled-in socket address structure whose length is
167b5677b36Schristosspecified by the
168b5677b36Schristos.Fa ai_addrlen
169b5677b36Schristosmember.
170b5677b36Schristos.Pp
171b5677b36SchristosIf the
172b5677b36Schristos.Dv AI_PASSIVE
173b5677b36Schristosbit is set in the
174b5677b36Schristos.Fa ai_flags
175b5677b36Schristosmember of the
176b5677b36Schristos.Fa hints
177b5677b36Schristosstructure, then the caller plans to use the returned socket address
178b5677b36Schristosstructure in a call to
179b5677b36Schristos.Fn bind .
180b5677b36SchristosIn this case, if the
181b5677b36Schristos.Fa nodename
182b5677b36Schristosargument is a
183b5677b36Schristos.Dv NULL
184b5677b36Schristospointer, then the IP address portion of the socket
185b5677b36Schristosaddress structure will be set to
186b5677b36Schristos.Dv INADDR_ANY
187b5677b36Schristosfor an IPv4 address or
188b5677b36Schristos.Dv IN6ADDR_ANY_INIT
189b5677b36Schristosfor an IPv6 address.
190b5677b36Schristos.Pp
191b5677b36SchristosIf the
192b5677b36Schristos.Dv AI_PASSIVE
193b5677b36Schristosbit is not set in the
194b5677b36Schristos.Fa ai_flags
195b5677b36Schristosmember of the
196b5677b36Schristos.Fa hints
197b5677b36Schristosstructure, then the returned socket address structure will be ready for a
198b5677b36Schristoscall to
199b5677b36Schristos.Fn connect
200b5677b36Schristos.Pq for a connection-oriented protocol
201b5677b36Schristosor either
202b5677b36Schristos.Fn connect ,
203b5677b36Schristos.Fn sendto ,
204b5677b36Schristosor
205b5677b36Schristos.Fn sendmsg
206b5677b36Schristos.Pq for a connectionless protocol .
207b5677b36SchristosIn this case, if the
208b5677b36Schristos.Fa nodename
209b5677b36Schristosargument is a
210b5677b36Schristos.Dv NULL
211b5677b36Schristospointer, then the IP address portion of the
212b5677b36Schristossocket address structure will be set to the loopback address.
213b5677b36Schristos.Pp
214b5677b36SchristosIf the
215b5677b36Schristos.Dv AI_CANONNAME
216b5677b36Schristosbit is set in the
217b5677b36Schristos.Fa ai_flags
218b5677b36Schristosmember of the
219b5677b36Schristos.Fa hints
220b5677b36Schristosstructure, then upon successful return the
221b5677b36Schristos.Fa ai_canonname
222b5677b36Schristosmember of the first
223b5677b36Schristos.Li addrinfo
224b5677b36Schristosstructure in the linked list will point to a null-terminated string
225b5677b36Schristoscontaining the canonical name of the specified
226b5677b36Schristos.Fa nodename .
227b5677b36Schristos.Pp
228b5677b36SchristosIf the
229b5677b36Schristos.Dv AI_NUMERICHOST
230b5677b36Schristosbit is set in the
231b5677b36Schristos.Fa ai_flags
232b5677b36Schristosmember of the
233b5677b36Schristos.Fa hints
234b5677b36Schristosstructure, then a
235b5677b36Schristos.Pf non Dv -NULL
236b5677b36Schristos.Fa nodename
237b5677b36Schristosstring must be a numeric host address string.
238b5677b36SchristosOtherwise an error of
239b5677b36Schristos.Dv EAI_NONAME
240b5677b36Schristosis returned.
241b5677b36SchristosThis flag prevents any type of name resolution service (e.g., the DNS)
242b5677b36Schristosfrom being called.
243b5677b36Schristos.Pp
244b5677b36SchristosAll of the information returned by
245b5677b36Schristos.Fn getaddrinfo
246b5677b36Schristosis dynamically allocated:
247b5677b36Schristosthe
248b5677b36Schristos.Li addrinfo
249b5677b36Schristosstructures, and the socket address structures and canonical node name
250b5677b36Schristosstrings pointed to by the addrinfo structures.
251b5677b36SchristosTo return this information to the system the function
252b5677b36SchristosFn freeaddrinfo
253b5677b36Schristosis called.
254b5677b36SchristosThe
255b5677b36Schristos.Fa addrinfo
256b5677b36Schristosstructure pointed to by the
257b5677b36Schristos.Fa ai argument
258b5677b36Schristosis freed, along with any dynamic storage pointed to by the structure.
259b5677b36SchristosThis operation is repeated until a
260b5677b36Schristos.Dv NULL
261b5677b36Schristos.Fa ai_next
262b5677b36Schristospointer is encountered.
263b5677b36Schristos.Pp
264b5677b36SchristosTo aid applications in printing error messages based on the
265b5677b36Schristos.Dv EAI_xxx
266b5677b36Schristoscodes returned by
267b5677b36Schristos.Fn getaddrinfo ,
268b5677b36Schristos.Fn gai_strerror
269b5677b36Schristosis defined.
270b5677b36SchristosThe argument is one of the
271b5677b36Schristos.Dv EAI_xxx
272b5677b36Schristosvalues defined earlier and the return value points to a string describing
273b5677b36Schristosthe error.
274b5677b36SchristosIf the argument is not one of the
275b5677b36Schristos.Dv EAI_xxx
276b5677b36Schristosvalues, the function still returns a pointer to a string whose contents
277b5677b36Schristosindicate an unknown error.
278b5677b36Schristos.Sh FILES
279b5677b36Schristos.Bl -tag -width /etc/resolv.conf -compact
280b5677b36Schristos.It Pa /etc/hosts
281b5677b36Schristos.It Pa /etc/host.conf
282b5677b36Schristos.It Pa /etc/resolv.conf
283b5677b36Schristos.El
284b5677b36Schristos.Sh DIAGNOSTICS
285b5677b36SchristosError return status from
286b5677b36Schristos.Fn getaddrinfo
287b5677b36Schristosis zero on success and non-zero on errors.
288b5677b36SchristosNon-zero error codes are defined in
289b5677b36Schristos.Li <netdb.h> ,
290b5677b36Schristosand as follows:
291b5677b36Schristos.Pp
292b5677b36Schristos.Bl -tag -width EAI_ADDRFAMILY -compact
293b5677b36Schristos.It Dv EAI_ADDRFAMILY
294b5677b36Schristosaddress family for nodename not supported
295b5677b36Schristos.It Dv EAI_AGAIN
296b5677b36Schristostemporary failure in name resolution
297b5677b36Schristos.It Dv EAI_BADFLAGS
298b5677b36Schristosinvalid value for ai_flags
299b5677b36Schristos.It Dv EAI_FAIL
300b5677b36Schristosnon-recoverable failure in name resolution
301b5677b36Schristos.It Dv EAI_FAMILY
302b5677b36Schristosai_family not supported
303b5677b36Schristos.It Dv EAI_MEMORY
304b5677b36Schristosmemory allocation failure
305b5677b36Schristos.It Dv EAI_NODATA
306b5677b36Schristosno address associated with nodename
307b5677b36Schristos.It Dv EAI_NONAME
308b5677b36Schristosnodename nor servname provided, or not known
309b5677b36Schristos.It Dv EAI_SERVICE
310b5677b36Schristosservname not supported for ai_socktype
311b5677b36Schristos.It Dv EAI_SOCKTYPE
312b5677b36Schristosai_socktype not supported
313b5677b36Schristos.It Dv EAI_SYSTEM
314b5677b36Schristossystem error returned in errno
315b5677b36Schristos.El
316b5677b36Schristos.Pp
317b5677b36SchristosIf called with proper argument,
318b5677b36Schristos.Fn gai_strerror
319b5677b36Schristosreturns a pointer to a string describing the given error code.
320b5677b36SchristosIf the argument is not one of the
321b5677b36Schristos.Dv EAI_xxx
322b5677b36Schristosvalues, the function still returns a pointer to a string whose contents
323b5677b36Schristosindicate an unknown error.
324b5677b36Schristos.Sh SEE ALSO
325b5677b36Schristos.Xr getnameinfo @LIB_NETWORK_EXT@ ,
326b5677b36Schristos.Xr gethostbyname @LIB_NETWORK_EXT@ ,
327b5677b36Schristos.Xr getservbyname @LIB_NETWORK_EXT@ ,
328b5677b36Schristos.Xr hosts @FORMAT_EXT@ ,
329b5677b36Schristos.Xr services @FORMAT_EXT@ ,
330b5677b36Schristos.Xr hostname @DESC_EXT@
331b5677b36Schristos.Pp
332b5677b36SchristosR. Gilligan, S.  Thomson, J. Bound, and W. Stevens,
333b5677b36Schristos``Basic Socket Interface Extensions for IPv6,'' RFC2133, April 1997.
334b5677b36Schristos.Sh HISTORY
335b5677b36SchristosThe implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
336b5677b36Schristos.Sh STANDARDS
337b5677b36SchristosThe
338b5677b36Schristos.Fn getaddrinfo
339b5677b36Schristosfunction is defined IEEE POSIX 1003.1g draft specification,
340b5677b36Schristosand documented in ``Basic Socket Interface Extensions for IPv6''
341b5677b36Schristos(RFC2133).
342b5677b36Schristos.Sh BUGS
343b5677b36SchristosThe text was shamelessly copied from RFC2133.
344