]> git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/getnameinfo.3
Libinfo-392.tar.gz
[apple/libinfo.git] / lookup.subproj / getnameinfo.3
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: src/lib/libc/net/getnameinfo.3,v 1.25 2007/02/28 21:28:33 bms Exp $
20 .\"
21 .Dd February 28, 2007
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 .Fd #include <sys/types.h>
29 .Fd #include <sys/socket.h>
30 .Fd #include <netdb.h>
31 .Ft int
32 .Fo getnameinfo
33 .Fa "const struct sockaddr *sa" "socklen_t salen" "char *host"
34 .Fa "socklen_t hostlen" "char *serv" "socklen_t servlen" "int flags"
35 .\".Fa "size_t hostlen" "char *serv" "size_t servlen" "int flags"
36 .Fc
37 .Sh DESCRIPTION
38 The
39 .Fn getnameinfo
40 function is used to convert a
41 .Li sockaddr
42 structure to a pair of host name and service strings.
43 It is a replacement for and provides more flexibility than the
44 .Xr gethostbyaddr 3
45 and
46 .Xr getservbyport 3
47 functions and is the converse of the
48 .Xr getaddrinfo 3
49 function.
50 .Pp
51 If a link-layer address is passed to
52 .Fn getnameinfo ,
53 its ASCII representation will be stored in
54 .Fa host .
55 The string pointed to by
56 .Fa serv
57 will be set to the empty string if non-NULL;
58 .Fa flags
59 will always be ignored.
60 This is intended as a replacement for the legacy
61 .Xr link_ntoa 3
62 function.
63 .Pp
64 The
65 .Li sockaddr
66 structure
67 .Fa sa
68 should point to either a
69 .Li sockaddr_in ,
70 .Li sockaddr_in6
71 or
72 .Li sockaddr_dl
73 structure (for IPv4, IPv6 or link-layer respectively) that is
74 .Fa salen
75 bytes long.
76 .Pp
77 The host and service names associated with
78 .Fa sa
79 are stored in
80 .Fa host
81 and
82 .Fa serv
83 which have length parameters
84 .Fa hostlen
85 and
86 .Fa servlen .
87 The maximum value for
88 .Fa hostlen
89 is
90 .Dv NI_MAXHOST
91 and
92 the maximum value for
93 .Fa servlen
94 is
95 .Dv NI_MAXSERV ,
96 as defined by
97 .Aq Pa netdb.h .
98 If a length parameter is zero, no string will be stored.
99 Otherwise, enough space must be provided to store the
100 host name or service string plus a byte for the NUL terminator.
101 .Pp
102 The
103 .Fa flags
104 argument is formed by
105 .Tn OR Ns 'ing
106 the following values:
107 .Bl -tag -width "NI_NUMERICHOSTXX"
108 .It Dv NI_NOFQDN
109 A fully qualified domain name is not required for local hosts.
110 The local part of the fully qualified domain name is returned instead.
111 .It Dv NI_NUMERICHOST
112 Return the address in numeric form, as if calling
113 .Xr inet_ntop 3 ,
114 instead of a host name.
115 .It Dv NI_NAMEREQD
116 A name is required.
117 If the host name cannot be found in DNS and this flag is set,
118 a non-zero error code is returned.
119 If the host name is not found and the flag is not set, the
120 address is returned in numeric form.
121 .It NI_NUMERICSERV
122 The service name is returned as a digit string representing the port number.
123 .It NI_DGRAM
124 Specifies that the service being looked up is a datagram
125 service, and causes
126 .Xr getservbyport 3
127 to be called with a second argument of
128 .Dq udp
129 instead of its default of
130 .Dq tcp .
131 This is required for the few ports (512\-514) that have different services
132 for
133 .Tn UDP
134 and
135 .Tn TCP .
136 .El
137 .Pp
138 This implementation allows numeric IPv6 address notation with scope identifier,
139 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
140 IPv6 link-local address will appear as a string like
141 .Dq Li fe80::1%ne0 .
142 Refer to
143 .Xr getaddrinfo 3
144 for more information.
145 .Sh RETURN VALUES
146 .Fn getnameinfo
147 returns zero on success or one of the error codes listed in
148 .Xr gai_strerror 3
149 if an error occurs.
150 .Sh EXAMPLES
151 The following code tries to get a numeric host name, and service name,
152 for a given socket address.
153 Observe that there is no hardcoded reference to a particular address family.
154 .Bd -literal -offset indent
155 struct sockaddr *sa; /* input */
156 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
157
158 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
159 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
160 errx(1, "could not get numeric hostname");
161 /*NOTREACHED*/
162 }
163 printf("host=%s, serv=%s\en", hbuf, sbuf);
164 .Ed
165 .Pp
166 The following version checks if the socket address has a reverse address mapping:
167 .Bd -literal -offset indent
168 struct sockaddr *sa; /* input */
169 char hbuf[NI_MAXHOST];
170
171 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
172 NI_NAMEREQD)) {
173 errx(1, "could not resolve hostname");
174 /*NOTREACHED*/
175 }
176 printf("host=%s\en", hbuf);
177 .Ed
178 .Sh SEE ALSO
179 .Xr gai_strerror 3 ,
180 .Xr getaddrinfo 3 ,
181 .Xr gethostbyaddr 3 ,
182 .Xr getservbyport 3 ,
183 .Xr inet_ntop 3 ,
184 .Xr link_ntoa 3 ,
185 .Xr resolver 3 ,
186 .Xr hosts 5 ,
187 .Xr resolv.conf 5 ,
188 .Xr services 5 ,
189 .Xr hostname 7 ,
190 .Xr named 8
191 .Rs
192 .%A R. Gilligan
193 .%A S. Thomson
194 .%A J. Bound
195 .%A W. Stevens
196 .%T Basic Socket Interface Extensions for IPv6
197 .%R RFC 2553
198 .%D March 1999
199 .Re
200 .Rs
201 .%A S. Deering
202 .%A B. Haberman
203 .%A T. Jinmei
204 .%A E. Nordmark
205 .%A B. Zill
206 .%T "IPv6 Scoped Address Architecture"
207 .%R internet draft
208 .%N draft-ietf-ipv6-scoping-arch-02.txt
209 .%O work in progress material
210 .Re
211 .Rs
212 .%A Craig Metz
213 .%T Protocol Independence Using the Sockets API
214 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
215 .%D June 2000
216 .Re
217 .Sh STANDARDS
218 The
219 .Fn getnameinfo
220 function is defined by the
221 .St -p1003.1g-2000
222 draft specification and documented in
223 .Tn "RFC 2553" ,
224 .Dq Basic Socket Interface Extensions for IPv6 .
225 .Sh CAVEATS
226 .Fn getnameinfo
227 can return both numeric and FQDN forms of the address specified in
228 .Fa sa .
229 There is no return value that indicates whether the string returned in
230 .Fa host
231 is a result of binary to numeric-text translation (like
232 .Xr inet_ntop 3 ) ,
233 or is the result of a DNS reverse lookup.
234 Because of this, malicious parties could set up a PTR record as follows:
235 .Bd -literal -offset indent
236 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
237 .Ed
238 .Pp
239 and trick the caller of
240 .Fn getnameinfo
241 into believing that
242 .Fa sa
243 is
244 .Li 10.1.1.1
245 when it is actually
246 .Li 127.0.0.1 .
247 .Pp
248 To prevent such attacks, the use of
249 .Dv NI_NAMEREQD
250 is recommended when the result of
251 .Fn getnameinfo
252 is used
253 for access control purposes:
254 .Bd -literal -offset indent
255 struct sockaddr *sa;
256 socklen_t salen;
257 char addr[NI_MAXHOST];
258 struct addrinfo hints, *res;
259 int error;
260
261 error = getnameinfo(sa, salen, addr, sizeof(addr),
262 NULL, 0, NI_NAMEREQD);
263 if (error == 0) {
264 memset(&hints, 0, sizeof(hints));
265 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
266 hints.ai_flags = AI_NUMERICHOST;
267 if (getaddrinfo(addr, "0", &hints, &res) == 0) {
268 /* malicious PTR record */
269 freeaddrinfo(res);
270 printf("bogus PTR record\en");
271 return -1;
272 }
273 /* addr is FQDN as a result of PTR lookup */
274 } else {
275 /* addr is numeric string */
276 error = getnameinfo(sa, salen, addr, sizeof(addr),
277 NULL, 0, NI_NUMERICHOST);
278 }
279 .Ed
280 .\".Pp
281 .\".Ox
282 .\"intentionally uses a different
283 .\".Dv NI_MAXHOST
284 .\"value from what
285 .\".Tn "RFC 2553"
286 .\"suggests, to avoid buffer length handling mistakes.