🔨」 fix: fixed some things.

This commit is contained in:
2025-08-17 23:17:00 +02:00
parent 33a65e4ac6
commit b4b66a2bb9
4 changed files with 38 additions and 29 deletions

View File

@ -17,6 +17,7 @@ extern int rx_count;
extern char *address;
extern double *times;
extern int sock;
extern char **hosts;
extern bool verbose;
typedef struct {

View File

@ -11,6 +11,7 @@ double *times = NULL;
int rx_count = 0;
int tx_count = 0;
bool verbose = DEFAULT_VERBOSE;
char **hosts;
void init_args_t(args_t *args) {
OPT_WHILE {

View File

@ -28,11 +28,15 @@ void sigint(int sig) {
close(sock);
print_stats();
// free(times);
// free(hosts);
exit(EXIT_SUCCESS);
}
options_t init_opt(args_t *args) {
options_t opt;
hosts = args->hosts;
verbose = args->opts[VERBOSE] != false ? true : DEFAULT_VERBOSE;
opt.count = args->arg[COUNT] != -1 ? args->arg[COUNT] : DEFAULT_COUNT;
if (args->arg[SIZE] > sizeof(struct icmphdr) || args->arg[SIZE] == -1) {
@ -84,12 +88,16 @@ int send_ping(int socket, struct sockaddr_in *dest, options_t opt) {
process_icmp(recvbuf, bytes, &addr, seq, &pkt_start, &pkt_end);
if (check_for_count(opt)) {
print_stats();
free(times);
times = NULL;
return EXIT_SUCCESS;
}
sleep(opt.interval);
if (check_for_timeout(loop_start, opt)) {
print_stats();
free(times);
times = NULL;
return EXIT_SUCCESS;
}
}
@ -100,35 +108,32 @@ int ping(args_t *args) {
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;
struct addrinfo hints, *res;
address = args->hosts[0];
struct sockaddr_in addr;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = IPPROTO_ICMP;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = IPPROTO_ICMP;
int status = getaddrinfo(address, NULL, &hints, &res);
if (status != 0) {
fprintf(stderr, "Bad address: %s\n", address);
ret = EXIT_FAILURE;
continue;
}
memcpy(&addr, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
sock = init_socket(opt);
if (sock < 0) {
break;
}
printf("PING %s (%s): %d data bytes\n", address,
inet_ntoa(addr.sin_addr), opt.size);
ret = send_ping(sock, &addr, opt);
int status = getaddrinfo(address, NULL, &hints, &res);
if (status != 0) {
fprintf(stderr, "Bad address: %s\n", address);
ret = EXIT_FAILURE;
}
memcpy(&addr, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
sock = init_socket(opt);
if (sock < 0) {
return -1;
}
printf("PING %s (%s): %d data bytes\n", address, inet_ntoa(addr.sin_addr),
opt.size);
ret = send_ping(sock, &addr, opt);
return ret;
}

View File

@ -61,8 +61,9 @@ bool check_for_timeout(struct timeval start, options_t opt) {
double span =
(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
if (span > opt.timeout)
if (span > opt.timeout) {
return true;
}
return false;
}
@ -77,7 +78,8 @@ void print_stats(void) {
bool check_for_count(options_t opt) {
if (opt.count == -1)
return false;
if (tx_count >= opt.count)
if (tx_count >= opt.count) {
return true;
}
return false;
}