00001 /* This simple example shows how to send raw packets to the network using 00002 the Packet Capture Driver 00003 00004 Copyright (C) 1999 - 2002 Politecnico di Torino 00005 00006 This file is part of the Packet Capture Driver Developer's Pack. 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #include <stdio.h> 00024 #include <conio.h> 00025 #include <time.h> 00026 00027 #include "..\..\Include\packet32.h" 00028 00029 00030 #define Max_Num_Adapter 10 00031 00032 // Prototypes 00033 00034 void PrintPackets(LPPACKET lpPacket); 00035 00036 char AdapterList[Max_Num_Adapter][8192]; 00037 00038 00039 00040 int main(int argc, char **argv) 00041 { 00042 00043 char packetbuff[5000]; 00044 00045 // define a pointer to a ADAPTER structure 00046 00047 LPADAPTER lpAdapter = 0; 00048 00049 // define a pointer to a PACKET structure 00050 00051 LPPACKET lpPacket; 00052 00053 int i,npacks,Snaplen; 00054 DWORD dwErrorCode; 00055 00056 DWORD dwVersion; 00057 DWORD dwWindowsMajorVersion; 00058 00059 //unicode strings (winnt) 00060 WCHAR AdapterName[8192]; // string that contains a list of the network adapters 00061 WCHAR *temp,*temp1; 00062 00063 //ascii strings (win95) 00064 char AdapterNamea[8192]; // string that contains a list of the network adapters 00065 char *tempa,*temp1a; 00066 00067 int AdapterNum=0,Open; 00068 ULONG AdapterLength; 00069 00070 float cpu_time; 00071 00072 printf("Traffic Generator v 0.9999\nCopyright 1999 Loris Degioanni (loris@netgroup-serv.polito.it)"); 00073 printf("\nSends a set of packets to the network."); 00074 00075 if (argc == 1){ 00076 printf("\n\n Usage: tg [-i adapter] -n npacks -s size"); 00077 printf("\n size is between 60 and 1514\n\n"); 00078 return -1; 00079 } 00080 00081 00082 AdapterNamea[0]=0; 00083 00084 //get the command line parameters 00085 for(i=1;i<argc;i+=2){ 00086 00087 switch (argv[i] [1]) 00088 { 00089 00090 case 'i': 00091 sscanf(argv[i+1],"%s",AdapterNamea); 00092 break; 00093 00094 case 'n': 00095 sscanf(argv[i+1],"%d",&npacks); 00096 break; 00097 00098 case 's': 00099 sscanf(argv[i+1],"%d",&Snaplen); 00100 break; 00101 00102 } 00103 00104 } 00105 00106 00107 00108 if(AdapterNamea[0]==0){ 00109 00110 // 00111 // Obtain the name of the adapters installed on this machine 00112 // 00113 printf("Adapters installed:\n"); 00114 i=0; 00115 00116 // the data returned by PacketGetAdapterNames is different in Win95 and in WinNT. 00117 // We have to check the os on which we are running 00118 dwVersion=GetVersion(); 00119 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion))); 00120 if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)) 00121 { // Windows NT 00122 AdapterLength = sizeof(AdapterName); 00123 00124 if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){ 00125 printf("Unable to retrieve the list of the adapters!\n"); 00126 return -1; 00127 } 00128 00129 temp=AdapterName; 00130 temp1=AdapterName; 00131 while ((*temp!='\0')||(*(temp-1)!='\0')) 00132 { 00133 if (*temp=='\0') 00134 { 00135 memcpy(AdapterList[i],temp1,(temp-temp1)*2); 00136 temp1=temp+1; 00137 i++; 00138 } 00139 00140 temp++; 00141 } 00142 00143 AdapterNum=i; 00144 for (i=0;i<AdapterNum;i++) 00145 wprintf(L"\n%d- %s\n",i+1,AdapterList[i]); 00146 printf("\n"); 00147 00148 } 00149 00150 else //windows 95 00151 { 00152 AdapterLength = sizeof(AdapterNamea); 00153 00154 if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE){ 00155 printf("Unable to retrieve the list of the adapters!\n"); 00156 return -1; 00157 } 00158 tempa=AdapterNamea; 00159 temp1a=AdapterNamea; 00160 00161 while ((*tempa!='\0')||(*(tempa-1)!='\0')) 00162 { 00163 if (*tempa=='\0') 00164 { 00165 memcpy(AdapterList[i],temp1a,tempa-temp1a); 00166 temp1a=tempa+1; 00167 i++; 00168 } 00169 tempa++; 00170 } 00171 00172 AdapterNum=i; 00173 for (i=0;i<AdapterNum;i++) 00174 printf("\n%d- %s\n",i+1,AdapterList[i]); 00175 printf("\n"); 00176 00177 } 00178 00179 do 00180 { 00181 printf("Select the number of the adapter to open : ");scanf("%d",&Open); 00182 if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 00183 } while (Open>AdapterNum); 00184 00185 00186 00187 00188 lpAdapter = PacketOpenAdapter(AdapterList[Open-1]); 00189 00190 if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) 00191 { 00192 dwErrorCode=GetLastError(); 00193 printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode); 00194 00195 return(-1); 00196 } 00197 00198 } 00199 else{ 00200 00201 lpAdapter = PacketOpenAdapter(AdapterNamea); 00202 00203 if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) 00204 { 00205 dwErrorCode=GetLastError(); 00206 printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode); 00207 00208 return(-1); 00209 } 00210 00211 } 00212 00213 if((lpPacket = PacketAllocatePacket())==NULL){ 00214 printf("\nError:failed to allocate the LPPACKET structure."); 00215 return (-1); 00216 } 00217 00218 packetbuff[0]=1; 00219 packetbuff[1]=1; 00220 packetbuff[2]=1; 00221 packetbuff[3]=1; 00222 packetbuff[4]=1; 00223 packetbuff[5]=1; 00224 00225 packetbuff[6]=2; 00226 packetbuff[7]=2; 00227 packetbuff[8]=2; 00228 packetbuff[9]=2; 00229 packetbuff[10]=2; 00230 packetbuff[11]=2; 00231 00232 for(i=12;i<1514;i++){ 00233 packetbuff[i]=i%256; 00234 } 00235 00236 PacketInitPacket(lpPacket,packetbuff,Snaplen); 00237 // capture the packet 00238 00239 00240 if(PacketSetNumWrites(lpAdapter,npacks)==FALSE){ 00241 printf("warning: Unable to send more than one packet in a single write!\n"); 00242 } 00243 00244 printf("\n\nGenerating %d packets...",npacks); 00245 00246 cpu_time = clock (); 00247 00248 if(PacketSendPacket(lpAdapter,lpPacket,TRUE)==FALSE){ 00249 printf("Error sending the packets!\n"); 00250 return -1; 00251 } 00252 00253 cpu_time = (clock() - cpu_time)/CLK_TCK; 00254 00255 printf ("\n\nElapsed time: %5.3f\n", cpu_time); 00256 printf ("\nTotal packets generated = %d", npacks); 00257 printf ("\nTotal bytes generated = %d", (Snaplen+24)*npacks); 00258 printf ("\nTotal bits generated = %d", (Snaplen+24)*npacks*8); 00259 printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time)); 00260 printf ("\nAverage bytes per second = %d", (int)((double)((Snaplen+24)*npacks)/cpu_time)); 00261 printf ("\nAverage bits per second = %d", (int)((double)((Snaplen+24)*npacks*8)/cpu_time)); 00262 printf ("\n"); 00263 00264 PacketFreePacket(lpPacket); 00265 00266 // close the adapter and exit 00267 00268 PacketCloseAdapter(lpAdapter); 00269 return (0); 00270 }
documentation. Copyright (c) 2002 Politecnico di Torino. All rights reserved.