drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mysql_protocol.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
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
18 */
19
20#include <config.h>
21
22#include <boost/program_options.hpp>
23
24#include <algorithm>
25#include <climits>
26
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>
44
45#define MIN_HANDSHAKE_SIZE 6
46#define PROTOCOL_VERSION 10
47
48namespace po= boost::program_options;
49using namespace std;
50using namespace drizzled;
51
52namespace drizzle_plugin {
53
54static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
55
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;
62
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;
67
68ProtocolCounters ListenMySQLProtocol::mysql_counters;
69
70void ListenMySQLProtocol::addCountersToTable()
71{
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));
75}
76
77const std::string ListenMySQLProtocol::getHost() const
78{
79 return _hostname;
80}
81
83{
84 return port.get();
85}
86
88{
89 int new_fd= acceptTcp(fd);
90 return new_fd == -1 ? NULL : new ClientMySQLProtocol(new_fd, getCounters());
91}
92
93ClientMySQLProtocol::ClientMySQLProtocol(int fd, ProtocolCounters& set_counters) :
94 _is_interactive(false),
95 counters(set_counters)
96{
97 net.vio= 0;
98
99 if (fd == -1)
100 return;
101
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();
106}
107
108ClientMySQLProtocol::~ClientMySQLProtocol()
109{
110 if (net.vio)
111 net.vio->close();
112}
113
115{
116 return net.get_sd();
117}
118
120{
121 return net.vio != 0;
122}
123
125{
126 if (net.vio == NULL)
127 return false;
128 bool ret= net.write(packet.ptr(), packet.length());
129 packet.length(0);
130 return ret;
131}
132
134{
135 if (net.vio)
136 {
137 net.close();
138 net.end();
139 counters.connected.decrement();
140 }
141}
142
144{
145 counters.connectionCount.increment();
146 counters.connected.increment();
147
148 /* Use "connect_timeout" value during connection phase */
149 net.set_read_timeout(connect_timeout.get());
150 net.set_write_timeout(connect_timeout.get());
151
152 if (checkConnection())
153 {
154 if (counters.connected > counters.max_connections)
155 {
156 std::string errmsg(ER(ER_CON_COUNT_ERROR));
157 sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
158 counters.failedConnections.increment();
159 }
160 else
161 {
162 sendOK();
163 }
164 }
165 else
166 {
167 sendError(session->main_da().sql_errno(), session->main_da().message());
168 counters.failedConnections.increment();
169 return false;
170 }
171
172 /* Connect completed, set read/write timeouts back to default */
173 net.set_read_timeout(read_timeout.get());
174 net.set_write_timeout(write_timeout.get());
175 return true;
176}
177
178bool ClientMySQLProtocol::readCommand(char **l_packet, uint32_t& packet_length)
179{
180 /*
181 This thread will do a blocking read from the client which
182 will be interrupted when the next command is received from
183 the client, the connection is closed or "net_wait_timeout"
184 number of seconds has passed
185 */
186#ifdef NEVER
187 /* We can do this much more efficiently with poll timeouts or watcher thread,
188 disabling for now, which means net_wait_timeout == read_timeout. */
189 drizzleclient_net_set_read_timeout(&net,
190 session->variables.net_wait_timeout);
191#endif
192
193 net.pkt_nr=0;
194 packet_length= net.read();
195 if (packet_length == packet_error)
196 {
197 /* Check if we can continue without closing the connection */
198
199 if(net.last_errno== ER_NET_PACKET_TOO_LARGE)
200 my_error(ER_NET_PACKET_TOO_LARGE, MYF(0));
201 if (session->main_da().status() == Diagnostics_area::DA_ERROR)
202 sendError(session->main_da().sql_errno(), session->main_da().message());
203 else
204 sendOK();
205
206 if (net.error_ != 3)
207 return false; // We have to close it.
208
209 net.error_= 0;
210 }
211
212 *l_packet= (char*) net.read_pos;
213
214 /*
215 'packet_length' contains length of data, as it was stored in packet
216 header. In case of malformed header, drizzleclient_net_read returns zero.
217 If packet_length is not zero, drizzleclient_net_read ensures that the returned
218 number of bytes was actually read from network.
219 There is also an extra safety measure in drizzleclient_net_read:
220 it sets packet[packet_length]= 0, but only for non-zero packets.
221 */
222
223 if (packet_length == 0) /* safety */
224 {
225 /* Initialize with COM_SLEEP packet */
226 (*l_packet)[0]= COM_SLEEP;
227 packet_length= 1;
228 }
229 else
230 {
231 /* Map from MySQL commands to Drizzle commands. */
232 switch ((*l_packet)[0])
233 {
234 case 0: /* SLEEP */
235 case 1: /* QUIT */
236 case 2: /* INIT_DB */
237 case 3: /* QUERY */
238 break;
239
240 case 8: /* SHUTDOWN */
241 (*l_packet)[0]= COM_SHUTDOWN;
242 break;
243
244 case 12: /* KILL */
245 (*l_packet)[0]= COM_KILL;
246 break;
247
248 case 14: /* PING */
249 (*l_packet)[0]= COM_PING;
250 break;
251
252 default:
253 /* Respond with unknown command for MySQL commands we don't support. */
254 (*l_packet)[0]= COM_END;
255 packet_length= 1;
256 }
257 }
258
259 /* Do not rely on drizzleclient_net_read, extra safety against programming errors. */
260 (*l_packet)[packet_length]= '\0'; /* safety */
261
262#ifdef NEVER
263 /* See comment above. */
264 /* Restore read timeout value */
265 drizzleclient_net_set_read_timeout(&net, session->variables.net_read_timeout);
266#endif
267
268 return true;
269}
270
291
293{
294 unsigned char buff[DRIZZLE_ERRMSG_SIZE+10],*pos;
295 const char *message= NULL;
296 uint32_t tmp;
297
298 if (!net.vio) // hack for re-parsing queries
299 {
300 return;
301 }
302
303 buff[0]=0; // No fields
304 if (session->main_da().status() == Diagnostics_area::DA_OK)
305 {
306 if (client_capabilities & CLIENT_FOUND_ROWS && session->main_da().found_rows())
307 pos=storeLength(buff+1,session->main_da().found_rows());
308 else
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());
312 pos+=2;
313 tmp= min(session->main_da().total_warn_count(), (uint32_t)65535);
314 message= session->main_da().message();
315 }
316 else
317 {
318 pos=storeLength(buff+1,0);
319 pos=storeLength(pos, 0);
320 int2store(pos, session->server_status);
321 pos+=2;
322 tmp= min(session->total_warn_count, (uint32_t)65535);
323 }
324
325 /* We can only return up to 65535 warnings in two bytes */
326 int2store(pos, tmp);
327 pos+= 2;
328
329 session->main_da().can_overwrite_status= true;
330
331 if (message && message[0])
332 {
333 size_t length= strlen(message);
334 pos=storeLength(pos,length);
335 memcpy(pos,(unsigned char*) message,length);
336 pos+=length;
337 }
338 net.write(buff, pos - buff);
339 net.flush();
340
341 session->main_da().can_overwrite_status= false;
342}
343
358
360{
361 /* Set to true if no active vio, to work well in case of --init-file */
362 if (net.vio)
363 {
364 session->main_da().can_overwrite_status= true;
365 writeEOFPacket(session->main_da().server_status(), session->main_da().total_warn_count());
366 net.flush();
367 session->main_da().can_overwrite_status= false;
368 }
369 packet.shrink(buffer_length.get());
370}
371
372
373void ClientMySQLProtocol::sendError(drizzled::error_t sql_errno, const char *err)
374{
375 // buff[]: sql_errno:2 + ('#':1 + SQLSTATE_LENGTH:5) + DRIZZLE_ERRMSG_SIZE:512
376 unsigned char buff[2 + 1 + SQLSTATE_LENGTH + DRIZZLE_ERRMSG_SIZE];
377
378 assert(sql_errno != EE_OK);
379 assert(err && err[0]);
380
381 /*
382 It's one case when we can push an error even though there
383 is an OK or EOF already.
384 */
385 session->main_da().can_overwrite_status= true;
386
387 /* Abort multi-result sets */
388 session->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
389
397
398 if (net.vio == 0)
399 {
400 return;
401 }
402
403 int2store(buff, static_cast<uint16_t>(sql_errno));
404
405 /* The first # is to make the client backward compatible */
406 buff[2]= '#';
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));
409
410 char *tmp= strncpy((char*)pos, err, DRIZZLE_ERRMSG_SIZE-1);
411 tmp+= strlen((char*)pos);
412 tmp[0]= '\0';
413 net.write_command(255, data_ref(), data_ref(buff, tmp));
414 net.flush();
415 session->main_da().can_overwrite_status= false;
416}
417
437{
438 List<Item>::iterator it(list.begin());
439 unsigned char buff[80];
440 String tmp((char*) buff,sizeof(buff),&my_charset_bin);
441
442 unsigned char *row_pos= storeLength(buff, list.size());
443 (void) net.write(buff, row_pos - buff);
444
445 while (Item* item=it++)
446 {
447 SendField field;
448 item->make_field(&field);
449
450 packet.length(0);
451
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);
459
460 /* Store fixed length fields */
461 char* pos= (char*) packet.ptr()+packet.length();
462 *pos++= 12; // Length of packed fields
463 /* No conversion */
464 int2store(pos, field.charsetnr);
465 int4store(pos+2, field.length);
466
467 if (true) // _using_mysql41_protocol)
468 {
469 /* Switch to MySQL field numbering. */
470 switch (field.type)
471 {
472 case DRIZZLE_TYPE_LONG:
473 pos[6]= DRIZZLE_COLUMN_TYPE_LONG;
474 break;
475
476 case DRIZZLE_TYPE_DOUBLE:
477 pos[6]= DRIZZLE_COLUMN_TYPE_DOUBLE;
478 break;
479
480 case DRIZZLE_TYPE_NULL:
481 pos[6]= DRIZZLE_COLUMN_TYPE_NULL;
482 break;
483
484 case DRIZZLE_TYPE_TIMESTAMP:
485 pos[6]= DRIZZLE_COLUMN_TYPE_TIMESTAMP;
486 break;
487
488 case DRIZZLE_TYPE_LONGLONG:
489 pos[6]= DRIZZLE_COLUMN_TYPE_LONGLONG;
490 break;
491
492 case DRIZZLE_TYPE_DATETIME:
493 pos[6]= DRIZZLE_COLUMN_TYPE_DATETIME;
494 break;
495
496 case DRIZZLE_TYPE_TIME:
497 pos[6]= DRIZZLE_COLUMN_TYPE_TIME;
498 break;
499
500 case DRIZZLE_TYPE_DATE:
501 pos[6]= DRIZZLE_COLUMN_TYPE_DATE;
502 break;
503
504 case DRIZZLE_TYPE_VARCHAR:
505 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
506 break;
507
508 case DRIZZLE_TYPE_MICROTIME:
509 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
510 break;
511
512 case DRIZZLE_TYPE_UUID:
513 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
514 break;
515
516 case DRIZZLE_TYPE_IPV6:
517 pos[6]= DRIZZLE_COLUMN_TYPE_VARCHAR;
518 break;
519
520 case DRIZZLE_TYPE_BOOLEAN:
521 pos[6]= DRIZZLE_COLUMN_TYPE_TINY;
522 break;
523
524 case DRIZZLE_TYPE_DECIMAL:
525 pos[6]= (char)DRIZZLE_COLUMN_TYPE_NEWDECIMAL;
526 break;
527
528 case DRIZZLE_TYPE_ENUM:
529 pos[6]= (char)DRIZZLE_COLUMN_TYPE_ENUM;
530 break;
531
532 case DRIZZLE_TYPE_BLOB:
533 pos[6]= (char)DRIZZLE_COLUMN_TYPE_BLOB;
534 break;
535 }
536 }
537 else
538 {
539 /* Add one to compensate for tinyint removal from enum. */
540 pos[6]= field.type + 1;
541 }
542
543 int2store(pos+7,field.flags);
544 pos[9]= (char) field.decimals;
545 pos[10]= 0; // For the future
546 pos[11]= 0; // For the future
547 pos+= 12;
548
549 packet.length((uint32_t) (pos - packet.ptr()));
550 if (flush())
551 break;
552 }
553
554 /*
555 Mark the end of meta-data result set, and store session->server_status,
556 to show that there is no cursor.
557 Send no warning information, as it will be sent at statement end.
558 */
559 writeEOFPacket(session->server_status, session->total_warn_count);
560}
561
562void ClientMySQLProtocol::store(Field *from)
563{
564 if (from->is_null())
565 return store();
566 if (from->type() == DRIZZLE_TYPE_BOOLEAN)
567 {
568 return store(from->val_int());
569 }
570
571 char buff[MAX_FIELD_WIDTH];
572 String str(buff,sizeof(buff), &my_charset_bin);
573
574 from->val_str_internal(&str);
575
576 netStoreData(str.ptr(), str.length());
577}
578
579void ClientMySQLProtocol::store()
580{
581 char buff[1];
582 buff[0]= (char)251;
583 packet.append(buff, sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
584}
585
586void ClientMySQLProtocol::store(int32_t from)
587{
588 char buff[12];
589 netStoreData(buff, internal::int10_to_str(from, buff, -10) - buff);
590}
591
592void ClientMySQLProtocol::store(uint32_t from)
593{
594 char buff[11];
595 netStoreData(buff, internal::int10_to_str(from, buff, 10) - buff);
596}
597
598void ClientMySQLProtocol::store(int64_t from)
599{
600 char buff[22];
601 netStoreData(buff, internal::int64_t10_to_str(from, buff, -10) - buff);
602}
603
604void ClientMySQLProtocol::store(uint64_t from)
605{
606 char buff[21];
607 netStoreData(buff, internal::int64_t10_to_str(from, buff, 10) - buff);
608}
609
610void ClientMySQLProtocol::store(double from, uint32_t decimals, String *buffer)
611{
612 buffer->set_real(from, decimals, session->charset());
613 netStoreData(buffer->ptr(), buffer->length());
614}
615
616void ClientMySQLProtocol::store(const char *from, size_t length)
617{
618 netStoreData(from, length);
619}
620
621bool ClientMySQLProtocol::wasAborted()
622{
623 return net.error_ && net.vio != 0;
624}
625
626bool ClientMySQLProtocol::haveError()
627{
628 return net.error_ || net.vio == 0;
629}
630
631bool ClientMySQLProtocol::checkConnection()
632{
633 char scramble[SCRAMBLE_LENGTH];
634 identifier::user::mptr user_identifier= identifier::User::make_shared();
635
636 makeScramble(scramble);
637
638 // TCP/IP connection
639 {
640 char ip[NI_MAXHOST];
641 uint16_t peer_port;
642
643 if (net.peer_addr(ip, NI_MAXHOST, peer_port))
644 {
645 my_error(ER_BAD_HOST_ERROR, MYF(0), ip);
646 return false;
647 }
648
649 user_identifier->setAddress(ip);
650 }
651 net.keepalive(true);
652
653 char* end;
654 uint32_t pkt_len= 0;
655 uint32_t server_capabilites;
656 {
657 /* buff[] needs to big enough to hold the server_version variable */
658 char buff[SERVER_VERSION_LENGTH + SCRAMBLE_LENGTH + 64];
659
660 server_capabilites= CLIENT_BASIC_FLAGS | CLIENT_PROTOCOL_MYSQL41;
661
662#ifdef HAVE_COMPRESS
663 server_capabilites|= CLIENT_COMPRESS;
664#endif /* HAVE_COMPRESS */
665
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);
670 *end= 0;
671 end++;
672
673 int4store((unsigned char*) end, session->variables.pseudo_thread_id);
674 end+= 4;
675
676 /* We don't use scramble anymore. */
677 memcpy(end, scramble, SCRAMBLE_LENGTH_323);
678 end+= SCRAMBLE_LENGTH_323;
679 *end++= 0; /* an empty byte for some reason */
680
681 int2store(end, server_capabilites);
682 /* write server characteristics: up to 16 bytes allowed */
683 end[2]=(char) default_charset_info->number;
684 int2store(end+3, session->server_status);
685 memset(end+5, 0, 13);
686 end+= 18;
687
688 /* Write scramble tail. */
689 memcpy(end, scramble + SCRAMBLE_LENGTH_323, SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
690 end+= (SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
691 *end++= 0; /* an empty byte for some reason */
692
693 /* At this point we write connection message and read reply */
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)
697 {
698 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
699 return false;
700 }
701 }
702 packet.alloc(buffer_length.get());
703
704 client_capabilities= uint2korr(net.read_pos);
705 if (not (client_capabilities & CLIENT_PROTOCOL_MYSQL41))
706 {
707 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
708 return false;
709 }
710
711 client_capabilities|= ((uint32_t) uint2korr(net.read_pos + 2)) << 16;
712 // session->max_client_packet_length= uint4korr(net.read_pos + 4);
713 end= (char*) net.read_pos + 32;
714
715 /*
716 Disable those bits which are not supported by the server.
717 This is a precautionary measure, if the client lies. See Bug#27944.
718 */
719 client_capabilities&= server_capabilites;
720
721 if (end >= (char*) net.read_pos + pkt_len + 2)
722 {
723 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
724 return false;
725 }
726
727 char *user= end;
728 char *passwd= strchr(user, '\0')+1;
729 uint32_t user_len= passwd - user - 1;
730 char *l_db= passwd;
731
732 /*
733 Only support new password format.
734
735 Cast *passwd to an unsigned char, so that it doesn't extend the sign for
736 *passwd > 127 and become 2**32-127+ after casting to uint.
737 */
738 uint32_t passwd_len;
739 if (client_capabilities & CLIENT_SECURE_CONNECTION &&
740 passwd < (char *) net.read_pos + pkt_len)
741 {
742 passwd_len= (unsigned char)(*passwd++);
743 if (passwd_len > 0 and client_capabilities & CLIENT_CAPABILITIES_PLUGIN_AUTH)
744 {
745 user_identifier->setPasswordType(identifier::User::PLAIN_TEXT);
746 }
747 else if (passwd_len > 0)
748 {
749 user_identifier->setPasswordType(identifier::User::MYSQL_HASH);
750 user_identifier->setPasswordContext(scramble, SCRAMBLE_LENGTH);
751 }
752 }
753 else
754 {
755 passwd_len= 0;
756 }
757
758 if (client_capabilities & CLIENT_CONNECT_WITH_DB &&
759 passwd < (char *) net.read_pos + pkt_len)
760 {
761 l_db= l_db + passwd_len + 1;
762 }
763 else
764 {
765 l_db= NULL;
766 }
767
768 /* strlen() can't be easily deleted without changing client */
769 uint32_t db_len= l_db ? strlen(l_db) : 0;
770
771 if (passwd + passwd_len + db_len > (char *) net.read_pos + pkt_len)
772 {
773 my_error(ER_HANDSHAKE_ERROR, MYF(0), user_identifier->address().c_str());
774 return false;
775 }
776
777 /* If username starts and ends in "'", chop them off */
778 if (user_len > 1 && user[0] == '\'' && user[user_len - 1] == '\'')
779 {
780 user[user_len-1]= 0;
781 user++;
782 user_len-= 2;
783 }
784
785 if (client_capabilities & CLIENT_INTERACTIVE)
786 {
787 _is_interactive= true;
788 }
789
790 if (client_capabilities & CLIENT_CAPABILITIES_PLUGIN_AUTH)
791 {
792 passwd_len= strlen(passwd);
793 }
794
795 user_identifier->setUser(user);
796 session->setUser(user_identifier);
797
798 return session->checkUser(string(passwd, passwd_len), string(l_db ? l_db : ""));
799
800}
801
802void ClientMySQLProtocol::netStoreData(const void* from, size_t length)
803{
804 size_t packet_length= packet.length();
805 /*
806 The +9 comes from that strings of length longer than 16M require
807 9 bytes to be stored (see storeLength).
808 */
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()));
814}
815
820
821void ClientMySQLProtocol::writeEOFPacket(uint32_t server_status, uint32_t total_warn_count)
822{
823 unsigned char buff[5];
824 /*
825 Don't send warn count during SP execution, as the warn_list
826 is cleared between substatements, and mysqltest gets confused
827 */
828 uint32_t tmp= min(total_warn_count, (uint32_t)65535);
829 buff[0]= DRIZZLE_PROTOCOL_NO_MORE_DATA;
830 int2store(buff+1, tmp);
831 /*
832 The following test should never be true, but it's better to do it
833 because if 'is_fatal_error' is set the server is not going to execute
834 other queries (see the if test in dispatch_command / COM_QUERY)
835 */
836 if (session->is_fatal_error)
837 server_status&= ~SERVER_MORE_RESULTS_EXISTS;
838 int2store(buff + 3, server_status);
839 net.write(buff, 5);
840}
841
842/*
843 Store an integer with simple packing into a output package
844
845 buffer Store the packed integer here
846 length integers to store
847
848 This is mostly used to store lengths of strings. We have to cast
849 the result for the LL() becasue of a bug in Forte CC compiler.
850
851 RETURN
852 Position in 'buffer' after the packed length
853*/
854
855unsigned char *ClientMySQLProtocol::storeLength(unsigned char *buffer, uint64_t length)
856{
857 if (length < 251)
858 {
859 *buffer= (unsigned char) length;
860 return buffer+1;
861 }
862 /* 251 is reserved for NULL */
863 if (length < 65536)
864 {
865 *buffer++= 252;
866 int2store(buffer, (uint32_t) length);
867 return buffer+2;
868 }
869 if (length < 16777216)
870 {
871 *buffer++=253;
872 int3store(buffer, (uint32_t) length);
873 return buffer+3;
874 }
875 *buffer++=254;
876 int8store(buffer, length);
877 return buffer+8;
878}
879
880void ClientMySQLProtocol::makeScramble(char *scramble)
881{
882 /* This is the MySQL algorithm with minimal changes. */
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);
886
887 void *pointer= this;
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;
892
893 for (char *end= scramble + SCRAMBLE_LENGTH; scramble != end; scramble++)
894 {
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);
898 }
899}
900
901static int init(drizzled::module::Context &context)
902{
903 /* Initialize random seeds for the MySQL algorithm with minimal changes. */
904 time_t seed_time= time(NULL);
905 random_seed1= seed_time % random_max;
906 random_seed2= (seed_time / 2) % random_max;
907
908 const module::option_map &vm= context.getOptions();
909
910 context.add(new plugin::Create_function<MySQLPassword>(MySQLPasswordName));
911
912 ListenMySQLProtocol* listen_obj= new ListenMySQLProtocol("mysql_protocol", vm["bind-address"].as<std::string>());
913 listen_obj->addCountersToTable();
914 context.add(listen_obj);
915 context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
916 context.registerVariable(new sys_var_constrained_value<uint32_t>("connect_timeout", connect_timeout));
917 context.registerVariable(new sys_var_constrained_value<uint32_t>("read_timeout", read_timeout));
918 context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
919 context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
920 context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
921 context.registerVariable(new sys_var_const_string_val("bind_address", vm["bind-address"].as<std::string>()));
922 context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters.max_connections));
923
924 return 0;
925}
926
927static void init_options(drizzled::module::option_context &context)
928{
929 context("port",
930 po::value<port_constraint>(&port)->default_value(3306),
931 _("Port number to use for connection or 0 for default to with MySQL "
932 "protocol."));
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),
938 _("Read Timeout."));
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),
944 _("Retry Count."));
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."));
954}
955
956} /* namespace drizzle_plugin */
957
958DRIZZLE_DECLARE_PLUGIN
959{
960 DRIZZLE_VERSION_ID,
961 "mysql_protocol",
962 "0.1",
963 "Eric Day",
964 N_("MySQL network protocol"),
965 PLUGIN_LICENSE_GPL,
966 drizzle_plugin::init,
967 NULL,
968 drizzle_plugin::init_options
969}
970DRIZZLE_DECLARE_PLUGIN_END;
void writeEOFPacket(uint32_t server_status, uint32_t total_warn_count)
virtual void sendFields(drizzled::List< drizzled::Item > &)
virtual void sendError(const drizzled::error_t sql_errno, const char *err)
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.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.