-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.c
47 lines (35 loc) · 1.01 KB
/
count.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <pcap/pcap.h>
static int count = 0;
void callback( u_char *useless,
const struct pcap_pkthdr *h,
const u_char *bytes ) {
++count;
}
int main( int argc, char **argv ) {
char errbuf[PCAP_ERRBUF_SIZE];
if( argv[1] == NULL ) {
printf( "ERROR: first argument should be the pcap file - aborting\n" );
exit( EXIT_FAILURE );
}
pcap_t *cap_data = pcap_open_offline( argv[1], errbuf );
if( cap_data == NULL ) {
printf( "ERROR: %s - aborting\n", errbuf );
exit( EXIT_FAILURE );
}
int ret;
do {
ret = pcap_loop( cap_data, 0, callback, NULL );
} while( ret > 0 );
if( ret == -1 ) {
printf( "ERROR: %s - aborting\n", errbuf );
exit( EXIT_FAILURE );
}
if( ret == -2 ) {
printf( "ERROR: this should not happen, I never call pcap_breakloop() - aborting\n" );
exit( EXIT_FAILURE );
}
printf( "Successfully counted %d packets\n", count );
exit( EXIT_SUCCESS );
}