~drizzle-trunk/drizzle/development

383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
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; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
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.
15
 *
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
1 by brian
clean slate
19
 */
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
20
612.2.4 by Monty Taylor
Moved some defines to config.h. Stopped including config.h directly anywhere.
21
#include <drizzled/global.h>
383.1.44 by Monty Taylor
Renamed drizzle.h to libdrizzle.h.
22
#include "libdrizzle.h"
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
23
#include "libdrizzle_priv.h"
779.3.37 by Monty Taylor
Renmaed libdrizzle in the tree to libdrizzleclient to avoid namespace clashes
24
#include "errmsg.h"
25
#include "vio.h"
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
26
#include <assert.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
1 by brian
clean slate
30
#include <signal.h>
31
#include <errno.h>
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
32
#include <sys/socket.h>
212.5.30 by Monty Taylor
Removed my_net.h. Pointless.
33
#include <sys/poll.h>
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
34
#include <zlib.h>
1 by brian
clean slate
35
36
/*
37
  The following handles the differences when this is linked between the
38
  client and the server.
39
40
  This gives an error if a too big packet is found
41
  The server can change this with the -O switch, but because the client
42
  can't normally do this the client should have a bigger max_allowed_packet.
43
*/
44
45
46
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
47
164 by Brian Aker
Commit cleanup of export types.
48
static bool net_write_buff(NET *net, const unsigned char *packet, uint32_t len);
1 by brian
clean slate
49
50
51
/** Init with packet info. */
52
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
53
bool drizzleclient_net_init(NET *net, Vio* vio)
1 by brian
clean slate
54
{
55
  net->vio = vio;
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
56
  drizzleclient_net_local_init(net);            /* Set some limits */
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
57
  if (!(net->buff=(unsigned char*) malloc((size_t) net->max_packet+
679 by Brian Aker
Style cleanup, additional assert on net write()
58
                                          NET_HEADER_SIZE + COMP_HEADER_SIZE)))
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
59
    return(1);
1 by brian
clean slate
60
  net->buff_end=net->buff+net->max_packet;
61
  net->error=0; net->return_status=0;
62
  net->pkt_nr=net->compress_pkt_nr=0;
63
  net->write_pos=net->read_pos = net->buff;
64
  net->last_error[0]=0;
65
  net->compress=0; net->reading_or_writing=0;
66
  net->where_b = net->remain_in_buf=0;
67
  net->last_errno=0;
68
  net->unused= 0;
69
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
70
  if (vio != 0)                    /* If real connection */
1 by brian
clean slate
71
  {
840.1.19 by Monty Taylor
Renamed vio.
72
    net->fd  = drizzleclient_vio_fd(vio);            /* For perl DBI/DBD */
73
    drizzleclient_vio_fastsend(vio);
1 by brian
clean slate
74
  }
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
75
  return(0);
1 by brian
clean slate
76
}
77
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
78
bool drizzleclient_net_init_sock(NET * net, int sock, int flags)
383.1.53 by Monty Taylor
Removed more direct use of vio.
79
{
80
840.1.19 by Monty Taylor
Renamed vio.
81
  Vio *drizzleclient_vio_tmp= drizzleclient_vio_new(sock, VIO_TYPE_TCPIP, flags);
82
  if (drizzleclient_vio_tmp == NULL)
383.1.53 by Monty Taylor
Removed more direct use of vio.
83
    return true;
84
  else
840.1.19 by Monty Taylor
Renamed vio.
85
    if (drizzleclient_net_init(net, drizzleclient_vio_tmp))
383.1.53 by Monty Taylor
Removed more direct use of vio.
86
    {
87
      /* Only delete the temporary vio if we didn't already attach it to the
88
       * NET object.
89
       */
840.1.19 by Monty Taylor
Renamed vio.
90
      if (drizzleclient_vio_tmp && (net->vio != drizzleclient_vio_tmp))
91
        drizzleclient_vio_delete(drizzleclient_vio_tmp);
383.1.53 by Monty Taylor
Removed more direct use of vio.
92
      else
93
      {
94
        (void) shutdown(sock, SHUT_RDWR);
95
        (void) close(sock);
96
      }
97
      return true;
98
    }
99
  return false;
100
}
1 by brian
clean slate
101
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
102
void drizzleclient_net_end(NET *net)
1 by brian
clean slate
103
{
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
104
  if (net->buff != NULL)
105
    free(net->buff);
679 by Brian Aker
Style cleanup, additional assert on net write()
106
  net->buff= NULL;
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
107
  return;
1 by brian
clean slate
108
}
109
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
110
void drizzleclient_net_close(NET *net)
383.1.52 by Monty Taylor
Added net_close_dirty function to hide vio a bit better. Started removing include violite.h from places.
111
{
112
  if (net->vio != NULL)
113
  {
840.1.19 by Monty Taylor
Renamed vio.
114
    drizzleclient_vio_delete(net->vio);
383.1.52 by Monty Taylor
Added net_close_dirty function to hide vio a bit better. Started removing include violite.h from places.
115
    net->vio= 0;
116
  }
117
}
1 by brian
clean slate
118
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
119
bool drizzleclient_net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
383.1.54 by Monty Taylor
More violite refactoring/removal.
120
{
840.1.19 by Monty Taylor
Renamed vio.
121
  return drizzleclient_vio_peer_addr(net->vio, buf, port, buflen);
383.1.54 by Monty Taylor
More violite refactoring/removal.
122
}
123
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
124
void drizzleclient_net_keepalive(NET *net, bool flag)
383.1.54 by Monty Taylor
More violite refactoring/removal.
125
{
840.1.19 by Monty Taylor
Renamed vio.
126
  drizzleclient_vio_keepalive(net->vio, flag);
383.1.54 by Monty Taylor
More violite refactoring/removal.
127
}
128
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
129
int drizzleclient_net_get_sd(NET *net)
383.1.54 by Monty Taylor
More violite refactoring/removal.
130
{
131
  return net->vio->sd;
132
}
133
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
134
bool drizzleclient_net_should_close(NET *net)
383.1.54 by Monty Taylor
More violite refactoring/removal.
135
{
136
  return net->error || (net->vio == 0);
137
}
138
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
139
bool drizzleclient_net_more_data(NET *net)
383.1.54 by Monty Taylor
More violite refactoring/removal.
140
{
141
  return (net->vio == 0 || net->vio->read_pos < net->vio->read_end);
142
}
143
1 by brian
clean slate
144
/** Realloc the packet buffer. */
145
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
146
bool drizzleclient_net_realloc(NET *net, size_t length)
1 by brian
clean slate
147
{
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
148
  unsigned char *buff;
1 by brian
clean slate
149
  size_t pkt_length;
150
151
  if (length >= net->max_packet_size)
152
  {
153
    /* @todo: 1 and 2 codes are identical. */
154
    net->error= 1;
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
155
    net->last_errno= CR_NET_PACKET_TOO_LARGE;
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
156
    return(1);
1 by brian
clean slate
157
  }
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
158
  pkt_length = (length+IO_SIZE-1) & ~(IO_SIZE-1);
1 by brian
clean slate
159
  /*
160
    We must allocate some extra bytes for the end 0 and to be able to
161
    read big compressed blocks
162
  */
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
163
  if (!(buff= (unsigned char*) realloc((char*) net->buff, pkt_length +
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
164
                               NET_HEADER_SIZE + COMP_HEADER_SIZE)))
1 by brian
clean slate
165
  {
166
    /* @todo: 1 and 2 codes are identical. */
167
    net->error= 1;
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
168
    net->last_errno= CR_OUT_OF_MEMORY;
1 by brian
clean slate
169
    /* In the server the error is reported by MY_WME flag. */
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
170
    return(1);
1 by brian
clean slate
171
  }
172
  net->buff=net->write_pos=buff;
294 by Brian Aker
libdrizzle has ulong removed.
173
  net->buff_end=buff+(net->max_packet= (uint32_t) pkt_length);
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
174
  return(0);
1 by brian
clean slate
175
}
176
177
178
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
179
   Check if there is any data to be read from the socket.
