Skip to content

Commit 660e7de

Browse files
author
Your Name
committed
Add a definition for debugging
1 parent a63f3ac commit 660e7de

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

sockets/RAW/ping.c

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/*
22
* ping.c
33
* written by Ivan Ryabtsov ivriabtsov at gmail dot com
4+
* compile with: gcc -Wall -Wextra -g -O2 -DDEBUG=2 ping.c for maximum debug
5+
* gcc -Wall -Wextra -g -O2 -DDEBUG=1 ping.c for reduce debug
6+
* gcc -Wall -Wextra -g -O2 ping.c without debug
47
*/
58
#include <unistd.h>
69
#include <stdio.h>
@@ -25,6 +28,7 @@ struct stuff {
2528
int ping(const char *name);
2629
int routines(struct stuff *conn);
2730
unsigned short in_cksum(unsigned short *addr, size_t len);
31+
void pr_bytes(const char *str, int size);
2832

2933
int main(int argc, char *argv[])
3034
{
@@ -81,6 +85,7 @@ struct s_frame {
8185
struct icmphdr _icmphdr;
8286
char payload[LOAD_SIZ];
8387
};
88+
8489
int routines(struct stuff *conn)
8590
{
8691
struct s_frame *send_frame;
@@ -131,85 +136,134 @@ int routines(struct stuff *conn)
131136
/* comparison src and dest addresses */
132137
if ( *(unsigned int *)conn->h_ent->h_addr_list[0] ==
133138
((struct iphdr *)recv_frame)->saddr) {
139+
#if DEBUG > 0
134140
printf("package with a valid addresses\n");
141+
#else
142+
;
143+
#endif
135144
} else {
145+
#if DEBUG > 0
136146
printf("!!!!!src and dest addresses is not walid\n");
147+
#endif
137148
goto cont;
138149
}
139-
/*
140-
printf("s 0x%X, d 0x%X\n", ((struct iphdr *)recv_frame)->saddr,
141-
((struct iphdr *)recv_frame)->daddr);
142-
*/
143150
iphdrlen = ((struct iphdr *)recv_frame)->ihl * sizeof(int);
144151
/* check chksum in ip header */
145152
cksum = ((struct iphdr *)recv_frame)->check;
146153
((struct iphdr *)recv_frame)->check = 0;
147154
if (cksum == in_cksum((unsigned short *)recv_frame, iphdrlen)){
155+
#if DEBUG > 0
148156
printf("ip cksum is equal %hu\n", cksum);
157+
#else
158+
;
159+
#endif
149160
} else {
161+
#if DEBUG > 0
150162
printf("!!!!!ip cksum is not equal %hu\n", cksum);
163+
#endif
151164
goto cont;
152165
}
153166
/* compute len of icmp header */
154167
icmplen = ntohs(((struct iphdr *)recv_frame)->tot_len) -
155168
iphdrlen;
156-
/*printf("iphdr len: %d, icmp len: %d, all frame len: %hu\n",
157-
iphdrlen, icmplen,
158-
ntohs(((struct iphdr *)recv_frame)->tot_len));
159-
printf("%p, %p\n", recv_frame, recv_frame + iphdrlen);
160-
printf("%p, %p\n", recv_frame, (struct icmphdr *)(recv_frame +
161-
iphdrlen));
162-
printf("%d\n", ((struct icmphdr *)(recv_frame + iphdrlen))->
163-
checksum);*/
164-
/* check icmp id and sequence number */
165169
if (((struct icmphdr *)(recv_frame + iphdrlen))->un.echo.id ==
166170
send_frame->_icmphdr.un.echo.id) {
171+
#if DEBUG > 0
167172
printf("recv and send id is equal %hu\n",
168173
ntohs(send_frame->_icmphdr.un.echo.id));
174+
#else
175+
;
176+
#endif
169177
} else {
178+
#if DEBUG > 0
170179
printf("!!!!!id is not equal %hu\n",
171180
ntohs(send_frame->_icmphdr.un.echo.id));
181+
#endif
172182
goto cont;
173183
}
174184
if (((struct icmphdr *)(recv_frame + iphdrlen))->
175185
un.echo.sequence ==
176186
send_frame->_icmphdr.un.echo.sequence) {
187+
#if DEBUG > 0
177188
printf("recv end send seq is equal %hu\n",
178189
ntohs(send_frame->
179190
_icmphdr.un.echo.sequence));
191+
#else
192+
;
193+
#endif
180194
} else {
195+
#if DEBUG > 0
181196
printf("!!!!sequence is not equal l %hu, r %hu\n",
182197
ntohs(send_frame->
183198
_icmphdr.un.echo.sequence),
184199
htons(((struct icmphdr *)
185200
(recv_frame + iphdrlen))
186201
->un.echo.sequence));
202+
#else
203+
;
204+
#endif
187205
// goto cont;
188206
}
189207

190208
/* check chksum of icmp header */
191209
cksum = ((struct icmphdr *)(recv_frame + iphdrlen))->
192210
checksum;
193-
/*printf("recv chsum: %hu\n", cksum);*/
194211
((struct icmphdr *)(recv_frame + iphdrlen))->checksum = 0;
195212
if (cksum == in_cksum((unsigned short *)
196213
(recv_frame + iphdrlen), icmplen)) {
197-
printf("icmp cksum is equal %d\n", cksum);
198214
succ_cnt++;
199-
printf("%d\n", succ_cnt);
215+
#if DEBUG > 0
216+
printf("icmp cksum is equal %d\n", cksum);
217+
printf("count of success ping: %d\n", succ_cnt);
218+
#endif
200219
} else {
220+
#if DEBUG > 0
201221
printf("!!!!!icmp checksum is not walid %hu\n", cksum);
222+
#endif
202223
goto cont;
203224
}
204225
cont:
205226
seqtmp = ntohs(send_frame->_icmphdr.un.echo.sequence);
206227
seqtmp++;
207228
send_frame->_icmphdr.un.echo.sequence = htons(seqtmp);
229+
#if DEBUG > 0
230+
#if DEBUG == 2
231+
pr_bytes(recv_frame, ntohs(((struct iphdr *)recv_frame)->
232+
tot_len));
233+
#endif
208234
printf("next sequence is %hu\n=============\n", seqtmp);
235+
#endif
209236
}
210237
return succ_cnt;
211238
}
212239

240+
void pr_bytes(const char *str, int size)
241+
{
242+
unsigned char hex[] = {'0','1','2','3','4','5',
243+
'6','7','8','9','a','b','c','d','e','f'};
244+
char out[3];
245+
out[2] = '\0';
246+
unsigned char tmp;
247+
int i, c;
248+
for (i = 0; i < size; i++) {
249+
tmp = str[i] & 0xff;
250+
for (c = 1; c >= 0; c--) {
251+
out[c] = hex[tmp % 16];
252+
tmp /= 16;
253+
}
254+
if (i % 8 == 0 && i != 0) {
255+
if (i % 16 == 0) {
256+
printf("\n");
257+
} else {
258+
printf(" ");
259+
}
260+
}
261+
printf("%s ", out);
262+
}
263+
printf("\n");
264+
}
265+
266+
213267
unsigned short in_cksum(unsigned short *addr, size_t len)
214268
{
215269
int nleft = len;

0 commit comments

Comments
 (0)