1 .\" $NetBSD: getnameinfo.3,v 1.34 2005/01/12 14:44:11 wiz Exp $
2 .\" $KAME: getnameinfo.3,v 1.37 2005/01/05 03:23:05 itojun Exp $
3 .\" $OpenBSD: getnameinfo.3,v 1.36 2004/12/21 09:48:20 jmc Exp $
5 .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
6 .\" Copyright (C) 2000, 2001 Internet Software Consortium.
8 .\" Permission to use, copy, modify, and distribute this software for any
9 .\" purpose with or without fee is hereby granted, provided that the above
10 .\" copyright notice and this permission notice appear in all copies.
12 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
13 .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14 .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15 .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16 .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
17 .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18 .\" PERFORMANCE OF THIS SOFTWARE.
25 .Nd socket address structure to hostname and service name
31 .Fa "const struct sockaddr *restrict sa"
33 .Fa "char *restrict node"
34 .Fa "socklen_t nodelen"
35 .Fa "char *restrict service"
36 .Fa "socklen_t servicelen"
42 function is used to convert a
44 structure to a pair of host name and service strings.
45 It is a replacement for and provides more flexibility than the
49 functions and is the converse of the
57 should point to either a
61 structure (for IPv4 or IPv6 respectively) that is
65 The host and service names associated with
71 which have length parameters
79 and the maximum value for
85 If a length parameter is zero, no string will be stored.
86 Otherwise, enough space must be provided to store the
87 host name or service string plus a byte for the NUL terminator.
94 .Bl -tag -width "NI_NUMERICHOSTXX"
96 A fully qualified domain name is not required for local hosts.
97 The local part of the fully qualified domain name is returned instead.
99 Return the address in numeric form, as if calling
101 instead of a host name.
104 If the host name cannot be found in DNS and this flag is set,
105 a non-zero error code is returned.
106 If the host name is not found and the flag is not set, the
107 address is returned in numeric form.
109 The service name is returned as a digit string representing the port number.
111 Specifies that the service being looked up is a datagram
114 to be called with a second argument of
116 instead of its default of
118 This is required for the few ports (512\-514) that have different services
125 This implementation allows numeric IPv6 address notation with scope identifier,
126 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
127 IPv6 link-local address will appear as a string like
131 for more information.
134 returns zero on success or one of the error codes listed in
138 The following code tries to get a numeric host name, and service name,
139 for a given socket address.
140 Observe that there is no hardcoded reference to a particular address family.
141 .Bd -literal -offset indent
142 struct sockaddr *sa; /* input */
143 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
145 if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), sbuf,
146 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
147 errx(1, "could not get numeric hostname");
150 printf("host=%s, serv=%s\en", hbuf, sbuf);
153 The following version checks if the socket address has a reverse address mapping:
154 .Bd -literal -offset indent
155 struct sockaddr *sa; /* input */
156 char hbuf[NI_MAXHOST];
158 if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), NULL, 0,
160 errx(1, "could not resolve hostname");
163 printf("host=%s\en", hbuf);
166 .Fd #include <sys/types.h>
167 .Fd #include <sys/socket.h>
168 .Fd #include <netdb.h>
176 .Fa "const struct sockaddr *restrict sa"
177 .Fa "socklen_t salen"
178 .Fa "char *restrict node"
181 .Fa "size_t servicelen"
193 .Xr gethostbyaddr 3 ,
194 .Xr getservbyport 3 ,
207 .%T Basic Socket Interface Extensions for IPv6
217 .%T "IPv6 Scoped Address Architecture"
219 .%N draft-ietf-ipv6-scoping-arch-02.txt
220 .%O work in progress material
224 .%T Protocol Independence Using the Sockets API
225 .%B "Proceedings of the FREENIX track: 2000 USENIX annual technical conference"
231 function is defined by the
233 draft specification and documented in
235 .Dq Basic Socket Interface Extensions for IPv6 .
238 can return both numeric and FQDN forms of the address specified in
240 There is no return value that indicates whether the string returned in
242 is a result of binary to numeric-text translation (like
244 or is the result of a DNS reverse lookup.
245 Because of this, malicious parties could set up a PTR record as follows:
246 .Bd -literal -offset indent
247 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
250 and trick the caller of
259 To prevent such attacks, the use of
261 is recommended when the result of
263 is used for access control purposes:
264 .Bd -literal -offset indent
267 char addr[NI_MAXHOST];
268 struct addrinfo hints, *res;
271 error = getnameinfo(sa, salen, addr, sizeof(addr),
272 NULL, 0, NI_NAMEREQD);
274 memset(\*[Am]hints, 0, sizeof(hints));
275 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
276 hints.ai_flags = AI_NUMERICHOST;
277 if (getaddrinfo(addr, "0", \*[Am]hints, \*[Am]res) == 0) {
278 /* malicious PTR record */
280 printf("bogus PTR record\en");
283 /* addr is FQDN as a result of PTR lookup */
285 /* addr is numeric string */
286 error = getnameinfo(sa, salen, addr, sizeof(addr),
287 NULL, 0, NI_NUMERICHOST);
292 .\"intentionally uses a different
296 .\"suggests, to avoid buffer length handling mistakes.