MySQL  8.0.27
Source Code Documentation
socket_connection.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013, 2021, 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 also distributed 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 included with MySQL.
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 
25 #ifndef SOCKET_CONNECTION_INCLUDED
26 #define SOCKET_CONNECTION_INCLUDED
27 
28 #include "my_config.h"
29 
30 #include <sys/types.h>
31 #include <list>
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 #include "my_psi_config.h"
38 #include "mysql/psi/mysql_socket.h" // MYSQL_SOCKET
39 #ifdef HAVE_POLL_H
40 #include <poll.h>
41 #endif
42 
43 class Channel_info;
44 
45 extern const char *MY_BIND_ALL_ADDRESSES;
46 extern const char *ipv4_all_addresses;
47 extern const char *ipv6_all_addresses;
48 
49 #ifdef HAVE_PSI_STATEMENT_INTERFACE
51 #endif
52 
53 // Enum denoting type of socket whether unix socket or tcp socket.
55 // Enum denoting the interface which the socket listens to.
57 // Listen socket and it's attributes.
58 struct Listen_socket {
60  : m_socket(socket),
61  m_socket_type(socket_type),
65  const std::string *network_namespace,
66  Socket_interface_type socket_interface)
67  : m_socket(socket),
68  m_socket_type(socket_type),
70  m_socket_interface(socket_interface) {}
73  const std::string *m_network_namespace;
75  m_socket_interface; // Interface type which the socket listens for.
76 };
77 
78 // typedef for a container holding sockets which the server is listening for
79 // connections.
80 typedef std::vector<Listen_socket> socket_vector_t;
81 
82 /**
83  Plain structure to collect together a host name/ip address and
84  a corresponding network namespace if set and pass these information
85  to different functions as a single unit.
86 */
88  std::string address, network_namespace;
89  Bind_address_info() = default;
90 
91  explicit Bind_address_info(const std::string &addr) : address(addr) {}
92 
93  Bind_address_info(const std::string &addr, const std::string &nspace)
94  : address(addr), network_namespace(nspace) {}
95 };
96 
97 /**
98  This class represents the Mysqld_socket_listener which prepare the
99  listener sockets to recieve connection events from the client. The
100  Mysqld_socket_listener may be composed of either or both a tcp socket
101  which listen on a default mysqld tcp port or a user specified port
102  via mysqld command-line and a unix socket which is bind to a mysqld
103  defaul pathname.
104 */
106  /*
107  Addresses to listen to and network namespace for
108  every address if set.
109  */
110  std::list<Bind_address_info> m_bind_addresses;
111  /*
112  Address to listen to an admin connection request
113  and network namespace if set.
114  */
116  uint m_tcp_port; // TCP port to bind to
117  uint m_admin_tcp_port; // TCP port to bind to for support admin connection
118  bool m_use_separate_thread_for_admin; // use a separate thread for listening
119  // to admin interface
120  uint m_backlog; // backlog specifying length of pending connection queue
121  uint m_port_timeout; // port timeout value
122  std::string m_unix_sockname; // unix socket pathname to bind to
123  bool m_unlink_sockname; // Unlink socket & lock file if true.
124  // Container storing listen socket and their attributes.
127 
128 #ifdef HAVE_POLL
129  struct poll_info_t {
130  std::vector<struct pollfd> m_fds;
131  std::vector<MYSQL_SOCKET> m_pfs_fds;
132  };
133  // poll related info. used in poll for listening to connection events.
135 #else
136  struct select_info_t {
137  fd_set m_read_fds, m_client_fds;
138  my_socket m_max_used_connection;
139  select_info_t() : m_max_used_connection(0) { FD_ZERO(&m_client_fds); }
140  };
141  // select info for used in select for listening to connection events.
142  select_info_t m_select_info;
143 #endif // HAVE_POLL
144 
145  public:
146  /**
147  Constructor to setup a listener for listen to connect events from
148  clients.
149 
150  @param bind_addresses list of addresses to listen to
151  @param tcp_port TCP port to bind to
152  @param admin_bind_addr address to listen admin connection
153  @param admin_tcp_port TCP port for admin connection to bind
154  @param use_separate_thread_for_admin Listen to connection requests
155  on admin interface in a separate thread
156  @param backlog backlog specifying length of pending
157  connection queue used in listen.
158  @param port_timeout portname.
159  @param unix_sockname pathname for unix socket to bind to
160  */
161  Mysqld_socket_listener(const std::list<Bind_address_info> &bind_addresses,
162  uint tcp_port,
163  const Bind_address_info &admin_bind_addr,
164  uint admin_tcp_port,
165  bool use_separate_thread_for_admin, uint backlog,
166  uint port_timeout, std::string unix_sockname);
167 
168  /**
169  Set up a listener - set of sockets to listen for connection events
170  from clients.
171 
172  In case a server is started with the option
173  use_separate_thread_for_admin=true invocation of this method also spawns a
174  thread to handle incoming connection requests on admin interface.
175 
176  @retval false listener sockets setup to be used to listen for connect
177  events true failure in setting up the listener.
178  */
179  bool setup_listener();
180 
181  /**
182  The body of the event loop that listen for connection events from clients.
183 
184  @retval Channel_info Channel_info object abstracting the connected client
185  details for processing this connection.
186  */
188 
189  /**
190  Close the listener.
191 
192  In case a server is started with the option
193  use_separate_thread_for_admin=true this method also shutdowns a thread for
194  handling of incoming connection requests on admin interface and joins it.
195  */
196  void close_listener();
197 
199  if (!m_socket_vector.empty()) close_listener();
200  }
201 
202  /**
203  Spawn admin connection handler thread if separate thread is required to
204  accept admin connections.
205 
206  @return true unable to spawn admin connect handler thread else false
207  */
209 
210  private:
211  /**
212  Add a socket to a set of sockets being waiting for a new
213  connection request.
214 
215  @param listen_socket Socket to listen for.
216  */
217  void add_socket_to_listener(MYSQL_SOCKET listen_socket);
218 
219  /**
220  Get a socket ready to accept incoming connection.
221  @return A socket ready to accept a new incoming connection.
222  */
223  const Listen_socket *get_listen_socket() const;
224 
225  /**
226  Set up connection events for poll or select.
227 
228  @param socket_vector sockets to listen for connection requests.
229  */
230  void setup_connection_events(const socket_vector_t &socket_vector);
231 };
232 
234 
236 
238 
239 #endif // SOCKET_CONNECTION_INCLUDED.
This abstract base class represents connection channel information about a new connection.
Definition: channel_info.h:46
This class represents the Mysqld_socket_listener which prepare the listener sockets to recieve connec...
Definition: socket_connection.h:105
void close_listener()
Close the listener.
Definition: socket_connection.cc:1434
std::string m_unix_sockname
Definition: socket_connection.h:122
void setup_connection_events(const socket_vector_t &socket_vector)
Set up connection events for poll or select.
Definition: socket_connection.cc:880
uint m_port_timeout
Definition: socket_connection.h:121
bool m_use_separate_thread_for_admin
Definition: socket_connection.h:118
uint m_backlog
Definition: socket_connection.h:120
MYSQL_SOCKET m_admin_interface_listen_socket
Definition: socket_connection.h:126
uint m_tcp_port
Definition: socket_connection.h:116
std::list< Bind_address_info > m_bind_addresses
Definition: socket_connection.h:110
bool m_unlink_sockname
Definition: socket_connection.h:123
Channel_info * listen_for_connection_event()
The body of the event loop that listen for connection events from clients.
Definition: socket_connection.cc:1340
const Listen_socket * get_listen_socket() const
Get a socket ready to accept incoming connection.
Definition: socket_connection.cc:1298
uint m_admin_tcp_port
Definition: socket_connection.h:117
bool setup_listener()
Set up a listener - set of sockets to listen for connection events from clients.
Definition: socket_connection.cc:1242
poll_info_t m_poll_info
Definition: socket_connection.h:134
~Mysqld_socket_listener()
Definition: socket_connection.h:198
Bind_address_info m_admin_bind_address
Definition: socket_connection.h:115
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:831
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:864
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:1232
socket_vector_t m_socket_vector
Definition: socket_connection.h:125
Dialog Client Authentication nullptr
Definition: dialog.cc:352
int my_socket
Definition: my_io_bits.h:59
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
static uint tcp_port
Definition: mysqladmin.cc:69
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:56
Performance schema instrumentation interface.
required string network_namespace
Definition: replication_asynchronous_connection_failover.proto:33
std::vector< Listen_socket > socket_vector_t
Definition: socket_connection.h:80
ulong get_connection_errors_query_block()
Definition: socket_connection.cc:119
const char * ipv6_all_addresses
Definition: socket_connection.cc:290
Socket_interface_type
Definition: socket_connection.h:56
const char * ipv4_all_addresses
Definition: socket_connection.cc:288
Socket_type
Definition: socket_connection.h:54
ulong get_connection_errors_accept()
Definition: socket_connection.cc:123
PSI_statement_info stmt_info_new_packet
Definition: init_net_server_extension.cc:49
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:286
ulong get_connection_errors_tcpwrap()
Definition: socket_connection.cc:125
Plain structure to collect together a host name/ip address and a corresponding network namespace if s...
Definition: socket_connection.h:87
std::string network_namespace
Definition: socket_connection.h:88
Bind_address_info(const std::string &addr)
Definition: socket_connection.h:91
Bind_address_info()=default
Bind_address_info(const std::string &addr, const std::string &nspace)
Definition: socket_connection.h:93
std::string address
Definition: socket_connection.h:88
Definition: socket_connection.h:58
Socket_type m_socket_type
Definition: socket_connection.h:72
const std::string * m_network_namespace
Definition: socket_connection.h:73
Socket_interface_type m_socket_interface
Definition: socket_connection.h:75
Listen_socket(MYSQL_SOCKET socket, Socket_type socket_type, const std::string *network_namespace, Socket_interface_type socket_interface)
Definition: socket_connection.h:64
Listen_socket(MYSQL_SOCKET socket, Socket_type socket_type)
Definition: socket_connection.h:59
MYSQL_SOCKET m_socket
Definition: socket_connection.h:71
An instrumented socket.
Definition: mysql_socket_bits.h:34
Definition: socket_connection.h:129
std::vector< struct pollfd > m_fds
Definition: socket_connection.h:130
std::vector< MYSQL_SOCKET > m_pfs_fds
Definition: socket_connection.h:131
Statement instrument information.
Definition: psi_statement_bits.h:110
unsigned int uint
Definition: uca-dump.cc:29