blob: 0b9fe5165f80c40ceb609ad62dd5e1e59475ff34 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $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"
Mattias Falk149f7df2011-02-15 08:45:26 +010067#include <sys/system_properties.h>
68#include <stdlib.h>
69#include <unistd.h>
70#include <sys/un.h>
71#include <errno.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072#else
73#include <resolv.h>
74#endif
75#include <stddef.h>
76#include <string.h>
77
78static 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
93struct sockinet {
94 u_char si_len;
95 u_char si_family;
96 u_short si_port;
97};
98
99static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
100 socklen_t, char *, socklen_t, int));
101#ifdef INET6
102static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
103 socklen_t, int));
104static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t,
105 int));
106#endif
107static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
108 socklen_t, char *, socklen_t, int));
109static int hexname __P((const u_int8_t *, size_t, char *, socklen_t));
110
111/*
112 * Top-level getnameinfo() code. Look at the address family, and pick an
113 * appropriate function to call.
114 */
115int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags)
116{
117 switch (sa->sa_family) {
118 case AF_INET:
119 case AF_INET6:
120 return getnameinfo_inet(sa, salen, host, hostlen,
121 serv, servlen, flags);
122#if 0
123 case AF_LINK:
124 return getnameinfo_link(sa, salen, host, hostlen,
125 serv, servlen, flags);
126#endif
127 default:
128 return EAI_FAMILY;
129 }
130}
131
Mattias Falk149f7df2011-02-15 08:45:26 +0100132#ifdef ANDROID_CHANGES
133/* On success length of the host name is returned. A return
134 * value of 0 means there's no host name associated with
135 * the address. On failure -1 is returned in which case
136 * normal execution flow shall continue. */
137static int
138android_gethostbyaddr_proxy(struct hostent* hp, const char *addr, socklen_t addrLen, int addrFamily) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139
Mattias Falk149f7df2011-02-15 08:45:26 +0100140 int sock;
141 const int one = 1;
142 struct sockaddr_un proxy_addr;
143 const char* cache_mode = getenv("ANDROID_DNS_MODE");
144 FILE* proxy = NULL;
145 int result = -1;
146
147 if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) {
148 // Don't use the proxy in local mode. This is used by the
149 // proxy itself.
150 return -1;
151 }
152
153 // Temporary cautious hack to disable the DNS proxy for processes
154 // requesting special treatment. Ideally the DNS proxy should
155 // accomodate these apps, though.
156 char propname[PROP_NAME_MAX];
157 char propvalue[PROP_VALUE_MAX];
158 snprintf(propname, sizeof(propname), "net.dns1.%d", getpid());
159 if (__system_property_get(propname, propvalue) > 0) {
160 return -1;
161 }
162 // create socket
163 sock = socket(AF_UNIX, SOCK_STREAM, 0);
164 if (sock < 0) {
165 return -1;
166 }
167
168 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
169 memset(&proxy_addr, 0, sizeof(proxy_addr));
170 proxy_addr.sun_family = AF_UNIX;
171 strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd",
172 sizeof(proxy_addr.sun_path));
173 if (TEMP_FAILURE_RETRY(connect(sock, (const struct sockaddr*) &proxy_addr,
174 sizeof(proxy_addr))) != 0) {
175 close(sock);
176 return -1;
177 }
178
179 // send request to DnsProxyListener
180 proxy = fdopen(sock,"r+");
181 if (proxy == NULL) {
182 goto exit;
183 }
184
185 if (fprintf(proxy, "gethostbyaddr %s %d %d", addr, addrLen, addrFamily) < 0) {
186 goto exit;
187 }
188
189 // literal NULL byte at end, required by FrameworkListener
190 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
191 goto exit;
192 }
193
194 result = 0;
195 uint32_t name_len;
196 if (fread(&name_len, sizeof(name_len), 1, proxy) != 1) {
197 goto exit;
198 }
199
200 name_len = ntohl(name_len);
201 if (name_len <= 0) {
202 goto exit;
203 }
204
205 if (fread(hp->h_name, name_len, 1, proxy) != 1) {
206 goto exit;
207 }
208
209 result = name_len;
210
211 exit:
212 if (proxy != NULL) {
213 fclose(proxy);
214 }
215
216 return result;
217}
218#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219/*
220 * getnameinfo_inet():
221 * Format an IPv4 or IPv6 sockaddr into a printable string.
222 */
223static int
224getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
225 const struct sockaddr *sa;
226 socklen_t salen;
227 char *host;
228 socklen_t hostlen;
229 char *serv;
230 socklen_t servlen;
231 int flags;
232{
233 const struct afd *afd;
234 struct servent *sp;
235 struct hostent *hp;
236 u_short port;
237 int family, i;
238 const char *addr;
239 u_int32_t v4a;
240 char numserv[512];
241 char numaddr[512];
242
243 /* sa is checked below */
244 /* host may be NULL */
245 /* serv may be NULL */
246
247 if (sa == NULL)
248 return EAI_FAIL;
249
250#ifdef BSD4_4
251 if (sa->sa_len != salen)
252 return EAI_FAIL;
253#endif
254
255 family = sa->sa_family;
256 for (i = 0; afdl[i].a_af; i++)
257 if (afdl[i].a_af == family) {
258 afd = &afdl[i];
259 goto found;
260 }
261 return EAI_FAMILY;
262
263 found:
264 if (salen != afd->a_socklen)
265 return EAI_FAIL;
266
267 /* network byte order */
268 port = ((const struct sockinet *)(const void *)sa)->si_port;
269 addr = (const char *)(const void *)sa + afd->a_off;
270
271 if (serv == NULL || servlen == 0) {
272 /*
273 * do nothing in this case.
274 * in case you are wondering if "&&" is more correct than
275 * "||" here: rfc2553bis-03 says that serv == NULL OR
276 * servlen == 0 means that the caller does not want the result.
277 */
278 } else {
279 if (flags & NI_NUMERICSERV)
280 sp = NULL;
281 else {
282 sp = getservbyport(port,
283 (flags & NI_DGRAM) ? "udp" : "tcp");
284 }
285 if (sp) {
286 if (strlen(sp->s_name) + 1 > (size_t)servlen)
287 return EAI_MEMORY;
288 strlcpy(serv, sp->s_name, servlen);
289 } else {
290 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
291 if (strlen(numserv) + 1 > (size_t)servlen)
292 return EAI_MEMORY;
293 strlcpy(serv, numserv, servlen);
294 }
295 }
296
297 switch (sa->sa_family) {
298 case AF_INET:
299 v4a = (u_int32_t)
300 ntohl(((const struct sockaddr_in *)
301 (const void *)sa)->sin_addr.s_addr);
302 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
303 flags |= NI_NUMERICHOST;
304 v4a >>= IN_CLASSA_NSHIFT;
305 if (v4a == 0)
306 flags |= NI_NUMERICHOST;
307 break;
308#ifdef INET6
309 case AF_INET6:
310 {
311 const struct sockaddr_in6 *sin6;
312 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
313 switch (sin6->sin6_addr.s6_addr[0]) {
314 case 0x00:
315 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
316 ;
317 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
318 ;
319 else
320 flags |= NI_NUMERICHOST;
321 break;
322 default:
323 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
324 flags |= NI_NUMERICHOST;
325 }
326 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
327 flags |= NI_NUMERICHOST;
328 break;
329 }
330 }
331 break;
332#endif
333 }
334 if (host == NULL || hostlen == 0) {
335 /*
336 * do nothing in this case.
337 * in case you are wondering if "&&" is more correct than
338 * "||" here: rfc2553bis-03 says that host == NULL or
339 * hostlen == 0 means that the caller does not want the result.
340 */
341 } else if (flags & NI_NUMERICHOST) {
342 size_t numaddrlen;
343
344 /* NUMERICHOST and NAMEREQD conflicts with each other */
345 if (flags & NI_NAMEREQD)
346 return EAI_NONAME;
347
348 switch(afd->a_af) {
349#ifdef INET6
350 case AF_INET6:
351 {
352 int error;
353
354 if ((error = ip6_parsenumeric(sa, addr, host,
355 hostlen, flags)) != 0)
356 return(error);
357 break;
358 }
359#endif
360 default:
361 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
362 == NULL)
363 return EAI_SYSTEM;
364 numaddrlen = strlen(numaddr);
365 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
366 return EAI_MEMORY;
367 strlcpy(host, numaddr, hostlen);
368 break;
369 }
370 } else {
Mattias Falk149f7df2011-02-15 08:45:26 +0100371#ifdef ANDROID_CHANGES
372 struct hostent android_proxy_hostent;
373 char android_proxy_buf[MAXDNAME];
374 android_proxy_hostent.h_name = android_proxy_buf;
375
376 int hostnamelen = android_gethostbyaddr_proxy(&android_proxy_hostent,
377 addr, afd->a_addrlen, afd->a_af);
378 if (hostnamelen >= 0) {
379 hp = (hostnamelen > 0) ? &android_proxy_hostent : NULL;
380 } else {
381 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
382 }
383#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800384 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
Mattias Falk149f7df2011-02-15 08:45:26 +0100385#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386
387 if (hp) {
388#if 0
389 /*
390 * commented out, since "for local host" is not
391 * implemented here - see RFC2553 p30
392 */
393 if (flags & NI_NOFQDN) {
394 char *p;
395 p = strchr(hp->h_name, '.');
396 if (p)
397 *p = '\0';
398 }
399#endif
400 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
401 return EAI_MEMORY;
402 }
403 strlcpy(host, hp->h_name, hostlen);
404 } else {
405 if (flags & NI_NAMEREQD)
406 return EAI_NONAME;
407 switch(afd->a_af) {
408#ifdef INET6
409 case AF_INET6:
410 {
411 int error;
412
413 if ((error = ip6_parsenumeric(sa, addr, host,
414 hostlen,
415 flags)) != 0)
416 return(error);
417 break;
418 }
419#endif
420 default:
421 if (inet_ntop(afd->a_af, addr, host,
422 hostlen) == NULL)
423 return EAI_SYSTEM;
424 break;
425 }
426 }
427 }
428 return(0);
429}
430
431#ifdef INET6
432static int
433ip6_parsenumeric(sa, addr, host, hostlen, flags)
434 const struct sockaddr *sa;
435 const char *addr;
436 char *host;
437 socklen_t hostlen;
438 int flags;
439{
440 size_t numaddrlen;
441 char numaddr[512];
442
443 assert(sa != NULL);
444 assert(addr != NULL);
445 assert(host != NULL);
446
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700447 if (hostlen < 0)
448 return EAI_OVERFLOW;
449
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
451 return EAI_SYSTEM;
452
453 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700454 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800455 return EAI_OVERFLOW;
456 strlcpy(host, numaddr, hostlen);
457
458 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
459 char zonebuf[MAXHOSTNAMELEN];
460 int zonelen;
461
462 zonelen = ip6_sa2str(
463 (const struct sockaddr_in6 *)(const void *)sa,
464 zonebuf, sizeof(zonebuf), flags);
465 if (zonelen < 0)
466 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700467 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468 return EAI_OVERFLOW;
469 /* construct <numeric-addr><delim><zoneid> */
470 memcpy(host + numaddrlen + 1, zonebuf,
471 (size_t)zonelen);
472 host[numaddrlen] = SCOPE_DELIMITER;
473 host[numaddrlen + 1 + zonelen] = '\0';
474 }
475
476 return 0;
477}
478
479/* ARGSUSED */
480static int
481ip6_sa2str(sa6, buf, bufsiz, flags)
482 const struct sockaddr_in6 *sa6;
483 char *buf;
484 size_t bufsiz;
485 int flags;
486{
487 unsigned int ifindex;
488 const struct in6_addr *a6;
489 int n;
490
491 assert(sa6 != NULL);
492 assert(buf != NULL);
493
494 ifindex = (unsigned int)sa6->sin6_scope_id;
495 a6 = &sa6->sin6_addr;
496
497#ifdef NI_NUMERICSCOPE
498 if ((flags & NI_NUMERICSCOPE) != 0) {
499 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
500 if (n < 0 || n >= bufsiz)
501 return -1;
502 else
503 return n;
504 }
505#endif
506
507 /* if_indextoname() does not take buffer size. not a good api... */
508 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
509 bufsiz >= IF_NAMESIZE) {
510 char *p = if_indextoname(ifindex, buf);
511 if (p) {
512 return(strlen(p));
513 }
514 }
515
516 /* last resort */
517 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
518 if (n < 0 || (size_t) n >= bufsiz)
519 return -1;
520 else
521 return n;
522}
523#endif /* INET6 */
524
525
526/*
527 * getnameinfo_link():
528 * Format a link-layer address into a printable format, paying attention to
529 * the interface type.
530 */
531/* ARGSUSED */
532static int
533getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
534 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
535 int flags)
536{
537 const struct sockaddr_dl *sdl =
538 (const struct sockaddr_dl *)(const void *)sa;
539 const struct ieee1394_hwaddr *iha;
540 int n;
541
542 if (serv != NULL && servlen > 0)
543 *serv = '\0';
544
545 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
546 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
547 if (n < 0 || (socklen_t) n > hostlen) {
548 *host = '\0';
549 return EAI_MEMORY;
550 }
551 return 0;
552 }
553
554 switch (sdl->sdl_type) {
555#ifdef IFT_ECONET
556 case IFT_ECONET:
557 if (sdl->sdl_alen < 2)
558 return EAI_FAMILY;
559 if (CLLADDR(sdl)[1] == 0)
560 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
561 else
562 n = snprintf(host, hostlen, "%u.%u",
563 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
564 if (n < 0 || (socklen_t) n >= hostlen) {
565 *host = '\0';
566 return EAI_MEMORY;
567 } else
568 return 0;
569#endif
570 case IFT_IEEE1394:
571 if (sdl->sdl_alen < sizeof(iha->iha_uid))
572 return EAI_FAMILY;
573 iha =
574 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
575 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
576 host, hostlen);
577 /*
578 * The following have zero-length addresses.
579 * IFT_ATM (net/if_atmsubr.c)
580 * IFT_FAITH (net/if_faith.c)
581 * IFT_GIF (net/if_gif.c)
582 * IFT_LOOP (net/if_loop.c)
583 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
584 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
585 * IFT_STF (net/if_stf.c)
586 * IFT_L2VLAN (net/if_vlan.c)
587 * IFT_PROPVIRTUAL (net/if_bridge.h>
588 */
589 /*
590 * The following use IPv4 addresses as link-layer addresses:
591 * IFT_OTHER (net/if_gre.c)
592 */
593 case IFT_ARCNET: /* default below is believed correct for all these. */
594 case IFT_ETHER:
595 case IFT_FDDI:
596 case IFT_HIPPI:
597 case IFT_ISO88025:
598 default:
599 return hexname((const u_int8_t *)CLLADDR(sdl),
600 (size_t)sdl->sdl_alen, host, hostlen);
601 }
602}
603
604static int
605hexname(cp, len, host, hostlen)
606 const u_int8_t *cp;
607 char *host;
608 size_t len;
609 socklen_t hostlen;
610{
611 int n;
612 size_t i;
613 char *outp = host;
614
615 *outp = '\0';
616 for (i = 0; i < len; i++) {
617 n = snprintf(outp, hostlen, "%s%02x",
618 i ? ":" : "", cp[i]);
619 if (n < 0 || (socklen_t) n >= hostlen) {
620 *host = '\0';
621 return EAI_MEMORY;
622 }
623 outp += n;
624 hostlen -= n;
625 }
626 return 0;
627}