~mohyt/drizzle/drizzle-alsosql

« back to all changes in this revision

Viewing changes to libdrizzle/handshake.cc

  • Committer: Brian Aker
  • Date: 2012-05-19 01:48:46 UTC
  • mfrom: (2553.2.1 workspace)
  • Revision ID: brian@tangent.org-20120519014846-cuitgiy2s4gqdrr4
Merge of build tree (Andrew's SSL patch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
  {
64
64
    drizzle_state_push(con, drizzle_state_write);
65
65
    drizzle_state_push(con, drizzle_state_handshake_client_write);
 
66
 
 
67
    if (con->ssl)
 
68
    {
 
69
      drizzle_state_push(con, drizzle_state_write);
 
70
      drizzle_state_push(con, drizzle_state_handshake_ssl_client_write);
 
71
    }
66
72
  }
67
73
 
68
74
  return drizzle_state_loop(con);
226
232
    drizzle_state_push(con, drizzle_state_packet_read);
227
233
    drizzle_state_push(con, drizzle_state_write);
228
234
    drizzle_state_push(con, drizzle_state_handshake_client_write);
 
235
    if (con->ssl)
 
236
    {
 
237
      drizzle_state_push(con, drizzle_state_write);
 
238
      drizzle_state_push(con, drizzle_state_handshake_ssl_client_write);
 
239
    }
229
240
  }
230
241
 
231
242
  return DRIZZLE_RETURN_OK;
491
502
  return DRIZZLE_RETURN_OK;
492
503
}
493
504
 
494
 
drizzle_return_t drizzle_state_handshake_client_write(drizzle_con_st *con)
 
505
int drizzle_compile_capabilities(drizzle_con_st *con)
495
506
{
496
 
  uint8_t *ptr;
497
507
  int capabilities;
498
 
  drizzle_return_t ret;
499
 
 
500
 
  if (con == NULL)
501
 
  {
502
 
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
503
 
  }
504
 
  drizzle_log_debug(con->drizzle, "drizzle_state_handshake_client_write");
505
 
 
506
 
  /* Calculate max packet size. */
507
 
  con->packet_size= 4   /* Capabilities */
508
 
                  + 4   /* Max packet size */
509
 
                  + 1   /* Charset */
510
 
                  + 23  /* Unused */
511
 
                  + strlen(con->user) + 1
512
 
                  + 1   /* Scramble size */
513
 
                  + DRIZZLE_MAX_SCRAMBLE_SIZE
514
 
                  + strlen(con->db) + 1;
515
 
 
516
 
  /* Assume the entire handshake packet will fit in the buffer. */
517
 
  if ((con->packet_size + 4) > DRIZZLE_MAX_BUFFER_SIZE)
518
 
  {
519
 
    drizzle_set_error(con->drizzle, "drizzle_state_handshake_client_write",
520
 
                      "buffer too small:%zu", con->packet_size + 4);
521
 
    return DRIZZLE_RETURN_INTERNAL_ERROR;
522
 
  }
523
 
 
524
 
  ptr= con->buffer_ptr;
525
 
 
526
 
  /* Store packet size at the end since it may change. */
527
 
  ptr[3]= con->packet_number;
528
 
  con->packet_number++;
529
 
  ptr+= 4;
530
508
 
531
509
  if (con->options & DRIZZLE_CON_MYSQL)
532
510
    con->capabilities|= DRIZZLE_CAPABILITIES_PROTOCOL_41;
550
528
    capabilities|= int(DRIZZLE_CAPABILITIES_PLUGIN_AUTH);
551
529
  }
552
530
 
553
 
  capabilities&= ~(int(DRIZZLE_CAPABILITIES_COMPRESS) | int(DRIZZLE_CAPABILITIES_SSL));
 
531
  if (con->ssl)
 
532
  {
 
533
    capabilities|= int(DRIZZLE_CAPABILITIES_SSL);
 
534
  }
 
535
 
 
536
  capabilities&= ~(int(DRIZZLE_CAPABILITIES_COMPRESS));
554
537
  if (con->db[0] == 0)
555
538
    capabilities&= ~int(DRIZZLE_CAPABILITIES_CONNECT_WITH_DB);
556
539
 
 
540
  return capabilities;
 
541
}
 
