Libinfo-278.tar.gz
[apple/libinfo.git] / lookup.subproj / getaddrinfo.3
1 .\"     $NetBSD: getaddrinfo.3,v 1.39 2005/01/11 06:01:41 itojun Exp $
2 .\"     $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $
3 .\"     $OpenBSD: getaddrinfo.3,v 1.35 2004/12/21 03:40:31 jaredy Exp $
4 .\"
5 .\" Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
6 .\" Copyright (C) 2000, 2001  Internet Software Consortium.
7 .\"
8 .\" Permission to use, copy, modify, and distribute this software for any
9 .\" purpose with or without fee is hereby granted, provided that the above
10 .\" copyright notice and this permission notice appear in all copies.
11 .\"
12 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
13 .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14 .\" AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15 .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16 .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
17 .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18 .\" PERFORMANCE OF THIS SOFTWARE.
19 .\"
20 .Dd December 20, 2004
21 .Dt GETADDRINFO 3
22 .Os
23 .Sh NAME
24 .Nm freeaddrinfo ,
25 .Nm getaddrinfo
26 .Nd socket address structure to host and service name
27 .Sh SYNOPSIS
28 .Fd #include <sys/socket.h>
29 .Fd #include <netdb.h>
30 .Ft void
31 .Fo freeaddrinfo
32 .Fa "struct addrinfo *ai"
33 .Fc
34 .Ft int
35 .Fo getaddrinfo
36 .Fa "const char *restrict nodename"
37 .Fa "const char *restrict servname"
38 .Fa "const struct addrinfo *restrict hints"
39 .Fa "struct addrinfo **restrict res"
40 .Fc
41 .Sh DESCRIPTION
42 The
43 .Fn getaddrinfo
44 function is used to get a list of
45 .Tn IP
46 addresses and port numbers for host
47 .Fa nodename
48 and service
49 .Fa servname .
50 It is a replacement for and provides more flexibility than the
51 .Xr gethostbyname 3
52 and
53 .Xr getservbyname 3
54 functions.
55 .Pp
56 The
57 .Fa nodename
58 and
59 .Fa servname
60 arguments are either pointers to NUL-terminated strings or the null pointer.
61 An acceptable value for
62 .Fa nodename
63 is either a valid host name or a numeric host address string consisting
64 of a dotted decimal IPv4 address or an IPv6 address.
65 The
66 .Fa servname
67 is either a decimal port number or a service name listed in
68 .Xr services 5 .
69 At least one of
70 .Fa nodename
71 and
72 .Fa servname
73 must be non-null.
74 .Pp
75 .Fa hints
76 is an optional pointer to a
77 .Li struct addrinfo ,
78 as defined by
79 .Aq Pa netdb.h :
80 .Bd -literal
81 struct addrinfo {
82         int ai_flags;           /* input flags */
83         int ai_family;          /* protocol family for socket */
84         int ai_socktype;        /* socket type */
85         int ai_protocol;        /* protocol for socket */
86         socklen_t ai_addrlen;   /* length of socket-address */
87         struct sockaddr *ai_addr; /* socket-address for socket */
88         char *ai_canonname;     /* canonical name for service location */
89         struct addrinfo *ai_next; /* pointer to next in list */
90 };
91 .Ed
92 .Pp
93 This structure can be used to provide hints concerning the type of socket
94 that the caller supports or wishes to use.
95 The caller can supply the following structure elements in
96 .Fa hints :
97 .Bl -tag -width "ai_socktypeXX"
98 .It Fa ai_family
99 The protocol family that should be used.
100 When
101 .Fa ai_family
102 is set to
103 .Dv PF_UNSPEC ,
104 it means the caller will accept any protocol family supported by the
105 operating system.
106 .It Fa ai_socktype
107 Denotes the type of socket that is wanted:
108 .Dv SOCK_STREAM ,
109 .Dv SOCK_DGRAM ,
110 or
111 .Dv SOCK_RAW .
112 When
113 .Fa ai_socktype
114 is zero the caller will accept any socket type.
115 .It Fa ai_protocol
116 Indicates which transport protocol is desired,
117 .Dv IPPROTO_UDP
118 or
119 .Dv IPPROTO_TCP .
120 If
121 .Fa ai_protocol
122 is zero the caller will accept any protocol.
123 .It Fa ai_flags
124 .Fa ai_flags
125 is formed by
126 .Tn OR Ns 'ing
127 the following values:
128 .Bl -tag -width "AI_CANONNAMEXX"
129 .It Dv AI_CANONNAME
130 If the
131 .Dv AI_CANONNAME
132 bit is set, a successful call to
133 .Fn getaddrinfo
134 will return a NUL-terminated string containing the canonical name
135 of the specified hostname in the
136 .Fa ai_canonname
137 element of the first
138 .Li addrinfo
139 structure returned.
140 .It Dv AI_NUMERICHOST
141 If the
142 .Dv AI_NUMERICHOST
143 bit is set, it indicates that
144 .Fa nodename
145 should be treated as a numeric string defining an IPv4 or IPv6 address
146 and no name resolution should be attempted.
147 .It Dv AI_PASSIVE
148 If the
149 .Dv AI_PASSIVE
150 bit is set it indicates that the returned socket address structure
151 is intended for use in a call to
152 .Xr bind 2 .
153 In this case, if the
154 .Fa nodename
155 argument is the null pointer, then the IP address portion of the
156 socket address structure will be set to
157 .Dv INADDR_ANY
158 for an IPv4 address or
159 .Dv IN6ADDR_ANY_INIT
160 for an IPv6 address.
161 .Pp
162 If the
163 .Dv AI_PASSIVE
164 bit is not set, the returned socket address structure will be ready
165 for use in a call to
166 .Xr connect 2
167 for a connection-oriented protocol or
168 .Xr connect 2 ,
169 .Xr sendto 2 ,
170 or
171 .Xr sendmsg 2
172 if a connectionless protocol was chosen.
173 The
174 .Tn IP
175 address portion of the socket address structure will be set to the
176 loopback address if
177 .Fa nodename
178 is the null pointer and
179 .Dv AI_PASSIVE
180 is not set.
181 .El
182 .El
183 .Pp
184 All other elements of the
185 .Li addrinfo
186 structure passed via
187 .Fa hints
188 must be zero or the null pointer.
189 .Pp
190 If
191 .Fa hints
192 is the null pointer,
193 .Fn getaddrinfo
194 behaves as if the caller provided a
195 .Li struct addrinfo
196 with
197 .Fa ai_family
198 set to
199 .Dv PF_UNSPEC
200 and all other elements set to zero or
201 .Dv NULL .
202 .Pp
203 After a successful call to
204 .Fn getaddrinfo ,
205 .Fa *res
206 is a pointer to a linked list of one or more
207 .Li addrinfo
208 structures.
209 The list can be traversed by following the
210 .Fa ai_next
211 pointer in each
212 .Li addrinfo
213 structure until a null pointer is encountered.
214 The three members
215 .Fa ai_family,
216 .Fa ai_socktype,
217 and
218 .Fa ai_protocol
219 in each returned
220 .Li addrinfo
221 structure are suitable for a call to
222 .Xr socket 2 .
223 For each
224 .Li addrinfo
225 structure in the list, the
226 .Fa ai_addr
227 member points to a filled-in socket address structure of length
228 .Fa ai_addrlen .
229 .Pp
230 This implementation of
231 .Fn getaddrinfo
232 allows numeric IPv6 address notation with scope identifier,
233 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
234 By appending the percent character and scope identifier to addresses,
235 one can fill the
236 .Li sin6_scope_id
237 field for addresses.
238 This would make management of scoped addresses easier
239 and allows cut-and-paste input of scoped addresses.
240 .Pp
241 At this moment the code supports only link-local addresses with the format.
242 The scope identifier is hardcoded to the name of the hardware interface
243 associated
244 with the link
245 .Po
246 such as
247 .Li ne0
248 .Pc .
249 An example is
250 .Dq Li fe80::1%ne0 ,
251 which means
252 .Do
253 .Li fe80::1
254 on the link associated with the
255 .Li ne0
256 interface
257 .Dc .
258 .Pp
259 The current implementation assumes a one-to-one relationship between
260 the interface and link, which is not necessarily true from the specification.
261 .Pp
262 All of the information returned by
263 .Fn getaddrinfo
264 is dynamically allocated: the
265 .Li addrinfo
266 structures themselves as well as the socket address structures and
267 the canonical host name strings included in the
268 .Li addrinfo
269 structures.
270 .Pp
271 Memory allocated for the dynamically allocated structures created by
272 a successful call to
273 .Fn getaddrinfo
274 is released by the
275 .Fn freeaddrinfo
276 function.
277 The
278 .Fa ai
279 pointer should be a
280 .Li addrinfo
281 structure created by a call to
282 .Fn getaddrinfo .
283 .Sh RETURN VALUES
284 .Fn getaddrinfo
285 returns zero on success or one of the error codes listed in
286 .Xr gai_strerror 3
287 if an error occurs.
288 .Sh EXAMPLES
289 The following code tries to connect to
290 .Dq Li www.kame.net
291 service
292 .Dq Li http
293 via a stream socket.
294 It loops through all the addresses available, regardless of address family.
295 If the destination resolves to an IPv4 address, it will use an
296 .Dv AF_INET
297 socket.
298 Similarly, if it resolves to IPv6, an
299 .Dv AF_INET6
300 socket is used.
301 Observe that there is no hardcoded reference to a particular address family.
302 The code works even if
303 .Fn getaddrinfo
304 returns addresses that are not IPv4/v6.
305 .Bd -literal -offset indent
306 struct addrinfo hints, *res, *res0;
307 int error;
308 int s;
309 const char *cause = NULL;
310
311 memset(&hints, 0, sizeof(hints));
312 hints.ai_family = PF_UNSPEC;
313 hints.ai_socktype = SOCK_STREAM;
314 error = getaddrinfo("www.kame.net", "http", &hints, &res0);
315 if (error) {
316         errx(1, "%s", gai_strerror(error));
317         /*NOTREACHED*/
318 }
319 s = -1;
320 for (res = res0; res; res = res->ai_next) {
321         s = socket(res->ai_family, res->ai_socktype,
322             res->ai_protocol);
323         if (s < 0) {
324                 cause = "socket";
325                 continue;
326         }
327
328         if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
329                 cause = "connect";
330                 close(s);
331                 s = -1;
332                 continue;
333         }
334
335         break;  /* okay we got one */
336 }
337 if (s < 0) {
338         err(1, "%s", cause);
339         /*NOTREACHED*/
340 }
341 freeaddrinfo(res0);
342 .Ed
343 .Pp
344 The following example tries to open a wildcard listening socket onto service
345 .Dq Li http ,
346 for all the address families available.
347 .Bd -literal -offset indent
348 struct addrinfo hints, *res, *res0;
349 int error;
350 int s[MAXSOCK];
351 int nsock;
352 const char *cause = NULL;
353
354 memset(&hints, 0, sizeof(hints));
355 hints.ai_family = PF_UNSPEC;
356 hints.ai_socktype = SOCK_STREAM;
357 hints.ai_flags = AI_PASSIVE;
358 error = getaddrinfo(NULL, "http", &hints, &res0);
359 if (error) {
360         errx(1, "%s", gai_strerror(error));
361         /*NOTREACHED*/
362 }
363 nsock = 0;
364 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
365         s[nsock] = socket(res->ai_family, res->ai_socktype,
366             res->ai_protocol);
367         if (s[nsock] < 0) {
368                 cause = "socket";
369                 continue;
370         }
371
372         if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
373                 cause = "bind";
374                 close(s[nsock]);
375                 continue;
376         }
377         (void) listen(s[nsock], 5);
378
379         nsock++;
380 }
381 if (nsock == 0) {
382         err(1, "%s", cause);
383         /*NOTREACHED*/
384 }
385 freeaddrinfo(res0);
386 .Ed
387 .Sh LEGACY SYNOPSIS
388 .Fd #include <sys/types.h>
389 .Fd #include <sys/socket.h>
390 .Fd #include <netdb.h>
391 .Pp
392 The include file
393 .In sys/types.h
394 is necessary.
395 .Sh SEE ALSO
396 .Xr bind 2 ,
397 .Xr connect 2 ,
398 .Xr send 2 ,
399 .Xr socket 2 ,
400 .Xr gai_strerror 3 ,
401 .Xr gethostbyname 3 ,
402 .Xr getnameinfo 3 ,
403 .Xr getservbyname 3 ,
404 .Xr resolver 3 ,
405 .Xr hosts 5 ,
406 .Xr resolv.conf 5 ,
407 .Xr services 5 ,
408 .Xr hostname 7 ,
409 .Xr named 8
410 .Rs
411 .%A R. Gilligan
412 .%A S. Thomson
413 .%A J. Bound
414 .%A J. McCann
415 .%A W. Stevens
416 .%T Basic Socket Interface Extensions for IPv6
417 .%R RFC 3493
418 .%D February 2003
419 .Re
420 .Rs
421 .%A S. Deering
422 .%A B. Haberman
423 .%A T. Jinmei
424 .%A E. Nordmark
425 .%A B. Zill
426 .%T "IPv6 Scoped Address Architecture"
427 .%R internet draft
428 .%N draft-ietf-ipv6-scoping-arch-02.txt
429 .%O work in progress material
430 .Re
431 .Rs
432 .%A Craig Metz
433 .%T Protocol Independence Using the Sockets API
434 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
435 .%D June 2000
436 .Re
437 .Sh STANDARDS
438 The
439 .Fn getaddrinfo
440 function is defined by the
441 .St -p1003.1g-2000
442 draft specification and documented in
443 .Dv "RFC 3493" ,
444 .Dq Basic Socket Interface Extensions for IPv6 .