tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / net / getnameinfo.c
blob1216cea31325d6be8ef82111ed330e7f3786ef95
1 /* $NetBSD: getnameinfo.c,v 1.54 2013/08/16 15:27:12 christos Exp $ */
2 /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */
4 /*
5 * Copyright (c) 2000 Ben Harris.
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
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.
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.
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.
48 #include <sys/cdefs.h>
49 #if defined(LIBC_SCCS) && !defined(lint)
50 __RCSID("$NetBSD: getnameinfo.c,v 1.54 2013/08/16 15:27:12 christos Exp $");
51 #endif /* LIBC_SCCS and not lint */
53 #include "namespace.h"
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/un.h>
57 #include <net/if.h>
58 #if !defined(__minix)
59 #include <net/if_dl.h>
60 #include <net/if_ieee1394.h>
61 #include <net/if_types.h>
62 #include <netatalk/at.h>
63 #endif /* !defined(__minix) */
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
67 #include <assert.h>
68 #include <limits.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <stddef.h>
72 #include <string.h>
74 #include "servent.h"
75 #include "hostent.h"
77 #ifdef __weak_alias
78 __weak_alias(getnameinfo,_getnameinfo)
79 #endif
81 static const struct afd {
82 int a_af;
83 socklen_t a_addrlen;
84 socklen_t a_socklen;
85 int a_off;
86 } afdl [] = {
87 #ifdef INET6
88 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
89 offsetof(struct sockaddr_in6, sin6_addr)},
90 #endif
91 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
92 offsetof(struct sockaddr_in, sin_addr)},
93 {0, 0, 0, 0},
96 struct sockinet {
97 u_char si_len;
98 u_char si_family;
99 u_short si_port;
102 static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
103 socklen_t, char *, socklen_t, int);
104 #ifdef INET6
105 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
106 socklen_t, int);
107 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
108 #endif
109 #if !defined(__minix)
110 static int getnameinfo_atalk(const struct sockaddr *, socklen_t, char *,
111 socklen_t, char *, socklen_t, int);
112 static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
113 socklen_t, char *, socklen_t, int);
115 static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
116 socklen_t, char *, socklen_t, int);
117 static int hexname(const uint8_t *, size_t, char *, socklen_t);
118 #endif /* !defined(__minix) */
121 * Top-level getnameinfo() code. Look at the address family, and pick an
122 * appropriate function to call.
125 getnameinfo(const struct sockaddr *sa, socklen_t salen,
126 char *host, socklen_t hostlen,
127 char *serv, socklen_t servlen,
128 int flags)
131 switch (sa->sa_family) {
132 #if !defined(__minix)
133 case AF_APPLETALK:
134 return getnameinfo_atalk(sa, salen, host, hostlen,
135 serv, servlen, flags);
136 #endif /* !defined(__minix) */
137 case AF_INET:
138 case AF_INET6:
139 return getnameinfo_inet(sa, salen, host, hostlen,
140 serv, servlen, flags);
141 #if !defined(__minix)
142 case AF_LINK:
143 return getnameinfo_link(sa, salen, host, hostlen,
144 serv, servlen, flags);
145 case AF_LOCAL:
146 return getnameinfo_local(sa, salen, host, hostlen,
147 serv, servlen, flags);
148 #endif /* !defined(__minix) */
149 default:
150 return EAI_FAMILY;
154 #if !defined(__minix)
156 * getnameinfo_atalk():
157 * Format an AppleTalk address into a printable format.
159 /* ARGSUSED */
160 static int
161 getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
162 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
163 int flags)
165 char numserv[8];
166 int n, m=0;
168 const struct sockaddr_at *sat =
169 (const struct sockaddr_at *)(const void *)sa;
171 if (serv != NULL && servlen > 0) {
172 snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
173 if (strlen(numserv) + 1 > servlen)
174 return EAI_MEMORY;
175 strlcpy(serv, numserv, servlen);
178 n = snprintf(host, hostlen, "%u.%u",
179 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
181 if (n < 0 || (socklen_t)(m+n) >= hostlen)
182 goto errout;
184 m += n;
186 if (sat->sat_range.r_netrange.nr_phase) {
187 n = snprintf(host+m, hostlen-m, " phase %u",
188 sat->sat_range.r_netrange.nr_phase);
190 if (n < 0 || (socklen_t)(m+n) >= hostlen)
191 goto errout;
193 m += n;
195 if (sat->sat_range.r_netrange.nr_firstnet) {
196 n = snprintf(host+m, hostlen-m, " range %u - %u",
197 ntohs(sat->sat_range.r_netrange.nr_firstnet),
198 ntohs(sat->sat_range.r_netrange.nr_lastnet ));
200 if (n < 0 || (socklen_t)(m+n) >= hostlen)
201 goto errout;
203 m += n;
206 return 0;
208 errout:
209 if (host && hostlen>0)
210 host[m] = '\0'; /* XXX ??? */
212 return EAI_MEMORY;
216 * getnameinfo_local():
217 * Format an local address into a printable format.
219 /* ARGSUSED */
220 static int
221 getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
222 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
223 int flags)
225 const struct sockaddr_un *sun =
226 (const struct sockaddr_un *)(const void *)sa;
228 if (serv != NULL && servlen > 0)
229 serv[0] = '\0';
231 if (host && hostlen > 0)
232 strlcpy(host, sun->sun_path,
233 MIN(sizeof(sun->sun_path) + 1, hostlen));
235 return 0;
237 #endif /* !defined(__minix) */
240 * getnameinfo_inet():
241 * Format an IPv4 or IPv6 sockaddr into a printable string.
243 static int
244 getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
245 char *host, socklen_t hostlen,
246 char *serv, socklen_t servlen,
247 int flags)
249 const struct afd *afd;
250 struct servent *sp;
251 struct hostent *hp;
252 u_short port;
253 int family, i;
254 const char *addr;
255 uint32_t v4a;
256 char numserv[512];
257 char numaddr[512];
259 /* sa is checked below */
260 /* host may be NULL */
261 /* serv may be NULL */
263 if (sa == NULL)
264 return EAI_FAIL;
266 family = sa->sa_family;
267 for (i = 0; afdl[i].a_af; i++)
268 if (afdl[i].a_af == family) {
269 afd = &afdl[i];
270 goto found;
272 return EAI_FAMILY;
274 found:
275 if (salen != afd->a_socklen)
276 return EAI_FAIL;
278 /* network byte order */
279 port = ((const struct sockinet *)(const void *)sa)->si_port;
280 addr = (const char *)(const void *)sa + afd->a_off;
282 if (serv == NULL || servlen == 0) {
284 * do nothing in this case.
285 * in case you are wondering if "&&" is more correct than
286 * "||" here: rfc2553bis-03 says that serv == NULL OR
287 * servlen == 0 means that the caller does not want the result.
289 } else {
290 struct servent_data svd;
291 struct servent sv;
293 if (flags & NI_NUMERICSERV)
294 sp = NULL;
295 else {
296 (void)memset(&svd, 0, sizeof(svd));
297 sp = getservbyport_r(port,
298 (flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
300 if (sp) {
301 if (strlen(sp->s_name) + 1 > servlen) {
302 endservent_r(&svd);
303 return EAI_MEMORY;
305 strlcpy(serv, sp->s_name, servlen);
306 endservent_r(&svd);
307 } else {
308 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
309 if (strlen(numserv) + 1 > servlen)
310 return EAI_MEMORY;
311 strlcpy(serv, numserv, servlen);
315 switch (sa->sa_family) {
316 case AF_INET:
317 v4a = (uint32_t)
318 ntohl(((const struct sockaddr_in *)
319 (const void *)sa)->sin_addr.s_addr);
320 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
321 flags |= NI_NUMERICHOST;
322 v4a >>= IN_CLASSA_NSHIFT;
323 if (v4a == 0)
324 flags |= NI_NUMERICHOST;
325 break;
326 #ifdef INET6
327 case AF_INET6:
329 const struct sockaddr_in6 *sin6;
330 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
331 switch (sin6->sin6_addr.s6_addr[0]) {
332 case 0x00:
333 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
335 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
337 else
338 flags |= NI_NUMERICHOST;
339 break;
340 default:
341 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
342 flags |= NI_NUMERICHOST;
344 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
345 flags |= NI_NUMERICHOST;
346 break;
349 break;
350 #endif
352 if (host == NULL || hostlen == 0) {
354 * do nothing in this case.
355 * in case you are wondering if "&&" is more correct than
356 * "||" here: rfc2553bis-03 says that host == NULL or
357 * hostlen == 0 means that the caller does not want the result.
359 } else if (flags & NI_NUMERICHOST) {
360 size_t numaddrlen;
362 /* NUMERICHOST and NAMEREQD conflicts with each other */
363 if (flags & NI_NAMEREQD)
364 return EAI_NONAME;
366 switch(afd->a_af) {
367 #ifdef INET6
368 case AF_INET6:
370 int error;
372 if ((error = ip6_parsenumeric(sa, addr, host,
373 hostlen, flags)) != 0)
374 return(error);
375 break;
377 #endif
378 default:
379 if (inet_ntop(afd->a_af, addr, numaddr,
380 (socklen_t)sizeof(numaddr)) == NULL)
381 return EAI_SYSTEM;
382 numaddrlen = strlen(numaddr);
383 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
384 return EAI_MEMORY;
385 strlcpy(host, numaddr, hostlen);
386 break;
388 } else {
389 struct hostent hent;
390 char hbuf[4096];
391 int he;
392 hp = gethostbyaddr_r(addr, afd->a_addrlen, afd->a_af, &hent,
393 hbuf, sizeof(hbuf), &he);
395 if (hp) {
396 #if 0
398 * commented out, since "for local host" is not
399 * implemented here - see RFC2553 p30
401 if (flags & NI_NOFQDN) {
402 char *p;
403 p = strchr(hp->h_name, '.');
404 if (p)
405 *p = '\0';
407 #endif
408 if (strlen(hp->h_name) + 1 > hostlen) {
409 return EAI_MEMORY;
411 strlcpy(host, hp->h_name, hostlen);
412 } else {
413 if (flags & NI_NAMEREQD)
414 return EAI_NONAME;
415 switch(afd->a_af) {
416 #ifdef INET6
417 case AF_INET6:
419 int error;
421 if ((error = ip6_parsenumeric(sa, addr, host,
422 hostlen,
423 flags)) != 0)
424 return(error);
425 break;
427 #endif
428 default:
429 if (inet_ntop(afd->a_af, addr, host,
430 hostlen) == NULL)
431 return EAI_SYSTEM;
432 break;
436 return(0);
439 #ifdef INET6
440 static int
441 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
442 socklen_t hostlen, int flags)
444 size_t numaddrlen;
445 char numaddr[512];
447 _DIAGASSERT(sa != NULL);
448 _DIAGASSERT(addr != NULL);
449 _DIAGASSERT(host != NULL);
451 if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr))
452 == NULL)
453 return EAI_SYSTEM;
455 numaddrlen = strlen(numaddr);
456 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
457 return EAI_OVERFLOW;
458 strlcpy(host, numaddr, hostlen);
460 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
461 char zonebuf[MAXHOSTNAMELEN];
462 int zonelen;
464 zonelen = ip6_sa2str(
465 (const struct sockaddr_in6 *)(const void *)sa,
466 zonebuf, sizeof(zonebuf), flags);
467 if (zonelen < 0)
468 return EAI_OVERFLOW;
469 if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
470 return EAI_OVERFLOW;
471 /* construct <numeric-addr><delim><zoneid> */
472 memcpy(host + numaddrlen + 1, zonebuf,
473 (size_t)zonelen);
474 host[numaddrlen] = SCOPE_DELIMITER;
475 host[numaddrlen + 1 + zonelen] = '\0';
478 return 0;
481 /* ARGSUSED */
482 static int
483 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
485 unsigned int ifindex;
486 const struct in6_addr *a6;
487 int n;
489 _DIAGASSERT(sa6 != NULL);
490 _DIAGASSERT(buf != NULL);
492 ifindex = (unsigned int)sa6->sin6_scope_id;
493 a6 = &sa6->sin6_addr;
495 #ifdef NI_NUMERICSCOPE
496 if ((flags & NI_NUMERICSCOPE) != 0) {
497 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
498 if (n < 0 || (size_t)n >= bufsiz)
499 return -1;
500 else
501 return n;
503 #endif
505 /* if_indextoname() does not take buffer size. not a good api... */
506 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
507 bufsiz >= IF_NAMESIZE) {
508 char *p = if_indextoname(ifindex, buf);
509 if (p) {
510 return (int)strlen(p);
514 /* last resort */
515 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
516 if (n < 0 || (size_t) n >= bufsiz)
517 return -1;
518 else
519 return n;
521 #endif /* INET6 */
524 #if !defined(__minix)
526 * getnameinfo_link():
527 * Format a link-layer address into a printable format, paying attention to
528 * the interface type.
530 /* ARGSUSED */
531 static int
532 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
533 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
534 int flags)
536 const struct sockaddr_dl *sdl =
537 (const struct sockaddr_dl *)(const void *)sa;
538 const struct ieee1394_hwaddr *iha;
539 int n;
541 if (serv != NULL && servlen > 0)
542 *serv = '\0';
544 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
545 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
546 if (n < 0 || (socklen_t) n > hostlen) {
547 *host = '\0';
548 return EAI_MEMORY;
550 return 0;
553 switch (sdl->sdl_type) {
554 #ifdef IFT_ECONET
555 case IFT_ECONET:
556 if (sdl->sdl_alen < 2)
557 return EAI_FAMILY;
558 if (CLLADDR(sdl)[1] == 0)
559 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
560 else
561 n = snprintf(host, hostlen, "%u.%u",
562 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
563 if (n < 0 || (socklen_t) n >= hostlen) {
564 *host = '\0';
565 return EAI_MEMORY;
566 } else
567 return 0;
568 #endif
569 case IFT_IEEE1394:
570 if (sdl->sdl_alen < sizeof(iha->iha_uid))
571 return EAI_FAMILY;
572 iha =
573 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
574 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
575 host, hostlen);
577 * The following have zero-length addresses.
578 * IFT_ATM (net/if_atmsubr.c)
579 * IFT_FAITH (net/if_faith.c)
580 * IFT_GIF (net/if_gif.c)
581 * IFT_LOOP (net/if_loop.c)
582 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
583 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
584 * IFT_STF (net/if_stf.c)
585 * IFT_L2VLAN (net/if_vlan.c)
586 * IFT_PROPVIRTUAL (net/if_bridge.h>
589 * The following use IPv4 addresses as link-layer addresses:
590 * IFT_OTHER (net/if_gre.c)
592 case IFT_ARCNET: /* default below is believed correct for all these. */
593 case IFT_ETHER:
594 case IFT_FDDI:
595 case IFT_HIPPI:
596 case IFT_ISO88025:
597 default:
598 return hexname((const uint8_t *)CLLADDR(sdl),
599 (size_t)sdl->sdl_alen, host, hostlen);
603 static int
604 hexname(const uint8_t *cp, size_t len, char *host, socklen_t hostlen)
606 int n;
607 size_t i;
608 char *outp = host;
610 *outp = '\0';
611 for (i = 0; i < len; i++) {
612 n = snprintf(outp, hostlen, "%s%02x",
613 i ? ":" : "", cp[i]);
614 if (n < 0 || (socklen_t) n >= hostlen) {
615 *host = '\0';
616 return EAI_MEMORY;
618 outp += n;
619 hostlen -= n;
621 return 0;
623 #endif /* !defined(__minix) */