180
181
   @param sd   socket descriptor
182
183
   @retval
184
   0  No data to read
185
   @retval
186
   1  Data or EOF to read
187
   @retval
188
   -1   Don't know if data is ready or not
1 by brian
clean slate
189
*/
190
266.7.8 by Andy Lester
const happy
191
static bool net_data_is_ready(int sd)
1 by brian
clean slate
192
{
193
  struct pollfd ufds;
194
  int res;
195
196
  ufds.fd= sd;
197
  ufds.events= POLLIN | POLLPRI;
198
  if (!(res= poll(&ufds, 1, 0)))
199
    return 0;
200
  if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)))
201
    return 0;
202
  return 1;
203
}
204
205
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
206
   Remove unwanted characters from connection
207
   and check if disconnected.
208
209
   Read from socket until there is nothing more to read. Discard
210
   what is read.
211
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
212
   If there is anything when to read 'drizzleclient_net_clear' is called this
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
213
   normally indicates an error in the protocol.
214
215
   When connection is properly closed (for TCP it means with
216
   a FIN packet), then select() considers a socket "ready to read",
217
   in the sense that there's EOF to read, but read() returns 0.
218
219
   @param net            NET handler
220
   @param clear_buffer           if <> 0, then clear all data from comm buff
1 by brian
clean slate
221
*/
222
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
223
void drizzleclient_net_clear(NET *net, bool clear_buffer)
1 by brian
clean slate
224
{
225
  if (clear_buffer)
226
  {
266.7.8 by Andy Lester
const happy
227
    while (net_data_is_ready(net->vio->sd) > 0)
1 by brian
clean slate
228
    {
229
      /* The socket is ready */
840.1.19 by Monty Taylor
Renamed vio.
230
      if (drizzleclient_vio_read(net->vio, net->buff,
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
231
                   (size_t) net->max_packet) <= 0)
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
232
      {
1 by brian
clean slate
233
        net->error= 2;
234
        break;
235
      }
236
    }
237
  }
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
238
  net->pkt_nr=net->compress_pkt_nr=0;        /* Ready for new command */
1 by brian
clean slate
239
  net->write_pos=net->buff;
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
240
  return;
1 by brian
clean slate
241
}
242
243
244
/** Flush write_buffer if not empty. */
245
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
246
bool drizzleclient_net_flush(NET *net)
1 by brian
clean slate
247
{
277 by Brian Aker
Cleanup of my_bool from extra and libdrizzle
248
  bool error= 0;
1 by brian
clean slate
249
  if (net->buff != net->write_pos)
250
  {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
251
    error=drizzleclient_net_real_write(net, net->buff,
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
252
                         (size_t) (net->write_pos - net->buff)) ? 1 : 0;
1 by brian
clean slate
253
    net->write_pos=net->buff;
254
  }
255
  /* Sync packet number if using compression */
256
  if (net->compress)
257
    net->pkt_nr=net->compress_pkt_nr;
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
258
  return(error);
1 by brian
clean slate
259
}
260
261
262
/*****************************************************************************
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
263
 ** Write something to server/client buffer
264
 *****************************************************************************/
