0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 07:31:03 +08:00

Problem: inconsistent handling of ZAP replies

Solution: unification, pulled up common behaviour to zap_client_t/zap_client_common_handshake_t
This commit is contained in:
sigiesec 2017-08-16 17:12:49 +02:00
parent 8c58ef7f5c
commit 8dce0396fb
7 changed files with 39 additions and 43 deletions

View File

@ -492,15 +492,9 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
if (rc != 0)
return -1;
rc = receive_and_process_zap_reply ();
if (rc == 0)
handle_zap_status_code ();
else
if (errno == EAGAIN)
state = waiting_for_zap_reply;
else
if (rc == -1)
return -1;
}
else
} else
state = sending_ready;
return parse_metadata (initiate_plaintext + crypto_box_ZEROBYTES + 128,
@ -573,12 +567,4 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
crypto_box_PUBLICKEYBYTES);
}
int zmq::curve_server_t::receive_and_process_zap_reply ()
{
int rc = zap_client_t::receive_and_process_zap_reply ();
if (rc == -1 && errno == EPROTO)
current_error_detail = zap;
return rc;
}
#endif

View File

@ -104,7 +104,6 @@ namespace zmq
int produce_error (msg_t *msg_) const;
int send_zap_request (const uint8_t *key);
int receive_and_process_zap_reply ();
};
}

View File

@ -130,7 +130,7 @@ int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_)
return -1;
rc = receive_and_process_zap_reply ();
if (rc != 0) {
if (errno != EAGAIN)
if (rc == -1)
return -1;
expecting_zap_reply = true;
}
@ -192,7 +192,7 @@ int zmq::gssapi_server_t::zap_msg_available ()
const int rc = receive_and_process_zap_reply ();
if (rc == 0)
state = send_ready;
return rc;
return rc == -1 ? -1 : 0;
}
zmq::mechanism_t::status_t zmq::gssapi_server_t::status () const

View File

@ -79,7 +79,7 @@ int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_)
return -1;
zap_request_sent = true;
rc = receive_and_process_zap_reply ();
if (rc != 0)
if (rc == -1 || rc == 1)
return -1;
zap_reply_received = true;
}
@ -170,7 +170,7 @@ int zmq::null_mechanism_t::zap_msg_available ()
const int rc = receive_and_process_zap_reply ();
if (rc == 0)
zap_reply_received = true;
return rc;
return rc == -1 ? -1 : 0;
}
zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const

View File

@ -172,16 +172,7 @@ int zmq::plain_server_t::process_hello (msg_t *msg_)
rc = send_zap_request (username, password);
if (rc != 0)
return -1;
rc = receive_and_process_zap_reply ();
if (rc == 0)
handle_zap_status_code ();
else
if (errno == EAGAIN)
state = waiting_for_zap_reply;
else
return -1;
return 0;
return receive_and_process_zap_reply () == -1 ? -1 : 0;
}
int zmq::plain_server_t::produce_welcome (msg_t *msg_) const

View File

@ -159,8 +159,12 @@ int zap_client_t::receive_and_process_zap_reply ()
for (int i = 0; i < 7; i++) {
rc = session->read_zap_msg (&msg[i]);
if (rc == -1)
if (rc == -1) {
if (errno == EAGAIN) {
return 1;
}
return close_and_return (msg, -1);
}
if ((msg[i].flags () & msg_t::more) == (i < 6 ? 0 : msg_t::more)) {
// CURVE I : ZAP handler sent incomplete reply message
errno = EPROTO;
@ -209,8 +213,9 @@ int zap_client_t::receive_and_process_zap_reply ()
rc = parse_metadata (static_cast<const unsigned char *> (msg[6].data ()),
msg[6].size (), true);
if (rc != 0)
if (rc != 0) {
return close_and_return (msg, -1);
}
// Close all reply frames
for (int i = 0; i < 7; i++) {
@ -218,6 +223,8 @@ int zap_client_t::receive_and_process_zap_reply ()
errno_assert (rc2 == 0);
}
handle_zap_status_code ();
return 0;
}
@ -253,12 +260,7 @@ int zap_client_common_handshake_t::zap_msg_available ()
errno = EFSM;
return -1;
}
const int rc = receive_and_process_zap_reply ();
if (rc == 0)
handle_zap_status_code ();
else if (errno == EPROTO)
current_error_detail = mechanism_t::zap;
return rc;
return receive_and_process_zap_reply () == -1 ? -1 : 0;
}
void zap_client_common_handshake_t::handle_zap_status_code ()
@ -295,4 +297,21 @@ mechanism_t::error_detail_t zap_client_common_handshake_t::error_detail () const
return current_error_detail;
}
int zap_client_common_handshake_t::receive_and_process_zap_reply ()
{
int rc = zap_client_t::receive_and_process_zap_reply ();
switch (rc) {
case -1:
if (errno == EPROTO)
current_error_detail = mechanism_t::zap;
break;
case 1:
// TODO shouldn't the state already be this?
state = waiting_for_zap_reply;
break;
case 0:
break;
}
return rc;
}
}

View File

@ -54,8 +54,8 @@ class zap_client_t : public virtual mechanism_t
size_t *credentials_sizes,
size_t credentials_count);
int receive_and_process_zap_reply ();
virtual int receive_and_process_zap_reply ();
virtual void handle_zap_status_code () {}
protected:
session_base_t *const session;
@ -85,12 +85,13 @@ class zap_client_common_handshake_t : public zap_client_t
const options_t &options_,
state_t zap_reply_ok_state);
// methods from mechanism_t
// methods from mechanism_t
status_t status () const;
int zap_msg_available ();
error_detail_t error_detail () const;
// own methods
// zap_client_t methods
int receive_and_process_zap_reply ();
void handle_zap_status_code ();
// Current FSM state