Commit d65231be authored by Martin Hurton's avatar Martin Hurton

Update CURVE mechanism to the latest ZAP revision

parent 7541debe
...@@ -33,9 +33,11 @@ ...@@ -33,9 +33,11 @@
#include "wire.hpp" #include "wire.hpp"
zmq::curve_server_t::curve_server_t (session_base_t *session_, zmq::curve_server_t::curve_server_t (session_base_t *session_,
const std::string &peer_address_,
const options_t &options_) : const options_t &options_) :
mechanism_t (options_), mechanism_t (options_),
session (session_), session (session_),
peer_address (peer_address_),
state (expect_hello), state (expect_hello),
expecting_zap_reply (false), expecting_zap_reply (false),
cn_nonce (1) cn_nonce (1)
...@@ -512,7 +514,7 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key) ...@@ -512,7 +514,7 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key)
rc = session->write_zap_msg (&msg); rc = session->write_zap_msg (&msg);
errno_assert (rc == 0); errno_assert (rc == 0);
// Sequence frame // Request ID frame
rc = msg.init_size (1); rc = msg.init_size (1);
errno_assert (rc == 0); errno_assert (rc == 0);
memcpy (msg.data (), "1", 1); memcpy (msg.data (), "1", 1);
...@@ -527,6 +529,14 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key) ...@@ -527,6 +529,14 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key)
rc = session->write_zap_msg (&msg); rc = session->write_zap_msg (&msg);
errno_assert (rc == 0); errno_assert (rc == 0);
// Address frame
rc = msg.init_size (peer_address.length ());
errno_assert (rc == 0);
memcpy (msg.data (), peer_address.c_str (), peer_address.length ());
msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg);
errno_assert (rc == 0);
// Mechanism frame // Mechanism frame
rc = msg.init_size (5); rc = msg.init_size (5);
errno_assert (rc == 0); errno_assert (rc == 0);
...@@ -546,18 +556,19 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key) ...@@ -546,18 +556,19 @@ void zmq::curve_server_t::send_zap_request (const uint8_t *key)
int zmq::curve_server_t::receive_and_process_zap_reply () int zmq::curve_server_t::receive_and_process_zap_reply ()
{ {
int rc = 0; int rc = 0;
msg_t msg [6]; msg_t msg [7]; // ZAP reply consists of 7 frames
for (int i = 0; i < 6; i++) { // Initialize all reply frames
for (int i = 0; i < 7; i++) {
rc = msg [i].init (); rc = msg [i].init ();
errno_assert (rc == 0); errno_assert (rc == 0);
} }
for (int i = 0; i < 6; i++) { for (int i = 0; i < 7; i++) {
rc = session->read_zap_msg (&msg [i]); rc = session->read_zap_msg (&msg [i]);
if (rc == -1) if (rc == -1)
break; break;
if ((msg [i].flags () & msg_t::more) == (i < 5? 0: msg_t::more)) { if ((msg [i].flags () & msg_t::more) == (i < 6? 0: msg_t::more)) {
errno = EPROTO; errno = EPROTO;
rc = -1; rc = -1;
break; break;
...@@ -579,7 +590,7 @@ int zmq::curve_server_t::receive_and_process_zap_reply () ...@@ -579,7 +590,7 @@ int zmq::curve_server_t::receive_and_process_zap_reply ()
goto error; goto error;
} }
// Sequence number frame // Request id frame
if (msg [2].size () != 1 || memcmp (msg [2].data (), "1", 1)) { if (msg [2].size () != 1 || memcmp (msg [2].data (), "1", 1)) {
errno = EPROTO; errno = EPROTO;
goto error; goto error;
...@@ -591,8 +602,12 @@ int zmq::curve_server_t::receive_and_process_zap_reply () ...@@ -591,8 +602,12 @@ int zmq::curve_server_t::receive_and_process_zap_reply ()
goto error; goto error;
} }
// Process metadata frame
rc = parse_metadata (static_cast <const unsigned char*> (msg [6].data ()),
msg [6].size ());
error: error:
for (int i = 0; i < 6; i++) { for (int i = 0; i < 7; i++) {
const int rc2 = msg [i].close (); const int rc2 = msg [i].close ();
errno_assert (rc2 == 0); errno_assert (rc2 == 0);
} }
......
...@@ -50,6 +50,7 @@ namespace zmq ...@@ -50,6 +50,7 @@ namespace zmq
public: public:
curve_server_t (session_base_t *session_, curve_server_t (session_base_t *session_,
const std::string &peer_address_,
const options_t &options_); const options_t &options_);
virtual ~curve_server_t (); virtual ~curve_server_t ();
...@@ -74,6 +75,8 @@ namespace zmq ...@@ -74,6 +75,8 @@ namespace zmq
session_base_t * const session; session_base_t * const session;
const std::string peer_address;
// Current FSM state // Current FSM state
state_t state; state_t state;
......
...@@ -84,6 +84,9 @@ zmq::stream_engine_t::stream_engine_t (fd_t fd_, const options_t &options_, ...@@ -84,6 +84,9 @@ zmq::stream_engine_t::stream_engine_t (fd_t fd_, const options_t &options_,
// Put the socket into non-blocking mode. // Put the socket into non-blocking mode.
unblock_socket (s); unblock_socket (s);
if (!get_peer_ip_address (s, peer_address))
peer_address = "";
#ifdef SO_NOSIGPIPE #ifdef SO_NOSIGPIPE
// Make sure that SIGPIPE signal is not generated when writing to a // Make sure that SIGPIPE signal is not generated when writing to a
// connection that was already closed by the peer. // connection that was already closed by the peer.
...@@ -534,7 +537,8 @@ bool zmq::stream_engine_t::handshake () ...@@ -534,7 +537,8 @@ bool zmq::stream_engine_t::handshake ()
else else
if (memcmp (greeting_recv + 12, "CURVE\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) { if (memcmp (greeting_recv + 12, "CURVE\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) == 0) {
if (options.as_server) if (options.as_server)
mechanism = new (std::nothrow) curve_server_t (session, options); mechanism = new (std::nothrow)
curve_server_t (session, peer_address, options);
else else
mechanism = new (std::nothrow) curve_client_t (options); mechanism = new (std::nothrow) curve_client_t (options);
alloc_assert (mechanism); alloc_assert (mechanism);
......
...@@ -187,6 +187,8 @@ namespace zmq ...@@ -187,6 +187,8 @@ namespace zmq
// Socket // Socket
zmq::socket_base_t *socket; zmq::socket_base_t *socket;
std::string peer_address;
stream_engine_t (const stream_engine_t&); stream_engine_t (const stream_engine_t&);
const stream_engine_t &operator = (const stream_engine_t&); const stream_engine_t &operator = (const stream_engine_t&);
}; };
......
...@@ -29,6 +29,7 @@ zap_handler (void *zap) ...@@ -29,6 +29,7 @@ zap_handler (void *zap)
char *version = s_recv (zap); char *version = s_recv (zap);
char *sequence = s_recv (zap); char *sequence = s_recv (zap);
char *domain = s_recv (zap); char *domain = s_recv (zap);
char *address = s_recv (zap);
char *mechanism = s_recv (zap); char *mechanism = s_recv (zap);
char *client_key = s_recv (zap); char *client_key = s_recv (zap);
...@@ -39,11 +40,13 @@ zap_handler (void *zap) ...@@ -39,11 +40,13 @@ zap_handler (void *zap)
s_sendmore (zap, sequence); s_sendmore (zap, sequence);
s_sendmore (zap, "200"); s_sendmore (zap, "200");
s_sendmore (zap, "OK"); s_sendmore (zap, "OK");
s_send (zap, "anonymous"); s_sendmore (zap, "anonymous");
s_send (zap, "");
free (version); free (version);
free (sequence); free (sequence);
free (domain); free (domain);
free (address);
free (mechanism); free (mechanism);
free (client_key); free (client_key);
......
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