summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/ethers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/ethers.c')
-rw-r--r--src/lib/libc/net/ethers.c270
1 files changed, 270 insertions, 0 deletions
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c
new file mode 100644
index 0000000000..9df876b6f4
--- /dev/null
+++ b/src/lib/libc/net/ethers.c
@@ -0,0 +1,270 @@
1/* $OpenBSD: ethers.c,v 1.9 1998/06/21 22:13:44 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * ethers(3) a la Sun.
32 * Originally Written by Roland McGrath <roland@frob.com> 10/14/93.
33 * Substantially modified by Todd C. Miller <Todd.Miller@courtesan.com>
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char rcsid[] = "$OpenBSD: ethers.c,v 1.9 1998/06/21 22:13:44 millert Exp $";
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <net/if.h>
43#include <netinet/in.h>
44#include <netinet/if_ether.h>
45#include <sys/param.h>
46#include <paths.h>
47#include <errno.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <ctype.h>
52
53#ifndef _PATH_ETHERS
54#define _PATH_ETHERS "/etc/ethers"
55#endif
56
57static char * _ether_aton __P((char *, struct ether_addr *));
58
59char *
60ether_ntoa(e)
61 struct ether_addr *e;
62{
63 static char a[] = "xx:xx:xx:xx:xx:xx";
64
65 if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF ||
66 e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF ||
67 e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) {
68 errno = EINVAL;
69 return (NULL);
70 }
71
72 (void)sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
73 e->ether_addr_octet[0], e->ether_addr_octet[1],
74 e->ether_addr_octet[2], e->ether_addr_octet[3],
75 e->ether_addr_octet[4], e->ether_addr_octet[5]);
76
77 return (a);
78}
79
80static char *
81_ether_aton(s, e)
82 char *s;
83 struct ether_addr *e;
84{
85 int i;
86 long l;
87 char *pp;
88
89 while (isspace(*s))
90 s++;
91
92 /* expect 6 hex octets separated by ':' or space/NUL if last octet */
93 for (i = 0; i < 6; i++) {
94 l = strtol(s, &pp, 16);
95 if (pp == s || l > 0xFF)
96 return (NULL);
97 if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0'))))
98 return (NULL);
99 e->ether_addr_octet[i] = (u_char)l;
100 s = pp + 1;
101 }
102
103 /* return character after the octets ala strtol(3) */
104 return (pp);
105}
106
107struct ether_addr *
108ether_aton(s)
109 char *s;
110{
111 static struct ether_addr n;
112
113 return (_ether_aton(s, &n) ? &n : NULL);
114}
115
116int
117ether_ntohost(hostname, e)
118 char *hostname;
119 struct ether_addr *e;
120{
121 FILE *f;
122 char buf[BUFSIZ+1], *p;
123 size_t len;
124 struct ether_addr try;
125#ifdef YP
126 char trybuf[sizeof("xx:xx:xx:xx:xx:xx")];
127 int trylen;
128#endif
129
130 if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF ||
131 e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF ||
132 e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) {
133 errno = EINVAL;
134 return (-1);
135 }
136
137#ifdef YP
138 sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
139 e->ether_addr_octet[0], e->ether_addr_octet[1],
140 e->ether_addr_octet[2], e->ether_addr_octet[3],
141 e->ether_addr_octet[4], e->ether_addr_octet[5]);
142 trylen = strlen(trybuf);
143#endif
144
145 f = fopen(_PATH_ETHERS, "r");
146 if (f == NULL)
147 return (-1);
148 while ((p = fgetln(f, &len)) != NULL) {
149 if (p[len-1] == '\n')
150 len--;
151 if (len > sizeof(buf) - 2)
152 continue;
153 (void)memcpy(buf, p, len);
154 buf[len] = '\n'; /* code assumes newlines later on */
155 buf[len+1] = '\0';
156#ifdef YP
157 /* A + in the file means try YP now. */
158 if (!strncmp(buf, "+\n", sizeof(buf))) {
159 char *ypbuf, *ypdom;
160 int ypbuflen;
161
162 if (yp_get_default_domain(&ypdom))
163 continue;
164 if (yp_match(ypdom, "ethers.byaddr", trybuf,
165 trylen, &ypbuf, &ypbuflen))
166 continue;
167 if (ether_line(ypbuf, &try, hostname) == 0) {
168 free(ypbuf);
169 (void)fclose(f);
170 return (0);
171 }
172 free(ypbuf);
173 continue;
174 }
175#endif
176 if (ether_line(buf, &try, hostname) == 0 &&
177 memcmp((void *)&try, (void *)e, sizeof(try)) == 0) {
178 (void)fclose(f);
179 return (0);
180 }
181 }
182 (void)fclose(f);
183 errno = ENOENT;
184 return (-1);
185}
186
187int
188ether_hostton(hostname, e)
189 char *hostname;
190 struct ether_addr *e;
191{
192 FILE *f;
193 char buf[BUFSIZ+1], *p;
194 char try[MAXHOSTNAMELEN];
195 size_t len;
196#ifdef YP
197 int hostlen = strlen(hostname);
198#endif
199
200 f = fopen(_PATH_ETHERS, "r");
201 if (f==NULL)
202 return (-1);
203
204 while ((p = fgetln(f, &len)) != NULL) {
205 if (p[len-1] == '\n')
206 len--;
207 if (len > sizeof(buf) - 2)
208 continue;
209 memcpy(buf, p, len);
210 buf[len] = '\n'; /* code assumes newlines later on */
211 buf[len+1] = '\0';
212#ifdef YP
213 /* A + in the file means try YP now. */
214 if (!strncmp(buf, "+\n", sizeof(buf))) {
215 char *ypbuf, *ypdom;
216 int ypbuflen;
217
218 if (yp_get_default_domain(&ypdom))
219 continue;
220 if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
221 &ypbuf, &ypbuflen))
222 continue;
223 if (ether_line(ypbuf, e, try) == 0) {
224 free(ypbuf);
225 (void)fclose(f);
226 return (0);
227 }
228 free(ypbuf);
229 continue;
230 }
231#endif
232 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
233 (void)fclose(f);
234 return (0);
235 }
236 }
237 (void)fclose(f);
238 errno = ENOENT;
239 return (-1);
240}
241
242int
243ether_line(line, e, hostname)
244 char *line;
245 struct ether_addr *e;
246 char *hostname;
247{
248 char *p;
249 size_t n;
250
251 /* Parse "xx:xx:xx:xx:xx:xx" */
252 if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t'))
253 goto bad;
254
255 /* Now get the hostname */
256 while (isspace(*p))
257 p++;
258 if (*p == '\0')
259 goto bad;
260 n = strcspn(p, " \t\n");
261 if (n >= MAXHOSTNAMELEN)
262 goto bad;
263 (void)strncpy(hostname, p, n);
264 hostname[n] = '\0';
265 return (0);
266
267bad:
268 errno = EINVAL;
269 return (-1);
270}