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

Testapp.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999 - 2002
00003  *  Politecnico di Torino.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that: (1) source code distributions
00007  * retain the above copyright notice and this paragraph in its entirety, (2)
00008  * distributions including binary code include the above copyright notice and
00009  * this paragraph in its entirety in the documentation or other materials
00010  * provided with the distribution, and (3) all advertising materials mentioning
00011  * features or use of this software display the following acknowledgement:
00012  * ``This product includes software developed by the Politecnico
00013  * di Torino, and its contributors.'' Neither the name of
00014  * the University nor the names of its contributors may be used to endorse
00015  * or promote products derived from this software without specific prior
00016  * written permission.
00017  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00018  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020  */
00021 
00022 #include <stdio.h>
00023 #include <conio.h>
00024 
00025 
00026 #include "..\..\Include\packet32.h"
00027 #include "..\..\Include\ntddndis.h"
00028 
00029 #define Max_Num_Adapter 10
00030 
00031 // Prototypes
00032 
00033 void PrintPackets(LPPACKET lpPacket);
00034 
00035 char        AdapterList[Max_Num_Adapter][1024];
00036 
00037 int main()
00038 {
00039 
00040     //define a pointer to an ADAPTER structure
00041 
00042     LPADAPTER  lpAdapter = 0;
00043 
00044     //define a pointer to a PACKET structure
00045 
00046     LPPACKET   lpPacket;
00047 
00048     int        i;
00049     DWORD      dwErrorCode;
00050 
00051     DWORD dwVersion;
00052     DWORD dwWindowsMajorVersion;
00053 
00054     //unicode strings (winnt)
00055     WCHAR       AdapterName[8192]; // string that contains a list of the network adapters
00056     WCHAR       *temp,*temp1;
00057 
00058     //ascii strings (win95)
00059     char        AdapterNamea[8192]; // string that contains a list of the network adapters
00060     char        *tempa,*temp1a;
00061 
00062 
00063     int         AdapterNum=0,Open;
00064     ULONG       AdapterLength;
00065     
00066     char buffer[256000];  // buffer to hold the data coming from the driver
00067 
00068     struct bpf_stat stat;
00069     
00070     //
00071     // Obtain the name of the adapters installed on this machine
00072     //
00073     printf("Packet.dll test application. Library version:%s\n", PacketGetVersion());
00074 
00075     printf("Adapters installed:\n");
00076     i=0;    
00077 
00078     // the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
00079     // We have to check the os on which we are running
00080     dwVersion=GetVersion();
00081     dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
00082     if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
00083     {  // Windows NT
00084         AdapterLength = sizeof(AdapterName);
00085 
00086         if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
00087             printf("Unable to retrieve the list of the adapters!\n");
00088             return -1;
00089         }
00090         temp=AdapterName;
00091         temp1=AdapterName;
00092         while ((*temp!='\0')||(*(temp-1)!='\0'))
00093         {
00094             if (*temp=='\0') 
00095             {
00096                 memcpy(AdapterList[i],temp1,(temp-temp1)*2);
00097                 temp1=temp+1;
00098                 i++;
00099         }
00100     
00101         temp++;
00102         }
00103       
00104         AdapterNum=i;
00105         for (i=0;i<AdapterNum;i++)
00106             wprintf(L"\n%d- %s\n",i+1,AdapterList[i]);
00107         printf("\n");
00108         
00109     }
00110 
00111     else    //windows 95
00112     {
00113         AdapterLength = sizeof(AdapterNamea);
00114 
00115         if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE){
00116             printf("Unable to retrieve the list of the adapters!\n");
00117             return -1;
00118         }
00119         tempa=AdapterNamea;
00120         temp1a=AdapterNamea;
00121 
00122         while ((*tempa!='\0')||(*(tempa-1)!='\0'))
00123         {
00124             if (*tempa=='\0') 
00125             {
00126                 memcpy(AdapterList[i],temp1a,tempa-temp1a);
00127                 temp1a=tempa+1;
00128                 i++;
00129             }
00130             tempa++;
00131         }
00132           
00133         AdapterNum=i;
00134         for (i=0;i<AdapterNum;i++)
00135             printf("\n%d- %s\n",i+1,AdapterList[i]);
00136         printf("\n");
00137 
00138     }
00139 
00140     do 
00141     {
00142         printf("Select the number of the adapter to open : ");
00143         scanf("%d",&Open);
00144         if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 
00145     } while (Open>AdapterNum);
00146     
00147 
00148     
00149     
00150     lpAdapter =   PacketOpenAdapter(AdapterList[Open-1]);
00151     
00152     if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
00153     {
00154         dwErrorCode=GetLastError();
00155         printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 
00156 
00157         return -1;
00158     }   
00159 
00160     // set the network adapter in promiscuous mode
00161     
00162     if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){
00163             printf("Warning: unable to set promiscuous mode!\n");
00164     }
00165 
00166     // set a 512K buffer in the driver
00167     if(PacketSetBuff(lpAdapter,512000)==FALSE){
00168             printf("Unable to set the kernel buffer!\n");
00169             return -1;
00170     }
00171 
00172     // set a 1 second read timeout
00173     if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){
00174             printf("Warning: unable to set the read tiemout!\n");
00175     }
00176 
00177     //allocate and initialize a packet structure that will be used to
00178     //receive the packets.
00179     if((lpPacket = PacketAllocatePacket())==NULL){
00180         printf("\nError: failed to allocate the LPPACKET structure.");
00181         return (-1);
00182     }
00183     PacketInitPacket(lpPacket,(char*)buffer,256000);
00184     
00185     //main capture loop
00186     while(!kbhit())
00187     {
00188         // capture the packets
00189         if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE){
00190             printf("Error: PacketReceivePacket failed");
00191             return (-1);
00192         }
00193 
00194         PrintPackets(lpPacket);
00195     }
00196 
00197 
00198     //print the capture statistics
00199     if(PacketGetStats(lpAdapter,&stat)==FALSE){
00200             printf("Warning: unable to get stats from the kernel!\n");
00201     }
00202     else
00203         printf("\n\n%d packets received.\n%d Packets lost",stat.bs_recv,stat.bs_drop);
00204 
00205     PacketFreePacket(lpPacket);
00206     
00207     // close the adapter and exit
00208 
00209     PacketCloseAdapter(lpAdapter);
00210     return (0);
00211 }
00212 
00213 // this function prints the content of a block of packets received from the driver
00214 
00215 void PrintPackets(LPPACKET lpPacket)
00216 {
00217 
00218     ULONG   i, j, ulLines, ulen, ulBytesReceived;
00219     char    *pChar, *pLine, *base;
00220     char    *buf;
00221     u_int off=0;
00222     u_int tlen,tlen1;
00223     struct bpf_hdr *hdr;
00224     
00225         ulBytesReceived = lpPacket->ulBytesReceived;
00226 
00227 
00228         buf = lpPacket->Buffer;
00229 
00230         off=0;
00231 
00232         while(off<ulBytesReceived){ 
00233             if(kbhit())return;
00234         hdr=(struct bpf_hdr *)(buf+off);
00235         tlen1=hdr->bh_datalen;
00236         tlen=hdr->bh_caplen;
00237         printf("Packet length, captured portion: %ld, %ld\n", tlen1, tlen);
00238         off+=hdr->bh_hdrlen;
00239 
00240         ulLines = (tlen + 15) / 16;
00241 
00242         pChar =(char*)(buf+off);
00243         base=pChar;
00244         off=Packet_WORDALIGN(off+tlen);
00245         
00246         for ( i=0; i<ulLines; i++ )
00247         {
00248 
00249             pLine =pChar;
00250 
00251             printf( "%08lx : ", pChar-base );
00252 
00253             ulen=tlen;
00254             ulen = ( ulen > 16 ) ? 16 : ulen;
00255             tlen -= ulen;
00256 
00257             for ( j=0; j<ulen; j++ )
00258                 printf( "%02x ", *(BYTE *)pChar++ );
00259 
00260             if ( ulen < 16 )
00261                 printf( "%*s", (16-ulen)*3, " " );
00262 
00263             pChar = pLine;
00264 
00265             for ( j=0; j<ulen; j++, pChar++ )
00266                 printf( "%c", isprint( *pChar ) ? *pChar : '.' );
00267 
00268             printf( "\n" );
00269         } 
00270 
00271         printf( "\n" );
00272         }
00273 } 
00274 
00275  

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