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