MySQL 8.0.40
Source Code Documentation
socket_connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2013, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef SOCKET_CONNECTION_INCLUDED
27#define SOCKET_CONNECTION_INCLUDED
28
29#include "my_config.h"
30
31#include <sys/types.h>
32#include <list>
33#include <map>
34#include <string>
35#include <vector>
36
37#include "my_psi_config.h"
39#include "mysql/psi/mysql_socket.h" // MYSQL_SOCKET
40#ifdef HAVE_POLL_H
41#include <poll.h>
42#endif
43
44class Channel_info;
45
46extern const char *MY_BIND_ALL_ADDRESSES;
47extern const char *ipv4_all_addresses;
48extern const char *ipv6_all_addresses;
49
50#ifdef HAVE_PSI_STATEMENT_INTERFACE
52#endif
53
54// Enum denoting type of socket whether unix socket or tcp socket.
56// Enum denoting the interface which the socket listens to.
58// Listen socket and it's attributes.
62 m_socket_type(socket_type),
66 const std::string *network_namespace,
67 Socket_interface_type socket_interface)
69 m_socket_type(socket_type),
71 m_socket_interface(socket_interface) {}
74 const std::string *m_network_namespace;
76 m_socket_interface; // Interface type which the socket listens for.
77};
78
79// typedef for a container holding sockets which the server is listening for
80// connections.
81typedef std::vector<Listen_socket> socket_vector_t;
82
83/**
84 Plain structure to collect together a host name/ip address and
85 a corresponding network namespace if set and pass these information
86 to different functions as a single unit.
87*/
90 Bind_address_info() = default;
91
92 explicit Bind_address_info(const std::string &addr) : address(addr) {}
93
94 Bind_address_info(const std::string &addr, const std::string &nspace)
95 : address(addr), network_namespace(nspace) {}
96};
97
98/**
99 This class represents the Mysqld_socket_listener which prepares the
100 listener sockets to receive connection events from the client. The
101 Mysqld_socket_listener may be composed of either or both a tcp socket
102 which listens on a default mysqld tcp port or a user specified port
103 via mysqld command-line and a unix socket which is bound to a mysqld
104 default pathname.
105*/
107 /*
108 Addresses to listen to and network namespace for
109 every address if set.
110 */
111 std::list<Bind_address_info> m_bind_addresses;
112 /*
113 Address to listen to an admin connection request
114 and network namespace if set.
115 */
117 uint m_tcp_port; // TCP port to bind to
118 uint m_admin_tcp_port; // TCP port to bind to for support admin connection
119 bool m_use_separate_thread_for_admin; // use a separate thread for listening
120 // to admin interface
121 uint m_backlog; // backlog specifying length of pending connection queue
122 uint m_port_timeout; // port timeout value
123 std::string m_unix_sockname; // unix socket pathname to bind to
124 bool m_unlink_sockname; // Unlink socket & lock file if true.
125 // Container storing listen socket and their attributes.
128
129#ifdef HAVE_POLL
130 struct poll_info_t {
131 std::vector<struct pollfd> m_fds;
132 std::vector<MYSQL_SOCKET> m_pfs_fds;
133 };
134 // poll related info. used in poll for listening to connection events.
136#else
137 struct select_info_t {
138 fd_set m_read_fds, m_client_fds;
139 my_socket m_max_used_connection;
140 select_info_t() : m_max_used_connection(0) { FD_ZERO(&m_client_fds); }
141 };
142 // select info for used in select for listening to connection events.
143 select_info_t m_select_info;
144#endif // HAVE_POLL
145
146 public:
147 /**
148 Constructor to setup a listener for listen to connect events from
149 clients.
150
151 @param bind_addresses list of addresses to listen to
152 @param tcp_port TCP port to bind to
153 @param admin_bind_addr address to listen admin connection
154 @param admin_tcp_port TCP port for admin connection to bind
155 @param use_separate_thread_for_admin Listen to connection requests
156 on admin interface in a separate thread
157 @param backlog backlog specifying length of pending
158 connection queue used in listen.
159 @param port_timeout portname.
160 @param unix_sockname pathname for unix socket to bind to
161 */
162 Mysqld_socket_listener(const std::list<Bind_address_info> &bind_addresses,
164 const Bind_address_info &admin_bind_addr,
165 uint admin_tcp_port,
166 bool use_separate_thread_for_admin, uint backlog,
167 uint port_timeout, std::string unix_sockname);
168
169 /**
170 Set up a listener - set of sockets to listen for connection events
171 from clients.
172
173 In case a server is started with the option
174 use_separate_thread_for_admin=true invocation of this method also spawns a
175 thread to handle incoming connection requests on admin interface.
176
177 @retval false listener sockets setup to be used to listen for connect
178 events true failure in setting up the listener.
179 */
180 bool setup_listener();
181
182 /**
183 The body of the event loop that listen for connection events from clients.
184
185 @retval Channel_info Channel_info object abstracting the connected client
186 details for processing this connection.
187 */
189
190 /**
191 Close the listener.
192
193 In case a server is started with the option
194 use_separate_thread_for_admin=true this method also shutdowns a thread for
195 handling of incoming connection requests on admin interface and joins it.
196 */
197 void close_listener();
198
200 if (!m_socket_vector.empty()) close_listener();
201 }
202
203 /**
204 Spawn admin connection handler thread if separate thread is required to
205 accept admin connections.
206
207 @return true unable to spawn admin connect handler thread else false
208 */
210
211 private:
212 /**
213 Add a socket to a set of sockets being waiting for a new
214 connection request.
215
216 @param listen_socket Socket to listen for.
217 */
218 void add_socket_to_listener(MYSQL_SOCKET listen_socket);
219
220 /**
221 Get a socket ready to accept incoming connection.
222 @return A socket ready to accept a new incoming connection.
223 */
224 const Listen_socket *get_listen_socket() const;
225
226 /**
227 Set up connection events for poll or select.
228
229 @param socket_vector sockets to listen for connection requests.
230 */
231 void setup_connection_events(const socket_vector_t &socket_vector);
232};
233
235
237
239
240#endif // SOCKET_CONNECTION_INCLUDED.
This abstract base class represents connection channel information about a new connection.
Definition: channel_info.h:47
This class represents the Mysqld_socket_listener which prepares the listener sockets to receive conne...
Definition: socket_connection.h:106
void close_listener()
Close the listener.
Definition: socket_connection.cc:1442
std::string m_unix_sockname
Definition: socket_connection.h:123
void setup_connection_events(const socket_vector_t &socket_vector)
Set up connection events for poll or select.
Definition: socket_connection.cc:888
uint m_port_timeout
Definition: socket_connection.h:122
bool m_use_separate_thread_for_admin
Definition: socket_connection.h:119
uint m_backlog
Definition: socket_connection.h:121
MYSQL_SOCKET m_admin_interface_listen_socket
Definition: socket_connection.h:127
uint m_tcp_port
Definition: socket_connection.h:117
std::list< Bind_address_info > m_bind_addresses
Definition: socket_connection.h:111
bool m_unlink_sockname
Definition: socket_connection.h:124
Channel_info * listen_for_connection_event()
The body of the event loop that listen for connection events from clients.
Definition: socket_connection.cc:1348
const Listen_socket * get_listen_socket() const
Get a socket ready to accept incoming connection.
Definition: socket_connection.cc:1306
uint m_admin_tcp_port
Definition: socket_connection.h:118
bool setup_listener()
Set up a listener - set of sockets to listen for connection events from clients.
Definition: socket_connection.cc:1250
poll_info_t m_poll_info
Definition: socket_connection.h:135
~Mysqld_socket_listener()
Definition: socket_connection.h:199
Bind_address_info m_admin_bind_address
Definition: socket_connection.h:116
Mysqld_socket_listener(const std::list< Bind_address_info > &bind_addresses, uint tcp_port, const Bind_address_info &admin_bind_addr, uint admin_tcp_port, bool use_separate_thread_for_admin, uint backlog, uint port_timeout, std::string unix_sockname)
Constructor to setup a listener for listen to connect events from clients.
Definition: socket_connection.cc:839
void add_socket_to_listener(MYSQL_SOCKET listen_socket)
Add a socket to a set of sockets being waiting for a new connection request.
Definition: socket_connection.cc:872
bool check_and_spawn_admin_connection_handler_thread() const
Spawn admin connection handler thread if separate thread is required to accept admin connections.
Definition: socket_connection.cc:1240
socket_vector_t m_socket_vector
Definition: socket_connection.h:126
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
int my_socket
Definition: mysql.h:65
static uint tcp_port
Definition: mysqladmin.cc:70
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:63
Performance schema instrumentation interface.
required string network_namespace
Definition: replication_asynchronous_connection_failover.proto:34
std::vector< Listen_socket > socket_vector_t
Definition: socket_connection.h:81
ulong get_connection_errors_query_block()
Definition: socket_connection.cc:123
const char * ipv6_all_addresses
Definition: socket_connection.cc:296
Socket_interface_type
Definition: socket_connection.h:57
const char * ipv4_all_addresses
Definition: socket_connection.cc:294
Socket_type
Definition: socket_connection.h:55
ulong get_connection_errors_accept()
Definition: socket_connection.cc:127
PSI_statement_info stmt_info_new_packet
Definition: init_net_server_extension.cc:50
const char * MY_BIND_ALL_ADDRESSES
MY_BIND_ALL_ADDRESSES defines a special value for the bind-address option, which means that the serve...
Definition: socket_connection.cc:292
ulong get_connection_errors_tcpwrap()
Definition: socket_connection.cc:129
Plain structure to collect together a host name/ip address and a corresponding network namespace if s...
Definition: socket_connection.h:88
std::string network_namespace
Definition: socket_connection.h:89
Bind_address_info(const std::string &addr)
Definition: socket_connection.h:92
Bind_address_info()=default
Bind_address_info(const std::string &addr, const std::string &nspace)
Definition: socket_connection.h:94
std::string address
Definition: socket_connection.h:89
Definition: socket_connection.h:59
Socket_type m_socket_type
Definition: socket_connection.h:73
const std::string * m_network_namespace
Definition: socket_connection.h:74
Socket_interface_type m_socket_interface
Definition: socket_connection.h:76
Listen_socket(MYSQL_SOCKET socket, Socket_type socket_type, const std::string *network_namespace, Socket_interface_type socket_interface)
Definition: socket_connection.h:65
Listen_socket(MYSQL_SOCKET socket, Socket_type socket_type)
Definition: socket_connection.h:60
MYSQL_SOCKET m_socket
Definition: socket_connection.h:72
An instrumented socket.
Definition: mysql_socket_bits.h:35
Definition: socket_connection.h:130
std::vector< struct pollfd > m_fds
Definition: socket_connection.h:131
std::vector< MYSQL_SOCKET > m_pfs_fds
Definition: socket_connection.h:132
Statement instrument information.
Definition: psi_statement_bits.h:133
unsigned int uint
Definition: uca9-dump.cc:75