2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995 Bill Paul <wpaul@ctr.columbia.edu>.
5 * Copyright (c) 2007 Robert N. M. Watson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 * ethernet address conversion and lookup routines
37 * Written by Bill Paul <wpaul@ctr.columbia.edu>
38 * Center for Telecommunications Research
39 * Columbia University, New York City
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/socket.h>
48 #include <net/ethernet.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
62 #define _PATH_ETHERS "/etc/ethers"
66 * Parse a string of text containing an ethernet address and hostname and
67 * separate it into its component parts.
70 ether_line(const char *l, struct ether_addr *e, char *hostname)
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);
77 for (i = 0; i < 6; i++)
86 * Convert an ASCII representation of an ethernet address to binary form.
89 ether_aton_r(const char *a, struct ether_addr *e)
92 unsigned int o0, o1, o2, o3, o4, o5;
94 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
107 ether_aton(const char *a)
109 static struct ether_addr e;
111 return (ether_aton_r(a, &e));
115 * Convert a binary representation of an ethernet address to an ASCII string.
118 ether_ntoa_r(const struct ether_addr *n, char *a)
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]);
130 ether_ntoa(const struct ether_addr *n)
134 return (ether_ntoa_r(n, a));
138 * Map an ethernet address to a hostname. Use either /etc/ethers or NIS/YP.
141 ether_ntohost(char *hostname, const struct ether_addr *e)
144 char buf[BUFSIZ + 2];
145 struct ether_addr local_ether;
146 char local_host[MAXHOSTNAMELEN];
154 if ((fp = fopen(_PATH_ETHERS, "re")) == NULL)
156 while (fgets(buf,BUFSIZ,fp)) {
161 if (yp_get_default_domain(&yp_domain))
163 ether_a = ether_ntoa(e);
164 if (yp_match(yp_domain, "ethers.byaddr", ether_a,
165 strlen(ether_a), &result, &resultlen)) {
168 strncpy(buf, result, resultlen);
169 buf[resultlen] = '\0';
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);
188 * Map a hostname to an ethernet address using /etc/ethers or NIS/YP.
191 ether_hostton(const char *hostname, struct ether_addr *e)
194 char buf[BUFSIZ + 2];
195 struct ether_addr local_ether;
196 char local_host[MAXHOSTNAMELEN];
203 if ((fp = fopen(_PATH_ETHERS, "re")) == NULL)
205 while (fgets(buf,BUFSIZ,fp)) {
210 if (yp_get_default_domain(&yp_domain))
212 if (yp_match(yp_domain, "ethers.byname", hostname,
213 strlen(hostname), &result, &resultlen)) {
216 strncpy(buf, result, resultlen);
217 buf[resultlen] = '\0';
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);