3 Copyright (c) 2003, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/pch.hpp"
41 #include <boost/bind.hpp>
43 #include "libtorrent/tracker_manager.hpp"
44 #include "libtorrent/http_tracker_connection.hpp"
45 #include "libtorrent/udp_tracker_connection.hpp"
46 #include "libtorrent/entry.hpp"
47 #include "libtorrent/bencode.hpp"
48 #include "libtorrent/torrent.hpp"
49 #include "libtorrent/peer_connection.hpp"
51 using namespace libtorrent
;
52 using boost::tuples::make_tuple
;
53 using boost::tuples::tuple
;
60 minimum_tracker_response_length
= 3,
61 http_buffer_size
= 2048
68 timeout_handler::timeout_handler(io_service
& ios
)
69 : m_start_time(time_now())
70 , m_read_time(time_now())
72 , m_completion_timeout(0)
77 void timeout_handler::set_timeout(int completion_timeout
, int read_timeout
)
79 m_completion_timeout
= completion_timeout
;
80 m_read_timeout
= read_timeout
;
81 m_start_time
= m_read_time
= time_now();
85 int timeout
= (std::min
)(
86 m_read_timeout
, (std::min
)(m_completion_timeout
, m_read_timeout
));
88 m_timeout
.expires_at(m_read_time
+ seconds(timeout
), ec
);
89 m_timeout
.async_wait(bind(
90 &timeout_handler::timeout_callback
, self(), _1
));
93 void timeout_handler::restart_read_timeout()
95 m_read_time
= time_now();
98 void timeout_handler::cancel()
101 m_completion_timeout
= 0;
103 m_timeout
.cancel(ec
);
106 void timeout_handler::timeout_callback(error_code
const& error
)
109 if (m_completion_timeout
== 0) return;
111 ptime
now(time_now());
112 time_duration receive_timeout
= now
- m_read_time
;
113 time_duration completion_timeout
= now
- m_start_time
;
116 < total_seconds(receive_timeout
)
117 || m_completion_timeout
118 < total_seconds(completion_timeout
))
126 int timeout
= (std::min
)(
127 m_read_timeout
, (std::min
)(m_completion_timeout
, m_read_timeout
));
129 m_timeout
.expires_at(m_read_time
+ seconds(timeout
), ec
);
130 m_timeout
.async_wait(
131 bind(&timeout_handler::timeout_callback
, self(), _1
));
134 tracker_connection::tracker_connection(
136 , tracker_request
const& req
138 , address bind_interface_
139 , boost::weak_ptr
<request_callback
> r
)
140 : timeout_handler(ios
)
142 , m_bind_interface(bind_interface_
)
147 boost::shared_ptr
<request_callback
> tracker_connection::requester()
149 return m_requester
.lock();
152 void tracker_connection::fail(int code
, char const* msg
)
154 boost::shared_ptr
<request_callback
> cb
= requester();
155 if (cb
) cb
->tracker_request_error(m_req
, code
, msg
);
159 void tracker_connection::fail_timeout()
161 boost::shared_ptr
<request_callback
> cb
= requester();
162 if (cb
) cb
->tracker_request_timed_out(m_req
);
166 void tracker_connection::close()
169 m_man
.remove_request(this);
172 void tracker_manager::remove_request(tracker_connection
const* c
)
174 mutex_t::scoped_lock
l(m_mutex
);
176 tracker_connections_t::iterator i
= std::find(m_connections
.begin()
177 , m_connections
.end(), boost::intrusive_ptr
<const tracker_connection
>(c
));
178 if (i
== m_connections
.end()) return;
180 m_connections
.erase(i
);
183 void tracker_manager::queue_request(
185 , connection_queue
& cc
186 , tracker_request req
187 , std::string
const& auth
189 , boost::weak_ptr
<request_callback
> c
)
191 mutex_t::scoped_lock
l(m_mutex
);
192 TORRENT_ASSERT(req
.num_want
>= 0);
193 if (req
.event
== tracker_request::stopped
)
196 TORRENT_ASSERT(!m_abort
|| req
.event
== tracker_request::stopped
);
197 if (m_abort
&& req
.event
!= tracker_request::stopped
)
200 std::string protocol
= req
.url
.substr(0, req
.url
.find(':'));
202 boost::intrusive_ptr
<tracker_connection
> con
;
204 #ifdef TORRENT_USE_OPENSSL
205 if (protocol
== "http" || protocol
== "https")
207 if (protocol
== "http")
210 con
= new http_tracker_connection(
211 ios
, cc
, *this, req
, bind_infc
, c
212 , m_settings
, m_proxy
, auth
);
214 else if (protocol
== "udp")
216 con
= new udp_tracker_connection(
217 ios
, cc
, *this, req
, bind_infc
218 , c
, m_settings
, m_proxy
);
222 if (boost::shared_ptr
<request_callback
> r
= c
.lock())
223 r
->tracker_request_error(req
, -1, "unknown protocol in tracker url: "
228 m_connections
.push_back(con
);
230 boost::shared_ptr
<request_callback
> cb
= con
->requester();
231 if (cb
) cb
->m_manager
= this;
234 void tracker_manager::abort_all_requests()
236 // removes all connections from m_connections
237 // except those with a requester == 0 (since those are
238 // 'event=stopped'-requests)
239 mutex_t::scoped_lock
l(m_mutex
);
242 tracker_connections_t keep_connections
;
244 while (!m_connections
.empty())
246 boost::intrusive_ptr
<tracker_connection
>& c
= m_connections
.back();
249 m_connections
.pop_back();
252 tracker_request
const& req
= c
->tracker_req();
253 if (req
.event
== tracker_request::stopped
)
255 keep_connections
.push_back(c
);
256 m_connections
.pop_back();
259 // close will remove the entry from m_connections
262 #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
263 boost::shared_ptr
<request_callback
> rc
= c
->requester();
264 if (rc
) rc
->debug_log("aborting: " + req
.url
);
269 std::swap(m_connections
, keep_connections
);
272 bool tracker_manager::empty() const
274 mutex_t::scoped_lock
l(m_mutex
);
275 return m_connections
.empty();
278 int tracker_manager::num_requests() const
280 mutex_t::scoped_lock
l(m_mutex
);
281 return m_connections
.size();