19#ifndef FASTDDS_RTPS_COMMON__LOCATOR_HPP
20#define FASTDDS_RTPS_COMMON__LOCATOR_HPP
29#include <fastdds/config.hpp>
30#include <fastdds/dds/log/Log.hpp>
31#include <fastdds/fastdds_dll.hpp>
32#include <fastdds/rtps/common/Types.hpp>
33#include <fastdds/utils/IPLocator.hpp>
40#define LOCATOR_INVALID(loc) {loc.kind = LOCATOR_KIND_INVALID; loc.port = LOCATOR_PORT_INVALID; \
41 LOCATOR_ADDRESS_INVALID(loc.address); \
44#define LOCATOR_KIND_INVALID -1
47#define LOCATOR_ADDRESS_INVALID(a) {std::memset(a, 0x00, 16 * sizeof(octet));}
50#define LOCATOR_PORT_INVALID 0
53#define LOCATOR_KIND_RESERVED 0
55#define LOCATOR_KIND_UDPv4 1
57#define LOCATOR_KIND_UDPv6 2
59#define LOCATOR_KIND_TCPv4 4
61#define LOCATOR_KIND_TCPv6 8
63#define LOCATOR_KIND_SHM 16 + FASTDDS_VERSION_MAJOR
95 : kind(LOCATOR_KIND_UDPv4)
98 LOCATOR_ADDRESS_INVALID(address);
107 std::memcpy(address, loc.address, 16 *
sizeof(
octet));
122 : kind(LOCATOR_KIND_UDPv4)
125 LOCATOR_ADDRESS_INVALID(address);
135 LOCATOR_ADDRESS_INVALID(address);
178 uint16_t field)
const
180 return address[field];
188 LOCATOR_ADDRESS_INVALID(address);
203 if (loc.
kind == LOCATOR_KIND_UDPv4 || loc.
kind == LOCATOR_KIND_TCPv4)
205 for (uint8_t i = 12; i < 16; ++i)
213 else if (loc.
kind == LOCATOR_KIND_UDPv6 || loc.
kind == LOCATOR_KIND_TCPv6)
215 for (uint8_t i = 0; i < 16; ++i)
236 return (0 <= loc.
kind);
251 return memcmp(&loc1, &loc2,
sizeof(
Locator_t)) < 0;
293 return !(loc1 == loc2);
313 std::ostream& output,
319 case LOCATOR_KIND_TCPv4:
324 case LOCATOR_KIND_UDPv4:
329 case LOCATOR_KIND_TCPv6:
334 case LOCATOR_KIND_UDPv6:
339 case LOCATOR_KIND_SHM:
346 output <<
"Invalid_locator:[_]:0";
352 if (loc.
kind == LOCATOR_KIND_UDPv4 || loc.
kind == LOCATOR_KIND_TCPv4)
356 else if (loc.
kind == LOCATOR_KIND_UDPv6 || loc.
kind == LOCATOR_KIND_TCPv6)
360 else if (loc.
kind == LOCATOR_KIND_SHM)
373 if (loc.
kind == LOCATOR_KIND_TCPv4 || loc.
kind == LOCATOR_KIND_TCPv6)
380 output <<
"]:" << loc.
port;
406 std::istream::sentry s(input);
410 std::ios_base::iostate excp_mask = input.exceptions();
414 input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
422 std::stringbuf sb_kind;
423 std::stringbuf sb_address;
424 std::string str_kind;
428 input.get(sb_kind,
':');
429 str_kind = sb_kind.str();
431 if (str_kind ==
"SHM")
433 kind = LOCATOR_KIND_SHM;
435 else if (str_kind ==
"TCPv4")
437 kind = LOCATOR_KIND_TCPv4;
439 else if (str_kind ==
"TCPv6")
441 kind = LOCATOR_KIND_TCPv6;
443 else if (str_kind ==
"UDPv4")
445 kind = LOCATOR_KIND_UDPv4;
447 else if (str_kind ==
"UDPv6")
449 kind = LOCATOR_KIND_UDPv6;
453 kind = LOCATOR_KIND_INVALID;
454 loc.
kind = LOCATOR_KIND_INVALID;
457 if (kind != LOCATOR_KIND_INVALID)
460 input >> punct >> punct;
463 input.get(sb_address,
']');
464 address = sb_address.str();
467 if ((kind == LOCATOR_KIND_UDPv4 || kind == LOCATOR_KIND_TCPv4) &&
471 if (addresses.first.empty())
473 loc.
kind = LOCATOR_KIND_INVALID;
474 EPROSIMA_LOG_WARNING(LOCATOR,
"Error deserializing Locator");
477 address = *addresses.first.begin();
479 if ((kind == LOCATOR_KIND_UDPv6 || kind == LOCATOR_KIND_TCPv6) &&
483 if (addresses.second.empty())
485 loc.
kind = LOCATOR_KIND_INVALID;
486 EPROSIMA_LOG_WARNING(LOCATOR,
"Error deserializing Locator");
489 address = *addresses.second.begin();
493 input >> punct >> punct;
501 catch (std::ios_base::failure& )
503 loc.
kind = LOCATOR_KIND_INVALID;
504 EPROSIMA_LOG_WARNING(LOCATOR,
"Error deserializing Locator");
507 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.
static FASTDDS_EXPORTED_API void createLocator(int32_t kindin, const std::string &address, uint32_t portin, Locator_t &locator)
Fills locator with the given parameters.
Class Locator_t, uniquely identifies a communication channel for a particular transport.
Definition Locator.hpp:71
int32_t kind
Specifies the locator type.
Definition Locator.hpp:87
octet address[16]
IP address.
Definition Locator.hpp:91
Locator_t(uint32_t portin)
Port constructor.
Definition Locator.hpp:120
bool set_address(const Locator_t &other)
Set the locator IP address using another locator.
Definition Locator.hpp:154
octet get_address(uint16_t field) const
Getter for a specific field of the locator IP address.
Definition Locator.hpp:177
octet * get_address()
Getter for the locator IP address.
Definition Locator.hpp:166
Locator_t(int32_t kindin, uint32_t portin)
Kind and port constructor.
Definition Locator.hpp:129
uint32_t port
Network port.
Definition Locator.hpp:89
void set_Invalid_Address()
Automatic setter for setting locator IP address to invalid address (0).
Definition Locator.hpp:186
Locator_t(Locator_t &&loc)
Move constructor.
Definition Locator.hpp:102
Locator_t()
Default constructor.
Definition Locator.hpp:94
Locator_t(const Locator_t &loc)
Copy constructor.
Definition Locator.hpp:111
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:117
bool IsLocatorValid(const Locator_t &loc)
Auxiliary method to check that locator kind is not LOCATOR_KIND_INVALID (-1).
Definition Locator.hpp:233
unsigned char octet
Definition Types.hpp:83
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:513
std::vector< Locator_t >::iterator LocatorListIterator
Iterator to iterate over a vector of locators.
Definition Locator.hpp:512
bool IsAddressDefined(const Locator_t &loc)
Auxiliary method to check that IP address is not invalid (0).
Definition Locator.hpp:200