TestPacketSend.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
  3. * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  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.
  19. *
  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.
  31. *
  32. */
  33. #include <stdio.h>
  34. #include <conio.h>
  35. #include <time.h>
  36. #include <Packet32.h>
  37. #define Max_Num_Adapter 10
  38. #include <tchar.h>
  39. BOOL LoadNpcapDlls()
  40. {
  41. TCHAR npcap_dir[512];
  42. UINT len;
  43. len = GetSystemDirectory(npcap_dir, 480);
  44. if (!len) {
  45. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  46. return FALSE;
  47. }
  48. _tcscat_s(npcap_dir, 512, TEXT("\\Npcap"));
  49. if (SetDllDirectory(npcap_dir) == 0) {
  50. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  51. return FALSE;
  52. }
  53. return TRUE;
  54. }
  55. // Prototypes
  56. void PrintPackets(LPPACKET lpPacket);
  57. char AdapterList[Max_Num_Adapter][8192];
  58. int main(int argc, char **argv)
  59. {
  60. char packetbuff[5000];
  61. // define a pointer to a ADAPTER structure
  62. LPADAPTER lpAdapter = 0;
  63. // define a pointer to a PACKET structure
  64. LPPACKET lpPacket;
  65. int i,npacks,Snaplen;
  66. DWORD dwErrorCode;
  67. //ascii strings
  68. char AdapterName[8192]; // string that contains a list of the network adapters
  69. char *temp,*temp1;
  70. int AdapterNum=0,Open;
  71. ULONG AdapterLength;
  72. float cpu_time;
  73. /* Load Npcap and its functions. */
  74. if (!LoadNpcapDlls())
  75. {
  76. fprintf(stderr, "Couldn't load Npcap\n");
  77. exit(1);
  78. }
  79. printf("Traffic Generator v 0.9999\nCopyright 1999 Loris Degioanni (loris@netgroup-serv.polito.it)");
  80. printf("\nSends a set of packets to the network using packet.dll API.\n");
  81. if (argc == 1){
  82. printf("\n\n Usage: TestpacketSend [-i adapter] -n npacks -s size");
  83. printf("\n size is between 60 and 1514\n\n");
  84. return -1;
  85. }
  86. AdapterName[0]=0;
  87. //get the command line parameters
  88. for(i=1;i<argc;i+=2){
  89. switch (argv[i] [1])
  90. {
  91. case 'i':
  92. sscanf_s(argv[i+1],"%s",AdapterName, (unsigned)_countof(AdapterName));
  93. break;
  94. case 'n':
  95. sscanf_s(argv[i+1],"%d",&npacks);
  96. break;
  97. case 's':
  98. sscanf_s(argv[i+1],"%d",&Snaplen);
  99. break;
  100. }
  101. }
  102. if(AdapterName[0]==0){
  103. //
  104. // Obtain the name of the adapters installed on this machine
  105. //
  106. printf("Adapters installed:\n");
  107. i=0;
  108. AdapterLength = sizeof(AdapterName);
  109. if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
  110. printf("Unable to retrieve the list of the adapters!\n");
  111. return -1;
  112. }
  113. temp=AdapterName;
  114. temp1=AdapterName;
  115. while ((*temp!='\0')||(*(temp-1)!='\0'))
  116. {
  117. if (*temp=='\0')
  118. {
  119. memcpy(AdapterList[i],temp1,temp-temp1);
  120. temp1=temp+1;
  121. i++;
  122. }
  123. temp++;
  124. }
  125. AdapterNum=i;
  126. for (i=0;i<AdapterNum;i++)
  127. printf("\n%d- %s\n",i+1,AdapterList[i]);
  128. printf("\n");
  129. do
  130. {
  131. printf("Select the number of the adapter to open : ");scanf_s("%d",&Open);
  132. if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum);
  133. } while (Open>AdapterNum);
  134. lpAdapter = PacketOpenAdapter(AdapterList[Open-1]);
  135. if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
  136. {
  137. dwErrorCode=GetLastError();
  138. printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode);
  139. return(-1);
  140. }
  141. }
  142. else{
  143. lpAdapter = PacketOpenAdapter(AdapterName);
  144. if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
  145. {
  146. dwErrorCode=GetLastError();
  147. printf("Unable to open the driver, Error Code : %lx\n",dwErrorCode);
  148. return(-1);
  149. }
  150. }
  151. if((lpPacket = PacketAllocatePacket())==NULL){
  152. printf("\nError:failed to allocate the LPPACKET structure.");
  153. return (-1);
  154. }
  155. packetbuff[0]=1;
  156. packetbuff[1]=1;
  157. packetbuff[2]=1;
  158. packetbuff[3]=1;
  159. packetbuff[4]=1;
  160. packetbuff[5]=1;
  161. packetbuff[6]=2;
  162. packetbuff[7]=2;
  163. packetbuff[8]=2;
  164. packetbuff[9]=2;
  165. packetbuff[10]=2;
  166. packetbuff[11]=2;
  167. for(i=12;i<1514;i++){
  168. packetbuff[i]= (char)i;
  169. }
  170. PacketInitPacket(lpPacket,packetbuff,Snaplen);
  171. // capture the packet
  172. if(PacketSetNumWrites(lpAdapter,npacks)==FALSE){
  173. printf("warning: Unable to send more than one packet in a single write!\n");
  174. }
  175. printf("\n\nGenerating %d packets...",npacks);
  176. cpu_time = (float)clock ();
  177. if(PacketSendPacket(lpAdapter,lpPacket,TRUE)==FALSE){
  178. printf("Error sending the packets!\n");
  179. return -1;
  180. }
  181. cpu_time = (clock() - cpu_time)/CLK_TCK;
  182. printf ("\n\nElapsed time: %5.3f\n", cpu_time);
  183. printf ("\nTotal packets generated = %d", npacks);
  184. printf ("\nTotal bytes generated = %d", (Snaplen+24)*npacks);
  185. printf ("\nTotal bits generated = %d", (Snaplen+24)*npacks*8);
  186. printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));
  187. printf ("\nAverage bytes per second = %d", (int)((double)((Snaplen+24)*npacks)/cpu_time));
  188. printf ("\nAverage bits per second = %d", (int)((double)((Snaplen+24)*npacks*8)/cpu_time));
  189. printf ("\n");
  190. PacketFreePacket(lpPacket);
  191. // close the adapter and exit
  192. PacketCloseAdapter(lpAdapter);
  193. return (0);
  194. }