84 lines
1.7 KiB
C
84 lines
1.7 KiB
C
#include <ping.h>
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/time.h>
|
|
#include <unistd.h>
|
|
#include <math.h>
|
|
|
|
void append_time(double time) {
|
|
if (times == NULL) {
|
|
times = malloc(sizeof(double));
|
|
times[0] = time;
|
|
} else {
|
|
times = reallocarray(times, rx_count, sizeof(double));
|
|
times[rx_count - 1] = time;
|
|
}
|
|
}
|
|
|
|
double get_max_rtt(void) {
|
|
double max = 0;
|
|
for (int i = 0; i < rx_count; i++) {
|
|
if (times[i] > max)
|
|
max = times[i];
|
|
}
|
|
return max;
|
|
}
|
|
|
|
double get_min_rtt(void) {
|
|
double min = times[0];
|
|
for (int i = 0; i < rx_count; i++) {
|
|
if (times[i] < min)
|
|
min = times[i];
|
|
}
|
|
return min;
|
|
}
|
|
|
|
double get_avg_rtt(void) {
|
|
double avg = 0;
|
|
for (int i = 0; i < rx_count; i++) {
|
|
avg += times[i];
|
|
}
|
|
avg = avg / rx_count;
|
|
if (avg < 0) {
|
|
avg = 0;
|
|
}
|
|
return avg;
|
|
}
|
|
|
|
double get_stddev_rtt(void) {
|
|
double stddev = 0.0;
|
|
for (int i = 0; i < rx_count; i++) {
|
|
stddev += pow(times[i] - get_avg_rtt(), 2);
|
|
}
|
|
stddev = sqrt(stddev / rx_count);
|
|
return stddev;
|
|
}
|
|
|
|
bool check_for_timeout(struct timeval start, options_t opt) {
|
|
struct timeval end;
|
|
gettimeofday(&end, NULL);
|
|
|
|
double span =
|
|
(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
|
|
if (span > opt.timeout)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
void print_stats(void) {
|
|
printf("--- %s ping statistics ---\n", address);
|
|
printf("%d packets transmitted, %d packets received, %d%% packet loss\n",
|
|
tx_count, rx_count, (100 * (tx_count - rx_count) / tx_count));
|
|
printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f\n",
|
|
get_min_rtt(), get_avg_rtt(), get_max_rtt(), get_stddev_rtt());
|
|
}
|
|
|
|
bool check_for_count(options_t opt) {
|
|
if (opt.count == -1)
|
|
return false;
|
|
if (tx_count >= opt.count)
|
|
return true;
|
|
return false;
|
|
}
|