xref: /netbsd-src/lib/libc/net/getnameinfo.3 (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1.\"	$NetBSD: getnameinfo.3,v 1.16 2001/11/15 06:46:33 itojun 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. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     From: @(#)gethostbyname.3	8.4 (Berkeley) 5/25/95
36.\"
37.Dd May 25, 1995
38.Dt GETNAMEINFO 3
39.Os
40.\"
41.Sh NAME
42.Nm getnameinfo
43.Nd address-to-nodename translation in protocol-independent manner
44.\"
45.Sh LIBRARY
46.Lb libc
47.\"
48.Sh SYNOPSIS
49.Fd #include <sys/types.h>
50.Fd #include <sys/socket.h>
51.Fd #include <netdb.h>
52.Ft int
53.Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" \
54"char *host" "size_t hostlen" "char *serv" "size_t servlen" "int flags"
55.\"
56.Sh DESCRIPTION
57The
58.Fn getnameinfo
59function is defined for protocol-independent address-to-nodename translation.
60Its functionality is a reverse conversion of
61.Xr getaddrinfo 3 ,
62and implements similar functionality with
63.Xr gethostbyaddr 3
64and
65.Xr getservbyport 3
66in more sophisticated manner.
67.Pp
68This function looks up an IP address and port number provided by the
69caller in the DNS and system-specific database, and returns text
70strings for both in buffers provided by the caller.
71The function indicates successful completion by a zero return value;
72a non-zero return value indicates failure.
73.Pp
74The first argument,
75.Fa sa ,
76points to either a
77.Li sockaddr_in
78structure (for IPv4) or a
79.Li sockaddr_in6
80structure (for IPv6) that holds the IP address and port number.
81The
82.Fa salen
83argument gives the length of the
84.Li sockaddr_in
85or
86.Li sockaddr_in6
87structure.
88.Pp
89The function returns the nodename associated with the IP address in
90the buffer pointed to by the
91.Fa host
92argument.
93The caller provides the size of this buffer via the
94.Fa hostlen
95argument.
96The service name associated with the port number is returned in the buffer
97pointed to by
98.Fa serv ,
99and the
100.Fa servlen
101argument gives the length of this buffer.
102The caller specifies not to return either string by providing a zero
103value for the
104.Fa hostlen
105or
106.Fa servlen
107arguments.
108Otherwise, the caller must provide buffers large enough to hold the
109nodename and the service name, including the terminating null characters.
110.Pp
111Unfortunately most systems do not provide constants that specify the
112maximum size of either a fully-qualified domain name or a service name.
113Therefore to aid the application in allocating buffers for these two
114returned strings the following constants are defined in
115.Aq Pa netdb.h :
116.Bd -literal -offset
117#define NI_MAXHOST  1025
118#define NI_MAXSERV    32
119.Ed
120.Pp
121The first value is actually defined as the constant
122.Dv MAXDNAME
123in recent versions of BIND's
124.Aq Pa arpa/nameser.h
125header
126.Po
127older versions of BIND define this constant to be 256
128.Pc
129and the second is a guess based on the services listed in the current
130Assigned Numbers RFC.
131.Pp
132The final argument is a
133.Fa flag
134that changes the default actions of this function.
135By default the fully-qualified domain name (FQDN) for the host is
136looked up in the DNS and returned.
137If the flag bit
138.Dv NI_NOFQDN
139is set, only the nodename portion of the FQDN is returned for local hosts.
140.Pp
141If the
142.Fa flag
143bit
144.Dv NI_NUMERICHOST
145is set, or if the host's name cannot be located in the DNS,
146the numeric form of the host's address is returned instead of its name
147.Po
148e.g., by calling
149.Fn inet_ntop
150instead of
151.Fn gethostbyaddr
152.Pc .
153If the
154.Fa flag
155bit
156.Dv NI_NAMEREQD
157is set, an error is returned if the host's name cannot be located in the DNS.
158.Pp
159If the flag bit
160.Dv NI_NUMERICSERV
161is set, the numeric form of the service address is returned
162.Pq e.g., its port number
163instead of its name.
164The two
165.Dv NI_NUMERICxxx
166flags are required to support the
167.Fl n
168flag that many commands provide.
169.Pp
170A fifth flag bit,
171.Dv NI_DGRAM ,
172specifies that the service is a datagram service, and causes
173.Fn getservbyport
174to be called with a second argument of
175.Dq udp
176instead of its default of
177.Dq tcp .
178This is required for the few ports (512-514)
179that have different services for UDP and TCP.
180.Pp
181These
182.Dv NI_xxx
183flags are defined in
184.Aq Pa netdb.h .
185.\"
186.Ss Extension for scoped IPv6 address
187The implementation allows experimental numeric IPv6 address notation with
188scope identifier.
189IPv6 link-local address will appear as string like
190.Dq Li fe80::1%ne0 .
191Refer to
192.Xr getaddrinfo 3
193for the notation.
194.\"
195.Sh EXAMPLES
196The following code tries to get numeric hostname, and service name,
197for given socket address.
198Observe that there is no hardcoded reference to particular address family.
199.Bd -literal -offset indent
200struct sockaddr *sa;	/* input */
201char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
202
203if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
204    sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
205	errx(1, "could not get numeric hostname");
206	/*NOTREACHED*/
207}
208printf("host=%s, serv=%s\\n", hbuf, sbuf);
209.Ed
210.Pp
211The following version checks if the socket address has reverse address mapping.
212.Bd -literal -offset indent
213struct sockaddr *sa;	/* input */
214char hbuf[NI_MAXHOST];
215
216if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
217    NI_NAMEREQD)) {
218	errx(1, "could not resolve hostname");
219	/*NOTREACHED*/
220}
221printf("host=%s\\n", hbuf);
222.Ed
223.\"
224.Sh DIAGNOSTICS
225The function indicates successful completion by a zero return value;
226a non-zero return value indicates failure.
227Error codes are as below:
228.Bl -tag -width Er
229.It Dv EAI_AGAIN
230The name could not be resolved at this time.
231Future attempts may succeed.
232.It Dv EAI_BADFLAGS
233The flags had an invalid value.
234.It Dv EAI_FAIL
235A non-recoverable error occurred.
236.It Dv EAI_FAMILY
237The address family was not recognized or the address length was invalid
238for the specified family.
239.It Dv EAI_MEMORY
240There was a memory allocation failure.
241.It Dv EAI_NONAME
242The name does not resolve for the supplied parameters.
243.Dv NI_NAMEREQD
244is set and the host's name cannot be located,
245or both nodename and servname were null.
246.It Dv EAI_SYSTEM
247A system error occurred.
248The error code can be found in errno.
249.El
250.\"
251.Sh SEE ALSO
252.Xr getaddrinfo 3 ,
253.Xr gethostbyaddr 3 ,
254.Xr getservbyport 3 ,
255.Xr hosts 5 ,
256.Xr resolv.conf 5 ,
257.Xr services 5 ,
258.Xr hostname 7 ,
259.Xr named 8
260.Pp
261.Rs
262.%A R. Gilligan
263.%A S. Thomson
264.%A J. Bound
265.%A W. Stevens
266.%T Basic Socket Interface Extensions for IPv6
267.%R RFC2553
268.%D March 1999
269.Re
270.Rs
271.%A Tatsuya Jinmei
272.%A Atsushi Onoe
273.%T "An Extension of Format for IPv6 Scoped Addresses"
274.%R internet draft
275.%N draft-ietf-ipngwg-scopedaddr-format-02.txt
276.%O work in progress material
277.Re
278.Rs
279.%A Craig Metz
280.%T Protocol Independence Using the Sockets API
281.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
282.%D June 2000
283.Re
284.\"
285.Sh STANDARDS
286The
287.Fn getnameinfo
288function is defined IEEE POSIX 1003.1g draft specification,
289and documented in
290.Dq Basic Socket Interface Extensions for IPv6
291.Pq RFC2553 .
292.\"
293.Sh HISTORY
294The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
295.\"
296.Sh BUGS
297The current implementation is not thread-safe.
298.Pp
299The text was shamelessly copied from RFC2553.
300