net_serv.cc 29.7 KB
Newer Older
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1
/* Copyright (C) 2000 MySQL AB
2

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8
   This program is distributed in the hope that it will be useful,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10 11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
13 14 15
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
16

17 18 19 20 21 22 23
/*
  This file is the net layer API for the MySQL client/server protocol,
  which is a tightly coupled, proprietary protocol owned by MySQL AB.
  Any re-implementations of this protocol must also be under GPL
  unless one has got an license from MySQL AB stating otherwise.
*/

24 25
/*
  Write and read of logical packets to/from socket
bk@work.mysql.com's avatar
bk@work.mysql.com committed
26

27 28 29 30
  Writes are cached into net_buffer_length big packets.
  Read packets are reallocated dynamicly when reading big packets.
  Each logical packet has the following pre-info:
  3 byte length & 1 byte package-number.
31 32 33

  This file needs to be written in C as it's used by the libmysql client as a
  C file.
34
*/
35

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
36 37 38 39
/*
  HFTODO this must be hidden if we don't want client capabilities in 
  embedded library
 */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
40 41 42
#ifdef __WIN__
#include <winsock.h>
#endif
43
#include <my_global.h>
44 45
#include <mysql.h>
#include <mysql_embed.h>
tonu@hundin.mysql.fi's avatar
tonu@hundin.mysql.fi committed
46
#include <mysql_com.h>
47
#include <mysqld_error.h>
bk@work.mysql.com's avatar
bk@work.mysql.com committed
48 49
#include <my_sys.h>
#include <m_string.h>
50 51
#include <my_net.h>
#include <violite.h>
bk@work.mysql.com's avatar
bk@work.mysql.com committed
52 53 54
#include <signal.h>
#include <errno.h>

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
55
#ifdef EMBEDDED_LIBRARY
56
#undef MYSQL_SERVER
57
#undef MYSQL_CLIENT
58
#define MYSQL_CLIENT
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
59 60 61
#endif /*EMBEDDED_LIBRARY */


62 63 64 65 66 67 68 69 70
/*
  The following handles the differences when this is linked between the
  client and the server.

  This gives an error if a too big packet is found
  The server can change this with the -O switch, but because the client
  can't normally do this the client should have a bigger max_allowed_packet.
*/

71 72 73
#if defined(__WIN__) || !defined(MYSQL_SERVER)
  /* The following is because alarms doesn't work on windows. */
#define NO_ALARM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74
#endif
75

76
#ifndef NO_ALARM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
77 78 79
#include "my_pthread.h"
void sql_print_error(const char *format,...);
#else
80
#define DONT_USE_THR_ALARM
81
#endif /* NO_ALARM */
82 83

#include "thr_alarm.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
84

85
#ifdef MYSQL_SERVER
86 87 88 89 90
/*
  The following variables/functions should really not be declared
  extern, but as it's hard to include mysql_priv.h here, we have to
  live with this for a while.
*/
91
extern uint test_flags;
92
extern ulong bytes_sent, bytes_received, net_big_packet_count;
93
extern pthread_mutex_t LOCK_bytes_sent , LOCK_bytes_received;
94
#ifndef MYSQL_INSTANCE_MANAGER
95
extern void query_cache_insert(NET *net, const char *packet, ulong length);
96
#define USE_QUERY_CACHE
97
#define update_statistics(A) A
98 99 100 101
#endif /* MYSQL_INSTANCE_MANGER */
#endif /* defined(MYSQL_SERVER) && !defined(MYSQL_INSTANCE_MANAGER) */

#if !defined(MYSQL_SERVER) || defined(MYSQL_INSTANCE_MANAGER)
102
#define update_statistics(A)
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
103
#define thd_increment_bytes_sent(N)
104 105
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
106
#define TEST_BLOCKING		8
107
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
108

109
static my_bool net_write_buff(NET *net,const char *packet,ulong len);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
110 111 112 113


	/* Init with packet info */

