@@ -649,7 +649,6 @@ Transition ConnectionHandle::Wait() {
649
649
}
650
650
651
651
Transition ConnectionHandle::SSL_handshake () {
652
-
653
652
if (conn_SSL_context == nullptr ) {
654
653
// TODO(Tianyi) encapsulate this
655
654
conn_SSL_context = SSL_new (PelotonServer::ssl_context);
@@ -663,9 +662,8 @@ Transition ConnectionHandle::SSL_handshake() {
663
662
}
664
663
}
665
664
666
- bool handshake_fail = false ;
667
- // TODO(Yuchen): post-connection verification?
668
- while (!handshake_fail) {
665
+ // TODO(Yuchen): post-connection verification?
666
+ while (true ) {
669
667
// clear current thread's error queue before any OpenSSL call
670
668
ERR_clear_error ();
671
669
int ssl_accept_ret = SSL_accept (conn_SSL_context);
@@ -686,16 +684,14 @@ Transition ConnectionHandle::SSL_handshake() {
686
684
" ssl_error_ssl, %s" ,
687
685
error_string);
688
686
}
689
- handshake_fail = true ;
690
- break ;
687
+ return FINISH;
691
688
}
692
689
case SSL_ERROR_ZERO_RETURN: {
693
690
LOG_ERROR (
694
691
" Could not accept SSL connection: EOF detected, "
695
692
" ssl_error_zero_return, %s" ,
696
693
error_string);
697
- handshake_fail = true ;
698
- break ;
694
+ return FINISH;
699
695
}
700
696
case SSL_ERROR_SYSCALL: {
701
697
if (ecode < 0 ) {
@@ -706,139 +702,47 @@ Transition ConnectionHandle::SSL_handshake() {
706
702
" ssl_sys_call, %s" ,
707
703
error_string);
708
704
}
709
- handshake_fail = true ;
710
- break ;
705
+ return FINISH;
711
706
}
712
707
case SSL_ERROR_WANT_READ: {
713
708
UpdateEventFlags (EV_READ | EV_PERSIST);
714
- break ;
709
+ return Transition::NEED_DATA ;
715
710
}
716
711
case SSL_ERROR_WANT_WRITE: {
717
712
UpdateEventFlags (EV_WRITE | EV_PERSIST);
718
- break ;
713
+ return Transition::NEED_DATA ;
719
714
}
720
715
default : {
721
716
LOG_ERROR (" Unrecognized SSL error code: %d" , err);
722
- handshake_fail = true ;
717
+ return FINISH ;
723
718
}
724
719
}
725
720
}
726
- if (!handshake_fail) {
727
- // handshake succeeds, reset ssl_sent_
728
- ssl_handshake_ = false ;
729
- finish_startup_packet_ = false ;
730
- return Transition::NEED_DATA;
731
- } else {
732
- throw NetworkProcessException (" SSL handshake failure" );
733
- }
734
-
735
-
736
721
}
737
722
738
723
Transition ConnectionHandle::Process () {
739
- // TODO(tianyu): further simplify this logic
740
- if (!finish_startup_packet_) {
741
- // Process initial
742
- if (ssl_handshake_) {
743
- // start SSL handshake
744
- conn_SSL_context = SSL_new (PelotonServer::ssl_context);
745
- SSL_set_session_id_context (conn_SSL_context, nullptr , 0 );
746
- if (SSL_set_fd (conn_SSL_context, sock_fd_) == 0 ) {
747
- LOG_ERROR (" Failed to set SSL fd" );
748
- PL_ASSERT (false );
749
- }
750
-
751
- bool handshake_fail = false ;
752
- // TODO(Yuchen): post-connection verification?
753
- while (!handshake_fail) {
754
- // clear current thread's error queue before any OpenSSL call
755
- ERR_clear_error ();
756
- int ssl_accept_ret = SSL_accept (conn_SSL_context);
757
- if (ssl_accept_ret > 0 ) {
758
- break ;
759
- }
760
- int err = SSL_get_error (conn_SSL_context, ssl_accept_ret);
761
- int ecode = ERR_get_error ();
762
- char error_string[120 ];
763
- ERR_error_string (ecode, error_string);
764
- switch (err) {
765
- case SSL_ERROR_SSL: {
766
- if (ecode < 0 ) {
767
- LOG_ERROR (" Could not accept SSL connection" );
768
- } else {
769
- LOG_ERROR (
770
- " Could not accept SSL connection: EOF detected, "
771
- " ssl_error_ssl, %s" ,
772
- error_string);
773
- }
774
- handshake_fail = true ;
775
- break ;
776
- }
777
- case SSL_ERROR_ZERO_RETURN: {
778
- LOG_ERROR (
779
- " Could not accept SSL connection: EOF detected, "
780
- " ssl_error_zero_return, %s" ,
781
- error_string);
782
- handshake_fail = true ;
783
- break ;
784
- }
785
- case SSL_ERROR_SYSCALL: {
786
- if (ecode < 0 ) {
787
- LOG_ERROR (" Could not accept SSL connection, %s" , error_string);
788
- } else {
789
- LOG_ERROR (
790
- " Could not accept SSL connection: EOF detected, "
791
- " ssl_sys_call, %s" ,
792
- error_string);
793
- }
794
- handshake_fail = true ;
795
- break ;
796
- }
797
- case SSL_ERROR_WANT_READ:
798
- case SSL_ERROR_WANT_WRITE:
799
- break ;
800
- default : {
801
- LOG_ERROR (" Unrecognized SSL error code: %d" , err);
802
- handshake_fail = true ;
803
- }
804
- }
805
- }
806
- if (!handshake_fail) {
807
- // handshake succeeds, reset ssl_sent_
808
- ssl_handshake_ = false ;
809
- finish_startup_packet_ = false ;
810
- return Transition::NEED_DATA;
811
- } else {
812
- throw NetworkProcessException (" SSL handshake failure" );
813
- }
814
- }
724
+ if (protocol_handler_ == nullptr ) {
725
+ // TODO(Tianyi) Check the rbuf here before we create one if we have
726
+ // another protocol handler
727
+ protocol_handler_ = ProtocolHandlerFactory::CreateProtocolHandler (
728
+ ProtocolHandlerType::Postgres, &traffic_cop_);
729
+ }
815
730
816
- switch (ProcessInitial ()) {
817
- case ProcessResult::COMPLETE:
818
- return Transition::PROCEED;
819
- case ProcessResult::MORE_DATA_REQUIRED:
820
- return Transition::NEED_DATA;
821
- default : // PROCESSING is impossible to happens in initial packets
822
- throw NetworkProcessException (" Should not reach" );
823
- }
824
- } else {
825
- ProcessResult status =
826
- protocol_handler_->Process (*rbuf_, (size_t )handler_->Id ());
731
+ ProcessResult status =
732
+ protocol_handler_->Process (*rbuf_, (size_t )handler_->Id ());
827
733
828
- switch (status) {
829
- case ProcessResult::MORE_DATA_REQUIRED:
830
- return Transition::NEED_DATA;
831
- case ProcessResult::COMPLETE:
832
- return Transition::PROCEED;
833
- case ProcessResult::PROCESSING:
834
- EventUtil::EventDel (network_event);
835
- LOG_TRACE (" ProcessResult: queueing" );
836
- return Transition::GET_RESULT;
837
- case ProcessResult::TERMINATE:
838
- return Transition::FINISH;
839
- }
734
+ switch (status) {
735
+ case ProcessResult::MORE_DATA_REQUIRED:
736
+ return Transition::NEED_DATA;
737
+ case ProcessResult::COMPLETE:
738
+ return Transition::PROCEED;
739
+ case ProcessResult::PROCESSING:
740
+ EventUtil::EventDel (network_event);
741
+ LOG_TRACE (" ProcessResult: queueing" );
742
+ return Transition::GET_RESULT;
743
+ case ProcessResult::TERMINATE:
744
+ return Transition::FINISH;
840
745
}
841
- throw NetworkProcessException (" Unexpected process result" );
842
746
}
843
747
844
748
Transition ConnectionHandle::ProcessWrite () {
0 commit comments