22#include <boost/program_options.hpp>
27#include <drizzled/vc_version.h>
28#include <drizzled/gettext.h>
29#include <drizzled/error.h>
30#include <drizzled/error/sql_state.h>
31#include <drizzled/session.h>
32#include <drizzled/internal/m_string.h>
34#include <drizzled/util/tokenize.h>
35#include <plugin/mysql_protocol/errmsg.h>
36#include <plugin/mysql_protocol/mysql_protocol.h>
37#include <plugin/mysql_protocol/mysql_password.h>
38#include <plugin/mysql_protocol/options.h>
39#include <drizzled/identifier.h>
40#include <drizzled/plugin/function.h>
41#include <drizzled/diagnostics_area.h>
42#include <drizzled/system_variables.h>
43#include <plugin/mysql_protocol/wire_types.h>
45#define MIN_HANDSHAKE_SIZE 6
46#define PROTOCOL_VERSION 10
48namespace po= boost::program_options;
54static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
56static port_constraint port;
57static timeout_constraint connect_timeout;
58static timeout_constraint read_timeout;
59static timeout_constraint write_timeout;
60static retry_constraint retry_count;
61static buffer_constraint buffer_length;
63static uint32_t random_seed1;
64static uint32_t random_seed2;
65static const uint32_t random_max= 0x3FFFFFFF;
66static const double random_max_double= (double)0x3FFFFFFF;
70void 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));
93ClientMySQLProtocol::ClientMySQLProtocol(
int fd,
ProtocolCounters& set_counters) :
94 _is_interactive(false),
95 counters(set_counters)
102 net.init(fd, buffer_length.get());
103 net.set_read_timeout(read_timeout.get());
104 net.set_write_timeout(write_timeout.get());
105 net.retry_count=retry_count.get();
108ClientMySQLProtocol::~ClientMySQLProtocol()
128 bool ret= net.write(packet.ptr(), packet.length());
139 counters.connected.decrement();
145 counters.connectionCount.increment();
146 counters.connected.increment();
149 net.set_read_timeout(connect_timeout.get());
150 net.set_write_timeout(connect_timeout.get());
152 if (checkConnection())
154 if (counters.connected > counters.max_connections)
156 std::string errmsg(ER(ER_CON_COUNT_ERROR));
157 sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
158 counters.failedConnections.increment();
167 sendError(session->main_da().sql_errno(), session->main_da().message());
168 counters.failedConnections.increment();
173 net.set_read_timeout(read_timeout.get());
174 net.set_write_timeout(write_timeout.get());
189 drizzleclient_net_set_read_timeout(&net,
190 session->variables.net_wait_timeout);
194 packet_length= net.read();
195 if (packet_length == packet_error)
199 if(net.last_errno== ER_NET_PACKET_TOO_LARGE)
200 my_error(ER_NET_PACKET_TOO_LARGE, MYF(0));
202 sendError(session->main_da().sql_errno(), session->main_da().message());
212 *l_packet= (
char*) net.read_pos;
223 if (packet_length == 0)
226 (*l_packet)[0]= COM_SLEEP;
232 switch ((*l_packet)[0])
241 (*l_packet)[0]= COM_SHUTDOWN;
245 (*l_packet)[0]= COM_KILL;
249 (*l_packet)[0]= COM_PING;
254 (*l_packet)[0]= COM_END;
260 (*l_packet)[packet_length]=
'\0';
265 drizzleclient_net_set_read_timeout(&net, session->variables.net_read_timeout);
294 unsigned char buff[DRIZZLE_ERRMSG_SIZE+10],*pos;
295 const char *message= NULL;
306 if (client_capabilities & CLIENT_FOUND_ROWS && session->main_da().found_rows())
307 pos=storeLength(buff+1,session->main_da().found_rows());
309 pos=storeLength(buff+1,session->main_da().affected_rows());
310 pos=storeLength(pos, session->main_da().last_insert_id());
311 int2store(pos, session->main_da().server_status());
313 tmp= min(session->main_da().total_warn_count(), (uint32_t)65535);
314 message= session->main_da().message();
318 pos=storeLength(buff+1,0);
319 pos=storeLength(pos, 0);
320 int2store(pos, session->server_status);
322 tmp= min(session->total_warn_count, (uint32_t)65535);
329 session->main_da().can_overwrite_status=
true;
331 if (message && message[0])
333 size_t length= strlen(message);
334 pos=storeLength(pos,length);
335 memcpy(pos,(
unsigned char*) message,length);
338 net.write(buff, pos - buff);
341 session->main_da().can_overwrite_status=
false;
364 session->main_da().can_overwrite_status=
true;
365 writeEOFPacket(session->main_da().server_status(), session->main_da().total_warn_count());
367 session->main_da().can_overwrite_status=
false;
369 packet.shrink(buffer_length.get());
376 unsigned char buff[2 + 1 + SQLSTATE_LENGTH + DRIZZLE_ERRMSG_SIZE];
378 assert(sql_errno != EE_OK);
379 assert(err && err[0]);
385 session->main_da().can_overwrite_status=
true;
388 session->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
403 int2store(buff,
static_cast<uint16_t
>(sql_errno));
407 unsigned char* pos= (
unsigned char*) strcpy((
char*) buff+3, error::convert_to_sqlstate(sql_errno));
408 pos+= strlen(error::convert_to_sqlstate(sql_errno));
410 char *tmp= strncpy((
char*)pos, err, DRIZZLE_ERRMSG_SIZE-1);
411 tmp+= strlen((
char*)pos);
413 net.write_command(255, data_ref(), data_ref(buff, tmp));
415 session->main_da().can_overwrite_status=
false;
438 List<Item>::iterator it(list.begin());
439 unsigned char buff[80];
440 String tmp((
char*) buff,
sizeof(buff),&my_charset_bin);
442 unsigned char *row_pos= storeLength(buff, list.size());
443 (void) net.write(buff, row_pos - buff);
445 while (
Item* item=it++)
448 item->make_field(&field);
452 store(STRING_WITH_LEN(
"def"));
453 store(field.db_name);
454 store(field.table_name);
455 store(field.org_table_name);
456 store(field.col_name);
457 store(field.org_col_name);
458 packet.realloc(packet.length()+12);
461 char* pos= (
char*) packet.ptr()+packet.length();
464 int2store(pos, field.charsetnr);
465 int4store(pos+2, field.length);
472 case DRIZZLE_TYPE_LONG:
473 pos[6]= DRIZZLE_COLUMN_TYPE_LONG;
476 case DRIZZLE_TYPE_DOUBLE:
477 pos[6]= DRIZZLE_COLUMN_TYPE_DOUBLE;
480 case DRIZZLE_TYPE_NULL:
481 pos[6]= DRIZZLE_COLUMN_TYPE_NULL;
484 case DRIZZLE_TYPE_TIMESTAMP:
485 pos[6]= DRIZZLE_COLUMN_TYPE_TIMESTAMP;
488 case DRIZZLE_TYPE_LONGLONG:
489 pos[6]= DRIZZLE_COLUMN_TYPE_LONGLONG;
492 case DRIZZLE_TYPE_DATETIME:
493 pos[6]= DRIZZLE_COLUMN_TYPE_DATETIME;
496 case DRIZZLE_TYPE_TIME:
497 pos[6]= DRIZZLE_COLUMN_TYPE_TIME;
500 case DRIZZLE_TYPE_DATE:
501 pos[6]= DRIZZLE_COLUMN_TYPE_DATE;
504 case DRIZZLE_TYPE_VARCHAR:
505 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
508 case DRIZZLE_TYPE_MICROTIME:
509 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
512 case DRIZZLE_TYPE_UUID:
513 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
516 case DRIZZLE_TYPE_IPV6:
517 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
520 case DRIZZLE_TYPE_BOOLEAN:
521 pos[6]= DRIZZLE_COLUMN_TYPE_TINY;
524 case DRIZZLE_TYPE_DECIMAL:
525 pos[6]= (char)DRIZZLE_COLUMN_TYPE_NEWDECIMAL;
528 case DRIZZLE_TYPE_ENUM:
529 pos[6]= (char)DRIZZLE_COLUMN_TYPE_ENUM;
532 case DRIZZLE_TYPE_BLOB:
533 pos[6]= (char)DRIZZLE_COLUMN_TYPE_BLOB;
540 pos[6]= field.type + 1;
543 int2store(pos+7,field.flags);
544 pos[9]= (char) field.decimals;
549 packet.length((uint32_t) (pos - packet.ptr()));
559 writeEOFPacket(session->server_status, session->total_warn_count);
562void ClientMySQLProtocol::store(
Field *from)
566 if (from->type() == DRIZZLE_TYPE_BOOLEAN)
568 return store(from->val_int());
571 char buff[MAX_FIELD_WIDTH];
572 String str(buff,
sizeof(buff), &my_charset_bin);
574 from->val_str_internal(&str);
576 netStoreData(str.ptr(), str.length());
579void ClientMySQLProtocol::store()
583 packet.append(buff,
sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
586void ClientMySQLProtocol::store(int32_t from)
589 netStoreData(buff, internal::int10_to_str(from, buff, -10) - buff);
592void ClientMySQLProtocol::store(uint32_t from)
595 netStoreData(buff, internal::int10_to_str(from, buff, 10) - buff);
598void ClientMySQLProtocol::store(int64_t from)
601 netStoreData(buff, internal::int64_t10_to_str(from, buff, -10) - buff);
604void ClientMySQLProtocol::store(uint64_t from)
607 netStoreData(buff, internal::int64_t10_to_str(from, buff, 10) - buff);
610void ClientMySQLProtocol::store(
double from, uint32_t decimals,
String *buffer)
612 buffer->set_real(from, decimals, session->charset());
613 netStoreData(buffer->ptr(), buffer->length());
616void ClientMySQLProtocol::store(
const char *from,
size_t length)
618 netStoreData(from, length);
621bool ClientMySQLProtocol::wasAborted()
623 return net.error_ && net.vio != 0;
626bool ClientMySQLProtocol::haveError()
628 return net.error_ || net.vio == 0;
631bool ClientMySQLProtocol::checkConnection()
633 char scramble[SCRAMBLE_LENGTH];
634 identifier::user::mptr user_identifier= identifier::User::make_shared();
636 makeScramble(scramble);
643 if (net.peer_addr(ip, NI_MAXHOST, peer_port))
645 my_error(ER_BAD_HOST_ERROR, MYF(0), ip);
649 user_identifier->setAddress(ip);
655 uint32_t server_capabilites;
658 char buff[SERVER_VERSION_LENGTH + SCRAMBLE_LENGTH + 64];
660 server_capabilites= CLIENT_BASIC_FLAGS | CLIENT_PROTOCOL_MYSQL41;
663 server_capabilites|= CLIENT_COMPRESS;
666 end= buff + strlen(DRIZZLE_RELEASE_VERSION);
667 if ((end - buff) >= SERVER_VERSION_LENGTH)
668 end= buff + (SERVER_VERSION_LENGTH - 1);
669 memcpy(buff, DRIZZLE_RELEASE_VERSION, end - buff);
673 int4store((
unsigned char*) end, session->variables.pseudo_thread_id);
677 memcpy(end, scramble, SCRAMBLE_LENGTH_323);
678 end+= SCRAMBLE_LENGTH_323;
681 int2store(end, server_capabilites);
683 end[2]=(char) default_charset_info->number;
684 int2store(end+3, session->server_status);
685 memset(end+5, 0, 13);
689 memcpy(end, scramble + SCRAMBLE_LENGTH_323, SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
690 end+= (SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
694 if (net.write_command(PROTOCOL_VERSION, data_ref(), data_ref(buff, end))
695 || (pkt_len= net.read()) == packet_error
696 || pkt_len < MIN_HANDSHAKE_SIZE)
698 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
702 packet.alloc(buffer_length.get());
704 client_capabilities= uint2korr(net.read_pos);
705 if (not (client_capabilities & CLIENT_PROTOCOL_MYSQL41))
707 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
711 client_capabilities|= ((uint32_t) uint2korr(net.read_pos + 2)) << 16;
713 end= (
char*) net.read_pos + 32;
719 client_capabilities&= server_capabilites;
721 if (end >= (
char*) net.read_pos + pkt_len + 2)
723 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
728 char *passwd= strchr(user,
'\0')+1;
729 uint32_t user_len= passwd - user - 1;
739 if (client_capabilities & CLIENT_SECURE_CONNECTION &&
740 passwd < (
char *) net.read_pos + pkt_len)
742 passwd_len= (
unsigned char)(*passwd++);
743 if (passwd_len > 0 and client_capabilities & CLIENT_CAPABILITIES_PLUGIN_AUTH)
745 user_identifier->setPasswordType(identifier::User::PLAIN_TEXT);
747 else if (passwd_len > 0)
749 user_identifier->setPasswordType(identifier::User::MYSQL_HASH);
750 user_identifier->setPasswordContext(scramble, SCRAMBLE_LENGTH);
758 if (client_capabilities & CLIENT_CONNECT_WITH_DB &&
759 passwd < (
char *) net.read_pos + pkt_len)
761 l_db= l_db + passwd_len + 1;
769 uint32_t db_len= l_db ? strlen(l_db) : 0;
771 if (passwd + passwd_len + db_len > (
char *) net.read_pos + pkt_len)
773 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
778 if (user_len > 1 && user[0] ==
'\'' && user[user_len - 1] ==
'\'')
785 if (client_capabilities & CLIENT_INTERACTIVE)
787 _is_interactive=
true;
790 if (client_capabilities & CLIENT_CAPABILITIES_PLUGIN_AUTH)
792 passwd_len= strlen(passwd);
795 user_identifier->setUser(user);
796 session->setUser(user_identifier);
798 return session->checkUser(
string(passwd, passwd_len),
string(l_db ? l_db :
""));
802void ClientMySQLProtocol::netStoreData(
const void* from,
size_t length)
804 size_t packet_length= packet.length();
809 if (packet_length+9+length > packet.alloced_length())
810 packet.realloc(packet_length+9+length);
811 unsigned char *to= storeLength((
unsigned char*) packet.ptr()+packet_length, length);
812 memcpy(to,from,length);
813 packet.length((
size_t) (to+length-(
unsigned char*) packet.ptr()));
823 unsigned char buff[5];
828 uint32_t tmp= min(total_warn_count, (uint32_t)65535);
829 buff[0]= DRIZZLE_PROTOCOL_NO_MORE_DATA;
830 int2store(buff+1, tmp);
836 if (session->is_fatal_error)
837 server_status&= ~SERVER_MORE_RESULTS_EXISTS;
838 int2store(buff + 3, server_status);
855unsigned char *ClientMySQLProtocol::storeLength(
unsigned char *buffer, uint64_t length)
859 *buffer= (
unsigned char) length;
866 int2store(buffer, (uint32_t) length);
869 if (length < 16777216)
872 int3store(buffer, (uint32_t) length);
876 int8store(buffer, length);
880void ClientMySQLProtocol::makeScramble(
char *scramble)
883 random_seed1= (random_seed1 * 3 + random_seed2) % random_max;
884 random_seed2= (random_seed1 + random_seed2 + 33) % random_max;
885 uint32_t seed=
static_cast<uint32_t
>((
static_cast<double>(random_seed1) / random_max_double) * 0xffffffff);
888 uint32_t pointer_seed;
889 memcpy(&pointer_seed, &pointer, 4);
890 uint32_t random1= (seed + pointer_seed) % random_max;
891 uint32_t random2= (seed + session->variables.pseudo_thread_id + net.vio->get_fd()) % random_max;
893 for (
char *end= scramble + SCRAMBLE_LENGTH; scramble != end; scramble++)
895 random1= (random1 * 3 + random2) % random_max;
896 random2= (random1 + random2 + 33) % random_max;
897 *scramble=
static_cast<char>((
static_cast<double>(random1) / random_max_double) * 94 + 33);
904 time_t seed_time= time(NULL);
905 random_seed1= seed_time % random_max;
906 random_seed2= (seed_time / 2) % random_max;
908 const module::option_map &vm= context.getOptions();
910 context.add(
new plugin::Create_function<MySQLPassword>(MySQLPasswordName));
913 listen_obj->addCountersToTable();
914 context.add(listen_obj);
922 context.registerVariable(
new sys_var_uint32_t_ptr(
"max-connections", &ListenMySQLProtocol::mysql_counters.max_connections));
930 po::value<port_constraint>(&port)->default_value(3306),
931 _(
"Port number to use for connection or 0 for default to with MySQL "
933 context(
"connect-timeout",
934 po::value<timeout_constraint>(&connect_timeout)->default_value(10),
935 _(
"Connect Timeout."));
936 context(
"read-timeout",
937 po::value<timeout_constraint>(&read_timeout)->default_value(30),
939 context(
"write-timeout",
940 po::value<timeout_constraint>(&write_timeout)->default_value(60),
941 _(
"Write Timeout."));
942 context(
"retry-count",
943 po::value<retry_constraint>(&retry_count)->default_value(10),
945 context(
"buffer-length",
946 po::value<buffer_constraint>(&buffer_length)->default_value(16384),
947 _(
"Buffer length."));
948 context(
"bind-address",
949 po::value<string>()->default_value(
"0.0.0.0"),
950 _(
"Address to bind to."));
951 context(
"max-connections",
952 po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters.max_connections)->default_value(1000),
953 _(
"Maximum simultaneous connections."));
958DRIZZLE_DECLARE_PLUGIN
964 N_(
"MySQL network protocol"),
966 drizzle_plugin::init,
968 drizzle_plugin::init_options
970DRIZZLE_DECLARE_PLUGIN_END;
void writeEOFPacket(uint32_t server_status, uint32_t total_warn_count)
virtual bool authenticate()
virtual int getFileDescriptor()
virtual void sendFields(drizzled::List< drizzled::Item > &)
virtual void sendError(const drizzled::error_t sql_errno, const char *err)
virtual bool isConnected()
virtual bool readCommand(char **packet, uint32_t &packet_length)
virtual in_port_t getPort() const
virtual const std::string getHost() const
virtual drizzled::plugin::Client * getClient(int fd)
TODO: Rename this file - func.h is stupid.
An Proxy Wrapper around boost::program_options::variables_map.