~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzleclient/net_serv.c

  • Committer: Monty Taylor
  • Date: 2009-03-04 02:16:28 UTC
  • mto: (917.1.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 912.
  • Revision ID: mordred@inaugust.com-20090304021628-rfq0b16uoi09g8tx
Fix to make VPATH builds work again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#define __need_timeval 1
22
 
 
 
21
#include <drizzled/global.h>
23
22
#include "libdrizzle.h"
24
23
#include "libdrizzle_priv.h"
25
 
#include <libdrizzle/errmsg.h>
26
 
#include <vio/violite.h>
 
24
#include "errmsg.h"
 
25
#include "vio.h"
27
26
#include <assert.h>
28
27
#include <stdio.h>
29
28
#include <stdlib.h>
44
43
*/
45
44
 
46
45
 
47
 
#define update_statistics(A)
48
 
#define thd_increment_bytes_sent(N)
49
 
 
50
 
#define TEST_BLOCKING        8
51
46
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
52
 
#define MIN_COMPRESS_LENGTH             50      /* Don't compress small bl. */
53
47
 
54
48
static bool net_write_buff(NET *net, const unsigned char *packet, uint32_t len);
55
49
 
56
50
 
57
51
/** Init with packet info. */
58
52
 
59
 
bool my_net_init(NET *net, Vio* vio)
 
53
bool drizzleclient_net_init(NET *net, Vio* vio)
60
54
{
61
55
  net->vio = vio;
62
 
  my_net_local_init(net);            /* Set some limits */
 
56
  drizzleclient_net_local_init(net);            /* Set some limits */
63
57
  if (!(net->buff=(unsigned char*) malloc((size_t) net->max_packet+
64
 
                                  NET_HEADER_SIZE + COMP_HEADER_SIZE)))
 
58
                                          NET_HEADER_SIZE + COMP_HEADER_SIZE)))
65
59
    return(1);
66
60
  net->buff_end=net->buff+net->max_packet;
67
61
  net->error=0; net->return_status=0;
75
69
 
76
70
  if (vio != 0)                    /* If real connection */
77
71
  {
78
 
    net->fd  = vio_fd(vio);            /* For perl DBI/DBD */
79
 
    vio_fastsend(vio);
 
72
    net->fd  = drizzleclient_vio_fd(vio);            /* For perl DBI/DBD */
 
73
    drizzleclient_vio_fastsend(vio);
80
74
  }
81
75
  return(0);
82
76
}
83
77
 
84
 
bool net_init_sock(NET * net, int sock, int flags)
 
