1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <drizzled/current_session.h>
24
#include <drizzled/error.h>
25
#include <drizzled/session.h>
33
#include <sys/socket.h>
42
namespace drizzle_plugin
46
using namespace drizzled;
49
The following handles the differences when this is linked between the
50
client and the server.
52
This gives an error if a too big packet is found
53
The server can change this with the -O switch, but because the client
54
can't normally do this the client should have a bigger max_allowed_packet.
57
/* Constants when using compression */
58
#define NET_HEADER_SIZE 4 /* standard header size */
59
#define COMP_HEADER_SIZE 3 /* compression header extra size */
61
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
62
const char *not_error_sqlstate= "00000";
64
static bool net_write_buff(NET *net, const unsigned char *packet, uint32_t len);
65
static int drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len);
67
/** Init with packet info. */
69
bool drizzleclient_net_init(NET *net, Vio* vio, uint32_t buffer_length)
72
net->max_packet= (uint32_t) buffer_length;
73
net->max_packet_size= max(buffer_length, drizzled::global_system_variables.max_allowed_packet);
75
if (!(net->buff=(unsigned char*) malloc((size_t) net->max_packet+
76
NET_HEADER_SIZE + COMP_HEADER_SIZE)))
78
net->buff_end=net->buff+net->max_packet;
79
net->error=0; net->return_status=0;
80
net->pkt_nr=net->compress_pkt_nr=0;
81
net->write_pos=net->read_pos = net->buff;
83
net->compress=0; net->reading_or_writing=0;
84
net->where_b = net->remain_in_buf=0;
88
if (vio != 0) /* If real connection */
90
net->fd = vio->get_fd(); /* For perl DBI/DBD */
96
bool drizzleclient_net_init_sock(NET * net, int sock, uint32_t buffer_length)
98
Vio *vio_tmp= new Vio(sock);
102
if (drizzleclient_net_init(net, vio_tmp, buffer_length))
104
/* Only delete the temporary vio if we didn't already attach it to the
107
if (vio_tmp && (net->vio != vio_tmp))
111
(void) shutdown(sock, SHUT_RDWR);
119
void drizzleclient_net_end(NET *net)
121
if (net->buff != NULL)
127
void drizzleclient_net_close(NET *net)
129
if (net->vio != NULL)
136
bool drizzleclient_net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
138
return net->vio->peer_addr(buf, port, buflen);
141
void drizzleclient_net_keepalive(NET *net, bool flag)
143
net->vio->keepalive(flag);
146
int drizzleclient_net_get_sd(NET *net)
148
return net->vio->get_fd();
151
bool drizzleclient_net_more_data(NET *net)
153
return (net->vio == 0 || net->vio->get_read_pos() < net->vio->get_read_end());
156
/** Realloc the packet buffer. */
158
static bool drizzleclient_net_realloc(NET *net, size_t length)
163
if (length >= net->max_packet_size)
165
/* @todo: 1 and 2 codes are identical. */
167
net->last_errno= ER_NET_PACKET_TOO_LARGE;
168
my_error(ER_NET_PACKET_TOO_LARGE, MYF(0));
171
pkt_length = (length+IO_SIZE-1) & ~(IO_SIZE-1);
173
We must allocate some extra bytes for the end 0 and to be able to
174
read big compressed blocks
176
if (!(buff= (unsigned char*) realloc((char*) net->buff, pkt_length +
177
NET_HEADER_SIZE + COMP_HEADER_SIZE)))
179
/* @todo: 1 and 2 codes are identical. */
181
net->last_errno= CR_OUT_OF_MEMORY;
182
/* In the server the error is reported by MY_WME flag. */
185
net->buff=net->write_pos=buff;
186
net->buff_end=buff+(net->max_packet= (uint32_t) pkt_length);
192
Check if there is any data to be read from the socket.
194
@param sd socket descriptor
199
1 Data or EOF to read
201
-1 Don't know if data is ready or not
204
static bool net_data_is_ready(int sd)
210
ufds.events= POLLIN | POLLPRI;
211
if (!(res= poll(&ufds, 1, 0)))
213
if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)))
219
Remove unwanted characters from connection
220
and check if disconnected.
222
Read from socket until there is nothing more to read. Discard
225
If there is anything when to read 'drizzleclient_net_clear' is called this
226
normally indicates an error in the protocol.
228
When connection is properly closed (for TCP it means with
229
a FIN packet), then select() considers a socket "ready to read",
230
in the sense that there's EOF to read, but read() returns 0.
232
@param net NET handler
233
@param clear_buffer if <> 0, then clear all data from comm buff
236
void drizzleclient_net_clear(NET *net, bool clear_buffer)
240
while (net_data_is_ready(net->vio->get_fd()) > 0)
242
/* The socket is ready */
243
if (net->vio->read(net->buff, (size_t) net->max_packet) <= 0)
250
net->pkt_nr=net->compress_pkt_nr=0; /* Ready for new command */
251
net->write_pos=net->buff;
256
/** Flush write_buffer if not empty. */
258
bool drizzleclient_net_flush(NET *net)
261
if (net->buff != net->write_pos)
263
error=drizzleclient_net_real_write(net, net->buff,
264
(size_t) (net->write_pos - net->buff)) ? 1 : 0;
265
net->write_pos=net->buff;
267
/* Sync packet number if using compression */
269
net->pkt_nr=net->compress_pkt_nr;
274
/*****************************************************************************
275
** Write something to server/client buffer
276
*****************************************************************************/
279
Write a logical packet with packet header.
281
Format: Packet length (3 bytes), packet number(1 byte)
282
When compression is used a 3 byte compression length is added
285
If compression is used the original package is modified!
289
drizzleclient_net_write(NET *net,const unsigned char *packet,size_t len)
291
unsigned char buff[NET_HEADER_SIZE];
292
if (unlikely(!net->vio)) /* nowhere to write */
295
Big packets are handled by splitting them in packets of MAX_PACKET_LENGTH
296
length. The last packet is always a packet that is < MAX_PACKET_LENGTH.
297
(The last packet may even have a length of 0)
299
while (len >= MAX_PACKET_LENGTH)
301
const uint32_t z_size = MAX_PACKET_LENGTH;
302
int3store(buff, z_size);
303
buff[3]= (unsigned char) net->pkt_nr++;
304
if (net_write_buff(net, buff, NET_HEADER_SIZE) ||
305
net_write_buff(net, packet, z_size))
310
/* Write last packet */
312
buff[3]= (unsigned char) net->pkt_nr++;
313
if (net_write_buff(net, buff, NET_HEADER_SIZE))
315
return net_write_buff(net,packet,len) ? 1 : 0;
319
Send a command to the server.
321
The reason for having both header and packet is so that libdrizzle
322
can easy add a header to a special command (like prepared statements)
323
without having to re-alloc the string.
325
As the command is part of the first data packet, we have to do some data
326
juggling to put the command in there, without having to create a new
329
This function will split big packets into sub-packets if needed.
330
(Each sub packet can only be 2^24 bytes)
332
@param net NET handler
333
@param command Command in MySQL server (enum enum_server_command)
334
@param header Header to write after command
335
@param head_len Length of header
336
@param packet Query or parameter to query
337
@param len Length of packet
346
drizzleclient_net_write_command(NET *net,unsigned char command,
347
const unsigned char *header, size_t head_len,
348
const unsigned char *packet, size_t len)
350
uint32_t length=len+1+head_len; /* 1 extra byte for command */
351
unsigned char buff[NET_HEADER_SIZE+1];
352
uint32_t header_size=NET_HEADER_SIZE+1;
354
buff[4]=command; /* For first packet */
356
if (length >= MAX_PACKET_LENGTH)
358
/* Take into account that we have the command in the first header */
359
len= MAX_PACKET_LENGTH - 1 - head_len;
362
int3store(buff, MAX_PACKET_LENGTH);
363
buff[3]= (unsigned char) net->pkt_nr++;
364
if (net_write_buff(net, buff, header_size) ||
365
net_write_buff(net, header, head_len) ||
366
net_write_buff(net, packet, len))
369
length-= MAX_PACKET_LENGTH;
370
len= MAX_PACKET_LENGTH;
372
header_size= NET_HEADER_SIZE;
373
} while (length >= MAX_PACKET_LENGTH);
374
len=length; /* Data left to be written */
376
int3store(buff,length);
377
buff[3]= (unsigned char) net->pkt_nr++;
378
return((net_write_buff(net, buff, header_size) ||
379
(head_len && net_write_buff(net, header, head_len)) ||
380
net_write_buff(net, packet, len) || drizzleclient_net_flush(net)) ? 1 : 0 );
384
Caching the data in a local buffer before sending it.
386
Fill up net->buffer and send it to the client when full.
388
If the rest of the to-be-sent-packet is bigger than buffer,
389
send it in one big block (to avoid copying to internal buffer).
390
If not, copy the rest of the data to the buffer and return without
393
@param net Network handler
394
@param packet Packet to send
395
@param len Length of packet
398
The cached buffer can be sent as it is with 'drizzleclient_net_flush()'.
399
In this code we have to be careful to not send a packet longer than
400
MAX_PACKET_LENGTH to drizzleclient_net_real_write() if we are using the compressed
401
protocol as we store the length of the compressed packet in 3 bytes.
410
net_write_buff(NET *net, const unsigned char *packet, uint32_t len)
412
uint32_t left_length;
413
if (net->compress && net->max_packet > MAX_PACKET_LENGTH)
414
left_length= MAX_PACKET_LENGTH - (net->write_pos - net->buff);
416
left_length= (uint32_t) (net->buff_end - net->write_pos);
418
if (len > left_length)
420
if (net->write_pos != net->buff)
422
/* Fill up already used packet and write it */
423
memcpy(net->write_pos,packet,left_length);
424
if (drizzleclient_net_real_write(net, net->buff,
425
(size_t) (net->write_pos - net->buff) + left_length))
427
net->write_pos= net->buff;
428
packet+= left_length;
434
We can't have bigger packets than 16M with compression
435
Because the uncompressed length is stored in 3 bytes
437
left_length= MAX_PACKET_LENGTH;
438
while (len > left_length)
440
if (drizzleclient_net_real_write(net, packet, left_length))
442
packet+= left_length;
446
if (len > net->max_packet)
447
return drizzleclient_net_real_write(net, packet, len) ? 1 : 0;
448
/* Send out rest of the blocks as full sized blocks */
450
memcpy(net->write_pos,packet,len);
451
net->write_pos+= len;
457
Read and write one packet using timeouts.
458
If needed, the packet is compressed before sending.
461
- TODO is it needed to set this variable if we have no socket
465
TODO: rewrite this in a manner to do non-block writes. If a write can not be made, and we are
466
in the server, yield to another process and come back later.
469
drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len)
472
const unsigned char *pos, *end;
473
uint32_t retry_count= 0;
475
/* Backup of the original SO_RCVTIMEO timeout */
478
return(-1); /* socket can't be used */
480
net->reading_or_writing=2;
485
const uint32_t header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE;
486
if (!(b= (unsigned char*) malloc(len + NET_HEADER_SIZE +
490
net->last_errno= CR_OUT_OF_MEMORY;
491
/* In the server, the error is reported by MY_WME flag. */
492
net->reading_or_writing= 0;
495
memcpy(b+header_length,packet,len);
497
complen= len * 120 / 100 + 12;
498
unsigned char * compbuf= (unsigned char *) malloc(complen);
501
uLongf tmp_complen= complen;
502
int res= compress((Bytef*) compbuf, &tmp_complen,
503
(Bytef*) (b+header_length),
505
complen= tmp_complen;
509
if ((res != Z_OK) || (complen >= len))
513
size_t tmplen= complen;
522
int3store(&b[NET_HEADER_SIZE],complen);
524
b[3]=(unsigned char) (net->compress_pkt_nr++);
531
/* Loop until we have read everything */
535
// TODO - see bug comment below - will we crash now?
536
if ((long) (length= net->vio->write( pos, (size_t) (end-pos))) <= 0)
539
* We could end up here with net->vio == NULL
541
* If that is the case, we exit the while loop
543
if (net->vio == NULL)
546
const bool interrupted= net->vio->should_retry();
548
If we read 0, or we were interrupted this means that
549
we need to switch to blocking mode and wait until the timeout
550
on the socket kicks in.
552
if ((interrupted || length == 0))
556
while (net->vio->blocking(true, &old_mode) < 0)
558
if (net->vio->should_retry() && retry_count++ < net->retry_count)
560
net->error= 2; /* Close socket */
561
net->last_errno= ER_NET_PACKET_TOO_LARGE;
562
my_error(ER_NET_PACKET_TOO_LARGE, MYF(0));
570
if (retry_count++ < net->retry_count)
574
if (net->vio->get_errno() == EINTR)
578
net->error= 2; /* Close socket */
579
net->last_errno= (interrupted ? CR_NET_WRITE_INTERRUPTED :
580
CR_NET_ERROR_ON_WRITE);
585
/* If this is an error we may not have a current_session any more */
587
current_session->status_var.bytes_sent+= length;
590
if ((net->compress) && (packet != NULL))
591
free((char*) packet);
592
net->reading_or_writing=0;
594
return(((int) (pos != end)));
599
Reads one packet to net->buff + net->where_b.
600
Long packets are handled by drizzleclient_net_read().
601
This function reallocates the net->buff buffer if necessary.
604
Returns length of packet.
608
my_real_read(NET *net, size_t *complen)
612
uint32_t i,retry_count=0;
613
size_t len=packet_error;
614
uint32_t remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
619
net->reading_or_writing= 1;
620
/* Read timeout is set in drizzleclient_net_set_read_timeout */
622
pos = net->buff + net->where_b; /* net->packet -4 */
624
for (i= 0; i < 2 ; i++)
628
/* First read is done with non blocking mode */
629
if ((long) (length= net->vio->read(pos, remain)) <= 0L)
631
if (net->vio == NULL)
634
const bool interrupted = net->vio->should_retry();
637
{ /* Probably in MIT threads */
638
if (retry_count++ < net->retry_count)
641
if (net->vio->get_errno() == EINTR)
646
net->error= 2; /* Close socket */
647
net->last_errno= (net->vio->was_interrupted() ?
648
CR_NET_READ_INTERRUPTED :
652
remain -= (uint32_t) length;
654
current_session->status_var.bytes_received+= length;
657
{ /* First parts is packet length */
660
if (net->buff[net->where_b + 3] != (unsigned char) net->pkt_nr)
663
/* Not a NET error on the client. XXX: why? */
664
my_error(ER_NET_PACKETS_OUT_OF_ORDER, MYF(0));
667
net->compress_pkt_nr= ++net->pkt_nr;
671
If the packet is compressed then complen > 0 and contains the
672
number of bytes in the uncompressed packet
674
*complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE]));
677
len=uint3korr(net->buff+net->where_b);
678
if (!len) /* End of big multi-packet */
680
helping = max(len,*complen) + net->where_b;
681
/* The necessary size of net->buff */
682
if (helping >= net->max_packet)
684
if (drizzleclient_net_realloc(net,helping))
686
/* Clear the buffer so libdrizzle doesn't keep retrying */
689
length= read(net->vio->get_fd(), net->buff, min((size_t)net->max_packet, len));
690
assert((long)length > 0L);
694
len= packet_error; /* Return error and close connection */
698
pos=net->buff + net->where_b;
699
remain = (uint32_t) len;
704
net->reading_or_writing= 0;
711
Read a packet from the client/server and return it without the internal
714
If the packet is the first packet of a multi-packet packet
715
(which is indicated by the length of the packet = 0xffffff) then
716
all sub packets are read and concatenated.
718
If the packet was compressed, its uncompressed and the length of the
719
uncompressed packet is returned.
722
The function returns the length of the found packet or packet_error.
723
net->read_pos points to the read data.
727
drizzleclient_net_read(NET *net)
733
len = my_real_read(net,&complen);
734
if (len == MAX_PACKET_LENGTH)
736
/* First packet of a multi-packet. Concatenate the packets */
737
uint32_t save_pos = net->where_b;
738
size_t total_length= 0;
743
len = my_real_read(net,&complen);
744
} while (len == MAX_PACKET_LENGTH);
745
if (len != packet_error)
747
net->where_b = save_pos;
749
net->read_pos = net->buff + net->where_b;
750
if (len != packet_error)
751
net->read_pos[len]=0; /* Safeguard for drizzleclient_use_result */
756
/* We are using the compressed protocol */
759
uint32_t start_of_packet;
760
uint32_t first_packet_offset;
761
uint32_t read_length, multi_byte_packet=0;
763
if (net->remain_in_buf)
765
buf_length= net->buf_length; /* Data left in old packet */
766
first_packet_offset= start_of_packet= (net->buf_length -
768
/* Restore the character that was overwritten by the end 0 */
769
net->buff[start_of_packet]= net->save_char;
773
/* reuse buffer, as there is nothing in it that we need */
774
buf_length= start_of_packet= first_packet_offset= 0;
780
if (buf_length - start_of_packet >= NET_HEADER_SIZE)
782
read_length = uint3korr(net->buff+start_of_packet);
785
/* End of multi-byte packet */
786
start_of_packet += NET_HEADER_SIZE;
789
if (read_length + NET_HEADER_SIZE <= buf_length - start_of_packet)
791
if (multi_byte_packet)
793
/* Remove packet header for second packet */
794
memmove(net->buff + first_packet_offset + start_of_packet,
795
net->buff + first_packet_offset + start_of_packet +
797
buf_length - start_of_packet);
798
start_of_packet += read_length;
799
buf_length -= NET_HEADER_SIZE;
802
start_of_packet+= read_length + NET_HEADER_SIZE;
804
if (read_length != MAX_PACKET_LENGTH) /* last package */
806
multi_byte_packet= 0; /* No last zero len packet */
809
multi_byte_packet= NET_HEADER_SIZE;
810
/* Move data down to read next data packet after current one */
811
if (first_packet_offset)
813
memmove(net->buff,net->buff+first_packet_offset,
814
buf_length-first_packet_offset);
815
buf_length-=first_packet_offset;
816
start_of_packet -= first_packet_offset;
817
first_packet_offset=0;
822
/* Move data down to read next data packet after current one */
823
if (first_packet_offset)
825
memmove(net->buff,net->buff+first_packet_offset,
826
buf_length-first_packet_offset);
827
buf_length-=first_packet_offset;
828
start_of_packet -= first_packet_offset;
829
first_packet_offset=0;
832
net->where_b=buf_length;
833
if ((packet_len = my_real_read(net,&complen)) == packet_error)
838
unsigned char * compbuf= (unsigned char *) malloc(complen);
841
uLongf tmp_complen= complen;
842
int error= uncompress((Bytef*) compbuf, &tmp_complen,
843
(Bytef*) (net->buff + net->where_b),
845
complen= tmp_complen;
849
net->error= 2; /* caller will close socket */
850
net->last_errno= CR_NET_UNCOMPRESS_ERROR;
854
memcpy((net->buff + net->where_b), compbuf, complen);
863
buf_length+= complen;
865
net->read_pos= net->buff+ first_packet_offset + NET_HEADER_SIZE;
866
net->buf_length= buf_length;
867
net->remain_in_buf= (uint32_t) (buf_length - start_of_packet);
868
len = ((uint32_t) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
870
net->save_char= net->read_pos[len]; /* Must be saved */
871
net->read_pos[len]=0; /* Safeguard for drizzleclient_use_result */
877
void drizzleclient_net_set_read_timeout(NET *net, uint32_t timeout)
879
net->read_timeout= timeout;
882
net->vio->timeout(0, timeout);
888
void drizzleclient_net_set_write_timeout(NET *net, uint32_t timeout)
890
net->write_timeout= timeout;
893
net->vio->timeout(1, timeout);
898
Clear possible error state of struct NET
900
@param net clear the state of the argument
903
void drizzleclient_drizzleclient_net_clear_error(NET *net)
906
net->last_error[0]= '\0';
907
strcpy(net->sqlstate, not_error_sqlstate);
910
} /* namespace drizzle_plugin */