1 .\" $NetBSD: getnameinfo.3,v 1.41 2013/08/18 10:40:06 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
29 .Fn getnameinfo "const struct sockaddr * restrict sa" "socklen_t salen" \
30 "char * restrict host" "socklen_t hostlen" "char * restrict serv" \
31 "socklen_t servlen" "int flags"
35 function is used to convert a
37 structure to a pair of host name and service strings.
38 It is a replacement for and provides more flexibility than the
42 functions and is the converse of the
66 The host and service names associated with
72 which have length parameters
80 and the maximum value for
86 If a length parameter is zero, no string will be stored.
87 Otherwise, enough space must be provided to store the
88 host name or service string plus a byte for the NUL terminator.
95 .Bl -tag -width "NI_NUMERICHOSTXX"
97 A fully qualified domain name is not required for local hosts.
98 The local part of the fully qualified domain name is returned instead.
100 Return the address in numeric form, as if calling
102 instead of a host name.
105 If the host name cannot be found in DNS and this flag is set,
106 a non-zero error code is returned.
107 If the host name is not found and the flag is not set, the
108 address is returned in numeric form.
110 The service name is returned as a digit string representing the port number.
112 Specifies that the service being looked up is a datagram
115 to be called with a second argument of
117 instead of its default of
119 This is required for the few ports (512\-514) that have different services
126 This implementation allows numeric IPv6 address notation with scope identifier,
127 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
128 IPv6 link-local address will appear as a string like
132 for more information.
135 returns zero on success or one of the error codes listed in
139 The following code tries to get a numeric host name, and service name,
140 for a given socket address.
141 Observe that there is no hardcoded reference to a particular address family.
142 .Bd -literal -offset indent
143 struct sockaddr *sa; /* input */
144 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
146 if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), sbuf,
147 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
148 errx(1, "could not get numeric hostname");
151 printf("host=%s, serv=%s\en", hbuf, sbuf);
154 The following version checks if the socket address has a reverse address mapping:
155 .Bd -literal -offset indent
156 struct sockaddr *sa; /* input */
157 char hbuf[NI_MAXHOST];
159 if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), NULL, 0,
161 errx(1, "could not resolve hostname");
164 printf("host=%s\en", hbuf);
169 .Xr gethostbyaddr 3 ,
170 .Xr getservbyport 3 ,
183 .%T Basic Socket Interface Extensions for IPv6
193 .%T "IPv6 Scoped Address Architecture"
195 .%N draft-ietf-ipv6-scoping-arch-02.txt
196 .%O work in progress material
200 .%T Protocol Independence Using the Sockets API
201 .%B "Proceedings of the FREENIX track: 2000 USENIX annual technical conference"
207 function is defined by the
209 draft specification and documented in
211 .Dq Basic Socket Interface Extensions for IPv6 .
214 can return both numeric and FQDN forms of the address specified in
216 There is no return value that indicates whether the string returned in
218 is a result of binary to numeric-text translation (like
220 or is the result of a DNS reverse lookup.
221 Because of this, malicious parties could set up a PTR record as follows:
222 .Bd -literal -offset indent
223 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
226 and trick the caller of
235 To prevent such attacks, the use of
237 is recommended when the result of
239 is used for access control purposes:
240 .Bd -literal -offset indent
243 char addr[NI_MAXHOST];
244 struct addrinfo hints, *res;
247 error = getnameinfo(sa, salen, addr, sizeof(addr),
248 NULL, 0, NI_NAMEREQD);
250 memset(\*[Am]hints, 0, sizeof(hints));
251 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
252 hints.ai_flags = AI_NUMERICHOST;
253 if (getaddrinfo(addr, "0", \*[Am]hints, \*[Am]res) == 0) {
254 /* malicious PTR record */
256 printf("bogus PTR record\en");
259 /* addr is FQDN as a result of PTR lookup */
261 /* addr is numeric string */
262 error = getnameinfo(sa, salen, addr, sizeof(addr),
263 NULL, 0, NI_NUMERICHOST);
268 .\"intentionally uses a different
272 .\"suggests, to avoid buffer length handling mistakes.