~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/oldlibdrizzle/oldlibdrizzle.cc

  • Committer: Brian Aker
  • Date: 2009-09-22 07:35:28 UTC
  • mfrom: (971.6.10 eday-dev)
  • Revision ID: brian@gaz-20090922073528-xgm634aomuflqxl3
MergeĀ Eric

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
extern uint32_t drizzled_tcp_port;
39
39
 
40
40
static const unsigned int PACKET_BUFFER_EXTRA_ALLOC= 1024;
41
 
static uint32_t _port= 0;
42
41
static uint32_t connect_timeout;
43
42
static uint32_t read_timeout;
44
43
static uint32_t write_timeout;
45
44
static uint32_t retry_count;
46
45
 
47
 
ListenOldLibdrizzle::ListenOldLibdrizzle()
48
 
{
49
 
  port= (in_port_t) _port;
50
 
}
51
 
 
52
46
in_port_t ListenOldLibdrizzle::getPort(void) const
53
47
{
54
 
  if (port == 0)
55
 
    return (in_port_t ) drizzled_tcp_port;
56
 
 
57
 
  return port;
 
48
  return (in_port_t) drizzled_tcp_port;
58
49
}
59
50
 
60
 
plugin::Protocol *ListenOldLibdrizzle::protocolFactory(void) const
 
51
plugin::Client *ListenOldLibdrizzle::getClient(int fd)
61
52
{
62
 
  return new ProtocolOldLibdrizzle;
 
53
  if (fd == -1)
 
54
    return new (nothrow) ClientOldLibdrizzle(-1);
 
55
 
 
56
  int new_fd;
 
57
  new_fd= acceptTcp(fd);
 
58
  if (new_fd == -1)
 
59
    return NULL;
 
60
 
 
61
  return new (nothrow) ClientOldLibdrizzle(new_fd);
63
62
}
64
63
 
65
64
static void write_eof_packet(Session *session, NET *net,
66
65
                             uint32_t server_status, uint32_t total_warn_count);
67
66
 
68
 
bool ProtocolOldLibdrizzle::isConnected()
 
67
bool ClientOldLibdrizzle::isConnected()
69
68
{
70
69
  return net.vio != 0;
71
70
}
72
71
 
73
 
void ProtocolOldLibdrizzle::setError(char error)
 
72
void ClientOldLibdrizzle::setError(char error)
74
73
{
75
74
  net.error= error;
76
75
}
77
76
 
78
 
bool ProtocolOldLibdrizzle::haveError(void)
 
77
bool ClientOldLibdrizzle::haveError(void)
79
78
{
80
79
  return net.error || net.vio == 0;
81
80
}
82
81
 
83
 
bool ProtocolOldLibdrizzle::wasAborted(void)
 
82
bool ClientOldLibdrizzle::wasAborted(void)
84
83
{
85
84
  return net.error && net.vio != 0;
86
85
}
87
86
 
88
 
bool ProtocolOldLibdrizzle::haveMoreData(void)
 
87
bool ClientOldLibdrizzle::haveMoreData(void)
89
88
{
90
89
  return drizzleclient_net_more_data(&net);
91
90
}
92
91
 
93
 
bool ProtocolOldLibdrizzle::isReading(void)
 
92
bool ClientOldLibdrizzle::isReading(void)
94
93
{
95
94
  return net.reading_or_writing == 1;
96
95
}
97
96
 
98
 
bool ProtocolOldLibdrizzle::isWriting(void)
 
97
bool ClientOldLibdrizzle::isWriting(void)
99
98
{
100
99
  return net.reading_or_writing == 2;
101
100
}
102
101
 
103
 
bool ProtocolOldLibdrizzle::netStoreData(const unsigned char *from, size_t length)
 
