Sync ping and ping6 a bit, fix style and indentation.

This commit is contained in:
Denis Vlasenko 2006-09-02 16:13:36 +00:00
parent d53dd3e627
commit cb6874cc66
2 changed files with 32 additions and 35 deletions

View File

@ -31,7 +31,6 @@
#include <stdlib.h> #include <stdlib.h>
#include "busybox.h" #include "busybox.h"
enum { enum {
DEFDATALEN = 56, DEFDATALEN = 56,
MAXIPLEN = 60, MAXIPLEN = 60,
@ -78,7 +77,8 @@ static int in_cksum(unsigned short *buf, int sz)
/* simple version */ /* simple version */
#ifndef CONFIG_FEATURE_FANCY_PING #ifndef CONFIG_FEATURE_FANCY_PING
static char *hostname = NULL; static char *hostname;
static void noresp(int ign) static void noresp(int ign)
{ {
printf("No response from %s\n", hostname); printf("No response from %s\n", hostname);
@ -163,10 +163,7 @@ static int myid, options;
static unsigned long tmin = ULONG_MAX, tmax, tsum; static unsigned long tmin = ULONG_MAX, tmax, tsum;
static char rcvd_tbl[MAX_DUP_CHK / 8]; static char rcvd_tbl[MAX_DUP_CHK / 8];
#ifndef CONFIG_FEATURE_FANCY_PING6 static struct hostent *hostent;
static
#endif
struct hostent *hostent;
static void sendping(int); static void sendping(int);
static void pingstats(int); static void pingstats(int);
@ -278,11 +275,11 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from)
icmppkt = (struct icmp *) (buf + hlen); icmppkt = (struct icmp *) (buf + hlen);
if (icmppkt->icmp_id != myid) if (icmppkt->icmp_id != myid)
return; /* not our ping */ return; /* not our ping */
if (icmppkt->icmp_type == ICMP_ECHOREPLY) { if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
u_int16_t recv_seq = ntohs(icmppkt->icmp_seq); u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
++nreceived; ++nreceived;
tp = (struct timeval *) icmppkt->icmp_data; tp = (struct timeval *) icmppkt->icmp_data;
if ((tv.tv_usec -= tp->tv_usec) < 0) { if ((tv.tv_usec -= tp->tv_usec) < 0) {
@ -352,7 +349,7 @@ static void ping(const char *host)
sizeof(sockopt)); sizeof(sockopt));
printf("PING %s (%s): %d data bytes\n", printf("PING %s (%s): %d data bytes\n",
hostent->h_name, hostent->h_name,
inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
datalen); datalen);
@ -400,13 +397,13 @@ int ping_main(int argc, char **argv)
break; break;
case 'c': case 'c':
if (--argc <= 0) if (--argc <= 0)
bb_show_usage(); bb_show_usage();
argv++; argv++;
pingcount = atoi(*argv); pingcount = atoi(*argv);
break; break;
case 's': case 's':
if (--argc <= 0) if (--argc <= 0)
bb_show_usage(); bb_show_usage();
argv++; argv++;
datalen = atoi(*argv); datalen = atoi(*argv);
break; break;

View File

@ -68,7 +68,7 @@ static void ping(const char *host);
#ifndef CONFIG_FEATURE_FANCY_PING6 #ifndef CONFIG_FEATURE_FANCY_PING6
static struct hostent *h; static struct hostent *h;
void noresp(int ign) static void noresp(int ign)
{ {
printf("No response from %s\n", h->h_name); printf("No response from %s\n", h->h_name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -101,8 +101,10 @@ static void ping(const char *host)
c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0, c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0,
(struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6)); (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
if (c < 0 || c != sizeof(packet)) if (c < 0 || c != sizeof(packet)) {
if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
bb_perror_msg_and_die("sendto"); bb_perror_msg_and_die("sendto");
}
signal(SIGALRM, noresp); signal(SIGALRM, noresp);
alarm(5); /* give the host 5000ms to respond */ alarm(5); /* give the host 5000ms to respond */
@ -124,6 +126,7 @@ static void ping(const char *host)
break; break;
} }
} }
if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
printf("%s is alive!\n", h->h_name); printf("%s is alive!\n", h->h_name);
return; return;
} }
@ -150,10 +153,7 @@ static int myid, options;
static unsigned long tmin = ULONG_MAX, tmax, tsum; static unsigned long tmin = ULONG_MAX, tmax, tsum;
static char rcvd_tbl[MAX_DUP_CHK / 8]; static char rcvd_tbl[MAX_DUP_CHK / 8];
# ifdef CONFIG_FEATURE_FANCY_PING static struct hostent *hostent;
extern
# endif
struct hostent *hostent;
static void sendping(int); static void sendping(int);
static void pingstats(int); static void pingstats(int);
@ -267,10 +267,10 @@ static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit
icmppkt = (struct icmp6_hdr *) packet; icmppkt = (struct icmp6_hdr *) packet;
if (icmppkt->icmp6_id != myid) if (icmppkt->icmp6_id != myid)
return; /* not our ping */ return; /* not our ping */
if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) { if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
++nreceived; ++nreceived;
tp = (struct timeval *) &icmppkt->icmp6_data8[4]; tp = (struct timeval *) &icmppkt->icmp6_data8[4];
if ((tv.tv_usec -= tp->tv_usec) < 0) { if ((tv.tv_usec -= tp->tv_usec) < 0) {
@ -339,7 +339,7 @@ static void ping(const char *host)
struct icmp6_filter filt; struct icmp6_filter filt;
if (!(options & O_VERBOSE)) { if (!(options & O_VERBOSE)) {
ICMP6_FILTER_SETBLOCKALL(&filt); ICMP6_FILTER_SETBLOCKALL(&filt);
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt); ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
} else { } else {
ICMP6_FILTER_SETPASSALL(&filt); ICMP6_FILTER_SETPASSALL(&filt);
} }
@ -374,8 +374,8 @@ static void ping(const char *host)
printf("PING %s (%s): %d data bytes\n", printf("PING %s (%s): %d data bytes\n",
hostent->h_name, hostent->h_name,
inet_ntop(AF_INET6, &pingaddr.sin6_addr, inet_ntop(AF_INET6, &pingaddr.sin6_addr,
buf, sizeof(buf)), buf, sizeof(buf)),
datalen); datalen);
signal(SIGINT, pingstats); signal(SIGINT, pingstats);
@ -384,18 +384,18 @@ static void ping(const char *host)
sendping(0); sendping(0);
/* listen for replies */ /* listen for replies */
msg.msg_name=&from; msg.msg_name = &from;
msg.msg_namelen=sizeof(from); msg.msg_namelen = sizeof(from);
msg.msg_iov=&iov; msg.msg_iov = &iov;
msg.msg_iovlen=1; msg.msg_iovlen = 1;
msg.msg_control=control_buf; msg.msg_control = control_buf;
iov.iov_base=packet; iov.iov_base = packet;
iov.iov_len=sizeof(packet); iov.iov_len = sizeof(packet);
while (1) { while (1) {
int c; int c;
struct cmsghdr *cmsgptr = NULL; struct cmsghdr *cmsgptr = NULL;
int hoplimit=-1; int hoplimit = -1;
msg.msg_controllen=sizeof(control_buf); msg.msg_controllen = sizeof(control_buf);
if ((c = recvmsg(pingsock, &msg, 0)) < 0) { if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
if (errno == EINTR) if (errno == EINTR)
@ -407,7 +407,7 @@ static void ping(const char *host)
cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
if (cmsgptr->cmsg_level == SOL_IPV6 && if (cmsgptr->cmsg_level == SOL_IPV6 &&
cmsgptr->cmsg_type == IPV6_HOPLIMIT ) { cmsgptr->cmsg_type == IPV6_HOPLIMIT ) {
hoplimit=*(int*)CMSG_DATA(cmsgptr); hoplimit = *(int*)CMSG_DATA(cmsgptr);
} }
} }
unpack(packet, c, &from, hoplimit); unpack(packet, c, &from, hoplimit);
@ -441,19 +441,19 @@ int ping6_main(int argc, char **argv)
break; break;
case 'c': case 'c':
if (--argc <= 0) if (--argc <= 0)
bb_show_usage(); bb_show_usage();
argv++; argv++;
pingcount = atoi(*argv); pingcount = atoi(*argv);
break; break;
case 's': case 's':
if (--argc <= 0) if (--argc <= 0)
bb_show_usage(); bb_show_usage();
argv++; argv++;
datalen = atoi(*argv); datalen = atoi(*argv);
break; break;
case 'I': case 'I':
if (--argc <= 0) if (--argc <= 0)
bb_show_usage(); bb_show_usage();
argv++; argv++;
ifname = *argv; ifname = *argv;
break; break;