blob: eebdd077161173784fe12f4d1fe82178592a70e5 [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
Yves Gerey665174f2018-06-19 15:03:05 +020046StunRequestManager::StunRequestManager(rtc::Thread* thread) : thread_(thread) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047
Tommi25452572022-04-12 12:51:40 +020048StunRequestManager::~StunRequestManager() = default;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049
50void StunRequestManager::Send(StunRequest* request) {
51 SendDelayed(request, 0);
52}
53
54void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
Tommi86aa03e2022-04-12 09:17:57 +020055 RTC_DCHECK_RUN_ON(thread_);
56 RTC_DCHECK_EQ(this, request->manager());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 request->Construct();
Tommi25452572022-04-12 12:51:40 +020058 auto [iter, was_inserted] =
59 requests_.emplace(request->id(), absl::WrapUnique(request));
60 RTC_DCHECK(was_inserted);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000061 if (delay > 0) {
Tommi25452572022-04-12 12:51:40 +020062 thread_->PostDelayed(RTC_FROM_HERE, delay, iter->second.get(),
63 MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000064 } else {
Tommi25452572022-04-12 12:51:40 +020065 thread_->Send(RTC_FROM_HERE, iter->second.get(), MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000066 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067}
68
Tommi86aa03e2022-04-12 09:17:57 +020069void StunRequestManager::FlushForTest(int msg_type) {
70 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020071 for (const auto& [unused, request] : requests_) {
honghaiz6b9ab922016-01-05 09:06:12 -080072 if (msg_type == kAllRequests || msg_type == request->type()) {
Tommi25452572022-04-12 12:51:40 +020073 thread_->Clear(request.get(), MSG_STUN_SEND);
74 thread_->Send(RTC_FROM_HERE, request.get(), MSG_STUN_SEND, NULL);
honghaiz6b9ab922016-01-05 09:06:12 -080075 }
Honghai Zhang85975432015-11-12 11:07:12 -080076 }
77}
78
Tommi86aa03e2022-04-12 09:17:57 +020079bool StunRequestManager::HasRequestForTest(int msg_type) {
80 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020081 for (const auto& [unused, request] : requests_) {
honghaize2af9ef2016-03-03 08:27:47 -080082 if (msg_type == kAllRequests || msg_type == request->type()) {
83 return true;
84 }
85 }
86 return false;
87}
88
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000089void StunRequestManager::Clear() {
Tommi86aa03e2022-04-12 09:17:57 +020090 RTC_DCHECK_RUN_ON(thread_);
Tommi25452572022-04-12 12:51:40 +020091 requests_.clear();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092}
93
94bool StunRequestManager::CheckResponse(StunMessage* msg) {
Tommi86aa03e2022-04-12 09:17:57 +020095 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096 RequestMap::iterator iter = requests_.find(msg->transaction_id());
Peter Thatcher1cf6f812015-05-15 10:40:45 -070097 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -070098 // TODO(pthatcher): Log unknown responses without being too spammy
99 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700101 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
Tommi25452572022-04-12 12:51:40 +0200103 StunRequest* request = iter->second.get();
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000104
105 // Now that we know the request, we can see if the response is
106 // integrity-protected or not.
107 // For some tests, the message integrity is not set in the request.
108 // Complain, and then don't check.
109 bool skip_integrity_checking = false;
110 if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) {
111 skip_integrity_checking = true;
112 } else {
113 msg->ValidateMessageIntegrity(request->msg()->password());
114 }
115
Tommi25452572022-04-12 12:51:40 +0200116 bool success = true;
117
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700118 if (!msg->GetNonComprehendedAttributes().empty()) {
119 // If a response contains unknown comprehension-required attributes, it's
120 // simply discarded and the transaction is considered failed. See RFC5389
121 // sections 7.3.3 and 7.3.4.
122 RTC_LOG(LS_ERROR) << ": Discarding response due to unknown "
123 "comprehension-required attribute.";
Tommi25452572022-04-12 12:51:40 +0200124 success = false;
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700125 } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000126 if (!msg->IntegrityOk() && !skip_integrity_checking) {
127 return false;
128 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 request->OnResponse(msg);
130 } else if (msg->type() == GetStunErrorResponseType(request->type())) {
131 request->OnErrorResponse(msg);
132 } else {
Harald Alvestrand5f341302021-11-24 10:01:32 +0000133 RTC_LOG(LS_ERROR) << "Received response with wrong type: " << msg->type()
134 << " (expecting "
135 << GetStunSuccessResponseType(request->type()) << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136 return false;
137 }
138
Tommi25452572022-04-12 12:51:40 +0200139 requests_.erase(iter);
140 return success;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141}
142
Tommi86aa03e2022-04-12 09:17:57 +0200143bool StunRequestManager::empty() const {
144 RTC_DCHECK_RUN_ON(thread_);
145 return requests_.empty();
146}
147
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148bool StunRequestManager::CheckResponse(const char* data, size_t size) {
Tommi86aa03e2022-04-12 09:17:57 +0200149 RTC_DCHECK_RUN_ON(thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150 // Check the appropriate bytes of the stream to see if they match the
151 // transaction ID of a response we are expecting.
152
153 if (size < 20)
154 return false;
155
156 std::string id;
157 id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
158
159 RequestMap::iterator iter = requests_.find(id);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700160 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700161 // TODO(pthatcher): Log unknown responses without being too spammy
162 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700164 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165
166 // Parse the STUN message and continue processing as usual.
167
jbauchf1f87202016-03-30 06:43:37 -0700168 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700169 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700170 if (!response->Read(&buf)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100171 RTC_LOG(LS_WARNING) << "Failed to read STUN response "
172 << rtc::hex_encode(id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700174 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175
176 return CheckResponse(response.get());
177}
178
Tommi25452572022-04-12 12:51:40 +0200179void StunRequestManager::OnRequestTimedOut(StunRequest* request) {
180 RTC_DCHECK_RUN_ON(thread_);
181 requests_.erase(request->id());
182}
183
Tommi86aa03e2022-04-12 09:17:57 +0200184StunRequest::StunRequest(StunRequestManager& manager)
185 : manager_(manager),
Yves Gerey665174f2018-06-19 15:03:05 +0200186 msg_(new StunMessage()),
Tommi86aa03e2022-04-12 09:17:57 +0200187 tstamp_(0),
188 count_(0),
189 timeout_(false) {
Yves Gerey665174f2018-06-19 15:03:05 +0200190 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191}
192
Tommi86aa03e2022-04-12 09:17:57 +0200193StunRequest::StunRequest(StunRequestManager& manager,
Tommi278b19d2022-04-12 14:03:40 +0200194 std::unique_ptr<StunMessage> message)
Tommi86aa03e2022-04-12 09:17:57 +0200195 : manager_(manager),
Tommi278b19d2022-04-12 14:03:40 +0200196 msg_(std::move(message)),
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
203StunRequest::~StunRequest() {
Tommi86aa03e2022-04-12 09:17:57 +0200204 manager_.network_thread()->Clear(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205}
206
207void StunRequest::Construct() {
208 if (msg_->type() == 0) {
Tommi86aa03e2022-04-12 09:17:57 +0200209 Prepare(msg_.get());
nisseede5da42017-01-12 05:15:36 -0800210 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211 }
212}
213
214int 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);
Tommi86aa03e2022-04-12 09:17:57 +0200242 manager_.SignalSendPacket(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