~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/mysql_protocol.cc

Merged in move of status variables.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 *
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
18
18
 */
19
19
 
20
20
#include "config.h"
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"
 
28
 
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"
36
 
 
37
 
#define PROTOCOL_VERSION 10
38
 
 
39
 
namespace po= boost::program_options;
 
33
 
40
34
using namespace std;
41
35
using namespace drizzled;
42
36
 
43
 
namespace drizzle_plugin
 
37
#define PROTOCOL_VERSION 10
 
38
 
 
39
namespace drizzled
44
40
{
 
41
extern uint32_t global_thread_id;
 
42
}
45
43
 
46
 
std::vector<std::string> ClientMySQLProtocol::mysql_admin_ip_addresses;
47
44
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
 
 
 
45
static uint32_t port;
 
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;
60
56
 
61
 
 
62
 
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
63
 
 
64
 
ListenMySQLProtocol::~ListenMySQLProtocol()
65
 
{ }
66
 
 
67
 
const std::string ListenMySQLProtocol::getHost(void) const
 
57
const char* ListenMySQLProtocol::getHost(void) const
68
58
{
69
 
  return _hostname;
 
59
  return bind_address;
70
60
}
71
61
 
72
62
in_port_t ListenMySQLProtocol::getPort(void) const
73
63
{
74
 
  return port.get();
 
64
  return (in_port_t) port;
75
65
}
76
66
 
77
67
plugin::Client *ListenMySQLProtocol::getClient(int fd)
81
71
  if (new_fd == -1)
82
72
    return NULL;
83
73
 
84
 
  return new ClientMySQLProtocol(new_fd, _using_mysql41_protocol, getCounters());
 
74
  return new (nothrow) ClientMySQLProtocol(new_fd, using_mysql41_protocol);
85
75
}
86
76
 
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)
 
77
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol_arg):
 
78
  using_mysql41_protocol(using_mysql41_protocol_arg)
91
79
{
92
 
  
93
80
  net.vio= 0;
94
81
 
95
82
  if (fd == -1)
96
83
    return;
97
84
 
98
 
  if (drizzleclient_net_init_sock(&net, fd, buffer_length.get()))
 
85
  if (drizzleclient_net_init_sock(&net, fd, buffer_length))
99
86
    throw bad_alloc();
100
87
 
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();
 
88
  drizzleclient_net_set_read_timeout(&net, read_timeout);
 
89
  drizzleclient_net_set_write_timeout(&net, write_timeout);
 
90
  net.retry_count=retry_count;
104
91
}
105
92
 
106
93
ClientMySQLProtocol::~ClientMySQLProtocol()
107
94
{
108
95
  if (net.vio)
109
 
    net.vio->close();
 
96
    vio_close(net.vio);
110
97
}
111
98
 
112
99
int ClientMySQLProtocol::getFileDescriptor(void)
145
132
  { 
146
133
    drizzleclient_net_close(&net);
147
134
    drizzleclient_net_end(&net);
148
 
    if (is_admin_connection)
149
 
      counters->adminConnected.decrement();
150
 
    else
151
 
      counters->connected.decrement();
152
135
  }
153
136
}
154
137
 
155
138
bool ClientMySQLProtocol::authenticate()
156
139
{
157
140
  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
 
  }
168
141
 
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);
172
145
 
173
146
  connection_is_valid= checkConnection();
174
147
 
175
148
  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
 
  }
 
149
    sendOK();
188
150
  else
189
151
  {
190
152
    sendError(session->main_da.sql_errno(), session->main_da.message());
191
 
    counters->failedConnections.increment();
192
153
    return false;
193
154
  }
194
155
 
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);
198
159
  return true;
199
160
}
200
161
 
231
192
      return false;                       // We have to close it.
232
193
 
233
194
    net.error= 0;
 
195
    *packet_length= 0;
 
196
    return true;
234
197
  }
235
198
 
236
199
  *l_packet= (char*) net.read_pos;
250
213
    (*l_packet)[0]= (unsigned char) COM_SLEEP;
251
214
    *packet_length= 1;
252
215
  }
253
 
  else if (_using_mysql41_protocol)
 
216
  else if (using_mysql41_protocol)
254
217
  {
255
218
    /* Map from MySQL commands to Drizzle commands. */
256
219
    switch ((int)(*l_packet)[0])
390
353
    drizzleclient_net_flush(&net);
391
354
    session->main_da.can_overwrite_status= false;
392
355
  }
