add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples / PacketDriver / TestPacketSend / TestPacketSend.c
blobea3f6e8444278b775ac508afd571a4daedbb41e7
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 <stdio.h>
35 #include <conio.h>
36 #include <time.h>
38 #include "..\..\..\Include\packet32.h"
41 #define Max_Num_Adapter 10
43 // Prototypes
45 void PrintPackets(LPPACKET lpPacket);
47 char AdapterList[Max_Num_Adapter][8192];
51 int main(int argc, char **argv)
53 char packetbuff[5000];
55 // define a pointer to a ADAPTER structure
57 LPADAPTER lpAdapter = 0;
59 // define a pointer to a PACKET structure
61 LPPACKET lpPacket;
63 int i,npacks,Snaplen;
64 DWORD dwErrorCode;
66 //ascii strings
67 char AdapterName[8192]; // string that contains a list of the network adapters
68 char *temp,*temp1;
70 int AdapterNum=0,Open;
71 ULONG AdapterLength;
73 float cpu_time;
75 printf("Traffic Generator v 0.9999\nCopyright 1999 Loris Degioanni (loris@netgroup-serv.polito.it)");
76 printf("\nSends a set of packets to the network using packet.dll API.\n");
78 if (argc == 1){
79 printf("\n\n Usage: TestpacketSend [-i adapter] -n npacks -s size");
80 printf("\n size is between 60 and 1514\n\n");
81 return -1;
85 AdapterName[0]=0;
87 //get the command line parameters
88 for(i=1;i<argc;i+=2){
90 switch (argv[i] [1])
93 case 'i':
94 sscanf(argv[i+1],"%s",AdapterName);
95 break;
97 case 'n':
98 sscanf(argv[i+1],"%d",&npacks);
99 break;
101 case 's':
102 sscanf(argv[i+1],"%d",&Snaplen);
103 break;
111 if(AdapterName[0]==0){
114 // Obtain the name of the adapters installed on this machine
116 printf("Adapters installed:\n");
117 i=0;
119 AdapterLength = sizeof(AdapterName);
121 if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
122 printf("Unable to retrieve the list of the adapters!\n");
123 return -1;
125 temp=AdapterName;
126 temp1=AdapterName;
128 while ((*temp!='\0')||(*(temp-1)!='\0'))
130 if (*temp=='\0')
132 memcpy(AdapterList[i],temp1,temp-temp1);
133 temp1=temp+1;
134 i++;
136 temp++;
139 AdapterNum=i;
140 for (i=0;i<AdapterNum;i++)
141 printf("\n%d- %s\n",i+1,AdapterList[i]);
142 printf("\n");
146 printf("Select the number of the adapter to open : ");scanf("%d",&Open);
147 if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum);
148 } while (Open>AdapterNum);
153 lpAdapter = PacketOpenAdapter(AdapterList[Open-1]);
155 if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
157 dwErrorCode=GetLastError();
158 printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode);
160 return(-1);
164 else{
166 lpAdapter = PacketOpenAdapter(AdapterName);
168 if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
170 dwErrorCode=GetLastError();
171 printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode);
173 return(-1);
178 if((lpPacket = PacketAllocatePacket())==NULL){
179 printf("\nError:failed to allocate the LPPACKET structure.");
180 return (-1);
183 packetbuff[0]=1;
184 packetbuff[1]=1;
185 packetbuff[2]=1;
186 packetbuff[3]=1;
187 packetbuff[4]=1;
188 packetbuff[5]=1;
190 packetbuff[6]=2;
191 packetbuff[7]=2;
192 packetbuff[8]=2;
193 packetbuff[9]=2;
194 packetbuff[10]=2;
195 packetbuff[11]=2;
197 for(i=12;i<1514;i++){
198 packetbuff[i]=i%256;
201 PacketInitPacket(lpPacket,packetbuff,Snaplen);
202 // capture the packet
205 if(PacketSetNumWrites(lpAdapter,npacks)==FALSE){
206 printf("warning: Unable to send more than one packet in a single write!\n");
209 printf("\n\nGenerating %d packets...",npacks);
211 cpu_time = (float)clock ();
213 if(PacketSendPacket(lpAdapter,lpPacket,TRUE)==FALSE){
214 printf("Error sending the packets!\n");
215 return -1;
218 cpu_time = (clock() - cpu_time)/CLK_TCK;
220 printf ("\n\nElapsed time: %5.3f\n", cpu_time);
221 printf ("\nTotal packets generated = %d", npacks);
222 printf ("\nTotal bytes generated = %d", (Snaplen+24)*npacks);
223 printf ("\nTotal bits generated = %d", (Snaplen+24)*npacks*8);
224 printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));
225 printf ("\nAverage bytes per second = %d", (int)((double)((Snaplen+24)*npacks)/cpu_time));
226 printf ("\nAverage bits per second = %d", (int)((double)((Snaplen+24)*npacks*8)/cpu_time));
227 printf ("\n");
229 PacketFreePacket(lpPacket);
231 // close the adapter and exit
233 PacketCloseAdapter(lpAdapter);
234 return (0);