Skip to content

Commit 747c49e

Browse files
author
Your Name
committed
remade debugging, checking type and code on str: 178 - 191
1 parent 9204030 commit 747c49e

File tree

1 file changed

+32
-70
lines changed

1 file changed

+32
-70
lines changed

sockets/RAW/ping.c

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
#include <netinet/ip.h>
2020
#include <string.h>
2121

22+
#if DEBUG > 0
23+
#define pr_dbg(...) fprintf(stderr, __VA_ARGS__)
24+
#else
25+
#define pr_dbg(...) ;
26+
#endif
27+
2228
struct stuff {
2329
int recv_sock, send_sock;
2430
struct sockaddr_in recv_sockaddr, send_sockaddr;
@@ -32,19 +38,20 @@ void pr_bytes(const char *str, int size);
3238

3339
int main(int argc, char *argv[])
3440
{
41+
pr_dbg("pr_dbg is works\n");
3542
if (argc < 2) {
36-
printf("need hostname\n");
43+
fprintf(stderr, "need hostname\n");
3744
exit(1);
3845
}
3946
if (ping(argv[1])) {
4047
printf("host %s is alive\n", argv[1]);
48+
return 0;
4149
} else {
4250
printf("host %s is not alive\n", argv[1]);
51+
return 1;
4352
}
44-
return 0;
4553
}
4654

47-
4855
int ping(const char *name)
4956
{
5057
struct stuff *conn;
@@ -62,7 +69,7 @@ int ping(const char *name)
6269
conn->send_sockaddr.sin_family = AF_INET;
6370
conn->send_sockaddr.sin_addr.s_addr =
6471
*(in_addr_t *)conn->h_ent->h_addr_list[0];
65-
/*printf("0x%X\n", send_sockaddr.sin_addr.s_addr);*/
72+
/*pr_dbg("0x%X\n", send_sockaddr.sin_addr.s_addr);*/
6673
if ( (conn->recv_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) ==
6774
-1) {
6875
perror("socket: ");
@@ -71,7 +78,7 @@ int ping(const char *name)
7178
conn->recv_sockaddr.sin_family = AF_INET;
7279
conn->recv_sockaddr.sin_addr.s_addr = *(long *)conn->
7380
h_ent->h_addr_list[0];
74-
/*printf("0x%X\n", recv_sockaddr.sin_addr.s_addr);*/
81+
/*pr_dbg("0x%X\n", recv_sockaddr.sin_addr.s_addr);*/
7582
if (routines(conn) >= 3) {
7683
return 1;
7784
} else {
@@ -132,19 +139,12 @@ int routines(struct stuff *conn)
132139
perror("!!!!!recvfrom: ");
133140
goto cont;
134141
}
135-
/*sleep(1);*/
136142
/* comparison src and dest addresses */
137143
if ( *(unsigned int *)conn->h_ent->h_addr_list[0] ==
138144
((struct iphdr *)recv_frame)->saddr) {
139-
#if DEBUG > 0
140-
printf("package with a valid addresses\n");
141-
#else
142-
;
143-
#endif
145+
pr_dbg("package with a valid addresses\n");
144146
} else {
145-
#if DEBUG > 0
146-
printf("!!!!!src and dest addresses is not walid\n");
147-
#endif
147+
pr_dbg("!!!!!src and dest addresses is not walid\n");
148148
goto cont;
149149
}
150150
iphdrlen = ((struct iphdr *)recv_frame)->ihl * sizeof(int);
@@ -153,15 +153,9 @@ int routines(struct stuff *conn)
153153
cksum = ((struct iphdr *)recv_frame)->check;
154154
((struct iphdr *)recv_frame)->check = 0;
155155
if (cksum == in_cksum((unsigned short *)recv_frame, iphdrlen)){
156-
#if DEBUG > 0
157-
printf("ip cksum is equal %hu\n", cksum);
158-
#else
159-
;
160-
#endif
156+
pr_dbg("ip cksum is equal %hu\n", cksum);
161157
} else {
162-
#if DEBUG > 0
163-
printf("!!!!!ip cksum is not equal %hu\n", cksum);
164-
#endif
158+
pr_dbg("!!!!!ip cksum is not equal %hu\n", cksum);
165159
goto cont;
166160
}
167161

@@ -175,15 +169,9 @@ int routines(struct stuff *conn)
175169
((struct icmphdr *)(recv_frame + iphdrlen))->checksum = 0;
176170
if (cksum == in_cksum((unsigned short *)
177171
(recv_frame + iphdrlen), icmplen)) {
178-
#if DEBUG > 0
179-
printf("icmp cksum is equal %d\n", cksum);
180-
#else
181-
;
182-
#endif
172+
pr_dbg("icmp cksum is equal %d\n", cksum);
183173
} else {
184-
#if DEBUG > 0
185-
printf("!!!!!icmp checksum is not walid %hu\n", cksum);
186-
#endif
174+
pr_dbg("!!!!!icmp checksum is not walid %hu\n", cksum);
187175
goto cont;
188176
}
189177

@@ -192,81 +180,56 @@ int routines(struct stuff *conn)
192180
ICMP_ECHOREPLY && ((struct icmphdr *)
193181
(recv_frame + iphdrlen))->code
194182
== ICMP_ECHOREPLY ) {
195-
#if DEBUG > 0
196-
printf("icmp type and code is ICMP_ECHOREPLY\n");
197-
#else
198-
;
199-
#endif
183+
pr_dbg("icmp type and code is ICMP_ECHOREPLY\n");
200184
} else {
201-
#if DEBUG > 0
202-
printf("!!!!!icmp is not ICMP_ECHOREPLY\n");
203-
printf("type: %hu, code: %hu\n",
185+
pr_dbg("!!!!!icmp is not ICMP_ECHOREPLY\n");
186+
pr_dbg("!!!!!type: %hu, code: %hu\n",
204187
((struct icmphdr *)(recv_frame +
205188
iphdrlen))->type,
206189
((struct icmphdr *)(recv_frame +
207190
iphdrlen))->code);
208-
#else
209-
;
210-
#endif
211191
}
212192

213193
/* check id */
214194
if (((struct icmphdr *)(recv_frame + iphdrlen))->un.echo.id ==
215195
send_frame->_icmphdr.un.echo.id) {
216-
#if DEBUG > 0
217-
printf("recv and send id is equal %hu\n",
196+
pr_dbg("recv and send id is equal %hu\n",
218197
ntohs(send_frame->_icmphdr.un.echo.id));
219-
#else
220-
;
221-
#endif
222198
} else {
223-
#if DEBUG > 0
224-
printf("!!!!!id is not equal %hu\n",
199+
pr_dbg("!!!!!id is not equal %hu\n",
225200
ntohs(send_frame->_icmphdr.un.echo.id));
226-
#endif
227201
goto cont;
228202
}
229203

230204
/* check sequence */
231205
if (((struct icmphdr *)(recv_frame + iphdrlen))->
232206
un.echo.sequence ==
233207
send_frame->_icmphdr.un.echo.sequence) {
234-
#if DEBUG > 0
235-
printf("recv end send seq is equal %hu\n",
208+
pr_dbg("recv end send seq is equal %hu\n",
236209
ntohs(send_frame->
237210
_icmphdr.un.echo.sequence));
238-
#else
239-
;
240-
#endif
241211
} else {
242-
#if DEBUG > 0
243-
printf("!!!!!sequence is not equal l %hu, r %hu\n",
212+
pr_dbg("!!!!!sequence is not equal l %hu, r %hu\n",
244213
ntohs(send_frame->
245214
_icmphdr.un.echo.sequence),
246215
htons(((struct icmphdr *)
247216
(recv_frame + iphdrlen))
248217
->un.echo.sequence));
249-
#else
250-
;
251-
#endif
252218
}
253219

254-
#if DEBUG > 0
220+
/* increment the counter of successful pings */
255221
succ_cnt++;
256-
printf("count of success ping: %d\n", succ_cnt);
257-
#endif
222+
pr_dbg("count of success ping: %d\n", succ_cnt);
258223
cont:
259224
seqtmp = ntohs(send_frame->_icmphdr.un.echo.sequence);
260225
seqtmp++;
261226
send_frame->_icmphdr.un.echo.sequence = htons(seqtmp);
262227
/* print all frame if DEBUG > 1 */
263-
#if DEBUG > 0
264228
#if DEBUG == 2
265229
pr_bytes(recv_frame, ntohs(((struct iphdr *)recv_frame)->
266230
tot_len));
267231
#endif
268-
printf("next sequence is %hu\n=============\n", seqtmp);
269-
#endif
232+
pr_dbg("next sequence is %hu\n=============\n", seqtmp);
270233
}
271234
return succ_cnt;
272235
}
@@ -287,17 +250,16 @@ void pr_bytes(const char *str, int size)
287250
}
288251
if (i % 8 == 0 && i != 0) {
289252
if (i % 16 == 0) {
290-
printf("\n");
253+
fprintf(stderr, "\n");
291254
} else {
292-
printf(" ");
255+
fprintf(stderr, " ");
293256
}
294257
}
295-
printf("%s ", out);
258+
fprintf(stderr, "%s ", out);
296259
}
297-
printf("\n");
260+
fprintf(stderr, "\n");
298261
}
299262

300-
301263
unsigned short in_cksum(unsigned short *addr, size_t len)
302264
{
303265
int nleft = len;

0 commit comments

Comments
 (0)