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 $
4 .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5 .\" Copyright (C) 2000, 2001 Internet Software Consortium.
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.
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.
19 .\" $FreeBSD: src/lib/libc/net/getnameinfo.3,v 1.25 2007/02/28 21:28:33 bms Exp $
26 .Nd socket address structure to hostname and service name
35 .Fa "const struct sockaddr * restrict sa" "socklen_t salen" "char * restrict host"
36 .Fa "size_t hostlen" "char * restrict serv" "size_t servlen" "int flags"
41 function is used to convert a
43 structure to a pair of host name and service strings.
44 It is a replacement for and provides more flexibility than the
48 functions and is the converse of the
52 If a link-layer address is passed to
54 its ASCII representation will be stored in
56 The string pointed to by
58 will be set to the empty string if non-NULL;
60 will always be ignored.
61 This is intended as a replacement for the legacy
69 should point to either a
74 structure (for IPv4, IPv6 or link-layer respectively) that is
78 The host and service names associated with
84 which have length parameters
99 If a length parameter is zero, no string will be stored.
100 Otherwise, enough space must be provided to store the
101 host name or service string plus a byte for the NUL terminator.
105 argument is formed by
107 the following values:
108 .Bl -tag -width ".Dv NI_NUMERICSCOPE"
110 A fully qualified domain name is not required for local hosts.
111 The local part of the fully qualified domain name is returned instead.
112 .It Dv NI_NUMERICHOST
113 Return the address in numeric form, as if calling
115 instead of a host name.
118 If the host name cannot be found in DNS and this flag is set,
119 a non-zero error code is returned.
120 If the host name is not found and the flag is not set, the
121 address is returned in numeric form.
122 .It Dv NI_NUMERICSERV
123 The service name is returned as a digit string representing the port number.
124 .It Dv NI_NUMERICSCOPE
125 For IPv6 addresses, scope identifier is returned as a digit string instead
128 Specifies that the service being looked up is a datagram
131 to be called with a second argument of
133 instead of its default of
135 This is required for the few ports (512\-514) that have different services
142 This implementation allows numeric IPv6 address notation with scope identifier,
143 as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt.
144 IPv6 link-local address will appear as a string like
148 for more information.
151 returns zero on success or one of the error codes listed in
155 The following code tries to get a numeric host name, and service name,
156 for a given socket address.
157 Observe that there is no hardcoded reference to a particular address family.
158 .Bd -literal -offset indent
159 struct sockaddr *sa; /* input */
160 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
162 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
163 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
164 errx(1, "could not get numeric hostname");
167 printf("host=%s, serv=%s\en", hbuf, sbuf);
170 The following version checks if the socket address has a reverse address mapping:
171 .Bd -literal -offset indent
172 struct sockaddr *sa; /* input */
173 char hbuf[NI_MAXHOST];
175 if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
177 errx(1, "could not resolve hostname");
180 printf("host=%s\en", hbuf);
185 .Xr gethostbyaddr 3 ,
186 .Xr getservbyport 3 ,
200 .%T Basic Socket Interface Extensions for IPv6
210 .%T "IPv6 Scoped Address Architecture"
212 .%N draft-ietf-ipv6-scoping-arch-02.txt
213 .%O work in progress material
217 .%T Protocol Independence Using the Sockets API
218 .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference"
224 function is defined by the
226 draft specification and documented in
228 .Dq Basic Socket Interface Extensions for IPv6 .
231 can return both numeric and FQDN forms of the address specified in
233 There is no return value that indicates whether the string returned in
235 is a result of binary to numeric-text translation (like
237 or is the result of a DNS reverse lookup.
238 Because of this, malicious parties could set up a PTR record as follows:
239 .Bd -literal -offset indent
240 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
243 and trick the caller of
252 To prevent such attacks, the use of
254 is recommended when the result of
257 for access control purposes:
258 .Bd -literal -offset indent
261 char addr[NI_MAXHOST];
262 struct addrinfo hints, *res;
265 error = getnameinfo(sa, salen, addr, sizeof(addr),
266 NULL, 0, NI_NAMEREQD);
268 memset(&hints, 0, sizeof(hints));
269 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
270 hints.ai_flags = AI_NUMERICHOST;
271 if (getaddrinfo(addr, "0", &hints, &res) == 0) {
272 /* malicious PTR record */
274 printf("bogus PTR record\en");
277 /* addr is FQDN as a result of PTR lookup */
279 /* addr is numeric string */
280 error = getnameinfo(sa, salen, addr, sizeof(addr),
281 NULL, 0, NI_NUMERICHOST);
286 .\"intentionally uses a different
290 .\"suggests, to avoid buffer length handling mistakes.