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

Commit

Permalink
Fixes, added unit tests, all working well now
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
TechnikEmpire committed Jul 26, 2018
1 parent 290ea07 commit 846a09a
Show file tree
Hide file tree
Showing 19 changed files with 2,298 additions and 271 deletions.
14 changes: 13 additions & 1 deletion WinDivertSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2042
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinDivertSharp", "WinDivertSharp\WinDivertSharp.csproj", "{9B55D8CE-0B1C-4754-909D-140C4584FC1F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinDivertSharp", "WinDivertSharp\WinDivertSharp.csproj", "{9B55D8CE-0B1C-4754-909D-140C4584FC1F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinDivertSharpTests", "WinDivertSharpTests\WinDivertSharpTests.csproj", "{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinDivertTestRunner", "WinDivertTestRunner\WinDivertTestRunner.csproj", "{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +19,14 @@ Global
{9B55D8CE-0B1C-4754-909D-140C4584FC1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B55D8CE-0B1C-4754-909D-140C4584FC1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B55D8CE-0B1C-4754-909D-140C4584FC1F}.Release|Any CPU.Build.0 = Release|Any CPU
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EF2D21F-2B16-469E-A56D-716B7F2FFD54}.Release|Any CPU.Build.0 = Release|Any CPU
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09395949-3ED9-4ED0-B5AD-3D29054DBFB8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
109 changes: 9 additions & 100 deletions WinDivertSharp/Extensions/IntegralTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,110 +1,19 @@
/*
* Copyright © 2018-Present Jesse Nicholson
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

using System.Runtime.CompilerServices;
using System;
using System.Collections.Generic;
using System.Text;

namespace WinDivertSharp.Extensions
{
/// <summary>
/// Some handy-dandy extensions for integral types.
/// </summary>
public static class IntegralTypeExtensions
internal static class IntegralTypeExtensions
{
/// <summary>
/// Swaps the endianness of the short.
/// </summary>
/// <param name="val">
/// The current value.
/// </param>
/// <returns>
/// The short value in reverse-order from its current state.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short SwapByteOrder(this short val)
{
return (short)(((ushort)val).SwapByteOrder());
}

/// <summary>
/// Swaps the endianness of the ushort.
/// </summary>
/// <param name="val">
/// The current value.
/// </param>
/// <returns>
/// The ushort value in reverse-order from its current state.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort SwapByteOrder(this ushort val)
{
return (ushort)(((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8));
}

/// <summary>
/// Swaps the endianness of the integer.
/// </summary>
/// <param name="val">
/// The current value.
/// </param>
/// <returns>
/// The integer value in reverse-order from its current state.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SwapByteOrder(this int val)
{
return (int)(((uint)val).SwapByteOrder());
}

/// <summary>
/// Swaps the endianness of the unsigned integer.
/// </summary>
/// <param name="val">
/// The current value.
/// </param>
/// <returns>
/// The unsigned integer value in reverse-order from its current state.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint SwapByteOrder(this uint val)
{
val = (val >> 16) | (val << 16);
return ((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8);
}

/// <summary>
/// Swaps the endianness of the long integer.
/// </summary>
/// <param name="val">
/// The current value.
/// </param>
/// <returns>
/// The long integer value in reverse-order from its current state.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long SwapByteOrder(this long val)
public static byte GetBit(this byte @byte, int index)
{
return (long)(((ulong)val).SwapByteOrder());
return (byte)(@byte & (1 << index - 1));
}

/// <summary>
/// Swaps the endianness of the unsigned long integer.
/// </summary>
/// <param name="val">
/// The current value.
/// </param>
/// <returns>
/// The unsigned long integer value in reverse-order from its current state.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong SwapByteOrder(this ulong val)
public static byte SetBit(this byte @byte, int index)
{
val = (val >> 32) | (val << 32);
val = ((val & 0xFFFF0000FFFF0000) >> 16) | ((val & 0x0000FFFF0000FFFF) << 16);
return ((val & 0xFF00FF00FF00FF00) >> 8) | ((val & 0x00FF00FF00FF00FF) << 8);
return (byte)(@byte & (1 << index - 1));
}
}
}
}
89 changes: 41 additions & 48 deletions WinDivertSharp/IPv6Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace WinDivertSharp
/// Represents an IPV6 header.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe struct IPv6Header
public struct IPv6Header
{
/// TrafficClass0 : 4
/// Version : 4
Expand Down Expand Up @@ -75,8 +75,10 @@ public unsafe struct IPv6Header
/// <summary>
/// Private member for <see cref="SrcAddr"/>
/// </summary>
//[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U4)]
private fixed uint _srcAddr[4];
private uint _srcAddrA;
private uint _srcAddrB;
private uint _srcAddrC;
private uint _srcAddrD;

/// <summary>
/// Gets or sets the source IP address.
Expand All @@ -88,49 +90,46 @@ public IPAddress SrcAddr
{
get
{
fixed (uint* addr = this._srcAddr)
{
var b1 = BitConverter.GetBytes(addr[0]);
var b2 = BitConverter.GetBytes(addr[1]);
var b3 = BitConverter.GetBytes(addr[2]);
var b4 = BitConverter.GetBytes(addr[3]);
var bytes = new byte[] {
var b1 = BitConverter.GetBytes(_srcAddrA);
var b2 = BitConverter.GetBytes(_srcAddrB);
var b3 = BitConverter.GetBytes(_srcAddrC);
var b4 = BitConverter.GetBytes(_srcAddrD);
var bytes = new byte[] {
b1[0], b1[1], b1[2], b1[3],
b2[0], b2[1], b2[2], b2[3],
b3[0], b3[1], b3[2], b3[3],
b4[0], b4[1], b4[2], b4[3]
};

return new IPAddress(bytes);
}
return new IPAddress(bytes);
}

set
{
fixed (uint* addr = this._srcAddr)
{
var valueBytes = value.GetAddressBytes();
var valueBytes = value.GetAddressBytes();

Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");

if (valueBytes.Length != 16)
{
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
}

addr[0] = BitConverter.ToUInt32(valueBytes, 0);
addr[1] = BitConverter.ToUInt32(valueBytes, 4);
addr[2] = BitConverter.ToUInt32(valueBytes, 8);
addr[3] = BitConverter.ToUInt32(valueBytes, 12);
if (valueBytes.Length != 16)
{
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
}

_srcAddrA = BitConverter.ToUInt32(valueBytes, 0);
_srcAddrB = BitConverter.ToUInt32(valueBytes, 4);
_srcAddrC = BitConverter.ToUInt32(valueBytes, 8);
_srcAddrD = BitConverter.ToUInt32(valueBytes, 12);
}
}

/// <summary>
/// Private member for <see cref="DstAddr"/>
/// </summary>
//[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U4)]
private fixed uint _dstAddr[4];
private uint _dstAddrA;
private uint _dstAddrB;
private uint _dstAddrC;
private uint _dstAddrD;

/// <summary>
/// Gets or sets the destination IP address.
Expand All @@ -142,41 +141,35 @@ public IPAddress DstAddr
{
get
{
fixed (uint* addr = this._dstAddr)
{
var b1 = BitConverter.GetBytes(addr[0]);
var b2 = BitConverter.GetBytes(addr[1]);
var b3 = BitConverter.GetBytes(addr[2]);
var b4 = BitConverter.GetBytes(addr[3]);
var bytes = new byte[] {
var b1 = BitConverter.GetBytes(_dstAddrA);
var b2 = BitConverter.GetBytes(_dstAddrB);
var b3 = BitConverter.GetBytes(_dstAddrC);
var b4 = BitConverter.GetBytes(_dstAddrD);
var bytes = new byte[] {
b1[0], b1[1], b1[2], b1[3],
b2[0], b2[1], b2[2], b2[3],
b3[0], b3[1], b3[2], b3[3],
b4[0], b4[1], b4[2], b4[3]
};

return new IPAddress(bytes);
}
return new IPAddress(bytes);
}

set
{
fixed (uint* addr = this._dstAddr)
{
var valueBytes = value.GetAddressBytes();
var valueBytes = value.GetAddressBytes();

Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");
Debug.Assert(valueBytes.Length == 16, "Not a valid IPV6 address.");

if (valueBytes.Length != 16)
{
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
}

addr[0] = BitConverter.ToUInt32(valueBytes, 0);
addr[1] = BitConverter.ToUInt32(valueBytes, 4);
addr[2] = BitConverter.ToUInt32(valueBytes, 8);
addr[3] = BitConverter.ToUInt32(valueBytes, 12);
if (valueBytes.Length != 16)
{
throw new ArgumentException("Not a valid IPV6 address.", nameof(SrcAddr));
}

_dstAddrA = BitConverter.ToUInt32(valueBytes, 0);
_dstAddrB = BitConverter.ToUInt32(valueBytes, 4);
_dstAddrC = BitConverter.ToUInt32(valueBytes, 8);
_dstAddrD = BitConverter.ToUInt32(valueBytes, 12);
}
}

Expand Down
Loading

0 comments on commit 846a09a

Please sign in to comment.