Basic Work With libpcap - Protocol Parsing - The Callback Function's Code (Cont.)
/* lets get the src and dst addresses - translate from */
/* network-byte-order binary data. */
inet_ntop(AF_INET, &ip_hdr->saddr, src_ip, sizeof(src_ip));
inet_ntop(AF_INET, &ip_hdr->daddr, dst_ip, sizeof(dst_ip));
/* lets get the port numbers - the payload of the IP packet is TCP. */
/* NOTE: in IP, the ihl (IP Header Length) field contains the number */
/* of 4-octet chunks composing the IP packet's header. */
tcp_hdr = (struct tcphdr*)(p_data + ip_hdr->ihl * 4);
src_port = ntohs(tcp_hdr->source); /* ports are in network byte order. */
dst_port = ntohs(tcp_hdr->dest);
printf("PACKET: src %s:%d, dst %s:%d\n",
src_ip, src_port, dst_ip, dst_port);
Originally written by
guy keren