blob: 79cd61841e3864c33c5ac00bd94e6d8ad9d76d69 [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
Tommi25452572022-04-12 12:51:40 +020018#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/helpers.h"
21#include "rtc_base/logging.h"
Steve Antonf4172382020-01-27 15:45:02 -080022#include "rtc_base/string_encode.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/time_utils.h" // For TimeMillis
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
25namespace cricket {
26
Peter Boström0c4e06b2015-10-07 12:23:21 +020027const uint32_t MSG_STUN_SEND = 1;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028
pthatcher94a2f212017-02-08 14:42:22 -080029// RFC 5389 says SHOULD be 500ms.
30// For years, this was 100ms, but for networks that
31// experience moments of high RTT (such as 2G networks), this doesn't
32// work well.
33const int STUN_INITIAL_RTO = 250; // milliseconds
34
35// The timeout doubles each retransmission, up to this many times
36// RFC 5389 says SHOULD retransmit 7 times.
37// This has been 8 for years (not sure why).
Jonas Olssona4d87372019-07-05 19:08:33 +020038const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9
pthatcher94a2f212017-02-08 14:42:22 -080039
40// We also cap the doubling, even though the standard doesn't say to.
41// This has been 1.6 seconds for years, but for networks that
42// experience moments of high RTT (such as 2G networks), this doesn't
43// work well.
44const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000046StunRequestManager::StunRequestManager(
47 rtc::Thread* thread,
48 std::function<void(const void*, size_t, StunRequest*)> send_packet)
49 : thread_(thread), send_packet_(std::move(send_packet)) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050
Tommi25452572022-04-12 12:51:40 +020051StunRequestManager::~StunRequestManager() = default;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052
53void StunRequestManager::Send(StunRequest* request) {
54 SendDelayed(request, 0);
55}
56
57void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
Tommi86aa03e2022-04-12 09:17:57 +020058 RTC_DCHECK_RUN_ON(thread_);
59 RTC_DCHECK_EQ(this, request->manager());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060 request->Construct();
Tommi25452572022-04-12 12:51:40 +020061 auto [iter, was_inserted] =
62 requests_.emplace(request->id(), absl::WrapUnique(request));
63 RTC_DCHECK(was_inserted);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000064 if (delay > 0) {
Tommi25452572022-04-12 12:51:40 +020065 thread_->PostDelayed(RTC_FROM_HERE, delay, iter->second.get(),
66 MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000067 } else {
Tommi25452572022-04-12 12:51:40 +020068 thread_->Send(RTC_FROM_HERE, iter->second.get(), 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_);
Tommi25452572022-04-12 12:51:40 +020074 for (const auto& [unused, request] : requests_) {
honghaiz6b9ab922016-01-05 09:06:12 -080075 if (msg_type == kAllRequests || msg_type == request->type()) {
Tommi25452572022-04-12 12:51:40 +020076 thread_->Clear(request.get(), MSG_STUN_SEND);
77 thread_->Send(RTC_FROM_HERE, request.get(), MSG_STUN_SEND, NULL);
honghaiz6b9ab922016-01-05 09:06:12 -080078 }
Honghai Zhang85975432015-11-12 11:07:12 -080079 }
80}
81
Tommi86aa03e2022-04-12 09:17:57 +020082bool StunRequestManager::HasRequestForTest(int msg_type) {
83 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020084 for (const auto& [unused, request] : requests_) {
honghaize2af9ef2016-03-03 08:27:47 -080085 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::Clear() {
Tommi86aa03e2022-04-12 09:17:57 +020093 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020094 requests_.clear();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095}
96
97bool StunRequestManager::CheckResponse(StunMessage* msg) {
Tommi86aa03e2022-04-12 09:17:57 +020098 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099 RequestMap::iterator iter = requests_.find(msg->transaction_id());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700100 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700101 // TODO(pthatcher): Log unknown responses without being too spammy
102 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700104 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105
Tommi25452572022-04-12 12:51:40 +0200106 StunRequest* request = iter->second.get();
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000107
108 // Now that we know the request, we can see if the response is
109 // integrity-protected or not.
110 // For some tests, the message integrity is not set in the request.
111 // Complain, and then don't check.
112 bool skip_integrity_checking = false;
113 if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) {
114 skip_integrity_checking = true;
115 } else {
116 msg->ValidateMessageIntegrity(request->msg()->password());
117 }
118
Tommi25452572022-04-12 12:51:40 +0200119 bool success = true;
120
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700121 if (!msg->GetNonComprehendedAttributes().empty()) {
122 // If a response contains unknown comprehension-required attributes, it's
123 // simply discarded and the transaction is considered failed. See RFC5389
124 // sections 7.3.3 and 7.3.4.
125 RTC_LOG(LS_ERROR) << ": Discarding response due to unknown "
126 "comprehension-required attribute.";
Tommi25452572022-04-12 12:51:40 +0200127 success = false;
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700128 } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000129 if (!msg->IntegrityOk() && !skip_integrity_checking) {
130 return false;
131 }
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 {
Harald Alvestrand5f341302021-11-24 10:01:32 +0000136 RTC_LOG(LS_ERROR) << "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
Tommi25452572022-04-12 12:51:40 +0200142 requests_.erase(iter);
143 return success;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144}
145
Tommi86aa03e2022-04-12 09:17:57 +0200146bool StunRequestManager::empty() const {
147 RTC_DCHECK_RUN_ON(thread_);
148 return requests_.empty();
149}
150
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151bool StunRequestManager::CheckResponse(const char* data, size_t size) {
Tommi86aa03e2022-04-12 09:17:57 +0200152 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153 // Check the appropriate bytes of the stream to see if they match the
154 // transaction ID of a response we are expecting.
155
156 if (size < 20)
157 return false;
158
159 std::string id;
160 id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
161
162 RequestMap::iterator iter = requests_.find(id);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700163 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700164 // TODO(pthatcher): Log unknown responses without being too spammy
165 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700167 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168
169 // Parse the STUN message and continue processing as usual.
170
jbauchf1f87202016-03-30 06:43:37 -0700171 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700172 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700173 if (!response->Read(&buf)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100174 RTC_LOG(LS_WARNING) << "Failed to read STUN response "
175 << rtc::hex_encode(id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700177 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000178
179 return CheckResponse(response.get());
180}
181
Tommi25452572022-04-12 12:51:40 +0200182void StunRequestManager::OnRequestTimedOut(StunRequest* request) {
183 RTC_DCHECK_RUN_ON(thread_);
184 requests_.erase(request->id());
185}
186
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +0000187void StunRequestManager::SendPacket(const void* data,
188 size_t size,
189 StunRequest* request) {
190 RTC_DCHECK_EQ(this, request->manager());
191 send_packet_(data, size, request);
192}
193
Tommi86aa03e2022-04-12 09:17:57 +0200194StunRequest::StunRequest(StunRequestManager& manager)
195 : manager_(manager),
Yves Gerey665174f2018-06-19 15:03:05 +0200196 msg_(new StunMessage()),
Tommi86aa03e2022-04-12 09:17:57 +0200197 tstamp_(0),
198 count_(0),
199 timeout_(false) {
Yves Gerey665174f2018-06-19 15:03:05 +0200200 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201}
202
Tommi86aa03e2022-04-12 09:17:57 +0200203StunRequest::StunRequest(StunRequestManager& manager,
Tommi278b19d2022-04-12 14:03:40 +0200204 std::unique_ptr<StunMessage> message)
Tommi86aa03e2022-04-12 09:17:57 +0200205 : manager_(manager),
Tommi278b19d2022-04-12 14:03:40 +0200206 msg_(std::move(message)),
Tommi86aa03e2022-04-12 09:17:57 +0200207 tstamp_(0),
208 count_(0),
209 timeout_(false) {
Yves Gerey665174f2018-06-19 15:03:05 +0200210 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211}
212
213StunRequest::~StunRequest() {
Tommi86aa03e2022-04-12 09:17:57 +0200214 manager_.network_thread()->Clear(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215}
216
217void StunRequest::Construct() {
218 if (msg_->type() == 0) {
Tommi86aa03e2022-04-12 09:17:57 +0200219 Prepare(msg_.get());
nisseede5da42017-01-12 05:15:36 -0800220 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 }
222}
223
224int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800225 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226 return msg_->type();
227}
228
229const StunMessage* StunRequest::msg() const {
Tommi86aa03e2022-04-12 09:17:57 +0200230 return msg_.get();
Jonas Orelandbdcee282017-10-10 14:01:40 +0200231}
232
honghaiz34b11eb2016-03-16 08:55:44 -0700233int StunRequest::Elapsed() const {
Tommi86aa03e2022-04-12 09:17:57 +0200234 RTC_DCHECK_RUN_ON(network_thread());
nisse1bffc1d2016-05-02 08:18:55 -0700235 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236}
237
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000238void StunRequest::OnMessage(rtc::Message* pmsg) {
Tommi86aa03e2022-04-12 09:17:57 +0200239 RTC_DCHECK_RUN_ON(network_thread());
nisseede5da42017-01-12 05:15:36 -0800240 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241
242 if (timeout_) {
243 OnTimeout();
Tommi25452572022-04-12 12:51:40 +0200244 manager_.OnRequestTimedOut(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 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);
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +0000252 manager_.SendPacket(buf.Data(), buf.Length(), this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700254 OnSent();
Tommi86aa03e2022-04-12 09:17:57 +0200255 manager_.network_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() {
Tommi86aa03e2022-04-12 09:17:57 +0200260 RTC_DCHECK_RUN_ON(network_thread());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000261 count_ += 1;
pthatcher94a2f212017-02-08 14:42:22 -0800262 int retransmissions = (count_ - 1);
Mirko Bonadeif3879462020-04-14 13:44:09 +0200263 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000264 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700265 }
Tommi86aa03e2022-04-12 09:17:57 +0200266 RTC_DLOG(LS_VERBOSE) << "Sent STUN request " << count_
267 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700268}
269
270int StunRequest::resend_delay() {
Tommi86aa03e2022-04-12 09:17:57 +0200271 RTC_DCHECK_RUN_ON(network_thread());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700272 if (count_ == 0) {
273 return 0;
274 }
pthatcher94a2f212017-02-08 14:42:22 -0800275 int retransmissions = (count_ - 1);
276 int rto = STUN_INITIAL_RTO << retransmissions;
277 return std::min(rto, STUN_MAX_RTO);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000278}
279
Tommi86aa03e2022-04-12 09:17:57 +0200280void StunRequest::set_timed_out() {
281 RTC_DCHECK_RUN_ON(network_thread());
282 timeout_ = true;
283}
284
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000285} // namespace cricket