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.3264
1 files changed, 101 insertions, 163 deletions
diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3
index ed8b082970..e74fdf6ed7 100644
--- a/src/lib/libc/net/getaddrinfo.3
+++ b/src/lib/libc/net/getaddrinfo.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: getaddrinfo.3,v 1.28 2004/04/14 10:06:03 jmc Exp $ 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 $ 2.\" $KAME: getaddrinfo.3,v 1.29 2001/02/12 09:24:45 itojun Exp $
3.\" 3.\"
4.\" Copyright (c) 1983, 1987, 1991, 1993 4.\" Copyright (c) 1983, 1987, 1991, 1993
@@ -36,8 +36,7 @@
36.\" 36.\"
37.Sh NAME 37.Sh NAME
38.Nm getaddrinfo , 38.Nm getaddrinfo ,
39.Nm freeaddrinfo , 39.Nm freeaddrinfo
40.Nm gai_strerror
41.Nd nodename-to-address translation in protocol-independent manner 40.Nd nodename-to-address translation in protocol-independent manner
42.\" 41.\"
43.Sh SYNOPSIS 42.Sh SYNOPSIS
@@ -49,8 +48,6 @@
49"const struct addrinfo *hints" "struct addrinfo **res" 48"const struct addrinfo *hints" "struct addrinfo **res"
50.Ft void 49.Ft void
51.Fn freeaddrinfo "struct addrinfo *ai" 50.Fn freeaddrinfo "struct addrinfo *ai"
52.Ft "char *"
53.Fn gai_strerror "int ecode"
54.\" 51.\"
55.Sh DESCRIPTION 52.Sh DESCRIPTION
56The 53The
@@ -62,6 +59,67 @@ and
62.Xr getservbyname 3 , 59.Xr getservbyname 3 ,
63but in a more sophisticated manner. 60but in a more sophisticated manner.
64.Pp 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
65The 123The
66.Li addrinfo 124.Li addrinfo
67structure is defined as a result of including the 125structure is defined as a result of including the
@@ -168,7 +226,7 @@ structure the three members
168and 226and
169.Fa ai_protocol 227.Fa ai_protocol
170are the corresponding arguments for a call to the 228are the corresponding arguments for a call to the
171.Fn socket 229.Xr socket 2
172function. 230function.
173In each 231In each
174.Li addrinfo 232.Li addrinfo
@@ -179,15 +237,17 @@ specified by the
179.Fa ai_addrlen 237.Fa ai_addrlen
180member. 238member.
181.Pp 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
182If the 245If the
183.Dv AI_PASSIVE 246.Dv AI_PASSIVE
184bit is set in the 247bit is set,
185.Fa ai_flags 248the caller plans to use the returned socket address
186member of the
187.Fa hints
188structure, then the caller plans to use the returned socket address
189structure in a call to 249structure in a call to
190.Fn bind . 250.Xr bind 2 .
191In this case, if the 251In this case, if the
192.Fa nodename 252.Fa nodename
193argument is a null pointer, then the IP address portion of the socket 253argument is a null pointer, then the IP address portion of the socket
@@ -199,73 +259,64 @@ for an IPv6 address.
199.Pp 259.Pp
200If the 260If the
201.Dv AI_PASSIVE 261.Dv AI_PASSIVE
202bit is not set in the 262bit is not set,
203.Fa ai_flags 263the returned socket address structure will be ready for a
204member of the
205.Fa hints
206structure, then the returned socket address structure will be ready for a
207call to 264call to
208.Fn connect 265.Xr connect 2
209.Pq for a connection-oriented protocol 266.Pq for a connection-oriented protocol
210or either 267or either
211.Fn connect , 268.Xr connect 2 ,
212.Fn sendto , 269.Xr sendto 2 ,
213or 270or
214.Fn sendmsg 271.Xr sendmsg 2
215.Pq for a connectionless protocol . 272.Pq for a connectionless protocol .
216In this case, if the 273In this case, if the
217.Fa nodename 274.Fa nodename
218argument is a null pointer, then the IP address portion of the 275argument is a null pointer, then the IP address portion of the
219socket address structure will be set to the loopback address. 276socket address structure will be set to the loopback address.
220.Pp 277.It AI_CANONNAME
221If the 278If the
222.Dv AI_CANONNAME 279.Dv AI_CANONNAME
223bit is set in the 280bit is set,
224.Fa ai_flags 281then upon successful return the
225member of the
226.Fa hints
227structure, then upon successful return the
228.Fa ai_canonname 282.Fa ai_canonname
229member of the first 283member of the first
230.Li addrinfo 284.Li addrinfo
231structure in the linked list will point to a NUL-terminated string 285structure in the linked list will point to a NUL-terminated string
232containing the canonical name of the specified 286containing the canonical name of the specified
233.Fa nodename . 287.Fa nodename .
234.Pp 288.It AI_NUMERICHOST
235If the 289If the
236.Dv AI_NUMERICHOST 290.Dv AI_NUMERICHOST
237bit is set in the 291bit is set,
238.Fa ai_flags 292then a non-null
239member of the
240.Fa hints
241structure, then a non-null
242.Fa nodename 293.Fa nodename
243string must be a numeric host address string. 294string must be a numeric host address string.
244Otherwise an error of 295Otherwise an error of
245.Dv EAI_NONAME 296.Dv EAI_NONAME
246is returned. 297is returned.
247This flag prevents any type of name resolution service (e.g., the DNS) 298This flag prevents any type of name resolution service,
299such as DNS,
248from being called. 300from being called.
249.Pp 301.It AI_NUMERICSERV
250If the 302If the
251.Dv AI_NUMERICSERV 303.Dv AI_NUMERICSERV
252bit is set in the 304bit is set,
253.Fa ai_flags 305then a non-null
254member of the
255.Fa hints
256structure, then a non-null
257.Fa servname 306.Fa servname
258string must be a numeric port string. 307string must be a numeric port string.
259Otherwise an error of 308Otherwise an error of
260.Dv EAI_NONAME 309.Dv EAI_NONAME
261is returned. 310is returned.
262This flag prevents any type of name resolution service (e.g., the NIS) 311This flag prevents any type of name resolution service,
312such as NIS,
263from being called. 313from being called.
314.El
264.Pp 315.Pp
265The arguments to 316The arguments to
266.Fn getaddrinfo 317.Fn getaddrinfo
267must sufficiently be consistent and unambiguous. 318must be sufficiently consistent and unambiguous.
268Here are pitfall cases you may encounter: 319Issues to watch out for are:
269.Bl -bullet 320.Bl -bullet
270.It 321.It
271.Fn getaddrinfo 322.Fn getaddrinfo
@@ -322,72 +373,13 @@ does not identify any socket type, and
322.Fn getaddrinfo 373.Fn getaddrinfo
323is not allowed to glob the argument in such case. 374is not allowed to glob the argument in such case.
324.El 375.El
325.Pp
326All of the information returned by
327.Fn getaddrinfo
328is dynamically allocated:
329the
330.Li addrinfo
331structures, the socket address structures, and canonical node name
332strings pointed to by the addrinfo structures.
333To return this information to the system the function
334.Fn freeaddrinfo
335is called.
336The
337.Fa addrinfo
338structure pointed to by the
339.Fa ai argument
340is freed, along with any dynamic storage pointed to by the structure.
341This operation is repeated until a
342.Dv NULL
343.Fa ai_next
344pointer is encountered.
345.Pp
346To aid applications in printing error messages based on the
347.Dv EAI_xxx
348codes returned by
349.Fn getaddrinfo ,
350.Fn gai_strerror
351is defined.
352The argument is one of the
353.Dv EAI_xxx
354values defined earlier and the return value points to a string describing
355the error.
356If the argument is not one of the
357.Dv EAI_xxx
358values, the function still returns a pointer to a string whose contents
359indicate an unknown error.
360.\" 376.\"
361.Ss Extension for scoped IPv6 address 377.Sh RETURN VALUES
362The implementation allows experimental numeric IPv6 address notation with 378.Fn getaddrinfo
363scope identifier. 379returns zero on success, and non-zero on errors.
364By appending the percent character and scope identifier to addresses, 380See
365you can fill the 381.Xr gai_strerror 3
366.Li sin6_scope_id 382for a description of the non-zero error codes.
367field for addresses.
368This would make management of scoped address easier,
369and allows cut-and-paste input of scoped address.
370.Pp
371At this moment the code supports only link-local addresses with the format.
372Scope identifier is hardcoded to the name of the hardware interface associated
373with the link.
374.Po
375such as
376.Li ne0
377.Pc .
378An example is
379.Dq Li fe80::1%ne0 ,
380which means
381.Do
382.Li fe80::1
383on the link associated with the
384.Li ne0
385interface
386.Dc .
387.Pp
388The implementation is still very experimental and non-standard.
389The current implementation assumes a one-to-one relationship between
390the interface and link, which is not necessarily true from the specification.
391.\" 383.\"
392.Sh EXAMPLES 384.Sh EXAMPLES
393The following code tries to connect to 385The following code tries to connect to
@@ -489,60 +481,8 @@ if (nsock == 0) {
489freeaddrinfo(res0); 481freeaddrinfo(res0);
490.Ed 482.Ed
491.\" 483.\"
492.Sh DIAGNOSTICS
493Error return status from
494.Fn getaddrinfo
495is zero on success and non-zero on errors.
496Non-zero error codes are defined in
497.Aq Pa netdb.h ,
498and as follows:
499.Pp
500.Bl -tag -width EAI_ADDRFAMILY -compact
501.It Dv EAI_ADDRFAMILY
502Address family for
503.Fa nodename
504not supported.
505.It Dv EAI_AGAIN
506Temporary failure in name resolution.
507.It Dv EAI_BADFLAGS
508Invalid value for
509.Fa ai_flags .
510.It Dv EAI_FAIL
511Non-recoverable failure in name resolution.
512.It Dv EAI_FAMILY
513.Fa ai_family
514not supported.
515.It Dv EAI_MEMORY
516Memory allocation failure.
517.It Dv EAI_NODATA
518No address associated with
519.Fa nodename .
520.It Dv EAI_NONAME
521.Fa nodename
522nor
523.Fa servname
524provided, or not known.
525.It Dv EAI_SERVICE
526.Fa servname
527not supported for
528.Fa ai_socktype .
529.It Dv EAI_SOCKTYPE
530.Fa ai_socktype
531not supported.
532.It Dv EAI_SYSTEM
533System error returned in
534.Va errno .
535.El
536.Pp
537If called with proper argument,
538.Fn gai_strerror
539returns a pointer to a string describing the given error code.
540If the argument is not one of the
541.Dv EAI_xxx
542values, the function still returns a pointer to a string whose contents
543indicate an unknown error.
544.\"
545.Sh SEE ALSO 484.Sh SEE ALSO
485.Xr gai_strerror 3 ,
546.Xr gethostbyname 3 , 486.Xr gethostbyname 3 ,
547.Xr getnameinfo 3 , 487.Xr getnameinfo 3 ,
548.Xr getservbyname 3 , 488.Xr getservbyname 3 ,
@@ -589,5 +529,3 @@ The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
589.\" 529.\"
590.Sh BUGS 530.Sh BUGS
591The current implementation is not thread-safe. 531The current implementation is not thread-safe.
592.Pp
593The text was shamelessly copied from RFC 2553.