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