From 7d224d7871922af060785fa2801b90a496017ede Mon Sep 17 00:00:00 2001 From: Thomas Rodgers Date: Fri, 16 Jan 2015 14:52:16 -0600 Subject: [PATCH] Add 'Peer-Address' property to connection metadata Allows non-C/C++ based clients easy access to the peer's IP address via zmq_msg_gets(&msg, "Peer-Address") instead of zmq_msg_get(&msg, ZMQ_SRCFD) followed by calls to getpeername and getnameinfo --- .gitignore | 1 + doc/zmq_msg_gets.txt | 1 + src/stream_engine.cpp | 7 ++++++- tests/test_metadata.cpp | 4 +++- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a815b4b2..cc89fbfc 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,4 @@ zeromq-*.tar.gz zeromq-*.zip core build +test-suite.log diff --git a/doc/zmq_msg_gets.txt b/doc/zmq_msg_gets.txt index d2583246..b909092b 100644 --- a/doc/zmq_msg_gets.txt +++ b/doc/zmq_msg_gets.txt @@ -28,6 +28,7 @@ function: Socket-Type Identity Resource + Peer-Address Other properties may be defined based on the underlying security, see ZAP auththenticated connection sample below. diff --git a/src/stream_engine.cpp b/src/stream_engine.cpp index d07b8e90..5734fdd9 100644 --- a/src/stream_engine.cpp +++ b/src/stream_engine.cpp @@ -671,7 +671,7 @@ bool zmq::stream_engine_t::handshake () options.mechanism == ZMQ_GSSAPI? "GSSAPI": "OTHER", mechanism); - + error (protocol_error); return false; } @@ -806,6 +806,11 @@ void zmq::stream_engine_t::mechanism_ready () properties_t properties; properties_t::const_iterator it; + // If we have a peer_address, add it to metadata + if (!peer_address.empty()) { + properties.insert(std::make_pair("Peer-Address", peer_address)); + } + // Add ZAP properties. const properties_t& zap_properties = mechanism->get_zap_properties (); it = zap_properties.begin (); diff --git a/tests/test_metadata.cpp b/tests/test_metadata.cpp index 36a22b76..09f9dbfc 100644 --- a/tests/test_metadata.cpp +++ b/tests/test_metadata.cpp @@ -41,7 +41,7 @@ zap_handler (void *handler) assert (streq (version, "1.0")); assert (streq (mechanism, "NULL")); - + s_sendmore (handler, version); s_sendmore (handler, sequence); if (streq (domain, "DOMAIN")) { @@ -100,6 +100,8 @@ int main (void) assert (streq (zmq_msg_gets (&msg, "Hello"), "World")); assert (streq (zmq_msg_gets (&msg, "Socket-Type"), "DEALER")); assert (streq (zmq_msg_gets (&msg, "User-Id"), "anonymous")); + assert (streq (zmq_msg_gets (&msg, "Peer-Address"), "127.0.0.1")); + assert (zmq_msg_gets (&msg, "No Such") == NULL); assert (zmq_errno () == EINVAL); zmq_msg_close (&msg);