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