ethers.c revision 1.17 1 1.17 mycroft /* $NetBSD: ethers.c,v 1.17 2000/01/22 22:19:14 mycroft Exp $ */
2 1.5 cgd
3 1.1 deraadt /*
4 1.1 deraadt * ethers(3N) a la Sun.
5 1.1 deraadt *
6 1.1 deraadt * Written by Roland McGrath <roland (at) frob.com> 10/14/93.
7 1.1 deraadt * Public domain.
8 1.1 deraadt */
9 1.1 deraadt
10 1.10 jtc #include "namespace.h"
11 1.15 lukem #include <sys/param.h>
12 1.1 deraadt #include <sys/socket.h>
13 1.15 lukem
14 1.1 deraadt #include <net/if.h>
15 1.8 is #include <net/if_ether.h>
16 1.1 deraadt #include <netinet/in.h>
17 1.15 lukem
18 1.15 lukem #include <assert.h>
19 1.15 lukem #include <errno.h>
20 1.1 deraadt #include <paths.h>
21 1.1 deraadt #include <stdio.h>
22 1.3 jtc #include <stdlib.h>
23 1.3 jtc #include <string.h>
24 1.15 lukem
25 1.9 christos #ifdef YP
26 1.9 christos #include <rpcsvc/ypclnt.h>
27 1.10 jtc #endif
28 1.10 jtc
29 1.10 jtc #ifdef __weak_alias
30 1.17 mycroft __weak_alias(ether_aton,_ether_aton)
31 1.17 mycroft __weak_alias(ether_hostton,_ether_hostton)
32 1.17 mycroft __weak_alias(ether_line,_ether_line)
33 1.17 mycroft __weak_alias(ether_ntoa,_ether_ntoa)
34 1.17 mycroft __weak_alias(ether_ntohost,_ether_ntohost)
35 1.9 christos #endif
36 1.1 deraadt
37 1.1 deraadt #ifndef _PATH_ETHERS
38 1.1 deraadt #define _PATH_ETHERS "/etc/ethers"
39 1.1 deraadt #endif
40 1.1 deraadt
41 1.1 deraadt char *
42 1.1 deraadt ether_ntoa(e)
43 1.1 deraadt struct ether_addr *e;
44 1.1 deraadt {
45 1.13 mycroft static char a[18];
46 1.1 deraadt
47 1.15 lukem _DIAGASSERT(e != NULL);
48 1.15 lukem
49 1.7 mrg snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
50 1.1 deraadt e->ether_addr_octet[0], e->ether_addr_octet[1],
51 1.1 deraadt e->ether_addr_octet[2], e->ether_addr_octet[3],
52 1.1 deraadt e->ether_addr_octet[4], e->ether_addr_octet[5]);
53 1.1 deraadt return a;
54 1.1 deraadt }
55 1.1 deraadt
56 1.1 deraadt struct ether_addr *
57 1.1 deraadt ether_aton(s)
58 1.11 lukem const char *s;
59 1.1 deraadt {
60 1.1 deraadt static struct ether_addr n;
61 1.1 deraadt u_int i[6];
62 1.1 deraadt
63 1.15 lukem _DIAGASSERT(s != NULL);
64 1.15 lukem
65 1.1 deraadt if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
66 1.1 deraadt &i[2], &i[3], &i[4], &i[5]) == 6) {
67 1.1 deraadt n.ether_addr_octet[0] = (u_char)i[0];
68 1.1 deraadt n.ether_addr_octet[1] = (u_char)i[1];
69 1.1 deraadt n.ether_addr_octet[2] = (u_char)i[2];
70 1.1 deraadt n.ether_addr_octet[3] = (u_char)i[3];
71 1.1 deraadt n.ether_addr_octet[4] = (u_char)i[4];
72 1.1 deraadt n.ether_addr_octet[5] = (u_char)i[5];
73 1.1 deraadt return &n;
74 1.1 deraadt }
75 1.1 deraadt return NULL;
76 1.1 deraadt }
77 1.1 deraadt
78 1.6 mikel int
79 1.1 deraadt ether_ntohost(hostname, e)
80 1.1 deraadt char *hostname;
81 1.1 deraadt struct ether_addr *e;
82 1.1 deraadt {
83 1.1 deraadt FILE *f;
84 1.11 lukem char *p;
85 1.12 mjacob size_t len;
86 1.1 deraadt struct ether_addr try;
87 1.1 deraadt #ifdef YP
88 1.1 deraadt char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
89 1.1 deraadt int trylen;
90 1.15 lukem #endif
91 1.1 deraadt
92 1.15 lukem _DIAGASSERT(hostname != NULL);
93 1.15 lukem _DIAGASSERT(e != NULL);
94 1.15 lukem
95 1.15 lukem #ifdef YP
96 1.11 lukem trylen = snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x",
97 1.1 deraadt e->ether_addr_octet[0], e->ether_addr_octet[1],
98 1.1 deraadt e->ether_addr_octet[2], e->ether_addr_octet[3],
99 1.1 deraadt e->ether_addr_octet[4], e->ether_addr_octet[5]);
100 1.1 deraadt #endif
101 1.1 deraadt
102 1.1 deraadt f = fopen(_PATH_ETHERS, "r");
103 1.11 lukem if (f == NULL)
104 1.1 deraadt return -1;
105 1.11 lukem while ((p = fgetln(f, &len)) != NULL) {
106 1.11 lukem if (p[len - 1] != '\n')
107 1.11 lukem continue; /* skip lines w/o \n */
108 1.11 lukem p[--len] = '\0';
109 1.1 deraadt #ifdef YP
110 1.1 deraadt /* A + in the file means try YP now. */
111 1.11 lukem if (len == 1 && *p == '+') {
112 1.1 deraadt char *ypbuf, *ypdom;
113 1.1 deraadt int ypbuflen;
114 1.1 deraadt
115 1.1 deraadt if (yp_get_default_domain(&ypdom))
116 1.1 deraadt continue;
117 1.1 deraadt if (yp_match(ypdom, "ethers.byaddr", trybuf,
118 1.1 deraadt trylen, &ypbuf, &ypbuflen))
119 1.1 deraadt continue;
120 1.1 deraadt if (ether_line(ypbuf, &try, hostname) == 0) {
121 1.1 deraadt free(ypbuf);
122 1.1 deraadt (void)fclose(f);
123 1.1 deraadt return 0;
124 1.1 deraadt }
125 1.1 deraadt free(ypbuf);
126 1.2 deraadt continue;
127 1.1 deraadt }
128 1.1 deraadt #endif
129 1.11 lukem if (ether_line(p, &try, hostname) == 0 &&
130 1.14 christos memcmp(&try, e, sizeof try) == 0) {
131 1.1 deraadt (void)fclose(f);
132 1.1 deraadt return 0;
133 1.1 deraadt }
134 1.1 deraadt }
135 1.1 deraadt (void)fclose(f);
136 1.1 deraadt errno = ENOENT;
137 1.1 deraadt return -1;
138 1.1 deraadt }
139 1.1 deraadt
140 1.6 mikel int
141 1.1 deraadt ether_hostton(hostname, e)
142 1.11 lukem const char *hostname;
143 1.1 deraadt struct ether_addr *e;
144 1.1 deraadt {
145 1.1 deraadt FILE *f;
146 1.11 lukem char *p;
147 1.12 mjacob size_t len;
148 1.11 lukem char try[MAXHOSTNAMELEN + 1];
149 1.1 deraadt #ifdef YP
150 1.1 deraadt int hostlen = strlen(hostname);
151 1.1 deraadt #endif
152 1.1 deraadt
153 1.15 lukem _DIAGASSERT(hostname != NULL);
154 1.15 lukem _DIAGASSERT(e != NULL);
155 1.15 lukem
156 1.1 deraadt f = fopen(_PATH_ETHERS, "r");
157 1.1 deraadt if (f==NULL)
158 1.1 deraadt return -1;
159 1.1 deraadt
160 1.11 lukem while ((p = fgetln(f, &len)) != NULL) {
161 1.11 lukem if (p[len - 1] != '\n')
162 1.11 lukem continue; /* skip lines w/o \n */
163 1.11 lukem p[--len] = '\0';
164 1.1 deraadt #ifdef YP
165 1.1 deraadt /* A + in the file means try YP now. */
166 1.11 lukem if (len == 1 && *p == '+') {
167 1.1 deraadt char *ypbuf, *ypdom;
168 1.1 deraadt int ypbuflen;
169 1.1 deraadt
170 1.1 deraadt if (yp_get_default_domain(&ypdom))
171 1.1 deraadt continue;
172 1.1 deraadt if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
173 1.1 deraadt &ypbuf, &ypbuflen))
174 1.1 deraadt continue;
175 1.1 deraadt if (ether_line(ypbuf, e, try) == 0) {
176 1.1 deraadt free(ypbuf);
177 1.1 deraadt (void)fclose(f);
178 1.1 deraadt return 0;
179 1.1 deraadt }
180 1.1 deraadt free(ypbuf);
181 1.2 deraadt continue;
182 1.1 deraadt }
183 1.1 deraadt #endif
184 1.11 lukem if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
185 1.1 deraadt (void)fclose(f);
186 1.1 deraadt return 0;
187 1.1 deraadt }
188 1.1 deraadt }
189 1.1 deraadt (void)fclose(f);
190 1.1 deraadt errno = ENOENT;
191 1.1 deraadt return -1;
192 1.1 deraadt }
193 1.1 deraadt
194 1.6 mikel int
195 1.1 deraadt ether_line(l, e, hostname)
196 1.11 lukem const char *l;
197 1.1 deraadt struct ether_addr *e;
198 1.1 deraadt char *hostname;
199 1.1 deraadt {
200 1.1 deraadt u_int i[6];
201 1.11 lukem static char buf[sizeof " %x:%x:%x:%x:%x:%x %s\\n" + 21];
202 1.11 lukem /* XXX: 21 == strlen (ASCII representation of 2^64) */
203 1.15 lukem
204 1.15 lukem _DIAGASSERT(l != NULL);
205 1.15 lukem _DIAGASSERT(e != NULL);
206 1.15 lukem _DIAGASSERT(hostname != NULL);
207 1.1 deraadt
208 1.11 lukem if (! buf[0])
209 1.11 lukem snprintf(buf, sizeof buf, " %%x:%%x:%%x:%%x:%%x:%%x %%%ds\\n",
210 1.11 lukem MAXHOSTNAMELEN);
211 1.11 lukem if (sscanf(l, buf,
212 1.11 lukem &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
213 1.1 deraadt e->ether_addr_octet[0] = (u_char)i[0];
214 1.1 deraadt e->ether_addr_octet[1] = (u_char)i[1];
215 1.1 deraadt e->ether_addr_octet[2] = (u_char)i[2];
216 1.1 deraadt e->ether_addr_octet[3] = (u_char)i[3];
217 1.1 deraadt e->ether_addr_octet[4] = (u_char)i[4];
218 1.1 deraadt e->ether_addr_octet[5] = (u_char)i[5];
219 1.1 deraadt return 0;
220 1.1 deraadt }
221 1.1 deraadt errno = EINVAL;
222 1.1 deraadt return -1;
223 1.1 deraadt }
224