1/* 2 * libjingle 3 * Copyright 2004--2005, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#ifndef TALK_P2P_BASE_STUN_H_ 29#define TALK_P2P_BASE_STUN_H_ 30 31// This file contains classes for dealing with the STUN protocol, as specified 32// in RFC 5389, and its descendants. 33 34#include <string> 35#include <vector> 36 37#include "webrtc/base/basictypes.h" 38#include "webrtc/base/bytebuffer.h" 39#include "webrtc/base/socketaddress.h" 40 41namespace cricket { 42 43// These are the types of STUN messages defined in RFC 5389. 44enum StunMessageType { 45 STUN_BINDING_REQUEST = 0x0001, 46 STUN_BINDING_INDICATION = 0x0011, 47 STUN_BINDING_RESPONSE = 0x0101, 48 STUN_BINDING_ERROR_RESPONSE = 0x0111, 49}; 50 51// These are all known STUN attributes, defined in RFC 5389 and elsewhere. 52// Next to each is the name of the class (T is StunTAttribute) that implements 53// that type. 54// RETRANSMIT_COUNT is the number of outstanding pings without a response at 55// the time the packet is generated. 56enum StunAttributeType { 57 STUN_ATTR_MAPPED_ADDRESS = 0x0001, // Address 58 STUN_ATTR_USERNAME = 0x0006, // ByteString 59 STUN_ATTR_MESSAGE_INTEGRITY = 0x0008, // ByteString, 20 bytes 60 STUN_ATTR_ERROR_CODE = 0x0009, // ErrorCode 61 STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000a, // UInt16List 62 STUN_ATTR_REALM = 0x0014, // ByteString 63 STUN_ATTR_NONCE = 0x0015, // ByteString 64 STUN_ATTR_XOR_MAPPED_ADDRESS = 0x0020, // XorAddress 65 STUN_ATTR_SOFTWARE = 0x8022, // ByteString 66 STUN_ATTR_ALTERNATE_SERVER = 0x8023, // Address 67 STUN_ATTR_FINGERPRINT = 0x8028, // UInt32 68 STUN_ATTR_RETRANSMIT_COUNT = 0xFF00 // UInt32 69}; 70 71// These are the types of the values associated with the attributes above. 72// This allows us to perform some basic validation when reading or adding 73// attributes. Note that these values are for our own use, and not defined in 74// RFC 5389. 75enum StunAttributeValueType { 76 STUN_VALUE_UNKNOWN = 0, 77 STUN_VALUE_ADDRESS = 1, 78 STUN_VALUE_XOR_ADDRESS = 2, 79 STUN_VALUE_UINT32 = 3, 80 STUN_VALUE_UINT64 = 4, 81 STUN_VALUE_BYTE_STRING = 5, 82 STUN_VALUE_ERROR_CODE = 6, 83 STUN_VALUE_UINT16_LIST = 7 84}; 85 86// These are the types of STUN addresses defined in RFC 5389. 87enum StunAddressFamily { 88 // NB: UNDEF is not part of the STUN spec. 89 STUN_ADDRESS_UNDEF = 0, 90 STUN_ADDRESS_IPV4 = 1, 91 STUN_ADDRESS_IPV6 = 2 92}; 93 94// These are the types of STUN error codes defined in RFC 5389. 95enum StunErrorCode { 96 STUN_ERROR_TRY_ALTERNATE = 300, 97 STUN_ERROR_BAD_REQUEST = 400, 98 STUN_ERROR_UNAUTHORIZED = 401, 99 STUN_ERROR_UNKNOWN_ATTRIBUTE = 420, 100 STUN_ERROR_STALE_CREDENTIALS = 430, // GICE only 101 STUN_ERROR_STALE_NONCE = 438, 102 STUN_ERROR_SERVER_ERROR = 500, 103 STUN_ERROR_GLOBAL_FAILURE = 600 104}; 105 106// Strings for the error codes above. 107extern const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[]; 108extern const char STUN_ERROR_REASON_BAD_REQUEST[]; 109extern const char STUN_ERROR_REASON_UNAUTHORIZED[]; 110extern const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[]; 111extern const char STUN_ERROR_REASON_STALE_CREDENTIALS[]; 112extern const char STUN_ERROR_REASON_STALE_NONCE[]; 113extern const char STUN_ERROR_REASON_SERVER_ERROR[]; 114 115// The mask used to determine whether a STUN message is a request/response etc. 116const uint32 kStunTypeMask = 0x0110; 117 118// STUN Attribute header length. 119const size_t kStunAttributeHeaderSize = 4; 120 121// Following values correspond to RFC5389. 122const size_t kStunHeaderSize = 20; 123const size_t kStunTransactionIdOffset = 8; 124const size_t kStunTransactionIdLength = 12; 125const uint32 kStunMagicCookie = 0x2112A442; 126const size_t kStunMagicCookieLength = sizeof(kStunMagicCookie); 127 128// Following value corresponds to an earlier version of STUN from 129// RFC3489. 130const size_t kStunLegacyTransactionIdLength = 16; 131 132// STUN Message Integrity HMAC length. 133const size_t kStunMessageIntegritySize = 20; 134 135class StunAttribute; 136class StunAddressAttribute; 137class StunXorAddressAttribute; 138class StunUInt32Attribute; 139class StunUInt64Attribute; 140class StunByteStringAttribute; 141class StunErrorCodeAttribute; 142class StunUInt16ListAttribute; 143 144// Records a complete STUN/TURN message. Each message consists of a type and 145// any number of attributes. Each attribute is parsed into an instance of an 146// appropriate class (see above). The Get* methods will return instances of 147// that attribute class. 148class StunMessage { 149 public: 150 StunMessage(); 151 virtual ~StunMessage(); 152 153 int type() const { return type_; } 154 size_t length() const { return length_; } 155 const std::string& transaction_id() const { return transaction_id_; } 156 157 // Returns true if the message confirms to RFC3489 rather than 158 // RFC5389. The main difference between two version of the STUN 159 // protocol is the presence of the magic cookie and different length 160 // of transaction ID. For outgoing packets version of the protocol 161 // is determined by the lengths of the transaction ID. 162 bool IsLegacy() const; 163 164 void SetType(int type) { type_ = static_cast<uint16>(type); } 165 bool SetTransactionID(const std::string& str); 166 167 // Gets the desired attribute value, or NULL if no such attribute type exists. 168 const StunAddressAttribute* GetAddress(int type) const; 169 const StunUInt32Attribute* GetUInt32(int type) const; 170 const StunUInt64Attribute* GetUInt64(int type) const; 171 const StunByteStringAttribute* GetByteString(int type) const; 172 173 // Gets these specific attribute values. 174 const StunErrorCodeAttribute* GetErrorCode() const; 175 const StunUInt16ListAttribute* GetUnknownAttributes() const; 176 177 // Takes ownership of the specified attribute, verifies it is of the correct 178 // type, and adds it to the message. The return value indicates whether this 179 // was successful. 180 bool AddAttribute(StunAttribute* attr); 181 182 // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value. 183 // This can't currently be done on a StunMessage, since it is affected by 184 // padding data (which we discard when reading a StunMessage). 185 static bool ValidateMessageIntegrity(const char* data, size_t size, 186 const std::string& password); 187 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message. 188 bool AddMessageIntegrity(const std::string& password); 189 bool AddMessageIntegrity(const char* key, size_t keylen); 190 191 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT. 192 static bool ValidateFingerprint(const char* data, size_t size); 193 194 // Adds a FINGERPRINT attribute that is valid for the current message. 195 bool AddFingerprint(); 196 197 // Parses the STUN packet in the given buffer and records it here. The 198 // return value indicates whether this was successful. 199 bool Read(rtc::ByteBuffer* buf); 200 201 // Writes this object into a STUN packet. The return value indicates whether 202 // this was successful. 203 bool Write(rtc::ByteBuffer* buf) const; 204 205 // Creates an empty message. Overridable by derived classes. 206 virtual StunMessage* CreateNew() const { return new StunMessage(); } 207 208 protected: 209 // Verifies that the given attribute is allowed for this message. 210 virtual StunAttributeValueType GetAttributeValueType(int type) const; 211 212 private: 213 StunAttribute* CreateAttribute(int type, size_t length) /* const*/; 214 const StunAttribute* GetAttribute(int type) const; 215 static bool IsValidTransactionId(const std::string& transaction_id); 216 217 uint16 type_; 218 uint16 length_; 219 std::string transaction_id_; 220 std::vector<StunAttribute*>* attrs_; 221}; 222 223// Base class for all STUN/TURN attributes. 224class StunAttribute { 225 public: 226 virtual ~StunAttribute() { 227 } 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. 240 virtual bool Read(rtc::ByteBuffer* buf) = 0; 241 242 // Writes the body (not the type or length) to the given buffer. Return 243 // value is true if successful. 244 virtual bool Write(rtc::ByteBuffer* buf) const = 0; 245 246 // Creates an attribute object with the given type and smallest length. 247 static StunAttribute* Create(StunAttributeValueType value_type, uint16 type, 248 uint16 length, StunMessage* owner); 249 // TODO: Allow these create functions to take parameters, to reduce 250 // the amount of work callers need to do to initialize attributes. 251 static StunAddressAttribute* CreateAddress(uint16 type); 252 static StunXorAddressAttribute* CreateXorAddress(uint16 type); 253 static StunUInt32Attribute* CreateUInt32(uint16 type); 254 static StunUInt64Attribute* CreateUInt64(uint16 type); 255 static StunByteStringAttribute* CreateByteString(uint16 type); 256 static StunErrorCodeAttribute* CreateErrorCode(); 257 static StunUInt16ListAttribute* CreateUnknownAttributes(); 258 259 protected: 260 StunAttribute(uint16 type, uint16 length); 261 void SetLength(uint16 length) { length_ = length; } 262 void WritePadding(rtc::ByteBuffer* buf) const; 263 void ConsumePadding(rtc::ByteBuffer* buf) const; 264 265 private: 266 uint16 type_; 267 uint16 length_; 268}; 269 270// Implements STUN attributes that record an Internet address. 271class StunAddressAttribute : public StunAttribute { 272 public: 273 static const uint16 SIZE_UNDEF = 0; 274 static const uint16 SIZE_IP4 = 8; 275 static const uint16 SIZE_IP6 = 20; 276 StunAddressAttribute(uint16 type, const rtc::SocketAddress& addr); 277 StunAddressAttribute(uint16 type, uint16 length); 278 279 virtual StunAttributeValueType value_type() const { 280 return STUN_VALUE_ADDRESS; 281 } 282 283 StunAddressFamily family() const { 284 switch (address_.ipaddr().family()) { 285 case AF_INET: 286 return STUN_ADDRESS_IPV4; 287 case AF_INET6: 288 return STUN_ADDRESS_IPV6; 289 } 290 return STUN_ADDRESS_UNDEF; 291 } 292 293 const rtc::SocketAddress& GetAddress() const { return address_; } 294 const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); } 295 uint16 port() const { return address_.port(); } 296 297 void SetAddress(const rtc::SocketAddress& addr) { 298 address_ = addr; 299 EnsureAddressLength(); 300 } 301 void SetIP(const rtc::IPAddress& ip) { 302 address_.SetIP(ip); 303 EnsureAddressLength(); 304 } 305 void SetPort(uint16 port) { address_.SetPort(port); } 306 307 virtual bool Read(rtc::ByteBuffer* buf); 308 virtual bool Write(rtc::ByteBuffer* buf) const; 309 310 private: 311 void EnsureAddressLength() { 312 switch (family()) { 313 case STUN_ADDRESS_IPV4: { 314 SetLength(SIZE_IP4); 315 break; 316 } 317 case STUN_ADDRESS_IPV6: { 318 SetLength(SIZE_IP6); 319 break; 320 } 321 default: { 322 SetLength(SIZE_UNDEF); 323 break; 324 } 325 } 326 } 327 rtc::SocketAddress address_; 328}; 329 330// Implements STUN attributes that record an Internet address. When encoded 331// in a STUN message, the address contained in this attribute is XORed with the 332// transaction ID of the message. 333class StunXorAddressAttribute : public StunAddressAttribute { 334 public: 335 StunXorAddressAttribute(uint16 type, const rtc::SocketAddress& addr); 336 StunXorAddressAttribute(uint16 type, uint16 length, 337 StunMessage* owner); 338 339 virtual StunAttributeValueType value_type() const { 340 return STUN_VALUE_XOR_ADDRESS; 341 } 342 virtual void SetOwner(StunMessage* owner) { 343 owner_ = owner; 344 } 345 virtual bool Read(rtc::ByteBuffer* buf); 346 virtual bool Write(rtc::ByteBuffer* buf) const; 347 348 private: 349 rtc::IPAddress GetXoredIP() const; 350 StunMessage* owner_; 351}; 352 353// Implements STUN attributes that record a 32-bit integer. 354class StunUInt32Attribute : public StunAttribute { 355 public: 356 static const uint16 SIZE = 4; 357 StunUInt32Attribute(uint16 type, uint32 value); 358 explicit StunUInt32Attribute(uint16 type); 359 360 virtual StunAttributeValueType value_type() const { 361 return STUN_VALUE_UINT32; 362 } 363 364 uint32 value() const { return bits_; } 365 void SetValue(uint32 bits) { bits_ = bits; } 366 367 bool GetBit(size_t index) const; 368 void SetBit(size_t index, bool value); 369 370 virtual bool Read(rtc::ByteBuffer* buf); 371 virtual bool Write(rtc::ByteBuffer* buf) const; 372 373 private: 374 uint32 bits_; 375}; 376 377class StunUInt64Attribute : public StunAttribute { 378 public: 379 static const uint16 SIZE = 8; 380 StunUInt64Attribute(uint16 type, uint64 value); 381 explicit StunUInt64Attribute(uint16 type); 382 383 virtual StunAttributeValueType value_type() const { 384 return STUN_VALUE_UINT64; 385 } 386 387 uint64 value() const { return bits_; } 388 void SetValue(uint64 bits) { bits_ = bits; } 389 390 virtual bool Read(rtc::ByteBuffer* buf); 391 virtual bool Write(rtc::ByteBuffer* buf) const; 392 393 private: 394 uint64 bits_; 395}; 396 397// Implements STUN attributes that record an arbitrary byte string. 398class StunByteStringAttribute : public StunAttribute { 399 public: 400 explicit StunByteStringAttribute(uint16 type); 401 StunByteStringAttribute(uint16 type, const std::string& str); 402 StunByteStringAttribute(uint16 type, const void* bytes, size_t length); 403 StunByteStringAttribute(uint16 type, uint16 length); 404 ~StunByteStringAttribute(); 405 406 virtual StunAttributeValueType value_type() const { 407 return STUN_VALUE_BYTE_STRING; 408 } 409 410 const char* bytes() const { return bytes_; } 411 std::string GetString() const { return std::string(bytes_, length()); } 412 413 void CopyBytes(const char* bytes); // uses strlen 414 void CopyBytes(const void* bytes, size_t length); 415 416 uint8 GetByte(size_t index) const; 417 void SetByte(size_t index, uint8 value); 418 419 virtual bool Read(rtc::ByteBuffer* buf); 420 virtual bool Write(rtc::ByteBuffer* buf) const; 421 422 private: 423 void SetBytes(char* bytes, size_t length); 424 425 char* bytes_; 426}; 427 428// Implements STUN attributes that record an error code. 429class StunErrorCodeAttribute : public StunAttribute { 430 public: 431 static const uint16 MIN_SIZE = 4; 432 StunErrorCodeAttribute(uint16 type, int code, const std::string& reason); 433 StunErrorCodeAttribute(uint16 type, uint16 length); 434 ~StunErrorCodeAttribute(); 435 436 virtual StunAttributeValueType value_type() const { 437 return STUN_VALUE_ERROR_CODE; 438 } 439 440 // The combined error and class, e.g. 0x400. 441 int code() const; 442 void SetCode(int code); 443 444 // The individual error components. 445 int eclass() const { return class_; } 446 int number() const { return number_; } 447 const std::string& reason() const { return reason_; } 448 void SetClass(uint8 eclass) { class_ = eclass; } 449 void SetNumber(uint8 number) { number_ = number; } 450 void SetReason(const std::string& reason); 451 452 bool Read(rtc::ByteBuffer* buf); 453 bool Write(rtc::ByteBuffer* buf) const; 454 455 private: 456 uint8 class_; 457 uint8 number_; 458 std::string reason_; 459}; 460 461// Implements STUN attributes that record a list of attribute names. 462class StunUInt16ListAttribute : public StunAttribute { 463 public: 464 StunUInt16ListAttribute(uint16 type, uint16 length); 465 ~StunUInt16ListAttribute(); 466 467 virtual StunAttributeValueType value_type() const { 468 return STUN_VALUE_UINT16_LIST; 469 } 470 471 size_t Size() const; 472 uint16 GetType(int index) const; 473 void SetType(int index, uint16 value); 474 void AddType(uint16 value); 475 476 bool Read(rtc::ByteBuffer* buf); 477 bool Write(rtc::ByteBuffer* buf) const; 478 479 private: 480 std::vector<uint16>* attr_types_; 481}; 482 483// Returns the (successful) response type for the given request type. 484// Returns -1 if |request_type| is not a valid request type. 485int GetStunSuccessResponseType(int request_type); 486 487// Returns the error response type for the given request type. 488// Returns -1 if |request_type| is not a valid request type. 489int GetStunErrorResponseType(int request_type); 490 491// Returns whether a given message is a request type. 492bool IsStunRequestType(int msg_type); 493 494// Returns whether a given message is an indication type. 495bool IsStunIndicationType(int msg_type); 496 497// Returns whether a given response is a success type. 498bool IsStunSuccessResponseType(int msg_type); 499 500// Returns whether a given response is an error type. 501bool IsStunErrorResponseType(int msg_type); 502 503// Computes the STUN long-term credential hash. 504bool ComputeStunCredentialHash(const std::string& username, 505 const std::string& realm, const std::string& password, std::string* hash); 506 507// TODO: Move the TURN/ICE stuff below out to separate files. 508extern const char TURN_MAGIC_COOKIE_VALUE[4]; 509 510// "GTURN" STUN methods. 511// TODO: Rename these methods to GTURN_ to make it clear they aren't 512// part of standard STUN/TURN. 513enum RelayMessageType { 514 // For now, using the same defs from TurnMessageType below. 515 // STUN_ALLOCATE_REQUEST = 0x0003, 516 // STUN_ALLOCATE_RESPONSE = 0x0103, 517 // STUN_ALLOCATE_ERROR_RESPONSE = 0x0113, 518 STUN_SEND_REQUEST = 0x0004, 519 STUN_SEND_RESPONSE = 0x0104, 520 STUN_SEND_ERROR_RESPONSE = 0x0114, 521 STUN_DATA_INDICATION = 0x0115, 522}; 523 524// "GTURN"-specific STUN attributes. 525// TODO: Rename these attributes to GTURN_ to avoid conflicts. 526enum RelayAttributeType { 527 STUN_ATTR_LIFETIME = 0x000d, // UInt32 528 STUN_ATTR_MAGIC_COOKIE = 0x000f, // ByteString, 4 bytes 529 STUN_ATTR_BANDWIDTH = 0x0010, // UInt32 530 STUN_ATTR_DESTINATION_ADDRESS = 0x0011, // Address 531 STUN_ATTR_SOURCE_ADDRESS2 = 0x0012, // Address 532 STUN_ATTR_DATA = 0x0013, // ByteString 533 STUN_ATTR_OPTIONS = 0x8001, // UInt32 534}; 535 536// A "GTURN" STUN message. 537class RelayMessage : public StunMessage { 538 protected: 539 virtual StunAttributeValueType GetAttributeValueType(int type) const { 540 switch (type) { 541 case STUN_ATTR_LIFETIME: return STUN_VALUE_UINT32; 542 case STUN_ATTR_MAGIC_COOKIE: return STUN_VALUE_BYTE_STRING; 543 case STUN_ATTR_BANDWIDTH: return STUN_VALUE_UINT32; 544 case STUN_ATTR_DESTINATION_ADDRESS: return STUN_VALUE_ADDRESS; 545 case STUN_ATTR_SOURCE_ADDRESS2: return STUN_VALUE_ADDRESS; 546 case STUN_ATTR_DATA: return STUN_VALUE_BYTE_STRING; 547 case STUN_ATTR_OPTIONS: return STUN_VALUE_UINT32; 548 default: return StunMessage::GetAttributeValueType(type); 549 } 550 } 551 virtual StunMessage* CreateNew() const { return new RelayMessage(); } 552}; 553 554// Defined in TURN RFC 5766. 555enum TurnMessageType { 556 STUN_ALLOCATE_REQUEST = 0x0003, 557 STUN_ALLOCATE_RESPONSE = 0x0103, 558 STUN_ALLOCATE_ERROR_RESPONSE = 0x0113, 559 TURN_REFRESH_REQUEST = 0x0004, 560 TURN_REFRESH_RESPONSE = 0x0104, 561 TURN_REFRESH_ERROR_RESPONSE = 0x0114, 562 TURN_SEND_INDICATION = 0x0016, 563 TURN_DATA_INDICATION = 0x0017, 564 TURN_CREATE_PERMISSION_REQUEST = 0x0008, 565 TURN_CREATE_PERMISSION_RESPONSE = 0x0108, 566 TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118, 567 TURN_CHANNEL_BIND_REQUEST = 0x0009, 568 TURN_CHANNEL_BIND_RESPONSE = 0x0109, 569 TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119, 570}; 571 572enum TurnAttributeType { 573 STUN_ATTR_CHANNEL_NUMBER = 0x000C, // UInt32 574 STUN_ATTR_TURN_LIFETIME = 0x000d, // UInt32 575 STUN_ATTR_XOR_PEER_ADDRESS = 0x0012, // XorAddress 576 // TODO(mallinath) - Uncomment after RelayAttributes are renamed. 577 // STUN_ATTR_DATA = 0x0013, // ByteString 578 STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016, // XorAddress 579 STUN_ATTR_EVEN_PORT = 0x0018, // ByteString, 1 byte. 580 STUN_ATTR_REQUESTED_TRANSPORT = 0x0019, // UInt32 581 STUN_ATTR_DONT_FRAGMENT = 0x001A, // No content, Length = 0 582 STUN_ATTR_RESERVATION_TOKEN = 0x0022, // ByteString, 8 bytes. 583 // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and 584 // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes 585 // by appending G to attribute name. 586}; 587 588// RFC 5766-defined errors. 589enum TurnErrorType { 590 STUN_ERROR_FORBIDDEN = 403, 591 STUN_ERROR_ALLOCATION_MISMATCH = 437, 592 STUN_ERROR_WRONG_CREDENTIALS = 441, 593 STUN_ERROR_UNSUPPORTED_PROTOCOL = 442 594}; 595extern const char STUN_ERROR_REASON_FORBIDDEN[]; 596extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[]; 597extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[]; 598extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[]; 599class TurnMessage : public StunMessage { 600 protected: 601 virtual StunAttributeValueType GetAttributeValueType(int type) const { 602 switch (type) { 603 case STUN_ATTR_CHANNEL_NUMBER: return STUN_VALUE_UINT32; 604 case STUN_ATTR_TURN_LIFETIME: return STUN_VALUE_UINT32; 605 case STUN_ATTR_XOR_PEER_ADDRESS: return STUN_VALUE_XOR_ADDRESS; 606 case STUN_ATTR_DATA: return STUN_VALUE_BYTE_STRING; 607 case STUN_ATTR_XOR_RELAYED_ADDRESS: return STUN_VALUE_XOR_ADDRESS; 608 case STUN_ATTR_EVEN_PORT: return STUN_VALUE_BYTE_STRING; 609 case STUN_ATTR_REQUESTED_TRANSPORT: return STUN_VALUE_UINT32; 610 case STUN_ATTR_DONT_FRAGMENT: return STUN_VALUE_BYTE_STRING; 611 case STUN_ATTR_RESERVATION_TOKEN: return STUN_VALUE_BYTE_STRING; 612 default: return StunMessage::GetAttributeValueType(type); 613 } 614 } 615 virtual StunMessage* CreateNew() const { return new TurnMessage(); } 616}; 617 618// RFC 5245 ICE STUN attributes. 619enum IceAttributeType { 620 STUN_ATTR_PRIORITY = 0x0024, // UInt32 621 STUN_ATTR_USE_CANDIDATE = 0x0025, // No content, Length = 0 622 STUN_ATTR_ICE_CONTROLLED = 0x8029, // UInt64 623 STUN_ATTR_ICE_CONTROLLING = 0x802A // UInt64 624}; 625 626// RFC 5245-defined errors. 627enum IceErrorCode { 628 STUN_ERROR_ROLE_CONFLICT = 487, 629}; 630extern const char STUN_ERROR_REASON_ROLE_CONFLICT[]; 631 632// A RFC 5245 ICE STUN message. 633class IceMessage : public StunMessage { 634 protected: 635 virtual StunAttributeValueType GetAttributeValueType(int type) const { 636 switch (type) { 637 case STUN_ATTR_PRIORITY: return STUN_VALUE_UINT32; 638 case STUN_ATTR_USE_CANDIDATE: return STUN_VALUE_BYTE_STRING; 639 case STUN_ATTR_ICE_CONTROLLED: return STUN_VALUE_UINT64; 640 case STUN_ATTR_ICE_CONTROLLING: return STUN_VALUE_UINT64; 641 default: return StunMessage::GetAttributeValueType(type); 642 } 643 } 644 virtual StunMessage* CreateNew() const { return new IceMessage(); } 645}; 646 647} // namespace cricket 648 649#endif // TALK_P2P_BASE_STUN_H_ 650