net_serv.cc 27.4 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB
2

unknown's avatar
unknown 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

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

unknown's avatar
unknown 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 */
unknown's avatar
unknown 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.
*/

unknown's avatar
unknown committed
24 25
/*
  Write and read of logical packets to/from socket
unknown's avatar
unknown committed
26

unknown's avatar
unknown committed
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.
unknown's avatar
unknown committed
34
*/
35

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

unknown's avatar
SCRUM  
unknown committed
55
#ifdef EMBEDDED_LIBRARY
56
#undef MYSQL_SERVER
57
#undef MYSQL_CLIENT
58
#define MYSQL_CLIENT
unknown's avatar
SCRUM  
unknown committed
59 60 61
#endif /*EMBEDDED_LIBRARY */


unknown's avatar
unknown committed
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.
*/

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

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

#include "thr_alarm.h"
unknown's avatar
unknown committed
84

unknown's avatar
unknown committed
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.
*/
unknown's avatar
unknown committed
91
extern uint test_flags;
92
extern ulong bytes_sent, bytes_received, net_big_packet_count;
unknown's avatar
unknown committed
93
extern pthread_mutex_t LOCK_bytes_sent , LOCK_bytes_received;
unknown's avatar
unknown committed
94
#ifndef MYSQL_INSTANCE_MANAGER
95
extern void query_cache_insert(NET *net, const char *packet, ulong length);
unknown's avatar
unknown committed
96
#define USE_QUERY_CACHE
97
#define update_statistics(A) A
unknown's avatar
unknown committed
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)
unknown's avatar
unknown committed
103
#define thd_increment_bytes_sent(N)
unknown's avatar
unknown committed
104 105
#endif

unknown's avatar
unknown committed
106
#define TEST_BLOCKING		8
107
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
unknown's avatar
unknown committed
108

109
static my_bool net_write_buff(NET *net,const char *packet,ulong len);
unknown's avatar
unknown committed
110 111 112 113


	/* Init with packet info */

114
my_bool my_net_init(NET *net, Vio* vio)
unknown's avatar
unknown committed
115
{
116
  DBUG_ENTER("my_net_init");
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
122
  net->buff_end=net->buff+net->max_packet;
unknown's avatar
unknown committed
123 124
  net->vio = vio;
  net->no_send_ok = 0;
125
  net->no_send_eof = 0;
unknown's avatar
unknown committed
126
  net->error=0; net->return_errno=0; net->return_status=0;
unknown's avatar
unknown committed
127
  net->pkt_nr=net->compress_pkt_nr=0;
unknown's avatar
unknown committed
128 129 130 131 132
  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;
unknown's avatar
unknown committed
133
  net->query_cache_query=0;
unknown's avatar
unknown committed
134
  net->report_error= 0;
unknown's avatar
unknown committed
135 136 137 138

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

unknown's avatar
unknown committed
151

unknown's avatar
unknown committed
152 153
void net_end(NET *net)
{
154
  DBUG_ENTER("net_end");
unknown's avatar
unknown committed
155 156
  my_free((gptr) net->buff,MYF(MY_ALLOW_ZERO_PTR));
  net->buff=0;
157
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
158 159
}

unknown's avatar
unknown committed
160

unknown's avatar
unknown committed
161 162
/* Realloc the packet buffer */

163
my_bool net_realloc(NET *net, ulong length)
unknown's avatar
unknown committed
164 165 166
{
  uchar *buff;
  ulong pkt_length;
167 168 169
  DBUG_ENTER("net_realloc");
  DBUG_PRINT("enter",("length: %lu", length));

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

	/* Remove unwanted characters from connection */

void net_clear(NET *net)
{
unknown's avatar
unknown committed
202
  DBUG_ENTER("net_clear");
203
#if !defined(EXTRA_DEBUG) && !defined(EMBEDDED_LIBRARY)
unknown's avatar
unknown committed
204
  {
unknown's avatar
unknown committed
205 206 207 208 209 210 211 212 213 214
    int count;					/* One may get 'unused' warn */
    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);
    }
unknown's avatar
unknown committed
215 216
  }
#endif /* EXTRA_DEBUG */
unknown's avatar
unknown committed
217
  net->pkt_nr=net->compress_pkt_nr=0;		/* Ready for new command */
unknown's avatar
unknown committed
218
  net->write_pos=net->buff;
unknown's avatar
unknown committed
219
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
220 221 222 223
}

	/* Flush write_buffer if not empty. */

