Skip to content

Commit

Permalink
Mac validation added.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xba1a committed Aug 19, 2013
1 parent a07e0e1 commit edaef6b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BUFF_SIZE=1024

IF_NAME=lo
IF_NAME=eth0
PK_DST_MAC=00:01:9B:04:03:3C

ETHER_HEADER
Expand Down
53 changes: 49 additions & 4 deletions help.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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;
}

/**
Expand Down
1 change: 0 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit edaef6b

Please sign in to comment.