UDPv4Transport.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_UDPV4_TRANSPORT_H
  15. #define _FASTDDS_UDPV4_TRANSPORT_H
  16. #include <fastdds/rtps/transport/UDPTransportInterface.h>
  17. #include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
  18. namespace eprosima{
  19. namespace fastdds{
  20. namespace rtps{
  21. /**
  22. * This is a default UDPv4 implementation.
  23. * - Opening an output channel by passing a locator will open a socket per interface on the given port.
  24. * This collection of sockets constitute the "outbound channel". In other words, a channel corresponds
  25. * to a port + a direction.
  26. *
  27. * - It is possible to provide a white list at construction, which limits the interfaces the transport
  28. * will ever be able to interact with. If left empty, all interfaces are allowed.
  29. *
  30. * - Opening an input channel by passing a locator will open a socket listening on the given port on every
  31. * whitelisted interface, and join the multicast channel specified by the locator address. Hence, any locator
  32. * that does not correspond to the multicast range will simply open the port without a subsequent join. Joining
  33. * multicast groups late is supported by attempting to open the channel again with the same port + a
  34. * multicast address (the OpenInputChannel function will fail, however, because no new channel has been
  35. * opened in a strict sense).
  36. * @ingroup TRANSPORT_MODULE
  37. */
  38. class UDPv4Transport : public UDPTransportInterface
  39. {
  40. public:
  41. RTPS_DllAPI UDPv4Transport(const UDPv4TransportDescriptor&);
  42. virtual ~UDPv4Transport() override;
  43. virtual const UDPTransportDescriptor* configuration() const override;
  44. /**
  45. * Starts listening on the specified port, and if the specified address is in the
  46. * multicast range, it joins the specified multicast group,
  47. */
  48. virtual bool OpenInputChannel(const fastrtps::rtps::Locator_t&, TransportReceiverInterface*, uint32_t) override;
  49. virtual fastrtps::rtps::LocatorList_t NormalizeLocator(const fastrtps::rtps::Locator_t& locator) override;
  50. virtual bool is_local_locator(const fastrtps::rtps::Locator_t& locator) const override;
  51. TransportDescriptorInterface* get_configuration() override { return &configuration_; }
  52. virtual void AddDefaultOutputLocator(fastrtps::rtps::LocatorList_t &defaultList) override;
  53. virtual bool getDefaultMetatrafficMulticastLocators(fastrtps::rtps::LocatorList_t &locators,
  54. uint32_t metatraffic_multicast_port) const override;
  55. virtual bool getDefaultMetatrafficUnicastLocators(fastrtps::rtps::LocatorList_t &locators,
  56. uint32_t metatraffic_unicast_port) const override;
  57. bool getDefaultUnicastLocators(fastrtps::rtps::LocatorList_t &locators, uint32_t unicast_port) const override;
  58. protected:
  59. //! Constructor with no descriptor is necessary for implementations derived from this class.
  60. UDPv4Transport();
  61. UDPv4TransportDescriptor configuration_;
  62. virtual bool compare_locator_ip(const fastrtps::rtps::Locator_t& lh, const fastrtps::rtps::Locator_t& rh) const override;
  63. virtual bool compare_locator_ip_and_port(const fastrtps::rtps::Locator_t& lh, const fastrtps::rtps::Locator_t& rh) const override;
  64. virtual void endpoint_to_locator(asio::ip::udp::endpoint& endpoint, fastrtps::rtps::Locator_t& locator) override;
  65. virtual void fill_local_ip(fastrtps::rtps::Locator_t& loc) const override;
  66. virtual asio::ip::udp::endpoint GenerateAnyAddressEndpoint(uint16_t port) override;
  67. virtual asio::ip::udp::endpoint generate_endpoint(uint16_t port) override;
  68. virtual asio::ip::udp::endpoint generate_endpoint(const std::string& sIp, uint16_t port) override;
  69. virtual asio::ip::udp::endpoint generate_endpoint(const fastrtps::rtps::Locator_t& loc, uint16_t port) override;
  70. virtual asio::ip::udp::endpoint generate_local_endpoint(const fastrtps::rtps::Locator_t& loc, uint16_t port) override;
  71. virtual asio::ip::udp generate_protocol() const override;
  72. virtual void get_ips(std::vector<fastrtps::rtps::IPFinder::info_IP>& locNames, bool return_loopback = false) override;
  73. eProsimaUDPSocket OpenAndBindInputSocket(const std::string& sIp, uint16_t port, bool is_multicast) override;
  74. //! Checks if the given interface is allowed by the white list.
  75. virtual bool is_interface_allowed(const std::string& interface) const override;
  76. /**
  77. * Method to get a list of interfaces to bind the socket associated to the given locator.
  78. * @return Vector of interfaces in string format.
  79. */
  80. virtual std::vector<std::string> get_binding_interfaces_list() override;
  81. //! Checks for whether locator is allowed.
  82. virtual bool is_locator_allowed(const fastrtps::rtps::Locator_t&) const override;
  83. //! Checks if the given interface is allowed by the white list.
  84. bool is_interface_allowed(const asio::ip::address_v4& ip) const;
  85. //! Checks if the interfaces white list is empty.
  86. virtual bool is_interface_whitelist_empty() const override;
  87. std::vector<asio::ip::address_v4> interface_whitelist_;
  88. virtual void set_receive_buffer_size(uint32_t size) override;
  89. virtual void set_send_buffer_size(uint32_t size) override;
  90. virtual void SetSocketOutboundInterface(eProsimaUDPSocket&, const std::string&) override;
  91. };
  92. } // namespace rtps
  93. } // namespace fastdds
  94. } // namespace eprosima
  95. #endif // _FASTDDS_UDPV4_TRANSPORT_H