224
my_bool net_flush(NET *net)
unknown's avatar
unknown committed
225
{
226
  my_bool error= 0;
unknown's avatar
unknown committed
227 228 229
  DBUG_ENTER("net_flush");
  if (net->buff != net->write_pos)
  {
230 231
    error=test(net_real_write(net,(char*) net->buff,
			      (ulong) (net->write_pos - net->buff)));
unknown's avatar
unknown committed
232 233
    net->write_pos=net->buff;
  }
unknown's avatar
unknown committed
234 235 236
  /* Sync packet number if using compression */
  if (net->compress)
    net->pkt_nr=net->compress_pkt_nr;
unknown's avatar
unknown committed
237 238 239 240 241 242 243 244 245
  DBUG_RETURN(error);
}


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

/*
unknown's avatar
unknown committed
246 247 248 249 250 251
  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!
unknown's avatar
unknown committed
252 253
*/

254
my_bool
unknown's avatar
unknown committed
255 256 257
my_net_write(NET *net,const char *packet,ulong len)
{
  uchar buff[NET_HEADER_SIZE];
258
  /*
259 260 261
    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)
262
  */
263
  while (len >= MAX_PACKET_LENGTH)
264
  {
265
    const ulong z_size = MAX_PACKET_LENGTH;
266
    int3store(buff, z_size);
unknown's avatar
unknown committed
267
    buff[3]= (uchar) net->pkt_nr++;
268 269 270 271 272 273 274
    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 */
unknown's avatar
unknown committed
275
  int3store(buff,len);
unknown's avatar
unknown committed
276
  buff[3]= (uchar) net->pkt_nr++;
unknown's avatar
unknown committed
277 278
  if (net_write_buff(net,(char*) buff,NET_HEADER_SIZE))
    return 1;
unknown's avatar
unknown committed
279
#ifndef DEBUG_DATA_PACKETS
unknown's avatar
unknown committed
280
  DBUG_DUMP("packet_header",(char*) buff,NET_HEADER_SIZE);
unknown's avatar
unknown committed
281
#endif
282
  return test(net_write_buff(net,packet,len));
unknown's avatar
unknown committed
283 284
}

285 286
/*
  Send a command to the server.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

  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
311 312
*/

313 314 315 316
my_bool
net_write_command(NET *net,uchar command,
		  const char *header, ulong head_len,
		  const char *packet, ulong len)
unknown's avatar
unknown committed
317
{
318
  ulong length=len+1+head_len;			/* 1 extra byte for command */
319 320
  uchar buff[NET_HEADER_SIZE+1];
  uint header_size=NET_HEADER_SIZE+1;
321 322 323
  DBUG_ENTER("net_write_command");
  DBUG_PRINT("enter",("length: %lu", len));

324
  buff[4]=command;				/* For first packet */
unknown's avatar
unknown committed
325

326
  if (length >= MAX_PACKET_LENGTH)
327 328
  {
    /* Take into account that we have the command in the first header */
unknown's avatar
unknown committed
329
    len= MAX_PACKET_LENGTH - 1 - head_len;
330 331
    do
    {
332
      int3store(buff, MAX_PACKET_LENGTH);
unknown's avatar
unknown committed
333
      buff[3]= (uchar) net->pkt_nr++;
334
      if (net_write_buff(net,(char*) buff, header_size) ||
335 336
	  net_write_buff(net, header, head_len) ||
	  net_write_buff(net, packet, len))
337
	DBUG_RETURN(1);
338
      packet+= len;
339 340
      length-= MAX_PACKET_LENGTH;
      len= MAX_PACKET_LENGTH;
unknown's avatar
unknown committed
341
      head_len= 0;
342 343
      header_size= NET_HEADER_SIZE;
    } while (length >= MAX_PACKET_LENGTH);
344 345
    len=length;					/* Data left to be written */
  }
unknown's avatar
unknown committed
346
  int3store(buff,length);
unknown's avatar
unknown committed
347
  buff[3]= (uchar) net->pkt_nr++;
unknown's avatar
unknown committed
348
  DBUG_RETURN(test(net_write_buff(net, (char*) buff, header_size) ||
349
	      (head_len && net_write_buff(net, (char*) header, head_len)) ||
unknown's avatar
unknown committed
350
	      net_write_buff(net, packet, len) || net_flush(net)));
unknown's avatar
unknown committed
351 352
}

353 354
/*
  Caching the data in a local buffer before sending it.
355

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  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
unknown's avatar
unknown committed
374 375
    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.
376 377 378 379

  RETURN
  0	ok
  1
380
*/
unknown's avatar
unknown committed
381

382
static my_bool
383
net_write_buff(NET *net,const char *packet,ulong len)
unknown's avatar
unknown committed
384
{
385 386 387 388 389
  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);
unknown's avatar
unknown committed
390

unknown's avatar
unknown committed
391 392 393
#ifdef DEBUG_DATA_PACKETS
  DBUG_DUMP("data", packet, len);
#endif
394
  if (len > left_length)
unknown's avatar
unknown committed
395
  {
396
    if (net->write_pos != net->buff)
397
    {
398 399 400 401
      /* 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))
402
	return 1;
403
      net->write_pos= net->buff;
404 405 406
      packet+= left_length;
      len-= left_length;
    }
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    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 */
unknown's avatar
unknown committed
425 426
  }
  memcpy((char*) net->write_pos,packet,len);
427
  net->write_pos+= len;
unknown's avatar
unknown committed
428 429 430
  return 0;
}

431 432 433 434 435

/*
  Read and write one packet using timeouts.
  If needed, the packet is compressed before sending.
*/
unknown's avatar
unknown committed
436 437 438 439

int
net_real_write(NET *net,const char *packet,ulong len)
{
unknown's avatar
unknown committed
440
  long int length;
unknown's avatar
unknown committed
441 442
  char *pos,*end;
  thr_alarm_t alarmed;
unknown's avatar
unknown committed
443
#ifndef NO_ALARM
unknown's avatar
unknown committed
444 445 446 447 448 449
  ALARM alarm_buff;
#endif
  uint retry_count=0;
  my_bool net_blocking = vio_is_blocking(net->vio);
  DBUG_ENTER("net_real_write");

unknown's avatar
unknown committed
450 451
#if defined(MYSQL_SERVER) && defined(HAVE_QUERY_CACHE) \
                          && !defined(MYSQL_INSTANCE_MANAGER)
452 453
  if (net->query_cache_query != 0)
    query_cache_insert(net, packet, len);
unknown's avatar
unknown committed
454 455
#endif

unknown's avatar
unknown committed
456 457 458 459 460 461 462 463 464 465
  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;
unknown's avatar
unknown committed
466 467
    if (!(b=(uchar*) my_malloc((uint32) len + NET_HEADER_SIZE +
			       COMP_HEADER_SIZE, MYF(MY_WME))))
unknown's avatar
unknown committed
468 469
    {
#ifdef MYSQL_SERVER
unknown's avatar
unknown committed
470 471
      net->last_errno= ER_OUT_OF_RESOURCES;
      net->error= 2;
472
      /* TODO is it needed to set this variable if we have no socket */
unknown's avatar
unknown committed
473
      net->report_error= 1;
unknown's avatar
unknown committed
474
#endif
unknown's avatar
unknown committed
475
      net->reading_or_writing= 0;
unknown's avatar
unknown committed
476 477 478 479 480 481 482 483
      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);
unknown's avatar
unknown committed
484
    b[3]=(uchar) (net->compress_pkt_nr++);
unknown's avatar
unknown committed
485 486 487 488 489 490
    len+= header_length;
    packet= (char*) b;
  }
#endif /* HAVE_COMPRESS */

  /* DBUG_DUMP("net",packet,len); */
unknown's avatar
unknown committed
491
#ifndef NO_ALARM
unknown's avatar
unknown committed
492 493
  thr_alarm_init(&alarmed);
  if (net_blocking)
unknown's avatar
unknown committed
494
    thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff);
