blob: d8ac03743f2de74eab2e020280322340918382fc [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 *,
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000102 socklen_t, char *, socklen_t, int));
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{
125 switch (sa->sa_family) {
126 case AF_INET:
127 case AF_INET6:
128 return getnameinfo_inet(sa, salen, host, hostlen,
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000129 serv, servlen, flags);
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700130#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131 case AF_LINK:
132 return getnameinfo_link(sa, salen, host, hostlen,
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000133 serv, servlen, flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134#endif
135 default:
136 return EAI_FAMILY;
137 }
138}
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000139
Mattias Falk149f7df2011-02-15 08:45:26 +0100140#ifdef ANDROID_CHANGES
141/* On success length of the host name is returned. A return
142 * value of 0 means there's no host name associated with
143 * the address. On failure -1 is returned in which case
144 * normal execution flow shall continue. */
145static int
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000146android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800147
Mattias Falk149f7df2011-02-15 08:45:26 +0100148 int sock;
149 const int one = 1;
150 struct sockaddr_un proxy_addr;
151 const char* cache_mode = getenv("ANDROID_DNS_MODE");
152 FILE* proxy = NULL;
153 int result = -1;
154
155 if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) {
156 // Don't use the proxy in local mode. This is used by the
157 // proxy itself.
158 return -1;
159 }
160
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000161 // Temporary cautious hack to disable the DNS proxy for processes
162 // requesting special treatment. Ideally the DNS proxy should
163 // accomodate these apps, though.
164 char propname[PROP_NAME_MAX];
165 char propvalue[PROP_VALUE_MAX];
166 snprintf(propname, sizeof(propname), "net.dns1.%d", getpid());
167 if (__system_property_get(propname, propvalue) > 0) {
168 return -1;
169 }
Mattias Falk149f7df2011-02-15 08:45:26 +0100170 // create socket
171 sock = socket(AF_UNIX, SOCK_STREAM, 0);
172 if (sock < 0) {
173 return -1;
174 }
175
176 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
177 memset(&proxy_addr, 0, sizeof(proxy_addr));
178 proxy_addr.sun_family = AF_UNIX;
179 strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd",
180 sizeof(proxy_addr.sun_path));
Brian Carlstromc13fa642011-04-27 11:55:43 -0700181 if (TEMP_FAILURE_RETRY(connect(sock, (const struct sockaddr*) (void*) &proxy_addr,
Mattias Falk149f7df2011-02-15 08:45:26 +0100182 sizeof(proxy_addr))) != 0) {
183 close(sock);
184 return -1;
185 }
186
187 // send request to DnsProxyListener
188 proxy = fdopen(sock,"r+");
189 if (proxy == NULL) {
190 goto exit;
191 }
192
Brian Carlstromc13fa642011-04-27 11:55:43 -0700193 char buf[INET6_ADDRSTRLEN]; // big enough for IPv4 and IPv6
Selim Gurun06e18312012-02-27 15:58:54 -0800194 const char* addrStr = inet_ntop(addrFamily, addr, buf, sizeof(buf));
Brian Carlstromc13fa642011-04-27 11:55:43 -0700195 if (addrStr == NULL) {
196 goto exit;
197 }
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000198 if (fprintf(proxy, "gethostbyaddr %s %d %d", addrStr, addrLen, addrFamily) < 0) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100199 goto exit;
200 }
201
202 // literal NULL byte at end, required by FrameworkListener
203 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
204 goto exit;
205 }
206
207 result = 0;
Robert Greenwaltc59ba452012-03-09 11:34:27 -0800208 char msg_buf[4];
Selim Gurun06e18312012-02-27 15:58:54 -0800209 // read result code for gethostbyaddr
210 if (fread(msg_buf, 1, sizeof(msg_buf), proxy) != sizeof(msg_buf)) {
211 goto exit;
212 }
213
214 int result_code = (int)strtol(msg_buf, NULL, 10);
215 // verify the code itself
216 if (result_code != DnsProxyQueryResult) {
217 goto exit;
218 }
219
Mattias Falk149f7df2011-02-15 08:45:26 +0100220 uint32_t name_len;
221 if (fread(&name_len, sizeof(name_len), 1, proxy) != 1) {
222 goto exit;
223 }
224
225 name_len = ntohl(name_len);
Selim Gurun06e18312012-02-27 15:58:54 -0800226 if (name_len <= 0 || name_len >= nameBufLen) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100227 goto exit;
228 }
229
Selim Gurun06e18312012-02-27 15:58:54 -0800230 if (fread(nameBuf, name_len, 1, proxy) != 1) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100231 goto exit;
232 }
233
234 result = name_len;
235
236 exit:
237 if (proxy != NULL) {
238 fclose(proxy);
239 }
240
241 return result;
242}
243#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244/*
245 * getnameinfo_inet():
246 * Format an IPv4 or IPv6 sockaddr into a printable string.
247 */
248static int
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000249getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800250 const struct sockaddr *sa;
251 socklen_t salen;
252 char *host;
253 socklen_t hostlen;
254 char *serv;
255 socklen_t servlen;
256 int flags;
257{
258 const struct afd *afd;
259 struct servent *sp;
260 struct hostent *hp;
261 u_short port;
262 int family, i;
263 const char *addr;
264 u_int32_t v4a;
265 char numserv[512];
266 char numaddr[512];
267
268 /* sa is checked below */
269 /* host may be NULL */
270 /* serv may be NULL */
271
272 if (sa == NULL)
273 return EAI_FAIL;
274
275#ifdef BSD4_4
276 if (sa->sa_len != salen)
277 return EAI_FAIL;
278#endif
279
280 family = sa->sa_family;
281 for (i = 0; afdl[i].a_af; i++)
282 if (afdl[i].a_af == family) {
283 afd = &afdl[i];
284 goto found;
285 }
286 return EAI_FAMILY;
287
288 found:
289 if (salen != afd->a_socklen)
290 return EAI_FAIL;
291
292 /* network byte order */
293 port = ((const struct sockinet *)(const void *)sa)->si_port;
294 addr = (const char *)(const void *)sa + afd->a_off;
295
296 if (serv == NULL || servlen == 0) {
297 /*
298 * do nothing in this case.
299 * in case you are wondering if "&&" is more correct than
300 * "||" here: rfc2553bis-03 says that serv == NULL OR
301 * servlen == 0 means that the caller does not want the result.
302 */
303 } else {
304 if (flags & NI_NUMERICSERV)
305 sp = NULL;
306 else {
307 sp = getservbyport(port,
308 (flags & NI_DGRAM) ? "udp" : "tcp");
309 }
310 if (sp) {
311 if (strlen(sp->s_name) + 1 > (size_t)servlen)
312 return EAI_MEMORY;
313 strlcpy(serv, sp->s_name, servlen);
314 } else {
315 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
316 if (strlen(numserv) + 1 > (size_t)servlen)
317 return EAI_MEMORY;
318 strlcpy(serv, numserv, servlen);
319 }
320 }
321
322 switch (sa->sa_family) {
323 case AF_INET:
324 v4a = (u_int32_t)
325 ntohl(((const struct sockaddr_in *)
326 (const void *)sa)->sin_addr.s_addr);
327 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
328 flags |= NI_NUMERICHOST;
329 v4a >>= IN_CLASSA_NSHIFT;
330 if (v4a == 0)
331 flags |= NI_NUMERICHOST;
332 break;
333#ifdef INET6
334 case AF_INET6:
335 {
336 const struct sockaddr_in6 *sin6;
337 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
338 switch (sin6->sin6_addr.s6_addr[0]) {
339 case 0x00:
340 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
341 ;
342 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
343 ;
344 else
345 flags |= NI_NUMERICHOST;
346 break;
347 default:
348 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
349 flags |= NI_NUMERICHOST;
350 }
351 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
352 flags |= NI_NUMERICHOST;
353 break;
354 }
355 }
356 break;
357#endif
358 }
359 if (host == NULL || hostlen == 0) {
360 /*
361 * do nothing in this case.
362 * in case you are wondering if "&&" is more correct than
363 * "||" here: rfc2553bis-03 says that host == NULL or
364 * hostlen == 0 means that the caller does not want the result.
365 */
366 } else if (flags & NI_NUMERICHOST) {
367 size_t numaddrlen;
368
369 /* NUMERICHOST and NAMEREQD conflicts with each other */
370 if (flags & NI_NAMEREQD)
371 return EAI_NONAME;
372
373 switch(afd->a_af) {
374#ifdef INET6
375 case AF_INET6:
376 {
377 int error;
378
379 if ((error = ip6_parsenumeric(sa, addr, host,
380 hostlen, flags)) != 0)
381 return(error);
382 break;
383 }
384#endif
385 default:
386 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
387 == NULL)
388 return EAI_SYSTEM;
389 numaddrlen = strlen(numaddr);
390 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
391 return EAI_MEMORY;
392 strlcpy(host, numaddr, hostlen);
393 break;
394 }
395 } else {
Mattias Falk149f7df2011-02-15 08:45:26 +0100396#ifdef ANDROID_CHANGES
397 struct hostent android_proxy_hostent;
398 char android_proxy_buf[MAXDNAME];
Mattias Falk149f7df2011-02-15 08:45:26 +0100399
Selim Gurun06e18312012-02-27 15:58:54 -0800400 int hostnamelen = android_gethostbyaddr_proxy(android_proxy_buf,
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000401 MAXDNAME, addr, afd->a_addrlen, afd->a_af);
Selim Gurun06e18312012-02-27 15:58:54 -0800402 if (hostnamelen > 0) {
403 hp = &android_proxy_hostent;
404 hp->h_name = android_proxy_buf;
405 } else if (!hostnamelen) {
406 hp = NULL;
Mattias Falk149f7df2011-02-15 08:45:26 +0100407 } else {
Robert Greenwaltb002a2f2013-01-19 00:40:24 +0000408 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
Mattias Falk149f7df2011-02-15 08:45:26 +0100409 }
410#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
Mattias Falk149f7df2011-02-15 08:45:26 +0100412#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413
414 if (hp) {
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700415#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 /*
417 * commented out, since "for local host" is not
418 * implemented here - see RFC2553 p30
419 */
420 if (flags & NI_NOFQDN) {
421 char *p;
422 p = strchr(hp->h_name, '.');
423 if (p)
424 *p = '\0';
425 }
426#endif
427 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
428 return EAI_MEMORY;
429 }
430 strlcpy(host, hp->h_name, hostlen);
431 } else {
432 if (flags & NI_NAMEREQD)
433 return EAI_NONAME;
434 switch(afd->a_af) {
435#ifdef INET6
436 case AF_INET6:
437 {
438 int error;
439
440 if ((error = ip6_parsenumeric(sa, addr, host,
441 hostlen,
442 flags)) != 0)
443 return(error);
444 break;
445 }
446#endif
447 default:
448 if (inet_ntop(afd->a_af, addr, host,
449 hostlen) == NULL)
450 return EAI_SYSTEM;
451 break;
452 }
453 }
454 }
455 return(0);
456}
457
458#ifdef INET6
459static int
460ip6_parsenumeric(sa, addr, host, hostlen, flags)
461 const struct sockaddr *sa;
462 const char *addr;
463 char *host;
464 socklen_t hostlen;
465 int flags;
466{
467 size_t numaddrlen;
468 char numaddr[512];
469
470 assert(sa != NULL);
471 assert(addr != NULL);
472 assert(host != NULL);
473
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700474 if (hostlen < 0)
475 return EAI_OVERFLOW;
476
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800477 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
478 return EAI_SYSTEM;
479
480 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700481 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482 return EAI_OVERFLOW;
483 strlcpy(host, numaddr, hostlen);
484
485 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
486 char zonebuf[MAXHOSTNAMELEN];
487 int zonelen;
488
489 zonelen = ip6_sa2str(
490 (const struct sockaddr_in6 *)(const void *)sa,
491 zonebuf, sizeof(zonebuf), flags);
492 if (zonelen < 0)
493 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700494 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800495 return EAI_OVERFLOW;
496 /* construct <numeric-addr><delim><zoneid> */
497 memcpy(host + numaddrlen + 1, zonebuf,
498 (size_t)zonelen);
499 host[numaddrlen] = SCOPE_DELIMITER;
500 host[numaddrlen + 1 + zonelen] = '\0';
501 }
502
503 return 0;
504}
505
506/* ARGSUSED */
507static int
508ip6_sa2str(sa6, buf, bufsiz, flags)
509 const struct sockaddr_in6 *sa6;
510 char *buf;
511 size_t bufsiz;
512 int flags;
513{
514 unsigned int ifindex;
515 const struct in6_addr *a6;
516 int n;
517
518 assert(sa6 != NULL);
519 assert(buf != NULL);
520
521 ifindex = (unsigned int)sa6->sin6_scope_id;
522 a6 = &sa6->sin6_addr;
523
524#ifdef NI_NUMERICSCOPE
525 if ((flags & NI_NUMERICSCOPE) != 0) {
526 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
527 if (n < 0 || n >= bufsiz)
528 return -1;
529 else
530 return n;
531 }
532#endif
533
534 /* if_indextoname() does not take buffer size. not a good api... */
535 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
536 bufsiz >= IF_NAMESIZE) {
537 char *p = if_indextoname(ifindex, buf);
538 if (p) {
539 return(strlen(p));
540 }
541 }
542
543 /* last resort */
544 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
545 if (n < 0 || (size_t) n >= bufsiz)
546 return -1;
547 else
548 return n;
549}
550#endif /* INET6 */
551
552
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700553#if defined(ANDROID_CHANGES) && defined(AF_LINK)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800554/*
555 * getnameinfo_link():
556 * Format a link-layer address into a printable format, paying attention to
557 * the interface type.
558 */
559/* ARGSUSED */
560static int
561getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
562 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
563 int flags)
564{
565 const struct sockaddr_dl *sdl =
566 (const struct sockaddr_dl *)(const void *)sa;
567 const struct ieee1394_hwaddr *iha;
568 int n;
569
570 if (serv != NULL && servlen > 0)
571 *serv = '\0';
572
573 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
574 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
575 if (n < 0 || (socklen_t) n > hostlen) {
576 *host = '\0';
577 return EAI_MEMORY;
578 }
579 return 0;
580 }
581
582 switch (sdl->sdl_type) {
583#ifdef IFT_ECONET
584 case IFT_ECONET:
585 if (sdl->sdl_alen < 2)
586 return EAI_FAMILY;
587 if (CLLADDR(sdl)[1] == 0)
588 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
589 else
590 n = snprintf(host, hostlen, "%u.%u",
591 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
592 if (n < 0 || (socklen_t) n >= hostlen) {
593 *host = '\0';
594 return EAI_MEMORY;
595 } else
596 return 0;
597#endif
598 case IFT_IEEE1394:
599 if (sdl->sdl_alen < sizeof(iha->iha_uid))
600 return EAI_FAMILY;
601 iha =
602 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
603 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
604 host, hostlen);
605 /*
606 * The following have zero-length addresses.
607 * IFT_ATM (net/if_atmsubr.c)
608 * IFT_FAITH (net/if_faith.c)
609 * IFT_GIF (net/if_gif.c)
610 * IFT_LOOP (net/if_loop.c)
611 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
612 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
613 * IFT_STF (net/if_stf.c)
614 * IFT_L2VLAN (net/if_vlan.c)
615 * IFT_PROPVIRTUAL (net/if_bridge.h>
616 */
617 /*
618 * The following use IPv4 addresses as link-layer addresses:
619 * IFT_OTHER (net/if_gre.c)
620 */
621 case IFT_ARCNET: /* default below is believed correct for all these. */
622 case IFT_ETHER:
623 case IFT_FDDI:
624 case IFT_HIPPI:
625 case IFT_ISO88025:
626 default:
627 return hexname((const u_int8_t *)CLLADDR(sdl),
628 (size_t)sdl->sdl_alen, host, hostlen);
629 }
630}
Elliott Hughes5056f1f2012-06-11 15:01:10 -0700631#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800632
633static int
634hexname(cp, len, host, hostlen)
635 const u_int8_t *cp;
636 char *host;
637 size_t len;
638 socklen_t hostlen;
639{
640 int n;
641 size_t i;
642 char *outp = host;
643
644 *outp = '\0';
645 for (i = 0; i < len; i++) {
646 n = snprintf(outp, hostlen, "%s%02x",
647 i ? ":" : "", cp[i]);
648 if (n < 0 || (socklen_t) n >= hostlen) {
649 *host = '\0';
650 return EAI_MEMORY;
651 }
652 outp += n;
653 hostlen -= n;
654 }
655 return 0;
656}