added avg, using floats instead of fixed points for min/max/avg/stddev
This commit is contained in:
2
Makefile
2
Makefile
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
# Project Meta Settings
|
# Project Meta Settings
|
||||||
NAME = ft_ping
|
NAME = ft_ping
|
||||||
RELEASE_TYPE = Debug
|
RELEASE_TYPE = Release
|
||||||
CURRENT_DIR = ${CURDIR}
|
CURRENT_DIR = ${CURDIR}
|
||||||
|
|
||||||
# Project Build Output Settings
|
# Project Build Output Settings
|
||||||
|
@ -28,9 +28,4 @@ struct ping_pkt {
|
|||||||
char msg[64 - sizeof(struct icmphdr)];
|
char msg[64 - sizeof(struct icmphdr)];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ping_delta {
|
|
||||||
int ms_top;
|
|
||||||
int ms_bottom;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //MAIN_H
|
#endif //MAIN_H
|
||||||
|
60
srcs/main.c
60
srcs/main.c
@ -25,7 +25,7 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state) {
|
|||||||
break;
|
break;
|
||||||
case ARG_TTL:
|
case ARG_TTL:
|
||||||
arguments->ttl = strtoul(arg, NULL, 10);
|
arguments->ttl = strtoul(arg, NULL, 10);
|
||||||
__attribute__((fallthrough));
|
break;
|
||||||
case ARGP_KEY_ARG:
|
case ARGP_KEY_ARG:
|
||||||
if (state->arg_num >= 2)
|
if (state->arg_num >= 2)
|
||||||
argp_usage(state);
|
argp_usage(state);
|
||||||
@ -108,45 +108,18 @@ void sig_handler(int sig) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ping_delta get_min(struct ping_delta a, struct ping_delta b) {
|
|
||||||
if (a.ms_top < b.ms_top) {
|
|
||||||
return a;
|
|
||||||
} else if (b.ms_top < a.ms_top) {
|
|
||||||
return b;
|
|
||||||
} else {
|
|
||||||
if (a.ms_bottom < b.ms_bottom) {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ping_delta get_max(struct ping_delta a, struct ping_delta b) {
|
|
||||||
if (a.ms_top > b.ms_top) {
|
|
||||||
return a;
|
|
||||||
} else if (b.ms_top > a.ms_top) {
|
|
||||||
return b;
|
|
||||||
} else {
|
|
||||||
if (a.ms_bottom > b.ms_bottom) {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
struct arguments arguments;
|
struct arguments arguments;
|
||||||
int timeout = 3000;
|
int timeout = 3000;
|
||||||
struct timespec recvt;
|
struct timespec recvt;
|
||||||
struct pollfd fds[1];
|
struct pollfd fds[1];
|
||||||
struct ping_delta min;
|
float min;
|
||||||
struct ping_delta max;
|
float max;
|
||||||
|
float avg;
|
||||||
|
|
||||||
min.ms_bottom = INT32_MAX;
|
min = INT32_MAX;
|
||||||
min.ms_top = INT32_MAX;
|
max = 0;
|
||||||
|
avg = 0;
|
||||||
max.ms_bottom = 0;
|
|
||||||
max.ms_top = 0;
|
|
||||||
|
|
||||||
arguments.verbose = 0;
|
arguments.verbose = 0;
|
||||||
arguments.ttl = 64;
|
arguments.ttl = 64;
|
||||||
@ -205,18 +178,17 @@ int main(int argc, char** argv) {
|
|||||||
struct ip* ip_header = (struct ip*)buf;
|
struct ip* ip_header = (struct ip*)buf;
|
||||||
struct icmphdr* icmp_reply = (struct icmphdr*)(buf + (ip_header->ip_hl << 2));
|
struct icmphdr* icmp_reply = (struct icmphdr*)(buf + (ip_header->ip_hl << 2));
|
||||||
|
|
||||||
struct ping_delta tmp;
|
float tmp =
|
||||||
|
|
||||||
tmp.ms_top =
|
|
||||||
((recvt.tv_sec * 1000 + recvt.tv_nsec / 1000000) - (sendt.tv_sec * 1000 + sendt.tv_nsec / 1000000));
|
((recvt.tv_sec * 1000 + recvt.tv_nsec / 1000000) - (sendt.tv_sec * 1000 + sendt.tv_nsec / 1000000));
|
||||||
tmp.ms_bottom = ((recvt.tv_nsec - sendt.tv_nsec) % 1000000 / 1000);
|
tmp += ((float) ((recvt.tv_nsec - sendt.tv_nsec) % 1000000) / 1000000.0);
|
||||||
|
|
||||||
printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%d.%d ms \n", bytes_received, arguments.args[0],
|
printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%.3f ms \n", bytes_received, arguments.args[0],
|
||||||
icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp.ms_top, tmp.ms_bottom);
|
icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp);
|
||||||
|
|
||||||
|
min = (((min) < (tmp)) ? (min) : (tmp));
|
||||||
|
max = (((max) > (tmp)) ? (max) : (tmp));
|
||||||
|
avg += tmp;
|
||||||
recvd++;
|
recvd++;
|
||||||
|
|
||||||
max = get_max(max, tmp);
|
|
||||||
min = get_min(min, tmp);
|
|
||||||
}
|
}
|
||||||
} else if (ret == 0) {
|
} else if (ret == 0) {
|
||||||
printf("No reply received within the timeout period\n");
|
printf("No reply received within the timeout period\n");
|
||||||
@ -229,6 +201,6 @@ int main(int argc, char** argv) {
|
|||||||
printf("---- %s ping statistics ----\n", arguments.args[0]);
|
printf("---- %s ping statistics ----\n", arguments.args[0]);
|
||||||
printf("%zu packets transmitted, %zu packets received, %zu%% packet loss\n", xmit, recvd,
|
printf("%zu packets transmitted, %zu packets received, %zu%% packet loss\n", xmit, recvd,
|
||||||
(xmit - recvd) / xmit * 100);
|
(xmit - recvd) / xmit * 100);
|
||||||
printf("round-trip min/avg/max/stddev = %d.%d/%d.%d/%%.3f/%%.3f ms\n", min.ms_top, min.ms_bottom, max.ms_top, max.ms_bottom);
|
printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%%.3f ms\n", min, max, avg / recvd);
|
||||||
close(sock);
|
close(sock);
|
||||||
}
|
}
|
Reference in New Issue
Block a user