339 lines
12 KiB
C++
339 lines
12 KiB
C++
#include "pch.h"
|
||
#include "tunnel.h"
|
||
#include "usrerr.h"
|
||
#include <strsafe.h>
|
||
#include <tchar.h>
|
||
#include <Iphlpapi.h>
|
||
#include <iostream>
|
||
|
||
#include "globalcfg.h"
|
||
#include "misc.h"
|
||
|
||
#pragma comment(lib, "Iphlpapi.lib")
|
||
|
||
int NetmaskToCIDR(const TCHAR *pNetMask) {
|
||
const TCHAR *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")};
|
||
|
||
for (int i = 0; i < static_cast<int>(std::size(tabCIDR)); i++) {
|
||
if (lstrcmp(tabCIDR[i], pNetMask) == 0) {
|
||
return i + 1;
|
||
}
|
||
}
|
||
|
||
return 0xFF;
|
||
}
|
||
|
||
const TCHAR *CIDRToNetmask(const UINT8 cidr) {
|
||
const TCHAR *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")};
|
||
|
||
if (cidr >= 1 && cidr <= std::size(tabCIDR)) {
|
||
return tabCIDR[cidr - 1];
|
||
}
|
||
|
||
return TEXT("");
|
||
}
|
||
|
||
int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) {
|
||
int ret;
|
||
TCHAR cmdBuf[MAX_PATH];
|
||
TCHAR cmdResult[MAX_PATH] = {};
|
||
const TCHAR *pToken[] = {TEXT("\r\n"), TEXT(" "), TEXT("InterfaceIndex:")};
|
||
|
||
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
|
||
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
|
||
return -ERR_INPUT_PARAMS;
|
||
}
|
||
|
||
if (pIndex == nullptr) {
|
||
SPDLOG_ERROR("Input pIndex params error");
|
||
return -ERR_INPUT_PARAMS;
|
||
}
|
||
|
||
if (FAILED(
|
||
StringCbPrintf(cmdBuf,
|
||
MAX_PATH,
|
||
"PowerShell -Command \"& {Get-NetAdapter -Name %s | Format-List -Property InterfaceIndex}\"",
|
||
pInterfaceName))) {
|
||
SPDLOG_ERROR("Format String Error");
|
||
return -ERR_MEMORY_STR;
|
||
}
|
||
|
||
if ((ret = RunCommand(cmdBuf, cmdResult, MAX_PATH)) != ERR_SUCCESS) {
|
||
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
||
return -ERR_CALL_SHELL;
|
||
}
|
||
|
||
SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\'", cmdBuf, cmdResult);
|
||
|
||
// <20>Ƴ<EFBFBD>
|
||
StringRemoveAll(cmdResult, TEXT("\r\n"));
|
||
StringRemoveAll(cmdResult, TEXT(" "));
|
||
StringRemoveAll(cmdResult, TEXT("InterfaceIndex:"));
|
||
|
||
*pIndex = strtol(cmdResult, nullptr, 10);
|
||
|
||
return ERR_SUCCESS;
|
||
}
|
||
|
||
/**
|
||
* @brief ɾ<><C9BE><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
||
* @param pInterfaceName <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* @return <20><><EFBFBD><EFBFBD>ִ<EFBFBD>н<EFBFBD><D0BD><EFBFBD> 0: <20>ɹ<EFBFBD><C9B9><EFBFBD> С<><D0A1>0 ʧ<><CAA7> @see USER_ERRNO
|
||
* - -ERR_INPUT_PARAMS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* - -ERR_MALLOC_MEMORY <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ʧ<EFBFBD><CAA7>
|
||
* - -ERR_MEMORY_STR <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* - -ERR_CALL_SHELL <20><><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
|
||
* - ERR_SUCCESS <20>ɹ<EFBFBD>
|
||
*/
|
||
int RemoveInterfaceIpAddress(const TCHAR *pInterfaceName) {
|
||
int ret;
|
||
TCHAR cmdBuf[MAX_PATH];
|
||
|
||
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;
|
||
}
|
||
|
||
if ((ret = RunCommand(cmdBuf, nullptr, 0)) != ERR_SUCCESS) {
|
||
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
|
||
return -ERR_CALL_SHELL;
|
||
}
|
||
|
||
return ERR_SUCCESS;
|
||
}
|
||
|
||
int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask) {
|
||
ULONG index = 0;
|
||
WCHAR pIfName[] = L"{7D92693A-0F1E-0212-176D-31226AF3E147}";
|
||
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;
|
||
}
|
||
|
||
//MultiByteToWideChar(CP_UTF8, 0, pInterfaceName, lstrlen(pInterfaceName), pIfName, MAX_PATH / sizeof(WCHAR));
|
||
|
||
if (GetAdapterIndex(pIfName, &index) != NO_ERROR) {
|
||
ShowWindowsErrorMessage("GetAdapterIndex");
|
||
return -ERR_SYS_CALL;
|
||
}
|
||
|
||
return ERR_SUCCESS;
|
||
}
|
||
|
||
int SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) {
|
||
TCHAR ipAddr[MAX_IP_LEN];
|
||
TCHAR ip[MAX_IP_LEN];
|
||
TCHAR cidrMask[4];
|
||
const TCHAR *netmask;
|
||
TCHAR *token, *p = nullptr;
|
||
|
||
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;
|
||
}
|
||
StringCbCopy(cidrMask, 4, token);
|
||
|
||
int cidr = atoi(cidrMask);
|
||
netmask = CIDRToNetmask(cidr);
|
||
|
||
SPDLOG_DEBUG("CIDR IpAddress convert:{0} --> {1}/{2}", pCidrIpaddr, ip, netmask);
|
||
|
||
return SetInterfaceIpAddress(pInterfaceName, ip, netmask);
|
||
}
|
||
|
||
int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
|
||
PIP_ADAPTER_INFO pAdapterInfo;
|
||
DWORD dwRetVal = 0;
|
||
|
||
if (pItemCounts == nullptr || pInfo == nullptr) {
|
||
SPDLOG_ERROR("Input params error: {0}");
|
||
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) {
|
||
SPDLOG_ERROR("Error allocating memory needed to call GetAdaptersinfo");
|
||
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) {
|
||
SPDLOG_ERROR("Error allocating memory needed to call GetAdaptersinfo\n");
|
||
return -ERR_MALLOC_MEMORY;
|
||
}
|
||
}
|
||
|
||
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
||
UINT32 id = 0;
|
||
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
|
||
while (pAdapter && id < NET_CARD_MAX) {
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
pInfo[id].InterfaceIndex = pAdapter->Index;
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
StringCbCopy(pInfo[id].NetCardUUID, MAX_ADAPTER_NAME_LENGTH, pAdapter->AdapterName);
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><CFB8><EFBFBD><EFBFBD>
|
||
StringCbCopy(pInfo[id].NetCardDescription, MAX_ADAPTER_DESCRIPTION_LENGTH, pAdapter->Description);
|
||
// <20><><EFBFBD><EFBFBD> IP <20><>ַ
|
||
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpAddress.String);
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
StringCbCopy(pInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpMask.String);
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ַ
|
||
StringCbCopy(pInfo[id].NetCardGateway, MAX_IP_LEN - 1, pAdapter->GatewayList.IpAddress.String);
|
||
// <20><><EFBFBD><EFBFBD> MAC <20><>ַ
|
||
StringCbPrintf(pInfo[id].NetCardMacAddr,
|
||
20 - 1,
|
||
"%02X:%02X:%02X:%02X:%02X:%02X",
|
||
pAdapter->Address[0],
|
||
pAdapter->Address[1],
|
||
pAdapter->Address[2],
|
||
pAdapter->Address[3],
|
||
pAdapter->Address[4],
|
||
pAdapter->Address[5]);
|
||
|
||
id++;
|
||
pAdapter = pAdapter->Next;
|
||
}
|
||
|
||
*pItemCounts = id;
|
||
} else {
|
||
SPDLOG_ERROR("GetAdaptersInfo failed with error: {0}\n", dwRetVal);
|
||
return -ERR_SYS_CALL;
|
||
}
|
||
|
||
if (pAdapterInfo) {
|
||
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
||
}
|
||
|
||
return ERR_SUCCESS;
|
||
}
|
||
|
||
#if 0
|
||
int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
|
||
int nRel, id = 0;
|
||
IP_ADDR_STRING *pIpAddrString;
|
||
PIP_ADAPTER_INFO pIpAdapterInfo;
|
||
unsigned long stSize;
|
||
|
||
if (pItemCounts == nullptr || pInfo == nullptr) {
|
||
return -ERR_INPUT_PARAMS;
|
||
}
|
||
|
||
pIpAdapterInfo = new IP_ADAPTER_INFO[NET_CARD_MAX];
|
||
stSize = sizeof(IP_ADAPTER_INFO) * NET_CARD_MAX;
|
||
|
||
// WIN32 API get net card information
|
||
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
|
||
|
||
if (ERROR_BUFFER_OVERFLOW == nRel) {
|
||
delete[] pIpAdapterInfo;
|
||
return -ERR_MALLOC_MEMORY;
|
||
}
|
||
|
||
PIP_ADAPTER_INFO cur = pIpAdapterInfo;
|
||
|
||
while (cur) {
|
||
pInfo[id].InterfaceIndex = cur->Index;
|
||
StringCbCopy(pInfo[id].NetCardUUID, MAX_ADAPTER_NAME_LENGTH, cur->AdapterName);
|
||
StringCbCopy(pInfo[id].NetCardDescription, MAX_ADAPTER_DESCRIPTION_LENGTH, cur->Description);
|
||
|
||
switch (cur->Type) {
|
||
case MIB_IF_TYPE_ETHERNET:
|
||
pIpAddrString = &(cur->IpAddressList);
|
||
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pIpAddrString->IpAddress.String);
|
||
StringCbCopy(pInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pIpAddrString->IpMask.String);
|
||
break;
|
||
case MIB_IF_TYPE_OTHER:
|
||
case MIB_IF_TYPE_TOKENRING:
|
||
case MIB_IF_TYPE_FDDI:
|
||
case MIB_IF_TYPE_PPP:
|
||
case MIB_IF_TYPE_LOOPBACK:
|
||
case MIB_IF_TYPE_SLIP:
|
||
break;
|
||
default: // WIFI ,Unknown type
|
||
pIpAddrString = &(cur->IpAddressList);
|
||
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pIpAddrString->IpAddress.String);
|
||
StringCbCopy(pInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pIpAddrString->IpMask.String);
|
||
break;
|
||
}
|
||
|
||
StringCbPrintf(pInfo[id].NetCardMacAddr,
|
||
20 - 1,
|
||
"%02X:%02X:%02X:%02X:%02X:%02X",
|
||
cur->Address[0],
|
||
cur->Address[1],
|
||
cur->Address[2],
|
||
cur->Address[3],
|
||
cur->Address[4],
|
||
cur->Address[5]);
|
||
|
||
id++;
|
||
cur = cur->Next;
|
||
}
|
||
|
||
*pItemCounts = id;
|
||
|
||
delete[] pIpAdapterInfo;
|
||
|
||
return ERR_SUCCESS;
|
||
}
|
||
#endif
|