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