add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples-pcap / readfile_ex / readfile_ex.c
blob2324c0651dfe4d010da6f103208d98bc30e0ef3f
1 #include <stdio.h>
2 #include <pcap.h>
4 #define LINE_LEN 16
6 int main(int argc, char **argv)
8 pcap_t *fp;
9 char errbuf[PCAP_ERRBUF_SIZE];
10 struct pcap_pkthdr *header;
11 const u_char *pkt_data;
12 u_int i=0;
13 int res;
15 if(argc != 2)
17 printf("usage: %s filename", argv[0]);
18 return -1;
22 /* Open the capture file */
23 if ((fp = pcap_open_offline(argv[1], // name of the device
24 errbuf // error buffer
25 )) == NULL)
27 fprintf(stderr,"\nUnable to open the file %s.\n", argv[1]);
28 return -1;
31 /* Retrieve the packets from the file */
32 while((res = pcap_next_ex(fp, &header, &pkt_data)) >= 0)
34 /* print pkt timestamp and pkt len */
35 printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
37 /* Print the packet */
38 for (i=1; (i < header->caplen + 1 ) ; i++)
40 printf("%.2x ", pkt_data[i-1]);
41 if ( (i % LINE_LEN) == 0) printf("\n");
44 printf("\n\n");
48 if (res == -1)
50 printf("Error reading the packets: %s\n", pcap_geterr(fp));
53 pcap_close(fp);
54 return 0;