Skip to content

Commit d209d2c

Browse files
committed
Added cping functionality.
1 parent a28c223 commit d209d2c

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

osx/ping.c

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ int sweepincr = 1; /* payload increment in sweep */
221221
int interval = 1000; /* interval between packets, ms */
222222
int waittime = MAXWAIT; /* timeout for each packet */
223223
long nrcvtimeout = 0; /* # of packets we got back after waittime */
224+
int printmaxtime_ms = 0;/* threshold for all pings to get returned before reporting them */
225+
time_t downsince = 0; /* time since last successful ping */
226+
time_t downtime = 0; /* total cumulative time of missed pings */
227+
char *target; /* host or ip entered on the command line */
224228

225229
/* timing */
226230
int timing; /* flag to do timing */
@@ -239,6 +243,7 @@ static void finish(void) __dead2;
239243
static void pinger(void);
240244
static char *pr_addr(struct in_addr);
241245
static char *pr_ntime(n_time);
246+
static char *timebufnow(char *, int, time_t);
242247
static void pr_icmph(struct icmp *);
243248
static void pr_iph(struct ip *);
244249
static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
@@ -262,7 +267,7 @@ main(argc, argv)
262267
struct sigaction si_sa;
263268
size_t sz;
264269
u_char *datap, packet[IP_MAXPACKET];
265-
char *ep, *source, *target, *payload;
270+
char *ep, *source, *payload;
266271
struct hostent *hp;
267272
#ifdef IPSEC_POLICY_IPSEC
268273
char *policy_in, *policy_out;
@@ -302,7 +307,7 @@ main(argc, argv)
302307

