59
60
#define COMP_HEADER_SIZE 3 /* compression header extra size */
61
62
#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);
64
static bool net_write_buff(NET*, const void*, uint32_t len);
65
65
static int drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len);
67
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);
104
if (drizzleclient_net_init(net, vio_tmp, buffer_length))
106
/* Only delete the temporary vio if we didn't already attach it to the
109
if (vio_tmp && (net->vio != vio_tmp))
115
(void) shutdown(sock, SHUT_RDWR);
123
void drizzleclient_net_end(NET *net)
125
if (net->buff != NULL)
131
void drizzleclient_net_close(NET *net)
133
drizzled::safe_delete(net->vio);
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());
69
void NET::init(int sock, uint32_t buffer_length)
72
max_packet= (uint32_t) buffer_length;
73
max_packet_size= max(buffer_length, drizzled::global_system_variables.max_allowed_packet);
75
buff= (unsigned char*) malloc((size_t) max_packet + NET_HEADER_SIZE + COMP_HEADER_SIZE);
76
buff_end= buff + max_packet;
78
pkt_nr= compress_pkt_nr= 0;
79
write_pos= read_pos= buff;
81
where_b= remain_in_buf= 0;
94
drizzled::safe_delete(vio);
97
bool NET::peer_addr(char *buf, size_t buflen, uint16_t& port)
99
return vio->peer_addr(buf, buflen, port);
102
void NET::keepalive(bool flag)
104
vio->keepalive(flag);
107
int NET::get_sd() const
109
return vio->get_fd();
156
112
/** Realloc the packet buffer. */
158
114
static bool drizzleclient_net_realloc(NET *net, size_t length)
163
116
if (length >= net->max_packet_size)
165
118
/* @todo: 1 and 2 codes are identical. */
167
120
net->last_errno= ER_NET_PACKET_TOO_LARGE;
168
121
my_error(ER_NET_PACKET_TOO_LARGE, MYF(0));
171
pkt_length = (length+IO_SIZE-1) & ~(IO_SIZE-1);
124
size_t pkt_length = (length + IO_SIZE - 1) & ~(IO_SIZE - 1);
173
126
We must allocate some extra bytes for the end 0 and to be able to
174
127
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;
129
unsigned char* buff= (unsigned char*)realloc((char*) net->buff, pkt_length + NET_HEADER_SIZE + COMP_HEADER_SIZE);
130
net->buff=net->write_pos= buff;
131
net->buff_end= buff + (net->max_packet= (uint32_t) pkt_length);
138
if (buff != write_pos)
140
error= drizzleclient_net_real_write(this, buff, write_pos - buff) ? true : false;
267
143
/* Sync packet number if using compression */
269
net->pkt_nr=net->compress_pkt_nr;
145
pkt_nr= compress_pkt_nr;
469
344
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
346
/* Backup of the original SO_RCVTIMEO timeout */
348
if (net->error_ == 2)
478
349
return(-1); /* socket can't be used */
480
net->reading_or_writing=2;
481
351
if (net->compress)
485
353
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;
354
unsigned char* b= (unsigned char*) malloc(len + NET_HEADER_SIZE + COMP_HEADER_SIZE);
495
355
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;
357
size_t complen= len * 120 / 100 + 12;
358
unsigned char* compbuf= new unsigned char[complen];
359
uLongf tmp_complen= complen;
360
int res= compress((Bytef*) compbuf, &tmp_complen,
361
(Bytef*) (b+header_length),
363
complen= tmp_complen;
367
if (res != Z_OK || complen >= len)
371
size_t tmplen= complen;
522
375
int3store(&b[NET_HEADER_SIZE],complen);
523
376
int3store(b,len);
873
724
net->read_pos[len]=0; /* Safeguard for drizzleclient_use_result */
879
void drizzleclient_net_set_read_timeout(NET *net, uint32_t timeout)
881
net->read_timeout= timeout;
884
net->vio->timeout(0, timeout);
890
void drizzleclient_net_set_write_timeout(NET *net, uint32_t timeout)
892
net->write_timeout= timeout;
895
net->vio->timeout(1, timeout);
900
Clear possible error state of struct NET
902
@param net clear the state of the argument
905
void drizzleclient_drizzleclient_net_clear_error(NET *net)
908
net->last_error[0]= '\0';
909
strcpy(net->sqlstate, not_error_sqlstate);
729
void NET::set_read_timeout(uint32_t timeout)
731
read_timeout_= timeout;
734
vio->timeout(0, timeout);
739
void NET::set_write_timeout(uint32_t timeout)
741
write_timeout_= timeout;
744
vio->timeout(1, timeout);
749
bool NET::write(const void* data, size_t size)
751
return drizzleclient_net_write(this, data, size);
754
bool NET::write_command(unsigned char command, data_ref header, data_ref body)
756
return drizzleclient_net_write_command(this, command, header.data(), header.size(), body.data(), body.size());
761
return drizzleclient_net_read(this);
912
764
} /* namespace drizzle_plugin */