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

Annotation of /trunk/mac2eui64.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (hide annotations) (download)
2015-09-29T08:26:40Z (8 years, 9 months ago) by trond
Content type: text/plain
File size: 8130 byte(s)
Added files from the 2003 version.

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