From 783bb890a9c7184b8c8acfe7aad6a75fdd60001a Mon Sep 17 00:00:00 2001 From: Martin Hurton Date: Mon, 1 Jul 2013 10:04:54 +0200 Subject: [PATCH] Check socket types during mechanism handshake --- src/curve_client.cpp | 9 -------- src/curve_client.hpp | 3 --- src/curve_server.cpp | 9 -------- src/curve_server.hpp | 3 --- src/mechanism.cpp | 48 +++++++++++++++++++++++++++++++++++++---- src/mechanism.hpp | 4 ++++ src/null_mechanism.cpp | 9 -------- src/null_mechanism.hpp | 4 ---- src/plain_mechanism.cpp | 9 -------- src/plain_mechanism.hpp | 5 ----- tests/test_raw_sock.cpp | 2 +- tests/test_stream.cpp | 4 ++-- 12 files changed, 51 insertions(+), 58 deletions(-) diff --git a/src/curve_client.cpp b/src/curve_client.cpp index e6d931c4..439995aa 100644 --- a/src/curve_client.cpp +++ b/src/curve_client.cpp @@ -404,13 +404,4 @@ int zmq::curve_client_t::process_ready (msg_t *msg_) return rc; } -int zmq::curve_client_t::property (const std::string name, - const void *value, size_t length) -{ - if (name == "Socket-Type") { - // TODO: Implement socket type checking - } - return 0; -} - #endif diff --git a/src/curve_client.hpp b/src/curve_client.hpp index f825ef3a..5f66337d 100644 --- a/src/curve_client.hpp +++ b/src/curve_client.hpp @@ -100,9 +100,6 @@ namespace zmq int process_welcome (msg_t *msg_); int initiate_msg (msg_t *msg_); int process_ready (msg_t *msg_); - - virtual int property (const std::string name, - const void *value, size_t length); }; } diff --git a/src/curve_server.cpp b/src/curve_server.cpp index 967d9657..84426a09 100644 --- a/src/curve_server.cpp +++ b/src/curve_server.cpp @@ -543,15 +543,6 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key) errno_assert (rc == 0); } -int zmq::curve_server_t::property (const std::string name, - const void *value, size_t length) -{ - if (name == "Socket-Type") { - // TODO: Implement socket type checking - } - return 0; -} - int zmq::curve_server_t::receive_and_process_zap_reply () { int rc = 0; diff --git a/src/curve_server.hpp b/src/curve_server.hpp index ba5fc4f2..cfd0d206 100644 --- a/src/curve_server.hpp +++ b/src/curve_server.hpp @@ -107,9 +107,6 @@ namespace zmq void send_zap_request (const uint8_t *key); int receive_and_process_zap_reply (); - - virtual int property (const std::string name, - const void *value, size_t length); }; } diff --git a/src/mechanism.cpp b/src/mechanism.cpp index 2f351ecf..3c410f77 100644 --- a/src/mechanism.cpp +++ b/src/mechanism.cpp @@ -100,12 +100,21 @@ int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_, ptr_ += value_length; bytes_left -= value_length; - const int rc = property (name, value, value_length); - if (rc == -1) - return -1; - if (name == "Identity" && options.recv_identity) set_peer_identity (value, value_length); + else + if (name == "Socket-Type") { + const std::string socket_type ((char *) value, value_length); + if (!check_socket_type (socket_type)) { + errno = EINVAL; + return -1; + } + } + else { + const int rc = property (name, value, value_length); + if (rc == -1) + return -1; + } } if (bytes_left > 0) { errno = EPROTO; @@ -121,3 +130,34 @@ int zmq::mechanism_t::property (const std::string name_, // property values and returns 0 to signal success. return 0; } + +bool zmq::mechanism_t::check_socket_type (const std::string type_) const +{ + switch (options.type) { + case ZMQ_REQ: + return type_ == "REP" || type_ == "ROUTER"; + case ZMQ_REP: + return type_ == "REQ" || type_ == "DEALER"; + case ZMQ_DEALER: + return type_ == "REP" || type_ == "DEALER" || type_ == "ROUTER"; + case ZMQ_ROUTER: + return type_ == "REQ" || type_ == "DEALER" || type_ == "ROUTER"; + case ZMQ_PUSH: + return type_ == "PULL"; + case ZMQ_PULL: + return type_ == "PUSH"; + case ZMQ_PUB: + return type_ == "SUB" || type_ == "XSUB"; + case ZMQ_SUB: + return type_ == "PUB" || type_ == "XPUB"; + case ZMQ_XPUB: + return type_ == "SUB" || type_ == "XSUB"; + case ZMQ_XSUB: + return type_ == "PUB" || type_ == "XPUB"; + case ZMQ_PAIR: + return type_ == "PAIR"; + default: + break; + } + return false; +} diff --git a/src/mechanism.hpp b/src/mechanism.hpp index 77c7749e..243083e8 100644 --- a/src/mechanism.hpp +++ b/src/mechanism.hpp @@ -90,6 +90,10 @@ namespace zmq private: blob_t identity; + + // Returns true iff socket associated with the mechanism + // is compatible with a given socket type 'type_'. + bool check_socket_type (const std::string type_) const; }; } diff --git a/src/null_mechanism.cpp b/src/null_mechanism.cpp index bbc5b660..070f76eb 100644 --- a/src/null_mechanism.cpp +++ b/src/null_mechanism.cpp @@ -116,12 +116,3 @@ bool zmq::null_mechanism_t::is_handshake_complete () const { return ready_command_received && ready_command_sent; } - -int zmq::null_mechanism_t::property (const std::string name, - const void *value, size_t length) -{ - if (name == "Socket-Type") { - // TODO: Implement socket type checking - } - return 0; -} diff --git a/src/null_mechanism.hpp b/src/null_mechanism.hpp index 4947d1f2..2c5d9c27 100644 --- a/src/null_mechanism.hpp +++ b/src/null_mechanism.hpp @@ -40,10 +40,6 @@ namespace zmq virtual int process_handshake_message (msg_t *msg_); virtual bool is_handshake_complete () const; - protected: - - virtual int property (const std::string name, - const void *value, size_t length); private: bool ready_command_sent; diff --git a/src/plain_mechanism.cpp b/src/plain_mechanism.cpp index 7614c977..20405df6 100644 --- a/src/plain_mechanism.cpp +++ b/src/plain_mechanism.cpp @@ -452,12 +452,3 @@ error: return rc; } - -int zmq::plain_mechanism_t::property (const std::string name, - const void *value, size_t length) -{ - if (name == "Socket-Type") { - // TODO: Implement socket type checking - } - return 0; -} diff --git a/src/plain_mechanism.hpp b/src/plain_mechanism.hpp index 3457aebe..abc318d3 100644 --- a/src/plain_mechanism.hpp +++ b/src/plain_mechanism.hpp @@ -43,11 +43,6 @@ namespace zmq virtual int zap_msg_available (); virtual bool is_handshake_complete () const; - protected: - - virtual int property (const std::string name, - const void *value, size_t length); - private: enum state_t { diff --git a/tests/test_raw_sock.cpp b/tests/test_raw_sock.cpp index 747cbb8c..f18d3b4a 100644 --- a/tests/test_raw_sock.cpp +++ b/tests/test_raw_sock.cpp @@ -118,7 +118,7 @@ int main (void) // Announce we are ready memcpy (buffer, "\0\51READY\0", 8); - memcpy (buffer + 8, "\13Socket-Type\0\0\0\6STREAM", 22); + memcpy (buffer + 8, "\13Socket-Type\0\0\0\6ROUTER", 22); memcpy (buffer + 30, "\10Identity\0\0\0\0", 13); // Send Ready command diff --git a/tests/test_stream.cpp b/tests/test_stream.cpp index 3e607e6a..58812b62 100644 --- a/tests/test_stream.cpp +++ b/tests/test_stream.cpp @@ -115,7 +115,7 @@ test_stream_to_dealer (void) // Announce we are ready memcpy (buffer, "\0\51READY\0", 8); - memcpy (buffer + 8, "\13Socket-Type\0\0\0\6STREAM", 22); + memcpy (buffer + 8, "\13Socket-Type\0\0\0\6ROUTER", 22); memcpy (buffer + 30, "\10Identity\0\0\0\0", 13); // Send Ready command @@ -224,4 +224,4 @@ int main (void) { test_stream_to_dealer (); test_stream_to_stream (); -} \ No newline at end of file +}