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