blob: 857a38107896ca32c8b643ccd34b247ee9591372 [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
Patrik Höglund56d94522019-11-18 15:53:32 +0100205 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT.
206 static bool ValidateFingerprint(const char* data, size_t size);
207
208 // Adds a FINGERPRINT attribute that is valid for the current message.
209 bool AddFingerprint();
210
211 // Parses the STUN packet in the given buffer and records it here. The
212 // return value indicates whether this was successful.
213 bool Read(rtc::ByteBufferReader* buf);
214
215 // Writes this object into a STUN packet. The return value indicates whether
216 // this was successful.
217 bool Write(rtc::ByteBufferWriter* buf) const;
218
219 // Creates an empty message. Overridable by derived classes.
220 virtual StunMessage* CreateNew() const;
221
222 // Modify the stun magic cookie used for this STUN message.
223 // This is used for testing.
224 void SetStunMagicCookie(uint32_t val);
225
226 protected:
227 // Verifies that the given attribute is allowed for this message.
228 virtual StunAttributeValueType GetAttributeValueType(int type) const;
229
230 private:
231 StunAttribute* CreateAttribute(int type, size_t length) /* const*/;
232 const StunAttribute* GetAttribute(int type) const;
233 static bool IsValidTransactionId(const std::string& transaction_id);
Jonas Oreland63737a92019-11-21 15:12:14 +0100234 bool AddMessageIntegrityOfType(int mi_attr_type,
235 size_t mi_attr_size,
236 const char* key,
237 size_t keylen);
238 static bool ValidateMessageIntegrityOfType(int mi_attr_type,
239 size_t mi_attr_size,
240 const char* data,
241 size_t size,
242 const std::string& password);
Patrik Höglund56d94522019-11-18 15:53:32 +0100243
244 uint16_t type_;
245 uint16_t length_;
246 std::string transaction_id_;
247 uint32_t reduced_transaction_id_;
248 std::vector<std::unique_ptr<StunAttribute>> attrs_;
249 uint32_t stun_magic_cookie_;
250};
251
252// Base class for all STUN/TURN attributes.
253class StunAttribute {
254 public:
255 virtual ~StunAttribute() {}
256
257 int type() const { return type_; }
258 size_t length() const { return length_; }
259
260 // Return the type of this attribute.
261 virtual StunAttributeValueType value_type() const = 0;
262
263 // Only XorAddressAttribute needs this so far.
264 virtual void SetOwner(StunMessage* owner) {}
265
266 // Reads the body (not the type or length) for this type of attribute from
267 // the given buffer. Return value is true if successful.
268 virtual bool Read(rtc::ByteBufferReader* buf) = 0;
269
270 // Writes the body (not the type or length) to the given buffer. Return
271 // value is true if successful.
272 virtual bool Write(rtc::ByteBufferWriter* buf) const = 0;
273
274 // Creates an attribute object with the given type and smallest length.
275 static StunAttribute* Create(StunAttributeValueType value_type,
276 uint16_t type,
277 uint16_t length,
278 StunMessage* owner);
279 // TODO(?): Allow these create functions to take parameters, to reduce
280 // the amount of work callers need to do to initialize attributes.
281 static std::unique_ptr<StunAddressAttribute> CreateAddress(uint16_t type);
282 static std::unique_ptr<StunXorAddressAttribute> CreateXorAddress(
283 uint16_t type);
284 static std::unique_ptr<StunUInt32Attribute> CreateUInt32(uint16_t type);
285 static std::unique_ptr<StunUInt64Attribute> CreateUInt64(uint16_t type);
286 static std::unique_ptr<StunByteStringAttribute> CreateByteString(
287 uint16_t type);
Jonas Oreland1721de12019-11-20 12:10:39 +0100288 static std::unique_ptr<StunUInt16ListAttribute> CreateUInt16ListAttribute(
289 uint16_t type);
Patrik Höglund56d94522019-11-18 15:53:32 +0100290 static std::unique_ptr<StunErrorCodeAttribute> CreateErrorCode();
291 static std::unique_ptr<StunUInt16ListAttribute> CreateUnknownAttributes();
292
293 protected:
294 StunAttribute(uint16_t type, uint16_t length);
295 void SetLength(uint16_t length) { length_ = length; }
296 void WritePadding(rtc::ByteBufferWriter* buf) const;
297 void ConsumePadding(rtc::ByteBufferReader* buf) const;
298
299 private:
300 uint16_t type_;
301 uint16_t length_;
302};
303
304// Implements STUN attributes that record an Internet address.
305class StunAddressAttribute : public StunAttribute {
306 public:
307 static const uint16_t SIZE_UNDEF = 0;
308 static const uint16_t SIZE_IP4 = 8;
309 static const uint16_t SIZE_IP6 = 20;
310 StunAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
311 StunAddressAttribute(uint16_t type, uint16_t length);
312
313 StunAttributeValueType value_type() const override;
314
315 StunAddressFamily family() const {
316 switch (address_.ipaddr().family()) {
317 case AF_INET:
318 return STUN_ADDRESS_IPV4;
319 case AF_INET6:
320 return STUN_ADDRESS_IPV6;
321 }
322 return STUN_ADDRESS_UNDEF;
323 }
324
325 const rtc::SocketAddress& GetAddress() const { return address_; }
326 const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
327 uint16_t port() const { return address_.port(); }
328
329 void SetAddress(const rtc::SocketAddress& addr) {
330 address_ = addr;
331 EnsureAddressLength();
332 }
333 void SetIP(const rtc::IPAddress& ip) {
334 address_.SetIP(ip);
335 EnsureAddressLength();
336 }
337 void SetPort(uint16_t port) { address_.SetPort(port); }
338
339 bool Read(rtc::ByteBufferReader* buf) override;
340 bool Write(rtc::ByteBufferWriter* buf) const override;
341
342 private:
343 void EnsureAddressLength() {
344 switch (family()) {
345 case STUN_ADDRESS_IPV4: {
346 SetLength(SIZE_IP4);
347 break;
348 }
349 case STUN_ADDRESS_IPV6: {
350 SetLength(SIZE_IP6);
351 break;
352 }
353 default: {
354 SetLength(SIZE_UNDEF);
355 break;
356 }
357 }
358 }
359 rtc::SocketAddress address_;
360};
361
362// Implements STUN attributes that record an Internet address. When encoded
363// in a STUN message, the address contained in this attribute is XORed with the
364// transaction ID of the message.
365class StunXorAddressAttribute : public StunAddressAttribute {
366 public:
367 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
368 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner);
369
370 StunAttributeValueType value_type() const override;
371 void SetOwner(StunMessage* owner) override;
372 bool Read(rtc::ByteBufferReader* buf) override;
373 bool Write(rtc::ByteBufferWriter* buf) const override;
374
375 private:
376 rtc::IPAddress GetXoredIP() const;
377 StunMessage* owner_;
378};
379
380// Implements STUN attributes that record a 32-bit integer.
381class StunUInt32Attribute : public StunAttribute {
382 public:
383 static const uint16_t SIZE = 4;
384 StunUInt32Attribute(uint16_t type, uint32_t value);
385 explicit StunUInt32Attribute(uint16_t type);
386
387 StunAttributeValueType value_type() const override;
388
389 uint32_t value() const { return bits_; }
390 void SetValue(uint32_t bits) { bits_ = bits; }
391
392 bool GetBit(size_t index) const;
393 void SetBit(size_t index, bool value);
394
395 bool Read(rtc::ByteBufferReader* buf) override;
396 bool Write(rtc::ByteBufferWriter* buf) const override;
397
398 private:
399 uint32_t bits_;
400};
401
402class StunUInt64Attribute : public StunAttribute {
403 public:
404 static const uint16_t SIZE = 8;
405 StunUInt64Attribute(uint16_t type, uint64_t value);
406 explicit StunUInt64Attribute(uint16_t type);
407
408 StunAttributeValueType value_type() const override;
409
410 uint64_t value() const { return bits_; }
411 void SetValue(uint64_t bits) { bits_ = bits; }
412
413 bool Read(rtc::ByteBufferReader* buf) override;
414 bool Write(rtc::ByteBufferWriter* buf) const override;
415
416 private:
417 uint64_t bits_;
418};
419
420// Implements STUN attributes that record an arbitrary byte string.
421class StunByteStringAttribute : public StunAttribute {
422 public:
423 explicit StunByteStringAttribute(uint16_t type);
424 StunByteStringAttribute(uint16_t type, const std::string& str);
425 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length);
426 StunByteStringAttribute(uint16_t type, uint16_t length);
427 ~StunByteStringAttribute() override;
428
429 StunAttributeValueType value_type() const override;
430
431 const char* bytes() const { return bytes_; }
432 std::string GetString() const { return std::string(bytes_, length()); }
433
434 void CopyBytes(const char* bytes); // uses strlen
435 void CopyBytes(const void* bytes, size_t length);
436
437 uint8_t GetByte(size_t index) const;
438 void SetByte(size_t index, uint8_t value);
439
440 bool Read(rtc::ByteBufferReader* buf) override;
441 bool Write(rtc::ByteBufferWriter* buf) const override;
442
443 private:
444 void SetBytes(char* bytes, size_t length);
445
446 char* bytes_;
447};
448
449// Implements STUN attributes that record an error code.
450class StunErrorCodeAttribute : public StunAttribute {
451 public:
452 static const uint16_t MIN_SIZE;
453 StunErrorCodeAttribute(uint16_t type, int code, const std::string& reason);
454 StunErrorCodeAttribute(uint16_t type, uint16_t length);
455 ~StunErrorCodeAttribute() override;
456
457 StunAttributeValueType value_type() const override;
458
459 // The combined error and class, e.g. 0x400.
460 int code() const;
461 void SetCode(int code);
462
463 // The individual error components.
464 int eclass() const { return class_; }
465 int number() const { return number_; }
466 const std::string& reason() const { return reason_; }
467 void SetClass(uint8_t eclass) { class_ = eclass; }
468 void SetNumber(uint8_t number) { number_ = number; }
469 void SetReason(const std::string& reason);
470
471 bool Read(rtc::ByteBufferReader* buf) override;
472 bool Write(rtc::ByteBufferWriter* buf) const override;
473
474 private:
475 uint8_t class_;
476 uint8_t number_;
477 std::string reason_;
478};
479
480// Implements STUN attributes that record a list of attribute names.
481class StunUInt16ListAttribute : public StunAttribute {
482 public:
483 StunUInt16ListAttribute(uint16_t type, uint16_t length);
484 ~StunUInt16ListAttribute() override;
485
486 StunAttributeValueType value_type() const override;
487
488 size_t Size() const;
489 uint16_t GetType(int index) const;
490 void SetType(int index, uint16_t value);
491 void AddType(uint16_t value);
Jonas Oreland63737a92019-11-21 15:12:14 +0100492 void AddTypeAtIndex(uint16_t index, uint16_t value);
Patrik Höglund56d94522019-11-18 15:53:32 +0100493
494 bool Read(rtc::ByteBufferReader* buf) override;
495 bool Write(rtc::ByteBufferWriter* buf) const override;
496
497 private:
498 std::vector<uint16_t>* attr_types_;
499};
500
501// Returns the (successful) response type for the given request type.
502// Returns -1 if |request_type| is not a valid request type.
503int GetStunSuccessResponseType(int request_type);
504
505// Returns the error response type for the given request type.
506// Returns -1 if |request_type| is not a valid request type.
507int GetStunErrorResponseType(int request_type);
508
509// Returns whether a given message is a request type.
510bool IsStunRequestType(int msg_type);
511
512// Returns whether a given message is an indication type.
513bool IsStunIndicationType(int msg_type);
514
515// Returns whether a given response is a success type.
516bool IsStunSuccessResponseType(int msg_type);
517
518// Returns whether a given response is an error type.
519bool IsStunErrorResponseType(int msg_type);
520
521// Computes the STUN long-term credential hash.
522bool ComputeStunCredentialHash(const std::string& username,
523 const std::string& realm,
524 const std::string& password,
525 std::string* hash);
526
527// Make a copy af |attribute| and return a new StunAttribute.
528// This is useful if you don't care about what kind of attribute you
529// are handling.
530//
531// The implementation copies by calling Write() followed by Read().
532//
533// If |tmp_buffer| is supplied this buffer will be used, otherwise
534// a buffer will created in the method.
535std::unique_ptr<StunAttribute> CopyStunAttribute(
536 const StunAttribute& attribute,
537 rtc::ByteBufferWriter* tmp_buffer_ptr = 0);
538
539// TODO(?): Move the TURN/ICE stuff below out to separate files.
540extern const char TURN_MAGIC_COOKIE_VALUE[4];
541
542// "GTURN" STUN methods.
543// TODO(?): Rename these methods to GTURN_ to make it clear they aren't
544// part of standard STUN/TURN.
545enum RelayMessageType {
546 // For now, using the same defs from TurnMessageType below.
547 // STUN_ALLOCATE_REQUEST = 0x0003,
548 // STUN_ALLOCATE_RESPONSE = 0x0103,
549 // STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
550 STUN_SEND_REQUEST = 0x0004,
551 STUN_SEND_RESPONSE = 0x0104,
552 STUN_SEND_ERROR_RESPONSE = 0x0114,
553 STUN_DATA_INDICATION = 0x0115,
554};
555
556// "GTURN"-specific STUN attributes.
557// TODO(?): Rename these attributes to GTURN_ to avoid conflicts.
558enum RelayAttributeType {
559 STUN_ATTR_LIFETIME = 0x000d, // UInt32
560 STUN_ATTR_MAGIC_COOKIE = 0x000f, // ByteString, 4 bytes
561 STUN_ATTR_BANDWIDTH = 0x0010, // UInt32
562 STUN_ATTR_DESTINATION_ADDRESS = 0x0011, // Address
563 STUN_ATTR_SOURCE_ADDRESS2 = 0x0012, // Address
564 STUN_ATTR_DATA = 0x0013, // ByteString
565 STUN_ATTR_OPTIONS = 0x8001, // UInt32
566};
567
568// A "GTURN" STUN message.
569class RelayMessage : public StunMessage {
570 protected:
571 StunAttributeValueType GetAttributeValueType(int type) const override;
572 StunMessage* CreateNew() const override;
573};
574
575// Defined in TURN RFC 5766.
576enum TurnMessageType {
577 STUN_ALLOCATE_REQUEST = 0x0003,
578 STUN_ALLOCATE_RESPONSE = 0x0103,
579 STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
580 TURN_REFRESH_REQUEST = 0x0004,
581 TURN_REFRESH_RESPONSE = 0x0104,
582 TURN_REFRESH_ERROR_RESPONSE = 0x0114,
583 TURN_SEND_INDICATION = 0x0016,
584 TURN_DATA_INDICATION = 0x0017,
585 TURN_CREATE_PERMISSION_REQUEST = 0x0008,
586 TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
587 TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
588 TURN_CHANNEL_BIND_REQUEST = 0x0009,
589 TURN_CHANNEL_BIND_RESPONSE = 0x0109,
590 TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
591};
592
593enum TurnAttributeType {
594 STUN_ATTR_CHANNEL_NUMBER = 0x000C, // UInt32
595 STUN_ATTR_TURN_LIFETIME = 0x000d, // UInt32
596 STUN_ATTR_XOR_PEER_ADDRESS = 0x0012, // XorAddress
597 // TODO(mallinath) - Uncomment after RelayAttributes are renamed.
598 // STUN_ATTR_DATA = 0x0013, // ByteString
599 STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016, // XorAddress
600 STUN_ATTR_EVEN_PORT = 0x0018, // ByteString, 1 byte.
601 STUN_ATTR_REQUESTED_TRANSPORT = 0x0019, // UInt32
602 STUN_ATTR_DONT_FRAGMENT = 0x001A, // No content, Length = 0
603 STUN_ATTR_RESERVATION_TOKEN = 0x0022, // ByteString, 8 bytes.
604 // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and
605 // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes
606 // by appending G to attribute name.
607};
608
609// RFC 5766-defined errors.
610enum TurnErrorType {
611 STUN_ERROR_FORBIDDEN = 403,
612 STUN_ERROR_ALLOCATION_MISMATCH = 437,
613 STUN_ERROR_WRONG_CREDENTIALS = 441,
614 STUN_ERROR_UNSUPPORTED_PROTOCOL = 442
615};
616
617extern const int SERVER_NOT_REACHABLE_ERROR;
618
619extern const char STUN_ERROR_REASON_FORBIDDEN[];
620extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
621extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
622extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
623class TurnMessage : public StunMessage {
624 protected:
625 StunAttributeValueType GetAttributeValueType(int type) const override;
626 StunMessage* CreateNew() const override;
627};
628
629enum IceAttributeType {
630 // RFC 5245 ICE STUN attributes.
631 STUN_ATTR_PRIORITY = 0x0024, // UInt32
632 STUN_ATTR_USE_CANDIDATE = 0x0025, // No content, Length = 0
633 STUN_ATTR_ICE_CONTROLLED = 0x8029, // UInt64
634 STUN_ATTR_ICE_CONTROLLING = 0x802A, // UInt64
635 // The following attributes are in the comprehension-optional range
636 // (0xC000-0xFFFF) and are not registered with IANA. These STUN attributes are
637 // intended for ICE and should NOT be used in generic use cases of STUN
638 // messages.
639 //
640 // Note that the value 0xC001 has already been assigned by IANA to
641 // ENF-FLOW-DESCRIPTION
642 // (https://www.iana.org/assignments/stun-parameters/stun-parameters.xml).
643 STUN_ATTR_NOMINATION = 0xC001, // UInt32
644 // UInt32. The higher 16 bits are the network ID. The lower 16 bits are the
645 // network cost.
646 STUN_ATTR_NETWORK_INFO = 0xC057,
647 // Experimental: Transaction ID of the last connectivity check received.
648 STUN_ATTR_LAST_ICE_CHECK_RECEIVED = 0xC058,
Jonas Oreland1721de12019-11-20 12:10:39 +0100649 // Uint16List. Miscellaneous attributes for future extension.
650 STUN_ATTR_GOOG_MISC_INFO = 0xC059,
Jonas Oreland63737a92019-11-21 15:12:14 +0100651 // MESSAGE-INTEGRITY truncated to 32-bit.
652 STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32 = 0xC060,
Patrik Höglund56d94522019-11-18 15:53:32 +0100653};
654
Jonas Oreland1721de12019-11-20 12:10:39 +0100655// When adding new attributes to STUN_ATTR_GOOG_MISC_INFO
656// (which is a list of uint16_t), append the indices of these attributes below
657// and do NOT change the exisiting indices. The indices of attributes must be
658// consistent with those used in ConnectionRequest::Prepare when forming a STUN
659// message for the ICE connectivity check, and they are used when parsing a
660// received STUN message.
661enum class IceGoogMiscInfoAttributeIndex {};
662
Patrik Höglund56d94522019-11-18 15:53:32 +0100663// RFC 5245-defined errors.
664enum IceErrorCode {
665 STUN_ERROR_ROLE_CONFLICT = 487,
666};
667extern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
668
669// A RFC 5245 ICE STUN message.
670class IceMessage : public StunMessage {
671 protected:
672 StunAttributeValueType GetAttributeValueType(int type) const override;
673 StunMessage* CreateNew() const override;
674};
675
676} // namespace cricket
677
678#endif // API_TRANSPORT_STUN_H_