1 /* $NetBSD: getnameinfo.c,v 1.46 2009/02/11 05:25:17 lukem Exp $ */
2 /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */
5 * Copyright (c) 2000 Ben Harris.
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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.
48 #include <sys/cdefs.h>
49 #if defined(LIBC_SCCS) && !defined(lint)
50 __RCSID("$NetBSD: getnameinfo.c,v 1.46 2009/02/11 05:25:17 lukem Exp $");
51 #endif /* LIBC_SCCS and not lint */
53 #include "namespace.h"
54 #include <sys/types.h>
55 #include <sys/socket.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>
73 __weak_alias(getnameinfo
,_getnameinfo
)
76 static const struct afd
{
83 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
84 offsetof(struct sockaddr_in6
, sin6_addr
)},
86 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
87 offsetof(struct sockaddr_in
, sin_addr
)},
97 static int getnameinfo_inet
__P((const struct sockaddr
*, socklen_t
, char *,
98 socklen_t
, char *, socklen_t
, int));
100 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
102 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t,
105 static int getnameinfo_link
__P((const struct sockaddr
*, socklen_t
, char *,
106 socklen_t
, char *, socklen_t
, int));
107 static int hexname
__P((const u_int8_t
*, size_t, char *, socklen_t
));
110 * Top-level getnameinfo() code. Look at the address family, and pick an
111 * appropriate function to call.
114 getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
115 const struct sockaddr
*sa
;
118 socklen_t hostlen
, servlen
;
122 switch (sa
->sa_family
) {
125 return getnameinfo_inet(sa
, salen
, host
, hostlen
,
126 serv
, servlen
, flags
);
128 return getnameinfo_link(sa
, salen
, host
, hostlen
,
129 serv
, servlen
, flags
);
137 * getnameinfo_inet():
138 * Format an IPv4 or IPv6 sockaddr into a printable string.
141 getnameinfo_inet(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
)
142 const struct sockaddr
*sa
;
150 const struct afd
*afd
;
160 /* sa is checked below */
161 /* host may be NULL */
162 /* serv may be NULL */
168 if (sa
->sa_len
!= salen
)
172 family
= sa
->sa_family
;
173 for (i
= 0; afdl
[i
].a_af
; i
++)
174 if (afdl
[i
].a_af
== family
) {
181 if (salen
!= afd
->a_socklen
)
184 /* network byte order */
185 port
= ((const struct sockinet
*)(const void *)sa
)->si_port
;
186 addr
= (const char *)(const void *)sa
+ afd
->a_off
;
188 if (serv
== NULL
|| servlen
== 0) {
190 * do nothing in this case.
191 * in case you are wondering if "&&" is more correct than
192 * "||" here: rfc2553bis-03 says that serv == NULL OR
193 * servlen == 0 means that the caller does not want the result.
196 struct servent_data svd
;
199 if (flags
& NI_NUMERICSERV
)
202 (void)memset(&svd
, 0, sizeof(svd
));
203 sp
= getservbyport_r(port
,
204 (flags
& NI_DGRAM
) ? "udp" : "tcp", &sv
, &svd
);
207 if (strlen(sp
->s_name
) + 1 > servlen
) {
211 strlcpy(serv
, sp
->s_name
, servlen
);
214 snprintf(numserv
, sizeof(numserv
), "%u", ntohs(port
));
215 if (strlen(numserv
) + 1 > servlen
)
217 strlcpy(serv
, numserv
, servlen
);
221 switch (sa
->sa_family
) {
224 ntohl(((const struct sockaddr_in
*)
225 (const void *)sa
)->sin_addr
.s_addr
);
226 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
227 flags
|= NI_NUMERICHOST
;
228 v4a
>>= IN_CLASSA_NSHIFT
;
230 flags
|= NI_NUMERICHOST
;
235 const struct sockaddr_in6
*sin6
;
236 sin6
= (const struct sockaddr_in6
*)(const void *)sa
;
237 switch (sin6
->sin6_addr
.s6_addr
[0]) {
239 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
241 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
244 flags
|= NI_NUMERICHOST
;
247 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
248 flags
|= NI_NUMERICHOST
;
250 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
251 flags
|= NI_NUMERICHOST
;
258 if (host
== NULL
|| hostlen
== 0) {
260 * do nothing in this case.
261 * in case you are wondering if "&&" is more correct than
262 * "||" here: rfc2553bis-03 says that host == NULL or
263 * hostlen == 0 means that the caller does not want the result.
265 } else if (flags
& NI_NUMERICHOST
) {
268 /* NUMERICHOST and NAMEREQD conflicts with each other */
269 if (flags
& NI_NAMEREQD
)
278 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
279 hostlen
, flags
)) != 0)
285 if (inet_ntop(afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
288 numaddrlen
= strlen(numaddr
);
289 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
291 strlcpy(host
, numaddr
, hostlen
);
295 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
300 * commented out, since "for local host" is not
301 * implemented here - see RFC2553 p30
303 if (flags
& NI_NOFQDN
) {
305 p
= strchr(hp
->h_name
, '.');
310 if (strlen(hp
->h_name
) + 1 > hostlen
) {
313 strlcpy(host
, hp
->h_name
, hostlen
);
315 if (flags
& NI_NAMEREQD
)
323 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
331 if (inet_ntop(afd
->a_af
, addr
, host
,
343 ip6_parsenumeric(sa
, addr
, host
, hostlen
, flags
)
344 const struct sockaddr
*sa
;
353 _DIAGASSERT(sa
!= NULL
);
354 _DIAGASSERT(addr
!= NULL
);
355 _DIAGASSERT(host
!= NULL
);
357 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
360 numaddrlen
= strlen(numaddr
);
361 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
363 strlcpy(host
, numaddr
, hostlen
);
365 if (((const struct sockaddr_in6
*)(const void *)sa
)->sin6_scope_id
) {
366 char zonebuf
[MAXHOSTNAMELEN
];
369 zonelen
= ip6_sa2str(
370 (const struct sockaddr_in6
*)(const void *)sa
,
371 zonebuf
, sizeof(zonebuf
), flags
);
374 if ((size_t) zonelen
+ 1 + numaddrlen
+ 1 > hostlen
)
376 /* construct <numeric-addr><delim><zoneid> */
377 memcpy(host
+ numaddrlen
+ 1, zonebuf
,
379 host
[numaddrlen
] = SCOPE_DELIMITER
;
380 host
[numaddrlen
+ 1 + zonelen
] = '\0';
388 ip6_sa2str(sa6
, buf
, bufsiz
, flags
)
389 const struct sockaddr_in6
*sa6
;
394 unsigned int ifindex
;
395 const struct in6_addr
*a6
;
398 _DIAGASSERT(sa6
!= NULL
);
399 _DIAGASSERT(buf
!= NULL
);
401 ifindex
= (unsigned int)sa6
->sin6_scope_id
;
402 a6
= &sa6
->sin6_addr
;
404 #ifdef NI_NUMERICSCOPE
405 if ((flags
& NI_NUMERICSCOPE
) != 0) {
406 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
407 if (n
< 0 || (size_t)n
>= bufsiz
)
414 /* if_indextoname() does not take buffer size. not a good api... */
415 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) &&
416 bufsiz
>= IF_NAMESIZE
) {
417 char *p
= if_indextoname(ifindex
, buf
);
424 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
425 if (n
< 0 || (size_t) n
>= bufsiz
)
434 * getnameinfo_link():
435 * Format a link-layer address into a printable format, paying attention to
436 * the interface type.
440 getnameinfo_link(const struct sockaddr
*sa
, socklen_t salen
,
441 char *host
, socklen_t hostlen
, char *serv
, socklen_t servlen
,
444 const struct sockaddr_dl
*sdl
=
445 (const struct sockaddr_dl
*)(const void *)sa
;
446 const struct ieee1394_hwaddr
*iha
;
449 if (serv
!= NULL
&& servlen
> 0)
452 if (sdl
->sdl_nlen
== 0 && sdl
->sdl_alen
== 0 && sdl
->sdl_slen
== 0) {
453 n
= snprintf(host
, hostlen
, "link#%u", sdl
->sdl_index
);
454 if (n
< 0 || (socklen_t
) n
> hostlen
) {
461 switch (sdl
->sdl_type
) {
464 if (sdl
->sdl_alen
< 2)
466 if (CLLADDR(sdl
)[1] == 0)
467 n
= snprintf(host
, hostlen
, "%u", CLLADDR(sdl
)[0]);
469 n
= snprintf(host
, hostlen
, "%u.%u",
470 CLLADDR(sdl
)[1], CLLADDR(sdl
)[0]);
471 if (n
< 0 || (socklen_t
) n
>= hostlen
) {
478 if (sdl
->sdl_alen
< sizeof(iha
->iha_uid
))
481 (const struct ieee1394_hwaddr
*)(const void *)CLLADDR(sdl
);
482 return hexname(iha
->iha_uid
, sizeof(iha
->iha_uid
),
485 * The following have zero-length addresses.
486 * IFT_ATM (net/if_atmsubr.c)
487 * IFT_FAITH (net/if_faith.c)
488 * IFT_GIF (net/if_gif.c)
489 * IFT_LOOP (net/if_loop.c)
490 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
491 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
492 * IFT_STF (net/if_stf.c)
493 * IFT_L2VLAN (net/if_vlan.c)
494 * IFT_PROPVIRTUAL (net/if_bridge.h>
497 * The following use IPv4 addresses as link-layer addresses:
498 * IFT_OTHER (net/if_gre.c)
500 case IFT_ARCNET
: /* default below is believed correct for all these. */
506 return hexname((const u_int8_t
*)CLLADDR(sdl
),
507 (size_t)sdl
->sdl_alen
, host
, hostlen
);
512 hexname(cp
, len
, host
, hostlen
)
523 for (i
= 0; i
< len
; i
++) {
524 n
= snprintf(outp
, hostlen
, "%s%02x",
525 i
? ":" : "", cp
[i
]);
526 if (n
< 0 || (socklen_t
) n
>= hostlen
) {