123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #if defined(_MSC_VER)
- #include <Windows.h>
- #include <WinSock.h>
- #include <IPHlpApi.h>
- #include <direct.h>
- #include <io.h>
- #include <Rpc.h>
- #pragma comment(lib, "Ws2_32.lib")
- #pragma comment(lib, "IPHlpApi.lib")
- #pragma comment(lib, "Rpcrt4.lib")
- #else
- #include <netdb.h>
- #include <unistd.h>
- #include <sys/un.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #include <net/if.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #endif
- #include <string.h>
- #include "log_util.h"
- #include "md5.h"
- #include <stdio.h>
- #include "uuid4.h"
- static const char *g_hex_hash = "0123456789ABCDEF";
- void md5_to_string(const char * buffer, int bufLen, char * md5)
- {
- int i = 0;
- unsigned char md5Buf[16];
- mbedtls_md5((const unsigned char *)buffer, bufLen, md5Buf);
- for(; i < 32; i+=2)
- {
- md5[i] = g_hex_hash[md5Buf[i >> 1] >> 4];
- md5[i+1] = g_hex_hash[md5Buf[i >> 1] & 0xF];
- }
- }
- int aos_base64_encode(const unsigned char *in, int inLen, char *out)
- {
- static const char *ENC =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- char *original_out = out;
- while (inLen) {
- // first 6 bits of char 1
- *out++ = ENC[*in >> 2];
- if (!--inLen) {
- // last 2 bits of char 1, 4 bits of 0
- *out++ = ENC[(*in & 0x3) << 4];
- *out++ = '=';
- *out++ = '=';
- break;
- }
- // last 2 bits of char 1, first 4 bits of char 2
- *out++ = ENC[((*in & 0x3) << 4) | (*(in + 1) >> 4)];
- in++;
- if (!--inLen) {
- // last 4 bits of char 2, 2 bits of 0
- *out++ = ENC[(*in & 0xF) << 2];
- *out++ = '=';
- break;
- }
- // last 4 bits of char 2, first 2 bits of char 3
- *out++ = ENC[((*in & 0xF) << 2) | (*(in + 1) >> 6)];
- in++;
- // last 6 bits of char 3
- *out++ = ENC[*in & 0x3F];
- in++, inLen--;
- }
- return (int)(out - original_out);
- }
- /*
- int signature_to_base64(const char * sig, int sigLen, const char * key, int keyLen, char * base64)
- {
- unsigned char sha1Buf[20];
- hmac_sha1(sha1Buf, key, keyLen << 3, sig, sigLen << 3);
- return aos_base64_encode((const unsigned char *)sha1Buf, 20, base64);
- }
- */
- void get_format_uuid(char* strbuffer, int ulen)
- {
- int uuidlen = 0;
- uuid4_init();
- uuid4_generate(strbuffer);
- }
- void GetTimeStr(time_t time, char* szTime)
- {
- #if defined(_MSC_VER)
- LARGE_INTEGER liTime, liFreq;
- double dTime;
- struct tm* tm = localtime(&time);
- QueryPerformanceFrequency(&liFreq);
- QueryPerformanceCounter(&liTime);
- dTime = (double)liTime.QuadPart / (double)liFreq.QuadPart;
- sprintf(szTime, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- (long)(dTime * 1000) % 1000
- );
- #else
- struct timeval tv;
- struct tm* tm = localtime(&time);
- gettimeofday(&tv, NULL);
- sprintf(szTime, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- tv.tv_usec / 1000
- );
- #endif //_MSC_VER
- }
- void GetUnitedTimeStr(time_t time, char* szTime)
- {
- struct tm* tm = localtime(&time);
- sprintf(szTime, "%04d-%02d-%02dT%02d:%02d:%02d.000000000Z",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec
- );
- }
- //相对独立,不用框架库提供的函数进行IP 获取 [Gifur@2025729]
- int GetLocalIP(char* ip_str)
- {
-
- #ifdef _WIN32
- struct hostent* ent;
- char tmp[256] = "";
- int i;
- __try {
- ent = gethostbyname(tmp);
- }
- __except (EXCEPTION_EXECUTE_HANDLER) {
- strcpy(ip_str, "0.0.0.0");
- return -1;
- }
- if (ent) {
- for (i = 0; ent->h_addr_list[i]; ++i) {
- if (ent->h_addrtype == AF_INET) {
- struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
- char* p = inet_ntoa(*in);
- if (p[0] != '0') {
- strcpy(ip_str, p);
- return 0;
- }
- }
- }
- }
- #else
- int sockfd = -1;
- struct ifconf ifconf;
- struct ifreq* ifreq = NULL;
- char strbuf[256] = { 0 };
- ifconf.ifc_len = 256;
- ifconf.ifc_buf = strbuf;
- if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
- ioctl(sockfd, SIOCGIFCONF, &ifconf); //get all socket info
- ifreq = (struct ifreq*)ifconf.ifc_buf;
- for (int i = (ifconf.ifc_len / sizeof(struct ifreq)); i > 0; i--) {
- if (ifreq->ifr_flags == AF_INET) { //for ipv4
- char* strIP = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr);
- ifreq++;
- if (NULL != strIP) {
- if (NULL == strstr(strIP, "198.168.") && NULL == strstr(strIP, "127.0.0.1")) {
- strcpy(ip_str, strIP);
- close(sockfd);
- return 0;
- }
- }
- }
- }
- close(sockfd);
- }
- #endif //_WIN32
- return -1;
- }
- void convertUnCharTotr(char* str, unsigned char* UnChar, int ucLen)
- {
- int i = 0;
- for (i = 0; i < ucLen; i++)
- {
- //格式化输str,每unsigned char 转换字符占两位置%x写输%X写输
- sprintf(str + i * 2, "%02x", UnChar[i]);
- }
- }
- void convertStrToUnChar(char* str, unsigned char* UnChar)
- {
- int i = strlen(str), j = 0, counter = 0;
- char c[2];
- unsigned int bytes[2];
- for(j = 0; j < i; j += 2)
- {
- if (0 == j % 2)
- {
- c[0] = str[j];
- c[1] = str[j + 1];
- sscanf(c, "%02x", &bytes[0]);
- UnChar[counter] = bytes[0];
- counter++;
- }
- }
- return;
- }
- int mkdir_foreach(char* file_path, int length) {
- int i;
- char tmpDirPath[256] = { 0 };
- for (i = 0; i < length; i++)
- {
- tmpDirPath[i] = file_path[i];
- if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
- {
- //_access()判断文件是否存在,并判断文件是否可写
- //int _access(const char *pathname, int mode);
- //pathname: 文件路径或目录路径; mode: 访问权限(在不同系统中可能用不能的宏定义重新定义)
- //当pathname为文件时,_access函数判断文件是否存在,并判断文件是否可以用mode值指定的模式进行访问。
- //当pathname为目录时,_access只判断指定目录是否存在,在Windows NT和Windows 2000中,所有的目录都只有读写权限
- //0——>只检查文件是否存在
- #if defined(_MSC_VER)
- if (_access(tmpDirPath, 0) == -1) {
- int ret = _mkdir(tmpDirPath);
- if (ret == -1) {
- return ret;
- }
- }
- #else
- if (access(tmpDirPath, 0) == -1) {
- int ret = mkdir(tmpDirPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
- if (ret == -1) {
- return ret;
- }
- }
- #endif //_MSC_VER
- }
- }
- return 0;
- }
|