Commit 783bb890 authored by Martin Hurton's avatar Martin Hurton

Check socket types during mechanism handshake

parent 58b10824
......@@ -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
......@@ -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);
};
}
......
......@@ -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;
......
......@@ -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);
};
}
......
......@@ -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;
}
......@@ -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;
};
}
......
......@@ -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;
}
......@@ -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;
......
......@@ -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;
}
......@@ -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 {
......
......@@ -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
......
......@@ -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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment