27
27
#include <algorithm>
28
28
#include <boost/program_options.hpp>
29
29
#include <drizzled/module/option_map.h>
30
#include "drizzled/util/tokenize.h"
31
30
#include "errmsg.h"
32
31
#include "mysql_protocol.h"
33
32
#include "mysql_password.h"
34
33
#include "options.h"
35
34
#include "table_function.h"
37
#include "drizzled/identifier.h"
39
#define PROTOCOL_VERSION 10
41
36
namespace po= boost::program_options;
42
37
using namespace std;
43
38
using namespace drizzled;
45
namespace drizzle_plugin
40
#define PROTOCOL_VERSION 10
48
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
49
42
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
51
static port_constraint port;
52
static timeout_constraint connect_timeout;
53
static timeout_constraint read_timeout;
54
static timeout_constraint write_timeout;
55
static retry_constraint retry_count;
56
static buffer_constraint buffer_length;
44
static uint32_t connect_timeout;
45
static uint32_t read_timeout;
46
static uint32_t write_timeout;
47
static uint32_t retry_count;
48
static uint32_t buffer_length;
49
static char* bind_address;
58
50
static uint32_t random_seed1;
59
51
static uint32_t random_seed2;
60
52
static const uint32_t random_max= 0x3FFFFFFF;
61
53
static const double random_max_double= (double)0x3FFFFFFF;
64
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
55
static plugin::TableFunction* mysql_status_table_function_ptr= NULL;
66
57
ListenMySQLProtocol::~ListenMySQLProtocol()
59
/* This is strdup'd from the options */
69
const std::string ListenMySQLProtocol::getHost(void) const
63
const char* ListenMySQLProtocol::getHost(void) const
74
68
in_port_t ListenMySQLProtocol::getPort(void) const
70
return (in_port_t) port;
79
73
plugin::Client *ListenMySQLProtocol::getClient(int fd)
86
return new ClientMySQLProtocol(new_fd, _using_mysql41_protocol, getCounters());
80
return new (nothrow) ClientMySQLProtocol(new_fd, using_mysql41_protocol);
89
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol, ProtocolCounters *set_counters):
90
is_admin_connection(false),
91
_using_mysql41_protocol(using_mysql41_protocol),
92
counters(set_counters)
83
drizzled::atomic<uint64_t> ClientMySQLProtocol::connectionCount;
84
drizzled::atomic<uint64_t> ClientMySQLProtocol::failedConnections;
85
drizzled::atomic<uint64_t> ClientMySQLProtocol::connected;
87
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol_arg):
88
using_mysql41_protocol(using_mysql41_protocol_arg)
100
if (drizzleclient_net_init_sock(&net, fd, buffer_length.get()))
95
if (drizzleclient_net_init_sock(&net, fd, buffer_length))
101
96
throw bad_alloc();
103
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
104
drizzleclient_net_set_write_timeout(&net, write_timeout.get());
105
net.retry_count=retry_count.get();
98
drizzleclient_net_set_read_timeout(&net, read_timeout);
99
drizzleclient_net_set_write_timeout(&net, write_timeout);
100
net.retry_count=retry_count;
108
103
ClientMySQLProtocol::~ClientMySQLProtocol()
148
143
drizzleclient_net_close(&net);
149
144
drizzleclient_net_end(&net);
150
if (is_admin_connection)
151
counters->adminConnected.decrement();
153
counters->connected.decrement();
145
connected.decrement();
157
149
bool ClientMySQLProtocol::authenticate()
159
151
bool connection_is_valid;
160
if (is_admin_connection)
162
counters->adminConnectionCount.increment();
163
counters->adminConnected.increment();
167
counters->connectionCount.increment();
168
counters->connected.increment();
153
connectionCount.increment();
154
connected.increment();
171
156
/* Use "connect_timeout" value during connection phase */
172
drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
173
drizzleclient_net_set_write_timeout(&net, connect_timeout.get());
157
drizzleclient_net_set_read_timeout(&net, connect_timeout);
158
drizzleclient_net_set_write_timeout(&net, connect_timeout);
175
160
connection_is_valid= checkConnection();
177
162
if (connection_is_valid)
179
if (not is_admin_connection and (counters->connected > counters->max_connections))
181
std::string errmsg(ER(ER_CON_COUNT_ERROR));
182
sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
183
counters->failedConnections.increment();
192
166
sendError(session->main_da.sql_errno(), session->main_da.message());
193
counters->failedConnections.increment();
167
failedConnections.increment();
197
170
/* Connect completed, set read/write timeouts back to default */
198
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
199
drizzleclient_net_set_write_timeout(&net, write_timeout.get());
171
drizzleclient_net_set_read_timeout(&net, read_timeout);
172
drizzleclient_net_set_write_timeout(&net, write_timeout);
955
void ClientMySQLProtocol::mysql_compose_ip_addresses(vector<string> options)
957
for (vector<string>::iterator it= options.begin();
961
tokenize(*it, mysql_admin_ip_addresses, ",", true);
965
897
static ListenMySQLProtocol *listen_obj= NULL;
966
898
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
968
900
static int init(drizzled::module::Context &context)
970
context.add(new MysqlProtocolStatus);
902
mysql_status_table_function_ptr= new MysqlProtocolStatus;
904
context.add(mysql_status_table_function_ptr);
972
905
/* Initialize random seeds for the MySQL algorithm with minimal changes. */
973
906
time_t seed_time= time(NULL);
974
907
random_seed1= seed_time % random_max;
975
908
random_seed2= (seed_time / 2) % random_max;
977
910
const module::option_map &vm= context.getOptions();
911
if (vm.count("port"))
915
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
920
if (vm.count("connect-timeout"))
922
if (connect_timeout < 1 || connect_timeout > 300)
924
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
929
if (vm.count("read-timeout"))
931
if (read_timeout < 1 || read_timeout > 300)
933
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
938
if (vm.count("write-timeout"))
940
if (write_timeout < 1 || write_timeout > 300)
942
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
947
if (vm.count("retry-count"))
949
if (retry_count < 1 || retry_count > 100)
951
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
956
if (vm.count("buffer-length"))
958
if (buffer_length < 1024 || buffer_length > 1024*1024)
960
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
965
if (vm.count("bind-address"))
967
bind_address= strdup(vm["bind-address"].as<string>().c_str());
979
975
mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
980
976
context.add(mysql_password);
982
listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
978
listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
983
979
context.add(listen_obj);
984
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
985
context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
986
context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
987
context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
988
context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
989
context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
990
context.registerVariable(new sys_var_const_string_val("bind_address",
991
vm["bind-address"].as<std::string>()));
993
context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
984
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
985
N_("Port number to use for connection or 0 for default to with MySQL "
987
NULL, NULL, 3306, 0, 65535, 0);
988
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
989
PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
990
NULL, NULL, 10, 1, 300, 0);
991
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
992
N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
993
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
994
N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
995
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
996
N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
997
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
998
N_("Buffer length."), NULL, NULL, 16384, 1024,
1000
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
1001
N_("Address to bind to."), NULL, NULL, NULL);
998
1003
static void init_options(drizzled::module::option_context &context)
1000
1005
context("port",
1001
po::value<port_constraint>(&port)->default_value(3306),
1006
po::value<uint32_t>(&port)->default_value(3306),
1002
1007
N_("Port number to use for connection or 0 for default to with MySQL "
1004
1009
context("connect-timeout",
1005
po::value<timeout_constraint>(&connect_timeout)->default_value(10),
1010
po::value<uint32_t>(&connect_timeout)->default_value(10),
1006
1011
N_("Connect Timeout."));
1007
1012
context("read-timeout",
1008
po::value<timeout_constraint>(&read_timeout)->default_value(30),
1013
po::value<uint32_t>(&read_timeout)->default_value(30),
1009
1014
N_("Read Timeout."));
1010
1015
context("write-timeout",
1011
po::value<timeout_constraint>(&write_timeout)->default_value(60),
1016
po::value<uint32_t>(&write_timeout)->default_value(60),
1012
1017
N_("Write Timeout."));
1013
1018
context("retry-count",
1014
po::value<retry_constraint>(&retry_count)->default_value(10),
1019
po::value<uint32_t>(&retry_count)->default_value(10),
1015
1020
N_("Retry Count."));
1016
1021
context("buffer-length",
1017
po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1022
po::value<uint32_t>(&buffer_length)->default_value(16384),
1018
1023
N_("Buffer length."));
1019
1024
context("bind-address",
1020
po::value<string>()->default_value(""),
1025
po::value<string>(),
1021
1026
N_("Address to bind to."));
1022
context("max-connections",
1023
po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1024
N_("Maximum simultaneous connections."));
1025
context("admin-ip-addresses",
1026
po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1027
N_("A restrictive IP address list for incoming admin connections."));
1029
static drizzle_sys_var* sys_variables[]= {
1030
DRIZZLE_SYSVAR(port),
1031
DRIZZLE_SYSVAR(connect_timeout),
1032
DRIZZLE_SYSVAR(read_timeout),
1033
DRIZZLE_SYSVAR(write_timeout),
1034
DRIZZLE_SYSVAR(retry_count),
1035
DRIZZLE_SYSVAR(buffer_length),
1036
DRIZZLE_SYSVAR(bind_address),
1030
1040
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1032
1042
var->type= SHOW_LONGLONG;
1033
1043
var->value= buff;
1034
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
1044
*((uint64_t *)buff)= ClientMySQLProtocol::connectionCount;