unknown's avatar
unknown committed
495 496
#else
  alarmed=0;
497
  vio_timeout(net->vio, net->write_timeout);
unknown's avatar
unknown committed
498
#endif /* NO_ALARM */
unknown's avatar
unknown committed
499 500 501 502

  pos=(char*) packet; end=pos+len;
  while (pos != end)
  {
unknown's avatar
unknown committed
503
    if ((long) (length=vio_write(net->vio,pos,(uint32) (end-pos))) <= 0)
unknown's avatar
unknown committed
504 505
    {
      my_bool interrupted = vio_should_retry(net->vio);
unknown's avatar
unknown committed
506
#if (!defined(__WIN__) && !defined(__EMX__) && !defined(OS2))
507
      if ((interrupted || length==0) && !thr_alarm_in_use(&alarmed))
unknown's avatar
unknown committed
508
      {
unknown's avatar
unknown committed
509
        if (!thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff))
unknown's avatar
unknown committed
510
        {                                       /* Always true for client */
511 512
	  my_bool old_mode;
	  while (vio_blocking(net->vio, TRUE, &old_mode) < 0)
unknown's avatar
unknown committed
513
	  {
514
	    if (vio_should_retry(net->vio) && retry_count++ < net->retry_count)
515
	      continue;
unknown's avatar
unknown committed
516
#ifdef EXTRA_DEBUG
517 518 519
	    fprintf(stderr,
		    "%s: my_net_write: fcntl returned error %d, aborting thread\n",
		    my_progname,vio_errno(net->vio));
unknown's avatar
unknown committed
520
#endif /* EXTRA_DEBUG */
521
#ifdef MYSQL_SERVER	    
unknown's avatar
unknown committed
522
	    net->last_errno= ER_NET_ERROR_ON_WRITE;
523
#endif
unknown's avatar
unknown committed
524 525
	    net->error= 2;                     /* Close socket */
            net->report_error= 1;
526
	    goto end;
unknown's avatar
unknown committed
527 528 529 530 531 532 533
	  }
	  retry_count=0;
	  continue;
	}
      }
      else
#endif /* (!defined(__WIN__) && !defined(__EMX__)) */
534
	if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) &&
