blob: ed94ccb5fcc6caf0316ef34b99723fb61e2a5438 [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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022
23namespace cricket {
24
Peter Boström0c4e06b2015-10-07 12:23:21 +020025const uint32_t MSG_STUN_SEND = 1;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026
pthatcher94a2f212017-02-08 14:42:22 -080027// RFC 5389 says SHOULD be 500ms.
28// For years, this was 100ms, but for networks that
29// experience moments of high RTT (such as 2G networks), this doesn't
30// work well.
31const int STUN_INITIAL_RTO = 250; // milliseconds
32
33// The timeout doubles each retransmission, up to this many times
34// RFC 5389 says SHOULD retransmit 7 times.
35// This has been 8 for years (not sure why).
Jonas Olssona4d87372019-07-05 19:08:33 +020036const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9
pthatcher94a2f212017-02-08 14:42:22 -080037
38// We also cap the doubling, even though the standard doesn't say to.
39// This has been 1.6 seconds for years, but for networks that
40// experience moments of high RTT (such as 2G networks), this doesn't
41// work well.
42const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043
Yves Gerey665174f2018-06-19 15:03:05 +020044StunRequestManager::StunRequestManager(rtc::Thread* thread) : thread_(thread) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045
46StunRequestManager::~StunRequestManager() {
47 while (requests_.begin() != requests_.end()) {
Yves Gerey665174f2018-06-19 15:03:05 +020048 StunRequest* request = requests_.begin()->second;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049 requests_.erase(requests_.begin());
50 delete request;
51 }
52}
53
54void StunRequestManager::Send(StunRequest* request) {
55 SendDelayed(request, 0);
56}
57
58void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
59 request->set_manager(this);
nisseede5da42017-01-12 05:15:36 -080060 RTC_DCHECK(requests_.find(request->id()) == requests_.end());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061 request->Construct();
62 requests_[request->id()] = request;
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000063 if (delay > 0) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070064 thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000065 } else {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070066 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000067 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068}
69
honghaiz6b9ab922016-01-05 09:06:12 -080070void StunRequestManager::Flush(int msg_type) {
Mirko Bonadei739baf02019-01-27 17:29:42 +010071 for (const auto& kv : requests_) {
Honghai Zhang85975432015-11-12 11:07:12 -080072 StunRequest* request = kv.second;
honghaiz6b9ab922016-01-05 09:06:12 -080073 if (msg_type == kAllRequests || msg_type == request->type()) {
74 thread_->Clear(request, MSG_STUN_SEND);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070075 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
honghaiz6b9ab922016-01-05 09:06:12 -080076 }
Honghai Zhang85975432015-11-12 11:07:12 -080077 }
78}
79
honghaize2af9ef2016-03-03 08:27:47 -080080bool StunRequestManager::HasRequest(int msg_type) {
Mirko Bonadei739baf02019-01-27 17:29:42 +010081 for (const auto& kv : requests_) {
honghaize2af9ef2016-03-03 08:27:47 -080082 StunRequest* request = kv.second;
83 if (msg_type == kAllRequests || msg_type == request->type()) {
84 return true;
85 }
86 }
87 return false;
88}
89
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090void StunRequestManager::Remove(StunRequest* request) {
nisseede5da42017-01-12 05:15:36 -080091 RTC_DCHECK(request->manager() == this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092 RequestMap::iterator iter = requests_.find(request->id());
93 if (iter != requests_.end()) {
nisseede5da42017-01-12 05:15:36 -080094 RTC_DCHECK(iter->second == request);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095 requests_.erase(iter);
96 thread_->Clear(request);
97 }
98}
99
100void StunRequestManager::Clear() {
101 std::vector<StunRequest*> requests;
102 for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i)
103 requests.push_back(i->second);
104
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 for (uint32_t i = 0; i < requests.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 // StunRequest destructor calls Remove() which deletes requests
Artem Titov2dbb4c92021-07-26 15:12:41 +0200107 // from `requests_`.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 delete requests[i];
109 }
110}
111
112bool StunRequestManager::CheckResponse(StunMessage* msg) {
113 RequestMap::iterator iter = requests_.find(msg->transaction_id());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700114 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700115 // TODO(pthatcher): Log unknown responses without being too spammy
116 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000117 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700118 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
120 StunRequest* request = iter->second;
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000121
122 // Now that we know the request, we can see if the response is
123 // integrity-protected or not.
124 // For some tests, the message integrity is not set in the request.
125 // Complain, and then don't check.
126 bool skip_integrity_checking = false;
127 if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) {
128 skip_integrity_checking = true;
129 } else {
130 msg->ValidateMessageIntegrity(request->msg()->password());
131 }
132
Taylor Brandstetterfb4351b2020-03-23 16:00:31 -0700133 if (!msg->GetNonComprehendedAttributes().empty()) {
134 // If a response contains unknown comprehension-required attributes, it's
135 // simply discarded and the transaction is considered failed. See RFC5389
136 // sections 7.3.3 and 7.3.4.
137 RTC_LOG(LS_ERROR) << ": Discarding response due to unknown "
138 "comprehension-required attribute.";
139 delete request;
140 return false;
141 } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
Harald Alvestrand07d83c82021-03-02 08:09:53 +0000142 if (!msg->IntegrityOk() && !skip_integrity_checking) {
143 return false;
144 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145 request->OnResponse(msg);
146 } else if (msg->type() == GetStunErrorResponseType(request->type())) {
147 request->OnErrorResponse(msg);
148 } else {
Harald Alvestrand5f341302021-11-24 10:01:32 +0000149 RTC_LOG(LS_ERROR) << "Received response with wrong type: " << msg->type()
150 << " (expecting "
151 << GetStunSuccessResponseType(request->type()) << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 return false;
153 }
154
155 delete request;
156 return true;
157}
158
159bool StunRequestManager::CheckResponse(const char* data, size_t size) {
160 // Check the appropriate bytes of the stream to see if they match the
161 // transaction ID of a response we are expecting.
162
163 if (size < 20)
164 return false;
165
166 std::string id;
167 id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength);
168
169 RequestMap::iterator iter = requests_.find(id);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700170 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700171 // TODO(pthatcher): Log unknown responses without being too spammy
172 // in the logs.
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 // Parse the STUN message and continue processing as usual.
177
jbauchf1f87202016-03-30 06:43:37 -0700178 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700179 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700180 if (!response->Read(&buf)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_WARNING) << "Failed to read STUN response "
182 << rtc::hex_encode(id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700184 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185
186 return CheckResponse(response.get());
187}
188
189StunRequest::StunRequest()
Tomas Gunnarsson77baeee2020-09-24 22:39:21 +0200190 : count_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200191 timeout_(false),
192 manager_(0),
193 msg_(new StunMessage()),
Mirko Bonadeif3879462020-04-14 13:44:09 +0200194 tstamp_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200195 msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196}
197
198StunRequest::StunRequest(StunMessage* request)
Tomas Gunnarsson77baeee2020-09-24 22:39:21 +0200199 : count_(0), timeout_(false), manager_(0), msg_(request), tstamp_(0) {
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() {
nisseede5da42017-01-12 05:15:36 -0800204 RTC_DCHECK(manager_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 if (manager_) {
206 manager_->Remove(this);
207 manager_->thread_->Clear(this);
208 }
209 delete msg_;
210}
211
212void StunRequest::Construct() {
213 if (msg_->type() == 0) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 Prepare(msg_);
nisseede5da42017-01-12 05:15:36 -0800215 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 }
217}
218
219int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800220 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 return msg_->type();
222}
223
224const StunMessage* StunRequest::msg() const {
225 return msg_;
226}
227
Jonas Orelandbdcee282017-10-10 14:01:40 +0200228StunMessage* StunRequest::mutable_msg() {
229 return msg_;
230}
231
honghaiz34b11eb2016-03-16 08:55:44 -0700232int StunRequest::Elapsed() const {
nisse1bffc1d2016-05-02 08:18:55 -0700233 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234}
235
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236void StunRequest::set_manager(StunRequestManager* manager) {
nisseede5da42017-01-12 05:15:36 -0800237 RTC_DCHECK(!manager_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000238 manager_ = manager;
239}
240
241void StunRequest::OnMessage(rtc::Message* pmsg) {
nisseede5da42017-01-12 05:15:36 -0800242 RTC_DCHECK(manager_ != NULL);
243 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000244
245 if (timeout_) {
246 OnTimeout();
247 delete this;
248 return;
249 }
250
nisse1bffc1d2016-05-02 08:18:55 -0700251 tstamp_ = rtc::TimeMillis();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252
jbauchf1f87202016-03-30 06:43:37 -0700253 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 msg_->Write(&buf);
255 manager_->SignalSendPacket(buf.Data(), buf.Length(), this);
256
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700257 OnSent();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700258 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this,
259 MSG_STUN_SEND, NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000260}
261
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700262void StunRequest::OnSent() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000263 count_ += 1;
pthatcher94a2f212017-02-08 14:42:22 -0800264 int retransmissions = (count_ - 1);
Mirko Bonadeif3879462020-04-14 13:44:09 +0200265 if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000266 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700267 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100268 RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_
269 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700270}
271
272int StunRequest::resend_delay() {
273 if (count_ == 0) {
274 return 0;
275 }
pthatcher94a2f212017-02-08 14:42:22 -0800276 int retransmissions = (count_ - 1);
277 int rto = STUN_INITIAL_RTO << retransmissions;
278 return std::min(rto, STUN_MAX_RTO);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000279}
280
281} // namespace cricket