/*
* Written by Pejman Moghadam / 1401-02-30
* Public domain.
*
*/
/* gcc src.c -lpcap */
#include <stdio.h>
#include <stdlib.h>
#include <pcap/pcap.h>
void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
static int packet_count = 0;
printf("Packet Count: %5d\t", ++packet_count);
printf("Recieved Packet Size: %5d\n", h->len);
}
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);
}
return 0;
}
|