diff --git a/TODO b/TODO index fd87a76..2ef714c 100644 --- a/TODO +++ b/TODO @@ -2,4 +2,4 @@ * Have one more look at IPv6 extension headers * Add support for type-1, type-2 routing headers * Add support for short form of IPv6 prefix in RA prefix option - * MAC validation is yet to be added + * MAC validation is yet to be added [19-08-2013] diff --git a/conf b/conf index 682355a..a0af654 100644 --- a/conf +++ b/conf @@ -1,6 +1,6 @@ BUFF_SIZE=1024 -IF_NAME=lo +IF_NAME=eth0 PK_DST_MAC=00:01:9B:04:03:3C ETHER_HEADER diff --git a/help.c b/help.c index 2fc9cf2..19d0e17 100644 --- a/help.c +++ b/help.c @@ -107,6 +107,41 @@ int32_t send_packet(const char *if_name, const char *dst_mac, const char *cp_buf return -1; } +/** + * @param mac The mac address that to be validated + * + * @return + * 0 Success + * -1 Error + * + * @Description + * Checks whether the given mac address is valid. + */ +int validate_mac(const char *mac) { + int i; + + if (strlen(mac) != 17) + goto err; + + for (i = 0; i < 17; i++) { + if ((((mac[i] >= 'A') && (mac[i] <= 'F')) || + ((mac[i] >= 'a') && (mac[i] <= 'f')) || + ((mac[i] >= '0') && (mac[i] <= '9'))) + && ((i+1) % 3 != 0)) + continue; + else if ((mac[i] == ':') && ((i+1)%3 == 0)) + continue; + else + goto err; + } + return 0; + +err: + PGEN_INFO("Mac validation failed"); + PGEN_PRINT_DATA("%s\n", mac); + return -1; +} + /** * @param dst Destination pointer where the resulting mac address * will be stored @@ -125,6 +160,14 @@ int32_t mac_writer(char *dst, const char *src) { char ind; int32_t i, j = 0; + if (!dst || !src) { + PGEN_INFO("Arguments NULL check failed"); + goto err; + } + + if (validate_mac(src)) + goto err; + for (i = 0; i < 17; i++) { ind = src[i]; if (ind >= '0' && ind <= '9') @@ -137,13 +180,15 @@ int32_t mac_writer(char *dst, const char *src) { dst[j++] = (unsigned char) seg; seg = 0; } - else { - PGEN_INFO("mac_writer returns error"); - return -1; - } + else + goto err; } dst[j] = (unsigned char) seg; return 0; + +err: + PGEN_INFO("mac_writer returns error"); + return -1; } /** diff --git a/main.c b/main.c index 56e0b62..47cee7f 100644 --- a/main.c +++ b/main.c @@ -127,7 +127,6 @@ int32_t main(int32_t argc, char **argv) { err: PGEN_INFO("ERROR CASE"); - PGEN_PRINT_DATA("Option: %s\tValue: %s\n", option, value); fclose(fp); /* free will accept NULL also */ free(cp_buff);