xref: /freebsd/lib/libc/net/ether_addr.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*
2  * Copyright (c) 1995
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * ethernet address conversion and lookup routines
33  *
34  * Written by Bill Paul <wpaul@ctr.columbia.edu>
35  * Center for Telecommunications Research
36  * Columbia University, New York City
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <stdio.h>
43 #include <paths.h>
44 #include <sys/types.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <net/ethernet.h>
50 #ifdef YP
51 #include <rpc/rpc.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
54 #endif
55 
56 #ifndef _PATH_ETHERS
57 #define _PATH_ETHERS "/etc/ethers"
58 #endif
59 
60 /*
61  * Parse a string of text containing an ethernet address and hostname
62  * and separate it into its component parts.
63  */
64 int
65 ether_line(l, e, hostname)
66 	const char *l;
67 	struct ether_addr *e;
68 	char *hostname;
69 {
70         int i, o[6];
71 
72         i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2],
73                                               &o[3], &o[4], &o[5],
74 					      hostname);
75 	if (i != 7)
76                 return (i);
77 
78         for (i=0; i<6; i++)
79                 e->octet[i] = o[i];
80         return (0);
81 }
82 
83 /*
84  * Convert an ASCII representation of an ethernet address to
85  * binary form.
86  */
87 struct
88 ether_addr *ether_aton(a)
89 	const char *a;
90 {
91         int i;
92 	static struct ether_addr o;
93 	unsigned int o0, o1, o2, o3, o4, o5;
94 
95         i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
96 
97         if (i != 6)
98                 return (NULL);
99 
100         o.octet[0]=o0;
101 	o.octet[1]=o1;
102 	o.octet[2]=o2;
103 	o.octet[3]=o3;
104 	o.octet[4]=o4;
105 	o.octet[5]=o5;
106 
107         return ((struct ether_addr *)&o);
108 }
109 
110 /*
111  * Convert a binary representation of an ethernet address to
112  * an ASCII string.
113  */
114 char
115 *ether_ntoa(n)
116 	const struct ether_addr *n;
117 {
118         int i;
119 	static char a[18];
120 
121         i = sprintf(a,"%x:%x:%x:%x:%x:%x",n->octet[0],n->octet[1],n->octet[2],
122                                           n->octet[3],n->octet[4],n->octet[5]);
123         if (i < 11)
124                 return (NULL);
125         return ((char *)&a);
126 }
127 
128 /*
129  * Map an ethernet address to a hostname. Use either /etc/ethers or
130  * NIS/YP.
131  */
132 int
133 ether_ntohost(hostname, e)
134 	char *hostname;
135 	const struct ether_addr *e;
136 {
137 	FILE *fp;
138 	char buf[BUFSIZ + 2];
139 	struct ether_addr local_ether;
140 	char local_host[MAXHOSTNAMELEN];
141 #ifdef YP
142 	char *result;
143 	int resultlen;
144 	char *ether_a;
145 	char *yp_domain;
146 #endif
147 	if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
148 		return (1);
149 
150 	while (fgets(buf,BUFSIZ,fp)) {
151 		if (buf[0] == '#')
152 			continue;
153 #ifdef YP
154 		if (buf[0] == '+') {
155 			if (yp_get_default_domain(&yp_domain))
156 				continue;
157 			ether_a = ether_ntoa(e);
158 			if (yp_match(yp_domain, "ethers.byaddr", ether_a,
159 				strlen(ether_a), &result, &resultlen)) {
160 				continue;
161 			}
162 			strncpy(buf, result, resultlen);
163 			buf[resultlen] = '\0';
164 			free(result);
165 		}
166 #endif
167 		if (!ether_line(buf, &local_ether, local_host)) {
168 			if (!bcmp((char *)&local_ether.octet[0],
169 				(char *)&e->octet[0], 6)) {
170 			/* We have a match */
171 				strcpy(hostname, local_host);
172 				fclose(fp);
173 				return(0);
174 			}
175 		}
176 	}
177 	fclose(fp);
178 	return (1);
179 }
180 
181 /*
182  * Map a hostname to an ethernet address using /etc/ethers or
183  * NIS/YP.
184  */
185 int
186 ether_hostton(hostname, e)
187 	const char *hostname;
188 	struct ether_addr *e;
189 {
190 	FILE *fp;
191 	char buf[BUFSIZ + 2];
192 	struct ether_addr local_ether;
193 	char local_host[MAXHOSTNAMELEN];
194 #ifdef YP
195 	char *result;
196 	int resultlen;
197 	char *yp_domain;
198 #endif
199 	if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
200 		return (1);
201 
202 	while (fgets(buf,BUFSIZ,fp)) {
203 		if (buf[0] == '#')
204 			continue;
205 #ifdef YP
206 		if (buf[0] == '+') {
207 			if (yp_get_default_domain(&yp_domain))
208 				continue;
209 			if (yp_match(yp_domain, "ethers.byname", hostname,
210 				strlen(hostname), &result, &resultlen)) {
211 				continue;
212 			}
213 			strncpy(buf, result, resultlen);
214 			buf[resultlen] = '\0';
215 			free(result);
216 		}
217 #endif
218 		if (!ether_line(buf, &local_ether, local_host)) {
219 			if (!strcmp(hostname, local_host)) {
220 				/* We have a match */
221 				bcopy((char *)&local_ether.octet[0],
222 					(char *)&e->octet[0], 6);
223 				fclose(fp);
224 				return(0);
225 			}
226 		}
227 	}
228 	fclose(fp);
229 	return (1);
230 }
231