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 * $DragonFly: src/lib/libc/net/eui64.c,v 1.1 2004/09/23 06:33:04 simokawa Exp $
72 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD: src/lib/libc/net/eui64.c,v 1.2 2004/06/01 19:30:13 brooks Exp $");
79 #include <sys/types.h>
80 #include <sys/eui64.h>
83 #include <sys/param.h>
86 #include <rpcsvc/yp_prot.h>
87 #include <rpcsvc/ypclnt.h>
91 #define _PATH_EUI64 "/etc/eui64"
94 static int eui64_line(const char *l
, struct eui64
*e
, char *hostname
,
98 * Parse a string of text containing an EUI-64 and hostname
99 * and separate it into its component parts.
102 eui64_line(const char *l
, struct eui64
*e
, char *hostname
, size_t len
)
104 char *line
, *linehead
, *cur
;
106 linehead
= strdup(l
);
107 if (linehead
== NULL
)
111 /* Find and parse the EUI64 */
112 while ((cur
= strsep(&line
, " \t\r\n")) != NULL
) {
114 if (eui64_aton(cur
, e
) == 0)
121 /* Find the hostname */
122 while ((cur
= strsep(&line
, " \t\r\n")) != NULL
) {
124 if (strlcpy(hostname
, cur
, len
) <= len
)
131 /* Make sure what remains is either whitespace or a comment */
132 while ((cur
= strsep(&line
, " \t\r\n")) != NULL
) {
147 * Convert an ASCII representation of an EUI-64 to binary form.
150 eui64_aton(const char *a
, struct eui64
*e
)
153 unsigned int 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
);
161 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x:%x:%x",
162 &o0
, &o1
, &o2
, &o3
, &o4
, &o5
, &o6
, &o7
);
165 /* classic fwcontrol/dconschat form */
166 i
= sscanf(a
, "0x%2x%2x%2x%2x%2x%2x%2x%2x",
167 &o0
, &o1
, &o2
, &o3
, &o4
, &o5
, &o6
, &o7
);
171 i
= sscanf(a
, "%x-%x-%x-%x-%x-%x",
172 &o0
, &o1
, &o2
, &o5
, &o6
, &o7
);
179 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x",
180 &o0
, &o1
, &o2
, &o5
, &o6
, &o7
);
203 * Convert a binary representation of an EUI-64 to an ASCII string.
206 eui64_ntoa(const struct eui64
*id
, char *a
, size_t len
)
210 i
= snprintf(a
, len
, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
211 id
->octet
[0], id
->octet
[1], id
->octet
[2], id
->octet
[3],
212 id
->octet
[4], id
->octet
[5], id
->octet
[6], id
->octet
[7]);
213 if (i
< 23 || i
>= len
)
219 * Map an EUI-64 to a hostname. Use either /etc/eui64 or NIS/YP.
222 eui64_ntohost(char *hostname
, size_t len
, const struct eui64
*id
)
225 char buf
[BUFSIZ
+ 2];
226 struct eui64 local_eui64
;
227 char local_host
[MAXHOSTNAMELEN
];
234 if ((fp
= fopen(_PATH_EUI64
, "r")) == NULL
)
237 while (fgets(buf
,BUFSIZ
,fp
)) {
242 if (yp_get_default_domain(&yp_domain
))
244 eui64_ntoa(id
, eui64_a
, sizeof(eui64_a
));
245 if (yp_match(yp_domain
, "eui64.byid", eui64_a
,
246 strlen(eui64_a
), &result
, &resultlen
)) {
249 strncpy(buf
, result
, resultlen
);
250 buf
[resultlen
] = '\0';
254 if (eui64_line(buf
, &local_eui64
, local_host
,
255 sizeof(local_host
)) == 0) {
256 if (bcmp(&local_eui64
.octet
[0],
257 &id
->octet
[0], EUI64_LEN
) == 0) {
258 /* We have a match */
259 strcpy(hostname
, local_host
);
270 * Map a hostname to an EUI-64 using /etc/eui64 or NIS/YP.
273 eui64_hostton(const char *hostname
, struct eui64
*id
)
276 char buf
[BUFSIZ
+ 2];
277 struct eui64 local_eui64
;
278 char local_host
[MAXHOSTNAMELEN
];
284 if ((fp
= fopen(_PATH_EUI64
, "r")) == NULL
)
287 while (fgets(buf
,BUFSIZ
,fp
)) {
292 if (yp_get_default_domain(&yp_domain
))
294 if (yp_match(yp_domain
, "eui64.byname", hostname
,
295 strlen(hostname
), &result
, &resultlen
)) {
298 strncpy(buf
, result
, resultlen
);
299 buf
[resultlen
] = '\0';
303 if (eui64_line(buf
, &local_eui64
, local_host
,
304 sizeof(local_host
)) == 0) {
305 if (strcmp(hostname
, local_host
) == 0) {
306 /* We have a match */
307 bcopy(&local_eui64
, id
, sizeof(struct eui64
));