From 5eba65474f9a310d2cd3ef294a95571db9f3d3a8 Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 7 Feb 2024 19:49:51 +0800 Subject: [PATCH] Update yasio to v4.2.0 --- thirdparty/README.md | 2 +- thirdparty/yasio/yasio/bindings/yasio_ni.cpp | 43 ++++--- thirdparty/yasio/yasio/config.hpp | 2 +- thirdparty/yasio/yasio/fsutils.hpp | 57 +++++++++ thirdparty/yasio/yasio/impl/mbedtls.hpp | 21 +-- thirdparty/yasio/yasio/impl/openssl.hpp | 28 ++-- thirdparty/yasio/yasio/split.hpp | 127 +++++++++++++++++++ thirdparty/yasio/yasio/ssl.hpp | 44 ------- thirdparty/yasio/yasio/yasio_fwd.hpp | 2 +- 9 files changed, 240 insertions(+), 86 deletions(-) create mode 100644 thirdparty/yasio/yasio/fsutils.hpp create mode 100644 thirdparty/yasio/yasio/split.hpp diff --git a/thirdparty/README.md b/thirdparty/README.md index 48c86a3c7b..4ae8ac39ac 100644 --- a/thirdparty/README.md +++ b/thirdparty/README.md @@ -253,7 +253,7 @@ ## yasio - [![Upstream](https://img.shields.io/github/v/release/yasio/yasio?label=Upstream)](https://github.com/yasio/yasio) -- Version: 5.0.0 +- Version: 4.2.0 - License: MIT WITH Anti-996 ## zlib diff --git a/thirdparty/yasio/yasio/bindings/yasio_ni.cpp b/thirdparty/yasio/yasio/bindings/yasio_ni.cpp index 51cd7017a3..94f32f58c9 100644 --- a/thirdparty/yasio/yasio/bindings/yasio_ni.cpp +++ b/thirdparty/yasio/yasio/bindings/yasio_ni.cpp @@ -77,21 +77,32 @@ YASIO_NI_API void yasio_init_globals(void(YASIO_INTEROP_DECL* pfn)(int level, co } YASIO_NI_API void yasio_cleanup_globals() { io_service::cleanup_globals(); } -struct yasio_event_data { - int kind; - int status; - int channel; - void* session; // transport - void* packet; - void* user; // event source +struct yasio_io_event { + int kind; // + int channel; + void* thandle; + union { + void* msg; + int status; // + }; + void* user; }; -YASIO_NI_API void* yasio_create_service(int channel_count, void(YASIO_INTEROP_DECL* event_cb)(yasio_event_data* event), void* user) + +YASIO_NI_API void* yasio_create_service(int channel_count, void(YASIO_INTEROP_DECL* event_cb)(yasio_io_event* event), void* user) { assert(!!event_cb); io_service* service = new io_service(channel_count); service->start([=](event_ptr e) { auto& pkt = e->packet(); - yasio_event_data event{e->kind(), e->status(), e->cindex(), e->transport(), !is_packet_empty(pkt) ? &pkt : nullptr, user}; + yasio_io_event event; + event.kind = e->kind(); + event.channel = e->cindex(); + event.thandle = e->transport(); + event.user = user; + if (event.kind == yasio::YEK_ON_PACKET) + event.msg = !is_packet_empty(pkt) ? &pkt : nullptr; + else + event.status = e->status(); event_cb(&event); }); return service; @@ -239,15 +250,17 @@ YASIO_NI_API int yasio_write(void* service_ptr, void* thandle, const char* bytes return service->write(reinterpret_cast(thandle), yasio::sbyte_buffer(bytes, bytes + len)); return -1; } -YASIO_NI_API int yasio_forward(void* service_ptr, void* thandle, void* bufferHandle, const char*(YASIO_INTEROP_DECL* pfnLockBuffer)(void* bufferHandle, int* bufferDataLen), void(YASIO_INTEROP_DECL* pfnUnlockBuffer)(void* bufferHandle)) +YASIO_NI_API int yasio_forward(void* service_ptr, void* thandle, void* bufferHandle, + const char*(YASIO_INTEROP_DECL* pfnLockBuffer)(void* bufferHandle, int* bufferDataLen), + void(YASIO_INTEROP_DECL* pfnUnlockBuffer)(void* bufferHandle)) { auto service = reinterpret_cast(service_ptr); - if (service) { - int len = 0; + if (service) + { + int len = 0; auto bytes = pfnLockBuffer(bufferHandle, &len); - return service->forward(reinterpret_cast(thandle), bytes, len, [bufferHandle,pfnUnlockBuffer](int,size_t){ - pfnUnlockBuffer(bufferHandle); - }); + return service->forward(reinterpret_cast(thandle), bytes, len, + [bufferHandle, pfnUnlockBuffer](int, size_t) { pfnUnlockBuffer(bufferHandle); }); } return -1; } diff --git a/thirdparty/yasio/yasio/config.hpp b/thirdparty/yasio/yasio/config.hpp index 4f4e5cfd8d..1189dd4b97 100644 --- a/thirdparty/yasio/yasio/config.hpp +++ b/thirdparty/yasio/yasio/config.hpp @@ -205,7 +205,7 @@ SOFTWARE. /* ** The yasio version macros */ -#define YASIO_VERSION_NUM 0x050000 +#define YASIO_VERSION_NUM 0x040200 /* ** The macros used by io_service. diff --git a/thirdparty/yasio/yasio/fsutils.hpp b/thirdparty/yasio/yasio/fsutils.hpp new file mode 100644 index 0000000000..46d20abb6e --- /dev/null +++ b/thirdparty/yasio/yasio/fsutils.hpp @@ -0,0 +1,57 @@ +////////////////////////////////////////////////////////////////////////////////////////// +// A multi-platform support c++11 library with focus on asynchronous socket I/O for any +// client application. +////////////////////////////////////////////////////////////////////////////////////////// +/* +The MIT License (MIT) + +Copyright (c) 2012-2024 HALX99 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#pragma once + +#include "yasio/string.hpp" +#include "yasio/string_view.hpp" +#include + +namespace yasio +{ +inline yasio::string read_text_file(cxx17::string_view file_path) +{ + std::ifstream fin(file_path.data(), std::ios_base::binary); + if (fin.is_open()) + { + fin.seekg(std::ios_base::end); + auto n = static_cast(fin.tellg()); + if (n > 0) + { + yasio::string ret; + ret.resize_and_overwrite(n, [&fin](char* out, size_t outlen) { + fin.seekg(std::ios_base::beg); + fin.read(out, outlen); + return outlen; + }); + return ret; + } + } + return yasio::string{}; +} +} // namespace yasio diff --git a/thirdparty/yasio/yasio/impl/mbedtls.hpp b/thirdparty/yasio/yasio/impl/mbedtls.hpp index 6a02ab4bac..618986d0a8 100644 --- a/thirdparty/yasio/yasio/impl/mbedtls.hpp +++ b/thirdparty/yasio/yasio/impl/mbedtls.hpp @@ -31,6 +31,8 @@ SOFTWARE. #if YASIO_SSL_BACKEND == 2 +# include "yasio/split.hpp" + YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts) { auto ctx = new yssl_ctx_st(); @@ -63,17 +65,16 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts) if (yasio__valid_str(opts.crtfile_)) // the cafile_ must be full path { int fail_count = 0; - yssl_splitpath(opts.crtfile_, [&](char* first, char* last) { - yssl_split_term null_term(last); + yasio::split( + opts.crtfile_, ',', [&](char* first, char* last) { + yasio::split_term null_term(last); - if ((ret = ::mbedtls_x509_crt_parse_file(&ctx->cert, first)) != 0) - { - ++fail_count; - YASIO_LOG("mbedtls_x509_crt_parse_file with ret=-0x%x", (unsigned int)-ret); - } - - return !!ret; - }); + if ((ret = ::mbedtls_x509_crt_parse_file(&ctx->cert, first)) != 0) + { + ++fail_count; + YASIO_LOG("mbedtls_x509_crt_parse_file with ret=-0x%x", (unsigned int)-ret); + } + }); if (!fail_count) authmode = MBEDTLS_SSL_VERIFY_REQUIRED; } diff --git a/thirdparty/yasio/yasio/impl/openssl.hpp b/thirdparty/yasio/yasio/impl/openssl.hpp index 5bacfd6dce..4c944304d6 100644 --- a/thirdparty/yasio/yasio/impl/openssl.hpp +++ b/thirdparty/yasio/yasio/impl/openssl.hpp @@ -31,6 +31,8 @@ SOFTWARE. #if YASIO_SSL_BACKEND == 1 // OpenSSL +# include "yasio/split.hpp" + // The ssl error mask (1 << 31), a little hack, but works # define YSSL_ERR_MASK 0x80000000 @@ -51,8 +53,8 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts) if (yasio__valid_str(opts.crtfile_)) { // CAfile for verify fail_count = 0; - yssl_splitpath(opts.crtfile_, [&](char* first, char* last) { - yssl_split_term null_term(last); + yasio::split(opts.crtfile_, ',', [&](char* first, char* last) { + yasio::split_term null_term(last); # if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3) /* OpenSSL 3.0.0 has deprecated SSL_CTX_load_verify_locations */ @@ -65,15 +67,13 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts) ++fail_count; YASIO_LOG("[global] load ca certifaction file failed!"); } - - return !ok; }); } /* - * client cert & key not implement yet, since it not common usecase - * SSL_CTX_use_certificate_chain_file - * SSL_CTX_use_PrivateKey_file - */ + * client cert & key not implement yet, since it not common usecase + * SSL_CTX_use_certificate_chain_file + * SSL_CTX_use_PrivateKey_file + */ if (!fail_count) { ::SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, ::SSL_CTX_get_verify_callback(ctx)); @@ -99,12 +99,12 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts) if (yasio__valid_str(opts.keyfile_) && ::SSL_CTX_use_PrivateKey_file(ctx, opts.keyfile_, SSL_FILETYPE_PEM) <= 0) YASIO_LOG("[gobal] load server private key file failed!"); - /* - * If client provide cert, then verify, otherwise skip verify - * Note: if SSL_VERIFY_FAIL_IF_NO_PEER_CERT specified, client must provide - * cert & key which is not common use, means 100% online servers doesn't require - * client to provide cert - */ + /* + * If client provide cert, then verify, otherwise skip verify + * Note: if SSL_VERIFY_FAIL_IF_NO_PEER_CERT specified, client must provide + * cert & key which is not common use, means 100% online servers doesn't require + * client to provide cert + */ ::SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, ::SSL_CTX_get_verify_callback(ctx)); } diff --git a/thirdparty/yasio/yasio/split.hpp b/thirdparty/yasio/yasio/split.hpp new file mode 100644 index 0000000000..eb821a8d4c --- /dev/null +++ b/thirdparty/yasio/yasio/split.hpp @@ -0,0 +1,127 @@ +////////////////////////////////////////////////////////////////////////////////////////// +// A multi-platform support c++11 library with focus on asynchronous socket I/O for any +// client application. +////////////////////////////////////////////////////////////////////////////////////////// +/* +The MIT License (MIT) + +Copyright (c) 2012-2024 HALX99 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +/* + * The standard split stub of yasio: + * The pred callback prototype: [](CStr first, CStr last) ->bool{ return true; } + * returns: + * true: want continue split + * false: abort split + * + */ + +#pragma once + +#include "yasio/string_view.hpp" +#include + +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable : 4706) +#endif + +namespace yasio +{ +template +inline void split_if(_CStr s, typename std::remove_pointer<_CStr>::type delim, _Pred&& pred) +{ + auto _Start = s; // the start of every string + auto _Ptr = s; // source string iterator + while ((_Ptr = strchr(_Ptr, delim))) + { + if (_Start <= _Ptr && !pred(_Start, _Ptr)) + return; + _Start = _Ptr + 1; + ++_Ptr; + } + pred(_Start, nullptr); // last one, end is nullptr +} + +template +inline void split_if_n(_CStr s, size_t slen, typename std::remove_pointer<_CStr>::type delim, _Pred&& pred) +{ + auto _Start = s; // the start of every string + auto _Ptr = s; // source string iterator + auto _End = s + slen; + while ((_Ptr = strchr(_Ptr, delim))) + { + if (_Ptr >= _End) + break; + + if (_Start <= _Ptr && !pred(_Start, _Ptr)) + return; + _Start = _Ptr + 1; + ++_Ptr; + } + if (_Start <= _End) + pred(_Start, _End); +} + +template +inline void split(_CStr s, typename std::remove_pointer<_CStr>::type delim, _Func&& func) +{ + split_if(s, delim, [func](_CStr first, _CStr last) { + func(first, last); + return true; + }); +} + +template +inline void split_n(_CStr s, size_t slen, typename std::remove_pointer<_CStr>::type delim, _Func&& func) +{ + split_if_n(s, slen, delim, [func](_CStr first, _CStr last) { + func(first, last); + return true; + }); +} + +struct split_term { + split_term(char* end) + { + if (end) + { + this->val_ = *end; + *end = '\0'; + this->end_ = end; + } + } + ~split_term() + { + if (this->end_) + *this->end_ = this->val_; + } + +private: + char* end_ = nullptr; + char val_ = '\0'; +}; +} // namespace yasio + +#if defined(_MSC_VER) +# pragma warning(pop) +#endif diff --git a/thirdparty/yasio/yasio/ssl.hpp b/thirdparty/yasio/yasio/ssl.hpp index 4f2d888058..3a54b23ab2 100644 --- a/thirdparty/yasio/yasio/ssl.hpp +++ b/thirdparty/yasio/yasio/ssl.hpp @@ -98,50 +98,6 @@ YASIO__DECL int yssl_read(yssl_st* ssl, void* data, size_t len, int& err); #define yasio__valid_str(cstr) (cstr && *cstr) #define yasio__c_str(str) (!str.empty() ? &str.front() : nullptr) -/* private use for split cert files */ -template -inline bool yssl_splitpath(char* str, _Fty&& func) -{ - auto _Start = str; // the start of every string - auto _Ptr = str; // source string iterator - bool aborted = false; - while ((_Ptr = strchr(_Ptr, ','))) - { - if (_Start <= _Ptr) - { - if ((aborted = func(_Start, _Ptr))) - break; - } - _Start = _Ptr + 1; - ++_Ptr; - } - - if (!aborted) - aborted = func(_Start, nullptr); // last one - return aborted; -} - -struct yssl_split_term { - yssl_split_term(char* end) - { - if (end) - { - this->val_ = *end; - *end = '\0'; - this->end_ = end; - } - } - ~yssl_split_term() - { - if (this->end_) - *this->end_ = this->val_; - } - -private: - char* end_ = nullptr; - char val_ = '\0'; -}; - #if YASIO_SSL_BACKEND == 1 // openssl # include "yasio/impl/openssl.hpp" #elif YASIO_SSL_BACKEND == 2 // mbedtls diff --git a/thirdparty/yasio/yasio/yasio_fwd.hpp b/thirdparty/yasio/yasio/yasio_fwd.hpp index e709670bf5..9e2aa1666e 100644 --- a/thirdparty/yasio/yasio/yasio_fwd.hpp +++ b/thirdparty/yasio/yasio/yasio_fwd.hpp @@ -40,7 +40,7 @@ class io_event; class io_channel; typedef class io_transport* transport_handle_t; } // namespace inet -#if !YASIO__HAS_NS_INLINE +#if !YASIO__HAS_CXX11 using namespace yasio::inet; #endif } // namespace yasio