From 5f4180862ddc8cd9315dc2a593cbc8ac02b4d23e Mon Sep 17 00:00:00 2001 From: "Jolan \"Blenderwizard\" Rathelot" Date: Mon, 19 May 2025 14:19:17 +0200 Subject: [PATCH] fixed ip addr output --- srcs/main.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/srcs/main.c b/srcs/main.c index 507427a..2e66f1f 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -162,7 +162,7 @@ double nsqrt(double a, double prec) { return c; } -void print_ip_hdr(struct ip* ip_header, char * ipsrc, char *ipdst) { +void print_ip_hdr(struct ip* ip_header, char* ipsrc, char* ipdst) { printf("IP Hdr dump:\n "); for (size_t i = 0; i < sizeof(*ip_header); i++) { printf("%02x", *((unsigned char*)ip_header + i)); @@ -317,12 +317,17 @@ int main(int argc, char** argv) { clock_gettime(CLOCK_MONOTONIC, &recvt); struct ip* ip_header = (struct ip*)buf; - struct ip *cp = (struct ip*) ((unsigned char*)ip_header + (ip_header->ip_hl << 2) + sizeof(struct icmphdr)); + struct ip* cp = + (struct ip*)((unsigned char*)ip_header + (ip_header->ip_hl << 2) + sizeof(struct icmphdr)); char strip[INET_ADDRSTRLEN] = {0}; char strip2[INET_ADDRSTRLEN] = {0}; - inet_ntop(AF_INET, &(cp->ip_src.s_addr), strip, INET_ADDRSTRLEN); - inet_ntop(AF_INET, &(cp->ip_dst.s_addr), strip2, INET_ADDRSTRLEN); + char strip3[INET_ADDRSTRLEN] = {0}; + char strip4[INET_ADDRSTRLEN] = {0}; + inet_ntop(AF_INET, &(ip_header->ip_src.s_addr), strip, INET_ADDRSTRLEN); + inet_ntop(AF_INET, &(ip_header->ip_dst.s_addr), strip2, INET_ADDRSTRLEN); + inet_ntop(AF_INET, &(cp->ip_src.s_addr), strip3, INET_ADDRSTRLEN); + inet_ntop(AF_INET, &(cp->ip_dst.s_addr), strip4, INET_ADDRSTRLEN); struct icmphdr* icmp_reply = (struct icmphdr*)(buf + (ip_header->ip_hl << 2)); if (icmp_reply->type != ICMP_ECHOREPLY) { @@ -343,7 +348,7 @@ int main(int argc, char** argv) { printf("%d bytes from %s: %s\n", ntohs(ip_header->ip_len) - (ip_header->ip_hl << 2), strip, str); if (arguments.verbose) { - print_ip_hdr(cp, strip, strip2); + print_ip_hdr(cp, strip3, strip4); } if (!stop)