1 /* $NetBSD: getnameinfo.c,v 1.43 2006/02/17 15:58:26 ginsbach Exp $ */
2 /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */
3
4 /*
5 * Copyright (c) 2000 Ben Harris.
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /*
35 * Issues to be discussed:
36 * - Thread safe-ness must be checked
37 * - RFC2553 says that we should raise error on short buffer. X/Open says
38 * we need to truncate the result. We obey RFC2553 (and X/Open should be
39 * modified). ipngwg rough consensus seems to follow RFC2553.
40 * - What is "local" in NI_FQDN?
41 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
42 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
43 * sin6_scope_id is filled - standardization status?
44 * XXX breaks backward compat for code that expects no scopeid.
45 * beware on merge.
46 */
47
48 #include <sys/cdefs.h>
49 #if defined(LIBC_SCCS) && !defined(lint)
50 __RCSID("$NetBSD: getnameinfo.c,v 1.43 2006/02/17 15:58:26 ginsbach Exp $");
51 #endif /* LIBC_SCCS and not lint */
52
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_ieee1394.h>
58 #include <net/if_types.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include "arpa_nameser.h"
62 #include <assert.h>
63 #include <limits.h>
64 #include <netdb.h>
65 #ifdef ANDROID_CHANGES
66 #include "resolv_private.h"
67 #include <sys/system_properties.h>
68 #include <stdlib.h>
69 #include <unistd.h>
70 #include <sys/un.h>
71 #include <errno.h>
72 #else
73 #include <resolv.h>
74 #endif
75 #include <stddef.h>
76 #include <string.h>
77
78 static const struct afd {
79 int a_af;
80 socklen_t a_addrlen;
81 socklen_t a_socklen;
82 int a_off;
83 } afdl [] = {
84 #ifdef INET6
85 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
86 offsetof(struct sockaddr_in6, sin6_addr)},
87 #endif
88 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
89 offsetof(struct sockaddr_in, sin_addr)},
90 {0, 0, 0, 0},
91 };
92
93 struct sockinet {
94 u_char si_len;
95 u_char si_family;
96 u_short si_port;
97 };
98
99 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
100 socklen_t, char *, socklen_t, int));
101 #ifdef INET6
102 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
103 socklen_t, int));
104 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t,
105 int));
106 #endif
107 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
108 socklen_t, char *, socklen_t, int));
109 static int hexname __P((const u_int8_t *, size_t, char *, socklen_t));
110
111 // This should be synchronized to ResponseCode.h
112 static const int DnsProxyQueryResult = 222;
113
114
115 /*
116 * Top-level getnameinfo() code. Look at the address family, and pick an
117 * appropriate function to call.
118 */
getnameinfo(const struct sockaddr * sa,socklen_t salen,char * host,size_t hostlen,char * serv,size_t servlen,int flags)119 int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags)
120 {
121 switch (sa->sa_family) {
122 case AF_INET:
123 case AF_INET6:
124 return getnameinfo_inet(sa, salen, host, hostlen,
125 serv, servlen, flags);
126 #if 0
127 case AF_LINK:
128 return getnameinfo_link(sa, salen, host, hostlen,
129 serv, servlen, flags);
130 #endif
131 default:
132 return EAI_FAMILY;
133 }
134 }
135
136 #ifdef ANDROID_CHANGES
137 /* On success length of the host name is returned. A return
138 * value of 0 means there's no host name associated with
139 * the address. On failure -1 is returned in which case
140 * normal execution flow shall continue. */
141 static int
android_gethostbyaddr_proxy(char * nameBuf,size_t nameBufLen,const void * addr,socklen_t addrLen,int addrFamily)142 android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily) {
143
144 int sock;
145 const int one = 1;
146 struct sockaddr_un proxy_addr;
147 const char* cache_mode = getenv("ANDROID_DNS_MODE");
148 FILE* proxy = NULL;
149 int result = -1;
150
151 if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) {
152 // Don't use the proxy in local mode. This is used by the
153 // proxy itself.
154 return -1;
155 }
156
157 // Temporary cautious hack to disable the DNS proxy for processes
158 // requesting special treatment. Ideally the DNS proxy should
159 // accomodate these apps, though.
160 char propname[PROP_NAME_MAX];
161 char propvalue[PROP_VALUE_MAX];
162 snprintf(propname, sizeof(propname), "net.dns1.%d", getpid());
163 if (__system_property_get(propname, propvalue) > 0) {
164 return -1;
165 }
166 // create socket
167 sock = socket(AF_UNIX, SOCK_STREAM, 0);
168 if (sock < 0) {
169 return -1;
170 }
171
172 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
173 memset(&proxy_addr, 0, sizeof(proxy_addr));
174 proxy_addr.sun_family = AF_UNIX;
175 strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd",
176 sizeof(proxy_addr.sun_path));
177 if (TEMP_FAILURE_RETRY(connect(sock, (const struct sockaddr*) (void*) &proxy_addr,
178 sizeof(proxy_addr))) != 0) {
179 close(sock);
180 return -1;
181 }
182
183 // send request to DnsProxyListener
184 proxy = fdopen(sock,"r+");
185 if (proxy == NULL) {
186 goto exit;
187 }
188
189 char buf[INET6_ADDRSTRLEN]; // big enough for IPv4 and IPv6
190 const char* addrStr = inet_ntop(addrFamily, addr, buf, sizeof(buf));
191 if (addrStr == NULL) {
192 goto exit;
193 }
194 if (fprintf(proxy, "gethostbyaddr %s %d %d", addrStr, addrLen, addrFamily) < 0) {
195 goto exit;
196 }
197
198 // literal NULL byte at end, required by FrameworkListener
199 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
200 goto exit;
201 }
202
203 result = 0;
204 char msg_buf[4];
205 // read result code for gethostbyaddr
206 if (fread(msg_buf, 1, sizeof(msg_buf), proxy) != sizeof(msg_buf)) {
207 goto exit;
208 }
209
210 int result_code = (int)strtol(msg_buf, NULL, 10);
211 // verify the code itself
212 if (result_code != DnsProxyQueryResult) {
213 goto exit;
214 }
215
216 uint32_t name_len;
217 if (fread(&name_len, sizeof(name_len), 1, proxy) != 1) {
218 goto exit;
219 }
220
221 name_len = ntohl(name_len);
222 if (name_len <= 0 || name_len >= nameBufLen) {
223 goto exit;
224 }
225
226 if (fread(nameBuf, name_len, 1, proxy) != 1) {
227 goto exit;
228 }
229
230 result = name_len;
231
232 exit:
233 if (proxy != NULL) {
234 fclose(proxy);
235 }
236
237 return result;
238 }
239 #endif
240 /*
241 * getnameinfo_inet():
242 * Format an IPv4 or IPv6 sockaddr into a printable string.
243 */
244 static int
getnameinfo_inet(sa,salen,host,hostlen,serv,servlen,flags)245 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
246 const struct sockaddr *sa;
247 socklen_t salen;
248 char *host;
249 socklen_t hostlen;
250 char *serv;
251 socklen_t servlen;
252 int flags;
253 {
254 const struct afd *afd;
255 struct servent *sp;
256 struct hostent *hp;
257 u_short port;
258 int family, i;
259 const char *addr;
260 u_int32_t v4a;
261 char numserv[512];
262 char numaddr[512];
263
264 /* sa is checked below */
265 /* host may be NULL */
266 /* serv may be NULL */
267
268 if (sa == NULL)
269 return EAI_FAIL;
270
271 #ifdef BSD4_4
272 if (sa->sa_len != salen)
273 return EAI_FAIL;
274 #endif
275
276 family = sa->sa_family;
277 for (i = 0; afdl[i].a_af; i++)
278 if (afdl[i].a_af == family) {
279 afd = &afdl[i];
280 goto found;
281 }
282 return EAI_FAMILY;
283
284 found:
285 if (salen != afd->a_socklen)
286 return EAI_FAIL;
287
288 /* network byte order */
289 port = ((const struct sockinet *)(const void *)sa)->si_port;
290 addr = (const char *)(const void *)sa + afd->a_off;
291
292 if (serv == NULL || servlen == 0) {
293 /*
294 * do nothing in this case.
295 * in case you are wondering if "&&" is more correct than
296 * "||" here: rfc2553bis-03 says that serv == NULL OR
297 * servlen == 0 means that the caller does not want the result.
298 */
299 } else {
300 if (flags & NI_NUMERICSERV)
301 sp = NULL;
302 else {
303 sp = getservbyport(port,
304 (flags & NI_DGRAM) ? "udp" : "tcp");
305 }
306 if (sp) {
307 if (strlen(sp->s_name) + 1 > (size_t)servlen)
308 return EAI_MEMORY;
309 strlcpy(serv, sp->s_name, servlen);
310 } else {
311 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
312 if (strlen(numserv) + 1 > (size_t)servlen)
313 return EAI_MEMORY;
314 strlcpy(serv, numserv, servlen);
315 }
316 }
317
318 switch (sa->sa_family) {
319 case AF_INET:
320 v4a = (u_int32_t)
321 ntohl(((const struct sockaddr_in *)
322 (const void *)sa)->sin_addr.s_addr);
323 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
324 flags |= NI_NUMERICHOST;
325 v4a >>= IN_CLASSA_NSHIFT;
326 if (v4a == 0)
327 flags |= NI_NUMERICHOST;
328 break;
329 #ifdef INET6
330 case AF_INET6:
331 {
332 const struct sockaddr_in6 *sin6;
333 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
334 switch (sin6->sin6_addr.s6_addr[0]) {
335 case 0x00:
336 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
337 ;
338 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
339 ;
340 else
341 flags |= NI_NUMERICHOST;
342 break;
343 default:
344 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
345 flags |= NI_NUMERICHOST;
346 }
347 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
348 flags |= NI_NUMERICHOST;
349 break;
350 }
351 }
352 break;
353 #endif
354 }
355 if (host == NULL || hostlen == 0) {
356 /*
357 * do nothing in this case.
358 * in case you are wondering if "&&" is more correct than
359 * "||" here: rfc2553bis-03 says that host == NULL or
360 * hostlen == 0 means that the caller does not want the result.
361 */
362 } else if (flags & NI_NUMERICHOST) {
363 size_t numaddrlen;
364
365 /* NUMERICHOST and NAMEREQD conflicts with each other */
366 if (flags & NI_NAMEREQD)
367 return EAI_NONAME;
368
369 switch(afd->a_af) {
370 #ifdef INET6
371 case AF_INET6:
372 {
373 int error;
374
375 if ((error = ip6_parsenumeric(sa, addr, host,
376 hostlen, flags)) != 0)
377 return(error);
378 break;
379 }
380 #endif
381 default:
382 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
383 == NULL)
384 return EAI_SYSTEM;
385 numaddrlen = strlen(numaddr);
386 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
387 return EAI_MEMORY;
388 strlcpy(host, numaddr, hostlen);
389 break;
390 }
391 } else {
392 #ifdef ANDROID_CHANGES
393 struct hostent android_proxy_hostent;
394 char android_proxy_buf[MAXDNAME];
395
396 int hostnamelen = android_gethostbyaddr_proxy(android_proxy_buf,
397 MAXDNAME, addr, afd->a_addrlen, afd->a_af);
398 if (hostnamelen > 0) {
399 hp = &android_proxy_hostent;
400 hp->h_name = android_proxy_buf;
401 } else if (!hostnamelen) {
402 hp = NULL;
403 } else {
404 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
405 }
406 #else
407 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
408 #endif
409
410 if (hp) {
411 #if 0
412 /*
413 * commented out, since "for local host" is not
414 * implemented here - see RFC2553 p30
415 */
416 if (flags & NI_NOFQDN) {
417 char *p;
418 p = strchr(hp->h_name, '.');
419 if (p)
420 *p = '\0';
421 }
422 #endif
423 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
424 return EAI_MEMORY;
425 }
426 strlcpy(host, hp->h_name, hostlen);
427 } else {
428 if (flags & NI_NAMEREQD)
429 return EAI_NONAME;
430 switch(afd->a_af) {
431 #ifdef INET6
432 case AF_INET6:
433 {
434 int error;
435
436 if ((error = ip6_parsenumeric(sa, addr, host,
437 hostlen,
438 flags)) != 0)
439 return(error);
440 break;
441 }
442 #endif
443 default:
444 if (inet_ntop(afd->a_af, addr, host,
445 hostlen) == NULL)
446 return EAI_SYSTEM;
447 break;
448 }
449 }
450 }
451 return(0);
452 }
453
454 #ifdef INET6
455 static int
ip6_parsenumeric(sa,addr,host,hostlen,flags)456 ip6_parsenumeric(sa, addr, host, hostlen, flags)
457 const struct sockaddr *sa;
458 const char *addr;
459 char *host;
460 socklen_t hostlen;
461 int flags;
462 {
463 size_t numaddrlen;
464 char numaddr[512];
465
466 assert(sa != NULL);
467 assert(addr != NULL);
468 assert(host != NULL);
469
470 if (hostlen < 0)
471 return EAI_OVERFLOW;
472
473 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
474 return EAI_SYSTEM;
475
476 numaddrlen = strlen(numaddr);
477 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
478 return EAI_OVERFLOW;
479 strlcpy(host, numaddr, hostlen);
480
481 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
482 char zonebuf[MAXHOSTNAMELEN];
483 int zonelen;
484
485 zonelen = ip6_sa2str(
486 (const struct sockaddr_in6 *)(const void *)sa,
487 zonebuf, sizeof(zonebuf), flags);
488 if (zonelen < 0)
489 return EAI_OVERFLOW;
490 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
491 return EAI_OVERFLOW;
492 /* construct <numeric-addr><delim><zoneid> */
493 memcpy(host + numaddrlen + 1, zonebuf,
494 (size_t)zonelen);
495 host[numaddrlen] = SCOPE_DELIMITER;
496 host[numaddrlen + 1 + zonelen] = '\0';
497 }
498
499 return 0;
500 }
501
502 /* ARGSUSED */
503 static int
ip6_sa2str(sa6,buf,bufsiz,flags)504 ip6_sa2str(sa6, buf, bufsiz, flags)
505 const struct sockaddr_in6 *sa6;
506 char *buf;
507 size_t bufsiz;
508 int flags;
509 {
510 unsigned int ifindex;
511 const struct in6_addr *a6;
512 int n;
513
514 assert(sa6 != NULL);
515 assert(buf != NULL);
516
517 ifindex = (unsigned int)sa6->sin6_scope_id;
518 a6 = &sa6->sin6_addr;
519
520 #ifdef NI_NUMERICSCOPE
521 if ((flags & NI_NUMERICSCOPE) != 0) {
522 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
523 if (n < 0 || n >= bufsiz)
524 return -1;
525 else
526 return n;
527 }
528 #endif
529
530 /* if_indextoname() does not take buffer size. not a good api... */
531 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
532 bufsiz >= IF_NAMESIZE) {
533 char *p = if_indextoname(ifindex, buf);
534 if (p) {
535 return(strlen(p));
536 }
537 }
538
539 /* last resort */
540 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
541 if (n < 0 || (size_t) n >= bufsiz)
542 return -1;
543 else
544 return n;
545 }
546 #endif /* INET6 */
547
548
549 /*
550 * getnameinfo_link():
551 * Format a link-layer address into a printable format, paying attention to
552 * the interface type.
553 */
554 /* ARGSUSED */
555 static int
getnameinfo_link(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)556 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
557 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
558 int flags)
559 {
560 const struct sockaddr_dl *sdl =
561 (const struct sockaddr_dl *)(const void *)sa;
562 const struct ieee1394_hwaddr *iha;
563 int n;
564
565 if (serv != NULL && servlen > 0)
566 *serv = '\0';
567
568 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
569 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
570 if (n < 0 || (socklen_t) n > hostlen) {
571 *host = '\0';
572 return EAI_MEMORY;
573 }
574 return 0;
575 }
576
577 switch (sdl->sdl_type) {
578 #ifdef IFT_ECONET
579 case IFT_ECONET:
580 if (sdl->sdl_alen < 2)
581 return EAI_FAMILY;
582 if (CLLADDR(sdl)[1] == 0)
583 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
584 else
585 n = snprintf(host, hostlen, "%u.%u",
586 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
587 if (n < 0 || (socklen_t) n >= hostlen) {
588 *host = '\0';
589 return EAI_MEMORY;
590 } else
591 return 0;
592 #endif
593 case IFT_IEEE1394:
594 if (sdl->sdl_alen < sizeof(iha->iha_uid))
595 return EAI_FAMILY;
596 iha =
597 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
598 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
599 host, hostlen);
600 /*
601 * The following have zero-length addresses.
602 * IFT_ATM (net/if_atmsubr.c)
603 * IFT_FAITH (net/if_faith.c)
604 * IFT_GIF (net/if_gif.c)
605 * IFT_LOOP (net/if_loop.c)
606 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
607 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
608 * IFT_STF (net/if_stf.c)
609 * IFT_L2VLAN (net/if_vlan.c)
610 * IFT_PROPVIRTUAL (net/if_bridge.h>
611 */
612 /*
613 * The following use IPv4 addresses as link-layer addresses:
614 * IFT_OTHER (net/if_gre.c)
615 */
616 case IFT_ARCNET: /* default below is believed correct for all these. */
617 case IFT_ETHER:
618 case IFT_FDDI:
619 case IFT_HIPPI:
620 case IFT_ISO88025:
621 default:
622 return hexname((const u_int8_t *)CLLADDR(sdl),
623 (size_t)sdl->sdl_alen, host, hostlen);
624 }
625 }
626
627 static int
hexname(cp,len,host,hostlen)628 hexname(cp, len, host, hostlen)
629 const u_int8_t *cp;
630 char *host;
631 size_t len;
632 socklen_t hostlen;
633 {
634 int n;
635 size_t i;
636 char *outp = host;
637
638 *outp = '\0';
639 for (i = 0; i < len; i++) {
640 n = snprintf(outp, hostlen, "%s%02x",
641 i ? ":" : "", cp[i]);
642 if (n < 0 || (socklen_t) n >= hostlen) {
643 *host = '\0';
644 return EAI_MEMORY;
645 }
646 outp += n;
647 hostlen -= n;
648 }
649 return 0;
650 }
651