--- trunk/mac2eui64.c 2015/09/29 08:26:40 3 +++ trunk/mac2eui64.c 2015/09/29 08:31:02 4 @@ -4,10 +4,12 @@ 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). + 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; } } @@ -102,10 +104,12 @@ void Version(const char * const argv0) "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" + "reverse zone 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,18 +128,18 @@ void Version(const char * const argv0) exit(EXIT_SUCCESS); } /* Version */ -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); +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); - 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); +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_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) +{ unsigned char MAC[6], EUI64[8]; if (ConstructMAC(argvi, MAC)) { @@ -154,8 +158,8 @@ void TransformMAC(const char * const argv0, const char InvertUniversalLocalBit(EUI64); PrintEUI64_InterfaceID(EUI64); - PrintEUI64_IP6_INT(EUI64); PrintEUI64_IP6_ARPA(EUI64); + //PrintEUI64_IP6_INT(EUI64); } /* TransformMAC() */ int ConstructMAC(const char *argvi, unsigned char * MAC) @@ -169,14 +173,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); } @@ -190,12 +194,14 @@ int ConstructMAC(const char *argvi, unsigned char * MA return ReturnValue; } /* ConstructMAC() */ +unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble); + unsigned char ConstructHexByte(unsigned char MSNybble, unsigned char LSNybble) { 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 +217,7 @@ unsigned char ConstructHexByte(unsigned char MSNybble, LSNybble -= '0'; } - Byte = (MSNybble << 4) | LSNybble; + Byte = (unsigned char)((MSNybble << 4) | LSNybble); return Byte; } /* ConstructHexByte() */ @@ -256,9 +262,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 +277,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 */