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