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