1 by brian
clean slate
265
266
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
267
   Write a logical packet with packet header.
268
269
   Format: Packet length (3 bytes), packet number(1 byte)
270
   When compression is used a 3 byte compression length is added
271
272
   @note
273
   If compression is used the original package is modified!
1 by brian
clean slate
274
*/
275
164 by Brian Aker
Commit cleanup of export types.
276
bool
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
277
drizzleclient_net_write(NET *net,const unsigned char *packet,size_t len)
1 by brian
clean slate
278
{
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
279
  unsigned char buff[NET_HEADER_SIZE];
1 by brian
clean slate
280
  if (unlikely(!net->vio)) /* nowhere to write */
281
    return 0;
282
  /*
283
    Big packets are handled by splitting them in packets of MAX_PACKET_LENGTH
284
    length. The last packet is always a packet that is < MAX_PACKET_LENGTH.
285
    (The last packet may even have a length of 0)
286
  */
287
  while (len >= MAX_PACKET_LENGTH)
288
  {
294 by Brian Aker
libdrizzle has ulong removed.
289
    const uint32_t z_size = MAX_PACKET_LENGTH;
1 by brian
clean slate
290
    int3store(buff, z_size);
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
291
    buff[3]= (unsigned char) net->pkt_nr++;
1 by brian
clean slate
292
    if (net_write_buff(net, buff, NET_HEADER_SIZE) ||
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
293
        net_write_buff(net, packet, z_size))
1 by brian
clean slate
294
      return 1;
295
    packet += z_size;
296
    len-=     z_size;
297
  }
298
  /* Write last packet */
299
  int3store(buff,len);
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
300
  buff[3]= (unsigned char) net->pkt_nr++;
1 by brian
clean slate
301
  if (net_write_buff(net, buff, NET_HEADER_SIZE))
302
    return 1;
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
303
  return net_write_buff(net,packet,len) ? 1 : 0;
1 by brian
clean slate
304
}
305
306
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
307
   Send a command to the server.
308
309
   The reason for having both header and packet is so that libdrizzle
310
   can easy add a header to a special command (like prepared statements)
311
   without having to re-alloc the string.
312
313
   As the command is part of the first data packet, we have to do some data
314
   juggling to put the command in there, without having to create a new
315
   packet.
316
317
   This function will split big packets into sub-packets if needed.
318
   (Each sub packet can only be 2^24 bytes)
319
320
   @param net        NET handler
321
   @param command    Command in MySQL server (enum enum_server_command)
322
   @param header    Header to write after command
323
   @param head_len    Length of header
324
   @param packet    Query or parameter to query
325
   @param len        Length of packet
326
327
   @retval
328
   0    ok
329
   @retval
330
   1    error
1 by brian
clean slate
331
*/
332
164 by Brian Aker
Commit cleanup of export types.
333
bool
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
334
drizzleclient_net_write_command(NET *net,unsigned char command,
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
335
                  const unsigned char *header, size_t head_len,
336
                  const unsigned char *packet, size_t len)
1 by brian
clean slate
337
{
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
338
  uint32_t length=len+1+head_len;            /* 1 extra byte for command */
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
339
  unsigned char buff[NET_HEADER_SIZE+1];
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
340
  uint32_t header_size=NET_HEADER_SIZE+1;
1 by brian
clean slate
341
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
342
  buff[4]=command;                /* For first packet */
1 by brian
clean slate
343
344
  if (length >= MAX_PACKET_LENGTH)
345
  {
346
    /* Take into account that we have the command in the first header */
347
    len= MAX_PACKET_LENGTH - 1 - head_len;
348
    do
349
    {
350
      int3store(buff, MAX_PACKET_LENGTH);
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
351
      buff[3]= (unsigned char) net->pkt_nr++;
1 by brian
clean slate
352
      if (net_write_buff(net, buff, header_size) ||
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
353
          net_write_buff(net, header, head_len) ||
354
          net_write_buff(net, packet, len))
355
        return(1);
1 by brian
clean slate
356
      packet+= len;
357
      length-= MAX_PACKET_LENGTH;
358
      len= MAX_PACKET_LENGTH;
359
      head_len= 0;
360
      header_size= NET_HEADER_SIZE;
361
    } while (length >= MAX_PACKET_LENGTH);
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
362
    len=length;                    /* Data left to be written */
1 by brian
clean slate
363
  }
364
  int3store(buff,length);
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
365
  buff[3]= (unsigned char) net->pkt_nr++;
366
  return((net_write_buff(net, buff, header_size) ||
367
          (head_len && net_write_buff(net, header, head_len)) ||
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
368
          net_write_buff(net, packet, len) || drizzleclient_net_flush(net)) ? 1 : 0 );
1 by brian
clean slate
369
}
370
371
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
372
   Caching the data in a local buffer before sending it.
