10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2004--2005, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_P2P_BASE_STUN_H_
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_P2P_BASE_STUN_H_
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This file contains classes for dealing with the STUN protocol, as specified
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// in RFC 5389, and its descendants.
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <vector>
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/basictypes.h"
382a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/bytebuffer.h"
392a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/socketaddress.h"
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// These are the types of STUN messages defined in RFC 5389.
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum StunMessageType {
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_BINDING_REQUEST                  = 0x0001,
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_BINDING_INDICATION               = 0x0011,
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_BINDING_RESPONSE                 = 0x0101,
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_BINDING_ERROR_RESPONSE           = 0x0111,
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// These are all known STUN attributes, defined in RFC 5389 and elsewhere.
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Next to each is the name of the class (T is StunTAttribute) that implements
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// that type.
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// RETRANSMIT_COUNT is the number of outstanding pings without a response at
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// the time the packet is generated.
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum StunAttributeType {
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_MAPPED_ADDRESS              = 0x0001,  // Address
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_USERNAME                    = 0x0006,  // ByteString
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_MESSAGE_INTEGRITY           = 0x0008,  // ByteString, 20 bytes
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_ERROR_CODE                  = 0x0009,  // ErrorCode
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_UNKNOWN_ATTRIBUTES          = 0x000a,  // UInt16List
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_REALM                       = 0x0014,  // ByteString
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_NONCE                       = 0x0015,  // ByteString
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_XOR_MAPPED_ADDRESS          = 0x0020,  // XorAddress
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_SOFTWARE                    = 0x8022,  // ByteString
6699a17fdd47621fec9dc34b80409579b6c210ab44guoweis@webrtc.org  STUN_ATTR_ALTERNATE_SERVER            = 0x8023,  // Address
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_FINGERPRINT                 = 0x8028,  // UInt32
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_RETRANSMIT_COUNT            = 0xFF00   // UInt32
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// These are the types of the values associated with the attributes above.
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This allows us to perform some basic validation when reading or adding
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// attributes. Note that these values are for our own use, and not defined in
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// RFC 5389.
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum StunAttributeValueType {
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_UNKNOWN                    = 0,
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_ADDRESS                    = 1,
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_XOR_ADDRESS                = 2,
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_UINT32                     = 3,
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_UINT64                     = 4,
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_BYTE_STRING                = 5,
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_ERROR_CODE                 = 6,
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_VALUE_UINT16_LIST                = 7
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// These are the types of STUN addresses defined in RFC 5389.
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum StunAddressFamily {
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // NB: UNDEF is not part of the STUN spec.
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ADDRESS_UNDEF                    = 0,
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ADDRESS_IPV4                     = 1,
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ADDRESS_IPV6                     = 2
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// These are the types of STUN error codes defined in RFC 5389.
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum StunErrorCode {
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_TRY_ALTERNATE              = 300,
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_BAD_REQUEST                = 400,
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_UNAUTHORIZED               = 401,
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_UNKNOWN_ATTRIBUTE          = 420,
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_STALE_CREDENTIALS          = 430,  // GICE only
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_STALE_NONCE                = 438,
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_SERVER_ERROR               = 500,
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_GLOBAL_FAILURE             = 600
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Strings for the error codes above.
10799a17fdd47621fec9dc34b80409579b6c210ab44guoweis@webrtc.orgextern const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[];
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_BAD_REQUEST[];
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_UNAUTHORIZED[];
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[];
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_STALE_CREDENTIALS[];
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_STALE_NONCE[];
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_SERVER_ERROR[];
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// The mask used to determine whether a STUN message is a request/response etc.
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst uint32 kStunTypeMask = 0x0110;
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// STUN Attribute header length.
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst size_t kStunAttributeHeaderSize = 4;
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Following values correspond to RFC5389.
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst size_t kStunHeaderSize = 20;
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst size_t kStunTransactionIdOffset = 8;
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst size_t kStunTransactionIdLength = 12;
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst uint32 kStunMagicCookie = 0x2112A442;
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst size_t kStunMagicCookieLength = sizeof(kStunMagicCookie);
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Following value corresponds to an earlier version of STUN from
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// RFC3489.
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst size_t kStunLegacyTransactionIdLength = 16;
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// STUN Message Integrity HMAC length.
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst size_t kStunMessageIntegritySize = 20;
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunAttribute;
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunAddressAttribute;
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunXorAddressAttribute;
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunUInt32Attribute;
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunUInt64Attribute;
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunByteStringAttribute;
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunErrorCodeAttribute;
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunUInt16ListAttribute;
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Records a complete STUN/TURN message.  Each message consists of a type and
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// any number of attributes.  Each attribute is parsed into an instance of an
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// appropriate class (see above).  The Get* methods will return instances of
1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// that attribute class.
1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunMessage {
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunMessage();
1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~StunMessage();
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int type() const { return type_; }
1540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  size_t length() const { return length_; }
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& transaction_id() const { return transaction_id_; }
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true if the message confirms to RFC3489 rather than
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // RFC5389. The main difference between two version of the STUN
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // protocol is the presence of the magic cookie and different length
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // of transaction ID. For outgoing packets version of the protocol
1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // is determined by the lengths of the transaction ID.
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool IsLegacy() const;
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetType(int type) { type_ = static_cast<uint16>(type); }
1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SetTransactionID(const std::string& str);
1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Gets the desired attribute value, or NULL if no such attribute type exists.
1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const StunAddressAttribute* GetAddress(int type) const;
1690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const StunUInt32Attribute* GetUInt32(int type) const;
1700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const StunUInt64Attribute* GetUInt64(int type) const;
1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const StunByteStringAttribute* GetByteString(int type) const;
1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Gets these specific attribute values.
1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const StunErrorCodeAttribute* GetErrorCode() const;
1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const StunUInt16ListAttribute* GetUnknownAttributes() const;
1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Takes ownership of the specified attribute, verifies it is of the correct
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // type, and adds it to the message. The return value indicates whether this
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // was successful.
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool AddAttribute(StunAttribute* attr);
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value.
1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This can't currently be done on a StunMessage, since it is affected by
1840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // padding data (which we discard when reading a StunMessage).
1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static bool ValidateMessageIntegrity(const char* data, size_t size,
1860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                       const std::string& password);
1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message.
1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool AddMessageIntegrity(const std::string& password);
1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool AddMessageIntegrity(const char* key, size_t keylen);
1900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT.
1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static bool ValidateFingerprint(const char* data, size_t size);
1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Adds a FINGERPRINT attribute that is valid for the current message.
1950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool AddFingerprint();
1960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Parses the STUN packet in the given buffer and records it here. The
1980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // return value indicates whether this was successful.
1992a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  bool Read(rtc::ByteBuffer* buf);
2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Writes this object into a STUN packet. The return value indicates whether
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // this was successful.
2032a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  bool Write(rtc::ByteBuffer* buf) const;
2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Creates an empty message. Overridable by derived classes.
2060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunMessage* CreateNew() const { return new StunMessage(); }
2070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
2090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Verifies that the given attribute is allowed for this message.
2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType GetAttributeValueType(int type) const;
2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
2130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunAttribute* CreateAttribute(int type, size_t length) /* const*/;
2140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const StunAttribute* GetAttribute(int type) const;
2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static bool IsValidTransactionId(const std::string& transaction_id);
2160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint16 type_;
2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint16 length_;
2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string transaction_id_;
2200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::vector<StunAttribute*>* attrs_;
2210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
2220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Base class for all STUN/TURN attributes.
2240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunAttribute {
2250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
2260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~StunAttribute() {
2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int type() const { return type_; }
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  size_t length() const { return length_; }
2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Return the type of this attribute.
2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const = 0;
2340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Only XorAddressAttribute needs this so far.
2360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetOwner(StunMessage* owner) {}
2370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Reads the body (not the type or length) for this type of attribute from
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // the given buffer.  Return value is true if successful.
2402a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Read(rtc::ByteBuffer* buf) = 0;
2410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Writes the body (not the type or length) to the given buffer.  Return
2430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // value is true if successful.
2442a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Write(rtc::ByteBuffer* buf) const = 0;
2450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Creates an attribute object with the given type and smallest length.
2470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunAttribute* Create(StunAttributeValueType value_type, uint16 type,
2480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                               uint16 length, StunMessage* owner);
2490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO: Allow these create functions to take parameters, to reduce
2500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // the amount of work callers need to do to initialize attributes.
2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunAddressAttribute* CreateAddress(uint16 type);
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunXorAddressAttribute* CreateXorAddress(uint16 type);
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunUInt32Attribute* CreateUInt32(uint16 type);
2540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunUInt64Attribute* CreateUInt64(uint16 type);
2550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunByteStringAttribute* CreateByteString(uint16 type);
2560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunErrorCodeAttribute* CreateErrorCode();
2570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static StunUInt16ListAttribute* CreateUnknownAttributes();
2580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
2600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunAttribute(uint16 type, uint16 length);
2610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetLength(uint16 length) { length_ = length; }
2622a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  void WritePadding(rtc::ByteBuffer* buf) const;
2632a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  void ConsumePadding(rtc::ByteBuffer* buf) const;
2640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
2660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint16 type_;
2670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint16 length_;
2680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
2690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Implements STUN attributes that record an Internet address.
2710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunAddressAttribute : public StunAttribute {
2720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
2730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static const uint16 SIZE_UNDEF = 0;
2740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static const uint16 SIZE_IP4 = 8;
2750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static const uint16 SIZE_IP6 = 20;
2762a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  StunAddressAttribute(uint16 type, const rtc::SocketAddress& addr);
2770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunAddressAttribute(uint16 type, uint16 length);
2780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const {
2800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_VALUE_ADDRESS;
2810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunAddressFamily family() const {
2840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    switch (address_.ipaddr().family()) {
2850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case AF_INET:
2860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        return STUN_ADDRESS_IPV4;
2870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case AF_INET6:
2880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        return STUN_ADDRESS_IPV6;
2890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
2900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_ADDRESS_UNDEF;
2910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2932a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  const rtc::SocketAddress& GetAddress() const { return address_; }
2942a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
2950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint16 port() const { return address_.port(); }
2960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2972a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  void SetAddress(const rtc::SocketAddress& addr) {
2980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    address_ = addr;
2990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EnsureAddressLength();
3000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3012a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  void SetIP(const rtc::IPAddress& ip) {
3020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    address_.SetIP(ip);
3030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EnsureAddressLength();
3040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetPort(uint16 port) { address_.SetPort(port); }
3060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3072a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Read(rtc::ByteBuffer* buf);
3082a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Write(rtc::ByteBuffer* buf) const;
3090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
3110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void EnsureAddressLength() {
3120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    switch (family()) {
3130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ADDRESS_IPV4: {
3140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetLength(SIZE_IP4);
3150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        break;
3160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ADDRESS_IPV6: {
3180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetLength(SIZE_IP6);
3190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        break;
3200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      default: {
3220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetLength(SIZE_UNDEF);
3230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        break;
3240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
3260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3272a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::SocketAddress address_;
3280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
3290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Implements STUN attributes that record an Internet address. When encoded
3310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// in a STUN message, the address contained in this attribute is XORed with the
3320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// transaction ID of the message.
3330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunXorAddressAttribute : public StunAddressAttribute {
3340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
3352a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  StunXorAddressAttribute(uint16 type, const rtc::SocketAddress& addr);
3360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunXorAddressAttribute(uint16 type, uint16 length,
3370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          StunMessage* owner);
3380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const {
3400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_VALUE_XOR_ADDRESS;
3410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetOwner(StunMessage* owner) {
3430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    owner_ = owner;
3440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3452a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Read(rtc::ByteBuffer* buf);
3462a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Write(rtc::ByteBuffer* buf) const;
3470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
3492a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::IPAddress GetXoredIP() const;
3500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunMessage* owner_;
3510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
3520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Implements STUN attributes that record a 32-bit integer.
3540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunUInt32Attribute : public StunAttribute {
3550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
3560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static const uint16 SIZE = 4;
3570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunUInt32Attribute(uint16 type, uint32 value);
3580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  explicit StunUInt32Attribute(uint16 type);
3590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const {
3610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_VALUE_UINT32;
3620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint32 value() const { return bits_; }
3650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetValue(uint32 bits) { bits_ = bits; }
3660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool GetBit(size_t index) const;
3680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetBit(size_t index, bool value);
3690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3702a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Read(rtc::ByteBuffer* buf);
3712a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Write(rtc::ByteBuffer* buf) const;
3720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
3740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint32 bits_;
3750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
3760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunUInt64Attribute : public StunAttribute {
3780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
3790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static const uint16 SIZE = 8;
3800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunUInt64Attribute(uint16 type, uint64 value);
3810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  explicit StunUInt64Attribute(uint16 type);
3820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const {
3840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_VALUE_UINT64;
3850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint64 value() const { return bits_; }
3880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetValue(uint64 bits) { bits_ = bits; }
3890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3902a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Read(rtc::ByteBuffer* buf);
3912a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Write(rtc::ByteBuffer* buf) const;
3920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
3940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint64 bits_;
3950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
3960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Implements STUN attributes that record an arbitrary byte string.
3980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunByteStringAttribute : public StunAttribute {
3990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
4000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  explicit StunByteStringAttribute(uint16 type);
4010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunByteStringAttribute(uint16 type, const std::string& str);
4020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunByteStringAttribute(uint16 type, const void* bytes, size_t length);
4030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunByteStringAttribute(uint16 type, uint16 length);
4040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ~StunByteStringAttribute();
4050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const {
4070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_VALUE_BYTE_STRING;
4080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const char* bytes() const { return bytes_; }
4110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string GetString() const { return std::string(bytes_, length()); }
4120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void CopyBytes(const char* bytes);  // uses strlen
4140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void CopyBytes(const void* bytes, size_t length);
4150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint8 GetByte(size_t index) const;
4170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetByte(size_t index, uint8 value);
4180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4192a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Read(rtc::ByteBuffer* buf);
4202a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual bool Write(rtc::ByteBuffer* buf) const;
4210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
4230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetBytes(char* bytes, size_t length);
4240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  char* bytes_;
4260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
4270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Implements STUN attributes that record an error code.
4290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunErrorCodeAttribute : public StunAttribute {
4300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
4310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static const uint16 MIN_SIZE = 4;
4320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunErrorCodeAttribute(uint16 type, int code, const std::string& reason);
4330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunErrorCodeAttribute(uint16 type, uint16 length);
4340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ~StunErrorCodeAttribute();
4350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const {
4370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_VALUE_ERROR_CODE;
4380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The combined error and class, e.g. 0x400.
4410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int code() const;
4420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetCode(int code);
4430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The individual error components.
4450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int eclass() const { return class_; }
4460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int number() const { return number_; }
4470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& reason() const { return reason_; }
4480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetClass(uint8 eclass) { class_ = eclass; }
4490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetNumber(uint8 number) { number_ = number; }
4500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetReason(const std::string& reason);
4510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4522a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  bool Read(rtc::ByteBuffer* buf);
4532a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  bool Write(rtc::ByteBuffer* buf) const;
4540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
4560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint8 class_;
4570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint8 number_;
4580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string reason_;
4590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
4600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Implements STUN attributes that record a list of attribute names.
4620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass StunUInt16ListAttribute : public StunAttribute {
4630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
4640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  StunUInt16ListAttribute(uint16 type, uint16 length);
4650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ~StunUInt16ListAttribute();
4660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType value_type() const {
4680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return STUN_VALUE_UINT16_LIST;
4690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  size_t Size() const;
4720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint16 GetType(int index) const;
4730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetType(int index, uint16 value);
4740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void AddType(uint16 value);
4750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4762a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  bool Read(rtc::ByteBuffer* buf);
4772a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  bool Write(rtc::ByteBuffer* buf) const;
4780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
4800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::vector<uint16>* attr_types_;
4810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
4820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns the (successful) response type for the given request type.
4840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns -1 if |request_type| is not a valid request type.
4850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgint GetStunSuccessResponseType(int request_type);
4860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns the error response type for the given request type.
4880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns -1 if |request_type| is not a valid request type.
4890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgint GetStunErrorResponseType(int request_type);
4900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns whether a given message is a request type.
4920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool IsStunRequestType(int msg_type);
4930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns whether a given message is an indication type.
4950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool IsStunIndicationType(int msg_type);
4960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns whether a given response is a success type.
4980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool IsStunSuccessResponseType(int msg_type);
4990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Returns whether a given response is an error type.
5010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool IsStunErrorResponseType(int msg_type);
5020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Computes the STUN long-term credential hash.
5040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool ComputeStunCredentialHash(const std::string& username,
5050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    const std::string& realm, const std::string& password, std::string* hash);
5060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// TODO: Move the TURN/ICE stuff below out to separate files.
5080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char TURN_MAGIC_COOKIE_VALUE[4];
5090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// "GTURN" STUN methods.
5110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// TODO: Rename these methods to GTURN_ to make it clear they aren't
5120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// part of standard STUN/TURN.
5130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum RelayMessageType {
5140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // For now, using the same defs from TurnMessageType below.
5150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // STUN_ALLOCATE_REQUEST              = 0x0003,
5160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // STUN_ALLOCATE_RESPONSE             = 0x0103,
5170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // STUN_ALLOCATE_ERROR_RESPONSE       = 0x0113,
5180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_SEND_REQUEST                     = 0x0004,
5190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_SEND_RESPONSE                    = 0x0104,
5200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_SEND_ERROR_RESPONSE              = 0x0114,
5210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_DATA_INDICATION                  = 0x0115,
5220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
5230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// "GTURN"-specific STUN attributes.
5250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// TODO: Rename these attributes to GTURN_ to avoid conflicts.
5260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum RelayAttributeType {
5270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_LIFETIME                    = 0x000d,  // UInt32
5280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_MAGIC_COOKIE                = 0x000f,  // ByteString, 4 bytes
5290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_BANDWIDTH                   = 0x0010,  // UInt32
5300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_DESTINATION_ADDRESS         = 0x0011,  // Address
5310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_SOURCE_ADDRESS2             = 0x0012,  // Address
5320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_DATA                        = 0x0013,  // ByteString
5330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_OPTIONS                     = 0x8001,  // UInt32
5340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
5350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// A "GTURN" STUN message.
5370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass RelayMessage : public StunMessage {
5380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
5390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType GetAttributeValueType(int type) const {
5400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    switch (type) {
5410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_LIFETIME:            return STUN_VALUE_UINT32;
5420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_MAGIC_COOKIE:        return STUN_VALUE_BYTE_STRING;
5430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_BANDWIDTH:           return STUN_VALUE_UINT32;
5440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_DESTINATION_ADDRESS: return STUN_VALUE_ADDRESS;
5450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_SOURCE_ADDRESS2:     return STUN_VALUE_ADDRESS;
5460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_DATA:                return STUN_VALUE_BYTE_STRING;
5470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_OPTIONS:             return STUN_VALUE_UINT32;
5480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      default: return StunMessage::GetAttributeValueType(type);
5490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
5500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
5510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunMessage* CreateNew() const { return new RelayMessage(); }
5520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
5530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Defined in TURN RFC 5766.
5550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum TurnMessageType {
5560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ALLOCATE_REQUEST                 = 0x0003,
5570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ALLOCATE_RESPONSE                = 0x0103,
5580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ALLOCATE_ERROR_RESPONSE          = 0x0113,
5590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_REFRESH_REQUEST                  = 0x0004,
5600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_REFRESH_RESPONSE                 = 0x0104,
5610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_REFRESH_ERROR_RESPONSE           = 0x0114,
5620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_SEND_INDICATION                  = 0x0016,
5630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_DATA_INDICATION                  = 0x0017,
5640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_CREATE_PERMISSION_REQUEST        = 0x0008,
5650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_CREATE_PERMISSION_RESPONSE       = 0x0108,
5660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
5670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_CHANNEL_BIND_REQUEST             = 0x0009,
5680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_CHANNEL_BIND_RESPONSE            = 0x0109,
5690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TURN_CHANNEL_BIND_ERROR_RESPONSE      = 0x0119,
5700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
5710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum TurnAttributeType {
5730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_CHANNEL_NUMBER              = 0x000C,  // UInt32
5740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_TURN_LIFETIME               = 0x000d,  // UInt32
5750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_XOR_PEER_ADDRESS            = 0x0012,  // XorAddress
5760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO(mallinath) - Uncomment after RelayAttributes are renamed.
5770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // STUN_ATTR_DATA                     = 0x0013,  // ByteString
5780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_XOR_RELAYED_ADDRESS         = 0x0016,  // XorAddress
5790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_EVEN_PORT                   = 0x0018,  // ByteString, 1 byte.
5800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_REQUESTED_TRANSPORT         = 0x0019,  // UInt32
5810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_DONT_FRAGMENT               = 0x001A,  // No content, Length = 0
5820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_RESERVATION_TOKEN           = 0x0022,  // ByteString, 8 bytes.
5830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and
5840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes
5850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // by appending G to attribute name.
5860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
5870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// RFC 5766-defined errors.
5890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum TurnErrorType {
5900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_FORBIDDEN                  = 403,
5910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_ALLOCATION_MISMATCH        = 437,
5920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_WRONG_CREDENTIALS          = 441,
5930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_UNSUPPORTED_PROTOCOL       = 442
5940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
5950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_FORBIDDEN[];
5960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
5970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
5980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
5990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass TurnMessage : public StunMessage {
6000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
6010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType GetAttributeValueType(int type) const {
6020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    switch (type) {
6030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_CHANNEL_NUMBER:      return STUN_VALUE_UINT32;
6040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_TURN_LIFETIME:       return STUN_VALUE_UINT32;
6050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_XOR_PEER_ADDRESS:    return STUN_VALUE_XOR_ADDRESS;
6060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_DATA:                return STUN_VALUE_BYTE_STRING;
6070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_XOR_RELAYED_ADDRESS: return STUN_VALUE_XOR_ADDRESS;
6080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_EVEN_PORT:           return STUN_VALUE_BYTE_STRING;
6090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_REQUESTED_TRANSPORT: return STUN_VALUE_UINT32;
6100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_DONT_FRAGMENT:       return STUN_VALUE_BYTE_STRING;
6110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_RESERVATION_TOKEN:   return STUN_VALUE_BYTE_STRING;
6120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      default: return StunMessage::GetAttributeValueType(type);
6130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
6140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
6150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunMessage* CreateNew() const { return new TurnMessage(); }
6160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
6170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// RFC 5245 ICE STUN attributes.
6190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum IceAttributeType {
6200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_PRIORITY                    = 0x0024,  // UInt32
6210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_USE_CANDIDATE               = 0x0025,  // No content, Length = 0
6220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_ICE_CONTROLLED              = 0x8029,  // UInt64
6230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ATTR_ICE_CONTROLLING             = 0x802A   // UInt64
6240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
6250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// RFC 5245-defined errors.
6270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgenum IceErrorCode {
6280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  STUN_ERROR_ROLE_CONFLICT              = 487,
6290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
6300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgextern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
6310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// A RFC 5245 ICE STUN message.
6330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass IceMessage : public StunMessage {
6340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
6350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunAttributeValueType GetAttributeValueType(int type) const {
6360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    switch (type) {
6370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_PRIORITY:        return STUN_VALUE_UINT32;
6380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_USE_CANDIDATE:   return STUN_VALUE_BYTE_STRING;
6390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_ICE_CONTROLLED:  return STUN_VALUE_UINT64;
6400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      case STUN_ATTR_ICE_CONTROLLING: return STUN_VALUE_UINT64;
6410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      default: return StunMessage::GetAttributeValueType(type);
6420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
6430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
6440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual StunMessage* CreateNew() const { return new IceMessage(); }
6450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
6460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace cricket
6480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_P2P_BASE_STUN_H_
650