1 .\" $NetBSD: getnameinfo.3,v 1.35 2005/03/21 13:30:50 kleink 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" "size_t hostlen" "char * restrict serv" \
31 "size_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
50 should point to either a
54 structure (for IPv4 or IPv6 respectively) that is
58 The host and service names associated with
64 which have length parameters
72 and the maximum value for
78 If a length parameter is zero, no string will be stored.
79 Otherwise, enough space must be provided to store the
80 host name or service string plus a byte for the NUL terminator.
87 .Bl -tag -width "NI_NUMERICHOSTXX"
89 A fully qualified domain name is not required for local hosts.
90 The local part of the fully qualified domain name is returned instead.
92 Return the address in numeric form, as if calling
94 instead of a host name.
97 If the host name cannot be found in DNS and this flag is set,
98 a non-zero error code is returned.
99 If the host name is not found and the flag is not set, the
100 address is returned in numeric form.
102 The service name is returned as a digit string representing the port number.
104 Specifies that the service being looked up is a datagram
107 to be called with a second argument of
109 instead of its default of
111 This is required for the few ports (512\-514) that have different services
118 This implementation allows numeric IPv6 address notation with scope identifier,
119 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
120 IPv6 link-local address will appear as a string like
124 for more information.
127 returns zero on success or one of the error codes listed in
131 The following code tries to get a numeric host name, and service name,
132 for a given socket address.
133 Observe that there is no hardcoded reference to a particular address family.
134 .Bd -literal -offset indent
135 struct sockaddr *sa; /* input */
136 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
138 if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), sbuf,
139 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
140 errx(1, "could not get numeric hostname");
143 printf("host=%s, serv=%s\en", hbuf, sbuf);
146 The following version checks if the socket address has a reverse address mapping:
147 .Bd -literal -offset indent
148 struct sockaddr *sa; /* input */
149 char hbuf[NI_MAXHOST];
151 if (getnameinfo(sa, sa-\*[Gt]sa_len, hbuf, sizeof(hbuf), NULL, 0,
153 errx(1, "could not resolve hostname");
156 printf("host=%s\en", hbuf);
161 .Xr gethostbyaddr 3 ,
162 .Xr getservbyport 3 ,
175 .%T Basic Socket Interface Extensions for IPv6
185 .%T "IPv6 Scoped Address Architecture"
187 .%N draft-ietf-ipv6-scoping-arch-02.txt
188 .%O work in progress material
192 .%T Protocol Independence Using the Sockets API
193 .%B "Proceedings of the FREENIX track: 2000 USENIX annual technical conference"
199 function is defined by the
201 draft specification and documented in
203 .Dq Basic Socket Interface Extensions for IPv6 .
206 can return both numeric and FQDN forms of the address specified in
208 There is no return value that indicates whether the string returned in
210 is a result of binary to numeric-text translation (like
212 or is the result of a DNS reverse lookup.
213 Because of this, malicious parties could set up a PTR record as follows:
214 .Bd -literal -offset indent
215 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
218 and trick the caller of
227 To prevent such attacks, the use of
229 is recommended when the result of
231 is used for access control purposes:
232 .Bd -literal -offset indent
235 char addr[NI_MAXHOST];
236 struct addrinfo hints, *res;
239 error = getnameinfo(sa, salen, addr, sizeof(addr),
240 NULL, 0, NI_NAMEREQD);
242 memset(\*[Am]hints, 0, sizeof(hints));
243 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
244 hints.ai_flags = AI_NUMERICHOST;
245 if (getaddrinfo(addr, "0", \*[Am]hints, \*[Am]res) == 0) {
246 /* malicious PTR record */
248 printf("bogus PTR record\en");
251 /* addr is FQDN as a result of PTR lookup */
253 /* addr is numeric string */
254 error = getnameinfo(sa, salen, addr, sizeof(addr),
255 NULL, 0, NI_NUMERICHOST);
259 The implementation of
264 .\"intentionally uses a different
268 .\"suggests, to avoid buffer length handling mistakes.