CommEntityUtil.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #ifndef RVC_MOD_COMM_ENTITY_UTIL_HPP_
  2. #define RVC_MOD_COMM_ENTITY_UTIL_HPP_
  3. #if defined(_MSC_VER)
  4. #include <Windows.h>
  5. #include <TlHelp32.h>
  6. #include <iphlpapi.h>
  7. #include <ws2tcpip.h>
  8. #include <Winsock2.h>
  9. #include <IPTypes.h>
  10. #include <WinInet.h>
  11. #include <ShlObj.h>
  12. #pragma comment(lib, "IPHLPAPI.lib")
  13. #pragma comment(lib, "Wininet.lib")
  14. #else
  15. #include <linux/ethtool.h>
  16. #include <linux/sockios.h>
  17. #include <net/if.h>
  18. #include <sys/ioctl.h>
  19. #include <unistd.h>
  20. #include <thread>
  21. #include <chrono>
  22. #endif //_MSC_VER
  23. #include "path.h"
  24. #include "toolkit.h"
  25. #include "charset.h"
  26. #include <string>
  27. #include <iostream>
  28. #include <sstream>
  29. #include <map>
  30. #include <cmath>
  31. #include "SpBase.h"
  32. #include "publicFunExport.h"
  33. #define MACSESION 6
  34. typedef unsigned long long ULLINT;
  35. namespace SP
  36. {
  37. namespace Module
  38. {
  39. namespace Comm
  40. {
  41. //TODO: CrossPlaform !!!!UOS的实现与winpr_GetTickCount64是一样的,可以直接替换无影响 [Gifur@2025822]
  42. inline ULLINT RVCGetTickCount()
  43. {
  44. #ifdef RVC_OS_WIN
  45. return GetTickCount64();
  46. #else
  47. struct timespec ts;
  48. clock_gettime(CLOCK_MONOTONIC, &ts);
  49. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  50. #endif // RVC_OS_WIN
  51. }
  52. } // comm
  53. namespace Util
  54. {
  55. /**
  56. * Need to delete *hexBuf if (*hexBuf) not null
  57. */
  58. static int StrBuf2HexBuf(const char* strBuf, PBYTE* hexBuf)
  59. {
  60. int len = strlen(strBuf);
  61. if (len == 0 || len % 2 != 0)
  62. return 0;
  63. BYTE* buf = new BYTE[len / 2];
  64. if (buf == NULL)
  65. return 0;
  66. int j = 0;
  67. for (int i = 0; i < len;) {
  68. int tmpVal;
  69. sscanf(strBuf + i, "%2X", &tmpVal);
  70. buf[j] = tmpVal;
  71. i += 2;
  72. j++;
  73. }
  74. *hexBuf = buf;
  75. return j;
  76. }
  77. static int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
  78. {
  79. char* tmpStr = *strBuf;
  80. DWORD count = 0;
  81. for (DWORD i = 0; i < len; ++i) {
  82. sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
  83. count += 2;
  84. }
  85. return 0;
  86. }
  87. /*
  88. //tips: need to delete result char*
  89. static char* ConvertBytesToHexStr(BYTE* pBuf, int nLen)
  90. {
  91. char* pRet = (char*)new char[nLen * 2 + 1];
  92. memset(pRet, '\0', nLen * 2 + 1);
  93. char* p = pRet;
  94. for (int i = 0; i < nLen; i++) {
  95. BYTE b = pBuf[i];
  96. BYTE l = (b >> 4) & 0x0F;
  97. if (l >= 10)
  98. *p = l - 10 + 'a';
  99. else
  100. *p = l + '0';
  101. p++;
  102. BYTE r = b & 0x0F;
  103. if (r >= 10)
  104. *p = r - 10 + 'a';
  105. else
  106. *p = r + '0';
  107. p++;
  108. }
  109. return pRet;
  110. }
  111. //same as hex2int ??
  112. static char HexChar2Dec(char c)
  113. {
  114. if ((c >= '0') && (c <= '9'))
  115. return c - '0';
  116. else if ((c >= 'A') && (c <= 'F'))
  117. return c - 'A' + 10;
  118. else if ((c >= 'a') && (c <= 'f'))
  119. return c - 'a' + 10;
  120. else
  121. return 0x10;
  122. }
  123. static int Hex2Int(char* s)
  124. {
  125. int ret = 0;
  126. while (*s) {
  127. int t = HexChar2Dec(*s);
  128. ret = ret * 16 + t;
  129. ++s;
  130. }
  131. return ret;
  132. }
  133. */
  134. inline static int hex2int(const char c)
  135. {
  136. if (c >= '0' && c <= '9') return (c - '0');
  137. if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
  138. if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
  139. return 0xFF;
  140. }
  141. /*
  142. static void ConvAscii(unsigned char* Hex, char* Ascii, unsigned int len)
  143. {
  144. const unsigned char hextable[] = { "0123456789ABCDEF" };
  145. unsigned int i;
  146. for (i = 0; i < len; i++) {
  147. Ascii[2 * i] = hextable[Hex[i] >> 4];
  148. Ascii[2 * i + 1] = hextable[Hex[i] & 0xf];
  149. }
  150. Ascii[2 * len] = 0;
  151. }
  152. static void AsciiToHex(char* Ascii, unsigned char* Hex, int len)
  153. {
  154. int i;
  155. unsigned char ch;
  156. for (i = 0; i < len; i = i + 2) {
  157. ch = (Ascii[i] & 0xf);
  158. if (Ascii[i] > '9')
  159. ch += 9;
  160. Hex[i / 2] = ch << 4;
  161. ch = Ascii[i + 1] & 0xf;
  162. if (Ascii[i + 1] > '9')
  163. ch += 9;
  164. Hex[i / 2] += ch;
  165. }
  166. }
  167. static bool IsNoStr(const char* str)
  168. {
  169. int len = strlen(str);
  170. if (len == 0)
  171. return true;
  172. for (int i = 0; i < len; ++i) {
  173. if (*(str + i) != ' ')
  174. return false;
  175. }
  176. return true;
  177. }*/
  178. inline static int Char2Int(char* ch) {
  179. int num = 0;
  180. for (int i = 0; i < strlen(ch); i++) {
  181. num += ((int)(ch[i] - '0')) * std::pow((float)10, (float)(strlen(ch) - i - 1));
  182. }
  183. return num;
  184. }
  185. static unsigned char Ch2Hex(char ch)
  186. {
  187. static const char* hex = "0123456789ABCDEF";
  188. for (unsigned char i = 0; i != 16; ++i)
  189. if (ch == hex[i])
  190. return i;
  191. return 0;
  192. }
  193. /** remembe to delete[] return value if not null*/
  194. static char* HexStr2Bytes(const std::string& str)
  195. {
  196. const int sz = str.length();
  197. char* ret = new char[sz / 2 + 1];
  198. if (ret == NULL) {
  199. return NULL;
  200. }
  201. for (int i = 0; i < sz; i += 2) {
  202. ret[i / 2] = (char)(((hex2int(str[i])) << 4) | hex2int(str[i + 1]));
  203. }
  204. ret[sz / 2] = 0x00;
  205. return ret;
  206. }
  207. /*WARNING: need to delete[] the returned value if it's not null*/
  208. static char* Hex2Str(const char* src, int& dstLen)
  209. {
  210. int i = 0;
  211. int cnt = 0;
  212. int len = strlen(src);
  213. unsigned char* d = new unsigned char[len];
  214. memset(d, 0, len);
  215. while (*src)
  216. {
  217. if (i & 1)
  218. {
  219. d[cnt++] |= Ch2Hex(*src);
  220. }
  221. else
  222. {
  223. d[cnt] = Ch2Hex(*src) << 4;
  224. }
  225. src++;
  226. i++;
  227. }
  228. dstLen = cnt;
  229. return (char*)d;
  230. }
  231. /*WARNING: need to delete[] the returned value if it's not null*/
  232. static char* Str2Hex(const char* src, int srcLen)
  233. {
  234. string ret;
  235. static const char* hex = "0123456789ABCDEF";
  236. for (int i = 0; i != srcLen; ++i)
  237. {
  238. ret.push_back(hex[(src[i] >> 4) & 0xf]);
  239. ret.push_back(hex[src[i] & 0xf]);
  240. }
  241. char* tmp = new char[ret.length() + 1];
  242. memset(tmp, 0, ret.length() + 1);
  243. memcpy(tmp, ret.c_str(), ret.length());
  244. return tmp;
  245. }
  246. /*duplicate with the above one ??*/
  247. static std::string Bytes2HexStr(char* bytes, std::size_t byteLen)
  248. {
  249. std::string str("");
  250. const std::string conv("0123456789ABCDEF");
  251. for (std::size_t i = 0; i < byteLen; ++i) {
  252. int b = 0x0F & (bytes[i] >> 4);
  253. str.push_back(conv[b]);
  254. b = 0x0F & (bytes[i]);
  255. str.push_back(conv[b]);
  256. }
  257. return str;
  258. }
  259. static std::string formatTime(const SYSTEMTIME& time)
  260. {
  261. char tBuf[1024] = "";
  262. sprintf(tBuf, "%04u-%02u-%02u %02u:%02u:%02u:%03u", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
  263. return tBuf;
  264. }
  265. static CSimpleStringA generateConsumeTimeJson(const CSimpleStringA& entityName, const CSimpleStringA& startTime, int cost)
  266. {
  267. return CSimpleStringA::Format("{\"name\":\"%s\",\"time\":\"%s\",\"cost\":%d}", entityName.GetData(), startTime.GetData(), cost);
  268. }
  269. static bool ShellExecute(const std::string& cmd, std::string& succResult, std::string& errResult)
  270. {
  271. succResult = errResult = "";
  272. char ps[1024] = { 0 };
  273. strcpy(ps, cmd.c_str());
  274. #if defined(_MSC_VER)
  275. STARTUPINFO si;
  276. PROCESS_INFORMATION pi;
  277. ZeroMemory(&si, sizeof(si));
  278. si.cb = sizeof(si);
  279. si.wShowWindow = SW_HIDE;
  280. ZeroMemory(&pi, sizeof(pi));
  281. if (!CreateProcess(NULL, ps, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
  282. errResult = errResult + "CreateProcess failed with error:" + strerror(GetLastError());
  283. return false;
  284. }
  285. // 等待程序结束,并获取其退出状态
  286. WaitForSingleObject(pi.hProcess, INFINITE);
  287. DWORD exitCode;
  288. if (!GetExitCodeProcess(pi.hProcess, &exitCode)) {
  289. errResult = errResult + "GetExitCodeProcess failed with error code " + strerror(GetLastError());
  290. return false;
  291. }
  292. CloseHandle(pi.hProcess);
  293. CloseHandle(pi.hThread);
  294. if (exitCode != 0)
  295. {
  296. errResult = errResult + "The program exited with code:" + strerror(exitCode);
  297. return false;
  298. }
  299. return true;
  300. #else
  301. char buf_ps[1024];
  302. char result[2049] = { 0 };
  303. FILE* ptr;
  304. if ((ptr = popen(ps, "r")) != NULL) {
  305. while (fgets(buf_ps, 1024, ptr) != NULL) {
  306. if (strlen(result) + strlen(buf_ps) > 2048)
  307. break;
  308. strcat(result, buf_ps);
  309. }
  310. pclose(ptr);
  311. const int len = strlen(result);
  312. for (int i = len - 1; i >= 0 && (result[i] == '\r' || result[i] == '\n'); --i) {
  313. result[i] = '\0';
  314. }
  315. succResult = result;
  316. return true;
  317. } else {
  318. sprintf(result, "popen %s error: %d", ps, errno);
  319. errResult = result;
  320. return false;
  321. }
  322. #endif //_MSC_VER
  323. }
  324. #if defined(_MSC_VER)
  325. static void ConvertUtf82GBK(std::string& str)
  326. {
  327. char* dst = ConvertUtf8ToGBK(str.c_str());
  328. str = dst;
  329. if(dst) free(dst);
  330. }
  331. static void ConvertGBK2Utf8(std::string& str)
  332. {
  333. int len = 0;
  334. char* dst = ConvertGBKToUtf8(str.c_str(), &len);
  335. str = dst;
  336. if (dst) free(dst);
  337. }
  338. #endif //_MSC_VER
  339. enum DataTypeToMask
  340. {
  341. DataMask_UNKNOWN,
  342. DataMask_IDCard, /*居民身份证*/
  343. DataMask_HKMOTWPermit, /*港澳台居住证*/
  344. DataMask_HKMOPermit, /*港澳往来内地通行证*/
  345. DataMask_TWPass, /*台胞证*/
  346. DataMask_ForeignerPR, /*外国人永久居留证*/
  347. DataMask_Passport, /*外国人护照*/
  348. DataMask_CardAccount, /*卡号*/
  349. };
  350. //to mask data
  351. //if data is null or empty or length of data is short than 3, break ;
  352. //return the first one and the last two characters by default
  353. static CSimpleStringA DataMask(DataTypeToMask eDataType, CSimpleStringA data)
  354. {
  355. if (data.IsNullOrEmpty())
  356. {
  357. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("data is null or empty");
  358. return "";
  359. }
  360. else if (data.GetLength() < 3)
  361. {
  362. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("data length is %d, too short");
  363. return "";
  364. }
  365. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("data type is %d, length is %d", eDataType, data.GetLength());
  366. switch (eDataType)
  367. {
  368. case DataMask_IDCard:
  369. case DataMask_HKMOTWPermit:
  370. case DataMask_ForeignerPR:
  371. if (data.GetLength() < 7)
  372. {
  373. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("data length is %d", data.GetLength());
  374. break;
  375. }
  376. return CSimpleStringA::Format("%s****%s", data.SubString(0, 6).GetData(), data.SubString(data.GetLength() - 1, 1).GetData());
  377. break;
  378. case DataMask_HKMOPermit:
  379. if (data.GetLength() < 5)
  380. {
  381. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("data length is %d", data.GetLength());
  382. break;
  383. }
  384. return CSimpleStringA::Format("%s****%s", data.SubString(0, 1).GetData(), data.SubString(data.GetLength() - 4, 4).GetData());
  385. break;
  386. case DataMask_CardAccount:
  387. if (data.GetLength() < 5)
  388. {
  389. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("data length is %d", data.GetLength());
  390. break;
  391. }
  392. if (data.GetLength() < 16)
  393. return CSimpleStringA::Format("%s**%s", data.SubString(0, 2).GetData(), data.SubString(data.GetLength() - 2, 2).GetData());
  394. else
  395. return CSimpleStringA::Format("%s****%s", data.SubString(0, 4).GetData(), data.SubString(data.GetLength() - 4, 4).GetData());
  396. break;
  397. case DataMask_TWPass:
  398. case DataMask_Passport:
  399. case DataMask_UNKNOWN:
  400. default:
  401. break;
  402. }
  403. return CSimpleStringA::Format("%s****%s", data.SubString(0, 1).GetData(), data.SubString(data.GetLength() - 2, 2).GetData());
  404. }
  405. } //namespace Util
  406. namespace Net{
  407. struct NetworkAdapterItem
  408. {
  409. int idx;
  410. std::string friend_name;
  411. std::string adapter_name;
  412. std::string description;
  413. std::string ip;
  414. std::string mask;
  415. std::string mac;
  416. std::string gateway;
  417. std::string dhcp;
  418. bool is_physical;
  419. DWORD type;
  420. int operStatus;
  421. bool isLocal;
  422. NetworkAdapterItem() :idx(0), friend_name(""), adapter_name(""), description(""), ip(""), mask(""),
  423. mac(""), gateway(""), dhcp(""), is_physical(true), type(0), operStatus(0), isLocal(false)
  424. {
  425. }
  426. };
  427. static bool IsLocalAdapter(const std::string& name)
  428. {
  429. #if defined(_MSC_VER)
  430. if (name.length() <= 0) {
  431. return false;
  432. }
  433. std::string key("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}");
  434. HKEY h_sub_key = NULL;
  435. LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, KEY_READ, &h_sub_key);
  436. if (ret != 0) {
  437. return false;
  438. }
  439. std::ostringstream str;
  440. str << name << "\\Connection";
  441. HKEY h_local_key = NULL;
  442. ret = RegOpenKeyEx(h_sub_key, str.str().c_str(), 0, KEY_READ, &h_local_key);
  443. if (0 != ret) {
  444. RegCloseKey(h_sub_key);
  445. return false;
  446. }
  447. DWORD type = REG_SZ;
  448. TCHAR buf[250];
  449. DWORD buf_size = 250;
  450. ret = RegQueryValueEx(h_local_key, "PnPInstanceId", 0, &type, (BYTE*)(buf), &buf_size);
  451. RegCloseKey(h_sub_key);
  452. RegCloseKey(h_local_key);
  453. if (0 != ret) {
  454. return false;
  455. }
  456. if (0 == strnicmp(buf, "PCI", strlen("PCI")) || 0 == strnicmp(buf, "USB", strlen("USB"))) {
  457. return true;
  458. }
  459. return false;
  460. #else
  461. ///*TODO(80374374@3/7/2023): */
  462. return true;
  463. #endif //_MSC_VER
  464. }
  465. static ErrorCodeEnum GetINETMacAddresses(CAutoArray<NetworkAdapterItem>& netLists)
  466. {
  467. #if defined(RVC_OS_WIN)
  468. PIP_ADAPTER_ADDRESSES pAddresses = NULL;
  469. ULONG family = AF_INET;
  470. ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
  471. ULONG outBufLen = sizeof(IP_ADAPTER_ADDRESSES);
  472. // Make an initial call to GetAdaptersAddresses to get the
  473. // size needed into the outBufLen variable
  474. if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW) {
  475. pAddresses = static_cast<PIP_ADAPTER_ADDRESSES>(HeapAlloc(GetProcessHeap(), 0, outBufLen));
  476. }
  477. if (NULL == pAddresses) {
  478. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pAddresses = NULL");
  479. return Error_Unexpect;
  480. }
  481. DWORD dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
  482. /* MACAddresses vAddress;*/
  483. if (dwRetVal != ERROR_SUCCESS) {
  484. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("dwRetVal = %d", dwRetVal);
  485. return Error_Unexpect;
  486. }
  487. PIP_ADAPTER_ADDRESSES pFirst = pAddresses;
  488. while (pAddresses) {
  489. if (pAddresses->FirstUnicastAddress &&
  490. pAddresses->IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
  491. pAddresses->OperStatus == IfOperStatusUp &&
  492. pAddresses->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET
  493. ) {
  494. NetworkAdapterItem netItem;
  495. BYTE* pa = pAddresses->PhysicalAddress;
  496. if (!pa) {
  497. pAddresses = pAddresses->Next ? pAddresses->Next : NULL;
  498. continue;
  499. }
  500. CSimpleStringA strFriendlyName(true);
  501. CSimpleStringA strDescription(true);
  502. DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->FriendlyName, -1, NULL, 0, NULL, FALSE);
  503. char* psText = new char[dwNum];
  504. if (psText != NULL) {
  505. WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->FriendlyName, -1, psText, dwNum, NULL, FALSE);
  506. strFriendlyName = psText;
  507. delete[] psText;
  508. }
  509. dwNum = WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->Description, -1, NULL, 0, NULL, FALSE);
  510. psText = new char[dwNum];
  511. if (psText != NULL) {
  512. WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->Description, -1, psText, dwNum, NULL, FALSE);
  513. strDescription = psText;
  514. delete[] psText;
  515. }
  516. char bAddressBytes[MACSESION];
  517. int bAddressInt[MACSESION];
  518. memset(bAddressBytes, 0, MACSESION);
  519. size_t nAddressSize = pAddresses->PhysicalAddressLength;
  520. memcpy(bAddressBytes, pa, (nAddressSize < MACSESION ? nAddressSize : MACSESION));
  521. for (int i = 0; i < MACSESION; ++i) {
  522. bAddressInt[i] = bAddressBytes[i];
  523. bAddressInt[i] &= 0x000000ff; // avoid "ff" leading bytes when "char" is lager then 0x7f
  524. }
  525. CSimpleStringA tmpmac = CSimpleStringA::Format("%02x:%02x:%02x:%02x:%02x:%02x",
  526. bAddressInt[0],
  527. bAddressInt[1],
  528. bAddressInt[2],
  529. bAddressInt[3],
  530. bAddressInt[4],
  531. bAddressInt[5]);
  532. sockaddr_in* sa_in = (sockaddr_in*)pAddresses->FirstUnicastAddress->Address.lpSockaddr;
  533. char buf_addr[100] = { 0 };
  534. CSimpleStringA tmpip = CSimpleStringA(inet_ntop(AF_INET, &(sa_in->sin_addr), buf_addr, 100));
  535. if (tmpip.Compare("127.0.0.1") == 0 && tmpmac.Compare("00:00:00:00:00:00") == 0) {
  536. //skip
  537. }
  538. else
  539. {
  540. netItem.friend_name = strFriendlyName;
  541. netItem.description = strDescription;
  542. netItem.mac = tmpmac;
  543. netItem.ip = tmpip;
  544. netItem.operStatus = (pAddresses->OperStatus == IfOperStatusUp) ? 1 : 0;//stay the same value with Linux/UOS
  545. netItem.type = pAddresses->IfType;
  546. netLists.Append(&netItem, 0, 1);
  547. }
  548. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s, %s: OperStatus: %d, IfType = %d, ip=%s, mac=%s"
  549. , strFriendlyName.GetData(), strDescription.GetData()
  550. , pAddresses->OperStatus, pAddresses->IfType, tmpip.GetData(), tmpmac.GetData());
  551. }
  552. pAddresses = pAddresses->Next ? pAddresses->Next : NULL;
  553. }
  554. HeapFree(GetProcessHeap(), 0, pFirst);
  555. return Error_Succeed;
  556. #else
  557. std::map<std::string, std::string> inteIPs;
  558. std::map<std::string, std::string> inteMacs;
  559. std::map<std::string, int> inteStatus;
  560. char buf[512];
  561. toolkit_interface_address_t* info;
  562. int count, i;
  563. toolkit_interface_addresses(&info, &count);
  564. i = count;
  565. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Number of interfaces: %d", count);
  566. while (i--) {
  567. toolkit_interface_address_t interface = info[i];
  568. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Name: %s", interface.name);
  569. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Internal? %s", interface.is_internal ? "Yes" : "No");
  570. if (interface.address.address4.sin_family == AF_INET) {
  571. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  572. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("IPv4 address: %s", buf);
  573. inteIPs[interface.name] = buf;
  574. }
  575. else if (interface.address.address4.sin_family == AF_INET6) {
  576. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  577. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("IPv6 address: %s", buf);
  578. //inteIPs[interface.name] = buf;
  579. }
  580. }
  581. toolkit_free_interface_addresses(info, count);
  582. int fd, interface;
  583. struct ifreq bufIfreq[16];
  584. struct ifconf ifc;
  585. char mac[32] = { 0 };
  586. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
  587. int i = 0;
  588. ifc.ifc_len = sizeof(bufIfreq);
  589. ifc.ifc_buf = (caddr_t)bufIfreq;
  590. if (!ioctl(fd, SIOCGIFCONF, (char*)& ifc)) {
  591. interface = ifc.ifc_len / sizeof(struct ifreq);
  592. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("interface num is %d", interface);
  593. while (i < interface) {
  594. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Name: %s", bufIfreq[i].ifr_name);
  595. if (!(ioctl(fd, SIOCGIFHWADDR, (char*)& bufIfreq[i]))) {
  596. sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X",
  597. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[0],
  598. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[1],
  599. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[2],
  600. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[3],
  601. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[4],
  602. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[5]);
  603. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("HWaddr %s", mac);
  604. inteMacs[bufIfreq[i].ifr_name] = mac;
  605. }
  606. struct ethtool_value edata;
  607. edata.cmd = ETHTOOL_GLINK;
  608. edata.data = 0;
  609. bufIfreq[i].ifr_data = (char*)& edata;
  610. //oiltmp@20231026 只检测了以太网卡
  611. if (ioctl(fd, SIOCETHTOOL, (char*)& bufIfreq[i]) == -1) {
  612. //up down
  613. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Name: %s is down", bufIfreq[i].ifr_name);
  614. inteStatus[bufIfreq[i].ifr_name] = 0;
  615. }
  616. else
  617. {
  618. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Name: %s is up", bufIfreq[i].ifr_name);
  619. inteStatus[bufIfreq[i].ifr_name] = 1;
  620. }
  621. i++;
  622. }
  623. }
  624. close(fd);
  625. }
  626. std::map<std::string, std::string>::const_iterator map_it = inteIPs.begin();
  627. while (map_it != inteIPs.end()) {
  628. NetworkAdapterItem netItem;
  629. CSimpleStringA tmpip(map_it->second.c_str());
  630. CSimpleStringA tmpmac(true);
  631. CSimpleStringA tmpname(true);
  632. auto it = inteMacs.find(std::string(map_it->first));
  633. if (it != inteMacs.end()) {
  634. tmpmac = it->second.c_str();
  635. }
  636. if (tmpip.Compare("127.0.0.1") == 0 && tmpmac.Compare("00:00:00:00:00:00") == 0) {
  637. //skip
  638. }
  639. else {
  640. tmpname = map_it->first.c_str();
  641. netItem.friend_name = "";//oiltmp
  642. netItem.description = tmpname;
  643. netItem.mac = tmpmac;
  644. netItem.ip = tmpip;
  645. netItem.operStatus = inteStatus[map_it->first];
  646. netItem.type = 6;//oiltest haven't find the in linux
  647. netLists.Append(&netItem, 0, 1);
  648. }
  649. ++map_it;
  650. }
  651. return Error_Succeed;
  652. #endif //RVC_OS_WIN
  653. }
  654. }//Net
  655. } // mod
  656. } // sp
  657. #endif //RVC_MOD_COMM_ENTITY_UTIL_HPP_