3 * CACE Technologies, Davis, California
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
43 #define TIME_TO_SLEEP_MS 5000
44 #define KERNEL_BUFFER_SIZE_BYTES 16000
53 char errbuf
[PCAP_ERRBUF_SIZE
];
54 struct pcap_stat stats
;
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
);
68 for(d
=alldevs
; d
; d
=d
->next
)
70 printf("%d. %s", ++i
, d
->name
);
72 printf(" (%s)\n", d
->description
);
74 printf(" (No description available)\n");
79 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
83 printf("Enter the interface number (1-%d):",i
);
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
);
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
108 fprintf(stderr
,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
109 /* Free the device list */
110 pcap_freealldevs(alldevs
);
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
;
123 /* If the interface is without addresses we suppose to be in a C class network */
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
);
136 /* At this point, we don't need any more the device list. Free it */
137 pcap_freealldevs(alldevs
);
140 if (pcap_setfilter(adhandle
, &fcode
)<0)
142 fprintf(stderr
,"\nError setting the filter.\n");
143 /* Free the device list */
144 pcap_freealldevs(alldevs
);
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
));
155 pcap_close(adhandle
);
158 /* cycle printing the statisctics from pcap_stats() */
161 pcap_stats(adhandle
, &stats
);
163 printf("Recv: %u, Dropped:%u\n",
169 Sleep(TIME_TO_SLEEP_MS
);