1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
41
#include <rak/functional.h>
42
#include <rak/string_manip.h>
44
#include "net/address_list.h"
45
#include "torrent/connection_manager.h"
46
#include "torrent/download_info.h"
47
#include "torrent/exceptions.h"
48
#include "torrent/http.h"
49
#include "torrent/object_stream.h"
50
#include "torrent/tracker_list.h"
52
#include "tracker_http.h"
59
TrackerHttp::TrackerHttp(TrackerList* parent, const std::string& url) :
62
m_get(Http::call_factory()),
65
m_get->signal_done().connect(sigc::mem_fun(*this, &TrackerHttp::receive_done));
66
m_get->signal_failed().connect(sigc::mem_fun(*this, &TrackerHttp::receive_failed));
68
// Haven't considered if this needs any stronger error detection,
69
// can dropping the '?' be used for malicious purposes?
70
size_t delim = url.rfind('?');
72
m_dropDeliminator = delim != std::string::npos &&
73
url.find('/', delim) == std::string::npos;
76
TrackerHttp::~TrackerHttp() {
82
TrackerHttp::is_busy() const {
83
return m_data != NULL;
87
TrackerHttp::send_state(int state) {
91
throw internal_error("TrackerHttp::send_state(...) does not have a valid m_parent.");
94
s.imbue(std::locale::classic());
99
DownloadInfo* info = m_parent->info();
101
*rak::copy_escape_html(info->hash().begin(), info->hash().end(), hash) = '\0';
102
*rak::copy_escape_html(info->local_id().begin(), info->local_id().end(), localId) = '\0';
105
<< (m_dropDeliminator ? '&' : '?')
106
<< "info_hash=" << hash
107
<< "&peer_id=" << localId;
110
s << "&key=" << std::hex << std::setw(8) << std::setfill('0') << m_parent->key() << std::dec;
112
if (!m_trackerId.empty())
113
s << "&trackerid=" << rak::copy_escape_html(m_trackerId);
115
const rak::socket_address* localAddress = rak::socket_address::cast_from(manager->connection_manager()->local_address());
117
if (localAddress->family() == rak::socket_address::af_inet &&
118
!localAddress->sa_inet()->is_address_any())
119
s << "&ip=" << localAddress->address_str();
121
if (info->is_compact())
124
if (m_parent->numwant() >= 0 && state != DownloadInfo::STOPPED)
125
s << "&numwant=" << m_parent->numwant();
127
if (manager->connection_manager()->listen_port())
128
s << "&port=" << manager->connection_manager()->listen_port();
130
s << "&uploaded=" << info->uploaded_adjusted()
131
<< "&downloaded=" << info->completed_adjusted()
132
<< "&left=" << info->slot_left()();
135
case DownloadInfo::STARTED:
136
s << "&event=started";
138
case DownloadInfo::STOPPED:
139
s << "&event=stopped";
141
case DownloadInfo::COMPLETED:
142
s << "&event=completed";
148
m_data = new std::stringstream();
150
m_get->set_url(s.str());
151
m_get->set_stream(m_data);
152
m_get->set_timeout(2 * 60);
158
TrackerHttp::close() {
163
m_get->set_stream(NULL);
170
TrackerHttp::type() const {
175
TrackerHttp::receive_done() {
177
throw internal_error("TrackerHttp::receive_done() called on an invalid object");
179
DownloadInfo* info = m_parent->info();
181
if (!info->signal_tracker_dump().empty()) {
182
std::string dump = m_data->str();
184
info->signal_tracker_dump().emit(m_get->url(), dump.c_str(), dump.size());
191
return receive_failed("Could not parse bencoded data");
194
return receive_failed("Root not a bencoded map");
196
if (b.has_key("failure reason"))
197
return receive_failed("Failure reason \"" +
198
(b.get_key("failure reason").is_string() ?
199
b.get_key_string("failure reason") :
200
std::string("failure reason not a string"))
203
if (b.has_key_value("interval"))
204
set_normal_interval(b.get_key_value("interval"));
206
if (b.has_key_value("min interval"))
207
set_min_interval(b.get_key_value("min interval"));
209
if (b.has_key_string("tracker id"))
210
m_trackerId = b.get_key_string("tracker id");
212
if (b.has_key_value("complete") && b.has_key_value("incomplete")) {
213
m_scrapeComplete = std::max<int64_t>(b.get_key_value("complete"), 0);
214
m_scrapeIncomplete = std::max<int64_t>(b.get_key_value("incomplete"), 0);
215
m_scrapeTimeLast = rak::timer::current().seconds();
218
if (b.has_key_value("downloaded"))
219
m_scrapeDownloaded = std::max<int64_t>(b.get_key_value("downloaded"), 0);
224
// Due to some trackers sending the wrong type when no peers are
225
// available, don't bork on it.
226
if (b.get_key("peers").is_string())
227
l.parse_address_compact(b.get_key_string("peers"));
229
else if (b.get_key("peers").is_list())
230
l.parse_address_normal(b.get_key_list("peers"));
232
} catch (bencode_error& e) {
233
return receive_failed(e.what());
237
m_parent->receive_success(this, &l);
241
TrackerHttp::receive_failed(std::string msg) {
242
// Does the order matter?
244
m_parent->receive_failed(this, msg);