socket is now RAW, ft_ping now check the received packet

This commit is contained in:
2024-09-12 22:51:47 +02:00
parent c32e36c7e1
commit 71be209ee6

11
main.c
View File

@@ -30,7 +30,6 @@ int ft_ping(struct t_socket sock, int *seq)
sock.fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); sock.fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock.fd < 0) if (sock.fd < 0)
{ {
printf(strerror(errno));
fprintf(stderr, "ERROR : socket() failed\n"); fprintf(stderr, "ERROR : socket() failed\n");
return (0); return (0);
} }
@@ -63,6 +62,16 @@ int ft_ping(struct t_socket sock, int *seq)
fprintf(stderr, "ERROR : invalid packet received (code)\n"); fprintf(stderr, "ERROR : invalid packet received (code)\n");
return (0); return (0);
} }
if (icmp_recv_header->un.echo.id != icmp_header->un.echo.id)
{
fprintf(stderr, "ERROR : invalid packet received (id is not matching)\n");
return (0);
}
if (icmp_recv_header->un.echo.sequence != icmp_header->un.echo.sequence)
{
fprintf(stderr, "ERROR : invalid packet received (sequence is not matching)\n");
return (0);
}
printf("success"); printf("success");
close(sock.fd); close(sock.fd);
return (1); return (1);