OpenDDS  Snapshot(2023/04/28-20:55)
Checklist.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_CHECKLIST_H
10 #define OPENDDS_DCPS_RTPS_ICE_CHECKLIST_H
11 
12 #if !defined (ACE_LACKS_PRAGMA_ONCE)
13 #pragma once
14 #endif /* ACE_LACKS_PRAGMA_ONCE */
15 
16 #include "Ice.h"
17 #include "Task.h"
18 
19 #include "dds/DCPS/TimeTypes.h"
20 #include "dds/DCPS/GuidUtils.h"
21 
22 #if !OPENDDS_SAFETY_PROFILE
23 #include "dds/DCPS/GuidUtils.h"
24 #include <iostream>
25 #endif
26 
27 #include <map>
28 
30 
31 namespace OpenDDS {
32 namespace ICE {
33 
34 typedef std::pair<std::string, std::string> FoundationType;
35 
37 public:
38  void add(const FoundationType& a_foundation)
39  {
40  std::pair<FoundationsType::iterator, bool> x = foundations_.insert(std::make_pair(a_foundation, 0));
41  ++x.first->second;
42  }
43 
44  bool remove(const FoundationType& a_foundation)
45  {
46  FoundationsType::iterator pos = foundations_.find(a_foundation);
47  OPENDDS_ASSERT(pos != foundations_.end());
48  --pos->second;
49 
50  if (pos->second == 0) {
51  foundations_.erase(pos);
52  return true;
53  }
54 
55  return false;
56  }
57 
58  bool contains(const FoundationType& a_foundation) const
59  {
60  return foundations_.find(a_foundation) != foundations_.end();
61  }
62 
63  bool operator==(const ActiveFoundationSet& a_other) const
64  {
65  return foundations_ == a_other.foundations_;
66  }
67 
68 private:
69  typedef std::map<FoundationType, size_t> FoundationsType;
70  FoundationsType foundations_;
71 };
72 
73 struct CandidatePair {
76  FoundationType const foundation;
79  // 1) set the use_candidate when local is controlling and
80  // 2) nominate when the response is successful (controlling and controlled)
81  bool const use_candidate;
82 
83  CandidatePair(const Candidate& a_local,
84  const Candidate& a_remote,
85  bool a_local_is_controlling,
86  bool a_use_candidate = false);
87 
88  bool operator==(const CandidatePair& a_other) const;
89 
90  static bool priority_sorted(const CandidatePair& x, const CandidatePair& y)
91  {
92  return x.priority > y.priority;
93  }
94 
95 private:
96  ACE_UINT64 compute_priority();
97 };
98 
100  ConnectivityCheck(const CandidatePair& a_candidate_pair,
101  const AgentInfo& a_local_agent_info, const AgentInfo& a_remote_agent_info,
102  ACE_UINT64 a_ice_tie_breaker, const DCPS::MonotonicTimePoint& a_expiration_date);
103 
105  {
106  return candidate_pair_;
107  }
108  const STUN::Message& request() const
109  {
110  return request_;
111  }
112  void cancel()
113  {
114  cancelled_ = true;
115  }
116  bool cancelled() const
117  {
118  return cancelled_;
119  }
121  {
122  return expiration_date_;
123  }
124  void password(const std::string& a_password)
125  {
126  request_.password = a_password;
127  }
128 private:
133 };
134 
135 inline bool operator==(const ConnectivityCheck& a_cc, const STUN::TransactionId& a_tid)
136 {
137  return a_cc.request().transaction_id == a_tid;
138 }
139 
140 inline bool operator==(const ConnectivityCheck& a_cc, const CandidatePair& a_cp)
141 {
142  return a_cc.candidate_pair() == a_cp;
143 }
144 
145 #if !OPENDDS_SAFETY_PROFILE
146 inline std::ostream& operator<<(std::ostream& stream, const GuidPair& guidp)
147 {
148  stream << guidp.local << ':' << guidp.remote;
149  return stream;
150 }
151 #endif
152 
153 struct EndpointManager;
154 
155 struct Checklist : public Task {
156  Checklist(EndpointManager* a_endpoint,
157  const AgentInfo& a_local, const AgentInfo& a_remote,
158  ACE_UINT64 a_ice_tie_breaker);
159 
160  void compute_active_foundations(ActiveFoundationSet& a_active_foundations) const;
161 
162  void check_invariants() const;
163 
164  bool has_transaction_id(const STUN::TransactionId& a_tid) const
165  {
166  return std::find(connectivity_checks_.begin(), connectivity_checks_.end(), a_tid) != connectivity_checks_.end();
167  }
168 
169  bool has_guid_pair(const GuidPair& a_guid_pair) const
170  {
171  return guids_.find(a_guid_pair) != guids_.end();
172  }
173 
174  void unfreeze();
175 
176  void unfreeze(const FoundationType& a_foundation);
177 
178  void generate_triggered_check(const ACE_INET_Addr& a_local_address, const ACE_INET_Addr& a_remote_address,
179  ACE_UINT32 a_priority,
180  bool a_use_candidate);
181 
182  void success_response(const ACE_INET_Addr& a_local_address,
183  const ACE_INET_Addr& a_remote_address,
184  const STUN::Message& a_message);
185 
186  void error_response(const ACE_INET_Addr& a_local_address,
187  const ACE_INET_Addr& a_remote_address,
188  const STUN::Message& a_message);
189 
190  void add_guid(const GuidPair& a_guid_pair);
191 
192  void remove_guid(const GuidPair& a_guid_pair);
193 
194  void add_guids(const GuidSetType& a_guids);
195 
196  void remove_guids();
197 
199  {
200  return guids_;
201  }
202 
203  ACE_INET_Addr selected_address() const;
204 
206  {
207  return original_remote_agent_info_;
208  }
209 
210  void set_remote_password(const std::string& a_password)
211  {
212  remote_agent_info_.password = a_password;
213  original_remote_agent_info_.password = a_password;
214  }
215 
216  void indication();
217 
218 private:
226 
227  typedef std::list<CandidatePair> CandidatePairsType;
228  CandidatePairsType frozen_;
229  CandidatePairsType waiting_;
230  CandidatePairsType in_progress_;
231  CandidatePairsType succeeded_;
232  CandidatePairsType failed_;
233  // The triggered check queue is a subset of waiting.
234  CandidatePairsType triggered_check_queue_;
235  // The valid list is a subset of succeeded.
236  CandidatePairsType valid_list_;
237  // These are iterators into valid_list_.
238  CandidatePairsType::const_iterator nominating_;
239  CandidatePairsType::const_iterator nominated_;
244  typedef std::list<ConnectivityCheck> ConnectivityChecksType;
245  ConnectivityChecksType connectivity_checks_;
246 
247  ~Checklist();
248 
249  void generate_candidate_pairs();
250 
251  void fix_foundations();
252 
253  void add_valid_pair(const CandidatePair& a_valid_pair);
254 
255  bool get_local_candidate(const ACE_INET_Addr& a_address, Candidate& a_candidate);
256 
257  bool get_remote_candidate(const ACE_INET_Addr& a_address, Candidate& a_candidate);
258 
259  bool is_succeeded(const CandidatePair& a_candidate_pair) const
260  {
261  return std::find(succeeded_.begin(), succeeded_.end(), a_candidate_pair) != succeeded_.end();
262  }
263 
264  bool is_in_progress(const CandidatePair& a_candidate_pair) const
265  {
266  return std::find(in_progress_.begin(), in_progress_.end(), a_candidate_pair) != in_progress_.end();
267  }
268 
269  void add_triggered_check(const CandidatePair& a_candidate_pair);
270 
271  void remove_from_in_progress(const CandidatePair& a_candidate_pair);
272 
273  void succeeded(const ConnectivityCheck& a_connectivity_check);
274 
275  void failed(const ConnectivityCheck& a_connectivity_check);
276 
277  // Measure of now many checks are left.
278  size_t size() const
279  {
280  return frozen_.size() + waiting_.size() + in_progress_.size();
281  }
282 
283  void do_next_check(const DCPS::MonotonicTimePoint& a_now);
284 
285  void execute(const DCPS::MonotonicTimePoint& a_now);
286 };
287 
289 
290 } // namespace ICE
291 } // namespace OpenDDS
292 
294 
295 #endif /* OPENDDS_RTPS_ICE_CHECKLIST_H */
296 #endif /* OPENDDS_SECURITY */
Candidate const local
Definition: Checklist.h:74
DCPS::TimeDuration check_interval_
Definition: Checklist.h:242
bool operator==(const ActiveFoundationSet &a_other) const
Definition: Checklist.h:63
void password(const std::string &a_password)
Definition: Checklist.h:124
AgentInfo remote_agent_info_
Definition: Checklist.h:222
const CandidatePair & candidate_pair() const
Definition: Checklist.h:104
const STUN::Message & request() const
Definition: Checklist.h:108
bool is_succeeded(const CandidatePair &a_candidate_pair) const
Definition: Checklist.h:259
std::pair< std::string, std::string > FoundationType
Definition: Checklist.h:34
#define OPENDDS_ASSERT(C)
Definition: Definitions.h:72
DCPS::RcHandle< Checklist > ChecklistPtr
Definition: Checklist.h:288
GuidSetType guids() const
Definition: Checklist.h:198
CandidatePairsType in_progress_
Definition: Checklist.h:230
void add(const FoundationType &a_foundation)
Definition: Checklist.h:38
size_t size() const
Definition: Checklist.h:278
CandidatePairsType waiting_
Definition: Checklist.h:229
Candidate const remote
Definition: Checklist.h:75
const AgentInfo & original_remote_agent_info() const
Definition: Checklist.h:205
CandidatePairsType frozen_
Definition: Checklist.h:228
bool const local_is_controlling_
Definition: Checklist.h:224
DCPS::MonotonicTimePoint last_indication_
Definition: Checklist.h:241
bool contains(const FoundationType &a_foundation) const
Definition: Checklist.h:58
CandidatePairsType succeeded_
Definition: Checklist.h:231
std::list< CandidatePair > CandidatePairsType
Definition: Checklist.h:227
ConnectivityChecksType connectivity_checks_
Definition: Checklist.h:245
CandidatePairsType::const_iterator nominated_
Definition: Checklist.h:239
bool const local_is_controlling
Definition: Checklist.h:77
DCPS::TimeDuration max_check_interval_
Definition: Checklist.h:243
bool has_guid_pair(const GuidPair &a_guid_pair) const
Definition: Checklist.h:169
static bool priority_sorted(const CandidatePair &x, const CandidatePair &y)
Definition: Checklist.h:90
unsigned long long ACE_UINT64
bool has_transaction_id(const STUN::TransactionId &a_tid) const
Definition: Checklist.h:164
FoundationType const foundation
Definition: Checklist.h:76
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
CandidatePairsType::const_iterator nominating_
Definition: Checklist.h:238
std::list< ConnectivityCheck > ConnectivityChecksType
Definition: Checklist.h:244
EndpointManager *const endpoint_manager_
Definition: Checklist.h:219
AgentInfo local_agent_info_
Definition: Checklist.h:221
ACE_UINT64 const ice_tie_breaker_
Definition: Checklist.h:225
CandidatePairsType valid_list_
Definition: Checklist.h:236
DCPS::MonotonicTimePoint expiration_date_
Definition: Checklist.h:132
AgentInfo original_remote_agent_info_
Definition: Checklist.h:223
bool is_in_progress(const CandidatePair &a_candidate_pair) const
Definition: Checklist.h:264
std::map< FoundationType, size_t > FoundationsType
Definition: Checklist.h:69
TransactionId transaction_id
Definition: Stun.h:186
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
std::ostream & operator<<(std::ostream &stream, const GuidPair &guidp)
Definition: Checklist.h:146
CandidatePairsType failed_
Definition: Checklist.h:232
ACE_UINT64 const priority
Definition: Checklist.h:78
void set_remote_password(const std::string &a_password)
Definition: Checklist.h:210
DCPS::MonotonicTimePoint expiration_date() const
Definition: Checklist.h:120
std::set< GuidPair > GuidSetType
Definition: RTPS/ICE/Ice.h:39
CandidatePairsType triggered_check_queue_
Definition: Checklist.h:234