blob: 532fe5bec9e947f127972bc2f4f27c0b4ca3563d [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 WEBRTC_P2P_BASE_STUN_H_
12#define WEBRTC_P2P_BASE_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 <string>
18#include <vector>
19
20#include "webrtc/base/basictypes.h"
21#include "webrtc/base/bytebuffer.h"
22#include "webrtc/base/socketaddress.h"
23
24namespace cricket {
25
26// These are the types of STUN messages defined in RFC 5389.
27enum StunMessageType {
28 STUN_BINDING_REQUEST = 0x0001,
29 STUN_BINDING_INDICATION = 0x0011,
30 STUN_BINDING_RESPONSE = 0x0101,
31 STUN_BINDING_ERROR_RESPONSE = 0x0111,
32};
33
34// These are all known STUN attributes, defined in RFC 5389 and elsewhere.
35// Next to each is the name of the class (T is StunTAttribute) that implements
36// that type.
37// RETRANSMIT_COUNT is the number of outstanding pings without a response at
38// the time the packet is generated.
39enum StunAttributeType {
40 STUN_ATTR_MAPPED_ADDRESS = 0x0001, // Address
41 STUN_ATTR_USERNAME = 0x0006, // ByteString
42 STUN_ATTR_MESSAGE_INTEGRITY = 0x0008, // ByteString, 20 bytes
43 STUN_ATTR_ERROR_CODE = 0x0009, // ErrorCode
44 STUN_ATTR_UNKNOWN_ATTRIBUTES = 0x000a, // UInt16List
45 STUN_ATTR_REALM = 0x0014, // ByteString
46 STUN_ATTR_NONCE = 0x0015, // ByteString
47 STUN_ATTR_XOR_MAPPED_ADDRESS = 0x0020, // XorAddress
48 STUN_ATTR_SOFTWARE = 0x8022, // ByteString
49 STUN_ATTR_ALTERNATE_SERVER = 0x8023, // Address
50 STUN_ATTR_FINGERPRINT = 0x8028, // UInt32
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000051 STUN_ATTR_ORIGIN = 0x802F, // ByteString
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 STUN_ATTR_RETRANSMIT_COUNT = 0xFF00 // UInt32
53};
54
55// These are the types of the values associated with the attributes above.
56// This allows us to perform some basic validation when reading or adding
57// attributes. Note that these values are for our own use, and not defined in
58// RFC 5389.
59enum StunAttributeValueType {
60 STUN_VALUE_UNKNOWN = 0,
61 STUN_VALUE_ADDRESS = 1,
62 STUN_VALUE_XOR_ADDRESS = 2,
63 STUN_VALUE_UINT32 = 3,
64 STUN_VALUE_UINT64 = 4,
65 STUN_VALUE_BYTE_STRING = 5,
66 STUN_VALUE_ERROR_CODE = 6,
67 STUN_VALUE_UINT16_LIST = 7
68};
69
70// These are the types of STUN addresses defined in RFC 5389.
71enum StunAddressFamily {
72 // NB: UNDEF is not part of the STUN spec.
73 STUN_ADDRESS_UNDEF = 0,
74 STUN_ADDRESS_IPV4 = 1,
75 STUN_ADDRESS_IPV6 = 2
76};
77
78// These are the types of STUN error codes defined in RFC 5389.
79enum StunErrorCode {
80 STUN_ERROR_TRY_ALTERNATE = 300,
81 STUN_ERROR_BAD_REQUEST = 400,
82 STUN_ERROR_UNAUTHORIZED = 401,
83 STUN_ERROR_UNKNOWN_ATTRIBUTE = 420,
84 STUN_ERROR_STALE_CREDENTIALS = 430, // GICE only
85 STUN_ERROR_STALE_NONCE = 438,
86 STUN_ERROR_SERVER_ERROR = 500,
87 STUN_ERROR_GLOBAL_FAILURE = 600
88};
89
90// Strings for the error codes above.
91extern const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[];
92extern const char STUN_ERROR_REASON_BAD_REQUEST[];
93extern const char STUN_ERROR_REASON_UNAUTHORIZED[];
94extern const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[];
95extern const char STUN_ERROR_REASON_STALE_CREDENTIALS[];
96extern const char STUN_ERROR_REASON_STALE_NONCE[];
97extern const char STUN_ERROR_REASON_SERVER_ERROR[];
98
99// The mask used to determine whether a STUN message is a request/response etc.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100const uint32_t kStunTypeMask = 0x0110;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101
102// STUN Attribute header length.
103const size_t kStunAttributeHeaderSize = 4;
104
105// Following values correspond to RFC5389.
106const size_t kStunHeaderSize = 20;
107const size_t kStunTransactionIdOffset = 8;
108const size_t kStunTransactionIdLength = 12;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200109const uint32_t kStunMagicCookie = 0x2112A442;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110const size_t kStunMagicCookieLength = sizeof(kStunMagicCookie);
111
112// Following value corresponds to an earlier version of STUN from
113// RFC3489.
114const size_t kStunLegacyTransactionIdLength = 16;
115
116// STUN Message Integrity HMAC length.
117const size_t kStunMessageIntegritySize = 20;
118
119class StunAttribute;
120class StunAddressAttribute;
121class StunXorAddressAttribute;
122class StunUInt32Attribute;
123class StunUInt64Attribute;
124class StunByteStringAttribute;
125class StunErrorCodeAttribute;
126class StunUInt16ListAttribute;
127
128// Records a complete STUN/TURN message. Each message consists of a type and
129// any number of attributes. Each attribute is parsed into an instance of an
130// appropriate class (see above). The Get* methods will return instances of
131// that attribute class.
132class StunMessage {
133 public:
134 StunMessage();
zsteinad94c4c2017-03-06 13:36:05 -0800135 virtual ~StunMessage() = default;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136
137 int type() const { return type_; }
138 size_t length() const { return length_; }
139 const std::string& transaction_id() const { return transaction_id_; }
140
141 // Returns true if the message confirms to RFC3489 rather than
142 // RFC5389. The main difference between two version of the STUN
143 // protocol is the presence of the magic cookie and different length
144 // of transaction ID. For outgoing packets version of the protocol
145 // is determined by the lengths of the transaction ID.
146 bool IsLegacy() const;
147
Peter Boström0c4e06b2015-10-07 12:23:21 +0200148 void SetType(int type) { type_ = static_cast<uint16_t>(type); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149 bool SetTransactionID(const std::string& str);
150
151 // Gets the desired attribute value, or NULL if no such attribute type exists.
152 const StunAddressAttribute* GetAddress(int type) const;
153 const StunUInt32Attribute* GetUInt32(int type) const;
154 const StunUInt64Attribute* GetUInt64(int type) const;
155 const StunByteStringAttribute* GetByteString(int type) const;
156
157 // Gets these specific attribute values.
158 const StunErrorCodeAttribute* GetErrorCode() const;
159 const StunUInt16ListAttribute* GetUnknownAttributes() const;
160
nissecc99bc22017-02-02 01:31:30 -0800161 // Takes ownership of the specified attribute and adds it to the message.
zsteinad94c4c2017-03-06 13:36:05 -0800162 // TODO(zstein): Take a unique_ptr instead of a raw pointer.
nissecc99bc22017-02-02 01:31:30 -0800163 void AddAttribute(StunAttribute* attr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000164
165 // Validates that a raw STUN message has a correct MESSAGE-INTEGRITY value.
166 // This can't currently be done on a StunMessage, since it is affected by
167 // padding data (which we discard when reading a StunMessage).
168 static bool ValidateMessageIntegrity(const char* data, size_t size,
169 const std::string& password);
170 // Adds a MESSAGE-INTEGRITY attribute that is valid for the current message.
171 bool AddMessageIntegrity(const std::string& password);
172 bool AddMessageIntegrity(const char* key, size_t keylen);
173
174 // Verifies that a given buffer is STUN by checking for a correct FINGERPRINT.
175 static bool ValidateFingerprint(const char* data, size_t size);
176
177 // Adds a FINGERPRINT attribute that is valid for the current message.
178 bool AddFingerprint();
179
180 // Parses the STUN packet in the given buffer and records it here. The
181 // return value indicates whether this was successful.
jbauchf1f87202016-03-30 06:43:37 -0700182 bool Read(rtc::ByteBufferReader* buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183
184 // Writes this object into a STUN packet. The return value indicates whether
185 // this was successful.
jbauchf1f87202016-03-30 06:43:37 -0700186 bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187
188 // Creates an empty message. Overridable by derived classes.
189 virtual StunMessage* CreateNew() const { return new StunMessage(); }
190
191 protected:
192 // Verifies that the given attribute is allowed for this message.
193 virtual StunAttributeValueType GetAttributeValueType(int type) const;
194
195 private:
196 StunAttribute* CreateAttribute(int type, size_t length) /* const*/;
197 const StunAttribute* GetAttribute(int type) const;
198 static bool IsValidTransactionId(const std::string& transaction_id);
199
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200 uint16_t type_;
201 uint16_t length_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 std::string transaction_id_;
zsteinad94c4c2017-03-06 13:36:05 -0800203 std::vector<std::unique_ptr<StunAttribute>> attrs_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204};
205
206// Base class for all STUN/TURN attributes.
207class StunAttribute {
208 public:
209 virtual ~StunAttribute() {
210 }
211
212 int type() const { return type_; }
213 size_t length() const { return length_; }
214
215 // Return the type of this attribute.
216 virtual StunAttributeValueType value_type() const = 0;
217
218 // Only XorAddressAttribute needs this so far.
219 virtual void SetOwner(StunMessage* owner) {}
220
221 // Reads the body (not the type or length) for this type of attribute from
222 // the given buffer. Return value is true if successful.
jbauchf1f87202016-03-30 06:43:37 -0700223 virtual bool Read(rtc::ByteBufferReader* buf) = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224
225 // Writes the body (not the type or length) to the given buffer. Return
226 // value is true if successful.
jbauchf1f87202016-03-30 06:43:37 -0700227 virtual bool Write(rtc::ByteBufferWriter* buf) const = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228
229 // Creates an attribute object with the given type and smallest length.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200230 static StunAttribute* Create(StunAttributeValueType value_type,
231 uint16_t type,
232 uint16_t length,
233 StunMessage* owner);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 // TODO: Allow these create functions to take parameters, to reduce
235 // the amount of work callers need to do to initialize attributes.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200236 static StunAddressAttribute* CreateAddress(uint16_t type);
237 static StunXorAddressAttribute* CreateXorAddress(uint16_t type);
238 static StunUInt32Attribute* CreateUInt32(uint16_t type);
239 static StunUInt64Attribute* CreateUInt64(uint16_t type);
240 static StunByteStringAttribute* CreateByteString(uint16_t type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 static StunErrorCodeAttribute* CreateErrorCode();
242 static StunUInt16ListAttribute* CreateUnknownAttributes();
243
244 protected:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200245 StunAttribute(uint16_t type, uint16_t length);
246 void SetLength(uint16_t length) { length_ = length; }
jbauchf1f87202016-03-30 06:43:37 -0700247 void WritePadding(rtc::ByteBufferWriter* buf) const;
248 void ConsumePadding(rtc::ByteBufferReader* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249
250 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200251 uint16_t type_;
252 uint16_t length_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253};
254
255// Implements STUN attributes that record an Internet address.
256class StunAddressAttribute : public StunAttribute {
257 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258 static const uint16_t SIZE_UNDEF = 0;
259 static const uint16_t SIZE_IP4 = 8;
260 static const uint16_t SIZE_IP6 = 20;
261 StunAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
262 StunAddressAttribute(uint16_t type, uint16_t length);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000263
264 virtual StunAttributeValueType value_type() const {
265 return STUN_VALUE_ADDRESS;
266 }
267
268 StunAddressFamily family() const {
269 switch (address_.ipaddr().family()) {
270 case AF_INET:
271 return STUN_ADDRESS_IPV4;
272 case AF_INET6:
273 return STUN_ADDRESS_IPV6;
274 }
275 return STUN_ADDRESS_UNDEF;
276 }
277
278 const rtc::SocketAddress& GetAddress() const { return address_; }
279 const rtc::IPAddress& ipaddr() const { return address_.ipaddr(); }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200280 uint16_t port() const { return address_.port(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000281
282 void SetAddress(const rtc::SocketAddress& addr) {
283 address_ = addr;
284 EnsureAddressLength();
285 }
286 void SetIP(const rtc::IPAddress& ip) {
287 address_.SetIP(ip);
288 EnsureAddressLength();
289 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200290 void SetPort(uint16_t port) { address_.SetPort(port); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000291
jbauchf1f87202016-03-30 06:43:37 -0700292 virtual bool Read(rtc::ByteBufferReader* buf);
293 virtual bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000294
295 private:
296 void EnsureAddressLength() {
297 switch (family()) {
298 case STUN_ADDRESS_IPV4: {
299 SetLength(SIZE_IP4);
300 break;
301 }
302 case STUN_ADDRESS_IPV6: {
303 SetLength(SIZE_IP6);
304 break;
305 }
306 default: {
307 SetLength(SIZE_UNDEF);
308 break;
309 }
310 }
311 }
312 rtc::SocketAddress address_;
313};
314
315// Implements STUN attributes that record an Internet address. When encoded
316// in a STUN message, the address contained in this attribute is XORed with the
317// transaction ID of the message.
318class StunXorAddressAttribute : public StunAddressAttribute {
319 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200320 StunXorAddressAttribute(uint16_t type, const rtc::SocketAddress& addr);
321 StunXorAddressAttribute(uint16_t type, uint16_t length, StunMessage* owner);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000322
323 virtual StunAttributeValueType value_type() const {
324 return STUN_VALUE_XOR_ADDRESS;
325 }
326 virtual void SetOwner(StunMessage* owner) {
327 owner_ = owner;
328 }
jbauchf1f87202016-03-30 06:43:37 -0700329 virtual bool Read(rtc::ByteBufferReader* buf);
330 virtual bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000331
332 private:
333 rtc::IPAddress GetXoredIP() const;
334 StunMessage* owner_;
335};
336
337// Implements STUN attributes that record a 32-bit integer.
338class StunUInt32Attribute : public StunAttribute {
339 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200340 static const uint16_t SIZE = 4;
341 StunUInt32Attribute(uint16_t type, uint32_t value);
342 explicit StunUInt32Attribute(uint16_t type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343
344 virtual StunAttributeValueType value_type() const {
345 return STUN_VALUE_UINT32;
346 }
347
Peter Boström0c4e06b2015-10-07 12:23:21 +0200348 uint32_t value() const { return bits_; }
349 void SetValue(uint32_t bits) { bits_ = bits; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000350
351 bool GetBit(size_t index) const;
352 void SetBit(size_t index, bool value);
353
jbauchf1f87202016-03-30 06:43:37 -0700354 virtual bool Read(rtc::ByteBufferReader* buf);
355 virtual bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000356
357 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200358 uint32_t bits_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000359};
360
361class StunUInt64Attribute : public StunAttribute {
362 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200363 static const uint16_t SIZE = 8;
364 StunUInt64Attribute(uint16_t type, uint64_t value);
365 explicit StunUInt64Attribute(uint16_t type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000366
367 virtual StunAttributeValueType value_type() const {
368 return STUN_VALUE_UINT64;
369 }
370
Peter Boström0c4e06b2015-10-07 12:23:21 +0200371 uint64_t value() const { return bits_; }
372 void SetValue(uint64_t bits) { bits_ = bits; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000373
jbauchf1f87202016-03-30 06:43:37 -0700374 virtual bool Read(rtc::ByteBufferReader* buf);
375 virtual bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000376
377 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200378 uint64_t bits_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000379};
380
381// Implements STUN attributes that record an arbitrary byte string.
382class StunByteStringAttribute : public StunAttribute {
383 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200384 explicit StunByteStringAttribute(uint16_t type);
385 StunByteStringAttribute(uint16_t type, const std::string& str);
386 StunByteStringAttribute(uint16_t type, const void* bytes, size_t length);
387 StunByteStringAttribute(uint16_t type, uint16_t length);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 ~StunByteStringAttribute();
389
390 virtual StunAttributeValueType value_type() const {
391 return STUN_VALUE_BYTE_STRING;
392 }
393
394 const char* bytes() const { return bytes_; }
395 std::string GetString() const { return std::string(bytes_, length()); }
396
397 void CopyBytes(const char* bytes); // uses strlen
398 void CopyBytes(const void* bytes, size_t length);
399
Peter Boström0c4e06b2015-10-07 12:23:21 +0200400 uint8_t GetByte(size_t index) const;
401 void SetByte(size_t index, uint8_t value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000402
jbauchf1f87202016-03-30 06:43:37 -0700403 virtual bool Read(rtc::ByteBufferReader* buf);
404 virtual bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000405
406 private:
407 void SetBytes(char* bytes, size_t length);
408
409 char* bytes_;
410};
411
412// Implements STUN attributes that record an error code.
413class StunErrorCodeAttribute : public StunAttribute {
414 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200415 static const uint16_t MIN_SIZE = 4;
416 StunErrorCodeAttribute(uint16_t type, int code, const std::string& reason);
417 StunErrorCodeAttribute(uint16_t type, uint16_t length);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 ~StunErrorCodeAttribute();
419
420 virtual StunAttributeValueType value_type() const {
421 return STUN_VALUE_ERROR_CODE;
422 }
423
424 // The combined error and class, e.g. 0x400.
425 int code() const;
426 void SetCode(int code);
427
428 // The individual error components.
429 int eclass() const { return class_; }
430 int number() const { return number_; }
431 const std::string& reason() const { return reason_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200432 void SetClass(uint8_t eclass) { class_ = eclass; }
433 void SetNumber(uint8_t number) { number_ = number; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000434 void SetReason(const std::string& reason);
435
jbauchf1f87202016-03-30 06:43:37 -0700436 bool Read(rtc::ByteBufferReader* buf);
437 bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000438
439 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200440 uint8_t class_;
441 uint8_t number_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442 std::string reason_;
443};
444
445// Implements STUN attributes that record a list of attribute names.
446class StunUInt16ListAttribute : public StunAttribute {
447 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200448 StunUInt16ListAttribute(uint16_t type, uint16_t length);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000449 ~StunUInt16ListAttribute();
450
451 virtual StunAttributeValueType value_type() const {
452 return STUN_VALUE_UINT16_LIST;
453 }
454
455 size_t Size() const;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200456 uint16_t GetType(int index) const;
457 void SetType(int index, uint16_t value);
458 void AddType(uint16_t value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000459
jbauchf1f87202016-03-30 06:43:37 -0700460 bool Read(rtc::ByteBufferReader* buf);
461 bool Write(rtc::ByteBufferWriter* buf) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000462
463 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200464 std::vector<uint16_t>* attr_types_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000465};
466
467// Returns the (successful) response type for the given request type.
468// Returns -1 if |request_type| is not a valid request type.
469int GetStunSuccessResponseType(int request_type);
470
471// Returns the error response type for the given request type.
472// Returns -1 if |request_type| is not a valid request type.
473int GetStunErrorResponseType(int request_type);
474
475// Returns whether a given message is a request type.
476bool IsStunRequestType(int msg_type);
477
478// Returns whether a given message is an indication type.
479bool IsStunIndicationType(int msg_type);
480
481// Returns whether a given response is a success type.
482bool IsStunSuccessResponseType(int msg_type);
483
484// Returns whether a given response is an error type.
485bool IsStunErrorResponseType(int msg_type);
486
487// Computes the STUN long-term credential hash.
488bool ComputeStunCredentialHash(const std::string& username,
489 const std::string& realm, const std::string& password, std::string* hash);
490
491// TODO: Move the TURN/ICE stuff below out to separate files.
492extern const char TURN_MAGIC_COOKIE_VALUE[4];
493
494// "GTURN" STUN methods.
495// TODO: Rename these methods to GTURN_ to make it clear they aren't
496// part of standard STUN/TURN.
497enum RelayMessageType {
498 // For now, using the same defs from TurnMessageType below.
499 // STUN_ALLOCATE_REQUEST = 0x0003,
500 // STUN_ALLOCATE_RESPONSE = 0x0103,
501 // STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
502 STUN_SEND_REQUEST = 0x0004,
503 STUN_SEND_RESPONSE = 0x0104,
504 STUN_SEND_ERROR_RESPONSE = 0x0114,
505 STUN_DATA_INDICATION = 0x0115,
506};
507
508// "GTURN"-specific STUN attributes.
509// TODO: Rename these attributes to GTURN_ to avoid conflicts.
510enum RelayAttributeType {
511 STUN_ATTR_LIFETIME = 0x000d, // UInt32
512 STUN_ATTR_MAGIC_COOKIE = 0x000f, // ByteString, 4 bytes
513 STUN_ATTR_BANDWIDTH = 0x0010, // UInt32
514 STUN_ATTR_DESTINATION_ADDRESS = 0x0011, // Address
515 STUN_ATTR_SOURCE_ADDRESS2 = 0x0012, // Address
516 STUN_ATTR_DATA = 0x0013, // ByteString
517 STUN_ATTR_OPTIONS = 0x8001, // UInt32
518};
519
520// A "GTURN" STUN message.
521class RelayMessage : public StunMessage {
522 protected:
523 virtual StunAttributeValueType GetAttributeValueType(int type) const {
524 switch (type) {
525 case STUN_ATTR_LIFETIME: return STUN_VALUE_UINT32;
526 case STUN_ATTR_MAGIC_COOKIE: return STUN_VALUE_BYTE_STRING;
527 case STUN_ATTR_BANDWIDTH: return STUN_VALUE_UINT32;
528 case STUN_ATTR_DESTINATION_ADDRESS: return STUN_VALUE_ADDRESS;
529 case STUN_ATTR_SOURCE_ADDRESS2: return STUN_VALUE_ADDRESS;
530 case STUN_ATTR_DATA: return STUN_VALUE_BYTE_STRING;
531 case STUN_ATTR_OPTIONS: return STUN_VALUE_UINT32;
532 default: return StunMessage::GetAttributeValueType(type);
533 }
534 }
535 virtual StunMessage* CreateNew() const { return new RelayMessage(); }
536};
537
538// Defined in TURN RFC 5766.
539enum TurnMessageType {
540 STUN_ALLOCATE_REQUEST = 0x0003,
541 STUN_ALLOCATE_RESPONSE = 0x0103,
542 STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
543 TURN_REFRESH_REQUEST = 0x0004,
544 TURN_REFRESH_RESPONSE = 0x0104,
545 TURN_REFRESH_ERROR_RESPONSE = 0x0114,
546 TURN_SEND_INDICATION = 0x0016,
547 TURN_DATA_INDICATION = 0x0017,
548 TURN_CREATE_PERMISSION_REQUEST = 0x0008,
549 TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
550 TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
551 TURN_CHANNEL_BIND_REQUEST = 0x0009,
552 TURN_CHANNEL_BIND_RESPONSE = 0x0109,
553 TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
554};
555
556enum TurnAttributeType {
557 STUN_ATTR_CHANNEL_NUMBER = 0x000C, // UInt32
558 STUN_ATTR_TURN_LIFETIME = 0x000d, // UInt32
559 STUN_ATTR_XOR_PEER_ADDRESS = 0x0012, // XorAddress
560 // TODO(mallinath) - Uncomment after RelayAttributes are renamed.
561 // STUN_ATTR_DATA = 0x0013, // ByteString
562 STUN_ATTR_XOR_RELAYED_ADDRESS = 0x0016, // XorAddress
563 STUN_ATTR_EVEN_PORT = 0x0018, // ByteString, 1 byte.
564 STUN_ATTR_REQUESTED_TRANSPORT = 0x0019, // UInt32
565 STUN_ATTR_DONT_FRAGMENT = 0x001A, // No content, Length = 0
566 STUN_ATTR_RESERVATION_TOKEN = 0x0022, // ByteString, 8 bytes.
567 // TODO(mallinath) - Rename STUN_ATTR_TURN_LIFETIME to STUN_ATTR_LIFETIME and
568 // STUN_ATTR_TURN_DATA to STUN_ATTR_DATA. Also rename RelayMessage attributes
569 // by appending G to attribute name.
570};
571
572// RFC 5766-defined errors.
573enum TurnErrorType {
574 STUN_ERROR_FORBIDDEN = 403,
575 STUN_ERROR_ALLOCATION_MISMATCH = 437,
576 STUN_ERROR_WRONG_CREDENTIALS = 441,
577 STUN_ERROR_UNSUPPORTED_PROTOCOL = 442
578};
579extern const char STUN_ERROR_REASON_FORBIDDEN[];
580extern const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[];
581extern const char STUN_ERROR_REASON_WRONG_CREDENTIALS[];
582extern const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[];
583class TurnMessage : public StunMessage {
584 protected:
585 virtual StunAttributeValueType GetAttributeValueType(int type) const {
586 switch (type) {
587 case STUN_ATTR_CHANNEL_NUMBER: return STUN_VALUE_UINT32;
588 case STUN_ATTR_TURN_LIFETIME: return STUN_VALUE_UINT32;
589 case STUN_ATTR_XOR_PEER_ADDRESS: return STUN_VALUE_XOR_ADDRESS;
590 case STUN_ATTR_DATA: return STUN_VALUE_BYTE_STRING;
591 case STUN_ATTR_XOR_RELAYED_ADDRESS: return STUN_VALUE_XOR_ADDRESS;
592 case STUN_ATTR_EVEN_PORT: return STUN_VALUE_BYTE_STRING;
593 case STUN_ATTR_REQUESTED_TRANSPORT: return STUN_VALUE_UINT32;
594 case STUN_ATTR_DONT_FRAGMENT: return STUN_VALUE_BYTE_STRING;
595 case STUN_ATTR_RESERVATION_TOKEN: return STUN_VALUE_BYTE_STRING;
596 default: return StunMessage::GetAttributeValueType(type);
597 }
598 }
599 virtual StunMessage* CreateNew() const { return new TurnMessage(); }
600};
601
602// RFC 5245 ICE STUN attributes.
603enum IceAttributeType {
honghaize1a0c942016-02-16 14:54:56 -0800604 STUN_ATTR_PRIORITY = 0x0024, // UInt32
605 STUN_ATTR_USE_CANDIDATE = 0x0025, // No content, Length = 0
606 STUN_ATTR_ICE_CONTROLLED = 0x8029, // UInt64
607 STUN_ATTR_ICE_CONTROLLING = 0x802A, // UInt64
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700608 STUN_ATTR_NOMINATION = 0xC001, // UInt32
honghaiza0c44ea2016-03-23 16:07:48 -0700609 // UInt32. The higher 16 bits are the network ID. The lower 16 bits are the
610 // network cost.
611 STUN_ATTR_NETWORK_INFO = 0xC057
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000612};
613
614// RFC 5245-defined errors.
615enum IceErrorCode {
616 STUN_ERROR_ROLE_CONFLICT = 487,
617};
618extern const char STUN_ERROR_REASON_ROLE_CONFLICT[];
619
620// A RFC 5245 ICE STUN message.
621class IceMessage : public StunMessage {
622 protected:
623 virtual StunAttributeValueType GetAttributeValueType(int type) const {
624 switch (type) {
honghaize1a0c942016-02-16 14:54:56 -0800625 case STUN_ATTR_PRIORITY:
honghaiza0c44ea2016-03-23 16:07:48 -0700626 case STUN_ATTR_NETWORK_INFO:
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700627 case STUN_ATTR_NOMINATION:
honghaize1a0c942016-02-16 14:54:56 -0800628 return STUN_VALUE_UINT32;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000629 case STUN_ATTR_USE_CANDIDATE: return STUN_VALUE_BYTE_STRING;
630 case STUN_ATTR_ICE_CONTROLLED: return STUN_VALUE_UINT64;
631 case STUN_ATTR_ICE_CONTROLLING: return STUN_VALUE_UINT64;
632 default: return StunMessage::GetAttributeValueType(type);
633 }
634 }
635 virtual StunMessage* CreateNew() const { return new IceMessage(); }
636};
637
638} // namespace cricket
639
640#endif // WEBRTC_P2P_BASE_STUN_H_