2 * Copyright (c) 1995 Bill Paul <wpaul@ctr.columbia.edu>.
3 * Copyright (c) 2007 Robert N. M. Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * ethernet address conversion and lookup routines
35 * Written by Bill Paul <wpaul@ctr.columbia.edu>
36 * Center for Telecommunications Research
37 * Columbia University, New York City
39 * $FreeBSD: src/lib/libc/net/ether_addr.c,v 1.17 2007/05/13 13:57:44 rwatson Exp $
40 * $DragonFly: src/lib/libc/net/ether_addr.c,v 1.4 2005/11/13 02:04:47 swildner Exp $
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
47 #include <net/ethernet.h>
51 #include <rpcsvc/yp_prot.h>
52 #include <rpcsvc/ypclnt.h>
61 #define _PATH_ETHERS "/etc/ethers"
65 * Parse a string of text containing an ethernet address and hostname and
66 * separate it into its component parts.
69 ether_line(const char *l
, struct ether_addr
*e
, char *hostname
)
73 i
= sscanf(l
, "%x:%x:%x:%x:%x:%x %s", &o
[0], &o
[1], &o
[2], &o
[3],
74 &o
[4], &o
[5], hostname
);
83 * Convert an ASCII representation of an ethernet address to binary form.
86 ether_aton_r(const char *a
, struct ether_addr
*e
)
89 unsigned int o0
, o1
, o2
, o3
, o4
, o5
;
91 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x", &o0
, &o1
, &o2
, &o3
, &o4
, &o5
);
104 ether_aton(const char *a
)
106 static struct ether_addr e
;
108 return (ether_aton_r(a
, &e
));
112 * Convert a binary representation of an ethernet address to an ASCII string.
115 ether_ntoa_r(const struct ether_addr
*n
, char *a
)
119 i
= sprintf(a
, "%02x:%02x:%02x:%02x:%02x:%02x", n
->octet
[0],
120 n
->octet
[1], n
->octet
[2], n
->octet
[3], n
->octet
[4], n
->octet
[5]);
127 ether_ntoa(const struct ether_addr
*n
)
131 return (ether_ntoa_r(n
, a
));
135 * Map an ethernet address to a hostname. Use either /etc/ethers or NIS/YP.
138 ether_ntohost(char *hostname
, const struct ether_addr
*e
)
141 char buf
[BUFSIZ
+ 2];
142 struct ether_addr local_ether
;
143 char local_host
[MAXHOSTNAMELEN
];
151 if ((fp
= fopen(_PATH_ETHERS
, "r")) == NULL
)
153 while (fgets(buf
,BUFSIZ
,fp
)) {
158 if (yp_get_default_domain(&yp_domain
))
160 ether_a
= ether_ntoa(e
);
161 if (yp_match(yp_domain
, "ethers.byaddr", ether_a
,
162 strlen(ether_a
), &result
, &resultlen
)) {
165 strncpy(buf
, result
, resultlen
);
166 buf
[resultlen
] = '\0';
170 if (!ether_line(buf
, &local_ether
, local_host
)) {
171 if (!bcmp((char *)&local_ether
.octet
[0],
172 (char *)&e
->octet
[0], 6)) {
173 /* We have a match. */
174 strcpy(hostname
, local_host
);
185 * Map a hostname to an ethernet address using /etc/ethers or NIS/YP.
188 ether_hostton(const char *hostname
, struct ether_addr
*e
)
191 char buf
[BUFSIZ
+ 2];
192 struct ether_addr local_ether
;
193 char local_host
[MAXHOSTNAMELEN
];
200 if ((fp
= fopen(_PATH_ETHERS
, "r")) == NULL
)
202 while (fgets(buf
,BUFSIZ
,fp
)) {
207 if (yp_get_default_domain(&yp_domain
))
209 if (yp_match(yp_domain
, "ethers.byname", hostname
,
210 strlen(hostname
), &result
, &resultlen
)) {
213 strncpy(buf
, result
, resultlen
);
214 buf
[resultlen
] = '\0';
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);