| 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 |
| 11 #include "webrtc/p2p/base/relayserver.h" | 11 #include "webrtc/p2p/base/relayserver.h" |
| 12 | 12 |
| 13 #ifdef WEBRTC_POSIX | 13 #ifdef WEBRTC_POSIX |
| 14 #include <errno.h> | 14 #include <errno.h> |
| 15 #endif // WEBRTC_POSIX | 15 #endif // WEBRTC_POSIX |
| 16 | 16 |
| 17 #include <algorithm> | 17 #include <algorithm> |
| 18 | 18 |
| 19 #include "webrtc/base/asynctcpsocket.h" | 19 #include "webrtc/base/asynctcpsocket.h" |
| 20 #include "webrtc/base/helpers.h" | 20 #include "webrtc/base/helpers.h" |
| 21 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/socketadapters.h" | 22 #include "webrtc/base/socketadapters.h" |
| 23 | 23 |
| 24 namespace cricket { | 24 namespace cricket { |
| 25 | 25 |
| 26 // By default, we require a ping every 90 seconds. | 26 // By default, we require a ping every 90 seconds. |
| 27 const int MAX_LIFETIME = 15 * 60 * 1000; | 27 const int MAX_LIFETIME = 15 * 60 * 1000; |
| 28 | 28 |
| 29 // The number of bytes in each of the usernames we use. | 29 // The number of bytes in each of the usernames we use. |
| 30 const uint32 USERNAME_LENGTH = 16; | 30 const uint32_t USERNAME_LENGTH = 16; |
| 31 | 31 |
| 32 // Calls SendTo on the given socket and logs any bad results. | 32 // Calls SendTo on the given socket and logs any bad results. |
| 33 void Send(rtc::AsyncPacketSocket* socket, const char* bytes, size_t size, | 33 void Send(rtc::AsyncPacketSocket* socket, const char* bytes, size_t size, |
| 34 const rtc::SocketAddress& addr) { | 34 const rtc::SocketAddress& addr) { |
| 35 rtc::PacketOptions options; | 35 rtc::PacketOptions options; |
| 36 int result = socket->SendTo(bytes, size, addr, options); | 36 int result = socket->SendTo(bytes, size, addr, options); |
| 37 if (result < static_cast<int>(size)) { | 37 if (result < static_cast<int>(size)) { |
| 38 LOG(LS_ERROR) << "SendTo wrote only " << result << " of " << size | 38 LOG(LS_ERROR) << "SendTo wrote only " << result << " of " << size |
| 39 << " bytes"; | 39 << " bytes"; |
| 40 } else if (result < 0) { | 40 } else if (result < 0) { |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } | 256 } |
| 257 | 257 |
| 258 // The initial packet should have a username (which identifies the binding). | 258 // The initial packet should have a username (which identifies the binding). |
| 259 const StunByteStringAttribute* username_attr = | 259 const StunByteStringAttribute* username_attr = |
| 260 msg.GetByteString(STUN_ATTR_USERNAME); | 260 msg.GetByteString(STUN_ATTR_USERNAME); |
| 261 if (!username_attr) { | 261 if (!username_attr) { |
| 262 LOG(LS_WARNING) << "Dropping packet: no username"; | 262 LOG(LS_WARNING) << "Dropping packet: no username"; |
| 263 return; | 263 return; |
| 264 } | 264 } |
| 265 | 265 |
| 266 uint32 length = | 266 uint32_t length = |
| 267 std::min(static_cast<uint32>(username_attr->length()), USERNAME_LENGTH); | 267 std::min(static_cast<uint32_t>(username_attr->length()), USERNAME_LENGTH); |
| 268 std::string username(username_attr->bytes(), length); | 268 std::string username(username_attr->bytes(), length); |
| 269 // TODO: Check the HMAC. | 269 // TODO: Check the HMAC. |
| 270 | 270 |
| 271 // The binding should already be present. | 271 // The binding should already be present. |
| 272 BindingMap::iterator biter = bindings_.find(username); | 272 BindingMap::iterator biter = bindings_.find(username); |
| 273 if (biter == bindings_.end()) { | 273 if (biter == bindings_.end()) { |
| 274 LOG(LS_WARNING) << "Dropping packet: no binding with username"; | 274 LOG(LS_WARNING) << "Dropping packet: no binding with username"; |
| 275 return; | 275 return; |
| 276 } | 276 } |
| 277 | 277 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 RelayServerBinding* binding; | 348 RelayServerBinding* binding; |
| 349 | 349 |
| 350 BindingMap::iterator biter = bindings_.find(username); | 350 BindingMap::iterator biter = bindings_.find(username); |
| 351 if (biter != bindings_.end()) { | 351 if (biter != bindings_.end()) { |
| 352 binding = biter->second; | 352 binding = biter->second; |
| 353 } else { | 353 } else { |
| 354 // NOTE: In the future, bindings will be created by the bot only. This | 354 // NOTE: In the future, bindings will be created by the bot only. This |
| 355 // else-branch will then disappear. | 355 // else-branch will then disappear. |
| 356 | 356 |
| 357 // Compute the appropriate lifetime for this binding. | 357 // Compute the appropriate lifetime for this binding. |
| 358 uint32 lifetime = MAX_LIFETIME; | 358 uint32_t lifetime = MAX_LIFETIME; |
| 359 const StunUInt32Attribute* lifetime_attr = | 359 const StunUInt32Attribute* lifetime_attr = |
| 360 request.GetUInt32(STUN_ATTR_LIFETIME); | 360 request.GetUInt32(STUN_ATTR_LIFETIME); |
| 361 if (lifetime_attr) | 361 if (lifetime_attr) |
| 362 lifetime = std::min(lifetime, lifetime_attr->value() * 1000); | 362 lifetime = std::min(lifetime, lifetime_attr->value() * 1000); |
| 363 | 363 |
| 364 binding = new RelayServerBinding(this, username, "0", lifetime); | 364 binding = new RelayServerBinding(this, username, "0", lifetime); |
| 365 binding->SignalTimeout.connect(this, &RelayServer::OnTimeout); | 365 binding->SignalTimeout.connect(this, &RelayServer::OnTimeout); |
| 366 bindings_[username] = binding; | 366 bindings_[username] = binding; |
| 367 | 367 |
| 368 if (log_bindings_) { | 368 if (log_bindings_) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 bindings_.erase(iter); | 523 bindings_.erase(iter); |
| 524 | 524 |
| 525 if (log_bindings_) { | 525 if (log_bindings_) { |
| 526 LOG(LS_INFO) << "Removed binding " << binding->username() << ", " | 526 LOG(LS_INFO) << "Removed binding " << binding->username() << ", " |
| 527 << bindings_.size() << " remaining"; | 527 << bindings_.size() << " remaining"; |
| 528 } | 528 } |
| 529 } | 529 } |
| 530 | 530 |
| 531 void RelayServer::OnMessage(rtc::Message *pmsg) { | 531 void RelayServer::OnMessage(rtc::Message *pmsg) { |
| 532 #if ENABLE_DEBUG | 532 #if ENABLE_DEBUG |
| 533 static const uint32 kMessageAcceptConnection = 1; | 533 static const uint32_t kMessageAcceptConnection = 1; |
| 534 ASSERT(pmsg->message_id == kMessageAcceptConnection); | 534 ASSERT(pmsg->message_id == kMessageAcceptConnection); |
| 535 #endif | 535 #endif |
| 536 rtc::MessageData* data = pmsg->pdata; | 536 rtc::MessageData* data = pmsg->pdata; |
| 537 rtc::AsyncSocket* socket = | 537 rtc::AsyncSocket* socket = |
| 538 static_cast <rtc::TypedMessageData<rtc::AsyncSocket*>*> | 538 static_cast <rtc::TypedMessageData<rtc::AsyncSocket*>*> |
| 539 (data)->data(); | 539 (data)->data(); |
| 540 AcceptConnection(socket); | 540 AcceptConnection(socket); |
| 541 delete data; | 541 delete data; |
| 542 } | 542 } |
| 543 | 543 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 | 609 |
| 610 StunAddressAttribute* addr_attr = | 610 StunAddressAttribute* addr_attr = |
| 611 StunAttribute::CreateAddress(STUN_ATTR_SOURCE_ADDRESS2); | 611 StunAttribute::CreateAddress(STUN_ATTR_SOURCE_ADDRESS2); |
| 612 addr_attr->SetIP(from_addr.ipaddr()); | 612 addr_attr->SetIP(from_addr.ipaddr()); |
| 613 addr_attr->SetPort(from_addr.port()); | 613 addr_attr->SetPort(from_addr.port()); |
| 614 msg.AddAttribute(addr_attr); | 614 msg.AddAttribute(addr_attr); |
| 615 | 615 |
| 616 StunByteStringAttribute* data_attr = | 616 StunByteStringAttribute* data_attr = |
| 617 StunAttribute::CreateByteString(STUN_ATTR_DATA); | 617 StunAttribute::CreateByteString(STUN_ATTR_DATA); |
| 618 ASSERT(size <= 65536); | 618 ASSERT(size <= 65536); |
| 619 data_attr->CopyBytes(data, uint16(size)); | 619 data_attr->CopyBytes(data, uint16_t(size)); |
| 620 msg.AddAttribute(data_attr); | 620 msg.AddAttribute(data_attr); |
| 621 | 621 |
| 622 SendStun(msg); | 622 SendStun(msg); |
| 623 } | 623 } |
| 624 | 624 |
| 625 void RelayServerConnection::SendStun(const StunMessage& msg) { | 625 void RelayServerConnection::SendStun(const StunMessage& msg) { |
| 626 // Note that the binding has been used again. | 626 // Note that the binding has been used again. |
| 627 binding_->NoteUsed(); | 627 binding_->NoteUsed(); |
| 628 | 628 |
| 629 cricket::SendStun(msg, socket_, addr_pair_.source()); | 629 cricket::SendStun(msg, socket_, addr_pair_.source()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 641 | 641 |
| 642 void RelayServerConnection::Lock() { | 642 void RelayServerConnection::Lock() { |
| 643 locked_ = true; | 643 locked_ = true; |
| 644 } | 644 } |
| 645 | 645 |
| 646 void RelayServerConnection::Unlock() { | 646 void RelayServerConnection::Unlock() { |
| 647 locked_ = false; | 647 locked_ = false; |
| 648 } | 648 } |
| 649 | 649 |
| 650 // IDs used for posted messages: | 650 // IDs used for posted messages: |
| 651 const uint32 MSG_LIFETIME_TIMER = 1; | 651 const uint32_t MSG_LIFETIME_TIMER = 1; |
| 652 | 652 |
| 653 RelayServerBinding::RelayServerBinding( | 653 RelayServerBinding::RelayServerBinding(RelayServer* server, |
| 654 RelayServer* server, const std::string& username, | 654 const std::string& username, |
| 655 const std::string& password, uint32 lifetime) | 655 const std::string& password, |
| 656 : server_(server), username_(username), password_(password), | 656 uint32_t lifetime) |
| 657 lifetime_(lifetime) { | 657 : server_(server), |
| 658 username_(username), |
| 659 password_(password), |
| 660 lifetime_(lifetime) { |
| 658 // For now, every connection uses the standard magic cookie value. | 661 // For now, every connection uses the standard magic cookie value. |
| 659 magic_cookie_.append( | 662 magic_cookie_.append( |
| 660 reinterpret_cast<const char*>(TURN_MAGIC_COOKIE_VALUE), | 663 reinterpret_cast<const char*>(TURN_MAGIC_COOKIE_VALUE), |
| 661 sizeof(TURN_MAGIC_COOKIE_VALUE)); | 664 sizeof(TURN_MAGIC_COOKIE_VALUE)); |
| 662 | 665 |
| 663 // Initialize the last-used time to now. | 666 // Initialize the last-used time to now. |
| 664 NoteUsed(); | 667 NoteUsed(); |
| 665 | 668 |
| 666 // Set the first timeout check. | 669 // Set the first timeout check. |
| 667 server_->thread()->PostDelayed(lifetime_, this, MSG_LIFETIME_TIMER); | 670 server_->thread()->PostDelayed(lifetime_, this, MSG_LIFETIME_TIMER); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 } else { | 740 } else { |
| 738 server_->thread()->PostDelayed(lifetime_, this, MSG_LIFETIME_TIMER); | 741 server_->thread()->PostDelayed(lifetime_, this, MSG_LIFETIME_TIMER); |
| 739 } | 742 } |
| 740 | 743 |
| 741 } else { | 744 } else { |
| 742 ASSERT(false); | 745 ASSERT(false); |
| 743 } | 746 } |
| 744 } | 747 } |
| 745 | 748 |
| 746 } // namespace cricket | 749 } // namespace cricket |
| OLD | NEW |