~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/mysql_protocol.cc

  • Committer: Brian Aker
  • Date: 2010-10-22 17:44:34 UTC
  • mto: This revision was merged to the branch mainline in revision 1873.
  • Revision ID: brian@tangent.org-20101022174434-q8fjovcpclzqer7n
TableShare is no longer in the house (i.e. we no longer directly have a copy
of it in cursor).

One more bit of the knot now gone.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"
36
35
 
37
 
#define PROTOCOL_VERSION 10
38
 
 
39
36
namespace po= boost::program_options;
40
37
using namespace std;
41
38
using namespace drizzled;
42
39
 
43
 
namespace drizzle_plugin
44
 
{
 
40
#define PROTOCOL_VERSION 10
45
41
 
46
 
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
47
42
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
48
 
 
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;
55
 
 
 
43
static uint32_t port;
 
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;
56
50
static uint32_t random_seed1;
57
51
static uint32_t random_seed2;
58
52
static const uint32_t random_max= 0x3FFFFFFF;
59
53
static const double random_max_double= (double)0x3FFFFFFF;
60
54
 
61
 
 
62
 
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
 
55
static plugin::TableFunction* mysql_status_table_function_ptr= NULL;
63
56
 
64
57
ListenMySQLProtocol::~ListenMySQLProtocol()
65
 
{ }
 
58
{
 
59
  /* This is strdup'd from the options */
 
60
  free(bind_address);
 
61
}
66
62
 
67
 
const std::string ListenMySQLProtocol::getHost(void) const
 
63
const char* ListenMySQLProtocol::getHost(void) const
68
64
{
69
 
  return _hostname;
 
65
  return bind_address;
70
66
}
71
67
 
72
68
in_port_t ListenMySQLProtocol::getPort(void) const
73
69
{
74
 
  return port.get();
 
70
  return (in_port_t) port;
75
71
}
76
72
 
77
73
plugin::Client *ListenMySQLProtocol::getClient(int fd)
81
77
  if (new_fd == -1)
82
78
    return NULL;
83
79
 
84
 
  return new ClientMySQLProtocol(new_fd, _using_mysql41_protocol, getCounters());
 
80
  return new (nothrow) ClientMySQLProtocol(new_fd, using_mysql41_protocol);
85
81
}
86
82
 
87
 
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol, ProtocolCounters *set_counters):
88
 
  is_admin_connection(false),
89
 
  _using_mysql41_protocol(using_mysql41_protocol),
90
 
  counters(set_counters)
 
83
drizzled::atomic<uint64_t> ClientMySQLProtocol::connectionCount;
 
84
drizzled::atomic<uint64_t> ClientMySQLProtocol::failedConnections;
 
85
drizzled::atomic<uint64_t> ClientMySQLProtocol::connected;
 
86
 
 
87
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol_arg):
 
88
  using_mysql41_protocol(using_mysql41_protocol_arg)
91
89
{
92
 
  
93
90
  net.vio= 0;
94
91
 
95
92
  if (fd == -1)
96
93
    return;
97
94
 
98
 
  if (drizzleclient_net_init_sock(&net, fd, buffer_length.get()))
 
95
  if (drizzleclient_net_init_sock(&net, fd, buffer_length))
99
96
    throw bad_alloc();
100
97
 
101
 
  drizzleclient_net_set_read_timeout(&net, read_timeout.get());
102
 
  drizzleclient_net_set_write_timeout(&net, write_timeout.get());
103
 
  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;
104
101
}
105
102
 
106
103
ClientMySQLProtocol::~ClientMySQLProtocol()
107
104
{
108
105
  if (net.vio)
109
 
    net.vio->close();
 
106
    vio_close(net.vio);
110
107
}
111
108
 
112
109
int ClientMySQLProtocol::getFileDescriptor(void)
145
142
  { 
146
143
    drizzleclient_net_close(&net);
147
144
    drizzleclient_net_end(&net);
148
 
    if (is_admin_connection)
149
 
      counters->adminConnected.decrement();
150
 
    else
151
 
      counters->connected.decrement();
 
145
    connected.decrement();
152
146
  }
153
147
}
154
148
 
