added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / tracker_manager.hpp
blobc9d1f52c66feb6daf8407ef8882d985e979499aa
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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 #ifndef TORRENT_TRACKER_MANAGER_HPP_INCLUDED
34 #define TORRENT_TRACKER_MANAGER_HPP_INCLUDED
36 #include <vector>
37 #include <string>
38 #include <utility>
39 #include <ctime>
41 #ifdef _MSC_VER
42 #pragma warning(push, 1)
43 #endif
45 #include <boost/shared_ptr.hpp>
46 #include <boost/cstdint.hpp>
47 #include <boost/weak_ptr.hpp>
48 #include <boost/intrusive_ptr.hpp>
49 #include <boost/thread/mutex.hpp>
50 #include <boost/thread/recursive_mutex.hpp>
51 #include <boost/tuple/tuple.hpp>
53 #ifdef _MSC_VER
54 #pragma warning(pop)
55 #endif
57 #include "libtorrent/socket.hpp"
58 #include "libtorrent/entry.hpp"
59 #include "libtorrent/session_settings.hpp"
60 #include "libtorrent/peer_id.hpp"
61 #include "libtorrent/peer.hpp"
62 #include "libtorrent/config.hpp"
63 #include "libtorrent/time.hpp"
64 #include "libtorrent/connection_queue.hpp"
65 #include "libtorrent/intrusive_ptr_base.hpp"
67 namespace libtorrent
69 struct request_callback;
70 class tracker_manager;
71 struct timeout_handler;
72 struct tracker_connection;
74 // returns -1 if gzip header is invalid or the header size in bytes
75 TORRENT_EXPORT int gzip_header(const char* buf, int size);
77 struct TORRENT_EXPORT tracker_request
79 tracker_request()
80 : kind(announce_request)
81 , event(none)
82 , key(0)
83 , num_want(0)
86 enum
88 announce_request,
89 scrape_request
90 } kind;
92 enum event_t
94 none,
95 completed,
96 started,
97 stopped
100 sha1_hash info_hash;
101 peer_id pid;
102 size_type downloaded;
103 size_type uploaded;
104 size_type left;
105 unsigned short listen_port;
106 event_t event;
107 std::string url;
108 int key;
109 int num_want;
110 std::string ipv6;
113 struct TORRENT_EXPORT request_callback
115 friend class tracker_manager;
116 request_callback(): m_manager(0) {}
117 virtual ~request_callback() {}
118 virtual void tracker_warning(tracker_request const& req
119 , std::string const& msg) = 0;
120 virtual void tracker_scrape_response(tracker_request const& req
121 , int complete, int incomplete, int downloads) {}
122 virtual void tracker_response(
123 tracker_request const& req
124 , std::vector<peer_entry>& peers
125 , int interval
126 , int complete
127 , int incomplete
128 , address const& external_ip) = 0;
129 virtual void tracker_request_timed_out(
130 tracker_request const& req) = 0;
131 virtual void tracker_request_error(
132 tracker_request const& req
133 , int response_code
134 , const std::string& description) = 0;
136 tcp::endpoint m_tracker_address;
138 #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
139 virtual void debug_log(const std::string& line) = 0;
140 #endif
141 private:
142 tracker_manager* m_manager;
145 struct TORRENT_EXPORT timeout_handler
146 : intrusive_ptr_base<timeout_handler>
147 , boost::noncopyable
149 timeout_handler(io_service& str);
151 void set_timeout(int completion_timeout, int read_timeout);
152 void restart_read_timeout();
153 void cancel();
155 virtual void on_timeout() = 0;
156 virtual ~timeout_handler() {}
158 private:
160 void timeout_callback(error_code const&);
162 boost::intrusive_ptr<timeout_handler> self()
163 { return boost::intrusive_ptr<timeout_handler>(this); }
165 // used for timeouts
166 // this is set when the request has been sent
167 ptime m_start_time;
168 // this is set every time something is received
169 ptime m_read_time;
170 // the asio async operation
171 deadline_timer m_timeout;
173 int m_completion_timeout;
174 int m_read_timeout;
176 typedef boost::mutex mutex_t;
177 mutable mutex_t m_mutex;
178 bool m_abort;
181 struct TORRENT_EXPORT tracker_connection
182 : timeout_handler
184 tracker_connection(tracker_manager& man
185 , tracker_request const& req
186 , io_service& ios
187 , address bind_interface
188 , boost::weak_ptr<request_callback> r);
190 boost::shared_ptr<request_callback> requester();
191 virtual ~tracker_connection() {}
193 tracker_request const& tracker_req() const { return m_req; }
195 void fail(int code, char const* msg);
196 void fail_timeout();
197 virtual void close();
198 address const& bind_interface() const { return m_bind_interface; }
200 protected:
201 boost::weak_ptr<request_callback> m_requester;
202 private:
203 address m_bind_interface;
204 tracker_manager& m_man;
205 const tracker_request m_req;
208 class TORRENT_EXPORT tracker_manager: boost::noncopyable
210 public:
212 tracker_manager(session_settings const& s, proxy_settings const& ps)
213 : m_settings(s)
214 , m_proxy(ps)
215 , m_abort(false) {}
217 void queue_request(
218 io_service& ios
219 , connection_queue& cc
220 , tracker_request r
221 , std::string const& auth
222 , address bind_infc
223 , boost::weak_ptr<request_callback> c
224 = boost::weak_ptr<request_callback>());
225 void abort_all_requests();
227 void remove_request(tracker_connection const*);
228 bool empty() const;
229 int num_requests() const;
231 private:
233 typedef boost::recursive_mutex mutex_t;
234 mutable mutex_t m_mutex;
236 typedef std::list<boost::intrusive_ptr<tracker_connection> >
237 tracker_connections_t;
238 tracker_connections_t m_connections;
239 session_settings const& m_settings;
240 proxy_settings const& m_proxy;
241 bool m_abort;
245 #endif // TORRENT_TRACKER_MANAGER_HPP_INCLUDED