1 by brian
clean slate
373
374
   Fill up net->buffer and send it to the client when full.
375
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
376
   If the rest of the to-be-sent-packet is bigger than buffer,
377
   send it in one big block (to avoid copying to internal buffer).
378
   If not, copy the rest of the data to the buffer and return without
379
   sending data.
380
381
   @param net        Network handler
382
   @param packet    Packet to send
383
   @param len        Length of packet
384
385
   @note
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
386
   The cached buffer can be sent as it is with 'drizzleclient_net_flush()'.
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
387
   In this code we have to be careful to not send a packet longer than
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
388
   MAX_PACKET_LENGTH to drizzleclient_net_real_write() if we are using the compressed
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
389
   protocol as we store the length of the compressed packet in 3 bytes.
390
391
   @retval
392
   0    ok
393
   @retval
394
   1
1 by brian
clean slate
395
*/
396
164 by Brian Aker
Commit cleanup of export types.
397
static bool
398
net_write_buff(NET *net, const unsigned char *packet, uint32_t len)
1 by brian
clean slate
399
{
294 by Brian Aker
libdrizzle has ulong removed.
400
  uint32_t left_length;
1 by brian
clean slate
401
  if (net->compress && net->max_packet > MAX_PACKET_LENGTH)
402
    left_length= MAX_PACKET_LENGTH - (net->write_pos - net->buff);
403
  else
294 by Brian Aker
libdrizzle has ulong removed.
404
    left_length= (uint32_t) (net->buff_end - net->write_pos);
1 by brian
clean slate
405
406
  if (len > left_length)
407
  {
408
    if (net->write_pos != net->buff)
409
    {
410
      /* Fill up already used packet and write it */
212.6.16 by Mats Kindahl
Removing redundant use of casts in libdrizzle/ for memcmp(), memcpy(), memset(), and memmove().
411
      memcpy(net->write_pos,packet,left_length);
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
412
      if (drizzleclient_net_real_write(net, net->buff,
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
413
                         (size_t) (net->write_pos - net->buff) + left_length))
414
        return 1;
1 by brian
clean slate
415
      net->write_pos= net->buff;
416
      packet+= left_length;
417
      len-= left_length;
418
    }
419
    if (net->compress)
420
    {
421
      /*
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
422
        We can't have bigger packets than 16M with compression
423
        Because the uncompressed length is stored in 3 bytes
1 by brian
clean slate
424
      */
425
      left_length= MAX_PACKET_LENGTH;
426
      while (len > left_length)
427
      {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
428
        if (drizzleclient_net_real_write(net, packet, left_length))
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
429
          return 1;
430
        packet+= left_length;
431
        len-= left_length;
1 by brian
clean slate
432
      }
433
    }
434
    if (len > net->max_packet)
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
435
      return drizzleclient_net_real_write(net, packet, len) ? 1 : 0;
1 by brian
clean slate
436
    /* Send out rest of the blocks as full sized blocks */
437
  }
212.6.16 by Mats Kindahl
Removing redundant use of casts in libdrizzle/ for memcmp(), memcpy(), memset(), and memmove().
438
  memcpy(net->write_pos,packet,len);
1 by brian
clean slate
439
  net->write_pos+= len;
440
  return 0;
441
}
442
443
444
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
445
   Read and write one packet using timeouts.
446
   If needed, the packet is compressed before sending.
1 by brian
clean slate
447
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
448
   @todo
449
   - TODO is it needed to set this variable if we have no socket
