henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "p2p/base/stun.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
| 14 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/byteorder.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/crc32.h" |
| 20 | #include "rtc_base/logging.h" |
| 21 | #include "rtc_base/messagedigest.h" |
| 22 | #include "rtc_base/ptr_util.h" |
| 23 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 25 | using rtc::ByteBufferReader; |
| 26 | using rtc::ByteBufferWriter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 27 | |
| 28 | namespace cricket { |
| 29 | |
| 30 | const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server"; |
| 31 | const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request"; |
| 32 | const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized"; |
| 33 | const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden"; |
| 34 | const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials"; |
| 35 | const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch"; |
| 36 | const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce"; |
| 37 | const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials"; |
| 38 | const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol"; |
| 39 | const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict"; |
| 40 | const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error"; |
| 41 | |
| 42 | const char TURN_MAGIC_COOKIE_VALUE[] = { '\x72', '\xC6', '\x4B', '\xC6' }; |
| 43 | const char EMPTY_TRANSACTION_ID[] = "0000000000000000"; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 44 | const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 45 | |
| 46 | // StunMessage |
| 47 | |
| 48 | StunMessage::StunMessage() |
| 49 | : type_(0), |
| 50 | length_(0), |
| 51 | transaction_id_(EMPTY_TRANSACTION_ID) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 52 | RTC_DCHECK(IsValidTransactionId(transaction_id_)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 55 | StunMessage::~StunMessage() = default; |
| 56 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 57 | bool StunMessage::IsLegacy() const { |
| 58 | if (transaction_id_.size() == kStunLegacyTransactionIdLength) |
| 59 | return true; |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 60 | RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool StunMessage::SetTransactionID(const std::string& str) { |
| 65 | if (!IsValidTransactionId(str)) { |
| 66 | return false; |
| 67 | } |
| 68 | transaction_id_ = str; |
| 69 | return true; |
| 70 | } |
| 71 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 72 | static bool ImplementationDefinedRange(int attr_type) |
| 73 | { |
| 74 | return attr_type >= 0xC000 && attr_type <= 0xFFFF; |
| 75 | } |
| 76 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 77 | void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) { |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 78 | // Fail any attributes that aren't valid for this type of message, |
| 79 | // but allow any type for the range that is "implementation defined" |
| 80 | // in the RFC. |
| 81 | if (!ImplementationDefinedRange(attr->type())) { |
| 82 | RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type())); |
| 83 | } |
nisse | cc99bc2 | 2017-02-02 01:31:30 -0800 | [diff] [blame] | 84 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 85 | attr->SetOwner(this); |
| 86 | size_t attr_length = attr->length(); |
| 87 | if (attr_length % 4 != 0) { |
| 88 | attr_length += (4 - (attr_length % 4)); |
| 89 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 90 | length_ += static_cast<uint16_t>(attr_length + 4); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 91 | |
| 92 | attrs_.push_back(std::move(attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | const StunAddressAttribute* StunMessage::GetAddress(int type) const { |
| 96 | switch (type) { |
| 97 | case STUN_ATTR_MAPPED_ADDRESS: { |
| 98 | // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is |
| 99 | // missing. |
| 100 | const StunAttribute* mapped_address = |
| 101 | GetAttribute(STUN_ATTR_MAPPED_ADDRESS); |
| 102 | if (!mapped_address) |
| 103 | mapped_address = GetAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 104 | return reinterpret_cast<const StunAddressAttribute*>(mapped_address); |
| 105 | } |
| 106 | |
| 107 | default: |
| 108 | return static_cast<const StunAddressAttribute*>(GetAttribute(type)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | const StunUInt32Attribute* StunMessage::GetUInt32(int type) const { |
| 113 | return static_cast<const StunUInt32Attribute*>(GetAttribute(type)); |
| 114 | } |
| 115 | |
| 116 | const StunUInt64Attribute* StunMessage::GetUInt64(int type) const { |
| 117 | return static_cast<const StunUInt64Attribute*>(GetAttribute(type)); |
| 118 | } |
| 119 | |
| 120 | const StunByteStringAttribute* StunMessage::GetByteString(int type) const { |
| 121 | return static_cast<const StunByteStringAttribute*>(GetAttribute(type)); |
| 122 | } |
| 123 | |
| 124 | const StunErrorCodeAttribute* StunMessage::GetErrorCode() const { |
| 125 | return static_cast<const StunErrorCodeAttribute*>( |
| 126 | GetAttribute(STUN_ATTR_ERROR_CODE)); |
| 127 | } |
| 128 | |
deadbeef | 996fc6b | 2017-04-26 09:21:22 -0700 | [diff] [blame] | 129 | int StunMessage::GetErrorCodeValue() const { |
| 130 | const StunErrorCodeAttribute* error_attribute = GetErrorCode(); |
| 131 | return error_attribute ? error_attribute->code() : STUN_ERROR_GLOBAL_FAILURE; |
| 132 | } |
| 133 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 134 | const StunUInt16ListAttribute* StunMessage::GetUnknownAttributes() const { |
| 135 | return static_cast<const StunUInt16ListAttribute*>( |
| 136 | GetAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES)); |
| 137 | } |
| 138 | |
| 139 | // Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the |
| 140 | // procedure outlined in RFC 5389, section 15.4. |
| 141 | bool StunMessage::ValidateMessageIntegrity(const char* data, size_t size, |
| 142 | const std::string& password) { |
| 143 | // Verifying the size of the message. |
katrielc | e4bda24 | 2016-06-09 08:45:45 -0700 | [diff] [blame] | 144 | if ((size % 4) != 0 || size < kStunHeaderSize) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
| 148 | // Getting the message length from the STUN header. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 149 | uint16_t msg_length = rtc::GetBE16(&data[2]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 150 | if (size != (msg_length + kStunHeaderSize)) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | // Finding Message Integrity attribute in stun message. |
| 155 | size_t current_pos = kStunHeaderSize; |
| 156 | bool has_message_integrity_attr = false; |
katrielc | 1a20610 | 2016-06-20 05:13:16 -0700 | [diff] [blame] | 157 | while (current_pos + 4 <= size) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 158 | uint16_t attr_type, attr_length; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | // Getting attribute type and length. |
| 160 | attr_type = rtc::GetBE16(&data[current_pos]); |
| 161 | attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]); |
| 162 | |
| 163 | // If M-I, sanity check it, and break out. |
| 164 | if (attr_type == STUN_ATTR_MESSAGE_INTEGRITY) { |
| 165 | if (attr_length != kStunMessageIntegritySize || |
katrielc | 1a20610 | 2016-06-20 05:13:16 -0700 | [diff] [blame] | 166 | current_pos + sizeof(attr_type) + sizeof(attr_length) + attr_length > |
| 167 | size) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | has_message_integrity_attr = true; |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | // Otherwise, skip to the next attribute. |
| 175 | current_pos += sizeof(attr_type) + sizeof(attr_length) + attr_length; |
| 176 | if ((attr_length % 4) != 0) { |
| 177 | current_pos += (4 - (attr_length % 4)); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!has_message_integrity_attr) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | // Getting length of the message to calculate Message Integrity. |
| 186 | size_t mi_pos = current_pos; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 187 | std::unique_ptr<char[]> temp_data(new char[current_pos]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 188 | memcpy(temp_data.get(), data, current_pos); |
| 189 | if (size > mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize) { |
| 190 | // Stun message has other attributes after message integrity. |
| 191 | // Adjust the length parameter in stun message to calculate HMAC. |
| 192 | size_t extra_offset = size - |
| 193 | (mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize); |
| 194 | size_t new_adjusted_len = size - extra_offset - kStunHeaderSize; |
| 195 | |
| 196 | // Writing new length of the STUN message @ Message Length in temp buffer. |
| 197 | // 0 1 2 3 |
| 198 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 199 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 200 | // |0 0| STUN Message Type | Message Length | |
| 201 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 202 | rtc::SetBE16(temp_data.get() + 2, static_cast<uint16_t>(new_adjusted_len)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | char hmac[kStunMessageIntegritySize]; |
| 206 | size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, |
| 207 | password.c_str(), password.size(), |
| 208 | temp_data.get(), mi_pos, |
| 209 | hmac, sizeof(hmac)); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 210 | RTC_DCHECK(ret == sizeof(hmac)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 211 | if (ret != sizeof(hmac)) |
| 212 | return false; |
| 213 | |
| 214 | // Comparing the calculated HMAC with the one present in the message. |
| 215 | return memcmp(data + current_pos + kStunAttributeHeaderSize, |
| 216 | hmac, |
| 217 | sizeof(hmac)) == 0; |
| 218 | } |
| 219 | |
| 220 | bool StunMessage::AddMessageIntegrity(const std::string& password) { |
| 221 | return AddMessageIntegrity(password.c_str(), password.size()); |
| 222 | } |
| 223 | |
| 224 | bool StunMessage::AddMessageIntegrity(const char* key, |
| 225 | size_t keylen) { |
| 226 | // Add the attribute with a dummy value. Since this is a known attribute, it |
| 227 | // can't fail. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 228 | auto msg_integrity_attr_ptr = rtc::MakeUnique<StunByteStringAttribute>( |
| 229 | STUN_ATTR_MESSAGE_INTEGRITY, std::string(kStunMessageIntegritySize, '0')); |
| 230 | auto* msg_integrity_attr = msg_integrity_attr_ptr.get(); |
| 231 | AddAttribute(std::move(msg_integrity_attr_ptr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 232 | |
| 233 | // Calculate the HMAC for the message. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 234 | ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 235 | if (!Write(&buf)) |
| 236 | return false; |
| 237 | |
| 238 | int msg_len_for_hmac = static_cast<int>( |
| 239 | buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length()); |
| 240 | char hmac[kStunMessageIntegritySize]; |
| 241 | size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, |
| 242 | key, keylen, |
| 243 | buf.Data(), msg_len_for_hmac, |
| 244 | hmac, sizeof(hmac)); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 245 | RTC_DCHECK(ret == sizeof(hmac)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 246 | if (ret != sizeof(hmac)) { |
| 247 | LOG(LS_ERROR) << "HMAC computation failed. Message-Integrity " |
| 248 | << "has dummy value."; |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | // Insert correct HMAC into the attribute. |
| 253 | msg_integrity_attr->CopyBytes(hmac, sizeof(hmac)); |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | // Verifies a message is in fact a STUN message, by performing the checks |
| 258 | // outlined in RFC 5389, section 7.3, including the FINGERPRINT check detailed |
| 259 | // in section 15.5. |
| 260 | bool StunMessage::ValidateFingerprint(const char* data, size_t size) { |
| 261 | // Check the message length. |
| 262 | size_t fingerprint_attr_size = |
| 263 | kStunAttributeHeaderSize + StunUInt32Attribute::SIZE; |
| 264 | if (size % 4 != 0 || size < kStunHeaderSize + fingerprint_attr_size) |
| 265 | return false; |
| 266 | |
| 267 | // Skip the rest if the magic cookie isn't present. |
| 268 | const char* magic_cookie = |
| 269 | data + kStunTransactionIdOffset - kStunMagicCookieLength; |
| 270 | if (rtc::GetBE32(magic_cookie) != kStunMagicCookie) |
| 271 | return false; |
| 272 | |
| 273 | // Check the fingerprint type and length. |
| 274 | const char* fingerprint_attr_data = data + size - fingerprint_attr_size; |
| 275 | if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT || |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 276 | rtc::GetBE16(fingerprint_attr_data + sizeof(uint16_t)) != |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 277 | StunUInt32Attribute::SIZE) |
| 278 | return false; |
| 279 | |
| 280 | // Check the fingerprint value. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 281 | uint32_t fingerprint = |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 282 | rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize); |
| 283 | return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) == |
| 284 | rtc::ComputeCrc32(data, size - fingerprint_attr_size)); |
| 285 | } |
| 286 | |
| 287 | bool StunMessage::AddFingerprint() { |
| 288 | // Add the attribute with a dummy value. Since this is a known attribute, |
| 289 | // it can't fail. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 290 | auto fingerprint_attr_ptr = |
| 291 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_FINGERPRINT, 0); |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 292 | auto* fingerprint_attr = fingerprint_attr_ptr.get(); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 293 | AddAttribute(std::move(fingerprint_attr_ptr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 294 | |
| 295 | // Calculate the CRC-32 for the message and insert it. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 296 | ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 297 | if (!Write(&buf)) |
| 298 | return false; |
| 299 | |
| 300 | int msg_len_for_crc32 = static_cast<int>( |
| 301 | buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 302 | uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 303 | |
| 304 | // Insert the correct CRC-32, XORed with a constant, into the attribute. |
| 305 | fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE); |
| 306 | return true; |
| 307 | } |
| 308 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 309 | bool StunMessage::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 310 | if (!buf->ReadUInt16(&type_)) |
| 311 | return false; |
| 312 | |
| 313 | if (type_ & 0x8000) { |
| 314 | // RTP and RTCP set the MSB of first byte, since first two bits are version, |
| 315 | // and version is always 2 (10). If set, this is not a STUN packet. |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | if (!buf->ReadUInt16(&length_)) |
| 320 | return false; |
| 321 | |
| 322 | std::string magic_cookie; |
| 323 | if (!buf->ReadString(&magic_cookie, kStunMagicCookieLength)) |
| 324 | return false; |
| 325 | |
| 326 | std::string transaction_id; |
| 327 | if (!buf->ReadString(&transaction_id, kStunTransactionIdLength)) |
| 328 | return false; |
| 329 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 330 | uint32_t magic_cookie_int = |
| 331 | *reinterpret_cast<const uint32_t*>(magic_cookie.data()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 332 | if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) { |
| 333 | // If magic cookie is invalid it means that the peer implements |
| 334 | // RFC3489 instead of RFC5389. |
| 335 | transaction_id.insert(0, magic_cookie); |
| 336 | } |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 337 | RTC_DCHECK(IsValidTransactionId(transaction_id)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 338 | transaction_id_ = transaction_id; |
| 339 | |
| 340 | if (length_ != buf->Length()) |
| 341 | return false; |
| 342 | |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 343 | attrs_.resize(0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 344 | |
| 345 | size_t rest = buf->Length() - length_; |
| 346 | while (buf->Length() > rest) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 347 | uint16_t attr_type, attr_length; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 348 | if (!buf->ReadUInt16(&attr_type)) |
| 349 | return false; |
| 350 | if (!buf->ReadUInt16(&attr_length)) |
| 351 | return false; |
| 352 | |
Honghai Zhang | 3e02430 | 2016-09-22 09:52:16 -0700 | [diff] [blame] | 353 | std::unique_ptr<StunAttribute> attr( |
| 354 | CreateAttribute(attr_type, attr_length)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 355 | if (!attr) { |
| 356 | // Skip any unknown or malformed attributes. |
| 357 | if ((attr_length % 4) != 0) { |
| 358 | attr_length += (4 - (attr_length % 4)); |
| 359 | } |
| 360 | if (!buf->Consume(attr_length)) |
| 361 | return false; |
| 362 | } else { |
| 363 | if (!attr->Read(buf)) |
| 364 | return false; |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 365 | attrs_.push_back(std::move(attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 369 | RTC_DCHECK(buf->Length() == rest); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 373 | bool StunMessage::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 374 | buf->WriteUInt16(type_); |
| 375 | buf->WriteUInt16(length_); |
| 376 | if (!IsLegacy()) |
| 377 | buf->WriteUInt32(kStunMagicCookie); |
| 378 | buf->WriteString(transaction_id_); |
| 379 | |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 380 | for (const auto& attr : attrs_) { |
| 381 | buf->WriteUInt16(attr->type()); |
| 382 | buf->WriteUInt16(static_cast<uint16_t>(attr->length())); |
| 383 | if (!attr->Write(buf)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | return false; |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 385 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return true; |
| 389 | } |
| 390 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 391 | StunMessage* StunMessage::CreateNew() const { |
| 392 | return new StunMessage(); |
| 393 | } |
| 394 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 395 | StunAttributeValueType StunMessage::GetAttributeValueType(int type) const { |
| 396 | switch (type) { |
| 397 | case STUN_ATTR_MAPPED_ADDRESS: return STUN_VALUE_ADDRESS; |
| 398 | case STUN_ATTR_USERNAME: return STUN_VALUE_BYTE_STRING; |
| 399 | case STUN_ATTR_MESSAGE_INTEGRITY: return STUN_VALUE_BYTE_STRING; |
| 400 | case STUN_ATTR_ERROR_CODE: return STUN_VALUE_ERROR_CODE; |
| 401 | case STUN_ATTR_UNKNOWN_ATTRIBUTES: return STUN_VALUE_UINT16_LIST; |
| 402 | case STUN_ATTR_REALM: return STUN_VALUE_BYTE_STRING; |
| 403 | case STUN_ATTR_NONCE: return STUN_VALUE_BYTE_STRING; |
| 404 | case STUN_ATTR_XOR_MAPPED_ADDRESS: return STUN_VALUE_XOR_ADDRESS; |
| 405 | case STUN_ATTR_SOFTWARE: return STUN_VALUE_BYTE_STRING; |
| 406 | case STUN_ATTR_ALTERNATE_SERVER: return STUN_VALUE_ADDRESS; |
| 407 | case STUN_ATTR_FINGERPRINT: return STUN_VALUE_UINT32; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 408 | case STUN_ATTR_ORIGIN: return STUN_VALUE_BYTE_STRING; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 409 | case STUN_ATTR_RETRANSMIT_COUNT: return STUN_VALUE_UINT32; |
| 410 | default: return STUN_VALUE_UNKNOWN; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ { |
| 415 | StunAttributeValueType value_type = GetAttributeValueType(type); |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 416 | if (value_type != STUN_VALUE_UNKNOWN) { |
| 417 | return StunAttribute::Create(value_type, type, |
| 418 | static_cast<uint16_t>(length), this); |
| 419 | } else if (ImplementationDefinedRange(type)) { |
| 420 | // Read unknown attributes as STUN_VALUE_BYTE_STRING |
| 421 | return StunAttribute::Create(STUN_VALUE_BYTE_STRING, type, |
| 422 | static_cast<uint16_t>(length), this); |
| 423 | } else { |
| 424 | return NULL; |
| 425 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | const StunAttribute* StunMessage::GetAttribute(int type) const { |
zstein | ad94c4c | 2017-03-06 13:36:05 -0800 | [diff] [blame] | 429 | for (const auto& attr : attrs_) { |
| 430 | if (attr->type() == type) { |
| 431 | return attr.get(); |
| 432 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 433 | } |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | bool StunMessage::IsValidTransactionId(const std::string& transaction_id) { |
| 438 | return transaction_id.size() == kStunTransactionIdLength || |
| 439 | transaction_id.size() == kStunLegacyTransactionIdLength; |
| 440 | } |
| 441 | |
| 442 | // StunAttribute |
| 443 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 444 | StunAttribute::StunAttribute(uint16_t type, uint16_t length) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 445 | : type_(type), length_(length) { |
| 446 | } |
| 447 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 448 | void StunAttribute::ConsumePadding(ByteBufferReader* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 449 | int remainder = length_ % 4; |
| 450 | if (remainder > 0) { |
| 451 | buf->Consume(4 - remainder); |
| 452 | } |
| 453 | } |
| 454 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 455 | void StunAttribute::WritePadding(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 456 | int remainder = length_ % 4; |
| 457 | if (remainder > 0) { |
| 458 | char zeroes[4] = {0}; |
| 459 | buf->WriteBytes(zeroes, 4 - remainder); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | StunAttribute* StunAttribute::Create(StunAttributeValueType value_type, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 464 | uint16_t type, |
| 465 | uint16_t length, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 466 | StunMessage* owner) { |
| 467 | switch (value_type) { |
| 468 | case STUN_VALUE_ADDRESS: |
| 469 | return new StunAddressAttribute(type, length); |
| 470 | case STUN_VALUE_XOR_ADDRESS: |
| 471 | return new StunXorAddressAttribute(type, length, owner); |
| 472 | case STUN_VALUE_UINT32: |
| 473 | return new StunUInt32Attribute(type); |
| 474 | case STUN_VALUE_UINT64: |
| 475 | return new StunUInt64Attribute(type); |
| 476 | case STUN_VALUE_BYTE_STRING: |
| 477 | return new StunByteStringAttribute(type, length); |
| 478 | case STUN_VALUE_ERROR_CODE: |
| 479 | return new StunErrorCodeAttribute(type, length); |
| 480 | case STUN_VALUE_UINT16_LIST: |
| 481 | return new StunUInt16ListAttribute(type, length); |
| 482 | default: |
| 483 | return NULL; |
| 484 | } |
| 485 | } |
| 486 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 487 | std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress( |
| 488 | uint16_t type) { |
| 489 | return rtc::MakeUnique<StunAddressAttribute>(type, 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 490 | } |
| 491 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 492 | std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress( |
| 493 | uint16_t type) { |
| 494 | return rtc::MakeUnique<StunXorAddressAttribute>(type, 0, nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 495 | } |
| 496 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 497 | std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64( |
| 498 | uint16_t type) { |
| 499 | return rtc::MakeUnique<StunUInt64Attribute>(type); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 500 | } |
| 501 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 502 | std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32( |
| 503 | uint16_t type) { |
| 504 | return rtc::MakeUnique<StunUInt32Attribute>(type); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 505 | } |
| 506 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 507 | std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString( |
| 508 | uint16_t type) { |
| 509 | return rtc::MakeUnique<StunByteStringAttribute>(type, 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 510 | } |
| 511 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 512 | std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() { |
| 513 | return rtc::MakeUnique<StunErrorCodeAttribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 514 | STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE); |
| 515 | } |
| 516 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 517 | std::unique_ptr<StunUInt16ListAttribute> |
| 518 | StunAttribute::CreateUnknownAttributes() { |
| 519 | return rtc::MakeUnique<StunUInt16ListAttribute>(STUN_ATTR_UNKNOWN_ATTRIBUTES, |
| 520 | 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 523 | StunAddressAttribute::StunAddressAttribute(uint16_t type, |
| 524 | const rtc::SocketAddress& addr) |
| 525 | : StunAttribute(type, 0) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 526 | SetAddress(addr); |
| 527 | } |
| 528 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 529 | StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 530 | : StunAttribute(type, length) { |
| 531 | } |
| 532 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 533 | StunAttributeValueType StunAddressAttribute::value_type() const { |
| 534 | return STUN_VALUE_ADDRESS; |
| 535 | } |
| 536 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 537 | bool StunAddressAttribute::Read(ByteBufferReader* buf) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 538 | uint8_t dummy; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 539 | if (!buf->ReadUInt8(&dummy)) |
| 540 | return false; |
| 541 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 542 | uint8_t stun_family; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 543 | if (!buf->ReadUInt8(&stun_family)) { |
| 544 | return false; |
| 545 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 546 | uint16_t port; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 547 | if (!buf->ReadUInt16(&port)) |
| 548 | return false; |
| 549 | if (stun_family == STUN_ADDRESS_IPV4) { |
| 550 | in_addr v4addr; |
| 551 | if (length() != SIZE_IP4) { |
| 552 | return false; |
| 553 | } |
| 554 | if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) { |
| 555 | return false; |
| 556 | } |
| 557 | rtc::IPAddress ipaddr(v4addr); |
| 558 | SetAddress(rtc::SocketAddress(ipaddr, port)); |
| 559 | } else if (stun_family == STUN_ADDRESS_IPV6) { |
| 560 | in6_addr v6addr; |
| 561 | if (length() != SIZE_IP6) { |
| 562 | return false; |
| 563 | } |
| 564 | if (!buf->ReadBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr))) { |
| 565 | return false; |
| 566 | } |
| 567 | rtc::IPAddress ipaddr(v6addr); |
| 568 | SetAddress(rtc::SocketAddress(ipaddr, port)); |
| 569 | } else { |
| 570 | return false; |
| 571 | } |
| 572 | return true; |
| 573 | } |
| 574 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 575 | bool StunAddressAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 576 | StunAddressFamily address_family = family(); |
| 577 | if (address_family == STUN_ADDRESS_UNDEF) { |
| 578 | LOG(LS_ERROR) << "Error writing address attribute: unknown family."; |
| 579 | return false; |
| 580 | } |
| 581 | buf->WriteUInt8(0); |
| 582 | buf->WriteUInt8(address_family); |
| 583 | buf->WriteUInt16(address_.port()); |
| 584 | switch (address_.family()) { |
| 585 | case AF_INET: { |
| 586 | in_addr v4addr = address_.ipaddr().ipv4_address(); |
| 587 | buf->WriteBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr)); |
| 588 | break; |
| 589 | } |
| 590 | case AF_INET6: { |
| 591 | in6_addr v6addr = address_.ipaddr().ipv6_address(); |
| 592 | buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr)); |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | return true; |
| 597 | } |
| 598 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 599 | StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type, |
| 600 | const rtc::SocketAddress& addr) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 601 | : StunAddressAttribute(type, addr), owner_(NULL) { |
| 602 | } |
| 603 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 604 | StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type, |
| 605 | uint16_t length, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 606 | StunMessage* owner) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 607 | : StunAddressAttribute(type, length), owner_(owner) { |
| 608 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 609 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 610 | StunAttributeValueType StunXorAddressAttribute::value_type() const { |
| 611 | return STUN_VALUE_XOR_ADDRESS; |
| 612 | } |
| 613 | |
| 614 | void StunXorAddressAttribute::SetOwner(StunMessage* owner) { |
| 615 | owner_ = owner; |
| 616 | } |
| 617 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 618 | rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const { |
| 619 | if (owner_) { |
| 620 | rtc::IPAddress ip = ipaddr(); |
| 621 | switch (ip.family()) { |
| 622 | case AF_INET: { |
| 623 | in_addr v4addr = ip.ipv4_address(); |
| 624 | v4addr.s_addr = |
| 625 | (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie)); |
| 626 | return rtc::IPAddress(v4addr); |
| 627 | } |
| 628 | case AF_INET6: { |
| 629 | in6_addr v6addr = ip.ipv6_address(); |
| 630 | const std::string& transaction_id = owner_->transaction_id(); |
| 631 | if (transaction_id.length() == kStunTransactionIdLength) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 632 | uint32_t transactionid_as_ints[3]; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 633 | memcpy(&transactionid_as_ints[0], transaction_id.c_str(), |
| 634 | transaction_id.length()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 635 | uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 636 | // Transaction ID is in network byte order, but magic cookie |
| 637 | // is stored in host byte order. |
| 638 | ip_as_ints[0] = |
| 639 | (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie)); |
| 640 | ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]); |
| 641 | ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]); |
| 642 | ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]); |
| 643 | return rtc::IPAddress(v6addr); |
| 644 | } |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | // Invalid ip family or transaction ID, or missing owner. |
| 650 | // Return an AF_UNSPEC address. |
| 651 | return rtc::IPAddress(); |
| 652 | } |
| 653 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 654 | bool StunXorAddressAttribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 655 | if (!StunAddressAttribute::Read(buf)) |
| 656 | return false; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 657 | uint16_t xoredport = port() ^ (kStunMagicCookie >> 16); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 658 | rtc::IPAddress xored_ip = GetXoredIP(); |
| 659 | SetAddress(rtc::SocketAddress(xored_ip, xoredport)); |
| 660 | return true; |
| 661 | } |
| 662 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 663 | bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 664 | StunAddressFamily address_family = family(); |
| 665 | if (address_family == STUN_ADDRESS_UNDEF) { |
| 666 | LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family."; |
| 667 | return false; |
| 668 | } |
| 669 | rtc::IPAddress xored_ip = GetXoredIP(); |
| 670 | if (xored_ip.family() == AF_UNSPEC) { |
| 671 | return false; |
| 672 | } |
| 673 | buf->WriteUInt8(0); |
| 674 | buf->WriteUInt8(family()); |
| 675 | buf->WriteUInt16(port() ^ (kStunMagicCookie >> 16)); |
| 676 | switch (xored_ip.family()) { |
| 677 | case AF_INET: { |
| 678 | in_addr v4addr = xored_ip.ipv4_address(); |
| 679 | buf->WriteBytes(reinterpret_cast<const char*>(&v4addr), sizeof(v4addr)); |
| 680 | break; |
| 681 | } |
| 682 | case AF_INET6: { |
| 683 | in6_addr v6addr = xored_ip.ipv6_address(); |
| 684 | buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr)); |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | return true; |
| 689 | } |
| 690 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 691 | StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 692 | : StunAttribute(type, SIZE), bits_(value) { |
| 693 | } |
| 694 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 695 | StunUInt32Attribute::StunUInt32Attribute(uint16_t type) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 696 | : StunAttribute(type, SIZE), bits_(0) { |
| 697 | } |
| 698 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 699 | StunAttributeValueType StunUInt32Attribute::value_type() const { |
| 700 | return STUN_VALUE_UINT32; |
| 701 | } |
| 702 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 703 | bool StunUInt32Attribute::GetBit(size_t index) const { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 704 | RTC_DCHECK(index < 32); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 705 | return static_cast<bool>((bits_ >> index) & 0x1); |
| 706 | } |
| 707 | |
| 708 | void StunUInt32Attribute::SetBit(size_t index, bool value) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 709 | RTC_DCHECK(index < 32); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 710 | bits_ &= ~(1 << index); |
| 711 | bits_ |= value ? (1 << index) : 0; |
| 712 | } |
| 713 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 714 | bool StunUInt32Attribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 715 | if (length() != SIZE || !buf->ReadUInt32(&bits_)) |
| 716 | return false; |
| 717 | return true; |
| 718 | } |
| 719 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 720 | bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 721 | buf->WriteUInt32(bits_); |
| 722 | return true; |
| 723 | } |
| 724 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 725 | StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 726 | : StunAttribute(type, SIZE), bits_(value) { |
| 727 | } |
| 728 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 729 | StunUInt64Attribute::StunUInt64Attribute(uint16_t type) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 730 | : StunAttribute(type, SIZE), bits_(0) { |
| 731 | } |
| 732 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 733 | StunAttributeValueType StunUInt64Attribute::value_type() const { |
| 734 | return STUN_VALUE_UINT64; |
| 735 | } |
| 736 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 737 | bool StunUInt64Attribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 738 | if (length() != SIZE || !buf->ReadUInt64(&bits_)) |
| 739 | return false; |
| 740 | return true; |
| 741 | } |
| 742 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 743 | bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 744 | buf->WriteUInt64(bits_); |
| 745 | return true; |
| 746 | } |
| 747 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 748 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 749 | : StunAttribute(type, 0), bytes_(NULL) { |
| 750 | } |
| 751 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 752 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 753 | const std::string& str) |
| 754 | : StunAttribute(type, 0), bytes_(NULL) { |
| 755 | CopyBytes(str.c_str(), str.size()); |
| 756 | } |
| 757 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 758 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 759 | const void* bytes, |
| 760 | size_t length) |
| 761 | : StunAttribute(type, 0), bytes_(NULL) { |
| 762 | CopyBytes(bytes, length); |
| 763 | } |
| 764 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 765 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 766 | : StunAttribute(type, length), bytes_(NULL) { |
| 767 | } |
| 768 | |
| 769 | StunByteStringAttribute::~StunByteStringAttribute() { |
| 770 | delete [] bytes_; |
| 771 | } |
| 772 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 773 | StunAttributeValueType StunByteStringAttribute::value_type() const { |
| 774 | return STUN_VALUE_BYTE_STRING; |
| 775 | } |
| 776 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 777 | void StunByteStringAttribute::CopyBytes(const char* bytes) { |
| 778 | CopyBytes(bytes, strlen(bytes)); |
| 779 | } |
| 780 | |
| 781 | void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) { |
| 782 | char* new_bytes = new char[length]; |
| 783 | memcpy(new_bytes, bytes, length); |
| 784 | SetBytes(new_bytes, length); |
| 785 | } |
| 786 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 787 | uint8_t StunByteStringAttribute::GetByte(size_t index) const { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 788 | RTC_DCHECK(bytes_ != NULL); |
| 789 | RTC_DCHECK(index < length()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 790 | return static_cast<uint8_t>(bytes_[index]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 793 | void StunByteStringAttribute::SetByte(size_t index, uint8_t value) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 794 | RTC_DCHECK(bytes_ != NULL); |
| 795 | RTC_DCHECK(index < length()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 796 | bytes_[index] = value; |
| 797 | } |
| 798 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 799 | bool StunByteStringAttribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 800 | bytes_ = new char[length()]; |
| 801 | if (!buf->ReadBytes(bytes_, length())) { |
| 802 | return false; |
| 803 | } |
| 804 | |
| 805 | ConsumePadding(buf); |
| 806 | return true; |
| 807 | } |
| 808 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 809 | bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 810 | buf->WriteBytes(bytes_, length()); |
| 811 | WritePadding(buf); |
| 812 | return true; |
| 813 | } |
| 814 | |
| 815 | void StunByteStringAttribute::SetBytes(char* bytes, size_t length) { |
| 816 | delete [] bytes_; |
| 817 | bytes_ = bytes; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 818 | SetLength(static_cast<uint16_t>(length)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 819 | } |
| 820 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 821 | const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4; |
| 822 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 823 | StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, |
| 824 | int code, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 825 | const std::string& reason) |
| 826 | : StunAttribute(type, 0) { |
| 827 | SetCode(code); |
| 828 | SetReason(reason); |
| 829 | } |
| 830 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 831 | StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 832 | : StunAttribute(type, length), class_(0), number_(0) { |
| 833 | } |
| 834 | |
| 835 | StunErrorCodeAttribute::~StunErrorCodeAttribute() { |
| 836 | } |
| 837 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 838 | StunAttributeValueType StunErrorCodeAttribute::value_type() const { |
| 839 | return STUN_VALUE_ERROR_CODE; |
| 840 | } |
| 841 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 842 | int StunErrorCodeAttribute::code() const { |
| 843 | return class_ * 100 + number_; |
| 844 | } |
| 845 | |
| 846 | void StunErrorCodeAttribute::SetCode(int code) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 847 | class_ = static_cast<uint8_t>(code / 100); |
| 848 | number_ = static_cast<uint8_t>(code % 100); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | void StunErrorCodeAttribute::SetReason(const std::string& reason) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 852 | SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 853 | reason_ = reason; |
| 854 | } |
| 855 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 856 | bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 857 | uint32_t val; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 858 | if (length() < MIN_SIZE || !buf->ReadUInt32(&val)) |
| 859 | return false; |
| 860 | |
| 861 | if ((val >> 11) != 0) |
| 862 | LOG(LS_ERROR) << "error-code bits not zero"; |
| 863 | |
| 864 | class_ = ((val >> 8) & 0x7); |
| 865 | number_ = (val & 0xff); |
| 866 | |
| 867 | if (!buf->ReadString(&reason_, length() - 4)) |
| 868 | return false; |
| 869 | |
| 870 | ConsumePadding(buf); |
| 871 | return true; |
| 872 | } |
| 873 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 874 | bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 875 | buf->WriteUInt32(class_ << 8 | number_); |
| 876 | buf->WriteString(reason_); |
| 877 | WritePadding(buf); |
| 878 | return true; |
| 879 | } |
| 880 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 881 | StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 882 | : StunAttribute(type, length) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 883 | attr_types_ = new std::vector<uint16_t>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | StunUInt16ListAttribute::~StunUInt16ListAttribute() { |
| 887 | delete attr_types_; |
| 888 | } |
| 889 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 890 | StunAttributeValueType StunUInt16ListAttribute::value_type() const { |
| 891 | return STUN_VALUE_UINT16_LIST; |
| 892 | } |
| 893 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 894 | size_t StunUInt16ListAttribute::Size() const { |
| 895 | return attr_types_->size(); |
| 896 | } |
| 897 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 898 | uint16_t StunUInt16ListAttribute::GetType(int index) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 899 | return (*attr_types_)[index]; |
| 900 | } |
| 901 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 902 | void StunUInt16ListAttribute::SetType(int index, uint16_t value) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 903 | (*attr_types_)[index] = value; |
| 904 | } |
| 905 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 906 | void StunUInt16ListAttribute::AddType(uint16_t value) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 907 | attr_types_->push_back(value); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 908 | SetLength(static_cast<uint16_t>(attr_types_->size() * 2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 909 | } |
| 910 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 911 | bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 912 | if (length() % 2) |
| 913 | return false; |
| 914 | |
| 915 | for (size_t i = 0; i < length() / 2; i++) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 916 | uint16_t attr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 917 | if (!buf->ReadUInt16(&attr)) |
| 918 | return false; |
| 919 | attr_types_->push_back(attr); |
| 920 | } |
| 921 | // Padding of these attributes is done in RFC 5389 style. This is |
| 922 | // slightly different from RFC3489, but it shouldn't be important. |
| 923 | // RFC3489 pads out to a 32 bit boundary by duplicating one of the |
| 924 | // entries in the list (not necessarily the last one - it's unspecified). |
| 925 | // RFC5389 pads on the end, and the bytes are always ignored. |
| 926 | ConsumePadding(buf); |
| 927 | return true; |
| 928 | } |
| 929 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 930 | bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 931 | for (size_t i = 0; i < attr_types_->size(); ++i) { |
| 932 | buf->WriteUInt16((*attr_types_)[i]); |
| 933 | } |
| 934 | WritePadding(buf); |
| 935 | return true; |
| 936 | } |
| 937 | |
| 938 | int GetStunSuccessResponseType(int req_type) { |
| 939 | return IsStunRequestType(req_type) ? (req_type | 0x100) : -1; |
| 940 | } |
| 941 | |
| 942 | int GetStunErrorResponseType(int req_type) { |
| 943 | return IsStunRequestType(req_type) ? (req_type | 0x110) : -1; |
| 944 | } |
| 945 | |
| 946 | bool IsStunRequestType(int msg_type) { |
| 947 | return ((msg_type & kStunTypeMask) == 0x000); |
| 948 | } |
| 949 | |
| 950 | bool IsStunIndicationType(int msg_type) { |
| 951 | return ((msg_type & kStunTypeMask) == 0x010); |
| 952 | } |
| 953 | |
| 954 | bool IsStunSuccessResponseType(int msg_type) { |
| 955 | return ((msg_type & kStunTypeMask) == 0x100); |
| 956 | } |
| 957 | |
| 958 | bool IsStunErrorResponseType(int msg_type) { |
| 959 | return ((msg_type & kStunTypeMask) == 0x110); |
| 960 | } |
| 961 | |
| 962 | bool ComputeStunCredentialHash(const std::string& username, |
| 963 | const std::string& realm, |
| 964 | const std::string& password, |
| 965 | std::string* hash) { |
| 966 | // http://tools.ietf.org/html/rfc5389#section-15.4 |
| 967 | // long-term credentials will be calculated using the key and key is |
| 968 | // key = MD5(username ":" realm ":" SASLprep(password)) |
| 969 | std::string input = username; |
| 970 | input += ':'; |
| 971 | input += realm; |
| 972 | input += ':'; |
| 973 | input += password; |
| 974 | |
| 975 | char digest[rtc::MessageDigest::kMaxSize]; |
| 976 | size_t size = rtc::ComputeDigest( |
| 977 | rtc::DIGEST_MD5, input.c_str(), input.size(), |
| 978 | digest, sizeof(digest)); |
| 979 | if (size == 0) { |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | *hash = std::string(digest, size); |
| 984 | return true; |
| 985 | } |
| 986 | |
Steve Anton | ca7d54e | 2017-10-25 14:42:51 -0700 | [diff] [blame^] | 987 | StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const { |
| 988 | switch (type) { |
| 989 | case STUN_ATTR_LIFETIME: |
| 990 | return STUN_VALUE_UINT32; |
| 991 | case STUN_ATTR_MAGIC_COOKIE: |
| 992 | return STUN_VALUE_BYTE_STRING; |
| 993 | case STUN_ATTR_BANDWIDTH: |
| 994 | return STUN_VALUE_UINT32; |
| 995 | case STUN_ATTR_DESTINATION_ADDRESS: |
| 996 | return STUN_VALUE_ADDRESS; |
| 997 | case STUN_ATTR_SOURCE_ADDRESS2: |
| 998 | return STUN_VALUE_ADDRESS; |
| 999 | case STUN_ATTR_DATA: |
| 1000 | return STUN_VALUE_BYTE_STRING; |
| 1001 | case STUN_ATTR_OPTIONS: |
| 1002 | return STUN_VALUE_UINT32; |
| 1003 | default: |
| 1004 | return StunMessage::GetAttributeValueType(type); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | StunMessage* RelayMessage::CreateNew() const { |
| 1009 | return new RelayMessage(); |
| 1010 | } |
| 1011 | |
| 1012 | StunAttributeValueType TurnMessage::GetAttributeValueType(int type) const { |
| 1013 | switch (type) { |
| 1014 | case STUN_ATTR_CHANNEL_NUMBER: |
| 1015 | return STUN_VALUE_UINT32; |
| 1016 | case STUN_ATTR_TURN_LIFETIME: |
| 1017 | return STUN_VALUE_UINT32; |
| 1018 | case STUN_ATTR_XOR_PEER_ADDRESS: |
| 1019 | return STUN_VALUE_XOR_ADDRESS; |
| 1020 | case STUN_ATTR_DATA: |
| 1021 | return STUN_VALUE_BYTE_STRING; |
| 1022 | case STUN_ATTR_XOR_RELAYED_ADDRESS: |
| 1023 | return STUN_VALUE_XOR_ADDRESS; |
| 1024 | case STUN_ATTR_EVEN_PORT: |
| 1025 | return STUN_VALUE_BYTE_STRING; |
| 1026 | case STUN_ATTR_REQUESTED_TRANSPORT: |
| 1027 | return STUN_VALUE_UINT32; |
| 1028 | case STUN_ATTR_DONT_FRAGMENT: |
| 1029 | return STUN_VALUE_BYTE_STRING; |
| 1030 | case STUN_ATTR_RESERVATION_TOKEN: |
| 1031 | return STUN_VALUE_BYTE_STRING; |
| 1032 | default: |
| 1033 | return StunMessage::GetAttributeValueType(type); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | StunMessage* TurnMessage::CreateNew() const { |
| 1038 | return new TurnMessage(); |
| 1039 | } |
| 1040 | |
| 1041 | StunAttributeValueType IceMessage::GetAttributeValueType(int type) const { |
| 1042 | switch (type) { |
| 1043 | case STUN_ATTR_PRIORITY: |
| 1044 | case STUN_ATTR_NETWORK_INFO: |
| 1045 | case STUN_ATTR_NOMINATION: |
| 1046 | return STUN_VALUE_UINT32; |
| 1047 | case STUN_ATTR_USE_CANDIDATE: |
| 1048 | return STUN_VALUE_BYTE_STRING; |
| 1049 | case STUN_ATTR_ICE_CONTROLLED: |
| 1050 | return STUN_VALUE_UINT64; |
| 1051 | case STUN_ATTR_ICE_CONTROLLING: |
| 1052 | return STUN_VALUE_UINT64; |
| 1053 | default: |
| 1054 | return StunMessage::GetAttributeValueType(type); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | StunMessage* IceMessage::CreateNew() const { |
| 1059 | return new IceMessage(); |
| 1060 | } |
| 1061 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1062 | } // namespace cricket |