Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 8b3e45a

Browse files
author
Tianyi Chen
committed
some fix, need to be merge with others
1 parent 8117b24 commit 8b3e45a

File tree

2 files changed

+29
-124
lines changed

2 files changed

+29
-124
lines changed

src/include/network/network_state.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ enum class Transition {
4747
// TODO(tianyu) generalize this symbol, this is currently only used in process
4848
GET_RESULT,
4949
FINISH,
50-
RETRY
50+
RETRY,
51+
NEED_SSL_HANDSHAKE
5152
};
5253
}
5354
}

src/network/connection_handle.cpp

Lines changed: 27 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,6 @@ Transition ConnectionHandle::Wait() {
649649
}
650650

651651
Transition ConnectionHandle::SSL_handshake() {
652-
653652
if (conn_SSL_context == nullptr) {
654653
// TODO(Tianyi) encapsulate this
655654
conn_SSL_context = SSL_new(PelotonServer::ssl_context);
@@ -663,9 +662,8 @@ Transition ConnectionHandle::SSL_handshake() {
663662
}
664663
}
665664

666-
bool handshake_fail = false;
667-
// TODO(Yuchen): post-connection verification?
668-
while (!handshake_fail) {
665+
// TODO(Yuchen): post-connection verification?
666+
while (true) {
669667
// clear current thread's error queue before any OpenSSL call
670668
ERR_clear_error();
671669
int ssl_accept_ret = SSL_accept(conn_SSL_context);
@@ -686,16 +684,14 @@ Transition ConnectionHandle::SSL_handshake() {
686684
"ssl_error_ssl, %s",
687685
error_string);
688686
}
689-
handshake_fail = true;
690-
break;
687+
return FINISH;
691688
}
692689
case SSL_ERROR_ZERO_RETURN: {
693690
LOG_ERROR(
694691
"Could not accept SSL connection: EOF detected, "
695692
"ssl_error_zero_return, %s",
696693
error_string);
697-
handshake_fail = true;
698-
break;
694+
return FINISH;
699695
}
700696
case SSL_ERROR_SYSCALL: {
701697
if (ecode < 0) {
@@ -706,139 +702,47 @@ Transition ConnectionHandle::SSL_handshake() {
706702
"ssl_sys_call, %s",
707703
error_string);
708704
}
709-
handshake_fail = true;
710-
break;
705+
return FINISH;
711706
}
712707
case SSL_ERROR_WANT_READ: {
713708
UpdateEventFlags(EV_READ | EV_PERSIST);
714-
break;
709+
return Transition::NEED_DATA;
715710
}
716711
case SSL_ERROR_WANT_WRITE: {
717712
UpdateEventFlags(EV_WRITE | EV_PERSIST);
718-
break;
713+
return Transition::NEED_DATA;
719714
}
720715
default: {
721716
LOG_ERROR("Unrecognized SSL error code: %d", err);
722-
handshake_fail = true;
717+
return FINISH;
723718
}
724719
}
725720
}
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-
736721
}
737722

738723
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+
}
815730

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());
827733

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;
840745
}
841-
throw NetworkProcessException("Unexpected process result");
842746
}
843747

844748
Transition ConnectionHandle::ProcessWrite() {

0 commit comments

Comments
 (0)