CommEntityUtil.hpp 34 KB

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