blob: 7ab48e624f464942f76adbbd49b1897793af3e36 [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 Hughes5056f1f2012-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 Hughes5056f1f2012-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 *,
Mattias Falkf1464ff2011-08-23 14:34:14 +0200102 socklen_t, char *, socklen_t, int, const char*));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103#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 Hughes5056f1f2012-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 Hughes5056f1f2012-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
Selim Gurun06e18312012-02-27 15:58:54 -0800115// This should be synchronized to ResponseCode.h
116static const int DnsProxyQueryResult = 222;
117
118
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119/*
120 * Top-level getnameinfo() code. Look at the address family, and pick an
121 * appropriate function to call.
122 */
123int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags)
124{
Mattias Falkf1464ff2011-08-23 14:34:14 +0200125 return android_getnameinfoforiface(sa, salen, host, hostlen, serv, servlen, flags, NULL);
126}
127
128int android_getnameinfoforiface(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags, const char* iface)
129{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130 switch (sa->sa_family) {
131 case AF_INET:
132 case AF_INET6:
133 return getnameinfo_inet(sa, salen, host, hostlen,
Mattias Falkf1464ff2011-08-23 14:34:14 +0200134 serv, servlen, flags, iface);
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700135#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136 case AF_LINK:
137 return getnameinfo_link(sa, salen, host, hostlen,
Mattias Falkf1464ff2011-08-23 14:34:14 +0200138 serv, servlen, flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139#endif
140 default:
141 return EAI_FAMILY;
142 }
143}
Mattias Falk149f7df2011-02-15 08:45:26 +0100144#ifdef ANDROID_CHANGES
145/* On success length of the host name is returned. A return
146 * value of 0 means there's no host name associated with
147 * the address. On failure -1 is returned in which case
148 * normal execution flow shall continue. */
149static int
Mattias Falkf1464ff2011-08-23 14:34:14 +0200150android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily, const char* iface)
151{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152
Mattias Falk149f7df2011-02-15 08:45:26 +0100153 int sock;
154 const int one = 1;
155 struct sockaddr_un proxy_addr;
156 const char* cache_mode = getenv("ANDROID_DNS_MODE");
157 FILE* proxy = NULL;
158 int result = -1;
159
160 if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) {
161 // Don't use the proxy in local mode. This is used by the
162 // proxy itself.
163 return -1;
164 }
165
Mattias Falk149f7df2011-02-15 08:45:26 +0100166 // 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
Selim Gurun06e18312012-02-27 15:58:54 -0800190 const char* addrStr = inet_ntop(addrFamily, addr, buf, sizeof(buf));
Brian Carlstromc13fa642011-04-27 11:55:43 -0700191 if (addrStr == NULL) {
192 goto exit;
193 }
Mattias Falkf1464ff2011-08-23 14:34:14 +0200194 if (fprintf(proxy, "gethostbyaddr %s %d %d %d %s",
195 addrStr, addrLen, addrFamily, getpid(), iface == NULL ? "^" : iface) < 0) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100196 goto exit;
197 }
198
199 // literal NULL byte at end, required by FrameworkListener
200 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
201 goto exit;
202 }
203
204 result = 0;
Robert Greenwaltc59ba452012-03-09 11:34:27 -0800205 char msg_buf[4];
Selim Gurun06e18312012-02-27 15:58:54 -0800206 // read result code for gethostbyaddr
207 if (fread(msg_buf, 1, sizeof(msg_buf), proxy) != sizeof(msg_buf)) {
208 goto exit;
209 }
210
211 int result_code = (int)strtol(msg_buf, NULL, 10);
212 // verify the code itself
213 if (result_code != DnsProxyQueryResult) {
214 goto exit;
215 }
216
Mattias Falk149f7df2011-02-15 08:45:26 +0100217 uint32_t name_len;
218 if (fread(&name_len, sizeof(name_len), 1, proxy) != 1) {
219 goto exit;
220 }
221
222 name_len = ntohl(name_len);
Selim Gurun06e18312012-02-27 15:58:54 -0800223 if (name_len <= 0 || name_len >= nameBufLen) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100224 goto exit;
225 }
226
Selim Gurun06e18312012-02-27 15:58:54 -0800227 if (fread(nameBuf, name_len, 1, proxy) != 1) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100228 goto exit;
229 }
230
231 result = name_len;
232
233 exit:
234 if (proxy != NULL) {
235 fclose(proxy);
236 }
237
238 return result;
239}
240#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241/*
242 * getnameinfo_inet():
243 * Format an IPv4 or IPv6 sockaddr into a printable string.
244 */
245static int
Mattias Falkf1464ff2011-08-23 14:34:14 +0200246getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags, iface)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800247 const struct sockaddr *sa;
248 socklen_t salen;
249 char *host;
250 socklen_t hostlen;
251 char *serv;
252 socklen_t servlen;
253 int flags;
Mattias Falkf1464ff2011-08-23 14:34:14 +0200254 const char* iface;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255{
256 const struct afd *afd;
257 struct servent *sp;
258 struct hostent *hp;
259 u_short port;
260 int family, i;
261 const char *addr;
262 u_int32_t v4a;
263 char numserv[512];
264 char numaddr[512];
265
266 /* sa is checked below */
267 /* host may be NULL */
268 /* serv may be NULL */
269
270 if (sa == NULL)
271 return EAI_FAIL;
272
273#ifdef BSD4_4
274 if (sa->sa_len != salen)
275 return EAI_FAIL;
276#endif
277
278 family = sa->sa_family;
279 for (i = 0; afdl[i].a_af; i++)
280 if (afdl[i].a_af == family) {
281 afd = &afdl[i];
282 goto found;
283 }
284 return EAI_FAMILY;
285
286 found:
287 if (salen != afd->a_socklen)
288 return EAI_FAIL;
289
290 /* network byte order */
291 port = ((const struct sockinet *)(const void *)sa)->si_port;
292 addr = (const char *)(const void *)sa + afd->a_off;
293
294 if (serv == NULL || servlen == 0) {
295 /*
296 * do nothing in this case.
297 * in case you are wondering if "&&" is more correct than
298 * "||" here: rfc2553bis-03 says that serv == NULL OR
299 * servlen == 0 means that the caller does not want the result.
300 */
301 } else {
302 if (flags & NI_NUMERICSERV)
303 sp = NULL;
304 else {
305 sp = getservbyport(port,
306 (flags & NI_DGRAM) ? "udp" : "tcp");
307 }
308 if (sp) {
309 if (strlen(sp->s_name) + 1 > (size_t)servlen)
310 return EAI_MEMORY;
311 strlcpy(serv, sp->s_name, servlen);
312 } else {
313 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
314 if (strlen(numserv) + 1 > (size_t)servlen)
315 return EAI_MEMORY;
316 strlcpy(serv, numserv, servlen);
317 }
318 }
319
320 switch (sa->sa_family) {
321 case AF_INET:
322 v4a = (u_int32_t)
323 ntohl(((const struct sockaddr_in *)
324 (const void *)sa)->sin_addr.s_addr);
325 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
326 flags |= NI_NUMERICHOST;
327 v4a >>= IN_CLASSA_NSHIFT;
328 if (v4a == 0)
329 flags |= NI_NUMERICHOST;
330 break;
331#ifdef INET6
332 case AF_INET6:
333 {
334 const struct sockaddr_in6 *sin6;
335 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
336 switch (sin6->sin6_addr.s6_addr[0]) {
337 case 0x00:
338 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
339 ;
340 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
341 ;
342 else
343 flags |= NI_NUMERICHOST;
344 break;
345 default:
346 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
347 flags |= NI_NUMERICHOST;
348 }
349 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
350 flags |= NI_NUMERICHOST;
351 break;
352 }
353 }
354 break;
355#endif
356 }
357 if (host == NULL || hostlen == 0) {
358 /*
359 * do nothing in this case.
360 * in case you are wondering if "&&" is more correct than
361 * "||" here: rfc2553bis-03 says that host == NULL or
362 * hostlen == 0 means that the caller does not want the result.
363 */
364 } else if (flags & NI_NUMERICHOST) {
365 size_t numaddrlen;
366
367 /* NUMERICHOST and NAMEREQD conflicts with each other */
368 if (flags & NI_NAMEREQD)
369 return EAI_NONAME;
370
371 switch(afd->a_af) {
372#ifdef INET6
373 case AF_INET6:
374 {
375 int error;
376
377 if ((error = ip6_parsenumeric(sa, addr, host,
378 hostlen, flags)) != 0)
379 return(error);
380 break;
381 }
382#endif
383 default:
384 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
385 == NULL)
386 return EAI_SYSTEM;
387 numaddrlen = strlen(numaddr);
388 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
389 return EAI_MEMORY;
390 strlcpy(host, numaddr, hostlen);
391 break;
392 }
393 } else {
Mattias Falk149f7df2011-02-15 08:45:26 +0100394#ifdef ANDROID_CHANGES
395 struct hostent android_proxy_hostent;
396 char android_proxy_buf[MAXDNAME];
Mattias Falk149f7df2011-02-15 08:45:26 +0100397
Selim Gurun06e18312012-02-27 15:58:54 -0800398 int hostnamelen = android_gethostbyaddr_proxy(android_proxy_buf,
Mattias Falkf1464ff2011-08-23 14:34:14 +0200399 MAXDNAME, addr, afd->a_addrlen, afd->a_af, iface);
Selim Gurun06e18312012-02-27 15:58:54 -0800400 if (hostnamelen > 0) {
401 hp = &android_proxy_hostent;
402 hp->h_name = android_proxy_buf;
403 } else if (!hostnamelen) {
404 hp = NULL;
Mattias Falk149f7df2011-02-15 08:45:26 +0100405 } else {
Mattias Falkf1464ff2011-08-23 14:34:14 +0200406 hp = android_gethostbyaddrforiface(addr, afd->a_addrlen, afd->a_af, iface);
Mattias Falk149f7df2011-02-15 08:45:26 +0100407 }
408#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800409 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
Mattias Falk149f7df2011-02-15 08:45:26 +0100410#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411
412 if (hp) {
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700413#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800414 /*
415 * commented out, since "for local host" is not
416 * implemented here - see RFC2553 p30
417 */
418 if (flags & NI_NOFQDN) {
419 char *p;
420 p = strchr(hp->h_name, '.');
421 if (p)
422 *p = '\0';
423 }
424#endif
425 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
426 return EAI_MEMORY;
427 }
428 strlcpy(host, hp->h_name, hostlen);
429 } else {
430 if (flags & NI_NAMEREQD)
431 return EAI_NONAME;
432 switch(afd->a_af) {
433#ifdef INET6
434 case AF_INET6:
435 {
436 int error;
437
438 if ((error = ip6_parsenumeric(sa, addr, host,
439 hostlen,
440 flags)) != 0)
441 return(error);
442 break;
443 }
444#endif
445 default:
446 if (inet_ntop(afd->a_af, addr, host,
447 hostlen) == NULL)
448 return EAI_SYSTEM;
449 break;
450 }
451 }
452 }
453 return(0);
454}
455
456#ifdef INET6
457static int
458ip6_parsenumeric(sa, addr, host, hostlen, flags)
459 const struct sockaddr *sa;
460 const char *addr;
461 char *host;
462 socklen_t hostlen;
463 int flags;
464{
465 size_t numaddrlen;
466 char numaddr[512];
467
468 assert(sa != NULL);
469 assert(addr != NULL);
470 assert(host != NULL);
471
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700472 if (hostlen < 0)
473 return EAI_OVERFLOW;
474
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800475 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
476 return EAI_SYSTEM;
477
478 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700479 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800480 return EAI_OVERFLOW;
481 strlcpy(host, numaddr, hostlen);
482
483 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
484 char zonebuf[MAXHOSTNAMELEN];
485 int zonelen;
486
487 zonelen = ip6_sa2str(
488 (const struct sockaddr_in6 *)(const void *)sa,
489 zonebuf, sizeof(zonebuf), flags);
490 if (zonelen < 0)
491 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700492 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800493 return EAI_OVERFLOW;
494 /* construct <numeric-addr><delim><zoneid> */
495 memcpy(host + numaddrlen + 1, zonebuf,
496 (size_t)zonelen);
497 host[numaddrlen] = SCOPE_DELIMITER;
498 host[numaddrlen + 1 + zonelen] = '\0';
499 }
500
501 return 0;
502}
503
504/* ARGSUSED */
505static int
506ip6_sa2str(sa6, buf, bufsiz, flags)
507 const struct sockaddr_in6 *sa6;
508 char *buf;
509 size_t bufsiz;
510 int flags;
511{
512 unsigned int ifindex;
513 const struct in6_addr *a6;
514 int n;
515
516 assert(sa6 != NULL);
517 assert(buf != NULL);
518
519 ifindex = (unsigned int)sa6->sin6_scope_id;
520 a6 = &sa6->sin6_addr;
521
522#ifdef NI_NUMERICSCOPE
523 if ((flags & NI_NUMERICSCOPE) != 0) {
524 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
525 if (n < 0 || n >= bufsiz)
526 return -1;
527 else
528 return n;
529 }
530#endif
531
532 /* if_indextoname() does not take buffer size. not a good api... */
533 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
534 bufsiz >= IF_NAMESIZE) {
535 char *p = if_indextoname(ifindex, buf);
536 if (p) {
537 return(strlen(p));
538 }
539 }
540
541 /* last resort */
542 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
543 if (n < 0 || (size_t) n >= bufsiz)
544 return -1;
545 else
546 return n;
547}
548#endif /* INET6 */
549
550
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700551#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800552/*
553 * getnameinfo_link():
554 * Format a link-layer address into a printable format, paying attention to
555 * the interface type.
556 */
557/* ARGSUSED */
558static int
559getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
560 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
561 int flags)
562{
563 const struct sockaddr_dl *sdl =
564 (const struct sockaddr_dl *)(const void *)sa;
565 const struct ieee1394_hwaddr *iha;
566 int n;
567
568 if (serv != NULL && servlen > 0)
569 *serv = '\0';
570
571 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
572 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
573 if (n < 0 || (socklen_t) n > hostlen) {
574 *host = '\0';
575 return EAI_MEMORY;
576 }
577 return 0;
578 }
579
580 switch (sdl->sdl_type) {
581#ifdef IFT_ECONET
582 case IFT_ECONET:
583 if (sdl->sdl_alen < 2)
584 return EAI_FAMILY;
585 if (CLLADDR(sdl)[1] == 0)
586 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
587 else
588 n = snprintf(host, hostlen, "%u.%u",
589 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
590 if (n < 0 || (socklen_t) n >= hostlen) {
591 *host = '\0';
592 return EAI_MEMORY;
593 } else
594 return 0;
595#endif
596 case IFT_IEEE1394:
597 if (sdl->sdl_alen < sizeof(iha->iha_uid))
598 return EAI_FAMILY;
599 iha =
600 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
601 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
602 host, hostlen);
603 /*
604 * The following have zero-length addresses.
605 * IFT_ATM (net/if_atmsubr.c)
606 * IFT_FAITH (net/if_faith.c)
607 * IFT_GIF (net/if_gif.c)
608 * IFT_LOOP (net/if_loop.c)
609 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
610 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
611 * IFT_STF (net/if_stf.c)
612 * IFT_L2VLAN (net/if_vlan.c)
613 * IFT_PROPVIRTUAL (net/if_bridge.h>
614 */
615 /*
616 * The following use IPv4 addresses as link-layer addresses:
617 * IFT_OTHER (net/if_gre.c)
618 */
619 case IFT_ARCNET: /* default below is believed correct for all these. */
620 case IFT_ETHER:
621 case IFT_FDDI:
622 case IFT_HIPPI:
623 case IFT_ISO88025:
624 default:
625 return hexname((const u_int8_t *)CLLADDR(sdl),
626 (size_t)sdl->sdl_alen, host, hostlen);
627 }
628}
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700629#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630
631static int
632hexname(cp, len, host, hostlen)
633 const u_int8_t *cp;
634 char *host;
635 size_t len;
636 socklen_t hostlen;
637{
638 int n;
639 size_t i;
640 char *outp = host;
641
642 *outp = '\0';
643 for (i = 0; i < len; i++) {
644 n = snprintf(outp, hostlen, "%s%02x",
645 i ? ":" : "", cp[i]);
646 if (n < 0 || (socklen_t) n >= hostlen) {
647 *host = '\0';
648 return EAI_MEMORY;
649 }
650 outp += n;
651 hostlen -= n;
652 }
653 return 0;
654}