Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: webrtc/p2p/base/stun.cc

Issue 1821083002: Split ByteBuffer into writer/reader objects. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/p2p/base/stun.h ('k') | webrtc/p2p/base/stun_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "webrtc/p2p/base/stun.h" 11 #include "webrtc/p2p/base/stun.h"
12 12
13 #include <string.h> 13 #include <string.h>
14 14
15 #include "webrtc/base/byteorder.h" 15 #include "webrtc/base/byteorder.h"
16 #include "webrtc/base/common.h" 16 #include "webrtc/base/common.h"
17 #include "webrtc/base/crc32.h" 17 #include "webrtc/base/crc32.h"
18 #include "webrtc/base/logging.h" 18 #include "webrtc/base/logging.h"
19 #include "webrtc/base/messagedigest.h" 19 #include "webrtc/base/messagedigest.h"
20 #include "webrtc/base/scoped_ptr.h" 20 #include "webrtc/base/scoped_ptr.h"
21 #include "webrtc/base/stringencode.h" 21 #include "webrtc/base/stringencode.h"
22 22
23 using rtc::ByteBuffer; 23 using rtc::ByteBufferReader;
24 using rtc::ByteBufferWriter;
24 25
25 namespace cricket { 26 namespace cricket {
26 27
27 const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server"; 28 const char STUN_ERROR_REASON_TRY_ALTERNATE_SERVER[] = "Try Alternate Server";
28 const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request"; 29 const char STUN_ERROR_REASON_BAD_REQUEST[] = "Bad Request";
29 const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized"; 30 const char STUN_ERROR_REASON_UNAUTHORIZED[] = "Unauthorized";
30 const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden"; 31 const char STUN_ERROR_REASON_FORBIDDEN[] = "Forbidden";
31 const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials"; 32 const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials";
32 const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch"; 33 const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch";
33 const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce"; 34 const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce";
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 bool StunMessage::AddMessageIntegrity(const char* key, 213 bool StunMessage::AddMessageIntegrity(const char* key,
213 size_t keylen) { 214 size_t keylen) {
214 // Add the attribute with a dummy value. Since this is a known attribute, it 215 // Add the attribute with a dummy value. Since this is a known attribute, it
215 // can't fail. 216 // can't fail.
216 StunByteStringAttribute* msg_integrity_attr = 217 StunByteStringAttribute* msg_integrity_attr =
217 new StunByteStringAttribute(STUN_ATTR_MESSAGE_INTEGRITY, 218 new StunByteStringAttribute(STUN_ATTR_MESSAGE_INTEGRITY,
218 std::string(kStunMessageIntegritySize, '0')); 219 std::string(kStunMessageIntegritySize, '0'));
219 VERIFY(AddAttribute(msg_integrity_attr)); 220 VERIFY(AddAttribute(msg_integrity_attr));
220 221
221 // Calculate the HMAC for the message. 222 // Calculate the HMAC for the message.
222 rtc::ByteBuffer buf; 223 ByteBufferWriter buf;
223 if (!Write(&buf)) 224 if (!Write(&buf))
224 return false; 225 return false;
225 226
226 int msg_len_for_hmac = static_cast<int>( 227 int msg_len_for_hmac = static_cast<int>(
227 buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length()); 228 buf.Length() - kStunAttributeHeaderSize - msg_integrity_attr->length());
228 char hmac[kStunMessageIntegritySize]; 229 char hmac[kStunMessageIntegritySize];
229 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, 230 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1,
230 key, keylen, 231 key, keylen,
231 buf.Data(), msg_len_for_hmac, 232 buf.Data(), msg_len_for_hmac,
232 hmac, sizeof(hmac)); 233 hmac, sizeof(hmac));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 } 274 }
274 275
275 bool StunMessage::AddFingerprint() { 276 bool StunMessage::AddFingerprint() {
276 // Add the attribute with a dummy value. Since this is a known attribute, 277 // Add the attribute with a dummy value. Since this is a known attribute,
277 // it can't fail. 278 // it can't fail.
278 StunUInt32Attribute* fingerprint_attr = 279 StunUInt32Attribute* fingerprint_attr =
279 new StunUInt32Attribute(STUN_ATTR_FINGERPRINT, 0); 280 new StunUInt32Attribute(STUN_ATTR_FINGERPRINT, 0);
280 VERIFY(AddAttribute(fingerprint_attr)); 281 VERIFY(AddAttribute(fingerprint_attr));
281 282
282 // Calculate the CRC-32 for the message and insert it. 283 // Calculate the CRC-32 for the message and insert it.
283 rtc::ByteBuffer buf; 284 ByteBufferWriter buf;
284 if (!Write(&buf)) 285 if (!Write(&buf))
285 return false; 286 return false;
286 287
287 int msg_len_for_crc32 = static_cast<int>( 288 int msg_len_for_crc32 = static_cast<int>(
288 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length()); 289 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length());
289 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32); 290 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32);
290 291
291 // Insert the correct CRC-32, XORed with a constant, into the attribute. 292 // Insert the correct CRC-32, XORed with a constant, into the attribute.
292 fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE); 293 fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE);
293 return true; 294 return true;
294 } 295 }
295 296
296 bool StunMessage::Read(ByteBuffer* buf) { 297 bool StunMessage::Read(ByteBufferReader* buf) {
297 if (!buf->ReadUInt16(&type_)) 298 if (!buf->ReadUInt16(&type_))
298 return false; 299 return false;
299 300
300 if (type_ & 0x8000) { 301 if (type_ & 0x8000) {
301 // RTP and RTCP set the MSB of first byte, since first two bits are version, 302 // RTP and RTCP set the MSB of first byte, since first two bits are version,
302 // and version is always 2 (10). If set, this is not a STUN packet. 303 // and version is always 2 (10). If set, this is not a STUN packet.
303 return false; 304 return false;
304 } 305 }
305 306
306 if (!buf->ReadUInt16(&length_)) 307 if (!buf->ReadUInt16(&length_))
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 if (!attr->Read(buf)) 350 if (!attr->Read(buf))
350 return false; 351 return false;
351 attrs_->push_back(attr); 352 attrs_->push_back(attr);
352 } 353 }
353 } 354 }
354 355
355 ASSERT(buf->Length() == rest); 356 ASSERT(buf->Length() == rest);
356 return true; 357 return true;
357 } 358 }
358 359
359 bool StunMessage::Write(ByteBuffer* buf) const { 360 bool StunMessage::Write(ByteBufferWriter* buf) const {
360 buf->WriteUInt16(type_); 361 buf->WriteUInt16(type_);
361 buf->WriteUInt16(length_); 362 buf->WriteUInt16(length_);
362 if (!IsLegacy()) 363 if (!IsLegacy())
363 buf->WriteUInt32(kStunMagicCookie); 364 buf->WriteUInt32(kStunMagicCookie);
364 buf->WriteString(transaction_id_); 365 buf->WriteString(transaction_id_);
365 366
366 for (size_t i = 0; i < attrs_->size(); ++i) { 367 for (size_t i = 0; i < attrs_->size(); ++i) {
367 buf->WriteUInt16((*attrs_)[i]->type()); 368 buf->WriteUInt16((*attrs_)[i]->type());
368 buf->WriteUInt16(static_cast<uint16_t>((*attrs_)[i]->length())); 369 buf->WriteUInt16(static_cast<uint16_t>((*attrs_)[i]->length()));
369 if (!(*attrs_)[i]->Write(buf)) 370 if (!(*attrs_)[i]->Write(buf))
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 return transaction_id.size() == kStunTransactionIdLength || 411 return transaction_id.size() == kStunTransactionIdLength ||
411 transaction_id.size() == kStunLegacyTransactionIdLength; 412 transaction_id.size() == kStunLegacyTransactionIdLength;
412 } 413 }
413 414
414 // StunAttribute 415 // StunAttribute
415 416
416 StunAttribute::StunAttribute(uint16_t type, uint16_t length) 417 StunAttribute::StunAttribute(uint16_t type, uint16_t length)
417 : type_(type), length_(length) { 418 : type_(type), length_(length) {
418 } 419 }
419 420
420 void StunAttribute::ConsumePadding(rtc::ByteBuffer* buf) const { 421 void StunAttribute::ConsumePadding(ByteBufferReader* buf) const {
421 int remainder = length_ % 4; 422 int remainder = length_ % 4;
422 if (remainder > 0) { 423 if (remainder > 0) {
423 buf->Consume(4 - remainder); 424 buf->Consume(4 - remainder);
424 } 425 }
425 } 426 }
426 427
427 void StunAttribute::WritePadding(rtc::ByteBuffer* buf) const { 428 void StunAttribute::WritePadding(ByteBufferWriter* buf) const {
428 int remainder = length_ % 4; 429 int remainder = length_ % 4;
429 if (remainder > 0) { 430 if (remainder > 0) {
430 char zeroes[4] = {0}; 431 char zeroes[4] = {0};
431 buf->WriteBytes(zeroes, 4 - remainder); 432 buf->WriteBytes(zeroes, 4 - remainder);
432 } 433 }
433 } 434 }
434 435
435 StunAttribute* StunAttribute::Create(StunAttributeValueType value_type, 436 StunAttribute* StunAttribute::Create(StunAttributeValueType value_type,
436 uint16_t type, 437 uint16_t type,
437 uint16_t length, 438 uint16_t length,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 StunAddressAttribute::StunAddressAttribute(uint16_t type, 489 StunAddressAttribute::StunAddressAttribute(uint16_t type,
489 const rtc::SocketAddress& addr) 490 const rtc::SocketAddress& addr)
490 : StunAttribute(type, 0) { 491 : StunAttribute(type, 0) {
491 SetAddress(addr); 492 SetAddress(addr);
492 } 493 }
493 494
494 StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length) 495 StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length)
495 : StunAttribute(type, length) { 496 : StunAttribute(type, length) {
496 } 497 }
497 498
498 bool StunAddressAttribute::Read(ByteBuffer* buf) { 499 bool StunAddressAttribute::Read(ByteBufferReader* buf) {
499 uint8_t dummy; 500 uint8_t dummy;
500 if (!buf->ReadUInt8(&dummy)) 501 if (!buf->ReadUInt8(&dummy))
501 return false; 502 return false;
502 503
503 uint8_t stun_family; 504 uint8_t stun_family;
504 if (!buf->ReadUInt8(&stun_family)) { 505 if (!buf->ReadUInt8(&stun_family)) {
505 return false; 506 return false;
506 } 507 }
507 uint16_t port; 508 uint16_t port;
508 if (!buf->ReadUInt16(&port)) 509 if (!buf->ReadUInt16(&port))
(...skipping 17 matching lines...) Expand all
526 return false; 527 return false;
527 } 528 }
528 rtc::IPAddress ipaddr(v6addr); 529 rtc::IPAddress ipaddr(v6addr);
529 SetAddress(rtc::SocketAddress(ipaddr, port)); 530 SetAddress(rtc::SocketAddress(ipaddr, port));
530 } else { 531 } else {
531 return false; 532 return false;
532 } 533 }
533 return true; 534 return true;
534 } 535 }
535 536
536 bool StunAddressAttribute::Write(ByteBuffer* buf) const { 537 bool StunAddressAttribute::Write(ByteBufferWriter* buf) const {
537 StunAddressFamily address_family = family(); 538 StunAddressFamily address_family = family();
538 if (address_family == STUN_ADDRESS_UNDEF) { 539 if (address_family == STUN_ADDRESS_UNDEF) {
539 LOG(LS_ERROR) << "Error writing address attribute: unknown family."; 540 LOG(LS_ERROR) << "Error writing address attribute: unknown family.";
540 return false; 541 return false;
541 } 542 }
542 buf->WriteUInt8(0); 543 buf->WriteUInt8(0);
543 buf->WriteUInt8(address_family); 544 buf->WriteUInt8(address_family);
544 buf->WriteUInt16(address_.port()); 545 buf->WriteUInt16(address_.port());
545 switch (address_.family()) { 546 switch (address_.family()) {
546 case AF_INET: { 547 case AF_INET: {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 } 598 }
598 break; 599 break;
599 } 600 }
600 } 601 }
601 } 602 }
602 // Invalid ip family or transaction ID, or missing owner. 603 // Invalid ip family or transaction ID, or missing owner.
603 // Return an AF_UNSPEC address. 604 // Return an AF_UNSPEC address.
604 return rtc::IPAddress(); 605 return rtc::IPAddress();
605 } 606 }
606 607
607 bool StunXorAddressAttribute::Read(ByteBuffer* buf) { 608 bool StunXorAddressAttribute::Read(ByteBufferReader* buf) {
608 if (!StunAddressAttribute::Read(buf)) 609 if (!StunAddressAttribute::Read(buf))
609 return false; 610 return false;
610 uint16_t xoredport = port() ^ (kStunMagicCookie >> 16); 611 uint16_t xoredport = port() ^ (kStunMagicCookie >> 16);
611 rtc::IPAddress xored_ip = GetXoredIP(); 612 rtc::IPAddress xored_ip = GetXoredIP();
612 SetAddress(rtc::SocketAddress(xored_ip, xoredport)); 613 SetAddress(rtc::SocketAddress(xored_ip, xoredport));
613 return true; 614 return true;
614 } 615 }
615 616
616 bool StunXorAddressAttribute::Write(ByteBuffer* buf) const { 617 bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const {
617 StunAddressFamily address_family = family(); 618 StunAddressFamily address_family = family();
618 if (address_family == STUN_ADDRESS_UNDEF) { 619 if (address_family == STUN_ADDRESS_UNDEF) {
619 LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family."; 620 LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family.";
620 return false; 621 return false;
621 } 622 }
622 rtc::IPAddress xored_ip = GetXoredIP(); 623 rtc::IPAddress xored_ip = GetXoredIP();
623 if (xored_ip.family() == AF_UNSPEC) { 624 if (xored_ip.family() == AF_UNSPEC) {
624 return false; 625 return false;
625 } 626 }
626 buf->WriteUInt8(0); 627 buf->WriteUInt8(0);
(...skipping 26 matching lines...) Expand all
653 ASSERT(index < 32); 654 ASSERT(index < 32);
654 return static_cast<bool>((bits_ >> index) & 0x1); 655 return static_cast<bool>((bits_ >> index) & 0x1);
655 } 656 }
656 657
657 void StunUInt32Attribute::SetBit(size_t index, bool value) { 658 void StunUInt32Attribute::SetBit(size_t index, bool value) {
658 ASSERT(index < 32); 659 ASSERT(index < 32);
659 bits_ &= ~(1 << index); 660 bits_ &= ~(1 << index);
660 bits_ |= value ? (1 << index) : 0; 661 bits_ |= value ? (1 << index) : 0;
661 } 662 }
662 663
663 bool StunUInt32Attribute::Read(ByteBuffer* buf) { 664 bool StunUInt32Attribute::Read(ByteBufferReader* buf) {
664 if (length() != SIZE || !buf->ReadUInt32(&bits_)) 665 if (length() != SIZE || !buf->ReadUInt32(&bits_))
665 return false; 666 return false;
666 return true; 667 return true;
667 } 668 }
668 669
669 bool StunUInt32Attribute::Write(ByteBuffer* buf) const { 670 bool StunUInt32Attribute::Write(ByteBufferWriter* buf) const {
670 buf->WriteUInt32(bits_); 671 buf->WriteUInt32(bits_);
671 return true; 672 return true;
672 } 673 }
673 674
674 StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value) 675 StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value)
675 : StunAttribute(type, SIZE), bits_(value) { 676 : StunAttribute(type, SIZE), bits_(value) {
676 } 677 }
677 678
678 StunUInt64Attribute::StunUInt64Attribute(uint16_t type) 679 StunUInt64Attribute::StunUInt64Attribute(uint16_t type)
679 : StunAttribute(type, SIZE), bits_(0) { 680 : StunAttribute(type, SIZE), bits_(0) {
680 } 681 }
681 682
682 bool StunUInt64Attribute::Read(ByteBuffer* buf) { 683 bool StunUInt64Attribute::Read(ByteBufferReader* buf) {
683 if (length() != SIZE || !buf->ReadUInt64(&bits_)) 684 if (length() != SIZE || !buf->ReadUInt64(&bits_))
684 return false; 685 return false;
685 return true; 686 return true;
686 } 687 }
687 688
688 bool StunUInt64Attribute::Write(ByteBuffer* buf) const { 689 bool StunUInt64Attribute::Write(ByteBufferWriter* buf) const {
689 buf->WriteUInt64(bits_); 690 buf->WriteUInt64(bits_);
690 return true; 691 return true;
691 } 692 }
692 693
693 StunByteStringAttribute::StunByteStringAttribute(uint16_t type) 694 StunByteStringAttribute::StunByteStringAttribute(uint16_t type)
694 : StunAttribute(type, 0), bytes_(NULL) { 695 : StunAttribute(type, 0), bytes_(NULL) {
695 } 696 }
696 697
697 StunByteStringAttribute::StunByteStringAttribute(uint16_t type, 698 StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
698 const std::string& str) 699 const std::string& str)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 ASSERT(index < length()); 731 ASSERT(index < length());
731 return static_cast<uint8_t>(bytes_[index]); 732 return static_cast<uint8_t>(bytes_[index]);
732 } 733 }
733 734
734 void StunByteStringAttribute::SetByte(size_t index, uint8_t value) { 735 void StunByteStringAttribute::SetByte(size_t index, uint8_t value) {
735 ASSERT(bytes_ != NULL); 736 ASSERT(bytes_ != NULL);
736 ASSERT(index < length()); 737 ASSERT(index < length());
737 bytes_[index] = value; 738 bytes_[index] = value;
738 } 739 }
739 740
740 bool StunByteStringAttribute::Read(ByteBuffer* buf) { 741 bool StunByteStringAttribute::Read(ByteBufferReader* buf) {
741 bytes_ = new char[length()]; 742 bytes_ = new char[length()];
742 if (!buf->ReadBytes(bytes_, length())) { 743 if (!buf->ReadBytes(bytes_, length())) {
743 return false; 744 return false;
744 } 745 }
745 746
746 ConsumePadding(buf); 747 ConsumePadding(buf);
747 return true; 748 return true;
748 } 749 }
749 750
750 bool StunByteStringAttribute::Write(ByteBuffer* buf) const { 751 bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const {
751 buf->WriteBytes(bytes_, length()); 752 buf->WriteBytes(bytes_, length());
752 WritePadding(buf); 753 WritePadding(buf);
753 return true; 754 return true;
754 } 755 }
755 756
756 void StunByteStringAttribute::SetBytes(char* bytes, size_t length) { 757 void StunByteStringAttribute::SetBytes(char* bytes, size_t length) {
757 delete [] bytes_; 758 delete [] bytes_;
758 bytes_ = bytes; 759 bytes_ = bytes;
759 SetLength(static_cast<uint16_t>(length)); 760 SetLength(static_cast<uint16_t>(length));
760 } 761 }
(...skipping 20 matching lines...) Expand all
781 void StunErrorCodeAttribute::SetCode(int code) { 782 void StunErrorCodeAttribute::SetCode(int code) {
782 class_ = static_cast<uint8_t>(code / 100); 783 class_ = static_cast<uint8_t>(code / 100);
783 number_ = static_cast<uint8_t>(code % 100); 784 number_ = static_cast<uint8_t>(code % 100);
784 } 785 }
785 786
786 void StunErrorCodeAttribute::SetReason(const std::string& reason) { 787 void StunErrorCodeAttribute::SetReason(const std::string& reason) {
787 SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size())); 788 SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size()));
788 reason_ = reason; 789 reason_ = reason;
789 } 790 }
790 791
791 bool StunErrorCodeAttribute::Read(ByteBuffer* buf) { 792 bool StunErrorCodeAttribute::Read(ByteBufferReader* buf) {
792 uint32_t val; 793 uint32_t val;
793 if (length() < MIN_SIZE || !buf->ReadUInt32(&val)) 794 if (length() < MIN_SIZE || !buf->ReadUInt32(&val))
794 return false; 795 return false;
795 796
796 if ((val >> 11) != 0) 797 if ((val >> 11) != 0)
797 LOG(LS_ERROR) << "error-code bits not zero"; 798 LOG(LS_ERROR) << "error-code bits not zero";
798 799
799 class_ = ((val >> 8) & 0x7); 800 class_ = ((val >> 8) & 0x7);
800 number_ = (val & 0xff); 801 number_ = (val & 0xff);
801 802
802 if (!buf->ReadString(&reason_, length() - 4)) 803 if (!buf->ReadString(&reason_, length() - 4))
803 return false; 804 return false;
804 805
805 ConsumePadding(buf); 806 ConsumePadding(buf);
806 return true; 807 return true;
807 } 808 }
808 809
809 bool StunErrorCodeAttribute::Write(ByteBuffer* buf) const { 810 bool StunErrorCodeAttribute::Write(ByteBufferWriter* buf) const {
810 buf->WriteUInt32(class_ << 8 | number_); 811 buf->WriteUInt32(class_ << 8 | number_);
811 buf->WriteString(reason_); 812 buf->WriteString(reason_);
812 WritePadding(buf); 813 WritePadding(buf);
813 return true; 814 return true;
814 } 815 }
815 816
816 StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length) 817 StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length)
817 : StunAttribute(type, length) { 818 : StunAttribute(type, length) {
818 attr_types_ = new std::vector<uint16_t>(); 819 attr_types_ = new std::vector<uint16_t>();
819 } 820 }
(...skipping 12 matching lines...) Expand all
832 833
833 void StunUInt16ListAttribute::SetType(int index, uint16_t value) { 834 void StunUInt16ListAttribute::SetType(int index, uint16_t value) {
834 (*attr_types_)[index] = value; 835 (*attr_types_)[index] = value;
835 } 836 }
836 837
837 void StunUInt16ListAttribute::AddType(uint16_t value) { 838 void StunUInt16ListAttribute::AddType(uint16_t value) {
838 attr_types_->push_back(value); 839 attr_types_->push_back(value);
839 SetLength(static_cast<uint16_t>(attr_types_->size() * 2)); 840 SetLength(static_cast<uint16_t>(attr_types_->size() * 2));
840 } 841 }
841 842
842 bool StunUInt16ListAttribute::Read(ByteBuffer* buf) { 843 bool StunUInt16ListAttribute::Read(ByteBufferReader* buf) {
843 if (length() % 2) 844 if (length() % 2)
844 return false; 845 return false;
845 846
846 for (size_t i = 0; i < length() / 2; i++) { 847 for (size_t i = 0; i < length() / 2; i++) {
847 uint16_t attr; 848 uint16_t attr;
848 if (!buf->ReadUInt16(&attr)) 849 if (!buf->ReadUInt16(&attr))
849 return false; 850 return false;
850 attr_types_->push_back(attr); 851 attr_types_->push_back(attr);
851 } 852 }
852 // Padding of these attributes is done in RFC 5389 style. This is 853 // Padding of these attributes is done in RFC 5389 style. This is
853 // slightly different from RFC3489, but it shouldn't be important. 854 // slightly different from RFC3489, but it shouldn't be important.
854 // RFC3489 pads out to a 32 bit boundary by duplicating one of the 855 // RFC3489 pads out to a 32 bit boundary by duplicating one of the
855 // entries in the list (not necessarily the last one - it's unspecified). 856 // entries in the list (not necessarily the last one - it's unspecified).
856 // RFC5389 pads on the end, and the bytes are always ignored. 857 // RFC5389 pads on the end, and the bytes are always ignored.
857 ConsumePadding(buf); 858 ConsumePadding(buf);
858 return true; 859 return true;
859 } 860 }
860 861
861 bool StunUInt16ListAttribute::Write(ByteBuffer* buf) const { 862 bool StunUInt16ListAttribute::Write(ByteBufferWriter* buf) const {
862 for (size_t i = 0; i < attr_types_->size(); ++i) { 863 for (size_t i = 0; i < attr_types_->size(); ++i) {
863 buf->WriteUInt16((*attr_types_)[i]); 864 buf->WriteUInt16((*attr_types_)[i]);
864 } 865 }
865 WritePadding(buf); 866 WritePadding(buf);
866 return true; 867 return true;
867 } 868 }
868 869
869 int GetStunSuccessResponseType(int req_type) { 870 int GetStunSuccessResponseType(int req_type) {
870 return IsStunRequestType(req_type) ? (req_type | 0x100) : -1; 871 return IsStunRequestType(req_type) ? (req_type | 0x100) : -1;
871 } 872 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 digest, sizeof(digest)); 910 digest, sizeof(digest));
910 if (size == 0) { 911 if (size == 0) {
911 return false; 912 return false;
912 } 913 }
913 914
914 *hash = std::string(digest, size); 915 *hash = std::string(digest, size);
915 return true; 916 return true;
916 } 917 }
917 918
918 } // namespace cricket 919 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/p2p/base/stun.h ('k') | webrtc/p2p/base/stun_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698