unknown's avatar
unknown committed
535 536
	    interrupted)
      {
537
	if (retry_count++ < net->retry_count)
unknown's avatar
unknown committed
538 539 540 541 542 543 544
	    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)
unknown's avatar
unknown committed
545
      if (vio_errno(net->vio) == SOCKET_EINTR)
unknown's avatar
unknown committed
546 547 548 549 550
      {
	DBUG_PRINT("warning",("Interrupted write. Retrying..."));
	continue;
      }
#endif /* defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) */
unknown's avatar
unknown committed
551 552
      net->error= 2;				/* Close socket */
      net->report_error= 1;
unknown's avatar
unknown committed
553 554 555 556 557 558 559
#ifdef MYSQL_SERVER
      net->last_errno= (interrupted ? ER_NET_WRITE_INTERRUPTED :
			ER_NET_ERROR_ON_WRITE);
#endif /* MYSQL_SERVER */
      break;
    }
    pos+=length;
560
    update_statistics(thd_increment_bytes_sent(length));
unknown's avatar
unknown committed
561 562 563 564 565 566 567 568
  }
#ifndef __WIN__
 end:
#endif
#ifdef HAVE_COMPRESS
  if (net->compress)
    my_free((char*) packet,MYF(0));
#endif
569
  if (thr_alarm_in_use(&alarmed))
unknown's avatar
unknown committed
570
  {
571
    my_bool old_mode;
unknown's avatar
unknown committed
572
    thr_end_alarm(&alarmed);
573
    vio_blocking(net->vio, net_blocking, &old_mode);
unknown's avatar
unknown committed
574 575 576 577 578 579 580 581 582 583
  }
  net->reading_or_writing=0;
  DBUG_RETURN(((int) (pos != end)));
}


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

unknown's avatar
unknown committed
584
#ifndef NO_ALARM
unknown's avatar
unknown committed
585

586 587
static my_bool net_safe_read(NET *net, char *buff, uint32 length,
			     thr_alarm_t *alarmed)
unknown's avatar
unknown committed
588 589
{
  uint retry_count=0;
590
  while (length > 0)
unknown's avatar
unknown committed
591
  {
592 593
    int tmp;
    if ((tmp=vio_read(net->vio,(char*) net->buff, length)) <= 0)
unknown's avatar
unknown committed
594 595
    {
      my_bool interrupted = vio_should_retry(net->vio);
596
      if (!thr_got_alarm(alarmed) && interrupted)
unknown's avatar
unknown committed
597
      {					/* Probably in MIT threads */
598
	if (retry_count++ < net->retry_count)
unknown's avatar
unknown committed
599 600
	  continue;
      }
601
      return 1;
unknown's avatar
unknown committed
602
    }
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    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));

