OpenDDS  Snapshot(2023/04/28-20:55)
EndpointManager.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_ENDPOINTMANAGER_H
10 #define OPENDDS_DCPS_RTPS_ICE_ENDPOINTMANAGER_H
11 
12 #if !defined (ACE_LACKS_PRAGMA_ONCE)
13 #pragma once
14 #endif /* ACE_LACKS_PRAGMA_ONCE */
15 
16 #include "dds/DCPS/Definitions.h"
18 #include <ace/INET_Addr.h>
19 
20 #include "Ice.h"
21 #include "Task.h"
22 #include "Checklist.h"
23 
25 
26 namespace OpenDDS {
27 namespace ICE {
28 
29 class AgentImpl;
30 
34  ACE_UINT32 priority;
37 
38  DeferredTriggeredCheck(const ACE_INET_Addr& a_local_address,
39  const ACE_INET_Addr& a_remote_address,
40  ACE_UINT32 a_priority,
41  bool a_use_candidate,
42  const DCPS::MonotonicTimePoint& a_expiration_date)
43  : local_address(a_local_address)
44  , remote_address(a_remote_address)
45  , priority(a_priority)
46  , use_candidate(a_use_candidate)
47  , expiration_date(a_expiration_date)
48  {}
49 };
50 
54 
55  EndpointManager(AgentImpl* a_agent_impl, DCPS::WeakRcHandle<Endpoint> a_endpoint);
56 
57  const AgentInfo& agent_info() const
58  {
59  return agent_info_;
60  }
61 
62  typedef std::set<std::string> FoundationSet;
63  const FoundationSet& foundations() const
64  {
65  return foundations_;
66  }
67 
68  void add_agent_info_listener(const DCPS::GUID_t& a_local_guid,
69  DCPS::WeakRcHandle<AgentInfoListener> a_agent_info_listener)
70  {
71  agent_info_listeners_[a_local_guid] = a_agent_info_listener;
72  }
73 
74  void remove_agent_info_listener(const DCPS::GUID_t& a_local_guid)
75  {
76  agent_info_listeners_.erase(a_local_guid);
77  }
78 
79  void start_ice(const DCPS::GUID_t& a_local_guid,
80  const DCPS::GUID_t& a_remote_guid,
81  const AgentInfo& a_remote_agent_info);
82 
83  void stop_ice(const DCPS::GUID_t& local_guid,
84  const DCPS::GUID_t& remote_guid);
85 
86  ACE_INET_Addr get_address(const DCPS::GUID_t& a_local_guid,
87  const DCPS::GUID_t& a_remote_guid) const;
88 
89  void receive(const ACE_INET_Addr& a_local_address,
90  const ACE_INET_Addr& a_remote_address,
91  const STUN::Message& a_message);
92 
93  void set_responsible_checklist(const STUN::TransactionId& a_transaction_id, ChecklistPtr a_checklist)
94  {
95  OPENDDS_ASSERT(!transaction_id_to_checklist_.count(a_transaction_id));
96  transaction_id_to_checklist_[a_transaction_id] = a_checklist;
97  }
98 
99  void unset_responsible_checklist(const STUN::TransactionId& a_transaction_id, ChecklistPtr a_checklist)
100  {
101  TransactionIdToChecklistType::iterator pos = transaction_id_to_checklist_.find(a_transaction_id);
102  OPENDDS_ASSERT(pos != transaction_id_to_checklist_.end());
103  OPENDDS_ASSERT(pos->second == a_checklist);
104  ACE_UNUSED_ARG(a_checklist);
105  transaction_id_to_checklist_.erase(pos);
106  }
107 
108  void set_responsible_checklist(const GuidPair& a_guid_pair, ChecklistPtr a_checklist)
109  {
110  OPENDDS_ASSERT(!guid_pair_to_checklist_.count(a_guid_pair));
111  guid_pair_to_checklist_[a_guid_pair] = a_checklist;
112  }
113 
114  void unset_responsible_checklist(const GuidPair& a_guid_pair, ChecklistPtr a_checklist)
115  {
116  GuidPairToChecklistType::iterator pos = guid_pair_to_checklist_.find(a_guid_pair);
117  OPENDDS_ASSERT(pos != guid_pair_to_checklist_.end());
118  OPENDDS_ASSERT(pos->second == a_checklist);
119  ACE_UNUSED_ARG(a_checklist);
120  guid_pair_to_checklist_.erase(pos);
121  }
122 
123  void set_responsible_checklist(const std::string& a_username, ChecklistPtr a_checklist)
124  {
125  OPENDDS_ASSERT(!username_to_checklist_.count(a_username));
126  username_to_checklist_[a_username] = a_checklist;
127  }
128 
129  void unset_responsible_checklist(const std::string& a_username, ChecklistPtr a_checklist)
130  {
131  UsernameToChecklistType::iterator pos = username_to_checklist_.find(a_username);
132  OPENDDS_ASSERT(pos != username_to_checklist_.end());
133  OPENDDS_ASSERT(pos->second == a_checklist);
134  ACE_UNUSED_ARG(a_checklist);
135  username_to_checklist_.erase(pos);
136  }
137 
138  void unfreeze();
139 
140  void unfreeze(const FoundationType& a_foundation);
141 
142  void compute_active_foundations(ActiveFoundationSet& a_active_foundations) const;
143 
144  void check_invariants() const;
145 
146  void ice_connect(const GuidSetType& guids, const ACE_INET_Addr& addr)
147  {
148  DCPS::RcHandle<Endpoint> e = endpoint.lock();
149  if (e) {
150  e->ice_connect(guids, addr);
151  }
152  }
153 
154  void ice_disconnect(const GuidSetType& guids, const ACE_INET_Addr& addr)
155  {
156  DCPS::RcHandle<Endpoint> e = endpoint.lock();
157  if (e) {
158  e->ice_disconnect(guids, addr);
159  }
160  }
161 
162  void network_change();
163 
164  void send(const ACE_INET_Addr& address, const STUN::Message& message);
165 
166  void purge();
167 
168 private:
169  AddressListType host_addresses_; // Cached list of host addresses.
170  ACE_INET_Addr server_reflexive_address_; // Server-reflexive address (from STUN server).
171  ACE_INET_Addr stun_server_address_; // Address of the STUN server.
174  FoundationSet foundations_;
175 
176  // State variables for getting and maintaining the server reflexive address.
178  size_t send_count_;
180  STUN::Message binding_request_; // Binding request sent to STUN server.
181 
182  typedef std::deque<DeferredTriggeredCheck> DeferredTriggeredCheckListType;
183  typedef std::map<std::string, DeferredTriggeredCheckListType> DeferredTriggeredChecksType;
184  DeferredTriggeredChecksType deferred_triggered_checks_;
185 
186  // Managed by checklists.
187  typedef std::map<std::string, ChecklistPtr> UsernameToChecklistType;
188  UsernameToChecklistType username_to_checklist_;
189 
190  // Managed by checklists.
191  typedef std::map<STUN::TransactionId, ChecklistPtr> TransactionIdToChecklistType;
192  TransactionIdToChecklistType transaction_id_to_checklist_;
193 
194  // Managed by checklists.
195  typedef std::map<GuidPair, ChecklistPtr> GuidPairToChecklistType;
196  GuidPairToChecklistType guid_pair_to_checklist_;
197 
198  typedef std::map<DCPS::GUID_t, DCPS::WeakRcHandle<AgentInfoListener>, DCPS::GUID_tKeyLessThan> AgentInfoListenersType;
199  AgentInfoListenersType agent_info_listeners_;
200 
201  void change_username();
202 
203  void change_password(bool password_only);
204 
205  void set_host_addresses(const AddressListType& a_host_addresses);
206 
207  void set_server_reflexive_address(const ACE_INET_Addr& a_server_reflexive_address,
208  const ACE_INET_Addr& a_stun_server_address);
209 
210  void regenerate_agent_info(bool password_only);
211 
212  void server_reflexive_task(const DCPS::MonotonicTimePoint & a_now);
213 
214  bool success_response(const STUN::Message& a_message);
215 
216  bool error_response(const STUN::Message& a_message);
217 
218  ChecklistPtr create_checklist(const AgentInfo& a_remote_agent_info);
219 
220  // STUN Message processing.
221  STUN::Message make_unknown_attributes_error_response(const STUN::Message& a_message,
222  const std::vector<STUN::AttributeType>& a_unknown_attributes);
223 
224  STUN::Message make_bad_request_error_response(const STUN::Message& a_message,
225  const std::string& a_reason);
226 
227  STUN::Message make_unauthorized_error_response(const STUN::Message& a_message);
228 
229  void request(const ACE_INET_Addr& a_local_address,
230  const ACE_INET_Addr& a_remote_address,
231  const STUN::Message& a_message);
232 
233  void indication(const ACE_INET_Addr& a_local_address,
234  const ACE_INET_Addr& a_remote_address,
235  const STUN::Message& a_message);
236 
237  void success_response(const ACE_INET_Addr& a_local_address,
238  const ACE_INET_Addr& a_remote_address,
239  const STUN::Message& a_message);
240 
241  void error_response(const ACE_INET_Addr& a_local_address,
242  const ACE_INET_Addr& a_remote_address,
243  const STUN::Message& a_message);
244 
245  struct ServerReflexiveTask : public Task {
247  explicit ServerReflexiveTask(DCPS::RcHandle<EndpointManager> a_endpoint_manager);
248  void execute(const DCPS::MonotonicTimePoint& a_now);
249  };
251 
252  struct ChangePasswordTask : public Task {
254  explicit ChangePasswordTask(DCPS::RcHandle<EndpointManager> a_endpoint_manager);
255  void execute(const DCPS::MonotonicTimePoint& a_now);
256  };
258 };
259 
261 
262 } // namespace ICE
263 } // namespace OpenDDS
264 
266 
267 #endif /* OPENDDS_RTPS_ICE_ENDPOINT_MANAGER_H */
268 #endif /* OPENDDS_SECURITY */
DCPS::RcHandle< EndpointManager > EndpointManagerPtr
std::set< std::string > FoundationSet
GuidPairToChecklistType guid_pair_to_checklist_
DCPS::RcHandle< ServerReflexiveTask > server_reflexive_task_
void ice_disconnect(const GuidSetType &guids, const ACE_INET_Addr &addr)
std::pair< std::string, std::string > FoundationType
Definition: Checklist.h:34
#define OPENDDS_ASSERT(C)
Definition: Definitions.h:72
DCPS::WeakRcHandle< Endpoint > const endpoint
TransactionIdToChecklistType transaction_id_to_checklist_
ssize_t send(ACE_HANDLE handle, const void *buf, size_t len, int flags, const ACE_Time_Value *timeout=0)
void ice_connect(const GuidSetType &guids, const ACE_INET_Addr &addr)
void set_responsible_checklist(const std::string &a_username, ChecklistPtr a_checklist)
void set_responsible_checklist(const GuidPair &a_guid_pair, ChecklistPtr a_checklist)
std::map< std::string, ChecklistPtr > UsernameToChecklistType
void remove_agent_info_listener(const DCPS::GUID_t &a_local_guid)
void unset_responsible_checklist(const std::string &a_username, ChecklistPtr a_checklist)
const AgentInfo & agent_info() const
DCPS::WeakRcHandle< EndpointManager > endpoint_manager
AgentInfoListenersType agent_info_listeners_
DCPS::RcHandle< ChangePasswordTask > change_password_task_
std::map< GuidPair, ChecklistPtr > GuidPairToChecklistType
unsigned long long ACE_UINT64
DeferredTriggeredChecksType deferred_triggered_checks_
void add_agent_info_listener(const DCPS::GUID_t &a_local_guid, DCPS::WeakRcHandle< AgentInfoListener > a_agent_info_listener)
DeferredTriggeredCheck(const ACE_INET_Addr &a_local_address, const ACE_INET_Addr &a_remote_address, ACE_UINT32 a_priority, bool a_use_candidate, const DCPS::MonotonicTimePoint &a_expiration_date)
UsernameToChecklistType username_to_checklist_
DCPS::WeakRcHandle< EndpointManager > endpoint_manager
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
void unset_responsible_checklist(const GuidPair &a_guid_pair, ChecklistPtr a_checklist)
const FoundationSet & foundations() const
DCPS::MonotonicTimePoint expiration_date
RcHandle< T > lock() const
Definition: RcObject.h:188
std::map< std::string, DeferredTriggeredCheckListType > DeferredTriggeredChecksType
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
void unset_responsible_checklist(const STUN::TransactionId &a_transaction_id, ChecklistPtr a_checklist)
std::deque< DeferredTriggeredCheck > DeferredTriggeredCheckListType
std::map< DCPS::GUID_t, DCPS::WeakRcHandle< AgentInfoListener >, DCPS::GUID_tKeyLessThan > AgentInfoListenersType
void set_responsible_checklist(const STUN::TransactionId &a_transaction_id, ChecklistPtr a_checklist)
std::map< STUN::TransactionId, ChecklistPtr > TransactionIdToChecklistType
std::set< GuidPair > GuidSetType
Definition: RTPS/ICE/Ice.h:39