blob: d6210fc2dc59178c7f85701d4f7bdc6e8157b0f7 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "p2p/base/stun_request.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000013#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070014#include <memory>
Steve Anton6c38cc72017-11-29 10:25:58 -080015#include <vector>
kwiberg3ec46792016-04-27 07:22:53 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/helpers.h"
19#include "rtc_base/logging.h"
Steve Antonf4172382020-01-27 15:45:02 -080020#include "rtc_base/string_encode.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/time_utils.h" // For TimeMillis
Bjorn Terelius0d289722019-02-01 15:22:20 +010022#include "system_wrappers/include/field_trial.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000023
24namespace cricket {
25
Peter Boström0c4e06b2015-10-07 12:23:21 +020026const uint32_t MSG_STUN_SEND = 1;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027
pthatcher94a2f212017-02-08 14:42:22 -080028// RFC 5389 says SHOULD be 500ms.
29// For years, this was 100ms, but for networks that
30// experience moments of high RTT (such as 2G networks), this doesn't
31// work well.
32const int STUN_INITIAL_RTO = 250; // milliseconds
33
34// The timeout doubles each retransmission, up to this many times
35// RFC 5389 says SHOULD retransmit 7 times.
36// This has been 8 for years (not sure why).
Jonas Olssona4d87372019-07-05 19:08:33 +020037const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9
pthatcher94a2f212017-02-08 14:42:22 -080038
39// We also cap the doubling, even though the standard doesn't say to.
40// This has been 1.6 seconds for years, but for networks that
41// experience moments of high RTT (such as 2G networks), this doesn't
42// work well.
43const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044
Yves Gerey665174f2018-06-19 15:03:05 +020045StunRequestManager::StunRequestManager(rtc::Thread* thread) : thread_(thread) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046
47StunRequestManager::~StunRequestManager() {
48 while (requests_.begin() != requests_.end()) {
Yves Gerey665174f2018-06-19 15:03:05 +020049 StunRequest* request = requests_.begin()->second;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 requests_.erase(requests_.begin());
51 delete request;
52 }
53}
54
55void StunRequestManager::Send(StunRequest* request) {
56 SendDelayed(request, 0);
57}
58
59void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
60 request->set_manager(this);
nisseede5da42017-01-12 05:15:36 -080061 RTC_DCHECK(requests_.find(request->id()) == requests_.end());
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000062 request->set_origin(origin_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063 request->Construct();
64 requests_[request->id()] = request;
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000065 if (delay > 0) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070066 thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000067 } else {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070068 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000069 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070}
71
honghaiz6b9ab922016-01-05 09:06:12 -080072void StunRequestManager::Flush(int msg_type) {
Mirko Bonadei739baf02019-01-27 17:29:42 +010073 for (const auto& kv : requests_) {
Honghai Zhang85975432015-11-12 11:07:12 -080074 StunRequest* request = kv.second;
honghaiz6b9ab922016-01-05 09:06:12 -080075 if (msg_type == kAllRequests || msg_type == request->type()) {
76 thread_->Clear(request, MSG_STUN_SEND);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070077 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
honghaiz6b9ab922016-01-05 09:06:12 -080078 }
Honghai Zhang85975432015-11-12 11:07:12 -080079 }
80}
81
honghaize2af9ef2016-03-03 08:27:47 -080082bool StunRequestManager::HasRequest(int msg_type) {
Mirko Bonadei739baf02019-01-27 17:29:42 +010083 for (const auto& kv : requests_) {
honghaize2af9ef2016-03-03 08:27:47 -080084 StunRequest* request = kv.second;
85 if (msg_type == kAllRequests || msg_type == request->type()) {
86 return true;
87 }
88 }
89 return false;
90}
91
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092void StunRequestManager::Remove(StunRequest* request) {
nisseede5da42017-01-12 05:15:36 -080093 RTC_DCHECK(request->manager() == this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094 RequestMap::iterator iter = requests_.find(request->id());
95 if (iter != requests_.end()) {
nisseede5da42017-01-12 05:15:36 -080096 RTC_DCHECK(iter->second == request);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097 requests_.erase(iter);
98 thread_->Clear(request);
99 }
100}
101
102void StunRequestManager::Clear() {
103 std::vector<StunRequest*> requests;
104 for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i)
105 requests.push_back(i->second);
106
Peter Boström0c4e06b2015-10-07 12:23:21 +0200107 for (uint32_t i = 0; i < requests.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 // StunRequest destructor calls Remove() which deletes requests
109 // from |requests_|.
110 delete requests[i];
111 }
112}
113
114bool StunRequestManager::CheckResponse(StunMessage* msg) {
115 RequestMap::iterator iter = requests_.find(msg->transaction_id());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700116 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700117 // TODO(pthatcher): Log unknown responses without being too spammy
118 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700120 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000121
122 StunRequest* request = iter->second;
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700123 if (!msg->GetNonComprehendedAttributes().empty()) {
124 // If a response contains unknown comprehension-required attributes, it's
125 // simply discarded and the transaction is considered failed. See RFC5389
126 // sections 7.3.3 and 7.3.4.
127 RTC_LOG(LS_ERROR) << ": Discarding response due to unknown "
128 "comprehension-required attribute.";
129 delete request;
130 return false;
131 } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132 request->OnResponse(msg);
133 } else if (msg->type() == GetStunErrorResponseType(request->type())) {
134 request->OnErrorResponse(msg);
135 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100136 RTC_LOG(LERROR) << "Received response with wrong type: " << msg->type()
137 << " (expecting "
138 << GetStunSuccessResponseType(request->type()) << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 return false;
140 }
141
142 delete request;
143 return true;
144}
145
146bool StunRequestManager::CheckResponse(const char* data, size_t size) {
147 // Check the appropriate bytes of the stream to see if they match the
148 // transaction ID of a response we are expecting.
149
150 if (size < 20)
151 return false;
152
153 std::string id;
154 id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
155
156 RequestMap::iterator iter = requests_.find(id);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700157 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700158 // TODO(pthatcher): Log unknown responses without being too spammy
159 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700161 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000162
163 // Parse the STUN message and continue processing as usual.
164
jbauchf1f87202016-03-30 06:43:37 -0700165 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700166 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700167 if (!response->Read(&buf)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100168 RTC_LOG(LS_WARNING) << "Failed to read STUN response "
169 << rtc::hex_encode(id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700171 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172
173 return CheckResponse(response.get());
174}
175
176StunRequest::StunRequest()
Tomas Gunnarsson4d76a132020-09-12 15:41:52 +0200177 : rtc::MessageHandler(false),
178 count_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200179 timeout_(false),
180 manager_(0),
181 msg_(new StunMessage()),
Mirko Bonadeif3879462020-04-14 13:44:09 +0200182 tstamp_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200183 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184}
185
186StunRequest::StunRequest(StunMessage* request)
Tomas Gunnarsson4d76a132020-09-12 15:41:52 +0200187 : rtc::MessageHandler(false),
188 count_(0),
189 timeout_(false),
190 manager_(0),
191 msg_(request),
192 tstamp_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200193 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194}
195
196StunRequest::~StunRequest() {
nisseede5da42017-01-12 05:15:36 -0800197 RTC_DCHECK(manager_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 if (manager_) {
199 manager_->Remove(this);
200 manager_->thread_->Clear(this);
201 }
202 delete msg_;
203}
204
205void StunRequest::Construct() {
206 if (msg_->type() == 0) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000207 if (!origin_.empty()) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200208 msg_->AddAttribute(
209 std::make_unique<StunByteStringAttribute>(STUN_ATTR_ORIGIN, origin_));
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000210 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211 Prepare(msg_);
nisseede5da42017-01-12 05:15:36 -0800212 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 }
214}
215
216int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800217 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218 return msg_->type();
219}
220
221const StunMessage* StunRequest::msg() const {
222 return msg_;
223}
224
Jonas Orelandbdcee282017-10-10 14:01:40 +0200225StunMessage* StunRequest::mutable_msg() {
226 return msg_;
227}
228
honghaiz34b11eb2016-03-16 08:55:44 -0700229int StunRequest::Elapsed() const {
nisse1bffc1d2016-05-02 08:18:55 -0700230 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231}
232
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233void StunRequest::set_manager(StunRequestManager* manager) {
nisseede5da42017-01-12 05:15:36 -0800234 RTC_DCHECK(!manager_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 manager_ = manager;
236}
237
238void StunRequest::OnMessage(rtc::Message* pmsg) {
nisseede5da42017-01-12 05:15:36 -0800239 RTC_DCHECK(manager_ != NULL);
240 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241
242 if (timeout_) {
243 OnTimeout();
244 delete this;
245 return;
246 }
247
nisse1bffc1d2016-05-02 08:18:55 -0700248 tstamp_ = rtc::TimeMillis();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249
jbauchf1f87202016-03-30 06:43:37 -0700250 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 msg_->Write(&buf);
252 manager_->SignalSendPacket(buf.Data(), buf.Length(), this);
253
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700254 OnSent();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700255 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this,
256 MSG_STUN_SEND, NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257}
258
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700259void StunRequest::OnSent() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000260 count_ += 1;
pthatcher94a2f212017-02-08 14:42:22 -0800261 int retransmissions = (count_ - 1);
Mirko Bonadeif3879462020-04-14 13:44:09 +0200262 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000263 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700264 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100265 RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_
266 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700267}
268
269int StunRequest::resend_delay() {
270 if (count_ == 0) {
271 return 0;
272 }
pthatcher94a2f212017-02-08 14:42:22 -0800273 int retransmissions = (count_ - 1);
274 int rto = STUN_INITIAL_RTO << retransmissions;
275 return std::min(rto, STUN_MAX_RTO);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000276}
277
278} // namespace cricket