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