blob: 2a1ad6511216350695812ce8f9a2e1ed65fd627e [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());
Tommi25452572022-04-12 12:51:40 +020060 auto [iter, was_inserted] =
61 requests_.emplace(request->id(), absl::WrapUnique(request));
62 RTC_DCHECK(was_inserted);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000063 if (delay > 0) {
Tommi25452572022-04-12 12:51:40 +020064 thread_->PostDelayed(RTC_FROM_HERE, delay, iter->second.get(),
65 MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000066 } else {
Tommi25452572022-04-12 12:51:40 +020067 thread_->Send(RTC_FROM_HERE, iter->second.get(), MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000068 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069}
70
Tommi86aa03e2022-04-12 09:17:57 +020071void StunRequestManager::FlushForTest(int msg_type) {
72 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020073 for (const auto& [unused, request] : requests_) {
honghaiz6b9ab922016-01-05 09:06:12 -080074 if (msg_type == kAllRequests || msg_type == request->type()) {
Tommi25452572022-04-12 12:51:40 +020075 thread_->Clear(request.get(), MSG_STUN_SEND);
76 thread_->Send(RTC_FROM_HERE, request.get(), MSG_STUN_SEND, NULL);
honghaiz6b9ab922016-01-05 09:06:12 -080077 }
Honghai Zhang85975432015-11-12 11:07:12 -080078 }
79}
80
Tommi86aa03e2022-04-12 09:17:57 +020081bool StunRequestManager::HasRequestForTest(int msg_type) {
82 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020083 for (const auto& [unused, request] : requests_) {
honghaize2af9ef2016-03-03 08:27:47 -080084 if (msg_type == kAllRequests || msg_type == request->type()) {
85 return true;
86 }
87 }
88 return false;
89}
90
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091void StunRequestManager::Clear() {
Tommi86aa03e2022-04-12 09:17:57 +020092 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020093 requests_.clear();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094}
95
96bool StunRequestManager::CheckResponse(StunMessage* msg) {
Tommi86aa03e2022-04-12 09:17:57 +020097 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098 RequestMap::iterator iter = requests_.find(msg->transaction_id());
Peter Thatcher1cf6f812015-05-15 10:40:45 -070099 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700100 // TODO(pthatcher): Log unknown responses without being too spammy
101 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700103 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104
Tommi25452572022-04-12 12:51:40 +0200105 StunRequest* request = iter->second.get();
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000106
107 // Now that we know the request, we can see if the response is
108 // integrity-protected or not.
109 // For some tests, the message integrity is not set in the request.
110 // Complain, and then don't check.
111 bool skip_integrity_checking = false;
112 if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) {
113 skip_integrity_checking = true;
114 } else {
115 msg->ValidateMessageIntegrity(request->msg()->password());
116 }
117
Tommi25452572022-04-12 12:51:40 +0200118 bool success = true;
119
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700120 if (!msg->GetNonComprehendedAttributes().empty()) {
121 // If a response contains unknown comprehension-required attributes, it's
122 // simply discarded and the transaction is considered failed. See RFC5389
123 // sections 7.3.3 and 7.3.4.
124 RTC_LOG(LS_ERROR) << ": Discarding response due to unknown "
125 "comprehension-required attribute.";
Tommi25452572022-04-12 12:51:40 +0200126 success = false;
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700127 } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000128 if (!msg->IntegrityOk() && !skip_integrity_checking) {
129 return false;
130 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131 request->OnResponse(msg);
132 } else if (msg->type() == GetStunErrorResponseType(request->type())) {
133 request->OnErrorResponse(msg);
134 } else {
Harald Alvestrand5f341302021-11-24 10:01:32 +0000135 RTC_LOG(LS_ERROR) << "Received response with wrong type: " << msg->type()
136 << " (expecting "
137 << GetStunSuccessResponseType(request->type()) << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 return false;
139 }
140
Tommi25452572022-04-12 12:51:40 +0200141 requests_.erase(iter);
142 return success;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143}
144
Tommi86aa03e2022-04-12 09:17:57 +0200145bool StunRequestManager::empty() const {
146 RTC_DCHECK_RUN_ON(thread_);
147 return requests_.empty();
148}
149
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150bool StunRequestManager::CheckResponse(const char* data, size_t size) {
Tommi86aa03e2022-04-12 09:17:57 +0200151 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 // Check the appropriate bytes of the stream to see if they match the
153 // transaction ID of a response we are expecting.
154
155 if (size < 20)
156 return false;
157
158 std::string id;
159 id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
160
161 RequestMap::iterator iter = requests_.find(id);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700162 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700163 // TODO(pthatcher): Log unknown responses without being too spammy
164 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700166 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167
168 // Parse the STUN message and continue processing as usual.
169
jbauchf1f87202016-03-30 06:43:37 -0700170 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700171 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700172 if (!response->Read(&buf)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100173 RTC_LOG(LS_WARNING) << "Failed to read STUN response "
174 << rtc::hex_encode(id);
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 return CheckResponse(response.get());
179}
180
Tommi25452572022-04-12 12:51:40 +0200181void StunRequestManager::OnRequestTimedOut(StunRequest* request) {
182 RTC_DCHECK_RUN_ON(thread_);
183 requests_.erase(request->id());
184}
185
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +0000186void StunRequestManager::SendPacket(const void* data,
187 size_t size,
188 StunRequest* request) {
189 RTC_DCHECK_EQ(this, request->manager());
190 send_packet_(data, size, request);
191}
192
Tommi86aa03e2022-04-12 09:17:57 +0200193StunRequest::StunRequest(StunRequestManager& manager)
194 : manager_(manager),
Tommi408143d2022-06-01 15:29:31 +0200195 msg_(new StunMessage(STUN_INVALID_MESSAGE_TYPE)),
Tommi86aa03e2022-04-12 09:17:57 +0200196 tstamp_(0),
197 count_(0),
Tommi408143d2022-06-01 15:29:31 +0200198 timeout_(false) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199
Tommi86aa03e2022-04-12 09:17:57 +0200200StunRequest::StunRequest(StunRequestManager& manager,
Tommi278b19d2022-04-12 14:03:40 +0200201 std::unique_ptr<StunMessage> message)
Tommi86aa03e2022-04-12 09:17:57 +0200202 : manager_(manager),
Tommi278b19d2022-04-12 14:03:40 +0200203 msg_(std::move(message)),
Tommi86aa03e2022-04-12 09:17:57 +0200204 tstamp_(0),
205 count_(0),
206 timeout_(false) {
Tommi408143d2022-06-01 15:29:31 +0200207 RTC_DCHECK(!msg_->transaction_id().empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208}
209
210StunRequest::~StunRequest() {
Tommi86aa03e2022-04-12 09:17:57 +0200211 manager_.network_thread()->Clear(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212}
213
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800215 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 return msg_->type();
217}
218
219const StunMessage* StunRequest::msg() const {
Tommi86aa03e2022-04-12 09:17:57 +0200220 return msg_.get();
Jonas Orelandbdcee282017-10-10 14:01:40 +0200221}
222
honghaiz34b11eb2016-03-16 08:55:44 -0700223int StunRequest::Elapsed() const {
Tommi86aa03e2022-04-12 09:17:57 +0200224 RTC_DCHECK_RUN_ON(network_thread());
nisse1bffc1d2016-05-02 08:18:55 -0700225 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226}
227
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228void StunRequest::OnMessage(rtc::Message* pmsg) {
Tommi86aa03e2022-04-12 09:17:57 +0200229 RTC_DCHECK_RUN_ON(network_thread());
nisseede5da42017-01-12 05:15:36 -0800230 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231
232 if (timeout_) {
233 OnTimeout();
Tommi25452572022-04-12 12:51:40 +0200234 manager_.OnRequestTimedOut(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 return;
236 }
237
nisse1bffc1d2016-05-02 08:18:55 -0700238 tstamp_ = rtc::TimeMillis();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239
jbauchf1f87202016-03-30 06:43:37 -0700240 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 msg_->Write(&buf);
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +0000242 manager_.SendPacket(buf.Data(), buf.Length(), this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700244 OnSent();
Tommi86aa03e2022-04-12 09:17:57 +0200245 manager_.network_thread()->PostDelayed(RTC_FROM_HERE, resend_delay(), this,
246 MSG_STUN_SEND, NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247}
248
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700249void StunRequest::OnSent() {
Tommi86aa03e2022-04-12 09:17:57 +0200250 RTC_DCHECK_RUN_ON(network_thread());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 count_ += 1;
pthatcher94a2f212017-02-08 14:42:22 -0800252 int retransmissions = (count_ - 1);
Mirko Bonadeif3879462020-04-14 13:44:09 +0200253 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700255 }
Tommi86aa03e2022-04-12 09:17:57 +0200256 RTC_DLOG(LS_VERBOSE) << "Sent STUN request " << count_
257 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700258}
259
260int StunRequest::resend_delay() {
Tommi86aa03e2022-04-12 09:17:57 +0200261 RTC_DCHECK_RUN_ON(network_thread());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700262 if (count_ == 0) {
263 return 0;
264 }
pthatcher94a2f212017-02-08 14:42:22 -0800265 int retransmissions = (count_ - 1);
266 int rto = STUN_INITIAL_RTO << retransmissions;
267 return std::min(rto, STUN_MAX_RTO);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268}
269
Tommi86aa03e2022-04-12 09:17:57 +0200270void StunRequest::set_timed_out() {
271 RTC_DCHECK_RUN_ON(network_thread());
272 timeout_ = true;
273}
274
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275} // namespace cricket