| 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 "p2p/base/stun_dictionary.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <deque> |
| 15 | #include <utility> |
| 16 |
| 17 | #include "rtc_base/logging.h" |
| 18 |
| 19 | namespace cricket { |
| 20 |
| 21 | const StunAddressAttribute* StunDictionaryView::GetAddress(int key) const { |
| 22 | const StunAttribute* attr = GetOrNull(key, STUN_VALUE_ADDRESS); |
| 23 | if (attr == nullptr) { |
| 24 | return nullptr; |
| 25 | } |
| 26 | return reinterpret_cast<const StunAddressAttribute*>(attr); |
| 27 | } |
| 28 |
| 29 | const StunUInt32Attribute* StunDictionaryView::GetUInt32(int key) const { |
| 30 | const StunAttribute* attr = GetOrNull(key, STUN_VALUE_UINT32); |
| 31 | if (attr == nullptr) { |
| 32 | return nullptr; |
| 33 | } |
| 34 | return reinterpret_cast<const StunUInt32Attribute*>(attr); |
| 35 | } |
| 36 |
| 37 | const StunUInt64Attribute* StunDictionaryView::GetUInt64(int key) const { |
| 38 | const StunAttribute* attr = GetOrNull(key, STUN_VALUE_UINT64); |
| 39 | if (attr == nullptr) { |
| 40 | return nullptr; |
| 41 | } |
| 42 | return reinterpret_cast<const StunUInt64Attribute*>(attr); |
| 43 | } |
| 44 |
| 45 | const StunByteStringAttribute* StunDictionaryView::GetByteString( |
| 46 | int key) const { |
| 47 | const StunAttribute* attr = GetOrNull(key, STUN_VALUE_BYTE_STRING); |
| 48 | if (attr == nullptr) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | return reinterpret_cast<const StunByteStringAttribute*>(attr); |
| 52 | } |
| 53 |
| 54 | const StunUInt16ListAttribute* StunDictionaryView::GetUInt16List( |
| 55 | int key) const { |
| 56 | const StunAttribute* attr = GetOrNull(key, STUN_VALUE_UINT16_LIST); |
| 57 | if (attr == nullptr) { |
| 58 | return nullptr; |
| 59 | } |
| 60 | return reinterpret_cast<const StunUInt16ListAttribute*>(attr); |
| 61 | } |
| 62 |
| 63 | const StunAttribute* StunDictionaryView::GetOrNull( |
| 64 | int key, |
| 65 | absl::optional<StunAttributeValueType> type) const { |
| 66 | const auto it = attrs_.find(key); |
| 67 | if (it == attrs_.end()) { |
| 68 | return nullptr; |
| 69 | } |
| 70 |
| 71 | if (type && it->second->value_type() != *type) { |
| 72 | RTC_LOG(LS_WARNING) << "Get key: " << key << " with type: " << *type |
| 73 | << " found different type: " |
| 74 | << it->second->value_type(); |
| 75 | return nullptr; |
| 76 | } |
| 77 | return (*it).second.get(); |
| 78 | } |
| 79 |
| 80 | webrtc::RTCErrorOr< |
| 81 | std::pair<uint64_t, std::deque<std::unique_ptr<StunAttribute>>>> |
| 82 | StunDictionaryView::ParseDelta(const StunByteStringAttribute& delta) { |
| 83 | rtc::ByteBufferReader buf(delta.array_view()); |
| 84 | uint16_t magic; |
| 85 | if (!buf.ReadUInt16(&magic)) { |
| 86 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 87 | "Failed to read magic number"); |
| 88 | } |
| 89 | if (magic != kDeltaMagic) { |
| 90 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 91 | "Invalid magic number"); |
| 92 | } |
| 93 |
| 94 | uint16_t delta_version; |
| 95 | if (!buf.ReadUInt16(&delta_version)) { |
| 96 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 97 | "Failed to read version"); |
| 98 | } |
| 99 |
| 100 | if (delta_version != kDeltaVersion) { |
| 101 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 102 | "Unsupported delta version"); |
| 103 | } |
| 104 |
| 105 | // Now read all the attributes |
| 106 | std::deque<std::unique_ptr<StunAttribute>> attrs; |
| 107 | while (buf.Length()) { |
| 108 | uint16_t key, length, value_type; |
| 109 | if (!buf.ReadUInt16(&key)) { |
| 110 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 111 | "Failed to read attribute key"); |
| 112 | } |
| 113 | if (!buf.ReadUInt16(&length)) { |
| 114 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 115 | "Failed to read attribute length"); |
| 116 | } |
| 117 | if (!buf.ReadUInt16(&value_type)) { |
| 118 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 119 | "Failed to read value type"); |
| 120 | } |
| 121 |
| 122 | StunAttributeValueType value_type_enum = |
| 123 | static_cast<StunAttributeValueType>(value_type); |
| 124 | std::unique_ptr<StunAttribute> attr( |
| 125 | StunAttribute::Create(value_type_enum, key, length, nullptr)); |
| 126 | if (!attr) { |
| 127 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 128 | "Failed to create attribute"); |
| 129 | } |
| 130 | if (attr->length() != length) { |
| 131 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 132 | "Inconsistent attribute length"); |
| 133 | } |
| 134 | if (!attr->Read(&buf)) { |
| 135 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 136 | "Failed to read attribute content"); |
| 137 | } |
| 138 | attrs.push_back(std::move(attr)); |
| 139 | } |
| 140 |
| 141 | // The first attribute should be the version... |
| 142 | if (attrs.empty()) { |
| 143 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 144 | "Empty delta!"); |
| 145 | } |
| 146 |
| 147 | if (attrs[0]->type() != kVersionKey || |
| 148 | attrs[0]->value_type() != STUN_VALUE_UINT64) { |
| 149 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 150 | "Missing version!"); |
| 151 | } |
| 152 |
| 153 | uint64_t version_in_delta = |
| 154 | reinterpret_cast<const StunUInt64Attribute*>(attrs[0].get())->value(); |
| 155 | attrs.pop_front(); |
| 156 |
| 157 | return std::make_pair(std::max(version_in_delta, version_in_delta), |
| 158 | std::move(attrs)); |
| 159 | } |
| 160 |
| 161 | // Apply a delta return an StunUInt64Attribute to ack the update. |
| 162 | webrtc::RTCErrorOr< |
| 163 | std::pair<std::unique_ptr<StunUInt64Attribute>, std::vector<uint16_t>>> |
| 164 | StunDictionaryView::ApplyDelta(const StunByteStringAttribute& delta) { |
| 165 | auto parsed_delta = ParseDelta(delta); |
| 166 | if (!parsed_delta.ok()) { |
| 167 | return webrtc::RTCError(parsed_delta.error()); |
| 168 | } |
| 169 |
| 170 | uint64_t version_in_delta = parsed_delta.value().first; |
| 171 |
| 172 | // Check that update does not overflow max_bytes_stored_. |
| 173 | int new_bytes_stored = bytes_stored_; |
| 174 | for (auto& attr : parsed_delta.value().second) { |
| 175 | auto old_version = version_per_key_.find(attr->type()); |
| 176 | if (old_version == version_per_key_.end() || |
| 177 | version_in_delta > old_version->second) { |
| 178 | size_t new_length = attr->length(); |
| 179 | size_t old_length = GetLength(attr->type()); |
| 180 | if (old_version == version_per_key_.end()) { |
| 181 | new_length += sizeof(int64_t); |
| 182 | } |
| 183 |
| 184 | new_bytes_stored = new_bytes_stored + new_length - old_length; |
| 185 | if (new_bytes_stored <= 0) { |
| 186 | RTC_LOG(LS_WARNING) |
| 187 | << "attr: " << attr->type() << " old_length: " << old_length |
| 188 | << " new_length: " << new_length |
| 189 | << " bytes_stored_: " << bytes_stored_ |
| 190 | << " new_bytes_stored: " << new_bytes_stored; |
| 191 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER); |
| 192 | } |
| 193 | if (new_bytes_stored > max_bytes_stored_) { |
| 194 | RTC_LOG(LS_INFO) << "attr: " << attr->type() |
| 195 | << " old_length: " << old_length |
| 196 | << " new_length: " << new_length |
| 197 | << " bytes_stored_: " << bytes_stored_ |
| 198 | << " new_bytes_stored: " << new_bytes_stored; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | if (new_bytes_stored > max_bytes_stored_) { |
| 203 | RTC_LOG(LS_INFO) << " bytes_stored_: " << bytes_stored_ |
| 204 | << " new_bytes_stored: " << new_bytes_stored; |
| 205 | return webrtc::RTCError(webrtc::RTCErrorType::RESOURCE_EXHAUSTED); |
| 206 | } |
| 207 |
| 208 | // Apply the update. |
| 209 | std::vector<uint16_t> keys; |
| 210 | for (auto& attr : parsed_delta.value().second) { |
| 211 | if (version_in_delta > version_per_key_[attr->type()]) { |
| 212 | version_per_key_[attr->type()] = version_in_delta; |
| 213 | keys.push_back(attr->type()); |
| 214 | if (attr->value_type() == STUN_VALUE_BYTE_STRING && attr->length() == 0) { |
| 215 | attrs_.erase(attr->type()); |
| 216 | } else { |
| 217 | int attribute_type = attr->type(); |
| 218 | attrs_[attribute_type] = std::move(attr); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | bytes_stored_ = new_bytes_stored; |
| 223 |
| 224 | return std::make_pair(std::make_unique<StunUInt64Attribute>( |
| 225 | STUN_ATTR_GOOG_DELTA_ACK, version_in_delta), |
| 226 | std::move(keys)); |
| 227 | } |
| 228 |
| 229 | size_t StunDictionaryView::GetLength(int key) const { |
| 230 | auto attr = GetOrNull(key); |
| 231 | if (attr != nullptr) { |
| 232 | return attr->length(); |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 |
| 237 | void StunDictionaryWriter::Disable() { |
| 238 | disabled_ = true; |
| 239 | } |
| 240 |
| 241 | void StunDictionaryWriter::Delete(int key) { |
| 242 | if (disabled_) { |
| 243 | return; |
| 244 | } |
| 245 |
| 246 | if (dictionary_) { |
| 247 | if (dictionary_->attrs_.find(key) == dictionary_->attrs_.end()) { |
| 248 | return; |
| 249 | } |
| 250 | } |
| 251 |
| 252 | // remove any pending updates. |
| 253 | pending_.erase( |
| 254 | std::remove_if(pending_.begin(), pending_.end(), |
| 255 | [key](const auto& p) { return p.second->type() == key; }), |
| 256 | pending_.end()); |
| 257 |
| 258 | // Create tombstone. |
| 259 | auto tombstone = std::make_unique<StunByteStringAttribute>(key); |
| 260 |
| 261 | // add a pending entry. |
| 262 | pending_.push_back(std::make_pair(++version_, tombstone.get())); |
| 263 |
| 264 | // store the tombstone. |
| 265 | tombstones_[key] = std::move(tombstone); |
| 266 |
| 267 | if (dictionary_) { |
| 268 | // remove value |
| 269 | dictionary_->attrs_.erase(key); |
| 270 | } |
| 271 | } |
| 272 |
| 273 | void StunDictionaryWriter::Set(std::unique_ptr<StunAttribute> attr) { |
| 274 | if (disabled_) { |
| 275 | return; |
| 276 | } |
| 277 | int key = attr->type(); |
| 278 | // remove any pending updates. |
| 279 | pending_.erase( |
| 280 | std::remove_if(pending_.begin(), pending_.end(), |
| 281 | [key](const auto& p) { return p.second->type() == key; }), |
| 282 | pending_.end()); |
| 283 |
| 284 | // remove any existing key. |
| 285 | tombstones_.erase(key); |
| 286 |
| 287 | // create pending entry. |
| 288 | pending_.push_back(std::make_pair(++version_, attr.get())); |
| 289 |
| 290 | if (dictionary_) { |
| 291 | // store attribute. |
| 292 | dictionary_->attrs_[key] = std::move(attr); |
| 293 | } |
| 294 | } |
| 295 |
| 296 | // Create an StunByteStringAttribute containing the pending (e.g not ack:ed) |
| 297 | // modifications. |
| 298 | std::unique_ptr<StunByteStringAttribute> StunDictionaryWriter::CreateDelta() { |
| 299 | if (disabled_) { |
| 300 | return nullptr; |
| 301 | } |
| 302 | if (pending_.empty()) { |
| 303 | return nullptr; |
| 304 | } |
| 305 |
| 306 | rtc::ByteBufferWriter buf; |
| 307 | buf.WriteUInt16(StunDictionaryView::kDeltaMagic); // 0,1 |
| 308 | buf.WriteUInt16(StunDictionaryView::kDeltaVersion); // 2,3 |
| 309 |
| 310 | // max version in Delta. |
| 311 | buf.WriteUInt16(StunDictionaryView::kVersionKey); // 4,5 |
| 312 | buf.WriteUInt16(8); // 6,7 |
| 313 | buf.WriteUInt16(STUN_VALUE_UINT64); // 8,9 |
| 314 | buf.WriteUInt64(pending_.back().first); // 10-17 |
| 315 | // attributes |
| 316 | for (const auto& attr : pending_) { |
| 317 | buf.WriteUInt16(attr.second->type()); |
| 318 | buf.WriteUInt16(static_cast<uint16_t>(attr.second->length())); |
| 319 | buf.WriteUInt16(attr.second->value_type()); |
| 320 | if (!attr.second->Write(&buf)) { |
| 321 | RTC_LOG(LS_ERROR) << "Failed to write key: " << attr.second->type(); |
| 322 | return nullptr; |
| 323 | } |
| 324 | } |
| 325 | return std::make_unique<StunByteStringAttribute>(STUN_ATTR_GOOG_DELTA, |
| 326 | buf.Data(), buf.Length()); |
| 327 | } |
| 328 |
| 329 | // Apply a delta ack, i.e prune list of pending changes. |
| 330 | void StunDictionaryWriter::ApplyDeltaAck(const StunUInt64Attribute& ack) { |
| 331 | uint64_t acked_version = ack.value(); |
| 332 | auto entries_to_remove = std::remove_if( |
| 333 | pending_.begin(), pending_.end(), |
| 334 | [acked_version](const auto& p) { return p.first <= acked_version; }); |
| 335 |
| 336 | // remove tombstones. |
| 337 | for (auto it = entries_to_remove; it != pending_.end(); ++it) { |
| 338 | tombstones_.erase((*it).second->type()); |
| 339 | } |
| 340 | pending_.erase(entries_to_remove, pending_.end()); |
| 341 | } |
| 342 |
| 343 | // Check if a key has a pending change (i.e a change |
| 344 | // that has not been acked). |
| 345 | bool StunDictionaryWriter::Pending(int key) const { |
| 346 | for (const auto& attr : pending_) { |
| 347 | if (attr.second->type() == key) { |
| 348 | return true; |
| 349 | } |
| 350 | } |
| 351 | return false; |
| 352 | } |
| 353 |
| 354 | int StunDictionaryWriter::Pending() const { |
| 355 | return pending_.size(); |
| 356 | } |
| 357 |
| 358 | } // namespace cricket |
| 359 |