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 .\" $FreeBSD: src/lib/libc/net/getnameinfo.3,v 1.21 2005/01/23 16:02:48 gnn Exp $
4 .\" $DragonFly: src/lib/libc/net/getnameinfo.3,v 1.3 2005/01/23 17:03:13 hrs Exp $
6 .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
7 .\" Copyright (C) 2000, 2001 Internet Software Consortium.
9 .\" Permission to use, copy, modify, and distribute this software for any
10 .\" purpose with or without fee is hereby granted, provided that the above
11 .\" copyright notice and this permission notice appear in all copies.
13 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
14 .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15 .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
16 .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
18 .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 .\" PERFORMANCE OF THIS SOFTWARE.
26 .Nd socket address structure to hostname and service name
28 .Fd #include <sys/types.h>
29 .Fd #include <sys/socket.h>
30 .Fd #include <netdb.h>
32 .Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" "char *host" \
33 "size_t hostlen" "char *serv" "size_t servlen" "int flags"
37 function is used to convert a
39 structure to a pair of host name and service strings.
40 It is a replacement for and provides more flexibility than the
44 functions and is the converse of the
52 should point to either a
56 structure (for IPv4 or IPv6 respectively) that is
60 The host and service names associated with
66 which have length parameters
81 If a length parameter is zero, no string will be stored.
82 Otherwise, enough space must be provided to store the
83 host name or service string plus a byte for the NUL terminator.
90 .Bl -tag -width "NI_NUMERICHOSTXX"
92 A fully qualified domain name is not required for local hosts.
93 The local part of the fully qualified domain name is returned instead.
95 Return the address in numeric form, as if calling
97 instead of a host name.
100 If the host name cannot be found in DNS and this flag is set,
101 a non-zero error code is returned.
102 If the host name is not found and the flag is not set, the
103 address is returned in numeric form.
105 The service name is returned as a digit string representing the port number.
107 Specifies that the service being looked up is a datagram
110 to be called with a second argument of
112 instead of its default of
114 This is required for the few ports (512\-514) that have different services
121 This implementation allows numeric IPv6 address notation with scope identifier,
122 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
123 IPv6 link-local address will appear as a string like
127 for more information.
130 returns zero on success or one of the error codes listed in
134 The following code tries to get a numeric host name, and service name,
135 for a given socket address.
136 Observe that there is no hardcoded reference to a particular address family.
137 .Bd -literal -offset indent
138 struct sockaddr *sa; /* input */
139 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
141 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
142 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
143 errx(1, "could not get numeric hostname");
146 printf("host=%s, serv=%s\en", hbuf, sbuf);
149 The following version checks if the socket address has a reverse address mapping:
150 .Bd -literal -offset indent
151 struct sockaddr *sa; /* input */
152 char hbuf[NI_MAXHOST];
154 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
156 errx(1, "could not resolve hostname");
159 printf("host=%s\en", hbuf);
164 .Xr gethostbyaddr 3 ,
165 .Xr getservbyport 3 ,
178 .%T Basic Socket Interface Extensions for IPv6
188 .%T "IPv6 Scoped Address Architecture"
190 .%N draft-ietf-ipv6-scoping-arch-02.txt
191 .%O work in progress material
195 .%T Protocol Independence Using the Sockets API
196 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
202 function is defined by the
204 draft specification and documented in
206 .Dq Basic Socket Interface Extensions for IPv6 .
209 can return both numeric and FQDN forms of the address specified in
211 There is no return value that indicates whether the string returned in
213 is a result of binary to numeric-text translation (like
215 or is the result of a DNS reverse lookup.
216 Because of this, malicious parties could set up a PTR record as follows:
217 .Bd -literal -offset indent
218 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
221 and trick the caller of
230 To prevent such attacks, the use of
232 is recommended when the result of
235 for access control purposes:
236 .Bd -literal -offset indent
239 char addr[NI_MAXHOST];
240 struct addrinfo hints, *res;
243 error = getnameinfo(sa, salen, addr, sizeof(addr),
244 NULL, 0, NI_NAMEREQD);
246 memset(&hints, 0, sizeof(hints));
247 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
248 hints.ai_flags = AI_NUMERICHOST;
249 if (getaddrinfo(addr, "0", &hints, &res) == 0) {
250 /* malicious PTR record */
252 printf("bogus PTR record\en");
255 /* addr is FQDN as a result of PTR lookup */
257 /* addr is numeric string */
258 error = getnameinfo(sa, salen, addr, sizeof(addr),
259 NULL, 0, NI_NUMERICHOST);
263 The implementation of
268 .\"intentionally uses a different
272 .\"suggests, to avoid buffer length handling mistakes.