102
bool ClientOldLibdrizzle::netStoreData(const unsigned char *from, size_t length)
104
103
{
105
104
  size_t packet_length= packet->length();
106
105
  /*
127
126
  - id        : Stored in 1-9 bytes
128
127
  - server_status    : Copy of session->server_status;  Can be used by client
129
128
  to check if we are inside an transaction.
130
 
  New in 4.0 protocol
131
 
  - warning_count    : Stored in 2 bytes; New in 4.1 protocol
 
129
  New in 4.0 client
 
130
  - warning_count    : Stored in 2 bytes; New in 4.1 client
132
131
  - message        : Stored as packed length (1-9 bytes) + message.
133
132
  Is not stored if no message.
134
133
 
138
137
  @param message       Message to send to the client (Used by mysql_status)
139
138
*/
140
139
 
141
 
void ProtocolOldLibdrizzle::sendOK()
 
140
void ClientOldLibdrizzle::sendOK()
142
141
{
143
142
  unsigned char buff[DRIZZLE_ERRMSG_SIZE+10],*pos;
144
143
  const char *message= NULL;
196
195
  The eof packet has the following structure:
197
196
 
198
197
  - 254    (DRIZZLE_PROTOCOL_NO_MORE_DATA)    : Marker (1 byte)
199
 
  - warning_count    : Stored in 2 bytes; New in 4.1 protocol
 
198
  - warning_count    : Stored in 2 bytes; New in 4.1 client
200
199
  - status_flag    : Stored in 2 bytes;
201
200
  For flags like SERVER_MORE_RESULTS_EXISTS.
202
201
 
205
204
  client.
206
205
*/
207
206
 
208
 
void ProtocolOldLibdrizzle::sendEOF()
 
207
void ClientOldLibdrizzle::sendEOF()
209
208
{
210
209
  /* Set to true if no active vio, to work well in case of --init-file */
211
210
  if (net.vio != 0)
220
219
 
221
220
 
222
221
/**
223
 
  Format EOF packet according to the current protocol and
 
222
  Format EOF packet according to the current client and
224
223
  write it to the network output buffer.
225
224
*/
226
225
 
247
246
  drizzleclient_net_write(net, buff, 5);
248
247
}
249
248
 
250
 
void ProtocolOldLibdrizzle::sendError(uint32_t sql_errno, const char *err)
 
249
void ClientOldLibdrizzle::sendError(uint32_t sql_errno, const char *err)
251
250
{
252
251
  uint32_t length;
253
252
  /*
283
282
  int2store(buff,sql_errno);
284
283
  pos= buff+2;
285
284
 
286
 
  /* The first # is to make the protocol backward compatible */
 
285
  /* The first # is to make the client backward compatible */
287
286
  buff[2]= '#';
288
287
  pos= (unsigned char*) strcpy((char*) buff+3, drizzle_errno_to_sqlstate(sql_errno));
289
288
  pos+= strlen(drizzle_errno_to_sqlstate(sql_errno));
300
299
}
301
300
 
302
301
 
303
 
ProtocolOldLibdrizzle::ProtocolOldLibdrizzle()
 
302
ClientOldLibdrizzle::ClientOldLibdrizzle(int fd)
304
303
{
305
304
  scramble[0]= 0;
306
305
  net.vio= 0;
 
306
 
 
307
  if (fd == -1)
 
308
    return;
 
309
 
 
310
  if (drizzleclient_net_init_sock(&net, fd, 0))
 
311
    throw bad_alloc();
 
312
 
 
313
  drizzleclient_net_set_read_timeout(&net, read_timeout);
 
314
  drizzleclient_net_set_write_timeout(&net, write_timeout);
 
315
  net.retry_count=retry_count;
307
316
}
308
317
 
309
 
ProtocolOldLibdrizzle::~ProtocolOldLibdrizzle()
 
318
ClientOldLibdrizzle::~ClientOldLibdrizzle()
310
319
{
311
320
  if (net.vio)
312
321
    drizzleclient_vio_close(net.vio);
313
322
}
314
323
 
315
 
void ProtocolOldLibdrizzle::setSession(Session *session_arg)
 
324
void ClientOldLibdrizzle::setSession(Session *session_arg)
316
325
{
317
326
  session= session_arg;
318
327
  packet= &session->packet;
319
328
  convert= &session->convert_buffer;
 
329
  prepareForResend();
320
330
}
321
331
 
322
332
 
338
348
    1    Error  (Note that in this case the error is not sent to the
339
349
    client)
340
350
*/
341
 
bool ProtocolOldLibdrizzle::sendFields(List<Item> *list)
 
351
bool ClientOldLibdrizzle::sendFields(List<Item> *list)
342
352
{
343
353
  List_iterator_fast<Item> it(*list);
344
354
  Item *item;
379
389
    pos+= 12;
380
390
 
381
391
    packet->length((uint32_t) (pos - packet->ptr()));
382
 
    if (write())
 
392
    if (flush())
383
393
      break;                    /* purecov: inspected */
384
394
  }
385
395
 
401
411
}
402
412
 
403
413
 
404
 
bool ProtocolOldLibdrizzle::write()
 
414
bool ClientOldLibdrizzle::flush()
405
415
{
406
 
  return(drizzleclient_net_write(&net, (unsigned char*) packet->ptr(),
407
 
                           packet->length()));
 
416
  if (net.vio == NULL)
 
417
    return false;
 
418
  bool ret= drizzleclient_net_write(&net, (unsigned char*) packet->ptr(),
 
419
                           packet->length());
 
420
  prepareForResend();
 
421
  return ret;
408
422
}
409
423
 
410
 
void ProtocolOldLibdrizzle::free()
 
424
void ClientOldLibdrizzle::free()
411
425
{
412
426
  packet->free();
413
427
}
414
428
 
415
 
bool ProtocolOldLibdrizzle::setFileDescriptor(int fd)
416
 
{
417
 
  if (drizzleclient_net_init_sock(&net, fd, 0))
418
 
    return true;
419
 
 
420
 
  drizzleclient_net_set_read_timeout(&net, read_timeout);
421
 
  drizzleclient_net_set_write_timeout(&net, write_timeout);
422
 
  net.retry_count=retry_count;
423
 
 
424
 
  return false;
425
 
}
426
 
 
427
 
int ProtocolOldLibdrizzle::fileDescriptor(void)
 
429
int ClientOldLibdrizzle::getFileDescriptor(void)
428
430
{
429
431
  return drizzleclient_net_get_sd(&net);
430
432
}
431
433
 
432
 
bool ProtocolOldLibdrizzle::authenticate()
 
434
bool ClientOldLibdrizzle::authenticate()
433
435
{
434
436
  bool connection_is_valid;
435
437
 
453
455
  return true;
454
456
}
455
457
 
456
 
bool ProtocolOldLibdrizzle::readCommand(char **l_packet, uint32_t *packet_length)
 
458
bool ClientOldLibdrizzle::readCommand(char **l_packet, uint32_t *packet_length)
457
459
{
458
460
  /*
459
461
    This thread will do a blocking read from the client which
468
470
                                     session->variables.net_wait_timeout);
469
471
#endif
470
472
 
471
 
  session->clear_error();
472
 
  session->main_da.reset_diagnostics_area();
473
 
 
474
473
  net.pkt_nr=0;
475
474
 
476
475
  *packet_length= drizzleclient_net_read(&net);
483
482
    if (session->main_da.status() == Diagnostics_area::DA_ERROR)
484
483
      sendError(session->main_da.sql_errno(), session->main_da.message());
485
484
    else
486
 
      session->protocol->sendOK();
 
485
      sendOK();
487
486
 
488
487
    if (net.error != 3)
489
488
      return false;                       // We have to close it.
523
522
  return true;
524
523
}
525
524
 
526
 
void ProtocolOldLibdrizzle::close(void)
 
525
void ClientOldLibdrizzle::close(void)
527
526
{
528
527
  if (net.vio)
529
528
  { 
532
531
  }
533
532
}
534
533
 
535
 
void ProtocolOldLibdrizzle::forceClose(void)
 
534
void ClientOldLibdrizzle::forceClose(void)
536
535
{
537
536
  if (net.vio)
538
537
    drizzleclient_vio_close(net.vio);
539
538
}
540
539
 
541
 
void ProtocolOldLibdrizzle::prepareForResend()
 
540
void ClientOldLibdrizzle::prepareForResend()
542
541
{
543
542
  packet->length(0);
544
543
}
545
544
 
546
 
bool ProtocolOldLibdrizzle::store(void)
 
545
bool ClientOldLibdrizzle::store(void)
547
546
{
548
547
  char buff[1];
549
548
  buff[0]= (char)251;
551
550
}
552
551
 
553
552
 
554
 
bool ProtocolOldLibdrizzle::store(const char *from, size_t length)
 
553
bool ClientOldLibdrizzle::store(const char *from, size_t length)
555
554
{
556
555
  return netStoreData((const unsigned char *)from, length);
557
556
}
558
557
 
559
558
 
560
 
bool ProtocolOldLibdrizzle::store(int32_t from)
 
559
bool ClientOldLibdrizzle::store(int32_t from)
561
560
{
562
561
  char buff[12];
563
562
  return netStoreData((unsigned char*) buff,
564
563
                      (size_t) (int10_to_str(from, buff, -10) - buff));
565
564
}
566
565
 
567
 
bool ProtocolOldLibdrizzle::store(uint32_t from)
 
566
bool ClientOldLibdrizzle::store(uint32_t from)
568
567
{
569
568
  char buff[11];
570
569
  return netStoreData((unsigned char*) buff,
571
570
                      (size_t) (int10_to_str(from, buff, 10) - buff));
572
571
}
573
572
 
574
 
bool ProtocolOldLibdrizzle::store(int64_t from)
 
573
bool ClientOldLibdrizzle::store(int64_t from)
575
574
{
576
575
  char buff[22];
577
576
  return netStoreData((unsigned char*) buff,
578
577
                      (size_t) (int64_t10_to_str(from, buff, -10) - buff));
579
578
}
580
579
 
581
 
bool ProtocolOldLibdrizzle::store(uint64_t from)
 
580
bool ClientOldLibdrizzle::store(uint64_t from)
582
581
{
583
582
  char buff[21];
584
583
  return netStoreData((unsigned char*) buff,
586
585
}
587
586
 
588
587
 
589
 
bool ProtocolOldLibdrizzle::store(double from, uint32_t decimals, String *buffer)
 
588
bool ClientOldLibdrizzle::store(double from, uint32_t decimals, String *buffer)
590
589
{
591
590
  buffer->set_real(from, decimals, session->charset());
592
591
  return netStoreData((unsigned char*) buffer->ptr(), buffer->length());
593
592
}
594
593
 
595
594
 
596
 
bool ProtocolOldLibdrizzle::store(Field *from)
 
595
bool ClientOldLibdrizzle::store(Field *from)
597
596
{
598
597
  if (from->is_null())
599
598
    return store();
612
611
    we support 0-6 decimals for time.
613
612
*/
614
613
 
615
 
bool ProtocolOldLibdrizzle::store(const DRIZZLE_TIME *tm)
 
614
bool ClientOldLibdrizzle::store(const DRIZZLE_TIME *tm)
616
615
{
617
616
  char buff[40];
618
617
  uint32_t length;
658
657
  return netStoreData((unsigned char*) buff, length);
659
658
}
660
659
 
661
 
bool ProtocolOldLibdrizzle::checkConnection(void)
 
660
bool ClientOldLibdrizzle::checkConnection(void)
662
661
{
663
662
  uint32_t pkt_len= 0;
664
663
  char *end;
669
668
  // TCP/IP connection
670
669
  {
671
670
    char ip[NI_MAXHOST];
 
671
    uint16_t port;
672
672
 
673
 
    if (drizzleclient_net_peer_addr(&net, ip, &session->peer_port, NI_MAXHOST))
 
673
    if (drizzleclient_net_peer_addr(&net, ip, &port, NI_MAXHOST))
674
674
    {
675
675
      my_error(ER_BAD_HOST_ERROR, MYF(0), session->security_ctx.ip.c_str());
676
676
      return false;
780
780
    the size (1 byte) + string (not null-terminated). Hence in case of empty
781
781
    password both send '\0'.
782
782
 
783
 
    This strlen() can't be easily deleted without changing protocol.
 
783
    This strlen() can't be easily deleted without changing client.
784
784
 
785
785
    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
786
786
    *passwd > 127 and become 2**32-127+ after casting to uint.
789
789
    (unsigned char)(*passwd++) : strlen(passwd);
790
790
  l_db= client_capabilities & CLIENT_CONNECT_WITH_DB ? l_db + passwd_len + 1 : 0;
791
791
 
792
 
  /* strlen() can't be easily deleted without changing protocol */
 
792
  /* strlen() can't be easily deleted without changing client */
793
793
  uint32_t db_len= l_db ? strlen(l_db) : 0;
794
794
 
795
795
  if (passwd + passwd_len + db_len > (char *) net.read_pos + pkt_len)
848
848
  "oldlibdrizzle",
849
849
  "0.1",
850
850
  "Eric Day",
851
 
  "Old libdrizzle Protocol",
 
851
  "Old libdrizzle Client",
852
852
  PLUGIN_LICENSE_GPL,
853
853
  init,             /* Plugin Init */
854
854
  deinit,           /* Plugin Deinit */