155
149
bool ClientMySQLProtocol::authenticate()
156
150
{
157
151
  bool connection_is_valid;
158
 
  if (is_admin_connection)
159
 
  {
160
 
    counters->adminConnectionCount.increment();
161
 
    counters->adminConnected.increment();
162
 
  }
163
 
  else
164
 
  {
165
 
    counters->connectionCount.increment();
166
 
    counters->connected.increment();
167
 
  }
 
152
 
 
153
  connectionCount.increment();
 
154
  connected.increment();
168
155
 
169
156
  /* 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());
 
157
  drizzleclient_net_set_read_timeout(&net, connect_timeout);
 
158
  drizzleclient_net_set_write_timeout(&net, connect_timeout);
172
159
 
173
160
  connection_is_valid= checkConnection();
174
161
 
175
162
  if (connection_is_valid)
176
 
  {
177
 
    if (not is_admin_connection and (counters->connected > counters->max_connections))
178
 
    {
179
 
      std::string errmsg(ER(ER_CON_COUNT_ERROR));
180
 
      sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
181
 
      counters->failedConnections.increment();
182
 
    }
183
 
    else
184
 
    {
185
 
      sendOK();
186
 
    }
187
 
  }
 
163
    sendOK();
188
164
  else
189
165
  {
190
166
    sendError(session->main_da.sql_errno(), session->main_da.message());
191
 
    counters->failedConnections.increment();
 
167
    failedConnections.increment();
192
168
    return false;
193
169
  }
194
 
 
195
170
  /* 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());
 
171
  drizzleclient_net_set_read_timeout(&net, read_timeout);
 
172
  drizzleclient_net_set_write_timeout(&net, write_timeout);
198
173
  return true;
199
174
}
200
175
 
231
206
      return false;                       // We have to close it.
232
207
 
233
208
    net.error= 0;
 
209
    *packet_length= 0;
 
210
    return true;
234
211
  }
235
212
 
236
213
  *l_packet= (char*) net.read_pos;
250
227
    (*l_packet)[0]= (unsigned char) COM_SLEEP;
251
228
    *packet_length= 1;
252
229
  }
253
 
  else if (_using_mysql41_protocol)
 
230
  else if (using_mysql41_protocol)
254
231
  {
255
232
    /* Map from MySQL commands to Drizzle commands. */
256
233
    switch ((int)(*l_packet)[0])
390
367
    drizzleclient_net_flush(&net);
391
368
    session->main_da.can_overwrite_status= false;
392
369
  }
393
 
  packet.shrink(buffer_length.get());
 
370
  packet.shrink(buffer_length);
394
371
}
395
372
 
396
373
 
443
420
 
444
421
  drizzleclient_net_write_command(&net,(unsigned char) 255, (unsigned char*) "", 0, (unsigned char*) err, length);
445
422
 
446
 
  drizzleclient_net_flush(&net);
447
 
 
448
423
  session->main_da.can_overwrite_status= false;
449
424
}
450
425
 
500
475
    int2store(pos, field.charsetnr);
501
476
    int4store(pos+2, field.length);
502
477
 
503
 
    if (_using_mysql41_protocol)
 
478
    if (using_mysql41_protocol)
504
479
    {
505
480
      /* Switch to MySQL field numbering. */
506
481
      switch (field.type)
537
512
        pos[6]= 15;
538
513
        break;
539
514
 
540
 
      case DRIZZLE_TYPE_UUID:
541
 
        pos[6]= 15;
542
 
        break;
543
 
 
544
515
      case DRIZZLE_TYPE_DECIMAL:
545
516
        pos[6]= (char)246;
546
517
        break;
592
563
  char buff[MAX_FIELD_WIDTH];
593
564
  String str(buff,sizeof(buff), &my_charset_bin);
594
565
 
595
 
  from->val_str_internal(&str);
 
566
  from->val_str(&str);
596
567
 
597
568
  return netStoreData((const unsigned char *)str.ptr(), str.length());
598
569
}
688
659
 
689
660
    server_capabilites= CLIENT_BASIC_FLAGS;
690
661
 
691
 
    if (_using_mysql41_protocol)
 
662
    if (using_mysql41_protocol)
692
663
      server_capabilites|= CLIENT_PROTOCOL_MYSQL41;
693
664
 
694
665
#ifdef HAVE_COMPRESS
736
707
      return false;
737
708
    }
738
709
  }
739
 
  if (packet.alloc(buffer_length.get()))
 
710
  if (packet.alloc(buffer_length))
740
711
    return false; /* The error is set by alloc(). */
741
712
 
742
713
  client_capabilities= uint2korr(net.read_pos);
814
785
    user_len-= 2;
815
786
  }
816
787
 
817
 
  if (client_capabilities & CLIENT_ADMIN)
