[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tcpdump-patches
Subject:    Interaction of Packet.dll with Visual Basic
From:       "taganov" <taganov () netzero ! net>
Date:       2001-04-05 15:04:29
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello,

My name is Chris McKenzie.  I am Project Manager of a Windows based project written \
in Visual Basic.  I had come across your site awhile back while looking for \
information about how to set up a network card in promiscuous mode and listen to the \
data.  You may remember me asking questions about whether your Packet.dll could be \
used from Visual Basic or not.  

Well, I have answered that question.  As your Packet.dll currently stands, it is not \
usable from Visual Basic.  The reasons for this are many.  For starters, your dll \
makes much use of unsigned integers and longs, which Visual Basic is incapable of \
supporting.  So passing of integers and variables back and forth from Visual Basic \
and Packet.dll is impossible given your current architecture. In addition, VB cannot \
find the function calls in the dll.  It finds the dll just fine, but not any \
functions that require arguments.  I have found that adding a single word to the \
declaration of your functions solves this problem.  Thus,

    BOOLEAN PacketOpenVBAdapter (LPTSTR AdapterName)

    becomes

    BOOLEAN APIENTRY PacketOpenVBAdapter (LPTSTR AdapterName)

Of course, this has to be updated in the Packet32.h file as well.  

There is an additional problem with using your dll with VB. Many of your functions \
are declared as Structures, or pointers to structures, or other data types that VB \
cannot understand.

I have modified part of you packet.dll to function with my project.  What I've done, \
is created APIENTRY functions that VB can understand and talk to in your dll that \
then call the actual function that is targeted.  For instance \
                /*********************************************************************************
                
----------------------------------------------------------------------------------
 Added this function to be compatible with Visual Basic 6.0

    Parameters:

 LPTSTR (pointer to a string) LpAdapter is the name of the adapter to open.
   This information is found by calling the PacketGetAdapterNames routine.

 Programmed by: Chris McKenzie
----------------------------------------------------------------------------------
**********************************************************************************/
BOOLEAN APIENTRY PacketOpenVBAdapter (LPTSTR AdapterName)
{
 VBAdapter = PacketOpenAdapter(AdapterName);
 if (VBAdapter != NULL )
 { return TRUE;}
 else
 { return FALSE;}
}

This function uses a public adapter internal to the dll called VBAdapter.  There is \
also a corresponding VBPacket.  Visual Basic calls PacketOpenVBAdapter(), which then \
calls PacketOpenAdapter().  The process of declaring an adapter structure and packet \
structure is entirely invisible to the VB side of the operation. 

I took some of the functions from your testapp example and added modified versions of \
them to the dll.  In particular, ReturnPackets() is a modified version of \
PrintPackets from your testapp. In this manner, I was able to connect to, initialize, \
open, read the raw data coming in over the network card.  

I have included my changes to your source code.  I am aware that my code is probably \
very sloppy--but I am not a very experienced C++ programmer (I've done the "Hello \
World!" type stuff before this, but that's all). But perhaps it will help you to make \
your dll more accessible to other, higher-level languages, if you so desire.  I also \
would like to say, thank you for your amazing work on the entire WinPCap library.  

Sincerely,
Chris McKenzie


[Attachment #5 (text/html)]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4611.1300" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hello,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>My name is Chris McKenzie.&nbsp; I am Project 
Manager of a Windows based project written in Visual Basic.&nbsp; I had come 
across your site awhile back while looking for information about how to set up a 
network card in promiscuous mode and listen to the data.&nbsp; You may remember 
me asking questions about whether your Packet.dll could be used from Visual 
Basic or not.&nbsp; </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Well, I have answered that question.&nbsp; As your 
Packet.dll currently stands, it is not usable from Visual Basic.&nbsp; The 
reasons for this are many.&nbsp; For starters, your dll makes much use of 
unsigned integers and longs, which Visual Basic is incapable of 
supporting.&nbsp; So passing of integers and variables back and forth from 
Visual Basic and Packet.dll is impossible given your current architecture. In 
addition, VB cannot find the function calls in the dll.&nbsp; It finds the dll 
just fine, but not any functions that require arguments.&nbsp; I have found that 
adding a single word to the declaration of your functions solves this 
problem.&nbsp; Thus,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; BOOLEAN PacketOpenVBAdapter 
(LPTSTR AdapterName)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; becomes</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; BOOLEAN APIENTRY 
PacketOpenVBAdapter (LPTSTR AdapterName)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Of course, this has to be updated in the Packet32.h 
file as well.&nbsp; </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>There is an additional problem with using your dll 
with VB. Many of your functions are declared as Structures, or pointers to 
structures, or other data types that VB cannot understand.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I have modified part of you packet.dll to function 
with my project.&nbsp; What I've done, is created APIENTRY functions that VB 
<EM>can</EM> understand and talk to in your dll that then call the 
<EM>actual</EM> function that is targeted.&nbsp; For instance</FONT></DIV>
<DIV><FONT face=Arial 
size=2>/****************************************************************************** \
***<BR>----------------------------------------------------------------------------------<BR>&nbsp;Added \
 this function to be compatible with Visual Basic 6.0</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;Parameters:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;LPTSTR&nbsp;(pointer to a string) LpAdapter 
is the name of the adapter to open.<BR>&nbsp;&nbsp;&nbsp;This information is 
found by calling the PacketGetAdapterNames routine.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;Programmed by: Chris 
McKenzie<BR>-------------------------------------------------------------------------- \
--------<BR>**********************************************************************************/<BR>BOOLEAN \
 APIENTRY PacketOpenVBAdapter (LPTSTR AdapterName)<BR>{<BR>&nbsp;VBAdapter = 
PacketOpenAdapter(AdapterName);<BR>&nbsp;if (VBAdapter != NULL 
)<BR>&nbsp;{&nbsp;return TRUE;}<BR>&nbsp;else<BR>&nbsp;{&nbsp;return 
FALSE;}<BR>}</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>This function uses a public adapter internal to the 
dll called VBAdapter.&nbsp; There is also a corresponding VBPacket.&nbsp; Visual 
Basic calls PacketOpenVBAdapter(), which then calls PacketOpenAdapter().&nbsp; 
The process of declaring an adapter structure and packet structure is entirely 
invisible to the VB side of the operation. </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I took some of the functions from your testapp 
example and added modified versions of them to the dll.&nbsp; In particular, 
ReturnPackets() is a modified version of PrintPackets from your testapp. In this 
manner, I was able to connect to, initialize, open, read the raw data coming in 
over the network card.&nbsp;&nbsp;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I have included my changes to your source 
code.&nbsp; I am aware that my code is probably very sloppy--but I am not a very 
experienced C++ programmer (I've done the "Hello World!" type stuff before this, 
but that's all). But perhaps it will help you to make your dll more accessible 
to other, higher-level languages, if you so desire.&nbsp; I also would like to 
say, thank you for your amazing work on the entire WinPCap library.&nbsp; 
</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Sincerely,</FONT></DIV>
<DIV><FONT face=Arial size=2>Chris McKenzie</FONT></DIV></BODY></HTML>


["Packet.def" (application/octet-stream)]
["Packet32.c" (application/octet-stream)]

/*
 * Copyright (c) 1999, 2000
 *	Politecnico di Torino.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the Politecnico
 * di Torino, and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <windows.h>
#include <windowsx.h>
#include <crtdbg.h>
#include <ntddndis.h>
#include <ntddpack.h>
#include <packet32.h>
#include <sys/timeb.h>
#include <string.h>


#ifdef __cplusplus
extern "C"
{		
#endif				

TCHAR szWindowTitle[] = TEXT ("PACKET.DLL");
LPADAPTER lpTheAdapter = NULL;

#if DBG
#define ODS(_x) OutputDebugString(TEXT(_x))
#else
#define ODS(_x)
#endif

typedef DWORD(CALLBACK* OPENVXDHANDLE)(HANDLE);

BOOLEAN StartPacketDriver (LPTSTR ServiceName);
BOOLEAN StopPacketDriver (void);

/*********************************************************************************
----------------------------------------------------------------------------------
	These are things I added to the DLL to make it accessible to VB.

	Programmed by: Chris McKenzie
----------------------------------------------------------------------------------
**********************************************************************************/
LPADAPTER VBAdapter = NULL;
LPPACKET VBPacket;
#define SIMULTANEOU_READS 10
#define MAX_ETHERNET_FRAME_SIZE 1514

#define Max_Num_Adapter 10

//---------------------------------------------------------------------------
BOOL APIENTRY DllMain (HANDLE hModule,
					DWORD dwReason,
					LPVOID lpReserved)
{
    BOOL Status;

	ODS ("Packet32: DllEntry\n");
    switch (dwReason)
	{
		case DLL_PROCESS_ATTACH:
			Status = StartPacketDriver (TEXT ("PACKET"));
			break;
		case DLL_PROCESS_DETACH:
			Status = StopPacketDriver ();
			break;
		default:
			Status = TRUE;
			break;
	}
	return Status;
}

//---------------------------------------------------------------------------
BOOL PacketDeviceIoControl (LPADAPTER lpAdapterObject,
			      LPPACKET lpPacket,
			      ULONG ulIoctl,
			      BOOLEAN bSync)
{
	BOOLEAN Result;
	DWORD Error;

	ODS ("Packet32: PacketDeviceIoControl\n");
	_ASSERTE (lpAdapterObject != NULL);
	_ASSERTE (lpPacket != NULL);
	lpPacket->OverLapped.Offset = 0;
	lpPacket->OverLapped.OffsetHigh = 0;
	lpPacket->ulBytesReceived		= 0;
	if (!ResetEvent (lpPacket->OverLapped.hEvent))
	{
		lpPacket->bIoComplete = FALSE;
		return FALSE;
	}

    Result = DeviceIoControl (lpAdapterObject->hFile,
				ulIoctl,
				lpPacket->Buffer,
				lpPacket->Length,
				lpPacket->Buffer,
				lpPacket->Length,
				&(lpPacket->ulBytesReceived), 
				&(lpPacket->OverLapped));
	Error=GetLastError () ;
    
	if (!Result && bSync)
	{
		if (Error == ERROR_IO_PENDING)
		{
			Result = GetOverlappedResult (lpAdapterObject->hFile,
					&(lpPacket->OverLapped),
					&(lpPacket->ulBytesReceived), 
					TRUE);
		}
		else
			ODS ("Packet32: unsupported API call return error!\n");
	}

	lpPacket->bIoComplete = Result;

	return Result;
}


//---------------------------------------------------------------------------

/*********************************************************************************
----------------------------------------------------------------------------------
	Added this function to be compatible with Visual Basic 6.0

				Parameters:

	LPTSTR	(pointer to a string) LpAdapter is the name of the adapter to open.
			This information is found by calling the PacketGetAdapterNames routine.

	Programmed by: Chris McKenzie
----------------------------------------------------------------------------------
**********************************************************************************/
BOOLEAN APIENTRY PacketOpenVBAdapter (LPTSTR AdapterName)
{
	VBAdapter = PacketOpenAdapter(AdapterName);
	if (VBAdapter != NULL )
	{	return TRUE;}
	else
	{	return FALSE;}
}


LPADAPTER PacketOpenAdapter (LPTSTR AdapterName)
{

	LPPACKET	lpSupport;
 /*_PACKET {  
			HANDLE       hEvent;		//void *HANDLE;
			OVERLAPPED   OverLapped;	//_OVERLAPPED {
												DWORD   Internal;	//unsigned long DWORD;
												DWORD   InternalHigh;//unsigned long DWORD;
												DWORD   Offset;		//unsigned long DWORD;
												DWORD   OffsetHigh;	//unsigned long DWORD;
												HANDLE  hEvent;		//void *HANDLE;
												} OVERLAPPED, *LPOVERLAPPED;
			PVOID        Buffer;		// void *PVOID;
            UINT         Length;		// unsigned int
			UINT         ulBytesReceived;//unsigned int
			BOOLEAN      bIoComplete;	// BYTE  BOOLEAN; //unsigned char BYTE;
			}  PACKET, *LPPACKET;*/
	LPADAPTER	nAdapter;
/*_ADAPTER  { 
			HANDLE hFile;								//void *HANDLE;
            TCHAR  SymbolicLink[MAX_LINK_NAME_LENGTH];	//char TCHAR;
			int NumWrites;						
			HANDLE ReadEvent;							//void *HANDLE;
						 }  ADAPTER, *LPADAPTER;*/
	DWORD		error;					//unsigned long DWORD
	BOOL		res;					//int BOOL;
	OPENVXDHANDLE OpenVxDHandle;		//DWORD(CALLBACK* OPENVXDHANDLE)(HANDLE);
	DWORD		KernEvent;				//unsigned long DWORD;
	UINT		BytesReturned;			//unsigned int;
	struct _timeb time;

    ODS ("Packet32: PacketOpenAdapter\n");
	
	nAdapter = (LPADAPTER) GlobalAllocPtr (GMEM_MOVEABLE | GMEM_ZEROINIT,sizeof \
(ADAPTER));  if (nAdapter == NULL)
	{
		error=GetLastError();
		ODS ("Packet32: PacketOpenAdapter GlobalAlloc Failed\n");
		//set the error to the one on which we failed
		SetLastError(error);
		return NULL;
	}
	wsprintf (nAdapter->SymbolicLink,
		TEXT ("\\\\.\\%s"),
		TEXT ("PACKET.VXD"));
	nAdapter->hFile = CreateFile (nAdapter->SymbolicLink,
		GENERIC_WRITE | GENERIC_READ,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_FLAG_OVERLAPPED | FILE_FLAG_DELETE_ON_CLOSE, 
		0);

	if (nAdapter->hFile == INVALID_HANDLE_VALUE)
	{
		error=GetLastError();
		ODS ("Packet32: PacketOpenAdapter Could not open adapter\n");
		GlobalFreePtr (nAdapter);
		//set the error to the one on which we failed
		SetLastError(error);
		return NULL;
	}
	
	lpSupport=PacketAllocatePacket();
	
	PacketInitPacket(lpSupport,AdapterName,strlen(AdapterName));
	if (nAdapter && (nAdapter->hFile != INVALID_HANDLE_VALUE))
	{
		res=PacketDeviceIoControl(nAdapter,
			lpSupport,
			(ULONG) IOCTL_OPEN,
			TRUE);
		if (res==FALSE || ((char*)lpSupport->Buffer)[0]=='\0'){
			SetLastError(ERROR_FILE_NOT_FOUND);
			goto err;
		}
		PacketFreePacket(lpSupport);

		// Set the time zone
		_ftime(&time);
		if(DeviceIoControl(nAdapter->hFile,pBIOCSTIMEZONE,&time.timezone,2,NULL,0,&BytesReturned,NULL)==FALSE){
  error=GetLastError();
			ODS ("Packet32: PacketOpenAdapter Could not open adapter\n");
			GlobalFreePtr (nAdapter);
			//set the error to the one on which we failed
			SetLastError(error);
			return NULL;	
		}
		
		// create the read event
		nAdapter->ReadEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
		// obtain the pointer of OpenVxDHandle in KERNEL32.DLL.
		// It is not possible to reference statically this function, because it is present \
only in Win9x  OpenVxDHandle = (OPENVXDHANDLE) \
GetProcAddress(GetModuleHandle("KERNEL32"),"OpenVxDHandle");  // map the event to \
kernel mode  KernEvent=(DWORD)(OpenVxDHandle)(nAdapter->ReadEvent);
		// pass the event to the driver
		if(DeviceIoControl(nAdapter->hFile,pBIOCEVNAME,&KernEvent,4,NULL,0,&BytesReturned,NULL)==FALSE){
  error=GetLastError();
			ODS ("Packet32: PacketOpenAdapter Could not open adapter\n");
			GlobalFreePtr (nAdapter);
			//set the error to the one on which we failed
			SetLastError(error);
			return NULL;	
		}

		//set the maximum lookhahead size allowable with this NIC driver
		PacketSetMaxLookaheadsize(nAdapter);

		//set the number of times a single write will be repeated
		PacketSetNumWrites(nAdapter,1);
		
		return nAdapter;
	}
err:
	error=GetLastError();
	ODS ("Packet32: PacketOpenAdapter Could not open adapter\n");
	//set the error to the one on which we failed
	SetLastError(error);
    return NULL;
}


/*********************************************************************************
----------------------------------------------------------------------------------
	Added this function to be compatible with Visual Basic 6.0

				Parameters:

	LPTSTR	(pointer to a string) LpAdapter is the name of the adapter to open.
			This information is found by calling the PacketGetAdapterNames routine.

	Programmed by: Chris McKenzie
----------------------------------------------------------------------------------
**********************************************************************************/
BOOLEAN InitPacketDriver()
{
	DWORD      dwErrorCode;
	
	char buffer[256000];  // buffer to hold the data coming from the driver

	//struct bpf_stat stat;
	// the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
	// We have to check the os on which we are running	
	
	if (!VBAdapter || (VBAdapter->hFile == INVALID_HANDLE_VALUE))
	{
		dwErrorCode=GetLastError();

		return FALSE;
	}	

	// set the network adapter in promiscuous mode
	PacketSetHwFilter(VBAdapter,NDIS_PACKET_TYPE_PROMISCUOUS);

	// set a 512K buffer in the driver
	PacketSetBuff(VBAdapter,512000);

	// set a 1 second read timeout
	PacketSetReadTimeout(VBAdapter,1000);
	
	//allocate and initialize a packet structure that will be used to
	//receive the packets.
	if((VBPacket = PacketAllocatePacket())==NULL){
		return FALSE;
	}
	PacketInitPacket(VBPacket,(char*)buffer,256000);
	
	return TRUE;
}

/*********************************************************************************
----------------------------------------------------------------------------------
	Added this function to be compatible with Visual Basic 6.0

				Parameters:

	char *buffer	pointer to a string to hold the packets.
	int BufferLen	integer value containing the length of the packet.

	Programmed by: Chris McKenzie
----------------------------------------------------------------------------------
**********************************************************************************/
BOOLEAN APIENTRY ReturnPackets(char *buff, int *BufferLen)
{

	ULONG	ulBytesReceived;//j,ulLines, ulen, 	//unsigned long integers
	char	*buf;
	u_int off=0;									//unsigned integer

		// capture the packets
	if(PacketReceivePacket(VBAdapter,VBPacket,TRUE)==FALSE){
		return FALSE;}

	ulBytesReceived = VBPacket->ulBytesReceived;
	buf = VBPacket->Buffer;
	off = 0;
	while(off < ulBytesReceived){
		//copy vbpacket into buffer 
	   *buff = *buf;
		//increment pointers.
		buff++; buf++; off++;
	}
	//return the length of the buffer area
	*BufferLen = ulBytesReceived;
	return TRUE;
} 

 
//---------------------------------------------------------------------------
/* Function to set the working mode of the driver
mode	working mode
*/

BOOLEAN PacketSetMode(LPADAPTER AdapterObject,int mode)
{
	int BytesReturned;
		
    return DeviceIoControl(AdapterObject->hFile,pBIOCSMODE,&mode,4,NULL,0,&BytesReturned,NULL);
 }

//---------------------------------------------------------------------------
/* Function to set the the number of times a write will be repeated*/

BOOLEAN PacketSetNumWrites(LPADAPTER AdapterObject,int nwrites)
{
	AdapterObject->NumWrites=nwrites;
	return TRUE;
}

//---------------------------------------------------------------------------
/* This function is used to set the read timeout
timeout		value of timeout(milliseconds). 0 means infinite.
*/

BOOLEAN PacketSetReadTimeout(LPADAPTER AdapterObject,int timeout)
{
	int BytesReturned;
    return DeviceIoControl(AdapterObject->hFile,pBIOCSRTIMEOUT,&timeout,4,NULL,0,&BytesReturned,NULL);
 }

//---------------------------------------------------------------------------
/* This function allows to set the dimension of the packet buffer in the driver
parameters:
dim		dimension of the buffer (kilobytes)
*/

BOOLEAN PacketSetBuff(LPADAPTER AdapterObject,int dim)
{
	int BytesReturned;
    return DeviceIoControl(AdapterObject->hFile,pBIOCSETBUFFERSIZE,&dim,4,NULL,0,&BytesReturned,NULL);
 }

//---------------------------------------------------------------------------
/* Function to set the minimum amount of bytes that will be copied by the driver*/

BOOLEAN PacketSetMinToCopy(LPADAPTER AdapterObject,int nbytes)
{
	// not yet implemented in Windows 9x
    return TRUE;
}

//---------------------------------------------------------------------------
/* Function to set a bpf filter in the driver
parameters:
fp		the pointer to the beginning of the filtering program
*/

BOOLEAN PacketSetBpf(LPADAPTER AdapterObject,struct bpf_program *fp)
{
	int BytesReturned;

    return DeviceIoControl(AdapterObject->hFile,pBIOCSETF,(char*)fp->bf_insns,fp->bf_len*sizeof(struct \
bpf_insn),NULL,0,&BytesReturned,NULL); }



//---------------------------------------------------------------------------

BOOLEAN PacketGetStats(LPADAPTER AdapterObject,struct bpf_stat *s)
{
	int BytesReturned;

    return DeviceIoControl(AdapterObject->hFile,pBIOCGSTATS,NULL,0,s,sizeof(struct \
bpf_stat),&BytesReturned,NULL); }


//---------------------------------------------------------------------------
/*********************************************************************************
----------------------------------------------------------------------------------
	Added this function to be compatible with Visual Basic 6.0

				Parameters: (none)

----------------------------------------------------------------------------------
**********************************************************************************/
BOOLEAN PacketCloseVBAdapter (void)
{
	PacketFreePacket(VBPacket);
	PacketCloseAdapter(VBAdapter);
	return TRUE;
}


VOID PacketCloseAdapter (LPADAPTER lpAdapter)
{

	ODS ("Packet32: PacketCloseAdapter\n");

	// close the capture handle
	CloseHandle (lpAdapter->hFile);

	// close the read event
	CloseHandle (lpAdapter->ReadEvent);

	GlobalFreePtr (lpAdapter);
	lpAdapter = NULL;

}

//---------------------------------------------------------------------------
LPPACKET PacketAllocatePacket (void)
{
	LPPACKET lpPacket;

    lpPacket = (LPPACKET) GlobalAllocPtr (
					   GMEM_MOVEABLE | GMEM_ZEROINIT,
					   sizeof (PACKET));
    if (lpPacket == NULL)
	{
		ODS ("Packet32: PacketAllocatePacket: GlobalAlloc Failed\n");
		return NULL;
	}
    lpPacket->OverLapped.hEvent = CreateEvent (NULL,
						FALSE,
						FALSE,
						NULL);
    if (lpPacket->OverLapped.hEvent == NULL)
    {
		ODS ("Packet32: PacketAllocatePacket: CreateEvent Failed\n");
		GlobalFreePtr (lpPacket);
		return NULL;
    }
	lpPacket->Buffer=NULL;
	lpPacket->Length=0;
	lpPacket->ulBytesReceived	= 0;
	lpPacket->bIoComplete		= FALSE;

    return lpPacket;
}

//---------------------------------------------------------------------------
VOID PacketFreePacket (LPPACKET lpPacket)
{
	CloseHandle (lpPacket->OverLapped.hEvent);
    GlobalFreePtr (lpPacket);
}

//---------------------------------------------------------------------------
VOID PacketInitPacket (LPPACKET lpPacket,
				       PVOID Buffer,
				       UINT Length)
{
	lpPacket->Buffer = Buffer;
    lpPacket->Length = Length;
}

//---------------------------------------------------------------------------
BOOLEAN PacketSendPacket (LPADAPTER AdapterObject,
						LPPACKET lpPacket,
						BOOLEAN Sync)
{
	int i;
	for(i=0;i<AdapterObject->NumWrites;i++){
		if(PacketDeviceIoControl (AdapterObject,lpPacket,(ULONG) \
IOCTL_PROTOCOL_WRITE,Sync)==FALSE)  return FALSE;
	}

	return TRUE;
}

//---------------------------------------------------------------------------
BOOLEAN PacketReceivePacket(LPADAPTER AdapterObject,
		       LPPACKET lpPacket,
		       BOOLEAN Sync)
{
	return PacketDeviceIoControl(AdapterObject,
								lpPacket,
								(ULONG) IOCTL_PROTOCOL_READ,
								Sync);

}


//---------------------------------------------------------------------------
BOOLEAN PacketWaitPacket (LPADAPTER AdapterObject,
		       LPPACKET lpPacket)
{
	lpPacket->bIoComplete =  GetOverlappedResult( AdapterObject->hFile,
													&lpPacket->OverLapped,
													&lpPacket->ulBytesReceived,
												    TRUE );

	return lpPacket->bIoComplete;

}

//---------------------------------------------------------------------------
BOOLEAN PacketResetAdapter (LPADAPTER AdapterObject)
{
    UINT BytesReturned;
    DeviceIoControl (
		      AdapterObject->hFile,
		      (DWORD) IOCTL_PROTOCOL_RESET,
		      NULL,
		      0,
		      NULL,
		      0,
		      &BytesReturned,
		      NULL);
    return TRUE;
}

//---------------------------------------------------------------------------
BOOLEAN PacketRequest (LPADAPTER AdapterObject,
		    BOOLEAN Set,
		    PPACKET_OID_DATA OidData)
{
    UINT BytesReturned;
    BOOLEAN Result;
	OVERLAPPED Overlap;

	ODS ("Packet32: PacketRequest\n");
	_ASSERTE (AdapterObject != NULL);
	_ASSERTE (OidData != NULL);
	_ASSERTE (OidData->Data != NULL);
	Overlap.Offset = 0;
	Overlap.OffsetHigh = 0;
    Overlap.hEvent = CreateEvent (NULL,
						FALSE,
						FALSE,
						NULL);
    if (Overlap.hEvent == NULL)
    {
		ODS ("Packet32: PacketRequestPacket: CreateEvent Failed\n");
		return FALSE;
    }
	if (!ResetEvent(Overlap.hEvent))
	{
		ODS ("Packet32: PacketRequestPacket: ResetEvent Failed\n");
		return FALSE;
	}
    Result = DeviceIoControl (
			       AdapterObject->hFile,
	    (DWORD) Set ? IOCTL_PROTOCOL_SET_OID : IOCTL_PROTOCOL_QUERY_OID,
			       OidData,
			     sizeof (PACKET_OID_DATA) - 1 + OidData->Length,
			       OidData,
			     sizeof (PACKET_OID_DATA) - 1 + OidData->Length,
			       &BytesReturned,
			       &Overlap);
	if (!Result)
	{
		if (GetLastError() == ERROR_IO_PENDING)
		{
			Result = GetOverlappedResult(AdapterObject->hFile,
										&Overlap,
										&BytesReturned,
										TRUE);
		}
		else
		{
			ODS("Packet32: Unssupported API call return error!\n");
		}
	}
	if (BytesReturned == 0)
	{
		// There was an ndis error
		ODS ("Packet32: Ndis returned error to OID\n");
		Result = FALSE;
	}
	return Result;
}

//---------------------------------------------------------------------------
BOOLEAN PacketSetHwFilter (LPADAPTER AdapterObject,
				      ULONG Filter)
{
	BOOLEAN Status;
    ULONG IoCtlBufferLength = (sizeof (PACKET_OID_DATA) + sizeof (ULONG) - 1);
    PPACKET_OID_DATA OidData;

	ODS ("Packet32: PacketSetFilter\n");
	_ASSERTE (AdapterObject != NULL);
    OidData = GlobalAllocPtr (
			       GMEM_MOVEABLE | GMEM_ZEROINIT,
			       IoCtlBufferLength
      );
    if (OidData == NULL)
	{
		return FALSE;
	}
    OidData->Oid = OID_GEN_CURRENT_PACKET_FILTER;
    OidData->Length = sizeof (ULONG);
    *((PULONG) OidData->Data) = Filter;
    Status = PacketRequest (
			     AdapterObject,
			     TRUE,
			     OidData
      );
    GlobalFreePtr (OidData);
    return Status;
}
//---------------------------------------------------------------------------
BOOLEAN PacketGetNetType (LPADAPTER AdapterObject,NetType *type)
{
	BOOLEAN Status;
    ULONG IoCtlBufferLength = (sizeof (PACKET_OID_DATA) + sizeof (ULONG) - 1);
    PPACKET_OID_DATA OidData;

	ODS ("Packet32: PacketSetFilter\n");
	_ASSERTE (AdapterObject != NULL);
    OidData = GlobalAllocPtr (
			       GMEM_MOVEABLE | GMEM_ZEROINIT,
			       IoCtlBufferLength
      );
    if (OidData == NULL)
	{
		return FALSE;
	}
	//get the link-layer type
    OidData->Oid = OID_GEN_MEDIA_IN_USE;
    OidData->Length = sizeof (ULONG);
    Status = PacketRequest(AdapterObject,FALSE,OidData);
	if(Status==FALSE)return FALSE;

    type->LinkType=*((UINT*)OidData->Data);

	//get the link-layer speed
    OidData->Oid = OID_GEN_LINK_SPEED;
    OidData->Length = sizeof (ULONG);
    Status = PacketRequest(AdapterObject,FALSE,OidData);
	type->LinkSpeed=*((UINT*)OidData->Data)*100;
    GlobalFreePtr (OidData);
    return Status;
}

//---------------------------------------------------------------------------
BOOLEAN PacketSetMaxLookaheadsize (LPADAPTER AdapterObject)
{
	BOOLEAN Status;
    ULONG IoCtlBufferLength = (sizeof (PACKET_OID_DATA) + sizeof (ULONG) - 1);
    PPACKET_OID_DATA OidData;

	ODS ("Packet32: PacketSetFilter\n");
	_ASSERTE (AdapterObject != NULL);
    OidData = GlobalAllocPtr (
			       GMEM_MOVEABLE | GMEM_ZEROINIT,
			       IoCtlBufferLength
      );
    if (OidData == NULL)
	{
		return FALSE;
	}
	//set the size of the lookahead buffer to the maximum available by the the NIC \
driver  OidData->Oid=OID_GEN_MAXIMUM_LOOKAHEAD;
    OidData->Length=sizeof(ULONG);
    Status=PacketRequest(AdapterObject,FALSE,OidData);
    OidData->Oid=OID_GEN_CURRENT_LOOKAHEAD;
    Status=PacketRequest(AdapterObject,TRUE,OidData);
    GlobalFreePtr(OidData);
    return Status;
}

//---------------------------------------------------------------------------

BOOLEAN StartPacketDriver (LPTSTR lpstrServiceName)
{

    ODS ("Packet32: StartPacketDriver\n"); 

	return TRUE;
}

//---------------------------------------------------------------------------
BOOLEAN StopPacketDriver(void)
{
	
    ODS ("Packet32: StopPacketDriver\n"); 

	return TRUE;
}

//---------------------------------------------------------------------------

/*********************************************************************************
----------------------------------------------------------------------------------
	Modified PacketGetAdapterNames with the APIENTRY declarator which makes the
	function visible to Visual Basic.

			Parameters:

	PTSTR	(pointer to a string) pStr is a buffer area for the adapterlist.

	PULONG	(pointer to an unsigned long integer) is the length of the buffer area
			that is used.
----------------------------------------------------------------------------------
**********************************************************************************/
BOOLEAN APIENTRY PacketGetAdapterNames (PTSTR pStr,
				PULONG BufferSize)
{
    ULONG Result,i;
    LONG		Status;
	char		*TpStr;
	char		*TTpStr,*DpStr;
	LPADAPTER	adapter;
    PPACKET_OID_DATA  OidData;
    HKEY		Key,Key1;
	ULONG		BSize;
	ULONG		dim;
	char		NdisName[32];

    OidData=GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,256);
    if (OidData == NULL) {
        return FALSE;
    }

    Status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM",0,KEY_READ,&Key);
    Status=RegOpenKeyEx(Key,"CurrentControlSet",0,KEY_READ,&Key);
    Status=RegOpenKeyEx(Key,"Services",0,KEY_READ,&Key);
    Status=RegOpenKeyEx(Key,"class",0,KEY_READ,&Key);
    Status=RegOpenKeyEx(Key,"net",0,KEY_READ,&Key);
    if (Status != ERROR_SUCCESS) return FALSE;

	TpStr=pStr;
	BSize=*BufferSize;
	i=0;
	while((Result=RegEnumKey(Key,i,NdisName,32))==ERROR_SUCCESS)
	{
		Status=RegOpenKeyEx(Key,NdisName,0,KEY_READ,&Key1);
		Status=RegOpenKeyEx(Key1,"NDIS",0,KEY_READ,&Key1);
		dim=BSize;
        Status=RegQueryValueEx(Key1,"LOGDRIVERNAME",NULL,NULL,(LPBYTE)TpStr,&dim);
		i++;
		if(Status!=ERROR_SUCCESS) continue;
		BSize-=dim;
		TpStr+=dim;
	}
	
	TpStr[0]=0;
	*BufferSize-=BSize;

	if(Result==259){  //259 means OK
		i=0;
	
		(*BufferSize)++;
		TpStr=pStr;
		DpStr=pStr+*BufferSize;	
		
		while(*TpStr!=0){
			
			adapter=PacketOpenAdapter(TpStr);
			if(adapter==NULL){
				strcpy(DpStr,"Unknown");
				DpStr+=7;
				*DpStr++=0;
				
				while(*TpStr!=0){
					TpStr++;
				}
				
				TpStr++;
				continue;
			}
			
			OidData->Oid = OID_GEN_VENDOR_DESCRIPTION;
			OidData->Length = 256;
			Status = PacketRequest(adapter,FALSE,OidData);
			if(Status==0) return FALSE;
			
			TTpStr=(char*)(OidData->Data);
			while(*TTpStr!=0){
				*DpStr++=*TTpStr++;
			}
			*DpStr++=*TTpStr++;
			
			PacketCloseAdapter(adapter);
			
			while(*TpStr!=0){
				TpStr++;
			}
			
			TpStr++;
		}
		*DpStr=0;

		return TRUE;
	}
	
	return FALSE;
}

//---------------------------------------------------------------------------

BOOLEAN PacketGetNetInfo(LPTSTR AdapterName, PULONG netp, PULONG maskp)
{
	struct hostent* h;
	char szBuff[80];
	
    if(gethostname(szBuff, 79)) 
	{
		if(WSAGetLastError()==WSANOTINITIALISED){
			WORD wVersionRequested;
			WSADATA wsaData;

			wVersionRequested = MAKEWORD( 1, 1); 
			if(WSAStartup( wVersionRequested, &wsaData )!=0) return FALSE;

			if(gethostname(szBuff, 79))
			{
				return FALSE;
			}
			h=gethostbyname(szBuff);
			*netp=((h->h_addr_list[0][0]<<24))+
				((h->h_addr_list[0][1]<<16))+
				((h->h_addr_list[0][2]<<8))+
				((h->h_addr_list[0][3]));
			if (((*netp)&0x80000000)==0) *maskp=0xFF000000;
			else if (((*netp)&0xC0000000)==0x80000000) *maskp=0xFFFF0000;
			else if (((*netp)&0xE0000000)==0xC0000000) *maskp=0xFFFFFF00;
			else return FALSE;
			(*netp)&=*maskp;
			return TRUE;
			
		}
		else
		{
			return FALSE;
		}
	}
	
	h=gethostbyname(szBuff);
	*netp=((h->h_addr_list[0][0]<<24))+
		((h->h_addr_list[0][1]<<16))+
		((h->h_addr_list[0][2]<<8))+
		((h->h_addr_list[0][3]));
	if (((*netp)&0x80000000)==0) *maskp=0xFF000000;
	else if (((*netp)&0xC0000000)==0x80000000) *maskp=0xFFFF0000;
	else if (((*netp)&0xE0000000)==0xC0000000) *maskp=0xFFFFFF00;
	else return FALSE;
	(*netp)&=*maskp;
	
	return TRUE;
	
}

//---------------------------------------------------------------------------

#ifdef __cplusplus
}
#endif				



 


["Packet32.h" (application/octet-stream)]
Shop online without a credit card
http://www.rocketcash.com
RocketCash, a NetZero subsidiary


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic