19#ifndef FASTDDS_RTPS_COMMON__LOCATOR_HPP
20#define FASTDDS_RTPS_COMMON__LOCATOR_HPP
30#include <fastdds/config.hpp>
31#include <fastdds/dds/log/Log.hpp>
32#include <fastdds/fastdds_dll.hpp>
33#include <fastdds/rtps/common/Types.hpp>
34#include <fastdds/utils/IPLocator.hpp>
41#define LOCATOR_INVALID(loc) {loc.kind = LOCATOR_KIND_INVALID; loc.port = LOCATOR_PORT_INVALID; \
42 LOCATOR_ADDRESS_INVALID(loc.address); \
45#define LOCATOR_KIND_INVALID -1
48#define LOCATOR_ADDRESS_INVALID(a) {std::memset(a, 0x00, 16 * sizeof(octet));}
51#define LOCATOR_PORT_INVALID 0
54#define LOCATOR_KIND_RESERVED 0
56#define LOCATOR_KIND_UDPv4 1
58#define LOCATOR_KIND_UDPv6 2
60#define LOCATOR_KIND_TCPv4 4
62#define LOCATOR_KIND_TCPv6 8
64#define LOCATOR_KIND_SHM 16 + FASTDDS_VERSION_MAJOR
66#define LOCATOR_KIND_ETHERNET 0x02000000
100 :
kind(LOCATOR_KIND_UDPv4)
103 LOCATOR_ADDRESS_INVALID(
address);
127 :
kind(LOCATOR_KIND_UDPv4)
130 LOCATOR_ADDRESS_INVALID(
address);
140 LOCATOR_ADDRESS_INVALID(
address);
183 uint16_t field)
const
193 LOCATOR_ADDRESS_INVALID(
address);
221 if (loc.
kind == LOCATOR_KIND_UDPv4 || loc.
kind == LOCATOR_KIND_TCPv4)
223 for (uint8_t i = 12; i < 16; ++i)
231 else if (loc.
kind == LOCATOR_KIND_UDPv6 || loc.
kind == LOCATOR_KIND_TCPv6 ||
232 loc.
kind == LOCATOR_KIND_SHM || loc.
kind == LOCATOR_KIND_ETHERNET)
234 for (uint8_t i = 0; i < 16; ++i)
255 return (0 <= loc.
kind);
270 return memcmp(&loc1, &loc2,
sizeof(
Locator_t)) < 0;
312 return !(loc1 == loc2);
333 std::ostream& output,
339 case LOCATOR_KIND_TCPv4:
344 case LOCATOR_KIND_UDPv4:
349 case LOCATOR_KIND_TCPv6:
354 case LOCATOR_KIND_UDPv6:
359 case LOCATOR_KIND_SHM:
364 case LOCATOR_KIND_ETHERNET:
371 output <<
"Invalid_locator:[_]:0";
379 case LOCATOR_KIND_UDPv4:
380 case LOCATOR_KIND_TCPv4:
384 case LOCATOR_KIND_UDPv6:
385 case LOCATOR_KIND_TCPv6:
389 case LOCATOR_KIND_ETHERNET:
390 output << std::hex << std::setfill(
'0') << std::setw(2) << (int)loc.
address[10];
391 for (
int i = 1; i < 6; ++i)
393 output <<
":" << std::hex << std::setfill(
'0') << std::setw(2) << (int)loc.
address[10 + i];
397 case LOCATOR_KIND_SHM:
410 if (loc.
kind == LOCATOR_KIND_TCPv4 || loc.
kind == LOCATOR_KIND_TCPv6)
418 output <<
"]:" << loc.
port;
445 std::istream::sentry s(input);
449 std::ios_base::iostate excp_mask = input.exceptions();
453 input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
461 std::stringbuf sb_kind;
462 std::stringbuf sb_address;
463 std::string str_kind;
467 input.get(sb_kind,
':');
468 str_kind = sb_kind.str();
470 if (str_kind ==
"SHM")
472 kind = LOCATOR_KIND_SHM;
474 else if (str_kind ==
"ETH")
476 kind = LOCATOR_KIND_ETHERNET;
478 else if (str_kind ==
"TCPv4")
480 kind = LOCATOR_KIND_TCPv4;
482 else if (str_kind ==
"TCPv6")
484 kind = LOCATOR_KIND_TCPv6;
486 else if (str_kind ==
"UDPv4")
488 kind = LOCATOR_KIND_UDPv4;
490 else if (str_kind ==
"UDPv6")
492 kind = LOCATOR_KIND_UDPv6;
496 kind = LOCATOR_KIND_INVALID;
497 loc.
kind = LOCATOR_KIND_INVALID;
500 if (kind != LOCATOR_KIND_INVALID)
503 input >> punct >> punct;
506 input.get(sb_address,
']');
507 address = sb_address.str();
510 if ((kind == LOCATOR_KIND_UDPv4 || kind == LOCATOR_KIND_TCPv4) &&
514 if (addresses.first.empty())
516 loc.
kind = LOCATOR_KIND_INVALID;
517 EPROSIMA_LOG_WARNING(LOCATOR,
"Error deserializing Locator");
520 address = *addresses.first.begin();
522 if ((kind == LOCATOR_KIND_UDPv6 || kind == LOCATOR_KIND_TCPv6) &&
526 if (addresses.second.empty())
528 loc.
kind = LOCATOR_KIND_INVALID;
529 EPROSIMA_LOG_WARNING(LOCATOR,
"Error deserializing Locator");
532 address = *addresses.second.begin();
534 if ((kind == LOCATOR_KIND_SHM) && (address !=
"M") && (address !=
"_"))
536 loc.
kind = LOCATOR_KIND_INVALID;
537 EPROSIMA_LOG_WARNING(LOCATOR,
"Error deserializing Locator");
542 input >> punct >> punct;
550 catch (std::ios_base::failure& )
552 loc.
kind = LOCATOR_KIND_INVALID;
553 EPROSIMA_LOG_WARNING(LOCATOR,
"Error deserializing Locator");
556 input.exceptions(excp_mask);
static FASTDDS_EXPORTED_API bool isIPv4(const std::string &address)
Check whether a string contains an IPv4 format.
static FASTDDS_EXPORTED_API std::pair< std::set< std::string >, std::set< std::string > > resolveNameDNS(const std::string &address_name)
Resolve an address name by a DNS request and return the IP that this address references by a DNS serv...
static FASTDDS_EXPORTED_API uint16_t getLogicalPort(const Locator_t &locator)
Gets locator's logical port (as in RTCP protocol).
static FASTDDS_EXPORTED_API bool isIPv6(const std::string &address)
Check whether a string contains an IPv6 format.
static FASTDDS_EXPORTED_API uint16_t getPhysicalPort(const Locator_t &locator)
Gets locator's physical port (as in RTCP protocol).
static FASTDDS_EXPORTED_API std::string toIPv6string(const Locator_t &locator)
Returns a string representation of the locator's IPv6 following RFC 5952 recommendation.
static FASTDDS_EXPORTED_API std::string toIPv4string(const Locator_t &locator)
Returns a string representation of the locator's IPv4.
Class Locator_t, uniquely identifies a communication channel for a particular transport.
Definition Locator.hpp:74
int32_t kind
Specifies the locator type.
Definition Locator.hpp:92
octet address[16]
IP address.
Definition Locator.hpp:96
Locator_t(uint32_t portin)
Port constructor.
Definition Locator.hpp:125
bool set_address(const Locator_t &other)
Set the locator IP address using another locator.
Definition Locator.hpp:159
octet get_address(uint16_t field) const
Getter for a specific field of the locator IP address.
Definition Locator.hpp:182
octet * get_address()
Getter for the locator IP address.
Definition Locator.hpp:171
Locator_t(int32_t kindin, uint32_t portin)
Kind and port constructor.
Definition Locator.hpp:134
static Locator_t create_locator(int32_t kind, const std::string &address, uint32_t port)
Create a locator with the given parameters.
uint32_t port
Network port.
Definition Locator.hpp:94
void set_Invalid_Address()
Automatic setter for setting locator IP address to invalid address (0).
Definition Locator.hpp:191
Locator_t(Locator_t &&loc)
Move constructor.
Definition Locator.hpp:107
Locator_t()
Default constructor.
Definition Locator.hpp:99
Locator_t(const Locator_t &loc)
Copy constructor.
Definition Locator.hpp:116
Contains the RTPS protocol implementation.
std::istream & operator>>(std::istream &input, EntityId_t &enP)
Definition EntityId_t.hpp:289
bool operator==(const BuiltinTransportsOptions &bto1, const BuiltinTransportsOptions &bto2)
Equal to operator.
Definition BuiltinTransports.hpp:79
std::ostream & operator<<(std::ostream &output, BuiltinTransports transports)
Definition BuiltinTransports.hpp:118
bool IsLocatorValid(const Locator_t &loc)
Auxiliary method to check that locator kind is not LOCATOR_KIND_INVALID (-1).
Definition Locator.hpp:252
bool operator!=(const EntityId_t &id1, const EntityId_t &id2)
Guid prefix comparison operator.
Definition EntityId_t.hpp:267
bool operator<(const GUID_t &g1, const GUID_t &g2)
Definition Guid.hpp:192
std::vector< Locator_t >::const_iterator LocatorListConstIterator
Constant iterator to iterate over a vector of locators.
Definition Locator.hpp:562
uint8_t octet
Definition Types.hpp:83
std::vector< Locator_t >::iterator LocatorListIterator
Iterator to iterate over a vector of locators.
Definition Locator.hpp:561
eprosima::fastdds::rtps::Locator_t Locator
Definition Locator.hpp:572
bool IsAddressDefined(const Locator_t &loc)
Auxiliary method to check that IP address is not invalid (0).
Definition Locator.hpp:218