OpenDDS  Snapshot(2023/04/28-20:55)
Stun.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_STUN_H
10 #define OPENDDS_DCPS_RTPS_ICE_STUN_H
11 
12 #include "ace/INET_Addr.h"
13 #include "dds/DCPS/Serializer.h"
14 #include "dds/DCPS/GuidUtils.h"
15 
17 
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #pragma once
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 
22 #include <cstring>
23 #include <map>
24 #include <string>
25 #include <vector>
26 
28 
29 namespace OpenDDS {
30 namespace STUN {
31 
32 enum Class {
33  REQUEST = 0,
37 };
38 
39 enum Method {
40  BINDING = 0x001
41 };
42 
43 enum Family {
44  IPv4 = 0x01,
45  IPv6 = 0x02
46 };
47 
48 const ACE_UINT32 MAGIC_COOKIE = 0x2112A442;
49 const size_t HEADER_SIZE = 20;
50 
51 const ACE_UINT16 BAD_REQUEST = 400;
52 const ACE_UINT16 UNAUTHORIZED = 401;
53 const ACE_UINT16 UNKNOWN_ATTRIBUTE = 420;
54 
56  MAPPED_ADDRESS = 0x0001,
57  USERNAME = 0x0006,
59  ERROR_CODE = 0x0009,
61  // REALM = 0x0014,
62  // NONCE = 0x0015,
64  PRIORITY = 0x0024,
65  USE_CANDIDATE = 0x0025,
66 
67  // SOFTWARE = 0x8022,
68  // ALTERNATE_SERVER = 0x8023,
69  FINGERPRINT = 0x8028,
70  ICE_CONTROLLED = 0x8029,
71  ICE_CONTROLLING = 0x802A,
72 
73  GUID_PREFIX = 0xD000,
74 
75  LAST_ATTRIBUTE = 0xFFFF
76 };
77 
79  Attribute() : type(LAST_ATTRIBUTE), ice_tie_breaker(0), unknown_length(0) {}
80 
82 
83  ACE_INET_Addr mapped_address; // MAPPED_ADDRESS, XOR_MAPPED_ADDRESS
84  std::string username; // USERNAME
85  union {
86  unsigned char message_integrity[20]; // MESSAGE_INTEGRITY
87  ACE_UINT32 fingerprint; // FINGERPRINT
88  ACE_UINT32 priority; // PRIORITY
89  ACE_UINT64 ice_tie_breaker; // ICE_CONTROLLED, ICE_CONTROLLING
90  unsigned char guid_prefix[sizeof(DCPS::GuidPrefix_t)]; // GUID_PREFIX
91  };
92  struct Err {
93  Err() : code (0) {}
94  ACE_UINT16 code;
95  std::string reason;
96  } error;
97  std::vector<AttributeType> unknown_attributes;
98 
99  ACE_UINT16 unknown_length;
100 
101  ACE_UINT16 length() const;
102 };
103 
106 
108 Attribute make_username(const std::string& username);
109 
112 
114 Attribute make_error_code(ACE_UINT16 code, const std::string& reason);
115 
117 Attribute make_unknown_attributes(const std::vector<AttributeType>& unknown_attributes);
118 
121 
123 Attribute make_unknown_attribute(ACE_UINT16 type, ACE_UINT16 length);
124 
126 Attribute make_priority(ACE_UINT32 priority);
127 
130 
133 
135 Attribute make_ice_controlling(ACE_UINT64 ice_tie_breaker);
136 
138 Attribute make_ice_controlled(ACE_UINT64 ice_tie_breaker);
139 
141 Attribute make_guid_prefix(const DCPS::GuidPrefix_t& guid_prefix);
142 
144  ACE_UINT8 data[12];
146  {
147  std::memset(data, 0, sizeof data);
148  }
149  bool operator<(const TransactionId& other) const;
150  bool operator==(const TransactionId& other) const;
151  bool operator!=(const TransactionId& other) const;
152 };
153 
157 
159  : attribute(a)
160  , tid(t)
161  {}
162 };
163 
167 
169  : attribute(a)
170  , tid(t)
171  {}
172 };
173 
175 bool operator>>(DCPS::Serializer& serializer, AttributeHolder& holder);
176 
178 bool operator<<(DCPS::Serializer& serializer, ConstAttributeHolder& holder);
179 
181  typedef std::vector<Attribute> AttributesType;
182  typedef AttributesType::const_iterator const_iterator;
183 
187 
189  : class_(REQUEST), method(BINDING), block(0), length_(0), length_for_message_integrity_(0) {}
190 
191  void generate_transaction_id();
192 
193  void clear_transaction_id();
194 
195  void append_attribute(const Attribute& attribute)
196  {
197  attributes_.push_back(attribute);
198  length_ += (4 + attribute.length() + 3) & ~3;
199 
200  if (attribute.type == MESSAGE_INTEGRITY) {
201  length_for_message_integrity_ = length_;
202  }
203  }
204 
205  const_iterator begin() const
206  {
207  return attributes_.begin();
208  }
209  const_iterator end() const
210  {
211  return attributes_.end();
212  }
213  ACE_UINT16 length() const
214  {
215  return length_;
216  }
217  ACE_UINT16 length_for_message_integrity() const
218  {
219  return length_for_message_integrity_;
220  }
221 
222  std::vector<AttributeType> unknown_comprehension_required_attributes() const;
223  bool get_mapped_address(ACE_INET_Addr& address) const;
224  bool get_priority(ACE_UINT32& priority) const;
225  bool get_username(std::string& username) const;
226  bool has_message_integrity() const;
227  bool verify_message_integrity(const std::string& password) const;
228  void compute_message_integrity(const std::string& password, unsigned char message_integrity[20]) const;
229  bool has_error_code() const;
230  ACE_UINT16 get_error_code() const;
231  std::string get_error_reason() const;
232  bool has_unknown_attributes() const;
233  std::vector<AttributeType> get_unknown_attributes() const;
234  bool has_fingerprint() const;
235  ACE_UINT32 compute_fingerprint() const;
236  bool has_ice_controlled() const;
237  bool has_ice_controlling() const;
238  bool has_use_candidate() const;
239  bool get_guid_prefix(DCPS::GuidPrefix_t& guid_pefix) const;
240 
242  std::string password; // For integrity hashing.
243 
244 private:
245  AttributesType attributes_;
246  ACE_UINT16 length_;
248 };
249 
250 OpenDDS_Rtps_Export bool operator>>(DCPS::Serializer& serializer, Message& message);
251 OpenDDS_Rtps_Export bool operator<<(DCPS::Serializer& serializer, const Message& message);
252 
253 class Sender {
254 public:
255  virtual void send(const ACE_INET_Addr& address, const Message& message) = 0;
256  virtual ~Sender() {}
257 };
258 
260 public:
261  Participant(Sender* a_sender) : sender_(a_sender) {}
262 
263  void receive(const ACE_INET_Addr& address, const Message& message);
264 
265 private:
267 
268  void request(const ACE_INET_Addr& address, const Message& message);
269  void indication(const ACE_INET_Addr& /*address*/, const Message& message);
270  void success_response(const ACE_INET_Addr& /*address*/, const Message& /*message*/);
271  void error_response(const ACE_INET_Addr& /*address*/, const Message& /*message*/);
272 };
273 
275 
276 } // namespace STUN
277 } // namespace OpenDDS
278 
280 
281 #endif /* OPENDDS_RTPS_STUN_H */
282 #endif /* OPENDDS_SECURITY */
std::vector< AttributeType > unknown_attributes
Definition: Stun.h:97
OpenDDS_Rtps_Export Attribute make_guid_prefix(const DCPS::GuidPrefix_t &guid_prefix)
Definition: Stun.cpp:163
const ACE_UINT32 MAGIC_COOKIE
Definition: Stun.h:48
Participant(Sender *a_sender)
Definition: Stun.h:261
ConstAttributeHolder(const Attribute &a, const TransactionId &t)
Definition: Stun.h:168
const_iterator begin() const
Definition: Stun.h:205
Attribute make_ice_controlled(ACE_UINT64 ice_tie_breaker)
Definition: Stun.cpp:154
Attribute make_username(const std::string &username)
Definition: Stun.cpp:84
const ACE_UINT16 UNAUTHORIZED
Definition: Stun.h:52
ssize_t send(ACE_HANDLE handle, const void *buf, size_t len, int flags, const ACE_Time_Value *timeout=0)
Christopher Diggins *renamed files *fixing compilation errors *adding Visual C project file *removed make Max Lybbert *removed references to missing and unused as reported by Andy Elvey and Dan Kosecki *resynced with Christopher Diggins s branch as it exists in tree building code is back Christopher Diggins *resynced codebase with Chris s branch *removed tree building code
Definition: CHANGELOG.txt:8
Attribute make_xor_mapped_address(const ACE_INET_Addr &addr)
Definition: Stun.cpp:116
const size_t HEADER_SIZE
Definition: Stun.h:49
AttributesType attributes_
Definition: Stun.h:245
bool operator==(const DisjointSequence::OrderedRanges< T > &a, const DisjointSequence::OrderedRanges< T > &b)
Attribute make_use_candidate()
Definition: Stun.cpp:132
AttributeType
Definition: Stun.h:55
const ACE_UINT16 UNKNOWN_ATTRIBUTE
Definition: Stun.h:53
AttributeHolder(Attribute &a, const TransactionId &t)
Definition: Stun.h:158
ACE_UINT16 length_for_message_integrity() const
Definition: Stun.h:217
std::vector< Attribute > AttributesType
Definition: Stun.h:181
const TransactionId & tid
Definition: Stun.h:166
ACE_UINT16 length_for_message_integrity_
Definition: Stun.h:247
Class to serialize and deserialize data for DDS.
Definition: Serializer.h:369
const ACE_UINT16 BAD_REQUEST
Definition: Stun.h:51
#define OpenDDS_Rtps_Export
Definition: rtps_export.h:23
ACE_Message_Block * block
Definition: Stun.h:241
ACE_UINT32 fingerprint
Definition: Stun.h:87
Attribute make_ice_controlling(ACE_UINT64 ice_tie_breaker)
Definition: Stun.cpp:146
Attribute make_error_code(ACE_UINT16 code, const std::string &reason)
Definition: Stun.cpp:99
Attribute make_message_integrity()
Definition: Stun.cpp:92
std::string username
Definition: Stun.h:84
bool operator>>(DCPS::Serializer &serializer, AttributeHolder &holder)
Definition: Stun.cpp:179
Attribute make_fingerprint()
Definition: Stun.cpp:139
bool operator!=(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:125
ACE_UINT16 length() const
Definition: Stun.h:213
unsigned long long ACE_UINT64
ACE_UINT16 length_
Definition: Stun.h:246
AttributeType type
Definition: Stun.h:81
ACE_UINT32 priority
Definition: Stun.h:88
void append_attribute(const Attribute &attribute)
Definition: Stun.h:195
const_iterator end() const
Definition: Stun.h:209
Attribute make_unknown_attribute(ACE_UINT16 type, ACE_UINT16 length)
Definition: Stun.cpp:171
virtual ~Sender()
Definition: Stun.h:256
octet GuidPrefix_t[12]
Definition: DdsDcpsGuid.idl:19
Attribute make_unknown_attributes(const std::vector< AttributeType > &unknown_attributes)
Definition: Stun.cpp:108
AttributesType::const_iterator const_iterator
Definition: Stun.h:182
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
std::string password
Definition: Stun.h:242
const TransactionId & tid
Definition: Stun.h:156
unsigned char ACE_UINT8
Attribute make_mapped_address(const ACE_INET_Addr &addr)
Definition: Stun.cpp:76
const DCPS::Encoding encoding(DCPS::Encoding::KIND_UNALIGNED_CDR, DCPS::ENDIAN_BIG)
ACE_UINT64 ice_tie_breaker
Definition: Stun.h:89
TransactionId transaction_id
Definition: Stun.h:186
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
ACE_UINT16 unknown_length
Definition: Stun.h:99
ACE_UINT16 length() const
Definition: Stun.cpp:22
Attribute make_priority(ACE_UINT32 priority)
Definition: Stun.cpp:124
bool operator<(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:80
const Attribute & attribute
Definition: Stun.h:165
ACE_INET_Addr mapped_address
Definition: Stun.h:83
bool operator<<(DCPS::Serializer &serializer, ConstAttributeHolder &holder)
Definition: Stun.cpp:462