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