blob: db37b8e365068fd782d9099fb7ce075bf605cb41 [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
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700166 // Get a list of all of the attribute types in the "comprehension required"
167 // range that were not recognized.
168 std::vector<uint16_t> GetNonComprehendedAttributes() const;
169
Patrik Höglund56d94522019-11-18 15:53:32 +0100170 // Gets the desired attribute value, or NULL if no such attribute type exists.
171 const StunAddressAttribute* GetAddress(int type) const;
172 const StunUInt32Attribute* GetUInt32(int type) const;
173 const StunUInt64Attribute* GetUInt64(int type) const;
174 const StunByteStringAttribute* GetByteString(int type) const;
Jonas Oreland1721de12019-11-20 12:10:39 +0100175 const StunUInt16ListAttribute* GetUInt16List(int type) const;
Patrik Höglund56d94522019-11-18 15:53:32 +0100176
177 // Gets these specific attribute values.
178 const StunErrorCodeAttribute* GetErrorCode() const;
179 // Returns the code inside the error code attribute, if present, and
180 // STUN_ERROR_GLOBAL_FAILURE otherwise.
181 int GetErrorCodeValue() const;
182 const StunUInt16ListAttribute* GetUnknownAttributes() const;
183
184 // Takes ownership of the specified attribute and adds it to the message.
185 void AddAttribute(std::unique_ptr<StunAttribute> attr);
186
187 // Remove the last occurrence of an attribute.
188 std::unique_ptr<StunAttribute> RemoveAttribute(int type);
189
Jonas Oreland63737a92019-11-21 15:12:14 +0100190 // Remote all attributes and releases them.
191 void ClearAttributes();
192
Patrik Höglund56d94522019-11-18 15:53:32 +0100193 // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value.
194 // This can't currently be done on a StunMessage, since it is affected by
195 // padding data (which we discard when reading a StunMessage).
196 static bool ValidateMessageIntegrity(const char* data,
197 size_t size,
198 const std::string& password);
Jonas Oreland63737a92019-11-21 15:12:14 +0100199 static bool ValidateMessageIntegrity32(const char* data,
200 size_t size,
201 const std::string& password);
202
Patrik Höglund56d94522019-11-18 15:53:32 +0100203 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message.
204 bool AddMessageIntegrity(const std::string& password);
205 bool AddMessageIntegrity(const char* key, size_t keylen);
206
Jonas Oreland63737a92019-11-21 15:12:14 +0100207 // Adds a STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 attribute that is valid for the
208 // current message.
209 bool AddMessageIntegrity32(absl::string_view password);
210
Jonas Oreland253d50f2019-11-28 17:08:07 +0100211 // Verify that a buffer has stun magic cookie and one of the specified
212 // methods. Note that it does not check for the existance of FINGERPRINT.
213 static bool IsStunMethod(rtc::ArrayView<int> methods,
214 const char* data,
215 size_t size);
216
Patrik Höglund56d94522019-11-18 15:53:32 +0100217 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT.
218 static bool ValidateFingerprint(const char* data, size_t size);
219
220 // Adds a FINGERPRINT attribute that is valid for the current message.
221 bool AddFingerprint();
222
223 // Parses the STUN packet in the given buffer and records it here. The
224 // return value indicates whether this was successful.
225 bool Read(rtc::ByteBufferReader* buf);
226
227 // Writes this object into a STUN packet. The return value indicates whether
228 // this was successful.
229 bool Write(rtc::ByteBufferWriter* buf) const;
230
231 // Creates an empty message. Overridable by derived classes.
232 virtual StunMessage* CreateNew() const;
233
234 // Modify the stun magic cookie used for this STUN message.
235 // This is used for testing.
236 void SetStunMagicCookie(uint32_t val);
237
Jonas Oreland253d50f2019-11-28 17:08:07 +0100238 // Contruct a copy of |this|.
239 std::unique_ptr<StunMessage> Clone() const;
240
241 // Check if the attributes of this StunMessage equals those of |other|
242 // for all attributes that |attribute_type_mask| return true
243 bool EqualAttributes(const StunMessage* other,
244 std::function<bool(int type)> attribute_type_mask) const;
245
Patrik Höglund56d94522019-11-18 15:53:32 +0100246 protected:
247 // Verifies that the given attribute is allowed for this message.
248 virtual StunAttributeValueType GetAttributeValueType(int type) const;
249
Jonas Oreland253d50f2019-11-28 17:08:07 +0100250 std::vector<std::unique_ptr<StunAttribute>> attrs_;
251
Patrik Höglund56d94522019-11-18 15:53:32 +0100252 private:
253 StunAttribute* CreateAttribute(int type, size_t length) /* const*/;
254 const StunAttribute* GetAttribute(int type) const;
255 static bool IsValidTransactionId(const std::string& transaction_id);
Jonas Oreland63737a92019-11-21 15:12:14 +0100256 bool AddMessageIntegrityOfType(int mi_attr_type,
257 size_t mi_attr_size,
258 const char* key,
259 size_t keylen);
260 static bool ValidateMessageIntegrityOfType(int mi_attr_type,
261 size_t mi_attr_size,
262 const char* data,
263 size_t size,
264 const std::string& password);
Patrik Höglund56d94522019-11-18 15:53:32 +0100265
266 uint16_t type_;
267 uint16_t length_;
268 std::string transaction_id_;
269 uint32_t reduced_transaction_id_;
Patrik Höglund56d94522019-11-18 15:53:32 +0100270 uint32_t stun_magic_cookie_;
271};
272
273// Base class for all STUN/TURN attributes.
274class StunAttribute {
275 public:
276 virtual ~StunAttribute() {}
277
278 int type() const { return type_; }
279 size_t length() const { return length_; }
280
281 // Return the type of this attribute.
282 virtual StunAttributeValueType value_type() const = 0;
283
284 // Only XorAddressAttribute needs this so far.
285 virtual void SetOwner(StunMessage* owner) {}
286
287 // Reads the body (not the type or length) for this type of attribute from
288 // the given buffer. Return value is true if successful.
289 virtual bool Read(rtc::ByteBufferReader* buf) = 0;
290
291 // Writes the body (not the type or length) to the given buffer. Return
292 // value is true if successful.
293 virtual bool Write(rtc::ByteBufferWriter* buf) const = 0;
294
295 // Creates an attribute object with the given type and smallest length.
296 static StunAttribute* Create(StunAttributeValueType value_type,
297 uint16_t type,
298 uint16_t length,
299 StunMessage* owner);
300 // TODO(?): Allow these create functions to take parameters, to reduce
301 // the amount of work callers need to do to initialize attributes.
302 static std::unique_ptr<StunAddressAttribute> CreateAddress(uint16_t type);
303 static std::unique_ptr<StunXorAddressAttribute> CreateXorAddress(
304 uint16_t type);
305 static std::unique_ptr<StunUInt32Attribute> CreateUInt32(uint16_t type);
306 static std::unique_ptr<StunUInt64Attribute> CreateUInt64(uint16_t type);
307 static std::unique_ptr<StunByteStringAttribute> CreateByteString(
308 uint16_t type);
Jonas Oreland1721de12019-11-20 12:10:39 +0100309 static std::unique_ptr<StunUInt16ListAttribute> CreateUInt16ListAttribute(
310 uint16_t type);
Patrik Höglund56d94522019-11-18 15:53:32 +0100311 static std::unique_ptr<StunErrorCodeAttribute> CreateErrorCode();
312 static std::unique_ptr<StunUInt16ListAttribute> CreateUnknownAttributes();
313
314 protected:
315 StunAttribute(uint16_t type, uint16_t length);
316 void SetLength(uint16_t length) { length_ = length; }
317 void WritePadding(rtc::ByteBufferWriter* buf) const;
318 void ConsumePadding(rtc::ByteBufferReader* buf) const;
319
320 private:
321 uint16_t type_;
322 uint16_t length_;
323};
324
325// Implements STUN attributes that record an Internet address.
326class StunAddressAttribute : public StunAttribute {
327 public:
328 static const uint16_t SIZE_UNDEF = 0;
329 static const uint16_t SIZE_IP4 = 8;
330 static const uint16_t SIZE_IP6 = 20;
331 StunAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
332 StunAddressAttribute(uint16_t type, uint16_t length);
333
334 StunAttributeValueType value_type() const override;
335
336 StunAddressFamily family() const {
337 switch (address_.ipaddr().family()) {
338 case AF_INET:
339 return STUN_ADDRESS_IPV4;
340 case AF_INET6:
341 return STUN_ADDRESS_IPV6;
342 }
343 return STUN_ADDRESS_UNDEF;
344 }
345
346 const rtc::SocketAddress& GetAddress() const { return address_; }
347 const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
348 uint16_t port() const { return address_.port(); }
349
350 void SetAddress(const rtc::SocketAddress& addr) {
351 address_ = addr;
352 EnsureAddressLength();
353 }
354 void SetIP(const rtc::IPAddress& ip) {
355 address_.SetIP(ip);
356 EnsureAddressLength();
357 }
358 void SetPort(uint16_t port) { address_.SetPort(port); }
359
360 bool Read(rtc::ByteBufferReader* buf) override;
361 bool Write(rtc::ByteBufferWriter* buf) const override;
362
363 private:
364 void EnsureAddressLength() {
365 switch (family()) {
366 case STUN_ADDRESS_IPV4: {
367 SetLength(SIZE_IP4);
368 break;
369 }
370 case STUN_ADDRESS_IPV6: {
371 SetLength(SIZE_IP6);
372 break;
373 }
374 default: {
375 SetLength(SIZE_UNDEF);
376 break;
377 }
378 }
379 }
380 rtc::SocketAddress address_;
381};
382
383// Implements STUN attributes that record an Internet address. When encoded
384// in a STUN message, the address contained in this attribute is XORed with the
385// transaction ID of the message.
386class StunXorAddressAttribute : public StunAddressAttribute {
387 public:
388 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
389 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner);
390
391 StunAttributeValueType value_type() const override;
392 void SetOwner(StunMessage* owner) override;
393 bool Read(rtc::ByteBufferReader* buf) override;
394 bool Write(rtc::ByteBufferWriter* buf) const override;
395
396 private:
397 rtc::IPAddress GetXoredIP() const;
398 StunMessage* owner_;
399};
400
401// Implements STUN attributes that record a 32-bit integer.
402class StunUInt32Attribute : public StunAttribute {
403 public:
404 static const uint16_t SIZE = 4;
405 StunUInt32Attribute(uint16_t type, uint32_t value);
406 explicit StunUInt32Attribute(uint16_t type);
407
408 StunAttributeValueType value_type() const override;
409
410 uint32_t value() const { return bits_; }
411 void SetValue(uint32_t bits) { bits_ = bits; }
412
413 bool GetBit(size_t index) const;
414 void SetBit(size_t index, bool value);
415
416 bool Read(rtc::ByteBufferReader* buf) override;
417 bool Write(rtc::ByteBufferWriter* buf) const override;
418
419 private:
420 uint32_t bits_;
421};
422
423class StunUInt64Attribute : public StunAttribute {
424 public:
425 static const uint16_t SIZE = 8;
426 StunUInt64Attribute(uint16_t type, uint64_t value);
427 explicit StunUInt64Attribute(uint16_t type);
428
429 StunAttributeValueType value_type() const override;
430
431 uint64_t value() const { return bits_; }
432 void SetValue(uint64_t bits) { bits_ = bits; }
433
434 bool Read(rtc::ByteBufferReader* buf) override;
435 bool Write(rtc::ByteBufferWriter* buf) const override;
436
437 private:
438 uint64_t bits_;
439};
440
441// Implements STUN attributes that record an arbitrary byte string.
442class StunByteStringAttribute : public StunAttribute {
443 public:
444 explicit StunByteStringAttribute(uint16_t type);
445 StunByteStringAttribute(uint16_t type, const std::string& str);
446 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length);
447 StunByteStringAttribute(uint16_t type, uint16_t length);
448 ~StunByteStringAttribute() override;
449
450 StunAttributeValueType value_type() const override;
451
452 const char* bytes() const { return bytes_; }
453 std::string GetString() const { return std::string(bytes_, length()); }
454
455 void CopyBytes(const char* bytes); // uses strlen
456 void CopyBytes(const void* bytes, size_t length);
457
458 uint8_t GetByte(size_t index) const;
459 void SetByte(size_t index, uint8_t value);
460
461 bool Read(rtc::ByteBufferReader* buf) override;
462 bool Write(rtc::ByteBufferWriter* buf) const override;
463
464 private:
465 void SetBytes(char* bytes, size_t length);
466
467 char* bytes_;
468};
469
470// Implements STUN attributes that record an error code.
471class StunErrorCodeAttribute : public StunAttribute {
472 public:
473 static const uint16_t MIN_SIZE;
474 StunErrorCodeAttribute(uint16_t type, int code, const std::string& reason);
475 StunErrorCodeAttribute(uint16_t type, uint16_t length);
476 ~StunErrorCodeAttribute() override;
477
478 StunAttributeValueType value_type() const override;
479
480 // The combined error and class, e.g. 0x400.
481 int code() const;
482 void SetCode(int code);
483
484 // The individual error components.
485 int eclass() const { return class_; }
486 int number() const { return number_; }
487 const std::string& reason() const { return reason_; }
488 void SetClass(uint8_t eclass) { class_ = eclass; }
489 void SetNumber(uint8_t number) { number_ = number; }
490 void SetReason(const std::string& reason);
491
492 bool Read(rtc::ByteBufferReader* buf) override;
493 bool Write(rtc::ByteBufferWriter* buf) const override;
494
495 private:
496 uint8_t class_;
497 uint8_t number_;
498 std::string reason_;
499};
500
501// Implements STUN attributes that record a list of attribute names.
502class StunUInt16ListAttribute : public StunAttribute {
503 public:
504 StunUInt16ListAttribute(uint16_t type, uint16_t length);
505 ~StunUInt16ListAttribute() override;
506
507 StunAttributeValueType value_type() const override;
508
509 size_t Size() const;
510 uint16_t GetType(int index) const;
511 void SetType(int index, uint16_t value);
512 void AddType(uint16_t value);
Jonas Oreland63737a92019-11-21 15:12:14 +0100513 void AddTypeAtIndex(uint16_t index, uint16_t value);
Patrik Höglund56d94522019-11-18 15:53:32 +0100514
515 bool Read(rtc::ByteBufferReader* buf) override;
516 bool Write(rtc::ByteBufferWriter* buf) const override;
517
518 private:
519 std::vector<uint16_t>* attr_types_;
520};
521
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100522// Return a string e.g "STUN BINDING request".
523std::string StunMethodToString(int msg_type);
524
Patrik Höglund56d94522019-11-18 15:53:32 +0100525// Returns the (successful) response type for the given request type.
526// Returns -1 if |request_type| is not a valid request type.
527int GetStunSuccessResponseType(int request_type);
528
529// Returns the error response type for the given request type.
530// Returns -1 if |request_type| is not a valid request type.
531int GetStunErrorResponseType(int request_type);
532
533// Returns whether a given message is a request type.
534bool IsStunRequestType(int msg_type);
535
536// Returns whether a given message is an indication type.
537bool IsStunIndicationType(int msg_type);
538
539// Returns whether a given response is a success type.
540bool IsStunSuccessResponseType(int msg_type);
541
542// Returns whether a given response is an error type.
543bool IsStunErrorResponseType(int msg_type);
544
545// Computes the STUN long-term credential hash.
546bool ComputeStunCredentialHash(const std::string& username,
547 const std::string& realm,
548 const std::string& password,
549 std::string* hash);
550
551// Make a copy af |attribute| and return a new StunAttribute.
552// This is useful if you don't care about what kind of attribute you
553// are handling.
554//
555// The implementation copies by calling Write() followed by Read().
556//
557// If |tmp_buffer| is supplied this buffer will be used, otherwise
558// a buffer will created in the method.
559std::unique_ptr<StunAttribute> CopyStunAttribute(
560 const StunAttribute& attribute,
561 rtc::ByteBufferWriter* tmp_buffer_ptr = 0);
562
563// TODO(?): Move the TURN/ICE stuff below out to separate files.
564extern const char TURN_MAGIC_COOKIE_VALUE[4];
565
566// "GTURN" STUN methods.
567// TODO(?): Rename these methods to GTURN_ to make it clear they aren't
568// part of standard STUN/TURN.
569enum RelayMessageType {
570 // For now, using the same defs from TurnMessageType below.
571 // STUN_ALLOCATE_REQUEST = 0x0003,
572 // STUN_ALLOCATE_RESPONSE = 0x0103,
573 // STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
574 STUN_SEND_REQUEST = 0x0004,
575 STUN_SEND_RESPONSE = 0x0104,
576 STUN_SEND_ERROR_RESPONSE = 0x0114,
577 STUN_DATA_INDICATION = 0x0115,
578};
579
580// "GTURN"-specific STUN attributes.
581// TODO(?): Rename these attributes to GTURN_ to avoid conflicts.
582enum RelayAttributeType {
583 STUN_ATTR_LIFETIME = 0x000d, // UInt32
584 STUN_ATTR_MAGIC_COOKIE = 0x000f, // ByteString, 4 bytes
585 STUN_ATTR_BANDWIDTH = 0x0010, // UInt32
586 STUN_ATTR_DESTINATION_ADDRESS = 0x0011, // Address
587 STUN_ATTR_SOURCE_ADDRESS2 = 0x0012, // Address
588 STUN_ATTR_DATA = 0x0013, // ByteString
589 STUN_ATTR_OPTIONS = 0x8001, // UInt32
590};
591
592// A "GTURN" STUN message.
593class RelayMessage : public StunMessage {
594 protected:
595 StunAttributeValueType GetAttributeValueType(int type) const override;
596 StunMessage* CreateNew() const override;
597};
598
599// Defined in TURN RFC 5766.
600enum TurnMessageType {
601 STUN_ALLOCATE_REQUEST = 0x0003,
602 STUN_ALLOCATE_RESPONSE = 0x0103,
603 STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
604 TURN_REFRESH_REQUEST = 0x0004,
605 TURN_REFRESH_RESPONSE = 0x0104,
606 TURN_REFRESH_ERROR_RESPONSE = 0x0114,
607 TURN_SEND_INDICATION = 0x0016,
608 TURN_DATA_INDICATION = 0x0017,
609 TURN_CREATE_PERMISSION_REQUEST = 0x0008,
610 TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
611 TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
612 TURN_CHANNEL_BIND_REQUEST = 0x0009,
613 TURN_CHANNEL_BIND_RESPONSE = 0x0109,
614 TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
615};
616
617enum TurnAttributeType {
618 STUN_ATTR_CHANNEL_NUMBER = 0x000C, // UInt32
619 STUN_ATTR_TURN_LIFETIME = 0x000d, // UInt32
620 STUN_ATTR_XOR_PEER_ADDRESS = 0x0012, // XorAddress
621 // TODO(mallinath) - Uncomment after RelayAttributes are renamed.
622 // STUN_ATTR_DATA = 0x0013, // ByteString
623 STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016, // XorAddress
624 STUN_ATTR_EVEN_PORT = 0x0018, // ByteString, 1 byte.
625 STUN_ATTR_REQUESTED_TRANSPORT = 0x0019, // UInt32
626 STUN_ATTR_DONT_FRAGMENT = 0x001A, // No content, Length = 0
627 STUN_ATTR_RESERVATION_TOKEN = 0x0022, // ByteString, 8 bytes.
628 // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and
629 // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes
630 // by appending G to attribute name.
631};
632
633// RFC 5766-defined errors.
634enum TurnErrorType {
635 STUN_ERROR_FORBIDDEN = 403,
636 STUN_ERROR_ALLOCATION_MISMATCH = 437,
637 STUN_ERROR_WRONG_CREDENTIALS = 441,
638 STUN_ERROR_UNSUPPORTED_PROTOCOL = 442
639};
640
641extern const int SERVER_NOT_REACHABLE_ERROR;
642
643extern const char STUN_ERROR_REASON_FORBIDDEN[];
644extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
645extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
646extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
647class TurnMessage : public StunMessage {
648 protected:
649 StunAttributeValueType GetAttributeValueType(int type) const override;
650 StunMessage* CreateNew() const override;
651};
652
653enum IceAttributeType {
654 // RFC 5245 ICE STUN attributes.
655 STUN_ATTR_PRIORITY = 0x0024, // UInt32
656 STUN_ATTR_USE_CANDIDATE = 0x0025, // No content, Length = 0
657 STUN_ATTR_ICE_CONTROLLED = 0x8029, // UInt64
658 STUN_ATTR_ICE_CONTROLLING = 0x802A, // UInt64
659 // The following attributes are in the comprehension-optional range
660 // (0xC000-0xFFFF) and are not registered with IANA. These STUN attributes are
661 // intended for ICE and should NOT be used in generic use cases of STUN
662 // messages.
663 //
664 // Note that the value 0xC001 has already been assigned by IANA to
665 // ENF-FLOW-DESCRIPTION
666 // (https://www.iana.org/assignments/stun-parameters/stun-parameters.xml).
667 STUN_ATTR_NOMINATION = 0xC001, // UInt32
668 // UInt32. The higher 16 bits are the network ID. The lower 16 bits are the
669 // network cost.
Jonas Orelandfa543642020-09-16 10:44:54 +0200670 STUN_ATTR_GOOG_NETWORK_INFO = 0xC057,
Patrik Höglund56d94522019-11-18 15:53:32 +0100671 // Experimental: Transaction ID of the last connectivity check received.
Jonas Orelandfa543642020-09-16 10:44:54 +0200672 STUN_ATTR_GOOG_LAST_ICE_CHECK_RECEIVED = 0xC058,
Jonas Oreland1721de12019-11-20 12:10:39 +0100673 // Uint16List. Miscellaneous attributes for future extension.
674 STUN_ATTR_GOOG_MISC_INFO = 0xC059,
Jonas Orelandfa543642020-09-16 10:44:54 +0200675 // Obsolete.
676 STUN_ATTR_GOOG_OBSOLETE_1 = 0xC05A,
677 STUN_ATTR_GOOG_CONNECTION_ID = 0xC05B, // Not yet implemented.
678 STUN_ATTR_GOOG_DELTA = 0xC05C, // Not yet implemented.
679 STUN_ATTR_GOOG_DELTA_ACK = 0xC05D, // Not yet implemented.
Jonas Oreland63737a92019-11-21 15:12:14 +0100680 // MESSAGE-INTEGRITY truncated to 32-bit.
681 STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 = 0xC060,
Patrik Höglund56d94522019-11-18 15:53:32 +0100682};
683
Jonas Oreland1721de12019-11-20 12:10:39 +0100684// When adding new attributes to STUN_ATTR_GOOG_MISC_INFO
685// (which is a list of uint16_t), append the indices of these attributes below
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100686// and do NOT change the existing indices. The indices of attributes must be
Jonas Oreland1721de12019-11-20 12:10:39 +0100687// consistent with those used in ConnectionRequest::Prepare when forming a STUN
688// message for the ICE connectivity check, and they are used when parsing a
689// received STUN message.
Jonas Oreland219d8ce2020-01-15 15:29:48 +0100690enum class IceGoogMiscInfoBindingRequestAttributeIndex {
691 SUPPORT_GOOG_PING_VERSION = 0,
692};
Jonas Oreland9a52bd72019-12-11 11:35:48 +0100693
694enum class IceGoogMiscInfoBindingResponseAttributeIndex {
695 SUPPORT_GOOG_PING_VERSION = 0,
696};
Jonas Oreland1721de12019-11-20 12:10:39 +0100697
Patrik Höglund56d94522019-11-18 15:53:32 +0100698// RFC 5245-defined errors.
699enum IceErrorCode {
700 STUN_ERROR_ROLE_CONFLICT = 487,
701};
702extern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
703
704// A RFC 5245 ICE STUN message.
705class IceMessage : public StunMessage {
706 protected:
707 StunAttributeValueType GetAttributeValueType(int type) const override;
708 StunMessage* CreateNew() const override;
709};
710
711} // namespace cricket
712
713#endif // API_TRANSPORT_STUN_H_