2023-06-19 11:08:42 +00:00
|
|
|
|
#include "pch.h"
|
|
|
|
|
#include "tunnel.h"
|
|
|
|
|
#include "usrerr.h"
|
|
|
|
|
#include <strsafe.h>
|
|
|
|
|
#include <tchar.h>
|
|
|
|
|
#include <Iphlpapi.h>
|
|
|
|
|
#include <iostream>
|
2023-07-10 08:36:19 +00:00
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
#include <netlistmgr.h>
|
2023-07-17 10:55:01 +00:00
|
|
|
|
#include <timeapi.h>
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
|
|
|
|
#include "globalcfg.h"
|
2023-06-21 10:04:16 +00:00
|
|
|
|
#include "misc.h"
|
2023-07-10 08:36:19 +00:00
|
|
|
|
#include "network.h"
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
#include <netcon.h>
|
2023-06-25 07:57:47 +00:00
|
|
|
|
#include <shlwapi.h>
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
#pragma comment(lib, "Iphlpapi.lib")
|
2023-07-10 08:36:19 +00:00
|
|
|
|
#pragma comment(lib, "Ws2_32.lib")
|
|
|
|
|
|
|
|
|
|
static NIC_CONTENT g_NetAdapterInfo[NET_CARD_MAX];
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
static const TCHAR *g_TabCIDR[] = {
|
|
|
|
|
TEXT("128.0.0.0"), TEXT("192.0.0.0"), TEXT("224.0.0.0"), TEXT("240.0.0.0"),
|
|
|
|
|
TEXT("248.0.0.0"), TEXT("252.0.0.0"), TEXT("254.0.0.0"), TEXT("255.0.0.0"),
|
|
|
|
|
TEXT("255.128.0.0"), TEXT("255.192.0.0"), TEXT("255.224.0.0"), TEXT("255.240.0.0"),
|
|
|
|
|
TEXT("255.248.0.0"), TEXT("255.252.0.0"), TEXT("255.254.0.0"), TEXT("255.255.0.0"),
|
|
|
|
|
TEXT("255.255.128.0"), TEXT("255.255.192.0"), TEXT("255.255.224.0"), TEXT("255.255.240.0"),
|
|
|
|
|
TEXT("255.255.248.0"), TEXT("255.255.252.0"), TEXT("255.255.254.0"), TEXT("255.255.255.0"),
|
|
|
|
|
TEXT("255.255.255.128"), TEXT("255.255.255.192"), TEXT("255.255.255.224"), TEXT("255.255.255.240"),
|
|
|
|
|
TEXT("255.255.255.248"), TEXT("255.255.255.252"), TEXT("255.255.255.254"), TEXT("255.255.255.255")};
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
int NetmaskToCIDR(const TCHAR *pNetMask) {
|
2023-06-25 07:57:47 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < static_cast<int>(std::size(g_TabCIDR)); i++) {
|
|
|
|
|
if (lstrcmp(g_TabCIDR[i], pNetMask) == 0) {
|
2023-06-20 09:25:14 +00:00
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0xFF;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 10:04:16 +00:00
|
|
|
|
const TCHAR *CIDRToNetmask(const UINT8 cidr) {
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
if (cidr >= 1 && cidr <= std::size(g_TabCIDR)) {
|
|
|
|
|
return g_TabCIDR[cidr - 1];
|
2023-06-21 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TEXT("");
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
int GetInterfaceIfIndexByGUID(const TCHAR *pGUID, int *pIfIndex) {
|
|
|
|
|
PIP_ADAPTER_INFO pAdapterInfo;
|
|
|
|
|
DWORD dwRetVal;
|
|
|
|
|
|
|
|
|
|
if (pGUID == nullptr || lstrlen(pGUID) == 0) {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Input pGUID error: {0}"), pGUID);
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pIfIndex == nullptr) {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Input pIfIndex params error"));
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
|
|
|
|
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO)));
|
|
|
|
|
|
|
|
|
|
if (pAdapterInfo == nullptr) {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo"));
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
|
|
|
|
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, ulOutBufLen));
|
|
|
|
|
if (pAdapterInfo == nullptr) {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo\n"));
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*pIfIndex = -1;
|
|
|
|
|
|
|
|
|
|
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
|
|
|
|
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
|
|
|
|
|
while (pAdapter) {
|
|
|
|
|
|
|
|
|
|
if (StrCmp(pAdapter->AdapterName, pGUID) == 0) {
|
|
|
|
|
*pIfIndex = static_cast<int>(pAdapter->Index);
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
pAdapter = pAdapter->Next;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("GetAdaptersInfo failed with error: {0}\n"), dwRetVal);
|
2023-07-10 08:36:19 +00:00
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pAdapterInfo) {
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetInterfaceNameByGUID(const TCHAR *pGUID, TCHAR ifName[MAX_NETCARD_NAME]) {
|
|
|
|
|
VARIANT v;
|
|
|
|
|
INetConnection *pNC = nullptr;
|
|
|
|
|
IEnumVARIANT *pEV = nullptr;
|
|
|
|
|
IUnknown *pUnk = nullptr;
|
|
|
|
|
INetSharingEveryConnectionCollection *pNSECC = nullptr;
|
|
|
|
|
INetSharingManager *pNSM;
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
|
|
if (pGUID == nullptr || lstrlen(pGUID) == 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pGUID params error: {0}"), pGUID);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_NetSharingManager,
|
|
|
|
|
nullptr,
|
|
|
|
|
CLSCTX_ALL,
|
|
|
|
|
IID_INetSharingManager,
|
|
|
|
|
reinterpret_cast<void **>(&pNSM));
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pNSM == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("CoCreateInstance NetSharingManager failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VariantInit(&v);
|
|
|
|
|
hr = pNSM->get_EnumEveryConnection(&pNSECC);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pNSECC) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pNSECC->get__NewEnum(&pUnk);
|
|
|
|
|
pNSECC->Release();
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pUnk) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pUnk->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void **>(&pEV));
|
|
|
|
|
pUnk->Release();
|
|
|
|
|
if (hr != S_OK || !pUnk) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (S_OK == pEV->Next(1, &v, nullptr)) {
|
|
|
|
|
if (V_VT(&v) == VT_UNKNOWN) {
|
|
|
|
|
V_UNKNOWN(&v)->QueryInterface(IID_INetConnection, reinterpret_cast<void **>(&pNC));
|
|
|
|
|
if (pNC) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR strGuid[MAX_PATH] = {};
|
|
|
|
|
NETCON_PROPERTIES *pNP;
|
|
|
|
|
pNC->GetProperties(&pNP);
|
|
|
|
|
|
|
|
|
|
StringCbPrintf(strGuid,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
TEXT("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
|
|
|
|
|
pNP->guidId.Data1,
|
|
|
|
|
pNP->guidId.Data2,
|
|
|
|
|
pNP->guidId.Data3,
|
|
|
|
|
pNP->guidId.Data4[0],
|
|
|
|
|
pNP->guidId.Data4[1],
|
|
|
|
|
pNP->guidId.Data4[2],
|
|
|
|
|
pNP->guidId.Data4[3],
|
|
|
|
|
pNP->guidId.Data4[4],
|
|
|
|
|
pNP->guidId.Data4[5],
|
|
|
|
|
pNP->guidId.Data4[6],
|
|
|
|
|
pNP->guidId.Data4[7]);
|
|
|
|
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
|
|
|
|
|
if (StrCmp(pGUID, strGuid) != 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(ifName, 0, MAX_NETCARD_NAME);
|
|
|
|
|
ret = WideCharToTChar(pNP->pszwName, ifName, MAX_NETCARD_NAME);
|
|
|
|
|
|
|
|
|
|
// ִ<><D6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>
|
|
|
|
|
if (ret != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Convert Unicode wide char to TCHAR failed: {0}."), ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetInternetIfName(TCHAR ifName[MAX_NETCARD_NAME]) {
|
|
|
|
|
for (int i = 0; i < NET_CARD_MAX; i++) {
|
|
|
|
|
const PNIC_CONTENT pNic = &g_NetAdapterInfo[i];
|
|
|
|
|
|
|
|
|
|
bool bIsInternel;
|
|
|
|
|
|
|
|
|
|
if (ERR_SUCCESS == IsInternetConnectAdapter(pNic->InterfaceIndex, &bIsInternel) && bIsInternel) {
|
|
|
|
|
StringCbCopy(ifName, MAX_NETCARD_NAME, pNic->NetCardName);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-17 10:55:01 +00:00
|
|
|
|
int GetInterfaceGUIDByName(const TCHAR *pInterfaceName, GUID *pGuid) {
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lstrlen(g_NetAdapterInfo[0].NetCardUUID) == 0) {
|
|
|
|
|
PNIC_CONTENT nic;
|
|
|
|
|
int nItem;
|
|
|
|
|
|
|
|
|
|
if ((ret = GetAllNICInfo(&nic, &nItem)) != ERR_SUCCESS) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const auto &v : g_NetAdapterInfo) {
|
|
|
|
|
if (StrCmp(pInterfaceName, v.NetCardName) == 0) {
|
|
|
|
|
WCHAR strGuid[MAX_PATH];
|
|
|
|
|
|
|
|
|
|
if ((ret = TCharToWideChar(v.NetCardUUID, strGuid, MAX_PATH)) != ERR_SUCCESS) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CLSIDFromString(strGuid, pGuid) != NOERROR) {
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int WaitNetAdapterConnected(const TCHAR *pInterfaceName, int timeOutOfMs) {
|
|
|
|
|
INetworkListManager *pNLM;
|
|
|
|
|
IEnumNetworkConnections *pEnumConns;
|
|
|
|
|
INetwork *pINet;
|
|
|
|
|
INetworkConnection *pIConn;
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
GUID guid;
|
|
|
|
|
const DWORD startTime = timeGetTime();
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_NetworkListManager,
|
|
|
|
|
nullptr,
|
|
|
|
|
CLSCTX_ALL,
|
|
|
|
|
IID_INetworkListManager,
|
|
|
|
|
reinterpret_cast<void **>(&pNLM));
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pNLM == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
|
|
hr = pNLM->GetNetworkConnections(&pEnumConns);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pEnumConns == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (S_OK == pEnumConns->Next(1, &pIConn, nullptr)) {
|
|
|
|
|
GUID adpterGuid;
|
|
|
|
|
pIConn->GetAdapterId(&adpterGuid);
|
|
|
|
|
|
|
|
|
|
pIConn->GetNetwork(&pINet);
|
|
|
|
|
|
|
|
|
|
if (pINet) {
|
|
|
|
|
BSTR sName = {};
|
|
|
|
|
TCHAR ifName[MAX_PATH];
|
|
|
|
|
|
|
|
|
|
pINet->GetName(&sName);
|
|
|
|
|
|
|
|
|
|
if (WideCharToTChar(sName, ifName, MAX_PATH) != ERR_SUCCESS) {
|
|
|
|
|
SysFreeString(sName);
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SysFreeString(sName);
|
|
|
|
|
|
|
|
|
|
if (StrNCmp(pInterfaceName, ifName, lstrlen(pInterfaceName)) == 0) {
|
|
|
|
|
if (GetInterfaceGUIDByName(pInterfaceName, &guid) != ERR_SUCCESS) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (memcmp(&adpterGuid, &guid, sizeof(GUID)) == 0) {
|
|
|
|
|
SPDLOG_DEBUG(TEXT("Interface {0} network connected."), pInterfaceName);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sleep(1000);
|
|
|
|
|
} while (timeGetTime() - startTime <= static_cast<DWORD>(timeOutOfMs));
|
|
|
|
|
|
|
|
|
|
return -ERR_SYS_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
int GetAllNICInfo(PNIC_CONTENT *pInfo, int *pItemCounts) {
|
|
|
|
|
PNIC_CONTENT pNic;
|
|
|
|
|
PIP_ADAPTER_INFO pAdapterInfo;
|
|
|
|
|
DWORD dwRetVal;
|
|
|
|
|
|
|
|
|
|
if (pItemCounts == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pItemCounts params error"));
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pInfo == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInfo params error"));
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
|
|
|
|
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO)));
|
|
|
|
|
|
|
|
|
|
if (pAdapterInfo == nullptr) {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo"));
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
|
|
|
|
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, ulOutBufLen));
|
|
|
|
|
if (pAdapterInfo == nullptr) {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo\n"));
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
|
|
|
|
int id = 0;
|
|
|
|
|
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
|
|
|
|
|
while (pAdapter && id < NET_CARD_MAX) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
g_NetAdapterInfo[id].InterfaceIndex = static_cast<int>(pAdapter->Index);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
StringCbCopy(g_NetAdapterInfo[id].NetCardUUID, MAX_ADAPTER_NAME_LENGTH, pAdapter->AdapterName);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><CFB8><EFBFBD><EFBFBD>
|
|
|
|
|
StringCbCopy(g_NetAdapterInfo[id].NetCardDescription,
|
|
|
|
|
MAX_ADAPTER_DESCRIPTION_LENGTH,
|
|
|
|
|
pAdapter->Description);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> IP <20><>ַ
|
|
|
|
|
StringCbCopy(g_NetAdapterInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpAddress.String);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
StringCbCopy(g_NetAdapterInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpMask.String);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ַ
|
|
|
|
|
StringCbCopy(g_NetAdapterInfo[id].NetCardGateway, MAX_IP_LEN - 1, pAdapter->GatewayList.IpAddress.String);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> MAC <20><>ַ
|
|
|
|
|
StringCbPrintf(g_NetAdapterInfo[id].NetCardMacAddr,
|
|
|
|
|
20 - 1,
|
2023-07-14 09:29:02 +00:00
|
|
|
|
TEXT("%02X:%02X:%02X:%02X:%02X:%02X"),
|
2023-07-10 08:36:19 +00:00
|
|
|
|
pAdapter->Address[0],
|
|
|
|
|
pAdapter->Address[1],
|
|
|
|
|
pAdapter->Address[2],
|
|
|
|
|
pAdapter->Address[3],
|
|
|
|
|
pAdapter->Address[4],
|
|
|
|
|
pAdapter->Address[5]);
|
|
|
|
|
|
|
|
|
|
GetInterfaceNameByGUID(pAdapter->AdapterName, g_NetAdapterInfo[id].NetCardName);
|
|
|
|
|
|
|
|
|
|
id++;
|
|
|
|
|
pAdapter = pAdapter->Next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*pItemCounts = id;
|
|
|
|
|
} else {
|
2023-07-14 09:29:02 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("GetAdaptersInfo failed with error: {0}\n"), dwRetVal);
|
2023-07-10 08:36:19 +00:00
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pAdapterInfo) {
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pNic = static_cast<PNIC_CONTENT>(CoTaskMemAlloc(sizeof(NIC_CONTENT) * (*pItemCounts)));
|
|
|
|
|
|
|
|
|
|
if (pNic == nullptr) {
|
|
|
|
|
*pItemCounts = 0;
|
|
|
|
|
SPDLOG_ERROR(TEXT("Error allocating memory {0} bytes"), sizeof(NIC_CONTENT) * (*pItemCounts));
|
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pNic, 0, sizeof(NIC_CONTENT) * (*pItemCounts));
|
|
|
|
|
memcpy(pNic, g_NetAdapterInfo, sizeof(NIC_CONTENT) * *pItemCounts);
|
|
|
|
|
*pInfo = pNic;
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int IsInternetConnectAdapter(int ifIndex, bool *pRet) {
|
|
|
|
|
DWORD dwSize = 0;
|
|
|
|
|
DWORD dwRetVal;
|
|
|
|
|
PMIB_IPFORWARDTABLE pIpForwardTable;
|
|
|
|
|
|
|
|
|
|
if (ifIndex < 0 || ifIndex > 255) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input ifIndex params error: {0}"), ifIndex);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pRet == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pRet params error"));
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pIpForwardTable = static_cast<MIB_IPFORWARDTABLE *>(HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPFORWARDTABLE)));
|
|
|
|
|
|
|
|
|
|
if (pIpForwardTable == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), sizeof(MIB_IPFORWARDTABLE));
|
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetIpForwardTable(pIpForwardTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pIpForwardTable);
|
|
|
|
|
pIpForwardTable = static_cast<MIB_IPFORWARDTABLE *>(HeapAlloc(GetProcessHeap(), 0, dwSize));
|
|
|
|
|
if (pIpForwardTable == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), dwSize);
|
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*pRet = false;
|
|
|
|
|
|
|
|
|
|
if ((dwRetVal = GetIpForwardTable(pIpForwardTable, &dwSize, 0)) == NO_ERROR) {
|
|
|
|
|
for (DWORD i = 0; i < pIpForwardTable->dwNumEntries; i++) {
|
|
|
|
|
TCHAR ipStr[24] = {};
|
|
|
|
|
TCHAR maskStr[24] = {};
|
|
|
|
|
|
|
|
|
|
if (static_cast<int>(pIpForwardTable->table[i].dwForwardIfIndex) != ifIndex) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (InetNtop(AF_INET, &pIpForwardTable->table[i].dwForwardDest, ipStr, 24) == nullptr) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (InetNtop(AF_INET, &pIpForwardTable->table[i].dwForwardMask, maskStr, 24) == nullptr) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StrCmp(ipStr, TEXT("0.0.0.0")) == 0 && StrCmp(maskStr, TEXT("0.0.0.0"))) {
|
|
|
|
|
*pRet = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pIpForwardTable);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
} else {
|
|
|
|
|
SPDLOG_ERROR(TEXT("GetIpForwardTable failed: {0}."), dwRetVal);
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pIpForwardTable);
|
|
|
|
|
return ERR_GET_IPFOWARDTBL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, const bool isPrivate) {
|
|
|
|
|
INetworkListManager *pNLM;
|
|
|
|
|
IEnumNetworks *pEnumNetworks;
|
|
|
|
|
INetwork *pINet;
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_NetworkListManager,
|
|
|
|
|
nullptr,
|
|
|
|
|
CLSCTX_ALL,
|
|
|
|
|
IID_INetworkListManager,
|
|
|
|
|
reinterpret_cast<void **>(&pNLM));
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pNLM == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pNLM->GetNetworks(NLM_ENUM_NETWORK_ALL, &pEnumNetworks);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pEnumNetworks == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (S_OK == pEnumNetworks->Next(1, &pINet, nullptr)) {
|
|
|
|
|
BSTR sName = {};
|
|
|
|
|
TCHAR ifName[MAX_PATH];
|
|
|
|
|
|
|
|
|
|
pINet->GetName(&sName);
|
|
|
|
|
|
|
|
|
|
if (WideCharToTChar(sName, ifName, MAX_PATH) != ERR_SUCCESS) {
|
|
|
|
|
SysFreeString(sName);
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SysFreeString(sName);
|
|
|
|
|
|
|
|
|
|
if (StrCmp(pInterfaceName, ifName) == 0) {
|
|
|
|
|
pINet->SetCategory(isPrivate ? NLM_NETWORK_CATEGORY_PRIVATE : NLM_NETWORK_CATEGORY_PUBLIC);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, bool *pIsPrivate) {
|
2023-07-17 10:55:01 +00:00
|
|
|
|
INetworkListManager *pNLM;
|
|
|
|
|
//IEnumNetworks *pEnumNetworks;
|
|
|
|
|
IEnumNetworkConnections *pEnumConns;
|
|
|
|
|
INetwork *pINet;
|
|
|
|
|
INetworkConnection *pIConn;
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
GUID guid;
|
|
|
|
|
int ret;
|
2023-07-10 08:36:19 +00:00
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pIsPrivate == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pIsPrivate params error"));
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-17 10:55:01 +00:00
|
|
|
|
if ((ret = GetInterfaceGUIDByName(pInterfaceName, &guid)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Get NetCard [{0}] GUID error: {1}"), pInterfaceName, ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
hr = ::CoCreateInstance(CLSID_NetworkListManager,
|
|
|
|
|
nullptr,
|
|
|
|
|
CLSCTX_ALL,
|
|
|
|
|
IID_INetworkListManager,
|
|
|
|
|
reinterpret_cast<void **>(&pNLM));
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pNLM == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-17 10:55:01 +00:00
|
|
|
|
#if 0
|
2023-07-10 08:36:19 +00:00
|
|
|
|
hr = pNLM->GetNetworks(NLM_ENUM_NETWORK_ALL, &pEnumNetworks);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pEnumNetworks == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
2023-07-17 10:55:01 +00:00
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
while (S_OK == pEnumNetworks->Next(1, &pINet, nullptr)) {
|
2023-07-17 10:55:01 +00:00
|
|
|
|
BSTR sName = {};
|
|
|
|
|
WCHAR wName[MAX_PATH];
|
|
|
|
|
TCHAR ifName[MAX_PATH];
|
|
|
|
|
TCHAR guidName[MAX_PATH];
|
|
|
|
|
NLM_NETWORK_CATEGORY cat;
|
|
|
|
|
GUID netGuid;
|
2023-07-10 08:36:19 +00:00
|
|
|
|
|
|
|
|
|
pINet->GetName(&sName);
|
|
|
|
|
|
|
|
|
|
if (WideCharToTChar(sName, ifName, MAX_PATH) != ERR_SUCCESS) {
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SysFreeString(sName);
|
2023-07-17 10:55:01 +00:00
|
|
|
|
pINet->GetCategory(&cat);
|
|
|
|
|
pINet->GetNetworkId(&netGuid);
|
2023-07-10 08:36:19 +00:00
|
|
|
|
|
2023-07-17 10:55:01 +00:00
|
|
|
|
StringFromGUID2(netGuid, wName, MAX_PATH);
|
2023-07-10 08:36:19 +00:00
|
|
|
|
|
2023-07-17 10:55:01 +00:00
|
|
|
|
if (WideCharToTChar(wName, guidName, MAX_PATH) != ERR_SUCCESS) {
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//SPDLOG_DEBUG(TEXT("[{3}]:{0} --> {1} --> {2}"), pInterfaceName, ifName, guidName, i++);
|
|
|
|
|
|
|
|
|
|
if (memcmp(&netGuid, &guid, sizeof(GUID)) == 0) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
*pIsPrivate = (cat == NLM_NETWORK_CATEGORY_PRIVATE) ? true : false;
|
2023-07-17 10:55:01 +00:00
|
|
|
|
SPDLOG_DEBUG(TEXT("**** {0} category {1}"), pInterfaceName, *pIsPrivate);
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
2023-07-17 10:55:01 +00:00
|
|
|
|
|
|
|
|
|
//SPDLOG_DEBUG(TEXT("++ {0} --> {1}, connect {2}"), ifName, static_cast<int>(cat), static_cast<int>(conn));
|
|
|
|
|
|
|
|
|
|
if (StrNCmp(pInterfaceName, ifName, lstrlen(pInterfaceName)) == 0) {
|
|
|
|
|
SPDLOG_DEBUG(TEXT("++ {0} --> {1} --> {2}"), pInterfaceName, ifName, guidName);
|
|
|
|
|
/* pINet->GetNetworkConnections(&pEnumConns);
|
|
|
|
|
|
|
|
|
|
if (pEnumConns) {
|
|
|
|
|
while (S_OK == pEnumConns->Next(1, &pIConn, nullptr)) {
|
|
|
|
|
GUID adpterGuid, connGuid;
|
|
|
|
|
WCHAR aGuid[MAX_PATH];
|
|
|
|
|
WCHAR cGuid[MAX_PATH];
|
|
|
|
|
TCHAR strAGuid[MAX_PATH];
|
|
|
|
|
TCHAR strCGuid[MAX_PATH];
|
|
|
|
|
|
|
|
|
|
pIConn->GetAdapterId(&adpterGuid);
|
|
|
|
|
pIConn->GetConnectionId(&connGuid);
|
|
|
|
|
|
|
|
|
|
StringFromGUID2(adpterGuid, aGuid, MAX_PATH);
|
|
|
|
|
StringFromGUID2(connGuid, cGuid, MAX_PATH);
|
|
|
|
|
|
|
|
|
|
WideCharToTChar(aGuid, strAGuid, MAX_PATH);
|
|
|
|
|
WideCharToTChar(cGuid, strCGuid, MAX_PATH);
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG(TEXT("--AdapterId: {0}, ConnectionId: {1}"), strAGuid, strCGuid);
|
|
|
|
|
}
|
|
|
|
|
SPDLOG_ERROR(TEXT("Exit ........"));
|
|
|
|
|
} else {
|
|
|
|
|
SPDLOG_ERROR(TEXT("----------------------------------------------------- empty"));
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
/*if (StrCmp(TEXT("admin 3"), ifName) == 0) {
|
|
|
|
|
BSTR dName = {};
|
|
|
|
|
pINet->GetDescription(&dName);
|
|
|
|
|
SPDLOG_DEBUG(TEXT("++ {0} --> {1} --> {2}"), pInterfaceName, ifName, guidName);
|
|
|
|
|
}*/
|
|
|
|
|
//if (StrCmp(pInterfaceName, ifName) == 0) {
|
|
|
|
|
// //NLM_NETWORK_CATEGORY cat;
|
|
|
|
|
// //pINet->GetCategory(&cat);
|
|
|
|
|
|
|
|
|
|
// *pIsPrivate = (cat == NLM_NETWORK_CATEGORY_PRIVATE) ? true : false;
|
|
|
|
|
// return ERR_SUCCESS;
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
hr = pNLM->GetNetworkConnections(&pEnumConns);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pEnumConns == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (S_OK == pEnumConns->Next(1, &pIConn, nullptr)) {
|
|
|
|
|
GUID adpterGuid;
|
|
|
|
|
pIConn->GetNetwork(&pINet);
|
|
|
|
|
pIConn->GetAdapterId(&adpterGuid);
|
|
|
|
|
|
|
|
|
|
if (pINet) {
|
|
|
|
|
if (memcmp(&adpterGuid, &guid, sizeof(GUID)) == 0) {
|
|
|
|
|
NLM_NETWORK_CATEGORY cat;
|
|
|
|
|
pINet->GetCategory(&cat);
|
|
|
|
|
*pIsPrivate = (cat == NLM_NETWORK_CATEGORY_PRIVATE) ? true : false;
|
|
|
|
|
SPDLOG_DEBUG(TEXT("**** {0} category {1}"), pInterfaceName, *pIsPrivate);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-10 08:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SetNetConnectionSharing(const TCHAR *pInterfaceName, bool isEnable, bool isSetPrivate) {
|
|
|
|
|
VARIANT v;
|
|
|
|
|
INetConnection *pNC = nullptr;
|
|
|
|
|
INetSharingConfiguration *pNSC = nullptr;
|
|
|
|
|
IEnumVARIANT *pEV = nullptr;
|
|
|
|
|
IUnknown *pUnk = nullptr;
|
|
|
|
|
INetSharingEveryConnectionCollection *pNSECC = nullptr;
|
|
|
|
|
INetSharingManager *pNSM;
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_NetSharingManager,
|
|
|
|
|
nullptr,
|
|
|
|
|
CLSCTX_ALL,
|
|
|
|
|
IID_INetSharingManager,
|
|
|
|
|
reinterpret_cast<void **>(&pNSM));
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pNSM == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("CoCreateInstance NetSharingManager failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VariantInit(&v);
|
|
|
|
|
hr = pNSM->get_EnumEveryConnection(&pNSECC);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pNSECC) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pNSECC->get__NewEnum(&pUnk);
|
|
|
|
|
pNSECC->Release();
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pUnk) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pUnk->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void **>(&pEV));
|
|
|
|
|
pUnk->Release();
|
|
|
|
|
if (hr != S_OK || !pUnk) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (S_OK == pEV->Next(1, &v, nullptr)) {
|
|
|
|
|
if (V_VT(&v) == VT_UNKNOWN) {
|
|
|
|
|
V_UNKNOWN(&v)->QueryInterface(IID_INetConnection, reinterpret_cast<void **>(&pNC));
|
|
|
|
|
if (pNC) {
|
|
|
|
|
TCHAR ifName[MAX_PATH];
|
|
|
|
|
NETCON_PROPERTIES *pNP;
|
|
|
|
|
pNC->GetProperties(&pNP);
|
|
|
|
|
|
|
|
|
|
// ִ<><D6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>
|
|
|
|
|
if (WideCharToTChar(pNP->pszwName, ifName, MAX_PATH) != ERR_SUCCESS) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
|
|
|
|
|
if (StrCmp(pInterfaceName, ifName) != 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// δ<><CEB4><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (pNP->Status != NCS_CONNECTED) {
|
|
|
|
|
return -ERR_NET_UNCONNECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pNSC) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pNSC) {
|
|
|
|
|
if (isEnable) {
|
|
|
|
|
pNSC->DisableSharing();
|
|
|
|
|
Sleep(500);
|
|
|
|
|
pNSC->EnableSharing(isSetPrivate ? ICSSHARINGTYPE_PRIVATE : ICSSHARINGTYPE_PUBLIC);
|
|
|
|
|
} else {
|
|
|
|
|
pNSC->DisableSharing();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pNSC->Release();
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetNetConnectionSharing(const TCHAR *pInterfaceName, bool *pIsEnable) {
|
|
|
|
|
VARIANT v;
|
|
|
|
|
INetConnection *pNC = nullptr;
|
|
|
|
|
INetSharingConfiguration *pNSC = nullptr;
|
|
|
|
|
IEnumVARIANT *pEV = nullptr;
|
|
|
|
|
IUnknown *pUnk = nullptr;
|
|
|
|
|
INetSharingEveryConnectionCollection *pNSECC = nullptr;
|
|
|
|
|
INetSharingManager *pNSM;
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pIsEnable == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pIsEnable params error"));
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = ::CoCreateInstance(CLSID_NetSharingManager,
|
|
|
|
|
nullptr,
|
|
|
|
|
CLSCTX_ALL,
|
|
|
|
|
IID_INetSharingManager,
|
|
|
|
|
reinterpret_cast<void **>(&pNSM));
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || pNSM == nullptr) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("CoCreateInstance NetSharingManager failed: {0}."), hr);
|
|
|
|
|
return -ERR_CREATE_COMMOBJECT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VariantInit(&v);
|
|
|
|
|
hr = pNSM->get_EnumEveryConnection(&pNSECC);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pNSECC) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pNSECC->get__NewEnum(&pUnk);
|
|
|
|
|
pNSECC->Release();
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pUnk) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pUnk->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void **>(&pEV));
|
|
|
|
|
pUnk->Release();
|
|
|
|
|
if (hr != S_OK || !pUnk) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr);
|
|
|
|
|
return -ERR_SYS_CALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (S_OK == pEV->Next(1, &v, nullptr)) {
|
|
|
|
|
if (V_VT(&v) == VT_UNKNOWN) {
|
|
|
|
|
V_UNKNOWN(&v)->QueryInterface(IID_INetConnection, reinterpret_cast<void **>(&pNC));
|
|
|
|
|
if (pNC) {
|
|
|
|
|
TCHAR ifName[MAX_PATH];
|
|
|
|
|
NETCON_PROPERTIES *pNP;
|
|
|
|
|
pNC->GetProperties(&pNP);
|
|
|
|
|
|
|
|
|
|
// ִ<><D6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>
|
|
|
|
|
if (WideCharToTChar(pNP->pszwName, ifName, MAX_PATH) != ERR_SUCCESS) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
|
|
|
|
|
if (StrCmp(pInterfaceName, ifName) != 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC);
|
|
|
|
|
|
|
|
|
|
if (hr != S_OK || !pNSC) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pNSC) {
|
|
|
|
|
VARIANT_BOOL bRet = false;
|
|
|
|
|
pNSC->get_SharingEnabled(&bRet);
|
|
|
|
|
*pIsEnable = bRet;
|
|
|
|
|
pNSC->Release();
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 09:29:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ·<EFBFBD>ɱ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in] ifIndex <EFBFBD><EFBFBD><EFBFBD><EFBFBD> Index
|
|
|
|
|
* @param[in] pIP Ŀ<EFBFBD><EFBFBD> IP <EFBFBD><EFBFBD>ַ
|
|
|
|
|
* @param[in] pMask Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param[in] pGateway ·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>н<EFBFBD><EFBFBD><EFBFBD> 0: <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD> С<EFBFBD><EFBFBD>0 ʧ<EFBFBD><EFBFBD> @see USER_ERRNO
|
|
|
|
|
* - -ERR_INPUT_PARAMS <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* - -ERR_UN_SUPPORT IP<EFBFBD><EFBFBD>ַת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַʧ<EFBFBD><EFBFBD>
|
|
|
|
|
* - -ERR_NET_ADD_ROUTE <EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD>ɱ<EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
|
|
|
|
|
* - ERR_SUCCESS <EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
int AddRouteTable(int ifIndex, const char *pIP, const char *pMask, const char *pGateway) {
|
|
|
|
|
MIB_IPFORWARDROW IpForwardTable;
|
|
|
|
|
|
|
|
|
|
if (pIP == nullptr || lstrlen(pIP) < 8) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pIP params error: {0}"), pIP);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pMask == nullptr || lstrlen(pMask) < 8) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pMask params error: {0}"), pMask);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pGateway == nullptr || lstrlen(pGateway) < 8) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Input pGateway params error: {0}"), pGateway);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ZeroMemory(&IpForwardTable, sizeof(MIB_IPFORWARDROW));
|
|
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, pIP, &IpForwardTable.dwForwardDest) <= 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Convert {0} to network ipaddress error."), pIP);
|
|
|
|
|
return -ERR_UN_SUPPORT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, pMask, &IpForwardTable.dwForwardMask) <= 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Convert {0} to network ipaddress error."), pMask);
|
|
|
|
|
return -ERR_UN_SUPPORT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, pGateway, &IpForwardTable.dwForwardNextHop) <= 0) {
|
|
|
|
|
SPDLOG_ERROR(TEXT("Convert {0} to network ipaddress error."), pGateway);
|
|
|
|
|
return -ERR_UN_SUPPORT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IpForwardTable.dwForwardIfIndex = ifIndex;
|
|
|
|
|
IpForwardTable.dwForwardType = MIB_IPROUTE_TYPE_DIRECT;
|
|
|
|
|
IpForwardTable.dwForwardProto = MIB_IPPROTO_LOCAL;
|
|
|
|
|
IpForwardTable.dwForwardPolicy = 0;
|
|
|
|
|
IpForwardTable.dwForwardAge = 0;
|
|
|
|
|
IpForwardTable.dwForwardNextHopAS = 0;
|
|
|
|
|
IpForwardTable.dwForwardMetric1 = 0xFFFFFFFF;
|
|
|
|
|
IpForwardTable.dwForwardMetric2 = 0xFFFFFFFF;
|
|
|
|
|
IpForwardTable.dwForwardMetric3 = 0xFFFFFFFF;
|
|
|
|
|
IpForwardTable.dwForwardMetric4 = 0xFFFFFFFF;
|
|
|
|
|
IpForwardTable.dwForwardMetric5 = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
|
|
if (CreateIpForwardEntry(&IpForwardTable) != NO_ERROR) {
|
|
|
|
|
return -ERR_NET_ADD_ROUTE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
#if 0
|
2023-06-25 07:57:47 +00:00
|
|
|
|
int GetWindowsHyperVStatus(int *pEnabled) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
TCHAR cmdResult[2048];
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
|
|
|
|
|
if (pEnabled == nullptr) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Input pEnabled params error"));
|
2023-06-25 07:57:47 +00:00
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
2023-07-10 08:36:19 +00:00
|
|
|
|
TEXT("PowerShell -Command \"& {Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All "
|
|
|
|
|
"-Online | Format-List -Property State}\"")))) {
|
2023-06-25 07:57:47 +00:00
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = RunCommand(cmdBuf, cmdResult, 2048, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StrStr(cmdResult, TEXT("Enabled")) != nullptr) {
|
|
|
|
|
*pEnabled = TRUE;
|
|
|
|
|
} else {
|
|
|
|
|
*pEnabled = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_DEBUG(TEXT("Run Get Windows Hyper-V status Command({1}): {0} result: {2}\n"), cmdBuf, retCode, cmdResult);
|
2023-06-25 07:57:47 +00:00
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EnableWindowsHyperV(bool enabled) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
if (FAILED(StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
2023-07-10 08:36:19 +00:00
|
|
|
|
TEXT("PowerShell -Command \"& {Enable-WindowsOptionalFeature -Online -FeatureName "
|
|
|
|
|
"Microsoft-Hyper-V -All}\"")))) {
|
2023-06-25 07:57:47 +00:00
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (FAILED(StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
2023-07-10 08:36:19 +00:00
|
|
|
|
TEXT("PowerShell -Command \"& {Disable-WindowsOptionalFeature -Online -FeatureName "
|
|
|
|
|
"Microsoft-Hyper-V-Hypervisor}\"")))) {
|
2023-06-25 07:57:47 +00:00
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Run command [{0}] error: {1}"), cmdBuf, ret);
|
2023-06-25 07:57:47 +00:00
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (retCode != 0) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("PowerShell return error({1}): {0}"), cmdBuf, retCode);
|
2023-06-25 07:57:47 +00:00
|
|
|
|
return -ERR_PROCESS_RETURN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 10:04:16 +00:00
|
|
|
|
int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) {
|
2023-06-25 07:57:47 +00:00
|
|
|
|
int ret;
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
TCHAR cmdResult[MAX_PATH] = {};
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pIndex == nullptr) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Input pIndex params error"));
|
2023-06-21 10:04:16 +00:00
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(
|
|
|
|
|
StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
2023-07-10 08:36:19 +00:00
|
|
|
|
TEXT("PowerShell -Command \"& {Get-NetAdapter -Name %s | Format-List -Property InterfaceIndex}\""),
|
2023-06-21 10:04:16 +00:00
|
|
|
|
pInterfaceName))) {
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
if ((ret = RunCommand(cmdBuf, cmdResult, MAX_PATH, &retCode)) != ERR_SUCCESS) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Run command [{0}] error: {1}"), cmdBuf, ret);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_DEBUG(TEXT("Run command [{0}] resutl \'{1}\' return {2}"), cmdBuf, cmdResult, retCode);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
// <20>Ƴ<EFBFBD>
|
|
|
|
|
StringRemoveAll(cmdResult, TEXT("\r\n"));
|
|
|
|
|
StringRemoveAll(cmdResult, TEXT(" "));
|
|
|
|
|
StringRemoveAll(cmdResult, TEXT("InterfaceIndex:"));
|
|
|
|
|
|
|
|
|
|
*pIndex = strtol(cmdResult, nullptr, 10);
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
int SetNATRule(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pCidrIpaddr == nullptr || lstrlen(pCidrIpaddr) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pCidrIpaddr params error: {0}", pCidrIpaddr);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(
|
|
|
|
|
StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {New-NetNat -Name %s_nat -InternalIPInterfaceAddressPrefix %s}\"",
|
|
|
|
|
pInterfaceName,
|
|
|
|
|
pCidrIpaddr))) {
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode);
|
|
|
|
|
|
|
|
|
|
if (retCode != 0) {
|
|
|
|
|
SPDLOG_ERROR("PowerShell return error({1}): {0}", cmdBuf, retCode);
|
|
|
|
|
return -ERR_PROCESS_RETURN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RemoveNATRule(const TCHAR *pInterfaceName) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {Remove-NetNat -Name %s_nat -Confirm:$false}\"",
|
|
|
|
|
pInterfaceName))) {
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode);
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 10:04:16 +00:00
|
|
|
|
int RemoveInterfaceIpAddress(const TCHAR *pInterfaceName) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
2023-06-25 07:57:47 +00:00
|
|
|
|
DWORD retCode;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {Remove-NetIPAddress -InterfaceAlias %s -Confirm:$false}\"",
|
|
|
|
|
pInterfaceName))) {
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
2023-06-21 10:04:16 +00:00
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode);
|
|
|
|
|
|
2023-06-21 10:04:16 +00:00
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask) {
|
2023-06-25 07:57:47 +00:00
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
int cidr;
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
|
2023-06-21 10:04:16 +00:00
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pIpaddr == nullptr || lstrlen(pIpaddr) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pIpaddr params error: {0}", pIpaddr);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pNetmask == nullptr || lstrlen(pNetmask) == 0) {
|
|
|
|
|
SPDLOG_WARN("Input pNetmask params error: {0}", pNetmask);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
cidr = NetmaskToCIDR(pNetmask);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
if (FAILED(StringCbPrintf(
|
|
|
|
|
cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {New-NetIPAddress -InterfaceAlias %s -IPAddress %s -PrefixLength %d}\"",
|
|
|
|
|
pInterfaceName,
|
|
|
|
|
pIpaddr,
|
|
|
|
|
cidr))) {
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode);
|
|
|
|
|
|
2023-06-21 10:04:16 +00:00
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) {
|
2023-06-25 07:57:47 +00:00
|
|
|
|
TCHAR ipAddr[MAX_IP_LEN];
|
|
|
|
|
TCHAR ip[MAX_IP_LEN];
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
|
|
|
|
|
TCHAR *token, *p = nullptr;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pCidrIpaddr == nullptr || lstrlen(pCidrIpaddr) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pCidrIpaddr params error: {0}", pCidrIpaddr);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringCbCopy(ipAddr, MAX_IP_LEN, pCidrIpaddr);
|
|
|
|
|
|
|
|
|
|
// <20><>ȡǰ<C8A1><C7B0>IP<49><50>ַ
|
|
|
|
|
token = strtok_s(ipAddr, TEXT("/"), &p);
|
|
|
|
|
if (token == nullptr) {
|
|
|
|
|
SPDLOG_ERROR("CIDR IpAddress string format error: {0}", pCidrIpaddr);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
StringCbCopy(ip, MAX_IP_LEN, token);
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
token = strtok_s(nullptr, TEXT("/"), &p);
|
|
|
|
|
if (token == nullptr) {
|
|
|
|
|
SPDLOG_ERROR("CIDR IpAddress string format error: {0}", pCidrIpaddr);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
if (FAILED(StringCbPrintf(
|
|
|
|
|
cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {New-NetIPAddress -InterfaceAlias %s -IPAddress %s -PrefixLength %s}\"",
|
|
|
|
|
pInterfaceName,
|
|
|
|
|
ip,
|
|
|
|
|
token))) {
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
2023-06-25 07:57:47 +00:00
|
|
|
|
return ERR_SUCCESS;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
int IsInterfacePrivate(const TCHAR *pInterfaceName, bool *pIsPrivateMode) {
|
|
|
|
|
int ret;
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
TCHAR cmdResult[MAX_PATH] = {};
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if (pIsPrivateMode == nullptr) {
|
|
|
|
|
SPDLOG_ERROR("Input pIsPrivateMode params error");
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if (FAILED(StringCbPrintf(
|
|
|
|
|
cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {Get-NetConnectionProfile -Name %s | Format-List -Property NetworkCategory}\"",
|
|
|
|
|
pInterfaceName))) {
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if ((ret = RunCommand(cmdBuf, cmdResult, MAX_PATH, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\' return {2}", cmdBuf, cmdResult, retCode);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if (StrStr(cmdResult, TEXT("Private")) != nullptr) {
|
|
|
|
|
*pIsPrivateMode = true;
|
|
|
|
|
return ERR_SUCCESS;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
} else {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if (StrStr(cmdResult, TEXT("Public")) != nullptr) {
|
|
|
|
|
*pIsPrivateMode = false;
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
2023-06-21 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
2023-06-21 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
int SetInterfacePrivate(const TCHAR *pInterfaceName, bool isPrivate) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
DWORD retCode;
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
if (isPrivate) {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if (FAILED(StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {Get-NetConnectionProfile -InterfaceAlias %s | "
|
|
|
|
|
"Set-NetConnectionProfile -NetworkCategory Private}\"",
|
|
|
|
|
pInterfaceName))) {
|
2023-06-27 02:02:02 +00:00
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-07-10 08:36:19 +00:00
|
|
|
|
if (FAILED(StringCbPrintf(cmdBuf,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
"PowerShell -Command \"& {Get-NetConnectionProfile -InterfaceAlias %s | "
|
|
|
|
|
"Set-NetConnectionProfile -NetworkCategory Public}\"",
|
|
|
|
|
pInterfaceName))) {
|
2023-06-27 02:02:02 +00:00
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
2023-06-19 11:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
2023-06-19 11:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
if (retCode != 0) {
|
|
|
|
|
SPDLOG_ERROR("PowerShell return error({1}): {0}", cmdBuf, retCode);
|
|
|
|
|
return -ERR_PROCESS_RETURN;
|
|
|
|
|
}
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-07-10 08:36:19 +00:00
|
|
|
|
bool IsCustomNatPSCmdInstalled() {
|
2023-06-27 02:02:02 +00:00
|
|
|
|
TCHAR psCmdPath[MAX_PATH];
|
|
|
|
|
|
|
|
|
|
StringCbPrintf(psCmdPath,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
TEXT("%s\\system32\\WindowsPowerShell\\v1.0\\Modules\\wireguard"),
|
|
|
|
|
GetGlobalCfgInfo()->systemDirectory);
|
|
|
|
|
|
|
|
|
|
// <20>ж<EFBFBD> WireGuard NAT <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>װ
|
|
|
|
|
if (!PathFileExists(psCmdPath)) {
|
|
|
|
|
if (!CreateDirectory(psCmdPath, nullptr)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-19 11:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
StringCbCat(psCmdPath, MAX_PATH, "\\wireguard.psm1");
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
if (PathFileExists(psCmdPath)) {
|
|
|
|
|
// <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>װ
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-06-27 02:02:02 +00:00
|
|
|
|
return false;
|
2023-07-10 08:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int IsNetConnectionSharingEnabled(const TCHAR *pInterfaceName, bool *pIsEnabled) {
|
|
|
|
|
int ret;
|
|
|
|
|
DWORD retCode;
|
|
|
|
|
TCHAR cmdResult[MAX_PATH] = {};
|
|
|
|
|
TCHAR cmdBuf[512];
|
|
|
|
|
|
|
|
|
|
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
|
|
|
|
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pIsEnabled == nullptr) {
|
|
|
|
|
SPDLOG_ERROR("Input pIsEnabled params error");
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbPrintf(
|
|
|
|
|
cmdBuf,
|
|
|
|
|
512,
|
|
|
|
|
TEXT("PowerShell -NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass Invoke-Command "
|
|
|
|
|
"-ArgumentList '%s' -ScriptBlock {param($IFNAME);$netShare = New-Object -ComObject HNetCfg.HNetShare;"
|
|
|
|
|
"$privateConnection = $netShare.EnumEveryConnection |? { $netShare.NetConnectionProps.Invoke($_).Name "
|
|
|
|
|
"-eq "
|
|
|
|
|
"'wg_cli' };$privateConfig = "
|
|
|
|
|
"$netShare.INetSharingConfigurationForINetConnection.Invoke($privateConnection);"
|
|
|
|
|
"Write-Output $privateConfig}"),
|
|
|
|
|
pInterfaceName))) {
|
|
|
|
|
|
|
|
|
|
SPDLOG_ERROR("Format String Error");
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
|
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (retCode != 0) {
|
|
|
|
|
SPDLOG_ERROR("PowerShell return error({1}): {0}", cmdBuf, retCode);
|
|
|
|
|
return -ERR_PROCESS_RETURN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\' return {2}", cmdBuf, cmdResult, retCode);
|
|
|
|
|
|
|
|
|
|
if (StrStr(cmdResult, TEXT("False")) != nullptr) {
|
|
|
|
|
*pIsEnabled = false;
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
} else {
|
|
|
|
|
if (StrStr(cmdResult, TEXT("True")) != nullptr) {
|
|
|
|
|
*pIsEnabled = true;
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
#endif
|