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