Skip to content

Commit 9204030

Browse files
author
Your Name
committed
add validation code and type
1 parent 660e7de commit 9204030

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

sockets/RAW/ping.c

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ int routines(struct stuff *conn)
148148
goto cont;
149149
}
150150
iphdrlen = ((struct iphdr *)recv_frame)->ihl * sizeof(int);
151+
151152
/* check chksum in ip header */
152153
cksum = ((struct iphdr *)recv_frame)->check;
153154
((struct iphdr *)recv_frame)->check = 0;
@@ -163,9 +164,53 @@ int routines(struct stuff *conn)
163164
#endif
164165
goto cont;
165166
}
167+
166168
/* compute len of icmp header */
167169
icmplen = ntohs(((struct iphdr *)recv_frame)->tot_len) -
168170
iphdrlen;
171+
172+
/* check chksum of icmp header */
173+
cksum = ((struct icmphdr *)(recv_frame + iphdrlen))->
174+
checksum;
175+
((struct icmphdr *)(recv_frame + iphdrlen))->checksum = 0;
176+
if (cksum == in_cksum((unsigned short *)
177+
(recv_frame + iphdrlen), icmplen)) {
178+
#if DEBUG > 0
179+
printf("icmp cksum is equal %d\n", cksum);
180+
#else
181+
;
182+
#endif
183+
} else {
184+
#if DEBUG > 0
185+
printf("!!!!!icmp checksum is not walid %hu\n", cksum);
186+
#endif
187+
goto cont;
188+
}
189+
190+
/* check type and code */
191+
if (((struct icmphdr *)(recv_frame + iphdrlen))->type ==
192+
ICMP_ECHOREPLY && ((struct icmphdr *)
193+
(recv_frame + iphdrlen))->code
194+
== ICMP_ECHOREPLY ) {
195+
#if DEBUG > 0
196+
printf("icmp type and code is ICMP_ECHOREPLY\n");
197+
#else
198+
;
199+
#endif
200+
} else {
201+
#if DEBUG > 0
202+
printf("!!!!!icmp is not ICMP_ECHOREPLY\n");
203+
printf("type: %hu, code: %hu\n",
204+
((struct icmphdr *)(recv_frame +
205+
iphdrlen))->type,
206+
((struct icmphdr *)(recv_frame +
207+
iphdrlen))->code);
208+
#else
209+
;
210+
#endif
211+
}
212+
213+
/* check id */
169214
if (((struct icmphdr *)(recv_frame + iphdrlen))->un.echo.id ==
170215
send_frame->_icmphdr.un.echo.id) {
171216
#if DEBUG > 0
@@ -181,6 +226,8 @@ int routines(struct stuff *conn)
181226
#endif
182227
goto cont;
183228
}
229+
230+
/* check sequence */
184231
if (((struct icmphdr *)(recv_frame + iphdrlen))->
185232
un.echo.sequence ==
186233
send_frame->_icmphdr.un.echo.sequence) {
@@ -193,7 +240,7 @@ int routines(struct stuff *conn)
193240
#endif
194241
} else {
195242
#if DEBUG > 0
196-
printf("!!!!sequence is not equal l %hu, r %hu\n",
243+
printf("!!!!!sequence is not equal l %hu, r %hu\n",
197244
ntohs(send_frame->
198245
_icmphdr.un.echo.sequence),
199246
htons(((struct icmphdr *)
@@ -202,30 +249,17 @@ int routines(struct stuff *conn)
202249
#else
203250
;
204251
#endif
205-
// goto cont;
206252
}
207253

208-
/* check chksum of icmp header */
209-
cksum = ((struct icmphdr *)(recv_frame + iphdrlen))->
210-
checksum;
211-
((struct icmphdr *)(recv_frame + iphdrlen))->checksum = 0;
212-
if (cksum == in_cksum((unsigned short *)
213-
(recv_frame + iphdrlen), icmplen)) {
214-
succ_cnt++;
215254
#if DEBUG > 0
216-
printf("icmp cksum is equal %d\n", cksum);
217-
printf("count of success ping: %d\n", succ_cnt);
255+
succ_cnt++;
256+
printf("count of success ping: %d\n", succ_cnt);
218257
#endif
219-
} else {
220-
#if DEBUG > 0
221-
printf("!!!!!icmp checksum is not walid %hu\n", cksum);
222-
#endif
223-
goto cont;
224-
}
225258
cont:
226259
seqtmp = ntohs(send_frame->_icmphdr.un.echo.sequence);
227260
seqtmp++;
228261
send_frame->_icmphdr.un.echo.sequence = htons(seqtmp);
262+
/* print all frame if DEBUG > 1 */
229263
#if DEBUG > 0
230264
#if DEBUG == 2
231265
pr_bytes(recv_frame, ntohs(((struct iphdr *)recv_frame)->

0 commit comments

Comments
 (0)