Skip to content

Commit

Permalink
WinDivertAddress完善
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Sep 24, 2022
1 parent 95b1716 commit af666ba
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 229 deletions.
6 changes: 3 additions & 3 deletions App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ internal class Program
{
unsafe static void Main(string[] args)
{
using var win = new WinDivert("udp.DstPort == 53", WindivertLayer.Network, 0, WindivertFlag.ReadWrite);
using var win = new WinDivert("tcp.DstPort > 0", WinDivertLayer.Network);
using var packet = new WinDivertPacket();
var addr = new WindivertAddress();
var b = win.WinDivertRecv(packet, ref addr);
var addr = new WinDivertAddress();
var b = win.WinDivertRecv(packet, ref addr);
var result = packet.GetParseResult();

Console.WriteLine("Hello, World!");
Expand Down
11 changes: 0 additions & 11 deletions WindivertDotnet/WINDIVERT_DATA_NETWORK.cs

This file was deleted.

20 changes: 13 additions & 7 deletions WindivertDotnet/WinDivert.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.ComponentModel;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace WindivertDotnet
{
Expand All @@ -16,7 +16,7 @@ public partial class WinDivert : IDisposable
/// <param name="priority"></param>
/// <param name="flags"></param>
/// <exception cref="Win32Exception"></exception>
public WinDivert(string filter, WindivertLayer layer, short priority, WindivertFlag flags)
public WinDivert(string filter, WinDivertLayer layer, short priority = 0, WinDivertFlag flags = WinDivertFlag.None)
{
this.handle = WinDivertNative.WinDivertOpen(filter, layer, priority, flags);
if (this.handle.IsInvalid)
Expand All @@ -26,16 +26,22 @@ public WinDivert(string filter, WindivertLayer layer, short priority, WindivertF
}


public bool WinDivertRecv(
WinDivertPacket packet,
ref WindivertAddress addr)
public bool WinDivertRecv(WinDivertPacket packet, ref WinDivertAddress addr)
{
var length = 0;
var result = WinDivertNative.WinDivertRecv(this.handle, packet.Handle, packet.Capacity, ref length, ref addr);
packet.Length = length;
return result;
}

}

public Task<bool> WinDivertRecvAsync(WinDivertPacket packet, ref WinDivertAddress addr)
{
var length = 0;
var result = WinDivertNative.WinDivertRecv(this.handle, packet.Handle, packet.Capacity, ref length, ref addr);
packet.Length = length;
return Task.FromResult(true);
}


public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace WindivertDotnet
{
public unsafe partial struct WINDIVERT_DATA_FLOW
public unsafe struct WinDivertDataFlow
{
[NativeTypeName("UINT64")]
public ulong EndpointId;
Expand Down
9 changes: 9 additions & 0 deletions WindivertDotnet/WinDivertDataNetwork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WindivertDotnet
{
public struct WinDivertDataNetwork
{
public int IfIdx;

public int SubIfIdx;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace WindivertDotnet
{
public partial struct WINDIVERT_DATA_REFLECT
public struct WinDivertDataReflect
{
[NativeTypeName("INT64")]
public long Timestamp;

[NativeTypeName("UINT32")]
public uint ProcessId;

public WindivertLayer Layer;
public WinDivertLayer Layer;

[NativeTypeName("UINT64")]
public ulong Flags;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace WindivertDotnet
{
public unsafe partial struct WINDIVERT_DATA_SOCKET
public unsafe struct WinDivertDataSocket
{
[NativeTypeName("UINT64")]
public ulong EndpointId;
Expand Down
6 changes: 4 additions & 2 deletions WindivertDotnet/WinDivertHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ namespace WindivertDotnet
{
sealed class WinDivertHandle : SafeHandle
{
private static readonly IntPtr invalidHandleValue = new(unchecked((long)ulong.MaxValue));

private WinDivertHandle()
: base(invalidHandleValue: IntPtr.MaxValue, ownsHandle: true)
: base(invalidHandleValue, ownsHandle: true)
{
}

public override bool IsInvalid => this.handle == IntPtr.MaxValue;
public override bool IsInvalid => this.handle == invalidHandleValue;

protected override bool ReleaseHandle()
{
Expand Down
35 changes: 21 additions & 14 deletions WindivertDotnet/WinDivertNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,52 @@ static unsafe class WinDivertNative
[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern WinDivertHandle WinDivertOpen(
[MarshalAs(UnmanagedType.LPStr)] string filter,
WindivertLayer layer,
WinDivertLayer layer,
short priority,
WindivertFlag flags);
WinDivertFlag flags);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern bool WinDivertRecv(
WinDivertHandle handle,
IntPtr pPacket,
int packetLen,
ref int pRecvLen,
ref WindivertAddress pAddr);
ref WinDivertAddress pAddr);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertRecvEx([NativeTypeName("HANDLE")] WinDivertHandle handle, void* pPacket, uint packetLen, uint* pRecvLen, [NativeTypeName("UINT64")] ulong flags, WindivertAddress* pAddr, uint* pAddrLen, [NativeTypeName("LPOVERLAPPED")] NativeOverlapped* lpOverlapped);
public static extern bool WinDivertRecvEx(
WinDivertHandle handle,
IntPtr pPacket,
int packetLen,
ref int pRecvLen,
ulong flags,
ref WinDivertAddress pAddr,
ref uint pAddrLen,
ref NativeOverlapped lpOverlapped);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertSend([NativeTypeName("HANDLE")] WinDivertHandle handle, [NativeTypeName("const void *")] void* pPacket, uint packetLen, uint* pSendLen, [NativeTypeName("const WINDIVERT_ADDRESS *")] WindivertAddress* pAddr);
public static extern int WinDivertSend([NativeTypeName("HANDLE")] WinDivertHandle handle, [NativeTypeName("const void *")] void* pPacket, uint packetLen, uint* pSendLen, [NativeTypeName("const WINDIVERT_ADDRESS *")] WinDivertAddress* pAddr);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertSendEx([NativeTypeName("HANDLE")] WinDivertHandle handle, [NativeTypeName("const void *")] void* pPacket, uint packetLen, uint* pSendLen, [NativeTypeName("UINT64")] ulong flags, [NativeTypeName("const WINDIVERT_ADDRESS *")] WindivertAddress* pAddr, uint addrLen, [NativeTypeName("LPOVERLAPPED")] NativeOverlapped* lpOverlapped);
public static extern int WinDivertSendEx([NativeTypeName("HANDLE")] WinDivertHandle handle, [NativeTypeName("const void *")] void* pPacket, uint packetLen, uint* pSendLen, [NativeTypeName("UINT64")] ulong flags, [NativeTypeName("const WINDIVERT_ADDRESS *")] WinDivertAddress* pAddr, uint addrLen, [NativeTypeName("LPOVERLAPPED")] NativeOverlapped* lpOverlapped);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertShutdown([NativeTypeName("HANDLE")] WinDivertHandle handle, WindivertShutdown how);
public static extern int WinDivertShutdown([NativeTypeName("HANDLE")] WinDivertHandle handle, WinDivertShutdown how);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern bool WinDivertClose([NativeTypeName("HANDLE")] IntPtr handle);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertSetParam([NativeTypeName("HANDLE")] WinDivertHandle handle, WindivertParam param1, [NativeTypeName("UINT64")] ulong value);
public static extern int WinDivertSetParam([NativeTypeName("HANDLE")] WinDivertHandle handle, WinDivertParam param1, [NativeTypeName("UINT64")] ulong value);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertGetParam([NativeTypeName("HANDLE")] WinDivertHandle handle, WindivertParam param1, [NativeTypeName("UINT64 *")] ulong* pValue);
public static extern int WinDivertGetParam([NativeTypeName("HANDLE")] WinDivertHandle handle, WinDivertParam param1, [NativeTypeName("UINT64 *")] ulong* pValue);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("UINT64")]
Expand Down Expand Up @@ -89,23 +96,23 @@ public static extern bool WinDivertHelperParsePacket(

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertHelperCalcChecksums(void* pPacket, uint packetLen, WindivertAddress* pAddr, [NativeTypeName("UINT64")] ulong flags);
public static extern int WinDivertHelperCalcChecksums(void* pPacket, uint packetLen, WinDivertAddress* pAddr, [NativeTypeName("UINT64")] ulong flags);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertHelperDecrementTTL(void* pPacket, uint packetLen);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertHelperCompileFilter([NativeTypeName("const char *")] sbyte* filter, WindivertLayer layer, [NativeTypeName("char *")] sbyte* @object, uint objLen, [NativeTypeName("const char **")] sbyte** errorStr, uint* errorPos);
public static extern int WinDivertHelperCompileFilter([NativeTypeName("const char *")] sbyte* filter, WinDivertLayer layer, [NativeTypeName("char *")] sbyte* @object, uint objLen, [NativeTypeName("const char **")] sbyte** errorStr, uint* errorPos);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertHelperEvalFilter([NativeTypeName("const char *")] sbyte* filter, [NativeTypeName("const void *")] void* pPacket, uint packetLen, [NativeTypeName("const WINDIVERT_ADDRESS *")] WindivertAddress* pAddr);
public static extern int WinDivertHelperEvalFilter([NativeTypeName("const char *")] sbyte* filter, [NativeTypeName("const void *")] void* pPacket, uint packetLen, [NativeTypeName("const WINDIVERT_ADDRESS *")] WinDivertAddress* pAddr);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("BOOL")]
public static extern int WinDivertHelperFormatFilter([NativeTypeName("const char *")] sbyte* filter, WindivertLayer layer, [NativeTypeName("char *")] sbyte* buffer, uint bufLen);
public static extern int WinDivertHelperFormatFilter([NativeTypeName("const char *")] sbyte* filter, WinDivertLayer layer, [NativeTypeName("char *")] sbyte* buffer, uint bufLen);

[DllImport(library, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[return: NativeTypeName("UINT16")]
Expand Down
Loading

0 comments on commit af666ba

Please sign in to comment.