🏗️」 wip: kinda working

This commit is contained in:
2025-08-15 23:49:43 +02:00
parent 057fb25cf7
commit 162de73a21
5 changed files with 114 additions and 18 deletions

View File

@ -1,9 +1,11 @@
#include "help.h"
#include <cerrno>
#include <opt_parse.h>
#include <ping.h>
#include <utils.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
@ -16,10 +18,9 @@
#include <unistd.h>
void sigint(int sig) {
printf("--- %s ping statistics ---", address);
printf("%d packets transmitted, %d packets received, %f%% packet loss",
tx_count, rx_count, (tx_count - rx_count / 2.0) * 100);
printf("round-trip min/avg/max/stddev = %f/%f/%f/%f", get_min_rtt(), get_avg_rtt(), get_max_rtt(), get_stddev_rtt());
(void)sig;
exit(EXIT_SUCCESS);
}
options_t init_opt(args_t *args) {
@ -36,13 +37,57 @@ options_t init_opt(args_t *args) {
return opt;
}
int send_ping(args_t *args) {
options_t opt = init_opt(args);
struct timeval loop_start;
struct timeval loop_end;
int send_ping(int socket, struct sockaddr_in *dest, args_t *args) {
options_t opt = init_opt(args);
struct timeval loop_start, loop_end;
struct timeval pkt_start, pkt_end;
struct sockaddr_in addr;
int bytes;
char sendbuf[opt.size];
char recvbuf[opt.size + sizeof(struct ip)];
gettimeofday(&loop_start, 0);
while () {
while (727) {
init_packet(sendbuf, tx_count++, opt.size);
gettimeofday(&pkt_start, NULL);
bytes = send_icmp(socket, sendbuf, &addr, opt.size);
sleep(1);
if (check_for_timeout(loop_start, opt)) {
print_stats();
return EXIT_SUCCESS;
}
bytes = receive_icmp(socket, recvbuf, sizeof(recvbuf), &addr);
gettimeofday(&pkt_end, NULL);
if (bytes < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
printf("Request timeout for icmp_seq=%d\n", tx_count - 1);
} else {
fprintf(stderr, "recv failed");
}
} else
process_icmp(recvbuf, bytes, &addr, tx_count - 1, &pkt_end,
&pkt_end);
}
return EXIT_SUCCESS;
}
int ping(args_t *args) {
int ret;
for (int i = 0; args->hosts[i] != NULL; i++) {
struct sockaddr_in addr;
if (inet_pton(AF_INET, args->hosts[i], &addr) <= 0) {
fprintf(stderr, "Bar address: %s\n", args->hosts[i]);
return EXIT_FAILURE;
}
int socket = init_socket(args);
if (socket < 0) {
}
printf("PING %s (%s): %d data bytes\n", args->hosts[i], args->hosts[i],
args->opts[SIZE]);
ret = send_ping(socket, &addr, args);
}
return ret;
}