OpenDDS  Snapshot(2023/01/24-11:31)
RTPS/ICE/Ice.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 #ifdef OPENDDS_SECURITY
9 #ifndef OPENDDS_DCPS_RTPS_ICE_ICE_H
10 #define OPENDDS_DCPS_RTPS_ICE_ICE_H
11 
12 #include "dds/DCPS/Ice.h"
13 #include "Stun.h"
14 #include "dds/DCPS/TimeTypes.h"
15 #include "dds/DCPS/RcObject.h"
16 
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
18 #pragma once
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
20 
22 
23 namespace OpenDDS {
24 namespace ICE {
25 
26 typedef OPENDDS_VECTOR(ACE_INET_Addr) AddressListType;
27 
28 bool candidates_equal(const Candidate& x, const Candidate& y);
29 bool candidates_sorted(const Candidate& x, const Candidate& y);
30 
31 ACE_UINT32 local_priority(const ACE_INET_Addr& addr);
32 
33 Candidate make_host_candidate(const ACE_INET_Addr& address);
34 Candidate make_server_reflexive_candidate(const ACE_INET_Addr& address, const ACE_INET_Addr& base, const ACE_INET_Addr& server_address);
35 Candidate make_peer_reflexive_candidate(const ACE_INET_Addr& address, const ACE_INET_Addr& base, const ACE_INET_Addr& server_address, ACE_UINT32 priority);
36 Candidate make_peer_reflexive_candidate(const ACE_INET_Addr& address, ACE_UINT32 priority, size_t q);
37 
41 
42  GuidPair(const DCPS::GUID_t& a_local, const DCPS::GUID_t& a_remote) : local(a_local), remote(a_remote) {}
43 
44  bool operator<(const GuidPair& a_other) const
45  {
46  if (DCPS::GUID_tKeyLessThan()(this->local, a_other.local)) return true;
47 
48  if (DCPS::GUID_tKeyLessThan()(a_other.local, this->local)) return false;
49 
50  if (DCPS::GUID_tKeyLessThan()(this->remote, a_other.remote)) return true;
51 
52  if (DCPS::GUID_tKeyLessThan()(a_other.remote, this->remote)) return false;
53 
54  return false;
55  }
56 };
57 
58 typedef std::set<GuidPair> GuidSetType;
59 
61 public:
62  virtual ~Endpoint() {}
63  virtual AddressListType host_addresses() const = 0;
64  virtual void send(const ACE_INET_Addr& address, const STUN::Message& message) = 0;
65  virtual ACE_INET_Addr stun_server_address() const = 0;
66  virtual void ice_connect(const GuidSetType&, const ACE_INET_Addr&) {}
67  virtual void ice_disconnect(const GuidSetType&, const ACE_INET_Addr&) {}
68 };
69 
71 public:
72  virtual ~AgentInfoListener() {}
73  virtual void update_agent_info(const DCPS::GUID_t& a_local_guid,
74  const AgentInfo& a_agent_info) = 0;
75  virtual void remove_agent_info(const DCPS::GUID_t& a_local_guid) = 0;
76 };
77 
79 public:
81  T_a_(0, 50000),
82  connectivity_check_ttl_(5 * 60),
83  checklist_period_(10),
84  indication_period_(15),
85  nominated_ttl_(5 * 60),
86  server_reflexive_address_period_(30),
87  server_reflexive_indication_count_(10),
88  deferred_triggered_check_ttl_(5 * 60),
89  change_password_period_(5 * 60)
90  {}
91 
92  void T_a(const DCPS::TimeDuration& x)
93  {
94  T_a_ = x;
95  }
97  {
98  return T_a_;
99  }
100 
102  {
103  connectivity_check_ttl_ = x;
104  }
106  {
107  return connectivity_check_ttl_;
108  }
109 
111  {
112  checklist_period_ = x;
113  }
115  {
116  return checklist_period_;
117  }
118 
120  {
121  indication_period_ = x;
122  }
124  {
125  return indication_period_;
126  }
127 
129  {
130  nominated_ttl_ = x;
131  }
133  {
134  return nominated_ttl_;
135  }
136 
138  {
139  server_reflexive_address_period_ = x;
140  }
142  {
143  return server_reflexive_address_period_;
144  }
145 
147  {
148  server_reflexive_indication_count_ = x;
149  }
151  {
152  return server_reflexive_indication_count_;
153  }
154 
156  {
157  deferred_triggered_check_ttl_ = x;
158  }
160  {
161  return deferred_triggered_check_ttl_;
162  }
163 
165  {
166  change_password_period_ = x;
167  }
169  {
170  return change_password_period_;
171  }
172 
173  static Configuration* instance();
174 
175 private:
176  // Mininum time between consecutive sends.
177  // RFC 8445 Section 14.2
179  // Repeat a check for this long before failing it.
181  // Run all of the ordinary checks in a checklist in this amount of time.
183  // Send an indication at this interval once an address is selected.
185  // The nominated pair will still be valid if an indication has been received within this amount of time.
187  // Perform server-reflexive candidate gathering this often.
189  // Send this many binding indications to the STUN server before sending a binding request.
191  // Lifetime of a deferred triggered check.
193  // Change the password this often.
195 };
196 
197 class OpenDDS_Rtps_Export Agent : public virtual DCPS::RcObject {
198 public:
199  virtual ~Agent() {}
200  virtual void add_endpoint(DCPS::WeakRcHandle<Endpoint> a_endpoint) = 0;
201  virtual void remove_endpoint(DCPS::WeakRcHandle<Endpoint> a_endpoint) = 0;
202  virtual AgentInfo get_local_agent_info(DCPS::WeakRcHandle<Endpoint> a_endpoint) const = 0;
203  virtual void add_local_agent_info_listener(DCPS::WeakRcHandle<Endpoint> a_endpoint,
204  const DCPS::GUID_t& a_local_guid,
205  DCPS::WeakRcHandle<AgentInfoListener> a_agent_info_listener) = 0;
206  virtual void remove_local_agent_info_listener(DCPS::WeakRcHandle<Endpoint> a_endpoint,
207  const DCPS::GUID_t& a_local_guid) = 0;
208  virtual void start_ice(DCPS::WeakRcHandle<Endpoint> a_endpoint,
209  const DCPS::GUID_t& a_local_guid,
210  const DCPS::GUID_t& a_remote_guid,
211  const AgentInfo& a_remote_agent_info) = 0;
212  virtual void stop_ice(DCPS::WeakRcHandle<Endpoint> a_endpoint,
213  const DCPS::GUID_t& a_local_guid,
214  const DCPS::GUID_t& a_remote_guid) = 0;
215  virtual ACE_INET_Addr get_address(DCPS::WeakRcHandle<Endpoint> a_endpoint,
216  const DCPS::GUID_t& a_local_guid,
217  const DCPS::GUID_t& a_remote_guid) const = 0;
218 
219  // Receive a STUN message.
220  virtual void receive(DCPS::WeakRcHandle<Endpoint> a_endpoint,
221  const ACE_INET_Addr& a_local_address,
222  const ACE_INET_Addr& a_remote_address,
223  const STUN::Message& a_message) = 0;
224 
225  virtual void shutdown() = 0;
226 
227  static DCPS::RcHandle<Agent> instance();
228 };
229 
231 public:
232  enum StateChange {
236  SRSM_Change
237  };
238 
240  : message_class_(STUN::REQUEST)
241  , send_count_(0)
242  , latency_available_(false)
243  {}
244 
245  // Return Unset if transitioning from a determined SRA to an undetermined SRA.
246  // Return None otherwise.
247  StateChange send(const ACE_INET_Addr& address,
248  size_t indication_count_limit,
249  const DCPS::GuidPrefix_t& guid_prefix);
250 
251  // Return Set if transitioning from an undetermined SRA to a determined SRA.
252  // Return Change if transitioning from a determined SRA to a different SRA.
253  // Return None otherwise.
254  StateChange receive(const STUN::Message& message);
255 
256  const STUN::Message& message() const { return message_; }
257  const ACE_INET_Addr& unset_stun_server_address() const { return unset_stun_server_address_; }
258  const ACE_INET_Addr& stun_server_address() const { return stun_server_address_; }
259 
260  bool is_response(const STUN::Message& message) const
261  {
262  return message.transaction_id == message_.transaction_id;
263  }
264 
266  {
267  return latency_;
268  }
269 
270  bool latency_available() const
271  {
272  return latency_available_;
273  }
274 
275  void latency_available(bool flag)
276  {
277  latency_available_ = flag;
278  }
279 
280  bool connected() const
281  {
282  return server_reflexive_address_ != ACE_INET_Addr();
283  }
284 
285 private:
286  StateChange start(const ACE_INET_Addr& address, size_t indication_count_limit, const DCPS::GuidPrefix_t& guid_prefix);
287  StateChange stop();
288  StateChange next_send(size_t indication_count_limit, const DCPS::GuidPrefix_t& guid_prefix);
289  StateChange success_response(const STUN::Message& message);
290  StateChange error_response(const STUN::Message& message);
291 
297  size_t send_count_;
301  };
302 
303 } // namespace ICE
304 } // namespace OpenDDS
305 
307 
308 #endif /* OPENDDS_RTPS_ICE_H */
309 #endif /* OPENDDS_SECURITY */
virtual void ice_disconnect(const GuidSetType &, const ACE_INET_Addr &)
Definition: RTPS/ICE/Ice.h:67
const STUN::Message & message() const
Definition: RTPS/ICE/Ice.h:256
DCPS::TimeDuration nominated_ttl() const
Definition: RTPS/ICE/Ice.h:132
void server_reflexive_indication_count(size_t x)
Definition: RTPS/ICE/Ice.h:146
void deferred_triggered_check_ttl(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:155
DCPS::TimeDuration T_a_
Definition: RTPS/ICE/Ice.h:178
Candidate make_server_reflexive_candidate(const ACE_INET_Addr &address, const ACE_INET_Addr &base, const ACE_INET_Addr &server_address)
Definition: Ice.cpp:76
size_t server_reflexive_indication_count() const
Definition: RTPS/ICE/Ice.h:150
DCPS::TimeDuration checklist_period() const
Definition: RTPS/ICE/Ice.h:114
void connectivity_check_ttl(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:101
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
const ACE_INET_Addr & stun_server_address() const
Definition: RTPS/ICE/Ice.h:258
DCPS::TimeDuration T_a() const
Definition: RTPS/ICE/Ice.h:96
ssize_t send(ACE_HANDLE handle, const void *buf, size_t len, int flags, const ACE_Time_Value *timeout=0)
DCPS::TimeDuration indication_period_
Definition: RTPS/ICE/Ice.h:184
DCPS::TimeDuration server_reflexive_address_period() const
Definition: RTPS/ICE/Ice.h:141
octet GuidPrefix_t[12]
Definition: DdsDcpsGuid.idl:19
GuidPair(const DCPS::GUID_t &a_local, const DCPS::GUID_t &a_remote)
Definition: RTPS/ICE/Ice.h:42
DCPS::TimeDuration server_reflexive_address_period_
Definition: RTPS/ICE/Ice.h:188
DCPS::TimeDuration checklist_period_
Definition: RTPS/ICE/Ice.h:182
#define OpenDDS_Rtps_Export
Definition: rtps_export.h:23
void nominated_ttl(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:128
DCPS::TimeDuration indication_period() const
Definition: RTPS/ICE/Ice.h:123
ACE_UINT32 local_priority(const ACE_INET_Addr &addr)
Definition: Ice.cpp:56
typedef OPENDDS_VECTOR(ACE_INET_Addr) AddressListType
TransactionId transaction_id
Definition: Stun.h:186
void server_reflexive_address_period(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:137
DCPS::TimeDuration latency() const
Definition: RTPS/ICE/Ice.h:265
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
Candidate make_peer_reflexive_candidate(const ACE_INET_Addr &address, const ACE_INET_Addr &base, const ACE_INET_Addr &server_address, ACE_UINT32 priority)
Definition: Ice.cpp:88
DCPS::TimeDuration deferred_triggered_check_ttl() const
Definition: RTPS/ICE/Ice.h:159
void change_password_period(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:164
virtual void ice_connect(const GuidSetType &, const ACE_INET_Addr &)
Definition: RTPS/ICE/Ice.h:66
const ACE_INET_Addr & unset_stun_server_address() const
Definition: RTPS/ICE/Ice.h:257
bool candidates_sorted(const Candidate &x, const Candidate &y)
Definition: Ice.cpp:21
std::set< GuidPair > GuidSetType
Definition: RTPS/ICE/Ice.h:58
DCPS::TimeDuration deferred_triggered_check_ttl_
Definition: RTPS/ICE/Ice.h:192
DCPS::TimeDuration change_password_period_
Definition: RTPS/ICE/Ice.h:194
DCPS::TimeDuration nominated_ttl_
Definition: RTPS/ICE/Ice.h:186
bool is_response(const STUN::Message &message) const
Definition: RTPS/ICE/Ice.h:260
void indication_period(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:119
bool operator<(const GuidPair &a_other) const
Definition: RTPS/ICE/Ice.h:44
Candidate make_host_candidate(const ACE_INET_Addr &address)
Definition: Ice.cpp:64
void checklist_period(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:110
int shutdown(ACE_HANDLE handle, int how)
DCPS::TimeDuration connectivity_check_ttl() const
Definition: RTPS/ICE/Ice.h:105
DCPS::TimeDuration connectivity_check_ttl_
Definition: RTPS/ICE/Ice.h:180
void T_a(const DCPS::TimeDuration &x)
Definition: RTPS/ICE/Ice.h:92
bool candidates_equal(const Candidate &x, const Candidate &y)
Definition: Ice.cpp:34
DCPS::TimeDuration change_password_period() const
Definition: RTPS/ICE/Ice.h:168