2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 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, 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
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.
38 #include <remote-ext.h>
42 void main(int argc
, char **argv
)
44 pcap_t
*indesc
,*outdesc
;
45 char errbuf
[PCAP_ERRBUF_SIZE
];
46 char source
[PCAP_BUF_SIZE
];
50 pcap_send_queue
*squeue
;
51 struct pcap_pkthdr
*pktheader
;
56 /* Check the validity of the command line */
57 if (argc
<= 2 || argc
>= 5)
63 /* Retrieve the length of the capture file */
64 capfile
=fopen(argv
[1],"rb");
66 printf("Capture file not found!\n");
70 fseek(capfile
, 0, SEEK_END
);
71 caplen
= ftell(capfile
)- sizeof(struct pcap_file_header
);
74 /* Chek if the timestamps must be respected */
75 if(argc
== 4 && argv
[3][0] == 's')
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
85 NULL
, // port on the remote host
86 argv
[1], // name of the file we want to open
87 errbuf
// error buffer
90 fprintf(stderr
,"\nError creating a source string\n");
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
);
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
);
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");
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");
133 printf("Corrupted input file.\n");
134 pcap_sendqueue_destroy(squeue
);
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
));
154 /* free the send queue */
155 pcap_sendqueue_destroy(squeue
);
157 /* Close the input file */
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!
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");