818
 
  {
819
 
    if ((strncmp(user, "root", 4) == 0) and isAdminAllowed())
820
 
    {
821
 
      is_admin_connection= true;
822
 
    }
823
 
    else
824
 
    {
825
 
      my_error(ER_ADMIN_ACCESS, MYF(0));
826
 
      return false;
827
 
    }
828
 
  }
829
 
 
830
788
  session->getSecurityContext().setUser(user);
831
789
 
832
790
  return session->checkUser(string(passwd, passwd_len),
834
792
 
835
793
}
836
794
 
837
 
bool ClientMySQLProtocol::isAdminAllowed(void)
838
 
{
839
 
  if (std::find(mysql_admin_ip_addresses.begin(), mysql_admin_ip_addresses.end(), session->getSecurityContext().getIp()) != mysql_admin_ip_addresses.end())
840
 
    return true;
841
 
  else
842
 
    return false;
843
 
}
844
 
 
845
795
bool ClientMySQLProtocol::netStoreData(const unsigned char *from, size_t length)
846
796
{
847
797
  size_t packet_length= packet.length();
934
884
  uint32_t pointer_seed;
935
885
  memcpy(&pointer_seed, &pointer, 4);
936
886
  uint32_t random1= (seed + pointer_seed) % random_max;
937
 
  uint32_t random2= (seed + session->variables.pseudo_thread_id + net.vio->get_fd()) % random_max;
 
887
  uint32_t random2= (seed + session->variables.pseudo_thread_id + net.vio->sd) % random_max;
938
888
 
939
889
  for (char *end= scramble + SCRAMBLE_LENGTH; scramble != end; scramble++)
940
890
  {
944
894
  }
945
895
}
946
896
 
947
 
void ClientMySQLProtocol::mysql_compose_ip_addresses(vector<string> options)
948
 
{
949
 
  for (vector<string>::iterator it= options.begin();
950
 
       it != options.end();
951
 
       ++it)
952
 
  {
953
 
    tokenize(*it, mysql_admin_ip_addresses, ",", true);
954
 
  }
955
 
}
956
 
 
957
897
static ListenMySQLProtocol *listen_obj= NULL;
958
898
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
959
899
 
960
900
static int init(drizzled::module::Context &context)
961
901
{  
962
 
  context.add(new MysqlProtocolStatus);
 
902
  mysql_status_table_function_ptr= new MysqlProtocolStatus;
963
903
 
 
904
  context.add(mysql_status_table_function_ptr);
964
905
  /* Initialize random seeds for the MySQL algorithm with minimal changes. */
965
906
  time_t seed_time= time(NULL);
966
907
  random_seed1= seed_time % random_max;
967
908
  random_seed2= (seed_time / 2) % random_max;
968
909
 
969
910
  const module::option_map &vm= context.getOptions();
 
911
  if (vm.count("port"))
 
912
  { 
 
913
    if (port > 65535)
 
914
    {
 
915
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
 
916
      exit(-1);
 
917
    }
 
918
  }
 
919
 
 
920
  if (vm.count("connect-timeout"))
 
921
  {
 
922
    if (connect_timeout < 1 || connect_timeout > 300)
 
923
    {
 
924
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
 
925
      exit(-1);
 
926
    }
 
927
  }
 
928
 
 
929
  if (vm.count("read-timeout"))
 
930
  {
 
931
    if (read_timeout < 1 || read_timeout > 300)
 
932
    {
 
933
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
 
934
      exit(-1);
 
935
    }
 
936
  }
 
937
 
 
938
  if (vm.count("write-timeout"))
 
939
  {
 
940
    if (write_timeout < 1 || write_timeout > 300)
 
941
    {
 
942
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
 
943
      exit(-1);
 
944
    }
 
945
  }
 
946
 
 
947
  if (vm.count("retry-count"))
 
948
  {
 
949
    if (retry_count < 1 || retry_count > 100)
 
950
    {
 
951
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
 
952
      exit(-1);
 
953
    }
 
954
  }
 
955
 
 
956
  if (vm.count("buffer-length"))
 
957
  {
 
958
    if (buffer_length < 1024 || buffer_length > 1024*1024)
 
959
    {
 
960
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
 
961
      exit(-1);
 
962
    }
 
963
  }
 
964
 
 
965
  if (vm.count("bind-address"))
 
966
  {
 
967
    bind_address= strdup(vm["bind-address"].as<string>().c_str());
 
968
  }
 
969
 
 
970
  else
 
971
  {
 
972
    bind_address= NULL;
 
973
  }
970
974
 
971
975
  mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
972
976
  context.add(mysql_password);
973
977
 
974
 
  listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>(), true);
 
