f183c7b3d490fa798357a002e38d7d53a86bba62
[dragonfly.git] / lib / libc / net / eui64.c
1 /*
2  * Copyright 2004 The Aerospace Corporation.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * Copyright (c) 1995
29  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
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.
45  *
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
56  * SUCH DAMAGE.
57  *
58  * EUI-64 conversion and lookup routines
59  *
60  *
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
63  * by Brooks Davis
64  *
65  * Written by Bill Paul <wpaul@ctr.columbia.edu>
66  * Center for Telecommunications Research
67  * Columbia University, New York City
68  *
69  * $FreeBSD: src/lib/libc/net/eui64.c,v 1.2 2004/06/01 19:30:13 brooks Exp $
70  */
71
72 #include <stdio.h>
73 #include <paths.h>
74 #include <sys/types.h>
75 #include <sys/eui64.h>
76 #include <string.h>
77 #include <stdlib.h>
78 #include <sys/param.h>
79 #ifdef YP
80 #include <rpc/rpc.h>
81 #include <rpcsvc/yp_prot.h>
82 #include <rpcsvc/ypclnt.h>
83 #endif
84
85 #ifndef _PATH_EUI64
86 #define _PATH_EUI64 "/etc/eui64"
87 #endif
88
89 static int eui64_line(const char *l, struct eui64 *e, char *hostname,
90     size_t len);
91
92 /*
93  * Parse a string of text containing an EUI-64 and hostname
94  * and separate it into its component parts.
95  */
96 static int
97 eui64_line(const char *l, struct eui64 *e, char *hostname, size_t len)
98 {
99         char *line, *linehead, *cur;
100
101         linehead = strdup(l);
102         if (linehead == NULL)
103                 return (-1);
104         line = linehead;
105
106         /* Find and parse the EUI64 */
107         while ((cur = strsep(&line, " \t\r\n")) != NULL) {
108                 if (*cur != '\0') {
109                         if (eui64_aton(cur, e) == 0)
110                                 break;
111                         else
112                                 goto bad;
113                 }
114         }
115
116         /* Find the hostname */
117         while ((cur = strsep(&line, " \t\r\n")) != NULL) {
118                 if (*cur != '\0') {
119                         if (strlcpy(hostname, cur, len) <= len)
120                                 break;
121                         else
122                                 goto bad;
123                 }
124         }
125
126         /* Make sure what remains is either whitespace or a comment */
127         while ((cur = strsep(&line, " \t\r\n")) != NULL) {
128                 if (*cur == '#')
129                         break;
130                 if (*cur != '\0')
131                         goto bad;
132         }
133
134         return (0);
135
136 bad:
137         free(linehead);
138         return (-1);
139 }
140
141 /*
142  * Convert an ASCII representation of an EUI-64 to binary form.
143  */
144 int
145 eui64_aton(const char *a, struct eui64 *e)
146 {
147         int i;
148         unsigned int o0, o1, o2, o3, o4, o5, o6, o7;
149
150         /* canonical form */
151         i = sscanf(a, "%x-%x-%x-%x-%x-%x-%x-%x",
152             &o0, &o1, &o2, &o3, &o4, &o5, &o6, &o7);
153         if (i == EUI64_LEN)
154                 goto good;
155         /* ethernet form */
156         i = sscanf(a, "%x:%x:%x:%x:%x:%x:%x:%x",
157             &o0, &o1, &o2, &o3, &o4, &o5, &o6, &o7);
158         if (i == EUI64_LEN)
159                 goto good;
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);
163         if (i == EUI64_LEN)
164                 goto good;
165         /* MAC format (-) */
166         i = sscanf(a, "%x-%x-%x-%x-%x-%x",
167             &o0, &o1, &o2, &o5, &o6, &o7);
168         if (i == 6) {
169                 o3 = 0xff;
170                 o4 = 0xfe;
171                 goto good;
172         }
173         /* MAC format (:) */
174         i = sscanf(a, "%x:%x:%x:%x:%x:%x",
175             &o0, &o1, &o2, &o5, &o6, &o7);
176         if (i == 6) {
177                 o3 = 0xff;
178                 o4 = 0xfe;
179                 goto good;
180         }
181
182         return (-1);
183
184 good:
185         e->octet[0]=o0;
186         e->octet[1]=o1;
187         e->octet[2]=o2;
188         e->octet[3]=o3;
189         e->octet[4]=o4;
190         e->octet[5]=o5;
191         e->octet[6]=o6;
192         e->octet[7]=o7;
193
194         return (0);
195 }
196
197 /*
198  * Convert a binary representation of an EUI-64 to an ASCII string.
199  */
200 int
201 eui64_ntoa(const struct eui64 *id, char *a, size_t len)
202 {
203         int i;
204
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)
209                 return (-1);
210         return (0);
211 }
212
213 /*
214  * Map an EUI-64 to a hostname. Use either /etc/eui64 or NIS/YP.
215  */
216 int
217 eui64_ntohost(char *hostname, size_t len, const struct eui64 *id)
218 {
219         FILE *fp;
220         char buf[BUFSIZ + 2];
221         struct eui64 local_eui64;
222         char local_host[MAXHOSTNAMELEN];
223 #ifdef YP
224         char *result;
225         int resultlen;
226         char eui64_a[24];
227         char *yp_domain;
228 #endif
229         if ((fp = fopen(_PATH_EUI64, "r")) == NULL)
230                 return (1);
231
232         while (fgets(buf,BUFSIZ,fp)) {
233                 if (buf[0] == '#')
234                         continue;
235 #ifdef YP
236                 if (buf[0] == '+') {
237                         if (yp_get_default_domain(&yp_domain))
238                                 continue;
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)) {
242                                 continue;
243                         }
244                         strncpy(buf, result, resultlen);
245                         buf[resultlen] = '\0';
246                         free(result);
247                 }
248 #endif
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);
255                                 fclose(fp);
256                                 return(0);
257                         }
258                 }
259         }
260         fclose(fp);
261         return (1);
262 }
263
264 /*
265  * Map a hostname to an EUI-64 using /etc/eui64 or NIS/YP.
266  */
267 int
268 eui64_hostton(const char *hostname, struct eui64 *id)
269 {
270         FILE *fp;
271         char buf[BUFSIZ + 2];
272         struct eui64 local_eui64;
273         char local_host[MAXHOSTNAMELEN];
274 #ifdef YP
275         char *result;
276         int resultlen;
277         char *yp_domain;
278 #endif
279         if ((fp = fopen(_PATH_EUI64, "r")) == NULL)
280                 return (1);
281
282         while (fgets(buf,BUFSIZ,fp)) {
283                 if (buf[0] == '#')
284                         continue;
285 #ifdef YP
286                 if (buf[0] == '+') {
287                         if (yp_get_default_domain(&yp_domain))
288                                 continue;
289                         if (yp_match(yp_domain, "eui64.byname", hostname,
290                                 strlen(hostname), &result, &resultlen)) {
291                                 continue;
292                         }
293                         strncpy(buf, result, resultlen);
294                         buf[resultlen] = '\0';
295                         free(result);
296                 }
297 #endif
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));
303                                 fclose(fp);
304                                 return(0);
305                         }
306                 }
307         }
308         fclose(fp);
309         return (1);
310 }