Skip to content

Commit

Permalink
TCP support for Discovery server CLI and env var (#4097) (#4133)
Browse files Browse the repository at this point in the history
* TCP support for Discovery server CLI and env var (#4097)

* Refs #20021: TCP support for CLI Discovery server

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Add TCP to Discovery Server Example

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: CLI: Add argument for TCP port and example

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Add TCP support to environment variable

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Use autofill feature & check existing transports

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Update BlackboxTestsDiscovery for TCPv4/6 dns support

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Apply Revision's Suggestions

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Add test for TCP single IP address & Fix DNS test

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Add default TCP interface to CLI

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Uncrustify

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Update tests with new locator syntax & fix CLI

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Update Readme

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs #20021: Fix warnings

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

* Refs 20021: Update versions.md

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

---------

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>
(cherry picked from commit 2653efb)

# Conflicts:
#	versions.md

* Fix Conflicts

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>

---------

Signed-off-by: cferreiragonz <carlosferreira@eprosima.com>
Co-authored-by: Carlos Ferreira González <carlosferreira@eprosima.com>
  • Loading branch information
mergify[bot] and cferreiragonz authored Dec 18, 2023
1 parent 9eccb56 commit afe455f
Show file tree
Hide file tree
Showing 15 changed files with 750 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ bool HelloWorldPublisher::init(
break;
}

case TransportKind::TCPv4:
{
auto descriptor_tmp = std::make_shared<eprosima::fastdds::rtps::TCPv4TransportDescriptor>();
// descriptor_tmp->interfaceWhiteList.push_back(ip_server_address);
// One listening port must be added either in the pub or the sub
descriptor_tmp->add_listener_port(0);
descriptor = descriptor_tmp;

server_locator.kind = LOCATOR_KIND_TCPv4;
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(server_locator, server_port);
eprosima::fastrtps::rtps::IPLocator::setIPv4(server_locator, ip_server_address);
break;
}

case TransportKind::TCPv6:
{
auto descriptor_tmp = std::make_shared<eprosima::fastdds::rtps::TCPv6TransportDescriptor>();
// descriptor_tmp->interfaceWhiteList.push_back(ip_server_address);
// One listening port must be added either in the pub or the sub
descriptor_tmp->add_listener_port(0);
descriptor = descriptor_tmp;

server_locator.kind = LOCATOR_KIND_TCPv6;
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(server_locator, server_port);
eprosima::fastrtps::rtps::IPLocator::setIPv6(server_locator, ip_server_address);
break;
}

default:
break;
}
Expand Down
34 changes: 34 additions & 0 deletions examples/cpp/dds/DiscoveryServerExample/DiscoveryServerServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/UDPv6TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv6TransportDescriptor.h>
#include <fastrtps/attributes/ParticipantAttributes.h>

#include "DiscoveryServerServer.h"
Expand Down Expand Up @@ -136,6 +138,38 @@ bool DiscoveryServer::init(
break;
}

case TransportKind::TCPv4:
{
auto descriptor_tmp = std::make_shared<eprosima::fastdds::rtps::TCPv4TransportDescriptor>();
// descriptor_tmp->interfaceWhiteList.push_back(ip_listening_address);
descriptor_tmp->add_listener_port(server_port);
descriptor = descriptor_tmp;

listening_locator.kind = LOCATOR_KIND_TCPv4;
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(listening_locator, server_port);
eprosima::fastrtps::rtps::IPLocator::setIPv4(listening_locator, ip_listening_address);
connection_locator.kind = LOCATOR_KIND_TCPv4;
eprosima::fastrtps::rtps::IPLocator::setIPv4(connection_locator, ip_connection_address);
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(connection_locator, connection_server_port);
break;
}

case TransportKind::TCPv6:
{
auto descriptor_tmp = std::make_shared<eprosima::fastdds::rtps::TCPv6TransportDescriptor>();
// descriptor_tmp->interfaceWhiteList.push_back(ip_listening_address);
descriptor_tmp->add_listener_port(server_port);
descriptor = descriptor_tmp;

listening_locator.kind = LOCATOR_KIND_TCPv6;
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(listening_locator, server_port);
eprosima::fastrtps::rtps::IPLocator::setIPv6(listening_locator, ip_listening_address);
connection_locator.kind = LOCATOR_KIND_TCPv6;
eprosima::fastrtps::rtps::IPLocator::setIPv6(connection_locator, ip_connection_address);
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(connection_locator, connection_server_port);
break;
}

default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/UDPv6TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv6TransportDescriptor.h>
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/attributes/SubscriberAttributes.h>
#include <fastrtps/utils/IPLocator.h>
Expand Down Expand Up @@ -121,6 +123,34 @@ bool HelloWorldSubscriber::init(
break;
}

case TransportKind::TCPv4:
{
auto descriptor_tmp = std::make_shared<eprosima::fastdds::rtps::TCPv4TransportDescriptor>();
// descriptor_tmp->interfaceWhiteList.push_back(ip_server_address);
// One listening port must be added either in the pub or the sub
descriptor_tmp->add_listener_port(0);
descriptor = descriptor_tmp;

server_locator.kind = LOCATOR_KIND_TCPv4;
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(server_locator, server_port);
eprosima::fastrtps::rtps::IPLocator::setIPv4(server_locator, ip_server_address);
break;
}

case TransportKind::TCPv6:
{
auto descriptor_tmp = std::make_shared<eprosima::fastdds::rtps::TCPv6TransportDescriptor>();
// descriptor_tmp->interfaceWhiteList.push_back(ip_server_address);
// One listening port must be added either in the pub or the sub
descriptor_tmp->add_listener_port(0);
descriptor = descriptor_tmp;

server_locator.kind = LOCATOR_KIND_TCPv6;
eprosima::fastrtps::rtps::IPLocator::setLogicalPort(server_locator, server_port);
eprosima::fastrtps::rtps::IPLocator::setIPv4(server_locator, ip_server_address);
break;
}

default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,17 @@ int main(
{
transport = TransportKind::UDPv6;
}
else if (transport_str == "tcpv4")
{
transport = TransportKind::TCPv4;
}
else if (transport_str == "tcpv6")
{
transport = TransportKind::TCPv6;
}
else
{
print_warning("udpv4|udpv6", opt.name);
print_warning("udpv4|udpv6|tcpv4|tcpv6", opt.name);
}

break;
Expand Down
12 changes: 6 additions & 6 deletions examples/cpp/dds/DiscoveryServerExample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Publisher options:
Server address (Default address: 127.0.0.1).
-p <num> --connection-port=<num>
Server listening port (Default port: 16166).
--transport=<udpv4|udpv6>
Use Transport Protocol [udpv4|udpv6] (Default: udpv4).
--transport=<udpv4|udpv6|tcpv4|tcpv6>
Use Transport Protocol [udpv4|udpv6|tcpv4|tcpv6] (Default: udpv4).
-d <num> --connection-discovery-server-id <num>
Id of the Discovery Server to connect with. GUID will be
calculated from id (Default: 0).
Expand All @@ -49,8 +49,8 @@ Subscriber options:
Server address (Default address: 127.0.0.1).
-p <num> --connection-port=<num>
Server listening port (Default port: 16166).
--transport=<udpv4|udpv6>
Use Transport Protocol [udpv4|udpv6] (Default: udpv4).
--transport=<udpv4|udpv6|tcpv4|tcpv6>
Use Transport Protocol [udpv4|udpv6|tcpv4|tcpv6] (Default: udpv4).
-d <num> --connection-discovery-server-id <num>
Id of the Discovery Server to connect with. GUID will be
calculated from id (Default: 0).
Expand All @@ -63,8 +63,8 @@ DiscoveryServer options:
GUID will be calculated from id (Default: 0).
--listening-port=<num>
Server listening port (Default port: 16166).
--transport=<udpv4|udpv6>
Use Transport Protocol [udpv4|udpv6] (Default: udpv4).
--transport=<udpv4|udpv6|tcpv4|tcpv6>
Use Transport Protocol [udpv4|udpv6|tcpv4|tcpv6] (Default: udpv4).
-c <IPaddress> --connection-address=<IPaddress>
Server address (Default address: 127.0.0.1).
-p <num> --connection-port=<num>
Expand Down
10 changes: 6 additions & 4 deletions examples/cpp/dds/DiscoveryServerExample/arg_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ struct Arg : public option::Arg
if (
// transport == "shm" ||
transport == "udpv4" ||
transport == "udpv6"
transport == "udpv6" ||
transport == "tcpv4" ||
transport == "tcpv6"
)
{
return option::ARG_OK;
Expand Down Expand Up @@ -235,7 +237,7 @@ const option::Descriptor usage[] = {
"",
"transport",
Arg::Transport,
" \t--transport <trans> \tUse Transport Protocol [udpv4|udpv6] (UDPv4 by default)."
" \t--transport <trans> \tUse Transport Protocol [udpv4|udpv6|tcpv4|tcpv6] (UDPv4 by default)."
},
{
CONNECTION_DISCOVERY_SERVER_ID,
Expand Down Expand Up @@ -287,7 +289,7 @@ const option::Descriptor usage[] = {
"",
"transport",
Arg::Transport,
" \t--transport <trans> \tUse Transport Protocol [udpv4|udpv6] (UDPv4 by default)."
" \t--transport <trans> \tUse Transport Protocol [udpv4|udpv6|tcpv4|tcpv6] (UDPv4 by default)."
},
{
CONNECTION_DISCOVERY_SERVER_ID,
Expand Down Expand Up @@ -331,7 +333,7 @@ const option::Descriptor usage[] = {
"",
"transport",
Arg::Transport,
" \t--transport <trans> \tUse Transport Protocol [udpv4|udpv6] (UDPv4 by default)."
" \t--transport <trans> \tUse Transport Protocol [udpv4|udpv6|tcpv4|tcpv6] (UDPv4 by default)."
},
{
CONNECTION_PORT,
Expand Down
6 changes: 4 additions & 2 deletions examples/cpp/dds/DiscoveryServerExample/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ enum class TransportKind
{
UDPv4,
UDPv6,
TCPv4,
TCPv6,
SHM,
};

Expand Down Expand Up @@ -56,7 +58,7 @@ inline std::string get_ip_from_dns(
std::pair<std::set<std::string>, std::set<std::string>> dns_response =
eprosima::fastrtps::rtps::IPLocator::resolveNameDNS(domain_name);

if (kind == TransportKind::UDPv4)
if (kind == TransportKind::UDPv4 || kind == TransportKind::TCPv4)
{
if (dns_response.first.empty())
{
Expand All @@ -70,7 +72,7 @@ inline std::string get_ip_from_dns(
return solution;
}
}
else if (kind == TransportKind::UDPv6)
else if (kind == TransportKind::UDPv6 || kind == TransportKind::TCPv6)
{
if (dns_response.second.empty())
{
Expand Down
4 changes: 3 additions & 1 deletion include/fastdds/rtps/attributes/ServerAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@ std::basic_ostream<charT>& operator <<(
return output;
}

// port use if the ros environment variable doesn't specified one
// port used if the ros environment variable doesn't specify one
constexpr uint16_t DEFAULT_ROS2_SERVER_PORT = 11811;
// default server base guidPrefix
const char* const DEFAULT_ROS2_SERVER_GUIDPREFIX = "44.53.00.5f.45.50.52.4f.53.49.4d.41";
// port used by default for tcp transport
constexpr uint16_t DEFAULT_TCP_SERVER_PORT = 42100;

/* Environment variable to specify a semicolon-separated list of UDPv4 locators (ip:port) that define remote server
* locators.
Expand Down
10 changes: 9 additions & 1 deletion include/fastdds/rtps/common/Locator.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,15 @@ inline std::ostream& operator <<(
}

// Stream port
output << "]:" << loc.port;
if (loc.kind == LOCATOR_KIND_TCPv4 || loc.kind == LOCATOR_KIND_TCPv6)
{
output << "]:" << std::to_string(IPLocator::getPhysicalPort(loc)) << "-" << std::to_string(IPLocator::getLogicalPort(
loc));
}
else
{
output << "]:" << loc.port;
}

return output;
}
Expand Down
84 changes: 79 additions & 5 deletions src/cpp/rtps/RTPSDomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <rtps/transport/UDPv4Transport.h>
#include <rtps/transport/UDPv6Transport.h>
#include <rtps/transport/test_UDPv4Transport.h>
#include <rtps/transport/TCPv4Transport.h>
#include <rtps/transport/TCPv6Transport.h>

#include <fastrtps/utils/IPFinder.h>
#include <fastrtps/utils/IPLocator.h>
Expand Down Expand Up @@ -544,23 +546,95 @@ RTPSParticipant* RTPSDomainImpl::clientServerEnvironmentCreationOverride(
RemoteServerList_t& server_list = client_att.builtin.discovery_config.m_DiscoveryServers;
if (load_environment_server_info(server_list) && server_list.empty())
{
// it's not an error, the environment variable may not be set. Any issue with environment
// It's not an error, the environment variable may not be set. Any issue with environment
// variable syntax is EPROSIMA_LOG_ERROR already
return nullptr;
}

// check if some server requires the UDPv6 transport
// Check if some server requires the UDPv6, TCPv4 or TCPv6 transport
for (auto& server : server_list)
{
if (server.requires_transport<LOCATOR_KIND_UDPv6>())
{
// extend builtin transports with the UDPv6 transport
// Extend builtin transports with the UDPv6 transport
auto descriptor = std::make_shared<fastdds::rtps::UDPv6TransportDescriptor>();
descriptor->sendBufferSize = client_att.sendSocketBufferSize;
descriptor->receiveBufferSize = client_att.listenSocketBufferSize;
client_att.userTransports.push_back(std::move(descriptor));
break;
}
if (server.requires_transport<LOCATOR_KIND_TCPv4>())
{
// Check if a TCPv4 transport exists. Otherwise create it
fastdds::rtps::TCPTransportDescriptor* pT = nullptr;
std::shared_ptr<fastdds::rtps::TCPv4TransportDescriptor> p4;
bool no_tcpv4 = true;

for (auto sp : client_att.userTransports)
{
pT = dynamic_cast<fastdds::rtps::TCPTransportDescriptor*>(sp.get());

if (pT != nullptr)
{
if (!p4)
{
if ((p4 = std::dynamic_pointer_cast<fastdds::rtps::TCPv4TransportDescriptor>(sp)))
{
// TCPv4 transport already exists
no_tcpv4 = false;
break;
}
}
}
}
if (no_tcpv4)
{
// Extend builtin transports with the TCPv4 transport
auto descriptor = std::make_shared<fastdds::rtps::TCPv4TransportDescriptor>();
// Add automatic port
descriptor->add_listener_port(0);
descriptor->sendBufferSize = client_att.sendSocketBufferSize;
descriptor->receiveBufferSize = client_att.listenSocketBufferSize;
client_att.userTransports.push_back(std::move(descriptor));
}

}
if (server.requires_transport<LOCATOR_KIND_TCPv6>())
{
// Check if a TCPv6 transport exists. Otherwise create it
fastdds::rtps::TCPTransportDescriptor* pT = nullptr;
std::shared_ptr<fastdds::rtps::TCPv6TransportDescriptor> p6;
bool no_tcpv6 = true;

for (auto sp : client_att.userTransports)
{
pT = dynamic_cast<fastdds::rtps::TCPTransportDescriptor*>(sp.get());

if (pT != nullptr)
{
if (!p6)
{
// try to find a descriptor matching the listener port setup
if ((p6 = std::dynamic_pointer_cast<fastdds::rtps::TCPv6TransportDescriptor>(sp)))
{
// TCPv6 transport already exists
no_tcpv6 = false;
break;
}
}
}
}
if (no_tcpv6)
{
// Extend builtin transports with the TCPv6 transport
auto descriptor = std::make_shared<fastdds::rtps::TCPv6TransportDescriptor>();
// Add automatic port
descriptor->add_listener_port(0);
descriptor->sendBufferSize = client_att.sendSocketBufferSize;
descriptor->receiveBufferSize = client_att.listenSocketBufferSize;
client_att.userTransports.push_back(std::move(descriptor));
}
}
}

EPROSIMA_LOG_INFO(DOMAIN, "Detected auto client-server environment variable."
Expand All @@ -579,13 +653,13 @@ RTPSParticipant* RTPSDomainImpl::clientServerEnvironmentCreationOverride(
RTPSParticipant* part = createParticipant(domain_id, enabled, client_att, listen);
if (nullptr != part)
{
// client successfully created
// Client successfully created
EPROSIMA_LOG_INFO(DOMAIN, "Auto default server-client setup. Default client created.");
part->mp_impl->client_override(true);
return part;
}

// unable to create auto server-client default participants
// Unable to create auto server-client default participants
EPROSIMA_LOG_ERROR(DOMAIN, "Auto default server-client setup. Unable to create the client.");
return nullptr;
}
Expand Down
Loading

0 comments on commit afe455f

Please sign in to comment.