|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 Codership Oy <[email protected]> |
| 3 | + * |
| 4 | + * This file is part of wsrep-lib. |
| 5 | + * |
| 6 | + * Wsrep-lib is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Wsrep-lib is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "connection_monitor_service_v1.hpp" |
| 21 | +#include "service_helpers.hpp" |
| 22 | + |
| 23 | +#include "wsrep/buffer.hpp" |
| 24 | +#include "v26/wsrep_connection_monitor_service.h" |
| 25 | +#include "wsrep/connection_monitor_service.hpp" |
| 26 | + |
| 27 | +#include <cassert> |
| 28 | +#include <dlfcn.h> |
| 29 | +#include <cerrno> |
| 30 | + |
| 31 | +namespace wsrep_connection_monitor_service_v1 |
| 32 | +{ |
| 33 | + // Pointer to connection monitor service implementation provided by |
| 34 | + // the application. |
| 35 | + static wsrep::connection_monitor_service* connection_monitor_service_impl{ 0 }; |
| 36 | + static std::atomic<size_t> use_count; |
| 37 | + |
| 38 | + // |
| 39 | + // connection monitor service callbacks |
| 40 | + // |
| 41 | + |
| 42 | + void connection_monitor_connect_cb( |
| 43 | + wsrep_connection_monitor_context_t*, |
| 44 | + wsrep_connection_key_t id, |
| 45 | + const wsrep_buf_t* scheme, |
| 46 | + const wsrep_buf_t* local_address, |
| 47 | + const wsrep_buf_t* remote_address |
| 48 | + ) |
| 49 | + { |
| 50 | + assert(connection_monitor_service_impl); |
| 51 | + wsrep::const_buffer scheme_value(scheme->ptr, scheme->len); |
| 52 | + wsrep::const_buffer remote_addr(remote_address->ptr, remote_address->len); |
| 53 | + wsrep::const_buffer local_addr(local_address->ptr, local_address->len); |
| 54 | + connection_monitor_service_impl->connection_monitor_connect_cb( |
| 55 | + id, |
| 56 | + scheme_value, |
| 57 | + local_addr, |
| 58 | + remote_addr); |
| 59 | + } |
| 60 | + |
| 61 | + void connection_monitor_disconnect_cb( |
| 62 | + wsrep_connection_monitor_context_t*, |
| 63 | + wsrep_connection_key_t id |
| 64 | + ) |
| 65 | + { |
| 66 | + assert(connection_monitor_service_impl); |
| 67 | + connection_monitor_service_impl->connection_monitor_disconnect_cb(id); |
| 68 | + } |
| 69 | + |
| 70 | + void connection_monitor_ssl_info_cb( |
| 71 | + wsrep_connection_monitor_context_t*, |
| 72 | + wsrep_connection_key_t id, |
| 73 | + const wsrep_buf_t* cipher, |
| 74 | + const wsrep_buf_t* certificate_subject, |
| 75 | + const wsrep_buf_t* certificate_issuer, |
| 76 | + const wsrep_buf_t* version) |
| 77 | + { |
| 78 | + assert(connection_monitor_service_impl); |
| 79 | + wsrep::const_buffer ssl_cipher(cipher->ptr, cipher->len); |
| 80 | + wsrep::const_buffer cert_sub(certificate_subject->ptr, certificate_subject->len); |
| 81 | + wsrep::const_buffer cert_iss(certificate_issuer->ptr, certificate_issuer->len); |
| 82 | + wsrep::const_buffer vers(version->ptr, version->len); |
| 83 | + connection_monitor_service_impl->connection_monitor_ssl_info_cb( |
| 84 | + id, ssl_cipher, cert_sub, cert_iss, vers); |
| 85 | + } |
| 86 | + |
| 87 | + static wsrep_connection_monitor_service_v1_t connection_monitor_service_callbacks |
| 88 | + = { connection_monitor_connect_cb, |
| 89 | + connection_monitor_disconnect_cb, |
| 90 | + connection_monitor_ssl_info_cb, |
| 91 | + 0 }; |
| 92 | +} |
| 93 | + |
| 94 | +int wsrep::connection_monitor_service_v1_probe(void* dlh) |
| 95 | +{ |
| 96 | + typedef int (*init_fn)(wsrep_connection_monitor_service_v1_t*); |
| 97 | + typedef void (*deinit_fn)(); |
| 98 | + if (wsrep_impl::service_probe<init_fn>( |
| 99 | + dlh, WSREP_CONNECTION_MONITOR_SERVICE_INIT_FUNC_V1, "connection monitor service v1") || |
| 100 | + wsrep_impl::service_probe<deinit_fn>( |
| 101 | + dlh, WSREP_CONNECTION_MONITOR_SERVICE_DEINIT_FUNC_V1, "connection monitor service v1")) |
| 102 | + { |
| 103 | + wsrep::log_warning() << "Provider does not support connection monitor service v1"; |
| 104 | + return 1; |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +int wsrep::connection_monitor_service_v1_init(void* dlh, |
| 111 | + wsrep::connection_monitor_service* connection_service) |
| 112 | +{ |
| 113 | + if (not (dlh && connection_service)) return EINVAL; |
| 114 | + typedef int (*init_fn)(wsrep_connection_monitor_service_v1_t*); |
| 115 | + wsrep_connection_monitor_service_v1::connection_monitor_service_impl = connection_service; |
| 116 | + int ret(0); |
| 117 | + if ((ret = wsrep_impl::service_init<init_fn>( |
| 118 | + dlh, WSREP_CONNECTION_MONITOR_SERVICE_INIT_FUNC_V1, |
| 119 | + &wsrep_connection_monitor_service_v1::connection_monitor_service_callbacks, |
| 120 | + "connection monitor service v1"))) |
| 121 | + { |
| 122 | + wsrep_connection_monitor_service_v1::connection_monitor_service_impl = 0; |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + ++wsrep_connection_monitor_service_v1::use_count; |
| 127 | + } |
| 128 | + return ret; |
| 129 | +} |
| 130 | + |
| 131 | +void wsrep::connection_monitor_service_v1_deinit(void* dlh) |
| 132 | +{ |
| 133 | + typedef int (*deinit_fn)(); |
| 134 | + wsrep_impl::service_deinit<deinit_fn>( |
| 135 | + dlh, WSREP_CONNECTION_MONITOR_SERVICE_DEINIT_FUNC_V1, "connection monitor service v1"); |
| 136 | + assert(wsrep_connection_monitor_service_v1::use_count > 0); |
| 137 | + --wsrep_connection_monitor_service_v1::use_count; |
| 138 | + if (wsrep_connection_monitor_service_v1::use_count == 0) |
| 139 | + { |
| 140 | + wsrep_connection_monitor_service_v1::connection_monitor_service_impl = 0; |
| 141 | + } |
| 142 | +} |
0 commit comments