stun.h 25 KB

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