123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- #ifndef API_TRANSPORT_STUN_H_
- #define API_TRANSPORT_STUN_H_
- #include <stddef.h>
- #include <stdint.h>
- #include <memory>
- #include <string>
- #include <vector>
- #include "rtc_base/byte_buffer.h"
- #include "rtc_base/ip_address.h"
- #include "rtc_base/socket_address.h"
- namespace cricket {
- enum StunMessageType {
- STUN_BINDING_REQUEST = 0x0001,
- STUN_BINDING_INDICATION = 0x0011,
- STUN_BINDING_RESPONSE = 0x0101,
- STUN_BINDING_ERROR_RESPONSE = 0x0111,
-
-
-
- GOOG_PING_REQUEST = 0x200,
- GOOG_PING_RESPONSE = 0x300,
- GOOG_PING_ERROR_RESPONSE = 0x310,
- };
- enum StunAttributeType {
- STUN_ATTR_MAPPED_ADDRESS = 0x0001,
- STUN_ATTR_USERNAME = 0x0006,
- STUN_ATTR_MESSAGE_INTEGRITY = 0x0008,
- STUN_ATTR_ERROR_CODE = 0x0009,
- STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000a,
- STUN_ATTR_REALM = 0x0014,
- STUN_ATTR_NONCE = 0x0015,
- STUN_ATTR_XOR_MAPPED_ADDRESS = 0x0020,
- STUN_ATTR_SOFTWARE = 0x8022,
- STUN_ATTR_ALTERNATE_SERVER = 0x8023,
- STUN_ATTR_FINGERPRINT = 0x8028,
- STUN_ATTR_ORIGIN = 0x802F,
- STUN_ATTR_RETRANSMIT_COUNT = 0xFF00
- };
- enum StunAttributeValueType {
- STUN_VALUE_UNKNOWN = 0,
- STUN_VALUE_ADDRESS = 1,
- STUN_VALUE_XOR_ADDRESS = 2,
- STUN_VALUE_UINT32 = 3,
- STUN_VALUE_UINT64 = 4,
- STUN_VALUE_BYTE_STRING = 5,
- STUN_VALUE_ERROR_CODE = 6,
- STUN_VALUE_UINT16_LIST = 7
- };
- enum StunAddressFamily {
-
- STUN_ADDRESS_UNDEF = 0,
- STUN_ADDRESS_IPV4 = 1,
- STUN_ADDRESS_IPV6 = 2
- };
- enum StunErrorCode {
- STUN_ERROR_TRY_ALTERNATE = 300,
- STUN_ERROR_BAD_REQUEST = 400,
- STUN_ERROR_UNAUTHORIZED = 401,
- STUN_ERROR_UNKNOWN_ATTRIBUTE = 420,
- STUN_ERROR_STALE_CREDENTIALS = 430,
- STUN_ERROR_STALE_NONCE = 438,
- STUN_ERROR_SERVER_ERROR = 500,
- STUN_ERROR_GLOBAL_FAILURE = 600
- };
- extern const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[];
- extern const char STUN_ERROR_REASON_BAD_REQUEST[];
- extern const char STUN_ERROR_REASON_UNAUTHORIZED[];
- extern const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[];
- extern const char STUN_ERROR_REASON_STALE_CREDENTIALS[];
- extern const char STUN_ERROR_REASON_STALE_NONCE[];
- extern const char STUN_ERROR_REASON_SERVER_ERROR[];
- const uint32_t kStunTypeMask = 0x0110;
- const size_t kStunAttributeHeaderSize = 4;
- const size_t kStunHeaderSize = 20;
- const size_t kStunTransactionIdOffset = 8;
- const size_t kStunTransactionIdLength = 12;
- const uint32_t kStunMagicCookie = 0x2112A442;
- constexpr size_t kStunMagicCookieLength = sizeof(kStunMagicCookie);
- const size_t kStunLegacyTransactionIdLength = 16;
- const size_t kStunMessageIntegritySize = 20;
- const size_t kStunMessageIntegrity32Size = 4;
- class StunAddressAttribute;
- class StunAttribute;
- class StunByteStringAttribute;
- class StunErrorCodeAttribute;
- class StunUInt16ListAttribute;
- class StunUInt32Attribute;
- class StunUInt64Attribute;
- class StunXorAddressAttribute;
- class StunMessage {
- public:
- StunMessage();
- virtual ~StunMessage();
- int type() const { return type_; }
- size_t length() const { return length_; }
- const std::string& transaction_id() const { return transaction_id_; }
- uint32_t reduced_transaction_id() const { return reduced_transaction_id_; }
-
-
-
-
-
- bool IsLegacy() const;
- void SetType(int type) { type_ = static_cast<uint16_t>(type); }
- bool SetTransactionID(const std::string& str);
-
-
- std::vector<uint16_t> GetNonComprehendedAttributes() const;
-
- const StunAddressAttribute* GetAddress(int type) const;
- const StunUInt32Attribute* GetUInt32(int type) const;
- const StunUInt64Attribute* GetUInt64(int type) const;
- const StunByteStringAttribute* GetByteString(int type) const;
- const StunUInt16ListAttribute* GetUInt16List(int type) const;
-
- const StunErrorCodeAttribute* GetErrorCode() const;
-
-
- int GetErrorCodeValue() const;
- const StunUInt16ListAttribute* GetUnknownAttributes() const;
-
- void AddAttribute(std::unique_ptr<StunAttribute> attr);
-
- std::unique_ptr<StunAttribute> RemoveAttribute(int type);
-
- void ClearAttributes();
-
-
-
- static bool ValidateMessageIntegrity(const char* data,
- size_t size,
- const std::string& password);
- static bool ValidateMessageIntegrity32(const char* data,
- size_t size,
- const std::string& password);
-
- bool AddMessageIntegrity(const std::string& password);
- bool AddMessageIntegrity(const char* key, size_t keylen);
-
-
- bool AddMessageIntegrity32(absl::string_view password);
-
-
- static bool IsStunMethod(rtc::ArrayView<int> methods,
- const char* data,
- size_t size);
-
- static bool ValidateFingerprint(const char* data, size_t size);
-
- bool AddFingerprint();
-
-
- bool Read(rtc::ByteBufferReader* buf);
-
-
- bool Write(rtc::ByteBufferWriter* buf) const;
-
- virtual StunMessage* CreateNew() const;
-
-
- void SetStunMagicCookie(uint32_t val);
-
- std::unique_ptr<StunMessage> Clone() const;
-
-
- bool EqualAttributes(const StunMessage* other,
- std::function<bool(int type)> attribute_type_mask) const;
- protected:
-
- virtual StunAttributeValueType GetAttributeValueType(int type) const;
- std::vector<std::unique_ptr<StunAttribute>> attrs_;
- private:
- StunAttribute* CreateAttribute(int type, size_t length) ;
- const StunAttribute* GetAttribute(int type) const;
- static bool IsValidTransactionId(const std::string& transaction_id);
- bool AddMessageIntegrityOfType(int mi_attr_type,
- size_t mi_attr_size,
- const char* key,
- size_t keylen);
- static bool ValidateMessageIntegrityOfType(int mi_attr_type,
- size_t mi_attr_size,
- const char* data,
- size_t size,
- const std::string& password);
- uint16_t type_;
- uint16_t length_;
- std::string transaction_id_;
- uint32_t reduced_transaction_id_;
- uint32_t stun_magic_cookie_;
- };
- class StunAttribute {
- public:
- virtual ~StunAttribute() {}
- int type() const { return type_; }
- size_t length() const { return length_; }
-
- virtual StunAttributeValueType value_type() const = 0;
-
- virtual void SetOwner(StunMessage* owner) {}
-
-
- virtual bool Read(rtc::ByteBufferReader* buf) = 0;
-
-
- virtual bool Write(rtc::ByteBufferWriter* buf) const = 0;
-
- static StunAttribute* Create(StunAttributeValueType value_type,
- uint16_t type,
- uint16_t length,
- StunMessage* owner);
-
-
- static std::unique_ptr<StunAddressAttribute> CreateAddress(uint16_t type);
- static std::unique_ptr<StunXorAddressAttribute> CreateXorAddress(
- uint16_t type);
- static std::unique_ptr<StunUInt32Attribute> CreateUInt32(uint16_t type);
- static std::unique_ptr<StunUInt64Attribute> CreateUInt64(uint16_t type);
- static std::unique_ptr<StunByteStringAttribute> CreateByteString(
- uint16_t type);
- static std::unique_ptr<StunUInt16ListAttribute> CreateUInt16ListAttribute(
- uint16_t type);
- static std::unique_ptr<StunErrorCodeAttribute> CreateErrorCode();
- static std::unique_ptr<StunUInt16ListAttribute> CreateUnknownAttributes();
- protected:
- StunAttribute(uint16_t type, uint16_t length);
- void SetLength(uint16_t length) { length_ = length; }
- void WritePadding(rtc::ByteBufferWriter* buf) const;
- void ConsumePadding(rtc::ByteBufferReader* buf) const;
- private:
- uint16_t type_;
- uint16_t length_;
- };
- class StunAddressAttribute : public StunAttribute {
- public:
- static const uint16_t SIZE_UNDEF = 0;
- static const uint16_t SIZE_IP4 = 8;
- static const uint16_t SIZE_IP6 = 20;
- StunAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
- StunAddressAttribute(uint16_t type, uint16_t length);
- StunAttributeValueType value_type() const override;
- StunAddressFamily family() const {
- switch (address_.ipaddr().family()) {
- case AF_INET:
- return STUN_ADDRESS_IPV4;
- case AF_INET6:
- return STUN_ADDRESS_IPV6;
- }
- return STUN_ADDRESS_UNDEF;
- }
- const rtc::SocketAddress& GetAddress() const { return address_; }
- const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
- uint16_t port() const { return address_.port(); }
- void SetAddress(const rtc::SocketAddress& addr) {
- address_ = addr;
- EnsureAddressLength();
- }
- void SetIP(const rtc::IPAddress& ip) {
- address_.SetIP(ip);
- EnsureAddressLength();
- }
- void SetPort(uint16_t port) { address_.SetPort(port); }
- bool Read(rtc::ByteBufferReader* buf) override;
- bool Write(rtc::ByteBufferWriter* buf) const override;
- private:
- void EnsureAddressLength() {
- switch (family()) {
- case STUN_ADDRESS_IPV4: {
- SetLength(SIZE_IP4);
- break;
- }
- case STUN_ADDRESS_IPV6: {
- SetLength(SIZE_IP6);
- break;
- }
- default: {
- SetLength(SIZE_UNDEF);
- break;
- }
- }
- }
- rtc::SocketAddress address_;
- };
- class StunXorAddressAttribute : public StunAddressAttribute {
- public:
- StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
- StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner);
- StunAttributeValueType value_type() const override;
- void SetOwner(StunMessage* owner) override;
- bool Read(rtc::ByteBufferReader* buf) override;
- bool Write(rtc::ByteBufferWriter* buf) const override;
- private:
- rtc::IPAddress GetXoredIP() const;
- StunMessage* owner_;
- };
- class StunUInt32Attribute : public StunAttribute {
- public:
- static const uint16_t SIZE = 4;
- StunUInt32Attribute(uint16_t type, uint32_t value);
- explicit StunUInt32Attribute(uint16_t type);
- StunAttributeValueType value_type() const override;
- uint32_t value() const { return bits_; }
- void SetValue(uint32_t bits) { bits_ = bits; }
- bool GetBit(size_t index) const;
- void SetBit(size_t index, bool value);
- bool Read(rtc::ByteBufferReader* buf) override;
- bool Write(rtc::ByteBufferWriter* buf) const override;
- private:
- uint32_t bits_;
- };
- class StunUInt64Attribute : public StunAttribute {
- public:
- static const uint16_t SIZE = 8;
- StunUInt64Attribute(uint16_t type, uint64_t value);
- explicit StunUInt64Attribute(uint16_t type);
- StunAttributeValueType value_type() const override;
- uint64_t value() const { return bits_; }
- void SetValue(uint64_t bits) { bits_ = bits; }
- bool Read(rtc::ByteBufferReader* buf) override;
- bool Write(rtc::ByteBufferWriter* buf) const override;
- private:
- uint64_t bits_;
- };
- class StunByteStringAttribute : public StunAttribute {
- public:
- explicit StunByteStringAttribute(uint16_t type);
- StunByteStringAttribute(uint16_t type, const std::string& str);
- StunByteStringAttribute(uint16_t type, const void* bytes, size_t length);
- StunByteStringAttribute(uint16_t type, uint16_t length);
- ~StunByteStringAttribute() override;
- StunAttributeValueType value_type() const override;
- const char* bytes() const { return bytes_; }
- std::string GetString() const { return std::string(bytes_, length()); }
- void CopyBytes(const char* bytes);
- void CopyBytes(const void* bytes, size_t length);
- uint8_t GetByte(size_t index) const;
- void SetByte(size_t index, uint8_t value);
- bool Read(rtc::ByteBufferReader* buf) override;
- bool Write(rtc::ByteBufferWriter* buf) const override;
- private:
- void SetBytes(char* bytes, size_t length);
- char* bytes_;
- };
- class StunErrorCodeAttribute : public StunAttribute {
- public:
- static const uint16_t MIN_SIZE;
- StunErrorCodeAttribute(uint16_t type, int code, const std::string& reason);
- StunErrorCodeAttribute(uint16_t type, uint16_t length);
- ~StunErrorCodeAttribute() override;
- StunAttributeValueType value_type() const override;
-
- int code() const;
- void SetCode(int code);
-
- int eclass() const { return class_; }
- int number() const { return number_; }
- const std::string& reason() const { return reason_; }
- void SetClass(uint8_t eclass) { class_ = eclass; }
- void SetNumber(uint8_t number) { number_ = number; }
- void SetReason(const std::string& reason);
- bool Read(rtc::ByteBufferReader* buf) override;
- bool Write(rtc::ByteBufferWriter* buf) const override;
- private:
- uint8_t class_;
- uint8_t number_;
- std::string reason_;
- };
- class StunUInt16ListAttribute : public StunAttribute {
- public:
- StunUInt16ListAttribute(uint16_t type, uint16_t length);
- ~StunUInt16ListAttribute() override;
- StunAttributeValueType value_type() const override;
- size_t Size() const;
- uint16_t GetType(int index) const;
- void SetType(int index, uint16_t value);
- void AddType(uint16_t value);
- void AddTypeAtIndex(uint16_t index, uint16_t value);
- bool Read(rtc::ByteBufferReader* buf) override;
- bool Write(rtc::ByteBufferWriter* buf) const override;
- private:
- std::vector<uint16_t>* attr_types_;
- };
- std::string StunMethodToString(int msg_type);
- int GetStunSuccessResponseType(int request_type);
- int GetStunErrorResponseType(int request_type);
- bool IsStunRequestType(int msg_type);
- bool IsStunIndicationType(int msg_type);
- bool IsStunSuccessResponseType(int msg_type);
- bool IsStunErrorResponseType(int msg_type);
- bool ComputeStunCredentialHash(const std::string& username,
- const std::string& realm,
- const std::string& password,
- std::string* hash);
- std::unique_ptr<StunAttribute> CopyStunAttribute(
- const StunAttribute& attribute,
- rtc::ByteBufferWriter* tmp_buffer_ptr = 0);
- extern const char TURN_MAGIC_COOKIE_VALUE[4];
- enum RelayMessageType {
-
-
-
-
- STUN_SEND_REQUEST = 0x0004,
- STUN_SEND_RESPONSE = 0x0104,
- STUN_SEND_ERROR_RESPONSE = 0x0114,
- STUN_DATA_INDICATION = 0x0115,
- };
- enum RelayAttributeType {
- STUN_ATTR_LIFETIME = 0x000d,
- STUN_ATTR_MAGIC_COOKIE = 0x000f,
- STUN_ATTR_BANDWIDTH = 0x0010,
- STUN_ATTR_DESTINATION_ADDRESS = 0x0011,
- STUN_ATTR_SOURCE_ADDRESS2 = 0x0012,
- STUN_ATTR_DATA = 0x0013,
- STUN_ATTR_OPTIONS = 0x8001,
- };
- class RelayMessage : public StunMessage {
- protected:
- StunAttributeValueType GetAttributeValueType(int type) const override;
- StunMessage* CreateNew() const override;
- };
- enum TurnMessageType {
- STUN_ALLOCATE_REQUEST = 0x0003,
- STUN_ALLOCATE_RESPONSE = 0x0103,
- STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
- TURN_REFRESH_REQUEST = 0x0004,
- TURN_REFRESH_RESPONSE = 0x0104,
- TURN_REFRESH_ERROR_RESPONSE = 0x0114,
- TURN_SEND_INDICATION = 0x0016,
- TURN_DATA_INDICATION = 0x0017,
- TURN_CREATE_PERMISSION_REQUEST = 0x0008,
- TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
- TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
- TURN_CHANNEL_BIND_REQUEST = 0x0009,
- TURN_CHANNEL_BIND_RESPONSE = 0x0109,
- TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
- };
- enum TurnAttributeType {
- STUN_ATTR_CHANNEL_NUMBER = 0x000C,
- STUN_ATTR_TURN_LIFETIME = 0x000d,
- STUN_ATTR_XOR_PEER_ADDRESS = 0x0012,
-
-
- STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016,
- STUN_ATTR_EVEN_PORT = 0x0018,
- STUN_ATTR_REQUESTED_TRANSPORT = 0x0019,
- STUN_ATTR_DONT_FRAGMENT = 0x001A,
- STUN_ATTR_RESERVATION_TOKEN = 0x0022,
-
-
-
- };
- enum TurnErrorType {
- STUN_ERROR_FORBIDDEN = 403,
- STUN_ERROR_ALLOCATION_MISMATCH = 437,
- STUN_ERROR_WRONG_CREDENTIALS = 441,
- STUN_ERROR_UNSUPPORTED_PROTOCOL = 442
- };
- extern const int SERVER_NOT_REACHABLE_ERROR;
- extern const char STUN_ERROR_REASON_FORBIDDEN[];
- extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
- extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
- extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
- class TurnMessage : public StunMessage {
- protected:
- StunAttributeValueType GetAttributeValueType(int type) const override;
- StunMessage* CreateNew() const override;
- };
- enum IceAttributeType {
-
- STUN_ATTR_PRIORITY = 0x0024,
- STUN_ATTR_USE_CANDIDATE = 0x0025,
- STUN_ATTR_ICE_CONTROLLED = 0x8029,
- STUN_ATTR_ICE_CONTROLLING = 0x802A,
-
-
-
-
-
-
-
-
- STUN_ATTR_NOMINATION = 0xC001,
-
-
- STUN_ATTR_GOOG_NETWORK_INFO = 0xC057,
-
- STUN_ATTR_GOOG_LAST_ICE_CHECK_RECEIVED = 0xC058,
-
- STUN_ATTR_GOOG_MISC_INFO = 0xC059,
-
- STUN_ATTR_GOOG_OBSOLETE_1 = 0xC05A,
- STUN_ATTR_GOOG_CONNECTION_ID = 0xC05B,
- STUN_ATTR_GOOG_DELTA = 0xC05C,
- STUN_ATTR_GOOG_DELTA_ACK = 0xC05D,
-
- STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 = 0xC060,
- };
- enum class IceGoogMiscInfoBindingRequestAttributeIndex {
- SUPPORT_GOOG_PING_VERSION = 0,
- };
- enum class IceGoogMiscInfoBindingResponseAttributeIndex {
- SUPPORT_GOOG_PING_VERSION = 0,
- };
- enum IceErrorCode {
- STUN_ERROR_ROLE_CONFLICT = 487,
- };
- extern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
- class IceMessage : public StunMessage {
- protected:
- StunAttributeValueType GetAttributeValueType(int type) const override;
- StunMessage* CreateNew() const override;
- };
- }
- #endif
|