15
15
* You should have received a copy of the GNU General Public License
16
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
20
#include "config.h"
21
21
#include <drizzled/gettext.h>
22
22
#include <drizzled/error.h>
23
23
#include <drizzled/query_id.h>
24
#include <drizzled/error/sql_state.h>
24
#include <drizzled/sql_state.h>
25
25
#include <drizzled/session.h>
26
26
#include "drizzled/internal/m_string.h"
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"
36
#include "drizzled/identifier.h"
38
#define PROTOCOL_VERSION 10
34
#include "table_function.h"
40
36
namespace po= boost::program_options;
41
37
using namespace std;
42
38
using namespace drizzled;
44
namespace drizzle_plugin
40
#define PROTOCOL_VERSION 10
47
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
48
42
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
50
static port_constraint port;
51
static timeout_constraint connect_timeout;
52
static timeout_constraint read_timeout;
53
static timeout_constraint write_timeout;
54
static retry_constraint retry_count;
55
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;
57
50
static uint32_t random_seed1;
58
51
static uint32_t random_seed2;
59
52
static const uint32_t random_max= 0x3FFFFFFF;
60
53
static const double random_max_double= (double)0x3FFFFFFF;
63
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
55
static plugin::TableFunction* mysql_status_table_function_ptr= NULL;
65
57
ListenMySQLProtocol::~ListenMySQLProtocol()
68
void ListenMySQLProtocol::addCountersToTable()
70
counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connection_count"), &getCounters()->connectionCount));
71
counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connected"), &getCounters()->connected));
72
counters.push_back(new drizzled::plugin::ListenCounter(new std::string("failed_connections"), &getCounters()->failedConnections));
59
/* This is strdup'd from the options */
75
const std::string ListenMySQLProtocol::getHost(void) const
63
const char* ListenMySQLProtocol::getHost(void) const
80
68
in_port_t ListenMySQLProtocol::getPort(void) const
70
return (in_port_t) port;
85
73
plugin::Client *ListenMySQLProtocol::getClient(int fd)
154
143
drizzleclient_net_close(&net);
155
144
drizzleclient_net_end(&net);
156
if (is_admin_connection)
157
counters->adminConnected.decrement();
159
counters->connected.decrement();
145
connected.decrement();
163
149
bool ClientMySQLProtocol::authenticate()
165
151
bool connection_is_valid;
166
if (is_admin_connection)
168
counters->adminConnectionCount.increment();
169
counters->adminConnected.increment();
173
counters->connectionCount.increment();
174
counters->connected.increment();
153
connectionCount.increment();
154
connected.increment();
177
156
/* Use "connect_timeout" value during connection phase */
178
drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
179
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);
181
160
connection_is_valid= checkConnection();
183
162
if (connection_is_valid)
185
if (not is_admin_connection and (counters->connected > counters->max_connections))
187
std::string errmsg(ER(ER_CON_COUNT_ERROR));
188
sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
189
counters->failedConnections.increment();
198
166
sendError(session->main_da.sql_errno(), session->main_da.message());
199
counters->failedConnections.increment();
167
failedConnections.increment();
203
170
/* Connect completed, set read/write timeouts back to default */
204
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
205
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);
973
void ClientMySQLProtocol::mysql_compose_ip_addresses(vector<string> options)
975
for (vector<string>::iterator it= options.begin();
979
tokenize(*it, mysql_admin_ip_addresses, ",", true);
983
895
static ListenMySQLProtocol *listen_obj= NULL;
984
896
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
986
898
static int init(drizzled::module::Context &context)
900
mysql_status_table_function_ptr= new MysqlProtocolStatus;
902
context.add(mysql_status_table_function_ptr);
988
903
/* Initialize random seeds for the MySQL algorithm with minimal changes. */
989
904
time_t seed_time= time(NULL);
990
905
random_seed1= seed_time % random_max;
991
906
random_seed2= (seed_time / 2) % random_max;
993
908
const module::option_map &vm= context.getOptions();
909
if (vm.count("port"))
913
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
918
if (vm.count("connect-timeout"))
920
if (connect_timeout < 1 || connect_timeout > 300)
922
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
927
if (vm.count("read-timeout"))
929
if (read_timeout < 1 || read_timeout > 300)
931
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
936
if (vm.count("write-timeout"))
938
if (write_timeout < 1 || write_timeout > 300)
940
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
945
if (vm.count("retry-count"))
947
if (retry_count < 1 || retry_count > 100)
949
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
954
if (vm.count("buffer-length"))
956
if (buffer_length < 1024 || buffer_length > 1024*1024)
958
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
963
if (vm.count("bind-address"))
965
bind_address= strdup(vm["bind-address"].as<string>().c_str());
995
973
mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
996
974
context.add(mysql_password);
998
listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
999
listen_obj->addCountersToTable();
976
listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
1000
977
context.add(listen_obj);
1001
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
1002
context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
1003
context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
1004
context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
1005
context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
1006
context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
1007
context.registerVariable(new sys_var_const_string_val("bind_address",
1008
vm["bind-address"].as<std::string>()));
1010
context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
982
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
983
N_("Port number to use for connection or 0 for default to with MySQL "
985
NULL, NULL, 3306, 0, 65535, 0);
986
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
987
PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
988
NULL, NULL, 10, 1, 300, 0);
989
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
990
N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
991
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
992
N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
993
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
994
N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
995
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
996
N_("Buffer length."), NULL, NULL, 16384, 1024,
998
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
999
N_("Address to bind to."), NULL, NULL, NULL);
1015
1001
static void init_options(drizzled::module::option_context &context)
1017
1003
context("port",
1018
po::value<port_constraint>(&port)->default_value(3306),
1019
_("Port number to use for connection or 0 for default to with MySQL "
1004
po::value<uint32_t>(&port)->default_value(3306),
1005
N_("Port number to use for connection or 0 for default to with MySQL "
1021
1007
context("connect-timeout",
1022
po::value<timeout_constraint>(&connect_timeout)->default_value(10),
1023
_("Connect Timeout."));
1008
po::value<uint32_t>(&connect_timeout)->default_value(10),
1009
N_("Connect Timeout."));
1024
1010
context("read-timeout",
1025
po::value<timeout_constraint>(&read_timeout)->default_value(30),
1026
_("Read Timeout."));
1011
po::value<uint32_t>(&read_timeout)->default_value(30),
1012
N_("Read Timeout."));
1027
1013
context("write-timeout",
1028
po::value<timeout_constraint>(&write_timeout)->default_value(60),
1029
_("Write Timeout."));
1014
po::value<uint32_t>(&write_timeout)->default_value(60),
1015
N_("Write Timeout."));
1030
1016
context("retry-count",
1031
po::value<retry_constraint>(&retry_count)->default_value(10),
1017
po::value<uint32_t>(&retry_count)->default_value(10),
1018
N_("Retry Count."));
1033
1019
context("buffer-length",
1034
po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1035
_("Buffer length."));
1020
po::value<uint32_t>(&buffer_length)->default_value(16384),
1021
N_("Buffer length."));
1036
1022
context("bind-address",
1037
po::value<string>()->default_value(""),
1038
_("Address to bind to."));
1039
context("max-connections",
1040
po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1041
_("Maximum simultaneous connections."));
1042
context("admin-ip-addresses",
1043
po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1044
_("A restrictive IP address list for incoming admin connections."));
1047
} /* namespace drizzle_plugin */
1023
po::value<string>(),
1024
N_("Address to bind to."));
1027
static drizzle_sys_var* sys_variables[]= {
1028
DRIZZLE_SYSVAR(port),
1029
DRIZZLE_SYSVAR(connect_timeout),
1030
DRIZZLE_SYSVAR(read_timeout),
1031
DRIZZLE_SYSVAR(write_timeout),
1032
DRIZZLE_SYSVAR(retry_count),
1033
DRIZZLE_SYSVAR(buffer_length),
1034
DRIZZLE_SYSVAR(bind_address),
1038
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1040
var->type= SHOW_LONGLONG;
1042
*((uint64_t *)buff)= ClientMySQLProtocol::connectionCount;
1046
static int mysql_protocol_connected_count_func(drizzle_show_var *var, char *buff)
1048
var->type= SHOW_LONGLONG;
1050
*((uint64_t *)buff)= ClientMySQLProtocol::connected;
1054
static int mysql_protocol_failed_count_func(drizzle_show_var *var, char *buff)
1056
var->type= SHOW_LONGLONG;
1058
*((uint64_t *)buff)= ClientMySQLProtocol::failedConnections;
1062
static st_show_var_func_container mysql_protocol_connection_count=
1063
{ &mysql_protocol_connection_count_func };
1065
static st_show_var_func_container mysql_protocol_connected_count=
1066
{ &mysql_protocol_connected_count_func };
1068
static st_show_var_func_container mysql_protocol_failed_count=
1069
{ &mysql_protocol_failed_count_func };
1071
static drizzle_show_var mysql_protocol_status_variables[]= {
1073
(char*) &mysql_protocol_connection_count, SHOW_FUNC},
1075
(char*) &mysql_protocol_connected_count, SHOW_FUNC},
1076
{"Failed_connections",
1077
(char*) &mysql_protocol_failed_count, SHOW_FUNC},
1078
{NULL, NULL, SHOW_LONGLONG}
1081
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
1082
plugin::TableFunction::Generator(fields)
1084
status_var_ptr= mysql_protocol_status_variables;
1087
bool MysqlProtocolStatus::Generator::populate()
1089
MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
1090
char * const buff= (char *) &buff_data;
1091
drizzle_show_var tmp;
1093
if (status_var_ptr->name)
1095
std::ostringstream oss;
1096
string return_value;
1100
push(status_var_ptr->name);
1102
if (status_var_ptr->type == SHOW_FUNC)
1104
((mysql_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
1110
value= status_var_ptr->value;
1111
type= status_var_ptr->type;
1117
oss << *(uint64_t*) value;
1118
return_value= oss.str();
1123
if (return_value.length())
1049
1135
DRIZZLE_DECLARE_PLUGIN