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

Commit 527176e

Browse files
author
Tianyi Chen
committed
fix bug that SSL response should only contain the type (even without length)
1 parent 8d237c3 commit 527176e

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/include/network/marshal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ struct OutputPacket {
138138
size_t ptr; // ByteBuf cursor, which is used for get and put
139139
NetworkMessageType msg_type; // header
140140

141+
bool single_type_pkt; // there would be only a pkt type being written to the
142+
// buffer when this flag is true
141143
bool skip_header_write; // whether we should write header to socket wbuf
142144
size_t write_ptr; // cursor used to write packet content to socket wbuf
143145

@@ -146,6 +148,7 @@ struct OutputPacket {
146148
buf.resize(BUFFER_INIT_SIZE);
147149
buf.shrink_to_fit();
148150
buf.clear();
151+
single_type_pkt = false;
149152
len = ptr = write_ptr = 0;
150153
msg_type = NetworkMessageType::NULL_COMMAND;
151154
skip_header_write = true;

src/network/connection_handle.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ DEF_TRANSITION_GRAPH
121121
END_DEF
122122

123123
END_DEF
124-
// clang-format on
124+
// clang-format on
125125

126-
void ConnectionHandle::StateMachine::Accept(Transition action,
127-
ConnectionHandle &connection) {
126+
void ConnectionHandle::StateMachine::Accept(Transition action,
127+
ConnectionHandle &connection) {
128128
Transition next = action;
129129
while (next != Transition::NONE) {
130130
transition_result result = Delta_(current_state_, next);
@@ -466,16 +466,17 @@ WriteState ConnectionHandle::BufferWriteBytesHeader(OutputPacket *pkt) {
466466
wbuf_->buf[wbuf_->buf_ptr++] = type;
467467
}
468468

469-
// make len include its field size as well
470-
len_nb = htonl(len + sizeof(int32_t));
469+
if (!pkt->single_type_pkt) {
470+
// make len include its field size as well
471+
len_nb = htonl(len + sizeof(int32_t));
471472

472-
// TODO (Tianyi) Check why we need finish startup packet before
473-
// append the bytes of this integer in network-byte order
474-
std::copy(reinterpret_cast<uchar *>(&len_nb),
475-
reinterpret_cast<uchar *>(&len_nb) + 4,
476-
std::begin(wbuf_->buf) + wbuf_->buf_ptr);
477-
// move the write buffer pointer and update size of the socket buffer
478-
wbuf_->buf_ptr += sizeof(int32_t);
473+
// append the bytes of this integer in network-byte order
474+
std::copy(reinterpret_cast<uchar *>(&len_nb),
475+
reinterpret_cast<uchar *>(&len_nb) + 4,
476+
std::begin(wbuf_->buf) + wbuf_->buf_ptr);
477+
// move the write buffer pointer and update size of the socket buffer
478+
wbuf_->buf_ptr += sizeof(int32_t);
479+
}
479480

480481
wbuf_->buf_size = wbuf_->buf_ptr;
481482

@@ -558,7 +559,7 @@ Transition ConnectionHandle::CloseSocket() {
558559
conn_SSL_context = nullptr;
559560
}
560561

561-
while(true) {
562+
while (true) {
562563
int status = close(sock_fd_);
563564
if (status < 0) {
564565
// failed close
@@ -601,8 +602,7 @@ Transition ConnectionHandle::SSLHandshake() {
601602
// clear current thread's error queue before any OpenSSL call
602603
ERR_clear_error();
603604
int ssl_accept_ret = SSL_accept(conn_SSL_context);
604-
if (ssl_accept_ret > 0)
605-
return Transition::PROCEED;
605+
if (ssl_accept_ret > 0) return Transition::PROCEED;
606606

607607
int err = SSL_get_error(conn_SSL_context, ssl_accept_ret);
608608
int ecode = ERR_get_error();

src/network/postgres_protocol_handler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const std::unordered_map<std::string, std::string>
5858

5959
PostgresProtocolHandler::PostgresProtocolHandler(tcop::TrafficCop *traffic_cop)
6060
: ProtocolHandler(traffic_cop),
61+
init_stage_(true),
6162
txn_state_(NetworkTransactionStateType::IDLE) {}
6263

6364
PostgresProtocolHandler::~PostgresProtocolHandler() {}
@@ -976,15 +977,16 @@ ProcessResult PostgresProtocolHandler::ProcessInitialPacket(InputPacket *pkt) {
976977
int32_t proto_version = PacketGetInt(pkt, sizeof(int32_t));
977978
LOG_INFO("protocol version: %d", proto_version);
978979

980+
force_flush_ = true;
979981
// TODO(Yuchen): consider more about return value
980982
if (proto_version == SSL_MESSAGE_VERNO) {
981983
LOG_TRACE("process SSL MESSAGE");
982984
std::unique_ptr<OutputPacket> response(new OutputPacket());
983985
bool ssl_able = (PelotonServer::GetSSLLevel() != SSLLevel::SSL_DISABLE);
984986
response->msg_type =
985987
ssl_able ? NetworkMessageType::SSL_YES : NetworkMessageType::SSL_NO;
988+
response->single_type_pkt = true;
986989
responses_.push_back(std::move(response));
987-
force_flush_ = true;
988990
return ssl_able ? ProcessResult::NEED_SSL_HANDSHAKE
989991
: ProcessResult::COMPLETE;
990992
} else {

0 commit comments

Comments
 (0)