From 64ceda0d54edd728040258662c091d2ef31f66bd Mon Sep 17 00:00:00 2001
From: xljiulang <366193849@qq.com>
Date: Fri, 28 Oct 2022 22:12:56 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DWinDivertPacket=E5=88=9B?=
=?UTF-8?q?=E5=BB=BA=E6=97=B6=E7=94=B3=E8=AF=B7=E4=B8=A4=E6=AC=A1=E5=86=85?=
=?UTF-8?q?=E5=AD=98=E7=9A=84BUG?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
WindivertDotnet.Test/WinDivertAddressTest.cs | 3 +++
WindivertDotnet/WinDivertPacket.cs | 22 +++++++++-----------
WindivertDotnet/WindivertDotnet.csproj | 2 +-
3 files changed, 14 insertions(+), 13 deletions(-)
diff --git a/WindivertDotnet.Test/WinDivertAddressTest.cs b/WindivertDotnet.Test/WinDivertAddressTest.cs
index 111b1c7..52fcd49 100644
--- a/WindivertDotnet.Test/WinDivertAddressTest.cs
+++ b/WindivertDotnet.Test/WinDivertAddressTest.cs
@@ -12,6 +12,9 @@ public void CloneTest()
using var clone = addr.Clone();
Assert.Equal(addr.Flags, clone.Flags);
+
+ addr.Flags = WinDivertAddressFlag.IPv6;
+ Assert.NotEqual(addr.Flags, clone.Flags);
}
}
}
diff --git a/WindivertDotnet/WinDivertPacket.cs b/WindivertDotnet/WinDivertPacket.cs
index 4f67645..cf224e6 100644
--- a/WindivertDotnet/WinDivertPacket.cs
+++ b/WindivertDotnet/WinDivertPacket.cs
@@ -60,7 +60,7 @@ public int Length
///
/// 最大容量
public WinDivertPacket(int capacity = MTU_MAX)
- : this(MemoryNative.AllocZeroed(capacity), capacity, ownsHandle: true)
+ : base(ownsHandle: true)
{
this.Capacity = capacity;
this.handle = MemoryNative.AllocZeroed(capacity);
@@ -69,14 +69,14 @@ public WinDivertPacket(int capacity = MTU_MAX)
///
/// WinDivert的数据包
///
- ///
- ///
- ///
- private unsafe WinDivertPacket(IntPtr handle, int capacity, bool ownsHandle)
- : base(ownsHandle)
+ /// 外部创建的句柄
+ /// 字节长度
+ private unsafe WinDivertPacket(IntPtr handle, int length)
+ : base(ownsHandle: false)
{
this.handle = handle;
- this.Capacity = capacity;
+ this.length = length;
+ this.Capacity = length;
}
///
@@ -119,11 +119,7 @@ public unsafe Span GetSpan(int offset, int count)
public unsafe WinDivertPacket Slice(int offset, int count)
{
var pointer = this.GetPointer(offset, count);
- var handle = new IntPtr(pointer);
- return new WinDivertPacket(handle, count, ownsHandle: false)
- {
- length = count
- };
+ return new WinDivertPacket(new IntPtr(pointer), count);
}
@@ -134,6 +130,7 @@ public unsafe WinDivertPacket Slice(int offset, int count)
///
///
///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void* GetPointer(int offset, int count)
{
if (offset < 0 || offset > this.Capacity)
@@ -238,6 +235,7 @@ public bool CalcLoopbackFlag(WinDivertAddress addr)
///
///
///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe bool TryParseIPAddress(
[MaybeNullWhen(false)] out IPAddress srcAddr,
[MaybeNullWhen(false)] out IPAddress dstAddr)
diff --git a/WindivertDotnet/WindivertDotnet.csproj b/WindivertDotnet/WindivertDotnet.csproj
index 06cedc3..3233dde 100644
--- a/WindivertDotnet/WindivertDotnet.csproj
+++ b/WindivertDotnet/WindivertDotnet.csproj
@@ -1,7 +1,7 @@
- 1.1.0
+ 1.1.1
netcoreapp3.1;net6.0
enable
CA2012;IDE0079