diff --git a/lib_acl_cpp/include/acl_cpp/stream/sslbase_io.hpp b/lib_acl_cpp/include/acl_cpp/stream/sslbase_io.hpp index e197c2e11..164a9f6f9 100644 --- a/lib_acl_cpp/include/acl_cpp/stream/sslbase_io.hpp +++ b/lib_acl_cpp/include/acl_cpp/stream/sslbase_io.hpp @@ -71,6 +71,20 @@ public: return has_sni_; } + /** + * 设置本 SSL IO 对象的绑定对象,方便应用处理自身业务逻辑 + * @param ctx {void*} + */ + void set_ctx(void* ctx); + + /** + * 获得由 set_ctx() 设置的绑定对象 + * @return {void*} + */ + void* get_ctx() const { + return ctx_; + } + protected: sslbase_conf& base_conf_; bool server_side_; @@ -80,6 +94,7 @@ protected: ACL_VSTREAM* stream_; string sni_host_; // Just for client to set SNI. bool has_sni_; // Just for server to check SNI. + void* ctx_; // The context for every SSL IO. }; } // namespace acl diff --git a/lib_acl_cpp/samples/ssl/server/main.cpp b/lib_acl_cpp/samples/ssl/server/main.cpp index 67dd56adc..7053cafdd 100644 --- a/lib_acl_cpp/samples/ssl/server/main.cpp +++ b/lib_acl_cpp/samples/ssl/server/main.cpp @@ -16,12 +16,19 @@ public: bool check(acl::sslbase_io* io, const char* sni, acl::string& host) { if (io) { io->set_has_sni(true); + acl::sslbase_io* me = (acl::sslbase_io*) io->get_ctx(); + if (io != me) { + printf("Invalid io=%p, me=%p\r\n", io, me); + return false; + } + printf("ssl_sni_checker::check: sslbase_io=%p\r\n", io); + } else { + printf("ssl_sni_checker::check: sslbase_io=NULL\r\n"); } - printf("ssl_sni_checker::check: sslbase_io=%p\n", io); if (sni == NULL || *sni == 0) { - printf("Invalid SNI\r\n"); + printf("Invalid SNI=%p\r\n", sni); return false; } @@ -74,6 +81,9 @@ private: bool non_block = false; acl::sslbase_io* ssl = ssl_conf_.create(non_block); + // 设置私有对象,在 ssl_sni_checker::check() 中检查 + ssl->set_ctx(ssl); + // 对于使用 SSL 方式的流对象,需要将 SSL IO 流对象注册至网络 // 连接流对象中,即用 ssl io 替换 stream 中默认的底层 IO 过程 if (conn_->setup_hook(ssl) == ssl) { diff --git a/lib_acl_cpp/src/stream/sslbase_io.cpp b/lib_acl_cpp/src/stream/sslbase_io.cpp index e9c9d7643..54acfe4fd 100644 --- a/lib_acl_cpp/src/stream/sslbase_io.cpp +++ b/lib_acl_cpp/src/stream/sslbase_io.cpp @@ -17,6 +17,7 @@ sslbase_io::sslbase_io(sslbase_conf& conf, bool server_side, , handshake_ok_(false) , stream_(NULL) , has_sni_(false) +, ctx_(NULL) { refers_ = NEW atomic_long(0); } @@ -53,4 +54,8 @@ void sslbase_io::set_has_sni(bool yes) { has_sni_ = yes; } +void sslbase_io::set_ctx(void *ctx) { + ctx_ = ctx; +} + } // namespace acl