Skip to content
This repository was archived by the owner on Dec 24, 2023. It is now read-only.

Commit 846a09a

Browse files
committed
Fixes, added unit tests, all working well now
Minor API change, but we're keeping our version in sync with the target native version so #yolo. Fixes #1 Fixes #2 Converted all WinDivert tests to individual unit tests, all passing now. Added additional tests to make sure checksum calculation matched the calculation of an external library (not ideal not coding it ourselves, but if two libs can agree then that's OK for me).
1 parent 290ea07 commit 846a09a

19 files changed

+2298
-271
lines changed

WinDivertSharp.sln

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.27703.2042
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinDivertSharp", "WinDivertSharp\WinDivertSharp.csproj", "{9B55D8CE-0B1C-4754-909D-140C4584FC1F}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinDivertSharp", "WinDivertSharp\WinDivertSharp.csproj", "{9B55D8CE-0B1C-4754-909D-140C4584FC1F}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinDivertSharpTests", "WinDivertSharpTests\WinDivertSharpTests.csproj", "{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinDivertTestRunner", "WinDivertTestRunner\WinDivertTestRunner.csproj", "{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}"
711
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +19,14 @@ Global
1519
{9B55D8CE-0B1C-4754-909D-140C4584FC1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
1620
{9B55D8CE-0B1C-4754-909D-140C4584FC1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
1721
{9B55D8CE-0B1C-4754-909D-140C4584FC1F}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Release|Any CPU.Build.0 = Release|Any CPU
1830
EndGlobalSection
1931
GlobalSection(SolutionProperties) = preSolution
2032
HideSolutionNode = FALSE
Lines changed: 9 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,19 @@
1-
/*
2-
* Copyright © 2018-Present Jesse Nicholson
3-
* This Source Code Form is subject to the terms of the Mozilla Public
4-
* License, v. 2.0. If a copy of the MPL was not distributed with this
5-
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6-
*/
7-
8-
using System.Runtime.CompilerServices;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
94

105
namespace WinDivertSharp.Extensions
116
{
12-
/// <summary>
13-
/// Some handy-dandy extensions for integral types.
14-
/// </summary>
15-
public static class IntegralTypeExtensions
7+
internal static class IntegralTypeExtensions
168
{
17-
/// <summary>
18-
/// Swaps the endianness of the short.
19-
/// </summary>
20-
/// <param name="val">
21-
/// The current value.
22-
/// </param>
23-
/// <returns>
24-
/// The short value in reverse-order from its current state.
25-
/// </returns>
26-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27-
public static short SwapByteOrder(this short val)
28-
{
29-
return (short)(((ushort)val).SwapByteOrder());
30-
}
31-
32-
/// <summary>
33-
/// Swaps the endianness of the ushort.
34-
/// </summary>
35-
/// <param name="val">
36-
/// The current value.
37-
/// </param>
38-
/// <returns>
39-
/// The ushort value in reverse-order from its current state.
40-
/// </returns>
41-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
42-
public static ushort SwapByteOrder(this ushort val)
43-
{
44-
return (ushort)(((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8));
45-
}
46-
47-
/// <summary>
48-
/// Swaps the endianness of the integer.
49-
/// </summary>
50-
/// <param name="val">
51-
/// The current value.
52-
/// </param>
53-
/// <returns>
54-
/// The integer value in reverse-order from its current state.
55-
/// </returns>
56-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
57-
public static int SwapByteOrder(this int val)
58-
{
59-
return (int)(((uint)val).SwapByteOrder());
60-
}
61-
62-
/// <summary>
63-
/// Swaps the endianness of the unsigned integer.
64-
/// </summary>
65-
/// <param name="val">
66-
/// The current value.
67-
/// </param>
68-
/// <returns>
69-
/// The unsigned integer value in reverse-order from its current state.
70-
/// </returns>
71-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
72-
public static uint SwapByteOrder(this uint val)
73-
{
74-
val = (val >> 16) | (val << 16);
75-
return ((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8);
76-
}
77-
78-
/// <summary>
79-
/// Swaps the endianness of the long integer.
80-
/// </summary>
81-
/// <param name="val">
82-
/// The current value.
83-
/// </param>
84-
/// <returns>
85-
/// The long integer value in reverse-order from its current state.
86-
/// </returns>
87-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
88-
public static long SwapByteOrder(this long val)
9+
public static byte GetBit(this byte @byte, int index)
8910
{
90-
return (long)(((ulong)val).SwapByteOrder());
11+
return (byte)(@byte & (1 << index - 1));
9112
}
9213

93-
/// <summary>
94-
/// Swaps the endianness of the unsigned long integer.
95-
/// </summary>
96-
/// <param name="val">
97-
/// The current value.
98-
/// </param>
99-
/// <returns>
100-
/// The unsigned long integer value in reverse-order from its current state.
101-
/// </returns>
102-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
103-
public static ulong SwapByteOrder(this ulong val)
14+
public static byte SetBit(this byte @byte, int index)
10415
{
105-
val = (val >> 32) | (val << 32);
106-
val = ((val & 0xFFFF0000FFFF0000) >> 16) | ((val & 0x0000FFFF0000FFFF) << 16);
107-
return ((val & 0xFF00FF00FF00FF00) >> 8) | ((val & 0x00FF00FF00FF00FF) << 8);
16+
return (byte)(@byte & (1 << index - 1));
10817
}
10918
}
110-
}
19+
}

WinDivertSharp/IPv6Header.cs

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace WinDivertSharp
4444
/// Represents an IPV6 header.
4545
/// </summary>
4646
[StructLayout(LayoutKind.Sequential)]
47-
public unsafe struct IPv6Header
47+
public struct IPv6Header
4848
{
4949
/// TrafficClass0 : 4
5050
/// Version : 4
@@ -75,8 +75,10 @@ public unsafe struct IPv6Header
7575
/// <summary>
7676
/// Private member for <see cref="SrcAddr"/>
7777
/// </summary>
78-
//[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U4)]
79-
private fixed uint _srcAddr[4];
78+
private uint _srcAddrA;
79+
private uint _srcAddrB;
80+
private uint _srcAddrC;
81+
private uint _srcAddrD;
8082

8183
/// <summary>
8284
/// Gets or sets the source IP address.
@@ -88,49 +90,46 @@ public IPAddress SrcAddr
8890
{
8991
get
9092
{
91-
fixed (uint* addr = this._srcAddr)
92-
{
93-
var b1 = BitConverter.GetBytes(addr[0]);
94-
var b2 = BitConverter.GetBytes(addr[1]);
95-
var b3 = BitConverter.GetBytes(addr[2]);
96-
var b4 = BitConverter.GetBytes(addr[3]);
97-
var bytes = new byte[] {
93+
var b1 = BitConverter.GetBytes(_srcAddrA);
94+
var b2 = BitConverter.GetBytes(_srcAddrB);
95+
var b3 = BitConverter.GetBytes(_srcAddrC);
96+
var b4 = BitConverter.GetBytes(_srcAddrD);
97+
var bytes = new byte[] {
9898
b1[0], b1[1], b1[2], b1[3],
9999
b2[0], b2[1], b2[2], b2[3],
100100
b3[0], b3[1], b3[2], b3[3],
101101
b4[0], b4[1], b4[2], b4[3]
102102
};
103103

104-
return new IPAddress(bytes);
105-
}
104+
return new IPAddress(bytes);
106105
}
107106

108107
set
109108
{
110-
fixed (uint* addr = this._srcAddr)
111-
{
112-
var valueBytes = value.GetAddressBytes();
109+
var valueBytes = value.GetAddressBytes();
113110

114-
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
111+
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
115112

116-
if (valueBytes.Length != 16)
117-
{
118-
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
119-
}
120-
121-
addr[0] = BitConverter.ToUInt32(valueBytes, 0);
122-
addr[1] = BitConverter.ToUInt32(valueBytes, 4);
123-
addr[2] = BitConverter.ToUInt32(valueBytes, 8);
124-
addr[3] = BitConverter.ToUInt32(valueBytes, 12);
113+
if (valueBytes.Length != 16)
114+
{
115+
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
125116
}
117+
118+
_srcAddrA = BitConverter.ToUInt32(valueBytes, 0);
119+
_srcAddrB = BitConverter.ToUInt32(valueBytes, 4);
120+
_srcAddrC = BitConverter.ToUInt32(valueBytes, 8);
121+
_srcAddrD = BitConverter.ToUInt32(valueBytes, 12);
126122
}
127123
}
128124

129125
/// <summary>
130126
/// Private member for <see cref="DstAddr"/>
131127
/// </summary>
132128
//[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U4)]
133-
private fixed uint _dstAddr[4];
129+
private uint _dstAddrA;
130+
private uint _dstAddrB;
131+
private uint _dstAddrC;
132+
private uint _dstAddrD;
134133

135134
/// <summary>
136135
/// Gets or sets the destination IP address.
@@ -142,41 +141,35 @@ public IPAddress DstAddr
142141
{
143142
get
144143
{
145-
fixed (uint* addr = this._dstAddr)
146-
{
147-
var b1 = BitConverter.GetBytes(addr[0]);
148-
var b2 = BitConverter.GetBytes(addr[1]);
149-
var b3 = BitConverter.GetBytes(addr[2]);
150-
var b4 = BitConverter.GetBytes(addr[3]);
151-
var bytes = new byte[] {
144+
var b1 = BitConverter.GetBytes(_dstAddrA);
145+
var b2 = BitConverter.GetBytes(_dstAddrB);
146+
var b3 = BitConverter.GetBytes(_dstAddrC);
147+
var b4 = BitConverter.GetBytes(_dstAddrD);
148+
var bytes = new byte[] {
152149
b1[0], b1[1], b1[2], b1[3],
153150
b2[0], b2[1], b2[2], b2[3],
154151
b3[0], b3[1], b3[2], b3[3],
155152
b4[0], b4[1], b4[2], b4[3]
156153
};
157154

158-
return new IPAddress(bytes);
159-
}
155+
return new IPAddress(bytes);
160156
}
161157

162158
set
163159
{
164-
fixed (uint* addr = this._dstAddr)
165-
{
166-
var valueBytes = value.GetAddressBytes();
160+
var valueBytes = value.GetAddressBytes();
167161

168-
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
162+
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
169163

170-
if (valueBytes.Length != 16)
171-
{
172-
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
173-
}
174-
175-
addr[0] = BitConverter.ToUInt32(valueBytes, 0);
176-
addr[1] = BitConverter.ToUInt32(valueBytes, 4);
177-
addr[2] = BitConverter.ToUInt32(valueBytes, 8);
178-
addr[3] = BitConverter.ToUInt32(valueBytes, 12);
164+
if (valueBytes.Length != 16)
165+
{
166+
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
179167
}
168+
169+
_dstAddrA = BitConverter.ToUInt32(valueBytes, 0);
170+
_dstAddrB = BitConverter.ToUInt32(valueBytes, 4);
171+
_dstAddrC = BitConverter.ToUInt32(valueBytes, 8);
172+
_dstAddrD = BitConverter.ToUInt32(valueBytes, 12);
180173
}
181174
}
182175

0 commit comments

Comments
 (0)