add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples / smp_1 / smp_1.c
blob46be6049bd8d40496c947e811fdd0f5ea6663c55
1 /*
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <stdlib.h>
36 #include <stdio.h>
38 #include <pcap.h>
41 main(int argc, char **argv)
43 pcap_if_t *alldevs, *d;
44 pcap_t *fp;
45 u_int inum, i=0;
46 char errbuf[PCAP_ERRBUF_SIZE];
47 int res;
48 struct pcap_pkthdr *header;
49 const u_char *pkt_data;
50 struct pcap_pkthdr old;
52 char a[11];
54 printf("SMP_1\n");
55 printf("\nThis program tests the WinPcap kernel driver on SMP machines.\n");
56 printf("The program tests that timestamps on the captured packets are consistent,\n");
57 printf("and that the caplen is equal to the packet length.\n");
58 printf("If there is an error, it will print out a message saying \"Inconsistent XXX\"\n");
60 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
62 fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
63 exit(1);
66 /* Print the list */
67 for(d=alldevs; d; d=d->next)
69 printf("%d. %s", ++i, d->name);
70 if (d->description)
71 printf(" (%s)\n", d->description);
72 else
73 printf(" (No description available)\n");
76 if(i==0)
78 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
79 return -1;
82 printf("Enter the interface number (1-%d):",i);
83 scanf("%d", &inum);
85 if(inum < 1 || inum > i)
87 printf("\nInterface number out of range.\n");
88 /* Free the device list */
89 pcap_freealldevs(alldevs);
90 return -1;
93 /* Jump to the selected adapter */
94 for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
96 /* Open the device */
97 if ( (fp= pcap_open(d->name, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
99 fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
100 /* Free the device list */
101 pcap_freealldevs(alldevs);
102 return -1;
105 old.ts.tv_sec=0;
106 old.ts.tv_usec=0;
109 /* Read the packets */
110 while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0){
112 if(res == 0)
113 continue;
115 //check that caplen is equal to packet length
116 if (header->caplen!=header->len)
117 printf("Inconsistent header: CapLen %d\t Len %d\n",header->caplen,header->len);
119 //check that timestamps always grow
120 if ( old.ts.tv_sec > header->ts.tv_sec || (old.ts.tv_sec == header->ts.tv_sec && old.ts.tv_usec > header->ts.tv_usec))
121 printf("Inconsistent Timestamps! Old was %d.%.06d - New is %d.%.06d\n",old.ts.tv_sec,old.ts.tv_usec, header->ts.tv_sec,header->ts.tv_usec);
123 old=*header;
127 if(res == -1){
128 printf("Error reading the packets: %s\n", pcap_geterr(fp));
129 return -1;
132 scanf("%s",a);
134 return 0;