From ebba815a4da1be65c26769ea10b464ce9f00ec5c Mon Sep 17 00:00:00 2001 From: sigiesec Date: Wed, 16 Aug 2017 15:25:08 +0200 Subject: [PATCH] Problem: duplicate but equivalent state enums in curve_server_t and plain_server_t Solution: pull state enum up to zap_client_t and unify names of enum values --- src/curve_server.cpp | 34 +++++++++++++++++----------------- src/curve_server.hpp | 11 ----------- src/null_mechanism.cpp | 2 +- src/plain_server.cpp | 4 ++-- src/plain_server.hpp | 11 ----------- src/zap_client.hpp | 12 ++++++++++++ 6 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/curve_server.cpp b/src/curve_server.cpp index 9684433f..f462c9e0 100644 --- a/src/curve_server.cpp +++ b/src/curve_server.cpp @@ -43,7 +43,7 @@ zmq::curve_server_t::curve_server_t (session_base_t *session_, const options_t &options_) : mechanism_t (options_), zap_client_t (session_, peer_address_, options_), - state (expect_hello), + state (waiting_for_hello), current_error_detail (no_detail), cn_nonce (1), cn_peer_nonce (1) @@ -66,17 +66,17 @@ int zmq::curve_server_t::next_handshake_command (msg_t *msg_) int rc = 0; switch (state) { - case send_welcome: + case sending_welcome: rc = produce_welcome (msg_); if (rc == 0) - state = expect_initiate; + state = waiting_for_initiate; break; - case send_ready: + case sending_ready: rc = produce_ready (msg_); if (rc == 0) - state = connected; + state = ready; break; - case send_error: + case sending_error: rc = produce_error (msg_); if (rc == 0) state = error_sent; @@ -94,10 +94,10 @@ int zmq::curve_server_t::process_handshake_command (msg_t *msg_) int rc = 0; switch (state) { - case expect_hello: + case waiting_for_hello: rc = process_hello (msg_); break; - case expect_initiate: + case waiting_for_initiate: rc = process_initiate (msg_); break; default: @@ -124,7 +124,7 @@ int zmq::curve_server_t::process_handshake_command (msg_t *msg_) int zmq::curve_server_t::encode (msg_t *msg_) { - zmq_assert (state == connected); + zmq_assert (state == ready); const size_t mlen = crypto_box_ZEROBYTES + 1 + msg_->size (); @@ -176,7 +176,7 @@ int zmq::curve_server_t::encode (msg_t *msg_) int zmq::curve_server_t::decode (msg_t *msg_) { - zmq_assert (state == connected); + zmq_assert (state == ready); if (msg_->size () < 33) { // CURVE I : invalid CURVE client, sent malformed command @@ -250,7 +250,7 @@ int zmq::curve_server_t::zap_msg_available () // TODO I don't think that it is possible that this is called in any // state other than expect_zap_reply. It should be changed to // zmq_assert (state == expect_zap_reply); - if (state != expect_zap_reply) { + if (state != waiting_for_zap_reply) { errno = EFSM; return -1; } @@ -262,7 +262,7 @@ int zmq::curve_server_t::zap_msg_available () zmq::mechanism_t::status_t zmq::curve_server_t::status () const { - if (state == connected) + if (state == ready) return mechanism_t::ready; else if (state == error_sent) @@ -328,7 +328,7 @@ int zmq::curve_server_t::process_hello (msg_t *msg_) return -1; } - state = send_welcome; + state = sending_welcome; return rc; } @@ -528,12 +528,12 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_) handle_zap_status_code (); else if (errno == EAGAIN) - state = expect_zap_reply; + state = waiting_for_zap_reply; else return -1; } else - state = send_ready; + state = sending_ready; return parse_metadata (initiate_plaintext + crypto_box_ZEROBYTES + 128, clen - crypto_box_ZEROBYTES - 128); @@ -618,9 +618,9 @@ void zmq::curve_server_t::handle_zap_status_code () // we can assume here that status_code is a valid ZAP status code, // i.e. 200, 300, 400 or 500 if (status_code [0] == '2') { - state = send_ready; + state = sending_ready; } else { - state = send_error; + state = sending_error; int err = 0; switch (status_code [0]) { diff --git a/src/curve_server.hpp b/src/curve_server.hpp index a92db559..094e4d5b 100644 --- a/src/curve_server.hpp +++ b/src/curve_server.hpp @@ -79,17 +79,6 @@ namespace zmq private: - enum state_t { - expect_hello, - send_welcome, - expect_initiate, - expect_zap_reply, - send_ready, - send_error, - error_sent, - connected - }; - // Current FSM state state_t state; diff --git a/src/null_mechanism.cpp b/src/null_mechanism.cpp index 4876719e..ff856380 100644 --- a/src/null_mechanism.cpp +++ b/src/null_mechanism.cpp @@ -181,7 +181,7 @@ zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const ready_command_received || error_command_received; if (ready_command_sent && ready_command_received) - return ready; + return mechanism_t::ready; else if (command_sent && command_received) return error; diff --git a/src/plain_server.cpp b/src/plain_server.cpp index 5135ea91..08cd05fd 100644 --- a/src/plain_server.cpp +++ b/src/plain_server.cpp @@ -68,7 +68,7 @@ int zmq::plain_server_t::next_handshake_command (msg_t *msg_) case sending_error: rc = produce_error (msg_); if (rc == 0) - state = error_command_sent; + state = error_sent; break; default: errno = EAGAIN; @@ -109,7 +109,7 @@ zmq::mechanism_t::status_t zmq::plain_server_t::status () const if (state == ready) return mechanism_t::ready; else - if (state == error_command_sent) + if (state == error_sent) return mechanism_t::error; else return mechanism_t::handshaking; diff --git a/src/plain_server.hpp b/src/plain_server.hpp index e50de83a..23b45299 100644 --- a/src/plain_server.hpp +++ b/src/plain_server.hpp @@ -57,17 +57,6 @@ namespace zmq private: - enum state_t { - waiting_for_hello, - sending_welcome, - waiting_for_initiate, - sending_ready, - waiting_for_zap_reply, - sending_error, - error_command_sent, - ready - }; - state_t state; int produce_welcome (msg_t *msg_) const; diff --git a/src/zap_client.hpp b/src/zap_client.hpp index 74d3bf6c..a8b4e59b 100644 --- a/src/zap_client.hpp +++ b/src/zap_client.hpp @@ -63,6 +63,18 @@ class zap_client_t : public virtual mechanism_t // Status code as received from ZAP handler std::string status_code; + + enum state_t + { + waiting_for_hello, + sending_welcome, + waiting_for_initiate, + waiting_for_zap_reply, + sending_ready, + sending_error, + error_sent, + ready + }; }; }