getnameinfo.c revision 1.57 1 /* $NetBSD: getnameinfo.c,v 1.57 2015/09/03 15:01:19 christos 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.57 2015/09/03 15:01:19 christos Exp $");
51 #endif /* LIBC_SCCS and not lint */
52
53 #ifndef RUMP_ACTION
54 #include "namespace.h"
55 #endif
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/un.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_ieee1394.h>
62 #include <net/if_types.h>
63 #include <netatalk/at.h>
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>
73
74 #include "servent.h"
75 #include "hostent.h"
76
77 #ifndef RUMP_ACTION
78 #ifdef __weak_alias
79 __weak_alias(getnameinfo,_getnameinfo)
80 #endif
81 #endif
82
83 static const struct afd {
84 int a_af;
85 socklen_t a_addrlen;
86 socklen_t a_socklen;
87 int a_off;
88 } afdl [] = {
89 #ifdef INET6
90 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
91 offsetof(struct sockaddr_in6, sin6_addr)},
92 #endif
93 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
94 offsetof(struct sockaddr_in, sin_addr)},
95 {0, 0, 0, 0},
96 };
97
98 struct sockinet {
99 u_char si_len;
100 u_char si_family;
101 u_short si_port;
102 };
103
104 static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
105 socklen_t, char *, socklen_t, int);
106 #ifdef INET6
107 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
108 socklen_t, int);
109 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
110 #endif
111 static int getnameinfo_atalk(const struct sockaddr *, socklen_t, char *,
112 socklen_t, char *, socklen_t, int);
113 static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
114 socklen_t, char *, socklen_t, int);
115
116 static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
117 socklen_t, char *, socklen_t, int);
118 static int hexname(const uint8_t *, size_t, char *, socklen_t);
119
120 /*
121 * Top-level getnameinfo() code. Look at the address family, and pick an
122 * appropriate function to call.
123 */
124 int
125 getnameinfo(const struct sockaddr *sa, socklen_t salen,
126 char *host, socklen_t hostlen,
127 char *serv, socklen_t servlen,
128 int flags)
129 {
130
131 switch (sa->sa_family) {
132 case AF_APPLETALK:
133 return getnameinfo_atalk(sa, salen, host, hostlen,
134 serv, servlen, flags);
135 case AF_INET:
136 case AF_INET6:
137 return getnameinfo_inet(sa, salen, host, hostlen,
138 serv, servlen, flags);
139 case AF_LINK:
140 return getnameinfo_link(sa, salen, host, hostlen,
141 serv, servlen, flags);
142 case AF_LOCAL:
143 return getnameinfo_local(sa, salen, host, hostlen,
144 serv, servlen, flags);
145 default:
146 return EAI_FAMILY;
147 }
148 }
149
150 /*
151 * getnameinfo_atalk():
152 * Format an AppleTalk address into a printable format.
153 */
154 /* ARGSUSED */
155 static int
156 getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
157 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
158 int flags)
159 {
160 char numserv[8];
161 int n, m=0;
162
163 const struct sockaddr_at *sat =
164 (const struct sockaddr_at *)(const void *)sa;
165
166 if (serv != NULL && servlen > 0) {
167 snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
168 if (strlen(numserv) + 1 > servlen)
169 return EAI_MEMORY;
170 strlcpy(serv, numserv, servlen);
171 }
172
173 n = snprintf(host, hostlen, "%u.%u",
174 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
175
176 if (n < 0 || (socklen_t)(m+n) >= hostlen)
177 goto errout;
178
179 m += n;
180
181 if (sat->sat_range.r_netrange.nr_phase) {
182 n = snprintf(host+m, hostlen-m, " phase %u",
183 sat->sat_range.r_netrange.nr_phase);
184
185 if (n < 0 || (socklen_t)(m+n) >= hostlen)
186 goto errout;
187
188 m += n;
189 }
190 if (sat->sat_range.r_netrange.nr_firstnet) {
191 n = snprintf(host+m, hostlen-m, " range %u - %u",
192 ntohs(sat->sat_range.r_netrange.nr_firstnet),
193 ntohs(sat->sat_range.r_netrange.nr_lastnet ));
194
195 if (n < 0 || (socklen_t)(m+n) >= hostlen)
196 goto errout;
197
198 m += n;
199 }
200
201 return 0;
202
203 errout:
204 if (host && hostlen>0)
205 host[m] = '\0'; /* XXX ??? */
206
207 return EAI_MEMORY;
208 }
209
210 /*
211 * getnameinfo_local():
212 * Format an local address into a printable format.
213 */
214 /* ARGSUSED */
215 static int
216 getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
217 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
218 int flags)
219 {
220 const struct sockaddr_un *sun =
221 (const struct sockaddr_un *)(const void *)sa;
222
223 if (serv != NULL && servlen > 0)
224 serv[0] = '\0';
225
226 if (host && hostlen > 0)
227 strlcpy(host, sun->sun_path,
228 MIN(sizeof(sun->sun_path) + 1, hostlen));
229
230 return 0;
231 }
232
233 /*
234 * getnameinfo_inet():
235 * Format an IPv4 or IPv6 sockaddr into a printable string.
236 */
237 static int
238 getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
239 char *host, socklen_t hostlen,
240 char *serv, socklen_t servlen,
241 int flags)
242 {
243 const struct afd *afd;
244 struct servent *sp;
245 struct hostent *hp;
246 u_short port;
247 int family, i;
248 const char *addr;
249 uint32_t v4a;
250 char numserv[512];
251 char numaddr[512];
252
253 /* sa is checked below */
254 /* host may be NULL */
255 /* serv may be NULL */
256
257 if (sa == NULL)
258 return EAI_FAIL;
259
260 family = sa->sa_family;
261 for (i = 0; afdl[i].a_af; i++)
262 if (afdl[i].a_af == family) {
263 afd = &afdl[i];
264 goto found;
265 }
266 return EAI_FAMILY;
267
268 found:
269 if (salen != afd->a_socklen)
270 return EAI_FAIL;
271
272 /* network byte order */
273 port = ((const struct sockinet *)(const void *)sa)->si_port;
274 addr = (const char *)(const void *)sa + afd->a_off;
275
276 if (serv == NULL || servlen == 0) {
277 /*
278 * do nothing in this case.
279 * in case you are wondering if "&&" is more correct than
280 * "||" here: rfc2553bis-03 says that serv == NULL OR
281 * servlen == 0 means that the caller does not want the result.
282 */
283 } else {
284 struct servent_data svd;
285 struct servent sv;
286
287 if (flags & NI_NUMERICSERV)
288 sp = NULL;
289 else {
290 (void)memset(&svd, 0, sizeof(svd));
291 sp = getservbyport_r(port,
292 (flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
293 }
294 if (sp) {
295 if (strlen(sp->s_name) + 1 > servlen) {
296 endservent_r(&svd);
297 return EAI_MEMORY;
298 }
299 strlcpy(serv, sp->s_name, servlen);
300 endservent_r(&svd);
301 } else {
302 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
303 if (strlen(numserv) + 1 > servlen)
304 return EAI_MEMORY;
305 strlcpy(serv, numserv, servlen);
306 }
307 }
308
309 switch (sa->sa_family) {
310 case AF_INET:
311 v4a = (uint32_t)
312 ntohl(((const struct sockaddr_in *)
313 (const void *)sa)->sin_addr.s_addr);
314 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
315 flags |= NI_NUMERICHOST;
316 v4a >>= IN_CLASSA_NSHIFT;
317 if (v4a == 0)
318 flags |= NI_NUMERICHOST;
319 break;
320 #ifdef INET6
321 case AF_INET6:
322 {
323 const struct sockaddr_in6 *sin6;
324 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
325 switch (sin6->sin6_addr.s6_addr[0]) {
326 case 0x00:
327 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
328 ;
329 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
330 ;
331 else
332 flags |= NI_NUMERICHOST;
333 break;
334 default:
335 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
336 flags |= NI_NUMERICHOST;
337 }
338 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
339 flags |= NI_NUMERICHOST;
340 break;
341 }
342 }
343 break;
344 #endif
345 }
346 if (host == NULL || hostlen == 0) {
347 /*
348 * do nothing in this case.
349 * in case you are wondering if "&&" is more correct than
350 * "||" here: rfc2553bis-03 says that host == NULL or
351 * hostlen == 0 means that the caller does not want the result.
352 */
353 } else if (flags & NI_NUMERICHOST) {
354 size_t numaddrlen;
355
356 /* NUMERICHOST and NAMEREQD conflicts with each other */
357 if (flags & NI_NAMEREQD)
358 return EAI_NONAME;
359
360 switch(afd->a_af) {
361 #ifdef INET6
362 case AF_INET6:
363 {
364 int error;
365
366 if ((error = ip6_parsenumeric(sa, addr, host,
367 hostlen, flags)) != 0)
368 return(error);
369 break;
370 }
371 #endif
372 default:
373 if (inet_ntop(afd->a_af, addr, numaddr,
374 (socklen_t)sizeof(numaddr)) == NULL)
375 return EAI_SYSTEM;
376 numaddrlen = strlen(numaddr);
377 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
378 return EAI_MEMORY;
379 strlcpy(host, numaddr, hostlen);
380 break;
381 }
382 } else {
383 struct hostent hent;
384 char hbuf[4096];
385 int he;
386 hp = gethostbyaddr_r(addr, afd->a_addrlen, afd->a_af, &hent,
387 hbuf, sizeof(hbuf), &he);
388
389 if (hp) {
390 #if 0
391 /*
392 * commented out, since "for local host" is not
393 * implemented here - see RFC2553 p30
394 */
395 if (flags & NI_NOFQDN) {
396 char *p;
397 p = strchr(hp->h_name, '.');
398 if (p)
399 *p = '\0';
400 }
401 #endif
402 if (strlen(hp->h_name) + 1 > hostlen) {
403 return EAI_MEMORY;
404 }
405 strlcpy(host, hp->h_name, hostlen);
406 } else {
407 if (flags & NI_NAMEREQD)
408 return EAI_NONAME;
409 switch(afd->a_af) {
410 #ifdef INET6
411 case AF_INET6:
412 {
413 int error;
414
415 if ((error = ip6_parsenumeric(sa, addr, host,
416 hostlen,
417 flags)) != 0)
418 return(error);
419 break;
420 }
421 #endif
422 default:
423 if (inet_ntop(afd->a_af, addr, host,
424 hostlen) == NULL)
425 return EAI_SYSTEM;
426 break;
427 }
428 }
429 }
430 return(0);
431 }
432
433 #ifdef INET6
434 static int
435 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
436 socklen_t hostlen, int flags)
437 {
438 size_t numaddrlen;
439 char numaddr[512];
440
441 _DIAGASSERT(sa != NULL);
442 _DIAGASSERT(addr != NULL);
443 _DIAGASSERT(host != NULL);
444
445 if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr))
446 == NULL)
447 return EAI_SYSTEM;
448
449 numaddrlen = strlen(numaddr);
450 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
451 return EAI_OVERFLOW;
452 strlcpy(host, numaddr, hostlen);
453
454 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
455 char zonebuf[MAXHOSTNAMELEN];
456 int zonelen;
457
458 zonelen = ip6_sa2str(
459 (const struct sockaddr_in6 *)(const void *)sa,
460 zonebuf, sizeof(zonebuf), flags);
461 if (zonelen < 0)
462 return EAI_OVERFLOW;
463 if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
464 return EAI_OVERFLOW;
465 /* construct <numeric-addr><delim><zoneid> */
466 memcpy(host + numaddrlen + 1, zonebuf,
467 (size_t)zonelen);
468 host[numaddrlen] = SCOPE_DELIMITER;
469 host[numaddrlen + 1 + zonelen] = '\0';
470 }
471
472 return 0;
473 }
474
475 /* ARGSUSED */
476 static int
477 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
478 {
479 unsigned int ifindex;
480 const struct in6_addr *a6;
481 int n;
482
483 _DIAGASSERT(sa6 != NULL);
484 _DIAGASSERT(buf != NULL);
485
486 ifindex = (unsigned int)sa6->sin6_scope_id;
487 a6 = &sa6->sin6_addr;
488
489 #ifdef NI_NUMERICSCOPE
490 if ((flags & NI_NUMERICSCOPE) != 0) {
491 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
492 if (n < 0 || (size_t)n >= bufsiz)
493 return -1;
494 else
495 return n;
496 }
497 #endif
498
499 /* if_indextoname() does not take buffer size. not a good api... */
500 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
501 bufsiz >= IF_NAMESIZE) {
502 char *p = if_indextoname(ifindex, buf);
503 if (p) {
504 return (int)strlen(p);
505 }
506 }
507
508 /* last resort */
509 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
510 if (n < 0 || (size_t) n >= bufsiz)
511 return -1;
512 else
513 return n;
514 }
515 #endif /* INET6 */
516
517
518 /*
519 * getnameinfo_link():
520 * Format a link-layer address into a printable format, paying attention to
521 * the interface type.
522 */
523 /* ARGSUSED */
524 static int
525 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
526 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
527 int flags)
528 {
529 const struct sockaddr_dl *sdl =
530 (const struct sockaddr_dl *)(const void *)sa;
531 const struct ieee1394_hwaddr *iha;
532 int n;
533
534 if (serv != NULL && servlen > 0)
535 *serv = '\0';
536
537 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
538 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
539 goto out;
540 }
541
542 switch (sdl->sdl_type) {
543 #ifdef IFT_ECONET
544 case IFT_ECONET:
545 if (sdl->sdl_alen < 2)
546 return EAI_FAMILY;
547 if (CLLADDR(sdl)[1] == 0)
548 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
549 else
550 n = snprintf(host, hostlen, "%u.%u",
551 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
552 goto out;
553 #endif
554 case IFT_IEEE1394:
555 if (sdl->sdl_alen < sizeof(iha->iha_uid))
556 return EAI_FAMILY;
557 iha =
558 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
559 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
560 host, hostlen);
561 /*
562 * The following have zero-length addresses.
563 * IFT_ATM (net/if_atmsubr.c)
564 * IFT_FAITH (net/if_faith.c)
565 * IFT_GIF (net/if_gif.c)
566 * IFT_LOOP (net/if_loop.c)
567 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
568 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
569 * IFT_STF (net/if_stf.c)
570 * IFT_L2VLAN (net/if_vlan.c)
571 * IFT_PROPVIRTUAL (net/if_bridge.h>
572 */
573 /*
574 * The following use IPv4 addresses as link-layer addresses:
575 * IFT_OTHER (net/if_gre.c)
576 */
577 case IFT_ARCNET: /* default below is believed correct for all these. */
578 case IFT_ETHER:
579 case IFT_FDDI:
580 case IFT_HIPPI:
581 case IFT_ISO88025:
582 default:
583 return hexname((const uint8_t *)CLLADDR(sdl),
584 (size_t)sdl->sdl_alen, host, hostlen);
585 }
586 out:
587 if (n < 0 || (socklen_t) n >= hostlen) {
588 *host = '\0';
589 return EAI_MEMORY;
590 }
591 return 0;
592 }
593
594 static int
595 hexname(const uint8_t *cp, size_t len, char *host, socklen_t hostlen)
596 {
597 int n;
598 size_t i;
599 char *outp = host;
600
601 *outp = '\0';
602 for (i = 0; i < len; i++) {
603 n = snprintf(outp, hostlen, "%s%02x",
604 i ? ":" : "", cp[i]);
605 if (n < 0 || (socklen_t) n >= hostlen) {
606 *host = '\0';
607 return EAI_MEMORY;
608 }
609 outp += n;
610 hostlen -= n;
611 }
612 return 0;
613 }
614