blob: fcc47e7859fd2e30e3574fc3847ee55b8dfdd284 [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>
Elliott Hughesc3d45b82012-06-11 15:01:10 -070056#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057#include <net/if_dl.h>
Elliott Hughesc3d45b82012-06-11 15:01:10 -070058#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#include <net/if_ieee1394.h>
60#include <net/if_types.h>
61#include <netinet/in.h>
62#include <arpa/inet.h>
63#include "arpa_nameser.h"
64#include <assert.h>
65#include <limits.h>
66#include <netdb.h>
67#ifdef ANDROID_CHANGES
68#include "resolv_private.h"
Mattias Falk149f7df2011-02-15 08:45:26 +010069#include <sys/system_properties.h>
70#include <stdlib.h>
71#include <unistd.h>
72#include <sys/un.h>
73#include <errno.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074#else
75#include <resolv.h>
76#endif
77#include <stddef.h>
78#include <string.h>
79
80static const struct afd {
81 int a_af;
82 socklen_t a_addrlen;
83 socklen_t a_socklen;
84 int a_off;
85} afdl [] = {
86#ifdef INET6
87 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
88 offsetof(struct sockaddr_in6, sin6_addr)},
89#endif
90 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
91 offsetof(struct sockaddr_in, sin_addr)},
92 {0, 0, 0, 0},
93};
94
95struct sockinet {
96 u_char si_len;
97 u_char si_family;
98 u_short si_port;
99};
100
101static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
102 socklen_t, char *, socklen_t, int));
103#ifdef INET6
104static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
105 socklen_t, int));
106static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t,
107 int));
108#endif
Elliott Hughesc3d45b82012-06-11 15:01:10 -0700109#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
111 socklen_t, char *, socklen_t, int));
Elliott Hughesc3d45b82012-06-11 15:01:10 -0700112#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113static int hexname __P((const u_int8_t *, size_t, char *, socklen_t));
114
115/*
116 * Top-level getnameinfo() code. Look at the address family, and pick an
117 * appropriate function to call.
118 */
119int 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);
Elliott Hughesc3d45b82012-06-11 15:01:10 -0700126#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127 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
Mattias Falk149f7df2011-02-15 08:45:26 +0100136#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. */
141static int
Brian Carlstromc13fa642011-04-27 11:55:43 -0700142android_gethostbyaddr_proxy(struct hostent* hp, const void *addr, socklen_t addrLen, int addrFamily) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
Mattias Falk149f7df2011-02-15 08:45:26 +0100144 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));
Brian Carlstromc13fa642011-04-27 11:55:43 -0700177 if (TEMP_FAILURE_RETRY(connect(sock, (const struct sockaddr*) (void*) &proxy_addr,
Mattias Falk149f7df2011-02-15 08:45:26 +0100178 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
Brian Carlstromc13fa642011-04-27 11:55:43 -0700189 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) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100195 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 uint32_t name_len;
205 if (fread(&name_len, sizeof(name_len), 1, proxy) != 1) {
206 goto exit;
207 }
208
209 name_len = ntohl(name_len);
210 if (name_len <= 0) {
211 goto exit;
212 }
213
214 if (fread(hp->h_name, name_len, 1, proxy) != 1) {
215 goto exit;
216 }
217
218 result = name_len;
219
220 exit:
221 if (proxy != NULL) {
222 fclose(proxy);
223 }
224
225 return result;
226}
227#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228/*
229 * getnameinfo_inet():
230 * Format an IPv4 or IPv6 sockaddr into a printable string.
231 */
232static int
233getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
234 const struct sockaddr *sa;
235 socklen_t salen;
236 char *host;
237 socklen_t hostlen;
238 char *serv;
239 socklen_t servlen;
240 int flags;
241{
242 const struct afd *afd;
243 struct servent *sp;
244 struct hostent *hp;
245 u_short port;
246 int family, i;
247 const char *addr;
248 u_int32_t v4a;
249 char numserv[512];
250 char numaddr[512];
251
252 /* sa is checked below */
253 /* host may be NULL */
254 /* serv may be NULL */
255
256 if (sa == NULL)
257 return EAI_FAIL;
258
259#ifdef BSD4_4
260 if (sa->sa_len != salen)
261 return EAI_FAIL;
262#endif
263
264 family = sa->sa_family;
265 for (i = 0; afdl[i].a_af; i++)
266 if (afdl[i].a_af == family) {
267 afd = &afdl[i];
268 goto found;
269 }
270 return EAI_FAMILY;
271
272 found:
273 if (salen != afd->a_socklen)
274 return EAI_FAIL;
275
276 /* network byte order */
277 port = ((const struct sockinet *)(const void *)sa)->si_port;
278 addr = (const char *)(const void *)sa + afd->a_off;
279
280 if (serv == NULL || servlen == 0) {
281 /*
282 * do nothing in this case.
283 * in case you are wondering if "&&" is more correct than
284 * "||" here: rfc2553bis-03 says that serv == NULL OR
285 * servlen == 0 means that the caller does not want the result.
286 */
287 } else {
288 if (flags & NI_NUMERICSERV)
289 sp = NULL;
290 else {
291 sp = getservbyport(port,
292 (flags & NI_DGRAM) ? "udp" : "tcp");
293 }
294 if (sp) {
295 if (strlen(sp->s_name) + 1 > (size_t)servlen)
296 return EAI_MEMORY;
297 strlcpy(serv, sp->s_name, servlen);
298 } else {
299 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
300 if (strlen(numserv) + 1 > (size_t)servlen)
301 return EAI_MEMORY;
302 strlcpy(serv, numserv, servlen);
303 }
304 }
305
306 switch (sa->sa_family) {
307 case AF_INET:
308 v4a = (u_int32_t)
309 ntohl(((const struct sockaddr_in *)
310 (const void *)sa)->sin_addr.s_addr);
311 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
312 flags |= NI_NUMERICHOST;
313 v4a >>= IN_CLASSA_NSHIFT;
314 if (v4a == 0)
315 flags |= NI_NUMERICHOST;
316 break;
317#ifdef INET6
318 case AF_INET6:
319 {
320 const struct sockaddr_in6 *sin6;
321 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
322 switch (sin6->sin6_addr.s6_addr[0]) {
323 case 0x00:
324 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
325 ;
326 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
327 ;
328 else
329 flags |= NI_NUMERICHOST;
330 break;
331 default:
332 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
333 flags |= NI_NUMERICHOST;
334 }
335 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
336 flags |= NI_NUMERICHOST;
337 break;
338 }
339 }
340 break;
341#endif
342 }
343 if (host == NULL || hostlen == 0) {
344 /*
345 * do nothing in this case.
346 * in case you are wondering if "&&" is more correct than
347 * "||" here: rfc2553bis-03 says that host == NULL or
348 * hostlen == 0 means that the caller does not want the result.
349 */
350 } else if (flags & NI_NUMERICHOST) {
351 size_t numaddrlen;
352
353 /* NUMERICHOST and NAMEREQD conflicts with each other */
354 if (flags & NI_NAMEREQD)
355 return EAI_NONAME;
356
357 switch(afd->a_af) {
358#ifdef INET6
359 case AF_INET6:
360 {
361 int error;
362
363 if ((error = ip6_parsenumeric(sa, addr, host,
364 hostlen, flags)) != 0)
365 return(error);
366 break;
367 }
368#endif
369 default:
370 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
371 == NULL)
372 return EAI_SYSTEM;
373 numaddrlen = strlen(numaddr);
374 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
375 return EAI_MEMORY;
376 strlcpy(host, numaddr, hostlen);
377 break;
378 }
379 } else {
Mattias Falk149f7df2011-02-15 08:45:26 +0100380#ifdef ANDROID_CHANGES
381 struct hostent android_proxy_hostent;
382 char android_proxy_buf[MAXDNAME];
383 android_proxy_hostent.h_name = android_proxy_buf;
384
385 int hostnamelen = android_gethostbyaddr_proxy(&android_proxy_hostent,
386 addr, afd->a_addrlen, afd->a_af);
387 if (hostnamelen >= 0) {
388 hp = (hostnamelen > 0) ? &android_proxy_hostent : NULL;
389 } else {
390 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
391 }
392#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
Mattias Falk149f7df2011-02-15 08:45:26 +0100394#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395
396 if (hp) {
Elliott Hughesc3d45b82012-06-11 15:01:10 -0700397#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398 /*
399 * commented out, since "for local host" is not
400 * implemented here - see RFC2553 p30
401 */
402 if (flags & NI_NOFQDN) {
403 char *p;
404 p = strchr(hp->h_name, '.');
405 if (p)
406 *p = '\0';
407 }
408#endif
409 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
410 return EAI_MEMORY;
411 }
412 strlcpy(host, hp->h_name, hostlen);
413 } else {
414 if (flags & NI_NAMEREQD)
415 return EAI_NONAME;
416 switch(afd->a_af) {
417#ifdef INET6
418 case AF_INET6:
419 {
420 int error;
421
422 if ((error = ip6_parsenumeric(sa, addr, host,
423 hostlen,
424 flags)) != 0)
425 return(error);
426 break;
427 }
428#endif
429 default:
430 if (inet_ntop(afd->a_af, addr, host,
431 hostlen) == NULL)
432 return EAI_SYSTEM;
433 break;
434 }
435 }
436 }
437 return(0);
438}
439
440#ifdef INET6
441static int
442ip6_parsenumeric(sa, addr, host, hostlen, flags)
443 const struct sockaddr *sa;
444 const char *addr;
445 char *host;
446 socklen_t hostlen;
447 int flags;
448{
449 size_t numaddrlen;
450 char numaddr[512];
451
452 assert(sa != NULL);
453 assert(addr != NULL);
454 assert(host != NULL);
455
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700456 if (hostlen < 0)
457 return EAI_OVERFLOW;
458
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800459 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
460 return EAI_SYSTEM;
461
462 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700463 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464 return EAI_OVERFLOW;
465 strlcpy(host, numaddr, hostlen);
466
467 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
468 char zonebuf[MAXHOSTNAMELEN];
469 int zonelen;
470
471 zonelen = ip6_sa2str(
472 (const struct sockaddr_in6 *)(const void *)sa,
473 zonebuf, sizeof(zonebuf), flags);
474 if (zonelen < 0)
475 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700476 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800477 return EAI_OVERFLOW;
478 /* construct <numeric-addr><delim><zoneid> */
479 memcpy(host + numaddrlen + 1, zonebuf,
480 (size_t)zonelen);
481 host[numaddrlen] = SCOPE_DELIMITER;
482 host[numaddrlen + 1 + zonelen] = '\0';
483 }
484
485 return 0;
486}
487
488/* ARGSUSED */
489static int
490ip6_sa2str(sa6, buf, bufsiz, flags)
491 const struct sockaddr_in6 *sa6;
492 char *buf;
493 size_t bufsiz;
494 int flags;
495{
496 unsigned int ifindex;
497 const struct in6_addr *a6;
498 int n;
499
500 assert(sa6 != NULL);
501 assert(buf != NULL);
502
503 ifindex = (unsigned int)sa6->sin6_scope_id;
504 a6 = &sa6->sin6_addr;
505
506#ifdef NI_NUMERICSCOPE
507 if ((flags & NI_NUMERICSCOPE) != 0) {
508 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
509 if (n < 0 || n >= bufsiz)
510 return -1;
511 else
512 return n;
513 }
514#endif
515
516 /* if_indextoname() does not take buffer size. not a good api... */
517 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
518 bufsiz >= IF_NAMESIZE) {
519 char *p = if_indextoname(ifindex, buf);
520 if (p) {
521 return(strlen(p));
522 }
523 }
524
525 /* last resort */
526 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
527 if (n < 0 || (size_t) n >= bufsiz)
528 return -1;
529 else
530 return n;
531}
532#endif /* INET6 */
533
534
Elliott Hughesc3d45b82012-06-11 15:01:10 -0700535#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800536/*
537 * getnameinfo_link():
538 * Format a link-layer address into a printable format, paying attention to
539 * the interface type.
540 */
541/* ARGSUSED */
542static int
543getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
544 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
545 int flags)
546{
547 const struct sockaddr_dl *sdl =
548 (const struct sockaddr_dl *)(const void *)sa;
549 const struct ieee1394_hwaddr *iha;
550 int n;
551
552 if (serv != NULL && servlen > 0)
553 *serv = '\0';
554
555 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
556 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
557 if (n < 0 || (socklen_t) n > hostlen) {
558 *host = '\0';
559 return EAI_MEMORY;
560 }
561 return 0;
562 }
563
564 switch (sdl->sdl_type) {
565#ifdef IFT_ECONET
566 case IFT_ECONET:
567 if (sdl->sdl_alen < 2)
568 return EAI_FAMILY;
569 if (CLLADDR(sdl)[1] == 0)
570 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
571 else
572 n = snprintf(host, hostlen, "%u.%u",
573 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
574 if (n < 0 || (socklen_t) n >= hostlen) {
575 *host = '\0';
576 return EAI_MEMORY;
577 } else
578 return 0;
579#endif
580 case IFT_IEEE1394:
581 if (sdl->sdl_alen < sizeof(iha->iha_uid))
582 return EAI_FAMILY;
583 iha =
584 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
585 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
586 host, hostlen);
587 /*
588 * The following have zero-length addresses.
589 * IFT_ATM (net/if_atmsubr.c)
590 * IFT_FAITH (net/if_faith.c)
591 * IFT_GIF (net/if_gif.c)
592 * IFT_LOOP (net/if_loop.c)
593 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
594 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
595 * IFT_STF (net/if_stf.c)
596 * IFT_L2VLAN (net/if_vlan.c)
597 * IFT_PROPVIRTUAL (net/if_bridge.h>
598 */
599 /*
600 * The following use IPv4 addresses as link-layer addresses:
601 * IFT_OTHER (net/if_gre.c)
602 */
603 case IFT_ARCNET: /* default below is believed correct for all these. */
604 case IFT_ETHER:
605 case IFT_FDDI:
606 case IFT_HIPPI:
607 case IFT_ISO88025:
608 default:
609 return hexname((const u_int8_t *)CLLADDR(sdl),
610 (size_t)sdl->sdl_alen, host, hostlen);
611 }
612}
Elliott Hughesc3d45b82012-06-11 15:01:10 -0700613#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800614
615static int
616hexname(cp, len, host, hostlen)
617 const u_int8_t *cp;
618 char *host;
619 size_t len;
620 socklen_t hostlen;
621{
622 int n;
623 size_t i;
624 char *outp = host;
625
626 *outp = '\0';
627 for (i = 0; i < len; i++) {
628 n = snprintf(outp, hostlen, "%s%02x",
629 i ? ":" : "", cp[i]);
630 if (n < 0 || (socklen_t) n >= hostlen) {
631 *host = '\0';
632 return EAI_MEMORY;
633 }
634 outp += n;
635 hostlen -= n;
636 }
637 return 0;
638}