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