303308
outpack = outpackhdr + sizeof(struct ip);
304309
while ((ch = getopt(argc, argv,
305-
"Aab:c:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:"
310+
"Aab:c:DdF:fG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:"
306311
#ifdef IPSEC
307312
#ifdef IPSEC_POLICY_IPSEC
308313
"P:"
@@ -344,6 +349,9 @@ main(argc, argv)
344349
case 'd':
345350
options |= F_SO_DEBUG;
346351
break;
352+
case 'F':
353+
printmaxtime_ms = atoi(optarg);
354+
break;
347355
case 'f':
348356
if (uid) {
349357
errno = EPERM;
@@ -816,14 +824,16 @@ main(argc, argv)
816824
if (to->sin_family == AF_INET) {
817825
(void)printf("PING %s (%s)", hostname,
818826
inet_ntoa(to->sin_addr));
827+
if (printmaxtime_ms)
828+
(void)printf(" showing only pings > %d ms", printmaxtime_ms);
819829
if (source)
820830
(void)printf(" from %s", shostname);
821831
if (sweepmax)
822832
(void)printf(": (%d ... %d) data bytes\n",
823833
sweepmin, sweepmax);
824834
else
825835
(void)printf(": %d data bytes\n", datalen);
826-
836+
827837
} else {
828838
if (sweepmax)
829839
(void)printf("PING %s: (%d ... %d) data bytes\n",
@@ -974,8 +984,17 @@ main(argc, argv)
974984
nmissedmax = ntransmitted - nreceived - 1;
975985
if (options & F_MISSED)
976986
(void)write(STDOUT_FILENO, &BBELL, 1);
977-
if (!(options & F_QUIET))
978-
printf("Request timeout for icmp_seq %ld\n", ntransmitted - 2);
987+
if (!(options & F_QUIET)) {
988+
if (printmaxtime_ms > 0) {
989+
char outstr[200];
990+
991+
if (!downsince)
992+
downsince = time(NULL);
993+
(void)printf("\r%s (%s) DOWN: %ld s, total: %ld ",
994+
timebufnow(outstr,sizeof(outstr),downsince), target, time(NULL)-downsince , nmissedmax); fflush(stdout);
995+
} else
996+
printf("Request timeout for icmp_seq %ld\n", ntransmitted - 2);
997+
}
979998
}
980999
}
9811000
}
@@ -984,6 +1003,16 @@ main(argc, argv)
9841003
exit(0); /* Make the compiler happy */
9851004
}
9861005

1006+
static char *timebufnow(char *outstr, int len, time_t now)
1007+
{
1008+
struct tm *tmp;
1009+
1010+
if (!now) now = time(NULL);
1011+
tmp = localtime(&now);
1012+
strftime(outstr, len, "%a, %d %b %Y %T %z", tmp);
1013+
return outstr;
1014+
}
1015+
9871016
/*
9881017
* stopit --
9891018
* Set the global bit that causes the main loop to quit.
@@ -1067,7 +1096,14 @@ pinger(void)
10671096
usleep(FLOOD_BACKOFF);
10681097
return;
10691098
}
1070-
warn("sendto");
1099+
if (printmaxtime_ms==0) {
1100+
warn("sendto");
1101+
} else {
1102+
if ((errno!=64)&&(errno!=65)) {
1103+
fprintf(stderr,"\nErrno: %d ", errno);
1104+
warn("sendto");
1105+
}
1106+
}
10711107
} else {
10721108
warn("%s: partial write: %d of %d bytes",
10731109
hostname, i, cc);
@@ -1098,7 +1134,7 @@ pr_pack(buf, cc, from, tv)
10981134
struct icmp *icp;
10991135
struct ip *ip;
11001136
const void *tp;
1101-
double triptime;
1137+
double triptime=0.0;
11021138
int dupflag, hlen, i, j, recv_len, seq;
11031139
static int old_rrlen;
11041140
static char old_rr[MAX_IPOPTLEN];
@@ -1171,16 +1207,29 @@ pr_pack(buf, cc, from, tv)
11711207

11721208
if (options & F_FLOOD)
11731209
(void)write(STDOUT_FILENO, &BSPACE, 1);
1174-
else {
1175-
(void)printf("%d bytes from %s: icmp_seq=%u", cc,
1176-
inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
1177-
seq);
1178-
(void)printf(" ttl=%d", ip->ip_ttl);
1179-
if (timing)
1180-
(void)printf(" time=%.3f ms", triptime);
1181-
if (dupflag) {
1182-
if (!IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)))
1183-
(void)printf(" (DUP!)");
1210+
else if ( triptime >= printmaxtime_ms ) {
1211+
if (downsince>0) {
1212+
char outstr[200];
1213+
1214+
downtime+=downsince;
1215+
printf("\n%s (%s) UP. Down for %ld s, total downtime: %ld\n", timebufnow(outstr, sizeof(outstr), 0), target, time(NULL)-downsince, downtime);
1216+
downsince = 0;
1217+
}
1218+
if (printmaxtime_ms) {
1219+
char t[200];
1220+
(void)printf("%s (%s) %.3f ms > %d.0 ms",
1221+
timebufnow(t,sizeof(t),0), target, triptime, printmaxtime_ms );
1222+
} else {
1223+
(void)printf("%d bytes from %s: icmp_seq=%u", cc,
1224+
inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
1225+
seq);
1226+
(void)printf(" ttl=%d", ip->ip_ttl);
1227+
if (timing)
1228+
(void)printf(" time=%.3f ms", triptime);
1229+
if (dupflag) {
1230+
if (!IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)))
1231+
(void)printf(" (DUP!)");
1232+
}
11841233
}
11851234
if (options & F_AUDIBLE)
11861235
(void)write(STDOUT_FILENO, &BBELL, 1);
@@ -1346,7 +1395,7 @@ pr_pack(buf, cc, from, tv)
13461395
(void)printf("\nunknown option %x", *cp);
13471396
break;
13481397
}
1349-
if (!(options & F_FLOOD)) {
1398+
if (!(options & F_FLOOD) && ( triptime >= printmaxtime_ms )) {
13501399
(void)putchar('\n');
13511400
(void)fflush(stdout);
13521401
}

0 commit comments

Comments
 (0)