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

# Tg.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
    00054     char packetbuff[5000];
    00055
    00056     // define a pointer to a ADAPTER structure
    00057
    00058     LPADAPTER  lpAdapter = 0;
    00059
    00060     // define a pointer to a PACKET structure
    00061
    00062     LPPACKET   lpPacket;
    00063
    00064     int        i,npacks,Snaplen;
    00065     DWORD      dwErrorCode;
    00066
    00067     DWORD dwVersion;
    00068     DWORD dwWindowsMajorVersion;
    00069
    00070     //unicode strings (winnt)
    00071     WCHAR       AdapterName[8192]; // string that contains a list of the network adapters
    00072     WCHAR       *temp,*temp1;
    00073
    00074     //ascii strings (win95)
    00075     char        AdapterNamea[8192]; // string that contains a list of the network adapters
    00076     char        *tempa,*temp1a;
    00077
    00078     int         AdapterNum=0,Open;
    00079     ULONG       AdapterLength;
    00080
    00081     float   cpu_time;
    00082
    00083     printf("Traffic Generator v 0.9999\nCopyright 1999 Loris Degioanni (loris@netgroup-serv.polito.it)");
    00084     printf("\nSends a set of packets to the network.");
    00085
    00086     if (argc == 1){
    00087         printf("\n\n Usage: tg [-i adapter] -n npacks -s size");
    00088         printf("\n size is between 60 and 1514\n\n");
    00089         return -1;
    00090     }
    00091
    00092
    00093     AdapterNamea[0]=0;
    00094
    00095     //get the command line parameters
    00096     for(i=1;i<argc;i+=2){
    00097
    00098         switch (argv[i] [1])
    00099         {
    00100
    00101         case 'i':
    00102             sscanf(argv[i+1],"%s",AdapterNamea);
    00103             break;
    00104
    00105         case 'n':
    00106             sscanf(argv[i+1],"%d",&npacks);
    00107             break;
    00108
    00109         case 's':
    00110             sscanf(argv[i+1],"%d",&Snaplen);
    00111             break;
    00112
    00113         }
    00114
    00115     }
    00116
    00117
    00118
    00119     if(AdapterNamea[0]==0){
    00120
    00121         //
    00122         // Obtain the name of the adapters installed on this machine
    00123         //
    00124         printf("Adapters installed:\n");
    00125         i=0;
    00126
    00127         // the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
    00128         // We have to check the os on which we are running
    00129         dwVersion=GetVersion();
    00130         dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
    00131         if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
    00132         {  // Windows NT
    00133             AdapterLength = sizeof(AdapterName);
    00134
    00135             if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
    00136                 printf("Unable to retrieve the list of the adapters!\n");
    00137                 return -1;
    00138             }
    00139
    00140             temp=AdapterName;
    00141             temp1=AdapterName;
    00142             while ((*temp!='\0')||(*(temp-1)!='\0'))
    00143             {
    00144                 if (*temp=='\0')
    00145                 {
    00146                     memcpy(AdapterList[i],temp1,(temp-temp1)*2);
    00147                     temp1=temp+1;
    00148                     i++;
    00149                 }
    00150
    00151                 temp++;
    00152             }
    00153
    00154             AdapterNum=i;
    00155             for (i=0;i<AdapterNum;i++)
    00156                 wprintf(L"\n%d- %s\n",i+1,AdapterList[i]);
    00157             printf("\n");
    00158
    00159         }
    00160
    00161         else    //windows 95
    00162         {
    00163             AdapterLength = sizeof(AdapterNamea);
    00164
    00165             if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE){
    00166                 printf("Unable to retrieve the list of the adapters!\n");
    00167                 return -1;
    00168             }
    00169             tempa=AdapterNamea;
    00170             temp1a=AdapterNamea;
    00171
    00172             while ((*tempa!='\0')||(*(tempa-1)!='\0'))
    00173             {
    00174                 if (*tempa=='\0')
    00175                 {
    00176                     memcpy(AdapterList[i],temp1a,tempa-temp1a);
    00177                     temp1a=tempa+1;
    00178                     i++;
    00179                 }
    00180                 tempa++;
    00181             }
    00182
    00183             AdapterNum=i;
    00184             for (i=0;i<AdapterNum;i++)
    00185                 printf("\n%d- %s\n",i+1,AdapterList[i]);
    00186             printf("\n");
    00187
    00188         }
    00189
    00190         do
    00191         {
    00192             printf("Select the number of the adapter to open : ");scanf("%d",&Open);
    00193             if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum);
    00194         } while (Open>AdapterNum);
    00195
    00196
    00197
    00198
    00199         lpAdapter =   PacketOpenAdapter(AdapterList[Open-1]);
    00200
    00201         if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
    00202         {
    00203             dwErrorCode=GetLastError();
    00204             printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode);
    00205
    00206             return(-1);
    00207         }
    00208
    00209     }
    00210     else{
    00211
    00212         lpAdapter =  PacketOpenAdapter(AdapterNamea);
    00213
    00214         if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
    00215         {
    00216             dwErrorCode=GetLastError();
    00217             printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode);
    00218
    00219             return(-1);
    00220         }
    00221
    00222     }
    00223
    00224     if((lpPacket = PacketAllocatePacket())==NULL){
    00225         printf("\nError:failed to allocate the LPPACKET structure.");
    00226         return (-1);
    00227     }
    00228
    00229     packetbuff[0]=1;
    00230     packetbuff[1]=1;
    00231     packetbuff[2]=1;
    00232     packetbuff[3]=1;
    00233     packetbuff[4]=1;
    00234     packetbuff[5]=1;
    00235
    00236     packetbuff[6]=2;
    00237     packetbuff[7]=2;
    00238     packetbuff[8]=2;
    00239     packetbuff[9]=2;
    00240     packetbuff[10]=2;
    00241     packetbuff[11]=2;
    00242
    00243     for(i=12;i<1514;i++){
    00244         packetbuff[i]=i%256;
    00245     }
    00246
    00247     PacketInitPacket(lpPacket,packetbuff,Snaplen);
    00248     // capture the packet
    00249
    00250
    00251     if(PacketSetNumWrites(lpAdapter,npacks)==FALSE){
    00252         printf("warning: Unable to send more than one packet in a single write!\n");
    00253     }
    00254
    00255     printf("\n\nGenerating %d packets...",npacks);
    00256
    00257     cpu_time = clock ();
    00258
    00259     if(PacketSendPacket(lpAdapter,lpPacket,TRUE)==FALSE){
    00260         printf("Error sending the packets!\n");
    00261         return -1;
    00262     }
    00263
    00264     cpu_time = (clock() - cpu_time)/CLK_TCK;
    00265
    00266     printf ("\n\nElapsed time: %5.3f\n", cpu_time);
    00267     printf ("\nTotal packets generated = %d", npacks);
    00268     printf ("\nTotal bytes generated = %d", (Snaplen+24)*npacks);
    00269     printf ("\nTotal bits generated = %d", (Snaplen+24)*npacks*8);
    00270     printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));
    00271     printf ("\nAverage bytes per second = %d", (int)((double)((Snaplen+24)*npacks)/cpu_time));
    00272     printf ("\nAverage bits per second = %d", (int)((double)((Snaplen+24)*npacks*8)/cpu_time));
    00273     printf ("\n");
    00274
    00275     PacketFreePacket(lpPacket);
    00276
    00277     // close the adapter and exit
    00278
    00279     PacketCloseAdapter(lpAdapter);
    00280     return (0);
    00281 }

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