summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/getnameinfo.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/getnameinfo.3')
-rw-r--r--src/lib/libc/net/getnameinfo.3348
1 files changed, 348 insertions, 0 deletions
diff --git a/src/lib/libc/net/getnameinfo.3 b/src/lib/libc/net/getnameinfo.3
new file mode 100644
index 0000000000..3b3a0fd290
--- /dev/null
+++ b/src/lib/libc/net/getnameinfo.3
@@ -0,0 +1,348 @@
1.\" $OpenBSD: getnameinfo.3,v 1.27 2003/08/28 10:16:38 jmc Exp $
2.\" $KAME: getnameinfo.3,v 1.20 2001/01/05 13:37:37 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 GETNAMEINFO 3
35.Os
36.\"
37.Sh NAME
38.Nm getnameinfo
39.Nd address-to-nodename translation in protocol-independent manner
40.\"
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Fd #include <sys/socket.h>
44.Fd #include <netdb.h>
45.Ft int
46.Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" \
47"char *host" "size_t hostlen" "char *serv" "size_t servlen" "int flags"
48.\"
49.Sh DESCRIPTION
50The
51.Fn getnameinfo
52function is defined for protocol-independent address-to-nodename translation.
53Its functionality is a reverse conversion of
54.Xr getaddrinfo 3 ,
55and implements similar functionality to
56.Xr gethostbyaddr 3
57and
58.Xr getservbyport 3
59in a more sophisticated manner.
60.Pp
61This function looks up an IP address and port number provided by the
62caller in the DNS and system-specific database, and returns text
63strings for both in buffers provided by the caller.
64The function indicates successful completion by a zero return value;
65a non-zero return value indicates failure.
66.Pp
67The first argument,
68.Fa sa ,
69points to either a
70.Li sockaddr_in
71structure (for IPv4) or a
72.Li sockaddr_in6
73structure (for IPv6) that holds the IP address and port number.
74The
75.Fa salen
76argument gives the length of the
77.Li sockaddr_in
78or
79.Li sockaddr_in6
80structure.
81.Pp
82The function returns the nodename associated with the IP address in
83the buffer pointed to by the
84.Fa host
85argument.
86The caller provides the size of this buffer via the
87.Fa hostlen
88argument.
89The service name associated with the port number is returned in the buffer
90pointed to by
91.Fa serv ,
92and the
93.Fa servlen
94argument gives the length of this buffer.
95The caller specifies not to return either string by providing a zero
96value for the
97.Fa hostlen
98or
99.Fa servlen
100arguments.
101Otherwise, the caller must provide buffers large enough to hold the
102nodename and the service name, including the terminating null characters.
103.Pp
104Unfortunately most systems do not provide constants that specify the
105maximum size of either a fully-qualified domain name or a service name.
106Therefore to aid the application in allocating buffers for these two
107returned strings the following constants are defined in
108.Aq Pa netdb.h :
109.Bd -literal -offset
110#define NI_MAXHOST MAXHOSTNAMELEN
111#define NI_MAXSERV 32
112.Ed
113.Pp
114The first value is actually defined as the constant
115.Dv MAXDNAME
116in recent versions of BIND's
117.Aq Pa arpa/nameser.h
118header (older versions of BIND define this constant to be 256)
119and the second is a guess based on the services listed in the current
120Assigned Numbers RFC.
121.Pp
122The final argument is a
123.Fa flag
124that changes the default actions of this function.
125By default the fully-qualified domain name (FQDN) for the host is
126looked up in the DNS and returned.
127If the flag bit
128.Dv NI_NOFQDN
129is set, only the nodename portion of the FQDN is returned for local hosts.
130.Pp
131If the
132.Fa flag
133bit
134.Dv NI_NUMERICHOST
135is set, or if the host's name cannot be located in the DNS,
136the numeric form of the host's address is returned instead of its name
137.Po
138e.g., by calling
139.Fn inet_ntop
140instead of
141.Fn gethostbyaddr
142.Pc .
143If the
144.Fa flag
145bit
146.Dv NI_NAMEREQD
147is set, an error is returned if the host's name cannot be located in the DNS.
148.Pp
149If the flag bit
150.Dv NI_NUMERICSERV
151is set, the numeric form of the service address is returned
152.Pq e.g., its port number
153instead of its name.
154The two
155.Dv NI_NUMERICxxx
156flags are required to support the
157.Fl n
158flag that many commands provide.
159.Pp
160A fifth flag bit,
161.Dv NI_DGRAM ,
162specifies that the service is a datagram service, and causes
163.Fn getservbyport
164to be called with a second argument of
165.Qq udp
166instead of its default of
167.Qq tcp .
168This is required for the few ports (512-514)
169that have different services for UDP and TCP.
170.Pp
171These
172.Dv NI_xxx
173flags are defined in
174.Aq Pa netdb.h .
175.\"
176.Ss Extension for scoped IPv6 address
177The implementation allows experimental numeric IPv6 address notation with
178scope identifier.
179IPv6 link-local address will appear as a string like
180.Dq Li fe80::1%ne0 .
181Refer to
182.Xr getaddrinfo 3
183for the notation.
184.\"
185.Sh EXAMPLES
186The following code tries to get a numeric hostname, and service name,
187for given socket address.
188Observe that there is no hardcoded reference to a particular address family.
189.Bd -literal -offset indent
190struct sockaddr *sa; /* input */
191char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
192
193if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
194 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
195 errx(1, "could not get numeric hostname");
196 /*NOTREACHED*/
197}
198printf("host=%s, serv=%s\en", hbuf, sbuf);
199.Ed
200.Pp
201The following version checks if the socket address has reverse address mapping.
202.Bd -literal -offset indent
203struct sockaddr *sa; /* input */
204char hbuf[NI_MAXHOST];
205
206if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
207 NI_NAMEREQD)) {
208 errx(1, "could not resolve hostname");
209 /*NOTREACHED*/
210}
211printf("host=%s\en", hbuf);
212.Ed
213.\"
214.Sh DIAGNOSTICS
215The function indicates successful completion by a zero return value;
216a non-zero return value indicates failure.
217Error codes are as below:
218.Bl -tag -width Er
219.It Dv EAI_AGAIN
220The name could not be resolved at this time.
221Future attempts may succeed.
222.It Dv EAI_BADFLAGS
223The flags had an invalid value.
224.It Dv EAI_FAIL
225A non-recoverable error occurred.
226.It Dv EAI_FAMILY
227The address family was not recognized or the address length was invalid
228for the specified family.
229.It Dv EAI_MEMORY
230There was a memory allocation failure.
231.It Dv EAI_NONAME
232The name does not resolve for the supplied parameters.
233.Dv NI_NAMEREQD
234is set and the host's name cannot be located,
235or both nodename and servname were null.
236.It Dv EAI_SYSTEM
237A system error occurred.
238The error code can be found in errno.
239.El
240.\"
241.Sh SEE ALSO
242.Xr getaddrinfo 3 ,
243.Xr gethostbyaddr 3 ,
244.Xr getservbyport 3 ,
245.Xr hosts 5 ,
246.Xr resolv.conf 5 ,
247.Xr services 5 ,
248.Xr hostname 7 ,
249.Xr named 8
250.Rs
251.%A R. Gilligan
252.%A S. Thomson
253.%A J. Bound
254.%A W. Stevens
255.%T Basic Socket Interface Extensions for IPv6
256.%R RFC 2553
257.%D March 1999
258.Re
259.Rs
260.%A Tatsuya Jinmei
261.%A Atsushi Onoe
262.%T "An Extension of Format for IPv6 Scoped Addresses"
263.%R internet draft
264.%N draft-ietf-ipngwg-scopedaddr-format-02.txt
265.%O work in progress material
266.Re
267.Rs
268.%A Craig Metz
269.%T Protocol Independence Using the Sockets API
270.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
271.%D June 2000
272.Re
273.\"
274.Sh STANDARDS
275The
276.Fn getnameinfo
277function is defined in IEEE POSIX 1003.1g draft specification,
278and documented in
279.Dq Basic Socket Interface Extensions for IPv6
280.Pq RFC 2553 .
281.\"
282.Sh HISTORY
283The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
284.\"
285.Sh CAVEATS
286.Fn getnameinfo
287returns both numeric and FQDN notation of the address specified in
288.Fa sa .
289There is no return value that indicates if the string returned in
290.Fa host
291is a result of binary to numeric-text translation (like
292.Xr inet_ntop 3 ) ,
293or the result of DNS reverse lookup.
294Therefore, malicious parties could set up a PTR record as below:
295.Bd -literal -offset indent
2961.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
297.Ed
298.Pp
299and trick the caller of
300.Fn getnameinfo
301into believing that
302.Fa sa
303is
304.Li 10.1.1.1
305when it actually is
306.Li 127.0.0.1 .
307.Pp
308To prevent such attacks, the use of
309.Dv NI_NAMEREQD
310is recommended when you use the result of
311.Fn getnameinfo
312for access control purposes:
313.Bd -literal -offset indent
314struct sockaddr *sa;
315socklen_t salen;
316char addr[NI_MAXHOST];
317struct addrinfo hints, *res;
318int error;
319
320error = getnameinfo(sa, salen, addr, sizeof(addr),
321 NULL, 0, NI_NAMEREQD);
322if (error == 0) {
323 memset(&hints, 0, sizeof(hints));
324 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
325 hints.ai_flags = AI_NUMERICHOST;
326 if (getaddrinfo(addr, "0", &hints, &res) == 0) {
327 /* malicious PTR record */
328 freeaddrinfo(res);
329 printf("bogus PTR record\\n");
330 return -1;
331 }
332 /* addr is FQDN as a result of PTR lookup */
333} else {
334 /* addr is numeric string */
335 error = getnameinfo(sa, salen, addr, sizeof(addr),
336 NULL, 0, NI_NUMERICHOST);
337}
338.Ed
339.\"
340.Sh BUGS
341The current implementation is not thread-safe.
342.Pp
343The text was shamelessly copied from RFC 2553.
344.Pp
345.Ox
346intentionally uses a different
347.Dv NI_MAXHOST
348value from what RFC 2553 suggests, to avoid buffer length handling mistakes.