diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index 7a074868e..69543a9ae 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -144,6 +144,40 @@ list dht_stats_routing_table(dht_stats_alert const& a) 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() { using boost::noncopyable; @@ -657,4 +691,18 @@ void bind_alert() .add_property("routing_table", &dht_stats_routing_table) ; + class_, noncopyable>( + "dht_immutable_item_alert", no_init) + .add_property("item", &dht_immutable_item) + ; + + class_, noncopyable>( + "dht_mutable_item_alert", no_init) + .add_property("item", &dht_mutable_item) + ; + + class_, noncopyable>( + "dht_put_alert", no_init) + .add_property("item", &dht_put_item) + ; } diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index be1a7cf5a..fe1e47f9b 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -15,6 +15,7 @@ #include #include // for settings_map() #include +#include // for sign_mutable_item #include #include @@ -498,6 +499,43 @@ namespace TORRENT_ASSERT(!ec); 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 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& 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 buf; + bencode(std::back_inserter(buf), e); + ++seq; + sign_mutable_item(std::pair(&buf[0], buf.size()) + , std::pair(&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 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 @@ -505,11 +543,7 @@ void bind_session() { #ifndef TORRENT_DISABLE_DHT void (lt::session::*dht_get_immutable_item)(sha1_hash const&) = <::session::dht_get_item; - void (lt::session::*dht_get_mutable_item)(boost::array, std::string) = <::session::dht_get_item; sha1_hash (lt::session::*dht_put_immutable_item)(entry data) = <::session::dht_put_item; - void (lt::session::*dht_put_mutable_item)(boost::array key - , boost::function&, boost::uint64_t&, std::string const&)> cb - , std::string salt) = <::session::dht_put_item; // TODO: resolve the callback type for python #endif // TORRENT_DISABLE_DHT #ifndef TORRENT_NO_DEPRECATE @@ -683,9 +717,9 @@ void bind_session() .def("set_dht_settings", allow_threads(<::session::set_dht_settings)) .def("get_dht_settings", allow_threads(<::session::get_dht_settings)) .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_mutable_item", allow_threads(dht_put_mutable_item)) + .def("dht_put_mutable_item", &dht_put_mutable_item) .def("dht_get_peers", allow_threads(<::session::dht_get_peers)) .def("dht_announce", allow_threads(<::session::dht_announce)) #endif // TORRENT_DISABLE_DHT