UDPTransportInterface.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2019 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. #ifndef _FASTDDS_UDP_TRANSPORT_INTERFACE_H_
  15. #define _FASTDDS_UDP_TRANSPORT_INTERFACE_H_
  16. #include <asio.hpp>
  17. #include <thread>
  18. #include <fastdds/rtps/transport/TransportInterface.h>
  19. #include <fastdds/rtps/transport/UDPChannelResource.h>
  20. #include <fastdds/rtps/transport/UDPTransportDescriptor.h>
  21. #include <fastrtps/utils/IPFinder.h>
  22. #include <vector>
  23. #include <memory>
  24. #include <map>
  25. #include <mutex>
  26. namespace eprosima{
  27. namespace fastdds{
  28. namespace rtps{
  29. class UDPTransportInterface : public TransportInterface
  30. {
  31. public:
  32. virtual ~UDPTransportInterface() override;
  33. void clean();
  34. //! Removes the listening socket for the specified port.
  35. virtual bool CloseInputChannel(const fastrtps::rtps::Locator_t&) override;
  36. //! Removes all outbound sockets on the given port.
  37. void CloseOutputChannel(eProsimaUDPSocket& socket);
  38. //! Reports whether Locators correspond to the same port.
  39. virtual bool DoInputLocatorsMatch(const fastrtps::rtps::Locator_t&, const fastrtps::rtps::Locator_t&) const override;
  40. virtual const UDPTransportDescriptor* configuration() const = 0;
  41. bool init() override;
  42. //! Checks whether there are open and bound sockets for the given port.
  43. virtual bool IsInputChannelOpen(const fastrtps::rtps::Locator_t&) const override;
  44. //! Checks for TCP kinds.
  45. virtual bool IsLocatorSupported(const fastrtps::rtps::Locator_t&) const override;
  46. //! Opens a socket on the given address and port (as long as they are white listed).
  47. virtual bool OpenOutputChannel(
  48. SendResourceList& sender_resource_list,
  49. const fastrtps::rtps::Locator_t&) override;
  50. /**
  51. * Converts a given remote locator (that is, a locator referring to a remote
  52. * destination) to the main local locator whose channel can write to that
  53. * destination. In this case it will return a 0.0.0.0 address on that port.
  54. */
  55. virtual fastrtps::rtps::Locator_t RemoteToMainLocal(const fastrtps::rtps::Locator_t&) const override;
  56. /**
  57. * Transforms a remote locator into a locator optimized for local communications.
  58. *
  59. * If the remote locator corresponds to one of the local interfaces, it is converted
  60. * to the corresponding local address.
  61. *
  62. * @param [in] remote_locator Locator to be converted.
  63. * @param [out] result_locator Converted locator.
  64. *
  65. * @return false if the input locator is not supported/allowed by this transport, true otherwise.
  66. */
  67. virtual bool transform_remote_locator(
  68. const fastrtps::rtps::Locator_t& remote_locator,
  69. fastrtps::rtps::Locator_t& result_locator) const override;
  70. /**
  71. * Blocking Send through the specified channel. In both modes, using a localLocator of 0.0.0.0 will
  72. * send through all whitelisted interfaces provided the channel is open.
  73. * @param send_buffer Slice into the raw data to send.
  74. * @param send_buffer_size Size of the raw data. It will be used as a bounds check for the previous argument.
  75. * It must not exceed the send_buffer_size fed to this class during construction.
  76. * @param socket channel we're sending from.
  77. * @param destination_locators_begin pointer to destination locators iterator begin, the iterator can be advanced inside this fuction
  78. * so should not be reuse.
  79. * @param destination_locators_end pointer to destination locators iterator end, the iterator can be advanced inside this fuction
  80. * so should not be reuse.
  81. * @param only_multicast_purpose
  82. * @param max_blocking_time_point maximum blocking time.
  83. */
  84. virtual bool send(
  85. const fastrtps::rtps::octet* send_buffer,
  86. uint32_t send_buffer_size,
  87. eProsimaUDPSocket& socket,
  88. fastrtps::rtps::LocatorsIterator* destination_locators_begin,
  89. fastrtps::rtps::LocatorsIterator* destination_locators_end,
  90. bool only_multicast_purpose,
  91. const std::chrono::steady_clock::time_point& max_blocking_time_point);
  92. /**
  93. * Performs the locator selection algorithm for this transport.
  94. *
  95. * It basically consists of the following steps
  96. * - selector.transport_starts is called
  97. * - transport handles the selection state of each locator
  98. * - if a locator from an entry is selected, selector.select is called for that entry
  99. *
  100. * In the case of UDP, multicast locators are selected when present in more than one entry,
  101. * otherwise unicast locators are selected.
  102. *
  103. * @param [in, out] selector Locator selector.
  104. */
  105. virtual void select_locators(fastrtps::rtps::LocatorSelector& selector) const override;
  106. virtual bool fillMetatrafficMulticastLocator(fastrtps::rtps::Locator_t &locator,
  107. uint32_t metatraffic_multicast_port) const override;
  108. virtual bool fillMetatrafficUnicastLocator(fastrtps::rtps::Locator_t &locator, uint32_t metatraffic_unicast_port) const override;
  109. virtual bool configureInitialPeerLocator(fastrtps::rtps::Locator_t &locator, const fastrtps::rtps::PortParameters &port_params, uint32_t domainId,
  110. fastrtps::rtps::LocatorList_t& list) const override;
  111. virtual bool fillUnicastLocator(fastrtps::rtps::Locator_t &locator, uint32_t well_known_port) const override;
  112. virtual uint32_t max_recv_buffer_size() const override
  113. {
  114. return configuration()->maxMessageSize;
  115. }
  116. protected:
  117. friend class UDPChannelResource;
  118. // For UDPv6, the notion of channel corresponds to a port + direction tuple.
  119. asio::io_service io_service_;
  120. std::vector<fastrtps::rtps::IPFinder::info_IP> currentInterfaces;
  121. mutable std::recursive_mutex mInputMapMutex;
  122. std::map<uint16_t, std::vector<UDPChannelResource*>> mInputSockets;
  123. uint32_t mSendBufferSize;
  124. uint32_t mReceiveBufferSize;
  125. UDPTransportInterface(int32_t transport_kind);
  126. virtual bool compare_locator_ip(const fastrtps::rtps::Locator_t& lh, const fastrtps::rtps::Locator_t& rh) const = 0;
  127. virtual bool compare_locator_ip_and_port(const fastrtps::rtps::Locator_t& lh, const fastrtps::rtps::Locator_t& rh) const = 0;
  128. virtual void endpoint_to_locator(asio::ip::udp::endpoint& endpoint, fastrtps::rtps::Locator_t& locator) = 0;
  129. virtual void fill_local_ip(fastrtps::rtps::Locator_t& loc) const = 0;
  130. virtual asio::ip::udp::endpoint GenerateAnyAddressEndpoint(uint16_t port) = 0;
  131. virtual asio::ip::udp::endpoint generate_endpoint(uint16_t port) = 0;
  132. virtual asio::ip::udp::endpoint generate_endpoint(const std::string& sIp, uint16_t port) = 0;
  133. virtual asio::ip::udp::endpoint generate_endpoint(const fastrtps::rtps::Locator_t& loc, uint16_t port) = 0;
  134. virtual asio::ip::udp::endpoint generate_local_endpoint(const fastrtps::rtps::Locator_t& loc, uint16_t port) = 0;
  135. virtual asio::ip::udp generate_protocol() const = 0;
  136. virtual void get_ips(std::vector<fastrtps::rtps::IPFinder::info_IP>& locNames, bool return_loopback = false) = 0;
  137. //! Checks if the interfaces white list is empty.
  138. virtual bool is_interface_whitelist_empty() const = 0;
  139. //! Checks if the given interface is allowed by the white list.
  140. virtual bool is_interface_allowed(const std::string& interface) const = 0;
  141. /**
  142. * Method to get a list of interfaces to bind the socket associated to the given locator.
  143. * @return Vector of interfaces in string format.
  144. */
  145. virtual std::vector<std::string> get_binding_interfaces_list() = 0;
  146. bool OpenAndBindInputSockets(const fastrtps::rtps::Locator_t& locator, TransportReceiverInterface* receiver, bool is_multicast,
  147. uint32_t maxMsgSize);
  148. UDPChannelResource* CreateInputChannelResource(const std::string& sInterface, const fastrtps::rtps::Locator_t& locator,
  149. bool is_multicast, uint32_t maxMsgSize, TransportReceiverInterface* receiver);
  150. virtual eProsimaUDPSocket OpenAndBindInputSocket(const std::string& sIp, uint16_t port, bool is_multicast) = 0;
  151. eProsimaUDPSocket OpenAndBindUnicastOutputSocket(const asio::ip::udp::endpoint& endpoint, uint16_t& port);
  152. virtual void set_receive_buffer_size(uint32_t size) = 0;
  153. virtual void set_send_buffer_size(uint32_t size) = 0;
  154. virtual void SetSocketOutboundInterface(eProsimaUDPSocket&, const std::string&) = 0;
  155. /**
  156. * Send a buffer to a destination
  157. */
  158. bool send(
  159. const fastrtps::rtps::octet* send_buffer,
  160. uint32_t send_buffer_size,
  161. eProsimaUDPSocket& socket,
  162. const fastrtps::rtps::Locator_t& remote_locator,
  163. bool only_multicast_purpose,
  164. const std::chrono::microseconds& timeout);
  165. };
  166. } // namespace rtps
  167. } // namespace fastdds
  168. } // namespace eprosima
  169. #endif // _FASTDDS_UDP_TRANSPORT_INTERFACE_H_