vfs: check userland buffers before reading them.
[haiku.git] / src / kits / network / netresolv / irs / getnameinfo.c
blob2bff8f174ebb89b86bbdbe11f9ac82d82d3f23c2
1 /* $NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by WIDE Project and
20 * its contributors.
21 * 4. Neither the name of the project nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
39 * Issues to be discussed:
40 * - Thread safe-ness must be checked
41 * - RFC2553 says that we should raise error on short buffer. X/Open says
42 * we need to truncate the result. We obey RFC2553 (and X/Open should be
43 * modified). ipngwg rough consensus seems to follow RFC2553.
44 * - What is "local" in NI_FQDN?
45 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
46 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
47 * sin6_scope_id is filled - standardization status?
48 * XXX breaks backward compat for code that expects no scopeid.
49 * beware on merge.
52 #include <sys/cdefs.h>
53 #if defined(LIBC_SCCS) && !defined(lint)
54 __RCSID("$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $");
55 #endif /* LIBC_SCCS and not lint */
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <sys/un.h>
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
66 #include <assert.h>
67 #include <limits.h>
68 #include <netdb.h>
69 #include <resolv.h>
70 #include <stddef.h>
71 #include <string.h>
73 #include "nsswitch.h"
74 #include "servent.h"
76 static const struct afd {
77 int a_af;
78 socklen_t a_addrlen;
79 socklen_t a_socklen;
80 int a_off;
81 } afdl [] = {
82 #ifdef INET6
83 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
84 offsetof(struct sockaddr_in6, sin6_addr)},
85 #endif
86 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
87 offsetof(struct sockaddr_in, sin_addr)},
88 {0, 0, 0, 0},
91 struct sockinet {
92 u_char si_len;
93 u_char si_family;
94 u_short si_port;
97 static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
98 socklen_t, char *, socklen_t, int);
99 #ifdef INET6
100 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
101 socklen_t, int);
102 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
103 #endif
104 static int getnameinfo_atalk(const struct sockaddr *, socklen_t, char *,
105 socklen_t, char *, socklen_t, int);
106 static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
107 socklen_t, char *, socklen_t, int);
109 static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
110 socklen_t, char *, socklen_t, int);
111 static int hexname(const uint8_t *, size_t, char *, socklen_t);
114 + * Top-level getnameinfo() code. Look at the address family, and pick an
115 + * appropriate function to call.
116 + */
118 getnameinfo(const struct sockaddr *sa, socklen_t salen,
119 char *host, socklen_t hostlen,
120 char *serv, socklen_t servlen,
121 int flags)
124 switch (sa->sa_family) {
125 case AF_APPLETALK:
126 return getnameinfo_atalk(sa, salen, host, hostlen,
127 serv, servlen, flags);
128 case AF_INET:
129 case AF_INET6:
130 return getnameinfo_inet(sa, salen, host, hostlen,
131 serv, servlen, flags);
132 case AF_LINK:
133 return getnameinfo_link(sa, salen, host, hostlen,
134 serv, servlen, flags);
135 case AF_LOCAL:
136 return getnameinfo_local(sa, salen, host, hostlen,
137 serv, servlen, flags);
138 default:
139 return EAI_FAMILY;
144 * getnameinfo_atalk():
145 * Format an AppleTalk address into a printable format.
147 /* ARGSUSED */
148 static int
149 getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
150 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
151 int flags)
153 #if 0
154 char numserv[8];
155 int n, m=0;
157 const struct sockaddr_at *sat =
158 (const struct sockaddr_at *)(const void *)sa;
160 if (serv != NULL && servlen > 0) {
161 snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
162 if (strlen(numserv) + 1 > servlen)
163 return EAI_MEMORY;
164 strlcpy(serv, numserv, servlen);
167 n = snprintf(host, hostlen, "%u.%u",
168 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
170 if (n < 0 || (socklen_t)(m+n) >= hostlen)
171 goto errout;
173 m += n;
175 if (sat->sat_range.r_netrange.nr_phase) {
176 n = snprintf(host+m, hostlen-m, " phase %u",
177 sat->sat_range.r_netrange.nr_phase);
179 if (n < 0 || (socklen_t)(m+n) >= hostlen)
180 goto errout;
182 m += n;
184 if (sat->sat_range.r_netrange.nr_firstnet) {
185 n = snprintf(host+m, hostlen-m, " range %u - %u",
186 ntohs(sat->sat_range.r_netrange.nr_firstnet),
187 ntohs(sat->sat_range.r_netrange.nr_lastnet ));
189 if (n < 0 || (socklen_t)(m+n) >= hostlen)
190 goto errout;
192 m += n;
195 return 0;
197 errout:
198 if (host && hostlen>0)
199 host[m] = '\0'; /* XXX ??? */
201 #endif
202 return EAI_MEMORY;
206 * getnameinfo_local():
207 * Format an local address into a printable format.
209 /* ARGSUSED */
210 static int
211 getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
212 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
213 int flags)
215 const struct sockaddr_un *sun =
216 (const struct sockaddr_un *)(const void *)sa;
218 if (serv != NULL && servlen > 0)
219 serv[0] = '\0';
221 if (host && hostlen > 0)
222 strlcpy(host, sun->sun_path,
223 MIN(sizeof(sun->sun_path) + 1, hostlen));
225 return 0;
229 * getnameinfo_inet():
230 * Format an IPv4 or IPv6 sockaddr into a printable string.
232 static int
233 getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
234 char *host, socklen_t hostlen,
235 char *serv, socklen_t servlen,
236 int flags)
238 const struct afd *afd;
239 struct servent *sp;
240 struct hostent *hp;
241 u_short port;
242 int family, i;
243 const char *addr;
244 uint32_t v4a;
245 char numserv[512];
246 char numaddr[512];
248 /* sa is checked below */
249 /* host may be NULL */
250 /* serv may be NULL */
252 if (sa == NULL)
253 return EAI_FAIL;
256 family = sa->sa_family;
257 for (i = 0; afdl[i].a_af; i++)
258 if (afdl[i].a_af == family) {
259 afd = &afdl[i];
260 goto found;
262 return EAI_FAMILY;
264 found:
265 if (salen != afd->a_socklen)
266 return EAI_FAIL;
268 /* network byte order */
269 port = ((const struct sockinet *)(const void *)sa)->si_port;
270 addr = (const char *)(const void *)sa + afd->a_off;
272 if (serv == NULL || servlen == 0) {
274 * do nothing in this case.
275 * in case you are wondering if "&&" is more correct than
276 * "||" here: rfc2553bis-03 says that serv == NULL OR
277 * servlen == 0 means that the caller does not want the result.
279 } else {
280 struct servent_data svd;
281 struct servent sv;
283 if (flags & NI_NUMERICSERV)
284 sp = NULL;
285 else {
286 (void)memset(&svd, 0, sizeof(svd));
287 sp = getservbyport_r(port,
288 (flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
290 if (sp) {
291 if (strlen(sp->s_name) + 1 > servlen) {
292 endservent_r(&svd);
293 return EAI_MEMORY;
295 strlcpy(serv, sp->s_name, servlen);
296 endservent_r(&svd);
297 } else {
298 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
299 if (strlen(numserv) + 1 > servlen)
300 return EAI_MEMORY;
301 strlcpy(serv, numserv, servlen);
305 switch (sa->sa_family) {
306 case AF_INET:
307 v4a = (uint32_t)
308 ntohl(((const struct sockaddr_in *)
309 (const void *)sa)->sin_addr.s_addr);
310 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
311 flags |= NI_NUMERICHOST;
312 v4a >>= IN_CLASSA_NSHIFT;
313 if (v4a == 0)
314 flags |= NI_NUMERICHOST;
315 break;
316 #ifdef INET6
317 case AF_INET6:
319 const struct sockaddr_in6 *sin6;
320 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
321 switch (sin6->sin6_addr.s6_addr[0]) {
322 case 0x00:
323 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
325 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
327 else
328 flags |= NI_NUMERICHOST;
329 break;
330 default:
331 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
332 flags |= NI_NUMERICHOST;
334 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
335 flags |= NI_NUMERICHOST;
336 break;
339 break;
340 #endif
342 if (host == NULL || hostlen == 0) {
344 * do nothing in this case.
345 * in case you are wondering if "&&" is more correct than
346 * "||" here: rfc2553bis-03 says that host == NULL or
347 * hostlen == 0 means that the caller does not want the result.
349 } else if (flags & NI_NUMERICHOST) {
350 size_t numaddrlen;
352 /* NUMERICHOST and NAMEREQD conflicts with each other */
353 if (flags & NI_NAMEREQD)
354 return EAI_NONAME;
356 switch(afd->a_af) {
357 #ifdef INET6
358 case AF_INET6:
360 int error;
362 if ((error = ip6_parsenumeric(sa, addr, host,
363 hostlen, flags)) != 0)
364 return(error);
365 break;
367 #endif
368 default:
369 if (inet_ntop(afd->a_af, addr, numaddr,
370 (socklen_t)sizeof(numaddr)) == NULL)
371 return EAI_SYSTEM;
372 numaddrlen = strlen(numaddr);
373 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
374 return EAI_MEMORY;
375 strlcpy(host, numaddr, hostlen);
376 break;
378 } else {
379 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
381 if (hp) {
382 #if 0
384 * commented out, since "for local host" is not
385 * implemented here - see RFC2553 p30
387 if (flags & NI_NOFQDN) {
388 char *p;
389 p = strchr(hp->h_name, '.');
390 if (p)
391 *p = '\0';
393 #endif
394 if (strlen(hp->h_name) + 1 > hostlen) {
395 return EAI_MEMORY;
397 strlcpy(host, hp->h_name, hostlen);
398 } else {
399 if (flags & NI_NAMEREQD)
400 return EAI_NONAME;
401 switch(afd->a_af) {
402 #ifdef INET6
403 case AF_INET6:
405 int error;
407 if ((error = ip6_parsenumeric(sa, addr, host,
408 hostlen,
409 flags)) != 0)
410 return(error);
411 break;
413 #endif
414 default:
415 if (inet_ntop(afd->a_af, addr, host,
416 hostlen) == NULL)
417 return EAI_SYSTEM;
418 break;
422 return(0);
425 #ifdef INET6
426 static int
427 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
428 socklen_t hostlen, int flags)
430 size_t numaddrlen;
431 char numaddr[512];
433 assert(sa != NULL);
434 assert(addr != NULL);
435 assert(host != NULL);
437 if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr))
438 == NULL)
439 return EAI_SYSTEM;
441 numaddrlen = strlen(numaddr);
442 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
443 return EAI_OVERFLOW;
444 strlcpy(host, numaddr, hostlen);
446 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
447 char zonebuf[MAXHOSTNAMELEN];
448 int zonelen;
450 zonelen = ip6_sa2str(
451 (const struct sockaddr_in6 *)(const void *)sa,
452 zonebuf, sizeof(zonebuf), flags);
453 if (zonelen < 0)
454 return EAI_OVERFLOW;
455 if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
456 return EAI_OVERFLOW;
457 /* construct <numeric-addr><delim><zoneid> */
458 memcpy(host + numaddrlen + 1, zonebuf,
459 (size_t)zonelen);
460 host[numaddrlen] = SCOPE_DELIMITER;
461 host[numaddrlen + 1 + zonelen] = '\0';
464 return 0;
467 /* ARGSUSED */
468 static int
469 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
471 unsigned int ifindex;
472 const struct in6_addr *a6;
473 int n;
475 assert(sa6 != NULL);
476 assert(buf != NULL);
478 ifindex = (unsigned int)sa6->sin6_scope_id;
479 a6 = &sa6->sin6_addr;
481 #ifdef NI_NUMERICSCOPE
482 if ((flags & NI_NUMERICSCOPE) != 0) {
483 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
484 if (n < 0 || (size_t)n >= bufsiz)
485 return -1;
486 else
487 return n;
489 #endif
491 /* if_indextoname() does not take buffer size. not a good api... */
492 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
493 bufsiz >= IF_NAMESIZE) {
494 char *p = if_indextoname(ifindex, buf);
495 if (p) {
496 return (int)strlen(p);
500 /* last resort */
501 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
502 if (n < 0 || (size_t) n >= bufsiz)
503 return -1;
504 else
505 return n;
507 #endif /* INET6 */
511 * getnameinfo_link():
512 * Format a link-layer address into a printable format, paying attention to
513 * the interface type.
515 /* ARGSUSED */
516 static int
517 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
518 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
519 int flags)
521 const struct sockaddr_dl *sdl =
522 (const struct sockaddr_dl *)(const void *)sa;
523 int n;
525 if (serv != NULL && servlen > 0)
526 *serv = '\0';
528 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
529 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
530 if (n < 0 || (socklen_t) n > hostlen) {
531 *host = '\0';
532 return EAI_MEMORY;
534 return 0;
537 switch (sdl->sdl_type) {
538 #ifdef IFT_ECONET
539 case IFT_ECONET:
540 if (sdl->sdl_alen < 2)
541 return EAI_FAMILY;
542 if (LLADDR(sdl)[1] == 0)
543 n = snprintf(host, hostlen, "%u", LLADDR(sdl)[0]);
544 else
545 n = snprintf(host, hostlen, "%u.%u",
546 LLADDR(sdl)[1], LLADDR(sdl)[0]);
547 if (n < 0 || (socklen_t) n >= hostlen) {
548 *host = '\0';
549 return EAI_MEMORY;
550 } else
551 return 0;
552 #endif
553 #ifdef IFT_IEEE1394
554 case IFT_IEEE1394:
556 const struct ieee1394_hwaddr *iha;
557 if (sdl->sdl_alen < sizeof(iha->iha_uid))
558 return EAI_FAMILY;
559 iha =
560 (const struct ieee1394_hwaddr *)(const void *)LLADDR(sdl);
561 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
562 host, hostlen);
564 #endif
566 * The following have zero-length addresses.
567 * IFT_ATM (net/if_atmsubr.c)
568 * IFT_FAITH (net/if_faith.c)
569 * IFT_GIF (net/if_gif.c)
570 * IFT_LOOP (net/if_loop.c)
571 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
572 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
573 * IFT_STF (net/if_stf.c)
574 * IFT_L2VLAN (net/if_vlan.c)
575 * IFT_PROPVIRTUAL (net/if_bridge.h>
578 * The following use IPv4 addresses as link-layer addresses:
579 * IFT_OTHER (net/if_gre.c)
581 #ifdef IFT_ARCNET
582 case IFT_ARCNET: /* default below is believed correct for all these. */
583 #endif
584 case IFT_ETHER:
585 #ifdef IFT_FDDI
586 case IFT_FDDI:
587 #endif
588 #ifdef IFT_HIPPI
589 case IFT_HIPPI:
590 #endif
591 #ifdef IFT_ISO88025
592 case IFT_ISO88025:
593 #endif
594 default:
595 return hexname((const uint8_t *)LLADDR(sdl),
596 (size_t)sdl->sdl_alen, host, hostlen);
600 static int
601 hexname(const uint8_t *cp, size_t len, char *host, socklen_t hostlen)
603 int n;
604 size_t i;
605 char *outp = host;
607 *outp = '\0';
608 for (i = 0; i < len; i++) {
609 n = snprintf(outp, hostlen, "%s%02x",
610 i ? ":" : "", cp[i]);
611 if (n < 0 || (socklen_t) n >= hostlen) {
612 *host = '\0';
613 return EAI_MEMORY;
615 outp += n;
616 hostlen -= n;
618 return 0;