fix: loopback issues
This commit is contained in:
17
src/main.c
17
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++;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user