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