CommEntityUtil.hpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. #include <Pdh.h>
  13. #pragma comment(lib, "IPHLPAPI.lib")
  14. #pragma comment(lib, "Wininet.lib")
  15. #pragma comment(lib, "pdh.lib")
  16. #define streq stricmp
  17. #else
  18. #include <linux/ethtool.h>
  19. #include <linux/sockios.h>
  20. #include <net/if.h>
  21. #include <sys/ioctl.h>
  22. #include <unistd.h>
  23. #include <thread>
  24. #include <chrono>
  25. #define streq strcasecmp
  26. #endif //_MSC_VER
  27. #include "path.h"
  28. #include "toolkit.h"
  29. #include <string>
  30. #include <iostream>
  31. #include <sstream>
  32. #include <map>
  33. #include "SpBase.h"
  34. #include "publicFunExport.h"
  35. #define MACSESION 6
  36. #define DIV (1024 * 1024)
  37. #define DAY_DIV (24 * 60 * 60)
  38. #define HOURS_DIV (60 * 60)
  39. #define MINUS_DIV (60)
  40. typedef unsigned long long ULLINT;
  41. #define SLEEP(interval) std::this_thread::sleep_for(std::chrono::milliseconds(interval))
  42. namespace SP
  43. {
  44. namespace Module
  45. {
  46. namespace System
  47. {
  48. static BOOL GetSystemBootTime(CSmallDateTime& systemBootTime)
  49. {
  50. #if defined(_MSC_VER)
  51. PDH_STATUS Status;
  52. HQUERY Query = NULL;
  53. HCOUNTER hcElapsedTimeCount;
  54. BOOL fSuc = FALSE;
  55. Status = PdhOpenQuery(NULL, NULL, &Query);
  56. PDH_FMT_COUNTERVALUE counterValue;
  57. if (Status != ERROR_SUCCESS) {
  58. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("PdhOpenQuery failed with status 0x%x.", Status);
  59. goto Cleanup;
  60. }
  61. Status = PdhAddCounter(Query, "\\System\\System Up Time", NULL, &hcElapsedTimeCount);
  62. if (Status != ERROR_SUCCESS) {
  63. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("PdhAddCounter for SystemElapsedQuery failed with status 0x%x.", Status);
  64. goto Cleanup;
  65. }
  66. // 查询性能监视器数据
  67. Status = PdhCollectQueryData(Query);
  68. if (Status != ERROR_SUCCESS) {
  69. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("PdhCollectQueryData failed with 0x%x.", Status);
  70. goto Cleanup;
  71. }
  72. Status = PdhGetFormattedCounterValue(hcElapsedTimeCount, PDH_FMT_LARGE, NULL, &counterValue);
  73. if (Status == ERROR_SUCCESS) {
  74. ULONGLONG ulSinceSeconds = counterValue.largeValue;
  75. ULONG days = 0, hours = 0, minutes = 0, seconds = 0;
  76. days = ULONG(ulSinceSeconds / DAY_DIV);
  77. ulSinceSeconds %= DAY_DIV;
  78. hours = ULONG(ulSinceSeconds / HOURS_DIV);
  79. ulSinceSeconds %= HOURS_DIV;
  80. minutes = ULONG(ulSinceSeconds / MINUS_DIV);
  81. ulSinceSeconds %= MINUS_DIV;
  82. seconds = ULONG(ulSinceSeconds);
  83. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SystemElapseTime: %u:%02u:%02u:%02u", days, hours, minutes, seconds);
  84. FILETIME ftCurTime, ftStartTime;
  85. GetSystemTimeAsFileTime(&ftCurTime);
  86. ULARGE_INTEGER uliCurTime;
  87. uliCurTime.HighPart = ftCurTime.dwHighDateTime;
  88. uliCurTime.LowPart = ftCurTime.dwLowDateTime;
  89. uliCurTime.QuadPart -= counterValue.largeValue * 1e7;
  90. ftStartTime.dwHighDateTime = uliCurTime.HighPart;
  91. ftStartTime.dwLowDateTime = uliCurTime.LowPart;
  92. SYSTEMTIME stUTC, stLocal;
  93. FileTimeToSystemTime(&ftStartTime, &stUTC);
  94. char temp[22];
  95. SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
  96. sprintf_s(temp, 22, "%d-%02d-%02d %02d:%02d:%02d",
  97. stLocal.wYear, stLocal.wMonth, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
  98. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OSStartTime: %s", temp);
  99. systemBootTime.FromSystemTime(stLocal);
  100. fSuc = TRUE;
  101. }
  102. Cleanup:
  103. Status = PdhRemoveCounter(hcElapsedTimeCount);
  104. if (Query) {
  105. PdhCloseQuery(Query);
  106. }
  107. return fSuc;
  108. #else
  109. ///*TODO(80374374@3/7/2023): */
  110. return FALSE;
  111. #endif //_MSC_VER
  112. }
  113. }//system
  114. namespace Comm
  115. {
  116. /** Rely on ${OTHER_LIB_BASE_DIR}/libpublicFun and library target libpublicFun*/
  117. struct LogNotiyMessageStruct
  118. {
  119. std::string Reason;
  120. std::string ErrMsg;
  121. std::string RebootTime;
  122. LogNotiyMessageStruct(const CSimpleStringA& reason, const CSimpleStringA& errmsg, const CSimpleStringA& rebootTime)
  123. :Reason(reason.GetData()), ErrMsg(reason.GetData()), RebootTime(reason.GetData())
  124. {
  125. }
  126. LogNotiyMessageStruct(const std::string& reason, const std::string& errmsg, const std::string& rebootTime)
  127. :Reason(reason), ErrMsg(errmsg), RebootTime(rebootTime)
  128. {
  129. }
  130. LogNotiyMessageStruct(const char* reason, const char* errmsg, char* rebootTime)
  131. :Reason(reason), ErrMsg(errmsg), RebootTime(rebootTime)
  132. {
  133. }
  134. LogNotiyMessageStruct(const char* errmsg)
  135. :Reason(""), ErrMsg(errmsg), RebootTime("")
  136. {
  137. }
  138. std::string ToJsonString() const {
  139. std::map<std::string, std::string> srcData;
  140. srcData.insert(std::make_pair("reason", Reason));
  141. srcData.insert(std::make_pair("errmsg", ErrMsg));
  142. srcData.insert(std::make_pair("rebootTime", RebootTime));
  143. auto ret = generateJsonStr(srcData);
  144. if (ret.first) {
  145. return ret.second;
  146. } else {
  147. return ErrMsg;
  148. }
  149. }
  150. };
  151. struct LogCommNotiyStruct
  152. {
  153. NotifyLevelEnum eLevel;
  154. ErrorCodeEnum eSysCode;
  155. DWORD dwUserCode;
  156. LogCommNotiyStruct() :eLevel(Notify_None), eSysCode(Error_Unexpect), dwUserCode(0) {}
  157. LogCommNotiyStruct(NotifyLevelEnum level, ErrorCodeEnum errorCode, DWORD dwUserCode)
  158. :eLevel(level), eSysCode(errorCode), dwUserCode(dwUserCode) {}
  159. LogCommNotiyStruct(NotifyLevelEnum level, ErrorCodeEnum errorCode)
  160. :eLevel(level), eSysCode(errorCode), dwUserCode(0)
  161. { }
  162. virtual ~LogCommNotiyStruct() {}
  163. virtual void Notify(const LogNotiyMessageStruct& notifyMessage)
  164. {
  165. LogNotify(eLevel, eSysCode, dwUserCode, notifyMessage.ToJsonString().c_str());
  166. }
  167. };
  168. struct LogInfoNotiyStruct : public LogCommNotiyStruct
  169. {
  170. LogInfoNotiyStruct(ErrorCodeEnum errorCode, DWORD dwUserCode)
  171. :LogCommNotiyStruct(Notify_Info, errorCode, dwUserCode){}
  172. ~LogInfoNotiyStruct() {}
  173. };
  174. struct LogWarnNotiyStruct : public LogCommNotiyStruct
  175. {
  176. LogWarnNotiyStruct(ErrorCodeEnum errorCode, DWORD dwUserCode)
  177. :LogCommNotiyStruct(Notify_Warn, errorCode, dwUserCode)
  178. {
  179. }
  180. ~LogWarnNotiyStruct() {}
  181. };
  182. struct LogErrorNotiyStruct : public LogCommNotiyStruct
  183. {
  184. LogErrorNotiyStruct(ErrorCodeEnum errorCode, DWORD dwUserCode)
  185. :LogCommNotiyStruct(Notify_Error, errorCode, dwUserCode)
  186. {
  187. }
  188. ~LogErrorNotiyStruct() {}
  189. };
  190. static BOOL IsFirsRunAppAfterSystemBoot(CEntityBase* pEntity)
  191. {
  192. BOOL result(FALSE);
  193. CSystemRunInfo runInfo = { 0 };
  194. ErrorCodeEnum ec = pEntity->GetFunction()->GetSystemRunInfo(runInfo);
  195. if (ec == Error_Succeed) {
  196. CBootInfo bootInfo = { 0 };
  197. CSmallDateTime dateTime;
  198. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("systemRunInfo time: %s", (LPCTSTR)runInfo.tmStart.ToTimeString());
  199. ec = pEntity->GetFunction()->GetRebootInfo(/*runInfo.tmStart*/dateTime, bootInfo);
  200. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("bootInfor time: %s", (LPCTSTR)bootInfo.tmStart.ToTimeString());
  201. CSmallDateTime systemBootTime;
  202. const BOOL bRet = System::GetSystemBootTime(systemBootTime);
  203. if (bRet && systemBootTime > bootInfo.tmStart) {
  204. result = TRUE;
  205. }
  206. }
  207. return result;
  208. }
  209. inline
  210. CSimpleStringA GetCurrMachineType(CEntityBase* pEntity)
  211. {
  212. CSystemStaticInfo sysInfo;
  213. pEntity->GetFunction()->GetSystemStaticInfo(sysInfo);
  214. return sysInfo.strMachineType;
  215. }
  216. inline
  217. CSimpleStringA GetCurrEntityConfigPath(CEntityBase* pEntity)
  218. {
  219. CSimpleStringA strConfigDir(true);
  220. pEntity->GetFunction()->GetPath("cfg", strConfigDir);
  221. CSimpleStringA result(strConfigDir + SPLIT_SLASH_STR + pEntity->GetEntityName() + ".ini");
  222. Dbg("config path: %s", result.GetData());
  223. return result;
  224. }
  225. enum Site
  226. {
  227. CMB_UNKNOWN,
  228. CMB_LIB, /** 行内大堂*/
  229. CMB_FLB, /** 离行机器*/
  230. };
  231. #define SITE_ENUM_TYPE(MACRO) \
  232. MACRO(LIB)\
  233. MACRO(FLB)
  234. #define ENUM_MAP_CONVERT(elem) \
  235. if (streq(lpcszSiteName, "CMB."#elem) == 0) return CMB_##elem;
  236. /*!
  237. * convert cmb site name to enum type.
  238. */
  239. static Site Str2Site(LPCSTR lpcszSiteName)
  240. {
  241. if (lpcszSiteName == NULL || strlen(lpcszSiteName) == 0)
  242. return CMB_UNKNOWN;
  243. SITE_ENUM_TYPE(ENUM_MAP_CONVERT)
  244. return CMB_UNKNOWN;
  245. }
  246. #undef ENUM_MAP_CONVERT
  247. #define ENUM_MAP_CONVERT(elem) case CMB_##elem: return "CMB."#elem;
  248. static LPCSTR Site2Str(Site site)
  249. {
  250. switch (site) {
  251. SITE_ENUM_TYPE(ENUM_MAP_CONVERT)
  252. default:
  253. break;
  254. }
  255. return "Unkown";
  256. }
  257. enum What
  258. {
  259. RVC_UNKNOWN,
  260. RVC_Stand2S, /** 落地式大机*/
  261. RVC_PAD, /** PAD*/
  262. RVC_Desk2S, /** 低柜双屏*/
  263. RVC_CardStore, /** 卡库*/
  264. RVC_CardPrinter, /** 制卡机*/
  265. RVC_Stand1SPlus /** 单屏大机*/
  266. };
  267. #define MACHINE_ENUM_TYPE(MACRO) \
  268. MACRO(Stand2S)\
  269. MACRO(PAD)\
  270. MACRO(Desk2S)\
  271. MACRO(CardStore)\
  272. MACRO(CardPrinter)\
  273. MACRO(Stand1SPlus)
  274. #undef ENUM_MAP_CONVERT
  275. #define ENUM_MAP_CONVERT(elem) \
  276. if (streq(lpcszTypeName, "RVC."#elem) == 0) return RVC_##elem;
  277. /*!
  278. * convert cmb site name to enum type.
  279. */
  280. static What Str2Type(LPCSTR lpcszTypeName)
  281. {
  282. if (lpcszTypeName == NULL || strlen(lpcszTypeName) == 0)
  283. return RVC_UNKNOWN;
  284. MACHINE_ENUM_TYPE(ENUM_MAP_CONVERT)
  285. return RVC_UNKNOWN;
  286. }
  287. #undef ENUM_MAP_CONVERT
  288. #define ENUM_MAP_CONVERT(elem) case RVC_##elem: return "RVC."#elem;
  289. static LPCSTR Type2Str(What what)
  290. {
  291. switch (what) {
  292. MACHINE_ENUM_TYPE(ENUM_MAP_CONVERT)
  293. default:
  294. break;
  295. }
  296. return "Unkown";
  297. }
  298. struct TerminalMachineInfo
  299. {
  300. Site site;
  301. What type;
  302. struct {
  303. WORD minor;
  304. WORD major;
  305. } gen;
  306. };
  307. inline TerminalMachineInfo GetTerminalMachineInfo(CEntityBase* pEntity)
  308. {
  309. CSystemStaticInfo sysInfo;
  310. TerminalMachineInfo termInfo;
  311. pEntity->GetFunction()->GetSystemStaticInfo(sysInfo);
  312. termInfo.site = Str2Site(sysInfo.strSite);
  313. termInfo.type = Str2Type(sysInfo.strMachineType);
  314. termInfo.gen.major = sysInfo.MachineVersion.GetMajor();
  315. termInfo.gen.minor = sysInfo.MachineVersion.GetMinor();
  316. return termInfo;
  317. }
  318. inline ULLINT RVCGetTickCount()
  319. {
  320. #ifdef RVC_OS_WIN
  321. return GetTickCount64();
  322. #else
  323. struct timespec ts;
  324. clock_gettime(CLOCK_MONOTONIC, &ts);
  325. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  326. #endif // RVC_OS_WIN
  327. }
  328. } // comm
  329. namespace Util
  330. {
  331. static int StrBuf2HexBuf(const char* strBuf, PBYTE* hexBuf)
  332. {
  333. int len = strlen(strBuf);
  334. if (len == 0 || len % 2 != 0)
  335. return 0;
  336. BYTE* buf = new BYTE[len / 2];
  337. if (buf == NULL)
  338. return 0;
  339. int j = 0;
  340. for (int i = 0; i < len;) {
  341. int tmpVal;
  342. sscanf(strBuf + i, "%2X", &tmpVal);
  343. buf[j] = tmpVal;
  344. i += 2;
  345. j++;
  346. }
  347. *hexBuf = buf;
  348. return j;
  349. }
  350. static int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
  351. {
  352. char* tmpStr = *strBuf;
  353. DWORD count = 0;
  354. for (DWORD i = 0; i < len; ++i) {
  355. sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
  356. count += 2;
  357. }
  358. return 0;
  359. }
  360. //tips: need to delete result char*
  361. static char* ConvertBytesToHexStr(BYTE* pBuf, int nLen)
  362. {
  363. char* pRet = (char*)new char[nLen * 2 + 1];
  364. memset(pRet, '\0', nLen * 2 + 1);
  365. char* p = pRet;
  366. for (int i = 0; i < nLen; i++) {
  367. BYTE b = pBuf[i];
  368. BYTE l = (b >> 4) & 0x0F;
  369. if (l >= 10)
  370. *p = l - 10 + 'a';
  371. else
  372. *p = l + '0';
  373. p++;
  374. BYTE r = b & 0x0F;
  375. if (r >= 10)
  376. *p = r - 10 + 'a';
  377. else
  378. *p = r + '0';
  379. p++;
  380. }
  381. return pRet;
  382. }
  383. static char HexChar2Dec(char c)
  384. {
  385. if ((c >= '0') && (c <= '9'))
  386. return c - '0';
  387. else if ((c >= 'A') && (c <= 'F'))
  388. return c - 'A' + 10;
  389. else if ((c >= 'a') && (c <= 'f'))
  390. return c - 'a' + 10;
  391. else
  392. return 0x10;
  393. }
  394. static int Hex2Int(char* s)
  395. {
  396. int ret = 0;
  397. while (*s) {
  398. int t = HexChar2Dec(*s);
  399. ret = ret * 16 + t;
  400. ++s;
  401. }
  402. return ret;
  403. }
  404. static void ConvAscii(unsigned char* Hex, char* Ascii, unsigned int len)
  405. {
  406. const unsigned char hextable[] = { "0123456789ABCDEF" };
  407. unsigned int i;
  408. for (i = 0; i < len; i++) {
  409. Ascii[2 * i] = hextable[Hex[i] >> 4];
  410. Ascii[2 * i + 1] = hextable[Hex[i] & 0xf];
  411. }
  412. Ascii[2 * len] = 0;
  413. }
  414. static void AsciiToHex(char* Ascii, unsigned char* Hex, int len)
  415. {
  416. int i;
  417. unsigned char ch;
  418. for (i = 0; i < len; i = i + 2) {
  419. ch = (Ascii[i] & 0xf);
  420. if (Ascii[i] > '9')
  421. ch += 9;
  422. Hex[i / 2] = ch << 4;
  423. ch = Ascii[i + 1] & 0xf;
  424. if (Ascii[i + 1] > '9')
  425. ch += 9;
  426. Hex[i / 2] += ch;
  427. }
  428. }
  429. static bool IsNoStr(const char* str)
  430. {
  431. int len = strlen(str);
  432. if (len == 0)
  433. return true;
  434. for (int i = 0; i < len; ++i) {
  435. if (*(str + i) != ' ')
  436. return false;
  437. }
  438. return true;
  439. }
  440. static DWORD GetDuration(const SYSTEMTIME& time1, const SYSTEMTIME& time2)
  441. {
  442. #if defined(RVC_OS_WIN)
  443. ULARGE_INTEGER fTime1;/*FILETIME*/
  444. ULARGE_INTEGER fTime2;/*FILETIME*/
  445. SystemTimeToFileTime(&time1, (FILETIME*)&fTime1);
  446. SystemTimeToFileTime(&time2, (FILETIME*)&fTime2);
  447. unsigned __int64 dft = fTime2.QuadPart - fTime1.QuadPart;
  448. return DWORD(dft / 10000);
  449. #else
  450. ///**TODO(Gifur@9/3/2021): Bug 不能这样简单的实现,考虑凌晨,切换月份甚至是年份 */
  451. DWORD s1, s2;
  452. s1 = (time1.wMinute * 60 + time1.wSecond) * 1000 + time1.wMilliseconds;
  453. s2 = (time2.wMinute * 60 + time2.wSecond) * 1000 + time2.wMilliseconds;
  454. return s2 - s1;
  455. #endif //RVC_OS_WIN
  456. }
  457. static std::string formatTime(const SYSTEMTIME& time)
  458. {
  459. char tBuf[1024] = "";
  460. sprintf(tBuf, "%04u-%02u-%02u %02u:%02u:%02u:%03u", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
  461. return tBuf;
  462. }
  463. static bool ShellExecute(const std::string& cmd, std::string& succResult, std::string& errResult)
  464. {
  465. #if defined(_MSC_VER)
  466. ///*TODO(80374374@3/7/2023): */
  467. return false;
  468. #else
  469. char buf_ps[1024];
  470. char ps[1024] = { 0 };
  471. char result[2049] = { 0 };
  472. FILE* ptr;
  473. strcpy(ps, cmd.c_str());
  474. succResult = errResult = "";
  475. if ((ptr = popen(ps, "r")) != NULL) {
  476. while (fgets(buf_ps, 1024, ptr) != NULL) {
  477. if (strlen(result) + strlen(buf_ps) > 2048)
  478. break;
  479. strcat(result, buf_ps);
  480. }
  481. pclose(ptr);
  482. const int len = strlen(result);
  483. for (int i = len - 1; i >= 0 && (result[i] == '\r' || result[i] == '\n'); --i) {
  484. result[i] = '\0';
  485. }
  486. succResult = result;
  487. return true;
  488. } else {
  489. sprintf(result, "popen %s error: %d", ps, errno);
  490. errResult = result;
  491. return false;
  492. }
  493. #endif //_MSC_VER
  494. }
  495. static CSimpleStringA generateConsumeTimeJson(const CSimpleStringA& entityName, const CSimpleStringA& startTime, int cost)
  496. {
  497. return CSimpleStringA::Format("[{\"name\":\"%s\",\"time\":\"%s\",\"cost\":%d}]", entityName.GetData(), startTime.GetData(), cost);
  498. }
  499. #if defined(_MSC_VER)
  500. static char* ConvertUtf8ToGBK(const char* strUtf8)
  501. {
  502. int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0);
  503. WCHAR* wszGBK = new WCHAR[len + 1];
  504. memset(wszGBK, 0, len * 2 + 2);
  505. MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, wszGBK, len);
  506. len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
  507. char* szGBK = new char[len + 1];
  508. memset(szGBK, 0, len + 1);
  509. WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
  510. delete[] wszGBK;
  511. return szGBK;
  512. }
  513. static void ConvertUtf8ToGBK(std::string& str)
  514. {
  515. char* dst = ConvertUtf8ToGBK(str.c_str());
  516. str = dst;
  517. delete[] dst;
  518. }
  519. static char* ConvertGBKToUtf8(const char* gbk, int* n)
  520. {
  521. int len = MultiByteToWideChar(CP_ACP, 0, gbk, -1, NULL, 0);
  522. WCHAR* wszGBK = new WCHAR[len + 1];
  523. memset(wszGBK, 0, len * 2 + 2);
  524. MultiByteToWideChar(CP_ACP, 0, gbk, -1, wszGBK, len);
  525. len = WideCharToMultiByte(CP_UTF8, 0, wszGBK, -1, NULL, 0, NULL, NULL);
  526. char* szUtf8 = new char[len + 1];
  527. memset(szUtf8, 0, len + 1);
  528. WideCharToMultiByte(CP_UTF8, 0, wszGBK, -1, szUtf8, len, NULL, NULL);
  529. delete[] wszGBK;
  530. *n = len - 1;
  531. return szUtf8;
  532. }
  533. static void ConvertGBKToUtf8(std::string& str)
  534. {
  535. int len = 0;
  536. char* dst = ConvertGBKToUtf8(str.c_str(), &len);
  537. str = dst;
  538. delete[] dst;
  539. }
  540. #endif //_MSC_VER
  541. static std::string W2S(const std::wstring wstr)
  542. {
  543. #if defined(_MSC_VER)
  544. char* str = NULL;
  545. int n = ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
  546. if (n > 0) {
  547. str = new char[n + 1];
  548. if (str == NULL) {
  549. return std::string();
  550. }
  551. std::memset(str, 0, sizeof(char) * (n + 1));
  552. ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], n, NULL, NULL);
  553. std::string strr(str);
  554. delete str;
  555. return strr;
  556. }
  557. #else
  558. ///*TODO(80374374@3/7/2023): */
  559. #endif //_MSC_VER
  560. return std::string();
  561. }
  562. static std::wstring S2W(const std::string str)
  563. {
  564. #if defined(_MSC_VER)
  565. wchar_t* wstr = NULL;
  566. int n = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
  567. if (n > 0) {
  568. wstr = new wchar_t[n + 1];
  569. if (wstr == NULL) {
  570. return std::wstring();
  571. }
  572. std::memset(wstr, 0, (n + 1) * sizeof(wchar_t));
  573. ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wstr[0], n);
  574. std::wstring strr(wstr);
  575. delete wstr;
  576. return strr;
  577. }
  578. #else
  579. ///*TODO(80374374@3/7/2023): */
  580. #endif //_MSC_VER
  581. return std::wstring();
  582. }
  583. } //namespace Util
  584. namespace Net{
  585. struct NetworkAdapterItem
  586. {
  587. int idx;
  588. std::string friend_name;
  589. std::string adapter_name;
  590. std::string description;
  591. std::string ip;
  592. std::string mask;
  593. std::string mac;
  594. std::string gateway;
  595. std::string dhcp;
  596. bool is_physical;
  597. DWORD type;
  598. int operStatus;
  599. bool isLocal;
  600. NetworkAdapterItem() :idx(0), friend_name(""), adapter_name(""), description(""), ip(""), mask(""),
  601. mac(""), gateway(""), dhcp(""), is_physical(true), type(0), operStatus(0), isLocal(false)
  602. {
  603. }
  604. };
  605. static bool IsLocalAdapter(const std::string& name)
  606. {
  607. #if defined(_MSC_VER)
  608. if (name.length() <= 0) {
  609. return false;
  610. }
  611. std::string key("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}");
  612. HKEY h_sub_key = NULL;
  613. LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, KEY_READ, &h_sub_key);
  614. if (ret != 0) {
  615. return false;
  616. }
  617. std::ostringstream str;
  618. str << name << "\\Connection";
  619. HKEY h_local_key = NULL;
  620. ret = RegOpenKeyEx(h_sub_key, str.str().c_str(), 0, KEY_READ, &h_local_key);
  621. if (0 != ret) {
  622. RegCloseKey(h_sub_key);
  623. return false;
  624. }
  625. DWORD type = REG_SZ;
  626. TCHAR buf[250];
  627. DWORD buf_size = 250;
  628. ret = RegQueryValueEx(h_local_key, "PnPInstanceId", 0, &type, (BYTE*)(buf), &buf_size);
  629. RegCloseKey(h_sub_key);
  630. RegCloseKey(h_local_key);
  631. if (0 != ret) {
  632. return false;
  633. }
  634. if (0 == strnicmp(buf, "PCI", strlen("PCI")) || 0 == strnicmp(buf, "USB", strlen("USB"))) {
  635. return true;
  636. }
  637. return false;
  638. #else
  639. ///*TODO(80374374@3/7/2023): */
  640. return true;
  641. #endif //_MSC_VER
  642. }
  643. static ErrorCodeEnum GetINETMacAddresses(CAutoArray<NetworkAdapterItem>& netLists)
  644. {
  645. #if defined(RVC_OS_WIN)
  646. PIP_ADAPTER_ADDRESSES pAddresses = NULL;
  647. ULONG family = AF_INET;
  648. ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
  649. ULONG outBufLen = sizeof(IP_ADAPTER_ADDRESSES);
  650. // Make an initial call to GetAdaptersAddresses to get the
  651. // size needed into the outBufLen variable
  652. if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW) {
  653. pAddresses = static_cast<PIP_ADAPTER_ADDRESSES>(HeapAlloc(GetProcessHeap(), 0, outBufLen));
  654. }
  655. if (NULL == pAddresses) {
  656. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pAddresses = NULL");
  657. return Error_Unexpect;
  658. }
  659. DWORD dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
  660. /* MACAddresses vAddress;*/
  661. if (dwRetVal != ERROR_SUCCESS) {
  662. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("dwRetVal = %d", dwRetVal);
  663. return Error_Unexpect;
  664. }
  665. PIP_ADAPTER_ADDRESSES pFirst = pAddresses;
  666. while (pAddresses) {
  667. if (pAddresses->FirstUnicastAddress &&
  668. pAddresses->IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
  669. pAddresses->OperStatus == IfOperStatusUp &&
  670. pAddresses->FirstUnicastAddress->Address.lpSockaddr->sa_family == AF_INET
  671. ) {
  672. NetworkAdapterItem netItem;
  673. BYTE* pa = pAddresses->PhysicalAddress;
  674. if (!pa) {
  675. pAddresses = pAddresses->Next ? pAddresses->Next : NULL;
  676. continue;
  677. }
  678. CSimpleStringA strFriendlyName(true);
  679. CSimpleStringA strDescription(true);
  680. DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->FriendlyName, -1, NULL, 0, NULL, FALSE);
  681. char* psText = new char[dwNum];
  682. if (psText != NULL) {
  683. WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->FriendlyName, -1, psText, dwNum, NULL, FALSE);
  684. strFriendlyName = psText;
  685. delete[] psText;
  686. }
  687. dwNum = WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->Description, -1, NULL, 0, NULL, FALSE);
  688. psText = new char[dwNum];
  689. if (psText != NULL) {
  690. WideCharToMultiByte(CP_OEMCP, NULL, pAddresses->Description, -1, psText, dwNum, NULL, FALSE);
  691. strDescription = psText;
  692. delete[] psText;
  693. }
  694. char bAddressBytes[MACSESION];
  695. int bAddressInt[MACSESION];
  696. memset(bAddressBytes, 0, MACSESION);
  697. size_t nAddressSize = pAddresses->PhysicalAddressLength;
  698. memcpy(bAddressBytes, pa, (nAddressSize < MACSESION ? nAddressSize : MACSESION));
  699. for (int i = 0; i < MACSESION; ++i) {
  700. bAddressInt[i] = bAddressBytes[i];
  701. bAddressInt[i] &= 0x000000ff; // avoid "ff" leading bytes when "char" is lager then 0x7f
  702. }
  703. CSimpleStringA tmpmac = CSimpleStringA::Format("%02x:%02x:%02x:%02x:%02x:%02x",
  704. bAddressInt[0],
  705. bAddressInt[1],
  706. bAddressInt[2],
  707. bAddressInt[3],
  708. bAddressInt[4],
  709. bAddressInt[5]);
  710. sockaddr_in* sa_in = (sockaddr_in*)pAddresses->FirstUnicastAddress->Address.lpSockaddr;
  711. char buf_addr[100] = { 0 };
  712. CSimpleStringA tmpip = CSimpleStringA(inet_ntop(AF_INET, &(sa_in->sin_addr), buf_addr, 100));
  713. if (tmpip.Compare("127.0.0.1") == 0 && tmpmac.Compare("00:00:00:00:00:00") == 0) {
  714. //skip
  715. }
  716. else
  717. {
  718. netItem.friend_name = strFriendlyName;
  719. netItem.description = strDescription;
  720. netItem.mac = tmpmac;
  721. netItem.ip = tmpip;
  722. netItem.operStatus = (pAddresses->OperStatus == IfOperStatusUp) ? 1 : 0;//stay the same value with Linux/UOS
  723. netItem.type = pAddresses->IfType;
  724. netLists.Append(&netItem, 0, 1);
  725. }
  726. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s, %s: OperStatus: %d, IfType = %d, ip=%s, mac=%s"
  727. , strFriendlyName.GetData(), strDescription.GetData()
  728. , pAddresses->OperStatus, pAddresses->IfType, tmpip.GetData(), tmpmac.GetData());
  729. }
  730. pAddresses = pAddresses->Next ? pAddresses->Next : NULL;
  731. }
  732. HeapFree(GetProcessHeap(), 0, pFirst);
  733. return Error_Succeed;
  734. #else
  735. std::map<std::string, std::string> inteIPs;
  736. std::map<std::string, std::string> inteMacs;
  737. std::map<std::string, int> inteStatus;
  738. char buf[512];
  739. toolkit_interface_address_t* info;
  740. int count, i;
  741. toolkit_interface_addresses(&info, &count);
  742. i = count;
  743. Dbg("Number of interfaces: %d", count);
  744. while (i--) {
  745. toolkit_interface_address_t interface = info[i];
  746. Dbg("Name: %s", interface.name);
  747. Dbg("Internal? %s", interface.is_internal ? "Yes" : "No");
  748. if (interface.address.address4.sin_family == AF_INET) {
  749. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  750. Dbg("IPv4 address: %s", buf);
  751. inteIPs[interface.name] = buf;
  752. }
  753. else if (interface.address.address4.sin_family == AF_INET6) {
  754. toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
  755. Dbg("IPv6 address: %s", buf);
  756. //inteIPs[interface.name] = buf;
  757. }
  758. }
  759. toolkit_free_interface_addresses(info, count);
  760. int fd, interface;
  761. struct ifreq bufIfreq[16];
  762. struct ifconf ifc;
  763. char mac[32] = { 0 };
  764. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
  765. int i = 0;
  766. ifc.ifc_len = sizeof(bufIfreq);
  767. ifc.ifc_buf = (caddr_t)bufIfreq;
  768. if (!ioctl(fd, SIOCGIFCONF, (char*)& ifc)) {
  769. interface = ifc.ifc_len / sizeof(struct ifreq);
  770. Dbg("interface num is %d", interface);
  771. while (i < interface) {
  772. Dbg("Name: %s", bufIfreq[i].ifr_name);
  773. if (!(ioctl(fd, SIOCGIFHWADDR, (char*)& bufIfreq[i]))) {
  774. sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X",
  775. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[0],
  776. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[1],
  777. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[2],
  778. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[3],
  779. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[4],
  780. (unsigned char)bufIfreq[i].ifr_hwaddr.sa_data[5]);
  781. Dbg("HWaddr %s", mac);
  782. inteMacs[bufIfreq[i].ifr_name] = mac;
  783. }
  784. struct ethtool_value edata;
  785. edata.cmd = ETHTOOL_GLINK;
  786. edata.data = 0;
  787. bufIfreq[i].ifr_data = (char*)& edata;
  788. //oiltmp@20231026 只检测了以太网卡
  789. if (ioctl(fd, SIOCETHTOOL, (char*)& bufIfreq[i]) == -1) {
  790. //up down
  791. Dbg("Name: %s is down", bufIfreq[i].ifr_name);
  792. inteStatus[bufIfreq[i].ifr_name] = 0;
  793. }
  794. else
  795. {
  796. Dbg("Name: %s is up", bufIfreq[i].ifr_name);
  797. inteStatus[bufIfreq[i].ifr_name] = 1;
  798. }
  799. i++;
  800. }
  801. }
  802. close(fd);
  803. }
  804. std::map<std::string, std::string>::const_iterator map_it = inteIPs.begin();
  805. while (map_it != inteIPs.end()) {
  806. NetworkAdapterItem netItem;
  807. CSimpleStringA tmpip(map_it->second.c_str());
  808. CSimpleStringA tmpmac(true);
  809. CSimpleStringA tmpname(true);
  810. auto it = inteMacs.find(std::string(map_it->first));
  811. if (it != inteMacs.end()) {
  812. tmpmac = it->second.c_str();
  813. }
  814. if (tmpip.Compare("127.0.0.1") == 0 && tmpmac.Compare("00:00:00:00:00:00") == 0) {
  815. //skip
  816. }
  817. else {
  818. tmpname = map_it->first.c_str();
  819. netItem.friend_name = "";//oiltmp
  820. netItem.description = tmpname;
  821. netItem.mac = tmpmac;
  822. netItem.ip = tmpip;
  823. netItem.operStatus = inteStatus[map_it->first];
  824. netItem.type = 6;//oiltest haven't find the in linux
  825. netLists.Append(&netItem, 0, 1);
  826. }
  827. ++map_it;
  828. }
  829. return Error_Succeed;
  830. #endif //RVC_OS_WIN
  831. }
  832. static std::vector<NetworkAdapterItem> GetNetAdapterItems()
  833. {
  834. std::vector<NetworkAdapterItem> results;
  835. #if defined(_MSC_VER)
  836. ULONG flags = GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_INCLUDE_GATEWAYS;
  837. ULONG family = AF_UNSPEC;
  838. PIP_ADAPTER_ADDRESSES address_ptr = nullptr;
  839. ULONG out_buf_len = 15000;
  840. DWORD ret_val = 0;
  841. PIP_ADAPTER_ADDRESSES cur_addr_ptr = nullptr;
  842. PIP_ADAPTER_UNICAST_ADDRESS unicast_ptr = nullptr;
  843. PIP_ADAPTER_ANYCAST_ADDRESS anycast_ptr = nullptr;
  844. PIP_ADAPTER_MULTICAST_ADDRESS multicast_ptr = nullptr;
  845. PIP_ADAPTER_DNS_SERVER_ADDRESS dns_server_ptr = nullptr;
  846. IP_ADAPTER_PREFIX* prefix_ptr = nullptr;
  847. do {
  848. address_ptr = (PIP_ADAPTER_ADDRESSES)malloc(out_buf_len);
  849. if (address_ptr == nullptr) {
  850. return results;
  851. }
  852. ret_val = GetAdaptersAddresses(family, flags, NULL, address_ptr, &out_buf_len);
  853. if (ERROR_BUFFER_OVERFLOW == ret_val) {
  854. free(address_ptr);
  855. }
  856. } while (ret_val == ERROR_BUFFER_OVERFLOW);
  857. if (NO_ERROR == ret_val) {
  858. cur_addr_ptr = address_ptr;
  859. while (cur_addr_ptr) {
  860. std::string description = Util::W2S(cur_addr_ptr->Description);
  861. bool is_local = IsLocalAdapter(cur_addr_ptr->AdapterName);
  862. std::string friend_name = Util::W2S(cur_addr_ptr->FriendlyName);
  863. NetworkAdapterItem item;
  864. item.idx = cur_addr_ptr->IfIndex;
  865. item.adapter_name = cur_addr_ptr->AdapterName;
  866. item.description = description;
  867. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)(">>>>>>>>>>>>>>> friendly name: %s", friend_name.c_str());
  868. item.friend_name = friend_name;
  869. if (is_local) {
  870. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Adapter Desc: %s", description.c_str());
  871. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Adapter Name: %s", cur_addr_ptr->AdapterName);
  872. item.isLocal = true;
  873. }
  874. if (cur_addr_ptr->PhysicalAddressLength != 0) {
  875. char sz_addr[32] = { 0 };
  876. std::stringstream str;
  877. for (int i = 0; i < (int)cur_addr_ptr->PhysicalAddressLength; i++) {
  878. if (i == ((int)cur_addr_ptr->PhysicalAddressLength - 1))
  879. sprintf_s(sz_addr, "%.2X", (int)cur_addr_ptr->PhysicalAddress[i]);
  880. else
  881. sprintf_s(sz_addr, "%.2X:", (int)cur_addr_ptr->PhysicalAddress[i]);
  882. str << sz_addr;
  883. }
  884. item.mac = str.str();
  885. }
  886. item.type = cur_addr_ptr->IfType;
  887. item.operStatus = cur_addr_ptr->OperStatus;
  888. switch (cur_addr_ptr->IfType) {
  889. case MIB_IF_TYPE_OTHER:
  890. break;
  891. case MIB_IF_TYPE_ETHERNET:
  892. break;
  893. case MIB_IF_TYPE_TOKENRING:
  894. break;
  895. case MIB_IF_TYPE_FDDI:
  896. break;
  897. case MIB_IF_TYPE_PPP:
  898. break;
  899. case MIB_IF_TYPE_LOOPBACK:
  900. break;
  901. case MIB_IF_TYPE_SLIP:
  902. break;
  903. case IF_TYPE_IEEE80211:
  904. break;
  905. case IF_TYPE_WWANPP:
  906. case IF_TYPE_WWANPP2:
  907. //WWAN devices
  908. break;
  909. default:
  910. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unknown type %d", cur_addr_ptr->IfType);
  911. break;
  912. }
  913. unicast_ptr = cur_addr_ptr->FirstUnicastAddress;
  914. while (unicast_ptr) {
  915. char ip[120] = { 0 };
  916. if (AF_INET == unicast_ptr->Address.lpSockaddr->sa_family) {
  917. inet_ntop(PF_INET, &((sockaddr_in*)unicast_ptr->Address.lpSockaddr)->sin_addr, ip, sizeof(ip));
  918. } else if (AF_INET6 == unicast_ptr->Address.lpSockaddr->sa_family) {
  919. inet_ntop(PF_INET6, &((sockaddr_in*)unicast_ptr->Address.lpSockaddr)->sin_addr, ip, sizeof(ip));
  920. }
  921. unicast_ptr = unicast_ptr->Next;
  922. }
  923. if (cur_addr_ptr->Dhcpv4Server.lpSockaddr) {
  924. char ip[120] = { 0 };
  925. if (AF_INET == cur_addr_ptr->Dhcpv4Server.lpSockaddr->sa_family) {
  926. inet_ntop(PF_INET, &((sockaddr_in*)cur_addr_ptr->Dhcpv4Server.lpSockaddr)->sin_addr, ip, sizeof(ip));
  927. } else if (AF_INET6 == cur_addr_ptr->Dhcpv4Server.lpSockaddr->sa_family) {
  928. inet_ntop(PF_INET6, &((sockaddr_in*)cur_addr_ptr->Dhcpv4Server.lpSockaddr)->sin_addr, ip, sizeof(ip));
  929. }
  930. item.dhcp = ip;
  931. }
  932. dns_server_ptr = cur_addr_ptr->FirstDnsServerAddress;
  933. while (dns_server_ptr) {
  934. char ip[120] = { 0 };
  935. if (AF_INET == dns_server_ptr->Address.lpSockaddr->sa_family) {
  936. inet_ntop(PF_INET, &((sockaddr_in*)dns_server_ptr->Address.lpSockaddr)->sin_addr, ip, sizeof(ip));
  937. } else if (AF_INET6 == dns_server_ptr->Address.lpSockaddr->sa_family) {
  938. inet_ntop(PF_INET6, &((sockaddr_in*)dns_server_ptr->Address.lpSockaddr)->sin_addr, ip, sizeof(ip));
  939. }
  940. dns_server_ptr = dns_server_ptr->Next;
  941. }
  942. auto gateway_ptr = cur_addr_ptr->FirstGatewayAddress;
  943. while (gateway_ptr) {
  944. char ip[120] = { 0 };
  945. if (AF_INET == gateway_ptr->Address.lpSockaddr->sa_family) {
  946. inet_ntop(PF_INET, &((sockaddr_in*)gateway_ptr->Address.lpSockaddr)->sin_addr, ip, sizeof(ip));
  947. } else if (AF_INET6 == gateway_ptr->Address.lpSockaddr->sa_family) {
  948. inet_ntop(PF_INET6, &((sockaddr_in*)gateway_ptr->Address.lpSockaddr)->sin_addr, ip, sizeof(ip));
  949. }
  950. gateway_ptr = gateway_ptr->Next;
  951. }
  952. results.push_back(item);
  953. cur_addr_ptr = cur_addr_ptr->Next;
  954. }
  955. } else {
  956. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("GetAdaptersAddresses failed with error: %d", ret_val);
  957. }
  958. free(address_ptr);
  959. #else
  960. ///*TODO(80374374@3/7/2023): */
  961. #endif //_MSC_VER
  962. return results;
  963. }
  964. static BOOL CheckLANConnectStatus()
  965. {
  966. #if defined(_MSC_VER)
  967. DWORD dwFlag(0);
  968. BOOL bRet = InternetGetConnectedState(&dwFlag, 0);
  969. if ((dwFlag & INTERNET_CONNECTION_CONFIGURED) == INTERNET_CONNECTION_CONFIGURED) {
  970. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Local system has a valid connection to the Internet, but it might or might not be currently connected.");
  971. }
  972. if ((dwFlag & INTERNET_CONNECTION_LAN) == INTERNET_CONNECTION_LAN) {
  973. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Local system uses a local area network to connect to the Internet.");
  974. }
  975. if ((dwFlag & INTERNET_CONNECTION_MODEM) == INTERNET_CONNECTION_MODEM) {
  976. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Local system uses a modem to connect to the Internet.");
  977. }
  978. if ((dwFlag & INTERNET_CONNECTION_MODEM_BUSY) == INTERNET_CONNECTION_MODEM_BUSY) {
  979. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("No longer used: INTERNET_CONNECTION_MODEM_BUSY");
  980. }
  981. if ((dwFlag & INTERNET_CONNECTION_OFFLINE) == INTERNET_CONNECTION_OFFLINE) {
  982. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Local system is in offline mode.");
  983. }
  984. if ((dwFlag & INTERNET_CONNECTION_PROXY) == INTERNET_CONNECTION_PROXY) {
  985. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Local system uses a proxy server to connect to the Internet.");
  986. }
  987. if ((dwFlag & INTERNET_RAS_INSTALLED) == INTERNET_RAS_INSTALLED) {
  988. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Local system has RAS installed.");
  989. }
  990. if (bRet) {
  991. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("InternetGetConnectedState Succeed: 0x%08X", dwFlag);
  992. } else {
  993. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("InternetGetConnectedState failed: 0x%08X, GLE=%u", dwFlag, GetLastError());
  994. }
  995. return bRet;
  996. #else
  997. return FALSE;
  998. #endif //_MSC_VER
  999. }
  1000. static std::string GetWWWInfoThroughDig(const std::string& wwwUrl)
  1001. {
  1002. std::string succStr, errStr;
  1003. std::string runStr("dig ");
  1004. runStr += wwwUrl;
  1005. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  1006. return succStr;
  1007. } else {
  1008. return std::string("ShellExecute(") + wwwUrl + ") failed";
  1009. }
  1010. }
  1011. }//Net
  1012. } // mod
  1013. } // sp
  1014. #endif //RVC_MOD_COMM_ENTITY_UTIL_HPP_