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

Commit e42b1a0

Browse files
author
Tianyi Chen
committed
change ssl handshake and connection closing
1 parent 8b3e45a commit e42b1a0

File tree

2 files changed

+65
-73
lines changed

2 files changed

+65
-73
lines changed

src/include/network/network_state.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace network {
2121
enum class ConnState {
2222
READ, // State that reads data from the network
2323
WRITE, // State the writes data to the network
24-
WAIT, // State for waiting for some event to happen
2524
PROCESS, // State that runs the network protocol on received data
2625
CLOSING, // State for closing the client connection
2726
GET_RESULT, // State when triggered by worker thread that completes the task.

src/network/connection_handle.cpp

Lines changed: 65 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ DEF_TRANSITION_GRAPH
9494
END_DEF
9595

9696
DEFINE_STATE(SSL_HANDSHAKE)
97-
ON(WAKEUP) SET_STATE_TO(SSL_HANDSHAKE) AND_WAIT
9897
ON(NEED_DATA) SET_STATE_TO(SSL_HANDSHAKE) AND_WAIT
98+
ON(FINISH) SET_STATE_TO(CLOSING) AND_INVOKE(CloseSocket)
9999
ON(PROCEED) SET_STATE_TO(PROCESS) AND_INVOKE(Process)
100100
END_DEF
101101

@@ -117,6 +117,12 @@ DEF_TRANSITION_GRAPH
117117
ON(WAKEUP) SET_STATE_TO(GET_RESULT) AND_INVOKE(GetResult)
118118
ON(PROCEED) SET_STATE_TO(WRITE) AND_INVOKE(ProcessWrite)
119119
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+
120126
END_DEF
121127

122128
void ConnectionHandle::StateMachine::Accept(Transition action,
@@ -238,7 +244,7 @@ Transition ConnectionHandle::FillReadBuffer() {
238244
}
239245

240246
// return explicitly
241-
while (done == false) {
247+
while (!done) {
242248
if (rbuf_->buf_size == rbuf_->GetMaxSize()) {
243249
// we have filled the whole buffer, exit loop
244250
done = true;
@@ -609,26 +615,22 @@ Transition ConnectionHandle::CloseSocket() {
609615

610616
if (conn_SSL_context != nullptr) {
611617
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) {
615621
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) {
619623
LOG_TRACE("SSL shutdown is not finished yet");
620-
continue;
624+
return Transition::NEED_DATA;
621625
} 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);
628627
}
629628
}
629+
SSL_free(conn_SSL_context);
630+
conn_SSL_context = nullptr;
630631
}
631-
for (;;) {
632+
633+
while(true) {
632634
int status = close(sock_fd_);
633635
if (status < 0) {
634636
// failed close
@@ -642,80 +644,71 @@ Transition ConnectionHandle::CloseSocket() {
642644
}
643645
}
644646

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-
651647
Transition ConnectionHandle::SSL_handshake() {
652648
if (conn_SSL_context == nullptr) {
653-
// TODO(Tianyi) encapsulate this
654649
conn_SSL_context = SSL_new(PelotonServer::ssl_context);
655650
if (conn_SSL_context == nullptr) {
656651
throw NetworkProcessException("ssl context for conn failed");
657652
}
658653
SSL_set_session_id_context(conn_SSL_context, nullptr, 0);
659654
if (SSL_set_fd(conn_SSL_context, sock_fd_) == 0) {
660655
LOG_ERROR("Failed to set SSL fd");
661-
PL_ASSERT(false);
656+
return Transition::FINISH;
662657
}
663658
}
664659

665660
// 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 {
690676
LOG_ERROR(
691677
"Could not accept SSL connection: EOF detected, "
692-
"ssl_error_zero_return, %s",
678+
"ssl_error_ssl, %s",
693679
error_string);
694-
return FINISH;
695680
}
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);
718698
}
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;
719712
}
720713
}
721714
}

0 commit comments

Comments
 (0)