blob: 2870dcdfc528da0405d4ce94ecba846a9dfeb049 [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;
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000123
124 // Now that we know the request, we can see if the response is
125 // integrity-protected or not.
126 // For some tests, the message integrity is not set in the request.
127 // Complain, and then don't check.
128 bool skip_integrity_checking = false;
129 if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) {
130 skip_integrity_checking = true;
131 } else {
132 msg->ValidateMessageIntegrity(request->msg()->password());
133 }
134
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700135 if (!msg->GetNonComprehendedAttributes().empty()) {
136 // If a response contains unknown comprehension-required attributes, it's
137 // simply discarded and the transaction is considered failed. See RFC5389
138 // sections 7.3.3 and 7.3.4.
139 RTC_LOG(LS_ERROR) << ": Discarding response due to unknown "
140 "comprehension-required attribute.";
141 delete request;
142 return false;
143 } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000144 if (!msg->IntegrityOk() && !skip_integrity_checking) {
145 return false;
146 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 request->OnResponse(msg);
148 } else if (msg->type() == GetStunErrorResponseType(request->type())) {
149 request->OnErrorResponse(msg);
150 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(LERROR) << "Received response with wrong type: " << msg->type()
152 << " (expecting "
153 << GetStunSuccessResponseType(request->type()) << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000154 return false;
155 }
156
157 delete request;
158 return true;
159}
160
161bool StunRequestManager::CheckResponse(const char* data, size_t size) {
162 // Check the appropriate bytes of the stream to see if they match the
163 // transaction ID of a response we are expecting.
164
165 if (size < 20)
166 return false;
167
168 std::string id;
169 id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
170
171 RequestMap::iterator iter = requests_.find(id);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700172 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700173 // TODO(pthatcher): Log unknown responses without being too spammy
174 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700176 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177
178 // Parse the STUN message and continue processing as usual.
179
jbauchf1f87202016-03-30 06:43:37 -0700180 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700181 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700182 if (!response->Read(&buf)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100183 RTC_LOG(LS_WARNING) << "Failed to read STUN response "
184 << rtc::hex_encode(id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700186 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187
188 return CheckResponse(response.get());
189}
190
191StunRequest::StunRequest()
Tomas Gunnarsson77baeee2020-09-24 22:39:21 +0200192 : count_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200193 timeout_(false),
194 manager_(0),
195 msg_(new StunMessage()),
Mirko Bonadeif3879462020-04-14 13:44:09 +0200196 tstamp_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200197 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198}
199
200StunRequest::StunRequest(StunMessage* request)
Tomas Gunnarsson77baeee2020-09-24 22:39:21 +0200201 : count_(0), timeout_(false), manager_(0), msg_(request), tstamp_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200202 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203}
204
205StunRequest::~StunRequest() {
nisseede5da42017-01-12 05:15:36 -0800206 RTC_DCHECK(manager_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 if (manager_) {
208 manager_->Remove(this);
209 manager_->thread_->Clear(this);
210 }
211 delete msg_;
212}
213
214void StunRequest::Construct() {
215 if (msg_->type() == 0) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000216 if (!origin_.empty()) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200217 msg_->AddAttribute(
218 std::make_unique<StunByteStringAttribute>(STUN_ATTR_ORIGIN, origin_));
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000219 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 Prepare(msg_);
nisseede5da42017-01-12 05:15:36 -0800221 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 }
223}
224
225int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800226 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227 return msg_->type();
228}
229
230const StunMessage* StunRequest::msg() const {
231 return msg_;
232}
233
Jonas Orelandbdcee282017-10-10 14:01:40 +0200234StunMessage* StunRequest::mutable_msg() {
235 return msg_;
236}
237
honghaiz34b11eb2016-03-16 08:55:44 -0700238int StunRequest::Elapsed() const {
nisse1bffc1d2016-05-02 08:18:55 -0700239 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000240}
241
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000242void StunRequest::set_manager(StunRequestManager* manager) {
nisseede5da42017-01-12 05:15:36 -0800243 RTC_DCHECK(!manager_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000244 manager_ = manager;
245}
246
247void StunRequest::OnMessage(rtc::Message* pmsg) {
nisseede5da42017-01-12 05:15:36 -0800248 RTC_DCHECK(manager_ != NULL);
249 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000250
251 if (timeout_) {
252 OnTimeout();
253 delete this;
254 return;
255 }
256
nisse1bffc1d2016-05-02 08:18:55 -0700257 tstamp_ = rtc::TimeMillis();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000258
jbauchf1f87202016-03-30 06:43:37 -0700259 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000260 msg_->Write(&buf);
261 manager_->SignalSendPacket(buf.Data(), buf.Length(), this);
262
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700263 OnSent();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700264 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this,
265 MSG_STUN_SEND, NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000266}
267
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700268void StunRequest::OnSent() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000269 count_ += 1;
pthatcher94a2f212017-02-08 14:42:22 -0800270 int retransmissions = (count_ - 1);
Mirko Bonadeif3879462020-04-14 13:44:09 +0200271 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700273 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100274 RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_
275 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700276}
277
278int StunRequest::resend_delay() {
279 if (count_ == 0) {
280 return 0;
281 }
pthatcher94a2f212017-02-08 14:42:22 -0800282 int retransmissions = (count_ - 1);
283 int rto = STUN_INITIAL_RTO << retransmissions;
284 return std::min(rto, STUN_MAX_RTO);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000285}
286
287} // namespace cricket