3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#if defined(_MSC_VER) && _MSC_VER < 1300
29
#pragma warning(disable:4786)
31
#include "talk/base/logging.h"
32
#include "talk/p2p/base/stunrequest.h"
33
#include "talk/p2p/base/helpers.h"
39
const uint32 MSG_STUN_SEND = 1;
41
const int MAX_SENDS = 9;
42
const int DELAY_UNIT = 100; // 100 milliseconds
43
const int DELAY_MAX_FACTOR = 16;
45
StunRequestManager::StunRequestManager(Thread* thread) : thread_(thread) {
48
StunRequestManager::~StunRequestManager() {
49
while (requests_.begin() != requests_.end()) {
50
StunRequest *request = requests_.begin()->second;
51
requests_.erase(requests_.begin());
56
void StunRequestManager::Send(StunRequest* request) {
57
SendDelayed(request, 0);
60
void StunRequestManager::SendDelayed(StunRequest* request, int delay) {
61
request->set_manager(this);
62
assert(requests_.find(request->id()) == requests_.end());
63
requests_[request->id()] = request;
64
thread_->PostDelayed(delay, request, MSG_STUN_SEND, NULL);
67
void StunRequestManager::Remove(StunRequest* request) {
68
assert(request->manager() == this);
69
RequestMap::iterator iter = requests_.find(request->id());
70
if (iter != requests_.end()) {
71
assert(iter->second == request);
72
requests_.erase(iter);
73
thread_->Clear(request);
77
void StunRequestManager::Clear() {
78
std::vector<StunRequest*> requests;
79
for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i)
80
requests.push_back(i->second);
82
for (uint32 i = 0; i < requests.size(); ++i)
86
bool StunRequestManager::CheckResponse(StunMessage* msg) {
87
RequestMap::iterator iter = requests_.find(msg->transaction_id());
88
if (iter == requests_.end())
91
StunRequest* request = iter->second;
92
if (msg->type() == GetStunResponseType(request->type())) {
93
request->OnResponse(msg);
94
} else if (msg->type() == GetStunErrorResponseType(request->type())) {
95
request->OnErrorResponse(msg);
97
LOG(LERROR) << "Received response with wrong type: " << msg->type()
98
<< " (expecting " << GetStunResponseType(request->type()) << ")";
106
bool StunRequestManager::CheckResponse(const char* data, size_t size) {
107
// Check the appropriate bytes of the stream to see if they match the
108
// transaction ID of a response we are expecting.
114
id.append(data + 4, 16);
116
RequestMap::iterator iter = requests_.find(id);
117
if (iter == requests_.end())
120
// Parse the STUN message and continue processing as usual.
122
ByteBuffer buf(data, size);
127
return CheckResponse(&msg);
130
StunRequest::StunRequest()
131
: manager_(0), id_(CreateRandomString(16)), msg_(0), count_(0),
132
timeout_(false), tstamp_(0) {
135
StunRequest::StunRequest(StunMessage* request)
136
: manager_(0), id_(request->transaction_id()), msg_(request),
137
count_(0), timeout_(false) {
140
StunRequest::~StunRequest() {
141
assert(manager_ != NULL);
143
manager_->Remove(this);
144
manager_->thread_->Clear(this);
149
const StunMessageType StunRequest::type() {
154
void StunRequest::set_manager(StunRequestManager* manager) {
159
void StunRequest::OnMessage(Message* pmsg) {
161
assert(pmsg->message_id == MSG_STUN_SEND);
164
msg_ = new StunMessage();
165
msg_->SetTransactionID(id_);
167
assert(msg_->transaction_id() == id_);
176
tstamp_ = GetMillisecondCount();
180
manager_->SignalSendPacket(buf.Data(), buf.Length());
182
int delay = GetNextDelay();
183
manager_->thread_->PostDelayed(delay, this, MSG_STUN_SEND, NULL);
186
uint32 StunRequest::Elapsed() const {
187
return (GetMillisecondCount() - tstamp_);
190
int StunRequest::GetNextDelay() {
191
int delay = DELAY_UNIT * _min(1 << count_, DELAY_MAX_FACTOR);
193
if (count_ == MAX_SENDS)
198
} // namespace cricket