1 /* $FreeBSD: src/lib/libc/net/getnameinfo.c,v 1.20 2007/02/28 21:18:38 bms Exp $ */
2 /* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * Copyright (c) 2000 Ben Harris.
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/types.h>
49 #include <sys/socket.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <arpa/nameser.h>
62 static int getnameinfo_inet(const struct sockaddr
*, socklen_t
, char *,
63 size_t, char *, size_t, int);
65 static int ip6_parsenumeric(const struct sockaddr
*, const char *, char *,
67 static int ip6_sa2str(const struct sockaddr_in6
*, char *, size_t, int);
69 static int getnameinfo_link(const struct sockaddr
*, socklen_t
, char *,
70 size_t, char *, size_t, int);
71 static int hexname(const u_int8_t
*, size_t, char *, size_t);
74 getnameinfo(const struct sockaddr
* __restrict sa
, socklen_t salen
,
75 char * __restrict host
, size_t hostlen
, char * __restrict serv
,
76 size_t servlen
, int flags
)
79 switch (sa
->sa_family
) {
84 return getnameinfo_inet(sa
, salen
, host
, hostlen
, serv
,
87 return getnameinfo_link(sa
, salen
, host
, hostlen
, serv
,
94 static const struct afd
{
101 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
102 offsetof(struct sockaddr_in6
, sin6_addr
)},
104 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
105 offsetof(struct sockaddr_in
, sin_addr
)},
116 getnameinfo_inet(const struct sockaddr
*sa
, socklen_t salen
, char *host
,
117 size_t hostlen
, char *serv
, size_t servlen
, int flags
)
119 const struct afd
*afd
;
133 family
= sa
->sa_family
;
134 for (i
= 0; afdl
[i
].a_af
; i
++)
135 if (afdl
[i
].a_af
== family
) {
142 if (salen
!= afd
->a_socklen
)
145 /* network byte order */
146 port
= ((const struct sockinet
*)sa
)->si_port
;
147 addr
= (const char *)sa
+ afd
->a_off
;
149 if (serv
== NULL
|| servlen
== 0) {
151 * do nothing in this case.
152 * in case you are wondering if "&&" is more correct than
153 * "||" here: rfc2553bis-03 says that serv == NULL OR
154 * servlen == 0 means that the caller does not want the result.
157 if (flags
& NI_NUMERICSERV
)
160 sp
= getservbyport(port
,
161 (flags
& NI_DGRAM
) ? "udp" : "tcp");
164 if (strlen(sp
->s_name
) + 1 > servlen
)
166 strlcpy(serv
, sp
->s_name
, servlen
);
168 snprintf(numserv
, sizeof(numserv
), "%u", ntohs(port
));
169 if (strlen(numserv
) + 1 > servlen
)
171 strlcpy(serv
, numserv
, servlen
);
175 switch (sa
->sa_family
) {
178 ntohl(((const struct sockaddr_in
*)sa
)->sin_addr
.s_addr
);
179 if (IN_MULTICAST(v4a
) || IN_EXPERIMENTAL(v4a
))
180 flags
|= NI_NUMERICHOST
;
181 v4a
>>= IN_CLASSA_NSHIFT
;
183 flags
|= NI_NUMERICHOST
;
188 const struct sockaddr_in6
*sin6
;
189 sin6
= (const struct sockaddr_in6
*)sa
;
190 switch (sin6
->sin6_addr
.s6_addr
[0]) {
192 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
194 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
197 flags
|= NI_NUMERICHOST
;
200 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
201 flags
|= NI_NUMERICHOST
;
203 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
204 flags
|= NI_NUMERICHOST
;
211 if (host
== NULL
|| hostlen
== 0) {
213 * do nothing in this case.
214 * in case you are wondering if "&&" is more correct than
215 * "||" here: rfc2553bis-03 says that host == NULL or
216 * hostlen == 0 means that the caller does not want the result.
218 } else if (flags
& NI_NUMERICHOST
) {
221 /* NUMERICHOST and NAMEREQD conflicts with each other */
222 if (flags
& NI_NAMEREQD
)
231 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
232 hostlen
, flags
)) != 0)
238 if (inet_ntop(afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
241 numaddrlen
= strlen(numaddr
);
242 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
244 strlcpy(host
, numaddr
, hostlen
);
248 hp
= getipnodebyaddr(addr
, afd
->a_addrlen
, afd
->a_af
, &h_error
);
253 * commented out, since "for local host" is not
254 * implemented here - see RFC2553 p30
256 if (flags
& NI_NOFQDN
) {
258 p
= strchr(hp
->h_name
, '.');
263 if (strlen(hp
->h_name
) + 1 > hostlen
) {
267 strlcpy(host
, hp
->h_name
, hostlen
);
270 if (flags
& NI_NAMEREQD
)
278 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
286 if (inet_ntop(afd
->a_af
, addr
, host
,
298 ip6_parsenumeric(const struct sockaddr
*sa
, const char *addr
, char *host
,
299 size_t hostlen
, int flags
)
304 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
307 numaddrlen
= strlen(numaddr
);
308 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
310 strlcpy(host
, numaddr
, hostlen
);
312 if (((const struct sockaddr_in6
*)sa
)->sin6_scope_id
) {
313 char zonebuf
[MAXHOSTNAMELEN
];
316 zonelen
= ip6_sa2str(
317 (const struct sockaddr_in6
*)(const void *)sa
,
318 zonebuf
, sizeof(zonebuf
), flags
);
321 if (zonelen
+ 1 + numaddrlen
+ 1 > hostlen
)
324 /* construct <numeric-addr><delim><zoneid> */
325 memcpy(host
+ numaddrlen
+ 1, zonebuf
,
327 host
[numaddrlen
] = SCOPE_DELIMITER
;
328 host
[numaddrlen
+ 1 + zonelen
] = '\0';
336 ip6_sa2str(const struct sockaddr_in6
*sa6
, char *buf
, size_t bufsiz
,
339 unsigned int ifindex
;
340 const struct in6_addr
*a6
;
343 ifindex
= (unsigned int)sa6
->sin6_scope_id
;
344 a6
= &sa6
->sin6_addr
;
346 if ((flags
& NI_NUMERICSCOPE
) != 0) {
347 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
348 if (n
< 0 || n
>= bufsiz
)
354 /* if_indextoname() does not take buffer size. not a good api... */
355 if ((IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
) ||
356 IN6_IS_ADDR_MC_NODELOCAL(a6
)) && bufsiz
>= IF_NAMESIZE
) {
357 char *p
= if_indextoname(ifindex
, buf
);
364 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
365 if (n
< 0 || (size_t)n
>= bufsiz
)
373 * getnameinfo_link():
374 * Format a link-layer address into a printable format, paying attention to
375 * the interface type.
379 getnameinfo_link(const struct sockaddr
*sa
, socklen_t salen __unused
,
380 char *host
, size_t hostlen
, char *serv
, size_t servlen
,
383 const struct sockaddr_dl
*sdl
=
384 (const struct sockaddr_dl
*)(const void *)sa
;
387 if (serv
!= NULL
&& servlen
> 0)
390 if (sdl
->sdl_nlen
== 0 && sdl
->sdl_alen
== 0 && sdl
->sdl_slen
== 0) {
391 n
= snprintf(host
, hostlen
, "link#%d", sdl
->sdl_index
);
399 switch (sdl
->sdl_type
) {
401 * The following have zero-length addresses.
402 * IFT_GIF (net/gif/if_gif.c)
403 * IFT_LOOP (net/if_loop.c, net/disc/if_disc.c)
404 * IFT_PPP (net/sppp/if_spppsubr.c)
405 * IFT_SLIP (net/sl/if_sl.c)
406 * IFT_STF (net/stf/if_stf.c)
407 * IFT_L2VLAN (net/vlan/if_vlan.c)
408 * IFT_BRIDGE (net/bridge/if_bridge.c)
411 * The following use IPv4 addresses as link-layer addresses:
412 * IFT_OTHER (net/gre/if_gre.c)
414 /* default below is believed correct for all these. */
421 return hexname((u_int8_t
*)LLADDR(sdl
), (size_t)sdl
->sdl_alen
,
427 hexname(const u_int8_t
*cp
, size_t len
, char *host
, size_t hostlen
)
433 for (i
= 0; i
< len
; i
++) {
434 n
= snprintf(outp
, hostlen
, "%s%02x",
435 i
? ":" : "", cp
[i
]);
436 if (n
< 0 || n
>= hostlen
) {