add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples / misc / basic_dump_ex.c
blobaa4f3c85397d83b1aaab10dbb1306b1005d7a104
1 #include "pcap.h"
4 main()
6 pcap_if_t *alldevs;
7 pcap_if_t *d;
8 int inum;
9 int i=0;
10 pcap_t *adhandle;
11 int res;
12 char errbuf[PCAP_ERRBUF_SIZE];
13 struct tm *ltime;
14 char timestr[16];
15 struct pcap_pkthdr *header;
16 const u_char *pkt_data;
17 time_t local_tv_sec;
20 /* Retrieve the device list on the local machine */
21 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
23 fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
24 exit(1);
27 /* Print the list */
28 for(d=alldevs; d; d=d->next)
30 printf("%d. %s", ++i, d->name);
31 if (d->description)
32 printf(" (%s)\n", d->description);
33 else
34 printf(" (No description available)\n");
37 if(i==0)
39 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
40 return -1;
43 printf("Enter the interface number (1-%d):",i);
44 scanf("%d", &inum);
46 if(inum < 1 || inum > i)
48 printf("\nInterface number out of range.\n");
49 /* Free the device list */
50 pcap_freealldevs(alldevs);
51 return -1;
54 /* Jump to the selected adapter */
55 for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
57 /* Open the device */
58 if ( (adhandle= pcap_open(d->name, // name of the device
59 65536, // portion of the packet to capture.
60 // 65536 guarantees that the whole packet will be captured on all the link layers
61 PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
62 1000, // read timeout
63 NULL, // authentication on the remote machine
64 errbuf // error buffer
65 ) ) == NULL)
67 fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
68 /* Free the device list */
69 pcap_freealldevs(alldevs);
70 return -1;
73 printf("\nlistening on %s...\n", d->description);
75 /* At this point, we don't need any more the device list. Free it */
76 pcap_freealldevs(alldevs);
78 /* Retrieve the packets */
79 while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
81 if(res == 0)
82 /* Timeout elapsed */
83 continue;
85 /* convert the timestamp to readable format */
86 local_tv_sec = header->ts.tv_sec;
87 ltime=localtime(&local_tv_sec);
88 strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
90 printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
93 if(res == -1){
94 printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
95 return -1;
98 return 0;