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