/[mac2eui64]/trunk/mac2eui64.c
ViewVC logotype

Annotation of /trunk/mac2eui64.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (hide annotations) (download)
2015-09-29T09:12:20Z (8 years, 6 months ago) by trond
Content type: text/plain
File size: 9518 byte(s)
Changed the code to properly display both an IEEE EUI-64 format
identifier and a modified IEEE EUI-64 format identifier.

1 trond 3 /*
2     mac2eui64.c
3    
4 trond 5 Conversion from the IEEE 802 MAC format to the (modified) IEEE EUI-64 format.
5     Displays values suitable for use as a 64 bit interface ID in AAAA and A6 RRs.
6     Also displays labels suitable for use with PTR RRs in the reverse zone
7     ip6.arpa.
8 trond 3
9 trond 4 Updated in 2015 to conform to ISO C 2011.
10 trond 3
11 trond 4 Copyright (C) 2003 Trond Endrestøl <Trond.Endrestol@ximalas.info>
12    
13 trond 3 This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17    
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22    
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26    
27     $Ximalas$
28     */
29     #include <ctype.h>
30     #include <stdio.h>
31     #include <stdlib.h>
32     #include <unistd.h>
33    
34 trond 4 void Usage(const char * const argv0, int ReturnValue) __attribute__((noreturn));
35     void Version(const char * const argv0) __attribute__((noreturn));
36     void TransformMAC(const char * const argv0, const char * const argvi);
37    
38 trond 3 int main(int argc, char **argv)
39     {
40     int c;
41 trond 4 ssize_t i;
42 trond 3
43     opterr = 0;
44     while ((c = getopt(argc, argv, "hv")) != -1) {
45     switch (c) {
46     case 'h':
47     Usage(argv[0], EXIT_SUCCESS);
48 trond 4 //break;
49 trond 3
50     case 'v':
51     Version(argv[0]);
52 trond 4 //break;
53 trond 3
54     case '?':
55     fprintf(stderr, "%s: invalid option -%c\n", argv[0], optopt);
56     break;
57    
58     default:
59     fprintf(stderr, "%s: something is dead wrong with getopt() or with this program\n", argv[0]);
60     return EXIT_FAILURE;
61 trond 4 //break;
62 trond 3 }
63     }
64    
65     i = optind;
66    
67     if (i == argc)
68     Usage(argv[0], EXIT_FAILURE);
69    
70     TransformMAC(argv[0], argv[i++]);
71    
72     while (i < argc) {
73     puts("");
74     TransformMAC(argv[0], argv[i++]);
75     }
76    
77     return EXIT_SUCCESS;
78     } /* main() */
79    
80     void Usage(const char * const argv0, int ReturnValue)
81     {
82     fprintf((ReturnValue == EXIT_SUCCESS) ? stdout : stderr,
83     "Usage: %s [-h | -v | <macaddress1> [macaddress2] ... [macaddressN]]\n\n"
84    
85     "Options:\n"
86     "-h show this help\n"
87     "-v display version and copyright\n\n"
88    
89     "MAC addresses must consist of 12 hex digits with leading zeroes.\n"
90     "Any punctuation is ignored. Examples of valid MAC addresses:\n\n"
91    
92     " 00:50:DA:2C:8F:55\n"
93     " 00-50-DA-2C-8F-55\n"
94     " 0050.DA2C.8F55\n"
95     " 0050DA2C8F55\n",
96     argv0);
97     exit(ReturnValue);
98     } /* Usage() */
99    
100     void Version(const char * const argv0)
101     {
102     printf("%s version 1.0\n\n"
103    
104 trond 5 "Conversion from the IEEE 802 MAC format to the (modified) IEEE EUI-64 format.\n"
105     "Displays values suitable for use as a 64 bit interface ID in AAAA and A6 RRs.\n"
106     "Also displays labels suitable for use with PTR RRs in the reverse zone\n"
107     "ip6.arpa.\n\n"
108 trond 3
109 trond 4 "Updated in 2015 to conform to ISO C 2011.\n\n"
110 trond 3
111 trond 4 "Copyright (C) 2003 Trond Endrestøl <Trond.Endrestol@ximalas.info>\n\n"
112    
113 trond 3 "This program is free software; you can redistribute it and/or modify\n"
114     "it under the terms of the GNU General Public License as published by\n"
115     "the Free Software Foundation; either version 2 of the License, or\n"
116     "(at your option) any later version.\n\n"
117    
118     "This program is distributed in the hope that it will be useful,\n"
119     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
120     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
121     "GNU General Public License for more details.\n\n"
122    
123     "You should have received a copy of the GNU General Public License\n"
124     "along with this program; if not, write to the Free Software\n"
125     "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.\n",
126     argv0);
127    
128     exit(EXIT_SUCCESS);
129     } /* Version */
130    
131 trond 4 int ConstructMAC(const char *argvi, unsigned char *MAC);
132     void ConstructEUI64(const unsigned char * const MAC, unsigned char * const EUI64);
133 trond 5 void ConstructModEUI64(const unsigned char * const EUI64, unsigned char * const ModEUI64);
134 trond 4
135     void PrintMAC(const unsigned char * const MAC);
136     void PrintEUI64(const unsigned char * const EUI64);
137 trond 5 void PrintModEUI64(const unsigned char * const EUI64);
138 trond 4 void PrintEUI64_InterfaceID(const unsigned char * const EUI64);
139     void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64);
140     //void PrintEUI64_IP6_INT(const unsigned char * const EUI64);
141    
142 trond 3 void TransformMAC(const char * const argv0, const char * const argvi)
143     {
144 trond 5 unsigned char MAC[6], EUI64[8], ModEUI64[8];
145 trond 3
146     if (ConstructMAC(argvi, MAC)) {
147     fprintf(stderr, "%s: invalid MAC address: %s\n", argv0, argvi);
148     return;
149     }
150    
151     ConstructEUI64(MAC, EUI64);
152 trond 5 ConstructModEUI64(EUI64, ModEUI64);
153 trond 3
154     PrintMAC(MAC);
155     PrintEUI64(EUI64);
156 trond 5 PrintModEUI64(ModEUI64);
157     PrintEUI64_InterfaceID(ModEUI64);
158     PrintEUI64_IP6_ARPA(ModEUI64);
159     //PrintEUI64_IP6_INT(ModEUI64);
160 trond 3 } /* TransformMAC() */
161    
162     int ConstructMAC(const char *argvi, unsigned char * MAC)
163     {
164     unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble);
165    
166     int ReturnValue = EXIT_SUCCESS, NumDigits = 0;
167     size_t i = 0;
168     unsigned char MSNybble, LSNybble;
169    
170     while (*argvi) {
171     if (isxdigit(*argvi)) {
172     NumDigits++;
173 trond 4 MSNybble = (unsigned char)*argvi++;
174 trond 3
175     if (!isxdigit(*argvi)) {
176     break;
177     }
178    
179     NumDigits++;
180 trond 4 LSNybble = (unsigned char)*argvi;
181 trond 3
182     MAC[i++] = ConstructHexByte(MSNybble, LSNybble);
183     }
184    
185     argvi++;
186     }
187    
188     if (NumDigits != 12)
189     ReturnValue = EXIT_FAILURE;
190    
191     return ReturnValue;
192     } /* ConstructMAC() */
193    
194 trond 4 unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble);
195    
196 trond 3 unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble)
197     {
198     unsigned char Byte;
199    
200 trond 4 MSNybble = (unsigned char)toupper(MSNybble);
201     LSNybble = (unsigned char)toupper(LSNybble);
202 trond 3
203     if (MSNybble >= 'A') {
204     MSNybble = 0x0A + MSNybble - 'A';
205     }
206     else {
207     MSNybble -= '0';
208     }
209    
210     if (LSNybble >= 'A') {
211     LSNybble = 0x0A + LSNybble - 'A';
212     }
213     else {
214     LSNybble -= '0';
215     }
216    
217 trond 4 Byte = (unsigned char)((MSNybble << 4) | LSNybble);
218 trond 3
219     return Byte;
220     } /* ConstructHexByte() */
221    
222     void ConstructEUI64(const unsigned char * const MAC, unsigned char * const EUI64)
223     {
224     EUI64[0] = MAC[0];
225     EUI64[1] = MAC[1];
226     EUI64[2] = MAC[2];
227     EUI64[3] = 0xFF;
228 trond 5 EUI64[4] = 0xFF;
229 trond 3 EUI64[5] = MAC[3];
230     EUI64[6] = MAC[4];
231     EUI64[7] = MAC[5];
232     } /* ConstructEUI64() */
233    
234 trond 5 void InvertUniversalLocalBit(unsigned char * const EUI64);
235    
236     void ConstructModEUI64(const unsigned char * const EUI64, unsigned char * const ModEUI64)
237     {
238     ModEUI64[0] = EUI64[0];
239     ModEUI64[1] = EUI64[1];
240     ModEUI64[2] = EUI64[2];
241     ModEUI64[3] = EUI64[3];
242     ModEUI64[4] = 0xFE;
243     ModEUI64[5] = EUI64[5];
244     ModEUI64[6] = EUI64[6];
245     ModEUI64[7] = EUI64[7];
246    
247     InvertUniversalLocalBit(ModEUI64);
248     } /* ConstructModEUI64() */
249    
250 trond 3 void InvertUniversalLocalBit(unsigned char * const EUI64)
251     {
252     if (EUI64[0] & 0x02)
253     EUI64[0] &= ~0x02;
254     else
255     EUI64[0] |= 0x02;
256     } /* InvertUniversalLocalBit() */
257    
258     void PrintMAC(const unsigned char * const MAC)
259     {
260     printf("MAC:\t\t%02X:%02X:%02X:%02X:%02X:%02X\n",
261     MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
262     } /* PrintMAC() */
263    
264     void PrintEUI64(const unsigned char * const EUI64)
265     {
266     printf("EUI-64:\t\t%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
267     EUI64[0], EUI64[1], EUI64[2], EUI64[3],
268     EUI64[4], EUI64[5], EUI64[6], EUI64[7]);
269     } /* PrintEUI64() */
270    
271 trond 5 void PrintModEUI64(const unsigned char * const EUI64)
272     {
273     printf("Mod. EUI-64:\t%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
274     EUI64[0], EUI64[1], EUI64[2], EUI64[3],
275     EUI64[4], EUI64[5], EUI64[6], EUI64[7]);
276     } /* PrintModEUI64() */
277    
278 trond 3 void PrintEUI64_InterfaceID(const unsigned char * const EUI64)
279     {
280     printf("Interface ID:\t::%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
281     EUI64[0], EUI64[1], EUI64[2], EUI64[3],
282     EUI64[4], EUI64[5], EUI64[6], EUI64[7]);
283     } /* PrintEUI64_InterfaceID() */
284    
285 trond 4 void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64)
286 trond 3 {
287 trond 4 // printf("ip6.arpa:\t\\[x%02X%02X%02X%02X%02X%02X%02X%02X/64]\n",
288     // EUI64[0], EUI64[1], EUI64[2], EUI64[3],
289     // EUI64[4], EUI64[5], EUI64[6], EUI64[7]);
290    
291     printf("ip6.arpa/64:\t%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x\n",
292 trond 3 EUI64[7] & 0x0F, EUI64[7] >> 4,
293     EUI64[6] & 0x0F, EUI64[6] >> 4,
294     EUI64[5] & 0x0F, EUI64[5] >> 4,
295     EUI64[4] & 0x0F, EUI64[4] >> 4,
296     EUI64[3] & 0x0F, EUI64[3] >> 4,
297     EUI64[2] & 0x0F, EUI64[2] >> 4,
298     EUI64[1] & 0x0F, EUI64[1] >> 4,
299     EUI64[0] & 0x0F, EUI64[0] >> 4);
300     } /* PrintEUI64_IP6_ARPA() */
301    
302 trond 4 //void PrintEUI64_IP6_INT(const unsigned char * const EUI64)
303     //{
304     // printf("ip6.int/64:\t%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x\n",
305     // EUI64[7] & 0x0F, EUI64[7] >> 4,
306     // EUI64[6] & 0x0F, EUI64[6] >> 4,
307     // EUI64[5] & 0x0F, EUI64[5] >> 4,
308     // EUI64[4] & 0x0F, EUI64[4] >> 4,
309     // EUI64[3] & 0x0F, EUI64[3] >> 4,
310     // EUI64[2] & 0x0F, EUI64[2] >> 4,
311     // EUI64[1] & 0x0F, EUI64[1] >> 4,
312     // EUI64[0] & 0x0F, EUI64[0] >> 4);
313     //} /* PrintEUI64_IP6_INT() */
314    
315 trond 3 /* mac2eui64.c */

Properties

Name Value
svn:eol-style native
svn:keywords Ximalas=%H
svn:mime-type text/plain

svn@ximalas.info
ViewVC Help
Powered by ViewVC 1.3.0-dev