1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
.\" $OpenBSD: getnameinfo.3,v 1.28 2004/12/06 10:46:35 jmc Exp $
.\" $KAME: getnameinfo.3,v 1.20 2001/01/05 13:37:37 itojun Exp $
.\"
.\" Copyright (c) 1983, 1987, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
.\"
.Dd May 25, 1995
.Dt GETNAMEINFO 3
.Os
.\"
.Sh NAME
.Nm getnameinfo
.Nd address-to-nodename translation in protocol-independent manner
.\"
.Sh SYNOPSIS
.Fd #include <sys/types.h>
.Fd #include <sys/socket.h>
.Fd #include <netdb.h>
.Ft int
.Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" \
"char *host" "size_t hostlen" "char *serv" "size_t servlen" "int flags"
.\"
.Sh DESCRIPTION
The
.Fn getnameinfo
function is defined for protocol-independent address-to-nodename translation.
Its functionality is a reverse conversion of
.Xr getaddrinfo 3 ,
and implements similar functionality to
.Xr gethostbyaddr 3
and
.Xr getservbyport 3
in a more sophisticated manner.
.Pp
This function looks up an IP address and port number provided by the
caller in the DNS and system-specific database, and returns text
strings for both in buffers provided by the caller.
The function indicates successful completion by a zero return value;
a non-zero return value indicates failure.
.Pp
To aid applications in printing error messages based on the
.Dv EAI_xxx
codes returned by
.Fn getnameinfo ,
.Fn gai_strerror
is defined.
See
.Xr gai_strerror 3
for more information.
.Pp
The implementation allows experimental numeric IPv6 address notation with
scope identifier.
IPv6 link-local address will appear as a string like
.Dq Li fe80::1%ne0 .
Refer to
.Xr getaddrinfo 3
for the notation.
.Pp
The first argument,
.Fa sa ,
points to either a
.Li sockaddr_in
structure (for IPv4) or a
.Li sockaddr_in6
structure (for IPv6) that holds the IP address and port number.
The
.Fa salen
argument gives the length of the
.Li sockaddr_in
or
.Li sockaddr_in6
structure.
.Pp
The function returns the nodename associated with the IP address in
the buffer pointed to by the
.Fa host
argument.
The caller provides the size of this buffer via the
.Fa hostlen
argument.
The service name associated with the port number is returned in the buffer
pointed to by
.Fa serv ,
and the
.Fa servlen
argument gives the length of this buffer.
The caller specifies not to return either string by providing a zero
value for the
.Fa hostlen
or
.Fa servlen
arguments.
Otherwise, the caller must provide buffers large enough to hold the
nodename and the service name, including the terminating null characters.
.Pp
Unfortunately most systems do not provide constants that specify the
maximum size of either a fully-qualified domain name or a service name.
Therefore to aid the application in allocating buffers for these two
returned strings the following constants are defined in
.Aq Pa netdb.h :
.Bd -literal -offset indent
#define NI_MAXHOST MAXHOSTNAMELEN
#define NI_MAXSERV 32
.Ed
.Pp
The first value is actually defined as the constant
.Dv MAXDNAME
in recent versions of BIND's
.Aq Pa arpa/nameser.h
header (older versions of BIND define this constant to be 256)
and the second is a guess based on the services listed in the current
Assigned Numbers RFC.
.Pp
The final argument is a
.Fa flag
that changes the default actions of this function.
The flags are defined in
.Aq Pa netdb.h
and are as follows:
.Bl -tag -width "NI_NUMERICHOSTXX"
.It Dv NI_NOFQDN
By default the fully-qualified domain name (FQDN) for the host is
looked up using DNS and returned.
If the flag bit
.Dv NI_NOFQDN
is set, only the nodename portion of the FQDN is returned for local hosts.
.It Dv NI_NUMERICHOST
If the
.Fa flag
bit
.Dv NI_NUMERICHOST
is set, or if the host's name cannot be located using DNS,
the numeric form of the host's address is returned instead of its name
.Po
e.g., by calling
.Xr inet_ntop 3
instead of
.Xr gethostbyaddr 3
.Pc .
The two
.Dv NI_NUMERICxxx
flags are required to support the
.Fl n
flag that many commands provide.
.It Dv NI_NAMEREQD
If the
.Fa flag
bit
.Dv NI_NAMEREQD
is set, an error is returned if the host's name cannot be located using DNS.
.It Dv NI_NUMERICSERV
If the flag bit
.Dv NI_NUMERICSERV
is set, the numeric form of the service address is returned
.Pq e.g., its port number
instead of its name.
The two
.Dv NI_NUMERICxxx
flags are required to support the
.Fl n
flag that many commands provide.
.It Dv NI_DGRAM
A fifth flag bit,
.Dv NI_DGRAM ,
specifies that the service is a datagram service, and causes
.Xr getservbyport 3
to be called with a second argument of
.Qq udp
instead of its default of
.Qq tcp .
This is required for the few ports (512-514)
that have different services for UDP and TCP.
.El
.\"
.Sh RETURN VALUES
.Fn getnameinfo
returns zero on success, and non-zero on errors.
See
.Xr gai_strerror 3
for a description of the non-zero error codes.
.\"
.Sh EXAMPLES
The following code tries to get a numeric hostname, and service name,
for a given socket address.
Observe that there is no hardcoded reference to a particular address family.
.Bd -literal -offset indent
struct sockaddr *sa; /* input */
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
errx(1, "could not get numeric hostname");
/*NOTREACHED*/
}
printf("host=%s, serv=%s\en", hbuf, sbuf);
.Ed
.Pp
The following version checks if the socket address has reverse address mapping:
.Bd -literal -offset indent
struct sockaddr *sa; /* input */
char hbuf[NI_MAXHOST];
if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
NI_NAMEREQD)) {
errx(1, "could not resolve hostname");
/*NOTREACHED*/
}
printf("host=%s\en", hbuf);
.Ed
.\"
.Sh SEE ALSO
.Xr gai_strerror 3 ,
.Xr getaddrinfo 3 ,
.Xr gethostbyaddr 3 ,
.Xr getservbyport 3 ,
.Xr hosts 5 ,
.Xr resolv.conf 5 ,
.Xr services 5 ,
.Xr hostname 7 ,
.Xr named 8
.Rs
.%A R. Gilligan
.%A S. Thomson
.%A J. Bound
.%A W. Stevens
.%T Basic Socket Interface Extensions for IPv6
.%R RFC 2553
.%D March 1999
.Re
.Rs
.%A Tatsuya Jinmei
.%A Atsushi Onoe
.%T "An Extension of Format for IPv6 Scoped Addresses"
.%R internet draft
.%N draft-ietf-ipngwg-scopedaddr-format-02.txt
.%O work in progress material
.Re
.Rs
.%A Craig Metz
.%T Protocol Independence Using the Sockets API
.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
.%D June 2000
.Re
.\"
.Sh STANDARDS
The
.Fn getnameinfo
function is defined in IEEE POSIX 1003.1g draft specification,
and documented in
.Dq Basic Socket Interface Extensions for IPv6
.Pq RFC 2553 .
.\"
.Sh HISTORY
The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
.\"
.Sh CAVEATS
.Fn getnameinfo
returns both numeric and FQDN notation of the address specified in
.Fa sa .
There is no return value that indicates if the string returned in
.Fa host
is a result of binary to numeric-text translation (like
.Xr inet_ntop 3 ) ,
or the result of DNS reverse lookup.
Therefore, malicious parties could set up a PTR record as below:
.Bd -literal -offset indent
1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
.Ed
.Pp
and trick the caller of
.Fn getnameinfo
into believing that
.Fa sa
is
.Li 10.1.1.1
when it actually is
.Li 127.0.0.1 .
.Pp
To prevent such attacks, the use of
.Dv NI_NAMEREQD
is recommended when you use the result of
.Fn getnameinfo
for access control purposes:
.Bd -literal -offset indent
struct sockaddr *sa;
socklen_t salen;
char addr[NI_MAXHOST];
struct addrinfo hints, *res;
int error;
error = getnameinfo(sa, salen, addr, sizeof(addr),
NULL, 0, NI_NAMEREQD);
if (error == 0) {
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(addr, "0", &hints, &res) == 0) {
/* malicious PTR record */
freeaddrinfo(res);
printf("bogus PTR record\\n");
return -1;
}
/* addr is FQDN as a result of PTR lookup */
} else {
/* addr is numeric string */
error = getnameinfo(sa, salen, addr, sizeof(addr),
NULL, 0, NI_NUMERICHOST);
}
.Ed
.\"
.Sh BUGS
The current implementation is not thread-safe.
.Pp
.Ox
intentionally uses a different
.Dv NI_MAXHOST
value from what RFC 2553 suggests, to avoid buffer length handling mistakes.
|