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 00052 //define a pointer to an ADAPTER structure 00053 00054 LPADAPTER lpAdapter = 0; 00055 00056 //define a pointer to a PACKET structure 00057 00058 LPPACKET lpPacket; 00059 00060 int i; 00061 DWORD dwErrorCode; 00062 00063 DWORD dwVersion; 00064 DWORD dwWindowsMajorVersion; 00065 00066 //unicode strings (winnt) 00067 WCHAR AdapterName[8192]; // string that contains a list of the network adapters 00068 WCHAR *temp,*temp1; 00069 00070 //ascii strings (win95) 00071 char AdapterNamea[8192]; // string that contains a list of the network adapters 00072 char *tempa,*temp1a; 00073 00074 00075 int AdapterNum=0,Open; 00076 ULONG AdapterLength; 00077 00078 char buffer[256000]; // buffer to hold the data coming from the driver 00079 00080 struct bpf_stat stat; 00081 00082 // 00083 // Obtain the name of the adapters installed on this machine 00084 // 00085 printf("Packet.dll test application. Library version:%s\n", PacketGetVersion()); 00086 00087 printf("Adapters installed:\n"); 00088 i=0; 00089 00090 // the data returned by PacketGetAdapterNames is different in Win95 and in WinNT. 00091 // We have to check the os on which we are running 00092 dwVersion=GetVersion(); 00093 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion))); 00094 if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)) 00095 { // Windows NT 00096 AdapterLength = sizeof(AdapterName); 00097 00098 if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){ 00099 printf("Unable to retrieve the list of the adapters!\n"); 00100 return -1; 00101 } 00102 temp=AdapterName; 00103 temp1=AdapterName; 00104 while ((*temp!='\0')||(*(temp-1)!='\0')) 00105 { 00106 if (*temp=='\0') 00107 { 00108 memcpy(AdapterList[i],temp1,(temp-temp1)*2); 00109 temp1=temp+1; 00110 i++; 00111 } 00112 00113 temp++; 00114 } 00115 00116 AdapterNum=i; 00117 for (i=0;i<AdapterNum;i++) 00118 wprintf(L"\n%d- %s\n",i+1,AdapterList[i]); 00119 printf("\n"); 00120 00121 } 00122 00123 else //windows 95 00124 { 00125 AdapterLength = sizeof(AdapterNamea); 00126 00127 if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE){ 00128 printf("Unable to retrieve the list of the adapters!\n"); 00129 return -1; 00130 } 00131 tempa=AdapterNamea; 00132 temp1a=AdapterNamea; 00133 00134 while ((*tempa!='\0')||(*(tempa-1)!='\0')) 00135 { 00136 if (*tempa=='\0') 00137 { 00138 memcpy(AdapterList[i],temp1a,tempa-temp1a); 00139 temp1a=tempa+1; 00140 i++; 00141 } 00142 tempa++; 00143 } 00144 00145 AdapterNum=i; 00146 for (i=0;i<AdapterNum;i++) 00147 printf("\n%d- %s\n",i+1,AdapterList[i]); 00148 printf("\n"); 00149 00150 } 00151 00152 do 00153 { 00154 printf("Select the number of the adapter to open : "); 00155 scanf("%d",&Open); 00156 if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 00157 } while (Open>AdapterNum); 00158 00159 00160 00161 00162 lpAdapter = PacketOpenAdapter(AdapterList[Open-1]); 00163 00164 if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) 00165 { 00166 dwErrorCode=GetLastError(); 00167 printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 00168 00169 return -1; 00170 } 00171 00172 // set the network adapter in promiscuous mode 00173 00174 if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){ 00175 printf("Warning: unable to set promiscuous mode!\n"); 00176 } 00177 00178 // set a 512K buffer in the driver 00179 if(PacketSetBuff(lpAdapter,512000)==FALSE){ 00180 printf("Unable to set the kernel buffer!\n"); 00181 return -1; 00182 } 00183 00184 // set a 1 second read timeout 00185 if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){ 00186 printf("Warning: unable to set the read tiemout!\n"); 00187 } 00188 00189 //allocate and initialize a packet structure that will be used to 00190 //receive the packets. 00191 if((lpPacket = PacketAllocatePacket())==NULL){ 00192 printf("\nError: failed to allocate the LPPACKET structure."); 00193 return (-1); 00194 } 00195 PacketInitPacket(lpPacket,(char*)buffer,256000); 00196 00197 //main capture loop 00198 while(!kbhit()) 00199 { 00200 // capture the packets 00201 if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE){ 00202 printf("Error: PacketReceivePacket failed"); 00203 return (-1); 00204 } 00205 00206 PrintPackets(lpPacket); 00207 } 00208 00209 00210 //print the capture statistics 00211 if(PacketGetStats(lpAdapter,&stat)==FALSE){ 00212 printf("Warning: unable to get stats from the kernel!\n"); 00213 } 00214 else 00215 printf("\n\n%d packets received.\n%d Packets lost",stat.bs_recv,stat.bs_drop); 00216 00217 PacketFreePacket(lpPacket); 00218 00219 // close the adapter and exit 00220 00221 PacketCloseAdapter(lpAdapter); 00222 return (0); 00223 } 00224 00225 // this function prints the content of a block of packets received from the driver 00226 00227 void PrintPackets(LPPACKET lpPacket) 00228 { 00229 00230 ULONG i, j, ulLines, ulen, ulBytesReceived; 00231 char *pChar, *pLine, *base; 00232 char *buf; 00233 u_int off=0; 00234 u_int tlen,tlen1; 00235 struct bpf_hdr *hdr; 00236 00237 ulBytesReceived = lpPacket->ulBytesReceived; 00238 00239 00240 buf = lpPacket->Buffer; 00241 00242 off=0; 00243 00244 while(off<ulBytesReceived){ 00245 if(kbhit())return; 00246 hdr=(struct bpf_hdr *)(buf+off); 00247 tlen1=hdr->bh_datalen; 00248 tlen=hdr->bh_caplen; 00249 printf("Packet length, captured portion: %ld, %ld\n", tlen1, tlen); 00250 off+=hdr->bh_hdrlen; 00251 00252 ulLines = (tlen + 15) / 16; 00253 00254 pChar =(char*)(buf+off); 00255 base=pChar; 00256 off=Packet_WORDALIGN(off+tlen); 00257 00258 for ( i=0; i<ulLines; i++ ) 00259 { 00260 00261 pLine =pChar; 00262 00263 printf( "%08lx : ", pChar-base ); 00264 00265 ulen=tlen; 00266 ulen = ( ulen > 16 ) ? 16 : ulen; 00267 tlen -= ulen; 00268 00269 for ( j=0; j<ulen; j++ ) 00270 printf( "%02x ", *(BYTE *)pChar++ ); 00271 00272 if ( ulen < 16 ) 00273 printf( "%*s", (16-ulen)*3, " " ); 00274 00275 pChar = pLine; 00276 00277 for ( j=0; j<ulen; j++, pChar++ ) 00278 printf( "%c", isprint( *pChar ) ? *pChar : '.' ); 00279 00280 printf( "\n" ); 00281 } 00282 00283 printf( "\n" ); 00284 } 00285 } 00286 00287
documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.