1 /** @file
2 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials are licensed and made available
4 under the terms and conditions of the BSD License which accompanies this
5 distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php.
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10 **/
11 /* $NetBSD: getnameinfo.c,v 1.45 2006/10/15 16:14:46 christos Exp $ */
12 /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */
13
14 /*
15 * Copyright (c) 2000 Ben Harris.
16 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. Neither the name of the project nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 */
43
44 /*
45 * Issues to be discussed:
46 * - Thread safe-ness must be checked
47 * - RFC2553 says that we should raise error on short buffer. X/Open says
48 * we need to truncate the result. We obey RFC2553 (and X/Open should be
49 * modified). ipngwg rough consensus seems to follow RFC2553.
50 * - What is "local" in NI_FQDN?
51 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
52 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
53 * sin6_scope_id is filled - standardization status?
54 * XXX breaks backward compat for code that expects no scopeid.
55 * beware on merge.
56 */
57
58 #define INET6 1
59
60 #include <sys/cdefs.h>
61 #if defined(LIBC_SCCS) && !defined(lint)
62 __RCSID("$NetBSD: getnameinfo.c,v 1.45 2006/10/15 16:14:46 christos Exp $");
63 #endif /* LIBC_SCCS and not lint */
64
65 #include "namespace.h"
66 #include <sys/types.h>
67 #include <sys/socket.h>
68 #include <net/if.h>
69 #include <net/if_dl.h>
70 //#include <net/if_ieee1394.h>
71 //#include <net/if_types.h>
72 #include <netinet/in.h>
73 #include <arpa/inet.h>
74 #include <arpa/nameser.h>
75 #include <assert.h>
76 #include <limits.h>
77 #include <netdb.h>
78 #include <resolv.h>
79 #include <stddef.h>
80 #include <string.h>
81
82 #include <net/servent.h>
83
84 #define CLLADDR(x) ( LLADDR(x) )
85 #define endservent_r(svd) endservent()
86 #define getservbyport_r(Port,pProto,pSv,pSvd) getservbyport(Port,pProto)
87
88 #ifdef __weak_alias
89 __weak_alias(getnameinfo,_getnameinfo)
90 #endif
91
92 static
93 int
94 hexname(
95 const u_int8_t * cp,
96 size_t len,
97 char * host,
98 socklen_t hostlen
99 );
100
101 static const struct afd {
102 int a_af;
103 socklen_t a_addrlen;
104 socklen_t a_socklen;
105 int a_off;
106 } afdl [] = {
107 #ifdef INET6
108 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
109 offsetof(struct sockaddr_in6, sin6_addr)},
110 #endif
111 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
112 offsetof(struct sockaddr_in, sin_addr)},
113 {0, 0, 0, 0},
114 };
115
116 struct sockinet {
117 u_char si_len;
118 u_char si_family;
119 u_short si_port;
120 };
121
122 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
123 socklen_t, char *, socklen_t, int));
124 #ifdef INET6
125 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
126 socklen_t, int));
127 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t,
128 int));
129 #endif
130 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
131 socklen_t, char *, socklen_t, int));
132 static int hexname __P((const u_int8_t *, size_t, char *, socklen_t));
133
134 /*
135 * Top-level getnameinfo() code. Look at the address family, and pick an
136 * appropriate function to call.
137 */
138 int
getnameinfo(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)139 getnameinfo(
140 const struct sockaddr * sa,
141 socklen_t salen,
142 char * host,
143 socklen_t hostlen,
144 char * serv,
145 socklen_t servlen,
146 int flags
147 )
148 {
149
150 switch (sa->sa_family) {
151 case AF_INET:
152 case AF_INET6:
153 return getnameinfo_inet(sa, salen, host, hostlen,
154 serv, servlen, flags);
155 case AF_LINK:
156 return getnameinfo_link(sa, salen, host, hostlen,
157 serv, servlen, flags);
158 default:
159 return EAI_FAMILY;
160 }
161 }
162
163
164 /*
165 * getnameinfo_inet():
166 * Format an IPv4 or IPv6 sockaddr into a printable string.
167 */
168 static
169 int
getnameinfo_inet(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)170 getnameinfo_inet(
171 const struct sockaddr * sa,
172 socklen_t salen,
173 char * host,
174 socklen_t hostlen,
175 char * serv,
176 socklen_t servlen,
177 int flags
178 )
179 {
180 const struct afd *afd;
181 struct servent *sp;
182 struct hostent *hp;
183 u_short port;
184 int family, i;
185 const char *addr;
186 u_int32_t v4a;
187 char numserv[512];
188 char numaddr[512];
189
190 /* sa is checked below */
191 /* host may be NULL */
192 /* serv may be NULL */
193
194 if (sa == NULL)
195 return EAI_FAIL;
196
197 #ifdef BSD4_4
198 if (sa->sa_len != salen)
199 return EAI_FAIL;
200 #endif
201
202 family = sa->sa_family;
203 for (i = 0; afdl[i].a_af; i++)
204 if (afdl[i].a_af == family) {
205 afd = &afdl[i];
206 goto found;
207 }
208 return EAI_FAMILY;
209
210 found:
211 if (salen != afd->a_socklen)
212 return EAI_FAIL;
213
214 /* network byte order */
215 port = ((const struct sockinet *)(const void *)sa)->si_port;
216 addr = (const char *)(const void *)sa + afd->a_off;
217
218 if (serv == NULL || servlen == 0) {
219 /*
220 * do nothing in this case.
221 * in case you are wondering if "&&" is more correct than
222 * "||" here: rfc2553bis-03 says that serv == NULL OR
223 * servlen == 0 means that the caller does not want the result.
224 */
225 } else {
226 if (flags & NI_NUMERICSERV)
227 sp = NULL;
228 else {
229 struct servent_data svd;
230 // struct servent sv;
231
232 (void)memset(&svd, 0, sizeof(svd));
233 sp = getservbyport_r(port,
234 (flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
235 endservent_r(&svd);
236 }
237 if (sp) {
238 if (strlen(sp->s_name) + 1 > servlen)
239 return EAI_MEMORY;
240 strlcpy(serv, sp->s_name, servlen);
241 } else {
242 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
243 if (strlen(numserv) + 1 > servlen)
244 return EAI_MEMORY;
245 strlcpy(serv, numserv, servlen);
246 }
247 }
248
249 switch (sa->sa_family) {
250 case AF_INET:
251 v4a = (u_int32_t)
252 ntohl(((const struct sockaddr_in *)
253 (const void *)sa)->sin_addr.s_addr);
254 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
255 flags |= NI_NUMERICHOST;
256 v4a >>= IN_CLASSA_NSHIFT;
257 if (v4a == 0)
258 flags |= NI_NUMERICHOST;
259 break;
260 #ifdef INET6
261 case AF_INET6:
262 {
263 const struct sockaddr_in6 *sin6;
264 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
265 switch (sin6->sin6_addr.s6_addr[0]) {
266 case 0x00:
267 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
268 ;
269 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
270 ;
271 else
272 flags |= NI_NUMERICHOST;
273 break;
274 default:
275 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
276 flags |= NI_NUMERICHOST;
277 }
278 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
279 flags |= NI_NUMERICHOST;
280 break;
281 }
282 }
283 break;
284 #endif
285 }
286 if (host == NULL || hostlen == 0) {
287 /*
288 * do nothing in this case.
289 * in case you are wondering if "&&" is more correct than
290 * "||" here: rfc2553bis-03 says that host == NULL or
291 * hostlen == 0 means that the caller does not want the result.
292 */
293 } else if (flags & NI_NUMERICHOST) {
294 size_t numaddrlen;
295
296 /* NUMERICHOST and NAMEREQD conflicts with each other */
297 if (flags & NI_NAMEREQD)
298 return EAI_NONAME;
299
300 switch(afd->a_af) {
301 #ifdef INET6
302 case AF_INET6:
303 {
304 int error;
305
306 if ((error = ip6_parsenumeric(sa, addr, host,
307 hostlen, flags)) != 0)
308 return(error);
309 break;
310 }
311 #endif
312 default:
313 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
314 == NULL)
315 return EAI_SYSTEM;
316 numaddrlen = strlen(numaddr);
317 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
318 return EAI_MEMORY;
319 strlcpy(host, numaddr, hostlen);
320 break;
321 }
322 } else {
323 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
324
325 if (hp) {
326 #if 0
327 /*
328 * commented out, since "for local host" is not
329 * implemented here - see RFC2553 p30
330 */
331 if (flags & NI_NOFQDN) {
332 char *p;
333 p = strchr(hp->h_name, '.');
334 if (p)
335 *p = '\0';
336 }
337 #endif
338 if (strlen(hp->h_name) + 1 > hostlen) {
339 return EAI_MEMORY;
340 }
341 strlcpy(host, hp->h_name, hostlen);
342 } else {
343 if (flags & NI_NAMEREQD)
344 return EAI_NONAME;
345 switch(afd->a_af) {
346 #ifdef INET6
347 case AF_INET6:
348 {
349 int error;
350
351 if ((error = ip6_parsenumeric(sa, addr, host,
352 hostlen,
353 flags)) != 0)
354 return(error);
355 break;
356 }
357 #endif
358 default:
359 if (inet_ntop(afd->a_af, addr, host,
360 hostlen) == NULL)
361 return EAI_SYSTEM;
362 break;
363 }
364 }
365 }
366 return(0);
367 }
368
369 #ifdef INET6
370 static int
ip6_parsenumeric(const struct sockaddr * sa,const char * addr,char * host,socklen_t hostlen,int flags)371 ip6_parsenumeric(
372 const struct sockaddr *sa,
373 const char *addr,
374 char *host,
375 socklen_t hostlen,
376 int flags
377 )
378 {
379 size_t numaddrlen;
380 char numaddr[512];
381
382 _DIAGASSERT(sa != NULL);
383 _DIAGASSERT(addr != NULL);
384 _DIAGASSERT(host != NULL);
385
386 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
387 return EAI_SYSTEM;
388
389 numaddrlen = strlen(numaddr);
390 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
391 return EAI_OVERFLOW;
392 strlcpy(host, numaddr, hostlen);
393
394 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
395 char zonebuf[MAXHOSTNAMELEN];
396 int zonelen;
397
398 zonelen = ip6_sa2str(
399 (const struct sockaddr_in6 *)(const void *)sa,
400 zonebuf, sizeof(zonebuf), flags);
401 if (zonelen < 0)
402 return EAI_OVERFLOW;
403 if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
404 return EAI_OVERFLOW;
405 /* construct <numeric-addr><delim><zoneid> */
406 memcpy(host + numaddrlen + 1, zonebuf,
407 (size_t)zonelen);
408 host[numaddrlen] = SCOPE_DELIMITER;
409 host[numaddrlen + 1 + zonelen] = '\0';
410 }
411
412 return 0;
413 }
414
415 /* ARGSUSED */
416 static int
ip6_sa2str(const struct sockaddr_in6 * sa6,char * buf,size_t bufsiz,int flags)417 ip6_sa2str(
418 const struct sockaddr_in6 *sa6,
419 char *buf,
420 size_t bufsiz,
421 int flags
422 )
423 {
424 #if 0
425 unsigned int ifindex;
426 const struct in6_addr *a6;
427 #endif
428 int n;
429
430 _DIAGASSERT(sa6 != NULL);
431 _DIAGASSERT(buf != NULL);
432
433 #if 0
434 ifindex = (unsigned int)sa6->sin6_scope_id;
435 a6 = &sa6->sin6_addr;
436 #endif
437
438 #ifdef NI_NUMERICSCOPE
439 if ((flags & NI_NUMERICSCOPE) != 0) {
440 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
441 if ((n < 0) || ((size_t)n >= bufsiz))
442 return -1;
443 else
444 return n;
445 }
446 #endif
447
448 #if 0
449 /* if_indextoname() does not take buffer size. not a good api... */
450 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
451 bufsiz >= IF_NAMESIZE) {
452 char *p = if_indextoname(ifindex, buf);
453 if (p) {
454 return(strlen(p));
455 }
456 }
457 #endif // 0
458
459 /* last resort */
460 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
461 if (n < 0 || (size_t) n >= bufsiz)
462 return -1;
463 else
464 return n;
465 }
466 #endif /* INET6 */
467
468
469 /*
470 * getnameinfo_link():
471 * Format a link-layer address into a printable format, paying attention to
472 * the interface type.
473 */
474 /* ARGSUSED */
475 static
476 int
getnameinfo_link(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)477 getnameinfo_link (
478 const struct sockaddr * sa,
479 socklen_t salen,
480 char * host,
481 socklen_t hostlen,
482 char * serv,
483 socklen_t servlen,
484 int flags
485 )
486 {
487 const struct sockaddr_dl *sdl =
488 (const struct sockaddr_dl *)(const void *)sa;
489 // const struct ieee1394_hwaddr *iha;
490 int n;
491
492 if (serv != NULL && servlen > 0)
493 *serv = '\0';
494
495 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
496 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
497 if (n < 0 || (socklen_t) n > hostlen) {
498 *host = '\0';
499 return EAI_MEMORY;
500 }
501 return 0;
502 }
503
504 #if 0
505 switch (sdl->sdl_type) {
506 #ifdef IFT_ECONET
507 case IFT_ECONET:
508 if (sdl->sdl_alen < 2)
509 return EAI_FAMILY;
510 if (CLLADDR(sdl)[1] == 0)
511 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
512 else
513 n = snprintf(host, hostlen, "%u.%u",
514 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
515 if (n < 0 || (socklen_t) n >= hostlen) {
516 *host = '\0';
517 return EAI_MEMORY;
518 } else
519 return 0;
520 #endif
521 case IFT_IEEE1394:
522 if (sdl->sdl_alen < sizeof(iha->iha_uid))
523 return EAI_FAMILY;
524 iha =
525 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
526 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
527 host, hostlen);
528 /*
529 * The following have zero-length addresses.
530 * IFT_ATM (net/if_atmsubr.c)
531 * IFT_FAITH (net/if_faith.c)
532 * IFT_GIF (net/if_gif.c)
533 * IFT_LOOP (net/if_loop.c)
534 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
535 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
536 * IFT_STF (net/if_stf.c)
537 * IFT_L2VLAN (net/if_vlan.c)
538 * IFT_PROPVIRTUAL (net/if_bridge.h>
539 */
540 /*
541 * The following use IPv4 addresses as link-layer addresses:
542 * IFT_OTHER (net/if_gre.c)
543 */
544 case IFT_ARCNET: /* default below is believed correct for all these. */
545 case IFT_ETHER:
546 case IFT_FDDI:
547 case IFT_HIPPI:
548 case IFT_ISO88025:
549 default:
550 #endif // 0
551 return hexname((const u_int8_t *)CLLADDR(sdl),
552 (size_t)sdl->sdl_alen, host, hostlen);
553 // }
554 }
555
556 static
557 int
558 hexname(
559 const u_int8_t * cp,
560 size_t len,
561 char * host,
562 socklen_t hostlen
563 )
564 {
565 int n;
566 size_t i;
567 char *outp = host;
568
569 *outp = '\0';
570 for (i = 0; i < len; i++) {
571 n = snprintf(outp, hostlen, "%s%02x",
572 i ? ":" : "", cp[i]);
573 if (n < 0 || (socklen_t) n >= hostlen) {
574 *host = '\0';
575 return EAI_MEMORY;
576 }
577 outp += n;
578 hostlen -= n;
579 }
580 return 0;
581 }
582