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
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD: src/lib/libc/net/eui64.c,v 1.2 2004/06/01 19:30:13 brooks Exp $");
79 #include <sys/types.h>
83 #include <sys/eui64.h>
87 #include <sys/param.h>
91 #include <rpcsvc/yp_prot.h>
92 #include <rpcsvc/ypclnt.h>
96 #define _PATH_EUI64 "/etc/eui64"
99 static int eui64_line(const char *l
, struct eui64
*e
, char *hostname
,
103 * Parse a string of text containing an EUI-64 and hostname
104 * and separate it into its component parts.
107 eui64_line(const char *l
, struct eui64
*e
, char *hostname
, size_t len
)
109 char *line
, *linehead
, *cur
;
111 linehead
= strdup(l
);
112 if (linehead
== NULL
)
116 /* Find and parse the EUI64 */
117 while ((cur
= strsep(&line
, " \t\r\n")) != NULL
) {
119 if (eui64_aton(cur
, e
) == 0)
126 /* Find the hostname */
127 while ((cur
= strsep(&line
, " \t\r\n")) != NULL
) {
129 if (strlcpy(hostname
, cur
, len
) <= len
)
136 /* Make sure what remains is either whitespace or a comment */
137 while ((cur
= strsep(&line
, " \t\r\n")) != NULL
) {
153 * Convert an ASCII representation of an EUI-64 to binary form.
156 eui64_aton(const char *a
, struct eui64
*e
)
159 unsigned int o0
, o1
, o2
, o3
, o4
, o5
, o6
, o7
;
162 i
= sscanf(a
, "%x-%x-%x-%x-%x-%x-%x-%x",
163 &o0
, &o1
, &o2
, &o3
, &o4
, &o5
, &o6
, &o7
);
167 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x:%x:%x",
168 &o0
, &o1
, &o2
, &o3
, &o4
, &o5
, &o6
, &o7
);
171 /* classic fwcontrol/dconschat form */
172 i
= sscanf(a
, "0x%2x%2x%2x%2x%2x%2x%2x%2x",
173 &o0
, &o1
, &o2
, &o3
, &o4
, &o5
, &o6
, &o7
);
177 i
= sscanf(a
, "%x-%x-%x-%x-%x-%x",
178 &o0
, &o1
, &o2
, &o5
, &o6
, &o7
);
185 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x",
186 &o0
, &o1
, &o2
, &o5
, &o6
, &o7
);
209 * Convert a binary representation of an EUI-64 to an ASCII string.
212 eui64_ntoa(const struct eui64
*id
, char *a
, size_t len
)
216 i
= snprintf(a
, len
, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
217 id
->octet
[0], id
->octet
[1], id
->octet
[2], id
->octet
[3],
218 id
->octet
[4], id
->octet
[5], id
->octet
[6], id
->octet
[7]);
219 if (i
< 23 || i
>= len
)
226 * Map an EUI-64 to a hostname. Use either /etc/eui64 or NIS/YP.
229 eui64_ntohost(char *hostname
, size_t len
, const struct eui64
*id
)
232 char buf
[BUFSIZ
+ 2];
233 struct eui64 local_eui64
;
234 char local_host
[MAXHOSTNAMELEN
];
241 if ((fp
= fopen(_PATH_EUI64
, "r")) == NULL
)
244 while (fgets(buf
,BUFSIZ
,fp
)) {
249 if (yp_get_default_domain(&yp_domain
))
251 eui64_ntoa(id
, eui64_a
, sizeof(eui64_a
));
252 if (yp_match(yp_domain
, "eui64.byid", eui64_a
,
253 strlen(eui64_a
), &result
, &resultlen
)) {
256 strncpy(buf
, result
, resultlen
);
257 buf
[resultlen
] = '\0';
261 if (eui64_line(buf
, &local_eui64
, local_host
,
262 sizeof(local_host
)) == 0) {
263 if (bcmp(&local_eui64
.octet
[0],
264 &id
->octet
[0], EUI64_LEN
) == 0) {
265 /* We have a match */
266 strcpy(hostname
, local_host
);
277 * Map a hostname to an EUI-64 using /etc/eui64 or NIS/YP.
280 eui64_hostton(const char *hostname
, struct eui64
*id
)
283 char buf
[BUFSIZ
+ 2];
284 struct eui64 local_eui64
;
285 char local_host
[MAXHOSTNAMELEN
];
291 if ((fp
= fopen(_PATH_EUI64
, "r")) == NULL
)
294 while (fgets(buf
,BUFSIZ
,fp
)) {
299 if (yp_get_default_domain(&yp_domain
))
301 if (yp_match(yp_domain
, "eui64.byname", hostname
,
302 strlen(hostname
), &result
, &resultlen
)) {
305 strncpy(buf
, result
, resultlen
);
306 buf
[resultlen
] = '\0';
310 if (eui64_line(buf
, &local_eui64
, local_host
,
311 sizeof(local_host
)) == 0) {
312 if (strcmp(hostname
, local_host
) == 0) {
313 /* We have a match */
314 bcopy(&local_eui64
, id
, sizeof(struct eui64
));