diff --git a/srcs/main.c b/srcs/main.c index 0ef4b99..507427a 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) { +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)); @@ -176,15 +176,14 @@ void print_ip_hdr(struct ip* ip_header) { printf(" %1x %1x %02x %04x %04x %1x %04x %02x %02x %04x %s %s ", ip_header->ip_v, ip_header->ip_hl, ip_header->ip_tos, (ip_header->ip_len > 0x2000) ? ntohs(ip_header->ip_len) : ip_header->ip_len, ntohs(ip_header->ip_id), (ntohs(ip_header->ip_off) & 0xe000) >> 13, ntohs(ip_header->ip_off) & 0x1fff, - ip_header->ip_ttl, ip_header->ip_p, ntohs(ip_header->ip_sum), - inet_ntoa(*((struct in_addr*)&ip_header->ip_src)), inet_ntoa(*((struct in_addr*)&ip_header->ip_dst))); + ip_header->ip_ttl, ip_header->ip_p, ntohs(ip_header->ip_sum), ipsrc, ipdst); unsigned char* cp = (unsigned char*)ip_header + sizeof(*ip_header); for (size_t l = ip_header->ip_hl << 2; l > sizeof(*ip_header); l--) { printf("%02x", *cp++); } printf("\n"); - cp = (unsigned char*)ip_header + (ip_header->ip_hl << 2) * 2 + sizeof(struct icmphdr); + cp = (unsigned char*)ip_header + (ip_header->ip_hl << 2); if (ip_header->ip_p == IPPROTO_TCP) { printf("TCP: from port %u, to port %u (decimal)\n", (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); @@ -318,16 +317,19 @@ 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)); char strip[INET_ADDRSTRLEN] = {0}; - inet_ntop(AF_INET, &(ip_header->ip_src.s_addr), strip, INET_ADDRSTRLEN); + 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); struct icmphdr* icmp_reply = (struct icmphdr*)(buf + (ip_header->ip_hl << 2)); if (icmp_reply->type != ICMP_ECHOREPLY) { if (icmp_reply->type == ICMP_ECHO && ip_header->ip_src.s_addr == localhost.sin_addr.s_addr) { continue; } - if (icmp_reply->type == ICMP_ECHO && ip_header->ip_dst.s_addr == broadcast.sin_addr.s_addr) { + if (icmp_reply->type == ICMP_ECHO && strcmp(strip, strip) == 0) { continue; } char* str = ""; @@ -341,7 +343,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(ip_header); + print_ip_hdr(cp, strip, strip2); } if (!stop)