Commit 89e53131 authored by Simon Giesecke's avatar Simon Giesecke

Refactored zmq::stream_engine_t::handshake, extracted several sub-methods

parent c3739ff6
...@@ -490,18 +490,50 @@ bool zmq::stream_engine_t::handshake () ...@@ -490,18 +490,50 @@ bool zmq::stream_engine_t::handshake ()
zmq_assert (_handshaking); zmq_assert (_handshaking);
zmq_assert (_greeting_bytes_read < _greeting_size); zmq_assert (_greeting_bytes_read < _greeting_size);
// Receive the greeting. // Receive the greeting.
const int rc = receive_greeting ();
if (rc == -1)
return false;
const bool unversioned = rc != 0;
// Position of the revision field in the greeting.
const size_t revision_pos = 10;
if (!(this
->*select_handshake_fun (unversioned,
_greeting_recv[revision_pos])) ())
return false;
// Start polling for output if necessary.
if (_outsize == 0)
set_pollout (_handle);
// Handshaking was successful.
// Switch into the normal message flow.
_handshaking = false;
if (_has_handshake_timer) {
cancel_timer (handshake_timer_id);
_has_handshake_timer = false;
}
return true;
}
int zmq::stream_engine_t::receive_greeting ()
{
bool unversioned = false;
while (_greeting_bytes_read < _greeting_size) { while (_greeting_bytes_read < _greeting_size) {
const int n = tcp_read (_s, _greeting_recv + _greeting_bytes_read, const int n = tcp_read (_s, _greeting_recv + _greeting_bytes_read,
_greeting_size - _greeting_bytes_read); _greeting_size - _greeting_bytes_read);
if (n == 0) { if (n == 0) {
errno = EPIPE; errno = EPIPE;
error (connection_error); error (connection_error);
return false; return -1;
} }
if (n == -1) { if (n == -1) {
if (errno != EAGAIN) if (errno != EAGAIN)
error (connection_error); error (connection_error);
return false; return -1;
} }
_greeting_bytes_read += n; _greeting_bytes_read += n;
...@@ -509,8 +541,10 @@ bool zmq::stream_engine_t::handshake () ...@@ -509,8 +541,10 @@ bool zmq::stream_engine_t::handshake ()
// We have received at least one byte from the peer. // We have received at least one byte from the peer.
// If the first byte is not 0xff, we know that the // If the first byte is not 0xff, we know that the
// peer is using unversioned protocol. // peer is using unversioned protocol.
if (_greeting_recv[0] != 0xff) if (_greeting_recv[0] != 0xff) {
unversioned = true;
break; break;
}
if (_greeting_bytes_read < signature_size) if (_greeting_bytes_read < signature_size)
continue; continue;
...@@ -519,10 +553,19 @@ bool zmq::stream_engine_t::handshake () ...@@ -519,10 +553,19 @@ bool zmq::stream_engine_t::handshake ()
// with the 'flags' field if a regular message was sent). // with the 'flags' field if a regular message was sent).
// Zero indicates this is a header of a routing id message // Zero indicates this is a header of a routing id message
// (i.e. the peer is using the unversioned protocol). // (i.e. the peer is using the unversioned protocol).
if (!(_greeting_recv[9] & 0x01)) if (!(_greeting_recv[9] & 0x01)) {
unversioned = true;
break; break;
}
// The peer is using versioned protocol. // The peer is using versioned protocol.
receive_greeting_versioned ();
}
return unversioned ? 1 : 0;
}
void zmq::stream_engine_t::receive_greeting_versioned ()
{
// Send the major version number. // Send the major version number.
if (_outpos + _outsize == _greeting_send + signature_size) { if (_outpos + _outsize == _greeting_send + signature_size) {
if (_outsize == 0) if (_outsize == 0)
...@@ -563,14 +606,29 @@ bool zmq::stream_engine_t::handshake () ...@@ -563,14 +606,29 @@ bool zmq::stream_engine_t::handshake ()
} }
} }
} }
} }
// Position of the revision field in the greeting.
const size_t revision_pos = 10;
zmq::stream_engine_t::handshake_fun_t
zmq::stream_engine_t::select_handshake_fun (bool unversioned,
unsigned char revision)
{
// Is the peer using ZMTP/1.0 with no revision number? // Is the peer using ZMTP/1.0 with no revision number?
// If so, we send and receive rest of routing id message if (unversioned) {
if (_greeting_recv[0] != 0xff || !(_greeting_recv[9] & 0x01)) { return &stream_engine_t::handshake_v1_0_unversioned;
}
switch (revision) {
case ZMTP_1_0:
return &stream_engine_t::handshake_v1_0;
case ZMTP_2_0:
return &stream_engine_t::handshake_v2_0;
default:
return &stream_engine_t::handshake_v3_0;
}
}
bool zmq::stream_engine_t::handshake_v1_0_unversioned ()
{
// We send and receive rest of routing id message
if (_session->zap_enabled ()) { if (_session->zap_enabled ()) {
// reject ZMTP 1.0 connections if ZAP is enabled // reject ZMTP 1.0 connections if ZAP is enabled
error (protocol_error); error (protocol_error);
...@@ -598,7 +656,7 @@ bool zmq::stream_engine_t::handshake () ...@@ -598,7 +656,7 @@ bool zmq::stream_engine_t::handshake ()
zmq_assert (rc == 0); zmq_assert (rc == 0);
memcpy (_tx_msg.data (), _options.routing_id, _options.routing_id_size); memcpy (_tx_msg.data (), _options.routing_id, _options.routing_id_size);
_encoder->load_msg (&_tx_msg); _encoder->load_msg (&_tx_msg);
size_t buffer_size = _encoder->encode (&bufferp, header_size); const size_t buffer_size = _encoder->encode (&bufferp, header_size);
zmq_assert (buffer_size == header_size); zmq_assert (buffer_size == header_size);
// Make sure the decoder sees the data we have already received. // Make sure the decoder sees the data we have already received.
...@@ -617,7 +675,12 @@ bool zmq::stream_engine_t::handshake () ...@@ -617,7 +675,12 @@ bool zmq::stream_engine_t::handshake ()
// We are expecting routing id message. // We are expecting routing id message.
_process_msg = &stream_engine_t::process_routing_id_msg; _process_msg = &stream_engine_t::process_routing_id_msg;
} else if (_greeting_recv[revision_pos] == ZMTP_1_0) {
return true;
}
bool zmq::stream_engine_t::handshake_v1_0 ()
{
if (_session->zap_enabled ()) { if (_session->zap_enabled ()) {
// reject ZMTP 1.0 connections if ZAP is enabled // reject ZMTP 1.0 connections if ZAP is enabled
error (protocol_error); error (protocol_error);
...@@ -630,7 +693,12 @@ bool zmq::stream_engine_t::handshake () ...@@ -630,7 +693,12 @@ bool zmq::stream_engine_t::handshake ()
_decoder = _decoder =
new (std::nothrow) v1_decoder_t (in_batch_size, _options.maxmsgsize); new (std::nothrow) v1_decoder_t (in_batch_size, _options.maxmsgsize);
alloc_assert (_decoder); alloc_assert (_decoder);
} else if (_greeting_recv[revision_pos] == ZMTP_2_0) {
return true;
}
bool zmq::stream_engine_t::handshake_v2_0 ()
{
if (_session->zap_enabled ()) { if (_session->zap_enabled ()) {
// reject ZMTP 2.0 connections if ZAP is enabled // reject ZMTP 2.0 connections if ZAP is enabled
error (protocol_error); error (protocol_error);
...@@ -643,7 +711,12 @@ bool zmq::stream_engine_t::handshake () ...@@ -643,7 +711,12 @@ bool zmq::stream_engine_t::handshake ()
_decoder = new (std::nothrow) _decoder = new (std::nothrow)
v2_decoder_t (in_batch_size, _options.maxmsgsize, _options.zero_copy); v2_decoder_t (in_batch_size, _options.maxmsgsize, _options.zero_copy);
alloc_assert (_decoder); alloc_assert (_decoder);
} else {
return true;
}
bool zmq::stream_engine_t::handshake_v3_0 ()
{
_encoder = new (std::nothrow) v2_encoder_t (out_batch_size); _encoder = new (std::nothrow) v2_encoder_t (out_batch_size);
alloc_assert (_encoder); alloc_assert (_encoder);
...@@ -652,8 +725,8 @@ bool zmq::stream_engine_t::handshake () ...@@ -652,8 +725,8 @@ bool zmq::stream_engine_t::handshake ()
alloc_assert (_decoder); alloc_assert (_decoder);
if (_options.mechanism == ZMQ_NULL if (_options.mechanism == ZMQ_NULL
&& memcmp (_greeting_recv + 12, && memcmp (_greeting_recv + 12, "NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
"NULL\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) 20)
== 0) { == 0) {
_mechanism = new (std::nothrow) _mechanism = new (std::nothrow)
null_mechanism_t (_session, _peer_address, _options); null_mechanism_t (_session, _peer_address, _options);
...@@ -666,8 +739,7 @@ bool zmq::stream_engine_t::handshake () ...@@ -666,8 +739,7 @@ bool zmq::stream_engine_t::handshake ()
_mechanism = new (std::nothrow) _mechanism = new (std::nothrow)
plain_server_t (_session, _peer_address, _options); plain_server_t (_session, _peer_address, _options);
else else
_mechanism = _mechanism = new (std::nothrow) plain_client_t (_session, _options);
new (std::nothrow) plain_client_t (_session, _options);
alloc_assert (_mechanism); alloc_assert (_mechanism);
} }
#ifdef ZMQ_HAVE_CURVE #ifdef ZMQ_HAVE_CURVE
...@@ -679,8 +751,7 @@ bool zmq::stream_engine_t::handshake () ...@@ -679,8 +751,7 @@ bool zmq::stream_engine_t::handshake ()
_mechanism = new (std::nothrow) _mechanism = new (std::nothrow)
curve_server_t (_session, _peer_address, _options); curve_server_t (_session, _peer_address, _options);
else else
_mechanism = _mechanism = new (std::nothrow) curve_client_t (_session, _options);
new (std::nothrow) curve_client_t (_session, _options);
alloc_assert (_mechanism); alloc_assert (_mechanism);
} }
#endif #endif
...@@ -707,20 +778,6 @@ bool zmq::stream_engine_t::handshake () ...@@ -707,20 +778,6 @@ bool zmq::stream_engine_t::handshake ()
} }
_next_msg = &stream_engine_t::next_handshake_command; _next_msg = &stream_engine_t::next_handshake_command;
_process_msg = &stream_engine_t::process_handshake_command; _process_msg = &stream_engine_t::process_handshake_command;
}
// Start polling for output if necessary.
if (_outsize == 0)
set_pollout (_handle);
// Handshaking was successful.
// Switch into the normal message flow.
_handshaking = false;
if (_has_handshake_timer) {
cancel_timer (handshake_timer_id);
_has_handshake_timer = false;
}
return true; return true;
} }
......
...@@ -93,12 +93,22 @@ class stream_engine_t : public io_object_t, public i_engine ...@@ -93,12 +93,22 @@ class stream_engine_t : public io_object_t, public i_engine
// Function to handle network disconnections. // Function to handle network disconnections.
void error (error_reason_t reason_); void error (error_reason_t reason_);
// Receives the greeting message from the peer.
int receive_greeting ();
// Detects the protocol used by the peer. // Detects the protocol used by the peer.
bool handshake (); bool handshake ();
// Receive the greeting from the peer.
int receive_greeting ();
void receive_greeting_versioned ();
typedef bool (stream_engine_t::*handshake_fun_t) ();
static handshake_fun_t select_handshake_fun (bool unversioned,
unsigned char revision);
bool handshake_v1_0_unversioned ();
bool handshake_v1_0 ();
bool handshake_v2_0 ();
bool handshake_v3_0 ();
int routing_id_msg (msg_t *msg_); int routing_id_msg (msg_t *msg_);
int process_routing_id_msg (msg_t *msg_); int process_routing_id_msg (msg_t *msg_);
......
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