1 by brian
clean slate
450
*/
451
174 by Brian Aker
Removed alarm timeouts on writes.
452
/*
453
  TODO: rewrite this in a manner to do non-block writes. If a write can not be made, and we are
454
  in the server, yield to another process and come back later.
455
*/
1 by brian
clean slate
456
int
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
457
drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len)
1 by brian
clean slate
458
{
459
  size_t length;
679 by Brian Aker
Style cleanup, additional assert on net write()
460
  const unsigned char *pos, *end;
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
461
  uint32_t retry_count= 0;
1 by brian
clean slate
462
178 by Brian Aker
Set timeouts for writes as well.
463
  /* Backup of the original SO_RCVTIMEO timeout */
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
464
#ifndef __sun
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
465
  struct timespec backtime;
178 by Brian Aker
Set timeouts for writes as well.
466
  int error;
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
467
#endif
178 by Brian Aker
Set timeouts for writes as well.
468
1 by brian
clean slate
469
  if (net->error == 2)
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
470
    return(-1);                /* socket can't be used */
1 by brian
clean slate
471
472
  net->reading_or_writing=2;
473
  if (net->compress)
474
  {
475
    size_t complen;
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
476
    unsigned char *b;
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
477
    const uint32_t header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE;
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
478
    if (!(b= (unsigned char*) malloc(len + NET_HEADER_SIZE +
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
479
                             COMP_HEADER_SIZE)))
1 by brian
clean slate
480
    {
481
      net->error= 2;
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
482
      net->last_errno= CR_OUT_OF_MEMORY;
1 by brian
clean slate
483
      /* In the server, the error is reported by MY_WME flag. */
484
      net->reading_or_writing= 0;
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
485
      return(1);
1 by brian
clean slate
486
    }
487
    memcpy(b+header_length,packet,len);
488
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
489
    complen= len * 120 / 100 + 12;
490
    unsigned char * compbuf= (unsigned char *) malloc(complen);
491
    if (compbuf != NULL)
492
    {
493
      uLongf tmp_complen= complen;
494
      int res= compress((Bytef*) compbuf, &tmp_complen,
495
                        (Bytef*) (b+header_length),
496
                        len);
497
      complen= tmp_complen;
498
499
      free(compbuf);
500
501
      if ((res != Z_OK) || (complen >= len))
502
        complen= 0;
503
      else
504
      {
505
        size_t tmplen= complen;
506
        complen= len;
507
        len= tmplen;
508
      }
509
    }
510
    else
511
    {
1 by brian
clean slate
512
      complen=0;
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
513
    }
1 by brian
clean slate
514
    int3store(&b[NET_HEADER_SIZE],complen);
515
    int3store(b,len);
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
516
    b[3]=(unsigned char) (net->compress_pkt_nr++);
1 by brian
clean slate
517
    len+= header_length;
518
    packet= b;
519
  }
520
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
521
#ifndef __sun
178 by Brian Aker
Set timeouts for writes as well.
522
  /* Check for error, currently assert */
523
  if (net->write_timeout)
524
  {
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
525
    struct timespec waittime;
779.3.10 by Monty Taylor
Turned on -Wshadow.
526
    socklen_t time_len;
178 by Brian Aker
Set timeouts for writes as well.
527
528
    waittime.tv_sec= net->write_timeout;
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
529
    waittime.tv_nsec= 0;
178 by Brian Aker
Set timeouts for writes as well.
530
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
531
    memset(&backtime, 0, sizeof(struct timespec));
779.3.10 by Monty Taylor
Turned on -Wshadow.
532
    time_len= sizeof(struct timespec);
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
533
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
779.3.10 by Monty Taylor
Turned on -Wshadow.
534
                      &backtime, &time_len);
178 by Brian Aker
Set timeouts for writes as well.
535
    if (error != 0)
536
    {
537
      perror("getsockopt");
538
      assert(error == 0);
539
    }
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
540
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
541
                      &waittime, (socklen_t)sizeof(struct timespec));
178 by Brian Aker
Set timeouts for writes as well.
542
    assert(error == 0);
543
  }
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
544
#endif
545
1 by brian
clean slate
546
  pos= packet;
547
  end=pos+len;
174 by Brian Aker
Removed alarm timeouts on writes.
548
  /* Loop until we have read everything */
1 by brian
clean slate
549
  while (pos != end)
550
  {
679 by Brian Aker
Style cleanup, additional assert on net write()
551
    assert(pos);
840.1.19 by Monty Taylor
Renamed vio.
552
    if ((long) (length= drizzleclient_vio_write(net->vio, pos, (size_t) (end-pos))) <= 0)
1 by brian
clean slate
553
    {
840.1.19 by Monty Taylor
Renamed vio.
554
      const bool interrupted= drizzleclient_vio_should_retry(net->vio);
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
555
      /*
556
        If we read 0, or we were interrupted this means that
557
        we need to switch to blocking mode and wait until the timeout
174 by Brian Aker
Removed alarm timeouts on writes.
558
        on the socket kicks in.
559
      */
560
      if ((interrupted || length == 0))
1 by brian
clean slate
561
      {
174 by Brian Aker
Removed alarm timeouts on writes.
562
        bool old_mode;
563
840.1.19 by Monty Taylor
Renamed vio.
564
        while (drizzleclient_vio_blocking(net->vio, true, &old_mode) < 0)
174 by Brian Aker
Removed alarm timeouts on writes.
565
        {
840.1.19 by Monty Taylor
Renamed vio.
566
          if (drizzleclient_vio_should_retry(net->vio) && retry_count++ < net->retry_count)
174 by Brian Aker
Removed alarm timeouts on writes.
567
            continue;
568
          net->error= 2;                     /* Close socket */
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
569
          net->last_errno= CR_NET_PACKET_TOO_LARGE;
174 by Brian Aker
Removed alarm timeouts on writes.
570
          goto end;
571
        }
572
        retry_count=0;
573
        continue;
1 by brian
clean slate
574
      }
575
      else
576
      {
174 by Brian Aker
Removed alarm timeouts on writes.
577
        if (retry_count++ < net->retry_count)
578
          continue;
1 by brian
clean slate
579
      }
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
580
840.1.19 by Monty Taylor
Renamed vio.
581
      if (drizzleclient_vio_errno(net->vio) == EINTR)
1 by brian
clean slate
582
      {
174 by Brian Aker
Removed alarm timeouts on writes.
583
        continue;
1 by brian
clean slate
584
      }
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
585
      net->error= 2;                /* Close socket */
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
586
      net->last_errno= (interrupted ? CR_NET_WRITE_INTERRUPTED :
587
                        CR_NET_ERROR_ON_WRITE);
1 by brian
clean slate
588
      break;
589
    }
590
    pos+=length;
591
  }
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
592
end:
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
593
  if ((net->compress) && (packet != NULL))
594
    free((char*) packet);
1 by brian
clean slate
595
  net->reading_or_writing=0;
174 by Brian Aker
Removed alarm timeouts on writes.
596
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
597
#ifndef __sun
178 by Brian Aker
Set timeouts for writes as well.
598
  if (net->write_timeout)
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
599
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
600
                      &backtime, (socklen_t)sizeof(struct timespec));
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
601
#endif
178 by Brian Aker
Set timeouts for writes as well.
602
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
603
  return(((int) (pos != end)));
1 by brian
clean slate
604
}
605
606
607
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
608
   Reads one packet to net->buff + net->where_b.
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
609
   Long packets are handled by drizzleclient_net_read().
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
610
   This function reallocates the net->buff buffer if necessary.
1 by brian
clean slate
611
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
612
   @return
613
   Returns length of packet.
1 by brian
clean slate
614
*/
615
294 by Brian Aker
libdrizzle has ulong removed.
616
static uint32_t
1 by brian
clean slate
617
my_real_read(NET *net, size_t *complen)
618
{
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
619
  unsigned char *pos;
1 by brian
clean slate
620
  size_t length;
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
621
  uint32_t i,retry_count=0;
294 by Brian Aker
libdrizzle has ulong removed.
622
  uint32_t len=packet_error;
205 by Brian Aker
uint32 -> uin32_t
623
  uint32_t remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
624
                    NET_HEADER_SIZE);
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
625
626
#ifndef __sun
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
627
  /* Backup of the original SO_RCVTIMEO timeout */
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
628
  struct timespec backtime;
236.2.2 by rbradfor
Using correct coding standards for variable initialization
629
  int error= 0;
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
630
#endif
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
631
1 by brian
clean slate
632
  *complen = 0;
633
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
634
  net->reading_or_writing= 1;
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
635
  /* Read timeout is set in drizzleclient_net_set_read_timeout */
1 by brian
clean slate
636
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
637
  pos = net->buff + net->where_b;        /* net->packet -4 */
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
638
639
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
640
#ifndef __sun
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
641
  /* Check for error, currently assert */
175 by Brian Aker
If a read timeout exists, then set it up, otherwise just let the blocking
642
  if (net->read_timeout)
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
643
  {
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
644
    struct timespec waittime;
779.3.10 by Monty Taylor
Turned on -Wshadow.
645
    socklen_t time_len;
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
646
647
    waittime.tv_sec= net->read_timeout;
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
648
    waittime.tv_nsec= 0;
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
649
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
650
    memset(&backtime, 0, sizeof(struct timespec));
779.3.10 by Monty Taylor
Turned on -Wshadow.
651
    time_len= sizeof(struct timespec);
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
652
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
779.3.10 by Monty Taylor
Turned on -Wshadow.
653
                      &backtime, &time_len);
177 by brian
Fix for call (missed in the review the initial length setting).
654
    if (error != 0)
655
    {
656
      perror("getsockopt");
657
      assert(error == 0);
658
    }
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
659
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
660
                      &waittime, (socklen_t)sizeof(struct timespec));
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
661
    assert(error == 0);
662
  }
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
663
#endif
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
664
665
  for (i= 0; i < 2 ; i++)
666
  {
667
    while (remain > 0)
1 by brian
clean slate
668
    {
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
669
      /* First read is done with non blocking mode */
840.1.19 by Monty Taylor
Renamed vio.
670
      if ((long) (length= drizzleclient_vio_read(net->vio, pos, remain)) <= 0L)
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
671
      {
840.1.19 by Monty Taylor
Renamed vio.
672
        const bool interrupted = drizzleclient_vio_should_retry(net->vio);
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
673
674
        if (interrupted)
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
675
        {                    /* Probably in MIT threads */
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
676
          if (retry_count++ < net->retry_count)
677
            continue;
678
        }
840.1.19 by Monty Taylor
Renamed vio.
679
        if (drizzleclient_vio_errno(net->vio) == EINTR)
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
680
        {
681
          continue;
682
        }
683
        len= packet_error;
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
684
        net->error= 2;                /* Close socket */
840.1.19 by Monty Taylor
Renamed vio.
685
        net->last_errno= (drizzleclient_vio_was_interrupted(net->vio) ?
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
686
                          CR_NET_READ_INTERRUPTED :
687
                          CR_NET_READ_ERROR);
688
        ER(net->last_errno);
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
689
        goto end;
690
      }
205 by Brian Aker
uint32 -> uin32_t
691
      remain -= (uint32_t) length;
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
692
      pos+= length;
693
    }
694
    if (i == 0)
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
695
    {                    /* First parts is packet length */
294 by Brian Aker
libdrizzle has ulong removed.
696
      uint32_t helping;
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
697
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
698
      if (net->buff[net->where_b + 3] != (unsigned char) net->pkt_nr)
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
699
      {
700
        len= packet_error;
701
        /* Not a NET error on the client. XXX: why? */
702
        goto end;
703
      }
704
      net->compress_pkt_nr= ++net->pkt_nr;
705
      if (net->compress)
706
      {
707
        /*
708
          If the packet is compressed then complen > 0 and contains the
709
          number of bytes in the uncompressed packet
710
        */
711
        *complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE]));
