#include "comm.h" #include #ifndef _WIN32 #include #include #include #include #endif //NOT _WIN32 #define MAX_PATH_SIZE 256 void GetNewForm(const char* form, char* newForm) { int indexNum = 0; int acount = 0; newForm[0] = '{'; for (int i = 0; i < strlen(form); i++) { //if((i-1 >= 0 && form[i]=='\\') || (i-1 < 0)) if (form[i] == '%') { if (acount != 0) { newForm[++indexNum] = '"'; if (acount % 2 != 0) { newForm[++indexNum] = ':'; } else { newForm[++indexNum] = ','; } } newForm[++indexNum] = '"'; acount++; } if (form[i] == ' ') continue; newForm[++indexNum] = form[i]; } newForm[++indexNum] = '"'; newForm[++indexNum] = '}'; } string GetOutPutStr(const char* form, ...) { char* newForm = new char[strlen(form) * 3 + 5]; memset(newForm, 0, strlen(form) * 3 + 5); if (strlen(form) < 2) { strcpy(newForm, "{\"\"}"); } else { GetNewForm(form, newForm); } va_list vaList; va_start(vaList, form); #ifdef RVC_OS_WIN int acount = _vscprintf(newForm, vaList); #else int acount = vsnprintf(0, 0, newForm, vaList); va_end(vaList); va_start(vaList, form); #endif char* buf = new char[acount + 1]; memset(buf, 0, acount + 1); vsprintf(buf, newForm, vaList); va_end(vaList); string ret; ret.assign(buf); delete buf; delete newForm; return ret; } int no_begin_with(const char strA[], const char strB[]) { if (strA == NULL) return 1; if (strB == NULL) return 1; if (strlen(strA) == 0 || strlen(strB) == 0 || strlen(strA) < strlen(strB)) return 1; const char* pA = strA; const char* pB = strB; while (*pA != '\0' && *pB != '\0') { if (*pA != *pB) return 1; pA++; pB++; } return 0; } int str2int(const string str, int& ret) { if (str.size() == 0) return 1; ret = 0; int symbol = 0; for (int i = 0; i < str.size(); i++) { if (i == 0 && str[i] == '-') { symbol = 1; continue; } if (i == 0 && str[i] == '+') continue; if (i == 0) { while (str[i] == '0' && i < str.size()) { ++i; } if (i == str.size()) return 0; } if (str[i] < '0' || str[i] >'9') return 2; ret += (str[i] - '0') * pow(10, str.size() - i - 1); } if (symbol) ret -= 2 * ret; return 0; } int str2int(const string str) { int ret; str2int(str, ret); return ret; } vector split_str(string strA, string strB) { vector ret; //cout << "ret:" << ret.size() << endl; if (strA.size() == 0 || strB.size() == 0) { return ret; } char* chA = new char[strA.size()]; char* chB = new char[strB.size()]; memset(chA, 0, sizeof(chA)); memset(chB, 0, sizeof(chB)); strcpy(chA, strA.c_str()); strcpy(chB, strB.c_str()); char* tmp = strtok(chA, chB); while (tmp) { //cout << "tmp:" << tmp << endl; ret.push_back(tmp); tmp = strtok(NULL, chB); } delete[] chA; delete[] chB; return ret; } int ip2byte(const string str, BYTE ip[]) { vector ret = split_str(str, "."); vector::iterator it = ret.begin(); int index = 0; while (it != ret.end()) { int tmp = 0; if (str2int(*it, tmp)) return 1; else { if (tmp > 255) return 2; else { ip[index] = tmp; index++; } } it++; } return 0; } unsigned char Ch2Hex(char ch) { static const char* hex = "0123456789ABCDEF"; for (unsigned char i = 0; i != 16; ++i) if (ch == hex[i]) return i; return 0; } char* Hex2Str(const char* src, int& dstLen) { int i = 0; int cnt = 0; int len = strlen(src); unsigned char* d = new unsigned char[len]; memset(d, 0, len); while (*src) { if (i & 1) { d[cnt++] |= Ch2Hex(*src); } else { d[cnt] = Ch2Hex(*src) << 4; } src++; i++; } dstLen = cnt; return (char*)d; } char* Str2Hex(const char* src, int srcLen) { string ret; static const char* hex = "0123456789ABCDEF"; for (int i = 0; i != srcLen; ++i) { ret.push_back(hex[(src[i] >> 4) & 0xf]); ret.push_back(hex[src[i] & 0xf]); } char* tmp = new char[ret.length() + 1]; memset(tmp, 0, ret.length() + 1); memcpy(tmp, ret.c_str(), ret.length()); return tmp; } #ifdef __linux__ int getIPFromLinux(char* ip) { //if (strlen(ip) < 15) return 1; struct ifaddrs* ifAddrStruct = NULL; void* tmpAddrPtr = NULL; getifaddrs(&ifAddrStruct); while (ifAddrStruct != NULL) { if (ifAddrStruct->ifa_addr->sa_family == AF_INET) { // is a valid IP4 Address tmpAddrPtr = &((struct sockaddr_in*)ifAddrStruct->ifa_addr)->sin_addr; char addressBuffer[INET_ADDRSTRLEN]; inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); //printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer); //99开头行内办公网,10开头行内业务网。规范出自《招商银行总行网络规范汇编(2017年版).pdf》 if (!no_begin_with(addressBuffer, "99") || !no_begin_with(addressBuffer, "10")) { memset(ip, 0, sizeof(ip)); strcpy(ip, addressBuffer); } else if (strlen(ip) == 0 && no_begin_with(addressBuffer, "127.0")) { memset(ip, 0, sizeof(ip)); strcpy(ip, addressBuffer); } } /* else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6) { // check it is IP6 // is a valid IP6 Address tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; char addressBuffer[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer); } */ ifAddrStruct = ifAddrStruct->ifa_next; } //printf("ip=%s\n", ip); return 0; } void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id) { cpu_id.c_str(); std::ifstream ifs(file_name, std::ios::binary); if (!ifs.is_open()) { return; } char line[4096] = { 0 }; while (!ifs.eof()) { ifs.getline(line, sizeof(line)); if (!ifs.good()) { break; } const char* cpu = strstr(line, match_words); if (NULL == cpu) { continue; } cpu += strlen(match_words); while ('\0' != cpu[0]) { if (' ' != cpu[0]) { cpu_id.push_back(cpu[0]); } ++cpu; } if (!cpu_id.empty()) { break; } } ifs.close(); } bool get_cpu_id_by_system(std::string& cpu_id, const string save_path) { //cpu_id.c_str(); if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false; char dmidecode_result[MAX_PATH_SIZE] = { 0 }; strcpy(dmidecode_result, save_path.c_str()); strcat(dmidecode_result, "/.dmidecode_result.txt"); char command[512] = { 0 }; snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result); if (0 == system(command)) { parse_cpu_id(dmidecode_result, "ID:", cpu_id); } unlink(dmidecode_result); return(!cpu_id.empty()); } void parse_board_serial(const char* file_name, const char* match_words, std::string& board_serial) { board_serial.c_str(); std::ifstream ifs(file_name, std::ios::binary); if (!ifs.is_open()) { return; } char line[4096] = { 0 }; while (!ifs.eof()) { ifs.getline(line, sizeof(line)); if (!ifs.good()) { break; } const char* board = strstr(line, match_words); if (NULL == board) { continue; } board += strlen(match_words); while ('\0' != board[0]) { if (' ' != board[0]) { board_serial.push_back(board[0]); } ++board; } if ("None" == board_serial) { board_serial.clear(); continue; } if (!board_serial.empty()) { break; } } ifs.close(); } bool get_board_serial_by_system(std::string& board_serial,const string save_path) { //board_serial.c_str(); if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false; char dmidecode_result[MAX_PATH_SIZE] = { 0 }; strcpy(dmidecode_result, save_path.c_str()); strcat(dmidecode_result, "/.dmidecode_result.txt"); char command[512] = { 0 }; snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result); if (0 == system(command)) { parse_board_serial(dmidecode_result, "Serial Number:", board_serial); } else { return false; } unlink(dmidecode_result); return true; } bool parse_disk_serial(const char* line, int line_size, const char* match_words, std::string& serial_no) { const char* serial_s = strstr(line, match_words); if (NULL == serial_s) { return(false); } serial_s += strlen(match_words); while (isspace(serial_s[0])) { ++serial_s; } const char* serial_e = line + line_size; const char* comma = strchr(serial_s, ','); if (NULL != comma) { serial_e = comma; } while (serial_e > serial_s && isspace(serial_e[-1])) { --serial_e; } if (serial_e <= serial_s) { return(false); } std::string(serial_s, serial_e).swap(serial_no); return(true); } void get_disk_serial(const char* file_name, const char* match_words, std::vector& serial_no) { std::ifstream ifs(file_name, std::ios::binary); if (!ifs.is_open()) { return; } char line[4096] = { 0 }; while (!ifs.eof()) { ifs.getline(line, sizeof(line)); if (!ifs.good()) { break; } if (0 == ifs.gcount()) { continue; } string disk_serial; if (parse_disk_serial(line, ifs.gcount() - 1, match_words, disk_serial)) { //break; serial_no.push_back(disk_serial); } } ifs.close(); } bool isSpace(char x) { return x == ' '; } bool get_disk_serial_by_system(std::vector& serial_no, int& errCode, const string save_path) { //if (save_path.size() + strlen("/.lshw_result.txt") > MAX_PATH_SIZE) return false; //char lshw_result[MAX_PATH_SIZE] = { 0 }; //strcpy(lshw_result, save_path.c_str()); //strcat(lshw_result, "/.lshw_result.txt"); //char command[512] = { 0 }; //snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result); //if (0 == system(command)) //{ // get_disk_serial(lshw_result, "serial:", serial_no); //} //else { // return false; //} errCode = 0; struct hd_driveid id; int fd = open("/dev/hda", O_RDONLY | O_NONBLOCK); if (fd < 0 && errno == 2) { fd = open("/dev/sda", O_RDONLY | O_NONBLOCK); if (fd < 0 && errno ==2 ) { fd = open("/dev/nvme0n1", O_RDONLY | O_NONBLOCK); if (fd < 0) { perror("read failed:"); errCode = errno; return false; } } } if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) { string xx((const char*)(id.serial_no)); xx.erase(remove_if(xx.begin(), xx.end(), isSpace), xx.end()); serial_no.push_back(xx); } close(fd); //unlink(lshw_result); return(true); } #include bool file_is_exist(string filePath) { return (access(filePath.c_str(), F_OK) == 0); } bool dir_is_exist(string dirPath) { return (access(dirPath.c_str(), F_OK) == 0); } //创建成功返回0,创建失败返回1,已经存在返回2 int file_create(string filePath) { if (!file_is_exist(filePath)) { FILE* fp; if (fp = fopen(filePath.c_str(), "w")) { fclose(fp); return 0; } else { return 1; } } return 2; } #include //创建成功返回0 int dir_create(string dirPath) { if (!dir_is_exist(dirPath)) { return mkdir(dirPath.c_str(), S_IRWXU); } return 0; } //删除成功返回0 int remove_file(string filePath) { return remove(filePath.c_str()); } #include int remove_dir(string dirPath) { DIR* dir; struct dirent* ptr; dir = opendir(dirPath.c_str()); if (dir == NULL) return 1; while ((ptr = readdir(dir)) != NULL) { if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) continue; char path[1024] = { 0 }; strcpy(path, dirPath.c_str()); strcat(path, "/"); strcat(path, ptr->d_name); if (ptr->d_type == DT_DIR) { remove_dir(path); } else { if (remove_file(path) != 0) return 1; } } closedir(dir); return rmdir(dirPath.c_str()); } bool set_system_time(TIME* _time) { struct tm p; p.tm_year = _time->year - 1900; p.tm_mon = _time->month - 1; p.tm_mday = _time->day; p.tm_hour = _time->hour; p.tm_min = _time->minute; p.tm_sec = _time->second; //cout << p.tm_year << " " << p.tm_mon << " " << p.tm_mday << " " << p.tm_hour << " " << p.tm_min << " " << p.tm_sec << endl; time_t sec = mktime(&p); //cout << sec << endl; struct timeval tv; tv.tv_sec = sec; //cout << tv.tv_sec << endl; if (settimeofday(&tv, NULL) < 0) { return false; } return true; } bool set_system_time_by_sec(int sec) { struct timeval tv; gettimeofday(&tv, NULL); tv.tv_sec += sec; //cout << tv.tv_sec << endl; if (settimeofday(&tv, NULL) < 0) { return false; } return true; } TIME* get_system_time() { TIME* t = new TIME(); time_t timep; time(&timep); struct tm* p; p = localtime(&timep); t->year = 1900 + p->tm_year; t->month = 1 + p->tm_mon; t->day = p->tm_mday; t->hour = p->tm_hour; t->minute = p->tm_min; t->second = p->tm_sec; return t; } string time2str(const TIME* tim) { if (tim == NULL) return ""; string ret; char timeStr[16] = { 0 }; sprintf(timeStr, "%04d%02d%02d%02d%02d%02d", tim->year, tim->month, tim->day, tim->hour, tim->minute, tim->second); ret = timeStr; return ret; } TIME* str2time(string str) { if (str.length() < 14) return NULL; TIME* ret = new TIME(); ret->year = str2int(str.substr(0, 4)); ret->month = str2int(str.substr(4, 2)); ret->day = str2int(str.substr(6, 2)); ret->hour = str2int(str.substr(8, 2)); ret->minute = str2int(str.substr(10, 2)); ret->second = str2int(str.substr(12, 2)); return ret; } #endif // __linux__