114
my_bool my_net_init(NET *net, Vio* vio)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
115
{
116
  DBUG_ENTER("my_net_init");
117 118
  my_net_local_init(net);			/* Set some limits */
  if (!(net->buff=(uchar*) my_malloc((uint32) net->max_packet+
119 120
				     NET_HEADER_SIZE + COMP_HEADER_SIZE,
				     MYF(MY_WME))))
121
    DBUG_RETURN(1);
122
  net->buff_end=net->buff+net->max_packet;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
123
  net->vio = vio;
124
  net->no_send_ok= net->no_send_eof= net->no_send_error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
125
  net->error=0; net->return_errno=0; net->return_status=0;
126
  net->pkt_nr=net->compress_pkt_nr=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
127 128 129 130 131
  net->write_pos=net->read_pos = net->buff;
  net->last_error[0]=0;
  net->compress=0; net->reading_or_writing=0;
  net->where_b = net->remain_in_buf=0;
  net->last_errno=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
132
  net->query_cache_query=0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
133
  net->report_error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
134 135 136 137

  if (vio != 0)					/* If real connection */
  {
    net->fd  = vio_fd(vio);			/* For perl DBI/DBD */
138
#if defined(MYSQL_SERVER) && !defined(__WIN__) && !defined(__EMX__) && !defined(OS2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
139
    if (!(test_flags & TEST_BLOCKING))
140 141 142 143
    {
      my_bool old_mode;
      vio_blocking(vio, FALSE, &old_mode);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
144
#endif
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
145
    vio_fastsend(vio);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
146
  }
147
  DBUG_RETURN(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
148 149
}

150

bk@work.mysql.com's avatar
bk@work.mysql.com committed
151 152
void net_end(NET *net)
{
153
  DBUG_ENTER("net_end");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
154 155
  my_free((gptr) net->buff,MYF(MY_ALLOW_ZERO_PTR));
  net->buff=0;
156
  DBUG_VOID_RETURN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
157 158
}

159

bk@work.mysql.com's avatar
bk@work.mysql.com committed
160 161
/* Realloc the packet buffer */

162
my_bool net_realloc(NET *net, ulong length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
163 164 165
{
  uchar *buff;
  ulong pkt_length;
166 167 168
  DBUG_ENTER("net_realloc");
  DBUG_PRINT("enter",("length: %lu", length));

169
  if (length >= net->max_packet_size)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
170
  {
171 172
    DBUG_PRINT("error", ("Packet too large. Max size: %lu",
               net->max_packet_size));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
173 174 175
    net->error= 1;
    net->report_error= 1;
    net->last_errno= ER_NET_PACKET_TOO_LARGE;
176
    DBUG_RETURN(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
177
  }
178
  pkt_length = (length+IO_SIZE-1) & ~(IO_SIZE-1); 
179 180 181 182
  /*
    We must allocate some extra bytes for the end 0 and to be able to
    read big compressed blocks
  */
183
  if (!(buff=(uchar*) my_realloc((char*) net->buff, (uint32) pkt_length +
184 185
				 NET_HEADER_SIZE + COMP_HEADER_SIZE,
				 MYF(MY_WME))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
186
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
187 188 189
    net->error= 1;
    net->report_error= 1;
    net->last_errno= ER_OUT_OF_RESOURCES;
190
    DBUG_RETURN(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
191 192 193
  }
  net->buff=net->write_pos=buff;
  net->buff_end=buff+(net->max_packet=pkt_length);
194
  DBUG_RETURN(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
195 196
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210

/*
  Check if there is any data to be read from the socket

  SYNOPSIS
    net_data_is_ready()
    sd   socket descriptor

  DESCRIPTION
    Check if there is any data to be read from the socket.

  RETURN VALUES
    0	No data to read
    1	Data or EOF to read
211
    -1  Don't know if data is ready or not
212 213
*/

214
static int net_data_is_ready(my_socket sd)
215
{
216 217 218 219 220 221 222 223 224 225 226 227
#ifdef HAVE_POLL
  struct pollfd ufds;
  int res;

  ufds.fd= sd;
  ufds.events= POLLIN | POLLPRI;
  if (!(res= poll(&ufds, 1, 0)))
    return 0;
  if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)))
    return 0;
  return 1;
#else
228 229 230 231
  fd_set sfds;
  struct timeval tv;
  int res;

232 233
#ifndef __WIN__
  /* Windows uses an _array_ of 64 fd's as default, so it's safe */
234 235
  if (sd >= FD_SETSIZE)
    return -1;
236 237
  #define NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE
#endif
238

239 240 241 242 243 244
  FD_ZERO(&sfds);
  FD_SET(sd, &sfds);

  tv.tv_sec= tv.tv_usec= 0;

  if ((res= select(sd+1, &sfds, NULL, NULL, &tv)) < 0)
245
    return 0;
246 247
  else
    return test(res ? FD_ISSET(sd, &sfds) : 0);
248
#endif
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}


/*
  Remove unwanted characters from connection
  and check if disconnected

  SYNOPSIS
    net_clear()
    net			NET handler

  DESCRIPTION
    Read from socket until there is nothing more to read. Discard
    what is read.

    If there is anything when to read 'net_clear' is called this
    normally indicates an error in the protocol.

    When connection is properly closed (for TCP it means with
    a FIN packet), then select() considers a socket "ready to read",
    in the sense that there's EOF to read, but read() returns 0.

*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
272 273 274

void net_clear(NET *net)
{
275
  int count, ready;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
276
  DBUG_ENTER("net_clear");
277
#if !defined(EMBEDDED_LIBRARY)
278
  while((ready= net_data_is_ready(net->vio->sd)) > 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
279
  {
280 281 282 283 284 285 286 287 288 289 290 291
    /* The socket is ready */
    if ((count= vio_read(net->vio, (char*) (net->buff),
                         (uint32) net->max_packet)) > 0)
    {
      DBUG_PRINT("info",("skipped %d bytes from file: %s",
                         count, vio_description(net->vio)));
#ifdef EXTRA_DEBUG
      fprintf(stderr,"skipped %d bytes from file: %s\n",
              count, vio_description(net->vio));
#endif
    }
    else
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
292
    {
293 294 295
      DBUG_PRINT("info",("socket ready but only EOF to read - disconnected"));
      net->error= 2;
      break;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
296
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
297
  }
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
#ifdef NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE
  /* 'net_data_is_ready' returned "don't know" */
  if (ready == -1)
  {
    /* Read unblocking to clear net */
    my_bool old_mode;
    if (!vio_blocking(net->vio, FALSE, &old_mode))
    {
      while ((count= vio_read(net->vio, (char*) (net->buff),
                              (uint32) net->max_packet)) > 0)
	DBUG_PRINT("info",("skipped %d bytes from file: %s",
			   count, vio_description(net->vio)));
      vio_blocking(net->vio, TRUE, &old_mode);
    }
  }
#endif
314
#endif
315
  net->pkt_nr=net->compress_pkt_nr=0;		/* Ready for new command */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
316
  net->write_pos=net->buff;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
317
  DBUG_VOID_RETURN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
318 319
}

320

bk@work.mysql.com's avatar
bk@work.mysql.com committed
321 322
	/* Flush write_buffer if not empty. */

323
my_bool net_flush(NET *net)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
324
{
325
  my_bool error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
326 327 328
  DBUG_ENTER("net_flush");
  if (net->buff != net->write_pos)
  {
329 330
    error=test(net_real_write(net,(char*) net->buff,
			      (ulong) (net->write_pos - net->buff)));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
331 332
    net->write_pos=net->buff;
  }
333 334 335
  /* Sync packet number if using compression */
  if (net->compress)
    net->pkt_nr=net->compress_pkt_nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
336 337 338 339 340 341 342 343 344
  DBUG_RETURN(error);
}


/*****************************************************************************
** Write something to server/client buffer
*****************************************************************************/

/*
345 346 347 348 349 350
  Write a logical packet with packet header
  Format: Packet length (3 bytes), packet number(1 byte)
  When compression is used a 3 byte compression length is added

  NOTE
    If compression is used the original package is modified!
bk@work.mysql.com's avatar
bk@work.mysql.com committed
351 352
*/

353
my_bool
bk@work.mysql.com's avatar
bk@work.mysql.com committed
354 355 356
my_net_write(NET *net,const char *packet,ulong len)
{
  uchar buff[NET_HEADER_SIZE];
357
  if (unlikely(!net->vio)) /* nowhere to write */
358
    return 0;
359
  /*
360 361 362
    Big packets are handled by splitting them in packets of MAX_PACKET_LENGTH
    length. The last packet is always a packet that is < MAX_PACKET_LENGTH.
    (The last packet may even have a length of 0)
363
  */
364
  while (len >= MAX_PACKET_LENGTH)
365
  {
366
    const ulong z_size = MAX_PACKET_LENGTH;
367
    int3store(buff, z_size);
368
    buff[3]= (uchar) net->pkt_nr++;
369 370 371 372 373 374 375
    if (net_write_buff(net, (char*) buff, NET_HEADER_SIZE) ||
	net_write_buff(net, packet, z_size))
      return 1;
    packet += z_size;
    len-=     z_size;
  }
  /* Write last packet */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
376
  int3store(buff,len);
377
  buff[3]= (uchar) net->pkt_nr++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
378 379
  if (net_write_buff(net,(char*) buff,NET_HEADER_SIZE))
    return 1;
monty@mysql.com's avatar
monty@mysql.com committed
380
#ifndef DEBUG_DATA_PACKETS
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
381
  DBUG_DUMP("packet_header",(char*) buff,NET_HEADER_SIZE);
monty@mysql.com's avatar
monty@mysql.com committed
382
#endif
383
  return test(net_write_buff(net,packet,len));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
384 385
}

386 387
/*
  Send a command to the server.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

  SYNOPSIS
    net_write_command()
    net			NET handler
    command		Command in MySQL server (enum enum_server_command)
    header		Header to write after command
    head_len		Length of header
    packet		Query or parameter to query
    len			Length of packet

  DESCRIPTION
    The reason for having both header and packet is so that libmysql
    can easy add a header to a special command (like prepared statements)
    without having to re-alloc the string.

    As the command is part of the first data packet, we have to do some data
    juggling to put the command in there, without having to create a new
    packet.
    This function will split big packets into sub-packets if needed.
    (Each sub packet can only be 2^24 bytes)

  RETURN VALUES
    0	ok
    1	error
412 413
*/

414 415 416 417
my_bool
net_write_command(NET *net,uchar command,
		  const char *header, ulong head_len,
		  const char *packet, ulong len)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
418
{
419
  ulong length=len+1+head_len;			/* 1 extra byte for command */
420 421
  uchar buff[NET_HEADER_SIZE+1];
  uint header_size=NET_HEADER_SIZE+1;
422 423 424
  DBUG_ENTER("net_write_command");
  DBUG_PRINT("enter",("length: %lu", len));

425
  buff[4]=command;				/* For first packet */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
426

427
  if (length >= MAX_PACKET_LENGTH)
428 429
  {
    /* Take into account that we have the command in the first header */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
430
    len= MAX_PACKET_LENGTH - 1 - head_len;
431 432
    do
    {
433
      int3store(buff, MAX_PACKET_LENGTH);
434
      buff[3]= (uchar) net->pkt_nr++;
435
      if (net_write_buff(net,(char*) buff, header_size) ||
436 437
	  net_write_buff(net, header, head_len) ||
	  net_write_buff(net, packet, len))
438
	DBUG_RETURN(1);
439
      packet+= len;
440 441
      length-= MAX_PACKET_LENGTH;
      len= MAX_PACKET_LENGTH;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
442
      head_len= 0;
443 444
      header_size= NET_HEADER_SIZE;
    } while (length >= MAX_PACKET_LENGTH);
445 446
    len=length;					/* Data left to be written */
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
447
  int3store(buff,length);
448
  buff[3]= (uchar) net->pkt_nr++;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
449
  DBUG_RETURN(test(net_write_buff(net, (char*) buff, header_size) ||
450
	      (head_len && net_write_buff(net, (char*) header, head_len)) ||
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
451
	      net_write_buff(net, packet, len) || net_flush(net)));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
452 453
}

454 455
/*
  Caching the data in a local buffer before sending it.
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  SYNOPSIS
    net_write_buff()
    net		Network handler
    packet	Packet to send
    len		Length of packet

  DESCRIPTION
    Fill up net->buffer and send it to the client when full.

    If the rest of the to-be-sent-packet is bigger than buffer,
    send it in one big block (to avoid copying to internal buffer).
    If not, copy the rest of the data to the buffer and return without
    sending data.

  NOTES
    The cached buffer can be sent as it is with 'net_flush()'.

    In this code we have to be careful to not send a packet longer than
475 476
    MAX_PACKET_LENGTH to net_real_write() if we are using the compressed
    protocol as we store the length of the compressed packet in 3 bytes.
477 478 479 480

  RETURN
  0	ok
  1
481
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
482

483
static my_bool
484
net_write_buff(NET *net,const char *packet,ulong len)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
485
{
486 487 488 489 490
  ulong left_length;
  if (net->compress && net->max_packet > MAX_PACKET_LENGTH)
    left_length= MAX_PACKET_LENGTH - (net->write_pos - net->buff);
  else
    left_length= (ulong) (net->buff_end - net->write_pos);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
491

monty@mysql.com's avatar
monty@mysql.com committed
492 493 494
#ifdef DEBUG_DATA_PACKETS
  DBUG_DUMP("data", packet, len);
#endif
495
  if (len > left_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
496
  {
497
    if (net->write_pos != net->buff)
498
    {
499 500 501 502
      /* Fill up already used packet and write it */
      memcpy((char*) net->write_pos,packet,left_length);
      if (net_real_write(net,(char*) net->buff, 
			 (ulong) (net->write_pos - net->buff) + left_length))
503
	return 1;
504
      net->write_pos= net->buff;
505 506 507
      packet+= left_length;
      len-= left_length;
    }
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    if (net->compress)
    {
      /*
	We can't have bigger packets than 16M with compression
	Because the uncompressed length is stored in 3 bytes
      */
      left_length= MAX_PACKET_LENGTH;
      while (len > left_length)
      {
	if (net_real_write(net, packet, left_length))
	  return 1;
	packet+= left_length;
	len-= left_length;
      }
    }
    if (len > net->max_packet)
      return net_real_write(net, packet, len) ? 1 : 0;
    /* Send out rest of the blocks as full sized blocks */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
526 527
  }
  memcpy((char*) net->write_pos,packet,len);
528
  net->write_pos+= len;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
529 530 531
  return 0;
}

532 533 534 535 536

/*
  Read and write one packet using timeouts.
  If needed, the packet is compressed before sending.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
537 538 539 540

int
net_real_write(NET *net,const char *packet,ulong len)
{
541
  long int length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
542 543
  char *pos,*end;
  thr_alarm_t alarmed;
544
#ifndef NO_ALARM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
545 546 547 548 549 550
  ALARM alarm_buff;
#endif
  uint retry_count=0;
  my_bool net_blocking = vio_is_blocking(net->vio);
  DBUG_ENTER("net_real_write");

551 552
#if defined(MYSQL_SERVER) && defined(HAVE_QUERY_CACHE) \
                          && !defined(MYSQL_INSTANCE_MANAGER)
553 554
  if (net->query_cache_query != 0)
    query_cache_insert(net, packet, len);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
555 556
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
557 558 559 560 561 562 563 564 565 566
  if (net->error == 2)
    DBUG_RETURN(-1);				/* socket can't be used */

  net->reading_or_writing=2;
#ifdef HAVE_COMPRESS
  if (net->compress)
  {
    ulong complen;
    uchar *b;
    uint header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE;
567 568
    if (!(b=(uchar*) my_malloc((uint32) len + NET_HEADER_SIZE +
			       COMP_HEADER_SIZE, MYF(MY_WME))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
569 570
    {
#ifdef MYSQL_SERVER
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
571 572
      net->last_errno= ER_OUT_OF_RESOURCES;
      net->error= 2;
573
      /* TODO is it needed to set this variable if we have no socket */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
574
      net->report_error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
575
#endif
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
576
      net->reading_or_writing= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
577 578 579 580 581 582 583 584
      DBUG_RETURN(1);
    }
    memcpy(b+header_length,packet,len);

    if (my_compress((byte*) b+header_length,&len,&complen))
      complen=0;
    int3store(&b[NET_HEADER_SIZE],complen);
    int3store(b,len);
585
    b[3]=(uchar) (net->compress_pkt_nr++);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
586 587 588 589 590 591
    len+= header_length;
    packet= (char*) b;
  }
#endif /* HAVE_COMPRESS */

  /* DBUG_DUMP("net",packet,len); */
592
#ifndef NO_ALARM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
593 594
  thr_alarm_init(&alarmed);
  if (net_blocking)
595
    thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
596 597
#else
  alarmed=0;
598
  vio_timeout(net->vio, 1, net->write_timeout);
599
#endif /* NO_ALARM */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
600 601 602 603

  pos=(char*) packet; end=pos+len;
  while (pos != end)
  {
604
    if ((long) (length=vio_write(net->vio,pos,(uint32) (end-pos))) <= 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
605 606
    {
      my_bool interrupted = vio_should_retry(net->vio);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
607
#if (!defined(__WIN__) && !defined(__EMX__) && !defined(OS2))
608
      if ((interrupted || length==0) && !thr_alarm_in_use(&alarmed))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
609
      {
610
        if (!thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
611
        {                                       /* Always true for client */
612 613
	  my_bool old_mode;
	  while (vio_blocking(net->vio, TRUE, &old_mode) < 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
614
	  {
615
	    if (vio_should_retry(net->vio) && retry_count++ < net->retry_count)
616
	      continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
617
#ifdef EXTRA_DEBUG
618 619 620
	    fprintf(stderr,
		    "%s: my_net_write: fcntl returned error %d, aborting thread\n",
		    my_progname,vio_errno(net->vio));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
621
#endif /* EXTRA_DEBUG */
622
#ifdef MYSQL_SERVER	    
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
623
	    net->last_errno= ER_NET_ERROR_ON_WRITE;
624
#endif
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
625 626
	    net->error= 2;                     /* Close socket */
            net->report_error= 1;
627
	    goto end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
628 629 630 631 632 633 634
	  }
	  retry_count=0;
	  continue;
	}
      }
      else
#endif /* (!defined(__WIN__) && !defined(__EMX__)) */
635
	if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
636 637
	    interrupted)
      {
638
	if (retry_count++ < net->retry_count)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
639 640 641 642 643 644 645
	    continue;
#ifdef EXTRA_DEBUG
	  fprintf(stderr, "%s: write looped, aborting thread\n",
		  my_progname);
#endif /* EXTRA_DEBUG */
      }
#if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
646
      if (vio_errno(net->vio) == SOCKET_EINTR)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
647 648 649 650 651
      {
	DBUG_PRINT("warning",("Interrupted write. Retrying..."));
	continue;
      }
#endif /* defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
652 653
      net->error= 2;				/* Close socket */
      net->report_error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
654 655 656 657 658 659 660
#ifdef MYSQL_SERVER
      net->last_errno= (interrupted ? ER_NET_WRITE_INTERRUPTED :
			ER_NET_ERROR_ON_WRITE);
#endif /* MYSQL_SERVER */
      break;
    }
    pos+=length;
661
    update_statistics(thd_increment_bytes_sent(length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
662 663 664 665 666 667 668 669
  }
#ifndef __WIN__
 end:
#endif
#ifdef HAVE_COMPRESS
  if (net->compress)
    my_free((char*) packet,MYF(0));
#endif
670
  if (thr_alarm_in_use(&alarmed))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
671
  {
672
    my_bool old_mode;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
673
    thr_end_alarm(&alarmed);
674
    vio_blocking(net->vio, net_blocking, &old_mode);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
675 676 677 678 679 680 681 682 683 684
  }
  net->reading_or_writing=0;
  DBUG_RETURN(((int) (pos != end)));
}


/*****************************************************************************
** Read something from server/clinet
*****************************************************************************/

685
#ifndef NO_ALARM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
686

687 688
static my_bool net_safe_read(NET *net, char *buff, uint32 length,
			     thr_alarm_t *alarmed)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
689 690
{
  uint retry_count=0;
691
  while (length > 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
692
  {
693 694
    int tmp;
    if ((tmp=vio_read(net->vio,(char*) net->buff, length)) <= 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
695 696
    {
      my_bool interrupted = vio_should_retry(net->vio);
697
      if (!thr_got_alarm(alarmed) && interrupted)
698
      {					/* Probably in MIT threads */
699
	if (retry_count++ < net->retry_count)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
700 701
	  continue;
      }
702
      return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
703
    }
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    length-= tmp;
  }
  return 0;
}

/*
  Help function to clear the commuication buffer when we get a too big packet.

  SYNOPSIS
    my_net_skip_rest()
    net		Communication handle
    remain	Bytes to read
    alarmed	Parameter for thr_alarm()
    alarm_buff	Parameter for thr_alarm()

  RETURN VALUES
   0	Was able to read the whole packet
   1	Got mailformed packet from client
*/

static my_bool my_net_skip_rest(NET *net, uint32 remain, thr_alarm_t *alarmed,
				ALARM *alarm_buff)
{
  uint32 old=remain;
  DBUG_ENTER("my_net_skip_rest");
  DBUG_PRINT("enter",("bytes_to_skip: %u", (uint) remain));

731
  /* The following is good for debugging */
732
  update_statistics(thd_increment_net_big_packet_count(1));
733

734
  if (!thr_alarm_in_use(alarmed))
735 736
  {
    my_bool old_mode;
737
    if (thr_alarm(alarmed,net->read_timeout, alarm_buff) ||
738 739 740 741 742 743
	vio_blocking(net->vio, TRUE, &old_mode) < 0)
      DBUG_RETURN(1);				/* Can't setup, abort */
  }
  for (;;)
  {
    while (remain > 0)
744
    {
745 746 747
      uint length= min(remain, net->max_packet);
      if (net_safe_read(net, (char*) net->buff, length, alarmed))
	DBUG_RETURN(1);
748
      update_statistics(thd_increment_bytes_received(length));
749
      remain -= (uint32) length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
750
    }
751 752 753 754 755 756
    if (old != MAX_PACKET_LENGTH)
      break;
    if (net_safe_read(net, (char*) net->buff, NET_HEADER_SIZE, alarmed))
      DBUG_RETURN(1);
    old=remain= uint3korr(net->buff);
    net->pkt_nr++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
757
  }
758
  DBUG_RETURN(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
759
}
760
#endif /* NO_ALARM */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
761 762


763 764 765 766 767 768
/*
  Reads one packet to net->buff + net->where_b
  Returns length of packet.  Long packets are handled by my_net_read().
  This function reallocates the net->buff buffer if necessary.
*/

769
static ulong
bk@work.mysql.com's avatar
bk@work.mysql.com committed
770 771 772 773 774 775 776
my_real_read(NET *net, ulong *complen)
{
  uchar *pos;
  long length;
  uint i,retry_count=0;
  ulong len=packet_error;
  thr_alarm_t alarmed;
777
#ifndef NO_ALARM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
778 779 780
  ALARM alarm_buff;
#endif
  my_bool net_blocking=vio_is_blocking(net->vio);
781 782
  uint32 remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
		  NET_HEADER_SIZE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
783 784 785 786
  *complen = 0;

  net->reading_or_writing=1;
  thr_alarm_init(&alarmed);
787
#ifndef NO_ALARM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
788
  if (net_blocking)
789
    thr_alarm(&alarmed,net->read_timeout,&alarm_buff);
790
#else
791
  vio_timeout(net->vio, 0, net->read_timeout);
792
#endif /* NO_ALARM */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
793 794 795 796 797 798 799 800 801 802 803 804 805

    pos = net->buff + net->where_b;		/* net->packet -4 */
    for (i=0 ; i < 2 ; i++)
    {
      while (remain > 0)
      {
	/* First read is done with non blocking mode */
        if ((int) (length=vio_read(net->vio,(char*) pos,remain)) <= 0L)
        {
          my_bool interrupted = vio_should_retry(net->vio);

	  DBUG_PRINT("info",("vio_read returned %d,  errno: %d",
			     length, vio_errno(net->vio)));
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
806
#if (!defined(__WIN__) && !defined(__EMX__) && !defined(OS2)) || defined(MYSQL_SERVER)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
807 808 809 810 811
	  /*
	    We got an error that there was no data on the socket. We now set up
	    an alarm to not 'read forever', change the socket to non blocking
	    mode and try again
	  */
812
	  if ((interrupted || length == 0) && !thr_alarm_in_use(&alarmed))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
813
	  {
814
	    if (!thr_alarm(&alarmed,net->read_timeout,&alarm_buff)) /* Don't wait too long */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
815
	    {
816 817 818 819
	      my_bool old_mode;
	      while (vio_blocking(net->vio, TRUE, &old_mode) < 0)
	      {
		if (vio_should_retry(net->vio) &&
820
		    retry_count++ < net->retry_count)
821 822 823 824
		  continue;
		DBUG_PRINT("error",
			   ("fcntl returned error %d, aborting thread",
			    vio_errno(net->vio)));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
825
#ifdef EXTRA_DEBUG
826 827 828
		fprintf(stderr,
			"%s: read: fcntl returned error %d, aborting thread\n",
			my_progname,vio_errno(net->vio));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
829
#endif /* EXTRA_DEBUG */
830
		len= packet_error;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
831 832
		net->error= 2;                 /* Close socket */
	        net->report_error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
833
#ifdef MYSQL_SERVER
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
834
		net->last_errno= ER_NET_FCNTL_ERROR;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
835
#endif
836 837
		goto end;
	      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
838 839 840 841 842
	      retry_count=0;
	      continue;
	    }
	  }
#endif /* (!defined(__WIN__) && !defined(__EMX__)) || defined(MYSQL_SERVER) */
843
	  if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
844 845
	      interrupted)
	  {					/* Probably in MIT threads */
846
	    if (retry_count++ < net->retry_count)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
847 848 849 850 851 852 853 854 855 856 857 858 859
	      continue;
#ifdef EXTRA_DEBUG
	    fprintf(stderr, "%s: read looped with error %d, aborting thread\n",
		    my_progname,vio_errno(net->vio));
#endif /* EXTRA_DEBUG */
	  }
#if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER)
	  if (vio_should_retry(net->vio))
	  {
	    DBUG_PRINT("warning",("Interrupted read. Retrying..."));
	    continue;
	  }
#endif
860 861
	  DBUG_PRINT("error",("Couldn't read packet: remain: %u  errno: %d  length: %ld",
			      remain, vio_errno(net->vio), length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
862
	  len= packet_error;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
863 864
	  net->error= 2;				/* Close socket */
	  net->report_error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
865
#ifdef MYSQL_SERVER
866
	  net->last_errno= (vio_was_interrupted(net->vio) ? ER_NET_READ_INTERRUPTED :
bk@work.mysql.com's avatar
bk@work.mysql.com committed
867 868 869 870
			    ER_NET_READ_ERROR);
#endif
	  goto end;
	}
871
	remain -= (uint32) length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
872
	pos+= (ulong) length;
873
	update_statistics(thd_increment_bytes_received(length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
874 875 876 877
      }
      if (i == 0)
      {					/* First parts is packet length */
	ulong helping;
monty@mysql.com's avatar
monty@mysql.com committed
878 879
        DBUG_DUMP("packet_header",(char*) net->buff+net->where_b,
                  NET_HEADER_SIZE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
880 881 882 883 884
	if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr)
	{
	  if (net->buff[net->where_b] != (uchar) 255)
	  {
	    DBUG_PRINT("error",
885
		       ("Packets out of order (Found: %d, expected %u)",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
886
			(int) net->buff[net->where_b + 3],
887
			net->pkt_nr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
888 889 890 891 892 893 894
#ifdef EXTRA_DEBUG
	    fprintf(stderr,"Packets out of order (Found: %d, expected %d)\n",
		    (int) net->buff[net->where_b + 3],
		    (uint) (uchar) net->pkt_nr);
#endif
	  }
	  len= packet_error;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
895
	  net->report_error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
896 897 898 899 900
#ifdef MYSQL_SERVER
	  net->last_errno=ER_NET_PACKETS_OUT_OF_ORDER;
#endif
	  goto end;
	}
901
	net->compress_pkt_nr= ++net->pkt_nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
902 903 904
#ifdef HAVE_COMPRESS
	if (net->compress)
	{
905 906 907 908
	  /*
	    If the packet is compressed then complen > 0 and contains the
	    number of bytes in the uncompressed packet
	  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
909 910 911 912 913
	  *complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE]));
	}
#endif

	len=uint3korr(net->buff+net->where_b);
914 915
	if (!len)				/* End of big multi-packet */
	  goto end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
916 917 918 919
	helping = max(len,*complen) + net->where_b;
	/* The necessary size of net->buff */
	if (helping >= net->max_packet)
	{
920
	  if (net_realloc(net,helping))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
921
	  {
922 923 924 925
#if defined(MYSQL_SERVER) && !defined(NO_ALARM)
	    if (!net->compress &&
		!my_net_skip_rest(net, (uint32) len, &alarmed, &alarm_buff))
	      net->error= 3;		/* Successfully skiped packet */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
926
#endif
927
	    len= packet_error;          /* Return error and close connection */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
928 929 930 931
	    goto end;
	  }
	}
	pos=net->buff + net->where_b;
932
	remain = (uint32) len;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
933 934 935 936
      }
    }

end:
937
  if (thr_alarm_in_use(&alarmed))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
938
  {
939
    my_bool old_mode;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
940
    thr_end_alarm(&alarmed);
941
    vio_blocking(net->vio, net_blocking, &old_mode);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
942 943
  }
  net->reading_or_writing=0;
monty@mysql.com's avatar
monty@mysql.com committed
944 945 946 947
#ifdef DEBUG_DATA_PACKETS
  if (len != packet_error)
    DBUG_DUMP("data",(char*) net->buff+net->where_b, len);
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
948 949 950
  return(len);
}

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965

/*
  Read a packet from the client/server and return it without the internal
  package header.
  If the packet is the first packet of a multi-packet packet
  (which is indicated by the length of the packet = 0xffffff) then
  all sub packets are read and concatenated.
  If the packet was compressed, its uncompressed and the length of the
  uncompressed packet is returned.

  The function returns the length of the found packet or packet_error.
  net->read_pos points to the read data.
*/

ulong
bk@work.mysql.com's avatar
bk@work.mysql.com committed
966 967 968 969 970 971 972 973
my_net_read(NET *net)
{
  ulong len,complen;

#ifdef HAVE_COMPRESS
  if (!net->compress)
  {
#endif
974
    len = my_real_read(net,&complen);
975
    if (len == MAX_PACKET_LENGTH)
976 977
    {
      /* First packet of a multi-packet.  Concatenate the packets */
978
      ulong save_pos = net->where_b;
979 980 981 982 983
      ulong total_length=0;
      do
      {
	net->where_b += len;
	total_length += len;
984
	len = my_real_read(net,&complen);
985
      } while (len == MAX_PACKET_LENGTH);
986 987 988 989
      if (len != packet_error)
	len+= total_length;
      net->where_b = save_pos;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
990 991 992 993 994 995
    net->read_pos = net->buff + net->where_b;
    if (len != packet_error)
      net->read_pos[len]=0;		/* Safeguard for mysql_use_result */
    return len;
#ifdef HAVE_COMPRESS
  }
996
  else
bk@work.mysql.com's avatar
bk@work.mysql.com committed
997
  {
998 999
    /* We are using the compressed protocol */

1000 1001 1002
    ulong buf_length;
    ulong start_of_packet;
    ulong first_packet_offset;
1003 1004
    uint read_length, multi_byte_packet=0;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1005 1006
    if (net->remain_in_buf)
    {
1007
      buf_length= net->buf_length;		/* Data left in old packet */
1008 1009
      first_packet_offset= start_of_packet= (net->buf_length -
					     net->remain_in_buf);
1010
      /* Restore the character that was overwritten by the end 0 */
1011
      net->buff[start_of_packet]= net->save_char;
1012 1013 1014
    }
    else
    {
1015
      /* reuse buffer, as there is nothing in it that we need */
1016
      buf_length= start_of_packet= first_packet_offset= 0;
1017 1018 1019 1020
    }
    for (;;)
    {
      ulong packet_len;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1021

1022
      if (buf_length - start_of_packet >= NET_HEADER_SIZE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1023
      {
1024 1025 1026 1027 1028 1029 1030 1031
	read_length = uint3korr(net->buff+start_of_packet);
	if (!read_length)
	{ 
	  /* End of multi-byte packet */
	  start_of_packet += NET_HEADER_SIZE;
	  break;
	}
	if (read_length + NET_HEADER_SIZE <= buf_length - start_of_packet)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1032
	{
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	  if (multi_byte_packet)
	  {
	    /* Remove packet header for second packet */
	    memmove(net->buff + first_packet_offset + start_of_packet,
		    net->buff + first_packet_offset + start_of_packet +
		    NET_HEADER_SIZE,
		    buf_length - start_of_packet);
	    start_of_packet += read_length;
	    buf_length -= NET_HEADER_SIZE;
	  }
	  else
	    start_of_packet+= read_length + NET_HEADER_SIZE;

1046
	  if (read_length != MAX_PACKET_LENGTH)	/* last package */
1047
	  {
1048
	    multi_byte_packet= 0;		/* No last zero len packet */
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	    break;
	  }
	  multi_byte_packet= NET_HEADER_SIZE;
	  /* Move data down to read next data packet after current one */
	  if (first_packet_offset)
	  {
	    memmove(net->buff,net->buff+first_packet_offset,
		    buf_length-first_packet_offset);
	    buf_length-=first_packet_offset;
	    start_of_packet -= first_packet_offset;
	    first_packet_offset=0;
	  }
	  continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1062 1063 1064
	}
      }
      /* Move data down to read next data packet after current one */
1065
      if (first_packet_offset)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1066
      {
1067 1068 1069 1070 1071
	memmove(net->buff,net->buff+first_packet_offset,
		buf_length-first_packet_offset);
	buf_length-=first_packet_offset;
	start_of_packet -= first_packet_offset;
	first_packet_offset=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1072 1073
      }

1074 1075 1076 1077 1078 1079
      net->where_b=buf_length;
      if ((packet_len = my_real_read(net,&complen)) == packet_error)
	return packet_error;
      if (my_uncompress((byte*) net->buff + net->where_b, &packet_len,
			&complen))
      {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1080 1081
	net->error= 2;			/* caller will close socket */
	net->report_error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1082
#ifdef MYSQL_SERVER
1083
	net->last_errno=ER_NET_UNCOMPRESS_ERROR;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1084
#endif
1085 1086 1087
	return packet_error;
      }
      buf_length+=packet_len;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1088
    }
1089 1090 1091

    net->read_pos=      net->buff+ first_packet_offset + NET_HEADER_SIZE;
    net->buf_length=    buf_length;
1092 1093
    net->remain_in_buf= (ulong) (buf_length - start_of_packet);
    len = ((ulong) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
1094
           multi_byte_packet);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1095 1096 1097
    net->save_char= net->read_pos[len];	/* Must be saved */
    net->read_pos[len]=0;		/* Safeguard for mysql_use_result */
  }
1098
#endif /* HAVE_COMPRESS */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1099 1100
  return len;
}
1101