393
 
  packet.shrink(buffer_length.get());
 
356
  packet.shrink(buffer_length);
394
357
}
395
358
 
396
359
 
443
406
 
444
407
  drizzleclient_net_write_command(&net,(unsigned char) 255, (unsigned char*) "", 0, (unsigned char*) err, length);
445
408
 
446
 
  drizzleclient_net_flush(&net);
447
 
 
448
409
  session->main_da.can_overwrite_status= false;
449
410
}
450
411
 
500
461
    int2store(pos, field.charsetnr);
501
462
    int4store(pos+2, field.length);
502
463
 
503
 
    if (_using_mysql41_protocol)
 
464
    if (using_mysql41_protocol)
504
465
    {
505
466
      /* Switch to MySQL field numbering. */
506
467
      switch (field.type)
537
498
        pos[6]= 15;
538
499
        break;
539
500
 
540
 
      case DRIZZLE_TYPE_UUID:
541
 
        pos[6]= 15;
542
 
        break;
543
 
 
544
501
      case DRIZZLE_TYPE_DECIMAL:
545
502
        pos[6]= (char)246;
546
503
        break;
592
549
  char buff[MAX_FIELD_WIDTH];
593
550
  String str(buff,sizeof(buff), &my_charset_bin);
594
551
 
595
 
  from->val_str_internal(&str);
 
552
  from->val_str(&str);
596
553
 
597
554
  return netStoreData((const unsigned char *)str.ptr(), str.length());
598
555
}
688
645
 
689
646
    server_capabilites= CLIENT_BASIC_FLAGS;
690
647
 
691
 
    if (_using_mysql41_protocol)
 
648
    if (using_mysql41_protocol)
692
649
      server_capabilites|= CLIENT_PROTOCOL_MYSQL41;
693
650
 
694
651
#ifdef HAVE_COMPRESS
702
659
    *end= 0;
703
660
    end++;
704
661
 
705
 
    int4store((unsigned char*) end, session->variables.pseudo_thread_id);
 
662
    int4store((unsigned char*) end, global_thread_id);
706
663
    end+= 4;
707
664
 
708
665
    /* We don't use scramble anymore. */
736
693
      return false;
737
694
    }
738
695
  }
739
 
  if (packet.alloc(buffer_length.get()))
 
696
  if (packet.alloc(buffer_length))
740
697
    return false; /* The error is set by alloc(). */
741
698
 
742
699
  client_capabilities= uint2korr(net.read_pos);
814
771
    user_len-= 2;
815
772
  }
816
773
 
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
774
  session->getSecurityContext().setUser(user);
831
775
 
832
 
  return session->checkUser(string(passwd, passwd_len),
833
 
                            string(l_db ? l_db : ""));
834
 
 
835
 
}
836
 
 
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;
 
776
  return session->checkUser(passwd, passwd_len, l_db);
843
777
}
844
778
 
845
779
bool ClientMySQLProtocol::netStoreData(const unsigned char *from, size_t length)
934
868
  uint32_t pointer_seed;
935
869
  memcpy(&pointer_seed, &pointer, 4);
936
870
  uint32_t random1= (seed + pointer_seed) % random_max;
937
 
  uint32_t random2= (seed + session->variables.pseudo_thread_id + net.vio->get_fd()) % random_max;
 
871
  uint32_t random2= (seed + global_thread_id + net.vio->sd) % random_max;
938
872
 
939
873
  for (char *end= scramble + SCRAMBLE_LENGTH; scramble != end; scramble++)
940
874
  {
944
878
  }
945
879
}
946
880
 
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
881
static ListenMySQLProtocol *listen_obj= NULL;
958
882
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
959
883
 
960
884
static int init(drizzled::module::Context &context)
961
 
{  
962
 
  context.add(new MysqlProtocolStatus);
963
 
 
 
885
{
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;
968
890
 
969
 
  const module::option_map &vm= context.getOptions();
970
 
 
971
891
  mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
972
892
  context.add(mysql_password);
973
893
 
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>()));
984
 
 
985
 
  context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
986
 
 
987
 
  return 0;
988
 
}
989
 
 
990
 
static void init_options(drizzled::module::option_context &context)
991
 
{
992
 
  context("port",
993
 
          po::value<port_constraint>(&port)->default_value(3306),
994
 
          N_("Port number to use for connection or 0 for default to with MySQL "
995
 
                              "protocol."));
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."));
1020
 
}
1021
 
 
1022
 
