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