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
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
#include <drizzled/internal/m_string.h>
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
#include <libdrizzle/constants.h>
40
#define PROTOCOL_VERSION 10
42
35
namespace po= boost::program_options;
43
36
using namespace std;
44
37
using namespace drizzled;
46
namespace drizzle_plugin
39
#define PROTOCOL_VERSION 10
43
extern uint32_t global_thread_id;
49
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
50
46
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
52
static port_constraint port;
53
static timeout_constraint connect_timeout;
54
static timeout_constraint read_timeout;
55
static timeout_constraint write_timeout;
56
static retry_constraint retry_count;
57
static buffer_constraint buffer_length;
48
static uint32_t connect_timeout;
49
static uint32_t read_timeout;
50
static uint32_t write_timeout;
51
static uint32_t retry_count;
52
static uint32_t buffer_length;
53
static char* bind_address;
59
54
static uint32_t random_seed1;
60
55
static uint32_t random_seed2;
61
56
static const uint32_t random_max= 0x3FFFFFFF;
62
57
static const double random_max_double= (double)0x3FFFFFFF;
65
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
67
ListenMySQLProtocol::~ListenMySQLProtocol()
70
void ListenMySQLProtocol::addCountersToTable()
72
counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connection_count"), &getCounters()->connectionCount));
73
counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connected"), &getCounters()->connected));
74
counters.push_back(new drizzled::plugin::ListenCounter(new std::string("failed_connections"), &getCounters()->failedConnections));
77
const std::string ListenMySQLProtocol::getHost(void) const
59
const char* ListenMySQLProtocol::getHost(void) const
82
64
in_port_t ListenMySQLProtocol::getPort(void) const
66
return (in_port_t) port;
87
69
plugin::Client *ListenMySQLProtocol::getClient(int fd)
94
return new ClientMySQLProtocol(new_fd, _using_mysql41_protocol, getCounters());
76
return new (nothrow) ClientMySQLProtocol(new_fd, using_mysql41_protocol);
97
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol, ProtocolCounters *set_counters):
98
is_admin_connection(false),
99
_using_mysql41_protocol(using_mysql41_protocol),
100
counters(set_counters)
79
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol_arg):
80
using_mysql41_protocol(using_mysql41_protocol_arg)
108
if (drizzleclient_net_init_sock(&net, fd, buffer_length.get()))
87
if (drizzleclient_net_init_sock(&net, fd, buffer_length))
109
88
throw bad_alloc();
111
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
112
drizzleclient_net_set_write_timeout(&net, write_timeout.get());
113
net.retry_count=retry_count.get();
90
drizzleclient_net_set_read_timeout(&net, read_timeout);
91
drizzleclient_net_set_write_timeout(&net, write_timeout);
92
net.retry_count=retry_count;
116
95
ClientMySQLProtocol::~ClientMySQLProtocol()
122
101
int ClientMySQLProtocol::getFileDescriptor(void)
156
135
drizzleclient_net_close(&net);
157
136
drizzleclient_net_end(&net);
158
if (is_admin_connection)
159
counters->adminConnected.decrement();
161
counters->connected.decrement();
165
140
bool ClientMySQLProtocol::authenticate()
167
142
bool connection_is_valid;
168
if (is_admin_connection)
170
counters->adminConnectionCount.increment();
171
counters->adminConnected.increment();
175
counters->connectionCount.increment();
176
counters->connected.increment();
179
144
/* Use "connect_timeout" value during connection phase */
180
drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
181
drizzleclient_net_set_write_timeout(&net, connect_timeout.get());
145
drizzleclient_net_set_read_timeout(&net, connect_timeout);
146
drizzleclient_net_set_write_timeout(&net, connect_timeout);
183
148
connection_is_valid= checkConnection();
185
150
if (connection_is_valid)
187
if (not is_admin_connection and (counters->connected > counters->max_connections))
189
std::string errmsg(ER(ER_CON_COUNT_ERROR));
190
sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
191
counters->failedConnections.increment();
200
154
sendError(session->main_da.sql_errno(), session->main_da.message());
201
counters->failedConnections.increment();
205
158
/* Connect completed, set read/write timeouts back to default */
206
drizzleclient_net_set_read_timeout(&net, read_timeout.get());
207
drizzleclient_net_set_write_timeout(&net, write_timeout.get());
159
drizzleclient_net_set_read_timeout(&net, read_timeout);
160
drizzleclient_net_set_write_timeout(&net, write_timeout);
980
void ClientMySQLProtocol::mysql_compose_ip_addresses(vector<string> options)
982
for (vector<string>::iterator it= options.begin();
986
tokenize(*it, mysql_admin_ip_addresses, ",", true);
990
883
static ListenMySQLProtocol *listen_obj= NULL;
991
884
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
993
886
static int init(drizzled::module::Context &context)
995
889
/* Initialize random seeds for the MySQL algorithm with minimal changes. */
996
890
time_t seed_time= time(NULL);
997
891
random_seed1= seed_time % random_max;
998
892
random_seed2= (seed_time / 2) % random_max;
1000
894
const module::option_map &vm= context.getOptions();
895
if (vm.count("port"))
899
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
904
if (vm.count("connect-timeout"))
906
if (connect_timeout < 1 || connect_timeout > 300)
908
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
913
if (vm.count("read-timeout"))
915
if (read_timeout < 1 || read_timeout > 300)
917
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
922
if (vm.count("write-timeout"))
924
if (write_timeout < 1 || write_timeout > 300)
926
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
931
if (vm.count("retry-count"))
933
if (retry_count < 1 || retry_count > 100)
935
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
940
if (vm.count("buffer-length"))
942
if (buffer_length < 1024 || buffer_length > 1024*1024)
944
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
949
if (vm.count("bind-address"))
951
bind_address= strdup(vm["bind-address"].as<string>().c_str());
1002
959
mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
1003
960
context.add(mysql_password);
1005
listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
1006
listen_obj->addCountersToTable();
962
listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
1007
963
context.add(listen_obj);
1008
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
1009
context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
1010
context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
1011
context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
1012
context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
1013
context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
1014
context.registerVariable(new sys_var_const_string_val("bind_address",
1015
vm["bind-address"].as<std::string>()));
1017
context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
968
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
969
N_("Port number to use for connection or 0 for default to with MySQL "
971
NULL, NULL, 3306, 0, 65535, 0);
972
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
973
PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
974
NULL, NULL, 10, 1, 300, 0);
975
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
976
N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
977
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
978
N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
979
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
980
N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
981
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
982
N_("Buffer length."), NULL, NULL, 16384, 1024,
984
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
985
N_("Address to bind to."), NULL, NULL, NULL);
1022
987
static void init_options(drizzled::module::option_context &context)
1025
po::value<port_constraint>(&port)->default_value(3306),
1026
_("Port number to use for connection or 0 for default to with MySQL "
990
po::value<uint32_t>(&port)->default_value(3306),
991
N_("Port number to use for connection or 0 for default to with MySQL "
1028
993
context("connect-timeout",
1029
po::value<timeout_constraint>(&connect_timeout)->default_value(10),
1030
_("Connect Timeout."));
994
po::value<uint32_t>(&connect_timeout)->default_value(10),
995
N_("Connect Timeout."));
1031
996
context("read-timeout",
1032
po::value<timeout_constraint>(&read_timeout)->default_value(30),
1033
_("Read Timeout."));
997
po::value<uint32_t>(&read_timeout)->default_value(30),
998
N_("Read Timeout."));
1034
999
context("write-timeout",
1035
po::value<timeout_constraint>(&write_timeout)->default_value(60),
1036
_("Write Timeout."));
1000
po::value<uint32_t>(&write_timeout)->default_value(60),
1001
N_("Write Timeout."));
1037
1002
context("retry-count",
1038
po::value<retry_constraint>(&retry_count)->default_value(10),
1003
po::value<uint32_t>(&retry_count)->default_value(10),
1004
N_("Retry Count."));
1040
1005
context("buffer-length",
1041
po::value<buffer_constraint>(&buffer_length)->default_value(16384),
1042
_("Buffer length."));
1006
po::value<uint32_t>(&buffer_length)->default_value(16384),
1007
N_("Buffer length."));
1043
1008
context("bind-address",
1044
po::value<string>()->default_value(""),
1045
_("Address to bind to."));
1046
context("max-connections",
1047
po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
1048
_("Maximum simultaneous connections."));
1049
context("admin-ip-addresses",
1050
po::value<vector<string> >()->composing()->notifier(&ClientMySQLProtocol::mysql_compose_ip_addresses),
1051
_("A restrictive IP address list for incoming admin connections."));
1009
po::value<string>(),
1010
N_("Address to bind to."));
1054
} /* namespace drizzle_plugin */
1013
static drizzle_sys_var* sys_variables[]= {
1014
DRIZZLE_SYSVAR(port),
1015
DRIZZLE_SYSVAR(connect_timeout),
1016
DRIZZLE_SYSVAR(read_timeout),
1017
DRIZZLE_SYSVAR(write_timeout),
1018
DRIZZLE_SYSVAR(retry_count),
1019
DRIZZLE_SYSVAR(buffer_length),
1020
DRIZZLE_SYSVAR(bind_address),
1056
1024
DRIZZLE_DECLARE_PLUGIN