From 32ded2b457b2102dba4c15e00363f031d212b1c4 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Wed, 16 Mar 2011 13:26:23 +0100 Subject: Duplicate identities now checked with zmq_connect Signed-off-by: Martin Sustrik --- src/connect_session.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'src/connect_session.cpp') diff --git a/src/connect_session.cpp b/src/connect_session.cpp index 0df9854..c0951cb 100644 --- a/src/connect_session.cpp +++ b/src/connect_session.cpp @@ -28,12 +28,15 @@ zmq::connect_session_t::connect_session_t (class io_thread_t *io_thread_, const char *protocol_, const char *address_) : session_t (io_thread_, socket_, options_), protocol (protocol_), - address (address_) + address (address_), + connected (false) { } zmq::connect_session_t::~connect_session_t () { + if (connected && !peer_identity.empty ()) + unregister_session (peer_identity); } void zmq::connect_session_t::process_plug () @@ -107,8 +110,46 @@ void zmq::connect_session_t::start_connecting (bool wait_) zmq_assert (false); } -void zmq::connect_session_t::attached (const blob_t &peer_identity_) +bool zmq::connect_session_t::attached (const blob_t &peer_identity_) { + // If there was no previous connection... + if (!connected) { + + // Peer has transient identity. + if (peer_identity_.empty () || peer_identity_ [0] == 0) { + connected = true; + return true; + } + + // Peer has strong identity. Let's register it and check whether noone + // else is using the same identity. + if (!register_session (peer_identity_, this)) { + log ("DPID: duplicate peer identity - disconnecting peer"); + return false; + } + connected = true; + peer_identity = peer_identity_; + return true; + } + + // New engine from listener can conflict with existing engine. + // Alternatively, new engine created by reconnection process can + // conflict with engine supplied by listener in the meantime. + if (has_engine ()) { + log ("DPID: duplicate peer identity - disconnecting peer"); + return false; + } + + // If there have been a connection before, we have to check whether + // peer's identity haven't changed in the meantime. + if ((peer_identity_.empty () || peer_identity_ [0] == 0) && + peer_identity.empty ()) + return true; + if (peer_identity != peer_identity_) { + log ("CHID: peer have changed identity - disconnecting peer"); + return false; + } + return true; } void zmq::connect_session_t::detached () -- cgit v1.2.3