/* gcc src.c -lpcap */

#include <stdio.h>
#include <stdlib.h>
#include <pcap/pcap.h>

int packet_count = 0;

void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
	packet_count++;
}

int main()
{
	pcap_t *p;
	char errbuf[PCAP_ERRBUF_SIZE];
	p = pcap_open_offline("capture.pcap", errbuf);
	if (p == NULL) {
		fprintf(stderr, "\npcap_open_offline() failed: %s\n", errbuf);
		exit(EXIT_FAILURE);
	}

	if (pcap_loop(p, 0, packet_handler, NULL) < 0) {
		fprintf(stderr, "\npcap_loop() failed: %s\n", pcap_geterr(p));
		exit(EXIT_FAILURE);
	}

	fprintf(stdout, "Packet Count = %d\n", packet_count);

	return 0;
}