From 4351a2e4bbbf5df481d676cb4cadc757f2abd84c Mon Sep 17 00:00:00 2001 From: Namonay Date: Thu, 11 Sep 2025 14:23:16 +0200 Subject: [PATCH] fix: loopback issues --- src/main.c | 17 ++++++++++++----- src/packets.c | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 65b6bb6..ba82d45 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include "ft_ping.h" +#include "icmp_codes.h" struct packet_stats stats; bool loop = true; @@ -18,8 +19,8 @@ static int ft_ping(const int sock, const uint16_t seq, const struct sockaddr_in if (sendto(sock, packet.data, sizeof(packet.data), 0, (struct sockaddr *)dst, sizeof(struct sockaddr_in)) == -1) { - fprintf(stderr, "error : sending packet : Network is unreachable\n"); - return (0); + // fprintf(stderr, "error : sending packet : Network is unreachable\n"); + // return (0); } #ifdef DEBUG printf("\e[1;31m[DEBUG]\e[1;00m sendto() packet header: type:%d code:%d checksum:%x id:%d icmp_seq:%d\n", packet.icmp_hdr->type, packet.icmp_hdr->code, packet.icmp_hdr->checksum, packet.icmp_hdr->id, packet.icmp_hdr->seq); @@ -35,8 +36,9 @@ static int ft_recv(const int sock, const uint16_t seq, const double start, const double time; uint16_t checksum; - packet.icmp_hdr = (struct net_icmp_header *)(packet.data + 20); // get the packet icmp_header (starts at 20 bytes) memset(packet.data, 0, sizeof(packet.data)); + packet.icmp_hdr = (struct net_icmp_header *)(packet.data + 20); // get the packet icmp_header (starts at 20 bytes) + packet.icmp_hdr->type = -1; packet.n_bytes = recvfrom(sock, packet.data, sizeof(packet.data), 0, (struct sockaddr *)&packet.addr, (socklen_t *)&len); if (packet.n_bytes < 1) return (1); @@ -45,8 +47,13 @@ static int ft_recv(const int sock, const uint16_t seq, const double start, const packet.icmp_hdr->checksum = 0; if (parse_packet(packet, seq, checksum) == false) { - print_packet_error(packet); - return (1); + if (packet.icmp_hdr->type == ICMP_ECHO) + packet.n_bytes = recvfrom(sock, packet.data, sizeof(packet.data), 0, (struct sockaddr *)&packet.addr, (socklen_t *)&len); + else + { + print_packet_error(packet); + return (1); + } } fill_timestamp_array(&stats, time); stats.n_packet_recv++; diff --git a/src/packets.c b/src/packets.c index 644df21..07643e8 100644 --- a/src/packets.c +++ b/src/packets.c @@ -16,10 +16,10 @@ static const char *get_error_message(const uint8_t type, const uint8_t code) } bool parse_packet(const struct net_packet packet, const uint16_t seq, const uint16_t checksum) { - if (packet.icmp_hdr->seq != seq || make_checksum((uint16_t *)packet.icmp_hdr, sizeof(*packet.icmp_hdr)) != checksum) - return (false); if (packet.icmp_hdr->type != ICMP_ECHOREPLY) return (false); + if (packet.icmp_hdr->seq != seq || make_checksum((uint16_t *)packet.icmp_hdr, sizeof(*packet.icmp_hdr)) != checksum) + return (false); return (true); }