978
  listen_obj= new ListenMySQLProtocol("mysql_protocol", true);
975
979
  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>()));
984
 
 
985
 
  context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
986
980
 
987
981
  return 0;
988
982
}
989
983
 
 
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 "
 
986
                              "protocol."),
 
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,
 
999
                           1024*1024, 0);
 
1000
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
 
1001
                          N_("Address to bind to."), NULL, NULL, NULL);
 
1002
 
990
1003
static void init_options(drizzled::module::option_context &context)
991
1004
{
992
1005
  context("port",
993
 
          po::value<port_constraint>(&port)->default_value(3306),
 
1006
          po::value<uint32_t>(&port)->default_value(3306),
994
1007
          N_("Port number to use for connection or 0 for default to with MySQL "
995
1008
                              "protocol."));
996
1009
  context("connect-timeout",
997
 
          po::value<timeout_constraint>(&connect_timeout)->default_value(10),
 
1010
          po::value<uint32_t>(&connect_timeout)->default_value(10),
998
1011
          N_("Connect Timeout."));
999
1012
  context("read-timeout",
1000
 
          po::value<timeout_constraint>(&read_timeout)->default_value(30),
 
1013
          po::value<uint32_t>(&read_timeout)->default_value(30),
1001
1014
          N_("Read Timeout."));
1002
1015
  context("write-timeout",
1003
 
          po::value<timeout_constraint>(&write_timeout)->default_value(60),
 
1016
          po::value<uint32_t>(&write_timeout)->default_value(60),
1004
1017
          N_("Write Timeout."));
1005
1018
  context("retry-count",
1006
 
          po::value<retry_constraint>(&retry_count)->default_value(10),
 
1019
          po::value<uint32_t>(&retry_count)->default_value(10),
1007
1020
          N_("Retry Count."));
1008
1021
  context("buffer-length",
1009
 
          po::value<buffer_constraint>(&buffer_length)->default_value(16384),
 
1022
          po::value<uint32_t>(&buffer_length)->default_value(16384),
1010
1023
          N_("Buffer length."));
1011
1024
  context("bind-address",
1012
 
          po::value<string>()->default_value(""),
 
1025
          po::value<string>(),
1013
1026
          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."));
1020
1027
}
1021
1028
 
 
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),
 
1037
  NULL
 
1038
};
 
1039
 
1022
1040
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1023
1041
{
1024
1042
  var->type= SHOW_LONGLONG;
1025
1043
  var->value= buff;
1026
 
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
 
1044
  *((uint64_t *)buff)= ClientMySQLProtocol::connectionCount;
1027
1045
  return 0;
1028
1046
}
1029
1047
 
1031
1049
{
1032
1050
  var->type= SHOW_LONGLONG;
1033
1051
  var->value= buff;
1034
 
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connected;
 
1052
  *((uint64_t *)buff)= ClientMySQLProtocol::connected;
1035
1053
  return 0;
1036
1054
}
1037
1055
 
1039
1057
{
1040
1058
  var->type= SHOW_LONGLONG;
1041
1059
  var->value= buff;
1042
 
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->failedConnections;
 
1060
  *((uint64_t *)buff)= ClientMySQLProtocol::failedConnections;
1043
1061
  return 0;
1044
1062
}
1045
1063
 
1085
1103
 
1086
1104
    if (status_var_ptr->type == SHOW_FUNC)
1087
1105
    {
1088
 
      ((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
 
1106
      ((mysql_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
1089
1107
      value= buff;
1090
1108
      type= tmp.type;
1091
1109
    }
1116
1134
  return false;
1117
1135
}
1118
1136
 
1119
 
} /* namespace drizzle_plugin */
1120
 
 
1121
1137
DRIZZLE_DECLARE_PLUGIN
1122
1138
{
1123
1139
  DRIZZLE_VERSION_ID,
1126
1142
  "Eric Day",
1127
1143
  "MySQL Protocol Module",
1128
1144
  PLUGIN_LICENSE_GPL,
1129
 
  drizzle_plugin::init,             /* Plugin Init */
1130
 
  NULL, /* system variables */
1131
 
  drizzle_plugin::init_options    /* config options */
 
1145
  init,             /* Plugin Init */
 
1146
  sys_variables, /* system variables */
 
1147
  init_options    /* config options */
1132
1148
}
1133
1149
DRIZZLE_DECLARE_PLUGIN_END;