From 5229eeef5b92616c34e7f9b469a014f61d9070ad Mon Sep 17 00:00:00 2001 From: Thomas Rodgers Date: Sat, 14 Feb 2015 10:44:52 -0600 Subject: [PATCH] resolve #1347 Backport zmq_msg_gets "Peer-Address" --- .gitignore | 1 + doc/zmq_msg_gets.txt | 16 +++++++++++++++- src/stream_engine.cpp | 17 +++++++---------- tests/test_metadata.cpp | 1 + 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 2248fb57..100863bd 100644 --- a/.gitignore +++ b/.gitignore @@ -133,6 +133,7 @@ zeromq-*.tar.gz zeromq-*.zip core build +test-suite.log curve_keygen inproc_lat inproc_thr diff --git a/doc/zmq_msg_gets.txt b/doc/zmq_msg_gets.txt index f69c386b..3fc75d4a 100644 --- a/doc/zmq_msg_gets.txt +++ b/doc/zmq_msg_gets.txt @@ -19,8 +19,22 @@ property specified by the 'property' argument for the message pointed to by the 'message' argument. Both the 'property' argument and the 'value' shall be NULL-terminated UTF8-encoded strings. -The following properties can be retrieved with the _zmq_msg_gets()_ function: +Metadata is defined on a per-connection basis during the ZeroMQ connection +handshake as specified in . +The following ZMTP properties can be retrieved with the _zmq_msg_gets()_ +function: + + Socket-Type + Identity + Resource + +Additionally, when available for the underlying transport, the *Peer-Address* +property will return the IP address of the remote endpoint as returned by +getnameinfo(2). + +Other properties may be defined based on the underlying security mechanism, +see ZAP authenticated connection sample below. RETURN VALUE ------------ diff --git a/src/stream_engine.cpp b/src/stream_engine.cpp index 21763d2c..42c813b2 100644 --- a/src/stream_engine.cpp +++ b/src/stream_engine.cpp @@ -794,21 +794,18 @@ 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 (); - while (it != zap_properties.end ()) { - properties.insert (properties_t::value_type (it->first, it->second)); - ++it; - } + properties.insert(zap_properties.begin (), zap_properties.end ()); // Add ZMTP properties. const properties_t& zmtp_properties = mechanism->get_zmtp_properties (); - it = zmtp_properties.begin (); - while (it != zmtp_properties.end ()) { - properties.insert (properties_t::value_type (it->first, it->second)); - ++it; - } + properties.insert(zmtp_properties.begin (), zmtp_properties.end ()); zmq_assert (metadata == NULL); if (!properties.empty ()) diff --git a/tests/test_metadata.cpp b/tests/test_metadata.cpp index 36a22b76..28688b10 100644 --- a/tests/test_metadata.cpp +++ b/tests/test_metadata.cpp @@ -100,6 +100,7 @@ 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);