OCT 1. 增加UUID代码,移除系统libuuid依赖

This commit is contained in:
huangxin 2023-01-30 14:12:33 +08:00
parent 9fe48bc1ae
commit c0addec100
7 changed files with 128 additions and 12 deletions

View File

@ -19,11 +19,11 @@ FUNCTION(LINUX_INSTALL_SYSTEM_PACKAGE)
MESSAGE("Run this command to install system(${OS_DISTRIB_NAME}) dependencies libraries:")
IF (${OS_DISTRIB_NAME} MATCHES "CentOS")
MESSAGE(FATAL_ERROR "$sudo yum -y install libcurl-devel czmq-devel openssl-devel uuid-devel")
MESSAGE(FATAL_ERROR "$sudo yum -y install libcurl-devel czmq-devel openssl-devel")
ELSEIF (${OS_DISTRIB_NAME} MATCHES "Ubuntu")
MESSAGE(FATAL_ERROR "$sudo -S apt -y install libcurl4-openssl-dev libczmq-dev libssl-dev uuid-dev")
MESSAGE(FATAL_ERROR "$sudo -S apt -y install libcurl4-openssl-dev libczmq-dev libssl-dev")
ENDIF ()
ELSE ()
MESSAGE(FATAL_ERROR "Run command to install system dependencies libraries [libcurl,libssl,libcrypto,libzmq, uuid] by yourself")
MESSAGE(FATAL_ERROR "Run command to install system dependencies libraries [libcurl,libssl,libcrypto,libzmq] by yourself")
ENDIF ()
ENDFUNCTION(LINUX_INSTALL_SYSTEM_PACKAGE)

View File

@ -13,10 +13,8 @@ PKG_SEARCH_MODULE(LIBCURL QUIET libcurl)
PKG_SEARCH_MODULE(LIBSSL QUIET libssl)
PKG_SEARCH_MODULE(LIBCRYPTO QUIET libcrypto)
PKG_SEARCH_MODULE(LIBZMQ QUIET libzmq)
FIND_LIBRARY(LIBUUID uuid QUIET)
IF ((NOT LIBZMQ_FOUND) OR (NOT LIBCRYPTO_FOUND) OR (NOT LIBSSL_FOUND) OR (NOT LIBCURL_FOUND)
OR (${LIBUUID} MATCHES "LIBUUID-NOTFOUND"))
IF ((NOT LIBZMQ_FOUND) OR (NOT LIBCRYPTO_FOUND) OR (NOT LIBSSL_FOUND) OR (NOT LIBCURL_FOUND))
LINUX_INSTALL_SYSTEM_PACKAGE()
ENDIF ()
@ -25,7 +23,7 @@ SET(COMMON_LIBS "")
INCLUDE(../depend/third_libs.cmake)
LIST(APPEND COMMON_LIBS "${LIBCURL_LDFLAGS} ${LIBSSL_LDFLAGS} ${LIBCRYPTO_LDFLAGS}")
LIST(APPEND COMMON_LIBS "${LIBZMQ_LDFLAGS} ${LIBUUID}")
LIST(APPEND COMMON_LIBS "${LIBZMQ_LDFLAGS}")
LIST(APPEND COMMON_LIBS "-lm -lpthread")
ADD_DEFINITIONS(${COMMON_DEFINE})

View File

