blob: 8893b2a1ffd45d83fe149d418c93a31d1669d203 [file] [log] [blame]
Patrik Höglund56d94522019-11-18 15:53:32 +01001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef API_TRANSPORT_STUN_H_
12#define API_TRANSPORT_STUN_H_
13
14// This file contains classes for dealing with the STUN protocol, as specified
15// in RFC 5389, and its descendants.
16
17#include <stddef.h>
18#include <stdint.h>
Harald Alvestrandbee64082020-11-12 11:17:41 +000019#include <functional>
Patrik Höglund56d94522019-11-18 15:53:32 +010020#include <memory>
21#include <string>
22#include <vector>
23
Harald Alvestrandbee64082020-11-12 11:17:41 +000024#include "absl/strings/string_view.h"
25#include "api/array_view.h"
Patrik Höglund56d94522019-11-18 15:53:32 +010026#include "rtc_base/byte_buffer.h"
27#include "rtc_base/ip_address.h"
28#include "rtc_base/socket_address.h"
29
30namespace cricket {
31
32// These are the types of STUN messages defined in RFC 5389.
33enum StunMessageType {
34 STUN_BINDING_REQUEST = 0x0001,
35 STUN_BINDING_INDICATION = 0x0011,
36 STUN_BINDING_RESPONSE = 0x0101,
37 STUN_BINDING_ERROR_RESPONSE = 0x0111,
Jonas Oreland63737a92019-11-21 15:12:14 +010038
Jonas Oreland9a52bd72019-12-11 11:35:48 +010039 // Method 0x80, GOOG-PING is a variant of STUN BINDING
40 // that is sent instead of a STUN BINDING if the binding
41 // was identical to the one before.
Jonas Oreland63737a92019-11-21 15:12:14 +010042 GOOG_PING_REQUEST = 0x200,
43 GOOG_PING_RESPONSE = 0x300,
44 GOOG_PING_ERROR_RESPONSE = 0x310,
Patrik Höglund56d94522019-11-18 15:53:32 +010045};
46
47// These are all known STUN attributes, defined in RFC 5389 and elsewhere.
48// Next to each is the name of the class (T is StunTAttribute) that implements
49// that type.
50// RETRANSMIT_COUNT is the number of outstanding pings without a response at
51// the time the packet is generated.
52enum StunAttributeType {
53 STUN_ATTR_MAPPED_ADDRESS = 0x0001, // Address
54 STUN_ATTR_USERNAME = 0x0006, // ByteString
55 STUN_ATTR_MESSAGE_INTEGRITY = 0x0008, // ByteString, 20 bytes
56 STUN_ATTR_ERROR_CODE = 0x0009, // ErrorCode
57 STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000a, // UInt16List
58 STUN_ATTR_REALM = 0x0014, // ByteString
59 STUN_ATTR_NONCE = 0x0015, // ByteString
60 STUN_ATTR_XOR_MAPPED_ADDRESS = 0x0020, // XorAddress
61 STUN_ATTR_SOFTWARE = 0x8022, // ByteString
62 STUN_ATTR_ALTERNATE_SERVER = 0x8023, // Address
63 STUN_ATTR_FINGERPRINT = 0x8028, // UInt32
64 STUN_ATTR_ORIGIN = 0x802F, // ByteString
65 STUN_ATTR_RETRANSMIT_COUNT = 0xFF00 // UInt32
66};
67
68// These are the types of the values associated with the attributes above.
69// This allows us to perform some basic validation when reading or adding
70// attributes. Note that these values are for our own use, and not defined in
71// RFC 5389.
72enum StunAttributeValueType {
73 STUN_VALUE_UNKNOWN = 0,
74 STUN_VALUE_ADDRESS = 1,
75 STUN_VALUE_XOR_ADDRESS = 2,
76 STUN_VALUE_UINT32 = 3,
77 STUN_VALUE_UINT64 = 4,
78 STUN_VALUE_BYTE_STRING = 5,
79 STUN_VALUE_ERROR_CODE = 6,
80 STUN_VALUE_UINT16_LIST = 7
81};
82
83// These are the types of STUN addresses defined in RFC 5389.
84enum StunAddressFamily {
85 // NB: UNDEF is not part of the STUN spec.
86 STUN_ADDRESS_UNDEF = 0,
87 STUN_ADDRESS_IPV4 = 1,
88 STUN_ADDRESS_IPV6 = 2
89};
90
91// These are the types of STUN error codes defined in RFC 5389.
92enum StunErrorCode {
93 STUN_ERROR_TRY_ALTERNATE = 300,
94 STUN_ERROR_BAD_REQUEST = 400,
95 STUN_ERROR_UNAUTHORIZED = 401,
96 STUN_ERROR_UNKNOWN_ATTRIBUTE = 420,
97 STUN_ERROR_STALE_CREDENTIALS = 430, // GICE only
98 STUN_ERROR_STALE_NONCE = 438,
99 STUN_ERROR_SERVER_ERROR = 500,
100 STUN_ERROR_GLOBAL_FAILURE = 600
101};
102
103// Strings for the error codes above.
104extern const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[];
105extern const char STUN_ERROR_REASON_BAD_REQUEST[];
106extern const char STUN_ERROR_REASON_UNAUTHORIZED[];
107extern const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[];
108extern const char STUN_ERROR_REASON_STALE_CREDENTIALS[];
109extern const char STUN_ERROR_REASON_STALE_NONCE[];
110extern const char STUN_ERROR_REASON_SERVER_ERROR[];
111
112// The mask used to determine whether a STUN message is a request/response etc.
113const uint32_t kStunTypeMask = 0x0110;
114
115// STUN Attribute header length.
116const size_t kStunAttributeHeaderSize = 4;
117
118// Following values correspond to RFC5389.
119const size_t kStunHeaderSize = 20;
120const size_t kStunTransactionIdOffset = 8;
121const size_t kStunTransactionIdLength = 12;
122const uint32_t kStunMagicCookie = 0x2112A442;
123constexpr size_t kStunMagicCookieLength = sizeof(kStunMagicCookie);
124
125// Following value corresponds to an earlier version of STUN from
126// RFC3489.
127const size_t kStunLegacyTransactionIdLength = 16;
128
129// STUN Message Integrity HMAC length.
130const size_t kStunMessageIntegritySize = 20;
Jonas Oreland63737a92019-11-21 15:12:14 +0100131// Size of STUN_ATTR_MESSAGE_INTEGRITY_32
132const size_t kStunMessageIntegrity32Size = 4;
Patrik Höglund56d94522019-11-18 15:53:32 +0100133
134class StunAddressAttribute;
135class StunAttribute;
136class StunByteStringAttribute;
137class StunErrorCodeAttribute;
Patrik Höglund56d94522019-11-18 15:53:32 +0100138class StunUInt16ListAttribute;
139class StunUInt32Attribute;
140class StunUInt64Attribute;
141class StunXorAddressAttribute;
142
143// Records a complete STUN/TURN message. Each message consists of a type and
144// any number of attributes. Each attribute is parsed into an instance of an
145// appropriate class (see above). The Get* methods will return instances of
146// that attribute class.
147class StunMessage {
148 public:
149 StunMessage();
150 virtual ~StunMessage();
151
152 int type() const { return type_; }
153 size_t length() const { return length_; }
154 const std::string& transaction_id() const { return transaction_id_; }
155 uint32_t reduced_transaction_id() const { return reduced_transaction_id_; }
156
157 // Returns true if the message confirms to RFC3489 rather than
158 // RFC5389. The main difference between two version of the STUN
159 // protocol is the presence of the magic cookie and different length
160 // of transaction ID. For outgoing packets version of the protocol
161 // is determined by the lengths of the transaction ID.
162 bool IsLegacy() const;
163
164 void SetType(int type) { type_ = static_cast<uint16_t>(type); }
165 bool SetTransactionID(const std::string& str);
166
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700167 // Get a list of all of the attribute types in the "comprehension required"
168 // range that were not recognized.
169 std::vector<uint16_t> GetNonComprehendedAttributes() const;
170
Patrik Höglund56d94522019-11-18 15:53:32 +0100171 // Gets the desired attribute value, or NULL if no such attribute type exists.
172 const StunAddressAttribute* GetAddress(int type) const;
173 const StunUInt32Attribute* GetUInt32(int type) const;
174 const StunUInt64Attribute* GetUInt64(int type) const;
175 const StunByteStringAttribute* GetByteString(int type) const;
Jonas Oreland1721de12019-11-20 12:10:39 +0100176 const StunUInt16ListAttribute* GetUInt16List(int type) const;
Patrik Höglund56d94522019-11-18 15:53:32 +0100177
178 // Gets these specific attribute values.
179 const StunErrorCodeAttribute* GetErrorCode() const;
180 // Returns the code inside the error code attribute, if present, and
181 // STUN_ERROR_GLOBAL_FAILURE otherwise.
182 int GetErrorCodeValue() const;
183 const StunUInt16ListAttribute* GetUnknownAttributes() const;
184
185 // Takes ownership of the specified attribute and adds it to the message.
186 void AddAttribute(std::unique_ptr<StunAttribute> attr);
187
188 // Remove the last occurrence of an attribute.
189 std::unique_ptr<StunAttribute> RemoveAttribute(int type);
190
Jonas Oreland63737a92019-11-21 15:12:14 +0100191 // Remote all attributes and releases them.
192 void ClearAttributes();
193
Patrik Höglund56d94522019-11-18 15:53:32 +0100194 // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value.
195 // This can't currently be done on a StunMessage, since it is affected by
196 // padding data (which we discard when reading a StunMessage).
197 static bool ValidateMessageIntegrity(const char* data,
198 size_t size,
199 const std::string& password);
Jonas Oreland63737a92019-11-21 15:12:14 +0100200 static bool ValidateMessageIntegrity32(const char* data,
201 size_t size,
202 const std::string& password);
203
Patrik Höglund56d94522019-11-18 15:53:32 +0100204 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message.
205 bool AddMessageIntegrity(const std::string& password);
206 bool AddMessageIntegrity(const char* key, size_t keylen);
207
Jonas Oreland63737a92019-11-21 15:12:14 +0100208 // Adds a STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 attribute that is valid for the
209 // current message.
210 bool AddMessageIntegrity32(absl::string_view password);
211
Jonas Oreland253d50f2019-11-28 17:08:07 +0100212 // Verify that a buffer has stun magic cookie and one of the specified
213 // methods. Note that it does not check for the existance of FINGERPRINT.
214 static bool IsStunMethod(rtc::ArrayView<int> methods,
215 const char* data,
216 size_t size);
217
Patrik Höglund56d94522019-11-18 15:53:32 +0100218 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT.
219 static bool ValidateFingerprint(const char* data, size_t size);
220
221 // Adds a FINGERPRINT attribute that is valid for the current message.
222 bool AddFingerprint();
223
224 // Parses the STUN packet in the given buffer and records it here. The
225 // return value indicates whether this was successful.
226 bool Read(rtc::ByteBufferReader* buf);
227
228 // Writes this object into a STUN packet. The return value indicates whether
229 // this was successful.
230 bool Write(rtc::ByteBufferWriter* buf) const;
231
232 // Creates an empty message. Overridable by derived classes.
233 virtual StunMessage* CreateNew() const;
234
235 // Modify the stun magic cookie used for this STUN message.
236 // This is used for testing.
237 void SetStunMagicCookie(uint32_t val);
238
Jonas Oreland253d50f2019-11-28 17:08:07 +0100239 // Contruct a copy of |this|.
240 std::unique_ptr<StunMessage> Clone() const;
241
242 // Check if the attributes of this StunMessage equals those of |other|
243 // for all attributes that |attribute_type_mask| return true
244 bool EqualAttributes(const StunMessage* other,
245 std::function<bool(int type)> attribute_type_mask) const;
246
Patrik Höglund56d94522019-11-18 15:53:32 +0100247 protected:
248 // Verifies that the given attribute is allowed for this message.
249 virtual StunAttributeValueType GetAttributeValueType(int type) const;
250
Jonas Oreland253d50f2019-11-28 17:08:07 +0100251 std::vector<std::unique_ptr<StunAttribute>> attrs_;
252
Patrik Höglund56d94522019-11-18 15:53:32 +0100253 private:
254 StunAttribute* CreateAttribute(int type, size_t length) /* const*/;
255 const StunAttribute* GetAttribute(int type) const;
256 static bool IsValidTransactionId(const std::string& transaction_id);
Jonas Oreland63737a92019-11-21 15:12:14 +0100257 bool AddMessageIntegrityOfType(int mi_attr_type,
258 size_t mi_attr_size,
259 const char* key,
260 size_t keylen);
261 static bool ValidateMessageIntegrityOfType(int mi_attr_type,
262 size_t mi_attr_size,
263 const char* data,
264 size_t size,
265 const std::string& password);
Patrik Höglund56d94522019-11-18 15:53:32 +0100266
267 uint16_t type_;
268 uint16_t length_;
269 std::string transaction_id_;
270 uint32_t reduced_transaction_id_;
Patrik Höglund56d94522019-11-18 15:53:32 +0100271 uint32_t stun_magic_cookie_;
272};
273
274// Base class for all STUN/TURN attributes.
275class StunAttribute {
276 public:
277 virtual ~StunAttribute() {}
278
279 int type() const { return type_; }
280 size_t length() const { return length_; }
281
282 // Return the type of this attribute.
283 virtual StunAttributeValueType value_type() const = 0;
284
285 // Only XorAddressAttribute needs this so far.
286 virtual void SetOwner(StunMessage* owner) {}
287
288 // Reads the body (not the type or length) for this type of attribute from
289 // the given buffer. Return value is true if successful.
290 virtual bool Read(rtc::ByteBufferReader* buf) = 0;
291
292 // Writes the body (not the type or length) to the given buffer. Return
293 // value is true if successful.
294 virtual bool Write(rtc::ByteBufferWriter* buf) const = 0;
295
296 // Creates an attribute object with the given type and smallest length.
297 static StunAttribute* Create(StunAttributeValueType value_type,
298 uint16_t type,
299 uint16_t length,
300 StunMessage* owner);
301 // TODO(?): Allow these create functions to take parameters, to reduce
302 // the amount of work callers need to do to initialize attributes.
303 static std::unique_ptr<StunAddressAttribute> CreateAddress(uint16_t type);
304 static std::unique_ptr<StunXorAddressAttribute> CreateXorAddress(
305 uint16_t type);
306 static std::unique_ptr<StunUInt32Attribute> CreateUInt32(uint16_t type);
307 static std::unique_ptr<StunUInt64Attribute> CreateUInt64(uint16_t type);
308 static std::unique_ptr<StunByteStringAttribute> CreateByteString(
309 uint16_t type);
Jonas Oreland1721de12019-11-20 12:10:39 +0100310 static std::unique_ptr<StunUInt16ListAttribute> CreateUInt16ListAttribute(
311 uint16_t type);
Patrik Höglund56d94522019-11-18 15:53:32 +0100312 static std::unique_ptr<StunErrorCodeAttribute> CreateErrorCode();
313 static std::unique_ptr<StunUInt16ListAttribute> CreateUnknownAttributes();
314
315 protected:
316 StunAttribute(uint16_t type, uint16_t length);
317 void SetLength(uint16_t length) { length_ = length; }
318 void WritePadding(rtc::ByteBufferWriter* buf) const;
319 void ConsumePadding(rtc::ByteBufferReader* buf) const;
320
321 private:
322 uint16_t type_;
323 uint16_t length_;
324};
325
326// Implements STUN attributes that record an Internet address.
327class StunAddressAttribute : public StunAttribute {
328 public:
329 static const uint16_t SIZE_UNDEF = 0;
330 static const uint16_t SIZE_IP4 = 8;
331 static const uint16_t SIZE_IP6 = 20;
332 StunAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
333 StunAddressAttribute(uint16_t type, uint16_t length);
334
335 StunAttributeValueType value_type() const override;
336
337 StunAddressFamily family() const {
338 switch (address_.ipaddr().family()) {
339 case AF_INET:
340 return STUN_ADDRESS_IPV4;
341 case AF_INET6:
342 return STUN_ADDRESS_IPV6;
343 }
344 return STUN_ADDRESS_UNDEF;
345 }
346
347 const rtc::SocketAddress& GetAddress() const { return address_; }
348 const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
349 uint16_t port() const { return address_.port(); }
350
351 void SetAddress(const rtc::SocketAddress& addr) {
352 address_ = addr;
353 EnsureAddressLength();
354 }
355 void SetIP(const rtc::IPAddress& ip) {
356 address_.SetIP(ip);
357 EnsureAddressLength();
358 }
359 void SetPort(uint16_t port) { address_.SetPort(port); }
360
361 bool Read(rtc::ByteBufferReader* buf) override;
362 bool Write(rtc::ByteBufferWriter* buf) const override;
363
364 private:
365 void EnsureAddressLength() {
366 switch (family()) {
367 case STUN_ADDRESS_IPV4: {
368 SetLength(SIZE_IP4);
369 break;
370 }
371 case STUN_ADDRESS_IPV6: {
372 SetLength(SIZE_IP6);
373 break;
374 }
375 default: {
376 SetLength(SIZE_UNDEF);
377 break;
378 }
379 }
380 }
381 rtc::SocketAddress address_;
382};
383
384// Implements STUN attributes that record an Internet address. When encoded
385// in a STUN message, the address contained in this attribute is XORed with the
386// transaction ID of the message.
387class StunXorAddressAttribute : public StunAddressAttribute {
388 public:
389 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
390 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner);
391
392 StunAttributeValueType value_type() const override;
393 void SetOwner(StunMessage* owner) override;
394 bool Read(rtc::ByteBufferReader* buf) override;
395 bool Write(rtc::ByteBufferWriter* buf) const override;
396
397 private:
398 rtc::IPAddress GetXoredIP() const;
399 StunMessage* owner_;
400};
401
402// Implements STUN attributes that record a 32-bit integer.
403class StunUInt32Attribute : public StunAttribute {
404 public:
405 static const uint16_t SIZE = 4;
406 StunUInt32Attribute(uint16_t type, uint32_t value);
407 explicit StunUInt32Attribute(uint16_t type);
408
409 StunAttributeValueType value_type() const override;
410
411 uint32_t value() const { return bits_; }
412 void SetValue(uint32_t bits) { bits_ = bits; }
413
414 bool GetBit(size_t index) const;
415 void SetBit(size_t index, bool value);
416
417 bool Read(rtc::ByteBufferReader* buf) override;
418 bool Write(rtc::ByteBufferWriter* buf) const override;
419
420 private:
421 uint32_t bits_;
422};
423
424class StunUInt64Attribute : public StunAttribute {
425 public:
426 static const uint16_t SIZE = 8;
427 StunUInt64Attribute(uint16_t type, uint64_t value);
428 explicit StunUInt64Attribute(uint16_t type);
429
430 StunAttributeValueType value_type() const override;
431
432 uint64_t value() const { return bits_; }
433 void SetValue(uint64_t bits) { bits_ = bits; }
434
435 bool Read(rtc::ByteBufferReader* buf) override;
436 bool Write(rtc::ByteBufferWriter* buf) const override;
437
438 private:
439 uint64_t bits_;
440};
441
442// Implements STUN attributes that record an arbitrary byte string.
443class StunByteStringAttribute : public StunAttribute {
444 public:
445 explicit StunByteStringAttribute(uint16_t type);
446 StunByteStringAttribute(uint16_t type, const std::string& str);
447 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length);
448 StunByteStringAttribute(uint16_t type, uint16_t length);
449 ~StunByteStringAttribute() override;
450
451 StunAttributeValueType value_type() const override;
452
453 const char* bytes() const { return bytes_; }
454 std::string GetString() const { return std::string(bytes_, length()); }
455
456 void CopyBytes(const char* bytes); // uses strlen
457 void CopyBytes(const void* bytes, size_t length);
458
459 uint8_t GetByte(size_t index) const;
460 void SetByte(size_t index, uint8_t value);
461
462 bool Read(rtc::ByteBufferReader* buf) override;
463 bool Write(rtc::ByteBufferWriter* buf) const override;
464
465 private:
466 void SetBytes(char* bytes, size_t length);
467
468 char* bytes_;
469};
470
471// Implements STUN attributes that record an error code.
472class StunErrorCodeAttribute : public StunAttribute {
473 public:
474 static const uint16_t MIN_SIZE;
475 StunErrorCodeAttribute(uint16_t type, int code, const std::string& reason);
476 StunErrorCodeAttribute(uint16_t type, uint16_t length);
477 ~StunErrorCodeAttribute() override;
478
479 StunAttributeValueType value_type() const override;
480
481 // The combined error and class, e.g. 0x400.
482 int code() const;
483 void SetCode(int code);
484
485 // The individual error components.
486 int eclass() const { return class_; }
487 int number() const { return number_; }
488 const std::string& reason() const { return reason_; }
489 void SetClass(uint8_t eclass) { class_ = eclass; }
490 void SetNumber(uint8_t number) { number_ = number; }
491 void SetReason(const std::string& reason);
492
493 bool Read(rtc::ByteBufferReader* buf) override;
494 bool Write(rtc::ByteBufferWriter* buf) const override;
495
496 private:
497 uint8_t class_;
498 uint8_t number_;
499 std::string reason_;
500};
501
502// Implements STUN attributes that record a list of attribute names.
503class StunUInt16ListAttribute : public StunAttribute {
504 public:
505 StunUInt16ListAttribute(uint16_t type, uint16_t length);
506 ~StunUInt16ListAttribute() override;
507
508 StunAttributeValueType value_type() const override;
509
510 size_t Size() const;
511 uint16_t GetType(int index) const;
512 void SetType(int index, uint16_t value);
513 void AddType(uint16_t value);
Jonas Oreland63737a92019-11-21 15:12:14 +0100514 void AddTypeAtIndex(uint16_t index, uint16_t value);
Patrik Höglund56d94522019-11-18 15:53:32 +0100515
516 bool Read(rtc::ByteBufferReader* buf) override;
517 bool Write(rtc::ByteBufferWriter* buf) const override;
518
519 private:
520 std::vector<uint16_t>* attr_types_;
521};
522
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100523// Return a string e.g "STUN BINDING request".
524std::string StunMethodToString(int msg_type);
525
Patrik Höglund56d94522019-11-18 15:53:32 +0100526// Returns the (successful) response type for the given request type.
527// Returns -1 if |request_type| is not a valid request type.
528int GetStunSuccessResponseType(int request_type);
529
530// Returns the error response type for the given request type.
531// Returns -1 if |request_type| is not a valid request type.
532int GetStunErrorResponseType(int request_type);
533
534// Returns whether a given message is a request type.
535bool IsStunRequestType(int msg_type);
536
537// Returns whether a given message is an indication type.
538bool IsStunIndicationType(int msg_type);
539
540// Returns whether a given response is a success type.
541bool IsStunSuccessResponseType(int msg_type);
542
543// Returns whether a given response is an error type.
544bool IsStunErrorResponseType(int msg_type);
545
546// Computes the STUN long-term credential hash.
547bool ComputeStunCredentialHash(const std::string& username,
548 const std::string& realm,
549 const std::string& password,
550 std::string* hash);
551
552// Make a copy af |attribute| and return a new StunAttribute.
553// This is useful if you don't care about what kind of attribute you
554// are handling.
555//
556// The implementation copies by calling Write() followed by Read().
557//
558// If |tmp_buffer| is supplied this buffer will be used, otherwise
559// a buffer will created in the method.
560std::unique_ptr<StunAttribute> CopyStunAttribute(
561 const StunAttribute& attribute,
562 rtc::ByteBufferWriter* tmp_buffer_ptr = 0);
563
564// TODO(?): Move the TURN/ICE stuff below out to separate files.
565extern const char TURN_MAGIC_COOKIE_VALUE[4];
566
567// "GTURN" STUN methods.
568// TODO(?): Rename these methods to GTURN_ to make it clear they aren't
569// part of standard STUN/TURN.
570enum RelayMessageType {
571 // For now, using the same defs from TurnMessageType below.
572 // STUN_ALLOCATE_REQUEST = 0x0003,
573 // STUN_ALLOCATE_RESPONSE = 0x0103,
574 // STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
575 STUN_SEND_REQUEST = 0x0004,
576 STUN_SEND_RESPONSE = 0x0104,
577 STUN_SEND_ERROR_RESPONSE = 0x0114,
578 STUN_DATA_INDICATION = 0x0115,
579};
580
581// "GTURN"-specific STUN attributes.
582// TODO(?): Rename these attributes to GTURN_ to avoid conflicts.
583enum RelayAttributeType {
584 STUN_ATTR_LIFETIME = 0x000d, // UInt32
585 STUN_ATTR_MAGIC_COOKIE = 0x000f, // ByteString, 4 bytes
586 STUN_ATTR_BANDWIDTH = 0x0010, // UInt32
587 STUN_ATTR_DESTINATION_ADDRESS = 0x0011, // Address
588 STUN_ATTR_SOURCE_ADDRESS2 = 0x0012, // Address
589 STUN_ATTR_DATA = 0x0013, // ByteString
590 STUN_ATTR_OPTIONS = 0x8001, // UInt32
591};
592
593// A "GTURN" STUN message.
594class RelayMessage : public StunMessage {
595 protected:
596 StunAttributeValueType GetAttributeValueType(int type) const override;
597 StunMessage* CreateNew() const override;
598};
599
600// Defined in TURN RFC 5766.
601enum TurnMessageType {
602 STUN_ALLOCATE_REQUEST = 0x0003,
603 STUN_ALLOCATE_RESPONSE = 0x0103,
604 STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
605 TURN_REFRESH_REQUEST = 0x0004,
606 TURN_REFRESH_RESPONSE = 0x0104,
607 TURN_REFRESH_ERROR_RESPONSE = 0x0114,
608 TURN_SEND_INDICATION = 0x0016,
609 TURN_DATA_INDICATION = 0x0017,
610 TURN_CREATE_PERMISSION_REQUEST = 0x0008,
611 TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
612 TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
613 TURN_CHANNEL_BIND_REQUEST = 0x0009,
614 TURN_CHANNEL_BIND_RESPONSE = 0x0109,
615 TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
616};
617
618enum TurnAttributeType {
619 STUN_ATTR_CHANNEL_NUMBER = 0x000C, // UInt32
620 STUN_ATTR_TURN_LIFETIME = 0x000d, // UInt32
621 STUN_ATTR_XOR_PEER_ADDRESS = 0x0012, // XorAddress
622 // TODO(mallinath) - Uncomment after RelayAttributes are renamed.
623 // STUN_ATTR_DATA = 0x0013, // ByteString
624 STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016, // XorAddress
625 STUN_ATTR_EVEN_PORT = 0x0018, // ByteString, 1 byte.
626 STUN_ATTR_REQUESTED_TRANSPORT = 0x0019, // UInt32
627 STUN_ATTR_DONT_FRAGMENT = 0x001A, // No content, Length = 0
628 STUN_ATTR_RESERVATION_TOKEN = 0x0022, // ByteString, 8 bytes.
629 // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and
630 // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes
631 // by appending G to attribute name.
632};
633
634// RFC 5766-defined errors.
635enum TurnErrorType {
636 STUN_ERROR_FORBIDDEN = 403,
637 STUN_ERROR_ALLOCATION_MISMATCH = 437,
638 STUN_ERROR_WRONG_CREDENTIALS = 441,
639 STUN_ERROR_UNSUPPORTED_PROTOCOL = 442
640};
641
642extern const int SERVER_NOT_REACHABLE_ERROR;
643
644extern const char STUN_ERROR_REASON_FORBIDDEN[];
645extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
646extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
647extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
648class TurnMessage : public StunMessage {
649 protected:
650 StunAttributeValueType GetAttributeValueType(int type) const override;
651 StunMessage* CreateNew() const override;
652};
653
654enum IceAttributeType {
655 // RFC 5245 ICE STUN attributes.
656 STUN_ATTR_PRIORITY = 0x0024, // UInt32
657 STUN_ATTR_USE_CANDIDATE = 0x0025, // No content, Length = 0
658 STUN_ATTR_ICE_CONTROLLED = 0x8029, // UInt64
659 STUN_ATTR_ICE_CONTROLLING = 0x802A, // UInt64
660 // The following attributes are in the comprehension-optional range
661 // (0xC000-0xFFFF) and are not registered with IANA. These STUN attributes are
662 // intended for ICE and should NOT be used in generic use cases of STUN
663 // messages.
664 //
665 // Note that the value 0xC001 has already been assigned by IANA to
666 // ENF-FLOW-DESCRIPTION
667 // (https://www.iana.org/assignments/stun-parameters/stun-parameters.xml).
668 STUN_ATTR_NOMINATION = 0xC001, // UInt32
669 // UInt32. The higher 16 bits are the network ID. The lower 16 bits are the
670 // network cost.
Jonas Orelandfa543642020-09-16 10:44:54 +0200671 STUN_ATTR_GOOG_NETWORK_INFO = 0xC057,
Patrik Höglund56d94522019-11-18 15:53:32 +0100672 // Experimental: Transaction ID of the last connectivity check received.
Jonas Orelandfa543642020-09-16 10:44:54 +0200673 STUN_ATTR_GOOG_LAST_ICE_CHECK_RECEIVED = 0xC058,
Jonas Oreland1721de12019-11-20 12:10:39 +0100674 // Uint16List. Miscellaneous attributes for future extension.
675 STUN_ATTR_GOOG_MISC_INFO = 0xC059,
Jonas Orelandfa543642020-09-16 10:44:54 +0200676 // Obsolete.
677 STUN_ATTR_GOOG_OBSOLETE_1 = 0xC05A,
678 STUN_ATTR_GOOG_CONNECTION_ID = 0xC05B, // Not yet implemented.
679 STUN_ATTR_GOOG_DELTA = 0xC05C, // Not yet implemented.
680 STUN_ATTR_GOOG_DELTA_ACK = 0xC05D, // Not yet implemented.
Jonas Oreland63737a92019-11-21 15:12:14 +0100681 // MESSAGE-INTEGRITY truncated to 32-bit.
682 STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 = 0xC060,
Patrik Höglund56d94522019-11-18 15:53:32 +0100683};
684
Jonas Oreland1721de12019-11-20 12:10:39 +0100685// When adding new attributes to STUN_ATTR_GOOG_MISC_INFO
686// (which is a list of uint16_t), append the indices of these attributes below
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100687// and do NOT change the existing indices. The indices of attributes must be
Jonas Oreland1721de12019-11-20 12:10:39 +0100688// consistent with those used in ConnectionRequest::Prepare when forming a STUN
689// message for the ICE connectivity check, and they are used when parsing a
690// received STUN message.
Jonas Oreland219d8ce2020-01-15 15:29:48 +0100691enum class IceGoogMiscInfoBindingRequestAttributeIndex {
692 SUPPORT_GOOG_PING_VERSION = 0,
693};
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100694
695enum class IceGoogMiscInfoBindingResponseAttributeIndex {
696 SUPPORT_GOOG_PING_VERSION = 0,
697};
Jonas Oreland1721de12019-11-20 12:10:39 +0100698
Patrik Höglund56d94522019-11-18 15:53:32 +0100699// RFC 5245-defined errors.
700enum IceErrorCode {
701 STUN_ERROR_ROLE_CONFLICT = 487,
702};
703extern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
704
705// A RFC 5245 ICE STUN message.
706class IceMessage : public StunMessage {
707 protected:
708 StunAttributeValueType GetAttributeValueType(int type) const override;
709 StunMessage* CreateNew() const override;
710};
711
712} // namespace cricket
713
714#endif // API_TRANSPORT_STUN_H_