TestPacketCapture.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <Packet32.h>
  36. #include <ntddndis.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][1024];
  58. int main()
  59. {
  60. //define a pointer to an ADAPTER structure
  61. LPADAPTER lpAdapter = 0;
  62. //define a pointer to a PACKET structure
  63. LPPACKET lpPacket;
  64. int i;
  65. DWORD dwErrorCode;
  66. //ascii strings
  67. char AdapterName[8192]; // string that contains a list of the network adapters
  68. char *temp,*temp1;
  69. int AdapterNum=0,Open;
  70. ULONG AdapterLength;
  71. char buffer[256000]; // buffer to hold the data coming from the driver
  72. struct bpf_stat stat;
  73. /* Load Npcap and its functions. */
  74. if (!LoadNpcapDlls())
  75. {
  76. fprintf(stderr, "Couldn't load Npcap\n");
  77. exit(1);
  78. }
  79. //
  80. // Obtain the name of the adapters installed on this machine
  81. //
  82. printf("Packet.dll test application. Library version:%s\n", PacketGetVersion());
  83. printf("Adapters installed:\n");
  84. i=0;
  85. AdapterLength = sizeof(AdapterName);
  86. if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
  87. printf("Unable to retrieve the list of the adapters!\n");
  88. return -1;
  89. }
  90. temp=AdapterName;
  91. temp1=AdapterName;
  92. while ((*temp!='\0')||(*(temp-1)!='\0'))
  93. {
  94. if (*temp=='\0')
  95. {
  96. memcpy(AdapterList[i],temp1,temp-temp1);
  97. temp1=temp+1;
  98. i++;
  99. }
  100. temp++;
  101. }
  102. AdapterNum=i;
  103. for (i=0;i<AdapterNum;i++)
  104. printf("\n%d- %s\n",i+1,AdapterList[i]);
  105. printf("\n");
  106. do
  107. {
  108. printf("Select the number of the adapter to open : ");
  109. scanf_s("%d",&Open);
  110. if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum);
  111. } while (Open>AdapterNum);
  112. lpAdapter = PacketOpenAdapter(AdapterList[Open-1]);
  113. if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
  114. {
  115. dwErrorCode=GetLastError();
  116. printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode);
  117. return -1;
  118. }
  119. // set the network adapter in promiscuous mode
  120. if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){
  121. printf("Warning: unable to set promiscuous mode!\n");
  122. }
  123. // set a 512K buffer in the driver
  124. if(PacketSetBuff(lpAdapter,512000)==FALSE){
  125. printf("Unable to set the kernel buffer!\n");
  126. return -1;
  127. }
  128. // set a 1 second read timeout
  129. if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){
  130. printf("Warning: unable to set the read tiemout!\n");
  131. }
  132. //allocate and initialize a packet structure that will be used to
  133. //receive the packets.
  134. if((lpPacket = PacketAllocatePacket())==NULL){
  135. printf("\nError: failed to allocate the LPPACKET structure.");
  136. return (-1);
  137. }
  138. PacketInitPacket(lpPacket,(char*)buffer,256000);
  139. //main capture loop
  140. while(!_kbhit())
  141. {
  142. // capture the packets
  143. if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE){
  144. printf("Error: PacketReceivePacket failed");
  145. return (-1);
  146. }
  147. PrintPackets(lpPacket);
  148. }
  149. //print the capture statistics
  150. if(PacketGetStats(lpAdapter,&stat)==FALSE){
  151. printf("Warning: unable to get stats from the kernel!\n");
  152. }
  153. else
  154. printf("\n\n%d packets received.\n%d Packets lost",stat.bs_recv,stat.bs_drop);
  155. PacketFreePacket(lpPacket);
  156. // close the adapter and exit
  157. PacketCloseAdapter(lpAdapter);
  158. return (0);
  159. }
  160. // this function prints the content of a block of packets received from the driver
  161. void PrintPackets(LPPACKET lpPacket)
  162. {
  163. ULONG i, j, ulLines, ulen, ulBytesReceived;
  164. char *pChar, *pLine, *base;
  165. char *buf;
  166. u_int off=0;
  167. u_int tlen,tlen1;
  168. struct bpf_hdr *hdr;
  169. ulBytesReceived = lpPacket->ulBytesReceived;
  170. buf = lpPacket->Buffer;
  171. off=0;
  172. while(off<ulBytesReceived){
  173. if(_kbhit())return;
  174. hdr=(struct bpf_hdr *)(buf+off);
  175. tlen1=hdr->bh_datalen;
  176. tlen=hdr->bh_caplen;
  177. printf("Packet length, captured portion: %ld, %ld\n", tlen1, tlen);
  178. off+=hdr->bh_hdrlen;
  179. ulLines = (tlen + 15) / 16;
  180. pChar =(char*)(buf+off);
  181. base=pChar;
  182. off=Packet_WORDALIGN(off+tlen);
  183. for ( i=0; i<ulLines; i++ )
  184. {
  185. pLine =pChar;
  186. printf( "%p : ", (void *)(pChar - base));
  187. ulen=tlen;
  188. ulen = ( ulen > 16 ) ? 16 : ulen;
  189. tlen -= ulen;
  190. for ( j=0; j<ulen; j++ )
  191. printf( "%02x ", *(BYTE *)pChar++ );
  192. if ( ulen < 16 )
  193. printf( "%*s", (16-ulen)*3, " " );
  194. pChar = pLine;
  195. for ( j=0; j<ulen; j++, pChar++ )
  196. printf( "%c", isprint( (unsigned char)*pChar ) ? *pChar : '.' );
  197. printf( "\n" );
  198. }
  199. printf( "\n" );
  200. }
  201. }