🏗️」 wip: wtf is happening

This commit is contained in:
2025-08-17 21:14:31 +02:00
parent 4c13d78559
commit de4d18ccb7
4 changed files with 94 additions and 40 deletions

View File

@ -6,19 +6,21 @@
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
char *address = NULL;
int sock = -1;
int sock = -1;
void sigint(int sig) {
(void)sig;
@ -46,7 +48,7 @@ options_t init_opt(args_t *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;
struct sockaddr_in addr = *dest;
int bytes;
char sendbuf[opt.size];
@ -54,24 +56,33 @@ int send_ping(int socket, struct sockaddr_in *dest, options_t opt) {
gettimeofday(&loop_start, 0);
while (727) {
init_packet(sendbuf, tx_count++, opt.size);
int seq = tx_count;
printf("seq = %d\n", seq);
init_packet(sendbuf, seq, opt.size);
// write(1, sendbuf, opt.size);
gettimeofday(&pkt_start, NULL);
bytes = send_icmp(socket, sendbuf, &addr, opt.size);
tx_count++;
bytes = receive_icmp(socket, recvbuf, sizeof(recvbuf), &addr);
gettimeofday(&pkt_end, NULL);
if (bytes < 0) {
if (bytes <= 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
printf("Request timeout for icmp_seq=%d\n", tx_count - 1);
printf("Request timeout for icmp_seq=%d\n", seq);
} else {
fprintf(stderr, "recv failed\n");
fprintf(stderr, "recv failed: %s\n", strerror(errno));
}
} else
process_icmp(recvbuf, bytes, &addr, tx_count - 1, &pkt_end,
process_icmp(recvbuf, bytes, &addr, seq, &pkt_start,
&pkt_end);
if (check_for_count(opt)) {
print_stats();
return EXIT_SUCCESS;
}
sleep(opt.interval);
if (check_for_timeout(loop_start, opt)) {
print_stats();
@ -89,11 +100,23 @@ int ping(args_t *args) {
for (int i = 0; args->hosts[i] != NULL; i++) {
address = args->hosts[i];
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;
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);
if (inet_pton(AF_INET, args->hosts[i], &addr) <= 0) {
fprintf(stderr, "Bad address: %s\n", args->hosts[i]);
return EXIT_FAILURE;
}
sock = init_socket(opt);
if (sock < 0) {
break;