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"
29
31
#include "errmsg.h"
30
32
#include "mysql_protocol.h"
31
33
#include "mysql_password.h"
32
34
#include "options.h"
35
#include "table_function.h"
37
#include "drizzled/identifier.h"
39
#define PROTOCOL_VERSION 10
41
namespace po= boost::program_options;
34
42
using namespace std;
35
43
using namespace drizzled;
37
#define PROTOCOL_VERSION 10
45
namespace drizzle_plugin
41
extern uint32_t global_thread_id;
48
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
44
49
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
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;
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;
52
58
static uint32_t random_seed1;
53
59
static uint32_t random_seed2;
54
60
static const uint32_t random_max= 0x3FFFFFFF;
55
61
static const double random_max_double= (double)0x3FFFFFFF;
57
const char* ListenMySQLProtocol::getHost(void) const
64
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
66
ListenMySQLProtocol::~ListenMySQLProtocol()
69
const std::string ListenMySQLProtocol::getHost(void) const
62
74
in_port_t ListenMySQLProtocol::getPort(void) const
64
return (in_port_t) port;
67
79
plugin::Client *ListenMySQLProtocol::getClient(int fd)
133
148
drizzleclient_net_close(&net);
134
149
drizzleclient_net_end(&net);
150
if (is_admin_connection)
151
counters->adminConnected.decrement();
153
counters->connected.decrement();
138
157
bool ClientMySQLProtocol::authenticate()
140
159
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();
142
171
/* Use "connect_timeout" value during connection phase */
143
drizzleclient_net_set_read_timeout(&net, connect_timeout);
144
drizzleclient_net_set_write_timeout(&net, connect_timeout);
172
drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
173
drizzleclient_net_set_write_timeout(&net, connect_timeout.get());
146
175
connection_is_valid= checkConnection();
148
177
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();
152
192
sendError(session->main_da.sql_errno(), session->main_da.message());
193
counters->failedConnections.increment();
156
197
/* Connect completed, set read/write timeouts back to default */
157
drizzleclient_net_set_read_timeout(&net, read_timeout);
158
drizzleclient_net_set_write_timeout(&net, write_timeout);
198
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
199
drizzleclient_net_set_write_timeout(&net, write_timeout.get());
967
void ClientMySQLProtocol::mysql_compose_ip_addresses(vector<string> options)
969
for (vector<string>::iterator it= options.begin();
973
tokenize(*it, mysql_admin_ip_addresses, ",", true);
881
977
static ListenMySQLProtocol *listen_obj= NULL;
882
978
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
884
static int init(drizzled::plugin::Context &context)
980
static int init(drizzled::module::Context &context)
982
context.add(new MysqlProtocolStatus);
886
984
/* Initialize random seeds for the MySQL algorithm with minimal changes. */
887
985
time_t seed_time= time(NULL);
888
986
random_seed1= seed_time % random_max;
889
987
random_seed2= (seed_time / 2) % random_max;
989
const module::option_map &vm= context.getOptions();
891
991
mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
892
992
context.add(mysql_password);
894
listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
994
listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
895
995
context.add(listen_obj);
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),
996
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
997
context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
998
context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
999
context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
1000
context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
1001
context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
1002
context.registerVariable(new sys_var_const_string_val("bind_address",
1003
vm["bind-address"].as<std::string>()));
1005
context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
1010
static void init_options(drizzled::module::option_context &context)
1013
po::value<port_constraint>(&port)->default_value(3306),
1014
_("Port number to use for connection or 0 for default to with MySQL "
1016
context("connect-timeout",
1017
po::value<timeout_constraint>(&connect_timeout)->default_value(10),
1018
_("Connect Timeout."));
1019
context("read-timeout",
1020
po::value<timeout_constraint>(&read_timeout)->default_value(30),
1021
_("Read Timeout."));
1022
context("write-timeout",
1023
po::value<timeout_constraint>(&write_timeout)->default_value(60),
1024
_("Write Timeout."));
1025
context("retry-count",
1026
po::value<retry_constraint>(&retry_count)->default_value(10),
1028
context("buffer-length",
1029
po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1030
_("Buffer length."));
1031
context("bind-address",
1032
po::value<string>()->default_value(""),
1033
_("Address to bind to."));
1034
context("max-connections",
1035
po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1036
_("Maximum simultaneous connections."));
1037
context("admin-ip-addresses",
1038
po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1039
_("A restrictive IP address list for incoming admin connections."));
1042
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1044
var->type= SHOW_LONGLONG;
1046
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
1050
static int mysql_protocol_connected_count_func(drizzle_show_var *var, char *buff)
1052
var->type= SHOW_LONGLONG;
1054
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connected;
1058
static int mysql_protocol_failed_count_func(drizzle_show_var *var, char *buff)
1060
var->type= SHOW_LONGLONG;
1062
*((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->failedConnections;
1066
static st_show_var_func_container mysql_protocol_connection_count=
1067
{ &mysql_protocol_connection_count_func };
1069
static st_show_var_func_container mysql_protocol_connected_count=
1070
{ &mysql_protocol_connected_count_func };
1072
static st_show_var_func_container mysql_protocol_failed_count=
1073
{ &mysql_protocol_failed_count_func };
1075
static drizzle_show_var mysql_protocol_status_variables[]= {
1077
(char*) &mysql_protocol_connection_count, SHOW_FUNC},
1079
(char*) &mysql_protocol_connected_count, SHOW_FUNC},
1080
{"Failed_connections",
1081
(char*) &mysql_protocol_failed_count, SHOW_FUNC},
1082
{NULL, NULL, SHOW_LONGLONG}
1085
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
1086
plugin::TableFunction::Generator(fields)
1088
status_var_ptr= mysql_protocol_status_variables;
1091
bool MysqlProtocolStatus::Generator::populate()
1093
MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
1094
char * const buff= (char *) &buff_data;
1095
drizzle_show_var tmp;
1097
if (status_var_ptr->name)
1099
std::ostringstream oss;
1100
string return_value;
1104
push(status_var_ptr->name);
1106
if (status_var_ptr->type == SHOW_FUNC)
1108
((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
1114
value= status_var_ptr->value;
1115
type= status_var_ptr->type;
1121
oss << *(uint64_t*) value;
1122
return_value= oss.str();
1127
if (return_value.length())
1139
} /* namespace drizzle_plugin */
930
1141
DRIZZLE_DECLARE_PLUGIN
932
1143
DRIZZLE_VERSION_ID,
936
1147
"MySQL Protocol Module",
937
1148
PLUGIN_LICENSE_GPL,
938
init, /* Plugin Init */
939
sys_variables, /* system variables */
940
NULL /* config options */
1149
drizzle_plugin::init, /* Plugin Init */
1151
drizzle_plugin::init_options /* config options */
942
1153
DRIZZLE_DECLARE_PLUGIN_END;