Merge pull request #255 from bit/mutable_python

fix mutable dht entries in python bindings
This commit is contained in:
Arvid Norberg 2015-11-12 20:23:28 -05:00
commit aa30d64432
2 changed files with 88 additions and 6 deletions

View File

@ -144,6 +144,40 @@ list dht_stats_routing_table(dht_stats_alert const& a)
return result; return result;
} }
dict dht_immutable_item(dht_immutable_item_alert const& alert)
{
dict d;
d["key"] = alert.target.to_string();
d["value"] = alert.item.to_string();
return d;
}
dict dht_mutable_item(dht_mutable_item_alert const& alert)
{
dict d;
d["key"] = std::string(alert.key.data(), alert.key.size());
d["value"] = alert.item.to_string();
d["signature"] = std::string(alert.signature.data(), alert.signature.size());
d["seq"] = alert.seq;
d["salt"] = alert.salt;
d["authoritative"] = alert.authoritative;
return d;
}
dict dht_put_item(dht_put_alert const& alert)
{
dict d;
if (alert.target.is_all_zeros()) {
d["public_key"] = std::string(alert.public_key.data(), alert.public_key.size());
d["signature"] = std::string(alert.signature.data(), alert.signature.size());
d["seq"] = alert.seq;
d["salt"] = alert.salt;
} else {
d["target"] = alert.target.to_string();
}
return d;
}
void bind_alert() void bind_alert()
{ {
using boost::noncopyable; using boost::noncopyable;
@ -657,4 +691,18 @@ void bind_alert()
.add_property("routing_table", &dht_stats_routing_table) .add_property("routing_table", &dht_stats_routing_table)
; ;
class_<dht_immutable_item_alert, bases<alert>, noncopyable>(
"dht_immutable_item_alert", no_init)
.add_property("item", &dht_immutable_item)
;
class_<dht_mutable_item_alert, bases<alert>, noncopyable>(
"dht_mutable_item_alert", no_init)
.add_property("item", &dht_mutable_item)
;
class_<dht_put_alert, bases<alert>, noncopyable>(
"dht_put_alert", no_init)
.add_property("item", &dht_put_item)
;
} }

View File

@ -15,6 +15,7 @@
#include <libtorrent/bencode.hpp> #include <libtorrent/bencode.hpp>
#include <libtorrent/aux_/session_impl.hpp> // for settings_map() #include <libtorrent/aux_/session_impl.hpp> // for settings_map()
#include <libtorrent/torrent_info.hpp> #include <libtorrent/torrent_info.hpp>
#include <libtorrent/kademlia/item.hpp> // for sign_mutable_item
#include <libtorrent/extensions/lt_trackers.hpp> #include <libtorrent/extensions/lt_trackers.hpp>
#include <libtorrent/extensions/metadata_transfer.hpp> #include <libtorrent/extensions/metadata_transfer.hpp>
@ -498,6 +499,43 @@ namespace
TORRENT_ASSERT(!ec); TORRENT_ASSERT(!ec);
ses.load_state(e); ses.load_state(e);
} }
#ifndef TORRENT_DISABLE_DHT
void dht_get_mutable_item(lt::session& ses, std::string key, std::string salt)
{
TORRENT_ASSERT(key.size() == 32);
boost::array<char, 32> public_key;
std::copy(key.begin(), key.end(), public_key.begin());
ses.dht_get_item(public_key, salt);
}
void put_string(entry& e, boost::array<char, 64>& sig, boost::uint64_t& seq,
std::string const& salt, std::string public_key, std::string private_key,
std::string data)
{
using libtorrent::dht::sign_mutable_item;
e = data;
std::vector<char> buf;
bencode(std::back_inserter(buf), e);
++seq;
sign_mutable_item(std::pair<char const*, int>(&buf[0], buf.size())
, std::pair<char const*, int>(&salt[0], salt.size())
, seq, public_key.c_str(), private_key.c_str(), sig.data());
}
void dht_put_mutable_item(lt::session& ses, std::string private_key, std::string public_key,
std::string data, std::string salt)
{
TORRENT_ASSERT(private_key.size() == 64);
TORRENT_ASSERT(public_key.size() == 32);
boost::array<char, 32> key;
std::copy(public_key.begin(), public_key.end(), key.begin());
ses.dht_put_item(key, boost::bind(&put_string, _1, _2, _3, _4
, public_key, private_key, data)
, salt);
}
#endif
} // namespace unnamed } // namespace unnamed
@ -505,11 +543,7 @@ void bind_session()
{ {
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
void (lt::session::*dht_get_immutable_item)(sha1_hash const&) = &lt::session::dht_get_item; void (lt::session::*dht_get_immutable_item)(sha1_hash const&) = &lt::session::dht_get_item;
void (lt::session::*dht_get_mutable_item)(boost::array<char, 32>, std::string) = &lt::session::dht_get_item;
sha1_hash (lt::session::*dht_put_immutable_item)(entry data) = &lt::session::dht_put_item; sha1_hash (lt::session::*dht_put_immutable_item)(entry data) = &lt::session::dht_put_item;
void (lt::session::*dht_put_mutable_item)(boost::array<char, 32> key
, boost::function<void(entry&, boost::array<char,64>&, boost::uint64_t&, std::string const&)> cb
, std::string salt) = &lt::session::dht_put_item; // TODO: resolve the callback type for python
#endif // TORRENT_DISABLE_DHT #endif // TORRENT_DISABLE_DHT
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
@ -683,9 +717,9 @@ void bind_session()
.def("set_dht_settings", allow_threads(&lt::session::set_dht_settings)) .def("set_dht_settings", allow_threads(&lt::session::set_dht_settings))
.def("get_dht_settings", allow_threads(&lt::session::get_dht_settings)) .def("get_dht_settings", allow_threads(&lt::session::get_dht_settings))
.def("dht_get_immutable_item", allow_threads(dht_get_immutable_item)) .def("dht_get_immutable_item", allow_threads(dht_get_immutable_item))
.def("dht_get_mutable_item", allow_threads(dht_get_mutable_item)) .def("dht_get_mutable_item", &dht_get_mutable_item)
.def("dht_put_immutable_item", allow_threads(dht_put_immutable_item)) .def("dht_put_immutable_item", allow_threads(dht_put_immutable_item))
.def("dht_put_mutable_item", allow_threads(dht_put_mutable_item)) .def("dht_put_mutable_item", &dht_put_mutable_item)
.def("dht_get_peers", allow_threads(&lt::session::dht_get_peers)) .def("dht_get_peers", allow_threads(&lt::session::dht_get_peers))
.def("dht_announce", allow_threads(&lt::session::dht_announce)) .def("dht_announce", allow_threads(&lt::session::dht_announce))
#endif // TORRENT_DISABLE_DHT #endif // TORRENT_DISABLE_DHT