1 /* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2000 Ben Harris.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked
36 * - RFC2553 says that we should raise error on short buffer. X/Open says
37 * we need to truncate the result. We obey RFC2553 (and X/Open should be
38 * modified). ipngwg rough consensus seems to follow RFC2553.
39 * - What is "local" in NI_FQDN?
40 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
41 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
42 * sin6_scope_id is filled - standardization status?
43 * XXX breaks backward compat for code that expects no scopeid.
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/types.h>
51 #include <sys/socket.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/firewire.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59 #include <arpa/nameser.h>
66 static const struct afd *find_afd(int);
67 static int getnameinfo_inet(const struct afd *,
68 const struct sockaddr *, socklen_t, char *,
69 size_t, char *, size_t, int);
71 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
73 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
75 static int getnameinfo_link(const struct afd *,
76 const struct sockaddr *, socklen_t, char *,
77 size_t, char *, size_t, int);
78 static int hexname(const u_int8_t *, size_t, char *, size_t);
79 static int getnameinfo_un(const struct afd *,
80 const struct sockaddr *, socklen_t, char *,
81 size_t, char *, size_t, int);
83 static const struct afd {
88 int (*a_func)(const struct afd *,
89 const struct sockaddr *, socklen_t, char *,
90 size_t, char *, size_t, int);
93 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
94 offsetof(struct sockaddr_in6, sin6_addr),
97 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
98 offsetof(struct sockaddr_in, sin_addr),
100 #define sizeofmember(type, member) (sizeof(((type *)0)->member))
101 {PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path),
102 sizeof(struct sockaddr_un),
103 offsetof(struct sockaddr_un, sun_path),
105 {PF_LINK, sizeofmember(struct sockaddr_dl, sdl_data),
106 sizeof(struct sockaddr_dl),
107 offsetof(struct sockaddr_dl, sdl_data),
113 getnameinfo(const struct sockaddr *sa, socklen_t salen,
114 char *host, size_t hostlen, char *serv, size_t servlen,
117 const struct afd *afd;
122 afd = find_afd(sa->sa_family);
125 switch (sa->sa_family) {
128 * PF_LOCAL uses variable sa->sa_len depending on the
129 * content length of sun_path. Require 1 byte in
132 if (salen > afd->a_socklen ||
133 salen <= afd->a_socklen -
134 sizeofmember(struct sockaddr_un, sun_path))
138 if (salen <= afd->a_socklen -
139 sizeofmember(struct sockaddr_dl, sdl_data))
143 if (salen != afd->a_socklen)
148 return ((*afd->a_func)(afd, sa, salen, host, hostlen,
149 serv, servlen, flags));
152 static const struct afd *
155 const struct afd *afd;
159 for (afd = &afdl[0]; afd->a_af > 0; afd++) {
167 getnameinfo_inet(const struct afd *afd,
168 const struct sockaddr *sa, socklen_t salen,
169 char *host, size_t hostlen, char *serv, size_t servlen,
181 /* network byte order */
182 port = ((const struct sockaddr_in *)sa)->sin_port;
183 addr = (const char *)sa + afd->a_off;
185 if (serv == NULL || servlen == 0) {
187 * do nothing in this case.
188 * in case you are wondering if "&&" is more correct than
189 * "||" here: rfc2553bis-03 says that serv == NULL OR
190 * servlen == 0 means that the caller does not want the result.
193 if (flags & NI_NUMERICSERV)
196 sp = getservbyport(port,
197 (flags & NI_DGRAM) ? "udp" : "tcp");
200 if (strlen(sp->s_name) + 1 > servlen)
202 strlcpy(serv, sp->s_name, servlen);
204 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
205 if (strlen(numserv) + 1 > servlen)
207 strlcpy(serv, numserv, servlen);
211 switch (sa->sa_family) {
214 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
215 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
216 flags |= NI_NUMERICHOST;
217 v4a >>= IN_CLASSA_NSHIFT;
219 flags |= NI_NUMERICHOST;
224 const struct sockaddr_in6 *sin6;
225 sin6 = (const struct sockaddr_in6 *)sa;
226 switch (sin6->sin6_addr.s6_addr[0]) {
228 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
230 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
233 flags |= NI_NUMERICHOST;
236 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
237 flags |= NI_NUMERICHOST;
239 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
240 flags |= NI_NUMERICHOST;
247 if (host == NULL || hostlen == 0) {
249 * do nothing in this case.
250 * in case you are wondering if "&&" is more correct than
251 * "||" here: rfc2553bis-03 says that host == NULL or
252 * hostlen == 0 means that the caller does not want the result.
254 } else if (flags & NI_NUMERICHOST) {
257 /* NUMERICHOST and NAMEREQD conflicts with each other */
258 if (flags & NI_NAMEREQD)
267 if ((error = ip6_parsenumeric(sa, addr, host,
268 hostlen, flags)) != 0)
274 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
277 numaddrlen = strlen(numaddr);
278 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
280 strlcpy(host, numaddr, hostlen);
284 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
289 * commented out, since "for local host" is not
290 * implemented here - see RFC2553 p30
292 if (flags & NI_NOFQDN) {
294 p = strchr(hp->h_name, '.');
299 if (strlen(hp->h_name) + 1 > hostlen) {
303 strlcpy(host, hp->h_name, hostlen);
306 if (flags & NI_NAMEREQD)
314 if ((error = ip6_parsenumeric(sa, addr, host,
322 if (inet_ntop(afd->a_af, addr, host,
334 ip6_parsenumeric(const struct sockaddr *sa, const char *addr,
335 char *host, size_t hostlen, int flags)
340 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
343 numaddrlen = strlen(numaddr);
344 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
346 strlcpy(host, numaddr, hostlen);
348 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
349 char zonebuf[MAXHOSTNAMELEN];
352 zonelen = ip6_sa2str(
353 (const struct sockaddr_in6 *)(const void *)sa,
354 zonebuf, sizeof(zonebuf), flags);
357 if (zonelen + 1 + numaddrlen + 1 > hostlen)
360 /* construct <numeric-addr><delim><zoneid> */
361 memcpy(host + numaddrlen + 1, zonebuf,
363 host[numaddrlen] = SCOPE_DELIMITER;
364 host[numaddrlen + 1 + zonelen] = '\0';
372 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
374 unsigned int ifindex;
375 const struct in6_addr *a6;
378 ifindex = (unsigned int)sa6->sin6_scope_id;
379 a6 = &sa6->sin6_addr;
381 if ((flags & NI_NUMERICSCOPE) != 0) {
382 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
383 if (n < 0 || n >= bufsiz)
389 /* if_indextoname() does not take buffer size. not a good api... */
390 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
391 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
392 char *p = if_indextoname(ifindex, buf);
399 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
400 if (n < 0 || (size_t)n >= bufsiz)
408 * getnameinfo_link():
409 * Format a link-layer address into a printable format, paying attention to
410 * the interface type.
414 getnameinfo_link(const struct afd *afd,
415 const struct sockaddr *sa, socklen_t salen,
416 char *host, size_t hostlen, char *serv, size_t servlen, int flags)
418 const struct sockaddr_dl *sdl =
419 (const struct sockaddr_dl *)(const void *)sa;
420 const struct fw_hwaddr *iha;
423 if (serv != NULL && servlen > 0)
426 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
427 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
435 if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) {
441 memcpy(host, sdl->sdl_data, sdl->sdl_nlen);
446 switch (sdl->sdl_type) {
448 if (sdl->sdl_alen < sizeof(iha->sender_unique_ID_hi) +
449 sizeof(iha->sender_unique_ID_lo))
451 iha = (const struct fw_hwaddr *)(const void *)LLADDR(sdl);
452 return hexname((const u_int8_t *)&iha->sender_unique_ID_hi,
453 sizeof(iha->sender_unique_ID_hi) +
454 sizeof(iha->sender_unique_ID_lo),
457 * The following have zero-length addresses.
458 * IFT_GIF (net/if_gif.c)
459 * IFT_LOOP (net/if_loop.c)
460 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
461 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
462 * IFT_STF (net/if_stf.c)
463 * IFT_L2VLAN (net/if_vlan.c)
464 * IFT_BRIDGE (net/if_bridge.h>
467 * The following use IPv4 addresses as link-layer addresses:
468 * IFT_OTHER (net/if_gre.c)
469 * IFT_OTHER (netinet/ip_ipip.c)
471 /* default below is believed correct for all these. */
478 return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
484 hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen)
490 for (i = 0; i < len; i++) {
491 n = snprintf(outp, hostlen, "%s%02x",
492 i ? ":" : "", cp[i]);
493 if (n < 0 || n >= hostlen) {
505 * Format a UNIX IPC domain address (pathname).
509 getnameinfo_un(const struct afd *afd,
510 const struct sockaddr *sa, socklen_t salen,
511 char *host, size_t hostlen, char *serv, size_t servlen, int flags)
515 if (serv != NULL && servlen > 0)
517 if (host != NULL && hostlen > 0) {
518 pathlen = sa->sa_len - afd->a_off;
520 if (pathlen + 1 > hostlen) {
524 strlcpy(host, (const char *)sa + afd->a_off, pathlen + 1);