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.

278 lines
7.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "p2p/base/stun_request.h"
  11. #include <algorithm>
  12. #include <memory>
  13. #include <vector>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/helpers.h"
  16. #include "rtc_base/logging.h"
  17. #include "rtc_base/time_utils.h" // For TimeMillis
  18. #include "system_wrappers/include/field_trial.h"
  19. namespace cricket {
  20. const uint32_t MSG_STUN_SEND = 1;
  21. // RFC 5389 says SHOULD be 500ms.
  22. // For years, this was 100ms, but for networks that
  23. // experience moments of high RTT (such as 2G networks), this doesn't
  24. // work well.
  25. const int STUN_INITIAL_RTO = 250; // milliseconds
  26. // The timeout doubles each retransmission, up to this many times
  27. // RFC 5389 says SHOULD retransmit 7 times.
  28. // This has been 8 for years (not sure why).
  29. const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9
  30. const int STUN_MAX_RETRANSMISSIONS_RFC_5389 = 6; // Total sends: 7
  31. // We also cap the doubling, even though the standard doesn't say to.
  32. // This has been 1.6 seconds for years, but for networks that
  33. // experience moments of high RTT (such as 2G networks), this doesn't
  34. // work well.
  35. const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings
  36. namespace {
  37. const char kRfc5389StunRetransmissions[] = "WebRTC-Rfc5389StunRetransmissions";
  38. } // namespace
  39. StunRequestManager::StunRequestManager(rtc::Thread* thread) : thread_(thread) {}
  40. StunRequestManager::~StunRequestManager() {
  41. while (requests_.begin() != requests_.end()) {
  42. StunRequest* request = requests_.begin()->second;
  43. requests_.erase(requests_.begin());
  44. delete request;
  45. }
  46. }
  47. void StunRequestManager::Send(StunRequest* request) {
  48. SendDelayed(request, 0);
  49. }
  50. void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
  51. request->set_manager(this);
  52. RTC_DCHECK(requests_.find(request->id()) == requests_.end());
  53. request->set_origin(origin_);
  54. request->Construct();
  55. requests_[request->id()] = request;
  56. if (delay > 0) {
  57. thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL);
  58. } else {
  59. thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
  60. }
  61. }
  62. void StunRequestManager::Flush(int msg_type) {
  63. for (const auto& kv : requests_) {
  64. StunRequest* request = kv.second;
  65. if (msg_type == kAllRequests || msg_type == request->type()) {
  66. thread_->Clear(request, MSG_STUN_SEND);
  67. thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
  68. }
  69. }
  70. }
  71. bool StunRequestManager::HasRequest(int msg_type) {
  72. for (const auto& kv : requests_) {
  73. StunRequest* request = kv.second;
  74. if (msg_type == kAllRequests || msg_type == request->type()) {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. void StunRequestManager::Remove(StunRequest* request) {
  81. RTC_DCHECK(request->manager() == this);
  82. RequestMap::iterator iter = requests_.find(request->id());
  83. if (iter != requests_.end()) {
  84. RTC_DCHECK(iter->second == request);
  85. requests_.erase(iter);
  86. thread_->Clear(request);
  87. }
  88. }
  89. void StunRequestManager::Clear() {
  90. std::vector<StunRequest*> requests;
  91. for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i)
  92. requests.push_back(i->second);
  93. for (uint32_t i = 0; i < requests.size(); ++i) {
  94. // StunRequest destructor calls Remove() which deletes requests
  95. // from |requests_|.
  96. delete requests[i];
  97. }
  98. }
  99. bool StunRequestManager::CheckResponse(StunMessage* msg) {
  100. RequestMap::iterator iter = requests_.find(msg->transaction_id());
  101. if (iter == requests_.end()) {
  102. // TODO(pthatcher): Log unknown responses without being too spammy
  103. // in the logs.
  104. return false;
  105. }
  106. StunRequest* request = iter->second;
  107. if (msg->type() == GetStunSuccessResponseType(request->type())) {
  108. request->OnResponse(msg);
  109. } else if (msg->type() == GetStunErrorResponseType(request->type())) {
  110. request->OnErrorResponse(msg);
  111. } else {
  112. RTC_LOG(LERROR) << "Received response with wrong type: " << msg->type()
  113. << " (expecting "
  114. << GetStunSuccessResponseType(request->type()) << ")";
  115. return false;
  116. }
  117. delete request;
  118. return true;
  119. }
  120. bool StunRequestManager::CheckResponse(const char* data, size_t size) {
  121. // Check the appropriate bytes of the stream to see if they match the
  122. // transaction ID of a response we are expecting.
  123. if (size < 20)
  124. return false;
  125. std::string id;
  126. id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
  127. RequestMap::iterator iter = requests_.find(id);
  128. if (iter == requests_.end()) {
  129. // TODO(pthatcher): Log unknown responses without being too spammy
  130. // in the logs.
  131. return false;
  132. }
  133. // Parse the STUN message and continue processing as usual.
  134. rtc::ByteBufferReader buf(data, size);
  135. std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
  136. if (!response->Read(&buf)) {
  137. RTC_LOG(LS_WARNING) << "Failed to read STUN response "
  138. << rtc::hex_encode(id);
  139. return false;
  140. }
  141. return CheckResponse(response.get());
  142. }
  143. StunRequest::StunRequest()
  144. : count_(0),
  145. timeout_(false),
  146. manager_(0),
  147. msg_(new StunMessage()),
  148. tstamp_(0),
  149. in_rfc5389_retransmission_experiment_(
  150. webrtc::field_trial::IsEnabled(kRfc5389StunRetransmissions)) {
  151. msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
  152. }
  153. StunRequest::StunRequest(StunMessage* request)
  154. : count_(0),
  155. timeout_(false),
  156. manager_(0),
  157. msg_(request),
  158. tstamp_(0),
  159. in_rfc5389_retransmission_experiment_(
  160. webrtc::field_trial::IsEnabled(kRfc5389StunRetransmissions)) {
  161. msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
  162. }
  163. StunRequest::~StunRequest() {
  164. RTC_DCHECK(manager_ != NULL);
  165. if (manager_) {
  166. manager_->Remove(this);
  167. manager_->thread_->Clear(this);
  168. }
  169. delete msg_;
  170. }
  171. void StunRequest::Construct() {
  172. if (msg_->type() == 0) {
  173. if (!origin_.empty()) {
  174. msg_->AddAttribute(
  175. std::make_unique<StunByteStringAttribute>(STUN_ATTR_ORIGIN, origin_));
  176. }
  177. Prepare(msg_);
  178. RTC_DCHECK(msg_->type() != 0);
  179. }
  180. }
  181. int StunRequest::type() {
  182. RTC_DCHECK(msg_ != NULL);
  183. return msg_->type();
  184. }
  185. const StunMessage* StunRequest::msg() const {
  186. return msg_;
  187. }
  188. StunMessage* StunRequest::mutable_msg() {
  189. return msg_;
  190. }
  191. int StunRequest::Elapsed() const {
  192. return static_cast<int>(rtc::TimeMillis() - tstamp_);
  193. }
  194. void StunRequest::set_manager(StunRequestManager* manager) {
  195. RTC_DCHECK(!manager_);
  196. manager_ = manager;
  197. }
  198. void StunRequest::OnMessage(rtc::Message* pmsg) {
  199. RTC_DCHECK(manager_ != NULL);
  200. RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
  201. if (timeout_) {
  202. OnTimeout();
  203. delete this;
  204. return;
  205. }
  206. tstamp_ = rtc::TimeMillis();
  207. rtc::ByteBufferWriter buf;
  208. msg_->Write(&buf);
  209. manager_->SignalSendPacket(buf.Data(), buf.Length(), this);
  210. OnSent();
  211. manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this,
  212. MSG_STUN_SEND, NULL);
  213. }
  214. void StunRequest::OnSent() {
  215. count_ += 1;
  216. int retransmissions = (count_ - 1);
  217. if (retransmissions >= STUN_MAX_RETRANSMISSIONS ||
  218. (in_rfc5389_retransmission_experiment_ &&
  219. retransmissions >= STUN_MAX_RETRANSMISSIONS_RFC_5389)) {
  220. timeout_ = true;
  221. }
  222. RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_
  223. << "; resend delay = " << resend_delay();
  224. }
  225. int StunRequest::resend_delay() {
  226. if (count_ == 0) {
  227. return 0;
  228. }
  229. int retransmissions = (count_ - 1);
  230. int rto = STUN_INITIAL_RTO << retransmissions;
  231. return std::min(rto, STUN_MAX_RTO);
  232. }
  233. } // namespace cricket