From 0be8144176f766d6ac9092568e488c6f4cb499b9 Mon Sep 17 00:00:00 2001 From: Martin Hurton Date: Tue, 6 May 2014 17:07:50 +0200 Subject: [PATCH 1/2] Update mechanism API so we can check for ERROR status --- src/curve_client.cpp | 4 ++-- src/curve_client.hpp | 2 +- src/curve_server.cpp | 4 ++-- src/curve_server.hpp | 2 +- src/mechanism.hpp | 10 ++++++++-- src/null_mechanism.cpp | 7 +++++-- src/null_mechanism.hpp | 2 +- src/plain_mechanism.cpp | 4 ++-- src/plain_mechanism.hpp | 2 +- src/stream_engine.cpp | 34 ++++++++++++++++++++-------------- 10 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/curve_client.cpp b/src/curve_client.cpp index c02277d7..71db2de5 100644 --- a/src/curve_client.cpp +++ b/src/curve_client.cpp @@ -206,9 +206,9 @@ int zmq::curve_client_t::decode (msg_t *msg_) return rc; } -bool zmq::curve_client_t::is_handshake_complete () const +zmq::mechanism_t::status_t zmq::curve_client_t::status () const { - return state == connected; + return state == connected? mechanism_t::ready: mechanism_t::handshaking; } int zmq::curve_client_t::produce_hello (msg_t *msg_) diff --git a/src/curve_client.hpp b/src/curve_client.hpp index 26560bd4..05f81291 100644 --- a/src/curve_client.hpp +++ b/src/curve_client.hpp @@ -59,7 +59,7 @@ namespace zmq virtual int process_handshake_command (msg_t *msg_); virtual int encode (msg_t *msg_); virtual int decode (msg_t *msg_); - virtual bool is_handshake_complete () const; + virtual status_t status () const; private: diff --git a/src/curve_server.cpp b/src/curve_server.cpp index 4badef05..15196b90 100644 --- a/src/curve_server.cpp +++ b/src/curve_server.cpp @@ -237,9 +237,9 @@ int zmq::curve_server_t::zap_msg_available () return rc; } -bool zmq::curve_server_t::is_handshake_complete () const +zmq::mechanism_t::status_t zmq::curve_server_t::status () const { - return state == connected; + return state == connected? mechanism_t::ready: mechanism_t::handshaking; } int zmq::curve_server_t::process_hello (msg_t *msg_) diff --git a/src/curve_server.hpp b/src/curve_server.hpp index 7b966b63..261720c8 100644 --- a/src/curve_server.hpp +++ b/src/curve_server.hpp @@ -64,7 +64,7 @@ namespace zmq virtual int encode (msg_t *msg_); virtual int decode (msg_t *msg_); virtual int zap_msg_available (); - virtual bool is_handshake_complete () const; + virtual status_t status () const; private: diff --git a/src/mechanism.hpp b/src/mechanism.hpp index 8c8e3ad5..a3b43d4a 100644 --- a/src/mechanism.hpp +++ b/src/mechanism.hpp @@ -37,6 +37,12 @@ namespace zmq { public: + enum status_t { + handshaking, + ready, + error + }; + mechanism_t (const options_t &options_); virtual ~mechanism_t (); @@ -54,8 +60,8 @@ namespace zmq // Notifies mechanism about availability of ZAP message. virtual int zap_msg_available () { return 0; } - // True iff the handshake stage is complete? - virtual bool is_handshake_complete () const = 0; + // Returns the status of this mechanism. + virtual status_t status () const = 0; void set_peer_identity (const void *id_ptr, size_t id_size); diff --git a/src/null_mechanism.cpp b/src/null_mechanism.cpp index 141c4dff..0edbe334 100644 --- a/src/null_mechanism.cpp +++ b/src/null_mechanism.cpp @@ -152,9 +152,12 @@ int zmq::null_mechanism_t::zap_msg_available () return rc; } -bool zmq::null_mechanism_t::is_handshake_complete () const +zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const { - return ready_command_received && ready_command_sent; + if (ready_command_received && ready_command_sent) + return mechanism_t::ready; + else + return mechanism_t::handshaking; } void zmq::null_mechanism_t::send_zap_request () diff --git a/src/null_mechanism.hpp b/src/null_mechanism.hpp index 6912e749..32307d1c 100644 --- a/src/null_mechanism.hpp +++ b/src/null_mechanism.hpp @@ -42,7 +42,7 @@ namespace zmq virtual int next_handshake_command (msg_t *msg_); virtual int process_handshake_command (msg_t *msg_); virtual int zap_msg_available (); - virtual bool is_handshake_complete () const; + virtual status_t status () const; private: diff --git a/src/plain_mechanism.cpp b/src/plain_mechanism.cpp index f6caa112..60857678 100644 --- a/src/plain_mechanism.cpp +++ b/src/plain_mechanism.cpp @@ -118,9 +118,9 @@ int zmq::plain_mechanism_t::process_handshake_command (msg_t *msg_) return rc; } -bool zmq::plain_mechanism_t::is_handshake_complete () const +zmq::mechanism_t::status_t zmq::plain_mechanism_t::status () const { - return state == ready; + return state == ready? mechanism_t::ready: mechanism_t::handshaking; } int zmq::plain_mechanism_t::zap_msg_available () diff --git a/src/plain_mechanism.hpp b/src/plain_mechanism.hpp index d43c5442..df44b817 100644 --- a/src/plain_mechanism.hpp +++ b/src/plain_mechanism.hpp @@ -42,7 +42,7 @@ namespace zmq virtual int next_handshake_command (msg_t *msg_); virtual int process_handshake_command (msg_t *msg_); virtual int zap_msg_available (); - virtual bool is_handshake_complete () const; + virtual status_t status () const; private: diff --git a/src/stream_engine.cpp b/src/stream_engine.cpp index 0a0a5d66..f125b8b9 100644 --- a/src/stream_engine.cpp +++ b/src/stream_engine.cpp @@ -670,17 +670,21 @@ int zmq::stream_engine_t::next_handshake_command (msg_t *msg_) { zmq_assert (mechanism != NULL); - const int rc = mechanism->next_handshake_command (msg_); - if (rc == 0) { - msg_->set_flags (msg_t::command); - if (mechanism->is_handshake_complete ()) - mechanism_ready (); + if (mechanism->status () == mechanism_t::ready) { + mechanism_ready (); + return pull_and_encode (msg_); + } + else + if (mechanism->status () == mechanism_t::error) { + errno = EPROTO; + return -1; + } + else { + const int rc = mechanism->next_handshake_command (msg_); + if (rc == 0) + msg_->set_flags (msg_t::command); + return rc; } - // TODO: - // if (errno == EPROTO || errno == EACCES) - // return ERROR command to client - - return rc; } int zmq::stream_engine_t::process_handshake_command (msg_t *msg_) @@ -688,14 +692,16 @@ int zmq::stream_engine_t::process_handshake_command (msg_t *msg_) zmq_assert (mechanism != NULL); const int rc = mechanism->process_handshake_command (msg_); if (rc == 0) { - if (mechanism->is_handshake_complete ()) + if (mechanism->status () == mechanism_t::ready) mechanism_ready (); + else + if (mechanism->status () == mechanism_t::error) { + errno = EPROTO; + return -1; + } if (output_stopped) restart_output (); } - // TODO: - // if (errno == EPROTO || errno == EACCES) - // return ERROR command to client return rc; } From 43d8252446a8844d6afd05057c44d15eb7cb5e5b Mon Sep 17 00:00:00 2001 From: Martin Hurton Date: Tue, 6 May 2014 17:49:26 +0200 Subject: [PATCH 2/2] Update gssapi mechanism --- src/gssapi_client.cpp | 4 ++-- src/gssapi_client.hpp | 2 +- src/gssapi_server.cpp | 4 ++-- src/gssapi_server.hpp | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/gssapi_client.cpp b/src/gssapi_client.cpp index 91cb342a..607af3d0 100644 --- a/src/gssapi_client.cpp +++ b/src/gssapi_client.cpp @@ -153,9 +153,9 @@ int zmq::gssapi_client_t::decode (msg_t *msg_) return 0; } -bool zmq::gssapi_client_t::is_handshake_complete () const +zmq::mechanism_t::status_t zmq::gssapi_client_t::status () const { - return state == connected; + return state == connected? mechanism_t::ready: mechanism_t::handshaking; } int zmq::gssapi_client_t::initialize_context () diff --git a/src/gssapi_client.hpp b/src/gssapi_client.hpp index a7ef227b..b6253921 100644 --- a/src/gssapi_client.hpp +++ b/src/gssapi_client.hpp @@ -42,7 +42,7 @@ namespace zmq virtual int process_handshake_command (msg_t *msg_); virtual int encode (msg_t *msg_); virtual int decode (msg_t *msg_); - virtual bool is_handshake_complete () const; + virtual status_t status () const; private: diff --git a/src/gssapi_server.cpp b/src/gssapi_server.cpp index f0fb8ef9..4c10a392 100644 --- a/src/gssapi_server.cpp +++ b/src/gssapi_server.cpp @@ -315,9 +315,9 @@ int zmq::gssapi_server_t::zap_msg_available () return rc; } -bool zmq::gssapi_server_t::is_handshake_complete () const +zmq::mechanism_t::status_t zmq::gssapi_server_t::status () const { - return state == connected; + return state == connected? mechanism_t::ready: mechanism_t::handshaking; } int zmq::gssapi_server_t::produce_next_token (msg_t *msg_) diff --git a/src/gssapi_server.hpp b/src/gssapi_server.hpp index 84e9a5d7..55c706f2 100644 --- a/src/gssapi_server.hpp +++ b/src/gssapi_server.hpp @@ -46,7 +46,7 @@ namespace zmq virtual int encode (msg_t *msg_); virtual int decode (msg_t *msg_); virtual int zap_msg_available (); - virtual bool is_handshake_complete () const; + virtual status_t status () const; private: