🏗️」 wip: don't understand whats going on

This commit is contained in:
2025-08-16 18:53:02 +02:00
parent 162de73a21
commit b548965d2e
7 changed files with 61 additions and 32 deletions

View File

@ -1,7 +1,7 @@
#include "help.h"
#include <cerrno>
#include <opt_parse.h>
#include <ping.h>
#include <signal.h>
#include <utils.h>
#include <arpa/inet.h>
@ -17,9 +17,15 @@
#include <sys/types.h>
#include <unistd.h>
char *address = NULL;
int sock = -1;
void sigint(int sig) {
(void)sig;
close(sock);
print_stats();
exit(EXIT_SUCCESS);
}
@ -37,8 +43,7 @@ options_t init_opt(args_t *args) {
return opt;
}
int send_ping(int socket, struct sockaddr_in *dest, args_t *args) {
options_t opt = init_opt(args);
int send_ping(int socket, struct sockaddr_in *dest, options_t opt) {
struct timeval loop_start, loop_end;
struct timeval pkt_start, pkt_end;
struct sockaddr_in addr;
@ -52,42 +57,50 @@ int send_ping(int socket, struct sockaddr_in *dest, args_t *args) {
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");
fprintf(stderr, "recv failed\n");
}
} else
process_icmp(recvbuf, bytes, &addr, tx_count - 1, &pkt_end,
&pkt_end);
if (check_for_count(opt)) {
print_stats();
return EXIT_SUCCESS;
}
sleep(opt.interval);
if (check_for_timeout(loop_start, opt)) {
print_stats();
return EXIT_SUCCESS;
}
}
return EXIT_SUCCESS;
}
int ping(args_t *args) {
int ret;
int ret;
options_t opt = init_opt(args);
signal(SIGINT, &sigint);
for (int i = 0; args->hosts[i] != NULL; i++) {
address = args->hosts[i];
struct sockaddr_in addr;
if (inet_pton(AF_INET, args->hosts[i], &addr) <= 0) {
fprintf(stderr, "Bar address: %s\n", args->hosts[i]);
fprintf(stderr, "Bad address: %s\n", args->hosts[i]);
return EXIT_FAILURE;
}
int socket = init_socket(args);
if (socket < 0) {
sock = init_socket(opt);
if (sock < 0) {
break;
}
printf("PING %s (%s): %d data bytes\n", args->hosts[i], args->hosts[i],
args->opts[SIZE]);
printf("PING %s (%s): %d data bytes\n", address, address, opt.size);
ret = send_ping(socket, &addr, args);
ret = send_ping(sock, &addr, opt);
}
return ret;
}