From 6a392de2c6ba7e02fd1afb523aec4518d342975f Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 21 Sep 2024 17:46:53 +0800 Subject: [PATCH 1/2] Add compile options: -Wno-comment --- CMakeLists.txt | 76 ++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f4527f4b..78b6cfcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,26 +9,26 @@ check_struct_has_member("struct mmsghdr" msg_hdr sys/socket.h HAVE_MMSG_HDR) check_symbol_exists(sendmmsg sys/socket.h HAVE_SENDMMSG_API) check_symbol_exists(recvmmsg sys/socket.h HAVE_RECVMMSG_API) -if(HAVE_MMSG_HDR) +if (HAVE_MMSG_HDR) add_definitions(-DHAVE_MMSG_HDR) -endif() -if(HAVE_SENDMMSG_API) +endif () +if (HAVE_SENDMMSG_API) add_definitions(-DHAVE_SENDMMSG_API) -endif() -if(HAVE_RECVMMSG_API) +endif () +if (HAVE_RECVMMSG_API) add_definitions(-DHAVE_RECVMMSG_API) -endif() +endif () # check the socket buffer size set by the upper cmake project, if it is set, use the setting of the upper cmake project, otherwise set it to 256K # if the socket buffer size is set to 0, it means that the socket buffer size is not set, and the kernel default value is used(just for linux) -if(DEFINED SOCKET_DEFAULT_BUF_SIZE) +if (DEFINED SOCKET_DEFAULT_BUF_SIZE) if (SOCKET_DEFAULT_BUF_SIZE EQUAL 0) message(STATUS "Socket default buffer size is not set, use the kernel default value") - else() + else () message(STATUS "Socket default buffer size is set to ${SOCKET_DEFAULT_BUF_SIZE}") endif () add_definitions(-DSOCKET_DEFAULT_BUF_SIZE=${SOCKET_DEFAULT_BUF_SIZE}) -endif() +endif () #加载自定义模块 #Load custom modules set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") @@ -42,47 +42,49 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) #Set subdirectories set(SUB_DIR_LIST "Network" "Poller" "Thread" "Util") -if(WIN32) +if (WIN32) list(APPEND SUB_DIR_LIST "win32") #防止Windows.h包含Winsock.h #Prevent Windows.h from including Winsock.h add_definitions(-DWIN32_LEAN_AND_MEAN) if (MSVC) add_compile_options("/utf-8") - endif() + endif () set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -endif() +else () + add_compile_options("-Wno-comment") +endif () #安装目录 #Installation directory -if(WIN32) +if (WIN32) set(INSTALL_PATH_LIB $ENV{HOME}/${PROJECT_NAME}/lib) set(INSTALL_PATH_INCLUDE $ENV{HOME}/${PROJECT_NAME}/include) -else() +else () set(INSTALL_PATH_LIB lib) set(INSTALL_PATH_INCLUDE include) -endif() +endif () -foreach(SUB_DIR ${SUB_DIR_LIST}) +foreach (SUB_DIR ${SUB_DIR_LIST}) #遍历源文件 #Traverse source file aux_source_directory(src/${SUB_DIR} SRC_LIST) #安装头文件至系统目录 #Install header file to system directory install(DIRECTORY src/${SUB_DIR} DESTINATION ${INSTALL_PATH_INCLUDE} FILES_MATCHING PATTERN "*.h") -endforeach() +endforeach () #非苹果平台移除.mm类型的文件 #Remove .mm type files from non-apple platforms -if(NOT APPLE) +if (NOT APPLE) list(REMOVE_ITEM SRC_LIST "src/Network/Socket_ios.mm") -endif() +endif () -if(WIN32) +if (WIN32) set(LINK_LIB_LIST WS2_32 Iphlpapi shlwapi) -else() +else () set(LINK_LIB_LIST) -endif() +endif () set(ENABLE_OPENSSL ON CACHE BOOL "enable openssl") @@ -92,29 +94,29 @@ set(ASAN_USE_DELETE OFF CACHE BOOL "use delele[] or free when asan enabled") #查找openssl是否安装 #Find out if openssl is installed find_package(OpenSSL QUIET) -if(OPENSSL_FOUND AND ENABLE_OPENSSL) +if (OPENSSL_FOUND AND ENABLE_OPENSSL) message(STATUS "找到openssl库:\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL宏已打开") include_directories(${OPENSSL_INCLUDE_DIR}) add_definitions(-DENABLE_OPENSSL) - list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES}) -endif() + list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES}) +endif () #查找mysql是否安装 #Find out if mysql is installed find_package(MYSQL QUIET) -if(MYSQL_FOUND AND ENABLE_MYSQL) +if (MYSQL_FOUND AND ENABLE_MYSQL) message(STATUS "找到mysqlclient库:\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL宏已打开") include_directories(${MYSQL_INCLUDE_DIR}) include_directories(${MYSQL_INCLUDE_DIR}/mysql) add_definitions(-DENABLE_MYSQL) - list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES}) -endif() + list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES}) +endif () #是否使用delete[]替代free,用于解决开启asan后在MacOS上的卡死问题 #use delele[] or free when asan enabled -if(ASAN_USE_DELETE) +if (ASAN_USE_DELETE) add_definitions(-DASAN_USE_DELETE) -endif() +endif () #打印库文件 #Print library files @@ -123,20 +125,20 @@ message(STATUS "将链接依赖库:${LINK_LIB_LIST}") #Enable c++11 set(CMAKE_CXX_STANDARD 11) -if(NOT WIN32) +if (NOT WIN32) add_compile_options(-Wno-deprecated-declarations) add_compile_options(-Wno-predefined-identifier-outside-function) -endif() +endif () #编译动态库 #Compile dynamic library -if(NOT IOS AND NOT ANDROID AND NOT WIN32) +if (NOT IOS AND NOT ANDROID AND NOT WIN32) add_library(${PROJECT_NAME}_shared SHARED ${SRC_LIST}) target_include_directories(${PROJECT_NAME}_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) target_link_libraries(${PROJECT_NAME}_shared ${LINK_LIB_LIST}) set_target_properties(${PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME "${PROJECT_NAME}") - install(TARGETS ${PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB}) -endif() + install(TARGETS ${PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB}) +endif () #编译静态库 #Compile static library @@ -152,6 +154,6 @@ install(TARGETS ${PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB}) #测试程序 #Test program -if(NOT IOS) +if (NOT IOS) add_subdirectory(tests) -endif() +endif () From ba5deb113bb8bc28daf71fdb5005eaadd089b098 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 21 Sep 2024 17:47:50 +0800 Subject: [PATCH 2/2] Ensure thread safety of ssl initor --- src/Util/SSLBox.cpp | 10 ++++++++-- src/Util/SSLBox.h | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Util/SSLBox.cpp b/src/Util/SSLBox.cpp index 1362578f..3ea09343 100644 --- a/src/Util/SSLBox.cpp +++ b/src/Util/SSLBox.cpp @@ -93,8 +93,7 @@ SSL_Initor::~SSL_Initor() { #endif //defined(ENABLE_OPENSSL) } -bool SSL_Initor::loadCertificate(const string &pem_or_p12, bool server_mode, const string &password, bool is_file, - bool is_default) { +bool SSL_Initor::loadCertificate(const string &pem_or_p12, bool server_mode, const string &password, bool is_file, bool is_default) { auto cers = SSLUtil::loadPublicKey(pem_or_p12, password, is_file); auto key = SSLUtil::loadPrivateKey(pem_or_p12, password, is_file); auto ssl_ctx = SSLUtil::makeSSLContext(cers, key, server_mode, true); @@ -128,6 +127,7 @@ int SSL_Initor::findCertificate(SSL *ssl, int *, void *arg) { if (!ctx) { //未找到对应的证书 [AUTO-TRANSLATED:d4550e6f] //No corresponding certificate found + std::lock_guard lck(ref._mtx); WarnL << "Can not find any certificate of host: " << vhost << ", select default certificate of: " << ref._default_vhost[(bool) (arg)]; } @@ -153,6 +153,7 @@ int SSL_Initor::findCertificate(SSL *ssl, int *, void *arg) { } bool SSL_Initor::setContext(const string &vhost, const shared_ptr &ctx, bool server_mode, bool is_default) { + std::lock_guard lck(_mtx); if (!ctx) { return false; } @@ -240,6 +241,7 @@ void SSL_Initor::setupCtx(SSL_CTX *ctx) { } shared_ptr SSL_Initor::makeSSL(bool server_mode) { + std::lock_guard lck(_mtx); #if defined(ENABLE_OPENSSL) #ifdef SSL_ENABLE_SNI //openssl 版本支持SNI [AUTO-TRANSLATED:b8029f6c] @@ -256,6 +258,7 @@ shared_ptr SSL_Initor::makeSSL(bool server_mode) { } bool SSL_Initor::trustCertificate(X509 *cer, bool server_mode) { + std::lock_guard lck(_mtx); return SSLUtil::trustCertificate(_ctx_empty[server_mode].get(), cer); } @@ -276,6 +279,7 @@ std::shared_ptr SSL_Initor::getSSLCtx(const string &vhost, bool server_ } std::shared_ptr SSL_Initor::getSSLCtxWildcards(const string &vhost, bool server_mode) { + std::lock_guard lck(_mtx); for (auto &pr : _ctxs_wildcards[server_mode]) { auto pos = strcasestr(vhost.data(), pr.first.data()); if (pos && pos + pr.first.size() == &vhost.back() + 1) { @@ -286,6 +290,7 @@ std::shared_ptr SSL_Initor::getSSLCtxWildcards(const string &vhost, boo } std::shared_ptr SSL_Initor::getSSLCtx_l(const string &vhost_in, bool server_mode) { + std::lock_guard lck(_mtx); auto vhost = vhost_in; if (vhost.empty()) { if (!_default_vhost[server_mode].empty()) { @@ -309,6 +314,7 @@ std::shared_ptr SSL_Initor::getSSLCtx_l(const string &vhost_in, bool se } string SSL_Initor::defaultVhost(bool server_mode) { + std::lock_guard lck(_mtx); return _default_vhost[server_mode]; } diff --git a/src/Util/SSLBox.h b/src/Util/SSLBox.h index f2aeb4cb..3e543ae4 100644 --- a/src/Util/SSLBox.h +++ b/src/Util/SSLBox.h @@ -154,7 +154,7 @@ class SSL_Initor { * [AUTO-TRANSLATED:1b3438d0] */ - void setupCtx(SSL_CTX *ctx); + static void setupCtx(SSL_CTX *ctx); std::shared_ptr getSSLCtx_l(const std::string &vhost, bool server_mode); @@ -184,6 +184,7 @@ class SSL_Initor { }; private: + std::recursive_mutex _mtx; std::string _default_vhost[2]; std::shared_ptr _ctx_empty[2]; std::map, less_nocase> _ctxs[2];