blob: 682a17a945d4fe4d4728e8aaeb87ba885c3aaec7 [file] [log] [blame]
Patrik Höglund56d94522019-11-18 15:53:32 +01001/*
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
11#ifndef API_TRANSPORT_STUN_H_
12#define API_TRANSPORT_STUN_H_
13
14// This file contains classes for dealing with the STUN protocol, as specified
15// in RFC 5389, and its descendants.
16
17#include <stddef.h>
18#include <stdint.h>
Harald Alvestrand07d83c82021-03-02 08:09:53 +000019
Harald Alvestrandbee64082020-11-12 11:17:41 +000020#include <functional>
Patrik Höglund56d94522019-11-18 15:53:32 +010021#include <memory>
22#include <string>
23#include <vector>
24
Harald Alvestrandbee64082020-11-12 11:17:41 +000025#include "absl/strings/string_view.h"
26#include "api/array_view.h"
Patrik Höglund56d94522019-11-18 15:53:32 +010027#include "rtc_base/byte_buffer.h"
28#include "rtc_base/ip_address.h"
29#include "rtc_base/socket_address.h"
30
31namespace cricket {
32
33// These are the types of STUN messages defined in RFC 5389.
34enum StunMessageType {
35 STUN_BINDING_REQUEST = 0x0001,
36 STUN_BINDING_INDICATION = 0x0011,
37 STUN_BINDING_RESPONSE = 0x0101,
38 STUN_BINDING_ERROR_RESPONSE = 0x0111,
Jonas Oreland63737a92019-11-21 15:12:14 +010039
Jonas Oreland9a52bd72019-12-11 11:35:48 +010040 // Method 0x80, GOOG-PING is a variant of STUN BINDING
41 // that is sent instead of a STUN BINDING if the binding
42 // was identical to the one before.
Jonas Oreland63737a92019-11-21 15:12:14 +010043 GOOG_PING_REQUEST = 0x200,
44 GOOG_PING_RESPONSE = 0x300,
45 GOOG_PING_ERROR_RESPONSE = 0x310,
Patrik Höglund56d94522019-11-18 15:53:32 +010046};
47
48// These are all known STUN attributes, defined in RFC 5389 and elsewhere.
49// Next to each is the name of the class (T is StunTAttribute) that implements
50// that type.
51// RETRANSMIT_COUNT is the number of outstanding pings without a response at
52// the time the packet is generated.
53enum StunAttributeType {
54 STUN_ATTR_MAPPED_ADDRESS = 0x0001, // Address
55 STUN_ATTR_USERNAME = 0x0006, // ByteString
56 STUN_ATTR_MESSAGE_INTEGRITY = 0x0008, // ByteString, 20 bytes
57 STUN_ATTR_ERROR_CODE = 0x0009, // ErrorCode
58 STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000a, // UInt16List
59 STUN_ATTR_REALM = 0x0014, // ByteString
60 STUN_ATTR_NONCE = 0x0015, // ByteString
61 STUN_ATTR_XOR_MAPPED_ADDRESS = 0x0020, // XorAddress
62 STUN_ATTR_SOFTWARE = 0x8022, // ByteString
63 STUN_ATTR_ALTERNATE_SERVER = 0x8023, // Address
64 STUN_ATTR_FINGERPRINT = 0x8028, // UInt32
65 STUN_ATTR_ORIGIN = 0x802F, // ByteString
66 STUN_ATTR_RETRANSMIT_COUNT = 0xFF00 // UInt32
67};
68
69// These are the types of the values associated with the attributes above.
70// This allows us to perform some basic validation when reading or adding
71// attributes. Note that these values are for our own use, and not defined in
72// RFC 5389.
73enum StunAttributeValueType {
74 STUN_VALUE_UNKNOWN = 0,
75 STUN_VALUE_ADDRESS = 1,
76 STUN_VALUE_XOR_ADDRESS = 2,
77 STUN_VALUE_UINT32 = 3,
78 STUN_VALUE_UINT64 = 4,
79 STUN_VALUE_BYTE_STRING = 5,
80 STUN_VALUE_ERROR_CODE = 6,
81 STUN_VALUE_UINT16_LIST = 7
82};
83
84// These are the types of STUN addresses defined in RFC 5389.
85enum StunAddressFamily {
86 // NB: UNDEF is not part of the STUN spec.
87 STUN_ADDRESS_UNDEF = 0,
88 STUN_ADDRESS_IPV4 = 1,
89 STUN_ADDRESS_IPV6 = 2
90};
91
92// These are the types of STUN error codes defined in RFC 5389.
93enum StunErrorCode {
94 STUN_ERROR_TRY_ALTERNATE = 300,
95 STUN_ERROR_BAD_REQUEST = 400,
96 STUN_ERROR_UNAUTHORIZED = 401,
97 STUN_ERROR_UNKNOWN_ATTRIBUTE = 420,
98 STUN_ERROR_STALE_CREDENTIALS = 430, // GICE only
99 STUN_ERROR_STALE_NONCE = 438,
100 STUN_ERROR_SERVER_ERROR = 500,
101 STUN_ERROR_GLOBAL_FAILURE = 600
102};
103
104// Strings for the error codes above.
105extern const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[];
106extern const char STUN_ERROR_REASON_BAD_REQUEST[];
107extern const char STUN_ERROR_REASON_UNAUTHORIZED[];
108extern const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[];
109extern const char STUN_ERROR_REASON_STALE_CREDENTIALS[];
110extern const char STUN_ERROR_REASON_STALE_NONCE[];
111extern const char STUN_ERROR_REASON_SERVER_ERROR[];
112
113// The mask used to determine whether a STUN message is a request/response etc.
114const uint32_t kStunTypeMask = 0x0110;
115
116// STUN Attribute header length.
117const size_t kStunAttributeHeaderSize = 4;
118
119// Following values correspond to RFC5389.
120const size_t kStunHeaderSize = 20;
121const size_t kStunTransactionIdOffset = 8;
122const size_t kStunTransactionIdLength = 12;
123const uint32_t kStunMagicCookie = 0x2112A442;
124constexpr size_t kStunMagicCookieLength = sizeof(kStunMagicCookie);
125
126// Following value corresponds to an earlier version of STUN from
127// RFC3489.
128const size_t kStunLegacyTransactionIdLength = 16;
129
130// STUN Message Integrity HMAC length.
131const size_t kStunMessageIntegritySize = 20;
Jonas Oreland63737a92019-11-21 15:12:14 +0100132// Size of STUN_ATTR_MESSAGE_INTEGRITY_32
133const size_t kStunMessageIntegrity32Size = 4;
Patrik Höglund56d94522019-11-18 15:53:32 +0100134
135class StunAddressAttribute;
136class StunAttribute;
137class StunByteStringAttribute;
138class StunErrorCodeAttribute;
Patrik Höglund56d94522019-11-18 15:53:32 +0100139class StunUInt16ListAttribute;
140class StunUInt32Attribute;
141class StunUInt64Attribute;
142class StunXorAddressAttribute;
143
144// Records a complete STUN/TURN message. Each message consists of a type and
145// any number of attributes. Each attribute is parsed into an instance of an
146// appropriate class (see above). The Get* methods will return instances of
147// that attribute class.
148class StunMessage {
149 public:
150 StunMessage();
151 virtual ~StunMessage();
152
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000153 // The verification status of the message. This is checked on parsing,
154 // or set by AddMessageIntegrity.
155 enum class IntegrityStatus {
156 kNotSet,
157 kNoIntegrity, // Message-integrity attribute missing
158 kIntegrityOk, // Message-integrity checked OK
159 kIntegrityBad, // Message-integrity verification failed
160 };
161
Patrik Höglund56d94522019-11-18 15:53:32 +0100162 int type() const { return type_; }
163 size_t length() const { return length_; }
164 const std::string& transaction_id() const { return transaction_id_; }
165 uint32_t reduced_transaction_id() const { return reduced_transaction_id_; }
166
167 // Returns true if the message confirms to RFC3489 rather than
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000168 // RFC5389. The main difference between the two versions of the STUN
Patrik Höglund56d94522019-11-18 15:53:32 +0100169 // protocol is the presence of the magic cookie and different length
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000170 // of transaction ID. For outgoing packets the version of the protocol
Patrik Höglund56d94522019-11-18 15:53:32 +0100171 // is determined by the lengths of the transaction ID.
172 bool IsLegacy() const;
173
174 void SetType(int type) { type_ = static_cast<uint16_t>(type); }
175 bool SetTransactionID(const std::string& str);
176
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700177 // Get a list of all of the attribute types in the "comprehension required"
178 // range that were not recognized.
179 std::vector<uint16_t> GetNonComprehendedAttributes() const;
180
Patrik Höglund56d94522019-11-18 15:53:32 +0100181 // Gets the desired attribute value, or NULL if no such attribute type exists.
182 const StunAddressAttribute* GetAddress(int type) const;
183 const StunUInt32Attribute* GetUInt32(int type) const;
184 const StunUInt64Attribute* GetUInt64(int type) const;
185 const StunByteStringAttribute* GetByteString(int type) const;
Jonas Oreland1721de12019-11-20 12:10:39 +0100186 const StunUInt16ListAttribute* GetUInt16List(int type) const;
Patrik Höglund56d94522019-11-18 15:53:32 +0100187
188 // Gets these specific attribute values.
189 const StunErrorCodeAttribute* GetErrorCode() const;
190 // Returns the code inside the error code attribute, if present, and
191 // STUN_ERROR_GLOBAL_FAILURE otherwise.
192 int GetErrorCodeValue() const;
193 const StunUInt16ListAttribute* GetUnknownAttributes() const;
194
195 // Takes ownership of the specified attribute and adds it to the message.
196 void AddAttribute(std::unique_ptr<StunAttribute> attr);
197
198 // Remove the last occurrence of an attribute.
199 std::unique_ptr<StunAttribute> RemoveAttribute(int type);
200
Jonas Oreland63737a92019-11-21 15:12:14 +0100201 // Remote all attributes and releases them.
202 void ClearAttributes();
203
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000204 // Validates that a STUN message has a correct MESSAGE-INTEGRITY value.
205 // This uses the buffered raw-format message stored by Read().
206 IntegrityStatus ValidateMessageIntegrity(const std::string& password);
207
208 // Returns the current integrity status of the message.
209 IntegrityStatus integrity() const { return integrity_; }
210
211 // Shortcut for checking if integrity is verified.
212 bool IntegrityOk() const {
213 return integrity_ == IntegrityStatus::kIntegrityOk;
214 }
215
216 // Returns the password attribute used to set or check the integrity.
217 // Can only be called after adding or checking the integrity.
218 std::string password() const {
219 RTC_DCHECK(integrity_ != IntegrityStatus::kNotSet);
220 return password_;
221 }
Jonas Oreland63737a92019-11-21 15:12:14 +0100222
Patrik Höglund56d94522019-11-18 15:53:32 +0100223 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message.
224 bool AddMessageIntegrity(const std::string& password);
Patrik Höglund56d94522019-11-18 15:53:32 +0100225
Jonas Oreland63737a92019-11-21 15:12:14 +0100226 // Adds a STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 attribute that is valid for the
227 // current message.
228 bool AddMessageIntegrity32(absl::string_view password);
229
Jonas Oreland253d50f2019-11-28 17:08:07 +0100230 // Verify that a buffer has stun magic cookie and one of the specified
231 // methods. Note that it does not check for the existance of FINGERPRINT.
232 static bool IsStunMethod(rtc::ArrayView<int> methods,
233 const char* data,
234 size_t size);
235
Patrik Höglund56d94522019-11-18 15:53:32 +0100236 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT.
237 static bool ValidateFingerprint(const char* data, size_t size);
238
239 // Adds a FINGERPRINT attribute that is valid for the current message.
240 bool AddFingerprint();
241
242 // Parses the STUN packet in the given buffer and records it here. The
243 // return value indicates whether this was successful.
244 bool Read(rtc::ByteBufferReader* buf);
245
246 // Writes this object into a STUN packet. The return value indicates whether
247 // this was successful.
248 bool Write(rtc::ByteBufferWriter* buf) const;
249
250 // Creates an empty message. Overridable by derived classes.
251 virtual StunMessage* CreateNew() const;
252
253 // Modify the stun magic cookie used for this STUN message.
254 // This is used for testing.
255 void SetStunMagicCookie(uint32_t val);
256
Jonas Oreland253d50f2019-11-28 17:08:07 +0100257 // Contruct a copy of |this|.
258 std::unique_ptr<StunMessage> Clone() const;
259
260 // Check if the attributes of this StunMessage equals those of |other|
261 // for all attributes that |attribute_type_mask| return true
262 bool EqualAttributes(const StunMessage* other,
263 std::function<bool(int type)> attribute_type_mask) const;
264
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000265 // Expose raw-buffer ValidateMessageIntegrity function for testing.
266 static bool ValidateMessageIntegrityForTesting(const char* data,
267 size_t size,
268 const std::string& password) {
269 return ValidateMessageIntegrity(data, size, password);
270 }
271 // Expose raw-buffer ValidateMessageIntegrity function for testing.
272 static bool ValidateMessageIntegrity32ForTesting(
273 const char* data,
274 size_t size,
275 const std::string& password) {
276 return ValidateMessageIntegrity32(data, size, password);
277 }
278 // Validates that a STUN message in byte buffer form
279 // has a correct MESSAGE-INTEGRITY value.
280 // These functions are not recommended and will be deprecated; use
281 // ValidateMessageIntegrity(password) on the parsed form instead.
282 static bool ValidateMessageIntegrity(const char* data,
283 size_t size,
284 const std::string& password);
285 static bool ValidateMessageIntegrity32(const char* data,
286 size_t size,
287 const std::string& password);
288
Patrik Höglund56d94522019-11-18 15:53:32 +0100289 protected:
290 // Verifies that the given attribute is allowed for this message.
291 virtual StunAttributeValueType GetAttributeValueType(int type) const;
292
Jonas Oreland253d50f2019-11-28 17:08:07 +0100293 std::vector<std::unique_ptr<StunAttribute>> attrs_;
294
Patrik Höglund56d94522019-11-18 15:53:32 +0100295 private:
296 StunAttribute* CreateAttribute(int type, size_t length) /* const*/;
297 const StunAttribute* GetAttribute(int type) const;
298 static bool IsValidTransactionId(const std::string& transaction_id);
Jonas Oreland63737a92019-11-21 15:12:14 +0100299 bool AddMessageIntegrityOfType(int mi_attr_type,
300 size_t mi_attr_size,
301 const char* key,
302 size_t keylen);
303 static bool ValidateMessageIntegrityOfType(int mi_attr_type,
304 size_t mi_attr_size,
305 const char* data,
306 size_t size,
307 const std::string& password);
Patrik Höglund56d94522019-11-18 15:53:32 +0100308
309 uint16_t type_;
310 uint16_t length_;
311 std::string transaction_id_;
312 uint32_t reduced_transaction_id_;
Patrik Höglund56d94522019-11-18 15:53:32 +0100313 uint32_t stun_magic_cookie_;
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000314 // The original buffer for messages created by Read().
315 std::string buffer_;
316 IntegrityStatus integrity_ = IntegrityStatus::kNotSet;
317 std::string password_;
Patrik Höglund56d94522019-11-18 15:53:32 +0100318};
319
320// Base class for all STUN/TURN attributes.
321class StunAttribute {
322 public:
323 virtual ~StunAttribute() {}
324
325 int type() const { return type_; }
326 size_t length() const { return length_; }
327
328 // Return the type of this attribute.
329 virtual StunAttributeValueType value_type() const = 0;
330
331 // Only XorAddressAttribute needs this so far.
332 virtual void SetOwner(StunMessage* owner) {}
333
334 // Reads the body (not the type or length) for this type of attribute from
335 // the given buffer. Return value is true if successful.
336 virtual bool Read(rtc::ByteBufferReader* buf) = 0;
337
338 // Writes the body (not the type or length) to the given buffer. Return
339 // value is true if successful.
340 virtual bool Write(rtc::ByteBufferWriter* buf) const = 0;
341
342 // Creates an attribute object with the given type and smallest length.
343 static StunAttribute* Create(StunAttributeValueType value_type,
344 uint16_t type,
345 uint16_t length,
346 StunMessage* owner);
347 // TODO(?): Allow these create functions to take parameters, to reduce
348 // the amount of work callers need to do to initialize attributes.
349 static std::unique_ptr<StunAddressAttribute> CreateAddress(uint16_t type);
350 static std::unique_ptr<StunXorAddressAttribute> CreateXorAddress(
351 uint16_t type);
352 static std::unique_ptr<StunUInt32Attribute> CreateUInt32(uint16_t type);
353 static std::unique_ptr<StunUInt64Attribute> CreateUInt64(uint16_t type);
354 static std::unique_ptr<StunByteStringAttribute> CreateByteString(
355 uint16_t type);
Jonas Oreland1721de12019-11-20 12:10:39 +0100356 static std::unique_ptr<StunUInt16ListAttribute> CreateUInt16ListAttribute(
357 uint16_t type);
Patrik Höglund56d94522019-11-18 15:53:32 +0100358 static std::unique_ptr<StunErrorCodeAttribute> CreateErrorCode();
359 static std::unique_ptr<StunUInt16ListAttribute> CreateUnknownAttributes();
360
361 protected:
362 StunAttribute(uint16_t type, uint16_t length);
363 void SetLength(uint16_t length) { length_ = length; }
364 void WritePadding(rtc::ByteBufferWriter* buf) const;
365 void ConsumePadding(rtc::ByteBufferReader* buf) const;
366
367 private:
368 uint16_t type_;
369 uint16_t length_;
370};
371
372// Implements STUN attributes that record an Internet address.
373class StunAddressAttribute : public StunAttribute {
374 public:
375 static const uint16_t SIZE_UNDEF = 0;
376 static const uint16_t SIZE_IP4 = 8;
377 static const uint16_t SIZE_IP6 = 20;
378 StunAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
379 StunAddressAttribute(uint16_t type, uint16_t length);
380
381 StunAttributeValueType value_type() const override;
382
383 StunAddressFamily family() const {
384 switch (address_.ipaddr().family()) {
385 case AF_INET:
386 return STUN_ADDRESS_IPV4;
387 case AF_INET6:
388 return STUN_ADDRESS_IPV6;
389 }
390 return STUN_ADDRESS_UNDEF;
391 }
392
393 const rtc::SocketAddress& GetAddress() const { return address_; }
394 const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
395 uint16_t port() const { return address_.port(); }
396
397 void SetAddress(const rtc::SocketAddress& addr) {
398 address_ = addr;
399 EnsureAddressLength();
400 }
401 void SetIP(const rtc::IPAddress& ip) {
402 address_.SetIP(ip);
403 EnsureAddressLength();
404 }
405 void SetPort(uint16_t port) { address_.SetPort(port); }
406
407 bool Read(rtc::ByteBufferReader* buf) override;
408 bool Write(rtc::ByteBufferWriter* buf) const override;
409
410 private:
411 void EnsureAddressLength() {
412 switch (family()) {
413 case STUN_ADDRESS_IPV4: {
414 SetLength(SIZE_IP4);
415 break;
416 }
417 case STUN_ADDRESS_IPV6: {
418 SetLength(SIZE_IP6);
419 break;
420 }
421 default: {
422 SetLength(SIZE_UNDEF);
423 break;
424 }
425 }
426 }
427 rtc::SocketAddress address_;
428};
429
430// Implements STUN attributes that record an Internet address. When encoded
431// in a STUN message, the address contained in this attribute is XORed with the
432// transaction ID of the message.
433class StunXorAddressAttribute : public StunAddressAttribute {
434 public:
435 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
436 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner);
437
438 StunAttributeValueType value_type() const override;
439 void SetOwner(StunMessage* owner) override;
440 bool Read(rtc::ByteBufferReader* buf) override;
441 bool Write(rtc::ByteBufferWriter* buf) const override;
442
443 private:
444 rtc::IPAddress GetXoredIP() const;
445 StunMessage* owner_;
446};
447
448// Implements STUN attributes that record a 32-bit integer.
449class StunUInt32Attribute : public StunAttribute {
450 public:
451 static const uint16_t SIZE = 4;
452 StunUInt32Attribute(uint16_t type, uint32_t value);
453 explicit StunUInt32Attribute(uint16_t type);
454
455 StunAttributeValueType value_type() const override;
456
457 uint32_t value() const { return bits_; }
458 void SetValue(uint32_t bits) { bits_ = bits; }
459
460 bool GetBit(size_t index) const;
461 void SetBit(size_t index, bool value);
462
463 bool Read(rtc::ByteBufferReader* buf) override;
464 bool Write(rtc::ByteBufferWriter* buf) const override;
465
466 private:
467 uint32_t bits_;
468};
469
470class StunUInt64Attribute : public StunAttribute {
471 public:
472 static const uint16_t SIZE = 8;
473 StunUInt64Attribute(uint16_t type, uint64_t value);
474 explicit StunUInt64Attribute(uint16_t type);
475
476 StunAttributeValueType value_type() const override;
477
478 uint64_t value() const { return bits_; }
479 void SetValue(uint64_t bits) { bits_ = bits; }
480
481 bool Read(rtc::ByteBufferReader* buf) override;
482 bool Write(rtc::ByteBufferWriter* buf) const override;
483
484 private:
485 uint64_t bits_;
486};
487
488// Implements STUN attributes that record an arbitrary byte string.
489class StunByteStringAttribute : public StunAttribute {
490 public:
491 explicit StunByteStringAttribute(uint16_t type);
492 StunByteStringAttribute(uint16_t type, const std::string& str);
493 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length);
494 StunByteStringAttribute(uint16_t type, uint16_t length);
495 ~StunByteStringAttribute() override;
496
497 StunAttributeValueType value_type() const override;
498
499 const char* bytes() const { return bytes_; }
500 std::string GetString() const { return std::string(bytes_, length()); }
501
502 void CopyBytes(const char* bytes); // uses strlen
503 void CopyBytes(const void* bytes, size_t length);
504
505 uint8_t GetByte(size_t index) const;
506 void SetByte(size_t index, uint8_t value);
507
508 bool Read(rtc::ByteBufferReader* buf) override;
509 bool Write(rtc::ByteBufferWriter* buf) const override;
510
511 private:
512 void SetBytes(char* bytes, size_t length);
513
514 char* bytes_;
515};
516
517// Implements STUN attributes that record an error code.
518class StunErrorCodeAttribute : public StunAttribute {
519 public:
520 static const uint16_t MIN_SIZE;
521 StunErrorCodeAttribute(uint16_t type, int code, const std::string& reason);
522 StunErrorCodeAttribute(uint16_t type, uint16_t length);
523 ~StunErrorCodeAttribute() override;
524
525 StunAttributeValueType value_type() const override;
526
527 // The combined error and class, e.g. 0x400.
528 int code() const;
529 void SetCode(int code);
530
531 // The individual error components.
532 int eclass() const { return class_; }
533 int number() const { return number_; }
534 const std::string& reason() const { return reason_; }
535 void SetClass(uint8_t eclass) { class_ = eclass; }
536 void SetNumber(uint8_t number) { number_ = number; }
537 void SetReason(const std::string& reason);
538
539 bool Read(rtc::ByteBufferReader* buf) override;
540 bool Write(rtc::ByteBufferWriter* buf) const override;
541
542 private:
543 uint8_t class_;
544 uint8_t number_;
545 std::string reason_;
546};
547
548// Implements STUN attributes that record a list of attribute names.
549class StunUInt16ListAttribute : public StunAttribute {
550 public:
551 StunUInt16ListAttribute(uint16_t type, uint16_t length);
552 ~StunUInt16ListAttribute() override;
553
554 StunAttributeValueType value_type() const override;
555
556 size_t Size() const;
557 uint16_t GetType(int index) const;
558 void SetType(int index, uint16_t value);
559 void AddType(uint16_t value);
Jonas Oreland63737a92019-11-21 15:12:14 +0100560 void AddTypeAtIndex(uint16_t index, uint16_t value);
Patrik Höglund56d94522019-11-18 15:53:32 +0100561
562 bool Read(rtc::ByteBufferReader* buf) override;
563 bool Write(rtc::ByteBufferWriter* buf) const override;
564
565 private:
566 std::vector<uint16_t>* attr_types_;
567};
568
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100569// Return a string e.g "STUN BINDING request".
570std::string StunMethodToString(int msg_type);
571
Patrik Höglund56d94522019-11-18 15:53:32 +0100572// Returns the (successful) response type for the given request type.
573// Returns -1 if |request_type| is not a valid request type.
574int GetStunSuccessResponseType(int request_type);
575
576// Returns the error response type for the given request type.
577// Returns -1 if |request_type| is not a valid request type.
578int GetStunErrorResponseType(int request_type);
579
580// Returns whether a given message is a request type.
581bool IsStunRequestType(int msg_type);
582
583// Returns whether a given message is an indication type.
584bool IsStunIndicationType(int msg_type);
585
586// Returns whether a given response is a success type.
587bool IsStunSuccessResponseType(int msg_type);
588
589// Returns whether a given response is an error type.
590bool IsStunErrorResponseType(int msg_type);
591
592// Computes the STUN long-term credential hash.
593bool ComputeStunCredentialHash(const std::string& username,
594 const std::string& realm,
595 const std::string& password,
596 std::string* hash);
597
598// Make a copy af |attribute| and return a new StunAttribute.
599// This is useful if you don't care about what kind of attribute you
600// are handling.
601//
602// The implementation copies by calling Write() followed by Read().
603//
604// If |tmp_buffer| is supplied this buffer will be used, otherwise
605// a buffer will created in the method.
606std::unique_ptr<StunAttribute> CopyStunAttribute(
607 const StunAttribute& attribute,
608 rtc::ByteBufferWriter* tmp_buffer_ptr = 0);
609
610// TODO(?): Move the TURN/ICE stuff below out to separate files.
611extern const char TURN_MAGIC_COOKIE_VALUE[4];
612
613// "GTURN" STUN methods.
614// TODO(?): Rename these methods to GTURN_ to make it clear they aren't
615// part of standard STUN/TURN.
616enum RelayMessageType {
617 // For now, using the same defs from TurnMessageType below.
618 // STUN_ALLOCATE_REQUEST = 0x0003,
619 // STUN_ALLOCATE_RESPONSE = 0x0103,
620 // STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
621 STUN_SEND_REQUEST = 0x0004,
622 STUN_SEND_RESPONSE = 0x0104,
623 STUN_SEND_ERROR_RESPONSE = 0x0114,
624 STUN_DATA_INDICATION = 0x0115,
625};
626
627// "GTURN"-specific STUN attributes.
628// TODO(?): Rename these attributes to GTURN_ to avoid conflicts.
629enum RelayAttributeType {
630 STUN_ATTR_LIFETIME = 0x000d, // UInt32
631 STUN_ATTR_MAGIC_COOKIE = 0x000f, // ByteString, 4 bytes
632 STUN_ATTR_BANDWIDTH = 0x0010, // UInt32
633 STUN_ATTR_DESTINATION_ADDRESS = 0x0011, // Address
634 STUN_ATTR_SOURCE_ADDRESS2 = 0x0012, // Address
635 STUN_ATTR_DATA = 0x0013, // ByteString
636 STUN_ATTR_OPTIONS = 0x8001, // UInt32
637};
638
639// A "GTURN" STUN message.
640class RelayMessage : public StunMessage {
641 protected:
642 StunAttributeValueType GetAttributeValueType(int type) const override;
643 StunMessage* CreateNew() const override;
644};
645
646// Defined in TURN RFC 5766.
647enum TurnMessageType {
648 STUN_ALLOCATE_REQUEST = 0x0003,
649 STUN_ALLOCATE_RESPONSE = 0x0103,
650 STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
651 TURN_REFRESH_REQUEST = 0x0004,
652 TURN_REFRESH_RESPONSE = 0x0104,
653 TURN_REFRESH_ERROR_RESPONSE = 0x0114,
654 TURN_SEND_INDICATION = 0x0016,
655 TURN_DATA_INDICATION = 0x0017,
656 TURN_CREATE_PERMISSION_REQUEST = 0x0008,
657 TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
658 TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
659 TURN_CHANNEL_BIND_REQUEST = 0x0009,
660 TURN_CHANNEL_BIND_RESPONSE = 0x0109,
661 TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
662};
663
664enum TurnAttributeType {
665 STUN_ATTR_CHANNEL_NUMBER = 0x000C, // UInt32
666 STUN_ATTR_TURN_LIFETIME = 0x000d, // UInt32
667 STUN_ATTR_XOR_PEER_ADDRESS = 0x0012, // XorAddress
668 // TODO(mallinath) - Uncomment after RelayAttributes are renamed.
669 // STUN_ATTR_DATA = 0x0013, // ByteString
670 STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016, // XorAddress
671 STUN_ATTR_EVEN_PORT = 0x0018, // ByteString, 1 byte.
672 STUN_ATTR_REQUESTED_TRANSPORT = 0x0019, // UInt32
673 STUN_ATTR_DONT_FRAGMENT = 0x001A, // No content, Length = 0
674 STUN_ATTR_RESERVATION_TOKEN = 0x0022, // ByteString, 8 bytes.
675 // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and
676 // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes
677 // by appending G to attribute name.
678};
679
680// RFC 5766-defined errors.
681enum TurnErrorType {
682 STUN_ERROR_FORBIDDEN = 403,
683 STUN_ERROR_ALLOCATION_MISMATCH = 437,
684 STUN_ERROR_WRONG_CREDENTIALS = 441,
685 STUN_ERROR_UNSUPPORTED_PROTOCOL = 442
686};
687
688extern const int SERVER_NOT_REACHABLE_ERROR;
689
690extern const char STUN_ERROR_REASON_FORBIDDEN[];
691extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
692extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
693extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
694class TurnMessage : public StunMessage {
695 protected:
696 StunAttributeValueType GetAttributeValueType(int type) const override;
697 StunMessage* CreateNew() const override;
698};
699
700enum IceAttributeType {
701 // RFC 5245 ICE STUN attributes.
702 STUN_ATTR_PRIORITY = 0x0024, // UInt32
703 STUN_ATTR_USE_CANDIDATE = 0x0025, // No content, Length = 0
704 STUN_ATTR_ICE_CONTROLLED = 0x8029, // UInt64
705 STUN_ATTR_ICE_CONTROLLING = 0x802A, // UInt64
706 // The following attributes are in the comprehension-optional range
707 // (0xC000-0xFFFF) and are not registered with IANA. These STUN attributes are
708 // intended for ICE and should NOT be used in generic use cases of STUN
709 // messages.
710 //
711 // Note that the value 0xC001 has already been assigned by IANA to
712 // ENF-FLOW-DESCRIPTION
713 // (https://www.iana.org/assignments/stun-parameters/stun-parameters.xml).
714 STUN_ATTR_NOMINATION = 0xC001, // UInt32
715 // UInt32. The higher 16 bits are the network ID. The lower 16 bits are the
716 // network cost.
Jonas Orelandfa543642020-09-16 10:44:54 +0200717 STUN_ATTR_GOOG_NETWORK_INFO = 0xC057,
Patrik Höglund56d94522019-11-18 15:53:32 +0100718 // Experimental: Transaction ID of the last connectivity check received.
Jonas Orelandfa543642020-09-16 10:44:54 +0200719 STUN_ATTR_GOOG_LAST_ICE_CHECK_RECEIVED = 0xC058,
Jonas Oreland1721de12019-11-20 12:10:39 +0100720 // Uint16List. Miscellaneous attributes for future extension.
721 STUN_ATTR_GOOG_MISC_INFO = 0xC059,
Jonas Orelandfa543642020-09-16 10:44:54 +0200722 // Obsolete.
723 STUN_ATTR_GOOG_OBSOLETE_1 = 0xC05A,
724 STUN_ATTR_GOOG_CONNECTION_ID = 0xC05B, // Not yet implemented.
725 STUN_ATTR_GOOG_DELTA = 0xC05C, // Not yet implemented.
726 STUN_ATTR_GOOG_DELTA_ACK = 0xC05D, // Not yet implemented.
Jonas Oreland63737a92019-11-21 15:12:14 +0100727 // MESSAGE-INTEGRITY truncated to 32-bit.
728 STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 = 0xC060,
Patrik Höglund56d94522019-11-18 15:53:32 +0100729};
730
Jonas Oreland1721de12019-11-20 12:10:39 +0100731// When adding new attributes to STUN_ATTR_GOOG_MISC_INFO
732// (which is a list of uint16_t), append the indices of these attributes below
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100733// and do NOT change the existing indices. The indices of attributes must be
Jonas Oreland1721de12019-11-20 12:10:39 +0100734// consistent with those used in ConnectionRequest::Prepare when forming a STUN
735// message for the ICE connectivity check, and they are used when parsing a
736// received STUN message.
Jonas Oreland219d8ce2020-01-15 15:29:48 +0100737enum class IceGoogMiscInfoBindingRequestAttributeIndex {
738 SUPPORT_GOOG_PING_VERSION = 0,
739};
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100740
741enum class IceGoogMiscInfoBindingResponseAttributeIndex {
742 SUPPORT_GOOG_PING_VERSION = 0,
743};
Jonas Oreland1721de12019-11-20 12:10:39 +0100744
Patrik Höglund56d94522019-11-18 15:53:32 +0100745// RFC 5245-defined errors.
746enum IceErrorCode {
747 STUN_ERROR_ROLE_CONFLICT = 487,
748};
749extern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
750
751// A RFC 5245 ICE STUN message.
752class IceMessage : public StunMessage {
753 protected:
754 StunAttributeValueType GetAttributeValueType(int type) const override;
755 StunMessage* CreateNew() const override;
756};
757
758} // namespace cricket
759
760#endif // API_TRANSPORT_STUN_H_