henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "p2p/base/stunrequest.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 13 | #include <algorithm> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 14 | #include <memory> |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 15 | #include <vector> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 16 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame^] | 17 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/helpers.h" |
| 20 | #include "rtc_base/logging.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 25 | const uint32_t MSG_STUN_SEND = 1; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 26 | |
pthatcher | 94a2f21 | 2017-02-08 14:42:22 -0800 | [diff] [blame] | 27 | // 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. |
| 31 | const 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). |
| 36 | const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9 |
| 37 | |
| 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. |
| 42 | const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 43 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 44 | StunRequestManager::StunRequestManager(rtc::Thread* thread) : thread_(thread) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 45 | |
| 46 | StunRequestManager::~StunRequestManager() { |
| 47 | while (requests_.begin() != requests_.end()) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 48 | StunRequest* request = requests_.begin()->second; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | requests_.erase(requests_.begin()); |
| 50 | delete request; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void StunRequestManager::Send(StunRequest* request) { |
| 55 | SendDelayed(request, 0); |
| 56 | } |
| 57 | |
| 58 | void StunRequestManager::SendDelayed(StunRequest* request, int delay) { |
| 59 | request->set_manager(this); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 60 | RTC_DCHECK(requests_.find(request->id()) == requests_.end()); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 61 | request->set_origin(origin_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | request->Construct(); |
| 63 | requests_[request->id()] = request; |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame] | 64 | if (delay > 0) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 65 | thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL); |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame] | 66 | } else { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 67 | thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame] | 68 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | } |
| 70 | |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 71 | void StunRequestManager::Flush(int msg_type) { |
Honghai Zhang | 8597543 | 2015-11-12 11:07:12 -0800 | [diff] [blame] | 72 | for (const auto kv : requests_) { |
| 73 | StunRequest* request = kv.second; |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 74 | if (msg_type == kAllRequests || msg_type == request->type()) { |
| 75 | thread_->Clear(request, MSG_STUN_SEND); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 76 | thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 77 | } |
Honghai Zhang | 8597543 | 2015-11-12 11:07:12 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 81 | bool StunRequestManager::HasRequest(int msg_type) { |
| 82 | for (const auto kv : requests_) { |
| 83 | StunRequest* request = kv.second; |
| 84 | if (msg_type == kAllRequests || msg_type == request->type()) { |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | void StunRequestManager::Remove(StunRequest* request) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 92 | RTC_DCHECK(request->manager() == this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | RequestMap::iterator iter = requests_.find(request->id()); |
| 94 | if (iter != requests_.end()) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 95 | RTC_DCHECK(iter->second == request); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 96 | requests_.erase(iter); |
| 97 | thread_->Clear(request); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void StunRequestManager::Clear() { |
| 102 | std::vector<StunRequest*> requests; |
| 103 | for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i) |
| 104 | requests.push_back(i->second); |
| 105 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 106 | for (uint32_t i = 0; i < requests.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | // StunRequest destructor calls Remove() which deletes requests |
| 108 | // from |requests_|. |
| 109 | delete requests[i]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool StunRequestManager::CheckResponse(StunMessage* msg) { |
| 114 | RequestMap::iterator iter = requests_.find(msg->transaction_id()); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 115 | if (iter == requests_.end()) { |
Peter Thatcher | 3e95d3e | 2015-05-18 15:55:18 -0700 | [diff] [blame] | 116 | // TODO(pthatcher): Log unknown responses without being too spammy |
| 117 | // in the logs. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 118 | return false; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 119 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 120 | |
| 121 | StunRequest* request = iter->second; |
| 122 | if (msg->type() == GetStunSuccessResponseType(request->type())) { |
| 123 | request->OnResponse(msg); |
| 124 | } else if (msg->type() == GetStunErrorResponseType(request->type())) { |
| 125 | request->OnErrorResponse(msg); |
| 126 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 127 | RTC_LOG(LERROR) << "Received response with wrong type: " << msg->type() |
| 128 | << " (expecting " |
| 129 | << GetStunSuccessResponseType(request->type()) << ")"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
| 133 | delete request; |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool StunRequestManager::CheckResponse(const char* data, size_t size) { |
| 138 | // Check the appropriate bytes of the stream to see if they match the |
| 139 | // transaction ID of a response we are expecting. |
| 140 | |
| 141 | if (size < 20) |
| 142 | return false; |
| 143 | |
| 144 | std::string id; |
| 145 | id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength); |
| 146 | |
| 147 | RequestMap::iterator iter = requests_.find(id); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 148 | if (iter == requests_.end()) { |
Peter Thatcher | 3e95d3e | 2015-05-18 15:55:18 -0700 | [diff] [blame] | 149 | // TODO(pthatcher): Log unknown responses without being too spammy |
| 150 | // in the logs. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 151 | return false; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 152 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 153 | |
| 154 | // Parse the STUN message and continue processing as usual. |
| 155 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 156 | rtc::ByteBufferReader buf(data, size); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 157 | std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew()); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 158 | if (!response->Read(&buf)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 159 | RTC_LOG(LS_WARNING) << "Failed to read STUN response " |
| 160 | << rtc::hex_encode(id); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 161 | return false; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 162 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 163 | |
| 164 | return CheckResponse(response.get()); |
| 165 | } |
| 166 | |
| 167 | StunRequest::StunRequest() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 168 | : count_(0), |
| 169 | timeout_(false), |
| 170 | manager_(0), |
| 171 | msg_(new StunMessage()), |
| 172 | tstamp_(0) { |
| 173 | msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | StunRequest::StunRequest(StunMessage* request) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 177 | : count_(0), timeout_(false), manager_(0), msg_(request), tstamp_(0) { |
| 178 | msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | StunRequest::~StunRequest() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 182 | RTC_DCHECK(manager_ != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 183 | if (manager_) { |
| 184 | manager_->Remove(this); |
| 185 | manager_->thread_->Clear(this); |
| 186 | } |
| 187 | delete msg_; |
| 188 | } |
| 189 | |
| 190 | void StunRequest::Construct() { |
| 191 | if (msg_->type() == 0) { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 192 | if (!origin_.empty()) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame^] | 193 | msg_->AddAttribute(absl::make_unique<StunByteStringAttribute>( |
| 194 | STUN_ATTR_ORIGIN, origin_)); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 195 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 196 | Prepare(msg_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 197 | RTC_DCHECK(msg_->type() != 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | int StunRequest::type() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 202 | RTC_DCHECK(msg_ != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 203 | return msg_->type(); |
| 204 | } |
| 205 | |
| 206 | const StunMessage* StunRequest::msg() const { |
| 207 | return msg_; |
| 208 | } |
| 209 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 210 | StunMessage* StunRequest::mutable_msg() { |
| 211 | return msg_; |
| 212 | } |
| 213 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 214 | int StunRequest::Elapsed() const { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 215 | return static_cast<int>(rtc::TimeMillis() - tstamp_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 216 | } |
| 217 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 218 | void StunRequest::set_manager(StunRequestManager* manager) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 219 | RTC_DCHECK(!manager_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 220 | manager_ = manager; |
| 221 | } |
| 222 | |
| 223 | void StunRequest::OnMessage(rtc::Message* pmsg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 224 | RTC_DCHECK(manager_ != NULL); |
| 225 | RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 226 | |
| 227 | if (timeout_) { |
| 228 | OnTimeout(); |
| 229 | delete this; |
| 230 | return; |
| 231 | } |
| 232 | |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 233 | tstamp_ = rtc::TimeMillis(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 234 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 235 | rtc::ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 236 | msg_->Write(&buf); |
| 237 | manager_->SignalSendPacket(buf.Data(), buf.Length(), this); |
| 238 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 239 | OnSent(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 240 | manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, |
| 241 | MSG_STUN_SEND, NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 244 | void StunRequest::OnSent() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 245 | count_ += 1; |
pthatcher | 94a2f21 | 2017-02-08 14:42:22 -0800 | [diff] [blame] | 246 | int retransmissions = (count_ - 1); |
| 247 | if (retransmissions >= STUN_MAX_RETRANSMISSIONS) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 248 | timeout_ = true; |
Taylor Brandstetter | 5ef034a | 2016-05-25 17:20:35 -0700 | [diff] [blame] | 249 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 250 | RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_ |
| 251 | << "; resend delay = " << resend_delay(); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | int StunRequest::resend_delay() { |
| 255 | if (count_ == 0) { |
| 256 | return 0; |
| 257 | } |
pthatcher | 94a2f21 | 2017-02-08 14:42:22 -0800 | [diff] [blame] | 258 | int retransmissions = (count_ - 1); |
| 259 | int rto = STUN_INITIAL_RTO << retransmissions; |
| 260 | return std::min(rto, STUN_MAX_RTO); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | } // namespace cricket |