Skip to content

Commit 1199fbc

Browse files
committed
Refs #22872: Apply suggested changes
Signed-off-by: Carlosespicur <[email protected]>
1 parent 9aa018b commit 1199fbc

File tree

8 files changed

+122
-3
lines changed

8 files changed

+122
-3
lines changed

include/fastdds/dds/core/policy/QosPolicies.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,15 @@ class WireProtocolConfigQos : public QosPolicy
27342734
void easy_mode(
27352735
std::string ip)
27362736
{
2737+
// Check if the input is a valid IP
2738+
if (!rtps::IPLocator::isIPv4(ip))
2739+
{
2740+
EPROSIMA_LOG_ERROR(
2741+
WIREPROTOCOLQOS, "Invalid IP address format for ROS 2 Easy Mode. It must be an IPv4 address.");
2742+
2743+
return;
2744+
}
2745+
27372746
easy_mode_ = ip;
27382747
}
27392748

resources/xsd/fastdds_profiles.xsd

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
├ builtin [0~1],
132132
├ port [0~1],
133133
├ participantID [int32],
134+
├ easy_mode_ip [string],
134135
├ userTransports [0~1],
135136
| └ transport_id [1~*] [string],
136137
├ useBuiltinTransports [bool],
@@ -172,6 +173,7 @@
172173
<xs:element name="builtin" type="builtinAttributesType" minOccurs="0" maxOccurs="1"/>
173174
<xs:element name="port" type="portType" minOccurs="0" maxOccurs="1"/>
174175
<xs:element name="participantID" type="int32" minOccurs="0" maxOccurs="1"/>
176+
<xs:element name="easy_mode_ip" type="string" minOccurs="0" maxOccurs="1"/>
175177
<xs:element name="userTransports" minOccurs="0" maxOccurs="1">
176178
<xs:complexType>
177179
<xs:sequence>

src/cpp/rtps/attributes/ServerAttributes.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ const std::string& ros_easy_mode_env()
7373
{
7474
static std::string ip_value;
7575
SystemInfo::get_env(ROS2_EASY_MODE_URI, ip_value);
76+
77+
if (!ip_value.empty())
78+
{
79+
// Check that the value is a valid IPv4 address
80+
if (!IPLocator::isIPv4(ip_value))
81+
{
82+
EPROSIMA_LOG_ERROR(
83+
SERVERATTRIBUTES,
84+
"Invalid format: Easy Mode IP must be a valid IPv4 address. "
85+
"Ignoring " << ROS2_EASY_MODE_URI << " value.");
86+
87+
ip_value = "";
88+
}
89+
}
7690
return ip_value;
7791
}
7892

src/cpp/rtps/attributes/ServerAttributes.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ const std::string& ros_discovery_server_env();
198198

199199
/**
200200
* Get the value of environment variable ROS2_EASY_MODE_URI
201-
* @return The value of environment variable ROS2_EASY_MODE_URI. Empty string if the variable is not defined.
201+
* @return The value of environment variable ROS2_EASY_MODE_URI.
202+
* Empty string if the variable is not defined or does not have a valid IPv4 format.
202203
*/
203204
const std::string& ros_easy_mode_env();
204205

