OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 const char EMPTY_TRANSACTION_ID[] = "0000000000000000"; | 42 const char EMPTY_TRANSACTION_ID[] = "0000000000000000"; |
43 const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E; | 43 const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E; |
44 | 44 |
45 // StunMessage | 45 // StunMessage |
46 | 46 |
47 StunMessage::StunMessage() | 47 StunMessage::StunMessage() |
48 : type_(0), | 48 : type_(0), |
49 length_(0), | 49 length_(0), |
50 transaction_id_(EMPTY_TRANSACTION_ID) { | 50 transaction_id_(EMPTY_TRANSACTION_ID) { |
51 RTC_DCHECK(IsValidTransactionId(transaction_id_)); | 51 RTC_DCHECK(IsValidTransactionId(transaction_id_)); |
52 attrs_ = new std::vector<StunAttribute*>(); | |
53 } | |
54 | |
55 StunMessage::~StunMessage() { | |
56 for (size_t i = 0; i < attrs_->size(); i++) | |
57 delete (*attrs_)[i]; | |
58 delete attrs_; | |
59 } | 52 } |
60 | 53 |
61 bool StunMessage::IsLegacy() const { | 54 bool StunMessage::IsLegacy() const { |
62 if (transaction_id_.size() == kStunLegacyTransactionIdLength) | 55 if (transaction_id_.size() == kStunLegacyTransactionIdLength) |
63 return true; | 56 return true; |
64 RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength); | 57 RTC_DCHECK(transaction_id_.size() == kStunTransactionIdLength); |
65 return false; | 58 return false; |
66 } | 59 } |
67 | 60 |
68 bool StunMessage::SetTransactionID(const std::string& str) { | 61 bool StunMessage::SetTransactionID(const std::string& str) { |
69 if (!IsValidTransactionId(str)) { | 62 if (!IsValidTransactionId(str)) { |
70 return false; | 63 return false; |
71 } | 64 } |
72 transaction_id_ = str; | 65 transaction_id_ = str; |
73 return true; | 66 return true; |
74 } | 67 } |
75 | 68 |
76 void StunMessage::AddAttribute(StunAttribute* attr) { | 69 void StunMessage::AddAttribute(StunAttribute* attr) { |
77 // Fail any attributes that aren't valid for this type of message. | 70 // Fail any attributes that aren't valid for this type of message. |
78 RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type())); | 71 RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type())); |
79 | 72 |
80 attrs_->push_back(attr); | 73 attrs_.emplace_back(attr); |
81 attr->SetOwner(this); | 74 attr->SetOwner(this); |
82 size_t attr_length = attr->length(); | 75 size_t attr_length = attr->length(); |
83 if (attr_length % 4 != 0) { | 76 if (attr_length % 4 != 0) { |
84 attr_length += (4 - (attr_length % 4)); | 77 attr_length += (4 - (attr_length % 4)); |
85 } | 78 } |
86 length_ += static_cast<uint16_t>(attr_length + 4); | 79 length_ += static_cast<uint16_t>(attr_length + 4); |
87 } | 80 } |
88 | 81 |
89 const StunAddressAttribute* StunMessage::GetAddress(int type) const { | 82 const StunAddressAttribute* StunMessage::GetAddress(int type) const { |
90 switch (type) { | 83 switch (type) { |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 // If magic cookie is invalid it means that the peer implements | 314 // If magic cookie is invalid it means that the peer implements |
322 // RFC3489 instead of RFC5389. | 315 // RFC3489 instead of RFC5389. |
323 transaction_id.insert(0, magic_cookie); | 316 transaction_id.insert(0, magic_cookie); |
324 } | 317 } |
325 RTC_DCHECK(IsValidTransactionId(transaction_id)); | 318 RTC_DCHECK(IsValidTransactionId(transaction_id)); |
326 transaction_id_ = transaction_id; | 319 transaction_id_ = transaction_id; |
327 | 320 |
328 if (length_ != buf->Length()) | 321 if (length_ != buf->Length()) |
329 return false; | 322 return false; |
330 | 323 |
331 attrs_->resize(0); | 324 attrs_.resize(0); |
332 | 325 |
333 size_t rest = buf->Length() - length_; | 326 size_t rest = buf->Length() - length_; |
334 while (buf->Length() > rest) { | 327 while (buf->Length() > rest) { |
335 uint16_t attr_type, attr_length; | 328 uint16_t attr_type, attr_length; |
336 if (!buf->ReadUInt16(&attr_type)) | 329 if (!buf->ReadUInt16(&attr_type)) |
337 return false; | 330 return false; |
338 if (!buf->ReadUInt16(&attr_length)) | 331 if (!buf->ReadUInt16(&attr_length)) |
339 return false; | 332 return false; |
340 | 333 |
341 std::unique_ptr<StunAttribute> attr( | 334 std::unique_ptr<StunAttribute> attr( |
342 CreateAttribute(attr_type, attr_length)); | 335 CreateAttribute(attr_type, attr_length)); |
343 if (!attr) { | 336 if (!attr) { |
344 // Skip any unknown or malformed attributes. | 337 // Skip any unknown or malformed attributes. |
345 if ((attr_length % 4) != 0) { | 338 if ((attr_length % 4) != 0) { |
346 attr_length += (4 - (attr_length % 4)); | 339 attr_length += (4 - (attr_length % 4)); |
347 } | 340 } |
348 if (!buf->Consume(attr_length)) | 341 if (!buf->Consume(attr_length)) |
349 return false; | 342 return false; |
350 } else { | 343 } else { |
351 if (!attr->Read(buf)) | 344 if (!attr->Read(buf)) |
352 return false; | 345 return false; |
353 // TODO(honghaiz): Change |attrs_| to be a vector of unique_ptrs. | 346 attrs_.push_back(std::move(attr)); |
354 attrs_->push_back(attr.release()); | |
355 } | 347 } |
356 } | 348 } |
357 | 349 |
358 RTC_DCHECK(buf->Length() == rest); | 350 RTC_DCHECK(buf->Length() == rest); |
359 return true; | 351 return true; |
360 } | 352 } |
361 | 353 |
362 bool StunMessage::Write(ByteBufferWriter* buf) const { | 354 bool StunMessage::Write(ByteBufferWriter* buf) const { |
363 buf->WriteUInt16(type_); | 355 buf->WriteUInt16(type_); |
364 buf->WriteUInt16(length_); | 356 buf->WriteUInt16(length_); |
365 if (!IsLegacy()) | 357 if (!IsLegacy()) |
366 buf->WriteUInt32(kStunMagicCookie); | 358 buf->WriteUInt32(kStunMagicCookie); |
367 buf->WriteString(transaction_id_); | 359 buf->WriteString(transaction_id_); |
368 | 360 |
369 for (size_t i = 0; i < attrs_->size(); ++i) { | 361 for (const auto& attr : attrs_) { |
370 buf->WriteUInt16((*attrs_)[i]->type()); | 362 buf->WriteUInt16(attr->type()); |
371 buf->WriteUInt16(static_cast<uint16_t>((*attrs_)[i]->length())); | 363 buf->WriteUInt16(static_cast<uint16_t>(attr->length())); |
372 if (!(*attrs_)[i]->Write(buf)) | 364 if (!attr->Write(buf)) { |
373 return false; | 365 return false; |
| 366 } |
374 } | 367 } |
375 | 368 |
376 return true; | 369 return true; |
377 } | 370 } |
378 | 371 |
379 StunAttributeValueType StunMessage::GetAttributeValueType(int type) const { | 372 StunAttributeValueType StunMessage::GetAttributeValueType(int type) const { |
380 switch (type) { | 373 switch (type) { |
381 case STUN_ATTR_MAPPED_ADDRESS: return STUN_VALUE_ADDRESS; | 374 case STUN_ATTR_MAPPED_ADDRESS: return STUN_VALUE_ADDRESS; |
382 case STUN_ATTR_USERNAME: return STUN_VALUE_BYTE_STRING; | 375 case STUN_ATTR_USERNAME: return STUN_VALUE_BYTE_STRING; |
383 case STUN_ATTR_MESSAGE_INTEGRITY: return STUN_VALUE_BYTE_STRING; | 376 case STUN_ATTR_MESSAGE_INTEGRITY: return STUN_VALUE_BYTE_STRING; |
(...skipping 11 matching lines...) Expand all Loading... |
395 } | 388 } |
396 } | 389 } |
397 | 390 |
398 StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ { | 391 StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ { |
399 StunAttributeValueType value_type = GetAttributeValueType(type); | 392 StunAttributeValueType value_type = GetAttributeValueType(type); |
400 return StunAttribute::Create(value_type, type, static_cast<uint16_t>(length), | 393 return StunAttribute::Create(value_type, type, static_cast<uint16_t>(length), |
401 this); | 394 this); |
402 } | 395 } |
403 | 396 |
404 const StunAttribute* StunMessage::GetAttribute(int type) const { | 397 const StunAttribute* StunMessage::GetAttribute(int type) const { |
405 for (size_t i = 0; i < attrs_->size(); ++i) { | 398 for (const auto& attr : attrs_) { |
406 if ((*attrs_)[i]->type() == type) | 399 if (attr->type() == type) { |
407 return (*attrs_)[i]; | 400 return attr.get(); |
| 401 } |
408 } | 402 } |
409 return NULL; | 403 return NULL; |
410 } | 404 } |
411 | 405 |
412 bool StunMessage::IsValidTransactionId(const std::string& transaction_id) { | 406 bool StunMessage::IsValidTransactionId(const std::string& transaction_id) { |
413 return transaction_id.size() == kStunTransactionIdLength || | 407 return transaction_id.size() == kStunTransactionIdLength || |
414 transaction_id.size() == kStunLegacyTransactionIdLength; | 408 transaction_id.size() == kStunLegacyTransactionIdLength; |
415 } | 409 } |
416 | 410 |
417 // StunAttribute | 411 // StunAttribute |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 digest, sizeof(digest)); | 906 digest, sizeof(digest)); |
913 if (size == 0) { | 907 if (size == 0) { |
914 return false; | 908 return false; |
915 } | 909 } |
916 | 910 |
917 *hash = std::string(digest, size); | 911 *hash = std::string(digest, size); |
918 return true; | 912 return true; |
919 } | 913 } |
920 | 914 |
921 } // namespace cricket | 915 } // namespace cricket |
OLD | NEW |