ethers.c revision 1.2 1 /*
2 * ethers(3N) a la Sun.
3 *
4 * Written by Roland McGrath <roland (at) frob.com> 10/14/93.
5 * Public domain.
6 *
7 * $Id: ethers.c,v 1.2 1993/12/16 19:19:14 deraadt Exp $
8 */
9
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <net/if.h>
13 #include <netinet/in.h>
14 #include <netinet/if_ether.h>
15 #include <sys/param.h>
16 #include <paths.h>
17 #include <errno.h>
18 #include <stdio.h>
19
20 #ifndef _PATH_ETHERS
21 #define _PATH_ETHERS "/etc/ethers"
22 #endif
23
24 /*
25 * Sun uses this structure in netinet/if_ether.h.
26 * It looked like it would be harmless to change that file,
27 * but I didn't want to bother and struct ether_addr and u_char[6]
28 * are layed out identically in memory anyway.
29 */
30 struct ether_addr {
31 u_char ether_addr_octet[6];
32 };
33
34 char *
35 ether_ntoa(e)
36 struct ether_addr *e;
37 {
38 static char a[] = "xx:xx:xx:xx:xx:xx";
39
40 sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
41 e->ether_addr_octet[0], e->ether_addr_octet[1],
42 e->ether_addr_octet[2], e->ether_addr_octet[3],
43 e->ether_addr_octet[4], e->ether_addr_octet[5]);
44 return a;
45 }
46
47 struct ether_addr *
48 ether_aton(s)
49 char *s;
50 {
51 static struct ether_addr n;
52 u_int i[6];
53
54 if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
55 &i[2], &i[3], &i[4], &i[5]) == 6) {
56 n.ether_addr_octet[0] = (u_char)i[0];
57 n.ether_addr_octet[1] = (u_char)i[1];
58 n.ether_addr_octet[2] = (u_char)i[2];
59 n.ether_addr_octet[3] = (u_char)i[3];
60 n.ether_addr_octet[4] = (u_char)i[4];
61 n.ether_addr_octet[5] = (u_char)i[5];
62 return &n;
63 }
64 return NULL;
65 }
66
67 ether_ntohost(hostname, e)
68 char *hostname;
69 struct ether_addr *e;
70 {
71 FILE *f;
72 char buf[BUFSIZ];
73 struct ether_addr try;
74
75 #ifdef YP
76 char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
77 int trylen;
78
79 sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
80 e->ether_addr_octet[0], e->ether_addr_octet[1],
81 e->ether_addr_octet[2], e->ether_addr_octet[3],
82 e->ether_addr_octet[4], e->ether_addr_octet[5]);
83 trylen = strlen(trybuf);
84 #endif
85
86 f = fopen(_PATH_ETHERS, "r");
87 if (f==NULL)
88 return -1;
89 while (fgets(buf, sizeof buf, f)) {
90 #ifdef YP
91 /* A + in the file means try YP now. */
92 if (!strncmp(buf, "+\n", sizeof buf)) {
93 char *ypbuf, *ypdom;
94 int ypbuflen;
95
96 if (yp_get_default_domain(&ypdom))
97 continue;
98 if (yp_match(ypdom, "ethers.byaddr", trybuf,
99 trylen, &ypbuf, &ypbuflen))
100 continue;
101 if (ether_line(ypbuf, &try, hostname) == 0) {
102 free(ypbuf);
103 (void)fclose(f);
104 return 0;
105 }
106 free(ypbuf);
107 continue;
108 }
109 #endif
110 if (ether_line(buf, &try, hostname) == 0 &&
111 bcmp((char *)&try, (char *)e, sizeof try) == 0) {
112 (void)fclose(f);
113 return 0;
114 }
115 }
116 (void)fclose(f);
117 errno = ENOENT;
118 return -1;
119 }
120
121 ether_hostton(hostname, e)
122 char *hostname;
123 struct ether_addr *e;
124 {
125 FILE *f;
126 char buf[BUFSIZ];
127 char try[MAXHOSTNAMELEN];
128 #ifdef YP
129 int hostlen = strlen(hostname);
130 #endif
131
132 f = fopen(_PATH_ETHERS, "r");
133 if (f==NULL)
134 return -1;
135
136 while (fgets(buf, sizeof buf, f)) {
137 #ifdef YP
138 /* A + in the file means try YP now. */
139 if (!strncmp(buf, "+\n", sizeof buf)) {
140 char *ypbuf, *ypdom;
141 int ypbuflen;
142
143 if (yp_get_default_domain(&ypdom))
144 continue;
145 if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
146 &ypbuf, &ypbuflen))
147 continue;
148 if (ether_line(ypbuf, e, try) == 0) {
149 free(ypbuf);
150 (void)fclose(f);
151 return 0;
152 }
153 free(ypbuf);
154 continue;
155 }
156 #endif
157 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
158 (void)fclose(f);
159 return 0;
160 }
161 }
162 (void)fclose(f);
163 errno = ENOENT;
164 return -1;
165 }
166
167 ether_line(l, e, hostname)
168 char *l;
169 struct ether_addr *e;
170 char *hostname;
171 {
172 u_int i[6];
173
174 if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1],
175 &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
176 e->ether_addr_octet[0] = (u_char)i[0];
177 e->ether_addr_octet[1] = (u_char)i[1];
178 e->ether_addr_octet[2] = (u_char)i[2];
179 e->ether_addr_octet[3] = (u_char)i[3];
180 e->ether_addr_octet[4] = (u_char)i[4];
181 e->ether_addr_octet[5] = (u_char)i[5];
182 return 0;
183 }
184 errno = EINVAL;
185 return -1;
186 }
187