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