summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/getaddrinfo.3
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libc/net/getaddrinfo.3531
1 files changed, 0 insertions, 531 deletions
diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3
deleted file mode 100644
index e74fdf6ed7..0000000000
--- a/src/lib/libc/net/getaddrinfo.3
+++ /dev/null
@@ -1,531 +0,0 @@
1.\" $OpenBSD: getaddrinfo.3,v 1.29 2004/12/06 10:46:35 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.Nd nodename-to-address translation in protocol-independent manner
41.\"
42.Sh SYNOPSIS
43.Fd #include <sys/types.h>
44.Fd #include <sys/socket.h>
45.Fd #include <netdb.h>
46.Ft int
47.Fn getaddrinfo "const char *nodename" "const char *servname" \
48"const struct addrinfo *hints" "struct addrinfo **res"
49.Ft void
50.Fn freeaddrinfo "struct addrinfo *ai"
51.\"
52.Sh DESCRIPTION
53The
54.Fn getaddrinfo
55function is defined for protocol-independent nodename-to-address translation.
56It performs the functionality of
57.Xr gethostbyname 3
58and
59.Xr getservbyname 3 ,
60but in a more sophisticated manner.
61.Pp
62All of the information returned by
63.Fn getaddrinfo
64is dynamically allocated:
65the
66.Li addrinfo
67structures, the socket address structures, and canonical node name
68strings pointed to by the addrinfo structures.
69To return this information to the system the function
70.Fn freeaddrinfo
71is called.
72The
73.Fa addrinfo
74structure pointed to by the
75.Fa ai
76argument is freed,
77along with any dynamic storage pointed to by the structure.
78This operation is repeated until a
79.Dv NULL
80.Fa ai_next
81pointer is encountered.
82.Pp
83To aid applications in printing error messages based on the
84.Dv EAI_xxx
85codes returned by
86.Fn getaddrinfo ,
87.Fn gai_strerror
88is defined.
89See
90.Xr gai_strerror 3
91for more information.
92.Pp
93The implementation allows experimental numeric IPv6 address notation with
94scope identifier.
95By appending the percent character and scope identifier to addresses,
96you can fill the
97.Li sin6_scope_id
98field for addresses.
99This would make management of scoped address easier,
100and allows cut-and-paste input of scoped address.
101.Pp
102At this moment the code supports only link-local addresses with the format.
103Scope identifier is hardcoded to the name of the hardware interface associated
104with the link
105.Po
106such as
107.Li ne0
108.Pc .
109An example is
110.Dq Li fe80::1%ne0 ,
111which means
112.Do
113.Li fe80::1
114on the link associated with the
115.Li ne0
116interface
117.Dc .
118.Pp
119The IPv6 implementation is still very experimental and non-standard.
120The current implementation assumes a one-to-one relationship between
121the interface and link, which is not necessarily true from the specification.
122.Pp
123The
124.Li addrinfo
125structure is defined as a result of including the
126.Aq Pa netdb.h
127header:
128.Bd -literal
129struct addrinfo {
130 int ai_flags; /* input flags */
131 int ai_family; /* protocol family for socket */
132 int ai_socktype; /* socket type */
133 int ai_protocol; /* protocol for socket */
134 socklen_t ai_addrlen; /* length of socket-address */
135 struct sockaddr *ai_addr; /* socket-address for socket */
136 char *ai_canonname; /* canonical name for service location */
137 struct addrinfo *ai_next; /* pointer to next in list */
138};
139.Ed
140.Pp
141The
142.Fa nodename
143and
144.Fa servname
145arguments are pointers to NUL-terminated strings or
146.Dv NULL .
147One or both of these two arguments must be a non-null pointer.
148In the normal client scenario, both the
149.Fa nodename
150and
151.Fa servname
152are specified.
153In the normal server scenario, only the
154.Fa servname
155is specified.
156A non-null
157.Fa nodename
158string can be either a node name or a numeric host address string
159(i.e., a dotted-decimal IPv4 address or an IPv6 hex address).
160A non-null
161.Fa servname
162string can be either a service name or a decimal port number.
163.Pp
164The caller can optionally pass an
165.Li addrinfo
166structure, pointed to by the third argument,
167to provide hints concerning the type of socket that the caller supports.
168In this
169.Fa hints
170structure all members other than
171.Fa ai_flags ,
172.Fa ai_family ,
173.Fa ai_socktype ,
174and
175.Fa ai_protocol
176must be zero or a null pointer.
177A value of
178.Dv PF_UNSPEC
179for
180.Fa ai_family
181means the caller will accept any protocol family.
182A value of 0 for
183.Fa ai_socktype
184means the caller will accept any socket type.
185A value of 0 for
186.Fa ai_protocol
187means the caller will accept any protocol.
188For example, if the caller handles only TCP and not UDP, then the
189.Fa ai_socktype
190member of the hints structure should be set to
191.Dv SOCK_STREAM
192when
193.Fn getaddrinfo
194is called.
195If the caller handles only IPv4 and not IPv6, then the
196.Fa ai_family
197member of the
198.Fa hints
199structure should be set to
200.Dv PF_INET
201when
202.Fn getaddrinfo
203is called.
204If the third argument to
205.Fn getaddrinfo
206is a null pointer, this is the same as if the caller had filled in an
207.Li addrinfo
208structure initialized to zero with
209.Fa ai_family
210set to
211.Dv PF_UNSPEC .
212.Pp
213Upon successful return a pointer to a linked list of one or more
214.Li addrinfo
215structures is returned through the final argument.
216The caller can process each
217.Li addrinfo
218structure in this list by following the
219.Fa ai_next
220pointer, until a null pointer is encountered.
221In each returned
222.Li addrinfo
223structure the three members
224.Fa ai_family ,
225.Fa ai_socktype ,
226and
227.Fa ai_protocol
228are the corresponding arguments for a call to the
229.Xr socket 2
230function.
231In each
232.Li addrinfo
233structure the
234.Fa ai_addr
235member points to a filled-in socket address structure whose length is
236specified by the
237.Fa ai_addrlen
238member.
239.Pp
240The
241.Fa ai_flags
242argument can be set to any of the following values, OR'd together:
243.Bl -tag -width "AI_NUMERICHOSTXX"
244.It AI_PASSIVE
245If the
246.Dv AI_PASSIVE
247bit is set,
248the caller plans to use the returned socket address
249structure in a call to
250.Xr bind 2 .
251In this case, if the
252.Fa nodename
253argument is a null pointer, then the IP address portion of the socket
254address structure will be set to
255.Dv INADDR_ANY
256for an IPv4 address or
257.Dv IN6ADDR_ANY_INIT
258for an IPv6 address.
259.Pp
260If the
261.Dv AI_PASSIVE
262bit is not set,
263the returned socket address structure will be ready for a
264call to
265.Xr connect 2
266.Pq for a connection-oriented protocol
267or either
268.Xr connect 2 ,
269.Xr sendto 2 ,
270or
271.Xr sendmsg 2
272.Pq for a connectionless protocol .
273In this case, if the
274.Fa nodename
275argument is a null pointer, then the IP address portion of the
276socket address structure will be set to the loopback address.
277.It AI_CANONNAME
278If the
279.Dv AI_CANONNAME
280bit is set,
281then upon successful return the
282.Fa ai_canonname
283member of the first
284.Li addrinfo
285structure in the linked list will point to a NUL-terminated string
286containing the canonical name of the specified
287.Fa nodename .
288.It AI_NUMERICHOST
289If the
290.Dv AI_NUMERICHOST
291bit is set,
292then a non-null
293.Fa nodename
294string must be a numeric host address string.
295Otherwise an error of
296.Dv EAI_NONAME
297is returned.
298This flag prevents any type of name resolution service,
299such as DNS,
300from being called.
301.It AI_NUMERICSERV
302If the
303.Dv AI_NUMERICSERV
304bit is set,
305then a non-null
306.Fa servname
307string must be a numeric port string.
308Otherwise an error of
309.Dv EAI_NONAME
310is returned.
311This flag prevents any type of name resolution service,
312such as NIS,
313from being called.
314.El
315.Pp
316The arguments to
317.Fn getaddrinfo
318must be sufficiently consistent and unambiguous.
319Issues to watch out for are:
320.Bl -bullet
321.It
322.Fn getaddrinfo
323will raise an error if members of the
324.Fa hints
325structure are not consistent.
326For example, for internet address families,
327.Fn getaddrinfo
328will raise an error if you specify
329.Dv SOCK_STREAM
330to
331.Fa ai_socktype
332while you specify
333.Dv IPPROTO_UDP
334to
335.Fa ai_protocol .
336.It
337If you specify a
338.Fa servname
339which is defined only for certain
340.Fa ai_socktype ,
341.Fn getaddrinfo
342will raise an error because the arguments are not consistent.
343For example,
344.Fn getaddrinfo
345will raise an error if you ask for
346.Dq Li tftp
347service on
348.Dv SOCK_STREAM .
349.It
350For internet address families, if you specify
351.Fa servname
352while you set
353.Fa ai_socktype
354to
355.Dv SOCK_RAW ,
356.Fn getaddrinfo
357will raise an error, because service names are not defined for the internet
358.Dv SOCK_RAW
359space.
360.It
361If you specify a numeric
362.Fa servname ,
363while leaving
364.Fa ai_socktype
365and
366.Fa ai_protocol
367unspecified,
368.Fn getaddrinfo
369will raise an error.
370This is because the numeric
371.Fa servname
372does not identify any socket type, and
373.Fn getaddrinfo
374is not allowed to glob the argument in such case.
375.El
376.\"
377.Sh RETURN VALUES
378.Fn getaddrinfo
379returns zero on success, and non-zero on errors.
380See
381.Xr gai_strerror 3
382for a description of the non-zero error codes.
383.\"
384.Sh EXAMPLES
385The following code tries to connect to
386.Dq Li www.kame.net
387service
388.Dq Li http .
389via stream socket.
390It loops through all the addresses available, regardless of address family.
391If the destination resolves to an IPv4 address, it will use
392.Dv AF_INET
393socket.
394Similarly, if it resolves to IPv6,
395.Dv AF_INET6
396socket is used.
397Observe that there is no hardcoded reference to a particular address family.
398The code works even if
399.Nm getaddrinfo
400returns addresses that are not IPv4/v6.
401.Bd -literal -offset indent
402struct addrinfo hints, *res, *res0;
403int error;
404int s;
405const char *cause = NULL;
406
407memset(&hints, 0, sizeof(hints));
408hints.ai_family = PF_UNSPEC;
409hints.ai_socktype = SOCK_STREAM;
410error = getaddrinfo("www.kame.net", "http", &hints, &res0);
411if (error) {
412 errx(1, "%s", gai_strerror(error));
413 /*NOTREACHED*/
414}
415s = -1;
416for (res = res0; res; res = res->ai_next) {
417 s = socket(res->ai_family, res->ai_socktype,
418 res->ai_protocol);
419 if (s < 0) {
420 cause = "socket";
421 continue;
422 }
423
424 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
425 cause = "connect";
426 close(s);
427 s = -1;
428 continue;
429 }
430
431 break; /* okay we got one */
432}
433if (s < 0) {
434 err(1, "%s", cause);
435 /*NOTREACHED*/
436}
437freeaddrinfo(res0);
438.Ed
439.Pp
440The following example tries to open a wildcard listening socket onto service
441.Dq Li http ,
442for all the address families available.
443.Bd -literal -offset indent
444struct addrinfo hints, *res, *res0;
445int error;
446int s[MAXSOCK];
447int nsock;
448const char *cause = NULL;
449
450memset(&hints, 0, sizeof(hints));
451hints.ai_family = PF_UNSPEC;
452hints.ai_socktype = SOCK_STREAM;
453hints.ai_flags = AI_PASSIVE;
454error = getaddrinfo(NULL, "http", &hints, &res0);
455if (error) {
456 errx(1, "%s", gai_strerror(error));
457 /*NOTREACHED*/
458}
459nsock = 0;
460for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
461 s[nsock] = socket(res->ai_family, res->ai_socktype,
462 res->ai_protocol);
463 if (s[nsock] < 0) {
464 cause = "socket";
465 continue;
466 }
467
468 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
469 cause = "bind";
470 close(s[nsock]);
471 continue;
472 }
473 (void) listen(s[nsock], 5);
474
475 nsock++;
476}
477if (nsock == 0) {
478 err(1, "%s", cause);
479 /*NOTREACHED*/
480}
481freeaddrinfo(res0);
482.Ed
483.\"
484.Sh SEE ALSO
485.Xr gai_strerror 3 ,
486.Xr gethostbyname 3 ,
487.Xr getnameinfo 3 ,
488.Xr getservbyname 3 ,
489.Xr hosts 5 ,
490.Xr resolv.conf 5 ,
491.Xr services 5 ,
492.Xr hostname 7 ,
493.Xr named 8
494.Rs
495.%A R. Gilligan
496.%A S. Thomson
497.%A J. Bound
498.%A J. McCann
499.%A W. Stevens
500.%T Basic Socket Interface Extensions for IPv6
501.%R RFC 3493
502.%D February 2003
503.Re
504.Rs
505.%A Tatsuya Jinmei
506.%A Atsushi Onoe
507.%T "An Extension of Format for IPv6 Scoped Addresses"
508.%R internet draft
509.%N draft-ietf-ipngwg-scopedaddr-format-02.txt
510.%O work in progress material
511.Re
512.Rs
513.%A Craig Metz
514.%T Protocol Independence Using the Sockets API
515.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
516.%D June 2000
517.Re
518.\"
519.Sh STANDARDS
520The
521.Fn getaddrinfo
522function is defined in IEEE POSIX 1003.1g draft specification,
523and documented in
524.Dq Basic Socket Interface Extensions for IPv6
525.Pq RFC 3493 .
526.\"
527.Sh HISTORY
528The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
529.\"
530.Sh BUGS
531The current implementation is not thread-safe.