aboutsummaryrefslogtreecommitdiff
path: root/networking/ping.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/ping.c')
-rw-r--r--networking/ping.c85
1 files changed, 32 insertions, 53 deletions
diff --git a/networking/ping.c b/networking/ping.c
index 782b801c8..400d565d9 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -41,17 +41,10 @@ enum {
41 PINGINTERVAL = 1 /* second */ 41 PINGINTERVAL = 1 /* second */
42}; 42};
43 43
44#define O_QUIET (1 << 0)
45
46#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
47#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
48#define SET(bit) (A(bit) |= B(bit))
49#define CLR(bit) (A(bit) &= (~B(bit)))
50#define TST(bit) (A(bit) & B(bit))
51
52static void ping(const char *host); 44static void ping(const char *host);
53 45
54/* common routines */ 46/* common routines */
47
55static int in_cksum(unsigned short *buf, int sz) 48static int in_cksum(unsigned short *buf, int sz)
56{ 49{
57 int nleft = sz; 50 int nleft = sz;
@@ -75,8 +68,10 @@ static int in_cksum(unsigned short *buf, int sz)
75 return (ans); 68 return (ans);
76} 69}
77 70
78/* simple version */
79#ifndef CONFIG_FEATURE_FANCY_PING 71#ifndef CONFIG_FEATURE_FANCY_PING
72
73/* simple version */
74
80static char *hostname; 75static char *hostname;
81 76
82static void noresp(int ign) 77static void noresp(int ign)
@@ -153,14 +148,21 @@ int ping_main(int argc, char **argv)
153} 148}
154 149
155#else /* ! CONFIG_FEATURE_FANCY_PING */ 150#else /* ! CONFIG_FEATURE_FANCY_PING */
151
156/* full(er) version */ 152/* full(er) version */
153
154#define OPT_STRING "qc:s:I:"
155enum {
156 OPT_QUIET = 1 << 0,
157};
158
157static struct sockaddr_in pingaddr; 159static struct sockaddr_in pingaddr;
158static struct sockaddr_in sourceaddr; 160static struct sockaddr_in sourceaddr;
159static int pingsock = -1; 161static int pingsock = -1;
160static unsigned datalen; /* intentionally uninitialized to work around gcc bug */ 162static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
161 163
162static unsigned long ntransmitted, nreceived, nrepeats, pingcount; 164static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
163static int myid, options; 165static int myid;
164static unsigned long tmin = ULONG_MAX, tmax, tsum; 166static unsigned long tmin = ULONG_MAX, tmax, tsum;
165static char rcvd_tbl[MAX_DUP_CHK / 8]; 167static char rcvd_tbl[MAX_DUP_CHK / 8];
166 168
@@ -170,6 +172,12 @@ static void sendping(int);
170static void pingstats(int); 172static void pingstats(int);
171static void unpack(char *, int, struct sockaddr_in *); 173static void unpack(char *, int, struct sockaddr_in *);
172 174
175#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
176#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
177#define SET(bit) (A(bit) |= B(bit))
178#define CLR(bit) (A(bit) &= (~B(bit)))
179#define TST(bit) (A(bit) & B(bit))
180
173/**************************************************************************/ 181/**************************************************************************/
174 182
175static void pingstats(int junk) 183static void pingstats(int junk)
@@ -304,7 +312,7 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from)
304 dupflag = 0; 312 dupflag = 0;
305 } 313 }
306 314
307 if (options & O_QUIET) 315 if (option_mask32 & OPT_QUIET)
308 return; 316 return;
309 317
310 printf("%d bytes from %s: icmp_seq=%u", sz, 318 printf("%d bytes from %s: icmp_seq=%u", sz,
@@ -409,55 +417,26 @@ static int parse_nipquad(const char *str, struct sockaddr_in* addr)
409 417
410int ping_main(int argc, char **argv) 418int ping_main(int argc, char **argv)
411{ 419{
412 char *thisarg; 420 char *opt_c, *opt_s, *opt_I;
413 421
414 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */ 422 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
415 423
416 argc--; 424 /* exactly one argument needed */
417 argv++; 425 opt_complementary = "=1";
418 /* Parse any options */ 426 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
419 while (argc >= 1 && **argv == '-') { 427 if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c
420 thisarg = *argv; 428 if (option_mask32 & 4) datalen = xatou16(opt_s); // -s
421 thisarg++; 429 if (option_mask32 & 8) { // -I
422 switch (*thisarg) { 430/* TODO: ping6 accepts iface too:
423 case 'q': 431 if_index = if_nametoindex(*argv);
424 options |= O_QUIET; 432 if (!if_index) ...
425 break; 433make it true for ping. */
426 case 'c': 434 if (parse_nipquad(opt_I, &sourceaddr))
427 if (--argc <= 0)
428 bb_show_usage();
429 argv++;
430 pingcount = xatoul(*argv);
431 break;
432 case 's':
433 if (--argc <= 0)
434 bb_show_usage();
435 argv++;
436 datalen = xatou16(*argv);
437 break;
438 case 'I':
439 if (--argc <= 0)
440 bb_show_usage();
441 argv++;
442/* ping6 accepts iface too:
443 if_index = if_nametoindex(*argv);
444 if (!if_index) ...
445 make it true for ping too. TODO.
446*/
447 if (parse_nipquad(*argv, &sourceaddr))
448 bb_show_usage();
449 break;
450 default:
451 bb_show_usage(); 435 bb_show_usage();
452 }
453 argc--;
454 argv++;
455 } 436 }
456 if (argc < 1)
457 bb_show_usage();
458 437
459 myid = (int16_t) getpid(); 438 myid = (int16_t) getpid();
460 ping(*argv); 439 ping(argv[optind]);
461 return EXIT_SUCCESS; 440 return EXIT_SUCCESS;
462} 441}
463#endif /* ! CONFIG_FEATURE_FANCY_PING */ 442#endif /* ! CONFIG_FEATURE_FANCY_PING */