TCPv4Transport.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_TCPV4_TRANSPORT_H_
  15. #define _FASTDDS_TCPV4_TRANSPORT_H_
  16. #include <fastdds/rtps/transport/TCPTransportInterface.h>
  17. #include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
  18. #include <fastrtps/utils/IPFinder.h>
  19. #include <fastdds/rtps/transport/tcp/RTCPHeader.h>
  20. #include <asio.hpp>
  21. #include <thread>
  22. #include <vector>
  23. #include <map>
  24. #include <mutex>
  25. namespace eprosima{
  26. namespace fastdds{
  27. namespace rtps{
  28. /**
  29. * This is a default TCPv4 implementation.
  30. * - Opening an output channel by passing a remote locator will try to open a TCP conection with the endpoint.
  31. * If there is created a connection with the same endpoint, the transport will use the same one.
  32. *
  33. * - It is possible to provide a white list at construction, which limits the interfaces the transport
  34. * will ever be able to interact with. If left empty, all interfaces are allowed.
  35. *
  36. * - Opening an input channel by passing a locator will open a socket listening on the given physical port on every
  37. * whitelisted interface, it will wait for incomming connections until the receiver closes the channel.
  38. * Several endpoints can connect to other to the same physical port, because the OS creates a connection socket
  39. * after each establishment.
  40. * @ingroup TRANSPORT_MODULE
  41. */
  42. class TCPv4Transport : public TCPTransportInterface
  43. {
  44. protected:
  45. TCPv4TransportDescriptor configuration_;
  46. std::vector<asio::ip::address_v4> interface_whitelist_;
  47. //! Constructor with no descriptor is necessary for implementations derived from this class.
  48. TCPv4Transport();
  49. virtual bool compare_locator_ip(
  50. const fastrtps::rtps::Locator_t& lh,
  51. const fastrtps::rtps::Locator_t& rh) const override;
  52. virtual bool compare_locator_ip_and_port(
  53. const fastrtps::rtps::Locator_t& lh,
  54. const fastrtps::rtps::Locator_t& rh) const override;
  55. virtual void fill_local_ip(fastrtps::rtps::Locator_t& loc) const override;
  56. virtual asio::ip::tcp::endpoint generate_endpoint(uint16_t port) const override;
  57. virtual asio::ip::tcp::endpoint generate_endpoint(
  58. const fastrtps::rtps::Locator_t& loc,
  59. uint16_t port) const override;
  60. virtual asio::ip::tcp::endpoint generate_local_endpoint(
  61. fastrtps::rtps::Locator_t& loc,
  62. uint16_t port) const override;
  63. virtual asio::ip::tcp generate_protocol() const override;
  64. virtual asio::ip::tcp get_protocol_type() const override { return asio::ip::tcp::v4(); }
  65. virtual void get_ips(
  66. std::vector<fastrtps::rtps::IPFinder::info_IP>& locNames,
  67. bool return_loopback = false) const override;
  68. /**
  69. * Method to get a list of interfaces to bind the socket associated to the given locator.
  70. * @return Vector of interfaces in string format.
  71. */
  72. virtual std::vector<std::string> get_binding_interfaces_list() override;
  73. bool is_locator_allowed(const fastrtps::rtps::Locator_t& locator) const override;
  74. //! Checks if the given ip has been included in the white list to use it.
  75. virtual bool is_interface_allowed(const std::string& interface) const override;
  76. //! Checks if the given interface is allowed by the white list.
  77. bool is_interface_allowed(const asio::ip::address_v4& ip) const;
  78. //! Checks if the given interface is allowed by the white list.
  79. virtual bool is_interface_allowed(const fastrtps::rtps::Locator_t& loc) const override;
  80. //! Checks if the interfaces white list is empty.
  81. virtual bool is_interface_whitelist_empty() const override;
  82. virtual void set_receive_buffer_size(uint32_t size) override;
  83. virtual void set_send_buffer_size(uint32_t size) override;
  84. virtual void endpoint_to_locator(
  85. const asio::ip::tcp::endpoint& endpoint,
  86. fastrtps::rtps::Locator_t& locator) const override;
  87. public:
  88. RTPS_DllAPI TCPv4Transport(const TCPv4TransportDescriptor&);
  89. virtual ~TCPv4Transport() override;
  90. virtual const TCPTransportDescriptor* configuration() const override;
  91. virtual TCPTransportDescriptor* configuration() override;
  92. virtual fastrtps::rtps::LocatorList_t NormalizeLocator(const fastrtps::rtps::Locator_t& locator) override;
  93. virtual bool is_local_locator(const fastrtps::rtps::Locator_t& locator) const override;
  94. TransportDescriptorInterface* get_configuration() override { return &configuration_; }
  95. virtual void AddDefaultOutputLocator(fastrtps::rtps::LocatorList_t&) override;
  96. virtual uint16_t GetLogicalPortIncrement() const override;
  97. virtual uint16_t GetLogicalPortRange() const override;
  98. virtual uint16_t GetMaxLogicalPort() const override;
  99. virtual bool fillMetatrafficUnicastLocator(
  100. fastrtps::rtps::Locator_t &locator,
  101. uint32_t metatraffic_unicast_port) const override;
  102. virtual bool fillUnicastLocator(
  103. fastrtps::rtps::Locator_t &locator,
  104. uint32_t well_known_port) const override;
  105. };
  106. } // namespace rtps
  107. } // namespace fastdds
  108. } // namespace eprosima
  109. #endif // _FASTDDS_TCPV4_TRANSPORT_H_