1 /* $KAME: getnameinfo.c,v 1.72 2005/01/13 04:12:03 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 * - RFC2553 says that we should raise error on short buffer. X/Open says
35 * we need to truncate the result. We obey RFC2553 (and X/Open should be
36 * modified). ipngwg rough consensus seems to follow RFC2553. RFC3493 says
37 * nothing about it, but defines a new error code EAI_OVERFLOW which seems
38 * to be intended the code for this case.
39 * - What is "local" in NI_NOFQDN? (see comments in the code)
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 * - what should we do if we should do getservbyport("sctp")?
47 * Considerations about thread-safeness
48 * The code in this file is thread-safe, and so the thread-safeness of
49 * getnameinfo() depends on the property of backend functions.
50 * - getservbyport() is not thread safe for most systems we are targeting.
51 * - getipnodebyaddr() is thread safe. However, many resolver libraries
52 * used in the function are not thread safe.
53 * - gethostbyaddr() is usually not thread safe.
60 #ifndef HAVE_GETNAMEINFO
62 #include <sys/types.h>
63 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <arpa/nameser.h>
74 #ifdef HAVE_INTTYPES_H
77 #include "fetchmail.h"
78 #include "getaddrinfo.h"
80 static const struct afd {
88 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
89 offsetof(struct sockaddr_in6, sin6_addr),
90 offsetof(struct sockaddr_in6, sin6_port)},
92 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
93 offsetof(struct sockaddr_in, sin_addr),
94 offsetof(struct sockaddr_in, sin_port)},
99 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
101 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
105 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
106 const struct sockaddr *sa;
107 #ifdef HAVE_SOCKLEN_T
118 const struct afd *afd;
132 #ifdef HAVE_SA_LEN /*XXX*/
133 if (sa->sa_len != salen)
137 family = sa->sa_family;
138 for (i = 0; afdl[i].a_af; i++)
139 if (afdl[i].a_af == family) {
146 if (salen != afd->a_socklen)
149 /* network byte order */
150 memcpy(&port, (const char *)sa + afd->a_portoff, sizeof(port));
151 addr = (const char *)sa + afd->a_off;
153 if (serv == NULL || servlen == 0) {
155 * do nothing in this case.
156 * in case you are wondering if "&&" is more correct than
157 * "||" here: RFC3493 says that serv == NULL OR servlen == 0
158 * means that the caller does not want the result.
161 if (flags & NI_NUMERICSERV)
164 sp = getservbyport(port,
165 (flags & NI_DGRAM) ? "udp" : "tcp");
168 if (strlen(sp->s_name) + 1 > servlen)
170 strlcpy(serv, sp->s_name, servlen);
172 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
173 if (strlen(numserv) + 1 > servlen)
175 strlcpy(serv, numserv, servlen);
179 switch (sa->sa_family) {
182 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
183 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
184 flags |= NI_NUMERICHOST;
185 v4a >>= IN_CLASSA_NSHIFT;
187 flags |= NI_NUMERICHOST;
192 const struct sockaddr_in6 *sin6;
193 sin6 = (const struct sockaddr_in6 *)sa;
194 switch (sin6->sin6_addr.s6_addr[0]) {
196 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
198 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
201 flags |= NI_NUMERICHOST;
204 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
205 flags |= NI_NUMERICHOST;
206 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
207 flags |= NI_NUMERICHOST;
214 if (host == NULL || hostlen == 0) {
216 * do nothing in this case.
217 * in case you are wondering if "&&" is more correct than
218 * "||" here: RFC3493 says that host == NULL or hostlen == 0
219 * means that the caller does not want the result.
221 } else if (flags & NI_NUMERICHOST) {
222 /* NUMERICHOST and NAMEREQD conflicts with each other */
223 if (flags & NI_NAMEREQD)
228 #ifdef USE_GETIPNODEBY
229 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
231 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
241 if (flags & NI_NOFQDN) {
243 * According to RFC3493 section 6.2, NI_NOFQDN
244 * means "node name portion of the FQDN shall
245 * be returned for local hosts." The following
246 * code tries to implement it by returning the
247 * first label (the part before the first
248 * period) of the FQDN. However, it is not
249 * clear if this always makes sense, since the
250 * given address may be outside of "local
251 * hosts." Due to the unclear description, we
252 * disable the code in this implementation.
255 p = strchr(hp->h_name, '.');
260 if (strlen(hp->h_name) + 1 > hostlen) {
261 #ifdef USE_GETIPNODEBY
266 strlcpy(host, hp->h_name, hostlen);
267 #ifdef USE_GETIPNODEBY
271 if (flags & NI_NAMEREQD)
281 if ((error = ip6_parsenumeric(sa, addr, host,
289 #ifdef HAVE_INET_NTOP
290 if (inet_ntop(afd->a_af, addr, host,
294 if (afd->a_af == AF_INET) {
295 struct in_addr addr_tmp;
296 addr_tmp.s_addr = addr;
297 strlcpy(host, inet_ntoa(addr_tmp), hostlen);
311 ip6_parsenumeric(sa, addr, host, hostlen, flags)
312 const struct sockaddr *sa;
321 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
324 numaddrlen = strlen(numaddr);
325 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
327 strlcpy(host, numaddr, hostlen);
329 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
330 char zonebuf[MAXHOSTNAMELEN];
333 zonelen = ip6_sa2str(
334 (const struct sockaddr_in6 *)(const void *)sa,
335 zonebuf, sizeof(zonebuf), flags);
338 if (zonelen + 1 + numaddrlen + 1 > hostlen)
341 /* construct <numeric-addr><delim><zoneid> */
342 memcpy(host + numaddrlen + 1, zonebuf,
344 host[numaddrlen] = SCOPE_DELIMITER;
345 host[numaddrlen + 1 + zonelen] = '\0';
353 ip6_sa2str(sa6, buf, bufsiz, flags)
354 const struct sockaddr_in6 *sa6;
359 unsigned int ifindex;
360 const struct in6_addr *a6;
363 ifindex = (unsigned int)sa6->sin6_scope_id;
364 a6 = &sa6->sin6_addr;
366 #ifdef NI_NUMERICSCOPE
367 if ((flags & NI_NUMERICSCOPE) != 0) {
368 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
369 if (n < 0 || n >= bufsiz)
376 /* if_indextoname() does not take buffer size. not a good api... */
377 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
378 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
379 char *p = if_indextoname(ifindex, buf);
385 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
386 if (n < 0 || n >= bufsiz)