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(const struct sockaddr *sa, socklen_t salen,
90 char *host, size_t hostlen, char *serv, size_t servlen,
93 const struct afd *afd;
107 family = sa->sa_family;
108 for (i = 0; afdl[i].a_af; i++)
109 if (afdl[i].a_af == family) {
116 if (salen != afd->a_socklen)
119 /* network byte order */
120 port = ((const struct sockinet *)sa)->si_port;
121 addr = (const char *)sa + afd->a_off;
123 if (serv == NULL || servlen == 0) {
125 * do nothing in this case.
126 * in case you are wondering if "&&" is more correct than
127 * "||" here: rfc2553bis-03 says that serv == NULL OR
128 * servlen == 0 means that the caller does not want the result.
131 if (flags & NI_NUMERICSERV)
134 sp = getservbyport(port,
135 (flags & NI_DGRAM) ? "udp" : "tcp");
138 if (strlen(sp->s_name) + 1 > servlen)
140 strlcpy(serv, sp->s_name, servlen);
142 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
143 if (strlen(numserv) + 1 > servlen)
145 strlcpy(serv, numserv, servlen);
149 switch (sa->sa_family) {
152 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
153 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
154 flags |= NI_NUMERICHOST;
155 v4a >>= IN_CLASSA_NSHIFT;
157 flags |= NI_NUMERICHOST;
162 const struct sockaddr_in6 *sin6;
163 sin6 = (const struct sockaddr_in6 *)sa;
164 switch (sin6->sin6_addr.s6_addr[0]) {
166 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
168 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
171 flags |= NI_NUMERICHOST;
174 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
175 flags |= NI_NUMERICHOST;
177 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
178 flags |= NI_NUMERICHOST;
185 if (host == NULL || hostlen == 0) {
187 * do nothing in this case.
188 * in case you are wondering if "&&" is more correct than
189 * "||" here: rfc2553bis-03 says that host == NULL or
190 * hostlen == 0 means that the caller does not want the result.
192 } else if (flags & NI_NUMERICHOST) {
195 /* NUMERICHOST and NAMEREQD conflicts with each other */
196 if (flags & NI_NAMEREQD)
205 if ((error = ip6_parsenumeric(sa, addr, host,
206 hostlen, flags)) != 0)
212 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
215 numaddrlen = strlen(numaddr);
216 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
218 strlcpy(host, numaddr, hostlen);
222 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
227 * commented out, since "for local host" is not
228 * implemented here - see RFC2553 p30
230 if (flags & NI_NOFQDN) {
232 p = strchr(hp->h_name, '.');
237 if (strlen(hp->h_name) + 1 > hostlen) {
241 strlcpy(host, hp->h_name, hostlen);
244 if (flags & NI_NAMEREQD)
252 if ((error = ip6_parsenumeric(sa, addr, host,
260 if (inet_ntop(afd->a_af, addr, host,
272 ip6_parsenumeric(const struct sockaddr *sa, const char *addr,
273 char *host, size_t hostlen, int flags)
278 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
281 numaddrlen = strlen(numaddr);
282 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
284 strlcpy(host, numaddr, hostlen);
286 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
287 char zonebuf[MAXHOSTNAMELEN];
290 zonelen = ip6_sa2str(
291 (const struct sockaddr_in6 *)(const void *)sa,
292 zonebuf, sizeof(zonebuf), flags);
295 if (zonelen + 1 + numaddrlen + 1 > hostlen)
298 /* construct <numeric-addr><delim><zoneid> */
299 memcpy(host + numaddrlen + 1, zonebuf,
301 host[numaddrlen] = SCOPE_DELIMITER;
302 host[numaddrlen + 1 + zonelen] = '\0';
310 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
312 unsigned int ifindex;
313 const struct in6_addr *a6;
316 ifindex = (unsigned int)sa6->sin6_scope_id;
317 a6 = &sa6->sin6_addr;
319 #ifdef NI_NUMERICSCOPE
320 if ((flags & NI_NUMERICSCOPE) != 0) {
321 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
322 if (n < 0 || n >= bufsiz)
329 /* if_indextoname() does not take buffer size. not a good api... */
330 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
331 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
332 char *p = if_indextoname(ifindex, buf);
339 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
340 if (n < 0 || (size_t)n >= bufsiz)