src/cpp/xmlparser/XMLParser.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -2450,10 +2450,20 @@ XMLP_ret XMLParser::fillDataNode(
24502450
else if (strcmp(name, EASY_MODE_IP) == 0)
24512451
{
24522452
// easy_mode_ip - stringType
2453-
if (XMLP_ret::XML_OK != getXMLString(p_aux0, &participant_node.get()->rtps.easy_mode_ip, ident))
2453+
std::string str_aux;
2454+
if (XMLP_ret::XML_OK != getXMLString(p_aux0, &str_aux, ident))
24542455
{
24552456
return XMLP_ret::XML_ERROR;
24562457
}
2458+
2459+
// Check that the string is a valid IPv4 address
2460+
if (!fastdds::rtps::IPLocator::isIPv4(str_aux))
2461+
{
2462+
EPROSIMA_LOG_ERROR(XMLPARSER, "'easy_mode_ip' is not a valid IPv4 address.");
2463+
return XMLP_ret::XML_ERROR;
2464+
}
2465+
2466+
participant_node.get()->rtps.easy_mode_ip = str_aux;
24572467
}
24582468
else if (strcmp(name, FLOW_CONTROLLER_DESCRIPTOR_LIST) == 0)
24592469
{

test/blackbox/common/DDSBlackboxTestsDSEasyMode.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,78 @@ TEST(DSEasyMode, easy_discovery_mode_env_inconsistent_ip)
361361
stop_background_servers();
362362
#endif // _WIN32
363363
}
364+
365+
/**
366+
* Check that the environment variable ROS2_EASY_MODE is ignored if
367+
* it does not have a valid IPv4 format.
368+
*/
369+
TEST(DSEasyMode, easy_discovery_mode_env_invalid)
370+
{
371+
#ifndef _WIN32 // The feature is not supported on Windows yet
372+
PubSubWriter<HelloWorldPubSubType> writer(TEST_TOPIC_NAME);
373+
PubSubReader<HelloWorldPubSubType> reader(TEST_TOPIC_NAME);
374+
375+
// Set ROS2_EASY_MODE to an invalid string value
376+
set_easy_discovery_mode_env("Foo");
377+
378+
std::atomic<bool> writer_background_ds_discovered(false);
379+
std::atomic<bool> reader_background_ds_discovered(false);
380+
381+
writer.set_on_discovery_function(
382+
[&writer_background_ds_discovered](
383+
const eprosima::fastdds::rtps::ParticipantBuiltinTopicData& data,
384+
eprosima::fastdds::rtps::ParticipantDiscoveryStatus)
385+
{
386+
if (data.participant_name == "DiscoveryServerAuto")
387+
{
388+
writer_background_ds_discovered.store(true);
389+
}
390+
return true;
391+
});
392+
writer.init();
393+
394+
reader.set_on_discovery_function(
395+
[&reader_background_ds_discovered](const eprosima::fastdds::rtps::ParticipantBuiltinTopicData& data,
396+
eprosima::fastdds::rtps::ParticipantDiscoveryStatus)
397+
{
398+
if (data.participant_name == "DiscoveryServerAuto")
399+
{
400+
reader_background_ds_discovered.store(true);
401+
}
402+
return true;
403+
});
404+
reader.init();
405+
406+
ASSERT_TRUE(writer.isInitialized());
407+
ASSERT_TRUE(reader.isInitialized());
408+
409+
// Wait for endpoint discovery first
410+
writer.wait_discovery();
411+
reader.wait_discovery();
412+
413+
// Check that no Background DS was discovered,
414+
// only the other reader or writer
415+
ASSERT_GE(writer.get_participants_matched(), 1u);
416+
ASSERT_GE(reader.get_participants_matched(), 1u);
417+
ASSERT_FALSE(writer_background_ds_discovered.load());
418+
ASSERT_FALSE(reader_background_ds_discovered.load());
419+
#endif // _WIN32
420+
}
421+
422+
/**
423+
* Check that easy mode configuration is ignored when setting it
424+
* with code using WireProtocolConfigQos.
425+
*
426+
* Note: This test only checks that the configuration is ignored.
427+
* Probably it should be extended similarly to easy_discovery_mode_env_invalid
428+
* when Configuring Easy Mode via c++ and XML is implemented.
429+
*/
430+
TEST(DSEasyMode, wire_protocol_qos_params_invalid)
431+
{
432+
eprosima::fastdds::dds::WireProtocolConfigQos wire_protocol_qos;
433+
434+
// Try to set easy mode IP using an invalid IP address
435+
wire_protocol_qos.easy_mode("Foo");
436+
437+
ASSERT_TRUE(wire_protocol_qos.easy_mode().empty());
438+
}

test/unittest/xmlparser/XMLParserTests.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,7 @@ TEST_F(XMLParserTests, parseLogConfig)
18131813
* 2. Check missing DomainId value in tag
18141814
* 3. Check bad values for all attributes
18151815
* 4. Check a non existant attribute tag
1816+
* 5. Check a non valid Easy Mode IP
18161817
*/
18171818
TEST_F(XMLParserTests, fillDataNodeParticipantNegativeClauses)
18181819
{
@@ -1885,6 +1886,13 @@ TEST_F(XMLParserTests, fillDataNodeParticipantNegativeClauses)
18851886
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParserTest::fillDataNode_wrapper(titleElement, *participant_node));
18861887

18871888
}
1889+
1890+
// Check invalid easy_mode_ip value (not a valid IPv4 address)
1891+
std::string invalid_easy_mode_ip = "<easy_mode_ip>Foo</easy_mode_ip>";
1892+
snprintf(xml, xml_len, xml_p, invalid_easy_mode_ip.c_str());
1893+
ASSERT_EQ(tinyxml2::XMLError::XML_SUCCESS, xml_doc.Parse(xml));
1894+
titleElement = xml_doc.RootElement();
1895+
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParserTest::fillDataNode_wrapper(titleElement, *participant_node));
18881896
}
18891897
}
18901898

versions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Forthcoming
1717
* New `ROS2_EASY_MODE` environment variable
1818
* Extended CLI ``discovery`` command to handle Fast DDS daemon.
1919
* Added P2P builtin transport.
20-
* Added new `easy_mode_ip` XML tag and `easy_mode_` member in `WireProtocolConfigQos` for Discovery Server IP configuration.
20+
* Added new `easy_mode_ip` XML tag and `easy_mode` setter in `WireProtocolConfigQos` for Easy Mode IP configuration through XML and code.
2121

2222
Version v3.1.0
2323
--------------

0 commit comments

Comments
 (0)