542
 
 
543
drizzle_return_t drizzle_state_handshake_client_write(drizzle_con_st *con)
 
544
{
 
545
  uint8_t *ptr;
 
546
  int capabilities;
 
547
  int ssl_ret;
 
548
  drizzle_return_t ret;
 
549
 
 
550
  if (con == NULL)
 
551
  {
 
552
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
553
  }
 
554
  drizzle_log_debug(con->drizzle, "drizzle_state_handshake_client_write");
 
555
 
 
556
  if (con->ssl)
 
557
  {
 
558
    ssl_ret= SSL_connect(con->ssl);
 
559
    if (ssl_ret != 1)
 
560
    {
 
561
      drizzle_set_error(con->drizzle, "drizzle_state_handshake_client_write", "SSL error: %d", SSL_get_error(con->ssl, ssl_ret));
 
562
      return DRIZZLE_RETURN_SSL_ERROR;
 
563
    }
 
564
    con->ssl_state= DRIZZLE_SSL_STATE_HANDSHAKE_COMPLETE;
 
565
  }
 
566
 
 
567
  /* Calculate max packet size. */
 
568
  con->packet_size= 4   /* Capabilities */
 
569
                  + 4   /* Max packet size */
 
570
                  + 1   /* Charset */
 
571
                  + 23  /* Unused */
 
572
                  + strlen(con->user) + 1
 
573
                  + 1   /* Scramble size */
 
574
                  + DRIZZLE_MAX_SCRAMBLE_SIZE
 
575
                  + strlen(con->db) + 1;
 
576
 
 
577
  /* Assume the entire handshake packet will fit in the buffer. */
 
578
  if ((con->packet_size + 4) > DRIZZLE_MAX_BUFFER_SIZE)
 
579
  {
 
580
    drizzle_set_error(con->drizzle, "drizzle_state_handshake_client_write",
 
581
                      "buffer too small:%zu", con->packet_size + 4);
 
582
    return DRIZZLE_RETURN_INTERNAL_ERROR;
 
583
  }
 
584
 
 
585
  ptr= con->buffer_ptr;
 
586
 
 
587
  /* Store packet size at the end since it may change. */
 
588
  ptr[3]= con->packet_number;
 
589
  con->packet_number++;
 
590
  ptr+= 4;
 
591
 
 
592
  capabilities= drizzle_compile_capabilities(con);
 
593
 
557
594
  drizzle_set_byte4(ptr, capabilities);
558
595
  ptr+= 4;
559
596
 
588
625
  return DRIZZLE_RETURN_OK;
589
626
}
590
627
 
 
628
drizzle_return_t drizzle_state_handshake_ssl_client_write(drizzle_con_st *con)
 
629
{
 
630
  uint8_t *ptr;
 
631
  int capabilities;
 
632
 
 
633
  drizzle_log_debug(con->drizzle, "drizzle_state_handshake_ssl_client_write");
 
634
 
 
635
  /* SSL handshake packet structure */
 
636
  con->packet_size= 4   /* Capabilities */
 
637
                  + 4   /* Max packet size */
 
638
                  + 1   /* Charset */
 
639
                  + 23; /* Padding unused bytes */
 
640
 
 
641
  ptr= con->buffer_ptr;
 
642
  drizzle_set_byte3(ptr, con->packet_size);
 
643
  ptr[3]= con->packet_number;
 
644
  con->packet_number++;
 
645
  ptr+= 4;
 
646
 
 
647
  capabilities= drizzle_compile_capabilities(con);
 
648
  drizzle_set_byte4(ptr, capabilities);
 
649
  ptr+= 4;
 
650
  drizzle_set_byte4(ptr, con->max_packet_size);
 
651
  ptr+= 4;
 
652
 
 
653
  ptr[0]= con->charset;
 
654
 
 
655
  con->buffer_size+= con->packet_size + 4;
 
656
  ptr++;
 
657
 
 
658
  memset(ptr, 0, 23);
 
659
 
 
660
  drizzle_state_pop(con);
 
661
  return DRIZZLE_RETURN_OK;
 
662
}
 
663
 
591
664
drizzle_return_t drizzle_state_handshake_result_read(drizzle_con_st *con)
592
665
{
593
666
  if (con == NULL)