71
65
in_port_t ListenDrizzleProtocol::getPort(void) const
77
port= DRIZZLE_TCP_PORT;
79
if ((env = getenv("DRIZZLE_TCP_PORT")))
80
port= (uint32_t) atoi(env);
85
67
return (in_port_t) port;
88
plugin::Client *ListenDrizzleProtocol::getClient(int fd)
91
new_fd= acceptTcp(fd);
95
return new (nothrow) ClientDrizzleProtocol(new_fd, using_mysql41_protocol);
98
drizzled::atomic<uint64_t> ClientDrizzleProtocol::connectionCount;
99
drizzled::atomic<uint64_t> ClientDrizzleProtocol::failedConnections;
100
drizzled::atomic<uint64_t> ClientDrizzleProtocol::connected;
102
ClientDrizzleProtocol::ClientDrizzleProtocol(int fd, bool using_mysql41_protocol_arg):
103
using_mysql41_protocol(using_mysql41_protocol_arg)
110
if (drizzleclient_net_init_sock(&net, fd, 0, buffer_length))
113
drizzleclient_net_set_read_timeout(&net, read_timeout);
114
drizzleclient_net_set_write_timeout(&net, write_timeout);
115
net.retry_count=retry_count;
118
ClientDrizzleProtocol::~ClientDrizzleProtocol()
121
drizzleclient_vio_close(net.vio);
124
int ClientDrizzleProtocol::getFileDescriptor(void)
126
return drizzleclient_net_get_sd(&net);
129
bool ClientDrizzleProtocol::isConnected()
134
bool ClientDrizzleProtocol::isReading(void)
136
return net.reading_or_writing == 1;
139
bool ClientDrizzleProtocol::isWriting(void)
141
return net.reading_or_writing == 2;
144
bool ClientDrizzleProtocol::flush()
148
bool ret= drizzleclient_net_write(&net, (unsigned char*) packet.ptr(),
154
void ClientDrizzleProtocol::close(void)
158
drizzleclient_net_close(&net);
159
drizzleclient_net_end(&net);
160
connected.decrement();
164
bool ClientDrizzleProtocol::authenticate()
166
bool connection_is_valid;
168
connectionCount.increment();
169
connected.increment();
171
/* Use "connect_timeout" value during connection phase */
172
drizzleclient_net_set_read_timeout(&net, connect_timeout);
173
drizzleclient_net_set_write_timeout(&net, connect_timeout);
175
connection_is_valid= checkConnection();
177
if (connection_is_valid)
181
sendError(session->main_da.sql_errno(), session->main_da.message());
182
failedConnections.increment();
186
/* Connect completed, set read/write timeouts back to default */
187
drizzleclient_net_set_read_timeout(&net, read_timeout);
188
drizzleclient_net_set_write_timeout(&net, write_timeout);
192
bool ClientDrizzleProtocol::readCommand(char **l_packet, uint32_t *packet_length)
195
This thread will do a blocking read from the client which
196
will be interrupted when the next command is received from
197
the client, the connection is closed or "net_wait_timeout"
198
number of seconds has passed
201
/* We can do this much more efficiently with poll timeouts or watcher thread,
202
disabling for now, which means net_wait_timeout == read_timeout. */
203
drizzleclient_net_set_read_timeout(&net,
204
session->variables.net_wait_timeout);
209
*packet_length= drizzleclient_net_read(&net);
210
if (*packet_length == packet_error)
212
/* Check if we can continue without closing the connection */
214
if(net.last_errno== CR_NET_PACKET_TOO_LARGE)
215
my_error(ER_NET_PACKET_TOO_LARGE, MYF(0));
216
if (session->main_da.status() == Diagnostics_area::DA_ERROR)
217
sendError(session->main_da.sql_errno(), session->main_da.message());
222
return false; // We have to close it.
229
*l_packet= (char*) net.read_pos;
232
'packet_length' contains length of data, as it was stored in packet
233
header. In case of malformed header, drizzleclient_net_read returns zero.
234
If packet_length is not zero, drizzleclient_net_read ensures that the returned
235
number of bytes was actually read from network.
236
There is also an extra safety measure in drizzleclient_net_read:
237
it sets packet[packet_length]= 0, but only for non-zero packets.
240
if (*packet_length == 0) /* safety */
242
/* Initialize with COM_SLEEP packet */
243
(*l_packet)[0]= (unsigned char) COM_SLEEP;
246
else if (using_mysql41_protocol)
248
/* Map from MySQL commands to Drizzle commands. */
249
switch ((int)(*l_packet)[0])
253
case 2: /* INIT_DB */
257
case 8: /* SHUTDOWN */
258
(*l_packet)[0]= (unsigned char) COM_SHUTDOWN;
262
(*l_packet)[0]= (unsigned char) COM_SHUTDOWN;
267
/* Just drop connection for MySQL commands we don't support. */
268
(*l_packet)[0]= (unsigned char) COM_QUIT;
274
/* Do not rely on drizzleclient_net_read, extra safety against programming errors. */
275
(*l_packet)[*packet_length]= '\0'; /* safety */
278
/* See comment above. */
279
/* Restore read timeout value */
280
drizzleclient_net_set_read_timeout(&net,
281
session->variables.net_read_timeout);
288
Return ok to the client.
290
The ok packet has the following structure:
292
- 0 : Marker (1 byte)
293
- affected_rows : Stored in 1-9 bytes
294
- id : Stored in 1-9 bytes
295
- server_status : Copy of session->server_status; Can be used by client
296
to check if we are inside an transaction.
298
- warning_count : Stored in 2 bytes; New in 4.1 client
299
- message : Stored as packed length (1-9 bytes) + message.
300
Is not stored if no message.
302
@param session Thread handler
303
@param affected_rows Number of rows changed by statement
304
@param id Auto_increment id for first row (if used)
305
@param message Message to send to the client (Used by mysql_status)
308
void ClientDrizzleProtocol::sendOK()
310
unsigned char buff[DRIZZLE_ERRMSG_SIZE+10],*pos;
311
const char *message= NULL;
314
if (!net.vio) // hack for re-parsing queries
319
buff[0]=0; // No fields
320
if (session->main_da.status() == Diagnostics_area::DA_OK)
322
if (client_capabilities & CLIENT_FOUND_ROWS && session->main_da.found_rows())
323
pos=drizzleclient_net_store_length(buff+1,session->main_da.found_rows());
325
pos=drizzleclient_net_store_length(buff+1,session->main_da.affected_rows());
326
pos=drizzleclient_net_store_length(pos, session->main_da.last_insert_id());
327
int2store(pos, session->main_da.server_status());
329
tmp= min(session->main_da.total_warn_count(), (uint32_t)65535);
330
message= session->main_da.message();
334
pos=drizzleclient_net_store_length(buff+1,0);
335
pos=drizzleclient_net_store_length(pos, 0);
336
int2store(pos, session->server_status);
338
tmp= min(session->total_warn_count, (uint32_t)65535);
341
/* We can only return up to 65535 warnings in two bytes */
345
session->main_da.can_overwrite_status= true;
347
if (message && message[0])
349
size_t length= strlen(message);
350
pos=drizzleclient_net_store_length(pos,length);
351
memcpy(pos,(unsigned char*) message,length);
354
drizzleclient_net_write(&net, buff, (size_t) (pos-buff));
355
drizzleclient_net_flush(&net);
357
session->main_da.can_overwrite_status= false;
361
Send eof (= end of result set) to the client.
363
The eof packet has the following structure:
365
- 254 (DRIZZLE_PROTOCOL_NO_MORE_DATA) : Marker (1 byte)
366
- warning_count : Stored in 2 bytes; New in 4.1 client
367
- status_flag : Stored in 2 bytes;
368
For flags like SERVER_MORE_RESULTS_EXISTS.
370
Note that the warning count will not be sent if 'no_flush' is set as
371
we don't want to report the warning count until all data is sent to the
375
void ClientDrizzleProtocol::sendEOF()
377
/* Set to true if no active vio, to work well in case of --init-file */
380
session->main_da.can_overwrite_status= true;
381
writeEOFPacket(session->main_da.server_status(),
382
session->main_da.total_warn_count());
383
drizzleclient_net_flush(&net);
384
session->main_da.can_overwrite_status= false;
386
packet.shrink(buffer_length);
390
void ClientDrizzleProtocol::sendError(uint32_t sql_errno, const char *err)
394
buff[]: sql_errno:2 + ('#':1 + SQLSTATE_LENGTH:5) + DRIZZLE_ERRMSG_SIZE:512
396
unsigned char buff[2+1+SQLSTATE_LENGTH+DRIZZLE_ERRMSG_SIZE], *pos;
399
assert(err && err[0]);
402
It's one case when we can push an error even though there
403
is an OK or EOF already.
405
session->main_da.can_overwrite_status= true;
407
/* Abort multi-result sets */
408
session->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
411
Send a error string to client.
413
For SIGNAL/RESIGNAL and GET DIAGNOSTICS functionality it's
414
critical that every error that can be intercepted is issued in one
415
place only, my_message_sql.
423
int2store(buff,sql_errno);
426
/* The first # is to make the client backward compatible */
428
pos= (unsigned char*) strcpy((char*) buff+3, drizzle_errno_to_sqlstate(sql_errno));
429
pos+= strlen(drizzle_errno_to_sqlstate(sql_errno));
431
char *tmp= strncpy((char*)pos, err, DRIZZLE_ERRMSG_SIZE-1);
432
tmp+= strlen((char*)pos);
434
length= (uint32_t)(tmp-(char*)buff);
437
drizzleclient_net_write_command(&net,(unsigned char) 255, (unsigned char*) "", 0, (unsigned char*) err, length);
439
session->main_da.can_overwrite_status= false;
443
Send name and type of result to client.
445
Sum fields has table name empty and field_name.
447
@param Session Thread data object
448
@param list List of items to send to client
449
@param flag Bit mask with the following functions:
450
- 1 send number of rows
451
- 2 send default values
452
- 4 don't write eof packet
457
1 Error (Note that in this case the error is not sent to the
460
bool ClientDrizzleProtocol::sendFields(List<Item> *list)
462
List_iterator_fast<Item> it(*list);
464
unsigned char buff[80];
465
String tmp((char*) buff,sizeof(buff),&my_charset_bin);
467
unsigned char *row_pos= drizzleclient_net_store_length(buff, list->elements);
468
(void) drizzleclient_net_write(&net, buff, (size_t) (row_pos-buff));
474
item->make_field(&field);
478
if (store(STRING_WITH_LEN("def")) ||
479
store(field.db_name) ||
480
store(field.table_name) ||
481
store(field.org_table_name) ||
482
store(field.col_name) ||
483
store(field.org_col_name) ||
484
packet.realloc(packet.length()+12))
487
/* Store fixed length fields */
488
pos= (char*) packet.ptr()+packet.length();
489
*pos++= 12; // Length of packed fields
491
int2store(pos, field.charsetnr);
492
int4store(pos+2, field.length);
494
if (using_mysql41_protocol)
496
/* Switch to MySQL field numbering. */
499
case DRIZZLE_TYPE_LONG:
503
case DRIZZLE_TYPE_DOUBLE:
507
case DRIZZLE_TYPE_NULL:
511
case DRIZZLE_TYPE_TIMESTAMP:
515
case DRIZZLE_TYPE_LONGLONG:
519
case DRIZZLE_TYPE_DATETIME:
523
case DRIZZLE_TYPE_DATE:
527
case DRIZZLE_TYPE_VARCHAR:
531
case DRIZZLE_TYPE_DECIMAL:
535
case DRIZZLE_TYPE_ENUM:
539
case DRIZZLE_TYPE_BLOB:
546
/* Add one to compensate for tinyint removal from enum. */
547
pos[6]= field.type + 1;
550
int2store(pos+7,field.flags);
551
pos[9]= (char) field.decimals;
552
pos[10]= 0; // For the future
553
pos[11]= 0; // For the future
556
packet.length((uint32_t) (pos - packet.ptr()));
562
Mark the end of meta-data result set, and store session->server_status,
563
to show that there is no cursor.
564
Send no warning information, as it will be sent at statement end.
566
writeEOFPacket(session->server_status, session->total_warn_count);
570
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES),
575
bool ClientDrizzleProtocol::store(Field *from)
579
char buff[MAX_FIELD_WIDTH];
580
String str(buff,sizeof(buff), &my_charset_bin);
584
return netStoreData((const unsigned char *)str.ptr(), str.length());
587
bool ClientDrizzleProtocol::store(void)
591
return packet.append(buff, sizeof(buff), PACKET_BUFFER_EXTRA_ALLOC);
594
bool ClientDrizzleProtocol::store(int32_t from)
597
return netStoreData((unsigned char*) buff,
598
(size_t) (internal::int10_to_str(from, buff, -10) - buff));
601
bool ClientDrizzleProtocol::store(uint32_t from)
604
return netStoreData((unsigned char*) buff,
605
(size_t) (internal::int10_to_str(from, buff, 10) - buff));
608
bool ClientDrizzleProtocol::store(int64_t from)
611
return netStoreData((unsigned char*) buff,
612
(size_t) (internal::int64_t10_to_str(from, buff, -10) - buff));
615
bool ClientDrizzleProtocol::store(uint64_t from)
618
return netStoreData((unsigned char*) buff,
619
(size_t) (internal::int64_t10_to_str(from, buff, 10) - buff));
622
bool ClientDrizzleProtocol::store(double from, uint32_t decimals, String *buffer)
624
buffer->set_real(from, decimals, session->charset());
625
return netStoreData((unsigned char*) buffer->ptr(), buffer->length());
628
bool ClientDrizzleProtocol::store(const char *from, size_t length)
630
return netStoreData((const unsigned char *)from, length);
633
bool ClientDrizzleProtocol::wasAborted(void)
635
return net.error && net.vio != 0;
638
bool ClientDrizzleProtocol::haveMoreData(void)
640
return drizzleclient_net_more_data(&net);
643
bool ClientDrizzleProtocol::haveError(void)
645
return net.error || net.vio == 0;
648
bool ClientDrizzleProtocol::checkConnection(void)
658
if (drizzleclient_net_peer_addr(&net, ip, &peer_port, NI_MAXHOST))
660
my_error(ER_BAD_HOST_ERROR, MYF(0), session->getSecurityContext().getIp().c_str());
664
session->getSecurityContext().setIp(ip);
666
drizzleclient_net_keepalive(&net, true);
668
uint32_t server_capabilites;
670
/* buff[] needs to big enough to hold the server_version variable */
671
char buff[SERVER_VERSION_LENGTH + SCRAMBLE_LENGTH + 64];
673
server_capabilites= CLIENT_BASIC_FLAGS;
675
if (using_mysql41_protocol)
676
server_capabilites|= CLIENT_PROTOCOL_MYSQL41;
679
server_capabilites|= CLIENT_COMPRESS;
680
#endif /* HAVE_COMPRESS */
682
end= buff + strlen(PANDORA_RELEASE_VERSION);
683
if ((end - buff) >= SERVER_VERSION_LENGTH)
684
end= buff + (SERVER_VERSION_LENGTH - 1);
685
memcpy(buff, PANDORA_RELEASE_VERSION, end - buff);
689
int4store((unsigned char*) end, session->variables.pseudo_thread_id);
692
/* We don't use scramble anymore. */
693
memset(end, 'X', SCRAMBLE_LENGTH_323);
694
end+= SCRAMBLE_LENGTH_323;
695
*end++= 0; /* an empty byte for some reason */
697
int2store(end, server_capabilites);
698
/* write server characteristics: up to 16 bytes allowed */
699
end[2]=(char) default_charset_info->number;
700
int2store(end+3, session->server_status);
701
memset(end+5, 0, 13);
704
/* Write scramble tail. */
705
memset(end, 'X', SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
706
end+= (SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
707
*end++= 0; /* an empty byte for some reason */
709
/* At this point we write connection message and read reply */
710
if (drizzleclient_net_write_command(&net
711
, (unsigned char) PROTOCOL_VERSION
712
, (unsigned char*) ""
714
, (unsigned char*) buff
715
, (size_t) (end-buff))
716
|| (pkt_len= drizzleclient_net_read(&net)) == packet_error
717
|| pkt_len < MIN_HANDSHAKE_SIZE)
719
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->getSecurityContext().getIp().c_str());
723
if (packet.alloc(buffer_length))
724
return false; /* The error is set by alloc(). */
726
client_capabilities= uint2korr(net.read_pos);
729
client_capabilities|= ((uint32_t) uint2korr(net.read_pos + 2)) << 16;
730
session->max_client_packet_length= uint4korr(net.read_pos + 4);
731
end= (char*) net.read_pos + 32;
734
Disable those bits which are not supported by the server.
735
This is a precautionary measure, if the client lies. See Bug#27944.
737
client_capabilities&= server_capabilites;
739
if (end >= (char*) net.read_pos + pkt_len + 2)
741
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->getSecurityContext().getIp().c_str());
745
net.return_status= &session->server_status;
748
char *passwd= strchr(user, '\0')+1;
749
uint32_t user_len= passwd - user - 1;
753
Old clients send null-terminated string as password; new clients send
754
the size (1 byte) + string (not null-terminated). Hence in case of empty
755
password both send '\0'.
757
This strlen() can't be easily deleted without changing client.
759
Cast *passwd to an unsigned char, so that it doesn't extend the sign for
760
*passwd > 127 and become 2**32-127+ after casting to uint.
762
uint32_t passwd_len= client_capabilities & CLIENT_SECURE_CONNECTION ?
763
(unsigned char)(*passwd++) : strlen(passwd);
764
l_db= client_capabilities & CLIENT_CONNECT_WITH_DB ? l_db + passwd_len + 1 : 0;
766
/* strlen() can't be easily deleted without changing client */
767
uint32_t db_len= l_db ? strlen(l_db) : 0;
769
if (passwd + passwd_len + db_len > (char *) net.read_pos + pkt_len)
771
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->getSecurityContext().getIp().c_str());
775
/* If username starts and ends in "'", chop them off */
776
if (user_len > 1 && user[0] == '\'' && user[user_len - 1] == '\'')
783
session->getSecurityContext().setUser(user);
785
return session->checkUser(passwd, passwd_len, l_db);
788
bool ClientDrizzleProtocol::netStoreData(const unsigned char *from, size_t length)
790
size_t packet_length= packet.length();
792
The +9 comes from that strings of length longer than 16M require
793
9 bytes to be stored (see drizzleclient_net_store_length).
795
if (packet_length+9+length > packet.alloced_length() &&
796
packet.realloc(packet_length+9+length))
798
unsigned char *to= drizzleclient_net_store_length((unsigned char*) packet.ptr()+packet_length, length);
799
memcpy(to,from,length);
800
packet.length((size_t) (to+length-(unsigned char*) packet.ptr()));
805
Format EOF packet according to the current client and
806
write it to the network output buffer.
809
void ClientDrizzleProtocol::writeEOFPacket(uint32_t server_status,
810
uint32_t total_warn_count)
812
unsigned char buff[5];
814
Don't send warn count during SP execution, as the warn_list
815
is cleared between substatements, and mysqltest gets confused
817
uint32_t tmp= min(total_warn_count, (uint32_t)65535);
818
buff[0]= DRIZZLE_PROTOCOL_NO_MORE_DATA;
819
int2store(buff+1, tmp);
821
The following test should never be true, but it's better to do it
822
because if 'is_fatal_error' is set the server is not going to execute
823
other queries (see the if test in dispatch_command / COM_QUERY)
825
if (session->is_fatal_error)
826
server_status&= ~SERVER_MORE_RESULTS_EXISTS;
827
int2store(buff + 3, server_status);
828
drizzleclient_net_write(&net, buff, 5);
831
static int init(module::Context &context)
833
drizzle_status_table_function_ptr= new DrizzleProtocolStatus;
835
context.add(drizzle_status_table_function_ptr);
70
static int init(drizzled::module::Context &context)
837
72
const module::option_map &vm= context.getOptions();
838
73
if (vm.count("port"))