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

Diff of /trunk/mac2eui64.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 3 by trond, 2015-09-29T08:26:40Z Revision 4 by trond, 2015-09-29T08:31:02Z
# Line 4  Line 4 
4   Conversion from the IEEE 802 MAC format to the IEEE EUI-64 format.   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.   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   Also displayed are labels suitable for use with PTR RRs in the
7   two defined reverse zones ip6.arpa (A6) and ip6.int (AAAA).   reverse zone ip6.arpa.
8    
9   Copyright (C) 2003 Trond Endrestøl <trond@ramstind.gtf.ol.no>   Updated in 2015 to conform to ISO C 2011.
10    
11     Copyright (C) 2003 Trond Endrestøl <Trond.Endrestol@ximalas.info>
12    
13   This program is free software; you can redistribute it and/or modify   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   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   the Free Software Foundation; either version 2 of the License, or
# Line 29  Line 31 
31  #include <stdlib.h>  #include <stdlib.h>
32  #include <unistd.h>  #include <unistd.h>
33    
34    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  int main(int argc, char **argv)  int main(int argc, char **argv)
39  {  {
   void Usage(const char * const argv0, int ReturnValue);  
   void Version(const char * const argv0);  
   void TransformMAC(const char * const argv0, const char * const argvi);  
   
40    int c;    int c;
41    size_t i;    ssize_t i;
42    
43    opterr = 0;    opterr = 0;
44    while ((c = getopt(argc, argv, "hv")) != -1) {    while ((c = getopt(argc, argv, "hv")) != -1) {
45      switch (c) {      switch (c) {
46        case 'h':        case 'h':
47          Usage(argv[0], EXIT_SUCCESS);          Usage(argv[0], EXIT_SUCCESS);
48          break;          //break;
49    
50        case 'v':        case 'v':
51          Version(argv[0]);          Version(argv[0]);
52          break;          //break;
53    
54        case '?':        case '?':
55          fprintf(stderr, "%s: invalid option -%c\n", argv[0], optopt);          fprintf(stderr, "%s: invalid option -%c\n", argv[0], optopt);
# Line 56  int main(int argc, char **argv) Line 58  int main(int argc, char **argv)
58        default:        default:
59          fprintf(stderr, "%s: something is dead wrong with getopt() or with this program\n", argv[0]);          fprintf(stderr, "%s: something is dead wrong with getopt() or with this program\n", argv[0]);
60          return EXIT_FAILURE;          return EXIT_FAILURE;
61          break;          //break;
62        }        }
63      }      }
64    
# Line 102  void Version(const char * const argv0) Line 104  void Version(const char * const argv0)
104           "Conversion from the IEEE 802 MAC format to the IEEE EUI-64 format.\n"           "Conversion from the IEEE 802 MAC format to the IEEE EUI-64 format.\n"
105           "Displays values suitable for use as a 64 bit interface ID in A6 RRs.\n"           "Displays values suitable for use as a 64 bit interface ID in A6 RRs.\n"
106           "Also displayed are labels suitable for use with PTR RRs in the\n"           "Also displayed are labels suitable for use with PTR RRs in the\n"
107           "two defined reverse zones ip6.arpa (A6) and ip6.int (AAAA).\n\n"           "reverse zone ip6.arpa.\n\n"
108    
109           "Copyright (C) 2003 Trond Endrestøl <trond@ramstind.gtf.ol.no>\n\n"           "Updated in 2015 to conform to ISO C 2011.\n\n"
110    
111             "Copyright (C) 2003 Trond Endrestøl <Trond.Endrestol@ximalas.info>\n\n"
112    
113           "This program is free software; you can redistribute it and/or modify\n"           "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"           "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"           "the Free Software Foundation; either version 2 of the License, or\n"
# Line 124  void Version(const char * const argv0) Line 128  void Version(const char * const argv0)
128    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
129  } /* Version */  } /* Version */
130    
131  void TransformMAC(const char * const argv0, const char * const argvi)  int ConstructMAC(const char *argvi, unsigned char *MAC);
132  {  void ConstructEUI64(const unsigned char * const MAC, unsigned char * const EUI64);
133    int ConstructMAC(const char *argvi, unsigned char *MAC);  void InvertUniversalLocalBit(unsigned char * const EUI64);
   void ConstructEUI64(const unsigned char * const MAC, unsigned char * const EUI64);  
   void InvertUniversalLocalBit(unsigned char * const EUI64);  
134    
135    void PrintMAC(const unsigned char * const MAC);  void PrintMAC(const unsigned char * const MAC);
136    void PrintEUI64(const unsigned char * const EUI64);  void PrintEUI64(const unsigned char * const EUI64);
137    void PrintEUI64_InterfaceID(const unsigned char * const EUI64);  void PrintEUI64_InterfaceID(const unsigned char * const EUI64);
138    void PrintEUI64_IP6_INT(const unsigned char * const EUI64);  void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64);
139    void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64);  //void PrintEUI64_IP6_INT(const unsigned char * const EUI64);
140    
141    void TransformMAC(const char * const argv0, const char * const argvi)
142    {
143    unsigned char MAC[6], EUI64[8];    unsigned char MAC[6], EUI64[8];
144    
145    if (ConstructMAC(argvi, MAC)) {    if (ConstructMAC(argvi, MAC)) {
# Line 154  void TransformMAC(const char * const argv0, const char Line 158  void TransformMAC(const char * const argv0, const char
158    InvertUniversalLocalBit(EUI64);    InvertUniversalLocalBit(EUI64);
159    
160    PrintEUI64_InterfaceID(EUI64);    PrintEUI64_InterfaceID(EUI64);
   PrintEUI64_IP6_INT(EUI64);  
161    PrintEUI64_IP6_ARPA(EUI64);    PrintEUI64_IP6_ARPA(EUI64);
162      //PrintEUI64_IP6_INT(EUI64);
163  } /* TransformMAC() */  } /* TransformMAC() */
164    
165  int ConstructMAC(const char *argvi, unsigned char * MAC)  int ConstructMAC(const char *argvi, unsigned char * MAC)
# Line 169  int ConstructMAC(const char *argvi, unsigned char * MA Line 173  int ConstructMAC(const char *argvi, unsigned char * MA
173    while (*argvi) {    while (*argvi) {
174      if (isxdigit(*argvi)) {      if (isxdigit(*argvi)) {
175        NumDigits++;        NumDigits++;
176        MSNybble = *argvi++;        MSNybble = (unsigned char)*argvi++;
177    
178        if (!isxdigit(*argvi)) {        if (!isxdigit(*argvi)) {
179          break;          break;
180          }          }
181    
182        NumDigits++;        NumDigits++;
183        LSNybble = *argvi;        LSNybble = (unsigned char)*argvi;
184    
185        MAC[i++] = ConstructHexByte(MSNybble, LSNybble);        MAC[i++] = ConstructHexByte(MSNybble, LSNybble);
186        }        }
# Line 190  int ConstructMAC(const char *argvi, unsigned char * MA Line 194  int ConstructMAC(const char *argvi, unsigned char * MA
194    return ReturnValue;    return ReturnValue;
195  } /* ConstructMAC() */  } /* ConstructMAC() */
196    
197    unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble);
198    
199  unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble)  unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble)
200  {  {
201    unsigned char Byte;    unsigned char Byte;
202    
203    MSNybble = toupper(MSNybble);    MSNybble = (unsigned char)toupper(MSNybble);
204    LSNybble = toupper(LSNybble);    LSNybble = (unsigned char)toupper(LSNybble);
205    
206    if (MSNybble >= 'A') {    if (MSNybble >= 'A') {
207      MSNybble = 0x0A + MSNybble - 'A';      MSNybble = 0x0A + MSNybble - 'A';
# Line 211  unsigned char ConstructHexByte(unsigned char MSNybble, Line 217  unsigned char ConstructHexByte(unsigned char MSNybble,
217      LSNybble -= '0';      LSNybble -= '0';
218      }      }
219    
220    Byte = (MSNybble << 4) | LSNybble;    Byte = (unsigned char)((MSNybble << 4) | LSNybble);
221    
222    return Byte;    return Byte;
223  } /* ConstructHexByte() */  } /* ConstructHexByte() */
# Line 256  void PrintEUI64_InterfaceID(const unsigned char * cons Line 262  void PrintEUI64_InterfaceID(const unsigned char * cons
262           EUI64[4], EUI64[5], EUI64[6], EUI64[7]);           EUI64[4], EUI64[5], EUI64[6], EUI64[7]);
263  } /* PrintEUI64_InterfaceID() */  } /* PrintEUI64_InterfaceID() */
264    
265  void PrintEUI64_IP6_INT(const unsigned char * const EUI64)  void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64)
266  {  {
267    printf("ip6.int/64:\t%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x\n",  //  printf("ip6.arpa:\t\\[x%02X%02X%02X%02X%02X%02X%02X%02X/64]\n",
268    //         EUI64[0], EUI64[1], EUI64[2], EUI64[3],
269    //         EUI64[4], EUI64[5], EUI64[6], EUI64[7]);
270    
271      printf("ip6.arpa/64:\t%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x\n",
272           EUI64[7] & 0x0F, EUI64[7] >> 4,           EUI64[7] & 0x0F, EUI64[7] >> 4,
273           EUI64[6] & 0x0F, EUI64[6] >> 4,           EUI64[6] & 0x0F, EUI64[6] >> 4,
274           EUI64[5] & 0x0F, EUI64[5] >> 4,           EUI64[5] & 0x0F, EUI64[5] >> 4,
# Line 267  void PrintEUI64_IP6_INT(const unsigned char * const EU Line 277  void PrintEUI64_IP6_INT(const unsigned char * const EU
277           EUI64[2] & 0x0F, EUI64[2] >> 4,           EUI64[2] & 0x0F, EUI64[2] >> 4,
278           EUI64[1] & 0x0F, EUI64[1] >> 4,           EUI64[1] & 0x0F, EUI64[1] >> 4,
279           EUI64[0] & 0x0F, EUI64[0] >> 4);           EUI64[0] & 0x0F, EUI64[0] >> 4);
 } /* PrintEUI64_IP6_INT() */  
   
 void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64)  
 {  
   printf("ip6.arpa:\t\\[x%02X%02X%02X%02X%02X%02X%02X%02X/64]\n",  
          EUI64[0], EUI64[1], EUI64[2], EUI64[3],  
          EUI64[4], EUI64[5], EUI64[6], EUI64[7]);  
280  } /* PrintEUI64_IP6_ARPA() */  } /* PrintEUI64_IP6_ARPA() */
281    
282    //void PrintEUI64_IP6_INT(const unsigned char * const EUI64)
283    //{
284    //  printf("ip6.int/64:\t%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x\n",
285    //         EUI64[7] & 0x0F, EUI64[7] >> 4,
286    //         EUI64[6] & 0x0F, EUI64[6] >> 4,
287    //         EUI64[5] & 0x0F, EUI64[5] >> 4,
288    //         EUI64[4] & 0x0F, EUI64[4] >> 4,
289    //         EUI64[3] & 0x0F, EUI64[3] >> 4,
290    //         EUI64[2] & 0x0F, EUI64[2] >> 4,
291    //         EUI64[1] & 0x0F, EUI64[1] >> 4,
292    //         EUI64[0] & 0x0F, EUI64[0] >> 4);
293    //} /* PrintEUI64_IP6_INT() */
294    
295  /* mac2eui64.c */  /* mac2eui64.c */


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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