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

TestPacketCapture.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 00037 00038 #include "..\..\..\Include\packet32.h" 00039 #include "..\..\..\Include\ntddndis.h" 00040 00041 #define Max_Num_Adapter 10 00042 00043 // Prototypes 00044 00045 void PrintPackets(LPPACKET lpPacket); 00046 00047 char AdapterList[Max_Num_Adapter][1024]; 00048 00049 int main() 00050 { 00051 //define a pointer to an ADAPTER structure 00052 00053 LPADAPTER lpAdapter = 0; 00054 00055 //define a pointer to a PACKET structure 00056 00057 LPPACKET lpPacket; 00058 00059 int i; 00060 DWORD dwErrorCode; 00061 00062 //ascii strings 00063 char AdapterName[8192]; // string that contains a list of the network adapters 00064 char *temp,*temp1; 00065 00066 00067 int AdapterNum=0,Open; 00068 ULONG AdapterLength; 00069 00070 char buffer[256000]; // buffer to hold the data coming from the driver 00071 00072 struct bpf_stat stat; 00073 00074 // 00075 // Obtain the name of the adapters installed on this machine 00076 // 00077 printf("Packet.dll test application. Library version:%s\n", PacketGetVersion()); 00078 00079 printf("Adapters installed:\n"); 00080 i=0; 00081 00082 AdapterLength = sizeof(AdapterName); 00083 00084 if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){ 00085 printf("Unable to retrieve the list of the adapters!\n"); 00086 return -1; 00087 } 00088 temp=AdapterName; 00089 temp1=AdapterName; 00090 00091 while ((*temp!='\0')||(*(temp-1)!='\0')) 00092 { 00093 if (*temp=='\0') 00094 { 00095 memcpy(AdapterList[i],temp1,temp-temp1); 00096 temp1=temp+1; 00097 i++; 00098 } 00099 temp++; 00100 } 00101 00102 AdapterNum=i; 00103 for (i=0;i<AdapterNum;i++) 00104 printf("\n%d- %s\n",i+1,AdapterList[i]); 00105 printf("\n"); 00106 00107 00108 do 00109 { 00110 printf("Select the number of the adapter to open : "); 00111 scanf("%d",&Open); 00112 if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 00113 } while (Open>AdapterNum); 00114 00115 00116 00117 00118 lpAdapter = PacketOpenAdapter(AdapterList[Open-1]); 00119 00120 if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) 00121 { 00122 dwErrorCode=GetLastError(); 00123 printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 00124 00125 return -1; 00126 } 00127 00128 // set the network adapter in promiscuous mode 00129 00130 if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){ 00131 printf("Warning: unable to set promiscuous mode!\n"); 00132 } 00133 00134 // set a 512K buffer in the driver 00135 if(PacketSetBuff(lpAdapter,512000)==FALSE){ 00136 printf("Unable to set the kernel buffer!\n"); 00137 return -1; 00138 } 00139 00140 // set a 1 second read timeout 00141 if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){ 00142 printf("Warning: unable to set the read tiemout!\n"); 00143 } 00144 00145 //allocate and initialize a packet structure that will be used to 00146 //receive the packets. 00147 if((lpPacket = PacketAllocatePacket())==NULL){ 00148 printf("\nError: failed to allocate the LPPACKET structure."); 00149 return (-1); 00150 } 00151 PacketInitPacket(lpPacket,(char*)buffer,256000); 00152 00153 //main capture loop 00154 while(!kbhit()) 00155 { 00156 // capture the packets 00157 if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE){ 00158 printf("Error: PacketReceivePacket failed"); 00159 return (-1); 00160 } 00161 00162 PrintPackets(lpPacket); 00163 } 00164 00165 00166 //print the capture statistics 00167 if(PacketGetStats(lpAdapter,&stat)==FALSE){ 00168 printf("Warning: unable to get stats from the kernel!\n"); 00169 } 00170 else 00171 printf("\n\n%d packets received.\n%d Packets lost",stat.bs_recv,stat.bs_drop); 00172 00173 PacketFreePacket(lpPacket); 00174 00175 // close the adapter and exit 00176 00177 PacketCloseAdapter(lpAdapter); 00178 return (0); 00179 } 00180 00181 // this function prints the content of a block of packets received from the driver 00182 00183 void PrintPackets(LPPACKET lpPacket) 00184 { 00185 00186 ULONG i, j, ulLines, ulen, ulBytesReceived; 00187 char *pChar, *pLine, *base; 00188 char *buf; 00189 u_int off=0; 00190 u_int tlen,tlen1; 00191 struct bpf_hdr *hdr; 00192 00193 ulBytesReceived = lpPacket->ulBytesReceived; 00194 00195 00196 buf = lpPacket->Buffer; 00197 00198 off=0; 00199 00200 while(off<ulBytesReceived){ 00201 if(kbhit())return; 00202 hdr=(struct bpf_hdr *)(buf+off); 00203 tlen1=hdr->bh_datalen; 00204 tlen=hdr->bh_caplen; 00205 printf("Packet length, captured portion: %ld, %ld\n", tlen1, tlen); 00206 off+=hdr->bh_hdrlen; 00207 00208 ulLines = (tlen + 15) / 16; 00209 00210 pChar =(char*)(buf+off); 00211 base=pChar; 00212 off=Packet_WORDALIGN(off+tlen); 00213 00214 for ( i=0; i<ulLines; i++ ) 00215 { 00216 00217 pLine =pChar; 00218 00219 printf( "%08lx : ", pChar-base ); 00220 00221 ulen=tlen; 00222 ulen = ( ulen > 16 ) ? 16 : ulen; 00223 tlen -= ulen; 00224 00225 for ( j=0; j<ulen; j++ ) 00226 printf( "%02x ", *(BYTE *)pChar++ ); 00227 00228 if ( ulen < 16 ) 00229 printf( "%*s", (16-ulen)*3, " " ); 00230 00231 pChar = pLine; 00232 00233 for ( j=0; j<ulen; j++, pChar++ ) 00234 printf( "%c", isprint( *pChar ) ? *pChar : '.' ); 00235 00236 printf( "\n" ); 00237 } 00238 00239 printf( "\n" ); 00240 } 00241 } 00242 00243

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