add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples-pcap / savedump / savedump.c
blob8c7ead42134574e0b9073259feeac24a9a3080fe
1 #include "pcap.h"
3 /* prototype of the packet handler */
4 void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
6 main(int argc, char **argv)
8 pcap_if_t *alldevs;
9 pcap_if_t *d;
10 int inum;
11 int i=0;
12 pcap_t *adhandle;
13 char errbuf[PCAP_ERRBUF_SIZE];
14 pcap_dumper_t *dumpfile;
17 /* Check command line */
18 if(argc != 2)
20 printf("usage: %s filename", argv[0]);
21 return -1;
24 /* Retrieve the device list on the local machine */
25 if (pcap_findalldevs(&alldevs, errbuf) == -1)
27 fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
28 exit(1);
31 /* Print the list */
32 for(d=alldevs; d; d=d->next)
34 printf("%d. %s", ++i, d->name);
35 if (d->description)
36 printf(" (%s)\n", d->description);
37 else
38 printf(" (No description available)\n");
41 if(i==0)
43 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
44 return -1;
47 printf("Enter the interface number (1-%d):",i);
48 scanf("%d", &inum);
50 if(inum < 1 || inum > i)
52 printf("\nInterface number out of range.\n");
53 /* Free the device list */
54 pcap_freealldevs(alldevs);
55 return -1;
58 /* Jump to the selected adapter */
59 for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
62 /* Open the adapter */
63 if ((adhandle= pcap_open_live(d->name, // name of the device
64 65536, // portion of the packet to capture.
65 // 65536 grants that the whole packet will be captured on all the MACs.
66 1, // promiscuous mode (nonzero means promiscuous)
67 1000, // read timeout
68 errbuf // error buffer
69 )) == NULL)
71 fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
72 /* Free the device list */
73 pcap_freealldevs(alldevs);
74 return -1;
77 /* Open the dump file */
78 dumpfile = pcap_dump_open(adhandle, argv[1]);
80 if(dumpfile==NULL)
82 fprintf(stderr,"\nError opening output file\n");
83 return -1;
86 printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
88 /* At this point, we no longer need the device list. Free it */
89 pcap_freealldevs(alldevs);
91 /* start the capture */
92 pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);
94 pcap_close(adhandle);
95 return 0;
98 /* Callback function invoked by libpcap for every incoming packet */
99 void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
101 /* save the packet on the dump file */
102 pcap_dump(dumpfile, header, pkt_data);