OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 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 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 // If the actual socket allocation fails, send an internal error. | 385 // If the actual socket allocation fails, send an internal error. |
386 TurnServerAllocation* alloc = CreateAllocation(conn, proto, key); | 386 TurnServerAllocation* alloc = CreateAllocation(conn, proto, key); |
387 if (alloc) { | 387 if (alloc) { |
388 alloc->HandleTurnMessage(msg); | 388 alloc->HandleTurnMessage(msg); |
389 } else { | 389 } else { |
390 SendErrorResponse(conn, msg, STUN_ERROR_SERVER_ERROR, | 390 SendErrorResponse(conn, msg, STUN_ERROR_SERVER_ERROR, |
391 "Failed to allocate socket"); | 391 "Failed to allocate socket"); |
392 } | 392 } |
393 } | 393 } |
394 | 394 |
395 std::string TurnServer::GenerateNonce() const { | 395 std::string TurnServer::GenerateNonce(uint32_t now) const { |
396 // Generate a nonce of the form hex(now + HMAC-MD5(nonce_key_, now)) | 396 // Generate a nonce of the form hex(now + HMAC-MD5(nonce_key_, now)) |
397 uint32_t now = rtc::Time(); | |
398 std::string input(reinterpret_cast<const char*>(&now), sizeof(now)); | 397 std::string input(reinterpret_cast<const char*>(&now), sizeof(now)); |
399 std::string nonce = rtc::hex_encode(input.c_str(), input.size()); | 398 std::string nonce = rtc::hex_encode(input.c_str(), input.size()); |
400 nonce += rtc::ComputeHmac(rtc::DIGEST_MD5, nonce_key_, input); | 399 nonce += rtc::ComputeHmac(rtc::DIGEST_MD5, nonce_key_, input); |
401 ASSERT(nonce.size() == kNonceSize); | 400 ASSERT(nonce.size() == kNonceSize); |
402 return nonce; | 401 return nonce; |
403 } | 402 } |
404 | 403 |
405 bool TurnServer::ValidateNonce(const std::string& nonce) const { | 404 bool TurnServer::ValidateNonce(const std::string& nonce) const { |
406 // Check the size. | 405 // Check the size. |
407 if (nonce.size() != kNonceSize) { | 406 if (nonce.size() != kNonceSize) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 LOG(LS_INFO) << "Sending error response, type=" << resp.type() | 456 LOG(LS_INFO) << "Sending error response, type=" << resp.type() |
458 << ", code=" << code << ", reason=" << reason; | 457 << ", code=" << code << ", reason=" << reason; |
459 SendStun(conn, &resp); | 458 SendStun(conn, &resp); |
460 } | 459 } |
461 | 460 |
462 void TurnServer::SendErrorResponseWithRealmAndNonce( | 461 void TurnServer::SendErrorResponseWithRealmAndNonce( |
463 TurnServerConnection* conn, const StunMessage* msg, | 462 TurnServerConnection* conn, const StunMessage* msg, |
464 int code, const std::string& reason) { | 463 int code, const std::string& reason) { |
465 TurnMessage resp; | 464 TurnMessage resp; |
466 InitErrorResponse(msg, code, reason, &resp); | 465 InitErrorResponse(msg, code, reason, &resp); |
467 VERIFY(resp.AddAttribute(new StunByteStringAttribute( | 466 |
468 STUN_ATTR_NONCE, GenerateNonce()))); | 467 uint32_t timestamp = rtc::Time(); |
| 468 if (ts_for_next_nonce_) { |
| 469 timestamp = ts_for_next_nonce_; |
| 470 ts_for_next_nonce_ = 0; |
| 471 } |
| 472 VERIFY(resp.AddAttribute( |
| 473 new StunByteStringAttribute(STUN_ATTR_NONCE, GenerateNonce(timestamp)))); |
469 VERIFY(resp.AddAttribute(new StunByteStringAttribute( | 474 VERIFY(resp.AddAttribute(new StunByteStringAttribute( |
470 STUN_ATTR_REALM, realm_))); | 475 STUN_ATTR_REALM, realm_))); |
471 SendStun(conn, &resp); | 476 SendStun(conn, &resp); |
472 } | 477 } |
473 | 478 |
474 void TurnServer::SendErrorResponseWithAlternateServer( | 479 void TurnServer::SendErrorResponseWithAlternateServer( |
475 TurnServerConnection* conn, const StunMessage* msg, | 480 TurnServerConnection* conn, const StunMessage* msg, |
476 const rtc::SocketAddress& addr) { | 481 const rtc::SocketAddress& addr) { |
477 TurnMessage resp; | 482 TurnMessage resp; |
478 InitErrorResponse(msg, STUN_ERROR_TRY_ALTERNATE, | 483 InitErrorResponse(msg, STUN_ERROR_TRY_ALTERNATE, |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 thread_->PostDelayed(kChannelTimeout, this, MSG_ALLOCATION_TIMEOUT); | 947 thread_->PostDelayed(kChannelTimeout, this, MSG_ALLOCATION_TIMEOUT); |
943 } | 948 } |
944 | 949 |
945 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { | 950 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { |
946 ASSERT(msg->message_id == MSG_ALLOCATION_TIMEOUT); | 951 ASSERT(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
947 SignalDestroyed(this); | 952 SignalDestroyed(this); |
948 delete this; | 953 delete this; |
949 } | 954 } |
950 | 955 |
951 } // namespace cricket | 956 } // namespace cricket |
OLD | NEW |