[RouteHelper] Add WaitForUnicastIP

This commit is contained in:
Connection Refused
2021-11-10 14:09:38 +08:00
parent f68ed1abf0
commit d3814ca945
8 changed files with 98 additions and 22 deletions

15
RouteHelper/Based.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#ifndef BASED_H
#define BASED_H
#include <mutex>
#include <atomic>
#include <shared_mutex>
#include <condition_variable>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <netioapi.h>
#include <Windows.h>
#endif

View File

@@ -1,9 +1,9 @@
# RouteHelper # RouteHelper
```cpp ```cpp
ULONG ConvertLuidToIndex(ULONG64 id); ULONG ConvertLuidToIndex(ULONG64 id);
void WaitForUnicastIP();
BOOL CreateIPv4(const char* address, const char* netmask, ULONG index); BOOL CreateIPv4(const char* address, const char* netmask, ULONG index);
BOOL CreateUnicastIP(USHORT inet, const char* address, UINT8 cidr, ULONG index); BOOL CreateUnicastIP(USHORT inet, const char* address, UINT8 cidr, ULONG index);
BOOL RefreshIPTable(USHORT inet, ULONG index);
BOOL CreateRoute(USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, ULONG metric); BOOL CreateRoute(USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, ULONG metric);
BOOL DeleteRoute(USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, ULONG metric); BOOL DeleteRoute(USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, ULONG metric);
``` ```
@@ -12,15 +12,15 @@ BOOL DeleteRoute(USHORT inet, const char* address, UINT8 cidr, const char* gatew
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern uint ConvertLuidToIndex(ulong id); public static extern uint ConvertLuidToIndex(ulong id);
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern void WaitForUnicastIP();
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern bool CreateIPv4(string address, string netmask, uint index); public static extern bool CreateIPv4(string address, string netmask, uint index);
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern bool CreateUnicastIP(AddressFamily inet, string address, byte cidr, uint index); public static extern bool CreateUnicastIP(AddressFamily inet, string address, byte cidr, uint index);
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern bool RefreshIPTable(AddressFamily inet, uint index);
[DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)]
public static extern bool CreateRoute(AddressFamily inet, string address, byte cidr, string gateway, uint index, uint metric); public static extern bool CreateRoute(AddressFamily inet, string address, byte cidr, string gateway, uint index, uint metric);

View File

@@ -1,9 +1,5 @@
#include <WinSock2.h> #include "Based.h"
#include <WS2tcpip.h> #include "WaitGroup.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) BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{ {
@@ -14,6 +10,16 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
return TRUE; return TRUE;
} }
WaitGroup wg;
void UnicastIPChangeCallback(PVOID ctx, PMIB_UNICASTIPADDRESS_ROW row, MIB_NOTIFICATION_TYPE type)
{
UNREFERENCED_PARAMETER(ctx);
UNREFERENCED_PARAMETER(row);
UNREFERENCED_PARAMETER(type);
wg.Done();
}
bool make(PMIB_IPFORWARD_ROW2 rule, USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, ULONG metric) bool make(PMIB_IPFORWARD_ROW2 rule, USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, ULONG metric)
{ {
rule->InterfaceIndex = index; rule->InterfaceIndex = index;
@@ -76,6 +82,18 @@ extern "C" {
return index; return index;
} }
__declspec(dllexport) void __cdecl WaitForUnicastIP()
{
wg.Add(1);
HANDLE hCallback = NULL;
NotifyUnicastIpAddressChange(AF_INET, UnicastIPChangeCallback, NULL, FALSE, &hCallback);
wg.Wait();
CancelMibChangeNotify2(hCallback);
}
__declspec(dllexport) BOOL __cdecl CreateIPv4(const char* address, const char* netmask, ULONG index) __declspec(dllexport) BOOL __cdecl CreateIPv4(const char* address, const char* netmask, ULONG index)
{ {
ULONG addr = 0; ULONG addr = 0;
@@ -127,16 +145,6 @@ extern "C" {
return (NO_ERROR == CreateUnicastIpAddressEntry(&addr)) ? TRUE : 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, ULONG metric = 1) __declspec(dllexport) BOOL __cdecl CreateRoute(USHORT inet, const char* address, UINT8 cidr, const char* gateway, ULONG index, ULONG metric = 1)
{ {
MIB_IPFORWARD_ROW2 rule; MIB_IPFORWARD_ROW2 rule;

View File

@@ -93,6 +93,11 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="RouteHelper.cpp" /> <ClCompile Include="RouteHelper.cpp" />
<ClCompile Include="WaitGroup.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Based.h" />
<ClInclude Include="WaitGroup.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@@ -14,5 +14,16 @@
<ClCompile Include="RouteHelper.cpp"> <ClCompile Include="RouteHelper.cpp">
<Filter>Source</Filter> <Filter>Source</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="WaitGroup.cpp">
<Filter>Source</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="WaitGroup.h">
<Filter>Header</Filter>
</ClInclude>
<ClInclude Include="Based.h">
<Filter>Header</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

19
RouteHelper/WaitGroup.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include "WaitGroup.h"
void WaitGroup::Add(int size)
{
this->counter += size;
}
void WaitGroup::Done()
{
if (--this->counter <= 0)
this->condition.notify_all();
}
void WaitGroup::Wait()
{
std::unique_lock<std::mutex> lock(this->mutex);
condition.wait(lock, [&] { return counter <= 0; });
}

18
RouteHelper/WaitGroup.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#ifndef WAITGROUP_H
#define WAITGROUP_H
#include "Based.h"
class WaitGroup {
public:
void Add(int size);
void Done();
void Wait();
private:
std::mutex mutex;
std::atomic_int counter;
std::condition_variable condition;
};
#endif

View File

@@ -19,7 +19,7 @@ param (
[Parameter()] [Parameter()]
[bool] [bool]
$PublishReadyToRun = $True $PublishReadyToRun = $False
) )
Push-Location (Split-Path $MyInvocation.MyCommand.Path -Parent) Push-Location (Split-Path $MyInvocation.MyCommand.Path -Parent)
@@ -54,7 +54,7 @@ if ( -Not ( Test-Path ".\Netch\bin\$Configuration" ) ) {
-r 'win-x64' ` -r 'win-x64' `
-p:Platform='x64' ` -p:Platform='x64' `
-p:SelfContained=$SelfContained ` -p:SelfContained=$SelfContained `
-p:PublishTrimmed=$SelfContained ` -p:PublishTrimmed=$PublishReadyToRun `
-p:PublishSingleFile=$PublishSingleFile ` -p:PublishSingleFile=$PublishSingleFile `
-p:PublishReadyToRun=$PublishReadyToRun ` -p:PublishReadyToRun=$PublishReadyToRun `
-p:PublishReadyToRunShowWarnings=$PublishReadyToRun ` -p:PublishReadyToRunShowWarnings=$PublishReadyToRun `