diff options
author | Pavel Roskin <proski@gnu.org> | 2000-06-07 20:38:15 +0000 |
---|---|---|
committer | Pavel Roskin <proski@gnu.org> | 2000-06-07 20:38:15 +0000 |
commit | 0024abcbbc633f4493b97c4b344ed636ea176f3c (patch) | |
tree | f00955edc3be4d63b2fa0da1186ba9e8afdc6227 | |
parent | 0b66577368206a433411f6367c90ab70d82ba275 (diff) | |
download | busybox-w32-0024abcbbc633f4493b97c4b344ed636ea176f3c.tar.gz busybox-w32-0024abcbbc633f4493b97c4b344ed636ea176f3c.tar.bz2 busybox-w32-0024abcbbc633f4493b97c4b344ed636ea176f3c.zip |
Implemented "ping -s", fixed error messages and argument parsing
-rw-r--r-- | Changelog | 2 | ||||
-rw-r--r-- | docs/busybox.pod | 3 | ||||
-rw-r--r-- | networking/ping.c | 57 | ||||
-rw-r--r-- | ping.c | 57 |
4 files changed, 68 insertions, 51 deletions
@@ -63,6 +63,8 @@ | |||
63 | * Fixed 'swapon -a' and 'swapoff -a', which were broken. | 63 | * Fixed 'swapon -a' and 'swapoff -a', which were broken. |
64 | * Fixed 'mount -a' so it works as expected. | 64 | * Fixed 'mount -a' so it works as expected. |
65 | * Implemented 'ls -R' (enabled by enabling BB_FEATURE_LS_RECURSIVE) | 65 | * Implemented 'ls -R' (enabled by enabling BB_FEATURE_LS_RECURSIVE) |
66 | * Implemented "ping -s", fixed error messages and argument parsing - | ||
67 | Pavel Roskin | ||
66 | * More doc updates | 68 | * More doc updates |
67 | 69 | ||
68 | 70 | ||
diff --git a/docs/busybox.pod b/docs/busybox.pod index 3cd45f7bd..7a04b8f10 100644 --- a/docs/busybox.pod +++ b/docs/busybox.pod | |||
@@ -1250,6 +1250,7 @@ Send ICMP ECHO_REQUEST packets to network hosts. | |||
1250 | Options: | 1250 | Options: |
1251 | 1251 | ||
1252 | -c COUNT Send only COUNT pings. | 1252 | -c COUNT Send only COUNT pings. |
1253 | -s SIZE Send SIZE data bytes in packets (default=56). | ||
1253 | -q Quiet mode, only displays output at start | 1254 | -q Quiet mode, only displays output at start |
1254 | and when finished. | 1255 | and when finished. |
1255 | Example: | 1256 | Example: |
@@ -1948,4 +1949,4 @@ Enrique Zanardi <ezanardi@ull.es> | |||
1948 | 1949 | ||
1949 | =cut | 1950 | =cut |
1950 | 1951 | ||
1951 | # $Id: busybox.pod,v 1.36 2000/06/07 17:28:53 andersen Exp $ | 1952 | # $Id: busybox.pod,v 1.37 2000/06/07 20:38:15 proski Exp $ |
diff --git a/networking/ping.c b/networking/ping.c index 14a56cd55..0cfe06229 100644 --- a/networking/ping.c +++ b/networking/ping.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * $Id: ping.c,v 1.15 2000/05/12 19:41:47 erik Exp $ | 3 | * $Id: ping.c,v 1.16 2000/06/07 20:38:15 proski Exp $ |
4 | * Mini ping implementation for busybox | 4 | * Mini ping implementation for busybox |
5 | * | 5 | * |
6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> | 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
@@ -64,6 +64,8 @@ | |||
64 | #define CLR(bit) (A(bit) &= (~B(bit))) | 64 | #define CLR(bit) (A(bit) &= (~B(bit))) |
65 | #define TST(bit) (A(bit) & B(bit)) | 65 | #define TST(bit) (A(bit) & B(bit)) |
66 | 66 | ||
67 | static void ping(const char *host); | ||
68 | |||
67 | /* common routines */ | 69 | /* common routines */ |
68 | static int in_cksum(unsigned short *buf, int sz) | 70 | static int in_cksum(unsigned short *buf, int sz) |
69 | { | 71 | { |
@@ -104,7 +106,7 @@ static void noresp(int ign) | |||
104 | exit(0); | 106 | exit(0); |
105 | } | 107 | } |
106 | 108 | ||
107 | static int ping(const char *host) | 109 | static void ping(const char *host) |
108 | { | 110 | { |
109 | struct hostent *h; | 111 | struct hostent *h; |
110 | struct sockaddr_in pingaddr; | 112 | struct sockaddr_in pingaddr; |
@@ -113,7 +115,7 @@ static int ping(const char *host) | |||
113 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; | 115 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
114 | 116 | ||
115 | if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ | 117 | if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ |
116 | perror("ping"); | 118 | perror("ping: creating a raw socket"); |
117 | exit(1); | 119 | exit(1); |
118 | } | 120 | } |
119 | 121 | ||
@@ -140,7 +142,7 @@ static int ping(const char *host) | |||
140 | 142 | ||
141 | if (c < 0 || c != sizeof(packet)) { | 143 | if (c < 0 || c != sizeof(packet)) { |
142 | if (c < 0) | 144 | if (c < 0) |
143 | perror("ping"); | 145 | perror("ping: sendto"); |
144 | fprintf(stderr, "ping: write incomplete\n"); | 146 | fprintf(stderr, "ping: write incomplete\n"); |
145 | exit(1); | 147 | exit(1); |
146 | } | 148 | } |
@@ -156,7 +158,7 @@ static int ping(const char *host) | |||
156 | (struct sockaddr *) &from, &fromlen)) < 0) { | 158 | (struct sockaddr *) &from, &fromlen)) < 0) { |
157 | if (errno == EINTR) | 159 | if (errno == EINTR) |
158 | continue; | 160 | continue; |
159 | perror("ping"); | 161 | perror("ping: recvfrom"); |
160 | continue; | 162 | continue; |
161 | } | 163 | } |
162 | if (c >= 76) { /* ip + icmp */ | 164 | if (c >= 76) { /* ip + icmp */ |
@@ -168,7 +170,7 @@ static int ping(const char *host) | |||
168 | } | 170 | } |
169 | } | 171 | } |
170 | printf("%s is alive!\n", hostname); | 172 | printf("%s is alive!\n", hostname); |
171 | return (TRUE); | 173 | return; |
172 | } | 174 | } |
173 | 175 | ||
174 | extern int ping_main(int argc, char **argv) | 176 | extern int ping_main(int argc, char **argv) |
@@ -181,13 +183,14 @@ extern int ping_main(int argc, char **argv) | |||
181 | exit(TRUE); | 183 | exit(TRUE); |
182 | } | 184 | } |
183 | 185 | ||
184 | #else | 186 | #else /* ! BB_SIMPLE_PING */ |
185 | /* full(er) version */ | 187 | /* full(er) version */ |
186 | static const char *ping_usage = "ping [OPTION]... host\n" | 188 | static const char *ping_usage = "ping [OPTION]... host\n" |
187 | #ifndef BB_FEATURE_TRIVIAL_HELP | 189 | #ifndef BB_FEATURE_TRIVIAL_HELP |
188 | "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n" | 190 | "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n" |
189 | "Options:\n" | 191 | "Options:\n" |
190 | "\t-c COUNT\tSend only COUNT pings.\n" | 192 | "\t-c COUNT\tSend only COUNT pings.\n" |
193 | "\t-s SIZE\t\tSend SIZE data bytes in packets (default=56).\n" | ||
191 | "\t-q\t\tQuiet mode, only displays output at start\n" | 194 | "\t-q\t\tQuiet mode, only displays output at start\n" |
192 | "\t\t\tand when finished.\n" | 195 | "\t\t\tand when finished.\n" |
193 | #endif | 196 | #endif |
@@ -196,6 +199,7 @@ static const char *ping_usage = "ping [OPTION]... host\n" | |||
196 | static char *hostname = NULL; | 199 | static char *hostname = NULL; |
197 | static struct sockaddr_in pingaddr; | 200 | static struct sockaddr_in pingaddr; |
198 | static int pingsock = -1; | 201 | static int pingsock = -1; |
202 | static int datalen = DEFDATALEN; | ||
199 | 203 | ||
200 | static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0; | 204 | static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0; |
201 | static int myid = 0, options = 0; | 205 | static int myid = 0, options = 0; |
@@ -206,8 +210,6 @@ static void sendping(int); | |||
206 | static void pingstats(int); | 210 | static void pingstats(int); |
207 | static void unpack(char *, int, struct sockaddr_in *); | 211 | static void unpack(char *, int, struct sockaddr_in *); |
208 | 212 | ||
209 | static void ping(char *); | ||
210 | |||
211 | /**************************************************************************/ | 213 | /**************************************************************************/ |
212 | 214 | ||
213 | static void pingstats(int ign) | 215 | static void pingstats(int ign) |
@@ -234,7 +236,7 @@ static void sendping(int ign) | |||
234 | { | 236 | { |
235 | struct icmp *pkt; | 237 | struct icmp *pkt; |
236 | int i; | 238 | int i; |
237 | char packet[DEFDATALEN + 8]; | 239 | char packet[datalen + 8]; |
238 | 240 | ||
239 | pkt = (struct icmp *) packet; | 241 | pkt = (struct icmp *) packet; |
240 | 242 | ||
@@ -251,13 +253,11 @@ static void sendping(int ign) | |||
251 | i = sendto(pingsock, packet, sizeof(packet), 0, | 253 | i = sendto(pingsock, packet, sizeof(packet), 0, |
252 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); | 254 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
253 | 255 | ||
254 | if (i < 0 || i != sizeof(packet)) { | 256 | if (i < 0) |
255 | if (i < 0) | 257 | fatalError("ping: sendto: %s\n", strerror(errno)); |
256 | perror("ping"); | 258 | else if (i != sizeof(packet)) |
257 | fprintf(stderr, "ping wrote %d chars; %d expected\n", i, | 259 | fatalError("ping wrote %d chars; %d expected\n", i, |
258 | (int)sizeof(packet)); | 260 | (int)sizeof(packet)); |
259 | exit(1); | ||
260 | } | ||
261 | 261 | ||
262 | signal(SIGALRM, sendping); | 262 | signal(SIGALRM, sendping); |
263 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ | 263 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
@@ -303,7 +303,7 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from) | |||
303 | iphdr = (struct iphdr *) buf; | 303 | iphdr = (struct iphdr *) buf; |
304 | hlen = iphdr->ihl << 2; | 304 | hlen = iphdr->ihl << 2; |
305 | /* discard if too short */ | 305 | /* discard if too short */ |
306 | if (sz < (DEFDATALEN + ICMP_MINLEN)) | 306 | if (sz < (datalen + ICMP_MINLEN)) |
307 | return; | 307 | return; |
308 | 308 | ||
309 | sz -= hlen; | 309 | sz -= hlen; |
@@ -356,12 +356,12 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from) | |||
356 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); | 356 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); |
357 | } | 357 | } |
358 | 358 | ||
359 | static void ping(char *host) | 359 | static void ping(const char *host) |
360 | { | 360 | { |
361 | struct protoent *proto; | 361 | struct protoent *proto; |
362 | struct hostent *h; | 362 | struct hostent *h; |
363 | char buf[MAXHOSTNAMELEN]; | 363 | char buf[MAXHOSTNAMELEN]; |
364 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; | 364 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
365 | int sockopt; | 365 | int sockopt; |
366 | 366 | ||
367 | proto = getprotobyname("icmp"); | 367 | proto = getprotobyname("icmp"); |
@@ -372,7 +372,7 @@ static void ping(char *host) | |||
372 | if (errno == EPERM) { | 372 | if (errno == EPERM) { |
373 | fprintf(stderr, "ping: permission denied. (are you root?)\n"); | 373 | fprintf(stderr, "ping: permission denied. (are you root?)\n"); |
374 | } else { | 374 | } else { |
375 | perror("ping"); | 375 | perror("ping: creating a raw socket"); |
376 | } | 376 | } |
377 | exit(1); | 377 | exit(1); |
378 | } | 378 | } |
@@ -412,7 +412,7 @@ static void ping(char *host) | |||
412 | printf("PING %s (%s): %d data bytes\n", | 412 | printf("PING %s (%s): %d data bytes\n", |
413 | hostname, | 413 | hostname, |
414 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), | 414 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), |
415 | DEFDATALEN); | 415 | datalen); |
416 | 416 | ||
417 | signal(SIGINT, pingstats); | 417 | signal(SIGINT, pingstats); |
418 | 418 | ||
@@ -429,7 +429,7 @@ static void ping(char *host) | |||
429 | (struct sockaddr *) &from, &fromlen)) < 0) { | 429 | (struct sockaddr *) &from, &fromlen)) < 0) { |
430 | if (errno == EINTR) | 430 | if (errno == EINTR) |
431 | continue; | 431 | continue; |
432 | perror("ping"); | 432 | perror("ping: recvfrom"); |
433 | continue; | 433 | continue; |
434 | } | 434 | } |
435 | unpack(packet, c, &from); | 435 | unpack(packet, c, &from); |
@@ -455,10 +455,17 @@ extern int ping_main(int argc, char **argv) | |||
455 | options |= O_QUIET; | 455 | options |= O_QUIET; |
456 | break; | 456 | break; |
457 | case 'c': | 457 | case 'c': |
458 | argc--; | 458 | if (--argc <= 0) |
459 | usage(ping_usage); | ||
459 | argv++; | 460 | argv++; |
460 | pingcount = atoi(*argv); | 461 | pingcount = atoi(*argv); |
461 | break; | 462 | break; |
463 | case 's': | ||
464 | if (--argc <= 0) | ||
465 | usage(ping_usage); | ||
466 | argv++; | ||
467 | datalen = atoi(*argv); | ||
468 | break; | ||
462 | default: | 469 | default: |
463 | usage(ping_usage); | 470 | usage(ping_usage); |
464 | } | 471 | } |
@@ -472,7 +479,7 @@ extern int ping_main(int argc, char **argv) | |||
472 | ping(*argv); | 479 | ping(*argv); |
473 | exit(TRUE); | 480 | exit(TRUE); |
474 | } | 481 | } |
475 | #endif | 482 | #endif /* ! BB_SIMPLE_PING */ |
476 | 483 | ||
477 | /* | 484 | /* |
478 | * Copyright (c) 1989 The Regents of the University of California. | 485 | * Copyright (c) 1989 The Regents of the University of California. |
@@ -1,6 +1,6 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * $Id: ping.c,v 1.15 2000/05/12 19:41:47 erik Exp $ | 3 | * $Id: ping.c,v 1.16 2000/06/07 20:38:15 proski Exp $ |
4 | * Mini ping implementation for busybox | 4 | * Mini ping implementation for busybox |
5 | * | 5 | * |
6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> | 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
@@ -64,6 +64,8 @@ | |||
64 | #define CLR(bit) (A(bit) &= (~B(bit))) | 64 | #define CLR(bit) (A(bit) &= (~B(bit))) |
65 | #define TST(bit) (A(bit) & B(bit)) | 65 | #define TST(bit) (A(bit) & B(bit)) |
66 | 66 | ||
67 | static void ping(const char *host); | ||
68 | |||
67 | /* common routines */ | 69 | /* common routines */ |
68 | static int in_cksum(unsigned short *buf, int sz) | 70 | static int in_cksum(unsigned short *buf, int sz) |
69 | { | 71 | { |
@@ -104,7 +106,7 @@ static void noresp(int ign) | |||
104 | exit(0); | 106 | exit(0); |
105 | } | 107 | } |
106 | 108 | ||
107 | static int ping(const char *host) | 109 | static void ping(const char *host) |
108 | { | 110 | { |
109 | struct hostent *h; | 111 | struct hostent *h; |
110 | struct sockaddr_in pingaddr; | 112 | struct sockaddr_in pingaddr; |
@@ -113,7 +115,7 @@ static int ping(const char *host) | |||
113 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; | 115 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
114 | 116 | ||
115 | if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ | 117 | if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ |
116 | perror("ping"); | 118 | perror("ping: creating a raw socket"); |
117 | exit(1); | 119 | exit(1); |
118 | } | 120 | } |
119 | 121 | ||
@@ -140,7 +142,7 @@ static int ping(const char *host) | |||
140 | 142 | ||
141 | if (c < 0 || c != sizeof(packet)) { | 143 | if (c < 0 || c != sizeof(packet)) { |
142 | if (c < 0) | 144 | if (c < 0) |
143 | perror("ping"); | 145 | perror("ping: sendto"); |
144 | fprintf(stderr, "ping: write incomplete\n"); | 146 | fprintf(stderr, "ping: write incomplete\n"); |
145 | exit(1); | 147 | exit(1); |
146 | } | 148 | } |
@@ -156,7 +158,7 @@ static int ping(const char *host) | |||
156 | (struct sockaddr *) &from, &fromlen)) < 0) { | 158 | (struct sockaddr *) &from, &fromlen)) < 0) { |
157 | if (errno == EINTR) | 159 | if (errno == EINTR) |
158 | continue; | 160 | continue; |
159 | perror("ping"); | 161 | perror("ping: recvfrom"); |
160 | continue; | 162 | continue; |
161 | } | 163 | } |
162 | if (c >= 76) { /* ip + icmp */ | 164 | if (c >= 76) { /* ip + icmp */ |
@@ -168,7 +170,7 @@ static int ping(const char *host) | |||
168 | } | 170 | } |
169 | } | 171 | } |
170 | printf("%s is alive!\n", hostname); | 172 | printf("%s is alive!\n", hostname); |
171 | return (TRUE); | 173 | return; |
172 | } | 174 | } |
173 | 175 | ||
174 | extern int ping_main(int argc, char **argv) | 176 | extern int ping_main(int argc, char **argv) |
@@ -181,13 +183,14 @@ extern int ping_main(int argc, char **argv) | |||
181 | exit(TRUE); | 183 | exit(TRUE); |
182 | } | 184 | } |
183 | 185 | ||
184 | #else | 186 | #else /* ! BB_SIMPLE_PING */ |
185 | /* full(er) version */ | 187 | /* full(er) version */ |
186 | static const char *ping_usage = "ping [OPTION]... host\n" | 188 | static const char *ping_usage = "ping [OPTION]... host\n" |
187 | #ifndef BB_FEATURE_TRIVIAL_HELP | 189 | #ifndef BB_FEATURE_TRIVIAL_HELP |
188 | "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n" | 190 | "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n" |
189 | "Options:\n" | 191 | "Options:\n" |
190 | "\t-c COUNT\tSend only COUNT pings.\n" | 192 | "\t-c COUNT\tSend only COUNT pings.\n" |
193 | "\t-s SIZE\t\tSend SIZE data bytes in packets (default=56).\n" | ||
191 | "\t-q\t\tQuiet mode, only displays output at start\n" | 194 | "\t-q\t\tQuiet mode, only displays output at start\n" |
192 | "\t\t\tand when finished.\n" | 195 | "\t\t\tand when finished.\n" |
193 | #endif | 196 | #endif |
@@ -196,6 +199,7 @@ static const char *ping_usage = "ping [OPTION]... host\n" | |||
196 | static char *hostname = NULL; | 199 | static char *hostname = NULL; |
197 | static struct sockaddr_in pingaddr; | 200 | static struct sockaddr_in pingaddr; |
198 | static int pingsock = -1; | 201 | static int pingsock = -1; |
202 | static int datalen = DEFDATALEN; | ||
199 | 203 | ||
200 | static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0; | 204 | static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0; |
201 | static int myid = 0, options = 0; | 205 | static int myid = 0, options = 0; |
@@ -206,8 +210,6 @@ static void sendping(int); | |||
206 | static void pingstats(int); | 210 | static void pingstats(int); |
207 | static void unpack(char *, int, struct sockaddr_in *); | 211 | static void unpack(char *, int, struct sockaddr_in *); |
208 | 212 | ||
209 | static void ping(char *); | ||
210 | |||
211 | /**************************************************************************/ | 213 | /**************************************************************************/ |
212 | 214 | ||
213 | static void pingstats(int ign) | 215 | static void pingstats(int ign) |
@@ -234,7 +236,7 @@ static void sendping(int ign) | |||
234 | { | 236 | { |
235 | struct icmp *pkt; | 237 | struct icmp *pkt; |
236 | int i; | 238 | int i; |
237 | char packet[DEFDATALEN + 8]; | 239 | char packet[datalen + 8]; |
238 | 240 | ||
239 | pkt = (struct icmp *) packet; | 241 | pkt = (struct icmp *) packet; |
240 | 242 | ||
@@ -251,13 +253,11 @@ static void sendping(int ign) | |||
251 | i = sendto(pingsock, packet, sizeof(packet), 0, | 253 | i = sendto(pingsock, packet, sizeof(packet), 0, |
252 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); | 254 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
253 | 255 | ||
254 | if (i < 0 || i != sizeof(packet)) { | 256 | if (i < 0) |
255 | if (i < 0) | 257 | fatalError("ping: sendto: %s\n", strerror(errno)); |
256 | perror("ping"); | 258 | else if (i != sizeof(packet)) |
257 | fprintf(stderr, "ping wrote %d chars; %d expected\n", i, | 259 | fatalError("ping wrote %d chars; %d expected\n", i, |
258 | (int)sizeof(packet)); | 260 | (int)sizeof(packet)); |
259 | exit(1); | ||
260 | } | ||
261 | 261 | ||
262 | signal(SIGALRM, sendping); | 262 | signal(SIGALRM, sendping); |
263 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ | 263 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
@@ -303,7 +303,7 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from) | |||
303 | iphdr = (struct iphdr *) buf; | 303 | iphdr = (struct iphdr *) buf; |
304 | hlen = iphdr->ihl << 2; | 304 | hlen = iphdr->ihl << 2; |
305 | /* discard if too short */ | 305 | /* discard if too short */ |
306 | if (sz < (DEFDATALEN + ICMP_MINLEN)) | 306 | if (sz < (datalen + ICMP_MINLEN)) |
307 | return; | 307 | return; |
308 | 308 | ||
309 | sz -= hlen; | 309 | sz -= hlen; |
@@ -356,12 +356,12 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from) | |||
356 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); | 356 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); |
357 | } | 357 | } |
358 | 358 | ||
359 | static void ping(char *host) | 359 | static void ping(const char *host) |
360 | { | 360 | { |
361 | struct protoent *proto; | 361 | struct protoent *proto; |
362 | struct hostent *h; | 362 | struct hostent *h; |
363 | char buf[MAXHOSTNAMELEN]; | 363 | char buf[MAXHOSTNAMELEN]; |
364 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; | 364 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
365 | int sockopt; | 365 | int sockopt; |
366 | 366 | ||
367 | proto = getprotobyname("icmp"); | 367 | proto = getprotobyname("icmp"); |
@@ -372,7 +372,7 @@ static void ping(char *host) | |||
372 | if (errno == EPERM) { | 372 | if (errno == EPERM) { |
373 | fprintf(stderr, "ping: permission denied. (are you root?)\n"); | 373 | fprintf(stderr, "ping: permission denied. (are you root?)\n"); |
374 | } else { | 374 | } else { |
375 | perror("ping"); | 375 | perror("ping: creating a raw socket"); |
376 | } | 376 | } |
377 | exit(1); | 377 | exit(1); |
378 | } | 378 | } |
@@ -412,7 +412,7 @@ static void ping(char *host) | |||
412 | printf("PING %s (%s): %d data bytes\n", | 412 | printf("PING %s (%s): %d data bytes\n", |
413 | hostname, | 413 | hostname, |
414 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), | 414 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), |
415 | DEFDATALEN); | 415 | datalen); |
416 | 416 | ||
417 | signal(SIGINT, pingstats); | 417 | signal(SIGINT, pingstats); |
418 | 418 | ||
@@ -429,7 +429,7 @@ static void ping(char *host) | |||
429 | (struct sockaddr *) &from, &fromlen)) < 0) { | 429 | (struct sockaddr *) &from, &fromlen)) < 0) { |
430 | if (errno == EINTR) | 430 | if (errno == EINTR) |
431 | continue; | 431 | continue; |
432 | perror("ping"); | 432 | perror("ping: recvfrom"); |
433 | continue; | 433 | continue; |
434 | } | 434 | } |
435 | unpack(packet, c, &from); | 435 | unpack(packet, c, &from); |
@@ -455,10 +455,17 @@ extern int ping_main(int argc, char **argv) | |||
455 | options |= O_QUIET; | 455 | options |= O_QUIET; |
456 | break; | 456 | break; |
457 | case 'c': | 457 | case 'c': |
458 | argc--; | 458 | if (--argc <= 0) |
459 | usage(ping_usage); | ||
459 | argv++; | 460 | argv++; |
460 | pingcount = atoi(*argv); | 461 | pingcount = atoi(*argv); |
461 | break; | 462 | break; |
463 | case 's': | ||
464 | if (--argc <= 0) | ||
465 | usage(ping_usage); | ||
466 | argv++; | ||
467 | datalen = atoi(*argv); | ||
468 | break; | ||
462 | default: | 469 | default: |
463 | usage(ping_usage); | 470 | usage(ping_usage); |
464 | } | 471 | } |
@@ -472,7 +479,7 @@ extern int ping_main(int argc, char **argv) | |||
472 | ping(*argv); | 479 | ping(*argv); |
473 | exit(TRUE); | 480 | exit(TRUE); |
474 | } | 481 | } |
475 | #endif | 482 | #endif /* ! BB_SIMPLE_PING */ |
476 | 483 | ||
477 | /* | 484 | /* |
478 | * Copyright (c) 1989 The Regents of the University of California. | 485 | * Copyright (c) 1989 The Regents of the University of California. |