1 /* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Issues to be discussed:
34 * - Thread safe-ness must be checked
35 * - RFC2553 says that we should raise error on short buffer. X/Open says
36 * we need to truncate the result. We obey RFC2553 (and X/Open should be
37 * modified). ipngwg rough consensus seems to follow RFC2553.
38 * - What is "local" in NI_FQDN?
39 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
40 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
41 * sin6_scope_id is filled - standardization status?
42 * XXX breaks backward compat for code that expects no scopeid.
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
50 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <arpa/nameser.h>
61 static const struct afd {
68 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
69 offsetof(struct sockaddr_in6, sin6_addr)},
71 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
72 offsetof(struct sockaddr_in, sin_addr)},
83 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
85 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
89 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
90 const struct sockaddr *sa;
98 const struct afd *afd;
112 if (sa->sa_len != salen)
115 family = sa->sa_family;
116 for (i = 0; afdl[i].a_af; i++)
117 if (afdl[i].a_af == family) {
124 if (salen != afd->a_socklen)
127 /* network byte order */
128 port = ((const struct sockinet *)sa)->si_port;
129 addr = (const char *)sa + afd->a_off;
131 if (serv == NULL || servlen == 0) {
133 * do nothing in this case.
134 * in case you are wondering if "&&" is more correct than
135 * "||" here: rfc2553bis-03 says that serv == NULL OR
136 * servlen == 0 means that the caller does not want the result.
139 if (flags & NI_NUMERICSERV)
142 sp = getservbyport(port,
143 (flags & NI_DGRAM) ? "udp" : "tcp");
146 if (strlen(sp->s_name) + 1 > servlen)
148 strlcpy(serv, sp->s_name, servlen);
150 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
151 if (strlen(numserv) + 1 > servlen)
153 strlcpy(serv, numserv, servlen);
157 switch (sa->sa_family) {
160 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
161 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
162 flags |= NI_NUMERICHOST;
163 v4a >>= IN_CLASSA_NSHIFT;
165 flags |= NI_NUMERICHOST;
170 const struct sockaddr_in6 *sin6;
171 sin6 = (const struct sockaddr_in6 *)sa;
172 switch (sin6->sin6_addr.s6_addr[0]) {
174 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
176 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
179 flags |= NI_NUMERICHOST;
182 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
183 flags |= NI_NUMERICHOST;
185 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
186 flags |= NI_NUMERICHOST;
193 if (host == NULL || hostlen == 0) {
195 * do nothing in this case.
196 * in case you are wondering if "&&" is more correct than
197 * "||" here: rfc2553bis-03 says that host == NULL or
198 * hostlen == 0 means that the caller does not want the result.
200 } else if (flags & NI_NUMERICHOST) {
203 /* NUMERICHOST and NAMEREQD conflicts with each other */
204 if (flags & NI_NAMEREQD)
213 if ((error = ip6_parsenumeric(sa, addr, host,
214 hostlen, flags)) != 0)
220 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
223 numaddrlen = strlen(numaddr);
224 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
226 strlcpy(host, numaddr, hostlen);
230 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
235 * commented out, since "for local host" is not
236 * implemented here - see RFC2553 p30
238 if (flags & NI_NOFQDN) {
240 p = strchr(hp->h_name, '.');
245 if (strlen(hp->h_name) + 1 > hostlen) {
249 strlcpy(host, hp->h_name, hostlen);
252 if (flags & NI_NAMEREQD)
260 if ((error = ip6_parsenumeric(sa, addr, host,
268 if (inet_ntop(afd->a_af, addr, host,
280 ip6_parsenumeric(sa, addr, host, hostlen, flags)
281 const struct sockaddr *sa;
290 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
293 numaddrlen = strlen(numaddr);
294 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
296 strlcpy(host, numaddr, hostlen);
298 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
299 char zonebuf[MAXHOSTNAMELEN];
302 zonelen = ip6_sa2str(
303 (const struct sockaddr_in6 *)(const void *)sa,
304 zonebuf, sizeof(zonebuf), flags);
307 if (zonelen + 1 + numaddrlen + 1 > hostlen)
310 /* construct <numeric-addr><delim><zoneid> */
311 memcpy(host + numaddrlen + 1, zonebuf,
313 host[numaddrlen] = SCOPE_DELIMITER;
314 host[numaddrlen + 1 + zonelen] = '\0';
322 ip6_sa2str(sa6, buf, bufsiz, flags)
323 const struct sockaddr_in6 *sa6;
328 unsigned int ifindex;
329 const struct in6_addr *a6;
332 ifindex = (unsigned int)sa6->sin6_scope_id;
333 a6 = &sa6->sin6_addr;
335 #ifdef NI_NUMERICSCOPE
336 if ((flags & NI_NUMERICSCOPE) != 0) {
337 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
338 if (n < 0 || n >= bufsiz)
345 /* if_indextoname() does not take buffer size. not a good api... */
346 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
347 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
348 char *p = if_indextoname(ifindex, buf);
355 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
356 if (n < 0 || n >= bufsiz)