MySQL 9.1.0
Source Code Documentation
xcom_network_provider_native_lib.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef XCOM_NETWORK_PROVIDER_NATIVE_LIB_H
25#define XCOM_NETWORK_PROVIDER_NATIVE_LIB_H
26
27#include "xcom/result.h"
28#include "xcom/site_def.h"
29
30#ifndef XCOM_WITHOUT_OPENSSL
31#ifdef _WIN32
32/* In OpenSSL before 1.1.0, we need this first. */
33#include <Ws2tcpip.h>
34#include <winsock2.h>
35#endif /* _WIN32 */
36
37#include <openssl/err.h>
38#include <openssl/ssl.h>
39#endif /*! XCOM_WITHOUT_OPENSSL*/
40
41#define SYS_STRERROR_SIZE 512
42
44 public:
45 static result checked_create_socket(int domain, int type, int protocol);
46 static struct addrinfo *does_node_have_v4_address(struct addrinfo *retrieved);
47 static int timed_connect(int fd, struct sockaddr *sock_addr,
48 socklen_t sock_size);
49 static int timed_connect_sec(int fd, struct sockaddr *sock_addr,
50 socklen_t sock_size, int timeout);
51 static int timed_connect_msec(int fd, struct sockaddr *sock_addr,
52 socklen_t sock_size, int timeout);
53 static int allowlist_socket_accept(int fd, site_def const *xcom_config);
56
57 /**
58 * @brief Auxiliary method used in Synchronous connects in order to poll a
59 * connection for input, until a certain timeout is reached, and check
60 * all errors that might come out of the poll() call.
61 *
62 * @param fd file descriptor to poll
63 * @param timeout timeout (in msecs) to wait for activity in fd
64 *
65 * @return true in case of any error, false otherwise.
66 */
67 static bool poll_for_timed_connects(int fd, int timeout);
68
69 /**
70 * @brief After a poll call, this method will check for any errors that might
71 * have been returned from the poll itself.
72 *
73 * @param fd The file descriptor being poll-ed
74 * @param sysret Return value from poll call
75 * @param fds struct pollfd that contains fd used to call poll
76 *
77 * @return true In case of error
78 * @return false otherwise
79 */
80 static bool verify_poll_errors(int fd, int sysret, struct pollfd &fds);
81
82 private:
83 static void init_server_addr(struct sockaddr **sock_addr, socklen_t *sock_len,
84 xcom_port port, int family);
85 static result xcom_checked_socket(int domain, int type, int protocol);
88 static void gcs_shutdown_socket(int *sock);
89 static result gcs_close_socket(int *sock);
90};
91
92#ifndef XCOM_WITHOUT_OPENSSL
93
94#ifndef SSL_SUCCESS
95#define SSL_SUCCESS 1
96#define SSL_ERROR 0
97#endif
98
100 public:
101 /*
102 Initialize the SSL.
103
104 server_key_file - Path of file that contains the server's X509 key in PEM
105 format.
106 server_cert_file - Path of file that contains the server's X509 certificate
107 in PEM format. client_key_file - Path of file that contains the client's
108 X509 key in PEM format. client_cert_file - Path of file that contains the
109 client's X509 certificate in PEM format. ca_file - Path of file
110 that contains list of trusted SSL CAs. ca_path - Path of directory
111 that contains trusted SSL CA certificates in PEM format. crl_file -
112 Path of file that contains certificate revocation lists. crl_path -
113 Path of directory that contains certificate revocation list files. cipher -
114 List of permitted ciphers to use for connection encryption. tls_version -
115 Protocols permitted for secure connections.
116
117 Note that only the server_key_file/server_cert_file and the client_key_file/
118 client_cert_file are required and the rest of the pointers can be NULL.
119 If the key is provided along with the certificate, either the key file or
120 the other can be omitted.
121
122 The caller can free the parameters after the call if this is necessary.
123
124 Return 0 if success 1 otherwise.
125 */
126 static int xcom_init_ssl(const char *server_key_file,
127 const char *server_cert_file,
128 const char *client_key_file,
129 const char *client_cert_file, const char *ca_file,
130 const char *ca_path, const char *crl_file,
131 const char *crl_path, const char *cipher,
132 const char *tls_version,
133 const char *tls_ciphersuites);
134
135 /*
136 Cleans Up the SSL Configuration freeing allocated memory.
137 */
138 static void xcom_cleanup_ssl();
139
140 /*
141 Destroy the SSL Configuration freeing allocated memory.
142 */
143 static void xcom_destroy_ssl();
144
145 /*
146 Verify whether the server certificate matches the host to which
147 the connection is attempted.
148 */
149 static int ssl_verify_server_cert(SSL *ssl, const char *server_hostname);
150
151 /**
152 * @brief Establishes an SSL connection to a node that already has a
153 * connection in place.
154 *
155 * It is asynchronous in nature, since it unblocks the socket, establishes
156 * the connection and wait for the result via a poll mechanism no more
157 * than timeout miliseconds.
158 *
159 * It returns a pair that contains the established SSL connection and an
160 * error code (0 in case of success.)
161 *
162 * @param fd an established connection file descriptor.
163 * @param client_ctx the client context to use.
164 * @param hostname hostname to validate
165 * @param timeout timeout value in miliseconds.
166 *
167 * @return std::pair<SSL *, int> a pair containing:
168 * - An established SSL connection, or
169 * nullptr
170 * - 0 in case of success. An error code,
171 * otherwise.
172 */
173 static std::pair<SSL *, int> timed_connect_ssl_msec(
174 int fd, SSL_CTX *client_ctx, const std::string &hostname, int timeout);
175};
176
177/*
178 Pointers to the SSL Context for the server and client
179 contexts respectively.
180*/
181extern SSL_CTX *server_ctx;
182extern SSL_CTX *client_ctx;
183
184#endif /* !XCOM_WITHOUT_OPENSSL */
185#endif // XCOM_NETWORK_PROVIDER_H
Definition: xcom_network_provider_native_lib.h:43
static bool verify_poll_errors(int fd, int sysret, struct pollfd &fds)
After a poll call, this method will check for any errors that might have been returned from the poll ...
Definition: xcom_network_provider_native_lib.cc:384
static result create_server_socket()
Definition: xcom_network_provider_native_lib.cc:126
static result announce_tcp(xcom_port port)
Definition: xcom_network_provider_native_lib.cc:207
static int timed_connect(int fd, struct sockaddr *sock_addr, socklen_t sock_size)
Definition: xcom_network_provider_native_lib.cc:469
static int timed_connect_msec(int fd, struct sockaddr *sock_addr, socklen_t sock_size, int timeout)
Definition: xcom_network_provider_native_lib.cc:419
static struct addrinfo * does_node_have_v4_address(struct addrinfo *retrieved)
Retrieves a node IPv4 address, if it exists.
Definition: xcom_network_provider_native_lib.cc:526
static void gcs_shutdown_socket(int *sock)
Definition: xcom_network_provider_native_lib.cc:312
static int timed_connect_sec(int fd, struct sockaddr *sock_addr, socklen_t sock_size, int timeout)
Definition: xcom_network_provider_native_lib.cc:476
static result gcs_close_socket(int *sock)
Definition: xcom_network_provider_native_lib.cc:318
static result checked_create_socket(int domain, int type, int protocol)
Definition: xcom_network_provider_native_lib.cc:484
static bool poll_for_timed_connects(int fd, int timeout)
Auxiliary method used in Synchronous connects in order to poll a connection for input,...
Definition: xcom_network_provider_native_lib.cc:342
static result xcom_checked_socket(int domain, int type, int protocol)
Wrapper function which retries and checks errors from socket.
Definition: xcom_network_provider_native_lib.cc:111
static int allowlist_socket_accept(int fd, site_def const *xcom_config)
Definition: xcom_network_provider_native_lib.cc:286
static result create_server_socket_v4()
Definition: xcom_network_provider_native_lib.cc:177
static result gcs_shut_close_socket(int *sock)
Definition: xcom_network_provider_native_lib.cc:329
static void init_server_addr(struct sockaddr **sock_addr, socklen_t *sock_len, xcom_port port, int family)
Initializes a sockaddr prepared to be used in bind()
Definition: xcom_network_provider_native_lib.cc:77
Definition: xcom_network_provider_native_lib.h:99
static void xcom_cleanup_ssl()
Definition: xcom_network_provider_ssl_native_lib.cc:555
static std::pair< SSL *, int > timed_connect_ssl_msec(int fd, SSL_CTX *client_ctx, const std::string &hostname, int timeout)
Establishes an SSL connection to a node that already has a connection in place.
Definition: xcom_network_provider_ssl_native_lib.cc:703
static int ssl_verify_server_cert(SSL *ssl, const char *server_hostname)
Definition: xcom_network_provider_ssl_native_lib.cc:588
static void xcom_destroy_ssl()
Definition: xcom_network_provider_ssl_native_lib.cc:561
static int xcom_init_ssl(const char *server_key_file, const char *server_cert_file, const char *client_key_file, const char *client_cert_file, const char *ca_file, const char *ca_path, const char *crl_file, const char *crl_path, const char *cipher, const char *tls_version, const char *tls_ciphersuites)
Definition: xcom_network_provider_ssl_native_lib.cc:477
static MYSQL * sock
Definition: mysqlcheck.cc:57
constexpr value_type ssl
Definition: classic_protocol_constants.h:49
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
required string type
Definition: replication_group_member_actions.proto:34
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
Definition: result.h:30
Definition: site_struct.h:43
struct pollfd pollfd
Definition: task_os.h:114
unsigned short xcom_port
Definition: xcom_common.h:46
SSL_CTX * client_ctx
Definition: xcom_network_provider_ssl_native_lib.cc:173
SSL_CTX * server_ctx
Definition: xcom_network_provider_ssl_native_lib.cc:172