~ubuntu-branches/ubuntu/wily/libtorrent/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/libtorrent-0.12.6-ipv6-07.patch/src/tracker/tracker_http.cc

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
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.
 
8
// 
 
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.
 
13
// 
 
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
 
17
//
 
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
 
22
// including the two.
 
23
//
 
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.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#include "config.h"
 
38
 
 
39
#include <iomanip>
 
40
#include <sstream>
 
41
#include <rak/functional.h>
 
42
#include <rak/string_manip.h>
 
43
 
 
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"
 
51
 
 
52
#include "tracker_http.h"
 
53
 
 
54
#include "globals.h"
 
55
#include "manager.h"
 
56
 
 
57
namespace torrent {
 
58
 
 
59
TrackerHttp::TrackerHttp(TrackerList* parent, const std::string& url) :
 
60
  Tracker(parent, url),
 
61
 
 
62
  m_get(Http::call_factory()),
 
63
  m_data(NULL) {
 
64
 
 
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));
 
67
 
 
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('?');
 
71
 
 
72
  m_dropDeliminator = delim != std::string::npos &&
 
73
    url.find('/', delim) == std::string::npos;
 
74
}
 
75
 
 
76
TrackerHttp::~TrackerHttp() {
 
77
  delete m_get;
 
78
  delete m_data;
 
79
}
 
80
 
 
81
bool
 
82
TrackerHttp::is_busy() const {
 
83
  return m_data != NULL;
 
84
}
 
85
 
 
86
void
 
87
TrackerHttp::send_state(int state) {
 
88
  close();
 
89
 
 
90
  if (m_parent == NULL)
 
91
    throw internal_error("TrackerHttp::send_state(...) does not have a valid m_parent.");
 
92
 
 
93
  std::stringstream s;
 
94
  s.imbue(std::locale::classic());
 
95
 
 
96
  char hash[61];
 
97
  char localId[61];
 
98
 
 
99
  DownloadInfo* info = m_parent->info();
 
100
 
 
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';
 
103
 
 
104
  s << m_url
 
105
    << (m_dropDeliminator ? '&' : '?')
 
106
    << "info_hash=" << hash
 
107
    << "&peer_id=" << localId;
 
108
 
 
109
  if (m_parent->key())
 
110
    s << "&key=" << std::hex << std::setw(8) << std::setfill('0') << m_parent->key() << std::dec;
 
111
 
 
112
  if (!m_trackerId.empty())
 
113
    s << "&trackerid=" << rak::copy_escape_html(m_trackerId);
 
114
 
 
115
  const rak::socket_address* localAddress = rak::socket_address::cast_from(manager->connection_manager()->local_address());
 
116
 
 
117
  if (localAddress->family() == rak::socket_address::af_inet &&
 
118
      !localAddress->sa_inet()->is_address_any())
 
119
    s << "&ip=" << localAddress->address_str();
 
120
 
 
121
  if (info->is_compact())
 
122
    s << "&compact=1";
 
123
 
 
124
  if (m_parent->numwant() >= 0 && state != DownloadInfo::STOPPED)
 
125
    s << "&numwant=" << m_parent->numwant();
 
126
 
 
127
  if (manager->connection_manager()->listen_port())
 
128
    s << "&port=" << manager->connection_manager()->listen_port();
 
129
 
 
130
  s << "&uploaded=" << info->uploaded_adjusted()
 
131
    << "&downloaded=" << info->completed_adjusted()
 
132
    << "&left=" << info->slot_left()();
 
133
 
 
134
  switch(state) {
 
135
  case DownloadInfo::STARTED:
 
136
    s << "&event=started";
 
137
    break;
 
138
  case DownloadInfo::STOPPED:
 
139
    s << "&event=stopped";
 
140
    break;
 
141
  case DownloadInfo::COMPLETED:
 
142
    s << "&event=completed";
 
143
    break;
 
144
  default:
 
145
    break;
 
146
  }
 
147
 
 
148
  m_data = new std::stringstream();
 
149
 
 
150
  m_get->set_url(s.str());
 
151
  m_get->set_stream(m_data);
 
152
  m_get->set_timeout(2 * 60);
 
153
 
 
154
  m_get->start();
 
155
}
 
156
 
 
157
void
 
158
TrackerHttp::close() {
 
159
  if (m_data == NULL)
 
160
    return;
 
161
 
 
162
  m_get->close();
 
163
  m_get->set_stream(NULL);
 
164
 
 
165
  delete m_data;
 
166
  m_data = NULL;
 
167
}
 
168
 
 
169
TrackerHttp::Type
 
170
TrackerHttp::type() const {
 
171
  return TRACKER_HTTP;
 
172
}
 
173
 
 
174
void
 
175
TrackerHttp::receive_done() {
 
176
  if (m_data == NULL)
 
177
    throw internal_error("TrackerHttp::receive_done() called on an invalid object");
 
178
 
 
179
  DownloadInfo* info = m_parent->info();
 
180
 
 
181
  if (!info->signal_tracker_dump().empty()) {
 
182
    std::string dump = m_data->str();
 
183
 
 
184
    info->signal_tracker_dump().emit(m_get->url(), dump.c_str(), dump.size());
 
185
  }
 
186
 
 
187
  Object b;
 
188
  *m_data >> b;
 
189
 
 
190
  if (m_data->fail())
 
191
    return receive_failed("Could not parse bencoded data");
 
192
 
 
193
  if (!b.is_map())
 
194
    return receive_failed("Root not a bencoded map");
 
195
 
 
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"))
 
201
                         + "\"");
 
202
 
 
203
  if (b.has_key_value("interval"))
 
204
    set_normal_interval(b.get_key_value("interval"));
 
205
  
 
206
  if (b.has_key_value("min interval"))
 
207
    set_min_interval(b.get_key_value("min interval"));
 
208
 
 
209
  if (b.has_key_string("tracker id"))
 
210
    m_trackerId = b.get_key_string("tracker id");
 
211
 
 
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();
 
216
  }
 
217
 
 
218
  if (b.has_key_value("downloaded"))
 
219
    m_scrapeDownloaded = std::max<int64_t>(b.get_key_value("downloaded"), 0);
 
220
 
 
221
  AddressList l;
 
222
 
 
223
  try {
 
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"));
 
228
 
 
229
    else if (b.get_key("peers").is_list())
 
230
      l.parse_address_normal(b.get_key_list("peers"));
 
231
 
 
232
  } catch (bencode_error& e) {
 
233
    return receive_failed(e.what());
 
234
  }
 
235
 
 
236
  close();
 
237
  m_parent->receive_success(this, &l);
 
238
}
 
239
 
 
240
void
 
241
TrackerHttp::receive_failed(std::string msg) {
 
242
  // Does the order matter?
 
243
  close();
 
244
  m_parent->receive_failed(this, msg);
 
245
}
 
246
 
 
247
}