3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * ethernet address conversion and lookup routines
34 * Written by Bill Paul <wpaul@ctr.columbia.edu>
35 * Center for Telecommunications Research
36 * Columbia University, New York City
38 * $FreeBSD: src/lib/libc/net/ether_addr.c,v 1.10.2.5 2002/04/08 08:01:50 ru Exp $
39 * $DragonFly: src/lib/libcr/net/Attic/ether_addr.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
44 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <net/ethernet.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
57 #define _PATH_ETHERS "/etc/ethers"
61 * Parse a string of text containing an ethernet address and hostname
62 * and separate it into its component parts.
65 ether_line(l, e, hostname)
72 i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2],
84 * Convert an ASCII representation of an ethernet address to
88 ether_addr *ether_aton(a)
92 static struct ether_addr o;
93 unsigned int o0, o1, o2, o3, o4, o5;
95 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
107 return ((struct ether_addr *)&o);
111 * Convert a binary representation of an ethernet address to
116 const struct ether_addr *n;
121 i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
122 n->octet[0], n->octet[1], n->octet[2],
123 n->octet[3], n->octet[4], n->octet[5]);
130 * Map an ethernet address to a hostname. Use either /etc/ethers or
134 ether_ntohost(hostname, e)
136 const struct ether_addr *e;
139 char buf[BUFSIZ + 2];
140 struct ether_addr local_ether;
141 char local_host[MAXHOSTNAMELEN];
148 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
151 while (fgets(buf,BUFSIZ,fp)) {
156 if (yp_get_default_domain(&yp_domain))
158 ether_a = ether_ntoa(e);
159 if (yp_match(yp_domain, "ethers.byaddr", ether_a,
160 strlen(ether_a), &result, &resultlen)) {
163 strncpy(buf, result, resultlen);
164 buf[resultlen] = '\0';
168 if (!ether_line(buf, &local_ether, local_host)) {
169 if (!bcmp((char *)&local_ether.octet[0],
170 (char *)&e->octet[0], 6)) {
171 /* We have a match */
172 strcpy(hostname, local_host);
183 * Map a hostname to an ethernet address using /etc/ethers or
187 ether_hostton(hostname, e)
188 const char *hostname;
189 struct ether_addr *e;
192 char buf[BUFSIZ + 2];
193 struct ether_addr local_ether;
194 char local_host[MAXHOSTNAMELEN];
200 if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
203 while (fgets(buf,BUFSIZ,fp)) {
208 if (yp_get_default_domain(&yp_domain))
210 if (yp_match(yp_domain, "ethers.byname", hostname,
211 strlen(hostname), &result, &resultlen)) {
214 strncpy(buf, result, resultlen);
215 buf[resultlen] = '\0';
219 if (!ether_line(buf, &local_ether, local_host)) {
220 if (!strcmp(hostname, local_host)) {
221 /* We have a match */
222 bcopy((char *)&local_ether.octet[0],
223 (char *)&e->octet[0], 6);