add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples / pktdump_ex / pktdump_ex.c
bloba0d3fa9839f276e1d2f85a4fb289fd3345f95194
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>
39 // NOTE: remember to include WPCAP and HAVE_REMOTE among your
40 // preprocessor definitions.
43 #include <pcap.h>
45 #define LINE_LEN 16
47 main(int argc, char **argv)
49 pcap_if_t *alldevs, *d;
50 pcap_t *fp;
51 u_int inum, i=0;
52 char errbuf[PCAP_ERRBUF_SIZE];
53 int res;
54 struct pcap_pkthdr *header;
55 const u_char *pkt_data;
57 printf("pktdump_ex: prints the packets of the network using WinPcap.\n");
58 printf(" Usage: pktdump_ex [-s source]\n\n"
59 " Examples:\n"
60 " pktdump_ex -s file://c:/temp/file.acp\n"
61 " pktdump_ex -s rpcap://\\Device\\NPF_{C8736017-F3C3-4373-94AC-9A34B7DAD998}\n\n");
63 if(argc < 3)
66 printf("\nNo adapter selected: printing the device list:\n");
67 /* The user didn't provide a packet source: Retrieve the local device list */
68 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
70 fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
71 return -1;
74 /* Print the list */
75 for(d=alldevs; d; d=d->next)
77 printf("%d. %s\n ", ++i, d->name);
79 if (d->description)
80 printf(" (%s)\n", d->description);
81 else
82 printf(" (No description available)\n");
85 if (i==0)
87 fprintf(stderr,"No interfaces found! Exiting.\n");
88 return -1;
91 printf("Enter the interface number (1-%d):",i);
92 scanf("%d", &inum);
94 if (inum < 1 || inum > i)
96 printf("\nInterface number out of range.\n");
98 /* Free the device list */
99 pcap_freealldevs(alldevs);
100 return -1;
103 /* Jump to the selected adapter */
104 for (d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
106 /* Open the device */
107 if ( (fp= pcap_open(d->name,
108 100 /*snaplen*/,
109 PCAP_OPENFLAG_PROMISCUOUS /*flags*/,
110 20 /*read timeout*/,
111 NULL /* remote authentication */,
112 errbuf)
113 ) == NULL)
115 fprintf(stderr,"\nError opening adapter\n");
116 return -1;
119 else
121 // Do not check for the switch type ('-s')
122 if ( (fp= pcap_open(argv[2],
123 100 /*snaplen*/,
124 PCAP_OPENFLAG_PROMISCUOUS /*flags*/,
125 20 /*read timeout*/,
126 NULL /* remote authentication */,
127 errbuf)
128 ) == NULL)
130 fprintf(stderr,"\nError opening source: %s\n", errbuf);
131 return -1;
135 /* Read the packets */
136 while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
139 if(res == 0)
140 /* Timeout elapsed */
141 continue;
143 /* print pkt timestamp and pkt len */
144 printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
146 /* Print the packet */
147 for (i=1; (i < header->caplen + 1 ) ; i++)
149 printf("%.2x ", pkt_data[i-1]);
150 if ( (i % LINE_LEN) == 0) printf("\n");
153 printf("\n\n");
156 if(res == -1)
158 fprintf(stderr, "Error reading the packets: %s\n", pcap_geterr(fp));
159 return -1;
162 return 0;