comm.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #include "comm.h"
  2. #include <cstdarg>
  3. #ifndef _WIN32
  4. #include <sys/ioctl.h>
  5. #include <sys/stat.h>
  6. #include <linux/hdreg.h>
  7. #include <sys/fcntl.h>
  8. #endif //NOT _WIN32
  9. #define MAX_PATH_SIZE 256
  10. void GetNewForm(const char* form, char* newForm) {
  11. int indexNum = 0;
  12. int acount = 0;
  13. newForm[0] = '{';
  14. for (int i = 0; i < strlen(form); i++)
  15. {
  16. //if((i-1 >= 0 && form[i]=='\\') || (i-1 < 0))
  17. if (form[i] == '%') {
  18. if (acount != 0)
  19. {
  20. newForm[++indexNum] = '"';
  21. if (acount % 2 != 0) {
  22. newForm[++indexNum] = ':';
  23. }
  24. else {
  25. newForm[++indexNum] = ',';
  26. }
  27. }
  28. newForm[++indexNum] = '"';
  29. acount++;
  30. }
  31. if (form[i] == ' ') continue;
  32. newForm[++indexNum] = form[i];
  33. }
  34. newForm[++indexNum] = '"';
  35. newForm[++indexNum] = '}';
  36. }
  37. string GetOutPutStr(const char* form, ...) {
  38. char* newForm = new char[strlen(form) * 3 + 5];
  39. memset(newForm, 0, strlen(form) * 3 + 5);
  40. if (strlen(form) < 2) {
  41. strcpy(newForm, "{\"\"}");
  42. }
  43. else {
  44. GetNewForm(form, newForm);
  45. }
  46. va_list vaList;
  47. va_start(vaList, form);
  48. #ifdef RVC_OS_WIN
  49. int acount = _vscprintf(newForm, vaList);
  50. #else
  51. int acount = vsnprintf(0, 0, newForm, vaList);
  52. va_end(vaList);
  53. va_start(vaList, form);
  54. #endif
  55. char* buf = new char[acount + 1];
  56. memset(buf, 0, acount + 1);
  57. vsprintf(buf, newForm, vaList);
  58. va_end(vaList);
  59. string ret;
  60. ret.assign(buf);
  61. delete buf;
  62. delete newForm;
  63. return ret;
  64. }
  65. int no_begin_with(const char strA[], const char strB[]) {
  66. if (strA == NULL) return 1;
  67. if (strB == NULL) return 1;
  68. if (strlen(strA) == 0 || strlen(strB) == 0 || strlen(strA) < strlen(strB)) return 1;
  69. const char* pA = strA;
  70. const char* pB = strB;
  71. while (*pA != '\0' && *pB != '\0') {
  72. if (*pA != *pB) return 1;
  73. pA++;
  74. pB++;
  75. }
  76. return 0;
  77. }
  78. int str2int(const string str, int& ret) {
  79. if (str.size() == 0) return 1;
  80. ret = 0;
  81. int symbol = 0;
  82. for (int i = 0; i < str.size(); i++) {
  83. if (i == 0 && str[i] == '-') {
  84. symbol = 1;
  85. continue;
  86. }
  87. if (i == 0 && str[i] == '+') continue;
  88. if (i == 0) {
  89. while (str[i] == '0' && i < str.size()) {
  90. ++i;
  91. }
  92. if (i == str.size()) return 0;
  93. }
  94. if (str[i] < '0' || str[i] >'9') return 2;
  95. ret += (str[i] - '0') * pow(10, str.size() - i - 1);
  96. }
  97. if (symbol) ret -= 2 * ret;
  98. return 0;
  99. }
  100. int str2int(const string str) {
  101. int ret;
  102. str2int(str, ret);
  103. return ret;
  104. }
  105. vector<string> split_str(string strA, string strB) {
  106. vector<string> ret;
  107. //cout << "ret:" << ret.size() << endl;
  108. if (strA.size() == 0 || strB.size() == 0) {
  109. return ret;
  110. }
  111. char* chA = new char[strA.size()];
  112. char* chB = new char[strB.size()];
  113. memset(chA, 0, sizeof(chA));
  114. memset(chB, 0, sizeof(chB));
  115. strcpy(chA, strA.c_str());
  116. strcpy(chB, strB.c_str());
  117. char* tmp = strtok(chA, chB);
  118. while (tmp) {
  119. //cout << "tmp:" << tmp << endl;
  120. ret.push_back(tmp);
  121. tmp = strtok(NULL, chB);
  122. }
  123. delete[] chA;
  124. delete[] chB;
  125. return ret;
  126. }
  127. int ip2byte(const string str, BYTE ip[]) {
  128. vector<string> ret = split_str(str, ".");
  129. vector<string>::iterator it = ret.begin();
  130. int index = 0;
  131. while (it != ret.end()) {
  132. int tmp = 0;
  133. if (str2int(*it, tmp)) return 1;
  134. else {
  135. if (tmp > 255) return 2;
  136. else {
  137. ip[index] = tmp;
  138. index++;
  139. }
  140. }
  141. it++;
  142. }
  143. return 0;
  144. }
  145. unsigned char Ch2Hex(char ch)
  146. {
  147. static const char* hex = "0123456789ABCDEF";
  148. for (unsigned char i = 0; i != 16; ++i)
  149. if (ch == hex[i])
  150. return i;
  151. return 0;
  152. }
  153. char* Hex2Str(const char* src, int& dstLen)
  154. {
  155. int i = 0;
  156. int cnt = 0;
  157. int len = strlen(src);
  158. unsigned char* d = new unsigned char[len];
  159. memset(d, 0, len);
  160. while (*src)
  161. {
  162. if (i & 1)
  163. {
  164. d[cnt++] |= Ch2Hex(*src);
  165. }
  166. else
  167. {
  168. d[cnt] = Ch2Hex(*src) << 4;
  169. }
  170. src++;
  171. i++;
  172. }
  173. dstLen = cnt;
  174. return (char*)d;
  175. }
  176. char* Str2Hex(const char* src, int srcLen)
  177. {
  178. string ret;
  179. static const char* hex = "0123456789ABCDEF";
  180. for (int i = 0; i != srcLen; ++i)
  181. {
  182. ret.push_back(hex[(src[i] >> 4) & 0xf]);
  183. ret.push_back(hex[src[i] & 0xf]);
  184. }
  185. char* tmp = new char[ret.length() + 1];
  186. memset(tmp, 0, ret.length() + 1);
  187. memcpy(tmp, ret.c_str(), ret.length());
  188. return tmp;
  189. }
  190. #ifdef __linux__
  191. int getIPFromLinux(char* ip) {
  192. //if (strlen(ip) < 15) return 1;
  193. struct ifaddrs* ifAddrStruct = NULL;
  194. void* tmpAddrPtr = NULL;
  195. getifaddrs(&ifAddrStruct);
  196. while (ifAddrStruct != NULL) {
  197. if (ifAddrStruct->ifa_addr->sa_family == AF_INET) {
  198. // is a valid IP4 Address
  199. tmpAddrPtr = &((struct sockaddr_in*)ifAddrStruct->ifa_addr)->sin_addr;
  200. char addressBuffer[INET_ADDRSTRLEN];
  201. inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
  202. //printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
  203. //99开头行内办公网,10开头行内业务网。规范出自《招商银行总行网络规范汇编(2017年版).pdf》
  204. if (!no_begin_with(addressBuffer, "99") || !no_begin_with(addressBuffer, "10")) {
  205. memset(ip, 0, sizeof(ip));
  206. strcpy(ip, addressBuffer);
  207. }
  208. else if (strlen(ip) == 0 && no_begin_with(addressBuffer, "127.0")) {
  209. memset(ip, 0, sizeof(ip));
  210. strcpy(ip, addressBuffer);
  211. }
  212. }
  213. /*
  214. else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6) { // check it is IP6
  215. // is a valid IP6 Address
  216. tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
  217. char addressBuffer[INET6_ADDRSTRLEN];
  218. inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
  219. printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
  220. }
  221. */
  222. ifAddrStruct = ifAddrStruct->ifa_next;
  223. }
  224. //printf("ip=%s\n", ip);
  225. return 0;
  226. }
  227. void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id)
  228. {
  229. cpu_id.c_str();
  230. std::ifstream ifs(file_name, std::ios::binary);
  231. if (!ifs.is_open())
  232. {
  233. return;
  234. }
  235. char line[4096] = { 0 };
  236. while (!ifs.eof())
  237. {
  238. ifs.getline(line, sizeof(line));
  239. if (!ifs.good())
  240. {
  241. break;
  242. }
  243. const char* cpu = strstr(line, match_words);
  244. if (NULL == cpu)
  245. {
  246. continue;
  247. }
  248. cpu += strlen(match_words);
  249. while ('\0' != cpu[0])
  250. {
  251. if (' ' != cpu[0])
  252. {
  253. cpu_id.push_back(cpu[0]);
  254. }
  255. ++cpu;
  256. }
  257. if (!cpu_id.empty())
  258. {
  259. break;
  260. }
  261. }
  262. ifs.close();
  263. }
  264. bool get_cpu_id_by_system(std::string& cpu_id, const string save_path)
  265. {
  266. //cpu_id.c_str();
  267. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  268. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  269. strcpy(dmidecode_result, save_path.c_str());
  270. strcat(dmidecode_result, "/.dmidecode_result.txt");
  271. char command[512] = { 0 };
  272. snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
  273. if (0 == system(command))
  274. {
  275. parse_cpu_id(dmidecode_result, "ID:", cpu_id);
  276. }
  277. unlink(dmidecode_result);
  278. return(!cpu_id.empty());
  279. }
  280. void parse_board_serial(const char* file_name, const char* match_words, std::string& board_serial)
  281. {
  282. board_serial.c_str();
  283. std::ifstream ifs(file_name, std::ios::binary);
  284. if (!ifs.is_open())
  285. {
  286. return;
  287. }
  288. char line[4096] = { 0 };
  289. while (!ifs.eof())
  290. {
  291. ifs.getline(line, sizeof(line));
  292. if (!ifs.good())
  293. {
  294. break;
  295. }
  296. const char* board = strstr(line, match_words);
  297. if (NULL == board)
  298. {
  299. continue;
  300. }
  301. board += strlen(match_words);
  302. while ('\0' != board[0])
  303. {
  304. if (' ' != board[0])
  305. {
  306. board_serial.push_back(board[0]);
  307. }
  308. ++board;
  309. }
  310. if ("None" == board_serial)
  311. {
  312. board_serial.clear();
  313. continue;
  314. }
  315. if (!board_serial.empty())
  316. {
  317. break;
  318. }
  319. }
  320. ifs.close();
  321. }
  322. bool get_board_serial_by_system(std::string& board_serial,const string save_path)
  323. {
  324. //board_serial.c_str();
  325. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  326. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  327. strcpy(dmidecode_result, save_path.c_str());
  328. strcat(dmidecode_result, "/.dmidecode_result.txt");
  329. char command[512] = { 0 };
  330. snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
  331. if (0 == system(command))
  332. {
  333. parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
  334. }
  335. else {
  336. return false;
  337. }
  338. unlink(dmidecode_result);
  339. return true;
  340. }
  341. bool parse_disk_serial(const char* line, int line_size, const char* match_words, std::string& serial_no)
  342. {
  343. const char* serial_s = strstr(line, match_words);
  344. if (NULL == serial_s)
  345. {
  346. return(false);
  347. }
  348. serial_s += strlen(match_words);
  349. while (isspace(serial_s[0]))
  350. {
  351. ++serial_s;
  352. }
  353. const char* serial_e = line + line_size;
  354. const char* comma = strchr(serial_s, ',');
  355. if (NULL != comma)
  356. {
  357. serial_e = comma;
  358. }
  359. while (serial_e > serial_s && isspace(serial_e[-1]))
  360. {
  361. --serial_e;
  362. }
  363. if (serial_e <= serial_s)
  364. {
  365. return(false);
  366. }
  367. std::string(serial_s, serial_e).swap(serial_no);
  368. return(true);
  369. }
  370. void get_disk_serial(const char* file_name, const char* match_words, std::vector<string>& serial_no)
  371. {
  372. std::ifstream ifs(file_name, std::ios::binary);
  373. if (!ifs.is_open())
  374. {
  375. return;
  376. }
  377. char line[4096] = { 0 };
  378. while (!ifs.eof())
  379. {
  380. ifs.getline(line, sizeof(line));
  381. if (!ifs.good())
  382. {
  383. break;
  384. }
  385. if (0 == ifs.gcount())
  386. {
  387. continue;
  388. }
  389. string disk_serial;
  390. if (parse_disk_serial(line, ifs.gcount() - 1, match_words, disk_serial))
  391. {
  392. //break;
  393. serial_no.push_back(disk_serial);
  394. }
  395. }
  396. ifs.close();
  397. }
  398. bool isSpace(char x) { return x == ' '; }
  399. bool get_disk_serial_by_system(std::vector<string>& serial_no, int& errCode, const string save_path)
  400. {
  401. //if (save_path.size() + strlen("/.lshw_result.txt") > MAX_PATH_SIZE) return false;
  402. //char lshw_result[MAX_PATH_SIZE] = { 0 };
  403. //strcpy(lshw_result, save_path.c_str());
  404. //strcat(lshw_result, "/.lshw_result.txt");
  405. //char command[512] = { 0 };
  406. //snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
  407. //if (0 == system(command))
  408. //{
  409. // get_disk_serial(lshw_result, "serial:", serial_no);
  410. //}
  411. //else {
  412. // return false;
  413. //}
  414. errCode = 0;
  415. struct hd_driveid id;
  416. int fd = open("/dev/hda", O_RDONLY | O_NONBLOCK);
  417. if (fd < 0 && errno == 2) {
  418. fd = open("/dev/sda", O_RDONLY | O_NONBLOCK);
  419. if (fd < 0 && errno ==2 ) {
  420. fd = open("/dev/nvme0n1", O_RDONLY | O_NONBLOCK);
  421. if (fd < 0) {
  422. perror("read failed:");
  423. errCode = errno;
  424. return false;
  425. }
  426. }
  427. }
  428. if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
  429. string xx((const char*)(id.serial_no));
  430. xx.erase(remove_if(xx.begin(), xx.end(), isSpace), xx.end());
  431. serial_no.push_back(xx);
  432. }
  433. close(fd);
  434. //unlink(lshw_result);
  435. return(true);
  436. }
  437. #include <unistd.h>
  438. bool file_is_exist(string filePath) {
  439. return (access(filePath.c_str(), F_OK) == 0);
  440. }
  441. bool dir_is_exist(string dirPath) {
  442. return (access(dirPath.c_str(), F_OK) == 0);
  443. }
  444. //创建成功返回0,创建失败返回1,已经存在返回2
  445. int file_create(string filePath) {
  446. if (!file_is_exist(filePath)) {
  447. FILE* fp;
  448. if (fp = fopen(filePath.c_str(), "w")) {
  449. fclose(fp);
  450. return 0;
  451. }
  452. else {
  453. return 1;
  454. }
  455. }
  456. return 2;
  457. }
  458. #include <sys/stat.h>
  459. //创建成功返回0
  460. int dir_create(string dirPath) {
  461. if (!dir_is_exist(dirPath)) {
  462. return mkdir(dirPath.c_str(), S_IRWXU);
  463. }
  464. return 0;
  465. }
  466. //删除成功返回0
  467. int remove_file(string filePath) {
  468. return remove(filePath.c_str());
  469. }
  470. #include <dirent.h>
  471. int remove_dir(string dirPath) {
  472. DIR* dir;
  473. struct dirent* ptr;
  474. dir = opendir(dirPath.c_str());
  475. if (dir == NULL) return 1;
  476. while ((ptr = readdir(dir)) != NULL) {
  477. if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) continue;
  478. char path[1024] = { 0 };
  479. strcpy(path, dirPath.c_str());
  480. strcat(path, "/");
  481. strcat(path, ptr->d_name);
  482. if (ptr->d_type == DT_DIR) {
  483. remove_dir(path);
  484. }
  485. else {
  486. if (remove_file(path) != 0) return 1;
  487. }
  488. }
  489. closedir(dir);
  490. return rmdir(dirPath.c_str());
  491. }
  492. bool set_system_time(TIME* _time)
  493. {
  494. struct tm p;
  495. p.tm_year = _time->year - 1900;
  496. p.tm_mon = _time->month - 1;
  497. p.tm_mday = _time->day;
  498. p.tm_hour = _time->hour;
  499. p.tm_min = _time->minute;
  500. p.tm_sec = _time->second;
  501. //cout << p.tm_year << " " << p.tm_mon << " " << p.tm_mday << " " << p.tm_hour << " " << p.tm_min << " " << p.tm_sec << endl;
  502. time_t sec = mktime(&p);
  503. //cout << sec << endl;
  504. struct timeval tv;
  505. tv.tv_sec = sec;
  506. //cout << tv.tv_sec << endl;
  507. if (settimeofday(&tv, NULL) < 0)
  508. {
  509. return false;
  510. }
  511. return true;
  512. }
  513. bool set_system_time_by_sec(int sec)
  514. {
  515. struct timeval tv;
  516. gettimeofday(&tv, NULL);
  517. tv.tv_sec += sec;
  518. //cout << tv.tv_sec << endl;
  519. if (settimeofday(&tv, NULL) < 0)
  520. {
  521. return false;
  522. }
  523. return true;
  524. }
  525. TIME* get_system_time()
  526. {
  527. TIME* t = new TIME();
  528. time_t timep;
  529. time(&timep);
  530. struct tm* p;
  531. p = localtime(&timep);
  532. t->year = 1900 + p->tm_year;
  533. t->month = 1 + p->tm_mon;
  534. t->day = p->tm_mday;
  535. t->hour = p->tm_hour;
  536. t->minute = p->tm_min;
  537. t->second = p->tm_sec;
  538. return t;
  539. }
  540. string time2str(const TIME* tim) {
  541. if (tim == NULL) return "";
  542. string ret;
  543. char timeStr[16] = { 0 };
  544. sprintf(timeStr, "%04d%02d%02d%02d%02d%02d", tim->year,
  545. tim->month, tim->day, tim->hour, tim->minute, tim->second);
  546. ret = timeStr;
  547. return ret;
  548. }
  549. TIME* str2time(string str) {
  550. if (str.length() < 14) return NULL;
  551. TIME* ret = new TIME();
  552. ret->year = str2int(str.substr(0, 4));
  553. ret->month = str2int(str.substr(4, 2));
  554. ret->day = str2int(str.substr(6, 2));
  555. ret->hour = str2int(str.substr(8, 2));
  556. ret->minute = str2int(str.substr(10, 2));
  557. ret->second = str2int(str.substr(12, 2));
  558. return ret;
  559. }
  560. #endif // __linux__