630
  /* The following is good for debugging */
631
  update_statistics(thd_increment_net_big_packet_count(1));
632

633
  if (!thr_alarm_in_use(alarmed))
634 635
  {
    my_bool old_mode;
unknown's avatar
unknown committed
636
    if (thr_alarm(alarmed,net->read_timeout, alarm_buff) ||
637 638 639 640 641 642
	vio_blocking(net->vio, TRUE, &old_mode) < 0)
      DBUG_RETURN(1);				/* Can't setup, abort */
  }
  for (;;)
  {
    while (remain > 0)
643
    {
644 645 646
      uint length= min(remain, net->max_packet);
      if (net_safe_read(net, (char*) net->buff, length, alarmed))
	DBUG_RETURN(1);
647
      update_statistics(thd_increment_bytes_received(length));
648
      remain -= (uint32) length;
unknown's avatar
unknown committed
649
    }
650 651 652 653 654 655
    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++;
unknown's avatar
unknown committed
656
  }
657
  DBUG_RETURN(0);
unknown's avatar
unknown committed
658
}
unknown's avatar
unknown committed
659
#endif /* NO_ALARM */
unknown's avatar
unknown committed
660 661


662 663 664 665 666 667
/*
  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.
*/

unknown's avatar
unknown committed
668
static ulong
unknown's avatar
unknown committed
669 670 671 672 673 674 675
my_real_read(NET *net, ulong *complen)
{
  uchar *pos;
  long length;
  uint i,retry_count=0;
  ulong len=packet_error;
  thr_alarm_t alarmed;
unknown's avatar
unknown committed
676
#ifndef NO_ALARM
unknown's avatar
unknown committed
677 678 679
  ALARM alarm_buff;
#endif
  my_bool net_blocking=vio_is_blocking(net->vio);
unknown's avatar
unknown committed
680 681
  uint32 remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
		  NET_HEADER_SIZE);
unknown's avatar
unknown committed
682 683 684 685
  *complen = 0;

  net->reading_or_writing=1;
  thr_alarm_init(&alarmed);
unknown's avatar
unknown committed
686
#ifndef NO_ALARM
unknown's avatar
unknown committed
687
  if (net_blocking)
unknown's avatar
unknown committed
688
    thr_alarm(&alarmed,net->read_timeout,&alarm_buff);
689 690
#else
  vio_timeout(net->vio, net->read_timeout);
unknown's avatar
unknown committed
691
#endif /* NO_ALARM */
unknown's avatar
unknown committed
692 693 694 695 696 697 698 699 700 701 702 703 704

    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)));
unknown's avatar
unknown committed
705
#if (!defined(__WIN__) && !defined(__EMX__) && !defined(OS2)) || defined(MYSQL_SERVER)
unknown's avatar
unknown committed
706 707 708 709 710
	  /*
	    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
	  */
711
	  if ((interrupted || length == 0) && !thr_alarm_in_use(&alarmed))
unknown's avatar
unknown committed
712
	  {
unknown's avatar
unknown committed
713
	    if (!thr_alarm(&alarmed,net->read_timeout,&alarm_buff)) /* Don't wait too long */
unknown's avatar
unknown committed
714
	    {
715 716 717 718
	      my_bool old_mode;
	      while (vio_blocking(net->vio, TRUE, &old_mode) < 0)
	      {
		if (vio_should_retry(net->vio) &&
719
		    retry_count++ < net->retry_count)
720 721 722 723
		  continue;
		DBUG_PRINT("error",
			   ("fcntl returned error %d, aborting thread",
			    vio_errno(net->vio)));
unknown's avatar
unknown committed
724
#ifdef EXTRA_DEBUG
725 726 727
		fprintf(stderr,
			"%s: read: fcntl returned error %d, aborting thread\n",
			my_progname,vio_errno(net->vio));
unknown's avatar
unknown committed
728
#endif /* EXTRA_DEBUG */
729
		len= packet_error;
unknown's avatar
unknown committed
730 731
		net->error= 2;                 /* Close socket */
	        net->report_error= 1;
unknown's avatar
unknown committed
732
#ifdef MYSQL_SERVER
unknown's avatar
unknown committed
733
		net->last_errno= ER_NET_FCNTL_ERROR;
unknown's avatar
unknown committed
734
#endif
735 736
		goto end;
	      }
unknown's avatar
unknown committed
737 738 739 740 741
	      retry_count=0;
	      continue;
	    }
	  }
