blob: 532e5821a521f6a5aaee641aec106ff0582b513e [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>
Tommi86aa03e2022-04-12 09:17:57 +020015#include <utility>
Steve Anton6c38cc72017-11-29 10:25:58 -080016#include <vector>
kwiberg3ec46792016-04-27 07:22:53 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/helpers.h"
20#include "rtc_base/logging.h"
Steve Antonf4172382020-01-27 15:45:02 -080021#include "rtc_base/string_encode.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h" // For TimeMillis
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) {
Tommi86aa03e2022-04-12 09:17:57 +020060 RTC_DCHECK_RUN_ON(thread_);
61 RTC_DCHECK_EQ(this, request->manager());
nisseede5da42017-01-12 05:15:36 -080062 RTC_DCHECK(requests_.find(request->id()) == requests_.end());
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
Tommi86aa03e2022-04-12 09:17:57 +020072void StunRequestManager::FlushForTest(int msg_type) {
73 RTC_DCHECK_RUN_ON(thread_);
Mirko Bonadei739baf02019-01-27 17:29:42 +010074 for (const auto& kv : requests_) {
Honghai Zhang85975432015-11-12 11:07:12 -080075 StunRequest* request = kv.second;
honghaiz6b9ab922016-01-05 09:06:12 -080076 if (msg_type == kAllRequests || msg_type == request->type()) {
77 thread_->Clear(request, MSG_STUN_SEND);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070078 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
honghaiz6b9ab922016-01-05 09:06:12 -080079 }
Honghai Zhang85975432015-11-12 11:07:12 -080080 }
81}
82
Tommi86aa03e2022-04-12 09:17:57 +020083bool StunRequestManager::HasRequestForTest(int msg_type) {
84 RTC_DCHECK_RUN_ON(thread_);
Mirko Bonadei739baf02019-01-27 17:29:42 +010085 for (const auto& kv : requests_) {
honghaize2af9ef2016-03-03 08:27:47 -080086 StunRequest* request = kv.second;
87 if (msg_type == kAllRequests || msg_type == request->type()) {
88 return true;
89 }
90 }
91 return false;
92}
93
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094void StunRequestManager::Remove(StunRequest* request) {
Tommi86aa03e2022-04-12 09:17:57 +020095 RTC_DCHECK_RUN_ON(thread_);
nisseede5da42017-01-12 05:15:36 -080096 RTC_DCHECK(request->manager() == this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097 RequestMap::iterator iter = requests_.find(request->id());
98 if (iter != requests_.end()) {
nisseede5da42017-01-12 05:15:36 -080099 RTC_DCHECK(iter->second == request);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100 requests_.erase(iter);
101 thread_->Clear(request);
102 }
103}
104
105void StunRequestManager::Clear() {
Tommi86aa03e2022-04-12 09:17:57 +0200106 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 std::vector<StunRequest*> requests;
108 for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i)
109 requests.push_back(i->second);
110
Peter Boström0c4e06b2015-10-07 12:23:21 +0200111 for (uint32_t i = 0; i < requests.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 // StunRequest destructor calls Remove() which deletes requests
Artem Titov2dbb4c92021-07-26 15:12:41 +0200113 // from `requests_`.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 delete requests[i];
115 }
116}
117
118bool StunRequestManager::CheckResponse(StunMessage* msg) {
Tommi86aa03e2022-04-12 09:17:57 +0200119 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 RequestMap::iterator iter = requests_.find(msg->transaction_id());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700121 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700122 // TODO(pthatcher): Log unknown responses without being too spammy
123 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700125 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126
127 StunRequest* request = iter->second;
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000128
129 // Now that we know the request, we can see if the response is
130 // integrity-protected or not.
131 // For some tests, the message integrity is not set in the request.
132 // Complain, and then don't check.
133 bool skip_integrity_checking = false;
134 if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) {
135 skip_integrity_checking = true;
136 } else {
137 msg->ValidateMessageIntegrity(request->msg()->password());
138 }
139
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700140 if (!msg->GetNonComprehendedAttributes().empty()) {
141 // If a response contains unknown comprehension-required attributes, it's
142 // simply discarded and the transaction is considered failed. See RFC5389
143 // sections 7.3.3 and 7.3.4.
144 RTC_LOG(LS_ERROR) << ": Discarding response due to unknown "
145 "comprehension-required attribute.";
146 delete request;
147 return false;
148 } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000149 if (!msg->IntegrityOk() && !skip_integrity_checking) {
150 return false;
151 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 request->OnResponse(msg);
153 } else if (msg->type() == GetStunErrorResponseType(request->type())) {
154 request->OnErrorResponse(msg);
155 } else {
Harald Alvestrand5f341302021-11-24 10:01:32 +0000156 RTC_LOG(LS_ERROR) << "Received response with wrong type: " << msg->type()
157 << " (expecting "
158 << GetStunSuccessResponseType(request->type()) << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159 return false;
160 }
161
162 delete request;
163 return true;
164}
165
Tommi86aa03e2022-04-12 09:17:57 +0200166bool StunRequestManager::empty() const {
167 RTC_DCHECK_RUN_ON(thread_);
168 return requests_.empty();
169}
170
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171bool StunRequestManager::CheckResponse(const char* data, size_t size) {
Tommi86aa03e2022-04-12 09:17:57 +0200172 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 // Check the appropriate bytes of the stream to see if they match the
174 // transaction ID of a response we are expecting.
175
176 if (size < 20)
177 return false;
178
179 std::string id;
180 id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
181
182 RequestMap::iterator iter = requests_.find(id);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700183 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700184 // TODO(pthatcher): Log unknown responses without being too spammy
185 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700187 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188
189 // Parse the STUN message and continue processing as usual.
190
jbauchf1f87202016-03-30 06:43:37 -0700191 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700192 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700193 if (!response->Read(&buf)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100194 RTC_LOG(LS_WARNING) << "Failed to read STUN response "
195 << rtc::hex_encode(id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700197 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198
199 return CheckResponse(response.get());
200}
201
Tommi86aa03e2022-04-12 09:17:57 +0200202StunRequest::StunRequest(StunRequestManager& manager)
203 : manager_(manager),
Yves Gerey665174f2018-06-19 15:03:05 +0200204 msg_(new StunMessage()),
Tommi86aa03e2022-04-12 09:17:57 +0200205 tstamp_(0),
206 count_(0),
207 timeout_(false) {
Yves Gerey665174f2018-06-19 15:03:05 +0200208 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209}
210
Tommi86aa03e2022-04-12 09:17:57 +0200211StunRequest::StunRequest(StunRequestManager& manager,
212 std::unique_ptr<StunMessage> request)
213 : manager_(manager),
214 msg_(std::move(request)),
215 tstamp_(0),
216 count_(0),
217 timeout_(false) {
Yves Gerey665174f2018-06-19 15:03:05 +0200218 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219}
220
221StunRequest::~StunRequest() {
Tommi86aa03e2022-04-12 09:17:57 +0200222 manager_.Remove(this);
223 manager_.network_thread()->Clear(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224}
225
226void StunRequest::Construct() {
227 if (msg_->type() == 0) {
Tommi86aa03e2022-04-12 09:17:57 +0200228 Prepare(msg_.get());
nisseede5da42017-01-12 05:15:36 -0800229 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000230 }
231}
232
233int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800234 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 return msg_->type();
236}
237
238const StunMessage* StunRequest::msg() const {
Tommi86aa03e2022-04-12 09:17:57 +0200239 return msg_.get();
Jonas Orelandbdcee282017-10-10 14:01:40 +0200240}
241
honghaiz34b11eb2016-03-16 08:55:44 -0700242int StunRequest::Elapsed() const {
Tommi86aa03e2022-04-12 09:17:57 +0200243 RTC_DCHECK_RUN_ON(network_thread());
nisse1bffc1d2016-05-02 08:18:55 -0700244 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245}
246
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247void StunRequest::OnMessage(rtc::Message* pmsg) {
Tommi86aa03e2022-04-12 09:17:57 +0200248 RTC_DCHECK_RUN_ON(network_thread());
nisseede5da42017-01-12 05:15:36 -0800249 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);
Tommi86aa03e2022-04-12 09:17:57 +0200261 manager_.SignalSendPacket(buf.Data(), buf.Length(), this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700263 OnSent();
Tommi86aa03e2022-04-12 09:17:57 +0200264 manager_.network_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() {
Tommi86aa03e2022-04-12 09:17:57 +0200269 RTC_DCHECK_RUN_ON(network_thread());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000270 count_ += 1;
pthatcher94a2f212017-02-08 14:42:22 -0800271 int retransmissions = (count_ - 1);
Mirko Bonadeif3879462020-04-14 13:44:09 +0200272 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000273 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700274 }
Tommi86aa03e2022-04-12 09:17:57 +0200275 RTC_DLOG(LS_VERBOSE) << "Sent STUN request " << count_
276 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700277}
278
279int StunRequest::resend_delay() {
Tommi86aa03e2022-04-12 09:17:57 +0200280 RTC_DCHECK_RUN_ON(network_thread());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700281 if (count_ == 0) {
282 return 0;
283 }
pthatcher94a2f212017-02-08 14:42:22 -0800284 int retransmissions = (count_ - 1);
285 int rto = STUN_INITIAL_RTO << retransmissions;
286 return std::min(rto, STUN_MAX_RTO);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000287}
288
Tommi86aa03e2022-04-12 09:17:57 +0200289void StunRequest::set_timed_out() {
290 RTC_DCHECK_RUN_ON(network_thread());
291 timeout_ = true;
292}
293
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000294} // namespace cricket