--- trunk/mac2eui64.c 2015/09/29 08:26:40 3 +++ trunk/mac2eui64.c 2015/10/12 07:53:50 6 @@ -1,13 +1,15 @@ /* mac2eui64.c - Conversion from the IEEE 802 MAC format to the IEEE EUI-64 format. - Displays values suitable for use as a 64 bit interface ID in A6 RRs. - Also displayed are labels suitable for use with PTR RRs in the - two defined reverse zones ip6.arpa (A6) and ip6.int (AAAA). + Conversion from the IEEE 802 MAC format to the (modified) IEEE EUI-64 format. + Displays values suitable for use as a 64 bit interface ID in AAAA and A6 RRs. + Also displays labels suitable for use with PTR RRs in the reverse zone + ip6.arpa. - Copyright (C) 2003 Trond Endrestøl + Updated in 2015 to conform to ISO C 2011. + Copyright (C) 2003 Trond Endrestøl + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -29,25 +31,25 @@ #include #include +void Usage(const char * const argv0, int ReturnValue) __attribute__((noreturn)); +void Version(const char * const argv0) __attribute__((noreturn)); +void TransformMAC(const char * const argv0, const char * const argvi); + int main(int argc, char **argv) { - void Usage(const char * const argv0, int ReturnValue); - void Version(const char * const argv0); - void TransformMAC(const char * const argv0, const char * const argvi); - int c; - size_t i; + ssize_t i; opterr = 0; while ((c = getopt(argc, argv, "hv")) != -1) { switch (c) { case 'h': Usage(argv[0], EXIT_SUCCESS); - break; + //break; case 'v': Version(argv[0]); - break; + //break; case '?': fprintf(stderr, "%s: invalid option -%c\n", argv[0], optopt); @@ -56,7 +58,7 @@ int main(int argc, char **argv) default: fprintf(stderr, "%s: something is dead wrong with getopt() or with this program\n", argv[0]); return EXIT_FAILURE; - break; + //break; } } @@ -99,13 +101,15 @@ void Version(const char * const argv0) { printf("%s version 1.0\n\n" - "Conversion from the IEEE 802 MAC format to the IEEE EUI-64 format.\n" - "Displays values suitable for use as a 64 bit interface ID in A6 RRs.\n" - "Also displayed are labels suitable for use with PTR RRs in the\n" - "two defined reverse zones ip6.arpa (A6) and ip6.int (AAAA).\n\n" + "Conversion from the IEEE 802 MAC format to the (modified) IEEE EUI-64 format.\n" + "Displays values suitable for use as a 64 bit interface ID in AAAA and A6 RRs.\n" + "Also displays labels suitable for use with PTR RRs in the reverse zone\n" + "ip6.arpa.\n\n" - "Copyright (C) 2003 Trond Endrestøl \n\n" + "Updated in 2015 to conform to ISO C 2011.\n\n" + "Copyright (C) 2003 Trond Endrestøl \n\n" + "This program is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" "the Free Software Foundation; either version 2 of the License, or\n" @@ -124,44 +128,41 @@ void Version(const char * const argv0) exit(EXIT_SUCCESS); } /* Version */ +int ConstructMAC(const char *argvi, unsigned char *MAC); +void ConstructEUI64(const unsigned char * const MAC, unsigned char * const EUI64); +void ConstructModEUI64(const unsigned char * const EUI64, unsigned char * const ModEUI64); + +void PrintMAC(const unsigned char * const MAC); +void PrintEUI64(const unsigned char * const EUI64); +void PrintModEUI64(const unsigned char * const EUI64); +void PrintEUI64_InterfaceID(const unsigned char * const EUI64); +void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64); +//void PrintEUI64_IP6_INT(const unsigned char * const EUI64); + void TransformMAC(const char * const argv0, const char * const argvi) { - int ConstructMAC(const char *argvi, unsigned char *MAC); - void ConstructEUI64(const unsigned char * const MAC, unsigned char * const EUI64); - void InvertUniversalLocalBit(unsigned char * const EUI64); + unsigned char MAC[6], EUI64[8], ModEUI64[8]; - void PrintMAC(const unsigned char * const MAC); - void PrintEUI64(const unsigned char * const EUI64); - void PrintEUI64_InterfaceID(const unsigned char * const EUI64); - void PrintEUI64_IP6_INT(const unsigned char * const EUI64); - void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64); - - unsigned char MAC[6], EUI64[8]; - if (ConstructMAC(argvi, MAC)) { fprintf(stderr, "%s: invalid MAC address: %s\n", argv0, argvi); return; } ConstructEUI64(MAC, EUI64); + ConstructModEUI64(EUI64, ModEUI64); PrintMAC(MAC); PrintEUI64(EUI64); - - /* Transform the EUI-64 address into - the Modified EUI-64 address for use as - the value for the interface ID */ - InvertUniversalLocalBit(EUI64); - - PrintEUI64_InterfaceID(EUI64); - PrintEUI64_IP6_INT(EUI64); - PrintEUI64_IP6_ARPA(EUI64); + PrintModEUI64(ModEUI64); + PrintEUI64_InterfaceID(ModEUI64); + PrintEUI64_IP6_ARPA(ModEUI64); + //PrintEUI64_IP6_INT(ModEUI64); } /* TransformMAC() */ +unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble); + int ConstructMAC(const char *argvi, unsigned char * MAC) { - unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble); - int ReturnValue = EXIT_SUCCESS, NumDigits = 0; size_t i = 0; unsigned char MSNybble, LSNybble; @@ -169,14 +170,14 @@ int ConstructMAC(const char *argvi, unsigned char * MA while (*argvi) { if (isxdigit(*argvi)) { NumDigits++; - MSNybble = *argvi++; + MSNybble = (unsigned char)*argvi++; if (!isxdigit(*argvi)) { break; } NumDigits++; - LSNybble = *argvi; + LSNybble = (unsigned char)*argvi; MAC[i++] = ConstructHexByte(MSNybble, LSNybble); } @@ -194,8 +195,8 @@ unsigned char ConstructHexByte(unsigned char MSNybble, { unsigned char Byte; - MSNybble = toupper(MSNybble); - LSNybble = toupper(LSNybble); + MSNybble = (unsigned char)toupper(MSNybble); + LSNybble = (unsigned char)toupper(LSNybble); if (MSNybble >= 'A') { MSNybble = 0x0A + MSNybble - 'A'; @@ -211,7 +212,7 @@ unsigned char ConstructHexByte(unsigned char MSNybble, LSNybble -= '0'; } - Byte = (MSNybble << 4) | LSNybble; + Byte = (unsigned char)((MSNybble << 4) | LSNybble); return Byte; } /* ConstructHexByte() */ @@ -222,12 +223,28 @@ void ConstructEUI64(const unsigned char * const MAC, u EUI64[1] = MAC[1]; EUI64[2] = MAC[2]; EUI64[3] = 0xFF; - EUI64[4] = 0xFE; + EUI64[4] = 0xFF; EUI64[5] = MAC[3]; EUI64[6] = MAC[4]; EUI64[7] = MAC[5]; } /* ConstructEUI64() */ +void InvertUniversalLocalBit(unsigned char * const EUI64); + +void ConstructModEUI64(const unsigned char * const EUI64, unsigned char * const ModEUI64) +{ + ModEUI64[0] = EUI64[0]; + ModEUI64[1] = EUI64[1]; + ModEUI64[2] = EUI64[2]; + ModEUI64[3] = EUI64[3]; + ModEUI64[4] = 0xFE; + ModEUI64[5] = EUI64[5]; + ModEUI64[6] = EUI64[6]; + ModEUI64[7] = EUI64[7]; + + InvertUniversalLocalBit(ModEUI64); +} /* ConstructModEUI64() */ + void InvertUniversalLocalBit(unsigned char * const EUI64) { if (EUI64[0] & 0x02) @@ -249,6 +266,13 @@ void PrintEUI64(const unsigned char * const EUI64) EUI64[4], EUI64[5], EUI64[6], EUI64[7]); } /* PrintEUI64() */ +void PrintModEUI64(const unsigned char * const EUI64) +{ + printf("Mod. EUI-64:\t%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", + EUI64[0], EUI64[1], EUI64[2], EUI64[3], + EUI64[4], EUI64[5], EUI64[6], EUI64[7]); +} /* PrintModEUI64() */ + void PrintEUI64_InterfaceID(const unsigned char * const EUI64) { printf("Interface ID:\t::%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", @@ -256,9 +280,13 @@ void PrintEUI64_InterfaceID(const unsigned char * cons EUI64[4], EUI64[5], EUI64[6], EUI64[7]); } /* PrintEUI64_InterfaceID() */ -void PrintEUI64_IP6_INT(const unsigned char * const EUI64) +void PrintEUI64_IP6_ARPA(const unsigned char * const EUI64) { - 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", +// EUI64[0], EUI64[1], EUI64[2], EUI64[3], +// EUI64[4], EUI64[5], EUI64[6], EUI64[7]); + + printf("ip6.arpa/64:\t%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x\n", EUI64[7] & 0x0F, EUI64[7] >> 4, EUI64[6] & 0x0F, EUI64[6] >> 4, EUI64[5] & 0x0F, EUI64[5] >> 4, @@ -267,13 +295,19 @@ void PrintEUI64_IP6_INT(const unsigned char * const EU EUI64[2] & 0x0F, EUI64[2] >> 4, EUI64[1] & 0x0F, EUI64[1] >> 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]); } /* PrintEUI64_IP6_ARPA() */ + +//void PrintEUI64_IP6_INT(const unsigned char * const EUI64) +//{ +// printf("ip6.int/64:\t%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x\n", +// EUI64[7] & 0x0F, EUI64[7] >> 4, +// EUI64[6] & 0x0F, EUI64[6] >> 4, +// EUI64[5] & 0x0F, EUI64[5] >> 4, +// EUI64[4] & 0x0F, EUI64[4] >> 4, +// EUI64[3] & 0x0F, EUI64[3] >> 4, +// EUI64[2] & 0x0F, EUI64[2] >> 4, +// EUI64[1] & 0x0F, EUI64[1] >> 4, +// EUI64[0] & 0x0F, EUI64[0] >> 4); +//} /* PrintEUI64_IP6_INT() */ /* mac2eui64.c */