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

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

Issue 1362503003: Use suffixed {uint,int}{8,16,32,64}_t types. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: rebase + revert basictypes.h (to be landed separately just in case of a revert due to unexpected us… Created 5 years, 2 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
(...skipping 20 matching lines...) Expand all
31 const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials"; 31 const char STUN_ERROR_REASON_STALE_CREDENTIALS[] = "Stale Credentials";
32 const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch"; 32 const char STUN_ERROR_REASON_ALLOCATION_MISMATCH[] = "Allocation Mismatch";
33 const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce"; 33 const char STUN_ERROR_REASON_STALE_NONCE[] = "Stale Nonce";
34 const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials"; 34 const char STUN_ERROR_REASON_WRONG_CREDENTIALS[] = "Wrong Credentials";
35 const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol"; 35 const char STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL[] = "Unsupported Protocol";
36 const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict"; 36 const char STUN_ERROR_REASON_ROLE_CONFLICT[] = "Role Conflict";
37 const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error"; 37 const char STUN_ERROR_REASON_SERVER_ERROR[] = "Server Error";
38 38
39 const char TURN_MAGIC_COOKIE_VALUE[] = { '\x72', '\xC6', '\x4B', '\xC6' }; 39 const char TURN_MAGIC_COOKIE_VALUE[] = { '\x72', '\xC6', '\x4B', '\xC6' };
40 const char EMPTY_TRANSACTION_ID[] = "0000000000000000"; 40 const char EMPTY_TRANSACTION_ID[] = "0000000000000000";
41 const uint32 STUN_FINGERPRINT_XOR_VALUE = 0x5354554E; 41 const uint32_t STUN_FINGERPRINT_XOR_VALUE = 0x5354554E;
42 42
43 // StunMessage 43 // StunMessage
44 44
45 StunMessage::StunMessage() 45 StunMessage::StunMessage()
46 : type_(0), 46 : type_(0),
47 length_(0), 47 length_(0),
48 transaction_id_(EMPTY_TRANSACTION_ID) { 48 transaction_id_(EMPTY_TRANSACTION_ID) {
49 ASSERT(IsValidTransactionId(transaction_id_)); 49 ASSERT(IsValidTransactionId(transaction_id_));
50 attrs_ = new std::vector<StunAttribute*>(); 50 attrs_ = new std::vector<StunAttribute*>();
51 } 51 }
(...skipping 23 matching lines...) Expand all
75 // Fail any attributes that aren't valid for this type of message. 75 // Fail any attributes that aren't valid for this type of message.
76 if (attr->value_type() != GetAttributeValueType(attr->type())) { 76 if (attr->value_type() != GetAttributeValueType(attr->type())) {
77 return false; 77 return false;
78 } 78 }
79 attrs_->push_back(attr); 79 attrs_->push_back(attr);
80 attr->SetOwner(this); 80 attr->SetOwner(this);
81 size_t attr_length = attr->length(); 81 size_t attr_length = attr->length();
82 if (attr_length % 4 != 0) { 82 if (attr_length % 4 != 0) {
83 attr_length += (4 - (attr_length % 4)); 83 attr_length += (4 - (attr_length % 4));
84 } 84 }
85 length_ += static_cast<uint16>(attr_length + 4); 85 length_ += static_cast<uint16_t>(attr_length + 4);
86 return true; 86 return true;
87 } 87 }
88 88
89 const StunAddressAttribute* StunMessage::GetAddress(int type) const { 89 const StunAddressAttribute* StunMessage::GetAddress(int type) const {
90 switch (type) { 90 switch (type) {
91 case STUN_ATTR_MAPPED_ADDRESS: { 91 case STUN_ATTR_MAPPED_ADDRESS: {
92 // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is 92 // Return XOR-MAPPED-ADDRESS when MAPPED-ADDRESS attribute is
93 // missing. 93 // missing.
94 const StunAttribute* mapped_address = 94 const StunAttribute* mapped_address =
95 GetAttribute(STUN_ATTR_MAPPED_ADDRESS); 95 GetAttribute(STUN_ATTR_MAPPED_ADDRESS);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 // Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the 128 // Verifies a STUN message has a valid MESSAGE-INTEGRITY attribute, using the
129 // procedure outlined in RFC 5389, section 15.4. 129 // procedure outlined in RFC 5389, section 15.4.
130 bool StunMessage::ValidateMessageIntegrity(const char* data, size_t size, 130 bool StunMessage::ValidateMessageIntegrity(const char* data, size_t size,
131 const std::string& password) { 131 const std::string& password) {
132 // Verifying the size of the message. 132 // Verifying the size of the message.
133 if ((size % 4) != 0) { 133 if ((size % 4) != 0) {
134 return false; 134 return false;
135 } 135 }
136 136
137 // Getting the message length from the STUN header. 137 // Getting the message length from the STUN header.
138 uint16 msg_length = rtc::GetBE16(&data[2]); 138 uint16_t msg_length = rtc::GetBE16(&data[2]);
139 if (size != (msg_length + kStunHeaderSize)) { 139 if (size != (msg_length + kStunHeaderSize)) {
140 return false; 140 return false;
141 } 141 }
142 142
143 // Finding Message Integrity attribute in stun message. 143 // Finding Message Integrity attribute in stun message.
144 size_t current_pos = kStunHeaderSize; 144 size_t current_pos = kStunHeaderSize;
145 bool has_message_integrity_attr = false; 145 bool has_message_integrity_attr = false;
146 while (current_pos < size) { 146 while (current_pos < size) {
147 uint16 attr_type, attr_length; 147 uint16_t attr_type, attr_length;
148 // Getting attribute type and length. 148 // Getting attribute type and length.
149 attr_type = rtc::GetBE16(&data[current_pos]); 149 attr_type = rtc::GetBE16(&data[current_pos]);
150 attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]); 150 attr_length = rtc::GetBE16(&data[current_pos + sizeof(attr_type)]);
151 151
152 // If M-I, sanity check it, and break out. 152 // If M-I, sanity check it, and break out.
153 if (attr_type == STUN_ATTR_MESSAGE_INTEGRITY) { 153 if (attr_type == STUN_ATTR_MESSAGE_INTEGRITY) {
154 if (attr_length != kStunMessageIntegritySize || 154 if (attr_length != kStunMessageIntegritySize ||
155 current_pos + attr_length > size) { 155 current_pos + attr_length > size) {
156 return false; 156 return false;
157 } 157 }
(...skipping 22 matching lines...) Expand all
180 size_t extra_offset = size - 180 size_t extra_offset = size -
181 (mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize); 181 (mi_pos + kStunAttributeHeaderSize + kStunMessageIntegritySize);
182 size_t new_adjusted_len = size - extra_offset - kStunHeaderSize; 182 size_t new_adjusted_len = size - extra_offset - kStunHeaderSize;
183 183
184 // Writing new length of the STUN message @ Message Length in temp buffer. 184 // Writing new length of the STUN message @ Message Length in temp buffer.
185 // 0 1 2 3 185 // 0 1 2 3
186 // 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 186 // 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
187 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 187 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
188 // |0 0| STUN Message Type | Message Length | 188 // |0 0| STUN Message Type | Message Length |
189 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 189 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
190 rtc::SetBE16(temp_data.get() + 2, 190 rtc::SetBE16(temp_data.get() + 2, static_cast<uint16_t>(new_adjusted_len));
191 static_cast<uint16>(new_adjusted_len));
192 } 191 }
193 192
194 char hmac[kStunMessageIntegritySize]; 193 char hmac[kStunMessageIntegritySize];
195 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1, 194 size_t ret = rtc::ComputeHmac(rtc::DIGEST_SHA_1,
196 password.c_str(), password.size(), 195 password.c_str(), password.size(),
197 temp_data.get(), mi_pos, 196 temp_data.get(), mi_pos,
198 hmac, sizeof(hmac)); 197 hmac, sizeof(hmac));
199 ASSERT(ret == sizeof(hmac)); 198 ASSERT(ret == sizeof(hmac));
200 if (ret != sizeof(hmac)) 199 if (ret != sizeof(hmac))
201 return false; 200 return false;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 254
256 // Skip the rest if the magic cookie isn't present. 255 // Skip the rest if the magic cookie isn't present.
257 const char* magic_cookie = 256 const char* magic_cookie =
258 data + kStunTransactionIdOffset - kStunMagicCookieLength; 257 data + kStunTransactionIdOffset - kStunMagicCookieLength;
259 if (rtc::GetBE32(magic_cookie) != kStunMagicCookie) 258 if (rtc::GetBE32(magic_cookie) != kStunMagicCookie)
260 return false; 259 return false;
261 260
262 // Check the fingerprint type and length. 261 // Check the fingerprint type and length.
263 const char* fingerprint_attr_data = data + size - fingerprint_attr_size; 262 const char* fingerprint_attr_data = data + size - fingerprint_attr_size;
264 if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT || 263 if (rtc::GetBE16(fingerprint_attr_data) != STUN_ATTR_FINGERPRINT ||
265 rtc::GetBE16(fingerprint_attr_data + sizeof(uint16)) != 264 rtc::GetBE16(fingerprint_attr_data + sizeof(uint16_t)) !=
266 StunUInt32Attribute::SIZE) 265 StunUInt32Attribute::SIZE)
267 return false; 266 return false;
268 267
269 // Check the fingerprint value. 268 // Check the fingerprint value.
270 uint32 fingerprint = 269 uint32_t fingerprint =
271 rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize); 270 rtc::GetBE32(fingerprint_attr_data + kStunAttributeHeaderSize);
272 return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) == 271 return ((fingerprint ^ STUN_FINGERPRINT_XOR_VALUE) ==
273 rtc::ComputeCrc32(data, size - fingerprint_attr_size)); 272 rtc::ComputeCrc32(data, size - fingerprint_attr_size));
274 } 273 }
275 274
276 bool StunMessage::AddFingerprint() { 275 bool StunMessage::AddFingerprint() {
277 // Add the attribute with a dummy value. Since this is a known attribute, 276 // Add the attribute with a dummy value. Since this is a known attribute,
278 // it can't fail. 277 // it can't fail.
279 StunUInt32Attribute* fingerprint_attr = 278 StunUInt32Attribute* fingerprint_attr =
280 new StunUInt32Attribute(STUN_ATTR_FINGERPRINT, 0); 279 new StunUInt32Attribute(STUN_ATTR_FINGERPRINT, 0);
281 VERIFY(AddAttribute(fingerprint_attr)); 280 VERIFY(AddAttribute(fingerprint_attr));
282 281
283 // Calculate the CRC-32 for the message and insert it. 282 // Calculate the CRC-32 for the message and insert it.
284 rtc::ByteBuffer buf; 283 rtc::ByteBuffer buf;
285 if (!Write(&buf)) 284 if (!Write(&buf))
286 return false; 285 return false;
287 286
288 int msg_len_for_crc32 = static_cast<int>( 287 int msg_len_for_crc32 = static_cast<int>(
289 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length()); 288 buf.Length() - kStunAttributeHeaderSize - fingerprint_attr->length());
290 uint32 c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32); 289 uint32_t c = rtc::ComputeCrc32(buf.Data(), msg_len_for_crc32);
291 290
292 // Insert the correct CRC-32, XORed with a constant, into the attribute. 291 // Insert the correct CRC-32, XORed with a constant, into the attribute.
293 fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE); 292 fingerprint_attr->SetValue(c ^ STUN_FINGERPRINT_XOR_VALUE);
294 return true; 293 return true;
295 } 294 }
296 295
297 bool StunMessage::Read(ByteBuffer* buf) { 296 bool StunMessage::Read(ByteBuffer* buf) {
298 if (!buf->ReadUInt16(&type_)) 297 if (!buf->ReadUInt16(&type_))
299 return false; 298 return false;
300 299
301 if (type_ & 0x8000) { 300 if (type_ & 0x8000) {
302 // RTP and RTCP set the MSB of first byte, since first two bits are version, 301 // RTP and RTCP set the MSB of first byte, since first two bits are version,
303 // and version is always 2 (10). If set, this is not a STUN packet. 302 // and version is always 2 (10). If set, this is not a STUN packet.
304 return false; 303 return false;
305 } 304 }
306 305
307 if (!buf->ReadUInt16(&length_)) 306 if (!buf->ReadUInt16(&length_))
308 return false; 307 return false;
309 308
310 std::string magic_cookie; 309 std::string magic_cookie;
311 if (!buf->ReadString(&magic_cookie, kStunMagicCookieLength)) 310 if (!buf->ReadString(&magic_cookie, kStunMagicCookieLength))
312 return false; 311 return false;
313 312
314 std::string transaction_id; 313 std::string transaction_id;
315 if (!buf->ReadString(&transaction_id, kStunTransactionIdLength)) 314 if (!buf->ReadString(&transaction_id, kStunTransactionIdLength))
316 return false; 315 return false;
317 316
318 uint32 magic_cookie_int = 317 uint32_t magic_cookie_int =
319 *reinterpret_cast<const uint32*>(magic_cookie.data()); 318 *reinterpret_cast<const uint32_t*>(magic_cookie.data());
320 if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) { 319 if (rtc::NetworkToHost32(magic_cookie_int) != kStunMagicCookie) {
321 // If magic cookie is invalid it means that the peer implements 320 // If magic cookie is invalid it means that the peer implements
322 // RFC3489 instead of RFC5389. 321 // RFC3489 instead of RFC5389.
323 transaction_id.insert(0, magic_cookie); 322 transaction_id.insert(0, magic_cookie);
324 } 323 }
325 ASSERT(IsValidTransactionId(transaction_id)); 324 ASSERT(IsValidTransactionId(transaction_id));
326 transaction_id_ = transaction_id; 325 transaction_id_ = transaction_id;
327 326
328 if (length_ != buf->Length()) 327 if (length_ != buf->Length())
329 return false; 328 return false;
330 329
331 attrs_->resize(0); 330 attrs_->resize(0);
332 331
333 size_t rest = buf->Length() - length_; 332 size_t rest = buf->Length() - length_;
334 while (buf->Length() > rest) { 333 while (buf->Length() > rest) {
335 uint16 attr_type, attr_length; 334 uint16_t attr_type, attr_length;
336 if (!buf->ReadUInt16(&attr_type)) 335 if (!buf->ReadUInt16(&attr_type))
337 return false; 336 return false;
338 if (!buf->ReadUInt16(&attr_length)) 337 if (!buf->ReadUInt16(&attr_length))
339 return false; 338 return false;
340 339
341 StunAttribute* attr = CreateAttribute(attr_type, attr_length); 340 StunAttribute* attr = CreateAttribute(attr_type, attr_length);
342 if (!attr) { 341 if (!attr) {
343 // Skip any unknown or malformed attributes. 342 // Skip any unknown or malformed attributes.
344 if ((attr_length % 4) != 0) { 343 if ((attr_length % 4) != 0) {
345 attr_length += (4 - (attr_length % 4)); 344 attr_length += (4 - (attr_length % 4));
(...skipping 13 matching lines...) Expand all
359 358
360 bool StunMessage::Write(ByteBuffer* buf) const { 359 bool StunMessage::Write(ByteBuffer* buf) const {
361 buf->WriteUInt16(type_); 360 buf->WriteUInt16(type_);
362 buf->WriteUInt16(length_); 361 buf->WriteUInt16(length_);
363 if (!IsLegacy()) 362 if (!IsLegacy())
364 buf->WriteUInt32(kStunMagicCookie); 363 buf->WriteUInt32(kStunMagicCookie);
365 buf->WriteString(transaction_id_); 364 buf->WriteString(transaction_id_);
366 365
367 for (size_t i = 0; i < attrs_->size(); ++i) { 366 for (size_t i = 0; i < attrs_->size(); ++i) {
368 buf->WriteUInt16((*attrs_)[i]->type()); 367 buf->WriteUInt16((*attrs_)[i]->type());
369 buf->WriteUInt16(static_cast<uint16>((*attrs_)[i]->length())); 368 buf->WriteUInt16(static_cast<uint16_t>((*attrs_)[i]->length()));
370 if (!(*attrs_)[i]->Write(buf)) 369 if (!(*attrs_)[i]->Write(buf))
371 return false; 370 return false;
372 } 371 }
373 372
374 return true; 373 return true;
375 } 374 }
376 375
377 StunAttributeValueType StunMessage::GetAttributeValueType(int type) const { 376 StunAttributeValueType StunMessage::GetAttributeValueType(int type) const {
378 switch (type) { 377 switch (type) {
379 case STUN_ATTR_MAPPED_ADDRESS: return STUN_VALUE_ADDRESS; 378 case STUN_ATTR_MAPPED_ADDRESS: return STUN_VALUE_ADDRESS;
380 case STUN_ATTR_USERNAME: return STUN_VALUE_BYTE_STRING; 379 case STUN_ATTR_USERNAME: return STUN_VALUE_BYTE_STRING;
381 case STUN_ATTR_MESSAGE_INTEGRITY: return STUN_VALUE_BYTE_STRING; 380 case STUN_ATTR_MESSAGE_INTEGRITY: return STUN_VALUE_BYTE_STRING;
382 case STUN_ATTR_ERROR_CODE: return STUN_VALUE_ERROR_CODE; 381 case STUN_ATTR_ERROR_CODE: return STUN_VALUE_ERROR_CODE;
383 case STUN_ATTR_UNKNOWN_ATTRIBUTES: return STUN_VALUE_UINT16_LIST; 382 case STUN_ATTR_UNKNOWN_ATTRIBUTES: return STUN_VALUE_UINT16_LIST;
384 case STUN_ATTR_REALM: return STUN_VALUE_BYTE_STRING; 383 case STUN_ATTR_REALM: return STUN_VALUE_BYTE_STRING;
385 case STUN_ATTR_NONCE: return STUN_VALUE_BYTE_STRING; 384 case STUN_ATTR_NONCE: return STUN_VALUE_BYTE_STRING;
386 case STUN_ATTR_XOR_MAPPED_ADDRESS: return STUN_VALUE_XOR_ADDRESS; 385 case STUN_ATTR_XOR_MAPPED_ADDRESS: return STUN_VALUE_XOR_ADDRESS;
387 case STUN_ATTR_SOFTWARE: return STUN_VALUE_BYTE_STRING; 386 case STUN_ATTR_SOFTWARE: return STUN_VALUE_BYTE_STRING;
388 case STUN_ATTR_ALTERNATE_SERVER: return STUN_VALUE_ADDRESS; 387 case STUN_ATTR_ALTERNATE_SERVER: return STUN_VALUE_ADDRESS;
389 case STUN_ATTR_FINGERPRINT: return STUN_VALUE_UINT32; 388 case STUN_ATTR_FINGERPRINT: return STUN_VALUE_UINT32;
390 case STUN_ATTR_ORIGIN: return STUN_VALUE_BYTE_STRING; 389 case STUN_ATTR_ORIGIN: return STUN_VALUE_BYTE_STRING;
391 case STUN_ATTR_RETRANSMIT_COUNT: return STUN_VALUE_UINT32; 390 case STUN_ATTR_RETRANSMIT_COUNT: return STUN_VALUE_UINT32;
392 default: return STUN_VALUE_UNKNOWN; 391 default: return STUN_VALUE_UNKNOWN;
393 } 392 }
394 } 393 }
395 394
396 StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ { 395 StunAttribute* StunMessage::CreateAttribute(int type, size_t length) /*const*/ {
397 StunAttributeValueType value_type = GetAttributeValueType(type); 396 StunAttributeValueType value_type = GetAttributeValueType(type);
398 return StunAttribute::Create(value_type, type, 397 return StunAttribute::Create(value_type, type, static_cast<uint16_t>(length),
399 static_cast<uint16>(length), this); 398 this);
400 } 399 }
401 400
402 const StunAttribute* StunMessage::GetAttribute(int type) const { 401 const StunAttribute* StunMessage::GetAttribute(int type) const {
403 for (size_t i = 0; i < attrs_->size(); ++i) { 402 for (size_t i = 0; i < attrs_->size(); ++i) {
404 if ((*attrs_)[i]->type() == type) 403 if ((*attrs_)[i]->type() == type)
405 return (*attrs_)[i]; 404 return (*attrs_)[i];
406 } 405 }
407 return NULL; 406 return NULL;
408 } 407 }
409 408
410 bool StunMessage::IsValidTransactionId(const std::string& transaction_id) { 409 bool StunMessage::IsValidTransactionId(const std::string& transaction_id) {
411 return transaction_id.size() == kStunTransactionIdLength || 410 return transaction_id.size() == kStunTransactionIdLength ||
412 transaction_id.size() == kStunLegacyTransactionIdLength; 411 transaction_id.size() == kStunLegacyTransactionIdLength;
413 } 412 }
414 413
415 // StunAttribute 414 // StunAttribute
416 415
417 StunAttribute::StunAttribute(uint16 type, uint16 length) 416 StunAttribute::StunAttribute(uint16_t type, uint16_t length)
418 : type_(type), length_(length) { 417 : type_(type), length_(length) {
419 } 418 }
420 419
421 void StunAttribute::ConsumePadding(rtc::ByteBuffer* buf) const { 420 void StunAttribute::ConsumePadding(rtc::ByteBuffer* buf) const {
422 int remainder = length_ % 4; 421 int remainder = length_ % 4;
423 if (remainder > 0) { 422 if (remainder > 0) {
424 buf->Consume(4 - remainder); 423 buf->Consume(4 - remainder);
425 } 424 }
426 } 425 }
427 426
428 void StunAttribute::WritePadding(rtc::ByteBuffer* buf) const { 427 void StunAttribute::WritePadding(rtc::ByteBuffer* buf) const {
429 int remainder = length_ % 4; 428 int remainder = length_ % 4;
430 if (remainder > 0) { 429 if (remainder > 0) {
431 char zeroes[4] = {0}; 430 char zeroes[4] = {0};
432 buf->WriteBytes(zeroes, 4 - remainder); 431 buf->WriteBytes(zeroes, 4 - remainder);
433 } 432 }
434 } 433 }
435 434
436 StunAttribute* StunAttribute::Create(StunAttributeValueType value_type, 435 StunAttribute* StunAttribute::Create(StunAttributeValueType value_type,
437 uint16 type, uint16 length, 436 uint16_t type,
437 uint16_t length,
438 StunMessage* owner) { 438 StunMessage* owner) {
439 switch (value_type) { 439 switch (value_type) {
440 case STUN_VALUE_ADDRESS: 440 case STUN_VALUE_ADDRESS:
441 return new StunAddressAttribute(type, length); 441 return new StunAddressAttribute(type, length);
442 case STUN_VALUE_XOR_ADDRESS: 442 case STUN_VALUE_XOR_ADDRESS:
443 return new StunXorAddressAttribute(type, length, owner); 443 return new StunXorAddressAttribute(type, length, owner);
444 case STUN_VALUE_UINT32: 444 case STUN_VALUE_UINT32:
445 return new StunUInt32Attribute(type); 445 return new StunUInt32Attribute(type);
446 case STUN_VALUE_UINT64: 446 case STUN_VALUE_UINT64:
447 return new StunUInt64Attribute(type); 447 return new StunUInt64Attribute(type);
448 case STUN_VALUE_BYTE_STRING: 448 case STUN_VALUE_BYTE_STRING:
449 return new StunByteStringAttribute(type, length); 449 return new StunByteStringAttribute(type, length);
450 case STUN_VALUE_ERROR_CODE: 450 case STUN_VALUE_ERROR_CODE:
451 return new StunErrorCodeAttribute(type, length); 451 return new StunErrorCodeAttribute(type, length);
452 case STUN_VALUE_UINT16_LIST: 452 case STUN_VALUE_UINT16_LIST:
453 return new StunUInt16ListAttribute(type, length); 453 return new StunUInt16ListAttribute(type, length);
454 default: 454 default:
455 return NULL; 455 return NULL;
456 } 456 }
457 } 457 }
458 458
459 StunAddressAttribute* StunAttribute::CreateAddress(uint16 type) { 459 StunAddressAttribute* StunAttribute::CreateAddress(uint16_t type) {
460 return new StunAddressAttribute(type, 0); 460 return new StunAddressAttribute(type, 0);
461 } 461 }
462 462
463 StunXorAddressAttribute* StunAttribute::CreateXorAddress(uint16 type) { 463 StunXorAddressAttribute* StunAttribute::CreateXorAddress(uint16_t type) {
464 return new StunXorAddressAttribute(type, 0, NULL); 464 return new StunXorAddressAttribute(type, 0, NULL);
465 } 465 }
466 466
467 StunUInt64Attribute* StunAttribute::CreateUInt64(uint16 type) { 467 StunUInt64Attribute* StunAttribute::CreateUInt64(uint16_t type) {
468 return new StunUInt64Attribute(type); 468 return new StunUInt64Attribute(type);
469 } 469 }
470 470
471 StunUInt32Attribute* StunAttribute::CreateUInt32(uint16 type) { 471 StunUInt32Attribute* StunAttribute::CreateUInt32(uint16_t type) {
472 return new StunUInt32Attribute(type); 472 return new StunUInt32Attribute(type);
473 } 473 }
474 474
475 StunByteStringAttribute* StunAttribute::CreateByteString(uint16 type) { 475 StunByteStringAttribute* StunAttribute::CreateByteString(uint16_t type) {
476 return new StunByteStringAttribute(type, 0); 476 return new StunByteStringAttribute(type, 0);
477 } 477 }
478 478
479 StunErrorCodeAttribute* StunAttribute::CreateErrorCode() { 479 StunErrorCodeAttribute* StunAttribute::CreateErrorCode() {
480 return new StunErrorCodeAttribute( 480 return new StunErrorCodeAttribute(
481 STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE); 481 STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE);
482 } 482 }
483 483
484 StunUInt16ListAttribute* StunAttribute::CreateUnknownAttributes() { 484 StunUInt16ListAttribute* StunAttribute::CreateUnknownAttributes() {
485 return new StunUInt16ListAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES, 0); 485 return new StunUInt16ListAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES, 0);
486 } 486 }
487 487
488 StunAddressAttribute::StunAddressAttribute(uint16 type, 488 StunAddressAttribute::StunAddressAttribute(uint16_t type,
489 const rtc::SocketAddress& addr) 489 const rtc::SocketAddress& addr)
490 : StunAttribute(type, 0) { 490 : StunAttribute(type, 0) {
491 SetAddress(addr); 491 SetAddress(addr);
492 } 492 }
493 493
494 StunAddressAttribute::StunAddressAttribute(uint16 type, uint16 length) 494 StunAddressAttribute::StunAddressAttribute(uint16_t type, uint16_t length)
495 : StunAttribute(type, length) { 495 : StunAttribute(type, length) {
496 } 496 }
497 497
498 bool StunAddressAttribute::Read(ByteBuffer* buf) { 498 bool StunAddressAttribute::Read(ByteBuffer* buf) {
499 uint8 dummy; 499 uint8_t dummy;
500 if (!buf->ReadUInt8(&dummy)) 500 if (!buf->ReadUInt8(&dummy))
501 return false; 501 return false;
502 502
503 uint8 stun_family; 503 uint8_t stun_family;
504 if (!buf->ReadUInt8(&stun_family)) { 504 if (!buf->ReadUInt8(&stun_family)) {
505 return false; 505 return false;
506 } 506 }
507 uint16 port; 507 uint16_t port;
508 if (!buf->ReadUInt16(&port)) 508 if (!buf->ReadUInt16(&port))
509 return false; 509 return false;
510 if (stun_family == STUN_ADDRESS_IPV4) { 510 if (stun_family == STUN_ADDRESS_IPV4) {
511 in_addr v4addr; 511 in_addr v4addr;
512 if (length() != SIZE_IP4) { 512 if (length() != SIZE_IP4) {
513 return false; 513 return false;
514 } 514 }
515 if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) { 515 if (!buf->ReadBytes(reinterpret_cast<char*>(&v4addr), sizeof(v4addr))) {
516 return false; 516 return false;
517 } 517 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 } 550 }
551 case AF_INET6: { 551 case AF_INET6: {
552 in6_addr v6addr = address_.ipaddr().ipv6_address(); 552 in6_addr v6addr = address_.ipaddr().ipv6_address();
553 buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr)); 553 buf->WriteBytes(reinterpret_cast<char*>(&v6addr), sizeof(v6addr));
554 break; 554 break;
555 } 555 }
556 } 556 }
557 return true; 557 return true;
558 } 558 }
559 559
560 StunXorAddressAttribute::StunXorAddressAttribute(uint16 type, 560 StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
561 const rtc::SocketAddress& addr) 561 const rtc::SocketAddress& addr)
562 : StunAddressAttribute(type, addr), owner_(NULL) { 562 : StunAddressAttribute(type, addr), owner_(NULL) {
563 } 563 }
564 564
565 StunXorAddressAttribute::StunXorAddressAttribute(uint16 type, 565 StunXorAddressAttribute::StunXorAddressAttribute(uint16_t type,
566 uint16 length, 566 uint16_t length,
567 StunMessage* owner) 567 StunMessage* owner)
568 : StunAddressAttribute(type, length), owner_(owner) {} 568 : StunAddressAttribute(type, length), owner_(owner) {
569 }
569 570
570 rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const { 571 rtc::IPAddress StunXorAddressAttribute::GetXoredIP() const {
571 if (owner_) { 572 if (owner_) {
572 rtc::IPAddress ip = ipaddr(); 573 rtc::IPAddress ip = ipaddr();
573 switch (ip.family()) { 574 switch (ip.family()) {
574 case AF_INET: { 575 case AF_INET: {
575 in_addr v4addr = ip.ipv4_address(); 576 in_addr v4addr = ip.ipv4_address();
576 v4addr.s_addr = 577 v4addr.s_addr =
577 (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie)); 578 (v4addr.s_addr ^ rtc::HostToNetwork32(kStunMagicCookie));
578 return rtc::IPAddress(v4addr); 579 return rtc::IPAddress(v4addr);
579 } 580 }
580 case AF_INET6: { 581 case AF_INET6: {
581 in6_addr v6addr = ip.ipv6_address(); 582 in6_addr v6addr = ip.ipv6_address();
582 const std::string& transaction_id = owner_->transaction_id(); 583 const std::string& transaction_id = owner_->transaction_id();
583 if (transaction_id.length() == kStunTransactionIdLength) { 584 if (transaction_id.length() == kStunTransactionIdLength) {
584 uint32 transactionid_as_ints[3]; 585 uint32_t transactionid_as_ints[3];
585 memcpy(&transactionid_as_ints[0], transaction_id.c_str(), 586 memcpy(&transactionid_as_ints[0], transaction_id.c_str(),
586 transaction_id.length()); 587 transaction_id.length());
587 uint32* ip_as_ints = reinterpret_cast<uint32*>(&v6addr.s6_addr); 588 uint32_t* ip_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
588 // Transaction ID is in network byte order, but magic cookie 589 // Transaction ID is in network byte order, but magic cookie
589 // is stored in host byte order. 590 // is stored in host byte order.
590 ip_as_ints[0] = 591 ip_as_ints[0] =
591 (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie)); 592 (ip_as_ints[0] ^ rtc::HostToNetwork32(kStunMagicCookie));
592 ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]); 593 ip_as_ints[1] = (ip_as_ints[1] ^ transactionid_as_ints[0]);
593 ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]); 594 ip_as_ints[2] = (ip_as_ints[2] ^ transactionid_as_ints[1]);
594 ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]); 595 ip_as_ints[3] = (ip_as_ints[3] ^ transactionid_as_ints[2]);
595 return rtc::IPAddress(v6addr); 596 return rtc::IPAddress(v6addr);
596 } 597 }
597 break; 598 break;
598 } 599 }
599 } 600 }
600 } 601 }
601 // Invalid ip family or transaction ID, or missing owner. 602 // Invalid ip family or transaction ID, or missing owner.
602 // Return an AF_UNSPEC address. 603 // Return an AF_UNSPEC address.
603 return rtc::IPAddress(); 604 return rtc::IPAddress();
604 } 605 }
605 606
606 bool StunXorAddressAttribute::Read(ByteBuffer* buf) { 607 bool StunXorAddressAttribute::Read(ByteBuffer* buf) {
607 if (!StunAddressAttribute::Read(buf)) 608 if (!StunAddressAttribute::Read(buf))
608 return false; 609 return false;
609 uint16 xoredport = port() ^ (kStunMagicCookie >> 16); 610 uint16_t xoredport = port() ^ (kStunMagicCookie >> 16);
610 rtc::IPAddress xored_ip = GetXoredIP(); 611 rtc::IPAddress xored_ip = GetXoredIP();
611 SetAddress(rtc::SocketAddress(xored_ip, xoredport)); 612 SetAddress(rtc::SocketAddress(xored_ip, xoredport));
612 return true; 613 return true;
613 } 614 }
614 615
615 bool StunXorAddressAttribute::Write(ByteBuffer* buf) const { 616 bool StunXorAddressAttribute::Write(ByteBuffer* buf) const {
616 StunAddressFamily address_family = family(); 617 StunAddressFamily address_family = family();
617 if (address_family == STUN_ADDRESS_UNDEF) { 618 if (address_family == STUN_ADDRESS_UNDEF) {
618 LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family."; 619 LOG(LS_ERROR) << "Error writing xor-address attribute: unknown family.";
619 return false; 620 return false;
(...skipping 13 matching lines...) Expand all
633 } 634 }
634 case AF_INET6: { 635 case AF_INET6: {
635 in6_addr v6addr = xored_ip.ipv6_address(); 636 in6_addr v6addr = xored_ip.ipv6_address();
636 buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr)); 637 buf->WriteBytes(reinterpret_cast<const char*>(&v6addr), sizeof(v6addr));
637 break; 638 break;
638 } 639 }
639 } 640 }
640 return true; 641 return true;
641 } 642 }
642 643
643 StunUInt32Attribute::StunUInt32Attribute(uint16 type, uint32 value) 644 StunUInt32Attribute::StunUInt32Attribute(uint16_t type, uint32_t value)
644 : StunAttribute(type, SIZE), bits_(value) { 645 : StunAttribute(type, SIZE), bits_(value) {
645 } 646 }
646 647
647 StunUInt32Attribute::StunUInt32Attribute(uint16 type) 648 StunUInt32Attribute::StunUInt32Attribute(uint16_t type)
648 : StunAttribute(type, SIZE), bits_(0) { 649 : StunAttribute(type, SIZE), bits_(0) {
649 } 650 }
650 651
651 bool StunUInt32Attribute::GetBit(size_t index) const { 652 bool StunUInt32Attribute::GetBit(size_t index) const {
652 ASSERT(index < 32); 653 ASSERT(index < 32);
653 return static_cast<bool>((bits_ >> index) & 0x1); 654 return static_cast<bool>((bits_ >> index) & 0x1);
654 } 655 }
655 656
656 void StunUInt32Attribute::SetBit(size_t index, bool value) { 657 void StunUInt32Attribute::SetBit(size_t index, bool value) {
657 ASSERT(index < 32); 658 ASSERT(index < 32);
658 bits_ &= ~(1 << index); 659 bits_ &= ~(1 << index);
659 bits_ |= value ? (1 << index) : 0; 660 bits_ |= value ? (1 << index) : 0;
660 } 661 }
661 662
662 bool StunUInt32Attribute::Read(ByteBuffer* buf) { 663 bool StunUInt32Attribute::Read(ByteBuffer* buf) {
663 if (length() != SIZE || !buf->ReadUInt32(&bits_)) 664 if (length() != SIZE || !buf->ReadUInt32(&bits_))
664 return false; 665 return false;
665 return true; 666 return true;
666 } 667 }
667 668
668 bool StunUInt32Attribute::Write(ByteBuffer* buf) const { 669 bool StunUInt32Attribute::Write(ByteBuffer* buf) const {
669 buf->WriteUInt32(bits_); 670 buf->WriteUInt32(bits_);
670 return true; 671 return true;
671 } 672 }
672 673
673 StunUInt64Attribute::StunUInt64Attribute(uint16 type, uint64 value) 674 StunUInt64Attribute::StunUInt64Attribute(uint16_t type, uint64_t value)
674 : StunAttribute(type, SIZE), bits_(value) { 675 : StunAttribute(type, SIZE), bits_(value) {
675 } 676 }
676 677
677 StunUInt64Attribute::StunUInt64Attribute(uint16 type) 678 StunUInt64Attribute::StunUInt64Attribute(uint16_t type)
678 : StunAttribute(type, SIZE), bits_(0) { 679 : StunAttribute(type, SIZE), bits_(0) {
679 } 680 }
680 681
681 bool StunUInt64Attribute::Read(ByteBuffer* buf) { 682 bool StunUInt64Attribute::Read(ByteBuffer* buf) {
682 if (length() != SIZE || !buf->ReadUInt64(&bits_)) 683 if (length() != SIZE || !buf->ReadUInt64(&bits_))
683 return false; 684 return false;
684 return true; 685 return true;
685 } 686 }
686 687
687 bool StunUInt64Attribute::Write(ByteBuffer* buf) const { 688 bool StunUInt64Attribute::Write(ByteBuffer* buf) const {
688 buf->WriteUInt64(bits_); 689 buf->WriteUInt64(bits_);
689 return true; 690 return true;
690 } 691 }
691 692
692 StunByteStringAttribute::StunByteStringAttribute(uint16 type) 693 StunByteStringAttribute::StunByteStringAttribute(uint16_t type)
693 : StunAttribute(type, 0), bytes_(NULL) { 694 : StunAttribute(type, 0), bytes_(NULL) {
694 } 695 }
695 696
696 StunByteStringAttribute::StunByteStringAttribute(uint16 type, 697 StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
697 const std::string& str) 698 const std::string& str)
698 : StunAttribute(type, 0), bytes_(NULL) { 699 : StunAttribute(type, 0), bytes_(NULL) {
699 CopyBytes(str.c_str(), str.size()); 700 CopyBytes(str.c_str(), str.size());
700 } 701 }
701 702
702 StunByteStringAttribute::StunByteStringAttribute(uint16 type, 703 StunByteStringAttribute::StunByteStringAttribute(uint16_t type,
703 const void* bytes, 704 const void* bytes,
704 size_t length) 705 size_t length)
705 : StunAttribute(type, 0), bytes_(NULL) { 706 : StunAttribute(type, 0), bytes_(NULL) {
706 CopyBytes(bytes, length); 707 CopyBytes(bytes, length);
707 } 708 }
708 709
709 StunByteStringAttribute::StunByteStringAttribute(uint16 type, uint16 length) 710 StunByteStringAttribute::StunByteStringAttribute(uint16_t type, uint16_t length)
710 : StunAttribute(type, length), bytes_(NULL) { 711 : StunAttribute(type, length), bytes_(NULL) {
711 } 712 }
712 713
713 StunByteStringAttribute::~StunByteStringAttribute() { 714 StunByteStringAttribute::~StunByteStringAttribute() {
714 delete [] bytes_; 715 delete [] bytes_;
715 } 716 }
716 717
717 void StunByteStringAttribute::CopyBytes(const char* bytes) { 718 void StunByteStringAttribute::CopyBytes(const char* bytes) {
718 CopyBytes(bytes, strlen(bytes)); 719 CopyBytes(bytes, strlen(bytes));
719 } 720 }
720 721
721 void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) { 722 void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) {
722 char* new_bytes = new char[length]; 723 char* new_bytes = new char[length];
723 memcpy(new_bytes, bytes, length); 724 memcpy(new_bytes, bytes, length);
724 SetBytes(new_bytes, length); 725 SetBytes(new_bytes, length);
725 } 726 }
726 727
727 uint8 StunByteStringAttribute::GetByte(size_t index) const { 728 uint8_t StunByteStringAttribute::GetByte(size_t index) const {
728 ASSERT(bytes_ != NULL); 729 ASSERT(bytes_ != NULL);
729 ASSERT(index < length()); 730 ASSERT(index < length());
730 return static_cast<uint8>(bytes_[index]); 731 return static_cast<uint8_t>(bytes_[index]);
731 } 732 }
732 733
733 void StunByteStringAttribute::SetByte(size_t index, uint8 value) { 734 void StunByteStringAttribute::SetByte(size_t index, uint8_t value) {
734 ASSERT(bytes_ != NULL); 735 ASSERT(bytes_ != NULL);
735 ASSERT(index < length()); 736 ASSERT(index < length());
736 bytes_[index] = value; 737 bytes_[index] = value;
737 } 738 }
738 739
739 bool StunByteStringAttribute::Read(ByteBuffer* buf) { 740 bool StunByteStringAttribute::Read(ByteBuffer* buf) {
740 bytes_ = new char[length()]; 741 bytes_ = new char[length()];
741 if (!buf->ReadBytes(bytes_, length())) { 742 if (!buf->ReadBytes(bytes_, length())) {
742 return false; 743 return false;
743 } 744 }
744 745
745 ConsumePadding(buf); 746 ConsumePadding(buf);
746 return true; 747 return true;
747 } 748 }
748 749
749 bool StunByteStringAttribute::Write(ByteBuffer* buf) const { 750 bool StunByteStringAttribute::Write(ByteBuffer* buf) const {
750 buf->WriteBytes(bytes_, length()); 751 buf->WriteBytes(bytes_, length());
751 WritePadding(buf); 752 WritePadding(buf);
752 return true; 753 return true;
753 } 754 }
754 755
755 void StunByteStringAttribute::SetBytes(char* bytes, size_t length) { 756 void StunByteStringAttribute::SetBytes(char* bytes, size_t length) {
756 delete [] bytes_; 757 delete [] bytes_;
757 bytes_ = bytes; 758 bytes_ = bytes;
758 SetLength(static_cast<uint16>(length)); 759 SetLength(static_cast<uint16_t>(length));
759 } 760 }
760 761
761 StunErrorCodeAttribute::StunErrorCodeAttribute(uint16 type, int code, 762 StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type,
763 int code,
762 const std::string& reason) 764 const std::string& reason)
763 : StunAttribute(type, 0) { 765 : StunAttribute(type, 0) {
764 SetCode(code); 766 SetCode(code);
765 SetReason(reason); 767 SetReason(reason);
766 } 768 }
767 769
768 StunErrorCodeAttribute::StunErrorCodeAttribute(uint16 type, uint16 length) 770 StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type, uint16_t length)
769 : StunAttribute(type, length), class_(0), number_(0) { 771 : StunAttribute(type, length), class_(0), number_(0) {
770 } 772 }
771 773
772 StunErrorCodeAttribute::~StunErrorCodeAttribute() { 774 StunErrorCodeAttribute::~StunErrorCodeAttribute() {
773 } 775 }
774 776
775 int StunErrorCodeAttribute::code() const { 777 int StunErrorCodeAttribute::code() const {
776 return class_ * 100 + number_; 778 return class_ * 100 + number_;
777 } 779 }
778 780
779 void StunErrorCodeAttribute::SetCode(int code) { 781 void StunErrorCodeAttribute::SetCode(int code) {
780 class_ = static_cast<uint8>(code / 100); 782 class_ = static_cast<uint8_t>(code / 100);
781 number_ = static_cast<uint8>(code % 100); 783 number_ = static_cast<uint8_t>(code % 100);
782 } 784 }
783 785
784 void StunErrorCodeAttribute::SetReason(const std::string& reason) { 786 void StunErrorCodeAttribute::SetReason(const std::string& reason) {
785 SetLength(MIN_SIZE + static_cast<uint16>(reason.size())); 787 SetLength(MIN_SIZE + static_cast<uint16_t>(reason.size()));
786 reason_ = reason; 788 reason_ = reason;
787 } 789 }
788 790
789 bool StunErrorCodeAttribute::Read(ByteBuffer* buf) { 791 bool StunErrorCodeAttribute::Read(ByteBuffer* buf) {
790 uint32 val; 792 uint32_t val;
791 if (length() < MIN_SIZE || !buf->ReadUInt32(&val)) 793 if (length() < MIN_SIZE || !buf->ReadUInt32(&val))
792 return false; 794 return false;
793 795
794 if ((val >> 11) != 0) 796 if ((val >> 11) != 0)
795 LOG(LS_ERROR) << "error-code bits not zero"; 797 LOG(LS_ERROR) << "error-code bits not zero";
796 798
797 class_ = ((val >> 8) & 0x7); 799 class_ = ((val >> 8) & 0x7);
798 number_ = (val & 0xff); 800 number_ = (val & 0xff);
799 801
800 if (!buf->ReadString(&reason_, length() - 4)) 802 if (!buf->ReadString(&reason_, length() - 4))
801 return false; 803 return false;
802 804
803 ConsumePadding(buf); 805 ConsumePadding(buf);
804 return true; 806 return true;
805 } 807 }
806 808
807 bool StunErrorCodeAttribute::Write(ByteBuffer* buf) const { 809 bool StunErrorCodeAttribute::Write(ByteBuffer* buf) const {
808 buf->WriteUInt32(class_ << 8 | number_); 810 buf->WriteUInt32(class_ << 8 | number_);
809 buf->WriteString(reason_); 811 buf->WriteString(reason_);
810 WritePadding(buf); 812 WritePadding(buf);
811 return true; 813 return true;
812 } 814 }
813 815
814 StunUInt16ListAttribute::StunUInt16ListAttribute(uint16 type, uint16 length) 816 StunUInt16ListAttribute::StunUInt16ListAttribute(uint16_t type, uint16_t length)
815 : StunAttribute(type, length) { 817 : StunAttribute(type, length) {
816 attr_types_ = new std::vector<uint16>(); 818 attr_types_ = new std::vector<uint16_t>();
817 } 819 }
818 820
819 StunUInt16ListAttribute::~StunUInt16ListAttribute() { 821 StunUInt16ListAttribute::~StunUInt16ListAttribute() {
820 delete attr_types_; 822 delete attr_types_;
821 } 823 }
822 824
823 size_t StunUInt16ListAttribute::Size() const { 825 size_t StunUInt16ListAttribute::Size() const {
824 return attr_types_->size(); 826 return attr_types_->size();
825 } 827 }
826 828
827 uint16 StunUInt16ListAttribute::GetType(int index) const { 829 uint16_t StunUInt16ListAttribute::GetType(int index) const {
828 return (*attr_types_)[index]; 830 return (*attr_types_)[index];
829 } 831 }
830 832
831 void StunUInt16ListAttribute::SetType(int index, uint16 value) { 833 void StunUInt16ListAttribute::SetType(int index, uint16_t value) {
832 (*attr_types_)[index] = value; 834 (*attr_types_)[index] = value;
833 } 835 }
834 836
835 void StunUInt16ListAttribute::AddType(uint16 value) { 837 void StunUInt16ListAttribute::AddType(uint16_t value) {
836 attr_types_->push_back(value); 838 attr_types_->push_back(value);
837 SetLength(static_cast<uint16>(attr_types_->size() * 2)); 839 SetLength(static_cast<uint16_t>(attr_types_->size() * 2));
838 } 840 }
839 841
840 bool StunUInt16ListAttribute::Read(ByteBuffer* buf) { 842 bool StunUInt16ListAttribute::Read(ByteBuffer* buf) {
841 if (length() % 2) 843 if (length() % 2)
842 return false; 844 return false;
843 845
844 for (size_t i = 0; i < length() / 2; i++) { 846 for (size_t i = 0; i < length() / 2; i++) {
845 uint16 attr; 847 uint16_t attr;
846 if (!buf->ReadUInt16(&attr)) 848 if (!buf->ReadUInt16(&attr))
847 return false; 849 return false;
848 attr_types_->push_back(attr); 850 attr_types_->push_back(attr);
849 } 851 }
850 // Padding of these attributes is done in RFC 5389 style. This is 852 // Padding of these attributes is done in RFC 5389 style. This is
851 // slightly different from RFC3489, but it shouldn't be important. 853 // slightly different from RFC3489, but it shouldn't be important.
852 // RFC3489 pads out to a 32 bit boundary by duplicating one of the 854 // RFC3489 pads out to a 32 bit boundary by duplicating one of the
853 // entries in the list (not necessarily the last one - it's unspecified). 855 // entries in the list (not necessarily the last one - it's unspecified).
854 // RFC5389 pads on the end, and the bytes are always ignored. 856 // RFC5389 pads on the end, and the bytes are always ignored.
855 ConsumePadding(buf); 857 ConsumePadding(buf);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 digest, sizeof(digest)); 909 digest, sizeof(digest));
908 if (size == 0) { 910 if (size == 0) {
909 return false; 911 return false;
910 } 912 }
911 913
912 *hash = std::string(digest, size); 914 *hash = std::string(digest, size);
913 return true; 915 return true;
914 } 916 }
915 917
916 } // namespace cricket 918 } // 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