static int mysql_protocol_connection_count_func(drizzle_show_var *var, char *buff)
1023
 
{
1024
 
  var->type= SHOW_LONGLONG;
1025
 
  var->value= buff;
1026
 
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connectionCount;
1027
 
  return 0;
1028
 
}
1029
 
 
1030
 
static int mysql_protocol_connected_count_func(drizzle_show_var *var, char *buff)
1031
 
{
1032
 
  var->type= SHOW_LONGLONG;
1033
 
  var->value= buff;
1034
 
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->connected;
1035
 
  return 0;
1036
 
}
1037
 
 
1038
 
static int mysql_protocol_failed_count_func(drizzle_show_var *var, char *buff)
1039
 
{
1040
 
  var->type= SHOW_LONGLONG;
1041
 
  var->value= buff;
1042
 
  *((uint64_t *)buff)= ListenMySQLProtocol::mysql_counters->failedConnections;
1043
 
  return 0;
1044
 
}
1045
 
 
1046
 
static st_show_var_func_container mysql_protocol_connection_count=
1047
 
  { &mysql_protocol_connection_count_func };
1048
 
 
1049
 
static st_show_var_func_container mysql_protocol_connected_count=
1050
 
  { &mysql_protocol_connected_count_func };
1051
 
 
1052
 
static st_show_var_func_container mysql_protocol_failed_count=
1053
 
  { &mysql_protocol_failed_count_func };
1054
 
 
1055
 
static drizzle_show_var mysql_protocol_status_variables[]= {
1056
 
  {"Connections",
1057
 
  (char*) &mysql_protocol_connection_count, SHOW_FUNC},
1058
 
  {"Connected",
1059
 
  (char*) &mysql_protocol_connected_count, SHOW_FUNC},
1060
 
  {"Failed_connections",
1061
 
  (char*) &mysql_protocol_failed_count, SHOW_FUNC},
1062
 
  {NULL, NULL, SHOW_LONGLONG}
 
896
 
 
897
  return 0;
 
898
}
 
899
 
 
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 "
 
902
                              "protocol."),
 
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,
 
915
                           1024*1024, 0);
 
916
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
 
917
                          N_("Address to bind to."), NULL, NULL, NULL);
 
918
 
 
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),
 
927
  NULL
1063
928
};
1064
929
 
1065
 
MysqlProtocolStatus::Generator::Generator(drizzled::Field **fields) :
1066
 
  plugin::TableFunction::Generator(fields)
1067
 
{
1068
 
  status_var_ptr= mysql_protocol_status_variables;
1069
 
}
1070
 
 
1071
 
bool MysqlProtocolStatus::Generator::populate()
1072
 
{
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;
1076
 
 
1077
 
  if (status_var_ptr->name)
1078
 
  {
1079
 
    std::ostringstream oss;
1080
 
    string return_value;
1081
 
    const char *value;
1082
 
    int type;
1083
 
 
1084
 
    push(status_var_ptr->name);
1085
 
 
1086
 
    if (status_var_ptr->type == SHOW_FUNC)
1087
 
    {
1088
 
      ((drizzle_show_var_func)((st_show_var_func_container *)status_var_ptr->value)->func)(&tmp, buff);
1089
 
      value= buff;
1090
 
      type= tmp.type;
1091
 
    }
1092
 
    else
1093
 
    {
1094
 
      value= status_var_ptr->value;
1095
 
      type= status_var_ptr->type;
1096
 
    }
1097
 
 
1098
 
    switch(type)
1099
 
    {
1100
 
    case SHOW_LONGLONG:
1101
 
      oss << *(uint64_t*) value;
1102
 
      return_value= oss.str();
1103
 
      break;
1104
 
    default:
1105
 
      assert(0);
1106
 
    }
1107
 
    if (return_value.length())
1108
 
      push(return_value);
1109
 
    else
1110
 
      push(" ");
1111
 
 
1112
 
    status_var_ptr++;
1113
 
 
1114
 
    return true;
1115
 
  }
1116
 
  return false;
1117
 
}
1118
 
 
1119
 
} /* namespace drizzle_plugin */
1120
 
 
1121
930
DRIZZLE_DECLARE_PLUGIN
1122
931
{
1123
932
  DRIZZLE_VERSION_ID,
1124
 
  "mysql-protocol",
 
933
  "mysql_protocol",
1125
934
  "0.1",
1126
935
  "Eric Day",
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 */
1132
941
}
1133
942
DRIZZLE_DECLARE_PLUGIN_END;