Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wiresock committed Jun 11, 2019
0 parents commit f440df7
Show file tree
Hide file tree
Showing 75 changed files with 6,077 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vs/
bin/
obj/
packages/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Vadim Smirnov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
37 changes: 37 additions & 0 deletions NdisApi.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2043
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NdisApiDotNet", "NdisApiDotNet\NdisApiDotNet.csproj", "{40070452-8964-4B60-8986-0335B8DD4866}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NdisApiDotNetPacketDotNet", "NdisApiDotNetPacketDotNet\NdisApiDotNetPacketDotNet.csproj", "{6DE8F145-043C-40B9-800B-7E49D3F3D5BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NdisApiDemo", "NdisApiDemo\NdisApiDemo.csproj", "{4BC70C52-468D-463E-B8CC-E72D6F28C690}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{40070452-8964-4B60-8986-0335B8DD4866}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40070452-8964-4B60-8986-0335B8DD4866}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40070452-8964-4B60-8986-0335B8DD4866}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40070452-8964-4B60-8986-0335B8DD4866}.Release|Any CPU.Build.0 = Release|Any CPU
{6DE8F145-043C-40B9-800B-7E49D3F3D5BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DE8F145-043C-40B9-800B-7E49D3F3D5BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DE8F145-043C-40B9-800B-7E49D3F3D5BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DE8F145-043C-40B9-800B-7E49D3F3D5BF}.Release|Any CPU.Build.0 = Release|Any CPU
{4BC70C52-468D-463E-B8CC-E72D6F28C690}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BC70C52-468D-463E-B8CC-E72D6F28C690}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BC70C52-468D-463E-B8CC-E72D6F28C690}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BC70C52-468D-463E-B8CC-E72D6F28C690}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {960C2F87-D1B4-40BE-9913-C0042116B2B5}
EndGlobalSection
EndGlobal
11 changes: 11 additions & 0 deletions NdisApi.sln.licenseheader
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extensions: designer.cs generated.cs
extensions: .cs .cpp .h
// ----------------------------------------------
// <copyright file="%filename%" company="NT Kernel">
// Copyright (c) 2000-2018 NT Kernel Resources / Contributors
// All Rights Reserved.
// http://www.ntkernel.com
// ndisrd@ntkernel.com
// </copyright>
// ----------------------------------------------

6 changes: 6 additions & 0 deletions NdisApiDemo/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>
70 changes: 70 additions & 0 deletions NdisApiDemo/NdisApiDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4BC70C52-468D-463E-B8CC-E72D6F28C690}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>NdisApiDemo</RootNamespace>
<AssemblyName>NdisApiDemo</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="PacketDotNet, Version=0.20.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\PacketDotNet.0.20.1\lib\net45\PacketDotNet.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NdisApiDotNetPacketDotNet\NdisApiDotNetPacketDotNet.csproj">
<Project>{6de8f145-043c-40b9-800b-7e49d3f3d5bf}</Project>
<Name>NdisApiDotNetPacketDotNet</Name>
</ProjectReference>
<ProjectReference Include="..\NdisApiDotNet\NdisApiDotNet.csproj">
<Project>{40070452-8964-4b60-8986-0335b8dd4866}</Project>
<Name>NdisApiDotNet</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
109 changes: 109 additions & 0 deletions NdisApiDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// ----------------------------------------------
// <copyright file="Program.cs" company="NT Kernel">
// Copyright (c) 2000-2018 NT Kernel Resources / Contributors
// All Rights Reserved.
// http://www.ntkernel.com
// ndisrd@ntkernel.com
// </copyright>
// ----------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NdisApiDotNet;
using NdisApiDotNetPacketDotNet.Extensions;
using PacketDotNet;

