From c0addec10083de58476a5f9886c0881a3ec69597 Mon Sep 17 00:00:00 2001 From: huangxin Date: Mon, 30 Jan 2023 14:12:33 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E5=A2=9E=E5=8A=A0UUID=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E7=A7=BB=E9=99=A4=E7=B3=BB=E7=BB=9Flibuuid?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- depend/system_libs.cmake | 6 +-- srcs/CMakeLists.txt | 6 +-- srcs/libs/CMakeLists.txt | 1 + srcs/libs/include/uuid.h | 22 ++++++++ srcs/libs/network/inet_misc.c | 6 +-- srcs/libs/uuid/uuid.c | 95 +++++++++++++++++++++++++++++++++++ srcs/vcpe_main.c | 4 +- 7 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 srcs/libs/include/uuid.h create mode 100644 srcs/libs/uuid/uuid.c diff --git a/depend/system_libs.cmake b/depend/system_libs.cmake index 096f679..85b286f 100644 --- a/depend/system_libs.cmake +++ b/depend/system_libs.cmake @@ -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) \ No newline at end of file diff --git a/srcs/CMakeLists.txt b/srcs/CMakeLists.txt index 81602ef..9ac99bb 100644 --- a/srcs/CMakeLists.txt +++ b/srcs/CMakeLists.txt @@ -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}) diff --git a/srcs/libs/CMakeLists.txt b/srcs/libs/CMakeLists.txt index e51c96f..9b8a760 100644 --- a/srcs/libs/CMakeLists.txt +++ b/srcs/libs/CMakeLists.txt @@ -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) diff --git a/srcs/libs/include/uuid.h b/srcs/libs/include/uuid.h new file mode 100644 index 0000000..65e6d45 --- /dev/null +++ b/srcs/libs/include/uuid.h @@ -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 diff --git a/srcs/libs/network/inet_misc.c b/srcs/libs/network/inet_misc.c index 9a9a453..d87b90a 100644 --- a/srcs/libs/network/inet_misc.c +++ b/srcs/libs/network/inet_misc.c @@ -6,12 +6,12 @@ #include #include #include -#include #include #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); diff --git a/srcs/libs/uuid/uuid.c b/srcs/libs/uuid/uuid.c new file mode 100644 index 0000000..e33db49 --- /dev/null +++ b/srcs/libs/uuid/uuid.c @@ -0,0 +1,95 @@ +// +// Created by xajhuang on 2023/1/30. +// +#include +#include + +#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]; + } +} \ No newline at end of file diff --git a/srcs/vcpe_main.c b/srcs/vcpe_main.c index ebe20db..29d5ea8 100644 --- a/srcs/vcpe_main.c +++ b/srcs/vcpe_main.c @@ -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