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