linux上实现获取mac功能

This commit is contained in:
luoliangyi 2022-06-30 19:13:53 +08:00
parent 3ccb4d4298
commit 1d339e6613
4 changed files with 174 additions and 117 deletions

View File

@ -1,25 +1,175 @@
#include "HGUpgrade.h"
#include "base/HGDef.h"
#include "base/HGInc.h"
#include "base/HGUtility.h"
#include "base/HGMd5.h"
#include "base/HGUtility.h"
#include "curl/curl.h"
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <HGString.h>
#if defined(HG_CMP_MSC)
#include <iphlpapi.h>
#endif
static void GetMacAddrList(std::vector<std::string> &macList)
{
macList.clear();
#if defined(HG_CMP_MSC)
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
ULONG nRet = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
if (ERROR_BUFFER_OVERFLOW == nRet)
{
free(pAdapterInfo);
pAdapterInfo = nullptr;
pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
nRet = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
}
if (ERROR_SUCCESS == nRet)
{
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (nullptr != pAdapter)
{
DWORD dwCharacteristics = 0;
HKEY hKey = nullptr;
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}", 0, KEY_ENUMERATE_SUB_KEYS, &hKey);
if (nullptr != hKey)
{
DWORD dwIndex = 0;
while (1)
{
CHAR szValueName[256];
if (ERROR_SUCCESS != RegEnumKeyA(hKey, dwIndex, szValueName, 256))
{
break;
}
HKEY hKey2 = nullptr;
RegOpenKeyExA(hKey, szValueName, 0, KEY_QUERY_VALUE, &hKey2);
if (nullptr != hKey2)
{
DWORD dwType;
CHAR szData[256] = { 0 };
DWORD cbData = 256;
if (ERROR_SUCCESS == RegQueryValueExA(hKey2, "NetCfgInstanceId", nullptr, &dwType, (LPBYTE)szData, &cbData) && REG_SZ == dwType)
{
if (0 == _stricmp(szData, pAdapter->AdapterName))
{
if (ERROR_SUCCESS == RegQueryValueExA(hKey2, "Characteristics", nullptr, &dwType, (LPBYTE)szData, &cbData) && REG_DWORD == dwType)
{
dwCharacteristics = *(DWORD*)szData;
}
}
}
RegCloseKey(hKey2);
}
++dwIndex;
}
RegCloseKey(hKey);
}
if ((dwCharacteristics & 0x4))
{
std::string strMacAddr;
for (UINT i = 0; i < pAdapter->AddressLength; i++)
{
char str[10];
sprintf(str, "%02x", pAdapter->Address[i]);
strMacAddr += str;
if (i != pAdapter->AddressLength - 1)
strMacAddr += "-";
}
macList.push_back(strMacAddr);
}
pAdapter = pAdapter->Next;
}
}
free(pAdapterInfo);
pAdapterInfo = nullptr;
#else
DIR *dir = opendir("/sys/class/net");
if (NULL != dir)
{
printf("opendir success\n");
struct dirent *ent;
while ((ent = readdir(dir)) != nullptr)
{
if (1)
{
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
if (strstr(ent->d_name, "docker") == ent->d_name || strstr(ent->d_name, "sit") == ent->d_name
|| strstr(ent->d_name, "ip6tnl") == ent->d_name || strcmp(ent->d_name, "lo") == 0)
continue;
printf("name %s\n", ent->d_name);
char addrPath[256];
sprintf(addrPath, "/sys/class/net/%s/address", ent->d_name);
FILE *file = fopen(addrPath, "rb");
if (NULL != file)
{
char mac[1025] = {0};
fread(mac, 1, 1024, file);
if (strlen(mac) > 0)
{
if (mac[strlen(mac) - 1] == '\n')
mac[strlen(mac) - 1] = '\0';
}
if (strlen(mac) > 0)
{
macList.push_back(mac);
}
printf("mac %s\n", mac);
fclose(file);
}
else
{
printf("fopen fail\n");
}
}
}
closedir(dir);
}
else
{
printf("opendir fail\n");
}
#endif
}
static std::string GetCurrVersion()
{
std::string version = "0.0.0.0";
#if defined(HG_CMP_MSC)
HKEY hKey = NULL;
HKEY hKey = nullptr;
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\HuaGoScan", 0, KEY_QUERY_VALUE, &hKey);
if (NULL != hKey)
if (nullptr != hKey)
{
CHAR szData[MAX_PATH] = { 0 };
DWORD cbData = MAX_PATH;
if (ERROR_SUCCESS == RegQueryValueExA(hKey, "AppVersion", NULL, NULL, (LPBYTE)szData, &cbData))
if (ERROR_SUCCESS == RegQueryValueExA(hKey, "AppVersion", nullptr, nullptr, (LPBYTE)szData, &cbData))
{
version = szData;
}
@ -42,6 +192,11 @@ static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
return size * nmemb;
}
static bool Greater(const std::string &str1, const std::string &str2)
{
return str1 > str2;
}
// type:1表示安装 2表示卸载 src:来源 desc: 说明
static bool PostInfo(int type, const std::string &src, const std::string &desc)
{
@ -57,14 +212,20 @@ static bool PostInfo(int type, const std::string &src, const std::string &desc)
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
HGByte mac[256];
HGUInt macCount = 0;
HGBase_GetMACAddress(mac, 256, &macCount);
char macStr[64] = {0};
sprintf(macStr, "%02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
std::vector<std::string> macList;
GetMacAddrList(macList);
if (macList.empty())
macList.push_back("00-00-00-00-00-00");
std::sort(macList.begin(), macList.end(), Greater);
std::string strMacAddrTotal;
for (int i = 0; i < (int)macList.size(); ++i)
{
strMacAddrTotal += macList[i];
}
HGByte md5[16] = {0};
HGBase_MakeMd5(mac, macCount * 6, md5);
HGBase_MakeMd5((const HGByte *)strMacAddrTotal.c_str(), strMacAddrTotal.size(), md5);
char md5Str[64] = {0};
char *pstr = md5Str;
for (int i = 0; i < 16; ++i)
@ -77,7 +238,7 @@ static bool PostInfo(int type, const std::string &src, const std::string &desc)
char json[1024];
sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\"}",
type, macStr, md5Str, version.c_str(), src.c_str(), desc.c_str());
type, macList[0].c_str(), md5Str, version.c_str(), src.c_str(), desc.c_str());
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
@ -131,7 +292,7 @@ static bool Setup(const std::string& pkgPath)
char command[256];
sprintf(command, "%s %s", Utf8ToStdString(pkgPath).c_str(), "/verysilent");
if (CreateProcessA(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInfo))
if (CreateProcessA(nullptr, command, nullptr, nullptr, FALSE, 0, nullptr, nullptr, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hThread);

View File

@ -62,7 +62,6 @@ HGBase_GetFileName
HGBase_GetFilePrefix
HGBase_GetFileSuffix
HGBase_StandardiseFileName
HGBase_GetMACAddress
HGBase_SetProfileInt
HGBase_SetProfileString

View File

@ -579,103 +579,3 @@ HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar* result
strcpy(result, stdFileName.c_str());
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_GetMACAddress(HGByte *mac, HGUInt maxLen, HGUInt* macCount)
{
if (NULL == mac || 0 == maxLen || NULL == macCount)
{
return HGBASE_ERR_INVALIDARG;
}
*macCount = 0;
HGByte* p = mac;
HGUInt len = maxLen;
#if defined(HG_CMP_MSC)
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
ULONG nRet = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
if (ERROR_BUFFER_OVERFLOW == nRet)
{
free(pAdapterInfo);
pAdapterInfo = NULL;
pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
nRet = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
}
if (ERROR_SUCCESS == nRet)
{
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (NULL != pAdapter)
{
if (6 != pAdapter->AddressLength)
{
pAdapter = pAdapter->Next;
continue;
}
DWORD dwCharacteristics = 0;
HKEY hKey = NULL;
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}", 0, KEY_ENUMERATE_SUB_KEYS, &hKey);
if (NULL != hKey)
{
DWORD dwIndex = 0;
while (1)
{
CHAR szValueName[256];
if (ERROR_SUCCESS != RegEnumKeyA(hKey, dwIndex, szValueName, 256))
{
break;
}
HKEY hKey2 = NULL;
RegOpenKeyExA(hKey, szValueName, 0, KEY_QUERY_VALUE, &hKey2);
if (NULL != hKey2)
{
DWORD dwType;
CHAR szData[256] = { 0 };
DWORD cbData = 256;
if (ERROR_SUCCESS == RegQueryValueExA(hKey2, "NetCfgInstanceId", NULL, &dwType, (LPBYTE)szData, &cbData) && REG_SZ == dwType)
{
if (0 == _stricmp(szData, pAdapter->AdapterName))
{
if (ERROR_SUCCESS == RegQueryValueExA(hKey2, "Characteristics", NULL, &dwType, (LPBYTE)szData, &cbData) && REG_DWORD == dwType)
{
dwCharacteristics = *(DWORD*)szData;
}
}
}
RegCloseKey(hKey2);
}
++dwIndex;
}
RegCloseKey(hKey);
}
if ((dwCharacteristics & 0x4) && (len >= pAdapter->AddressLength))
{
memcpy(p, pAdapter->Address, pAdapter->AddressLength);
p += pAdapter->AddressLength;
len -= pAdapter->AddressLength;
++(*macCount);
}
pAdapter = pAdapter->Next;
}
}
free(pAdapterInfo);
pAdapterInfo = NULL;
#else
// TODO
#endif
return HGBASE_ERR_OK;
}

View File

@ -77,7 +77,4 @@ HGEXPORT HGResult HGAPI HGBase_GetFileSuffix(const HGChar *fileName, HGChar* suf
/* 将文件名标准化 */
HGEXPORT HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar *result, HGUInt maxLen);
/* 获取MAC地址 */
HGEXPORT HGResult HGAPI HGBase_GetMACAddress(HGByte *mac, HGUInt maxLen, HGUInt *macCount);
#endif /* __HGUTILITY_H__ */
#endif /* __HGUTILITY_H__ */