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