Basic Work With libpcap - Protocol Parsing - The Callback Function's Code

Here is a code to print the source/destination data for TCP traffic. */
static int packet_count = 0;
struct iphdr* ip_hdr;          /* to get IP protocol data.  */
struct tcphdr* tcp_hdr;        /* to get TCP protocol data. */
char src_ip[100], dst_ip[100];
int src_port, dst_port;

/* we're only interested in TCP packets. */
ip_hdr = (struct iphdr*)p_data;  /* the captured data is an IP packet. */
if (ip_hdr->protocol != IPPROTO_TCP) {
    printf("protocol in IP packet (0x%x) is not TCP\n", ip_hdr->protocol);
    return;
}
Originally written by Valid HTML 4.01!guy keren