| 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 | #include "api/transport/stun.h" |
| 12 |
| 13 | #include <string.h> |
| 14 |
| 15 | #include <algorithm> |
| 16 | #include <cstdint> |
| 17 | #include <iterator> |
| 18 | #include <memory> |
| 19 | #include <utility> |
| 20 |
| 21 | #include "rtc_base/byte_order.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/crc32.h" |
| 24 | #include "rtc_base/helpers.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 | #include "rtc_base/message_digest.h" |
| 27 | #include "system_wrappers/include/metrics.h" |
| 28 |
| 29 | using rtc::ByteBufferReader; |
| 30 | using rtc::ByteBufferWriter; |
| 31 |
| 32 | namespace cricket { |
| 33 |
| 34 | namespace { |
| 35 |
| 36 | const int k127Utf8CharactersLengthInBytes = 508; |
| 37 | const int kMessageIntegrityAttributeLength = 20; |
| 38 | const int kTheoreticalMaximumAttributeLength = 65535; |
| 39 |
| 40 | uint32_t ReduceTransactionId(absl::string_view transaction_id) { |
| 41 | RTC_DCHECK(transaction_id.length() == cricket::kStunTransactionIdLength || |
| 42 | transaction_id.length() == cricket::kStunLegacyTransactionIdLength) |
| 43 | << transaction_id.length(); |
| 44 | ByteBufferReader reader(rtc::MakeArrayView( |
| 45 | reinterpret_cast<const uint8_t*>(transaction_id.data()), |
| 46 | transaction_id.size())); |
| 47 | uint32_t result = 0; |
| 48 | uint32_t next; |
| 49 | while (reader.ReadUInt32(&next)) { |
| 50 | result ^= next; |
| 51 | } |
| 52 | return result; |
| 53 | } |
| 54 |
| 55 | // Check the maximum length of a BYTE_STRING attribute against specifications. |
| 56 | bool LengthValid(int type, int length) { |
| 57 | // "Less than 509 bytes" is intended to indicate a maximum of 127 |
| 58 | // UTF-8 characters, which may take up to 4 bytes per character. |
| 59 | switch (type) { |
| 60 | case STUN_ATTR_USERNAME: |
| 61 | return length <= |
| 62 | k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.3 |
| 63 | case STUN_ATTR_MESSAGE_INTEGRITY: |
| 64 | return length == |
| 65 | kMessageIntegrityAttributeLength; // RFC 8489 section 14.5 |
| 66 | case STUN_ATTR_REALM: |
| 67 | return length <= |
| 68 | k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.9 |
| 69 | case STUN_ATTR_NONCE: |
| 70 | return length <= |
| 71 | k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.10 |
| 72 | case STUN_ATTR_SOFTWARE: |
| 73 | return length <= |
| 74 | k127Utf8CharactersLengthInBytes; // RFC 8489 section 14.14 |
| 75 | case STUN_ATTR_DATA: |
| 76 | // No length restriction in RFC; it's the content of an UDP datagram, |
| 77 | // which in theory can be up to 65.535 bytes. |
| 78 | // TODO(bugs.webrtc.org/12179): Write a test to find the real limit. |
| 79 | return length <= kTheoreticalMaximumAttributeLength; |
| 80 | default: |
| 81 | // Return an arbitrary restriction for all other types. |
| 82 | return length <= kTheoreticalMaximumAttributeLength; |
| 83 | } |
| 84 | RTC_DCHECK_NOTREACHED(); |
| 85 | return true; |
| 86 | } |
| 87 |
| 88 | } // namespace |
| 89 |
| 90 | const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server"; |
| 91 | const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request"; |
| 92 | const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized"; |
| 93 | const char STUN_ERROR_REASON_UNKNOWN_ATTRIBUTE[] = "Unknown Attribute"; |
| 94 | const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden"; |
| 95 | const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch"; |
| 96 | const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce"; |
| 97 | const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials"; |
| 98 | const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol"; |
| 99 | const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict"; |
| 100 | const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error"; |
| 101 |
| 102 | const char TURN_MAGIC_COOKIE_VALUE[] = {'\x72', '\xC6', '\x4B', '\xC6'}; |
| 103 | const char EMPTY_TRANSACTION_ID[] = "0000000000000000"; |
| 104 | const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E; |
| 105 | const int SERVER_NOT_REACHABLE_ERROR = 701; |
| 106 |
| 107 | // StunMessage |
| 108 |
| 109 | StunMessage::StunMessage() |
| 110 | : StunMessage(STUN_INVALID_MESSAGE_TYPE, EMPTY_TRANSACTION_ID) {} |
| 111 |
| 112 | StunMessage::StunMessage(uint16_t type) |
| 113 | : StunMessage(type, GenerateTransactionId()) {} |
| 114 |
| 115 | StunMessage::StunMessage(uint16_t type, absl::string_view transaction_id) |
| 116 | : type_(type), |
| 117 | transaction_id_(transaction_id), |
| 118 | reduced_transaction_id_(ReduceTransactionId(transaction_id_)) { |
| 119 | RTC_DCHECK(IsValidTransactionId(transaction_id_)); |
| 120 | } |
| 121 |
| 122 | StunMessage::~StunMessage() = default; |
| 123 |
| 124 | bool StunMessage::IsLegacy() const { |
| 125 | if (transaction_id_.size() == kStunLegacyTransactionIdLength) |
| 126 | return true; |
| 127 | RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength); |
| 128 | return false; |
| 129 | } |
| 130 |
| 131 | static bool DesignatedExpertRange(int attr_type) { |
| 132 | return (attr_type >= 0x4000 && attr_type <= 0x7FFF) || |
| 133 | (attr_type >= 0xC000 && attr_type <= 0xFFFF); |
| 134 | } |
| 135 |
| 136 | void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) { |
| 137 | // Fail any attributes that aren't valid for this type of message, |
| 138 | // but allow any type for the range that in the RFC is reserved for |
| 139 | // the "designated experts". |
| 140 | if (!DesignatedExpertRange(attr->type())) { |
| 141 | RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type())); |
| 142 | } |
| 143 |
| 144 | attr->SetOwner(this); |
| 145 | size_t attr_length = attr->length(); |
| 146 | if (attr_length % 4 != 0) { |
| 147 | attr_length += (4 - (attr_length % 4)); |
| 148 | } |
| 149 | length_ += static_cast<uint16_t>(attr_length + 4); |
| 150 |
| 151 | attrs_.push_back(std::move(attr)); |
| 152 | } |
| 153 |
| 154 | std::unique_ptr<StunAttribute> StunMessage::RemoveAttribute(int type) { |
| 155 | std::unique_ptr<StunAttribute> attribute; |
| 156 | for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) { |
| 157 | if ((*it)->type() == type) { |
| 158 | attribute = std::move(*it); |
| 159 | attrs_.erase(std::next(it).base()); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | if (attribute) { |
| 164 | attribute->SetOwner(nullptr); |
| 165 | size_t attr_length = attribute->length(); |
| 166 | if (attr_length % 4 != 0) { |
| 167 | attr_length += (4 - (attr_length % 4)); |
| 168 | } |
| 169 | length_ -= static_cast<uint16_t>(attr_length + 4); |
| 170 | } |
| 171 | return attribute; |
| 172 | } |
| 173 |
| 174 | void StunMessage::ClearAttributes() { |
| 175 | for (auto it = attrs_.rbegin(); it != attrs_.rend(); ++it) { |
| 176 | (*it)->SetOwner(nullptr); |
| 177 | } |
| 178 | attrs_.clear(); |
| 179 | length_ = 0; |
| 180 | } |
| 181 |
| 182 | std::vector<uint16_t> StunMessage::GetNonComprehendedAttributes() const { |
| 183 | std::vector<uint16_t> unknown_attributes; |
| 184 | for (auto& attr : attrs_) { |
| 185 | // "comprehension-required" range is 0x0000-0x7FFF. |
| 186 | if (attr->type() >= 0x0000 && attr->type() <= 0x7FFF && |
| 187 | GetAttributeValueType(attr->type()) == STUN_VALUE_UNKNOWN) { |
| 188 | unknown_attributes.push_back(attr->type()); |
| 189 | } |
| 190 | } |
| 191 | return unknown_attributes; |
| 192 | } |
| 193 |
| 194 | const StunAddressAttribute* StunMessage::GetAddress(int type) const { |
| 195 | switch (type) { |
| 196 | case STUN_ATTR_MAPPED_ADDRESS: { |
| 197 | // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is |
| 198 | // missing. |
| 199 | const StunAttribute* mapped_address = |
| 200 | GetAttribute(STUN_ATTR_MAPPED_ADDRESS); |
| 201 | if (!mapped_address) |
| 202 | mapped_address = GetAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 203 | return reinterpret_cast<const StunAddressAttribute*>(mapped_address); |
| 204 | } |
| 205 |
| 206 | default: |
| 207 | return static_cast<const StunAddressAttribute*>(GetAttribute(type)); |
| 208 | } |
| 209 | } |
| 210 |
| 211 | const StunUInt32Attribute* StunMessage::GetUInt32(int type) const { |
| 212 | return static_cast<const StunUInt32Attribute*>(GetAttribute(type)); |
| 213 | } |
| 214 |
| 215 | const StunUInt64Attribute* StunMessage::GetUInt64(int type) const { |
| 216 | return static_cast<const StunUInt64Attribute*>(GetAttribute(type)); |
| 217 | } |
| 218 |
| 219 | const StunByteStringAttribute* StunMessage::GetByteString(int type) const { |
| 220 | return static_cast<const StunByteStringAttribute*>(GetAttribute(type)); |
| 221 | } |
| 222 |
| 223 | const StunUInt16ListAttribute* StunMessage::GetUInt16List(int type) const { |
| 224 | return static_cast<const StunUInt16ListAttribute*>(GetAttribute(type)); |
| 225 | } |
| 226 |
| 227 | const StunErrorCodeAttribute* StunMessage::GetErrorCode() const { |
| 228 | return static_cast<const StunErrorCodeAttribute*>( |
| 229 | GetAttribute(STUN_ATTR_ERROR_CODE)); |
| 230 | } |
| 231 |
| 232 | int StunMessage::GetErrorCodeValue() const { |
| 233 | const StunErrorCodeAttribute* error_attribute = GetErrorCode(); |
| 234 | return error_attribute ? error_attribute->code() : STUN_ERROR_GLOBAL_FAILURE; |
| 235 | } |
| 236 |
| 237 | const StunUInt16ListAttribute* StunMessage::GetUnknownAttributes() const { |
| 238 | return static_cast<const StunUInt16ListAttribute*>( |
| 239 | GetAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES)); |
| 240 | } |
| 241 |
| 242 | StunMessage::IntegrityStatus StunMessage::ValidateMessageIntegrity( |
| 243 | const std::string& password) { |
| 244 | RTC_DCHECK(integrity_ == IntegrityStatus::kNotSet) |
| 245 | << "Usage error: Verification should only be done once"; |
| 246 | password_ = password; |
| 247 | if (GetByteString(STUN_ATTR_MESSAGE_INTEGRITY)) { |
| 248 | if (ValidateMessageIntegrityOfType( |
| 249 | STUN_ATTR_MESSAGE_INTEGRITY, kStunMessageIntegritySize, |
| 250 | buffer_.c_str(), buffer_.size(), password)) { |
| 251 | integrity_ = IntegrityStatus::kIntegrityOk; |
| 252 | } else { |
| 253 | integrity_ = IntegrityStatus::kIntegrityBad; |
| 254 | } |
| 255 | } else if (GetByteString(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32)) { |
| 256 | if (ValidateMessageIntegrityOfType( |
| 257 | STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32, kStunMessageIntegrity32Size, |
| 258 | buffer_.c_str(), buffer_.size(), password)) { |
| 259 | integrity_ = IntegrityStatus::kIntegrityOk; |
| 260 | } else { |
| 261 | integrity_ = IntegrityStatus::kIntegrityBad; |
| 262 | } |
| 263 | } else { |
| 264 | integrity_ = IntegrityStatus::kNoIntegrity; |
| 265 | } |
| 266 | // Log the result of integrity checking. See crbug.com/1177125 for background. |
| 267 | // Convert args to integer for the benefit of the macros. |
| 268 | int bucket_count = static_cast<int>(IntegrityStatus::kMaxValue) + 1; |
| 269 | int integrity = static_cast<int>(integrity_); |
| 270 | if (IsStunRequestType(type_)) { |
| 271 | RTC_HISTOGRAM_ENUMERATION("WebRTC.Stun.Integrity.Request", integrity, |
| 272 | bucket_count); |
| 273 | } else if (IsStunSuccessResponseType(type_)) { |
| 274 | RTC_HISTOGRAM_ENUMERATION("WebRTC.Stun.Integrity.Response", integrity, |
| 275 | bucket_count); |
| 276 | } else if (IsStunIndicationType(type_)) { |
| 277 | RTC_HISTOGRAM_ENUMERATION("WebRTC.Stun.Integrity.Indication", integrity, |
| 278 | bucket_count); |
| 279 | } else { |
| 280 | RTC_DCHECK(IsStunErrorResponseType(type_)); |
| 281 | auto* error_attribute = GetErrorCode(); |
| 282 | if (!error_attribute) { |
| 283 | RTC_HISTOGRAM_ENUMERATION( |
| 284 | "WebRTC.Stun.Integrity.ErrorResponse.NoErrorAttribute", integrity, |
| 285 | bucket_count); |
| 286 | } else { |
| 287 | switch (error_attribute->code()) { |
| 288 | case STUN_ERROR_TRY_ALTERNATE: |
| 289 | RTC_HISTOGRAM_ENUMERATION( |
| 290 | "WebRTC.Stun.Integrity.ErrorResponse.TryAlternate", integrity, |
| 291 | bucket_count); |
| 292 | break; |
| 293 | case STUN_ERROR_BAD_REQUEST: |
| 294 | RTC_HISTOGRAM_ENUMERATION( |
| 295 | "WebRTC.Stun.Integrity.ErrorResponse.BadRequest", integrity, |
| 296 | bucket_count); |
| 297 | break; |
| 298 | case STUN_ERROR_UNAUTHORIZED: |
| 299 | RTC_HISTOGRAM_ENUMERATION( |
| 300 | "WebRTC.Stun.Integrity.ErrorResponse.Unauthorized", integrity, |
| 301 | bucket_count); |
| 302 | break; |
| 303 | case STUN_ERROR_UNKNOWN_ATTRIBUTE: |
| 304 | RTC_HISTOGRAM_ENUMERATION( |
| 305 | "WebRTC.Stun.Integrity.ErrorResponse.UnknownAttribute", integrity, |
| 306 | bucket_count); |
| 307 | break; |
| 308 | case STUN_ERROR_STALE_NONCE: |
| 309 | RTC_HISTOGRAM_ENUMERATION( |
| 310 | "WebRTC.Stun.Integrity.ErrorResponse.StaleNonce", integrity, |
| 311 | bucket_count); |
| 312 | break; |
| 313 | case STUN_ERROR_SERVER_ERROR: |
| 314 | RTC_HISTOGRAM_ENUMERATION( |
| 315 | "WebRTC.Stun.Integrity.ErrorResponse.ServerError", integrity, |
| 316 | bucket_count); |
| 317 | break; |
| 318 | case STUN_ERROR_GLOBAL_FAILURE: |
| 319 | RTC_HISTOGRAM_ENUMERATION( |
| 320 | "WebRTC.Stun.Integrity.ErrorResponse.GlobalFailure", integrity, |
| 321 | bucket_count); |
| 322 | break; |
| 323 | default: |
| 324 | RTC_HISTOGRAM_ENUMERATION( |
| 325 | "WebRTC.Stun.Integrity.ErrorResponse.ErrorOther", integrity, |
| 326 | bucket_count); |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | return integrity_; |
| 332 | } |
| 333 |
| 334 | StunMessage::IntegrityStatus StunMessage::RevalidateMessageIntegrity( |
| 335 | const std::string& password) { |
| 336 | RTC_LOG(LS_INFO) << "Message revalidation, old status was " |
| 337 | << static_cast<int>(integrity_); |
| 338 | integrity_ = IntegrityStatus::kNotSet; |
| 339 | return ValidateMessageIntegrity(password); |
| 340 | } |
| 341 |
| 342 | bool StunMessage::ValidateMessageIntegrityForTesting( |
| 343 | const char* data, |
| 344 | size_t size, |
| 345 | const std::string& password) { |
| 346 | return ValidateMessageIntegrityOfType(STUN_ATTR_MESSAGE_INTEGRITY, |
| 347 | kStunMessageIntegritySize, data, size, |
| 348 | password); |
| 349 | } |
| 350 |
| 351 | bool StunMessage::ValidateMessageIntegrity32ForTesting( |
| 352 | const char* data, |
| 353 | size_t size, |
| 354 | const std::string& password) { |
| 355 | return ValidateMessageIntegrityOfType(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32, |
| 356 | kStunMessageIntegrity32Size, data, size, |
| 357 | password); |
| 358 | } |
| 359 |
| 360 | // Deprecated |
| 361 | bool StunMessage::ValidateMessageIntegrity(const char* data, |
| 362 | size_t size, |
| 363 | const std::string& password) { |
| 364 | return ValidateMessageIntegrityOfType(STUN_ATTR_MESSAGE_INTEGRITY, |
| 365 | kStunMessageIntegritySize, data, size, |
| 366 | password); |
| 367 | } |
| 368 |
| 369 | // Deprecated |
| 370 | bool StunMessage::ValidateMessageIntegrity32(const char* data, |
| 371 | size_t size, |
| 372 | const std::string& password) { |
| 373 | return ValidateMessageIntegrityOfType(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32, |
| 374 | kStunMessageIntegrity32Size, data, size, |
| 375 | password); |
| 376 | } |
| 377 |
| 378 | // Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the |
| 379 | // procedure outlined in RFC 5389, section 15.4. |
| 380 | bool StunMessage::ValidateMessageIntegrityOfType(int mi_attr_type, |
| 381 | size_t mi_attr_size, |
| 382 | const char* data, |
| 383 | size_t size, |
| 384 | const std::string& password) { |
| 385 | RTC_DCHECK(mi_attr_size <= kStunMessageIntegritySize); |
| 386 |
| 387 | // Verifying the size of the message. |
| 388 | if ((size % 4) != 0 || size < kStunHeaderSize) { |
| 389 | return false; |
| 390 | } |
| 391 |
| 392 | // Getting the message length from the STUN header. |
| 393 | uint16_t msg_length = rtc::GetBE16(&data[2]); |
| 394 | if (size != (msg_length + kStunHeaderSize)) { |
| 395 | return false; |
| 396 | } |
| 397 |
| 398 | // Finding Message Integrity attribute in stun message. |
| 399 | size_t current_pos = kStunHeaderSize; |
| 400 | bool has_message_integrity_attr = false; |
| 401 | while (current_pos + 4 <= size) { |
| 402 | uint16_t attr_type, attr_length; |
| 403 | // Getting attribute type and length. |
| 404 | attr_type = rtc::GetBE16(&data[current_pos]); |
| 405 | attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]); |
| 406 |
| 407 | // If M-I, sanity check it, and break out. |
| 408 | if (attr_type == mi_attr_type) { |
| 409 | if (attr_length != mi_attr_size || |
| 410 | current_pos + sizeof(attr_type) + sizeof(attr_length) + attr_length > |
| 411 | size) { |
| 412 | return false; |
| 413 | } |
| 414 | has_message_integrity_attr = true; |
| 415 | break; |
| 416 | } |
| 417 |
| 418 | // Otherwise, skip to the next attribute. |
| 419 | current_pos += sizeof(attr_type) + sizeof(attr_length) + attr_length; |
| 420 | if ((attr_length % 4) != 0) { |
| 421 | current_pos += (4 - (attr_length % 4)); |
| 422 | } |
| 423 | } |
| 424 |
| 425 | if (!has_message_integrity_attr) { |
| 426 | return false; |
| 427 | } |
| 428 |
| 429 | // Getting length of the message to calculate Message Integrity. |
| 430 | size_t mi_pos = current_pos; |
| 431 | std::unique_ptr<char[]> temp_data(new char[current_pos]); |
| 432 | memcpy(temp_data.get(), data, current_pos); |
| 433 | if (size > mi_pos + kStunAttributeHeaderSize + mi_attr_size) { |
| 434 | // Stun message has other attributes after message integrity. |
| 435 | // Adjust the length parameter in stun message to calculate HMAC. |
| 436 | size_t extra_offset = |
| 437 | size - (mi_pos + kStunAttributeHeaderSize + mi_attr_size); |
| 438 | size_t new_adjusted_len = size - extra_offset - kStunHeaderSize; |
| 439 |
| 440 | // Writing new length of the STUN message @ Message Length in temp buffer. |
| 441 | // 0 1 2 3 |
| 442 | // 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 |
| 443 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 444 | // |0 0| STUN Message Type | Message Length | |
| 445 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 446 | rtc::SetBE16(temp_data.get() + 2, static_cast<uint16_t>(new_adjusted_len)); |
| 447 | } |
| 448 |
| 449 | char hmac[kStunMessageIntegritySize]; |
| 450 | size_t ret = |
| 451 | rtc::ComputeHmac(rtc::DIGEST_SHA_1, password.c_str(), password.size(), |
| 452 | temp_data.get(), mi_pos, hmac, sizeof(hmac)); |
| 453 | RTC_DCHECK(ret == sizeof(hmac)); |
| 454 | if (ret != sizeof(hmac)) { |
| 455 | return false; |
| 456 | } |
| 457 |
| 458 | // Comparing the calculated HMAC with the one present in the message. |
| 459 | return memcmp(data + current_pos + kStunAttributeHeaderSize, hmac, |
| 460 | mi_attr_size) == 0; |
| 461 | } |
| 462 |
| 463 | bool StunMessage::AddMessageIntegrity(absl::string_view password) { |
| 464 | return AddMessageIntegrityOfType(STUN_ATTR_MESSAGE_INTEGRITY, |
| 465 | kStunMessageIntegritySize, password); |
| 466 | } |
| 467 |
| 468 | bool StunMessage::AddMessageIntegrity32(absl::string_view password) { |
| 469 | return AddMessageIntegrityOfType(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32, |
| 470 | kStunMessageIntegrity32Size, password); |
| 471 | } |
| 472 |
| 473 | bool StunMessage::AddMessageIntegrityOfType(int attr_type, |
| 474 | size_t attr_size, |
| 475 | absl::string_view key) { |
| 476 | // Add the attribute with a dummy value. Since this is a known attribute, it |
| 477 | // can't fail. |
| 478 | RTC_DCHECK(attr_size <= kStunMessageIntegritySize); |
| 479 | auto msg_integrity_attr_ptr = std::make_unique<StunByteStringAttribute>( |
| 480 | attr_type, std::string(attr_size, '0')); |
| 481 | auto* msg_integrity_attr = msg_integrity_attr_ptr.get(); |
| 482 | AddAttribute(std::move(msg_integrity_attr_ptr)); |
| 483 |
| 484 | // Calculate the HMAC for the message. |
| 485 | ByteBufferWriter buf; |
| 486 | if (!Write(&buf)) |
| 487 | return false; |
| 488 |
| 489 | int msg_len_for_hmac = static_cast<int>( |
| 490 | buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length()); |
| 491 | char hmac[kStunMessageIntegritySize]; |
| 492 | size_t ret = |
| 493 | rtc::ComputeHmac(rtc::DIGEST_SHA_1, key.data(), key.size(), buf.Data(), |
| 494 | msg_len_for_hmac, hmac, sizeof(hmac)); |
| 495 | RTC_DCHECK(ret == sizeof(hmac)); |
| 496 | if (ret != sizeof(hmac)) { |
| 497 | RTC_LOG(LS_ERROR) << "HMAC computation failed. Message-Integrity " |
| 498 | "has dummy value."; |
| 499 | return false; |
| 500 | } |
| 501 |
| 502 | // Insert correct HMAC into the attribute. |
| 503 | msg_integrity_attr->CopyBytes(hmac, attr_size); |
| 504 | password_ = std::string(key); |
| 505 | integrity_ = IntegrityStatus::kIntegrityOk; |
| 506 | return true; |
| 507 | } |
| 508 |
| 509 | // Verifies a message is in fact a STUN message, by performing the checks |
| 510 | // outlined in RFC 5389, section 7.3, including the FINGERPRINT check detailed |
| 511 | // in section 15.5. |
| 512 | bool StunMessage::ValidateFingerprint(const char* data, size_t size) { |
| 513 | // Check the message length. |
| 514 | size_t fingerprint_attr_size = |
| 515 | kStunAttributeHeaderSize + StunUInt32Attribute::SIZE; |
| 516 | if (size % 4 != 0 || size < kStunHeaderSize + fingerprint_attr_size) |
| 517 | return false; |
| 518 |
| 519 | // Skip the rest if the magic cookie isn't present. |
| 520 | const char* magic_cookie = |
| 521 | data + kStunTransactionIdOffset - kStunMagicCookieLength; |
| 522 | if (rtc::GetBE32(magic_cookie) != kStunMagicCookie) |
| 523 | return false; |
| 524 |
| 525 | // Check the fingerprint type and length. |
| 526 | const char* fingerprint_attr_data = data + size - fingerprint_attr_size; |
| 527 | if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT || |
| 528 | rtc::GetBE16(fingerprint_attr_data + sizeof(uint16_t)) != |
| 529 | StunUInt32Attribute::SIZE) |
| 530 | return false; |
| 531 |
| 532 | // Check the fingerprint value. |
| 533 | uint32_t fingerprint = |
| 534 | rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize); |
| 535 | return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) == |
| 536 | rtc::ComputeCrc32(data, size - fingerprint_attr_size)); |
| 537 | } |
| 538 |
| 539 | // static |
| 540 | std::string StunMessage::GenerateTransactionId() { |
| 541 | return rtc::CreateRandomString(kStunTransactionIdLength); |
| 542 | } |
| 543 |
| 544 | bool StunMessage::IsStunMethod(rtc::ArrayView<int> methods, |
| 545 | const char* data, |
| 546 | size_t size) { |
| 547 | // Check the message length. |
| 548 | if (size % 4 != 0 || size < kStunHeaderSize) |
| 549 | return false; |
| 550 |
| 551 | // Skip the rest if the magic cookie isn't present. |
| 552 | const char* magic_cookie = |
| 553 | data + kStunTransactionIdOffset - kStunMagicCookieLength; |
| 554 | if (rtc::GetBE32(magic_cookie) != kStunMagicCookie) |
| 555 | return false; |
| 556 |
| 557 | int method = rtc::GetBE16(data); |
| 558 | for (int m : methods) { |
| 559 | if (m == method) { |
| 560 | return true; |
| 561 | } |
| 562 | } |
| 563 | return false; |
| 564 | } |
| 565 |
| 566 | bool StunMessage::AddFingerprint() { |
| 567 | // Add the attribute with a dummy value. Since this is a known attribute, |
| 568 | // it can't fail. |
| 569 | auto fingerprint_attr_ptr = |
| 570 | std::make_unique<StunUInt32Attribute>(STUN_ATTR_FINGERPRINT, 0); |
| 571 | auto* fingerprint_attr = fingerprint_attr_ptr.get(); |
| 572 | AddAttribute(std::move(fingerprint_attr_ptr)); |
| 573 |
| 574 | // Calculate the CRC-32 for the message and insert it. |
| 575 | ByteBufferWriter buf; |
| 576 | if (!Write(&buf)) |
| 577 | return false; |
| 578 |
| 579 | int msg_len_for_crc32 = static_cast<int>( |
| 580 | buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length()); |
| 581 | uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32); |
| 582 |
| 583 | // Insert the correct CRC-32, XORed with a constant, into the attribute. |
| 584 | fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE); |
| 585 | return true; |
| 586 | } |
| 587 |
| 588 | bool StunMessage::Read(ByteBufferReader* buf) { |
| 589 | // Keep a copy of the buffer data around for later verification. |
| 590 | buffer_.assign(reinterpret_cast<const char*>(buf->Data()), buf->Length()); |
| 591 |
| 592 | if (!buf->ReadUInt16(&type_)) { |
| 593 | return false; |
| 594 | } |
| 595 |
| 596 | if (type_ & 0x8000) { |
| 597 | // RTP and RTCP set the MSB of first byte, since first two bits are version, |
| 598 | // and version is always 2 (10). If set, this is not a STUN packet. |
| 599 | return false; |
| 600 | } |
| 601 |
| 602 | if (!buf->ReadUInt16(&length_)) { |
| 603 | return false; |
| 604 | } |
| 605 |
| 606 | absl::string_view magic_cookie; |
| 607 | if (!buf->ReadStringView(&magic_cookie, kStunMagicCookieLength)) { |
| 608 | return false; |
| 609 | } |
| 610 |
| 611 | std::string transaction_id; |
| 612 | if (!buf->ReadString(&transaction_id, kStunTransactionIdLength)) { |
| 613 | return false; |
| 614 | } |
| 615 |
| 616 | uint32_t magic_cookie_int; |
| 617 | static_assert(sizeof(magic_cookie_int) == kStunMagicCookieLength, |
| 618 | "Integer size mismatch: magic_cookie_int and kStunMagicCookie"); |
| 619 | std::memcpy(&magic_cookie_int, magic_cookie.data(), sizeof(magic_cookie_int)); |
| 620 | if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) { |
| 621 | // If magic cookie is invalid it means that the peer implements |
| 622 | // RFC3489 instead of RFC5389. |
| 623 | transaction_id.insert(0, magic_cookie); |
| 624 | } |
| 625 | RTC_DCHECK(IsValidTransactionId(transaction_id)); |
| 626 | transaction_id_ = transaction_id; |
| 627 | reduced_transaction_id_ = ReduceTransactionId(transaction_id_); |
| 628 |
| 629 | if (length_ != buf->Length()) { |
| 630 | return false; |
| 631 | } |
| 632 |
| 633 | attrs_.resize(0); |
| 634 |
| 635 | size_t rest = buf->Length() - length_; |
| 636 | while (buf->Length() > rest) { |
| 637 | uint16_t attr_type, attr_length; |
| 638 | if (!buf->ReadUInt16(&attr_type)) |
| 639 | return false; |
| 640 | if (!buf->ReadUInt16(&attr_length)) |
| 641 | return false; |
| 642 |
| 643 | std::unique_ptr<StunAttribute> attr( |
| 644 | CreateAttribute(attr_type, attr_length)); |
| 645 | if (!attr) { |
| 646 | // Skip any unknown or malformed attributes. |
| 647 | if ((attr_length % 4) != 0) { |
| 648 | attr_length += (4 - (attr_length % 4)); |
| 649 | } |
| 650 | if (!buf->Consume(attr_length)) { |
| 651 | return false; |
| 652 | } |
| 653 | } else { |
| 654 | if (!attr->Read(buf)) { |
| 655 | return false; |
| 656 | } |
| 657 | attrs_.push_back(std::move(attr)); |
| 658 | } |
| 659 | } |
| 660 |
| 661 | RTC_DCHECK(buf->Length() == rest); |
| 662 | return true; |
| 663 | } |
| 664 |
| 665 | bool StunMessage::Write(ByteBufferWriter* buf) const { |
| 666 | buf->WriteUInt16(type_); |
| 667 | buf->WriteUInt16(length_); |
| 668 | if (!IsLegacy()) |
| 669 | buf->WriteUInt32(stun_magic_cookie_); |
| 670 | buf->WriteString(transaction_id_); |
| 671 |
| 672 | for (const auto& attr : attrs_) { |
| 673 | buf->WriteUInt16(attr->type()); |
| 674 | buf->WriteUInt16(static_cast<uint16_t>(attr->length())); |
| 675 | if (!attr->Write(buf)) { |
| 676 | return false; |
| 677 | } |
| 678 | } |
| 679 |
| 680 | return true; |
| 681 | } |
| 682 |
| 683 | StunMessage* StunMessage::CreateNew() const { |
| 684 | return new StunMessage(); |
| 685 | } |
| 686 |
| 687 | void StunMessage::SetStunMagicCookie(uint32_t val) { |
| 688 | stun_magic_cookie_ = val; |
| 689 | } |
| 690 |
| 691 | void StunMessage::SetTransactionIdForTesting(absl::string_view transaction_id) { |
| 692 | RTC_DCHECK(IsValidTransactionId(transaction_id)); |
| 693 | transaction_id_ = std::string(transaction_id); |
| 694 | reduced_transaction_id_ = ReduceTransactionId(transaction_id_); |
| 695 | } |
| 696 |
| 697 | StunAttributeValueType StunMessage::GetAttributeValueType(int type) const { |
| 698 | switch (type) { |
| 699 | case STUN_ATTR_MAPPED_ADDRESS: |
| 700 | return STUN_VALUE_ADDRESS; |
| 701 | case STUN_ATTR_USERNAME: |
| 702 | return STUN_VALUE_BYTE_STRING; |
| 703 | case STUN_ATTR_MESSAGE_INTEGRITY: |
| 704 | return STUN_VALUE_BYTE_STRING; |
| 705 | case STUN_ATTR_ERROR_CODE: |
| 706 | return STUN_VALUE_ERROR_CODE; |
| 707 | case STUN_ATTR_UNKNOWN_ATTRIBUTES: |
| 708 | return STUN_VALUE_UINT16_LIST; |
| 709 | case STUN_ATTR_REALM: |
| 710 | return STUN_VALUE_BYTE_STRING; |
| 711 | case STUN_ATTR_NONCE: |
| 712 | return STUN_VALUE_BYTE_STRING; |
| 713 | case STUN_ATTR_XOR_MAPPED_ADDRESS: |
| 714 | return STUN_VALUE_XOR_ADDRESS; |
| 715 | case STUN_ATTR_SOFTWARE: |
| 716 | return STUN_VALUE_BYTE_STRING; |
| 717 | case STUN_ATTR_ALTERNATE_SERVER: |
| 718 | return STUN_VALUE_ADDRESS; |
| 719 | case STUN_ATTR_FINGERPRINT: |
| 720 | return STUN_VALUE_UINT32; |
| 721 | case STUN_ATTR_RETRANSMIT_COUNT: |
| 722 | return STUN_VALUE_UINT32; |
| 723 | case STUN_ATTR_GOOG_LAST_ICE_CHECK_RECEIVED: |
| 724 | return STUN_VALUE_BYTE_STRING; |
| 725 | case STUN_ATTR_GOOG_MISC_INFO: |
| 726 | return STUN_VALUE_UINT16_LIST; |
| 727 | case STUN_ATTR_GOOG_DELTA: |
| 728 | return STUN_VALUE_BYTE_STRING; |
| 729 | case STUN_ATTR_GOOG_DELTA_ACK: |
| 730 | return STUN_VALUE_UINT64; |
| 731 | default: |
| 732 | return STUN_VALUE_UNKNOWN; |
| 733 | } |
| 734 | } |
| 735 |
| 736 | StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ { |
| 737 | StunAttributeValueType value_type = GetAttributeValueType(type); |
| 738 | if (value_type != STUN_VALUE_UNKNOWN) { |
| 739 | return StunAttribute::Create(value_type, type, |
| 740 | static_cast<uint16_t>(length), this); |
| 741 | } else if (DesignatedExpertRange(type)) { |
| 742 | // Read unknown attributes as STUN_VALUE_BYTE_STRING |
| 743 | return StunAttribute::Create(STUN_VALUE_BYTE_STRING, type, |
| 744 | static_cast<uint16_t>(length), this); |
| 745 | } else { |
| 746 | return NULL; |
| 747 | } |
| 748 | } |
| 749 |
| 750 | const StunAttribute* StunMessage::GetAttribute(int type) const { |
| 751 | for (const auto& attr : attrs_) { |
| 752 | if (attr->type() == type) { |
| 753 | return attr.get(); |
| 754 | } |
| 755 | } |
| 756 | return NULL; |
| 757 | } |
| 758 |
| 759 | bool StunMessage::IsValidTransactionId(absl::string_view transaction_id) { |
| 760 | return transaction_id.size() == kStunTransactionIdLength || |
| 761 | transaction_id.size() == kStunLegacyTransactionIdLength; |
| 762 | } |
| 763 |
| 764 | bool StunMessage::EqualAttributes( |
| 765 | const StunMessage* other, |
| 766 | std::function<bool(int type)> attribute_type_mask) const { |
| 767 | RTC_DCHECK(other != nullptr); |
| 768 | rtc::ByteBufferWriter tmp_buffer_ptr1; |
| 769 | rtc::ByteBufferWriter tmp_buffer_ptr2; |
| 770 | for (const auto& attr : attrs_) { |
| 771 | if (attribute_type_mask(attr->type())) { |
| 772 | const StunAttribute* other_attr = other->GetAttribute(attr->type()); |
| 773 | if (other_attr == nullptr) { |
| 774 | return false; |
| 775 | } |
| 776 | tmp_buffer_ptr1.Clear(); |
| 777 | tmp_buffer_ptr2.Clear(); |
| 778 | attr->Write(&tmp_buffer_ptr1); |
| 779 | other_attr->Write(&tmp_buffer_ptr2); |
| 780 | if (tmp_buffer_ptr1.Length() != tmp_buffer_ptr2.Length()) { |
| 781 | return false; |
| 782 | } |
| 783 | if (memcmp(tmp_buffer_ptr1.Data(), tmp_buffer_ptr2.Data(), |
| 784 | tmp_buffer_ptr1.Length()) != 0) { |
| 785 | return false; |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 |
| 790 | for (const auto& attr : other->attrs_) { |
| 791 | if (attribute_type_mask(attr->type())) { |
| 792 | const StunAttribute* own_attr = GetAttribute(attr->type()); |
| 793 | if (own_attr == nullptr) { |
| 794 | return false; |
| 795 | } |
| 796 | // we have already compared all values... |
| 797 | } |
| 798 | } |
| 799 | return true; |
| 800 | } |
| 801 |
| 802 | // StunAttribute |
| 803 |
| 804 | StunAttribute::StunAttribute(uint16_t type, uint16_t length) |
| 805 | : type_(type), length_(length) {} |
| 806 |
| 807 | void StunAttribute::ConsumePadding(ByteBufferReader* buf) const { |
| 808 | int remainder = length_ % 4; |
| 809 | if (remainder > 0) { |
| 810 | buf->Consume(4 - remainder); |
| 811 | } |
| 812 | } |
| 813 |
| 814 | void StunAttribute::WritePadding(ByteBufferWriter* buf) const { |
| 815 | int remainder = length_ % 4; |
| 816 | if (remainder > 0) { |
| 817 | uint8_t zeroes[4] = {0}; |
| 818 | buf->WriteBytes(zeroes, 4 - remainder); |
| 819 | } |
| 820 | } |
| 821 |
| 822 | StunAttribute* StunAttribute::Create(StunAttributeValueType value_type, |
| 823 | uint16_t type, |
| 824 | uint16_t length, |
| 825 | StunMessage* owner) { |
| 826 | switch (value_type) { |
| 827 | case STUN_VALUE_ADDRESS: |
| 828 | return new StunAddressAttribute(type, length); |
| 829 | case STUN_VALUE_XOR_ADDRESS: |
| 830 | return new StunXorAddressAttribute(type, length, owner); |
| 831 | case STUN_VALUE_UINT32: |
| 832 | return new StunUInt32Attribute(type); |
| 833 | case STUN_VALUE_UINT64: |
| 834 | return new StunUInt64Attribute(type); |
| 835 | case STUN_VALUE_BYTE_STRING: |
| 836 | return new StunByteStringAttribute(type, length); |
| 837 | case STUN_VALUE_ERROR_CODE: |
| 838 | return new StunErrorCodeAttribute(type, length); |
| 839 | case STUN_VALUE_UINT16_LIST: |
| 840 | return new StunUInt16ListAttribute(type, length); |
| 841 | default: |
| 842 | return NULL; |
| 843 | } |
| 844 | } |
| 845 |
| 846 | std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress( |
| 847 | uint16_t type) { |
| 848 | return std::make_unique<StunAddressAttribute>(type, 0); |
| 849 | } |
| 850 |
| 851 | std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress( |
| 852 | uint16_t type) { |
| 853 | return std::make_unique<StunXorAddressAttribute>(type, 0, nullptr); |
| 854 | } |
| 855 |
| 856 | std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64( |
| 857 | uint16_t type) { |
| 858 | return std::make_unique<StunUInt64Attribute>(type); |
| 859 | } |
| 860 |
| 861 | std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32( |
| 862 | uint16_t type) { |
| 863 | return std::make_unique<StunUInt32Attribute>(type); |
| 864 | } |
| 865 |
| 866 | std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString( |
| 867 | uint16_t type) { |
| 868 | return std::make_unique<StunByteStringAttribute>(type, 0); |
| 869 | } |
| 870 |
| 871 | std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() { |
| 872 | return std::make_unique<StunErrorCodeAttribute>( |
| 873 | STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE); |
| 874 | } |
| 875 |
| 876 | std::unique_ptr<StunUInt16ListAttribute> |
| 877 | StunAttribute::CreateUInt16ListAttribute(uint16_t type) { |
| 878 | return std::make_unique<StunUInt16ListAttribute>(type, 0); |
| 879 | } |
| 880 |
| 881 | std::unique_ptr<StunUInt16ListAttribute> |
| 882 | StunAttribute::CreateUnknownAttributes() { |
| 883 | return std::make_unique<StunUInt16ListAttribute>(STUN_ATTR_UNKNOWN_ATTRIBUTES, |
| 884 | 0); |
| 885 | } |
| 886 |
| 887 | StunAddressAttribute::StunAddressAttribute(uint16_t type, |
| 888 | const rtc::SocketAddress& addr) |
| 889 | : StunAttribute(type, 0) { |
| 890 | SetAddress(addr); |
| 891 | } |
| 892 |
| 893 | StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length) |
| 894 | : StunAttribute(type, length) {} |
| 895 |
| 896 | StunAttributeValueType StunAddressAttribute::value_type() const { |
| 897 | return STUN_VALUE_ADDRESS; |
| 898 | } |
| 899 |
| 900 | bool StunAddressAttribute::Read(ByteBufferReader* buf) { |
| 901 | uint8_t dummy; |
| 902 | if (!buf->ReadUInt8(&dummy)) |
| 903 | return false; |
| 904 |
| 905 | uint8_t stun_family; |
| 906 | if (!buf->ReadUInt8(&stun_family)) { |
| 907 | return false; |
| 908 | } |
| 909 | uint16_t port; |
| 910 | if (!buf->ReadUInt16(&port)) |
| 911 | return false; |
| 912 | if (stun_family == STUN_ADDRESS_IPV4) { |
| 913 | in_addr v4addr; |
| 914 | if (length() != SIZE_IP4) { |
| 915 | return false; |
| 916 | } |
| 917 | if (!buf->ReadBytes(rtc::MakeArrayView(reinterpret_cast<uint8_t*>(&v4addr), |
| 918 | sizeof(v4addr)))) { |
| 919 | return false; |
| 920 | } |
| 921 | rtc::IPAddress ipaddr(v4addr); |
| 922 | SetAddress(rtc::SocketAddress(ipaddr, port)); |
| 923 | } else if (stun_family == STUN_ADDRESS_IPV6) { |
| 924 | in6_addr v6addr; |
| 925 | if (length() != SIZE_IP6) { |
| 926 | return false; |
| 927 | } |
| 928 | if (!buf->ReadBytes(rtc::MakeArrayView(reinterpret_cast<uint8_t*>(&v6addr), |
| 929 | sizeof(v6addr)))) { |
| 930 | return false; |
| 931 | } |
| 932 | rtc::IPAddress ipaddr(v6addr); |
| 933 | SetAddress(rtc::SocketAddress(ipaddr, port)); |
| 934 | } else { |
| 935 | return false; |
| 936 | } |
| 937 | return true; |
| 938 | } |
| 939 |
| 940 | bool StunAddressAttribute::Write(ByteBufferWriter* buf) const { |
| 941 | StunAddressFamily address_family = family(); |
| 942 | if (address_family == STUN_ADDRESS_UNDEF) { |
| 943 | RTC_LOG(LS_ERROR) << "Error writing address attribute: unknown family."; |
| 944 | return false; |
| 945 | } |
| 946 | buf->WriteUInt8(0); |
| 947 | buf->WriteUInt8(address_family); |
| 948 | buf->WriteUInt16(address_.port()); |
| 949 | switch (address_.family()) { |
| 950 | case AF_INET: { |
| 951 | in_addr v4addr = address_.ipaddr().ipv4_address(); |
| 952 | buf->WriteBytes(reinterpret_cast<uint8_t*>(&v4addr), sizeof(v4addr)); |
| 953 | break; |
| 954 | } |
| 955 | case AF_INET6: { |
| 956 | in6_addr v6addr = address_.ipaddr().ipv6_address(); |
| 957 | buf->WriteBytes(reinterpret_cast<uint8_t*>(&v6addr), sizeof(v6addr)); |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | return true; |
| 962 | } |
| 963 |
| 964 | StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type, |
| 965 | const rtc::SocketAddress& addr) |
| 966 | : StunAddressAttribute(type, addr), owner_(NULL) {} |
| 967 |
| 968 | StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type, |
| 969 | uint16_t length, |
| 970 | StunMessage* owner) |
| 971 | : StunAddressAttribute(type, length), owner_(owner) {} |
| 972 |
| 973 | StunAttributeValueType StunXorAddressAttribute::value_type() const { |
| 974 | return STUN_VALUE_XOR_ADDRESS; |
| 975 | } |
| 976 |
| 977 | void StunXorAddressAttribute::SetOwner(StunMessage* owner) { |
| 978 | owner_ = owner; |
| 979 | } |
| 980 |
| 981 | rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const { |
| 982 | if (owner_) { |
| 983 | rtc::IPAddress ip = ipaddr(); |
| 984 | switch (ip.family()) { |
| 985 | case AF_INET: { |
| 986 | in_addr v4addr = ip.ipv4_address(); |
| 987 | v4addr.s_addr = |
| 988 | (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie)); |
| 989 | return rtc::IPAddress(v4addr); |
| 990 | } |
| 991 | case AF_INET6: { |
| 992 | in6_addr v6addr = ip.ipv6_address(); |
| 993 | const std::string& transaction_id = owner_->transaction_id(); |
| 994 | if (transaction_id.length() == kStunTransactionIdLength) { |
| 995 | uint32_t transactionid_as_ints[3]; |
| 996 | memcpy(&transactionid_as_ints[0], transaction_id.c_str(), |
| 997 | transaction_id.length()); |
| 998 | uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr); |
| 999 | // Transaction ID is in network byte order, but magic cookie |
| 1000 | // is stored in host byte order. |
| 1001 | ip_as_ints[0] = |
| 1002 | (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie)); |
| 1003 | ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]); |
| 1004 | ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]); |
| 1005 | ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]); |
| 1006 | return rtc::IPAddress(v6addr); |
| 1007 | } |
| 1008 | break; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | // Invalid ip family or transaction ID, or missing owner. |
| 1013 | // Return an AF_UNSPEC address. |
| 1014 | return rtc::IPAddress(); |
| 1015 | } |
| 1016 |
| 1017 | bool StunXorAddressAttribute::Read(ByteBufferReader* buf) { |
| 1018 | if (!StunAddressAttribute::Read(buf)) |
| 1019 | return false; |
| 1020 | uint16_t xoredport = port() ^ (kStunMagicCookie >> 16); |
| 1021 | rtc::IPAddress xored_ip = GetXoredIP(); |
| 1022 | SetAddress(rtc::SocketAddress(xored_ip, xoredport)); |
| 1023 | return true; |
| 1024 | } |
| 1025 |
| 1026 | bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const { |
| 1027 | StunAddressFamily address_family = family(); |
| 1028 | if (address_family == STUN_ADDRESS_UNDEF) { |
| 1029 | RTC_LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family."; |
| 1030 | return false; |
| 1031 | } |
| 1032 | rtc::IPAddress xored_ip = GetXoredIP(); |
| 1033 | if (xored_ip.family() == AF_UNSPEC) { |
| 1034 | return false; |
| 1035 | } |
| 1036 | buf->WriteUInt8(0); |
| 1037 | buf->WriteUInt8(family()); |
| 1038 | buf->WriteUInt16(port() ^ (kStunMagicCookie >> 16)); |
| 1039 | switch (xored_ip.family()) { |
| 1040 | case AF_INET: { |
| 1041 | in_addr v4addr = xored_ip.ipv4_address(); |
| 1042 | buf->WriteBytes(reinterpret_cast<const uint8_t*>(&v4addr), |
| 1043 | sizeof(v4addr)); |
| 1044 | break; |
| 1045 | } |
| 1046 | case AF_INET6: { |
| 1047 | in6_addr v6addr = xored_ip.ipv6_address(); |
| 1048 | buf->WriteBytes(reinterpret_cast<const uint8_t*>(&v6addr), |
| 1049 | sizeof(v6addr)); |
| 1050 | break; |
| 1051 | } |
| 1052 | } |
| 1053 | return true; |
| 1054 | } |
| 1055 |
| 1056 | StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value) |
| 1057 | : StunAttribute(type, SIZE), bits_(value) {} |
| 1058 |
| 1059 | StunUInt32Attribute::StunUInt32Attribute(uint16_t type) |
| 1060 | : StunAttribute(type, SIZE), bits_(0) {} |
| 1061 |
| 1062 | StunAttributeValueType StunUInt32Attribute::value_type() const { |
| 1063 | return STUN_VALUE_UINT32; |
| 1064 | } |
| 1065 |
| 1066 | bool StunUInt32Attribute::GetBit(size_t index) const { |
| 1067 | RTC_DCHECK(index < 32); |
| 1068 | return static_cast<bool>((bits_ >> index) & 0x1); |
| 1069 | } |
| 1070 |
| 1071 | void StunUInt32Attribute::SetBit(size_t index, bool value) { |
| 1072 | RTC_DCHECK(index < 32); |
| 1073 | bits_ &= ~(1 << index); |
| 1074 | bits_ |= value ? (1 << index) : 0; |
| 1075 | } |
| 1076 |
| 1077 | bool StunUInt32Attribute::Read(ByteBufferReader* buf) { |
| 1078 | if (length() != SIZE || !buf->ReadUInt32(&bits_)) |
| 1079 | return false; |
| 1080 | return true; |
| 1081 | } |
| 1082 |
| 1083 | bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const { |
| 1084 | buf->WriteUInt32(bits_); |
| 1085 | return true; |
| 1086 | } |
| 1087 |
| 1088 | StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value) |
| 1089 | : StunAttribute(type, SIZE), bits_(value) {} |
| 1090 |
| 1091 | StunUInt64Attribute::StunUInt64Attribute(uint16_t type) |
| 1092 | : StunAttribute(type, SIZE), bits_(0) {} |
| 1093 |
| 1094 | StunAttributeValueType StunUInt64Attribute::value_type() const { |
| 1095 | return STUN_VALUE_UINT64; |
| 1096 | } |
| 1097 |
| 1098 | bool StunUInt64Attribute::Read(ByteBufferReader* buf) { |
| 1099 | if (length() != SIZE || !buf->ReadUInt64(&bits_)) |
| 1100 | return false; |
| 1101 | return true; |
| 1102 | } |
| 1103 |
| 1104 | bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const { |
| 1105 | buf->WriteUInt64(bits_); |
| 1106 | return true; |
| 1107 | } |
| 1108 |
| 1109 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type) |
| 1110 | : StunAttribute(type, 0), bytes_(NULL) {} |
| 1111 |
| 1112 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, |
| 1113 | absl::string_view str) |
| 1114 | : StunAttribute(type, 0), bytes_(NULL) { |
| 1115 | CopyBytes(str); |
| 1116 | } |
| 1117 |
| 1118 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, |
| 1119 | const void* bytes, |
| 1120 | size_t length) |
| 1121 | : StunAttribute(type, 0), bytes_(NULL) { |
| 1122 | CopyBytes(bytes, length); |
| 1123 | } |
| 1124 |
| 1125 | StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length) |
| 1126 | : StunAttribute(type, length), bytes_(NULL) {} |
| 1127 |
| 1128 | StunByteStringAttribute::~StunByteStringAttribute() { |
| 1129 | delete[] bytes_; |
| 1130 | } |
| 1131 |
| 1132 | StunAttributeValueType StunByteStringAttribute::value_type() const { |
| 1133 | return STUN_VALUE_BYTE_STRING; |
| 1134 | } |
| 1135 |
| 1136 | void StunByteStringAttribute::CopyBytes(absl::string_view bytes) { |
| 1137 | uint8_t* new_bytes = new uint8_t[bytes.size()]; |
| 1138 | memcpy(new_bytes, bytes.data(), bytes.size()); |
| 1139 | SetBytes(new_bytes, bytes.size()); |
| 1140 | } |
| 1141 |
| 1142 | void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) { |
| 1143 | uint8_t* new_bytes = new uint8_t[length]; |
| 1144 | memcpy(new_bytes, bytes, length); |
| 1145 | SetBytes(new_bytes, length); |
| 1146 | } |
| 1147 |
| 1148 | uint8_t StunByteStringAttribute::GetByte(size_t index) const { |
| 1149 | RTC_DCHECK(bytes_ != NULL); |
| 1150 | RTC_DCHECK(index < length()); |
| 1151 | return bytes_[index]; |
| 1152 | } |
| 1153 |
| 1154 | void StunByteStringAttribute::SetByte(size_t index, uint8_t value) { |
| 1155 | RTC_DCHECK(bytes_ != NULL); |
| 1156 | RTC_DCHECK(index < length()); |
| 1157 | bytes_[index] = value; |
| 1158 | } |
| 1159 |
| 1160 | bool StunByteStringAttribute::Read(ByteBufferReader* buf) { |
| 1161 | bytes_ = new uint8_t[length()]; |
| 1162 | if (!buf->ReadBytes(rtc::ArrayView<uint8_t>(bytes_, length()))) { |
| 1163 | return false; |
| 1164 | } |
| 1165 |
| 1166 | ConsumePadding(buf); |
| 1167 | return true; |
| 1168 | } |
| 1169 |
| 1170 | bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const { |
| 1171 | // Check that length is legal according to specs |
| 1172 | if (!LengthValid(type(), length())) { |
| 1173 | return false; |
| 1174 | } |
| 1175 | buf->WriteBytes(bytes_, length()); |
| 1176 | WritePadding(buf); |
| 1177 | return true; |
| 1178 | } |
| 1179 |
| 1180 | void StunByteStringAttribute::SetBytes(uint8_t* bytes, size_t length) { |
| 1181 | delete[] bytes_; |
| 1182 | bytes_ = bytes; |
| 1183 | SetLength(static_cast<uint16_t>(length)); |
| 1184 | } |
| 1185 |
| 1186 | const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4; |
| 1187 |
| 1188 | StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, |
| 1189 | int code, |
| 1190 | const std::string& reason) |
| 1191 | : StunAttribute(type, 0) { |
| 1192 | SetCode(code); |
| 1193 | SetReason(reason); |
| 1194 | } |
| 1195 |
| 1196 | StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length) |
| 1197 | : StunAttribute(type, length), class_(0), number_(0) {} |
| 1198 |
| 1199 | StunErrorCodeAttribute::~StunErrorCodeAttribute() {} |
| 1200 |
| 1201 | StunAttributeValueType StunErrorCodeAttribute::value_type() const { |
| 1202 | return STUN_VALUE_ERROR_CODE; |
| 1203 | } |
| 1204 |
| 1205 | int StunErrorCodeAttribute::code() const { |
| 1206 | return class_ * 100 + number_; |
| 1207 | } |
| 1208 |
| 1209 | void StunErrorCodeAttribute::SetCode(int code) { |
| 1210 | class_ = static_cast<uint8_t>(code / 100); |
| 1211 | number_ = static_cast<uint8_t>(code % 100); |
| 1212 | } |
| 1213 |
| 1214 | void StunErrorCodeAttribute::SetReason(const std::string& reason) { |
| 1215 | SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size())); |
| 1216 | reason_ = reason; |
| 1217 | } |
| 1218 |
| 1219 | bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) { |
| 1220 | uint32_t val; |
| 1221 | if (length() < MIN_SIZE || !buf->ReadUInt32(&val)) |
| 1222 | return false; |
| 1223 |
| 1224 | if ((val >> 11) != 0) |
| 1225 | RTC_LOG(LS_ERROR) << "error-code bits not zero"; |
| 1226 |
| 1227 | class_ = ((val >> 8) & 0x7); |
| 1228 | number_ = (val & 0xff); |
| 1229 |
| 1230 | if (!buf->ReadString(&reason_, length() - 4)) |
| 1231 | return false; |
| 1232 |
| 1233 | ConsumePadding(buf); |
| 1234 | return true; |
| 1235 | } |
| 1236 |
| 1237 | bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const { |
| 1238 | buf->WriteUInt32(class_ << 8 | number_); |
| 1239 | buf->WriteString(reason_); |
| 1240 | WritePadding(buf); |
| 1241 | return true; |
| 1242 | } |
| 1243 |
| 1244 | StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length) |
| 1245 | : StunAttribute(type, length) { |
| 1246 | attr_types_ = new std::vector<uint16_t>(); |
| 1247 | } |
| 1248 |
| 1249 | StunUInt16ListAttribute::~StunUInt16ListAttribute() { |
| 1250 | delete attr_types_; |
| 1251 | } |
| 1252 |
| 1253 | StunAttributeValueType StunUInt16ListAttribute::value_type() const { |
| 1254 | return STUN_VALUE_UINT16_LIST; |
| 1255 | } |
| 1256 |
| 1257 | size_t StunUInt16ListAttribute::Size() const { |
| 1258 | return attr_types_->size(); |
| 1259 | } |
| 1260 |
| 1261 | uint16_t StunUInt16ListAttribute::GetType(int index) const { |
| 1262 | return (*attr_types_)[index]; |
| 1263 | } |
| 1264 |
| 1265 | void StunUInt16ListAttribute::SetType(int index, uint16_t value) { |
| 1266 | (*attr_types_)[index] = value; |
| 1267 | } |
| 1268 |
| 1269 | void StunUInt16ListAttribute::AddType(uint16_t value) { |
| 1270 | attr_types_->push_back(value); |
| 1271 | SetLength(static_cast<uint16_t>(attr_types_->size() * 2)); |
| 1272 | } |
| 1273 |
| 1274 | void StunUInt16ListAttribute::AddTypeAtIndex(uint16_t index, uint16_t value) { |
| 1275 | if (attr_types_->size() < static_cast<size_t>(index + 1)) { |
| 1276 | attr_types_->resize(index + 1); |
| 1277 | } |
| 1278 | (*attr_types_)[index] = value; |
| 1279 | SetLength(static_cast<uint16_t>(attr_types_->size() * 2)); |
| 1280 | } |
| 1281 |
| 1282 | bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) { |
| 1283 | if (length() % 2) { |
| 1284 | return false; |
| 1285 | } |
| 1286 |
| 1287 | for (size_t i = 0; i < length() / 2; i++) { |
| 1288 | uint16_t attr; |
| 1289 | if (!buf->ReadUInt16(&attr)) |
| 1290 | return false; |
| 1291 | attr_types_->push_back(attr); |
| 1292 | } |
| 1293 | // Padding of these attributes is done in RFC 5389 style. This is |
| 1294 | // slightly different from RFC3489, but it shouldn't be important. |
| 1295 | // RFC3489 pads out to a 32 bit boundary by duplicating one of the |
| 1296 | // entries in the list (not necessarily the last one - it's unspecified). |
| 1297 | // RFC5389 pads on the end, and the bytes are always ignored. |
| 1298 | ConsumePadding(buf); |
| 1299 | return true; |
| 1300 | } |
| 1301 |
| 1302 | bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const { |
| 1303 | for (size_t i = 0; i < attr_types_->size(); ++i) { |
| 1304 | buf->WriteUInt16((*attr_types_)[i]); |
| 1305 | } |
| 1306 | WritePadding(buf); |
| 1307 | return true; |
| 1308 | } |
| 1309 |
| 1310 | std::string StunMethodToString(int msg_type) { |
| 1311 | switch (msg_type) { |
| 1312 | case STUN_BINDING_REQUEST: |
| 1313 | return "STUN BINDING request"; |
| 1314 | case STUN_BINDING_INDICATION: |
| 1315 | return "STUN BINDING indication"; |
| 1316 | case STUN_BINDING_RESPONSE: |
| 1317 | return "STUN BINDING response"; |
| 1318 | case STUN_BINDING_ERROR_RESPONSE: |
| 1319 | return "STUN BINDING error response"; |
| 1320 | case GOOG_PING_REQUEST: |
| 1321 | return "GOOG PING request"; |
| 1322 | case GOOG_PING_RESPONSE: |
| 1323 | return "GOOG PING response"; |
| 1324 | case GOOG_PING_ERROR_RESPONSE: |
| 1325 | return "GOOG PING error response"; |
| 1326 | case STUN_ALLOCATE_REQUEST: |
| 1327 | return "TURN ALLOCATE request"; |
| 1328 | case STUN_ALLOCATE_RESPONSE: |
| 1329 | return "TURN ALLOCATE response"; |
| 1330 | case STUN_ALLOCATE_ERROR_RESPONSE: |
| 1331 | return "TURN ALLOCATE error response"; |
| 1332 | case TURN_REFRESH_REQUEST: |
| 1333 | return "TURN REFRESH request"; |
| 1334 | case TURN_REFRESH_RESPONSE: |
| 1335 | return "TURN REFRESH response"; |
| 1336 | case TURN_REFRESH_ERROR_RESPONSE: |
| 1337 | return "TURN REFRESH error response"; |
| 1338 | case TURN_SEND_INDICATION: |
| 1339 | return "TURN SEND INDICATION"; |
| 1340 | case TURN_DATA_INDICATION: |
| 1341 | return "TURN DATA INDICATION"; |
| 1342 | case TURN_CREATE_PERMISSION_REQUEST: |
| 1343 | return "TURN CREATE PERMISSION request"; |
| 1344 | case TURN_CREATE_PERMISSION_RESPONSE: |
| 1345 | return "TURN CREATE PERMISSION response"; |
| 1346 | case TURN_CREATE_PERMISSION_ERROR_RESPONSE: |
| 1347 | return "TURN CREATE PERMISSION error response"; |
| 1348 | case TURN_CHANNEL_BIND_REQUEST: |
| 1349 | return "TURN CHANNEL BIND request"; |
| 1350 | case TURN_CHANNEL_BIND_RESPONSE: |
| 1351 | return "TURN CHANNEL BIND response"; |
| 1352 | case TURN_CHANNEL_BIND_ERROR_RESPONSE: |
| 1353 | return "TURN CHANNEL BIND error response"; |
| 1354 | default: |
| 1355 | return "UNKNOWN<" + std::to_string(msg_type) + ">"; |
| 1356 | } |
| 1357 | } |
| 1358 |
| 1359 | int GetStunSuccessResponseType(int req_type) { |
| 1360 | return IsStunRequestType(req_type) ? (req_type | 0x100) : -1; |
| 1361 | } |
| 1362 |
| 1363 | int GetStunErrorResponseType(int req_type) { |
| 1364 | return IsStunRequestType(req_type) ? (req_type | 0x110) : -1; |
| 1365 | } |
| 1366 |
| 1367 | bool IsStunRequestType(int msg_type) { |
| 1368 | return ((msg_type & kStunTypeMask) == 0x000); |
| 1369 | } |
| 1370 |
| 1371 | bool IsStunIndicationType(int msg_type) { |
| 1372 | return ((msg_type & kStunTypeMask) == 0x010); |
| 1373 | } |
| 1374 |
| 1375 | bool IsStunSuccessResponseType(int msg_type) { |
| 1376 | return ((msg_type & kStunTypeMask) == 0x100); |
| 1377 | } |
| 1378 |
| 1379 | bool IsStunErrorResponseType(int msg_type) { |
| 1380 | return ((msg_type & kStunTypeMask) == 0x110); |
| 1381 | } |
| 1382 |
| 1383 | bool ComputeStunCredentialHash(const std::string& username, |
| 1384 | const std::string& realm, |
| 1385 | const std::string& password, |
| 1386 | std::string* hash) { |
| 1387 | // http://tools.ietf.org/html/rfc5389#section-15.4 |
| 1388 | // long-term credentials will be calculated using the key and key is |
| 1389 | // key = MD5(username ":" realm ":" SASLprep(password)) |
| 1390 | std::string input = username; |
| 1391 | input += ':'; |
| 1392 | input += realm; |
| 1393 | input += ':'; |
| 1394 | input += password; |
| 1395 |
| 1396 | char digest[rtc::MessageDigest::kMaxSize]; |
| 1397 | size_t size = rtc::ComputeDigest(rtc::DIGEST_MD5, input.c_str(), input.size(), |
| 1398 | digest, sizeof(digest)); |
| 1399 | if (size == 0) { |
| 1400 | return false; |
| 1401 | } |
| 1402 |
| 1403 | *hash = std::string(digest, size); |
| 1404 | return true; |
| 1405 | } |
| 1406 |
| 1407 | std::unique_ptr<StunAttribute> CopyStunAttribute( |
| 1408 | const StunAttribute& attribute, |
| 1409 | rtc::ByteBufferWriter* tmp_buffer_ptr) { |
| 1410 | ByteBufferWriter tmpBuffer; |
| 1411 | if (tmp_buffer_ptr == nullptr) { |
| 1412 | tmp_buffer_ptr = &tmpBuffer; |
| 1413 | } |
| 1414 |
| 1415 | std::unique_ptr<StunAttribute> copy(StunAttribute::Create( |
| 1416 | attribute.value_type(), attribute.type(), |
| 1417 | static_cast<uint16_t>(attribute.length()), nullptr)); |
| 1418 |
| 1419 | if (!copy) { |
| 1420 | return nullptr; |
| 1421 | } |
| 1422 | tmp_buffer_ptr->Clear(); |
| 1423 | if (!attribute.Write(tmp_buffer_ptr)) { |
| 1424 | return nullptr; |
| 1425 | } |
| 1426 | rtc::ByteBufferReader reader(*tmp_buffer_ptr); |
| 1427 | if (!copy->Read(&reader)) { |
| 1428 | return nullptr; |
| 1429 | } |
| 1430 |
| 1431 | return copy; |
| 1432 | } |
| 1433 |
| 1434 | StunAttributeValueType RelayMessage::GetAttributeValueType(int type) const { |
| 1435 | switch (type) { |
| 1436 | case STUN_ATTR_LIFETIME: |
| 1437 | return STUN_VALUE_UINT32; |
| 1438 | case STUN_ATTR_MAGIC_COOKIE: |
| 1439 | return STUN_VALUE_BYTE_STRING; |
| 1440 | case STUN_ATTR_BANDWIDTH: |
| 1441 | return STUN_VALUE_UINT32; |
| 1442 | case STUN_ATTR_DESTINATION_ADDRESS: |
| 1443 | return STUN_VALUE_ADDRESS; |
| 1444 | case STUN_ATTR_SOURCE_ADDRESS2: |
| 1445 | return STUN_VALUE_ADDRESS; |
| 1446 | case STUN_ATTR_DATA: |
| 1447 | return STUN_VALUE_BYTE_STRING; |
| 1448 | case STUN_ATTR_OPTIONS: |
| 1449 | return STUN_VALUE_UINT32; |
| 1450 | default: |
| 1451 | return StunMessage::GetAttributeValueType(type); |
| 1452 | } |
| 1453 | } |
| 1454 |
| 1455 | StunMessage* RelayMessage::CreateNew() const { |
| 1456 | return new RelayMessage(); |
| 1457 | } |
| 1458 |
| 1459 | StunAttributeValueType TurnMessage::GetAttributeValueType(int type) const { |
| 1460 | switch (type) { |
| 1461 | case STUN_ATTR_CHANNEL_NUMBER: |
| 1462 | return STUN_VALUE_UINT32; |
| 1463 | case STUN_ATTR_TURN_LIFETIME: |
| 1464 | return STUN_VALUE_UINT32; |
| 1465 | case STUN_ATTR_XOR_PEER_ADDRESS: |
| 1466 | return STUN_VALUE_XOR_ADDRESS; |
| 1467 | case STUN_ATTR_DATA: |
| 1468 | return STUN_VALUE_BYTE_STRING; |
| 1469 | case STUN_ATTR_XOR_RELAYED_ADDRESS: |
| 1470 | return STUN_VALUE_XOR_ADDRESS; |
| 1471 | case STUN_ATTR_EVEN_PORT: |
| 1472 | return STUN_VALUE_BYTE_STRING; |
| 1473 | case STUN_ATTR_REQUESTED_TRANSPORT: |
| 1474 | return STUN_VALUE_UINT32; |
| 1475 | case STUN_ATTR_DONT_FRAGMENT: |
| 1476 | return STUN_VALUE_BYTE_STRING; |
| 1477 | case STUN_ATTR_RESERVATION_TOKEN: |
| 1478 | return STUN_VALUE_BYTE_STRING; |
| 1479 | default: |
| 1480 | return StunMessage::GetAttributeValueType(type); |
| 1481 | } |
| 1482 | } |
| 1483 |
| 1484 | StunMessage* TurnMessage::CreateNew() const { |
| 1485 | return new TurnMessage(); |
| 1486 | } |
| 1487 |
| 1488 | StunAttributeValueType IceMessage::GetAttributeValueType(int type) const { |
| 1489 | switch (type) { |
| 1490 | case STUN_ATTR_PRIORITY: |
| 1491 | case STUN_ATTR_GOOG_NETWORK_INFO: |
| 1492 | case STUN_ATTR_NOMINATION: |
| 1493 | return STUN_VALUE_UINT32; |
| 1494 | case STUN_ATTR_USE_CANDIDATE: |
| 1495 | return STUN_VALUE_BYTE_STRING; |
| 1496 | case STUN_ATTR_ICE_CONTROLLED: |
| 1497 | return STUN_VALUE_UINT64; |
| 1498 | case STUN_ATTR_ICE_CONTROLLING: |
| 1499 | return STUN_VALUE_UINT64; |
| 1500 | default: |
| 1501 | return StunMessage::GetAttributeValueType(type); |
| 1502 | } |
| 1503 | } |
| 1504 |
| 1505 | StunMessage* IceMessage::CreateNew() const { |
| 1506 | return new IceMessage(); |
| 1507 | } |
| 1508 |
| 1509 | std::unique_ptr<StunMessage> StunMessage::Clone() const { |
| 1510 | std::unique_ptr<StunMessage> copy(CreateNew()); |
| 1511 | if (!copy) { |
| 1512 | return nullptr; |
| 1513 | } |
| 1514 | rtc::ByteBufferWriter buf; |
| 1515 | if (!Write(&buf)) { |
| 1516 | return nullptr; |
| 1517 | } |
| 1518 | rtc::ByteBufferReader reader(buf); |
| 1519 | if (!copy->Read(&reader)) { |
| 1520 | return nullptr; |
| 1521 | } |
| 1522 | return copy; |
| 1523 | } |
| 1524 |
| 1525 | } // namespace cricket |
| 1526 |