diff --git a/src/main.c b/src/main.c index 2009aa9..72c6d3c 100644 --- a/src/main.c +++ b/src/main.c @@ -140,11 +140,13 @@ static int preload(struct setting const *settings, int sockfd, char const *packe return 0; } -int check_reply(struct sockaddr_in const *me, struct sockaddr_in const *sender, char *sent, char *received, size_t packet_size) +static int check_reply(struct sockaddr_in const *me, struct sockaddr_in const *sender, char *sent, char *received, size_t sent_size, size_t recv_size) { + if (sent_size != recv_size) + return 0; if (((struct icmphdr *)sent)->type != 0) return 0; - if (check_packet_conformity(sent, received, packet_size)) + if (check_packet_conformity(sent, received, sent_size)) return 1; return memcmp(sender, me, sizeof(struct sockaddr_in)); } @@ -207,20 +209,16 @@ int main(int ac, char **av) do { ret = recvfrom(sockfd, buffer, recv_packet_size, 0, (struct sockaddr *) &sender, &len); - if (ret < 0) - { - print_err("error: receive packet failed.%zd", ret); - goto error3; - } gettimeofday(&stop, NULL); } - while (check_reply(&settings.dest.ip, &sender, packet, buffer + sizeof(struct iphdr), packet_size)); + while (ret >= 0 && check_reply(&settings.dest.ip, &sender, packet, buffer + sizeof(struct iphdr), packet_size, ret)); - if (((struct icmphdr *) buffer + sizeof(struct iphdr))->type == 0) + if (ret >= (ssize_t) sizeof(struct icmphdr) && ((struct icmphdr *) buffer + sizeof(struct iphdr))->type == 0) + { stats.packets_received++; + print_recv(&settings, (struct icmphdr*) (buffer + sizeof(struct iphdr)), &start, &stop, &sender); + } - print_recv(&settings, (struct icmphdr*) (buffer + sizeof(struct iphdr)), &start, &stop, &sender); - sleep(1); if (packet_update(packet, settings.payload_size))