IPLocator.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file IPLocator.h
  16. *
  17. */
  18. #ifndef IP_LOCATOR_H_
  19. #define IP_LOCATOR_H_
  20. #include <vector>
  21. #include <string>
  22. #include <fastdds/rtps/common/Locator.h>
  23. namespace eprosima {
  24. namespace fastrtps {
  25. namespace rtps {
  26. /**
  27. * Class IPLocator, to provide helper functions to the IP based transports.
  28. * @ingroup UTILITIES_MODULE
  29. */
  30. class IPLocator
  31. {
  32. public:
  33. /**
  34. * Fills locator with the given parameters.
  35. * @param kindin Kind of the locator.
  36. * @param address IP Address of the locator as string.
  37. * @param portin Port of the locator.
  38. * @param locator Locator to be filled.
  39. */
  40. RTPS_DllAPI static void createLocator(
  41. int32_t kindin,
  42. const std::string& address,
  43. uint32_t portin,
  44. Locator_t& locator);
  45. //! Sets locator's IPv4.
  46. RTPS_DllAPI static bool setIPv4(
  47. Locator_t& locator,
  48. const unsigned char* addr);
  49. //! Sets locator's IPv4.
  50. RTPS_DllAPI static bool setIPv4(
  51. Locator_t& locator,
  52. octet o1,
  53. octet o2,
  54. octet o3,
  55. octet o4);
  56. //! Sets locator's IPv4.
  57. RTPS_DllAPI static bool setIPv4(
  58. Locator_t& locator,
  59. const std::string& ipv4);
  60. //! Copies locator's IPv4.
  61. RTPS_DllAPI static bool setIPv4(
  62. Locator_t& destlocator,
  63. const Locator_t& origlocator);
  64. //! Retrieves locator's IPv4 as octet array.
  65. RTPS_DllAPI static const octet* getIPv4(const Locator_t& locator);
  66. //! Check if the locator has IPv4.
  67. RTPS_DllAPI static bool hasIPv4(const Locator_t& locator);
  68. //! Returns a string representation of the locator's IPv4.
  69. RTPS_DllAPI static std::string toIPv4string(const Locator_t& locator);
  70. //! Copies locator's IPv4.
  71. RTPS_DllAPI static bool copyIPv4(
  72. const Locator_t& locator,
  73. unsigned char* dest);
  74. // IPv6
  75. //! Sets locator's IPv6.
  76. RTPS_DllAPI static bool setIPv6(
  77. Locator_t& locator,
  78. const unsigned char* addr);
  79. //! Sets locator's IPv6.
  80. RTPS_DllAPI static bool setIPv6(
  81. Locator_t& locator,
  82. uint16_t group0,
  83. uint16_t group1,
  84. uint16_t group2,
  85. uint16_t group3,
  86. uint16_t group4,
  87. uint16_t group5,
  88. uint16_t group6,
  89. uint16_t group7);
  90. //! Sets locator's IPv6.
  91. RTPS_DllAPI static bool setIPv6(
  92. Locator_t& locator,
  93. const std::string& ipv6);
  94. //! Copies locator's IPv6.
  95. RTPS_DllAPI static bool setIPv6(
  96. Locator_t& destlocator,
  97. const Locator_t& origlocator);
  98. //! Retrieves locator's IPv6 as octet array.
  99. RTPS_DllAPI static const octet* getIPv6(const Locator_t& locator);
  100. //! Check if the locator has IPv6.
  101. RTPS_DllAPI static bool hasIPv6(const Locator_t& locator);
  102. //! Returns a string representation of the locator's IPv6.
  103. RTPS_DllAPI static std::string toIPv6string(const Locator_t& locator);
  104. //! Copies locator's IPv6.
  105. RTPS_DllAPI static bool copyIPv6(
  106. const Locator_t& locator,
  107. unsigned char* dest);
  108. //! Sets locator's IP
  109. RTPS_DllAPI static bool ip(
  110. Locator_t& locator,
  111. const std::string& ip);
  112. //! Returns a string representation of the locator's IP.
  113. RTPS_DllAPI static std::string ip_to_string(const Locator_t& locator);
  114. // TCP
  115. //! Sets locator's logical port (as in RTCP protocol)
  116. RTPS_DllAPI static bool setLogicalPort(
  117. Locator_t& locator,
  118. uint16_t port);
  119. //! Gets locator's logical port (as in RTCP protocol)
  120. RTPS_DllAPI static uint16_t getLogicalPort(const Locator_t& locator);
  121. //! Sets locator's physical port (as in RTCP protocol)
  122. RTPS_DllAPI static bool setPhysicalPort(
  123. Locator_t& locator,
  124. uint16_t port);
  125. //! Gets locator's physical port (as in RTCP protocol)
  126. RTPS_DllAPI static uint16_t getPhysicalPort(const Locator_t& locator);
  127. // TCPv4
  128. //! Sets locator's WAN address (as in RTCP protocol)
  129. RTPS_DllAPI static bool setWan(
  130. Locator_t& locator,
  131. octet o1,
  132. octet o2,
  133. octet o3,
  134. octet o4);
  135. //! Sets locator's WAN address (as in RTCP protocol)
  136. RTPS_DllAPI static bool setWan(
  137. Locator_t& locator,
  138. const std::string& wan);
  139. //! Gets locator's WAN address (as in RTCP protocol)
  140. RTPS_DllAPI static const octet* getWan(const Locator_t& locator);
  141. //! Checks if the locator has WAN address (as in RTCP protocol)
  142. RTPS_DllAPI static bool hasWan(const Locator_t& locator);
  143. //! Retrieves a string representation of the locator's WAN address (as in RTCP protocol)
  144. RTPS_DllAPI static std::string toWanstring(const Locator_t& locator);
  145. //! Sets locator's LAN ID (as in RTCP protocol)
  146. RTPS_DllAPI static bool setLanID(
  147. Locator_t& locator,
  148. const std::string& lanId);
  149. //! Gets locator's LAN ID (as in RTCP protocol)
  150. RTPS_DllAPI static const octet* getLanID(const Locator_t& locator);
  151. //! Retrieves a string representation of the locator's LAN ID (as in RTCP protocol)
  152. RTPS_DllAPI static std::string toLanIDstring(const Locator_t& locator);
  153. //! Returns a new locator without logical port (as in RTCP protocol).
  154. RTPS_DllAPI static Locator_t toPhysicalLocator(const Locator_t& locator);
  155. //! Checks if a locator WAN address and IP address are the same (as in RTCP protocol).
  156. RTPS_DllAPI static bool ip_equals_wan(const Locator_t& locator);
  157. // Common
  158. //! Sets locator's RTPC port. Physical for UDP and logical for TCP (as in RTCP protocol)
  159. RTPS_DllAPI static bool setPortRTPS(
  160. Locator_t& locator,
  161. uint16_t port);
  162. //! Gets locator's RTPC port. Physical for UDP and logical for TCP (as in RTCP protocol)
  163. RTPS_DllAPI static uint16_t getPortRTPS(Locator_t& locator);
  164. //! Checks if a locator has local IP address.
  165. RTPS_DllAPI static bool isLocal(const Locator_t& locator);
  166. //! Checks if a locator has any IP address.
  167. RTPS_DllAPI static bool isAny(const Locator_t& locator);
  168. //! Checks if a both locators has the same IP address.
  169. RTPS_DllAPI static bool compareAddress(
  170. const Locator_t& loc1,
  171. const Locator_t& loc2,
  172. bool fullAddress = false);
  173. //! Checks if a both locators has the same IP address and physical port (as in RTCP protocol).
  174. RTPS_DllAPI static bool compareAddressAndPhysicalPort(
  175. const Locator_t& loc1,
  176. const Locator_t& loc2);
  177. //! Returns a string representation of the given locator.
  178. RTPS_DllAPI static std::string to_string(const Locator_t& locator);
  179. // UDP
  180. //! Checks if the locator has a multicast IP address.
  181. RTPS_DllAPI static bool isMulticast(const Locator_t& locator);
  182. private:
  183. IPLocator();
  184. virtual ~IPLocator();
  185. };
  186. }
  187. }
  188. } /* namespace eprosima */
  189. #endif /* IP_LOCATOR_H_ */