]> git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/getnameinfo_link.c
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / lookup.subproj / getnameinfo_link.c
1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * Copyright (c) 2000 Ben Harris.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <netdb.h>
34 #include <net/if_types.h>
35 #include <net/if_dl.h>
36
37 static int
38 hexname(const uint8_t *cp, size_t len, char *host, size_t hostlen)
39 {
40 int i, n;
41 char *outp = host;
42
43 *outp = '\0';
44 for (i = 0; i < len; i++)
45 {
46 n = snprintf(outp, hostlen, "%s%02x", i ? ":" : "", cp[i]);
47 if ((n < 0) || (n >= hostlen))
48 {
49 *host = '\0';
50 return EAI_MEMORY;
51 }
52
53 outp += n;
54 hostlen -= n;
55 }
56
57 return 0;
58 }
59
60 /*
61 * getnameinfo_link():
62 * Format a link-layer address into a printable format, paying attention to
63 * the interface type.
64 */
65 __private_extern__ int
66 getnameinfo_link(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
67 {
68 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)(const void *)sa;
69 int n;
70
71 if (serv != NULL && servlen > 0) *serv = '\0';
72
73 if ((sdl->sdl_nlen == 0) && (sdl->sdl_alen == 0) && (sdl->sdl_slen == 0))
74 {
75 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
76 if (n > hostlen)
77 {
78 *host = '\0';
79 return EAI_MEMORY;
80 }
81
82 return 0;
83 }
84
85 switch (sdl->sdl_type)
86 {
87 /*
88 * The following have zero-length addresses.
89 * IFT_ATM (net/if_atmsubr.c)
90 * IFT_FAITH (net/if_faith.c)
91 * IFT_GIF (net/if_gif.c)
92 * IFT_LOOP (net/if_loop.c)
93 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
94 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
95 * IFT_STF (net/if_stf.c)
96 * IFT_L2VLAN (net/if_vlan.c)
97 * IFT_BRIDGE (net/if_bridge.h>
98 */
99 /*
100 * The following use IPv4 addresses as link-layer addresses:
101 * IFT_OTHER (net/if_gre.c)
102 * IFT_OTHER (netinet/ip_ipip.c)
103 */
104 /* default below is believed correct for all these. */
105 case IFT_ARCNET:
106 case IFT_ETHER:
107 case IFT_FDDI:
108 case IFT_HIPPI:
109 case IFT_ISO88025:
110 default:
111 return hexname((uint8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen, host, hostlen);
112 }
113
114 /* NOTREACHED */
115 return EAI_FAIL;
116 }