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