@ -12,6 +12,7 @@ FILE(GLOB C_HEADS include/*.h include/uthash/*.h include/s2j/*.h vector/*.h ${CM
AUX_SOURCE_DIRECTORY(json C_SRC)
AUX_SOURCE_DIRECTORY(args C_SRC)
AUX_SOURCE_DIRECTORY(uuid C_SRC)
AUX_SOURCE_DIRECTORY(init C_SRC)
AUX_SOURCE_DIRECTORY(misc C_SRC)
AUX_SOURCE_DIRECTORY(banner C_SRC)

22
srcs/libs/include/uuid.h Normal file
View File

@ -0,0 +1,22 @@
//
// Created by xajhuang on 2023/1/30.
//
#ifndef VCPE_UUID_H
#define VCPE_UUID_H
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned char uuid_t[16];
#define uuid_generate(out) uuid_generate_random(out)
void uuid_generate_random(uuid_t out);
void uuid_unparse(const uuid_t uuid, char *out);
void uuid_copy(uuid_t dst, const uuid_t src);
void uuid_parse(const char *in, uuid_t uuid);
#ifdef __cplusplus
}
#endif
#endif //VCPE_UUID_H

View File

@ -6,12 +6,12 @@
#include <curl/curl.h>
#include <string.h>
#include <uv.h>
#include <uuid/uuid.h>
#include <unistd.h>
#include "inet_misc.h"
#include "config.h"
#include "misc.h"
#include "uuid.h"
#include "uthash/uthash.h"
#include "task_manager.h"
#include "user_errno.h"
@ -619,7 +619,7 @@ const char *inet_download_file_async(const char *pURL,
uuid_generate_random(msgId);
memset(strMsgId, 0, 64);
uuid_unparse_lower(msgId, strMsgId);
uuid_unparse(msgId, strMsgId);
pParams->pTaskUuid = strdup(strMsgId);
if (pPath == NULL) {
@ -736,7 +736,7 @@ const char *inet_http_post_async(const char *pURL, const char *pPost, on_http_re
uuid_generate_random(msgId);
memset(strMsgId, 0, 64);
uuid_unparse_lower(msgId, strMsgId);
uuid_unparse(msgId, strMsgId);
pParams->pTaskUuid = strdup(strMsgId);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writeDataCb);

95
srcs/libs/uuid/uuid.c Normal file
View File

@ -0,0 +1,95 @@
//
// Created by xajhuang on 2023/1/30.
//
#include <stdlib.h>
#include <time.h>
#include "uuid.h"
#include "config.h"
#define RAND_LENGTH (4)
static const unsigned char hex[16] = "0123456789abcdef";
void uuid_generate_random(uuid_t out) {
int i, j, rnd;
static int seeded = 0;
if (!seeded) {
srand(time(NULL));
seeded = 1;
}
for (i = 0; i < (16 / RAND_LENGTH); i++) {
rnd = rand();
for (j = 0; j < RAND_LENGTH; j++) {
out[i * RAND_LENGTH + j] = (0xff & rnd >> (8 * j));
}
}
// set the version to 4
out[6] = (out[6] & 0x0f) | 0x40;
// set the variant to 1 (a)
out[8] = (out[8] & 0x0f) | 0xa0;
}
int _hex2dec(char c) {
int i;
for (i = 0; i < 16; i++) {
if (hex[i] == c) {
return i;
}
}
return -1;
}
void uuid_parse(const char *in, uuid_t uuid) {
int i, j;
i = j = 0;
do {
switch (in[i]) {
case '-':
break;
default:
uuid[j++] = (_hex2dec(in[i++]) << 4) | _hex2dec(in[i]);
}
i++;
} while (j < 16 && i < 36);
}
void uuid_unparse(const uuid_t uuid, char *out) {
int i, j;
i = j = 0;
do {
switch (j) {
case 4:
case 6:
case 8:
case 10:
out[i++] = '-';
}
out[i++] = hex[(uuid[j] >> 4)];
out[i++] = hex[(0xf & uuid[j])];
j++;
} while (j < 16);
out[36] = 0;
}
void uuid_copy(uuid_t dst, const uuid_t src) {
int i;
for (i = 0; i < sizeof(uuid_t); i++) {
dst[i] = src[i];
}
}

View File

@ -8,6 +8,8 @@
#include "cmdline.h"
#include "task_manager.h"
#include "init.h"
#include "inet_misc.h"
#include "config.h"
#include "prj_config.h"
#include "user_errno.h"
@ -19,8 +21,6 @@
#ifdef OPENDHCPD_ON
#include "proto.h"
#include "inet_misc.h"
#include "config.h"
#endif
#ifdef OPENDHCPDDNS_ON