#endif /* (!defined(__WIN__) && !defined(__EMX__)) || defined(MYSQL_SERVER) */
742
	  if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) &&
unknown's avatar
unknown committed
743 744
	      interrupted)
	  {					/* Probably in MIT threads */
745
	    if (retry_count++ < net->retry_count)
unknown's avatar
unknown committed
746 747 748 749 750 751 752 753 754 755 756 757 758
	      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
759 760
	  DBUG_PRINT("error",("Couldn't read packet: remain: %u  errno: %d  length: %ld",
			      remain, vio_errno(net->vio), length));
unknown's avatar
unknown committed
761
	  len= packet_error;
unknown's avatar
unknown committed
762 763
	  net->error= 2;				/* Close socket */
	  net->report_error= 1;
unknown's avatar
unknown committed
764 765 766 767 768 769
#ifdef MYSQL_SERVER
	  net->last_errno= (interrupted ? ER_NET_READ_INTERRUPTED :
			    ER_NET_READ_ERROR);
#endif
	  goto end;
	}
unknown's avatar
unknown committed
770
	remain -= (uint32) length;
unknown's avatar
unknown committed
771
	pos+= (ulong) length;
772
	update_statistics(thd_increment_bytes_received(length));
unknown's avatar
unknown committed
773 774 775 776
      }
      if (i == 0)
      {					/* First parts is packet length */
	ulong helping;
unknown's avatar
unknown committed
777 778
        DBUG_DUMP("packet_header",(char*) net->buff+net->where_b,
                  NET_HEADER_SIZE);
unknown's avatar
unknown committed
779 780 781 782 783
	if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr)
	{
	  if (net->buff[net->where_b] != (uchar) 255)
	  {
	    DBUG_PRINT("error",
unknown's avatar
unknown committed
784
		       ("Packets out of order (Found: %d, expected %u)",
unknown's avatar
unknown committed
785
			(int) net->buff[net->where_b + 3],
unknown's avatar
unknown committed
786
			net->pkt_nr));
unknown's avatar
unknown committed
787 788 789 790 791 792 793
#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;
unknown's avatar
unknown committed
794
	  net->report_error= 1;
unknown's avatar
unknown committed
795 796 797 798 799
#ifdef MYSQL_SERVER
	  net->last_errno=ER_NET_PACKETS_OUT_OF_ORDER;
#endif
	  goto end;
	}
unknown's avatar
unknown committed
800
	net->compress_pkt_nr= ++net->pkt_nr;
unknown's avatar
unknown committed
801 802 803
#ifdef HAVE_COMPRESS
	if (net->compress)
	{
804 805 806 807
	  /*
	    If the packet is compressed then complen > 0 and contains the
	    number of bytes in the uncompressed packet
	  */
unknown's avatar
unknown committed
808 809 810 811 812
	  *complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE]));
	}
#endif

	len=uint3korr(net->buff+net->where_b);
813 814
	if (!len)				/* End of big multi-packet */
	  goto end;
unknown's avatar
unknown committed
815 816 817 818
	helping = max(len,*complen) + net->where_b;
	/* The necessary size of net->buff */
	if (helping >= net->max_packet)
	{
819
	  if (net_realloc(net,helping))
unknown's avatar
unknown committed
820
	  {
821 822 823 824
#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 */
unknown's avatar
unknown committed
825
#endif
826
	    len= packet_error;          /* Return error and close connection */
unknown's avatar
unknown committed
827 828 829 830
	    goto end;
	  }
	}
	pos=net->buff + net->where_b;
unknown's avatar
unknown committed
831
	remain = (uint32) len;
unknown's avatar
unknown committed
832 833 834 835
      }
    }

end:
836
  if (thr_alarm_in_use(&alarmed))
unknown's avatar
unknown committed
837
  {
838
    my_bool old_mode;
unknown's avatar
unknown committed
839
    thr_end_alarm(&alarmed);
840
    vio_blocking(net->vio, net_blocking, &old_mode);
unknown's avatar
unknown committed
841 842
  }
  net->reading_or_writing=0;
