📄 packet32h.cs
字号:
public int NumWrites;
public int ReadEvent;
public int ReadTimeOut;
};
public struct PADAPTER
{
public int hFile;
public int NumWrites;
public int ReadEvent;
public int ReadTimeOut;
};
public struct PACKET
{
public int Buffer;
public int Length;
public int ulBytesReceived;
};
public struct PPACKET
{
public int Buffer;
public int Length;
public int ulBytesReceived;
};
public struct PPACKET_OID_DATA
{
public uint Oid;
public int Length;
public int Data;
};
public struct PACKET_OID_DATA
{
public uint Oid;
public int Length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)] public byte [] Data;
};
public struct SOCKADDR_IN
{
public short sin_family;
public ushort sin_port;
public uint sin_addr;
public byte [] sin_zero; // [8];
};
public struct SOCKADDR
{
public short sin_family;
public ushort sin_port;
public uint sin_addr;
public byte [] sin_zero; // [8];
};
public struct NPF_IF_ADDR
{
public SOCKADDR [] IPAddress; ///< IP address.
public SOCKADDR [] SubnetMask; ///< Netmask for that address.
public SOCKADDR [] Broadcast; ///< Broadcast address.
};
public static uint SOCK_STREAM = 1; // stream socket
public static uint SOCK_DGRAM = 2; // datagram socket
public static uint SOCK_RAW = 3; // raw-protocol interface
public static uint SOCK_RDM = 4; // reliably-delivered message
public static uint SOCK_SEQPACKET = 5; // sequenced packet stream
public static uint SO_DEBUG = 0x0001; // turn on debugging info recording
public static uint SO_ACCEPTCONN = 0x0002; // socket has had listen()
public static uint SO_REUSEADDR = 0x0004; // allow local address reuse
public static uint SO_KEEPALIVE = 0x0008; // keep connections alive
public static uint SO_DONTROUTE = 0x0010; // just use interface addresses
public static uint SO_BROADCAST = 0x0020; // permit sending of broadcast msgs
public static uint SO_USELOOPBACK = 0x0040; // bypass hardware when possible
public static uint SO_LINGER = 0x0080; // linger on close if data present
public static uint SO_OOBINLINE = 0x0100; // leave received OOB data in line
public static uint SO_DONTLINGER = (uint)(~SO_LINGER);
public static uint SO_EXCLUSIVEADDRUSE = ((uint)(~SO_REUSEADDR)); // disallow local address reuse
public static uint SO_SNDBUF = 0x1001; // send buffer size
public static uint SO_RCVBUF = 0x1002; // receive buffer size
public static uint SO_SNDLOWAT = 0x1003; // send low-water mark
public static uint SO_RCVLOWAT = 0x1004; // receive low-water mark
public static uint SO_SNDTIMEO = 0x1005; // send timeout
public static uint SO_RCVTIMEO = 0x1006; // receive timeout
public static uint SO_ERROR = 0x1007; // get error status and clear
public static uint SO_TYPE = 0x1008; // get socket type
public static uint SO_GROUP_ID = 0x2001; // ID of a socket group
public static uint SO_GROUP_PRIORITY = 0x2002; // the relative priority within a group
public static uint SO_MAX_MSG_SIZE = 0x2003; // maximum message size
public static uint SO_PROTOCOL_INFOA = 0x2004; // WSAPROTOCOL_INFOA structure
public static uint SO_PROTOCOL_INFOW = 0x2005; // WSAPROTOCOL_INFOW structure
public static uint PVD_CONFIG = 0x3001; // configuration info for service provider
public static uint SO_CONDITIONAL_ACCEPT = 0x3002; // enable true conditional accept:
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
ref int lpInBuffer, int nInBufferSize,
int lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
int lpInBuffer, int nInBufferSize,
ref int lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
int lpInBuffer, int nInBufferSize,
int lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
ref PPACKET_OID_DATA lpInBuffer, int nInBufferSize,
byte [] lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
byte [] lpInBuffer, int nInBufferSize,
byte [] lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] unsafe public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
char * lpInBuffer, int nInBufferSize,
int lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
int lpInBuffer, int nInBufferSize,
ref BPF_STAT lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
char [] lpInBuffer, int nInBufferSize,
int lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
DeviceIoControl( int hDevice, uint dwIoControlCode,
uint [] lpInBuffer, int nInBufferSize,
int lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, int lpOverlapped );
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
ReadFile( int hFile, int lpBuffer, int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead, int lpOverlapped );
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
WriteFile( int hFile, int lpBuffer, int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten, int lpOverlapped );
[DllImport("kernel32.dll")] public extern static int
WriteFile( int hFile, int lpBuffer, uint nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten, int lpOverlapped );
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
WaitForSingleObject( int hHandle, uint dwMilliseconds );
[DllImport("kernel32.dll")] public extern static int
WaitForSingleObject( int hHandle, int dwMilliseconds );
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
QueryPerformanceFrequency(ref LARGE_INTEGER lpFrequency );
[DllImport("kernel32.dll")] public extern static int
QueryPerformanceFrequency(ref long lpFrequency );
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
QueryPerformanceCounter( ref LARGE_INTEGER lpPerformanceCount ); // address of current counter value
[DllImport("kernel32.dll")] public extern static int
QueryPerformanceCounter( ref long lpPerformanceCount ); // address of current counter value
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
SetEvent( int hEvent ); // handle of event object
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
CloseHandle( int hObject ); // handle to object to close
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
GetFullPathName(
int lpFileName, // address of name of file to find path for
int nBufferLength, // size, in characters, of path buffer
int lpBuffer, // address of path buffer
ref int lpFilePart // address of filename in path
);
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
CreateFile(
int lpFileName, // pointer to name of the file
int dwDesiredAccess, // access (read-write) mode
int dwShareMode, // share mode
int lpSecurityAttributes, // pointer to security attributes
int dwCreationDistribution, // how to create
int dwFlagsAndAttributes, // file attributes
int hTemplateFile // handle to file with attributes to copy
);
[DllImport("kernel32.dll")] public extern static int
CreateFile(
char [] lpFileName, // pointer to name of the file
int dwDesiredAccess, // access (read-write) mode
int dwShareMode, // share mode
int lpSecurityAttributes, // pointer to security attributes
int dwCreationDistribution, // how to create
int dwFlagsAndAttributes, // file attributes
int hTemplateFile // handle to file with attributes to copy
);
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
CreateEvent( int lpEventAttributes, // pointer to security attributes
int bManualReset, // flag for manual-reset event
int bInitialState, // flag for initial state
int lpName // pointer to event-object name
);
//**********************************************************************
[DllImport("kernel32.dll")] public extern static int
OpenEvent(
int dwDesiredAccess, // access flag
int bInheritHandle, // inherit flag
int lpName ); // pointer to event-object name
//**********************************************************************
[DllImport("ws2_32.dll")] public extern static int
WSAStartup ( int wVersionRequested, ref WSADATA lpWSAData );
//**********************************************************************
private ArrayList mCapturedPacketArray = new ArrayList();
private ADAPTERINFO mAdapterInfo;
private ADAPTERINFO_STR mAdapterInfoEx;
private PACKET mPacket;
private ADAPTER mAdapter;
private PACKET_OID_DATA mPacketOidData;
private BPF_STAT mBpfStat;
private BPF_PROGRAM mBpfProgram;
private NETTYPE mNetType;
private NPF_IF_ADDR mNpfIfAddr;
private int mPacketBufferSize;
private int mReadEvent;
private string mAdapterVersion;
private string [] mAdapterNames;
private string [] mAdapterDescriptions;
private int mPacketErrorCode;
private string mPacketErrorMessage;
private string mPacketLibraryVersion;
private int mScmHandle;
private int mSrvHandle;
private string mNPFServiceName;
private string mNPFServiceDesc;
private string mNPFRegistryLocation;
private string mNPFDriverPath;
//****************************************************************
//
//****************************************************************
public Packet32h()
{
mPacketLibraryVersion = "3.0 alpha3";
mPacketOidData.Data = new byte[1024];
mPacket.Buffer = 0;
mPacket.Length = 0;
mPacket.ulBytesReceived = 0;
mAdapter.hFile = 0;
mAdapter.NumWrites = 1;
mAdapter.ReadEvent = 0;
mAdapter.ReadTimeOut = 1000;
mPacketBufferSize = 0;
mReadEvent = 0;
mPacketErrorMessage = "";
mScmHandle = 0;
mSrvHandle = 0;
mNPFServiceName = "NPF";
mNPFServiceDesc = "Netgroup Packet Filter";
mNPFRegistryLocation = "SYSTEM\\CurrentControlSet\\Services\\NPF";
mNPFDriverPath = "system32\\drivers\\npf.sys";
mAdapterNames = new string[1];
mAdapterDescriptions = new string[1];
mAdapterVersion = "";
mNpfIfAddr.Broadcast = new SOCKADDR[10];
mNpfIfAddr.IPAddress = new SOCKADDR[10];
mNpfIfAddr.SubnetMask = new SOCKADDR[10];
mPacketErrorCode = 0;
mPacketErrorMessage = "";
PacketGetAdapterNames();
WinSockInit();
}
//****************************************************************
//
//****************************************************************
public ArrayList CapturedPacketArray
{
get { return mCapturedPacketArray; }
}
public ADAPTERINFO AdapterInfo
{ get { return mAdapterInfo; } }
public ADAPTERINFO_STR AdapterInfoEx
{ get { return mAdapterInfoEx; } }
public PACKET Packet
{ get { return mPacket; } }
public ADAPTER Adapter
{ get { return mAdapter; } }
public BPF_STAT BpfStat
{ get { return mBpfStat; } }
public BPF_PROGRAM BpfProgram
{
get { return mBpfProgram; }
set { mBpfProgram = value; }
}
public NETTYPE NetType
{ get { return mNetType; } }
public NPF_IF_ADDR NpfIfAddr
{ get { return mNpfIfAddr; } }
public int ReadEvent
{ get { return mReadEvent; } }
public string AdapterVersion
{ get { return mAdapterVersion; } }
public string [] AdapterNames
{ get { return mAdapterNames; } }
public string [] AdapterDescriptions
{ get { return mAdapterDescriptions; } }
public int PacketErrorCode
{ get { return mPacketErrorCode; } }
public string PacketErrorMessage
{ get { return mPacketErrorMessage; } }
public string PacketGetVersion()
{ return mPacketLibraryVersion; }
public void PacketGetReadEvent()
{ mReadEvent = mAdapter.ReadEvent; }
//****************************************************************
//
//****************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -