xref: /openbsd/lib/libc/net/ethers.c (revision df930be7)
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 ether_ntohost(hostname, e)
60 	char *hostname;
61 	struct ether_addr *e;
62 {
63 	FILE *f;
64 	char buf[BUFSIZ];
65 	struct ether_addr try;
66 
67 #ifdef YP
68 	char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
69 	int trylen;
70 
71 	sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
72 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
73 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
74 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
75 	trylen = strlen(trybuf);
76 #endif
77 
78 	f = fopen(_PATH_ETHERS, "r");
79 	if (f==NULL)
80 		return -1;
81 	while (fgets(buf, sizeof buf, f)) {
82 #ifdef YP
83 		/* A + in the file means try YP now.  */
84 		if (!strncmp(buf, "+\n", sizeof buf)) {
85 			char *ypbuf, *ypdom;
86 			int ypbuflen;
87 
88 			if (yp_get_default_domain(&ypdom))
89 				continue;
90 			if (yp_match(ypdom, "ethers.byaddr", trybuf,
91 			    trylen, &ypbuf, &ypbuflen))
92 				continue;
93 			if (ether_line(ypbuf, &try, hostname) == 0) {
94 				free(ypbuf);
95 				(void)fclose(f);
96 				return 0;
97 			}
98 			free(ypbuf);
99 			continue;
100 		}
101 #endif
102 		if (ether_line(buf, &try, hostname) == 0 &&
103 		    bcmp((char *)&try, (char *)e, sizeof try) == 0) {
104 			(void)fclose(f);
105 			return 0;
106 		}
107 	}
108 	(void)fclose(f);
109 	errno = ENOENT;
110 	return -1;
111 }
112 
113 ether_hostton(hostname, e)
114 	char *hostname;
115 	struct ether_addr *e;
116 {
117 	FILE *f;
118 	char buf[BUFSIZ];
119 	char try[MAXHOSTNAMELEN];
120 #ifdef YP
121 	int hostlen = strlen(hostname);
122 #endif
123 
124 	f = fopen(_PATH_ETHERS, "r");
125 	if (f==NULL)
126 		return -1;
127 
128 	while (fgets(buf, sizeof buf, f)) {
129 #ifdef YP
130 		/* A + in the file means try YP now.  */
131 		if (!strncmp(buf, "+\n", sizeof buf)) {
132 			char *ypbuf, *ypdom;
133 			int ypbuflen;
134 
135 			if (yp_get_default_domain(&ypdom))
136 				continue;
137 			if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
138 			    &ypbuf, &ypbuflen))
139 				continue;
140 			if (ether_line(ypbuf, e, try) == 0) {
141 				free(ypbuf);
142 				(void)fclose(f);
143 				return 0;
144 			}
145 			free(ypbuf);
146 			continue;
147 		}
148 #endif
149 		if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
150 			(void)fclose(f);
151 			return 0;
152 		}
153 	}
154 	(void)fclose(f);
155 	errno = ENOENT;
156 	return -1;
157 }
158 
159 ether_line(l, e, hostname)
160 	char *l;
161 	struct ether_addr *e;
162 	char *hostname;
163 {
164 	u_int i[6];
165 
166 	if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1],
167 	    &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
168 		e->ether_addr_octet[0] = (u_char)i[0];
169 		e->ether_addr_octet[1] = (u_char)i[1];
170 		e->ether_addr_octet[2] = (u_char)i[2];
171 		e->ether_addr_octet[3] = (u_char)i[3];
172 		e->ether_addr_octet[4] = (u_char)i[4];
173 		e->ether_addr_octet[5] = (u_char)i[5];
174 		return 0;
175 	}
176 	errno = EINVAL;
177 	return -1;
178 }
179