fix: loopback issues

This commit is contained in:
2025-09-11 14:23:16 +02:00
parent 078701117d
commit 4351a2e4bb
2 changed files with 14 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
#include "ft_ping.h" #include "ft_ping.h"
#include "icmp_codes.h"
struct packet_stats stats; struct packet_stats stats;
bool loop = true; 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) 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"); // fprintf(stderr, "error : sending packet : Network is unreachable\n");
return (0); // return (0);
} }
#ifdef DEBUG #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); 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; double time;
uint16_t checksum; 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)); 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); packet.n_bytes = recvfrom(sock, packet.data, sizeof(packet.data), 0, (struct sockaddr *)&packet.addr, (socklen_t *)&len);
if (packet.n_bytes < 1) if (packet.n_bytes < 1)
return (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; packet.icmp_hdr->checksum = 0;
if (parse_packet(packet, seq, checksum) == false) if (parse_packet(packet, seq, checksum) == false)
{ {
print_packet_error(packet); if (packet.icmp_hdr->type == ICMP_ECHO)
return (1); 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); fill_timestamp_array(&stats, time);
stats.n_packet_recv++; stats.n_packet_recv++;

View File

@@ -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) 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) if (packet.icmp_hdr->type != ICMP_ECHOREPLY)
return (false); return (false);
if (packet.icmp_hdr->seq != seq || make_checksum((uint16_t *)packet.icmp_hdr, sizeof(*packet.icmp_hdr)) != checksum)
return (false);
return (true); return (true);
} }