SharpDivert 1.1.0

dotnet add package SharpDivert --version 1.1.0
                    
NuGet\Install-Package SharpDivert -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SharpDivert" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpDivert" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SharpDivert" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SharpDivert --version 1.1.0
                    
#r "nuget: SharpDivert, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SharpDivert@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SharpDivert&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SharpDivert&version=1.1.0
                    
Install as a Cake Tool

SharpDivert

.NET bindings for WinDivert.

Getting Started

  1. Install SharpDivert from NuGet.
  2. Download the WinDivert v2.2 binary from the homepage and put it in the program directory.
  3. Write your code. WinDivert Reference Manual may help you.

The following sample code rewrites the TCP port for outgoing packets from 1234 to 80, and the TCP port for incoming packets from 80 to 1234. Run this code and try opening http://example.com:1234/ in your browser.

using System;
using SharpDivert;

using var divert = new WinDivert("(outbound and tcp.DstPort == 1234) or (inbound and tcp.SrcPort == 80)", WinDivert.Layer.Network, 0, 0);
var recvBuf = new Memory<byte>(new byte[WinDivert.MTUMax]);
var addrBuf = new Memory<WinDivertAddress>(new WinDivertAddress[1]);

while (true)
{
    var (recvLen, addrLen) = divert.RecvEx(recvBuf.Span, addrBuf.Span);
    var recv = recvBuf[..(int)recvLen];
    var addr = addrBuf[..(int)addrLen];
    foreach (var (i, p) in new WinDivertIndexedPacketParser(recv))
    {
        unsafe
        {
            if (addr.Span[i].Outbound)
            {
                Console.WriteLine(">");
                p.TCPHdr->DstPort = 80;
            }
            else
            {
                Console.WriteLine("<");
                p.TCPHdr->SrcPort = 1234;
            }
        }
        WinDivert.CalcChecksums(p.Packet.Span, ref addr.Span[i], 0);
    }
    _ = divert.SendEx(recv.Span, addr.Span);
}

Note that WinDivert must be run with administrative privileges. requestedExecutionLevel in app.manifest may be useful.

Security Warning

Be careful not to execute any malicious code, as applications containing WinDivert are run with administrative privileges. Program files, such as WinDivert.dll, should not be rewritten without administrative privileges. Make sure that the DLL search path is set properly to avoid loading invalid WinDivert.dll.

Remarks

SharpDivert is only used in a limited number of applications and has not been fully tested. Some features are missing. Issues and pull requests are welcome.

License

SharpDivert is dual-licensed under your choice of the GNU Lesser General Public License (LGPL) Version 3 or the GNU General Public License (GPL) Version 2. See the LICENSE file for more information.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 719 12/9/2024
1.1.0-beta.2 105 12/8/2024
1.1.0-beta.1 289 3/20/2022
1.0.1 817 10/24/2021
1.0.0 506 10/24/2021

Changes since v1.0.1

- Added support for .NET 8.0
- Added `WinDivert.FormatFilter` method.
- The following constants were added.
 - `QueueLengthDefault`, `QueueLengthMin`, `QueueLengthMax`
 - `QueueTimeDefault`, `QueueTimeMin`, `QueueTimeMax`
 - `QueueSizeDefault`, `QueueSizeMin`, `QueueSizeMax`
 - `PriorityHighest`, `PriorityLowest`
 - `BatchMax`
 - `MTUMax`
- SharpDivert now throws `WinDivertException` instead of `Win32Exception`. Users can now use the `WinDivertNativeMethod` field of `WinDivertException` to more accurately determine the source of the error.
- Some exceptions now output appropriate error messages.
- Minor fixes to `GetHashCode` for some types.
- Updated documentation, etc.