]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/getnameinfo.c
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.
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.
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 $ */
15 * Copyright (c) 2000 Ben Harris.
16 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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.
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
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.
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 */
65 #include "namespace.h"
66 #include <sys/types.h>
67 #include <sys/socket.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>
82 #include <net/servent.h>
84 #define CLLADDR(x) ( LLADDR(x) )
85 #define endservent_r(svd) endservent()
86 #define getservbyport_r(Port,pProto,pSv,pSvd) getservbyport(Port,pProto)
89 __weak_alias(getnameinfo
,_getnameinfo
)
101 static const struct afd
{
108 {PF_INET6
, sizeof(struct in6_addr
), sizeof(struct sockaddr_in6
),
109 offsetof(struct sockaddr_in6
, sin6_addr
)},
111 {PF_INET
, sizeof(struct in_addr
), sizeof(struct sockaddr_in
),
112 offsetof(struct sockaddr_in
, sin_addr
)},
122 static int getnameinfo_inet
__P((const struct sockaddr
*, socklen_t
, char *,
123 socklen_t
, char *, socklen_t
, int));
125 static int ip6_parsenumeric
__P((const struct sockaddr
*, const char *, char *,
127 static int ip6_sa2str
__P((const struct sockaddr_in6
*, char *, size_t,
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
));
135 * Top-level getnameinfo() code. Look at the address family, and pick an
136 * appropriate function to call.
140 const struct sockaddr
* sa
,
150 switch (sa
->sa_family
) {
153 return getnameinfo_inet(sa
, salen
, host
, hostlen
,
154 serv
, servlen
, flags
);
156 return getnameinfo_link(sa
, salen
, host
, hostlen
,
157 serv
, servlen
, flags
);
165 * getnameinfo_inet():
166 * Format an IPv4 or IPv6 sockaddr into a printable string.
171 const struct sockaddr
* sa
,
180 const struct afd
*afd
;
190 /* sa is checked below */
191 /* host may be NULL */
192 /* serv may be NULL */
198 if (sa
->sa_len
!= salen
)
202 family
= sa
->sa_family
;
203 for (i
= 0; afdl
[i
].a_af
; i
++)
204 if (afdl
[i
].a_af
== family
) {
211 if (salen
!= afd
->a_socklen
)
214 /* network byte order */
215 port
= ((const struct sockinet
*)(const void *)sa
)->si_port
;
216 addr
= (const char *)(const void *)sa
+ afd
->a_off
;
218 if (serv
== NULL
|| servlen
== 0) {
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.
226 if (flags
& NI_NUMERICSERV
)
229 struct servent_data svd
;
230 // struct servent sv;
232 (void)memset(&svd
, 0, sizeof(svd
));
233 sp
= getservbyport_r(port
,
234 (flags
& NI_DGRAM
) ? "udp" : "tcp", &sv
, &svd
);
238 if (strlen(sp
->s_name
) + 1 > servlen
)
240 strlcpy(serv
, sp
->s_name
, servlen
);
242 snprintf(numserv
, sizeof(numserv
), "%u", ntohs(port
));
243 if (strlen(numserv
) + 1 > servlen
)
245 strlcpy(serv
, numserv
, servlen
);
249 switch (sa
->sa_family
) {
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
;
258 flags
|= NI_NUMERICHOST
;
263 const struct sockaddr_in6
*sin6
;
264 sin6
= (const struct sockaddr_in6
*)(const void *)sa
;
265 switch (sin6
->sin6_addr
.s6_addr
[0]) {
267 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
269 else if (IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
))
272 flags
|= NI_NUMERICHOST
;
275 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
276 flags
|= NI_NUMERICHOST
;
278 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
))
279 flags
|= NI_NUMERICHOST
;
286 if (host
== NULL
|| hostlen
== 0) {
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.
293 } else if (flags
& NI_NUMERICHOST
) {
296 /* NUMERICHOST and NAMEREQD conflicts with each other */
297 if (flags
& NI_NAMEREQD
)
306 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
307 hostlen
, flags
)) != 0)
313 if (inet_ntop(afd
->a_af
, addr
, numaddr
, sizeof(numaddr
))
316 numaddrlen
= strlen(numaddr
);
317 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
319 strlcpy(host
, numaddr
, hostlen
);
323 hp
= gethostbyaddr(addr
, afd
->a_addrlen
, afd
->a_af
);
328 * commented out, since "for local host" is not
329 * implemented here - see RFC2553 p30
331 if (flags
& NI_NOFQDN
) {
333 p
= strchr(hp
->h_name
, '.');
338 if (strlen(hp
->h_name
) + 1 > hostlen
) {
341 strlcpy(host
, hp
->h_name
, hostlen
);
343 if (flags
& NI_NAMEREQD
)
351 if ((error
= ip6_parsenumeric(sa
, addr
, host
,
359 if (inet_ntop(afd
->a_af
, addr
, host
,
372 const struct sockaddr
*sa
,
382 _DIAGASSERT(sa
!= NULL
);
383 _DIAGASSERT(addr
!= NULL
);
384 _DIAGASSERT(host
!= NULL
);
386 if (inet_ntop(AF_INET6
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
389 numaddrlen
= strlen(numaddr
);
390 if (numaddrlen
+ 1 > hostlen
) /* don't forget terminator */
392 strlcpy(host
, numaddr
, hostlen
);
394 if (((const struct sockaddr_in6
*)(const void *)sa
)->sin6_scope_id
) {
395 char zonebuf
[MAXHOSTNAMELEN
];
398 zonelen
= ip6_sa2str(
399 (const struct sockaddr_in6
*)(const void *)sa
,
400 zonebuf
, sizeof(zonebuf
), flags
);
403 if ((size_t) zonelen
+ 1 + numaddrlen
+ 1 > hostlen
)
405 /* construct <numeric-addr><delim><zoneid> */
406 memcpy(host
+ numaddrlen
+ 1, zonebuf
,
408 host
[numaddrlen
] = SCOPE_DELIMITER
;
409 host
[numaddrlen
+ 1 + zonelen
] = '\0';
418 const struct sockaddr_in6
*sa6
,
425 unsigned int ifindex
;
426 const struct in6_addr
*a6
;
430 _DIAGASSERT(sa6
!= NULL
);
431 _DIAGASSERT(buf
!= NULL
);
434 ifindex
= (unsigned int)sa6
->sin6_scope_id
;
435 a6
= &sa6
->sin6_addr
;
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
))
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
);
460 n
= snprintf(buf
, bufsiz
, "%u", sa6
->sin6_scope_id
);
461 if (n
< 0 || (size_t) n
>= bufsiz
)
470 * getnameinfo_link():
471 * Format a link-layer address into a printable format, paying attention to
472 * the interface type.
478 const struct sockaddr
* sa
,
487 const struct sockaddr_dl
*sdl
=
488 (const struct sockaddr_dl
*)(const void *)sa
;
489 // const struct ieee1394_hwaddr *iha;
492 if (serv
!= NULL
&& servlen
> 0)
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
) {
505 switch (sdl
->sdl_type
) {
508 if (sdl
->sdl_alen
< 2)
510 if (CLLADDR(sdl
)[1] == 0)
511 n
= snprintf(host
, hostlen
, "%u", CLLADDR(sdl
)[0]);
513 n
= snprintf(host
, hostlen
, "%u.%u",
514 CLLADDR(sdl
)[1], CLLADDR(sdl
)[0]);
515 if (n
< 0 || (socklen_t
) n
>= hostlen
) {
522 if (sdl
->sdl_alen
< sizeof(iha
->iha_uid
))
525 (const struct ieee1394_hwaddr
*)(const void *)CLLADDR(sdl
);
526 return hexname(iha
->iha_uid
, sizeof(iha
->iha_uid
),
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>
541 * The following use IPv4 addresses as link-layer addresses:
542 * IFT_OTHER (net/if_gre.c)
544 case IFT_ARCNET
: /* default below is believed correct for all these. */
551 return hexname((const u_int8_t
*)CLLADDR(sdl
),
552 (size_t)sdl
->sdl_alen
, host
, hostlen
);
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
) {