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