1 /* $FreeBSD: src/lib/libc/net/getnameinfo.c,v 1.4.2.5 2002/07/31 10:11:09 ume Exp $ */
2 /* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked
36 * - RFC2553 says that we should raise error on short buffer. X/Open says
37 * we need to truncate the result. We obey RFC2553 (and X/Open should be
38 * modified). ipngwg rough consensus seems to follow RFC2553.
39 * - What is "local" in NI_FQDN?
40 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
41 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
42 * sin6_scope_id is filled - standardization status?
43 * XXX breaks backward compat for code that expects no scopeid.
47 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <arpa/nameser.h>
59 static const struct afd {
66 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
67 offsetof(struct sockaddr_in6, sin6_addr)},
69 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
70 offsetof(struct sockaddr_in, sin_addr)},
81 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
83 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
87 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
88 const struct sockaddr *sa;
96 const struct afd *afd;
110 if (sa->sa_len != salen)
113 family = sa->sa_family;
114 for (i = 0; afdl[i].a_af; i++)
115 if (afdl[i].a_af == family) {
122 if (salen != afd->a_socklen)
125 /* network byte order */
126 port = ((const struct sockinet *)sa)->si_port;
127 addr = (const char *)sa + afd->a_off;
129 if (serv == NULL || servlen == 0) {
131 * do nothing in this case.
132 * in case you are wondering if "&&" is more correct than
133 * "||" here: rfc2553bis-03 says that serv == NULL OR
134 * servlen == 0 means that the caller does not want the result.
137 if (flags & NI_NUMERICSERV)
140 sp = getservbyport(port,
141 (flags & NI_DGRAM) ? "udp" : "tcp");
144 if (strlen(sp->s_name) + 1 > servlen)
146 strlcpy(serv, sp->s_name, servlen);
148 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
149 if (strlen(numserv) + 1 > servlen)
151 strlcpy(serv, numserv, servlen);
155 switch (sa->sa_family) {
158 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
159 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
160 flags |= NI_NUMERICHOST;
161 v4a >>= IN_CLASSA_NSHIFT;
163 flags |= NI_NUMERICHOST;
168 const struct sockaddr_in6 *sin6;
169 sin6 = (const struct sockaddr_in6 *)sa;
170 switch (sin6->sin6_addr.s6_addr[0]) {
172 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
174 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
177 flags |= NI_NUMERICHOST;
180 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
181 flags |= NI_NUMERICHOST;
183 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
184 flags |= NI_NUMERICHOST;
191 if (host == NULL || hostlen == 0) {
193 * do nothing in this case.
194 * in case you are wondering if "&&" is more correct than
195 * "||" here: rfc2553bis-03 says that host == NULL or
196 * hostlen == 0 means that the caller does not want the result.
198 } else if (flags & NI_NUMERICHOST) {
201 /* NUMERICHOST and NAMEREQD conflicts with each other */
202 if (flags & NI_NAMEREQD)
211 if ((error = ip6_parsenumeric(sa, addr, host,
212 hostlen, flags)) != 0)
218 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
221 numaddrlen = strlen(numaddr);
222 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
224 strlcpy(host, numaddr, hostlen);
228 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
233 * commented out, since "for local host" is not
234 * implemented here - see RFC2553 p30
236 if (flags & NI_NOFQDN) {
238 p = strchr(hp->h_name, '.');
243 if (strlen(hp->h_name) + 1 > hostlen) {
247 strlcpy(host, hp->h_name, hostlen);
250 if (flags & NI_NAMEREQD)
258 if ((error = ip6_parsenumeric(sa, addr, host,
266 if (inet_ntop(afd->a_af, addr, host,
278 ip6_parsenumeric(sa, addr, host, hostlen, flags)
279 const struct sockaddr *sa;
288 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
291 numaddrlen = strlen(numaddr);
292 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
294 strlcpy(host, numaddr, hostlen);
296 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
297 char zonebuf[MAXHOSTNAMELEN];
300 zonelen = ip6_sa2str(
301 (const struct sockaddr_in6 *)(const void *)sa,
302 zonebuf, sizeof(zonebuf), flags);
305 if (zonelen + 1 + numaddrlen + 1 > hostlen)
308 /* construct <numeric-addr><delim><zoneid> */
309 memcpy(host + numaddrlen + 1, zonebuf,
311 host[numaddrlen] = SCOPE_DELIMITER;
312 host[numaddrlen + 1 + zonelen] = '\0';
320 ip6_sa2str(sa6, buf, bufsiz, flags)
321 const struct sockaddr_in6 *sa6;
326 unsigned int ifindex;
327 const struct in6_addr *a6;
330 ifindex = (unsigned int)sa6->sin6_scope_id;
331 a6 = &sa6->sin6_addr;
333 #ifdef NI_NUMERICSCOPE
334 if ((flags & NI_NUMERICSCOPE) != 0) {
335 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
336 if (n < 0 || n >= bufsiz)
343 /* if_indextoname() does not take buffer size. not a good api... */
344 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
345 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
346 char *p = if_indextoname(ifindex, buf);
353 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
354 if (n < 0 || n >= bufsiz)