blob: d97f18ccd202d23d429f587b91f4f0814c8689a5 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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.orgff689be2015-02-12 11:54:26 +000013#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070014#include <memory>
15
nisseede5da42017-01-12 05:15:36 -080016#include "webrtc/base/checks.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/base/common.h"
18#include "webrtc/base/helpers.h"
19#include "webrtc/base/logging.h"
Peter Thatcher1cf6f812015-05-15 10:40:45 -070020#include "webrtc/base/stringencode.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021
22namespace cricket {
23
Peter Boström0c4e06b2015-10-07 12:23:21 +020024const uint32_t MSG_STUN_SEND = 1;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
26const int MAX_SENDS = 9;
27const int DELAY_UNIT = 100; // 100 milliseconds
28const int DELAY_MAX_FACTOR = 16;
29
30StunRequestManager::StunRequestManager(rtc::Thread* thread)
31 : thread_(thread) {
32}
33
34StunRequestManager::~StunRequestManager() {
35 while (requests_.begin() != requests_.end()) {
36 StunRequest *request = requests_.begin()->second;
37 requests_.erase(requests_.begin());
38 delete request;
39 }
40}
41
42void StunRequestManager::Send(StunRequest* request) {
43 SendDelayed(request, 0);
44}
45
46void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
47 request->set_manager(this);
nisseede5da42017-01-12 05:15:36 -080048 RTC_DCHECK(requests_.find(request->id()) == requests_.end());
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000049 request->set_origin(origin_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 request->Construct();
51 requests_[request->id()] = request;
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000052 if (delay > 0) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070053 thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000054 } else {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070055 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
pthatcher@webrtc.orgfe672e32015-01-17 00:58:15 +000056 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057}
58
honghaiz6b9ab922016-01-05 09:06:12 -080059void StunRequestManager::Flush(int msg_type) {
Honghai Zhang85975432015-11-12 11:07:12 -080060 for (const auto kv : requests_) {
61 StunRequest* request = kv.second;
honghaiz6b9ab922016-01-05 09:06:12 -080062 if (msg_type == kAllRequests || msg_type == request->type()) {
63 thread_->Clear(request, MSG_STUN_SEND);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070064 thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL);
honghaiz6b9ab922016-01-05 09:06:12 -080065 }
Honghai Zhang85975432015-11-12 11:07:12 -080066 }
67}
68
honghaize2af9ef2016-03-03 08:27:47 -080069bool 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.org269fb4b2014-10-28 22:20:11 +000079void StunRequestManager::Remove(StunRequest* request) {
nisseede5da42017-01-12 05:15:36 -080080 RTC_DCHECK(request->manager() == this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081 RequestMap::iterator iter = requests_.find(request->id());
82 if (iter != requests_.end()) {
nisseede5da42017-01-12 05:15:36 -080083 RTC_DCHECK(iter->second == request);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084 requests_.erase(iter);
85 thread_->Clear(request);
86 }
87}
88
89void 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öm0c4e06b2015-10-07 12:23:21 +020094 for (uint32_t i = 0; i < requests.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095 // StunRequest destructor calls Remove() which deletes requests
96 // from |requests_|.
97 delete requests[i];
98 }
99}
100
101bool StunRequestManager::CheckResponse(StunMessage* msg) {
102 RequestMap::iterator iter = requests_.find(msg->transaction_id());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700103 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700104 // TODO(pthatcher): Log unknown responses without being too spammy
105 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700107 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108
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
125bool 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 Thatcher1cf6f812015-05-15 10:40:45 -0700136 if (iter == requests_.end()) {
Peter Thatcher3e95d3e2015-05-18 15:55:18 -0700137 // TODO(pthatcher): Log unknown responses without being too spammy
138 // in the logs.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700140 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141
142 // Parse the STUN message and continue processing as usual.
143
jbauchf1f87202016-03-30 06:43:37 -0700144 rtc::ByteBufferReader buf(data, size);
kwiberg3ec46792016-04-27 07:22:53 -0700145 std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700146 if (!response->Read(&buf)) {
147 LOG(LS_WARNING) << "Failed to read STUN response " << rtc::hex_encode(id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148 return false;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700149 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150
151 return CheckResponse(response.get());
152}
153
154StunRequest::StunRequest()
155 : count_(0), timeout_(false), manager_(0),
156 msg_(new StunMessage()), tstamp_(0) {
157 msg_->SetTransactionID(
158 rtc::CreateRandomString(kStunTransactionIdLength));
159}
160
161StunRequest::StunRequest(StunMessage* request)
162 : count_(0), timeout_(false), manager_(0),
163 msg_(request), tstamp_(0) {
164 msg_->SetTransactionID(
165 rtc::CreateRandomString(kStunTransactionIdLength));
166}
167
168StunRequest::~StunRequest() {
nisseede5da42017-01-12 05:15:36 -0800169 RTC_DCHECK(manager_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 if (manager_) {
171 manager_->Remove(this);
172 manager_->thread_->Clear(this);
173 }
174 delete msg_;
175}
176
177void StunRequest::Construct() {
178 if (msg_->type() == 0) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000179 if (!origin_.empty()) {
180 msg_->AddAttribute(new StunByteStringAttribute(STUN_ATTR_ORIGIN,
181 origin_));
182 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183 Prepare(msg_);
nisseede5da42017-01-12 05:15:36 -0800184 RTC_DCHECK(msg_->type() != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 }
186}
187
188int StunRequest::type() {
nisseede5da42017-01-12 05:15:36 -0800189 RTC_DCHECK(msg_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 return msg_->type();
191}
192
193const StunMessage* StunRequest::msg() const {
194 return msg_;
195}
196
honghaiz34b11eb2016-03-16 08:55:44 -0700197int StunRequest::Elapsed() const {
nisse1bffc1d2016-05-02 08:18:55 -0700198 return static_cast<int>(rtc::TimeMillis() - tstamp_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199}
200
201
202void StunRequest::set_manager(StunRequestManager* manager) {
nisseede5da42017-01-12 05:15:36 -0800203 RTC_DCHECK(!manager_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204 manager_ = manager;
205}
206
207void StunRequest::OnMessage(rtc::Message* pmsg) {
nisseede5da42017-01-12 05:15:36 -0800208 RTC_DCHECK(manager_ != NULL);
209 RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210
211 if (timeout_) {
212 OnTimeout();
213 delete this;
214 return;
215 }
216
nisse1bffc1d2016-05-02 08:18:55 -0700217 tstamp_ = rtc::TimeMillis();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218
jbauchf1f87202016-03-30 06:43:37 -0700219 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 msg_->Write(&buf);
221 manager_->SignalSendPacket(buf.Data(), buf.Length(), this);
222
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700223 OnSent();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700224 manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this,
225 MSG_STUN_SEND, NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226}
227
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700228void StunRequest::OnSent() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 count_ += 1;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700230 if (count_ == MAX_SENDS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231 timeout_ = true;
Taylor Brandstetter5ef034a2016-05-25 17:20:35 -0700232 }
233 LOG(LS_VERBOSE) << "Sent STUN request " << count_
234 << "; resend delay = " << resend_delay();
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700235}
236
237int 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.org269fb4b2014-10-28 22:20:11 +0000242}
243
244} // namespace cricket