Skip to content

Commit edaef6b

Browse files
committed
Mac validation added.
1 parent a07e0e1 commit edaef6b

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

TODO

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
* Have one more look at IPv6 extension headers
33
* Add support for type-1, type-2 routing headers
44
* Add support for short form of IPv6 prefix in RA prefix option
5-
* MAC validation is yet to be added
5+
* MAC validation is yet to be added [19-08-2013]

conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BUFF_SIZE=1024
22

3-
IF_NAME=lo
3+
IF_NAME=eth0
44
PK_DST_MAC=00:01:9B:04:03:3C
55

66
ETHER_HEADER

help.c

+49-4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,41 @@ int32_t send_packet(const char *if_name, const char *dst_mac, const char *cp_buf
107107
return -1;
108108
}
109109

110+
/**
111+
* @param mac The mac address that to be validated
112+
*
113+
* @return
114+
* 0 Success
115+
* -1 Error
116+
*
117+
* @Description
118+
* Checks whether the given mac address is valid.
119+
*/
120+
int validate_mac(const char *mac) {
121+
int i;
122+
123+
if (strlen(mac) != 17)
124+
goto err;
125+
126+
for (i = 0; i < 17; i++) {
127+
if ((((mac[i] >= 'A') && (mac[i] <= 'F')) ||
128+
((mac[i] >= 'a') && (mac[i] <= 'f')) ||
129+
((mac[i] >= '0') && (mac[i] <= '9')))
130+
&& ((i+1) % 3 != 0))
131+
continue;
132+
else if ((mac[i] == ':') && ((i+1)%3 == 0))
133+
continue;
134+
else
135+
goto err;
136+
}
137+
return 0;
138+
139+
err:
140+
PGEN_INFO("Mac validation failed");
141+
PGEN_PRINT_DATA("%s\n", mac);
142+
return -1;
143+
}
144+
110145
/**
111146
* @param dst Destination pointer where the resulting mac address
112147
* will be stored
@@ -125,6 +160,14 @@ int32_t mac_writer(char *dst, const char *src) {
125160
char ind;
126161
int32_t i, j = 0;
127162

163+
if (!dst || !src) {
164+
PGEN_INFO("Arguments NULL check failed");
165+
goto err;
166+
}
167+
168+
if (validate_mac(src))
169+
goto err;
170+
128171
for (i = 0; i < 17; i++) {
129172
ind = src[i];
130173
if (ind >= '0' && ind <= '9')
@@ -137,13 +180,15 @@ int32_t mac_writer(char *dst, const char *src) {
137180
dst[j++] = (unsigned char) seg;
138181
seg = 0;
139182
}
140-
else {
141-
PGEN_INFO("mac_writer returns error");
142-
return -1;
143-
}
183+
else
184+
goto err;
144185
}
145186
dst[j] = (unsigned char) seg;
146187
return 0;
188+
189+
err:
190+
PGEN_INFO("mac_writer returns error");
191+
return -1;
147192
}
148193

149194
/**

main.c

-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ int32_t main(int32_t argc, char **argv) {
127127

128128
err:
129129
PGEN_INFO("ERROR CASE");
130-
PGEN_PRINT_DATA("Option: %s\tValue: %s\n", option, value);
131130
fclose(fp);
132131
/* free will accept NULL also */
133132
free(cp_buff);

0 commit comments

Comments
 (0)