blob: 44376ced950f4e07b7ceefcb30445c1cf1725e73 [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()
Yves Gerey665174f2018-06-19 15:03:05 +0200177 : count_(0),
178 timeout_(false),
179 manager_(0),
180 msg_(new StunMessage()),
Mirko Bonadeif3879462020-04-14 13:44:09 +0200181 tstamp_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200182 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183}
184
185StunRequest::StunRequest(StunMessage* request)
Mirko Bonadeif3879462020-04-14 13:44:09 +0200186 : count_(0), timeout_(false), manager_(0), msg_(request), tstamp_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200187 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188}
189
190StunRequest::~StunRequest() {
nisseede5da42017-01-12 05:15:36 -0800191 RTC_DCHECK(manager_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 if (manager_) {
193 manager_->Remove(this);
194 manager_->thread_->Clear(this);
195 }
196 delete msg_;
197}
198
199void StunRequest::Construct() {
200 if (msg_->type() == 0) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000201 if (!origin_.empty()) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200202 msg_->AddAttribute(
203 std::make_unique<StunByteStringAttribute>(STUN_ATTR_ORIGIN, origin_));
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000204 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 Prepare(msg_);
nisseede5da42017-01-12 05:15:36 -0800206 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 }
208}
209
210int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800211 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 return msg_->type();
213}
214
215const StunMessage* StunRequest::msg() const {
216 return msg_;
217}
218
Jonas Orelandbdcee282017-10-10 14:01:40 +0200219StunMessage* StunRequest::mutable_msg() {
220 return msg_;
221}
222
honghaiz34b11eb2016-03-16 08:55:44 -0700223int StunRequest::Elapsed() const {
nisse1bffc1d2016-05-02 08:18:55 -0700224 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000225}
226
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227void StunRequest::set_manager(StunRequestManager* manager) {
nisseede5da42017-01-12 05:15:36 -0800228 RTC_DCHECK(!manager_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 manager_ = manager;
230}
231
232void StunRequest::OnMessage(rtc::Message* pmsg) {
nisseede5da42017-01-12 05:15:36 -0800233 RTC_DCHECK(manager_ != NULL);
234 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235
236 if (timeout_) {
237 OnTimeout();
238 delete this;
239 return;
240 }
241
nisse1bffc1d2016-05-02 08:18:55 -0700242 tstamp_ = rtc::TimeMillis();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243
jbauchf1f87202016-03-30 06:43:37 -0700244 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 msg_->Write(&buf);
246 manager_->SignalSendPacket(buf.Data(), buf.Length(), this);
247
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700248 OnSent();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700249 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this,
250 MSG_STUN_SEND, NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251}
252
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700253void StunRequest::OnSent() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 count_ += 1;
pthatcher94a2f212017-02-08 14:42:22 -0800255 int retransmissions = (count_ - 1);
Mirko Bonadeif3879462020-04-14 13:44:09 +0200256 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700258 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100259 RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_
260 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700261}
262
263int StunRequest::resend_delay() {
264 if (count_ == 0) {
265 return 0;
266 }
pthatcher94a2f212017-02-08 14:42:22 -0800267 int retransmissions = (count_ - 1);
268 int rto = STUN_INITIAL_RTO << retransmissions;
269 return std::min(rto, STUN_MAX_RTO);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000270}
271
272} // namespace cricket