xref: /freebsd/lib/libc/net/getnameinfo.3 (revision faf25f48d601ae39f5752602f3020e2e92605625)
1.\"	$KAME: getnameinfo.3,v 1.37 2005/01/05 03:23:05 itojun Exp $
2.\"	$OpenBSD: getnameinfo.3,v 1.36 2004/12/21 09:48:20 jmc Exp $
3.\"
4.\" Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
5.\" Copyright (C) 2000, 2001  Internet Software Consortium.
6.\"
7.\" Permission to use, copy, modify, and distribute this software for any
8.\" purpose with or without fee is hereby granted, provided that the above
9.\" copyright notice and this permission notice appear in all copies.
10.\"
11.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13.\" AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15.\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16.\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17.\" PERFORMANCE OF THIS SOFTWARE.
18.\"
19.\" $FreeBSD$
20.\"
21.Dd June 27, 2022
22.Dt GETNAMEINFO 3
23.Os
24.Sh NAME
25.Nm getnameinfo
26.Nd socket address structure to hostname and service name
27.Sh SYNOPSIS
28.In sys/types.h
29.In sys/socket.h
30.In netdb.h
31.Ft int
32.Fo getnameinfo
33.Fa "const struct sockaddr *sa" "socklen_t salen" "char *host"
34.Fa "size_t hostlen" "char *serv" "size_t servlen" "int flags"
35.Fc
36.Sh DESCRIPTION
37The
38.Fn getnameinfo
39function is used to convert a
40.Li sockaddr
41structure to a pair of host name and service strings.
42It is a replacement for and provides more flexibility than the
43.Xr gethostbyaddr 3
44and
45.Xr getservbyport 3
46functions and is the converse of the
47.Xr getaddrinfo 3
48function.
49.Pp
50If a link-layer address or UNIX-domain address is passed to
51.Fn getnameinfo ,
52its ASCII representation will be stored in
53.Fa host .
54The string pointed to by
55.Fa serv
56will be set to the empty string if non-NULL;
57.Fa flags
58will always be ignored.
59For a link-layer address,
60this can be used as a replacement of the legacy
61.Xr link_ntoa 3
62function.
63.Pp
64The
65.Li sockaddr
66structure
67.Fa sa
68should point to either a
69.Li sockaddr_in ,
70.Li sockaddr_in6 ,
71.Li sockaddr_dl ,
72or
73.Li sockaddr_un
74structure
75.Po for IPv4 ,
76IPv6,
77link-layer,
78or UNIX-domain respectively
79.Pc
80that is
81.Fa salen
82bytes long.
83If
84.Fa salen
85is shorter than the length corresponding to the specified
86address family or longer than
87.Fn sizeof "struct sockaddr_storage" ,
88it returns
89.Er EAI_FAMILY .
90Note that
91.Va sa->sa_len
92should be consistent with
93.Fa salen
94though the value of
95.Va sa->sa_len
96is not directly used in this function.
97.Pp
98The host and service names associated with
99.Fa sa
100are stored in
101.Fa host
102and
103.Fa serv
104which have length parameters
105.Fa hostlen
106and
107.Fa servlen .
108The maximum value for
109.Fa hostlen
110is
111.Dv NI_MAXHOST
112and
113the maximum value for
114.Fa servlen
115is
116.Dv NI_MAXSERV ,
117as defined by
118.Aq Pa netdb.h .
119If a length parameter is zero, no string will be stored.
120Otherwise, enough space must be provided to store the
121host name or service string plus a byte for the NUL terminator.
122.Pp
123The
124.Fa flags
125argument is formed by
126.Tn OR Ns 'ing
127the following values:
128.Bl -tag -width "NI_NUMERICSCOPEXX"
129.It Dv NI_NOFQDN
130A fully qualified domain name is not required for local hosts.
131The local part of the fully qualified domain name is returned instead.
132.It Dv NI_NUMERICHOST
133Return the address in numeric form, as if calling
134.Xr inet_ntop 3 ,
135instead of a host name.
136.It Dv NI_NAMEREQD
137A name is required.
138If the host name cannot be found in DNS and this flag is set,
139a non-zero error code is returned.
140If the host name is not found and the flag is not set, the
141address is returned in numeric form.
142.It NI_NUMERICSERV
143The service name is returned as a digit string representing the port number.
144.It NI_NUMERICSCOPE
145The scope identifier is returned as a digit string.
146.It NI_DGRAM
147Specifies that the service being looked up is a datagram
148service, and causes
149.Xr getservbyport 3
150to be called with a second argument of
151.Dq udp
152instead of its default of
153.Dq tcp .
154This is required for the few ports (512\-514) that have different services
155for
156.Tn UDP
157and
158.Tn TCP .
159.El
160.Pp
161This implementation allows numeric IPv6 address notation with scope identifier,
162as documented in chapter 11 of RFC 4007.
163IPv6 link-local address will appear as a string like
164.Dq Li fe80::1%ne0 .
165Refer to
166.Xr getaddrinfo 3
167for more information.
168.Sh RETURN VALUES
169.Fn getnameinfo
170returns zero on success or one of the error codes listed in
171.Xr gai_strerror 3
172if an error occurs.
173.Sh EXAMPLES
174The following code tries to get a numeric host name, and service name,
175for a given socket address.
176Observe that there is no hardcoded reference to a particular address family.
177.Bd -literal -offset indent
178struct sockaddr *sa;	/* input */
179char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
180
181if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
182    sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
183	errx(1, "could not get numeric hostname");
184	/* NOTREACHED */
185}
186printf("host=%s, serv=%s\en", hbuf, sbuf);
187.Ed
188.Pp
189The following version checks if the socket address has a reverse address mapping:
190.Bd -literal -offset indent
191struct sockaddr *sa;	/* input */
192char hbuf[NI_MAXHOST];
193
194if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
195    NI_NAMEREQD)) {
196	errx(1, "could not resolve hostname");
197	/* NOTREACHED */
198}
199printf("host=%s\en", hbuf);
200.Ed
201.Sh SEE ALSO
202.Xr gai_strerror 3 ,
203.Xr getaddrinfo 3 ,
204.Xr gethostbyaddr 3 ,
205.Xr getservbyport 3 ,
206.Xr inet_ntop 3 ,
207.Xr link_ntoa 3 ,
208.Xr resolver 3 ,
209.Xr inet 4 ,
210.Xr inet6 4 ,
211.Xr unix 4 ,
212.Xr hosts 5 ,
213.Xr resolv.conf 5 ,
214.Xr services 5 ,
215.Xr hostname 7
216.Rs
217.%A R. Gilligan
218.%A S. Thomson
219.%A J. Bound
220.%A J. McCann
221.%A W. Stevens
222.%T Basic Socket Interface Extensions for IPv6
223.%R RFC 3493
224.%D February 2003
225.Re
226.Rs
227.%A S. Deering
228.%A B. Haberman
229.%A T. Jinmei
230.%A E. Nordmark
231.%A B. Zill
232.%T "IPv6 Scoped Address Architecture"
233.%R RFC 4007
234.%D March 2005
235.Re
236.Rs
237.%A Craig Metz
238.%T Protocol Independence Using the Sockets API
239.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
240.%D June 2000
241.Re
242.Sh STANDARDS
243The
244.Fn getnameinfo
245function is defined by the
246.St -p1003.1-2004
247specification and documented in
248.Tn "RFC 3493" ,
249.Dq Basic Socket Interface Extensions for IPv6 .
250.Sh CAVEATS
251.Fn getnameinfo
252can return both numeric and FQDN forms of the address specified in
253.Fa sa .
254There is no return value that indicates whether the string returned in
255.Fa host
256is a result of binary to numeric-text translation (like
257.Xr inet_ntop 3 ) ,
258or is the result of a DNS reverse lookup.
259Because of this, malicious parties could set up a PTR record as follows:
260.Bd -literal -offset indent
2611.0.0.127.in-addr.arpa. IN PTR  10.1.1.1
262.Ed
263.Pp
264and trick the caller of
265.Fn getnameinfo
266into believing that
267.Fa sa
268is
269.Li 10.1.1.1
270when it is actually
271.Li 127.0.0.1 .
272.Pp
273To prevent such attacks, the use of
274.Dv NI_NAMEREQD
275is recommended when the result of
276.Fn getnameinfo
277is used
278for access control purposes:
279.Bd -literal -offset indent
280struct sockaddr *sa;
281socklen_t salen;
282char addr[NI_MAXHOST];
283struct addrinfo hints, *res;
284int error;
285
286error = getnameinfo(sa, salen, addr, sizeof(addr),
287    NULL, 0, NI_NAMEREQD);
288if (error == 0) {
289	memset(&hints, 0, sizeof(hints));
290	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
291	hints.ai_flags = AI_NUMERICHOST;
292	if (getaddrinfo(addr, "0", &hints, &res) == 0) {
293		/* malicious PTR record */
294		freeaddrinfo(res);
295		printf("bogus PTR record\en");
296		return -1;
297	}
298	/* addr is FQDN as a result of PTR lookup */
299} else {
300	/* addr is numeric string */
301	error = getnameinfo(sa, salen, addr, sizeof(addr),
302	    NULL, 0, NI_NUMERICHOST);
303}
304.Ed
305.\".Pp
306.\".Ox
307.\"intentionally uses a different
308.\".Dv NI_MAXHOST
309.\"value from what
310.\".Tn "RFC 2553"
311.\"suggests, to avoid buffer length handling mistakes.
312