OpenDDS  Snapshot(2023/04/28-20:55)
NetworkResource.h
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #ifndef OPENDDS_DCPS_NETWORK_RESOURCE_H
9 #define OPENDDS_DCPS_NETWORK_RESOURCE_H
10 
11 #include "dcps_export.h"
12 #include "PoolAllocator.h"
13 
14 #include <dds/OpenddsDcpsExtC.h>
15 
16 #include <tao/Basic_Types.h>
17 
18 #include <ace/INET_Addr.h>
19 #include <ace/CDR_Stream.h>
20 #include <ace/SString.h>
21 #include <ace/SOCK_Dgram.h>
22 
23 #include <cstring>
24 
26 
27 namespace OpenDDS {
28 namespace DCPS {
29 
30 struct HostnameInfo {
31  size_t index_;
33 };
34 
35 typedef OPENDDS_VECTOR(HostnameInfo) HostnameInfoVector;
36 
37 /**
38  * @struct NetworkResource
39  *
40  * @brief Defines a wrapper around address info which is used for advertise.
41  *
42  *
43  * This is used to send/receive an address information through transport.
44  */
47  explicit NetworkResource(const ACE_INET_Addr& addr);
48  explicit NetworkResource(const String& addr);
49 
50  ~NetworkResource();
51 
52  void dump() const;
53 
54  /// Accessor to populate the provided ACE_INET_Addr object from the
55  /// address string received through transport.
56  void to_addr(ACE_INET_Addr& addr) const;
57 
58  /// Reserve byte for some feature supports in the future.
59  /// e.g. version support.
61 
62  /// The address in string format. e.g. ip:port, hostname:port
64 };
65 
66 // Make sure that choose_single_coherent_address picks one of the IP interface addresses.
67 bool verify_hostname(const String& hostname, ACE_INET_Addr* addrs, size_t addr_count,
68  bool prefer_loopback, bool allow_ipv4_fallback);
69 
70 /// Helper function to get the fully qualified hostname.
71 /// It attempts to discover the FQDN by the network interface addresses, however
72 /// the result is impacted by the network configuration, so it returns name in the
73 /// order whoever is found first - FQDN, short hostname, name resolved from loopback
74 /// address. In the case using short hostname or name resolved from loopback, a
75 /// warning is logged. If there is no any name discovered from network interfaces,
76 /// an error is logged.
77 /// If ACE_HAS_IPV6, will give priority to IPV6 interfaces
80 
81 /// Helper function to get the vector of addresses which should
82 /// be advertised to peers
85 
86 /// Helper function to set the ttl on a socket appropriately
87 /// given whether it is IPV4 or IPV6
89 bool set_socket_multicast_ttl(const ACE_SOCK_Dgram& socket, const unsigned char& ttl);
90 
91 /// Helper function to create dual stack socket to support IPV4 and IPV6,
92 /// for IPV6 builds allows for setting IPV6_V6ONLY socket option to 0 before binding
93 /// Otherwise defaults to opening a socket based on the type of local_address
95 bool open_appropriate_socket_type(ACE_SOCK_Dgram& socket, const ACE_INET_Addr& local_address, int* proto_family = 0);
96 
98 ACE_INET_Addr choose_single_coherent_address(const OPENDDS_VECTOR(ACE_INET_Addr)& addrs, bool prefer_loopback = true, const String& name = String());
99 
100 extern OpenDDS_Dcps_Export
101 ACE_INET_Addr choose_single_coherent_address(const ACE_INET_Addr& addr, bool prefer_loopback = true);
102 
103 extern OpenDDS_Dcps_Export
104 ACE_INET_Addr choose_single_coherent_address(const String& hostname, bool prefer_loopback = true, bool allow_ipv4_fallback = true);
105 
106 inline void assign(DDS::OctetArray16& dest,
107  ACE_CDR::ULong ipv4addr_be)
108 {
109  std::memset(&dest[0], 0, 12);
110  dest[12] = ipv4addr_be >> 24;
111  dest[13] = ipv4addr_be >> 16;
112  dest[14] = ipv4addr_be >> 8;
113  dest[15] = ipv4addr_be;
114 }
115 
116 inline void
118 {
119  const void* raw = addr.get_addr();
120 #ifdef ACE_HAS_IPV6
121  if (addr.get_type() == AF_INET6) {
122  const sockaddr_in6* in = static_cast<const sockaddr_in6*>(raw);
123  std::memcpy(&dest[0], &in->sin6_addr, 16);
124  } else {
125 #else
126  {
127 #endif
128  const sockaddr_in* in = static_cast<const sockaddr_in*>(raw);
129  std::memset(&dest[0], 0, 12);
130  std::memcpy(&dest[12], &in->sin_addr, 4);
131  }
132 }
133 
134 // FUTURE: Remove the map parameter. Caller can deal with IPV6.
137  const Locator_t& locator,
138  bool map /*map IPV4 to IPV6 addr*/);
139 
141 void address_to_locator(Locator_t& locator,
142  const ACE_INET_Addr& dest);
143 
144 } // namespace DCPS
145 } // namespace OpenDDS
146 
148 
150 
151 /// Marshal into a buffer.
152 extern OpenDDS_Dcps_Export
155 
156 /// Demarshal from a buffer.
157 extern OpenDDS_Dcps_Export
160 
162 
163 #if defined (__ACE_INLINE__)
164 # include "NetworkResource.inl"
165 #endif /* __ACE_INLINE__ */
166 
167 #endif /* OPENDDS_DCPS_NETWORK_RESOURCE_H */
#define ACE_BEGIN_VERSIONED_NAMESPACE_DECL
void address_to_bytes(DDS::OctetArray16 &dest, const ACE_INET_Addr &addr)
const LogLevel::Value value
Definition: debug.cpp:61
std::string String
String addr_
The address in string format. e.g. ip:port, hostname:port.
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
int locator_to_address(ACE_INET_Addr &dest, const DCPS::Locator_t &locator, bool map)
int get_type(void) const
OpenDDS_Dcps_Export void address_to_locator(Locator_t &locator, const ACE_INET_Addr &addr)
bool verify_hostname(const String &hostname, ACE_INET_Addr *addr_array, size_t addr_count, bool prefer_loopback, bool allow_ipv4_fallback)
ACE_CDR::Boolean operator<<(Serializer &serializer, CoherentChangeControl &value)
Marshal/Insertion into a buffer.
bool set_socket_multicast_ttl(const ACE_SOCK_Dgram &socket, const unsigned char &ttl)
ACE_INET_Addr choose_single_coherent_address(const ACE_INET_Addr &address, bool prefer_loopback)
virtual void * get_addr(void) const
Defines a wrapper around address info which is used for advertise.
void get_interface_addrs(OPENDDS_VECTOR(ACE_INET_Addr)&addrs)
#define ACE_END_VERSIONED_NAMESPACE_DECL
ACE_UINT32 ULong
const char *const name
Definition: debug.cpp:60
bool open_appropriate_socket_type(ACE_SOCK_Dgram &socket, const ACE_INET_Addr &local_address, int *proto_family)
ACE_CDR::Octet Octet
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
String get_fully_qualified_hostname(ACE_INET_Addr *addr)
ACE_CDR::Boolean operator>>(Serializer &serializer, CoherentChangeControl &value)
void assign(EntityId_t &dest, const EntityId_t &src)
Definition: GuidUtils.h:157
typedef OPENDDS_VECTOR(ActionConnectionRecord) ConnectionRecords
bool Boolean
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
octet OctetArray16[16]