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