add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples / sendcap / sendcap.c
blobef1ee470a144dca1fccb1444aad7a605e4b9ca2d
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.
34 #include <stdlib.h>
35 #include <stdio.h>
37 #include <pcap.h>
38 #include <remote-ext.h>
40 void usage();
42 void main(int argc, char **argv)
44 pcap_t *indesc,*outdesc;
45 char errbuf[PCAP_ERRBUF_SIZE];
46 char source[PCAP_BUF_SIZE];
47 FILE *capfile;
48 int caplen, sync;
49 u_int res;
50 pcap_send_queue *squeue;
51 struct pcap_pkthdr *pktheader;
52 u_char *pktdata;
53 float cpu_time;
54 u_int npacks = 0;
56 /* Check the validity of the command line */
57 if (argc <= 2 || argc >= 5)
59 usage();
60 return;
63 /* Retrieve the length of the capture file */
64 capfile=fopen(argv[1],"rb");
65 if(!capfile){
66 printf("Capture file not found!\n");
67 return;
70 fseek(capfile , 0, SEEK_END);
71 caplen= ftell(capfile)- sizeof(struct pcap_file_header);
72 fclose(capfile);
74 /* Chek if the timestamps must be respected */
75 if(argc == 4 && argv[3][0] == 's')
76 sync = TRUE;
77 else
78 sync = FALSE;
80 /* Open the capture */
81 /* Create the source string according to the new WinPcap syntax */
82 if ( pcap_createsrcstr( source, // variable that will keep the source string
83 PCAP_SRC_FILE, // we want to open a file
84 NULL, // remote host
85 NULL, // port on the remote host
86 argv[1], // name of the file we want to open
87 errbuf // error buffer
88 ) != 0)
90 fprintf(stderr,"\nError creating a source string\n");
91 return;
94 /* Open the capture file */
95 if ( (indesc= pcap_open(source, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
97 fprintf(stderr,"\nUnable to open the file %s.\n", source);
98 return;
101 /* Open the output adapter */
102 if ( (outdesc= pcap_open(argv[2], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
104 fprintf(stderr,"\nUnable to open adapter %s.\n", source);
105 return;
108 /* Check the MAC type */
109 if (pcap_datalink(indesc) != pcap_datalink(outdesc))
111 printf("Warning: the datalink of the capture differs from the one of the selected interface.\n");
112 printf("Press a key to continue, or CTRL+C to stop.\n");
113 getchar();
116 /* Allocate a send queue */
117 squeue = pcap_sendqueue_alloc(caplen);
119 /* Fill the queue with the packets from the file */
120 while ((res = pcap_next_ex( indesc, &pktheader, &pktdata)) == 1)
122 if (pcap_sendqueue_queue(squeue, pktheader, pktdata) == -1)
124 printf("Warning: packet buffer too small, not all the packets will be sent.\n");
125 break;
128 npacks++;
131 if (res == -1)
133 printf("Corrupted input file.\n");
134 pcap_sendqueue_destroy(squeue);
135 return;
138 /* Transmit the queue */
140 cpu_time = (float)clock ();
142 if ((res = pcap_sendqueue_transmit(outdesc, squeue, sync)) < squeue->len)
144 printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", pcap_geterr(outdesc), res);
147 cpu_time = (clock() - cpu_time)/CLK_TCK;
149 printf ("\n\nElapsed time: %5.3f\n", cpu_time);
150 printf ("\nTotal packets generated = %d", npacks);
151 printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));
152 printf ("\n");
154 /* free the send queue */
155 pcap_sendqueue_destroy(squeue);
157 /* Close the input file */
158 pcap_close(indesc);
161 * lose the output adapter
162 * IMPORTANT: remember to close the adapter, otherwise there will be no guarantee that all the
163 * packets will be sent!
165 pcap_close(outdesc);
168 return;
172 void usage()
175 printf("\nSendcap, sends a libpcap/tcpdump capture file to the net. Copyright (C) 2002 Loris Degioanni.\n");
176 printf("\nUsage:\n");
177 printf("\t sendcap file_name adapter [s]\n");
178 printf("\nParameters:\n");
179 printf("\nfile_name: the name of the dump file that will be sent to the network\n");
180 printf("\nadapter: the device to use. Use \"WinDump -D\" for a list of valid devices\n");
181 printf("\ns: if present, forces the packets to be sent synchronously, i.e. respecting the timestamps in the dump file. This option will work only under Windows NTx.\n\n");
183 exit(0);