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