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