mirror of
https://github.com/netchx/netch.git
synced 2026-03-14 17:43:18 +08:00
Add RouteHelper
This commit is contained in:
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Redirector", "Redirector\Redirector.vcxproj", "{1676DEF3-FBE4-47D2-93A6-8F85EA2F5B74}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RouteHelper", "RouteHelper\RouteHelper.vcxproj", "{7374F7F4-1732-4DED-A603-8335F6704F10}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
@@ -26,6 +28,10 @@ Global
|
||||
{1676DEF3-FBE4-47D2-93A6-8F85EA2F5B74}.Debug|x64.Build.0 = Debug|x64
|
||||
{1676DEF3-FBE4-47D2-93A6-8F85EA2F5B74}.Release|x64.ActiveCfg = Release|x64
|
||||
{1676DEF3-FBE4-47D2-93A6-8F85EA2F5B74}.Release|x64.Build.0 = Release|x64
|
||||
{7374F7F4-1732-4DED-A603-8335F6704F10}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{7374F7F4-1732-4DED-A603-8335F6704F10}.Debug|x64.Build.0 = Debug|x64
|
||||
{7374F7F4-1732-4DED-A603-8335F6704F10}.Release|x64.ActiveCfg = Release|x64
|
||||
{7374F7F4-1732-4DED-A603-8335F6704F10}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
3
RouteHelper/.gitignore
vendored
Normal file
3
RouteHelper/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/bin
|
||||
/obj
|
||||
/*.vcxproj.user
|
||||
158
RouteHelper/RouteHelper.cpp
Normal file
158
RouteHelper/RouteHelper.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
#define _WINSOCK_DEPRECATED_NO_WARNINGS
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
#include <ws2ipdef.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <netioapi.h>
|
||||
#include <Windows.h>
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(hModule);
|
||||
UNREFERENCED_PARAMETER(lpReserved);
|
||||
UNREFERENCED_PARAMETER(ul_reason_for_call);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL make(PMIB_IPFORWARD_ROW2 rule, USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, int metric)
|
||||
{
|
||||
rule->InterfaceIndex = index;
|
||||
rule->DestinationPrefix.PrefixLength = cidr;
|
||||
|
||||
if (AF_INET == inet)
|
||||
{
|
||||
rule->DestinationPrefix.Prefix.Ipv4.sin_family = inet;
|
||||
if (!inet_pton(inet, address, &rule->DestinationPrefix.Prefix.Ipv4.sin_addr))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (strlen(gateway))
|
||||
{
|
||||
rule->NextHop.Ipv4.sin_family = inet;
|
||||
if (!inet_pton(inet, gateway, &rule->NextHop.Ipv4.sin_addr))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (AF_INET6 == inet)
|
||||
{
|
||||
rule->DestinationPrefix.Prefix.Ipv6.sin6_family = inet;
|
||||
if (!inet_pton(inet, address, &rule->DestinationPrefix.Prefix.Ipv6.sin6_addr))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (strlen(gateway))
|
||||
{
|
||||
rule->NextHop.Ipv6.sin6_family = inet;
|
||||
if (!inet_pton(inet, gateway, &rule->NextHop.Ipv6.sin6_addr))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rule->ValidLifetime = 0xffffffff;
|
||||
rule->PreferredLifetime = 0xffffffff;
|
||||
rule->Metric = metric;
|
||||
rule->Protocol = MIB_IPPROTO_NETMGMT;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
__declspec(dllexport) ULONG __cdecl ConvertLuidToIndex(ULONG64 id)
|
||||
{
|
||||
NET_LUID luid;
|
||||
luid.Value = id;
|
||||
|
||||
NET_IFINDEX index = 0;
|
||||
if (NO_ERROR != ConvertInterfaceLuidToIndex(&luid, &index))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
__declspec(dllexport) BOOL __cdecl CreateIPv4(const char* address, const char* netmask, ULONG index)
|
||||
{
|
||||
ULONG addr = inet_addr(address);
|
||||
ULONG mask = inet_addr(netmask);
|
||||
ULONG ctx = 0;
|
||||
ULONG inst = 0;
|
||||
|
||||
return (NO_ERROR == AddIPAddress(addr, mask, index, &ctx, &inst)) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
__declspec(dllexport) BOOL __cdecl CreateUnicastIP(USHORT inet, const char* address, UINT8 cidr, ULONG index)
|
||||
{
|
||||
MIB_UNICASTIPADDRESS_ROW addr;
|
||||
InitializeUnicastIpAddressEntry(&addr);
|
||||
|
||||
addr.InterfaceIndex = index;
|
||||
addr.OnLinkPrefixLength = cidr;
|
||||
|
||||
if (AF_INET == inet)
|
||||
{
|
||||
addr.Address.Ipv4.sin_family = inet;
|
||||
if (!inet_pton(inet, address, &addr.Address.Ipv4.sin_addr))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else if (AF_INET6 == inet)
|
||||
{
|
||||
addr.Address.Ipv6.sin6_family = inet;
|
||||
if (!inet_pton(inet, address, &addr.Address.Ipv6.sin6_addr))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (NO_ERROR == CreateUnicastIpAddressEntry(&addr)) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
__declspec(dllexport) BOOL __cdecl RefreshIPTable(USHORT inet, ULONG index)
|
||||
{
|
||||
if (NO_ERROR != FlushIpPathTable(inet))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (NO_ERROR == FlushIpNetTable2(inet, index)) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
__declspec(dllexport) BOOL __cdecl CreateRoute(USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, int metric = 1)
|
||||
{
|
||||
MIB_IPFORWARD_ROW2 rule;
|
||||
InitializeIpForwardEntry(&rule);
|
||||
|
||||
if (!make(&rule, inet, address, cidr, gateway, index, metric))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (NO_ERROR == CreateIpForwardEntry2(&rule)) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
__declspec(dllexport) BOOL __cdecl DeleteRoute(USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, int metric = 1)
|
||||
{
|
||||
MIB_IPFORWARD_ROW2 rule;
|
||||
InitializeIpForwardEntry(&rule);
|
||||
|
||||
if (!make(&rule, inet, address, cidr, gateway, index, metric))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return (NO_ERROR == DeleteIpForwardEntry2(&rule)) ? TRUE : FALSE;
|
||||
}
|
||||
}
|
||||
96
RouteHelper/RouteHelper.vcxproj
Normal file
96
RouteHelper/RouteHelper.vcxproj
Normal file
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{7374f7f4-1732-4ded-a603-8335f6704f10}</ProjectGuid>
|
||||
<RootNamespace>RouteHelper</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<OutDir>$(ProjectDir)bin\</OutDir>
|
||||
<IntDir>$(ProjectDir)obj\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<OutDir>$(ProjectDir)bin\</OutDir>
|
||||
<IntDir>$(ProjectDir)obj\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>ws2_32.lib;iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>ws2_32.lib;iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="RouteHelper.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
18
RouteHelper/RouteHelper.vcxproj.filters
Normal file
18
RouteHelper/RouteHelper.vcxproj.filters
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Header">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Source">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="RouteHelper.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user