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