2 * Issues to be discussed:
3 * - Thread safe-ness must be checked
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by WIDE Project and
22 * 4. Neither the name of the project nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <port_before.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
54 #include <port_after.h>
57 * Note that a_off will be dynamically adjusted so that to be consistent
58 * with the definition of sockaddr_in{,6}.
59 * The value presented below is just a guess.
67 /* first entry is linked last... */
68 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
69 offsetof(struct sockaddr_in, sin_addr)},
70 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
71 offsetof(struct sockaddr_in6, sin6_addr)},
83 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
85 #ifdef HAVE_SIN6_SCOPE_ID
86 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
90 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
91 const struct sockaddr *sa;
111 const struct sockaddr_in6 *sin6;
118 if (len != salen) return EAI_FAIL;
121 family = sa->sa_family;
122 for (i = 0; afdl[i].a_af; i++)
123 if (afdl[i].a_af == family) {
130 if (salen != afd->a_socklen) return EAI_FAIL;
132 port = ((const struct sockinet *)sa)->si_port; /* network byte order */
133 addr = (const char *)sa + afd->a_off;
135 if (serv == NULL || servlen == 0U) {
137 * rfc2553bis says that serv == NULL or servlen == 0 means that
138 * the caller does not want the result.
140 } else if (flags & NI_NUMERICSERV) {
141 sprintf(numserv, "%d", ntohs(port));
142 if (strlen(numserv) > servlen)
144 strcpy(serv, numserv);
146 sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
148 if (strlen(sp->s_name) + 1 > servlen)
150 strcpy(serv, sp->s_name);
155 switch (sa->sa_family) {
157 if (ntohl(*(const u_int32_t *)addr) >> IN_CLASSA_NSHIFT == 0)
158 flags |= NI_NUMERICHOST;
161 sin6 = (const struct sockaddr_in6 *)sa;
162 switch (sin6->sin6_addr.s6_addr[0]) {
164 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
166 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
169 flags |= NI_NUMERICHOST;
172 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
173 flags |= NI_NUMERICHOST;
174 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
175 flags |= NI_NUMERICHOST;
180 if (host == NULL || hostlen == 0U) {
182 * rfc2553bis says that host == NULL or hostlen == 0 means that
183 * the caller does not want the result.
185 } else if (flags & NI_NUMERICHOST) {
188 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
191 if (flags & NI_NOFQDN) {
192 p = strchr(hp->h_name, '.');
195 if (strlen(hp->h_name) + 1 > hostlen)
197 strcpy(host, hp->h_name);
199 if (flags & NI_NAMEREQD)
207 if ((error = ip6_parsenumeric(sa, addr, host,
215 if (inet_ntop(afd->a_af, addr, numaddr,
216 sizeof(numaddr)) == NULL)
218 if (strlen(numaddr) + 1 > hostlen)
220 strcpy(host, numaddr);
228 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
229 size_t hostlen, int flags)
234 #ifndef HAVE_SIN6_SCOPE_ID
239 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
243 numaddrlen = strlen(numaddr);
244 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
246 strcpy(host, numaddr);
248 #ifdef HAVE_SIN6_SCOPE_ID
249 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
250 char scopebuf[MAXHOSTNAMELEN]; /* XXX */
253 /* ip6_sa2str never fails */
254 scopelen = ip6_sa2str((const struct sockaddr_in6 *)sa,
255 scopebuf, sizeof(scopebuf), flags);
257 if (scopelen + 1 + numaddrlen + 1 > hostlen)
260 /* construct <numeric-addr><delim><scopeid> */
261 memcpy(host + numaddrlen + 1, scopebuf,
263 host[numaddrlen] = SCOPE_DELIMITER;
264 host[numaddrlen + 1 + scopelen] = '\0';
271 #ifdef HAVE_SIN6_SCOPE_ID
274 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf,
275 size_t bufsiz, int flags)
277 #ifdef USE_IFNAMELINKID
278 unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
279 const struct in6_addr *a6 = &sa6->sin6_addr;
283 #ifdef NI_NUMERICSCOPE
284 if (flags & NI_NUMERICSCOPE) {
285 sprintf(tmp, "%u", sa6->sin6_scope_id);
287 strncpy(buf, tmp, bufsiz - 1);
288 buf[bufsiz - 1] = '\0';
294 #ifdef USE_IFNAMELINKID
296 * For a link-local address, convert the index to an interface
297 * name, assuming a one-to-one mapping between links and interfaces.
298 * Note, however, that this assumption is stronger than the
299 * specification of the scoped address architecture; the
300 * specficication says that more than one interfaces can belong to
304 /* if_indextoname() does not take buffer size. not a good api... */
305 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
306 bufsiz >= IF_NAMESIZE) {
307 char *p = if_indextoname(ifindex, buf);
315 sprintf(tmp, "%u", sa6->sin6_scope_id);
317 strncpy(buf, tmp, bufsiz - 1);
318 buf[bufsiz - 1] = '\0';