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 family = sa->sa_family;
113 for (i = 0; afdl[i].a_af; i++)
114 if (afdl[i].a_af == family) {
121 if (salen != afd->a_socklen)
124 /* network byte order */
125 port = ((const struct sockinet *)sa)->si_port;
126 addr = (const char *)sa + afd->a_off;
128 if (serv == NULL || servlen == 0) {
130 * do nothing in this case.
131 * in case you are wondering if "&&" is more correct than
132 * "||" here: rfc2553bis-03 says that serv == NULL OR
133 * servlen == 0 means that the caller does not want the result.
136 if (flags & NI_NUMERICSERV)
139 sp = getservbyport(port,
140 (flags & NI_DGRAM) ? "udp" : "tcp");
143 if (strlen(sp->s_name) + 1 > servlen)
145 strlcpy(serv, sp->s_name, servlen);
147 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
148 if (strlen(numserv) + 1 > servlen)
150 strlcpy(serv, numserv, servlen);
154 switch (sa->sa_family) {
157 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
158 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
159 flags |= NI_NUMERICHOST;
160 v4a >>= IN_CLASSA_NSHIFT;
162 flags |= NI_NUMERICHOST;
167 const struct sockaddr_in6 *sin6;
168 sin6 = (const struct sockaddr_in6 *)sa;
169 switch (sin6->sin6_addr.s6_addr[0]) {
171 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
173 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
176 flags |= NI_NUMERICHOST;
179 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
180 flags |= NI_NUMERICHOST;
182 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
183 flags |= NI_NUMERICHOST;
190 if (host == NULL || hostlen == 0) {
192 * do nothing in this case.
193 * in case you are wondering if "&&" is more correct than
194 * "||" here: rfc2553bis-03 says that host == NULL or
195 * hostlen == 0 means that the caller does not want the result.
197 } else if (flags & NI_NUMERICHOST) {
200 /* NUMERICHOST and NAMEREQD conflicts with each other */
201 if (flags & NI_NAMEREQD)
210 if ((error = ip6_parsenumeric(sa, addr, host,
211 hostlen, flags)) != 0)
217 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
220 numaddrlen = strlen(numaddr);
221 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
223 strlcpy(host, numaddr, hostlen);
227 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
232 * commented out, since "for local host" is not
233 * implemented here - see RFC2553 p30
235 if (flags & NI_NOFQDN) {
237 p = strchr(hp->h_name, '.');
242 if (strlen(hp->h_name) + 1 > hostlen) {
246 strlcpy(host, hp->h_name, hostlen);
249 if (flags & NI_NAMEREQD)
257 if ((error = ip6_parsenumeric(sa, addr, host,
265 if (inet_ntop(afd->a_af, addr, host,
277 ip6_parsenumeric(sa, addr, host, hostlen, flags)
278 const struct sockaddr *sa;
287 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
290 numaddrlen = strlen(numaddr);
291 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
293 strlcpy(host, numaddr, hostlen);
295 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
296 char zonebuf[MAXHOSTNAMELEN];
299 zonelen = ip6_sa2str(
300 (const struct sockaddr_in6 *)(const void *)sa,
301 zonebuf, sizeof(zonebuf), flags);
304 if (zonelen + 1 + numaddrlen + 1 > hostlen)
307 /* construct <numeric-addr><delim><zoneid> */
308 memcpy(host + numaddrlen + 1, zonebuf,
310 host[numaddrlen] = SCOPE_DELIMITER;
311 host[numaddrlen + 1 + zonelen] = '\0';
319 ip6_sa2str(sa6, buf, bufsiz, flags)
320 const struct sockaddr_in6 *sa6;
325 unsigned int ifindex;
326 const struct in6_addr *a6;
329 ifindex = (unsigned int)sa6->sin6_scope_id;
330 a6 = &sa6->sin6_addr;
332 #ifdef NI_NUMERICSCOPE
333 if ((flags & NI_NUMERICSCOPE) != 0) {
334 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
335 if (n < 0 || n >= bufsiz)
342 /* if_indextoname() does not take buffer size. not a good api... */
343 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
344 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
345 char *p = if_indextoname(ifindex, buf);
352 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
353 if (n < 0 || (size_t)n >= bufsiz)