summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/getaddrinfo.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/getaddrinfo.3')
-rw-r--r--src/lib/libc/net/getaddrinfo.3577
1 files changed, 577 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..a9004e3207
--- /dev/null
+++ b/src/lib/libc/net/getaddrinfo.3
@@ -0,0 +1,577 @@
1.\" $OpenBSD: getaddrinfo.3,v 1.25 2003/08/08 09:26:02 jmc Exp $
2.\" $KAME: getaddrinfo.3,v 1.29 2001/02/12 09:24:45 itojun Exp $
3.\"
4.\" Copyright (c) 1983, 1987, 1991, 1993
5.\" The Regents of the University of California. All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\" notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\" notice, this list of conditions and the following disclaimer in the
14.\" documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
32.\"
33.Dd May 25, 1995
34.Dt GETADDRINFO 3
35.Os
36.\"
37.Sh NAME
38.Nm getaddrinfo ,
39.Nm freeaddrinfo ,
40.Nm gai_strerror
41.Nd nodename-to-address translation in protocol-independent manner
42.\"
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <sys/socket.h>
46.Fd #include <netdb.h>
47.Ft int
48.Fn getaddrinfo "const char *nodename" "const char *servname" \
49"const struct addrinfo *hints" "struct addrinfo **res"
50.Ft void
51.Fn freeaddrinfo "struct addrinfo *ai"
52.Ft "char *"
53.Fn gai_strerror "int ecode"
54.\"
55.Sh DESCRIPTION
56The
57.Fn getaddrinfo
58function is defined for protocol-independent nodename-to-address translation.
59It performs the functionality of
60.Xr gethostbyname 3
61and
62.Xr getservbyname 3 ,
63but in a more sophisticated manner.
64.Pp
65The
66.Li addrinfo
67structure is defined as a result of including the
68.Aq Pa netdb.h
69header:
70.Bd -literal
71struct addrinfo {
72 int ai_flags; /* input flags */
73 int ai_family; /* protocol family for socket */
74 int ai_socktype; /* socket type */
75 int ai_protocol; /* protocol for socket */
76 socklen_t ai_addrlen; /* length of socket-address */
77 struct sockaddr *ai_addr; /* socket-address for socket */
78 char *ai_canonname; /* canonical name for service location */
79 struct addrinfo *ai_next; /* pointer to next in list */
80};
81.Ed
82.Pp
83The
84.Fa nodename
85and
86.Fa servname
87arguments are pointers to NUL-terminated strings or
88.Dv NULL .
89One or both of these two arguments must be a non-null pointer.
90In the normal client scenario, both the
91.Fa nodename
92and
93.Fa servname
94are specified.
95In the normal server scenario, only the
96.Fa servname
97is specified.
98A non-null
99.Fa nodename
100string can be either a node name or a numeric host address string
101(i.e., a dotted-decimal IPv4 address or an IPv6 hex address).
102A non-null
103.Fa servname
104string can be either a service name or a decimal port number.
105.Pp
106The caller can optionally pass an
107.Li addrinfo
108structure, pointed to by the third argument,
109to provide hints concerning the type of socket that the caller supports.
110In this
111.Fa hints
112structure all members other than
113.Fa ai_flags ,
114.Fa ai_family ,
115.Fa ai_socktype ,
116and
117.Fa ai_protocol
118must be zero or a null pointer.
119A value of
120.Dv PF_UNSPEC
121for
122.Fa ai_family
123means the caller will accept any protocol family.
124A value of 0 for
125.Fa ai_socktype
126means the caller will accept any socket type.
127A value of 0 for
128.Fa ai_protocol
129means the caller will accept any protocol.
130For example, if the caller handles only TCP and not UDP, then the
131.Fa ai_socktype
132member of the hints structure should be set to
133.Dv SOCK_STREAM
134when
135.Fn getaddrinfo
136is called.
137If the caller handles only IPv4 and not IPv6, then the
138.Fa ai_family
139member of the
140.Fa hints
141structure should be set to
142.Dv PF_INET
143when
144.Fn getaddrinfo
145is called.
146If the third argument to
147.Fn getaddrinfo
148is a null pointer, this is the same as if the caller had filled in an
149.Li addrinfo
150structure initialized to zero with
151.Fa ai_family
152set to
153.Dv PF_UNSPEC .
154.Pp
155Upon successful return a pointer to a linked list of one or more
156.Li addrinfo
157structures is returned through the final argument.
158The caller can process each
159.Li addrinfo
160structure in this list by following the
161.Fa ai_next
162pointer, until a null pointer is encountered.
163In each returned
164.Li addrinfo
165structure the three members
166.Fa ai_family ,
167.Fa ai_socktype ,
168and
169.Fa ai_protocol
170are the corresponding arguments for a call to the
171.Fn socket
172function.
173In each
174.Li addrinfo
175structure the
176.Fa ai_addr
177member points to a filled-in socket address structure whose length is
178specified by the
179.Fa ai_addrlen
180member.
181.Pp
182If the
183.Dv AI_PASSIVE
184bit is set in the
185.Fa ai_flags
186member of the
187.Fa hints
188structure, then the caller plans to use the returned socket address
189structure in a call to
190.Fn bind .
191In this case, if the
192.Fa nodename
193argument is a null pointer, then the IP address portion of the socket
194address structure will be set to
195.Dv INADDR_ANY
196for an IPv4 address or
197.Dv IN6ADDR_ANY_INIT
198for an IPv6 address.
199.Pp
200If the
201.Dv AI_PASSIVE
202bit is not set in the
203.Fa ai_flags
204member of the
205.Fa hints
206structure, then the returned socket address structure will be ready for a
207call to
208.Fn connect
209.Pq for a connection-oriented protocol
210or either
211.Fn connect ,
212.Fn sendto ,
213or
214.Fn sendmsg
215.Pq for a connectionless protocol .
216In this case, if the
217.Fa nodename
218argument is a null pointer, then the IP address portion of the
219socket address structure will be set to the loopback address.
220.Pp
221If the
222.Dv AI_CANONNAME
223bit is set in the
224.Fa ai_flags
225member of the
226.Fa hints
227structure, then upon successful return the
228.Fa ai_canonname
229member of the first
230.Li addrinfo
231structure in the linked list will point to a NUL-terminated string
232containing the canonical name of the specified
233.Fa nodename .
234.Pp
235If the
236.Dv AI_NUMERICHOST
237bit is set in the
238.Fa ai_flags
239member of the
240.Fa hints
241structure, then a non-null
242.Fa nodename
243string must be a numeric host address string.
244Otherwise an error of
245.Dv EAI_NONAME
246is returned.
247This flag prevents any type of name resolution service (e.g., the DNS)
248from being called.
249.Pp
250The arguments to
251.Fn getaddrinfo
252must sufficiently be consistent and unambiguous.
253Here are pitfall cases you may encounter:
254.Bl -bullet
255.It
256.Fn getaddrinfo
257will raise an error if members of the
258.Fa hints
259structure are not consistent.
260For example, for internet address families,
261.Fn getaddrinfo
262will raise an error if you specify
263.Dv SOCK_STREAM
264to
265.Fa ai_socktype
266while you specify
267.Dv IPPROTO_UDP
268to
269.Fa ai_protocol .
270.It
271If you specify a
272.Fa servname
273which is defined only for certain
274.Fa ai_socktype ,
275.Fn getaddrinfo
276will raise an error because the arguments are not consistent.
277For example,
278.Fn getaddrinfo
279will raise an error if you ask for
280.Dq Li tftp
281service on
282.Dv SOCK_STREAM .
283.It
284For internet address families, if you specify
285.Fa servname
286while you set
287.Fa ai_socktype
288to
289.Dv SOCK_RAW ,
290.Fn getaddrinfo
291will raise an error, because service names are not defined for the internet
292.Dv SOCK_RAW
293space.
294.It
295If you specify a numeric
296.Fa servname ,
297while leaving
298.Fa ai_socktype
299and
300.Fa ai_protocol
301unspecified,
302.Fn getaddrinfo
303will raise an error.
304This is because the numeric
305.Fa servname
306does not identify any socket type, and
307.Fn getaddrinfo
308is not allowed to glob the argument in such case.
309.El
310.Pp
311All of the information returned by
312.Fn getaddrinfo
313is dynamically allocated:
314the
315.Li addrinfo
316structures, the socket address structures, and canonical node name
317strings pointed to by the addrinfo structures.
318To return this information to the system the function
319.Fn freeaddrinfo
320is called.
321The
322.Fa addrinfo
323structure pointed to by the
324.Fa ai argument
325is freed, along with any dynamic storage pointed to by the structure.
326This operation is repeated until a
327.Dv NULL
328.Fa ai_next
329pointer is encountered.
330.Pp
331To aid applications in printing error messages based on the
332.Dv EAI_xxx
333codes returned by
334.Fn getaddrinfo ,
335.Fn gai_strerror
336is defined.
337The argument is one of the
338.Dv EAI_xxx
339values defined earlier and the return value points to a string describing
340the error.
341If the argument is not one of the
342.Dv EAI_xxx
343values, the function still returns a pointer to a string whose contents
344indicate an unknown error.
345.\"
346.Ss Extension for scoped IPv6 address
347The implementation allows experimental numeric IPv6 address notation with
348scope identifier.
349By appending the percent character and scope identifier to addresses,
350you can fill the
351.Li sin6_scope_id
352field for addresses.
353This would make management of scoped address easier,
354and allows cut-and-paste input of scoped address.
355.Pp
356At this moment the code supports only link-local addresses with the format.
357Scope identifier is hardcoded to the name of the hardware interface associated
358with the link.
359.Po
360such as
361.Li ne0
362.Pc .
363An example is
364.Dq Li fe80::1%ne0 ,
365which means
366.Do
367.Li fe80::1
368on the link associated with the
369.Li ne0
370interface
371.Dc .
372.Pp
373The implementation is still very experimental and non-standard.
374The current implementation assumes a one-to-one relationship between
375the interface and link, which is not necessarily true from the specification.
376.\"
377.Sh EXAMPLES
378The following code tries to connect to
379.Dq Li www.kame.net
380service
381.Dq Li http .
382via stream socket.
383It loops through all the addresses available, regardless of address family.
384If the destination resolves to an IPv4 address, it will use
385.Dv AF_INET
386socket.
387Similarly, if it resolves to IPv6,
388.Dv AF_INET6
389socket is used.
390Observe that there is no hardcoded reference to a particular address family.
391The code works even if
392.Nm getaddrinfo
393returns addresses that are not IPv4/v6.
394.Bd -literal -offset indent
395struct addrinfo hints, *res, *res0;
396int error;
397int s;
398const char *cause = NULL;
399
400memset(&hints, 0, sizeof(hints));
401hints.ai_family = PF_UNSPEC;
402hints.ai_socktype = SOCK_STREAM;
403error = getaddrinfo("www.kame.net", "http", &hints, &res0);
404if (error) {
405 errx(1, "%s", gai_strerror(error));
406 /*NOTREACHED*/
407}
408s = -1;
409for (res = res0; res; res = res->ai_next) {
410 s = socket(res->ai_family, res->ai_socktype,
411 res->ai_protocol);
412 if (s < 0) {
413 cause = "socket";
414 continue;
415 }
416
417 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
418 cause = "connect";
419 close(s);
420 s = -1;
421 continue;
422 }
423
424 break; /* okay we got one */
425}
426if (s < 0) {
427 err(1, cause);
428 /*NOTREACHED*/
429}
430freeaddrinfo(res0);
431.Ed
432.Pp
433The following example tries to open a wildcard listening socket onto service
434.Dq Li http ,
435for all the address families available.
436.Bd -literal -offset indent
437struct addrinfo hints, *res, *res0;
438int error;
439int s[MAXSOCK];
440int nsock;
441const char *cause = NULL;
442
443memset(&hints, 0, sizeof(hints));
444hints.ai_family = PF_UNSPEC;
445hints.ai_socktype = SOCK_STREAM;
446hints.ai_flags = AI_PASSIVE;
447error = getaddrinfo(NULL, "http", &hints, &res0);
448if (error) {
449 errx(1, "%s", gai_strerror(error));
450 /*NOTREACHED*/
451}
452nsock = 0;
453for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
454 s[nsock] = socket(res->ai_family, res->ai_socktype,
455 res->ai_protocol);
456 if (s[nsock] < 0) {
457 cause = "socket";
458 continue;
459 }
460
461 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
462 cause = "bind";
463 close(s[nsock]);
464 continue;
465 }
466 (void) listen(s[nsock], 5);
467
468 nsock++;
469}
470if (nsock == 0) {
471 err(1, cause);
472 /*NOTREACHED*/
473}
474freeaddrinfo(res0);
475.Ed
476.\"
477.Sh DIAGNOSTICS
478Error return status from
479.Fn getaddrinfo
480is zero on success and non-zero on errors.
481Non-zero error codes are defined in
482.Aq Pa netdb.h ,
483and as follows:
484.Pp
485.Bl -tag -width EAI_ADDRFAMILY -compact
486.It Dv EAI_ADDRFAMILY
487Address family for
488.Fa nodename
489not supported.
490.It Dv EAI_AGAIN
491Temporary failure in name resolution.
492.It Dv EAI_BADFLAGS
493Invalid value for
494.Fa ai_flags .
495.It Dv EAI_FAIL
496Non-recoverable failure in name resolution.
497.It Dv EAI_FAMILY
498.Fa ai_family
499not supported.
500.It Dv EAI_MEMORY
501Memory allocation failure.
502.It Dv EAI_NODATA
503No address associated with
504.Fa nodename .
505.It Dv EAI_NONAME
506.Fa nodename
507nor
508.Fa servname
509provided, or not known.
510.It Dv EAI_SERVICE
511.Fa servname
512not supported for
513.Fa ai_socktype .
514.It Dv EAI_SOCKTYPE
515.Fa ai_socktype
516not supported.
517.It Dv EAI_SYSTEM
518System error returned in
519.Va errno .
520.El
521.Pp
522If called with proper argument,
523.Fn gai_strerror
524returns a pointer to a string describing the given error code.
525If the argument is not one of the
526.Dv EAI_xxx
527values, the function still returns a pointer to a string whose contents
528indicate an unknown error.
529.\"
530.Sh SEE ALSO
531.Xr gethostbyname 3 ,
532.Xr getnameinfo 3 ,
533.Xr getservbyname 3 ,
534.Xr hosts 5 ,
535.Xr resolv.conf 5 ,
536.Xr services 5 ,
537.Xr hostname 7 ,
538.Xr named 8
539.Rs
540.%A R. Gilligan
541.%A S. Thomson
542.%A J. Bound
543.%A W. Stevens
544.%T Basic Socket Interface Extensions for IPv6
545.%R RFC 2553
546.%D March 1999
547.Re
548.Rs
549.%A Tatsuya Jinmei
550.%A Atsushi Onoe
551.%T "An Extension of Format for IPv6 Scoped Addresses"
552.%R internet draft
553.%N draft-ietf-ipngwg-scopedaddr-format-02.txt
554.%O work in progress material
555.Re
556.Rs
557.%A Craig Metz
558.%T Protocol Independence Using the Sockets API
559.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
560.%D June 2000
561.Re
562.\"
563.Sh STANDARDS
564The
565.Fn getaddrinfo
566function is defined in IEEE POSIX 1003.1g draft specification,
567and documented in
568.Dq Basic Socket Interface Extensions for IPv6
569.Pq RFC 2553 .
570.\"
571.Sh HISTORY
572The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
573.\"
574.Sh BUGS
575The current implementation is not thread-safe.
576.Pp
577The text was shamelessly copied from RFC 2553.