xref: /openbsd-src/lib/libc/net/ethers.c (revision ceeee15711af8438df1d3511cb2d95675a664c27)
1 /*	$NetBSD: ethers.c,v 1.5 1995/02/25 06:20:28 cgd Exp $	*/
2 
3 /*
4  * ethers(3N) a la Sun.
5  *
6  * Written by Roland McGrath <roland@frob.com> 10/14/93.
7  * Public domain.
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 char *
27 ether_ntoa(e)
28 	struct ether_addr *e;
29 {
30 	static char a[] = "xx:xx:xx:xx:xx:xx";
31 
32 	sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
33 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
34 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
35 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
36 	return a;
37 }
38 
39 struct ether_addr *
40 ether_aton(s)
41 	char *s;
42 {
43 	static struct ether_addr n;
44 	u_int i[6];
45 
46 	if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
47 	    &i[2], &i[3], &i[4], &i[5]) == 6) {
48 		n.ether_addr_octet[0] = (u_char)i[0];
49 		n.ether_addr_octet[1] = (u_char)i[1];
50 		n.ether_addr_octet[2] = (u_char)i[2];
51 		n.ether_addr_octet[3] = (u_char)i[3];
52 		n.ether_addr_octet[4] = (u_char)i[4];
53 		n.ether_addr_octet[5] = (u_char)i[5];
54 		return &n;
55 	}
56 	return NULL;
57 }
58 
59 int
60 ether_ntohost(hostname, e)
61 	char *hostname;
62 	struct ether_addr *e;
63 {
64 	FILE *f;
65 	char buf[BUFSIZ];
66 	struct ether_addr try;
67 
68 #ifdef YP
69 	char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
70 	int trylen;
71 
72 	sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
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 	trylen = strlen(trybuf);
77 #endif
78 
79 	f = fopen(_PATH_ETHERS, "r");
80 	if (f==NULL)
81 		return -1;
82 	while (fgets(buf, sizeof buf, f)) {
83 #ifdef YP
84 		/* A + in the file means try YP now.  */
85 		if (!strncmp(buf, "+\n", sizeof buf)) {
86 			char *ypbuf, *ypdom;
87 			int ypbuflen;
88 
89 			if (yp_get_default_domain(&ypdom))
90 				continue;
91 			if (yp_match(ypdom, "ethers.byaddr", trybuf,
92 			    trylen, &ypbuf, &ypbuflen))
93 				continue;
94 			if (ether_line(ypbuf, &try, hostname) == 0) {
95 				free(ypbuf);
96 				(void)fclose(f);
97 				return 0;
98 			}
99 			free(ypbuf);
100 			continue;
101 		}
102 #endif
103 		if (ether_line(buf, &try, hostname) == 0 &&
104 		    bcmp((char *)&try, (char *)e, sizeof try) == 0) {
105 			(void)fclose(f);
106 			return 0;
107 		}
108 	}
109 	(void)fclose(f);
110 	errno = ENOENT;
111 	return -1;
112 }
113 
114 int
115 ether_hostton(hostname, e)
116 	char *hostname;
117 	struct ether_addr *e;
118 {
119 	FILE *f;
120 	char buf[BUFSIZ];
121 	char try[MAXHOSTNAMELEN];
122 #ifdef YP
123 	int hostlen = strlen(hostname);
124 #endif
125 
126 	f = fopen(_PATH_ETHERS, "r");
127 	if (f==NULL)
128 		return -1;
129 
130 	while (fgets(buf, sizeof buf, f)) {
131 #ifdef YP
132 		/* A + in the file means try YP now.  */
133 		if (!strncmp(buf, "+\n", sizeof buf)) {
134 			char *ypbuf, *ypdom;
135 			int ypbuflen;
136 
137 			if (yp_get_default_domain(&ypdom))
138 				continue;
139 			if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
140 			    &ypbuf, &ypbuflen))
141 				continue;
142 			if (ether_line(ypbuf, e, try) == 0) {
143 				free(ypbuf);
144 				(void)fclose(f);
145 				return 0;
146 			}
147 			free(ypbuf);
148 			continue;
149 		}
150 #endif
151 		if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
152 			(void)fclose(f);
153 			return 0;
154 		}
155 	}
156 	(void)fclose(f);
157 	errno = ENOENT;
158 	return -1;
159 }
160 
161 int
162 ether_line(l, e, hostname)
163 	char *l;
164 	struct ether_addr *e;
165 	char *hostname;
166 {
167 	u_int i[6];
168 
169 	if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1],
170 	    &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
171 		e->ether_addr_octet[0] = (u_char)i[0];
172 		e->ether_addr_octet[1] = (u_char)i[1];
173 		e->ether_addr_octet[2] = (u_char)i[2];
174 		e->ether_addr_octet[3] = (u_char)i[3];
175 		e->ether_addr_octet[4] = (u_char)i[4];
176 		e->ether_addr_octet[5] = (u_char)i[5];
177 		return 0;
178 	}
179 	errno = EINVAL;
180 	return -1;
181 }
182