diff --git a/include/gz/transport/Discovery.hh b/include/gz/transport/Discovery.hh index 161ebc4e..c22e0bbc 100644 --- a/include/gz/transport/Discovery.hh +++ b/include/gz/transport/Discovery.hh @@ -740,6 +740,26 @@ namespace gz } } + /// \brief Register a new relay address. + /// \param[in] _ip New IP address. + public: void AddRelayAddress(const std::string &_ip) + { + // Sanity check: Make sure that this IP address is not already saved. + for (auto const &addr : this->relayAddrs) + { + if (addr.sin_addr.s_addr == inet_addr(_ip.c_str())) + return; + } + + sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(_ip.c_str()); + addr.sin_port = htons(static_cast(this->port)); + + this->relayAddrs.push_back(addr); + } + /// \brief Broadcast periodic heartbeats. private: void UpdateHeartbeat() { @@ -1420,26 +1440,6 @@ namespace gz return true; } - /// \brief Register a new relay address. - /// \param[in] _ip New IP address. - private: void AddRelayAddress(const std::string &_ip) - { - // Sanity check: Make sure that this IP address is not already saved. - for (auto const &addr : this->relayAddrs) - { - if (addr.sin_addr.s_addr == inet_addr(_ip.c_str())) - return; - } - - sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(_ip.c_str()); - addr.sin_port = htons(static_cast(this->port)); - - this->relayAddrs.push_back(addr); - } - /// \brief Default activity interval value (ms.). /// \sa ActivityInterval. /// \sa SetActivityInterval. diff --git a/include/gz/transport/NodeOptions.hh b/include/gz/transport/NodeOptions.hh index 409b62e9..6754e0e0 100644 --- a/include/gz/transport/NodeOptions.hh +++ b/include/gz/transport/NodeOptions.hh @@ -20,6 +20,7 @@ #include #include +#include #include "gz/transport/config.hh" #include "gz/transport/Export.hh" @@ -126,6 +127,19 @@ namespace gz public: bool TopicRemap(const std::string &_fromTopic, std::string &_toTopic) const; + /// \brief Add relay IPs. This node will send UDP unicast traffic to these + /// addresses to connect networks when UDP multicast traffic is not + /// forwarded. + /// It's also possible to use the environment variable GZ_RELAY to add + /// relays. + /// \param[in] _relayIPs IPv4 addresses of unicast relays to add. + /// \return True if the relay list is valid or false otherwise. + public: bool SetRelays(const std::vector& _relayIPs); + + /// \brief Gets the list of relay addresses specified in this NodeOptions. + /// \return The list of relay addresses. + public: const std::vector& Relays() const; + #ifdef _WIN32 // Disable warning C4251 which is triggered by // std::unique_ptr diff --git a/src/Node.cc b/src/Node.cc index 4dbc2959..184ae336 100644 --- a/src/Node.cc +++ b/src/Node.cc @@ -512,6 +512,12 @@ Node::Node(const NodeOptions &_options) // Save the options. this->dataPtr->options = _options; + + // Add relays from the node options. + for(const auto& addr : _options.Relays()) { + this->dataPtr->shared->dataPtr->msgDiscovery->AddRelayAddress(addr); + this->dataPtr->shared->dataPtr->srvDiscovery->AddRelayAddress(addr); + } } ////////////////////////////////////////////////// diff --git a/src/NodeOptions.cc b/src/NodeOptions.cc index 86a9437c..e932f683 100644 --- a/src/NodeOptions.cc +++ b/src/NodeOptions.cc @@ -17,6 +17,7 @@ #include #include +#include #include "gz/transport/Helpers.hh" #include "gz/transport/NodeOptions.hh" @@ -136,3 +137,15 @@ bool NodeOptions::TopicRemap(const std::string &_fromTopic, return topicIt != this->dataPtr->topicsRemap.end(); } + +////////////////////////////////////////////////// +bool NodeOptions::SetRelays(const std::vector& _relayIPs) { + this->dataPtr->relayIPs = _relayIPs; + return true; +} + +////////////////////////////////////////////////// +const std::vector& NodeOptions::Relays() const { + return this->dataPtr->relayIPs; +} + diff --git a/src/NodeOptionsPrivate.hh b/src/NodeOptionsPrivate.hh index 684557bf..52ca79c7 100644 --- a/src/NodeOptionsPrivate.hh +++ b/src/NodeOptionsPrivate.hh @@ -20,6 +20,7 @@ #include #include +#include #include "gz/transport/config.hh" #include "gz/transport/NetUtils.hh" @@ -50,6 +51,9 @@ namespace gz /// \brief Table of remappings. The key is the original topic name and /// its value is the new topic name to be used instead. public: std::map topicsRemap; + + /// \brief List of unicast relay IPs. + public: std::vector relayIPs; }; } } diff --git a/src/NodeOptions_TEST.cc b/src/NodeOptions_TEST.cc index 96a870e6..c6b8b8d8 100644 --- a/src/NodeOptions_TEST.cc +++ b/src/NodeOptions_TEST.cc @@ -74,4 +74,11 @@ TEST(NodeOptionsTest, accessors) EXPECT_EQ(opts.Partition(), defaultPartition); EXPECT_TRUE(opts.SetPartition(aPartition)); EXPECT_EQ(opts.Partition(), aPartition); + + // Relay + std::string aRelay = "123.45.67.89"; + EXPECT_TRUE(opts.SetRelays({aRelay})); + auto relays = opts.Relays(); + ASSERT_EQ(relays.size(), 1); + EXPECT_EQ(relays[0], aRelay); }