blob: e8195aa5db0d1e670774b424461e446822b9c002 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_STUN_H_
12#define P2P_BASE_STUN_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14// This file contains classes for dealing with the STUN protocol, as specified
15// in RFC 5389, and its descendants.
16
Steve Anton6c38cc72017-11-29 10:25:58 -080017#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018#include <string>
19#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/basictypes.h"
22#include "rtc_base/bytebuffer.h"
23#include "rtc_base/socketaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
25namespace cricket {
26
27// These are the types of STUN messages defined in RFC 5389.
28enum StunMessageType {
29 STUN_BINDING_REQUEST = 0x0001,
30 STUN_BINDING_INDICATION = 0x0011,
31 STUN_BINDING_RESPONSE = 0x0101,
32 STUN_BINDING_ERROR_RESPONSE = 0x0111,
33};
34
35// These are all known STUN attributes, defined in RFC 5389 and elsewhere.
36// Next to each is the name of the class (T is StunTAttribute) that implements
37// that type.
38// RETRANSMIT_COUNT is the number of outstanding pings without a response at
39// the time the packet is generated.
40enum StunAttributeType {
41 STUN_ATTR_MAPPED_ADDRESS = 0x0001, // Address
42 STUN_ATTR_USERNAME = 0x0006, // ByteString
43 STUN_ATTR_MESSAGE_INTEGRITY = 0x0008, // ByteString, 20 bytes
44 STUN_ATTR_ERROR_CODE = 0x0009, // ErrorCode
45 STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000a, // UInt16List
46 STUN_ATTR_REALM = 0x0014, // ByteString
47 STUN_ATTR_NONCE = 0x0015, // ByteString
48 STUN_ATTR_XOR_MAPPED_ADDRESS = 0x0020, // XorAddress
49 STUN_ATTR_SOFTWARE = 0x8022, // ByteString
50 STUN_ATTR_ALTERNATE_SERVER = 0x8023, // Address
51 STUN_ATTR_FINGERPRINT = 0x8028, // UInt32
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000052 STUN_ATTR_ORIGIN = 0x802F, // ByteString
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053 STUN_ATTR_RETRANSMIT_COUNT = 0xFF00 // UInt32
54};
55
56// These are the types of the values associated with the attributes above.
57// This allows us to perform some basic validation when reading or adding
58// attributes. Note that these values are for our own use, and not defined in
59// RFC 5389.
60enum StunAttributeValueType {
61 STUN_VALUE_UNKNOWN = 0,
62 STUN_VALUE_ADDRESS = 1,
63 STUN_VALUE_XOR_ADDRESS = 2,
64 STUN_VALUE_UINT32 = 3,
65 STUN_VALUE_UINT64 = 4,
66 STUN_VALUE_BYTE_STRING = 5,
67 STUN_VALUE_ERROR_CODE = 6,
68 STUN_VALUE_UINT16_LIST = 7
69};
70
71// These are the types of STUN addresses defined in RFC 5389.
72enum StunAddressFamily {
73 // NB: UNDEF is not part of the STUN spec.
74 STUN_ADDRESS_UNDEF = 0,
75 STUN_ADDRESS_IPV4 = 1,
76 STUN_ADDRESS_IPV6 = 2
77};
78
79// These are the types of STUN error codes defined in RFC 5389.
80enum StunErrorCode {
81 STUN_ERROR_TRY_ALTERNATE = 300,
82 STUN_ERROR_BAD_REQUEST = 400,
83 STUN_ERROR_UNAUTHORIZED = 401,
84 STUN_ERROR_UNKNOWN_ATTRIBUTE = 420,
85 STUN_ERROR_STALE_CREDENTIALS = 430, // GICE only
86 STUN_ERROR_STALE_NONCE = 438,
87 STUN_ERROR_SERVER_ERROR = 500,
88 STUN_ERROR_GLOBAL_FAILURE = 600
89};
90
91// Strings for the error codes above.
92extern const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[];
93extern const char STUN_ERROR_REASON_BAD_REQUEST[];
94extern const char STUN_ERROR_REASON_UNAUTHORIZED[];
95extern const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[];
96extern const char STUN_ERROR_REASON_STALE_CREDENTIALS[];
97extern const char STUN_ERROR_REASON_STALE_NONCE[];
98extern const char STUN_ERROR_REASON_SERVER_ERROR[];
99
100// The mask used to determine whether a STUN message is a request/response etc.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101const uint32_t kStunTypeMask = 0x0110;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
103// STUN Attribute header length.
104const size_t kStunAttributeHeaderSize = 4;
105
106// Following values correspond to RFC5389.
107const size_t kStunHeaderSize = 20;
108const size_t kStunTransactionIdOffset = 8;
109const size_t kStunTransactionIdLength = 12;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200110const uint32_t kStunMagicCookie = 0x2112A442;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111const size_t kStunMagicCookieLength = sizeof(kStunMagicCookie);
112
113// Following value corresponds to an earlier version of STUN from
114// RFC3489.
115const size_t kStunLegacyTransactionIdLength = 16;
116
117// STUN Message Integrity HMAC length.
118const size_t kStunMessageIntegritySize = 20;
119
120class StunAttribute;
121class StunAddressAttribute;
122class StunXorAddressAttribute;
123class StunUInt32Attribute;
124class StunUInt64Attribute;
125class StunByteStringAttribute;
126class StunErrorCodeAttribute;
127class StunUInt16ListAttribute;
128
129// Records a complete STUN/TURN message. Each message consists of a type and
130// any number of attributes. Each attribute is parsed into an instance of an
131// appropriate class (see above). The Get* methods will return instances of
132// that attribute class.
133class StunMessage {
134 public:
135 StunMessage();
Steve Antonca7d54e2017-10-25 14:42:51 -0700136 virtual ~StunMessage();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137
138 int type() const { return type_; }
139 size_t length() const { return length_; }
140 const std::string& transaction_id() const { return transaction_id_; }
141
142 // Returns true if the message confirms to RFC3489 rather than
143 // RFC5389. The main difference between two version of the STUN
144 // protocol is the presence of the magic cookie and different length
145 // of transaction ID. For outgoing packets version of the protocol
146 // is determined by the lengths of the transaction ID.
147 bool IsLegacy() const;
148
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 void SetType(int type) { type_ = static_cast<uint16_t>(type); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150 bool SetTransactionID(const std::string& str);
151
152 // Gets the desired attribute value, or NULL if no such attribute type exists.
153 const StunAddressAttribute* GetAddress(int type) const;
154 const StunUInt32Attribute* GetUInt32(int type) const;
155 const StunUInt64Attribute* GetUInt64(int type) const;
156 const StunByteStringAttribute* GetByteString(int type) const;
157
158 // Gets these specific attribute values.
159 const StunErrorCodeAttribute* GetErrorCode() const;
deadbeef996fc6b2017-04-26 09:21:22 -0700160 // Returns the code inside the error code attribute, if present, and
161 // STUN_ERROR_GLOBAL_FAILURE otherwise.
162 int GetErrorCodeValue() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 const StunUInt16ListAttribute* GetUnknownAttributes() const;
164
nissecc99bc22017-02-02 01:31:30 -0800165 // Takes ownership of the specified attribute and adds it to the message.
zsteinf42cc9d2017-03-27 16:17:19 -0700166 void AddAttribute(std::unique_ptr<StunAttribute> attr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167
Jonas Oreland202994c2017-12-18 12:10:43 +0100168 // Remove the last occurrence of an attribute.
169 std::unique_ptr<StunAttribute> RemoveAttribute(int type);
170
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value.
172 // This can't currently be done on a StunMessage, since it is affected by
173 // padding data (which we discard when reading a StunMessage).
174 static bool ValidateMessageIntegrity(const char* data, size_t size,
175 const std::string& password);
176 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message.
177 bool AddMessageIntegrity(const std::string& password);
178 bool AddMessageIntegrity(const char* key, size_t keylen);
179
180 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT.
181 static bool ValidateFingerprint(const char* data, size_t size);
182
183 // Adds a FINGERPRINT attribute that is valid for the current message.
184 bool AddFingerprint();
185
186 // Parses the STUN packet in the given buffer and records it here. The
187 // return value indicates whether this was successful.
jbauchf1f87202016-03-30 06:43:37 -0700188 bool Read(rtc::ByteBufferReader* buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189
190 // Writes this object into a STUN packet. The return value indicates whether
191 // this was successful.
jbauchf1f87202016-03-30 06:43:37 -0700192 bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193
194 // Creates an empty message. Overridable by derived classes.
Steve Antonca7d54e2017-10-25 14:42:51 -0700195 virtual StunMessage* CreateNew() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196
Jonas Oreland7ca63112018-02-27 08:45:13 +0100197 // Modify the stun magic cookie used for this STUN message.
198 // This is used for testing.
199 void SetStunMagicCookie(uint32_t val);
200
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 protected:
202 // Verifies that the given attribute is allowed for this message.
203 virtual StunAttributeValueType GetAttributeValueType(int type) const;
204
205 private:
206 StunAttribute* CreateAttribute(int type, size_t length) /* const*/;
207 const StunAttribute* GetAttribute(int type) const;
208 static bool IsValidTransactionId(const std::string& transaction_id);
209
Peter Boström0c4e06b2015-10-07 12:23:21 +0200210 uint16_t type_;
211 uint16_t length_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 std::string transaction_id_;
zsteinad94c4c2017-03-06 13:36:05 -0800213 std::vector<std::unique_ptr<StunAttribute>> attrs_;
Jonas Oreland7ca63112018-02-27 08:45:13 +0100214 uint32_t stun_magic_cookie_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215};
216
217// Base class for all STUN/TURN attributes.
218class StunAttribute {
219 public:
220 virtual ~StunAttribute() {
221 }
222
223 int type() const { return type_; }
224 size_t length() const { return length_; }
225
226 // Return the type of this attribute.
227 virtual StunAttributeValueType value_type() const = 0;
228
229 // Only XorAddressAttribute needs this so far.
230 virtual void SetOwner(StunMessage* owner) {}
231
232 // Reads the body (not the type or length) for this type of attribute from
233 // the given buffer. Return value is true if successful.
jbauchf1f87202016-03-30 06:43:37 -0700234 virtual bool Read(rtc::ByteBufferReader* buf) = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235
236 // Writes the body (not the type or length) to the given buffer. Return
237 // value is true if successful.
jbauchf1f87202016-03-30 06:43:37 -0700238 virtual bool Write(rtc::ByteBufferWriter* buf) const = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239
240 // Creates an attribute object with the given type and smallest length.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200241 static StunAttribute* Create(StunAttributeValueType value_type,
242 uint16_t type,
243 uint16_t length,
244 StunMessage* owner);
Steve Anton6c38cc72017-11-29 10:25:58 -0800245 // TODO(?): Allow these create functions to take parameters, to reduce
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 // the amount of work callers need to do to initialize attributes.
zsteinf42cc9d2017-03-27 16:17:19 -0700247 static std::unique_ptr<StunAddressAttribute> CreateAddress(uint16_t type);
248 static std::unique_ptr<StunXorAddressAttribute> CreateXorAddress(
249 uint16_t type);
250 static std::unique_ptr<StunUInt32Attribute> CreateUInt32(uint16_t type);
251 static std::unique_ptr<StunUInt64Attribute> CreateUInt64(uint16_t type);
252 static std::unique_ptr<StunByteStringAttribute> CreateByteString(
253 uint16_t type);
254 static std::unique_ptr<StunErrorCodeAttribute> CreateErrorCode();
255 static std::unique_ptr<StunUInt16ListAttribute> CreateUnknownAttributes();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000256
257 protected:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258 StunAttribute(uint16_t type, uint16_t length);
259 void SetLength(uint16_t length) { length_ = length; }
jbauchf1f87202016-03-30 06:43:37 -0700260 void WritePadding(rtc::ByteBufferWriter* buf) const;
261 void ConsumePadding(rtc::ByteBufferReader* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262
263 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200264 uint16_t type_;
265 uint16_t length_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000266};
267
268// Implements STUN attributes that record an Internet address.
269class StunAddressAttribute : public StunAttribute {
270 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271 static const uint16_t SIZE_UNDEF = 0;
272 static const uint16_t SIZE_IP4 = 8;
273 static const uint16_t SIZE_IP6 = 20;
274 StunAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
275 StunAddressAttribute(uint16_t type, uint16_t length);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000276
Steve Antonca7d54e2017-10-25 14:42:51 -0700277 StunAttributeValueType value_type() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000278
279 StunAddressFamily family() const {
280 switch (address_.ipaddr().family()) {
281 case AF_INET:
282 return STUN_ADDRESS_IPV4;
283 case AF_INET6:
284 return STUN_ADDRESS_IPV6;
285 }
286 return STUN_ADDRESS_UNDEF;
287 }
288
289 const rtc::SocketAddress& GetAddress() const { return address_; }
290 const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200291 uint16_t port() const { return address_.port(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000292
293 void SetAddress(const rtc::SocketAddress& addr) {
294 address_ = addr;
295 EnsureAddressLength();
296 }
297 void SetIP(const rtc::IPAddress& ip) {
298 address_.SetIP(ip);
299 EnsureAddressLength();
300 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200301 void SetPort(uint16_t port) { address_.SetPort(port); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000302
Steve Antonca7d54e2017-10-25 14:42:51 -0700303 bool Read(rtc::ByteBufferReader* buf) override;
304 bool Write(rtc::ByteBufferWriter* buf) const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000305
306 private:
307 void EnsureAddressLength() {
308 switch (family()) {
309 case STUN_ADDRESS_IPV4: {
310 SetLength(SIZE_IP4);
311 break;
312 }
313 case STUN_ADDRESS_IPV6: {
314 SetLength(SIZE_IP6);
315 break;
316 }
317 default: {
318 SetLength(SIZE_UNDEF);
319 break;
320 }
321 }
322 }
323 rtc::SocketAddress address_;
324};
325
326// Implements STUN attributes that record an Internet address. When encoded
327// in a STUN message, the address contained in this attribute is XORed with the
328// transaction ID of the message.
329class StunXorAddressAttribute : public StunAddressAttribute {
330 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200331 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
332 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000333
Steve Antonca7d54e2017-10-25 14:42:51 -0700334 StunAttributeValueType value_type() const override;
335 void SetOwner(StunMessage* owner) override;
336 bool Read(rtc::ByteBufferReader* buf) override;
337 bool Write(rtc::ByteBufferWriter* buf) const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000338
339 private:
340 rtc::IPAddress GetXoredIP() const;
341 StunMessage* owner_;
342};
343
344// Implements STUN attributes that record a 32-bit integer.
345class StunUInt32Attribute : public StunAttribute {
346 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200347 static const uint16_t SIZE = 4;
348 StunUInt32Attribute(uint16_t type, uint32_t value);
349 explicit StunUInt32Attribute(uint16_t type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000350
Steve Antonca7d54e2017-10-25 14:42:51 -0700351 StunAttributeValueType value_type() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000352
Peter Boström0c4e06b2015-10-07 12:23:21 +0200353 uint32_t value() const { return bits_; }
354 void SetValue(uint32_t bits) { bits_ = bits; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000355
356 bool GetBit(size_t index) const;
357 void SetBit(size_t index, bool value);
358
Steve Antonca7d54e2017-10-25 14:42:51 -0700359 bool Read(rtc::ByteBufferReader* buf) override;
360 bool Write(rtc::ByteBufferWriter* buf) const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000361
362 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200363 uint32_t bits_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364};
365
366class StunUInt64Attribute : public StunAttribute {
367 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200368 static const uint16_t SIZE = 8;
369 StunUInt64Attribute(uint16_t type, uint64_t value);
370 explicit StunUInt64Attribute(uint16_t type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371
Steve Antonca7d54e2017-10-25 14:42:51 -0700372 StunAttributeValueType value_type() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000373
Peter Boström0c4e06b2015-10-07 12:23:21 +0200374 uint64_t value() const { return bits_; }
375 void SetValue(uint64_t bits) { bits_ = bits; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000376
Steve Antonca7d54e2017-10-25 14:42:51 -0700377 bool Read(rtc::ByteBufferReader* buf) override;
378 bool Write(rtc::ByteBufferWriter* buf) const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000379
380 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200381 uint64_t bits_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000382};
383
384// Implements STUN attributes that record an arbitrary byte string.
385class StunByteStringAttribute : public StunAttribute {
386 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200387 explicit StunByteStringAttribute(uint16_t type);
388 StunByteStringAttribute(uint16_t type, const std::string& str);
389 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length);
390 StunByteStringAttribute(uint16_t type, uint16_t length);
Steve Antonca7d54e2017-10-25 14:42:51 -0700391 ~StunByteStringAttribute() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000392
Steve Antonca7d54e2017-10-25 14:42:51 -0700393 StunAttributeValueType value_type() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394
395 const char* bytes() const { return bytes_; }
396 std::string GetString() const { return std::string(bytes_, length()); }
397
398 void CopyBytes(const char* bytes); // uses strlen
399 void CopyBytes(const void* bytes, size_t length);
400
Peter Boström0c4e06b2015-10-07 12:23:21 +0200401 uint8_t GetByte(size_t index) const;
402 void SetByte(size_t index, uint8_t value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000403
Steve Antonca7d54e2017-10-25 14:42:51 -0700404 bool Read(rtc::ByteBufferReader* buf) override;
405 bool Write(rtc::ByteBufferWriter* buf) const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000406
407 private:
408 void SetBytes(char* bytes, size_t length);
409
410 char* bytes_;
411};
412
413// Implements STUN attributes that record an error code.
414class StunErrorCodeAttribute : public StunAttribute {
415 public:
zsteinf42cc9d2017-03-27 16:17:19 -0700416 static const uint16_t MIN_SIZE;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200417 StunErrorCodeAttribute(uint16_t type, int code, const std::string& reason);
418 StunErrorCodeAttribute(uint16_t type, uint16_t length);
Steve Antonca7d54e2017-10-25 14:42:51 -0700419 ~StunErrorCodeAttribute() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000420
Steve Antonca7d54e2017-10-25 14:42:51 -0700421 StunAttributeValueType value_type() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000422
423 // The combined error and class, e.g. 0x400.
424 int code() const;
425 void SetCode(int code);
426
427 // The individual error components.
428 int eclass() const { return class_; }
429 int number() const { return number_; }
430 const std::string& reason() const { return reason_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200431 void SetClass(uint8_t eclass) { class_ = eclass; }
432 void SetNumber(uint8_t number) { number_ = number; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000433 void SetReason(const std::string& reason);
434
Steve Antonca7d54e2017-10-25 14:42:51 -0700435 bool Read(rtc::ByteBufferReader* buf) override;
436 bool Write(rtc::ByteBufferWriter* buf) const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000437
438 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200439 uint8_t class_;
440 uint8_t number_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000441 std::string reason_;
442};
443
444// Implements STUN attributes that record a list of attribute names.
445class StunUInt16ListAttribute : public StunAttribute {
446 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200447 StunUInt16ListAttribute(uint16_t type, uint16_t length);
Steve Antonca7d54e2017-10-25 14:42:51 -0700448 ~StunUInt16ListAttribute() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000449
Steve Antonca7d54e2017-10-25 14:42:51 -0700450 StunAttributeValueType value_type() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000451
452 size_t Size() const;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200453 uint16_t GetType(int index) const;
454 void SetType(int index, uint16_t value);
455 void AddType(uint16_t value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000456
Steve Antonca7d54e2017-10-25 14:42:51 -0700457 bool Read(rtc::ByteBufferReader* buf) override;
458 bool Write(rtc::ByteBufferWriter* buf) const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000459
460 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200461 std::vector<uint16_t>* attr_types_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000462};
463
464// Returns the (successful) response type for the given request type.
465// Returns -1 if |request_type| is not a valid request type.
466int GetStunSuccessResponseType(int request_type);
467
468// Returns the error response type for the given request type.
469// Returns -1 if |request_type| is not a valid request type.
470int GetStunErrorResponseType(int request_type);
471
472// Returns whether a given message is a request type.
473bool IsStunRequestType(int msg_type);
474
475// Returns whether a given message is an indication type.
476bool IsStunIndicationType(int msg_type);
477
478// Returns whether a given response is a success type.
479bool IsStunSuccessResponseType(int msg_type);
480
481// Returns whether a given response is an error type.
482bool IsStunErrorResponseType(int msg_type);
483
484// Computes the STUN long-term credential hash.
485bool ComputeStunCredentialHash(const std::string& username,
486 const std::string& realm, const std::string& password, std::string* hash);
487
Jonas Oreland202994c2017-12-18 12:10:43 +0100488// Make a copy af |attribute| and return a new StunAttribute.
489// This is useful if you don't care about what kind of attribute you
490// are handling.
491//
492// The implementation copies by calling Write() followed by Read().
493//
494// If |tmp_buffer| is supplied this buffer will be used, otherwise
495// a buffer will created in the method.
496std::unique_ptr<StunAttribute> CopyStunAttribute(
497 const StunAttribute& attribute,
498 rtc::ByteBufferWriter* tmp_buffer_ptr = 0);
499
Steve Anton6c38cc72017-11-29 10:25:58 -0800500// TODO(?): Move the TURN/ICE stuff below out to separate files.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000501extern const char TURN_MAGIC_COOKIE_VALUE[4];
502
503// "GTURN" STUN methods.
Steve Anton6c38cc72017-11-29 10:25:58 -0800504// TODO(?): Rename these methods to GTURN_ to make it clear they aren't
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000505// part of standard STUN/TURN.
506enum RelayMessageType {
507 // For now, using the same defs from TurnMessageType below.
508 // STUN_ALLOCATE_REQUEST = 0x0003,
509 // STUN_ALLOCATE_RESPONSE = 0x0103,
510 // STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
511 STUN_SEND_REQUEST = 0x0004,
512 STUN_SEND_RESPONSE = 0x0104,
513 STUN_SEND_ERROR_RESPONSE = 0x0114,
514 STUN_DATA_INDICATION = 0x0115,
515};
516
517// "GTURN"-specific STUN attributes.
Steve Anton6c38cc72017-11-29 10:25:58 -0800518// TODO(?): Rename these attributes to GTURN_ to avoid conflicts.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000519enum RelayAttributeType {
520 STUN_ATTR_LIFETIME = 0x000d, // UInt32
521 STUN_ATTR_MAGIC_COOKIE = 0x000f, // ByteString, 4 bytes
522 STUN_ATTR_BANDWIDTH = 0x0010, // UInt32
523 STUN_ATTR_DESTINATION_ADDRESS = 0x0011, // Address
524 STUN_ATTR_SOURCE_ADDRESS2 = 0x0012, // Address
525 STUN_ATTR_DATA = 0x0013, // ByteString
526 STUN_ATTR_OPTIONS = 0x8001, // UInt32
527};
528
529// A "GTURN" STUN message.
530class RelayMessage : public StunMessage {
531 protected:
Steve Antonca7d54e2017-10-25 14:42:51 -0700532 StunAttributeValueType GetAttributeValueType(int type) const override;
533 StunMessage* CreateNew() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000534};
535
536// Defined in TURN RFC 5766.
537enum TurnMessageType {
538 STUN_ALLOCATE_REQUEST = 0x0003,
539 STUN_ALLOCATE_RESPONSE = 0x0103,
540 STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
541 TURN_REFRESH_REQUEST = 0x0004,
542 TURN_REFRESH_RESPONSE = 0x0104,
543 TURN_REFRESH_ERROR_RESPONSE = 0x0114,
544 TURN_SEND_INDICATION = 0x0016,
545 TURN_DATA_INDICATION = 0x0017,
546 TURN_CREATE_PERMISSION_REQUEST = 0x0008,
547 TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
548 TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
549 TURN_CHANNEL_BIND_REQUEST = 0x0009,
550 TURN_CHANNEL_BIND_RESPONSE = 0x0109,
551 TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
552};
553
554enum TurnAttributeType {
555 STUN_ATTR_CHANNEL_NUMBER = 0x000C, // UInt32
556 STUN_ATTR_TURN_LIFETIME = 0x000d, // UInt32
557 STUN_ATTR_XOR_PEER_ADDRESS = 0x0012, // XorAddress
558 // TODO(mallinath) - Uncomment after RelayAttributes are renamed.
559 // STUN_ATTR_DATA = 0x0013, // ByteString
560 STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016, // XorAddress
561 STUN_ATTR_EVEN_PORT = 0x0018, // ByteString, 1 byte.
562 STUN_ATTR_REQUESTED_TRANSPORT = 0x0019, // UInt32
563 STUN_ATTR_DONT_FRAGMENT = 0x001A, // No content, Length = 0
564 STUN_ATTR_RESERVATION_TOKEN = 0x0022, // ByteString, 8 bytes.
565 // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and
566 // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes
567 // by appending G to attribute name.
568};
569
570// RFC 5766-defined errors.
571enum TurnErrorType {
572 STUN_ERROR_FORBIDDEN = 403,
573 STUN_ERROR_ALLOCATION_MISMATCH = 437,
574 STUN_ERROR_WRONG_CREDENTIALS = 441,
575 STUN_ERROR_UNSUPPORTED_PROTOCOL = 442
576};
577extern const char STUN_ERROR_REASON_FORBIDDEN[];
578extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
579extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
580extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
581class TurnMessage : public StunMessage {
582 protected:
Steve Antonca7d54e2017-10-25 14:42:51 -0700583 StunAttributeValueType GetAttributeValueType(int type) const override;
584 StunMessage* CreateNew() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000585};
586
587// RFC 5245 ICE STUN attributes.
588enum IceAttributeType {
honghaize1a0c942016-02-16 14:54:56 -0800589 STUN_ATTR_PRIORITY = 0x0024, // UInt32
590 STUN_ATTR_USE_CANDIDATE = 0x0025, // No content, Length = 0
591 STUN_ATTR_ICE_CONTROLLED = 0x8029, // UInt64
592 STUN_ATTR_ICE_CONTROLLING = 0x802A, // UInt64
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700593 STUN_ATTR_NOMINATION = 0xC001, // UInt32
honghaiza0c44ea2016-03-23 16:07:48 -0700594 // UInt32. The higher 16 bits are the network ID. The lower 16 bits are the
595 // network cost.
596 STUN_ATTR_NETWORK_INFO = 0xC057
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000597};
598
599// RFC 5245-defined errors.
600enum IceErrorCode {
601 STUN_ERROR_ROLE_CONFLICT = 487,
602};
603extern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
604
605// A RFC 5245 ICE STUN message.
606class IceMessage : public StunMessage {
607 protected:
Steve Antonca7d54e2017-10-25 14:42:51 -0700608 StunAttributeValueType GetAttributeValueType(int type) const override;
609 StunMessage* CreateNew() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000610};
611
612} // namespace cricket
613
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200614#endif // P2P_BASE_STUN_H_