2 * Copyright 2004 The Aerospace Corporation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * 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. The name of The Aerospace Corporation may not be used to endorse or
14 * promote products derived from this software.
16 * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by Bill Paul.
42 * 4. Neither the name of the author nor the names of any co-contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * EUI-64 conversion and lookup routines
61 * Converted from ether_addr.c rev
62 * FreeBSD: src/lib/libc/net/eui64.c,v 1.15 2002/04/08 07:51:10 ru Exp
65 * Written by Bill Paul <wpaul@ctr.columbia.edu>
66 * Center for Telecommunications Research
67 * Columbia University, New York City
69 * $FreeBSD: src/lib/libc/net/eui64.c,v 1.2 2004/06/01 19:30:13 brooks Exp $
74 #include <sys/types.h>
75 #include <sys/eui64.h>
78 #include <sys/param.h>
81 #include <rpcsvc/yp_prot.h>
82 #include <rpcsvc/ypclnt.h>
86 #define _PATH_EUI64 "/etc/eui64"
89 static int eui64_line(const char *l, struct eui64 *e, char *hostname,
93 * Parse a string of text containing an EUI-64 and hostname
94 * and separate it into its component parts.
97 eui64_line(const char *l, struct eui64 *e, char *hostname, size_t len)
99 char *line, *linehead, *cur;
101 linehead = strdup(l);
102 if (linehead == NULL)
106 /* Find and parse the EUI64 */
107 while ((cur = strsep(&line, " \t\r\n")) != NULL) {
109 if (eui64_aton(cur, e) == 0)
116 /* Find the hostname */
117 while ((cur = strsep(&line, " \t\r\n")) != NULL) {
119 if (strlcpy(hostname, cur, len) <= len)
126 /* Make sure what remains is either whitespace or a comment */
127 while ((cur = strsep(&line, " \t\r\n")) != NULL) {
142 * Convert an ASCII representation of an EUI-64 to binary form.
145 eui64_aton(const char *a, struct eui64 *e)
148 unsigned int o0, o1, o2, o3, o4, o5, o6, o7;
151 i = sscanf(a, "%x-%x-%x-%x-%x-%x-%x-%x",
152 &o0, &o1, &o2, &o3, &o4, &o5, &o6, &o7);
156 i = sscanf(a, "%x:%x:%x:%x:%x:%x:%x:%x",
157 &o0, &o1, &o2, &o3, &o4, &o5, &o6, &o7);
160 /* classic fwcontrol/dconschat form */
161 i = sscanf(a, "0x%2x%2x%2x%2x%2x%2x%2x%2x",
162 &o0, &o1, &o2, &o3, &o4, &o5, &o6, &o7);
166 i = sscanf(a, "%x-%x-%x-%x-%x-%x",
167 &o0, &o1, &o2, &o5, &o6, &o7);
174 i = sscanf(a, "%x:%x:%x:%x:%x:%x",
175 &o0, &o1, &o2, &o5, &o6, &o7);
198 * Convert a binary representation of an EUI-64 to an ASCII string.
201 eui64_ntoa(const struct eui64 *id, char *a, size_t len)
205 i = snprintf(a, len, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
206 id->octet[0], id->octet[1], id->octet[2], id->octet[3],
207 id->octet[4], id->octet[5], id->octet[6], id->octet[7]);
208 if (i < 23 || i >= len)
214 * Map an EUI-64 to a hostname. Use either /etc/eui64 or NIS/YP.
217 eui64_ntohost(char *hostname, size_t len __unused, const struct eui64 *id)
220 char buf[BUFSIZ + 2];
221 struct eui64 local_eui64;
222 char local_host[MAXHOSTNAMELEN];
229 if ((fp = fopen(_PATH_EUI64, "r")) == NULL)
232 while (fgets(buf,BUFSIZ,fp)) {
237 if (yp_get_default_domain(&yp_domain))
239 eui64_ntoa(id, eui64_a, sizeof(eui64_a));
240 if (yp_match(yp_domain, "eui64.byid", eui64_a,
241 strlen(eui64_a), &result, &resultlen)) {
244 strncpy(buf, result, resultlen);
245 buf[resultlen] = '\0';
249 if (eui64_line(buf, &local_eui64, local_host,
250 sizeof(local_host)) == 0) {
251 if (bcmp(&local_eui64.octet[0],
252 &id->octet[0], EUI64_LEN) == 0) {
253 /* We have a match */
254 strcpy(hostname, local_host);
265 * Map a hostname to an EUI-64 using /etc/eui64 or NIS/YP.
268 eui64_hostton(const char *hostname, struct eui64 *id)
271 char buf[BUFSIZ + 2];
272 struct eui64 local_eui64;
273 char local_host[MAXHOSTNAMELEN];
279 if ((fp = fopen(_PATH_EUI64, "r")) == NULL)
282 while (fgets(buf,BUFSIZ,fp)) {
287 if (yp_get_default_domain(&yp_domain))
289 if (yp_match(yp_domain, "eui64.byname", hostname,
290 strlen(hostname), &result, &resultlen)) {
293 strncpy(buf, result, resultlen);
294 buf[resultlen] = '\0';
298 if (eui64_line(buf, &local_eui64, local_host,
299 sizeof(local_host)) == 0) {
300 if (strcmp(hostname, local_host) == 0) {
301 /* We have a match */
302 bcopy(&local_eui64, id, sizeof(struct eui64));