78
bool drizzleclient_net_init_sock(NET * net, int sock, int flags)
85
79
{
86
80
 
87
 
  Vio *vio_tmp= vio_new(sock, VIO_TYPE_TCPIP, flags);
88
 
  if (vio_tmp == NULL)
 
81
  Vio *drizzleclient_vio_tmp= drizzleclient_vio_new(sock, VIO_TYPE_TCPIP, flags);
 
82
  if (drizzleclient_vio_tmp == NULL)
89
83
    return true;
90
84
  else
91
 
    if (my_net_init(net, vio_tmp))
 
85
    if (drizzleclient_net_init(net, drizzleclient_vio_tmp))
92
86
    {
93
87
      /* Only delete the temporary vio if we didn't already attach it to the
94
88
       * NET object.
95
89
       */
96
 
      if (vio_tmp && (net->vio != vio_tmp))
97
 
        vio_delete(vio_tmp);
 
90
      if (drizzleclient_vio_tmp && (net->vio != drizzleclient_vio_tmp))
 
91
        drizzleclient_vio_delete(drizzleclient_vio_tmp);
98
92
      else
99
93
      {
100
94
        (void) shutdown(sock, SHUT_RDWR);
105
99
  return false;
106
100
}
107
101
 
108
 
void net_end(NET *net)
 
102
void drizzleclient_net_end(NET *net)
109
103
{
110
104
  if (net->buff != NULL)
111
105
    free(net->buff);
112
 
  net->buff=0;
 
106
  net->buff= NULL;
113
107
  return;
114
108
}
115
109
 
116
 
void net_close(NET *net)
 
110
void drizzleclient_net_close(NET *net)
117
111
{
118
112
  if (net->vio != NULL)
119
113
  {
120
 
    vio_delete(net->vio);
 
114
    drizzleclient_vio_delete(net->vio);
121
115
    net->vio= 0;
122
116
  }
123
117
}
124
118
 
125
 
bool net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
126
 
{
127
 
  return vio_peer_addr(net->vio, buf, port, buflen);
128
 
}
129
 
 
130
 
void net_keepalive(NET *net, bool flag)
131
 
{
132
 
  vio_keepalive(net->vio, flag);
133
 
}
134
 
 
135
 
int net_get_sd(NET *net)
 
119
bool drizzleclient_net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
 
120
{
 
121
  return drizzleclient_vio_peer_addr(net->vio, buf, port, buflen);
 
122
}
 
123
 
 
124
void drizzleclient_net_keepalive(NET *net, bool flag)
 
125
{
 
126
  drizzleclient_vio_keepalive(net->vio, flag);
 
127
}
 
128
 
 
129
int drizzleclient_net_get_sd(NET *net)
136
130
{
137
131
  return net->vio->sd;
138
132
}
139
133
 
140
 
bool net_should_close(NET *net)
 
134
bool drizzleclient_net_should_close(NET *net)
141
135
{
142
136
  return net->error || (net->vio == 0);
143
137
}
144
138
 
145
 
bool net_more_data(NET *net)
 
139
bool drizzleclient_net_more_data(NET *net)
146
140
{
147
141
  return (net->vio == 0 || net->vio->read_pos < net->vio->read_end);
148
142
}
149
143
 
150
144
/** Realloc the packet buffer. */
151
145
 
152
 
bool net_realloc(NET *net, size_t length)
 
146
bool drizzleclient_net_realloc(NET *net, size_t length)
153
147
{
154
148
  unsigned char *buff;
155
149
  size_t pkt_length;
215
209
   Read from socket until there is nothing more to read. Discard
216
210
   what is read.
217
211
 
218
 
   If there is anything when to read 'net_clear' is called this
 
212
   If there is anything when to read 'drizzleclient_net_clear' is called this
219
213
   normally indicates an error in the protocol.
220
214
 
221
215
   When connection is properly closed (for TCP it means with
226
220
   @param clear_buffer           if <> 0, then clear all data from comm buff
227
221
*/
228
222
 
229
 
void net_clear(NET *net, bool clear_buffer)
 
223
void drizzleclient_net_clear(NET *net, bool clear_buffer)
230
224
{
231
225
  if (clear_buffer)
232
226
  {
233
227
    while (net_data_is_ready(net->vio->sd) > 0)
234
228
    {
235
229
      /* The socket is ready */
236
 
      if (vio_read(net->vio, net->buff,
 
230
      if (drizzleclient_vio_read(net->vio, net->buff,
237
231
                   (size_t) net->max_packet) <= 0)
238
232
      {
239
233
        net->error= 2;
249
243
 
250
244
/** Flush write_buffer if not empty. */
251
245
 
252
 
bool net_flush(NET *net)
 
246
bool drizzleclient_net_flush(NET *net)
253
247
{
254
248
  bool error= 0;
255
249
  if (net->buff != net->write_pos)
256
250
  {
257
 
    error=net_real_write(net, net->buff,
 
251
    error=drizzleclient_net_real_write(net, net->buff,
258
252
                         (size_t) (net->write_pos - net->buff)) ? 1 : 0;
259
253
    net->write_pos=net->buff;
260
254
  }
280
274
*/
281
275
 
282
276
bool
283
 
my_net_write(NET *net,const unsigned char *packet,size_t len)
 
277
drizzleclient_net_write(NET *net,const unsigned char *packet,size_t len)
284
278
{
285
279
  unsigned char buff[NET_HEADER_SIZE];
286
280
  if (unlikely(!net->vio)) /* nowhere to write */
337
331
*/
338
332
 
339
333
bool
340
 
net_write_command(NET *net,unsigned char command,
 
334
drizzleclient_net_write_command(NET *net,unsigned char command,
341
335
                  const unsigned char *header, size_t head_len,
342
336
                  const unsigned char *packet, size_t len)
343
337
{
371
365
  buff[3]= (unsigned char) net->pkt_nr++;
372
366
  return((net_write_buff(net, buff, header_size) ||
373
367
          (head_len && net_write_buff(net, header, head_len)) ||
374
 
          net_write_buff(net, packet, len) || net_flush(net)) ? 1 : 0 );
 
368
          net_write_buff(net, packet, len) || drizzleclient_net_flush(net)) ? 1 : 0 );
375
369
}
376
370
 
377
371
/**
389
383
   @param len        Length of packet
390
384
 
391
385
   @note
392
 
   The cached buffer can be sent as it is with 'net_flush()'.
 
386
   The cached buffer can be sent as it is with 'drizzleclient_net_flush()'.
393
387
   In this code we have to be careful to not send a packet longer than
394
 
   MAX_PACKET_LENGTH to net_real_write() if we are using the compressed
 
388
   MAX_PACKET_LENGTH to drizzleclient_net_real_write() if we are using the compressed
395
389
   protocol as we store the length of the compressed packet in 3 bytes.
396
390
 
397
391
   @retval
415
409
    {
416
410
      /* Fill up already used packet and write it */
417
411
      memcpy(net->write_pos,packet,left_length);
418
 
      if (net_real_write(net, net->buff,
 
412
      if (drizzleclient_net_real_write(net, net->buff,
419
413
                         (size_t) (net->write_pos - net->buff) + left_length))
420
414
        return 1;
421
415
      net->write_pos= net->buff;
431
425
      left_length= MAX_PACKET_LENGTH;
432
426
      while (len > left_length)
433
427
      {
434
 
        if (net_real_write(net, packet, left_length))
 
428
        if (drizzleclient_net_real_write(net, packet, left_length))
435
429
          return 1;
436
430
        packet+= left_length;
437
431
        len-= left_length;
438
432
      }
439
433
    }
440
434
    if (len > net->max_packet)
441
 
      return net_real_write(net, packet, len) ? 1 : 0;
 
435
      return drizzleclient_net_real_write(net, packet, len) ? 1 : 0;
442
436
    /* Send out rest of the blocks as full sized blocks */
443
437
  }
444
438
  memcpy(net->write_pos,packet,len);
460
454
  in the server, yield to another process and come back later.
461
455
*/
462
456
int
463
 
net_real_write(NET *net,const unsigned char *packet, size_t len)
 
457
drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len)
464
458
{
465
459
  size_t length;
466
 
  const unsigned char *pos,*end;
 
460
  const unsigned char *pos, *end;
467
461
  uint32_t retry_count= 0;
468
462
 
469
463
  /* Backup of the original SO_RCVTIMEO timeout */
529
523
  if (net->write_timeout)
530
524
  {
531
525
    struct timespec waittime;
532
 
    socklen_t length;
 
526
    socklen_t time_len;
533
527
 
534
528
    waittime.tv_sec= net->write_timeout;
535
529
    waittime.tv_nsec= 0;
536
530
 
537
531
    memset(&backtime, 0, sizeof(struct timespec));
538
 
    length= sizeof(struct timespec);
 
532
    time_len= sizeof(struct timespec);
539
533
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
540
 
                      &backtime, &length);
 
534
                      &backtime, &time_len);
541
535
    if (error != 0)
542
536
    {
543
537
      perror("getsockopt");
554
548
  /* Loop until we have read everything */
555
549
  while (pos != end)
556
550
  {
557
 
    if ((long) (length= vio_write(net->vio,pos,(size_t) (end-pos))) <= 0)
 
551
    assert(pos);
 
552
    if ((long) (length= drizzleclient_vio_write(net->vio, pos, (size_t) (end-pos))) <= 0)
558
553
    {
559
 
      const bool interrupted= vio_should_retry(net->vio);
 
554
      const bool interrupted= drizzleclient_vio_should_retry(net->vio);
560
555
      /*
561
556
        If we read 0, or we were interrupted this means that
562
557
        we need to switch to blocking mode and wait until the timeout
566
561
      {
567
562
        bool old_mode;
568
563
 
569
 
        while (vio_blocking(net->vio, true, &old_mode) < 0)
 
564
        while (drizzleclient_vio_blocking(net->vio, true, &old_mode) < 0)
570
565
        {
571
 
          if (vio_should_retry(net->vio) && retry_count++ < net->retry_count)
 
566
          if (drizzleclient_vio_should_retry(net->vio) && retry_count++ < net->retry_count)
572
567
            continue;
573
568
          net->error= 2;                     /* Close socket */
574
569
          net->last_errno= CR_NET_PACKET_TOO_LARGE;
583
578
          continue;
584
579
      }
585
580
 
586
 
      if (vio_errno(net->vio) == EINTR)
 
581
      if (drizzleclient_vio_errno(net->vio) == EINTR)
587
582
      {
588
583
        continue;
589
584
      }
593
588
      break;
594
589
    }
595
590
    pos+=length;
596
 
    update_statistics(thd_increment_bytes_sent(length));
597
591
  }
598
592
end:
599
593
  if ((net->compress) && (packet != NULL))
612
606
 
613
607
/**
614
608
   Reads one packet to net->buff + net->where_b.
615
 
   Long packets are handled by my_net_read().
 
609
   Long packets are handled by drizzleclient_net_read().
616
610
   This function reallocates the net->buff buffer if necessary.
617
611
 
618
612
   @return
638
632
  *complen = 0;
639
633
 
640
634
  net->reading_or_writing= 1;
641
 
  /* Read timeout is set in my_net_set_read_timeout */
 
635
  /* Read timeout is set in drizzleclient_net_set_read_timeout */
642
636
 
643
637
  pos = net->buff + net->where_b;        /* net->packet -4 */
644
638
 
648
642
  if (net->read_timeout)
649
643
  {
650
644
    struct timespec waittime;
651
 
    socklen_t length;
 
645
    socklen_t time_len;
652
646
 
653
647
    waittime.tv_sec= net->read_timeout;
654
648
    waittime.tv_nsec= 0;
655
649
 
656
650
    memset(&backtime, 0, sizeof(struct timespec));
657
 
    length= sizeof(struct timespec);
 
651
    time_len= sizeof(struct timespec);
658
652
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
659
 
                      &backtime, &length);
 
653
                      &backtime, &time_len);
660
654
    if (error != 0)
661
655
    {
662
656
      perror("getsockopt");
673
667
    while (remain > 0)
674
668
    {
675
669
      /* First read is done with non blocking mode */
676
 
      if ((long) (length= vio_read(net->vio, pos, remain)) <= 0L)
 
670
      if ((long) (length= drizzleclient_vio_read(net->vio, pos, remain)) <= 0L)
677
671
      {
678
 
        const bool interrupted = vio_should_retry(net->vio);
 
672
        const bool interrupted = drizzleclient_vio_should_retry(net->vio);
679
673
 
680
674
        if (interrupted)
681
675
        {                    /* Probably in MIT threads */
682
676
          if (retry_count++ < net->retry_count)
683
677
            continue;
684
678
        }
685
 
        if (vio_errno(net->vio) == EINTR)
 
679
        if (drizzleclient_vio_errno(net->vio) == EINTR)
686
680
        {
687
681
          continue;
688
682
        }
689
683
        len= packet_error;
690
684
        net->error= 2;                /* Close socket */
691
 
        net->last_errno= (vio_was_interrupted(net->vio) ?
 
685
        net->last_errno= (drizzleclient_vio_was_interrupted(net->vio) ?
692
686
                          CR_NET_READ_INTERRUPTED :
693
687
                          CR_NET_READ_ERROR);
694
688
        ER(net->last_errno);
696
690
      }
697
691
      remain -= (uint32_t) length;
698
692
      pos+= length;
699
 
      update_statistics(thd_increment_bytes_received(length));
700
693
    }
701
694
    if (i == 0)
702
695
    {                    /* First parts is packet length */
725
718
      /* The necessary size of net->buff */
726
719
      if (helping >= net->max_packet)
727
720
      {
728
 
        if (net_realloc(net,helping))
 
721
        if (drizzleclient_net_realloc(net,helping))
729
722
        {
730
723
          len= packet_error;          /* Return error and close connection */
731
724
          goto end;
766
759
*/
767
760
 
768
761
uint32_t
769
 
my_net_read(NET *net)
 
762
drizzleclient_net_read(NET *net)
770
763
{
771
764
  size_t len, complen;
772
765
 
790
783
    }
791
784
    net->read_pos = net->buff + net->where_b;
792
785
    if (len != packet_error)
793
 
      net->read_pos[len]=0;        /* Safeguard for drizzle_use_result */
 
786
      net->read_pos[len]=0;        /* Safeguard for drizzleclient_use_result */
794
787
    return len;
795
788
  }
796
789
  else
910
903
    len = ((uint32_t) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
911
904
           multi_byte_packet);
912
905
    net->save_char= net->read_pos[len];    /* Must be saved */
913
 
    net->read_pos[len]=0;        /* Safeguard for drizzle_use_result */
 
906
    net->read_pos[len]=0;        /* Safeguard for drizzleclient_use_result */
914
907
  }
915
908
  return len;
916
909
  }
917
910
 
918
911
 
919
 
void my_net_set_read_timeout(NET *net, uint32_t timeout)
 
912
void drizzleclient_net_set_read_timeout(NET *net, uint32_t timeout)
920
913
{
921
914
  net->read_timeout= timeout;
922
915
#ifndef __sun
923
916
  if (net->vio)
924
 
    vio_timeout(net->vio, 0, timeout);
 
917
    drizzleclient_vio_timeout(net->vio, 0, timeout);
925
918
#endif
926
919
  return;
927
920
}
928
921
 
929
922
 
930
 
void my_net_set_write_timeout(NET *net, uint32_t timeout)
 
923
void drizzleclient_net_set_write_timeout(NET *net, uint32_t timeout)
931
924
{
932
925
  net->write_timeout= timeout;
933
926
#ifndef __sun
934
927
  if (net->vio)
935
 
    vio_timeout(net->vio, 1, timeout);
 
928
    drizzleclient_vio_timeout(net->vio, 1, timeout);
936
929
#endif
937
930
  return;
938
931
}
942
935
  @param net  clear the state of the argument
943
936
*/
944
937
 
945
 
void net_clear_error(NET *net)
 
938
void drizzleclient_drizzleclient_net_clear_error(NET *net)
946
939
{
947
940
  net->last_errno= 0;
948
941
  net->last_error[0]= '\0';
949
 
  strcpy(net->sqlstate, sqlstate_get_not_error());
 
942
  strcpy(net->sqlstate, drizzleclient_sqlstate_get_not_error());
950
943
}
951
944