@@ -94,8 +94,8 @@ DEF_TRANSITION_GRAPH
94
94
END_DEF
95
95
96
96
DEFINE_STATE (SSL_HANDSHAKE)
97
- ON (WAKEUP) SET_STATE_TO(SSL_HANDSHAKE) AND_WAIT
98
97
ON (NEED_DATA) SET_STATE_TO(SSL_HANDSHAKE) AND_WAIT
98
+ ON (FINISH) SET_STATE_TO(CLOSING) AND_INVOKE(CloseSocket)
99
99
ON (PROCEED) SET_STATE_TO(PROCESS) AND_INVOKE(Process)
100
100
END_DEF
101
101
@@ -117,6 +117,12 @@ DEF_TRANSITION_GRAPH
117
117
ON (WAKEUP) SET_STATE_TO(GET_RESULT) AND_INVOKE(GetResult)
118
118
ON (PROCEED) SET_STATE_TO(WRITE) AND_INVOKE(ProcessWrite)
119
119
END_DEF
120
+
121
+ DEFINE_STATE (CLOSING)
122
+ ON (PROCEED) SET_STATE_TO(CLOSED) AND_WAIT
123
+ ON (NEED_DATA) SET_STATE_TO(CLOSING) AND_WAIT
124
+ END_DEF
125
+
120
126
END_DEF
121
127
122
128
void ConnectionHandle::StateMachine::Accept (Transition action,
@@ -238,7 +244,7 @@ Transition ConnectionHandle::FillReadBuffer() {
238
244
}
239
245
240
246
// return explicitly
241
- while (done == false ) {
247
+ while (! done) {
242
248
if (rbuf_->buf_size == rbuf_->GetMaxSize ()) {
243
249
// we have filled the whole buffer, exit loop
244
250
done = true ;
@@ -609,26 +615,22 @@ Transition ConnectionHandle::CloseSocket() {
609
615
610
616
if (conn_SSL_context != nullptr ) {
611
617
int shutdown_ret = 0 ;
612
- while ( true ) {
613
- ERR_clear_error ( );
614
- shutdown_ret = SSL_shutdown (conn_SSL_context);
618
+ ERR_clear_error ();
619
+ shutdown_ret = SSL_shutdown (conn_SSL_context );
620
+ if ( shutdown_ret != 0 ) {
615
621
int err = SSL_get_error (conn_SSL_context, shutdown_ret);
616
- if (shutdown_ret == 1 ) {
617
- break ;
618
- } else if (shutdown_ret == 0 ) {
622
+ if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
619
623
LOG_TRACE (" SSL shutdown is not finished yet" );
620
- continue ;
624
+ return Transition::NEED_DATA ;
621
625
} else {
622
- if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
623
- continue ;
624
- } else {
625
- LOG_ERROR (" Error shutting down ssl session, err: %d" , err);
626
- break ;
627
- }
626
+ LOG_ERROR (" Error shutting down ssl session, err: %d" , err);
628
627
}
629
628
}
629
+ SSL_free (conn_SSL_context);
630
+ conn_SSL_context = nullptr ;
630
631
}
631
- for (;;) {
632
+
633
+ while (true ) {
632
634
int status = close (sock_fd_);
633
635
if (status < 0 ) {
634
636
// failed close
@@ -642,80 +644,71 @@ Transition ConnectionHandle::CloseSocket() {
642
644
}
643
645
}
644
646
645
- Transition ConnectionHandle::Wait () {
646
- // TODO(tianyu): Maybe we don't need this state? Also, this name is terrible
647
- UpdateEventFlags (EV_READ | EV_PERSIST);
648
- return Transition::PROCEED;
649
- }
650
-
651
647
Transition ConnectionHandle::SSL_handshake () {
652
648
if (conn_SSL_context == nullptr ) {
653
- // TODO(Tianyi) encapsulate this
654
649
conn_SSL_context = SSL_new (PelotonServer::ssl_context);
655
650
if (conn_SSL_context == nullptr ) {
656
651
throw NetworkProcessException (" ssl context for conn failed" );
657
652
}
658
653
SSL_set_session_id_context (conn_SSL_context, nullptr , 0 );
659
654
if (SSL_set_fd (conn_SSL_context, sock_fd_) == 0 ) {
660
655
LOG_ERROR (" Failed to set SSL fd" );
661
- PL_ASSERT ( false ) ;
656
+ return Transition::FINISH ;
662
657
}
663
658
}
664
659
665
660
// TODO(Yuchen): post-connection verification?
666
- while (true ) {
667
- // clear current thread's error queue before any OpenSSL call
668
- ERR_clear_error ();
669
- int ssl_accept_ret = SSL_accept (conn_SSL_context);
670
- if (ssl_accept_ret > 0 ) {
671
- break ;
672
- }
673
- int err = SSL_get_error (conn_SSL_context, ssl_accept_ret);
674
- int ecode = ERR_get_error ();
675
- char error_string[120 ];
676
- ERR_error_string (ecode, error_string);
677
- switch (err) {
678
- case SSL_ERROR_SSL: {
679
- if (ecode < 0 ) {
680
- LOG_ERROR (" Could not accept SSL connection" );
681
- } else {
682
- LOG_ERROR (
683
- " Could not accept SSL connection: EOF detected, "
684
- " ssl_error_ssl, %s" ,
685
- error_string);
686
- }
687
- return FINISH;
688
- }
689
- case SSL_ERROR_ZERO_RETURN: {
661
+ // clear current thread's error queue before any OpenSSL call
662
+ ERR_clear_error ();
663
+ int ssl_accept_ret = SSL_accept (conn_SSL_context);
664
+ if (ssl_accept_ret > 0 )
665
+ return Transition::PROCEED;
666
+
667
+ int err = SSL_get_error (conn_SSL_context, ssl_accept_ret);
668
+ int ecode = ERR_get_error ();
669
+ char error_string[120 ];
670
+ ERR_error_string (ecode, error_string);
671
+ switch (err) {
672
+ case SSL_ERROR_SSL: {
673
+ if (ecode < 0 ) {
674
+ LOG_ERROR (" Could not accept SSL connection" );
675
+ } else {
690
676
LOG_ERROR (
691
677
" Could not accept SSL connection: EOF detected, "
692
- " ssl_error_zero_return , %s" ,
678
+ " ssl_error_ssl , %s" ,
693
679
error_string);
694
- return FINISH;
695
680
}
696
- case SSL_ERROR_SYSCALL: {
697
- if (ecode < 0 ) {
698
- LOG_ERROR (" Could not accept SSL connection, %s" , error_string);
699
- } else {
700
- LOG_ERROR (
701
- " Could not accept SSL connection: EOF detected, "
702
- " ssl_sys_call, %s" ,
703
- error_string);
704
- }
705
- return FINISH;
706
- }
707
- case SSL_ERROR_WANT_READ: {
708
- UpdateEventFlags (EV_READ | EV_PERSIST);
709
- return Transition::NEED_DATA;
710
- }
711
- case SSL_ERROR_WANT_WRITE: {
712
- UpdateEventFlags (EV_WRITE | EV_PERSIST);
713
- return Transition::NEED_DATA;
714
- }
715
- default : {
716
- LOG_ERROR (" Unrecognized SSL error code: %d" , err);
717
- return FINISH;
681
+ return Transition::FINISH;
682
+ }
683
+ case SSL_ERROR_ZERO_RETURN: {
684
+ LOG_ERROR (
685
+ " Could not accept SSL connection: EOF detected, "
686
+ " ssl_error_zero_return, %s" ,
687
+ error_string);
688
+ return Transition::FINISH;
689
+ }
690
+ case SSL_ERROR_SYSCALL: {
691
+ if (ecode < 0 ) {
692
+ LOG_ERROR (" Could not accept SSL connection, %s" , error_string);
693
+ } else {
694
+ LOG_ERROR (
695
+ " Could not accept SSL connection: EOF detected, "
696
+ " ssl_sys_call, %s" ,
697
+ error_string);
718
698
}
699
+ return Transition::FINISH;
700
+ }
701
+ case SSL_ERROR_WANT_READ: {
702
+ UpdateEventFlags (EV_READ | EV_PERSIST);
703
+ return Transition::NEED_DATA;
704
+ }
705
+ case SSL_ERROR_WANT_WRITE: {
706
+ UpdateEventFlags (EV_WRITE | EV_PERSIST);
707
+ return Transition::NEED_DATA;
708
+ }
709
+ default : {
710
+ LOG_ERROR (" Unrecognized SSL error code: %d" , err);
711
+ return Transition::FINISH;
719
712
}
720
713
}
721
714
}
0 commit comments