namespace NdisApiDemo
{
class Program
{
private static void Main()
{
var filter = NdisApi.Open();
if (!filter.IsValid)
throw new ApplicationException("Cannot load driver.");


Console.WriteLine($"Loaded driver: {filter.GetVersion()}.");

// Create and set event for the adapters.
var waitHandlesCollection = new List<ManualResetEvent>();
var tcpAdapters = new List<NetworkAdapter>();
foreach (var networkAdapter in filter.GetNetworkAdapters())
{
if (networkAdapter.IsValid)
{
var success = filter.SetAdapterMode(networkAdapter,
NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_TUNNEL |
NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_LOOPBACK_FILTER |
NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_LOOPBACK_BLOCK);

var manualResetEvent = new ManualResetEvent(false);

success &= filter.SetPacketEvent(networkAdapter, manualResetEvent.SafeWaitHandle);

if (success)
{
Console.WriteLine($"Added {networkAdapter.FriendlyName}.");

waitHandlesCollection.Add(manualResetEvent);
tcpAdapters.Add(networkAdapter);
}
}
}

var waitHandlesManualResetEvents = waitHandlesCollection.Cast<ManualResetEvent>().ToArray();
var waitHandles = waitHandlesCollection.Cast<WaitHandle>().ToArray();

var t1 = Task.Factory.StartNew(() => PassThruThread(filter, waitHandles, tcpAdapters.ToArray(), waitHandlesManualResetEvents));
Task.WaitAll(t1);

Console.Read();
}

/// <summary>
/// Starts a pass thru thread.
/// </summary>
/// <param name="filter">The filter.</param>
/// <param name="waitHandles">The wait handles.</param>
/// <param name="networkAdapters">The network adapters.</param>
/// <param name="waitHandlesManualResetEvents">The wait handles manual reset events.</param>
private static void PassThruThread(NdisApi filter, WaitHandle[] waitHandles, IReadOnlyList<NetworkAdapter> networkAdapters, IReadOnlyList<ManualResetEvent> waitHandlesManualResetEvents)
{
var ndisApiHelper = new NdisApiHelper();

var ethPackets = ndisApiHelper.CreateEthMRequest();

while (true)
{
var handle = WaitHandle.WaitAny(waitHandles);
ethPackets.AdapterHandle = networkAdapters[handle].Handle;

while (filter.ReadPackets(ref ethPackets))
{
var packets = ethPackets.Packets;
for (int i = 0; i < ethPackets.PacketsCount; i++)
{
var ethPacket = packets[i].GetEthernetPacket(ndisApiHelper);
if (ethPacket.PayloadPacket is IPv4Packet iPv4Packet)
{
if (iPv4Packet.PayloadPacket is TcpPacket tcpPacket)
{
Console.WriteLine($"{iPv4Packet.SourceAddress}:{tcpPacket.SourcePort} -> {iPv4Packet.DestinationAddress}:{tcpPacket.DestinationPort}.");
}
}
}

filter.SendPackets(ref ethPackets);
ethPackets.PacketsCount = 0;
}

waitHandlesManualResetEvents[handle].Reset();
}
}
}
}
46 changes: 46 additions & 0 deletions NdisApiDemo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------
// <copyright file="AssemblyInfo.cs" company="NT Kernel">
// Copyright (c) 2000-2018 NT Kernel Resources / Contributors
// All Rights Reserved.
// http://www.ntkernel.com
// ndisrd@ntkernel.com
// </copyright>
// ----------------------------------------------


using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NdisApiDemo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NdisApiDemo")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4bc70c52-468d-463e-b8cc-e72d6f28c690")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions NdisApiDemo/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="PacketDotNet" version="0.20.1" targetFramework="net462" />
</packages>
12 changes: 12 additions & 0 deletions NdisApiDotNet/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ----------------------------------------------
// <copyright file="AssemblyInfo.cs" company="NT Kernel">
// Copyright (c) 2000-2018 NT Kernel Resources / Contributors
// All Rights Reserved.
// http://www.ntkernel.com
// ndisrd@ntkernel.com
// </copyright>
// ----------------------------------------------


using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("NdisApiDotNetPacketDotNet")]
Loading

0 comments on commit f440df7

Please sign in to comment.