From 09c487f327083d8b02c426df582f31fce4540c26 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Sat, 29 Feb 2020 16:03:25 -0400 Subject: [PATCH] Add ability to use random routing ids for outgoing --- lokimq/lokimq.cpp | 9 ++++++++- lokimq/lokimq.h | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lokimq/lokimq.cpp b/lokimq/lokimq.cpp index 248759e..4cc9b9e 100644 --- a/lokimq/lokimq.cpp +++ b/lokimq/lokimq.cpp @@ -561,7 +561,14 @@ void LokiMQ::setup_outgoing_socket(zmq::socket_t& socket, string_view remote_pub } socket.setsockopt(ZMQ_HANDSHAKE_IVL, (int) HANDSHAKE_TIME.count()); socket.setsockopt(ZMQ_MAXMSGSIZE, MAX_MSG_SIZE); - socket.setsockopt(ZMQ_ROUTING_ID, pubkey.data(), pubkey.size()); + if (PUBKEY_BASED_ROUTING_ID) { + std::string routing_id; + routing_id.reserve(33); + routing_id += 'L'; // Prefix because routing id's starting with \0 are reserved by zmq (and our pubkey might start with \0) + routing_id.append(pubkey.begin(), pubkey.end()); + socket.setsockopt(ZMQ_ROUTING_ID, routing_id.data(), routing_id.size()); + } + // else let ZMQ pick a random one } std::pair diff --git a/lokimq/lokimq.h b/lokimq/lokimq.h index 7a4e29f..e0d5206 100644 --- a/lokimq/lokimq.h +++ b/lokimq/lokimq.h @@ -320,6 +320,16 @@ public: * closing the connection. Setting this only affects new outgoing connections. */ std::chrono::milliseconds HANDSHAKE_TIME = 10s; + /** Whether to use a zmq routing ID based on the pubkey for new outgoing connections. This is + * normally desirable as it allows the listener to recognize that the incoming connection is a + * reconnection from the same remote and handover routing to the new socket while closing off + * the (likely dead) old socket. This, however, prevents a single LokiMQ instance from + * establishing multiple connections to the same listening LokiMQ, which is sometimes useful + * (for example when testing), and so this option can be overridden to `false` to use completely + * random zmq routing ids on outgoing connections (which will thus allow multiple connections). + */ + bool PUBKEY_BASED_ROUTING_ID = true; + /** Maximum incoming message size; if a remote tries sending a message larger than this they get * disconnected. -1 means no limit. */ int64_t MAX_MSG_SIZE = 1 * 1024 * 1024;