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