From 34d5028ea8a3b1b7b66babed4d66be2028fd695d Mon Sep 17 00:00:00 2001 From: somdoron Date: Fri, 29 Apr 2016 10:48:46 +0300 Subject: [PATCH] allow specify binding address on radio with udp --- src/socket_base.cpp | 2 +- src/udp_address.cpp | 21 ++++++++++++++++----- src/udp_address.hpp | 2 +- tests/test_udp.cpp | 4 ++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/socket_base.cpp b/src/socket_base.cpp index 75b3758d..eef77193 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -882,7 +882,7 @@ int zmq::socket_base_t::connect (const char *addr_) if (protocol == "udp") { paddr->resolved.udp_addr = new (std::nothrow) udp_address_t (); alloc_assert (paddr->resolved.udp_addr); - rc = paddr->resolved.udp_addr->resolve (address.c_str()); + rc = paddr->resolved.udp_addr->resolve (address.c_str(), options.type == ZMQ_DISH); if (rc != 0) { LIBZMQ_DELETE(paddr); EXIT_MUTEX (); diff --git a/src/udp_address.cpp b/src/udp_address.cpp index 68b05030..494c6752 100644 --- a/src/udp_address.cpp +++ b/src/udp_address.cpp @@ -58,7 +58,7 @@ zmq::udp_address_t::~udp_address_t () { } -int zmq::udp_address_t::resolve (const char *name_) +int zmq::udp_address_t::resolve (const char *name_, bool receiver_) { // Find the ':' at end that separates address from the port number. const char *delimiter = strrchr (name_, ':'); @@ -80,7 +80,12 @@ int zmq::udp_address_t::resolve (const char *name_) dest_address.sin_family = AF_INET; dest_address.sin_port = htons (port); - dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ()); + + // Only when the udp is receiver we allow * as the address + if (addr_str == "*" && receiver_) + dest_address.sin_addr.s_addr = htons (INADDR_ANY); + else + dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ()); if (dest_address.sin_addr.s_addr == INADDR_NONE) { errno = EINVAL; @@ -104,9 +109,15 @@ int zmq::udp_address_t::resolve (const char *name_) return -1; } - bind_address.sin_family = AF_INET; - bind_address.sin_port = htons (port); - bind_address.sin_addr.s_addr = htons (INADDR_ANY); + // If a receiver and not a multicast, the dest address + // is actually the bind address + if (receiver_ && !is_mutlicast) + bind_address = dest_address; + else { + bind_address.sin_family = AF_INET; + bind_address.sin_port = htons (port); + bind_address.sin_addr.s_addr = htons (INADDR_ANY); + } address = name_; diff --git a/src/udp_address.hpp b/src/udp_address.hpp index 27359264..7dcc7065 100644 --- a/src/udp_address.hpp +++ b/src/udp_address.hpp @@ -48,7 +48,7 @@ namespace zmq udp_address_t (); virtual ~udp_address_t (); - int resolve (const char *name_); + int resolve (const char *name_, bool receiver_); // The opposite to resolve() virtual int to_string (std::string &addr_); diff --git a/tests/test_udp.cpp b/tests/test_udp.cpp index b5b1981f..1394e091 100644 --- a/tests/test_udp.cpp +++ b/tests/test_udp.cpp @@ -95,10 +95,10 @@ int main (void) void *radio = zmq_socket (ctx, ZMQ_RADIO); void *dish = zmq_socket (ctx, ZMQ_DISH); - int rc = zmq_connect (radio, "udp://127.0.0.1:5556"); + int rc = zmq_bind (dish, "udp://*:5556"); assert (rc == 0); - rc = zmq_bind (dish, "udp://127.0.0.1:5556"); + rc = zmq_connect (radio, "udp://127.0.0.1:5556"); assert (rc == 0); msleep (SETTLE_TIME);