add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples-pcap / stats / stats.c
blobcc3885fc763fa635e2c524b9aa8d41251d412563
1 /*
2 * Copyright (c) 2006
3 * CACE Technologies, Davis, California
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * This is a test for pcap_stats(). It opens an adapter, sets a filter that
33 * drops all the packets, and then cycles printing the result of pcap_stats().
34 * The following two defines can be used to specify the time between two calls
35 * to pcap_stats(), and the kernel buffer size.
37 * Author: Loris Degioanni, April 2006
41 #include "pcap.h"
43 #define TIME_TO_SLEEP_MS 5000
44 #define KERNEL_BUFFER_SIZE_BYTES 16000
46 main()
48 pcap_if_t *alldevs;
49 pcap_if_t *d;
50 int inum;
51 int i = 0;
52 pcap_t *adhandle;
53 char errbuf[PCAP_ERRBUF_SIZE];
54 struct pcap_stat stats;
55 unsigned int j;
56 u_int netmask;
57 char packet_filter[] = "greater 30000";
58 struct bpf_program fcode;
60 /* Retrieve the device list */
61 if(pcap_findalldevs(&alldevs, errbuf) == -1)
63 fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
64 exit(1);
67 /* Print the list */
68 for(d=alldevs; d; d=d->next)
70 printf("%d. %s", ++i, d->name);
71 if (d->description)
72 printf(" (%s)\n", d->description);
73 else
74 printf(" (No description available)\n");
77 if(i==0)
79 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
80 return -1;
83 printf("Enter the interface number (1-%d):",i);
84 scanf("%d", &inum);
86 /* Check if the user specified a valid adapter */
87 if(inum < 1 || inum > i)
89 printf("\nAdapter number out of range.\n");
91 /* Free the device list */
92 pcap_freealldevs(alldevs);
93 return -1;
96 /* Jump to the selected adapter */
97 for(d = alldevs, i = 0; i < inum-1 ;d = d->next, i++);
99 /* Open the adapter */
100 if ((adhandle = pcap_open_live(d->name, // name of the device
101 65536, // portion of the packet to capture.
102 // 65536 grants that the whole packet will be captured on all the MACs.
103 1, // promiscuous mode (nonzero means promiscuous)
104 1000, // read timeout
105 errbuf // error buffer
106 )) == NULL)
108 fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
109 /* Free the device list */
110 pcap_freealldevs(alldevs);
111 return -1;
114 printf("\nlistening on %s...\n", d->description);
116 if(d->addresses != NULL)
118 /* Retrieve the mask of the first address of the interface */
119 netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
121 else
123 /* If the interface is without addresses we suppose to be in a C class network */
124 netmask=0xffffff;
127 //compile the filter
128 if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
130 fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
131 /* Free the device list */
132 pcap_freealldevs(alldevs);
133 return -1;
136 /* At this point, we don't need any more the device list. Free it */
137 pcap_freealldevs(alldevs);
139 /* set the filter */
140 if (pcap_setfilter(adhandle, &fcode)<0)
142 fprintf(stderr,"\nError setting the filter.\n");
143 /* Free the device list */
144 pcap_freealldevs(alldevs);
145 return -1;
149 /* Set the kernel capture buffer size*/
150 if(pcap_setbuff(adhandle, KERNEL_BUFFER_SIZE_BYTES) != 0)
152 printf("Error setting the kernel buffer (%s)\n", pcap_geterr(adhandle));
153 return -1;
155 pcap_close(adhandle);
158 /* cycle printing the statisctics from pcap_stats() */
159 for(j = 0; ; j++)
161 pcap_stats(adhandle, &stats);
163 printf("Recv: %u, Dropped:%u\n",
164 stats.ps_recv,
165 stats.ps_drop);
167 if(j % 5 == 0)
169 Sleep(TIME_TO_SLEEP_MS);
173 return 0;