Initial commit from project. lib dependencies need checking
[packeteer.git] / test-libpcap.c
1 /*
2  * Focused test app to enable checking of libpcap with valgrind
3  * 
4  * Build with: $ gcc -W -Wall -g test-libpcap.c -lpcap -o test-libpcap
5  */
6
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <pcap.h>
11 #include <netinet/in.h>
12
13 int main(int argc, char **argv) {
14         pcap_t *descr=NULL;
15         char *dev = NULL, *device="eth1";
16         pcap_if_t *this_dev, *alldevsp;
17         char *pcap_filter = "tcp port 80";
18         char errbuf[PCAP_ERRBUF_SIZE];
19         struct bpf_program fp;
20
21         printf("Starting %s with %d arguments\n", argv[0], argc );
22         printf("Using %s\n", pcap_lib_version());
23         printf("Device %s wanted\n", device);
24         
25         printf("calling pcap_findalldevs()\n");
26         if (pcap_findalldevs(&alldevsp, errbuf) == 0) {
27          printf("pcap_findalldevs() done.\n");
28          this_dev = alldevsp;
29          printf("while()\n");
30          while (this_dev != NULL) {
31           printf("device %s\n",this_dev->name);
32           if (strcmp(device, this_dev->name) == 0) {
33            dev = this_dev->name;
34            break;
35           }
36           this_dev = this_dev->next;
37          } 
38         }
39         else {
40          fprintf(stderr, "Error: pcap_findalldevs()\n");         
41         }
42         this_dev =NULL;
43         pcap_freealldevs(alldevsp); // free list memory
44         
45         dev = pcap_lookupdev(errbuf);
46         printf("pcap_lookupdev() done\n");
47         if (dev != NULL) {
48                 descr = pcap_open_live(dev, BUFSIZ, 0, 1024, errbuf);
49                 printf("pcap_open_live() done\n");
50                 if (descr != NULL) {
51                         if(pcap_compile(descr, &fp, pcap_filter, 0, htonl(0xFFFF0000)) != -1)   {
52                                   printf("pcap_compile() done\n");
53                                   if(pcap_setfilter(descr, &fp) == 0) {
54                                           printf("pcap_setfilter() done\n");
55                                   }
56                                   pcap_freecode(&fp);
57                         }
58                         pcap_close(descr);
59                 }
60                 dev=NULL;
61         }
62         
63         printf("Finished\n");
64         return 0;
65 }