unknown's avatar
unknown committed
843 844 845 846
#ifdef DEBUG_DATA_PACKETS
  if (len != packet_error)
    DBUG_DUMP("data",(char*) net->buff+net->where_b, len);
#endif
unknown's avatar
unknown committed
847 848 849
  return(len);
}

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

/*
  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
unknown's avatar
unknown committed
865 866 867 868 869 870 871 872
my_net_read(NET *net)
{
  ulong len,complen;

#ifdef HAVE_COMPRESS
  if (!net->compress)
  {
#endif
873
    len = my_real_read(net,&complen);
874
    if (len == MAX_PACKET_LENGTH)
875 876
    {
      /* First packet of a multi-packet.  Concatenate the packets */
unknown's avatar
unknown committed
877
      ulong save_pos = net->where_b;
878 879 880 881 882
      ulong total_length=0;
      do
      {
	net->where_b += len;
	total_length += len;
883
	len = my_real_read(net,&complen);
884
      } while (len == MAX_PACKET_LENGTH);
885 886 887 888
      if (len != packet_error)
	len+= total_length;
      net->where_b = save_pos;
    }
unknown's avatar
unknown committed
889 890 891 892 893 894
    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
  }
895
  else
unknown's avatar
unknown committed
896
  {
897 898
    /* We are using the compressed protocol */

unknown's avatar
unknown committed
899 900 901
    ulong buf_length;
    ulong start_of_packet;
    ulong first_packet_offset;
902 903
    uint read_length, multi_byte_packet=0;

unknown's avatar
unknown committed
904 905
    if (net->remain_in_buf)
    {
906
      buf_length= net->buf_length;		/* Data left in old packet */
unknown's avatar
unknown committed
907 908
      first_packet_offset= start_of_packet= (net->buf_length -
					     net->remain_in_buf);
909
      /* Restore the character that was overwritten by the end 0 */
unknown's avatar
unknown committed
910
      net->buff[start_of_packet]= net->save_char;
911 912 913
    }
    else
    {
914
      /* reuse buffer, as there is nothing in it that we need */
unknown's avatar
unknown committed
915
      buf_length= start_of_packet= first_packet_offset= 0;
916 917 918 919
    }
    for (;;)
    {
      ulong packet_len;
unknown's avatar
unknown committed
920

921
      if (buf_length - start_of_packet >= NET_HEADER_SIZE)
unknown's avatar
unknown committed
922
      {
923 924 925 926 927 928 929 930
	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)
unknown's avatar
unknown committed
931
	{
932 933 934 935 936 937 938 939 940 941 942 943 944
	  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;

945
	  if (read_length != MAX_PACKET_LENGTH)	/* last package */
946
	  {
unknown's avatar
unknown committed
947
	    multi_byte_packet= 0;		/* No last zero len packet */
948 949 950 951 952 953 954 955 956 957 958 959 960
	    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;
unknown's avatar
unknown committed
961 962 963
	}
      }
      /* Move data down to read next data packet after current one */
964
      if (first_packet_offset)
unknown's avatar
unknown committed
965
      {
966 967 968 969 970
	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;
unknown's avatar
unknown committed
971 972
      }

973 974 975 976 977 978
      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))
      {
unknown's avatar
unknown committed
979 980
	net->error= 2;			/* caller will close socket */
	net->report_error= 1;
unknown's avatar
unknown committed
981
#ifdef MYSQL_SERVER
982
	net->last_errno=ER_NET_UNCOMPRESS_ERROR;
unknown's avatar
unknown committed
983
#endif
984 985 986
	return packet_error;
      }
      buf_length+=packet_len;
unknown's avatar
unknown committed
987
    }
988 989 990

    net->read_pos=      net->buff+ first_packet_offset + NET_HEADER_SIZE;
    net->buf_length=    buf_length;
unknown's avatar
unknown committed
991 992
    net->remain_in_buf= (ulong) (buf_length - start_of_packet);
    len = ((ulong) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
993
           multi_byte_packet);
unknown's avatar
unknown committed
994 995 996
    net->save_char= net->read_pos[len];	/* Must be saved */
    net->read_pos[len]=0;		/* Safeguard for mysql_use_result */
  }
997
#endif /* HAVE_COMPRESS */
unknown's avatar
unknown committed
998 999
  return len;
}
1000