Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

TestPacketSend.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999 - 2003
00003  * NetGroup, Politecnico di Torino (Italy)
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions 
00008  * are met:
00009  * 
00010  * 1. Redistributions of source code must retain the above copyright 
00011  * notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright 
00013  * notice, this list of conditions and the following disclaimer in the 
00014  * documentation and/or other materials provided with the distribution. 
00015  * 3. Neither the name of the Politecnico di Torino nor the names of its 
00016  * contributors may be used to endorse or promote products derived from 
00017  * this software without specific prior written permission. 
00018  * 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00023  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  * 
00031  */
00032 
00033 
00034 #include <stdio.h>
00035 #include <conio.h>
00036 #include <time.h>
00037 
00038 #include "..\..\..\Include\packet32.h"
00039 
00040 
00041 #define Max_Num_Adapter 10
00042 
00043 // Prototypes
00044 
00045 void PrintPackets(LPPACKET lpPacket);
00046 
00047 char        AdapterList[Max_Num_Adapter][8192];
00048 
00049 
00050 
00051 int main(int argc, char **argv)
00052 {   
00053 char packetbuff[5000];
00054 
00055 // define a pointer to a ADAPTER structure
00056 
00057 LPADAPTER  lpAdapter = 0;
00058 
00059 // define a pointer to a PACKET structure
00060 
00061 LPPACKET   lpPacket;
00062 
00063 int        i,npacks,Snaplen;
00064 DWORD      dwErrorCode;
00065 
00066 //ascii strings
00067 char        AdapterName[8192]; // string that contains a list of the network adapters
00068 char        *temp,*temp1;
00069 
00070 int         AdapterNum=0,Open;
00071 ULONG       AdapterLength;
00072 
00073 float   cpu_time;
00074     
00075     printf("Traffic Generator v 0.9999\nCopyright 1999 Loris Degioanni (loris@netgroup-serv.polito.it)");
00076     printf("\nSends a set of packets to the network using packet.dll API.\n");
00077     
00078     if (argc == 1){
00079         printf("\n\n Usage: TestpacketSend [-i adapter] -n npacks -s size");
00080         printf("\n size is between 60 and 1514\n\n");
00081         return -1;
00082     }
00083     
00084     
00085     AdapterName[0]=0;
00086     
00087     //get the command line parameters
00088     for(i=1;i<argc;i+=2){
00089         
00090         switch (argv[i] [1])
00091         {
00092             
00093         case 'i':
00094             sscanf(argv[i+1],"%s",AdapterName);
00095             break;
00096             
00097         case 'n':
00098             sscanf(argv[i+1],"%d",&npacks);
00099             break;
00100             
00101         case 's':
00102             sscanf(argv[i+1],"%d",&Snaplen);
00103             break;
00104             
00105         }
00106         
00107     }
00108     
00109     
00110     
00111     if(AdapterName[0]==0){
00112         
00113         //
00114         // Obtain the name of the adapters installed on this machine        
00115         //
00116         printf("Adapters installed:\n");
00117         i=0;
00118         
00119         AdapterLength = sizeof(AdapterName);
00120 
00121         if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
00122             printf("Unable to retrieve the list of the adapters!\n");
00123             return -1;
00124         }
00125         temp=AdapterName;
00126         temp1=AdapterName;
00127             
00128         while ((*temp!='\0')||(*(temp-1)!='\0'))
00129         {
00130             if (*temp=='\0') 
00131             {
00132                 memcpy(AdapterList[i],temp1,temp-temp1);
00133                 temp1=temp+1;
00134                 i++;
00135             }
00136             temp++;
00137         }
00138             
00139         AdapterNum=i;
00140         for (i=0;i<AdapterNum;i++)
00141             printf("\n%d- %s\n",i+1,AdapterList[i]);
00142         printf("\n");
00143             
00144         do 
00145         {
00146             printf("Select the number of the adapter to open : ");scanf("%d",&Open);
00147             if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 
00148         } while (Open>AdapterNum);
00149         
00150         
00151         
00152         
00153         lpAdapter =   PacketOpenAdapter(AdapterList[Open-1]);
00154         
00155         if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
00156         {
00157             dwErrorCode=GetLastError();
00158             printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode); 
00159             
00160             return(-1);
00161         }   
00162         
00163     }
00164     else{
00165         
00166         lpAdapter =  PacketOpenAdapter(AdapterName);
00167         
00168         if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
00169         {
00170             dwErrorCode=GetLastError();
00171             printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode); 
00172             
00173             return(-1);
00174         }
00175         
00176     }
00177     
00178     if((lpPacket = PacketAllocatePacket())==NULL){
00179         printf("\nError:failed to allocate the LPPACKET structure.");
00180         return (-1);
00181     }
00182     
00183     packetbuff[0]=1;
00184     packetbuff[1]=1;
00185     packetbuff[2]=1;
00186     packetbuff[3]=1;
00187     packetbuff[4]=1;
00188     packetbuff[5]=1;
00189     
00190     packetbuff[6]=2;
00191     packetbuff[7]=2;
00192     packetbuff[8]=2;
00193     packetbuff[9]=2;
00194     packetbuff[10]=2;
00195     packetbuff[11]=2;
00196     
00197     for(i=12;i<1514;i++){
00198         packetbuff[i]=i%256;
00199     }
00200     
00201     PacketInitPacket(lpPacket,packetbuff,Snaplen);
00202     // capture the packet
00203     
00204     
00205     if(PacketSetNumWrites(lpAdapter,npacks)==FALSE){
00206         printf("warning: Unable to send more than one packet in a single write!\n");
00207     }
00208     
00209     printf("\n\nGenerating %d packets...",npacks);
00210     
00211     cpu_time = (float)clock ();
00212     
00213     if(PacketSendPacket(lpAdapter,lpPacket,TRUE)==FALSE){
00214         printf("Error sending the packets!\n");
00215         return -1;
00216     }
00217     
00218     cpu_time = (clock() - cpu_time)/CLK_TCK;
00219     
00220     printf ("\n\nElapsed time: %5.3f\n", cpu_time);
00221     printf ("\nTotal packets generated = %d", npacks);
00222     printf ("\nTotal bytes generated = %d", (Snaplen+24)*npacks);
00223     printf ("\nTotal bits generated = %d", (Snaplen+24)*npacks*8);
00224     printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));
00225     printf ("\nAverage bytes per second = %d", (int)((double)((Snaplen+24)*npacks)/cpu_time));
00226     printf ("\nAverage bits per second = %d", (int)((double)((Snaplen+24)*npacks*8)/cpu_time));
00227     printf ("\n");
00228     
00229     PacketFreePacket(lpPacket);
00230     
00231     // close the adapter and exit
00232     
00233     PacketCloseAdapter(lpAdapter);
00234     return (0);
00235 }

documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.