25
25
#include <drizzled/session.h>
26
26
#include "drizzled/internal/m_string.h"
27
27
#include <algorithm>
28
#include <boost/program_options.hpp>
29
#include <drizzled/module/option_map.h>
30
#include "drizzled/util/tokenize.h"
31
29
#include "errmsg.h"
32
30
#include "mysql_protocol.h"
33
31
#include "mysql_password.h"
34
32
#include "options.h"
35
#include "table_function.h"
37
#define PROTOCOL_VERSION 10
39
namespace po= boost::program_options;
40
34
using namespace std;
41
35
using namespace drizzled;
43
namespace drizzle_plugin
37
#define PROTOCOL_VERSION 10
41
extern uint32_t global_thread_id;
46
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
47
44
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
49
static port_constraint port;
50
static timeout_constraint connect_timeout;
51
static timeout_constraint read_timeout;
52
static timeout_constraint write_timeout;
53
static retry_constraint retry_count;
54
static buffer_constraint buffer_length;
46
static uint32_t connect_timeout;
47
static uint32_t read_timeout;
48
static uint32_t write_timeout;
49
static uint32_t retry_count;
50
static uint32_t buffer_length;
51
static char* bind_address;
56
52
static uint32_t random_seed1;
57
53
static uint32_t random_seed2;
58
54
static const uint32_t random_max= 0x3FFFFFFF;
59
55
static const double random_max_double= (double)0x3FFFFFFF;
62
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
64
ListenMySQLProtocol::~ListenMySQLProtocol()
67
const std::string ListenMySQLProtocol::getHost(void) const
57
const char* ListenMySQLProtocol::getHost(void) const
72
62
in_port_t ListenMySQLProtocol::getPort(void) const
64
return (in_port_t) port;
77
67
plugin::Client *ListenMySQLProtocol::getClient(int fd)
146
133
drizzleclient_net_close(&net);
147
134
drizzleclient_net_end(&net);
148
if (is_admin_connection)
149
counters->adminConnected.decrement();
151
counters->connected.decrement();
155
138
bool ClientMySQLProtocol::authenticate()
157
140
bool connection_is_valid;
158
if (is_admin_connection)
160
counters->adminConnectionCount.increment();
161
counters->adminConnected.increment();
165
counters->connectionCount.increment();
166
counters->connected.increment();
169
142
/* Use "connect_timeout" value during connection phase */
170
drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
171
drizzleclient_net_set_write_timeout(&net, connect_timeout.get());
143
drizzleclient_net_set_read_timeout(&net, connect_timeout);
144
drizzleclient_net_set_write_timeout(&net, connect_timeout);
173
146
connection_is_valid= checkConnection();
175
148
if (connection_is_valid)
177
if (not is_admin_connection and (counters->connected > counters->max_connections))
179
std::string errmsg(ER(ER_CON_COUNT_ERROR));
180
sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
181
counters->failedConnections.increment();
190
152
sendError(session->main_da.sql_errno(), session->main_da.message());
191
counters->failedConnections.increment();
195
156
/* Connect completed, set read/write timeouts back to default */
196
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
197
drizzleclient_net_set_write_timeout(&net, write_timeout.get());
157
drizzleclient_net_set_read_timeout(&net, read_timeout);
158
drizzleclient_net_set_write_timeout(&net, write_timeout);
947
void ClientMySQLProtocol::mysql_compose_ip_addresses(vector<string> options)
949
for (vector<string>::iterator it= options.begin();
953
tokenize(*it, mysql_admin_ip_addresses, ",", true);
957
881
static ListenMySQLProtocol *listen_obj= NULL;
958
882
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
960
884
static int init(drizzled::module::Context &context)
962
context.add(new MysqlProtocolStatus);
964
886
/* Initialize random seeds for the MySQL algorithm with minimal changes. */
965
887
time_t seed_time= time(NULL);
966
888
random_seed1= seed_time % random_max;
967
889
random_seed2= (seed_time / 2) % random_max;
969
const module::option_map &vm= context.getOptions();
971
891
mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
972
892
context.add(mysql_password);
974
listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
894
listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
975
895
context.add(listen_obj);
976
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
977
context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
978
context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
979
context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
980
context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
981
context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
982
context.registerVariable(new sys_var_const_string_val("bind_address",
983
vm["bind-address"].as<std::string>()));
985
context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
990
static void init_options(drizzled::module::option_context &context)
993
po::value<port_constraint>(&port)->default_value(3306),
994
N_("Port number to use for connection or 0 for default to with MySQL "
996
context("connect-timeout",
997
po::value<timeout_constraint>(&connect_timeout)->default_value(10),
998
N_("Connect Timeout."));
999
context("read-timeout",
1000
po::value<timeout_constraint>(&read_timeout)->default_value(30),
1001
N_("Read Timeout."));
1002
context("write-timeout",
1003
po::value<timeout_constraint>(&write_timeout)->default_value(60),
1004
N_("Write Timeout."));
1005
context("retry-count",
1006
po::value<retry_constraint>(&retry_count)->default_value(10),
1007
N_("Retry Count."));
1008
context("buffer-length",
1009
po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1010
N_("Buffer length."));
1011
context("bind-address",
1012
po::value<string>()->default_value(""),
1013
N_("Address to bind to."));
1014
context("max-connections",
1015
po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1016
N_("Maximum simultaneous connections."));
1017
context("admin-ip-addresses",
1018
po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1019
N_("A restrictive IP address list for incoming admin connections."));
1022
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1024
var->type= SHOW_LONGLONG;
1026
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
1030
static int mysql_protocol_connected_count_func(drizzle_show_var *var, char *buff)
1032
var->type= SHOW_LONGLONG;
1034
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connected;
1038
static int mysql_protocol_failed_count_func(drizzle_show_var *var, char *buff)
1040
var->type= SHOW_LONGLONG;
1042
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->failedConnections;
1046
static st_show_var_func_container mysql_protocol_connection_count=
1047
{ &mysql_protocol_connection_count_func };
1049
static st_show_var_func_container mysql_protocol_connected_count=
1050
{ &mysql_protocol_connected_count_func };
1052
static st_show_var_func_container mysql_protocol_failed_count=
1053
{ &mysql_protocol_failed_count_func };
1055
static drizzle_show_var mysql_protocol_status_variables[]= {
1057
(char*) &mysql_protocol_connection_count, SHOW_FUNC},
1059
(char*) &mysql_protocol_connected_count, SHOW_FUNC},
1060
{"Failed_connections",
1061
(char*) &mysql_protocol_failed_count, SHOW_FUNC},
1062
{NULL, NULL, SHOW_LONGLONG}
900
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
901
N_("Port number to use for connection or 0 for default to with MySQL "
903
NULL, NULL, 3306, 0, 65535, 0);
904
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
905
PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
906
NULL, NULL, 10, 1, 300, 0);
907
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
908
N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
909
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
910
N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
911
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
912
N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
913
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
914
N_("Buffer length."), NULL, NULL, 16384, 1024,
916
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
917
N_("Address to bind to."), NULL, NULL, NULL);
919
static drizzle_sys_var* sys_variables[]= {
920
DRIZZLE_SYSVAR(port),
921
DRIZZLE_SYSVAR(connect_timeout),
922
DRIZZLE_SYSVAR(read_timeout),
923
DRIZZLE_SYSVAR(write_timeout),
924
DRIZZLE_SYSVAR(retry_count),
925
DRIZZLE_SYSVAR(buffer_length),
926
DRIZZLE_SYSVAR(bind_address),
1065
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
1066
plugin::TableFunction::Generator(fields)
1068
status_var_ptr= mysql_protocol_status_variables;
1071
bool MysqlProtocolStatus::Generator::populate()
1073
MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
1074
char * const buff= (char *) &buff_data;
1075
drizzle_show_var tmp;
1077
if (status_var_ptr->name)
1079
std::ostringstream oss;
1080
string return_value;
1084
push(status_var_ptr->name);
1086
if (status_var_ptr->type == SHOW_FUNC)
1088
((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
1094
value= status_var_ptr->value;
1095
type= status_var_ptr->type;
1101
oss << *(uint64_t*) value;
1102
return_value= oss.str();
1107
if (return_value.length())
1119
} /* namespace drizzle_plugin */
1121
930
DRIZZLE_DECLARE_PLUGIN
1123
932
DRIZZLE_VERSION_ID,
1127
936
"MySQL Protocol Module",
1128
937
PLUGIN_LICENSE_GPL,
1129
drizzle_plugin::init, /* Plugin Init */
1130
NULL, /* system variables */
1131
drizzle_plugin::init_options /* config options */
938
init, /* Plugin Init */
939
sys_variables, /* system variables */
940
NULL /* config options */
1133
942
DRIZZLE_DECLARE_PLUGIN_END;