You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/p2p/base/stunrequest.h"
  28. #include "talk/base/common.h"
  29. #include "talk/base/helpers.h"
  30. #include "talk/base/logging.h"
  31. namespace cricket {
  32. const uint32 MSG_STUN_SEND = 1;
  33. const int MAX_SENDS = 9;
  34. const int DELAY_UNIT = 100; // 100 milliseconds
  35. const int DELAY_MAX_FACTOR = 16;
  36. StunRequestManager::StunRequestManager(talk_base::Thread* thread)
  37. : thread_(thread) {
  38. }
  39. StunRequestManager::~StunRequestManager() {
  40. while (requests_.begin() != requests_.end()) {
  41. StunRequest *request = requests_.begin()->second;
  42. requests_.erase(requests_.begin());
  43. delete request;
  44. }
  45. }
  46. void StunRequestManager::Send(StunRequest* request) {
  47. SendDelayed(request, 0);
  48. }
  49. void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
  50. request->set_manager(this);
  51. ASSERT(requests_.find(request->id()) == requests_.end());
  52. request->Construct();
  53. requests_[request->id()] = request;
  54. thread_->PostDelayed(delay, request, MSG_STUN_SEND, NULL);
  55. }
  56. void StunRequestManager::Remove(StunRequest* request) {
  57. ASSERT(request->manager() == this);
  58. RequestMap::iterator iter = requests_.find(request->id());
  59. if (iter != requests_.end()) {
  60. ASSERT(iter->second == request);
  61. requests_.erase(iter);
  62. thread_->Clear(request);
  63. }
  64. }
  65. void StunRequestManager::Clear() {
  66. std::vector<StunRequest*> requests;
  67. for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i)
  68. requests.push_back(i->second);
  69. for (uint32 i = 0; i < requests.size(); ++i) {
  70. // StunRequest destructor calls Remove() which deletes requests
  71. // from |requests_|.
  72. delete requests[i];
  73. }
  74. }
  75. bool StunRequestManager::CheckResponse(StunMessage* msg) {
  76. RequestMap::iterator iter = requests_.find(msg->transaction_id());
  77. if (iter == requests_.end())
  78. return false;
  79. StunRequest* request = iter->second;
  80. if (msg->type() == GetStunSuccessResponseType(request->type())) {
  81. request->OnResponse(msg);
  82. } else if (msg->type() == GetStunErrorResponseType(request->type())) {
  83. request->OnErrorResponse(msg);
  84. } else {
  85. LOG(LERROR) << "Received response with wrong type: " << msg->type()
  86. << " (expecting "
  87. << GetStunSuccessResponseType(request->type()) << ")";
  88. return false;
  89. }
  90. delete request;
  91. return true;
  92. }
  93. bool StunRequestManager::CheckResponse(const char* data, size_t size) {
  94. // Check the appropriate bytes of the stream to see if they match the
  95. // transaction ID of a response we are expecting.
  96. if (size < 20)
  97. return false;
  98. std::string id;
  99. id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
  100. RequestMap::iterator iter = requests_.find(id);
  101. if (iter == requests_.end())
  102. return false;
  103. // Parse the STUN message and continue processing as usual.
  104. talk_base::ByteBuffer buf(data, size);
  105. talk_base::scoped_ptr<StunMessage> response(iter->second->msg_->CreateNew());
  106. if (!response->Read(&buf))
  107. return false;
  108. return CheckResponse(response.get());
  109. }
  110. StunRequest::StunRequest()
  111. : count_(0), timeout_(false), manager_(0),
  112. msg_(new StunMessage()), tstamp_(0) {
  113. msg_->SetTransactionID(
  114. talk_base::CreateRandomString(kStunTransactionIdLength));
  115. }
  116. StunRequest::StunRequest(StunMessage* request)
  117. : count_(0), timeout_(false), manager_(0),
  118. msg_(request), tstamp_(0) {
  119. msg_->SetTransactionID(
  120. talk_base::CreateRandomString(kStunTransactionIdLength));
  121. }
  122. StunRequest::~StunRequest() {
  123. ASSERT(manager_ != NULL);
  124. if (manager_) {
  125. manager_->Remove(this);
  126. manager_->thread_->Clear(this);
  127. }
  128. delete msg_;
  129. }
  130. void StunRequest::Construct() {
  131. if (msg_->type() == 0) {
  132. Prepare(msg_);
  133. ASSERT(msg_->type() != 0);
  134. }
  135. }
  136. int StunRequest::type() {
  137. ASSERT(msg_ != NULL);
  138. return msg_->type();
  139. }
  140. void StunRequest::set_manager(StunRequestManager* manager) {
  141. ASSERT(!manager_);
  142. manager_ = manager;
  143. }
  144. void StunRequest::OnMessage(talk_base::Message* pmsg) {
  145. ASSERT(manager_ != NULL);
  146. ASSERT(pmsg->message_id == MSG_STUN_SEND);
  147. if (timeout_) {
  148. OnTimeout();
  149. delete this;
  150. return;
  151. }
  152. tstamp_ = talk_base::Time();
  153. talk_base::ByteBuffer buf;
  154. msg_->Write(&buf);
  155. manager_->SignalSendPacket(buf.Data(), buf.Length(), this);
  156. int delay = GetNextDelay();
  157. manager_->thread_->PostDelayed(delay, this, MSG_STUN_SEND, NULL);
  158. }
  159. uint32 StunRequest::Elapsed() const {
  160. return talk_base::TimeSince(tstamp_);
  161. }
  162. int StunRequest::GetNextDelay() {
  163. int delay = DELAY_UNIT * talk_base::_min(1 << count_, DELAY_MAX_FACTOR);
  164. count_ += 1;
  165. if (count_ == MAX_SENDS)
  166. timeout_ = true;
  167. return delay;
  168. }
  169. } // namespace cricket