2007-01-07 22:50:11 +00:00
|
|
|
/*
|
2007-05-31 01:29:09 +00:00
|
|
|
* Copyright 2006 Alon Zakai ('Kripken') <kripkensteiner@gmail.com>
|
2007-01-07 22:50:11 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
2007-06-12 16:33:32 +00:00
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the OpenSSL
|
|
|
|
* library.
|
|
|
|
* You must obey the GNU General Public License in all respects for all of
|
|
|
|
* the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
* exception, you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete
|
|
|
|
* this exception statement from your version. If you delete this exception
|
|
|
|
* statement from all source files in the program, then also delete it here.
|
|
|
|
*
|
2007-01-07 22:50:11 +00:00
|
|
|
* Thank You: Some code portions were derived from BSD-licensed work by
|
|
|
|
* Arvid Norberg, and GPL-licensed work by Christophe Dumez
|
|
|
|
*/
|
|
|
|
|
|
|
|
//------------------
|
|
|
|
// TODO:
|
|
|
|
//
|
|
|
|
// The DHT capability requires UDP. We need to check that this port is in fact
|
|
|
|
// open, just like the normal TCP port for bittorrent.
|
|
|
|
//
|
|
|
|
//-----------------
|
|
|
|
#include <Python.h>
|
|
|
|
|
2007-05-25 21:48:33 +00:00
|
|
|
#include <boost/filesystem/exception.hpp>
|
2007-01-07 22:50:11 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2007-05-25 19:59:20 +00:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2007-01-07 22:50:11 +00:00
|
|
|
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/identify_client.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/ip_filter.hpp"
|
2007-05-25 19:59:20 +00:00
|
|
|
#include "libtorrent/upnp.hpp"
|
2007-05-25 21:48:33 +00:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
2007-05-25 19:59:20 +00:00
|
|
|
#include "libtorrent/natpmp.hpp"
|
2007-05-23 14:23:28 +00:00
|
|
|
#include "libtorrent/extensions/metadata_transfer.hpp"
|
|
|
|
#include "libtorrent/extensions/ut_pex.hpp"
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-23 02:39:50 +00:00
|
|
|
using namespace boost::filesystem;
|
2007-01-07 22:50:11 +00:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2007-05-25 20:11:54 +00:00
|
|
|
//----------------
|
2007-01-07 22:50:11 +00:00
|
|
|
// CONSTANTS
|
2007-05-25 20:11:54 +00:00
|
|
|
//----------------
|
2007-01-07 22:50:11 +00:00
|
|
|
|
|
|
|
#ifdef AMD64
|
|
|
|
#define python_long int
|
|
|
|
#else
|
|
|
|
#define python_long long
|
|
|
|
#endif
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
#define EVENT_NULL 0
|
|
|
|
#define EVENT_FINISHED 1
|
|
|
|
#define EVENT_PEER_ERROR 2
|
|
|
|
#define EVENT_INVALID_REQUEST 3
|
|
|
|
#define EVENT_FILE_ERROR 4
|
|
|
|
#define EVENT_HASH_FAILED_ERROR 5
|
|
|
|
#define EVENT_PEER_BAN_ERROR 6
|
|
|
|
#define EVENT_FASTRESUME_REJECTED_ERROR 8
|
|
|
|
#define EVENT_TRACKER 9
|
|
|
|
#define EVENT_OTHER 10
|
|
|
|
|
|
|
|
#define STATE_QUEUED 0
|
|
|
|
#define STATE_CHECKING 1
|
|
|
|
#define STATE_CONNECTING 2
|
|
|
|
#define STATE_DOWNLOADING_META 3
|
|
|
|
#define STATE_DOWNLOADING 4
|
|
|
|
#define STATE_FINISHED 5
|
|
|
|
#define STATE_SEEDING 6
|
|
|
|
#define STATE_ALLOCATING 7
|
|
|
|
|
|
|
|
#define DHT_ROUTER_PORT 6881
|
2007-01-07 22:50:11 +00:00
|
|
|
|
|
|
|
//-----------------
|
|
|
|
// TYPES
|
|
|
|
//-----------------
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
typedef long unique_ID_t;
|
2007-01-07 22:50:11 +00:00
|
|
|
typedef std::vector<bool> filter_out_t;
|
2007-05-31 01:29:09 +00:00
|
|
|
typedef std::string torrent_name_t;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
struct torrent_t
|
|
|
|
{
|
|
|
|
torrent_handle handle;
|
|
|
|
unique_ID_t unique_ID;
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<torrent_t> torrents_t;
|
|
|
|
typedef torrents_t::iterator torrents_t_iterator;
|
|
|
|
|
|
|
|
//---------------------------
|
|
|
|
// MODULE-GLOBAL VARIABLES
|
|
|
|
//---------------------------
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long M_unique_counter = 0;
|
|
|
|
session_settings *M_settings = NULL;
|
2007-06-07 03:57:19 +00:00
|
|
|
pe_settings *M_pe_settings = NULL;
|
2007-05-31 01:29:09 +00:00
|
|
|
session *M_ses = NULL;
|
|
|
|
PyObject *M_constants = NULL;
|
|
|
|
ip_filter *M_the_filter = NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
torrents_t *M_torrents = NULL;
|
|
|
|
|
|
|
|
//------------------------
|
|
|
|
// Exception types & macro
|
|
|
|
//------------------------
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
static PyObject *DelugeError = NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *InvalidEncodingError = NULL;
|
|
|
|
static PyObject *FilesystemError = NULL;
|
|
|
|
static PyObject *DuplicateTorrentError = NULL;
|
|
|
|
static PyObject *InvalidTorrentError = NULL;
|
|
|
|
|
|
|
|
#define RAISE_PTR(e,s) { printf("Raising error: %s\r\n", s); PyErr_SetString(e, s); return NULL; }
|
|
|
|
#define RAISE_INT(e,s) { printf("Raising error: %s\r\n", s); PyErr_SetString(e, s); return -1; }
|
|
|
|
|
|
|
|
//---------------------
|
|
|
|
// Internal functions
|
|
|
|
//---------------------
|
|
|
|
|
|
|
|
bool empty_name_check(const std::string & name)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
return 1;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
long handle_exists(torrent_handle &handle)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
for (unsigned long i = 0; i < M_torrents->size(); i++)
|
|
|
|
if ((*M_torrents)[i].handle == handle)
|
|
|
|
return 1;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return 0;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
long get_torrent_index(torrent_handle &handle)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
for (unsigned long i = 0; i < M_torrents->size(); i++)
|
|
|
|
if ((*M_torrents)[i].handle == handle)
|
|
|
|
{
|
|
|
|
// printf("Found: %li\r\n", i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
RAISE_INT(DelugeError, "Handle not found.");
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
long get_index_from_unique_ID(long unique_ID)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
assert(M_handles->size() == M_unique_IDs->size());
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
for (unsigned long i = 0; i < M_torrents->size(); i++)
|
|
|
|
if ((*M_torrents)[i].unique_ID == unique_ID)
|
|
|
|
return i;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
RAISE_INT(DelugeError, "No such unique_ID.");
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
|
|
|
long internal_add_torrent(std::string const& torrent_name,
|
|
|
|
float preferred_ratio,
|
|
|
|
bool compact_mode,
|
|
|
|
boost::filesystem::path const& save_path)
|
2007-01-07 22:50:11 +00:00
|
|
|
{
|
2007-05-23 02:39:50 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
std::ifstream in(torrent_name.c_str(), std::ios_base::binary);
|
|
|
|
in.unsetf(std::ios_base::skipws);
|
|
|
|
entry e;
|
|
|
|
e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_info t(e);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
entry resume_data;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
s << torrent_name << ".fastresume";
|
|
|
|
boost::filesystem::ifstream resumeFile(s.str(), std::ios_base::binary);
|
|
|
|
resumeFile.unsetf(std::ios_base::skipws);
|
|
|
|
resume_data = bdecode(std::istream_iterator<char>(resumeFile),
|
|
|
|
std::istream_iterator<char>());
|
|
|
|
}
|
|
|
|
catch (invalid_encoding&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (boost::filesystem::filesystem_error&) {}
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
// Create new torrent object
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_t new_torrent;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_handle h = M_ses->add_torrent(t, save_path, resume_data, compact_mode, 16 * 1024);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
// h.set_max_connections(60); // at some point we should use this
|
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
|
|
|
new_torrent.handle = h;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
new_torrent.unique_ID = M_unique_counter;
|
|
|
|
M_unique_counter++;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_torrents->push_back(new_torrent);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return (new_torrent.unique_ID);
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
void internal_remove_torrent(long index)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
assert(index < M_torrents->size());
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_handle& h = M_torrents->at(index).handle;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_ses->remove_torrent(h);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrents_t_iterator it = M_torrents->begin() + index;
|
|
|
|
M_torrents->erase(it);
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
long get_peer_index(tcp::endpoint addr, std::vector<peer_info> const& peers)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
long index = -1;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
for (unsigned long i = 0; i < peers.size(); i++)
|
|
|
|
if (peers[i].ip == addr)
|
|
|
|
index = i;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return index;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
// The following function contains code by Christophe Dumez and Arvid Norberg
|
2007-05-31 01:29:09 +00:00
|
|
|
void internal_add_files(torrent_info& t,
|
|
|
|
boost::filesystem::path const& p,
|
|
|
|
boost::filesystem::path const& l)
|
2007-01-07 22:50:11 +00:00
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
// change default checker, perhaps?
|
|
|
|
boost::filesystem::path f(p / l);
|
|
|
|
if (is_directory(f))
|
|
|
|
{
|
|
|
|
for (boost::filesystem::directory_iterator i(f), end; i != end; ++i)
|
|
|
|
internal_add_files(t, p, l / i->leaf());
|
|
|
|
} else
|
|
|
|
t.add_file(l, file_size(f));
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
long count_DHT_peers(entry &state)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
long num_peers = 0;
|
|
|
|
entry *nodes = state.find_key("nodes");
|
|
|
|
if (nodes)
|
|
|
|
{
|
|
|
|
entry::list_type &peers = nodes->list();
|
|
|
|
entry::list_type::const_iterator i;
|
|
|
|
i = peers.begin();
|
|
|
|
|
|
|
|
while (i != peers.end())
|
|
|
|
{
|
|
|
|
num_peers++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_peers;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=====================
|
|
|
|
// External functions
|
|
|
|
//=====================
|
|
|
|
|
|
|
|
static PyObject *torrent_pre_init(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "OOOOO", &DelugeError,
|
|
|
|
&InvalidEncodingError,
|
|
|
|
&FilesystemError,
|
|
|
|
&DuplicateTorrentError,
|
|
|
|
&InvalidTorrentError))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_init(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
printf("deluge_core; using libtorrent %s. Compiled with NDEBUG value: %d\r\n",
|
|
|
|
LIBTORRENT_VERSION,
|
|
|
|
NDEBUG);
|
|
|
|
|
|
|
|
// Tell Boost that we are on *NIX, so bloody '.'s are ok inside a directory name!
|
|
|
|
boost::filesystem::path::default_name_check(empty_name_check);
|
|
|
|
|
|
|
|
char *client_ID, *user_agent;
|
|
|
|
python_long v1,v2,v3,v4;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "siiiis", &client_ID, &v1, &v2, &v3, &v4, &user_agent))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
M_settings = new session_settings;
|
|
|
|
M_ses = new session(fingerprint(client_ID, v1, v2, v3, v4));
|
|
|
|
|
|
|
|
M_torrents = new torrents_t;
|
|
|
|
M_torrents->reserve(10); // pretty cheap, just 10
|
|
|
|
|
|
|
|
// Init values
|
|
|
|
|
|
|
|
M_settings->user_agent = std::string(user_agent);
|
|
|
|
|
|
|
|
M_ses->set_max_half_open_connections(-1);
|
|
|
|
M_ses->set_download_rate_limit(-1);
|
|
|
|
M_ses->set_upload_rate_limit(-1);
|
|
|
|
|
|
|
|
M_ses->set_settings(*M_settings);
|
|
|
|
M_ses->set_severity_level(alert::debug);
|
|
|
|
|
|
|
|
M_ses->add_extension(&libtorrent::create_metadata_plugin);
|
|
|
|
|
|
|
|
M_constants = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i,s:i}",
|
|
|
|
"EVENT_NULL", EVENT_NULL,
|
|
|
|
"EVENT_FINISHED", EVENT_FINISHED,
|
|
|
|
"EVENT_PEER_ERROR", EVENT_PEER_ERROR,
|
|
|
|
"EVENT_INVALID_REQUEST", EVENT_INVALID_REQUEST,
|
|
|
|
"EVENT_FILE_ERROR", EVENT_FILE_ERROR,
|
|
|
|
"EVENT_HASH_FAILED_ERROR", EVENT_HASH_FAILED_ERROR,
|
|
|
|
"EVENT_PEER_BAN_ERROR", EVENT_PEER_BAN_ERROR,
|
|
|
|
"EVENT_FASTRESUME_REJECTED_ERROR", EVENT_FASTRESUME_REJECTED_ERROR,
|
|
|
|
"EVENT_TRACKER", EVENT_TRACKER,
|
|
|
|
"EVENT_OTHER", EVENT_OTHER,
|
|
|
|
"STATE_QUEUED", STATE_QUEUED,
|
|
|
|
"STATE_CHECKING", STATE_CHECKING,
|
|
|
|
"STATE_CONNECTING", STATE_CONNECTING,
|
|
|
|
"STATE_DOWNLOADING_META", STATE_DOWNLOADING_META,
|
|
|
|
"STATE_DOWNLOADING", STATE_DOWNLOADING,
|
|
|
|
"STATE_FINISHED", STATE_FINISHED,
|
|
|
|
"STATE_SEEDING", STATE_SEEDING,
|
|
|
|
"STATE_ALLOCATING", STATE_ALLOCATING);
|
|
|
|
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *torrent_quit(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
M_settings->stop_tracker_timeout = 5;
|
|
|
|
M_ses->set_settings(*M_settings);
|
|
|
|
printf("core: removing torrents...\r\n");
|
|
|
|
delete M_torrents;
|
2007-06-09 01:32:54 +00:00
|
|
|
printf("core: removing settings...\r\n");
|
|
|
|
delete M_settings;
|
2007-06-08 22:25:53 +00:00
|
|
|
printf("core: shutting down session...\r\n");
|
|
|
|
delete M_ses; // 100% CPU...
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_DECREF(M_constants);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
printf("core shut down.\r\n");
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *torrent_save_fastresume(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
const char *torrent_name;
|
|
|
|
if (!PyArg_ParseTuple(args, "is", &unique_ID, &torrent_name))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_handle& h = M_torrents->at(index).handle;
|
|
|
|
// For valid torrents, save fastresume data
|
|
|
|
if (h.is_valid() && h.has_metadata())
|
|
|
|
{
|
|
|
|
h.pause();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
entry data = h.write_resume_data();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
std::stringstream s;
|
|
|
|
s << torrent_name << ".fastresume";
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
boost::filesystem::ofstream out(s.str(), std::ios_base::binary);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
out.unsetf(std::ios_base::skipws);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
bencode(std::ostream_iterator<char>(out), data);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
h.resume();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
|
|
|
} else
|
|
|
|
RAISE_PTR(DelugeError, "Invalid handle or no metadata for fastresume.");
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_set_max_half_open(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long arg;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &arg))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_ses->set_max_half_open_connections(arg);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_set_download_rate_limit(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long arg;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &arg))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
// printf("Capping download to %d bytes per second\r\n", (int)arg);
|
|
|
|
M_ses->set_download_rate_limit(arg);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_set_upload_rate_limit(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long arg;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &arg))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
// printf("Capping upload to %d bytes per second\r\n", (int)arg);
|
|
|
|
M_ses->set_upload_rate_limit(arg);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_set_listen_on(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
PyObject *port_vec;
|
|
|
|
if (!PyArg_ParseTuple(args, "O", &port_vec))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_ses->listen_on(std::make_pair( PyInt_AsLong(PyList_GetItem(port_vec, 0)),
|
|
|
|
PyInt_AsLong(PyList_GetItem(port_vec, 1))), "");
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_is_listening(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
long ret = (M_ses->is_listening() != 0);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return Py_BuildValue("i", ret);
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_listening_port(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
return Py_BuildValue("i", (python_long)M_ses->listen_port());
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_set_max_uploads(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long max_up;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &max_up))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_ses->set_max_uploads(max_up);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_set_max_connections(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long max_conn;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &max_conn))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
// printf("Setting max connections: %d\r\n", max_conn);
|
|
|
|
M_ses->set_max_connections(max_conn);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_add_torrent(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
const char *name, *save_dir;
|
|
|
|
python_long compact;
|
|
|
|
if (!PyArg_ParseTuple(args, "ssi", &name, &save_dir, &compact))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
boost::filesystem::path save_dir_2 (save_dir, empty_name_check);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
long ret = internal_add_torrent(name, 0, compact, save_dir_2);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return Py_BuildValue("i", ret);
|
|
|
|
}
|
|
|
|
catch (invalid_encoding&)
|
|
|
|
{ RAISE_PTR(InvalidEncodingError, ""); }
|
|
|
|
catch (invalid_torrent_file&)
|
|
|
|
{ RAISE_PTR(InvalidTorrentError, ""); }
|
|
|
|
catch (boost::filesystem::filesystem_error&)
|
|
|
|
{ RAISE_PTR(FilesystemError, ""); }
|
|
|
|
catch (duplicate_torrent&)
|
|
|
|
{ RAISE_PTR(DuplicateTorrentError, "libtorrent reports this is a duplicate torrent"); }
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_remove_torrent(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &unique_ID))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
internal_remove_torrent(index);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_get_num_torrents(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
return Py_BuildValue("i", M_torrents->size());
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_reannounce(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &unique_ID))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_torrents->at(index).handle.force_reannounce();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_pause(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &unique_ID))
|
|
|
|
return NULL;
|
2007-06-09 19:43:07 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_torrents->at(index).handle.pause();
|
2007-06-09 19:43:07 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_resume(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &unique_ID))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_torrents->at(index).handle.resume();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_get_torrent_state(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &unique_ID))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
torrent_t &t = M_torrents->at(index);
|
|
|
|
torrent_status s = t.handle.status();
|
|
|
|
const torrent_info &i = t.handle.get_torrent_info();
|
|
|
|
|
|
|
|
std::vector<peer_info> peers;
|
|
|
|
t.handle.get_peer_info(peers);
|
|
|
|
|
2007-06-05 08:53:04 +00:00
|
|
|
long connected_seeds = 0;
|
2007-06-09 02:44:03 +00:00
|
|
|
long connected_peers = 0;
|
2007-06-09 19:43:07 +00:00
|
|
|
long total_seeds = 0;
|
|
|
|
long total_peers = 0;
|
|
|
|
|
2007-06-09 02:44:03 +00:00
|
|
|
for (unsigned long i = 0; i < peers.size(); i++) {
|
2007-06-09 19:43:07 +00:00
|
|
|
|
2007-06-14 13:49:57 +00:00
|
|
|
connected_peers = s.num_peers - s.num_seeds;
|
|
|
|
|
|
|
|
connected_seeds = s.num_seeds;
|
|
|
|
|
|
|
|
total_seeds = s.num_complete != -1? s.num_complete : connected_seeds;
|
2007-06-14 12:45:00 +00:00
|
|
|
|
2007-06-14 13:49:57 +00:00
|
|
|
total_peers = s.num_incomplete != -1? s.num_incomplete : connected_peers;
|
2007-06-09 02:44:03 +00:00
|
|
|
}
|
2007-06-05 08:53:04 +00:00
|
|
|
|
2007-06-08 22:09:00 +00:00
|
|
|
return Py_BuildValue("{s:s,s:i,s:i,s:l,s:l,s:f,s:f,s:f,s:L,s:L,s:b,s:s,s:s,s:f,s:L,s:L,s:l,s:i,s:i,s:L,s:L,s:i,s:l,s:l,s:b,s:b,s:L,s:L,s:L}",
|
2007-05-31 09:12:41 +00:00
|
|
|
"name", t.handle.get_torrent_info().name().c_str(),
|
|
|
|
"num_files", t.handle.get_torrent_info().num_files(),
|
|
|
|
"state", s.state,
|
2007-06-05 08:53:04 +00:00
|
|
|
"num_peers", connected_peers,
|
|
|
|
"num_seeds", connected_seeds,
|
2007-05-31 09:12:41 +00:00
|
|
|
"distributed_copies", s.distributed_copies,
|
|
|
|
"download_rate", s.download_rate,
|
|
|
|
"upload_rate", s.upload_rate,
|
2007-06-08 22:09:00 +00:00
|
|
|
"total_download", s.total_download,
|
|
|
|
"total_upload", s.total_upload,
|
2007-05-31 09:12:41 +00:00
|
|
|
"tracker_ok", !s.current_tracker.empty(),
|
|
|
|
"next_announce", boost::posix_time::to_simple_string(s.next_announce).c_str(),
|
|
|
|
"tracker", s.current_tracker.c_str(),
|
2007-06-08 22:09:00 +00:00
|
|
|
"progress", s.progress,
|
|
|
|
"total_payload_download", s.total_payload_download,
|
|
|
|
"total_payload_upload", s.total_payload_upload,
|
|
|
|
"pieces", long(s.pieces), // this is really a std::vector<bool>*
|
|
|
|
"pieces_done", s.num_pieces,
|
|
|
|
"block_size", s.block_size,
|
|
|
|
"total_size", i.total_size(),
|
|
|
|
"piece_length", i.piece_length(),
|
|
|
|
"num_pieces", i.num_pieces(),
|
2007-06-14 12:45:00 +00:00
|
|
|
"total_peers", total_peers,
|
|
|
|
"total_seeds", total_seeds,
|
2007-06-08 22:09:00 +00:00
|
|
|
"is_paused", t.handle.is_paused(),
|
|
|
|
"is_seed", t.handle.is_seed(),
|
2007-06-14 12:45:00 +00:00
|
|
|
"total_done", s.total_done,
|
2007-06-08 22:09:00 +00:00
|
|
|
"total_wanted", s.total_wanted,
|
|
|
|
"total_wanted_done", s.total_wanted_done);
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *torrent_pop_event(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
std::auto_ptr<alert> a;
|
|
|
|
|
|
|
|
a = M_ses->pop_alert();
|
|
|
|
|
|
|
|
alert *popped_alert = a.get();
|
|
|
|
|
|
|
|
if (!popped_alert)
|
|
|
|
{
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
|
|
|
} else if (dynamic_cast<torrent_finished_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<torrent_finished_alert*>(popped_alert))->handle;
|
|
|
|
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i}", "event_type", EVENT_FINISHED,
|
|
|
|
"unique_ID",
|
|
|
|
M_torrents->at(index).unique_ID);
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<peer_error_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
peer_id peer_ID = (dynamic_cast<peer_error_alert*>(popped_alert))->pid;
|
|
|
|
std::string peer_IP =
|
|
|
|
(dynamic_cast<peer_error_alert*>(popped_alert))->ip.address().to_string();
|
|
|
|
|
|
|
|
return Py_BuildValue("{s:i,s:s,s:s,s:s}", "event_type", EVENT_PEER_ERROR,
|
|
|
|
"client_ID", identify_client(peer_ID).c_str(),
|
|
|
|
"ip", peer_IP.c_str(),
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
} else if (dynamic_cast<invalid_request_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
peer_id peer_ID = (dynamic_cast<invalid_request_alert*>(popped_alert))->pid;
|
|
|
|
|
|
|
|
return Py_BuildValue("{s:i,s:s,s:s}",
|
|
|
|
"event_type", EVENT_INVALID_REQUEST,
|
|
|
|
"client_ID", identify_client(peer_ID).c_str(),
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
} else if (dynamic_cast<file_error_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<file_error_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:s}",
|
|
|
|
"event_type", EVENT_FILE_ERROR,
|
|
|
|
"unique_ID", M_torrents->at(index).unique_ID,
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<hash_failed_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<hash_failed_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:i,s:s}",
|
|
|
|
"event_type", EVENT_HASH_FAILED_ERROR,
|
|
|
|
"unique_ID", M_torrents->at(index).unique_ID,
|
|
|
|
"piece_index",
|
|
|
|
long((dynamic_cast<hash_failed_alert*>(popped_alert))->piece_index),
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<peer_ban_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<peer_ban_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
std::string peer_IP = (dynamic_cast<peer_ban_alert*>(popped_alert))->ip.address().to_string();
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:s,s:s}",
|
|
|
|
"event_type", EVENT_PEER_BAN_ERROR,
|
|
|
|
"unique_ID", M_torrents->at(index).unique_ID,
|
|
|
|
"ip", peer_IP.c_str(),
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<fastresume_rejected_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<fastresume_rejected_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:s}",
|
|
|
|
"event_type", EVENT_FASTRESUME_REJECTED_ERROR,
|
|
|
|
"unique_ID", M_torrents->at(index).unique_ID,
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<tracker_announce_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<tracker_announce_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:s,s:s}",
|
|
|
|
"event_type", EVENT_TRACKER,
|
|
|
|
"unique_ID",
|
|
|
|
M_torrents->at(index).unique_ID,
|
|
|
|
"tracker_status", "Announce sent",
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<tracker_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<tracker_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:s,s:s}",
|
|
|
|
"event_type", EVENT_TRACKER,
|
|
|
|
"unique_ID",
|
|
|
|
M_torrents->at(index).unique_ID,
|
|
|
|
"tracker_status", "Bad response (status code=?)",
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<tracker_reply_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<tracker_reply_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:s,s:s}",
|
|
|
|
"event_type", EVENT_TRACKER,
|
|
|
|
"unique_ID",
|
|
|
|
M_torrents->at(index).unique_ID,
|
|
|
|
"tracker_status", "Announce succeeded",
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
} else if (dynamic_cast<tracker_warning_alert*>(popped_alert))
|
|
|
|
{
|
|
|
|
torrent_handle handle = (dynamic_cast<tracker_warning_alert*>(popped_alert))->handle;
|
|
|
|
long index = get_torrent_index(handle);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (handle_exists(handle))
|
|
|
|
return Py_BuildValue("{s:i,s:i,s:s,s:s}",
|
|
|
|
"event_type", EVENT_TRACKER,
|
|
|
|
"unique_ID",
|
|
|
|
M_torrents->at(index).unique_ID,
|
|
|
|
"tracker_status", "Warning in response",
|
|
|
|
"message", a->msg().c_str());
|
|
|
|
else
|
|
|
|
{ Py_INCREF(Py_None); return Py_None; }
|
|
|
|
}
|
|
|
|
|
|
|
|
return Py_BuildValue("{s:i,s:s}", "event_type", EVENT_OTHER,
|
|
|
|
"message", a->msg().c_str() );
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_get_session_info(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
session_status s = M_ses->status();
|
|
|
|
|
|
|
|
return Py_BuildValue("{s:l,s:f,s:f,s:f,s:f,s:l}",
|
|
|
|
"has_incoming_connections", long(s.has_incoming_connections),
|
|
|
|
"upload_rate", float(s.upload_rate),
|
|
|
|
"download_rate", float(s.download_rate),
|
|
|
|
"payload_upload_rate", float(s.payload_upload_rate),
|
|
|
|
"payload_download_rate", float(s.payload_download_rate),
|
|
|
|
"num_peers", long(s.num_peers));
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_get_peer_info(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &unique_ID))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
std::vector<peer_info> peers;
|
|
|
|
M_torrents->at(index).handle.get_peer_info(peers);
|
|
|
|
|
|
|
|
PyObject *peer_info;
|
|
|
|
PyObject *ret = PyTuple_New(peers.size());
|
|
|
|
PyObject *curr_piece, *py_pieces;
|
|
|
|
|
|
|
|
for (unsigned long i = 0; i < peers.size(); i++)
|
|
|
|
{
|
|
|
|
std::vector<bool> &pieces = peers[i].pieces;
|
|
|
|
unsigned long pieces_had = 0;
|
|
|
|
|
|
|
|
py_pieces = PyTuple_New(pieces.size());
|
|
|
|
|
|
|
|
for (unsigned long piece = 0; piece < pieces.size(); piece++)
|
|
|
|
{
|
|
|
|
if (pieces[piece])
|
|
|
|
pieces_had++;
|
|
|
|
|
|
|
|
curr_piece = Py_BuildValue("i", long(pieces[piece]));
|
|
|
|
PyTuple_SetItem(py_pieces, piece, curr_piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
peer_info = Py_BuildValue(
|
2007-06-09 02:44:03 +00:00
|
|
|
"{s:f,s:L,s:f,s:L,s:i,s:i,s:b,s:b,s:b,s:b,s:b,s:b,s:b,s:b,s:b,s:s,s:b,s:s,s:f,s:O,s:b,s:b}",
|
|
|
|
"download_speed", peers[i].down_speed,
|
|
|
|
"total_download", peers[i].total_download,
|
|
|
|
"upload_speed", peers[i].up_speed,
|
|
|
|
"total_upload", peers[i].total_upload,
|
|
|
|
"download_queue_length", peers[i].download_queue_length,
|
|
|
|
"upload_queue_length", peers[i].upload_queue_length,
|
|
|
|
"is_interesting", ((peers[i].flags & peer_info::interesting) != 0),
|
|
|
|
"is_choked", ((peers[i].flags & peer_info::choked) != 0),
|
|
|
|
"is_remote_interested", ((peers[i].flags & peer_info::remote_interested) != 0),
|
|
|
|
"is_remote_choked", ((peers[i].flags & peer_info::remote_choked) != 0),
|
|
|
|
"supports_extensions", ((peers[i].flags & peer_info::supports_extensions) != 0),
|
|
|
|
"is_local_connection", ((peers[i].flags & peer_info::local_connection) != 0),
|
|
|
|
"is_awaiting_handshake", ((peers[i].flags & peer_info::handshake) != 0),
|
|
|
|
"is_connecting", ((peers[i].flags & peer_info::connecting) != 0),
|
|
|
|
"is_queued", ((peers[i].flags & peer_info::queued) != 0),
|
2007-06-05 09:08:24 +00:00
|
|
|
"client", peers[i].client.c_str(),
|
2007-06-09 02:44:03 +00:00
|
|
|
"is_seed", ((peers[i].flags & peer_info::seed) != 0),
|
2007-06-05 09:08:24 +00:00
|
|
|
"ip", peers[i].ip.address().to_string().c_str(),
|
|
|
|
"peer_has", float(float(pieces_had)*100.0/pieces.size()),
|
|
|
|
"pieces", py_pieces,
|
2007-06-09 02:44:03 +00:00
|
|
|
"rc4_encrypted", ((peers[i].flags & peer_info::rc4_encrypted) != 0),
|
|
|
|
"plaintext_encrypted", ((peers[i].flags & peer_info::plaintext_encrypted) != 0)
|
2007-05-31 01:29:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Py_DECREF(py_pieces); // Assuming the previous line does NOT steal the ref, then this is
|
|
|
|
// needed!
|
|
|
|
|
|
|
|
PyTuple_SetItem(ret, i, peer_info);
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *torrent_get_file_info(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &unique_ID))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
std::vector<PyObject *> temp_files;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
PyObject *file_info;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
std::vector<float> progresses;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_t &t = M_torrents->at(index);
|
|
|
|
t.handle.file_progress(progresses);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_info::file_iterator start =
|
|
|
|
t.handle.get_torrent_info().begin_files();
|
|
|
|
torrent_info::file_iterator end =
|
|
|
|
t.handle.get_torrent_info().end_files();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long fileIndex = 0;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
for(torrent_info::file_iterator i = start; i != end; ++i)
|
|
|
|
{
|
|
|
|
file_entry const &currFile = (*i);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
file_info = Py_BuildValue(
|
|
|
|
"{s:s,s:d,s:d,s:f}",
|
|
|
|
"path", currFile.path.string().c_str(),
|
|
|
|
"offset", double(currFile.offset),
|
|
|
|
"size", double(currFile.size),
|
|
|
|
"progress", progresses[i - start]*100.0
|
|
|
|
);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
fileIndex++;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
temp_files.push_back(file_info);
|
|
|
|
};
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
PyObject *ret = PyTuple_New(temp_files.size());
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
for (unsigned long i = 0; i < temp_files.size(); i++)
|
|
|
|
PyTuple_SetItem(ret, i, temp_files[i]);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return ret;
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *torrent_set_filter_out(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long unique_ID;
|
|
|
|
PyObject *filter_out_object;
|
|
|
|
if (!PyArg_ParseTuple(args, "iO", &unique_ID, &filter_out_object))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
torrent_t &t = M_torrents->at(index);
|
|
|
|
long num_files = t.handle.get_torrent_info().num_files();
|
|
|
|
assert(PyList_Size(filter_out_object) == num_files);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
filter_out_t filter_out(num_files);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
for (long i = 0; i < num_files; i++)
|
|
|
|
{
|
|
|
|
filter_out.at(i) =
|
|
|
|
PyInt_AsLong(PyList_GetItem(filter_out_object, i));
|
|
|
|
};
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
t.handle.filter_files(filter_out);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
/*static PyObject *torrent_get_unique_IDs(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
PyObject *ret = PyTuple_New(M_torrents.size());
|
|
|
|
PyObject *temp;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
for (unsigned long i = 0; i < M_torrents.size(); i++)
|
|
|
|
{
|
|
|
|
temp = Py_BuildValue("i", M_torrents->at(i).unique_ID)
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
PyTuple_SetItem(ret, i, temp);
|
|
|
|
};
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return ret;
|
2007-01-07 22:50:11 +00:00
|
|
|
};*/
|
|
|
|
|
|
|
|
static PyObject *torrent_constants(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(M_constants); return M_constants;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_start_DHT(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
const char *DHT_path;
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &DHT_path))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// printf("Loading DHT state from %s\r\n", DHT_path);
|
|
|
|
|
|
|
|
boost::filesystem::path tempPath(DHT_path, empty_name_check);
|
|
|
|
boost::filesystem::ifstream DHT_state_file(tempPath, std::ios_base::binary);
|
|
|
|
DHT_state_file.unsetf(std::ios_base::skipws);
|
|
|
|
|
|
|
|
entry DHT_state;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DHT_state = bdecode(std::istream_iterator<char>(DHT_state_file),
|
|
|
|
std::istream_iterator<char>());
|
|
|
|
M_ses->start_dht(DHT_state);
|
|
|
|
// printf("DHT state recovered.\r\n");
|
|
|
|
|
|
|
|
// // Print out the state data from the FILE (not the session!)
|
|
|
|
// printf("Number of DHT peers in recovered state: %ld\r\n", count_DHT_peers(DHT_state));
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
|
|
|
printf("No DHT file to resume\r\n");
|
|
|
|
M_ses->start_dht();
|
|
|
|
}
|
|
|
|
|
|
|
|
M_ses->add_dht_router(std::make_pair(std::string("router.bittorrent.com"),
|
|
|
|
DHT_ROUTER_PORT));
|
|
|
|
M_ses->add_dht_router(std::make_pair(std::string("router.utorrent.com"),
|
|
|
|
DHT_ROUTER_PORT));
|
|
|
|
M_ses->add_dht_router(std::make_pair(std::string("router.bitcomet.com"),
|
|
|
|
DHT_ROUTER_PORT));
|
|
|
|
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_stop_DHT(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
const char *DHT_path;
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &DHT_path))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
// printf("Saving DHT state to %s\r\n", DHT_path);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
boost::filesystem::path tempPath = boost::filesystem::path(DHT_path, empty_name_check);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
entry DHT_state = M_ses->dht_state();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
// printf("Number of DHT peers in state, saving: %ld\r\n", count_DHT_peers(DHT_state));
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
boost::filesystem::ofstream out(tempPath, std::ios_base::binary);
|
|
|
|
out.unsetf(std::ios_base::skipws);
|
|
|
|
bencode(std::ostream_iterator<char>(out), DHT_state);
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
printf("An error occured in saving DHT\r\n");
|
|
|
|
std::cerr << e.what() << "\n";
|
|
|
|
}
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
static PyObject *torrent_get_DHT_info(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
entry DHT_state = M_ses->dht_state();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return Py_BuildValue("l", python_long(count_DHT_peers(DHT_state)));
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
/*
|
|
|
|
// DHT_state.print(cout);
|
|
|
|
entry *nodes = DHT_state.find_key("nodes");
|
|
|
|
if (!nodes)
|
|
|
|
return Py_BuildValue("l", -1); // No nodes - we are just starting up...
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
entry::list_type &peers = nodes->list();
|
|
|
|
entry::list_type::const_iterator i;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
python_long num_peers = 0;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
i = peers.begin();
|
|
|
|
while (i != peers.end())
|
|
|
|
{
|
|
|
|
num_peers++;
|
|
|
|
i++;
|
|
|
|
}
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
return Py_BuildValue("l", num_peers);
|
|
|
|
*/
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
// Create Torrents: call with something like:
|
|
|
|
// create_torrent("mytorrent.torrent", "directory or file to make a torrent out of",
|
|
|
|
// "tracker1\ntracker2\ntracker3", "no comment", 256, "Deluge");
|
|
|
|
// That makes a torrent with pieces of 256K, with "Deluge" as the creator string.
|
|
|
|
//
|
|
|
|
// The following function contains code by Christophe Dumez and Arvid Norberg
|
|
|
|
static PyObject *torrent_create_torrent(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
|
|
|
path::default_name_check(no_check);
|
|
|
|
|
|
|
|
char *destination, *comment, *creator_str, *input, *trackers;
|
|
|
|
python_long piece_size;
|
|
|
|
if (!PyArg_ParseTuple(args, "ssssis",
|
|
|
|
&destination, &input, &trackers, &comment, &piece_size, &creator_str))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
piece_size = piece_size * 1024;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
torrent_info t;
|
|
|
|
boost::filesystem::path full_path = complete(boost::filesystem::path(input));
|
|
|
|
boost::filesystem::ofstream out(complete(boost::filesystem::path(destination)), std::ios_base::binary);
|
|
|
|
|
|
|
|
internal_add_files(t, full_path.branch_path(), full_path.leaf());
|
|
|
|
t.set_piece_size(piece_size);
|
|
|
|
|
|
|
|
file_pool fp;
|
|
|
|
boost::scoped_ptr<storage_interface> st(
|
|
|
|
default_storage_constructor(t, full_path.branch_path(), fp));
|
|
|
|
|
|
|
|
std::string stdTrackers(trackers);
|
|
|
|
unsigned long index = 0, next = stdTrackers.find("\n");
|
|
|
|
while (1 == 1)
|
|
|
|
{
|
|
|
|
t.add_tracker(stdTrackers.substr(index, next-index));
|
|
|
|
index = next + 1;
|
|
|
|
if (next >= stdTrackers.length())
|
|
|
|
break;
|
|
|
|
next = stdTrackers.find("\n", index);
|
|
|
|
if (next == std::string::npos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int num = t.num_pieces();
|
|
|
|
std::vector<char> buf(piece_size);
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
|
|
|
st->read(&buf[0], i, 0, t.piece_size(i));
|
|
|
|
hasher h(&buf[0], t.piece_size(i));
|
|
|
|
t.set_hash(i, h.final());
|
|
|
|
}
|
|
|
|
|
|
|
|
t.set_creator(creator_str);
|
|
|
|
t.set_comment(comment);
|
|
|
|
|
|
|
|
entry e = t.create_torrent();
|
|
|
|
bencode(std::ostream_iterator<char>(out), e);
|
|
|
|
return Py_BuildValue("l", 1);
|
|
|
|
} catch (std::exception& e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.what() << "\n";
|
|
|
|
// return Py_BuildValue("l", 0);
|
|
|
|
RAISE_PTR(DelugeError, e.what());
|
|
|
|
}
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
static PyObject *torrent_reset_IP_filter(PyObject *self, PyObject *args)
|
2007-01-07 22:50:11 +00:00
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
// Remove existing filter, if there is one
|
|
|
|
if (M_the_filter != NULL)
|
|
|
|
delete M_the_filter;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
M_the_filter = new ip_filter();
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
M_ses->set_ip_filter(*M_the_filter);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
|
|
|
}
|
2007-01-07 22:50:11 +00:00
|
|
|
|
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
static PyObject *torrent_add_range_to_IP_filter(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if (M_the_filter == NULL) {
|
|
|
|
RAISE_PTR(DelugeError, "No filter defined, use reset_IP_filter");
|
|
|
|
}
|
2007-06-12 03:32:49 +00:00
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
char *start, *end;
|
|
|
|
if (!PyArg_ParseTuple(args, "ss", &start, &end))
|
|
|
|
return NULL;
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
address_v4 inet_start = address_v4::from_string(start);
|
|
|
|
address_v4 inet_end = address_v4::from_string(end);
|
|
|
|
M_the_filter->add_rule(inet_start, inet_end, ip_filter::blocked);
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_INCREF(Py_None); return Py_None;
|
2007-01-07 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2007-06-15 21:45:53 +00:00
|
|
|
static PyObject *torrent_use_upnp(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
python_long action;
|
|
|
|
PyArg_ParseTuple(args, "i", &action);
|
|
|
|
|
|
|
|
if (action){
|
|
|
|
M_ses->start_upnp();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
M_ses->stop_upnp();
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *torrent_use_natpmp(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
python_long action;
|
|
|
|
|
|
|
|
PyArg_ParseTuple(args, "i", &action);
|
|
|
|
|
|
|
|
if (action){
|
|
|
|
M_ses->start_natpmp();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
M_ses->stop_natpmp();
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *torrent_use_utpex(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
python_long action;
|
|
|
|
|
|
|
|
PyArg_ParseTuple(args, "i", &action);
|
|
|
|
|
|
|
|
if (action){
|
|
|
|
M_ses->add_extension(&libtorrent::create_ut_pex_plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
|
|
|
}
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
static PyObject *torrent_pe_settings(PyObject *self, PyObject *args)
|
|
|
|
{
|
2007-06-07 03:57:19 +00:00
|
|
|
M_pe_settings = new pe_settings();
|
2007-06-07 20:05:33 +00:00
|
|
|
libtorrent::pe_settings::enc_policy out, in, prefer;
|
|
|
|
libtorrent::pe_settings::enc_level level;
|
|
|
|
|
|
|
|
PyArg_ParseTuple(args, "iiii", &out, &in, &level, &prefer);
|
2007-06-10 08:42:19 +00:00
|
|
|
|
2007-06-07 20:05:33 +00:00
|
|
|
M_pe_settings->out_enc_policy = out;
|
|
|
|
M_pe_settings->in_enc_policy = in;
|
|
|
|
M_pe_settings->allowed_enc_level = level;
|
|
|
|
M_pe_settings->prefer_rc4 = prefer;
|
2007-06-10 08:42:19 +00:00
|
|
|
|
2007-06-07 03:57:19 +00:00
|
|
|
M_ses->set_pe_settings(*M_pe_settings);
|
2007-06-10 08:42:19 +00:00
|
|
|
|
2007-06-07 19:15:47 +00:00
|
|
|
return Py_None;
|
2007-05-31 01:29:09 +00:00
|
|
|
}
|
2007-01-07 22:50:11 +00:00
|
|
|
|
2007-06-16 07:00:55 +00:00
|
|
|
static PyObject *torrent_set_ratio(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
python_long unique_ID, num;
|
|
|
|
if (!PyArg_ParseTuple(args, "ii", &unique_ID, &num))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
long index = get_index_from_unique_ID(unique_ID);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
M_torrents->at(index).handle.set_ratio(num);
|
|
|
|
|
|
|
|
Py_INCREF(Py_None); return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-07 22:50:11 +00:00
|
|
|
//====================
|
|
|
|
// Python Module data
|
|
|
|
//====================
|
|
|
|
|
2007-05-31 01:29:09 +00:00
|
|
|
static PyMethodDef deluge_core_methods[] =
|
|
|
|
{
|
|
|
|
{"pe_settings", torrent_pe_settings, METH_VARARGS, "."},
|
|
|
|
{"pre_init", torrent_pre_init, METH_VARARGS, "."},
|
|
|
|
{"init", torrent_init, METH_VARARGS, "."},
|
|
|
|
{"quit", torrent_quit, METH_VARARGS, "."},
|
|
|
|
{"save_fastresume", torrent_save_fastresume, METH_VARARGS, "."},
|
|
|
|
{"set_max_half_open", torrent_set_max_half_open, METH_VARARGS, "."},
|
|
|
|
{"set_download_rate_limit", torrent_set_download_rate_limit, METH_VARARGS, "."},
|
|
|
|
{"set_upload_rate_limit", torrent_set_upload_rate_limit, METH_VARARGS, "."},
|
|
|
|
{"set_listen_on", torrent_set_listen_on, METH_VARARGS, "."},
|
|
|
|
{"is_listening", torrent_is_listening, METH_VARARGS, "."},
|
|
|
|
{"listening_port", torrent_listening_port, METH_VARARGS, "."},
|
|
|
|
{"set_max_uploads", torrent_set_max_uploads, METH_VARARGS, "."},
|
|
|
|
{"set_max_connections", torrent_set_max_connections, METH_VARARGS, "."},
|
|
|
|
{"add_torrent", torrent_add_torrent, METH_VARARGS, "."},
|
|
|
|
{"remove_torrent", torrent_remove_torrent, METH_VARARGS, "."},
|
|
|
|
{"get_num_torrents", torrent_get_num_torrents, METH_VARARGS, "."},
|
|
|
|
{"reannounce", torrent_reannounce, METH_VARARGS, "."},
|
|
|
|
{"pause", torrent_pause, METH_VARARGS, "."},
|
|
|
|
{"resume", torrent_resume, METH_VARARGS, "."},
|
|
|
|
{"get_torrent_state", torrent_get_torrent_state, METH_VARARGS, "."},
|
|
|
|
{"pop_event", torrent_pop_event, METH_VARARGS, "."},
|
|
|
|
{"get_session_info", torrent_get_session_info, METH_VARARGS, "."},
|
|
|
|
{"get_peer_info", torrent_get_peer_info, METH_VARARGS, "."},
|
|
|
|
{"get_file_info", torrent_get_file_info, METH_VARARGS, "."},
|
|
|
|
{"set_filter_out", torrent_set_filter_out, METH_VARARGS, "."},
|
|
|
|
{"constants", torrent_constants, METH_VARARGS, "."},
|
|
|
|
{"start_DHT", torrent_start_DHT, METH_VARARGS, "."},
|
|
|
|
{"stop_DHT", torrent_stop_DHT, METH_VARARGS, "."},
|
|
|
|
{"get_DHT_info", torrent_get_DHT_info, METH_VARARGS, "."},
|
|
|
|
{"create_torrent", torrent_create_torrent, METH_VARARGS, "."},
|
2007-06-12 08:13:16 +00:00
|
|
|
{"reset_IP_filter", torrent_reset_IP_filter, METH_VARARGS, "."},
|
|
|
|
{"add_range_to_IP_filter", torrent_add_range_to_IP_filter, METH_VARARGS, "."},
|
2007-06-15 21:45:53 +00:00
|
|
|
{"use_upnp", torrent_use_upnp, METH_VARARGS, "."},
|
|
|
|
{"use_natpmp", torrent_use_natpmp, METH_VARARGS, "."},
|
|
|
|
{"use_utpex", torrent_use_utpex, METH_VARARGS, "."},
|
2007-06-16 07:00:55 +00:00
|
|
|
{"set_ratio", torrent_set_ratio, METH_VARARGS, "."},
|
2007-05-31 01:29:09 +00:00
|
|
|
{NULL}
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
2007-01-10 22:09:03 +00:00
|
|
|
initdeluge_core(void)
|
2007-01-07 22:50:11 +00:00
|
|
|
{
|
2007-05-31 01:29:09 +00:00
|
|
|
Py_InitModule("deluge_core", deluge_core_methods);
|
2007-01-07 22:50:11 +00:00
|
|
|
};
|