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