712
      }
713
714
      len=uint3korr(net->buff+net->where_b);
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
715
      if (!len)                /* End of big multi-packet */
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
716
        goto end;
717
      helping = max(len,*complen) + net->where_b;
718
      /* The necessary size of net->buff */
719
      if (helping >= net->max_packet)
720
      {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
721
        if (drizzleclient_net_realloc(net,helping))
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
722
        {
723
          len= packet_error;          /* Return error and close connection */
724
          goto end;
725
        }
726
      }
727
      pos=net->buff + net->where_b;
205 by Brian Aker
uint32 -> uin32_t
728
      remain = (uint32_t) len;
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
729
    }
730
  }
1 by brian
clean slate
731
732
end:
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
733
#ifndef __sun
175 by Brian Aker
If a read timeout exists, then set it up, otherwise just let the blocking
734
  if  (net->read_timeout)
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
735
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
736
                      &backtime, (socklen_t)sizeof(struct timespec));
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
737
  assert(error == 0);
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
738
#endif
173 by Brian Aker
Removed thd alarm around socket, we now use timeouts.
739
  net->reading_or_writing= 0;
740
1 by brian
clean slate
741
  return(len);
742
}
743
744
745
/**
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
746
   Read a packet from the client/server and return it without the internal
747
   package header.
748
749
   If the packet is the first packet of a multi-packet packet
750
   (which is indicated by the length of the packet = 0xffffff) then
751
   all sub packets are read and concatenated.
752
753
   If the packet was compressed, its uncompressed and the length of the
754
   uncompressed packet is returned.
755
756
   @return
757
   The function returns the length of the found packet or packet_error.
758
   net->read_pos points to the read data.
1 by brian
clean slate
759
*/
760
164 by Brian Aker
Commit cleanup of export types.
761
uint32_t
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
762
drizzleclient_net_read(NET *net)
1 by brian
clean slate
763
{
764
  size_t len, complen;
765
766
  if (!net->compress)
767
  {
768
    len = my_real_read(net,&complen);
769
    if (len == MAX_PACKET_LENGTH)
770
    {
771
      /* First packet of a multi-packet.  Concatenate the packets */
294 by Brian Aker
libdrizzle has ulong removed.
772
      uint32_t save_pos = net->where_b;
1 by brian
clean slate
773
      size_t total_length= 0;
774
      do
775
      {
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
776
        net->where_b += len;
777
        total_length += len;
778
        len = my_real_read(net,&complen);
1 by brian
clean slate
779
      } while (len == MAX_PACKET_LENGTH);
780
      if (len != packet_error)
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
781
        len+= total_length;
1 by brian
clean slate
782
      net->where_b = save_pos;
783
    }
784
    net->read_pos = net->buff + net->where_b;
785
    if (len != packet_error)
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
786
      net->read_pos[len]=0;        /* Safeguard for drizzleclient_use_result */
1 by brian
clean slate
787
    return len;
788
  }
789
  else
790
  {
791
    /* We are using the compressed protocol */
792
294 by Brian Aker
libdrizzle has ulong removed.
793
    uint32_t buf_length;
794
    uint32_t start_of_packet;
795
    uint32_t first_packet_offset;
426 by Monty Taylor
Replaced struct timeval with struct timespec. Timeval doesn't seem to be
796
    uint32_t read_length, multi_byte_packet=0;
1 by brian
clean slate
797
798
    if (net->remain_in_buf)
799
    {
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
800
      buf_length= net->buf_length;        /* Data left in old packet */
1 by brian
clean slate
801
      first_packet_offset= start_of_packet= (net->buf_length -
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
802
                                             net->remain_in_buf);
1 by brian
clean slate
803
      /* Restore the character that was overwritten by the end 0 */
804
      net->buff[start_of_packet]= net->save_char;
805
    }
806
    else
807
    {
808
      /* reuse buffer, as there is nothing in it that we need */
809
      buf_length= start_of_packet= first_packet_offset= 0;
810
    }
811
    for (;;)
812
    {
294 by Brian Aker
libdrizzle has ulong removed.
813
      uint32_t packet_len;
1 by brian
clean slate
814
815
      if (buf_length - start_of_packet >= NET_HEADER_SIZE)
816
      {
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
817
        read_length = uint3korr(net->buff+start_of_packet);
818
        if (!read_length)
819
        {
820
          /* End of multi-byte packet */
821
          start_of_packet += NET_HEADER_SIZE;
822
          break;
823
        }
824
        if (read_length + NET_HEADER_SIZE <= buf_length - start_of_packet)
825
        {
826
          if (multi_byte_packet)
827
          {
828
            /* Remove packet header for second packet */
829
            memmove(net->buff + first_packet_offset + start_of_packet,
830
                    net->buff + first_packet_offset + start_of_packet +
831
                    NET_HEADER_SIZE,
832
                    buf_length - start_of_packet);
833
            start_of_packet += read_length;
834
            buf_length -= NET_HEADER_SIZE;
835
          }
836
          else
837
            start_of_packet+= read_length + NET_HEADER_SIZE;
1 by brian
clean slate
838
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
839
          if (read_length != MAX_PACKET_LENGTH)    /* last package */
840
          {
841
            multi_byte_packet= 0;        /* No last zero len packet */
842
            break;
843
          }
844
          multi_byte_packet= NET_HEADER_SIZE;
845
          /* Move data down to read next data packet after current one */
846
          if (first_packet_offset)
847
          {
848
            memmove(net->buff,net->buff+first_packet_offset,
849
                    buf_length-first_packet_offset);
850
            buf_length-=first_packet_offset;
851
            start_of_packet -= first_packet_offset;
852
            first_packet_offset=0;
853
          }
854
          continue;
855
        }
1 by brian
clean slate
856
      }
857
      /* Move data down to read next data packet after current one */
858
      if (first_packet_offset)
859
      {
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
860
        memmove(net->buff,net->buff+first_packet_offset,
861
                buf_length-first_packet_offset);
862
        buf_length-=first_packet_offset;
863
        start_of_packet -= first_packet_offset;
864
        first_packet_offset=0;
1 by brian
clean slate
865
      }
866
867
      net->where_b=buf_length;
868
      if ((packet_len = my_real_read(net,&complen)) == packet_error)
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
869
        return packet_error;
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
870
871
      if (complen)
1 by brian
clean slate
872
      {
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
873
        unsigned char * compbuf= (unsigned char *) malloc(complen);
874
        if (compbuf != NULL)
875
        {
876
          uLongf tmp_complen= complen;
877
          int error= uncompress((Bytef*) compbuf, &tmp_complen,
878
                                (Bytef*) (net->buff + net->where_b),
879
                                (uLong)packet_len);
880
          complen= tmp_complen;
881
882
          if (error != Z_OK)
883
          {
884
            net->error= 2;            /* caller will close socket */
885
            net->last_errno= CR_NET_UNCOMPRESS_ERROR;
886
          }
887
          else
888
          {
889
            memcpy((net->buff + net->where_b), compbuf, complen);
890
          }
891
          free(compbuf);
892
        }
1 by brian
clean slate
893
      }
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
894
      else
895
        complen= packet_len;
896
1 by brian
clean slate
897
    }
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
898
    buf_length+= complen;
1 by brian
clean slate
899
900
    net->read_pos=      net->buff+ first_packet_offset + NET_HEADER_SIZE;
901
    net->buf_length=    buf_length;
294 by Brian Aker
libdrizzle has ulong removed.
902
    net->remain_in_buf= (uint32_t) (buf_length - start_of_packet);
903
    len = ((uint32_t) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
1 by brian
clean slate
904
           multi_byte_packet);
383.1.36 by Monty Taylor
Fixed copyright header and spacing in net_serv.c.
905
    net->save_char= net->read_pos[len];    /* Must be saved */
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
906
    net->read_pos[len]=0;        /* Safeguard for drizzleclient_use_result */
1 by brian
clean slate
907
  }
908
  return len;
383.1.37 by Monty Taylor
Removed some mysys from net_serv.c.
909
  }
