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