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