1 by brian
clean slate
910
911
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
912
void drizzleclient_net_set_read_timeout(NET *net, uint32_t timeout)
1 by brian
clean slate
913
{
914
  net->read_timeout= timeout;
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
915
#ifndef __sun
1 by brian
clean slate
916
  if (net->vio)
840.1.19 by Monty Taylor
Renamed vio.
917
    drizzleclient_vio_timeout(net->vio, 0, timeout);
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
918
#endif
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
919
  return;
1 by brian
clean slate
920
}
921
922
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
923
void drizzleclient_net_set_write_timeout(NET *net, uint32_t timeout)
1 by brian
clean slate
924
{
925
  net->write_timeout= timeout;
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
926
#ifndef __sun
1 by brian
clean slate
927
  if (net->vio)
840.1.19 by Monty Taylor
Renamed vio.
928
    drizzleclient_vio_timeout(net->vio, 1, timeout);
139.1.14 by Trond Norbye
Solaris does not support SO_SNDTIMEO and SO_RCVTIMEO
929
#endif
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
930
  return;
1 by brian
clean slate
931
}
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
932
/**
933
  Clear possible error state of struct NET
934
935
  @param net  clear the state of the argument
936
*/
937
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
938
void drizzleclient_drizzleclient_net_clear_error(NET *net)
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
939
{
940
  net->last_errno= 0;
941
  net->last_error[0]= '\0';
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
942
  strcpy(net->sqlstate, drizzleclient_sqlstate_get_not_error());
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
943
}
944