From 5af649a58a1cca814bcd39d8e7c328df58a91621 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Wed, 25 Jun 2014 17:47:44 -0300 Subject: [PATCH] add consistency checks to node_impl::putDataSigned and a flag to mean that locally generated content should be kept for periodic refresh. --- .../include/libtorrent/aux_/session_impl.hpp | 2 +- .../libtorrent/kademlia/dht_tracker.hpp | 2 +- .../include/libtorrent/kademlia/node.hpp | 2 +- libtorrent/include/libtorrent/session.hpp | 2 +- libtorrent/src/kademlia/dht_tracker.cpp | 4 +- libtorrent/src/kademlia/node.cpp | 58 ++++++++++++++++--- libtorrent/src/session.cpp | 4 +- libtorrent/src/session_impl.cpp | 4 +- src/twister.cpp | 2 +- 9 files changed, 60 insertions(+), 20 deletions(-) diff --git a/libtorrent/include/libtorrent/aux_/session_impl.hpp b/libtorrent/include/libtorrent/aux_/session_impl.hpp index b43d9611..6ae37e5e 100644 --- a/libtorrent/include/libtorrent/aux_/session_impl.hpp +++ b/libtorrent/include/libtorrent/aux_/session_impl.hpp @@ -312,7 +312,7 @@ namespace libtorrent boost::int64_t timeutc, int seq); void dht_putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user); + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local); void dht_getData(std::string const &username, std::string const &resource, bool multi); diff --git a/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp b/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp index 78a8092c..bbf5a433 100644 --- a/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp +++ b/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp @@ -101,7 +101,7 @@ namespace libtorrent { namespace dht boost::int64_t timeutc, int seq); void putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user); + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local); void getData(std::string const &username, std::string const &resource, bool multi, boost::function fdata, diff --git a/libtorrent/include/libtorrent/kademlia/node.hpp b/libtorrent/include/libtorrent/kademlia/node.hpp index 6cc344c2..939447d5 100644 --- a/libtorrent/include/libtorrent/kademlia/node.hpp +++ b/libtorrent/include/libtorrent/kademlia/node.hpp @@ -228,7 +228,7 @@ public: boost::int64_t timeutc, int seq); void putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user); + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local); void getData(std::string const &username, std::string const &resource, bool multi, boost::function fdata, diff --git a/libtorrent/include/libtorrent/session.hpp b/libtorrent/include/libtorrent/session.hpp index 3b174d73..be0c75a4 100644 --- a/libtorrent/include/libtorrent/session.hpp +++ b/libtorrent/include/libtorrent/session.hpp @@ -443,7 +443,7 @@ namespace libtorrent boost::int64_t timeutc, int seq); void dht_putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user); + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local); void dht_getData(std::string const &username, std::string const &resource, bool multi); diff --git a/libtorrent/src/kademlia/dht_tracker.cpp b/libtorrent/src/kademlia/dht_tracker.cpp index be1dc8ac..5e0c109d 100644 --- a/libtorrent/src/kademlia/dht_tracker.cpp +++ b/libtorrent/src/kademlia/dht_tracker.cpp @@ -441,9 +441,9 @@ namespace libtorrent { namespace dht } void dht_tracker::putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user) + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local) { - m_dht.putDataSigned(username,resource, multi, p, sig_p, sig_user); + m_dht.putDataSigned(username,resource, multi, p, sig_p, sig_user, local); } void dht_tracker::getData(std::string const &username, std::string const &resource, bool multi, diff --git a/libtorrent/src/kademlia/node.cpp b/libtorrent/src/kademlia/node.cpp index e39af8ac..61c866d6 100644 --- a/libtorrent/src/kademlia/node.cpp +++ b/libtorrent/src/kademlia/node.cpp @@ -475,19 +475,59 @@ void node_impl::putData(std::string const &username, std::string const &resource } void node_impl::putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user) + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local) { printf("putDataSigned: username=%s,res=%s,multi=%d sig_user=%s\n", username.c_str(), resource.c_str(), multi, sig_user.c_str()); - // search for nodes with ids close to id or with peers - // for info-hash id. then send putData to them. - boost::intrusive_ptr ta(new dht_get(*this, username, resource, multi, - boost::bind(&nop), - boost::bind(&putData_fun, _1, boost::ref(*this), p, sig_p, sig_user), true)); - - // now send it to the network (start transversal algorithm) - ta->start(); + // consistency checks + entry const* seqEntry = p.find_key("seq"); + entry const* heightEntry = p.find_key("height"); + entry const* target = p.find_key("target"); + std::string n, r, t; + if( target ) { + entry const* nEntry = target->find_key("n"); + entry const* rEntry = target->find_key("r"); + entry const* tEntry = target->find_key("t"); + if( nEntry && nEntry->type() == entry::string_t ) + n = nEntry->string(); + if( rEntry && rEntry->type() == entry::string_t ) + r = rEntry->string(); + if( tEntry && tEntry->type() == entry::string_t ) + t = tEntry->string(); + } + if( p.find_key("v") && heightEntry && heightEntry->type() == entry::int_t && + (multi || (seqEntry && seqEntry->type() == entry::int_t)) && target && + n == username && r == resource && ((!multi && t == "s") || (multi && t == "m")) ) { + + // search for nodes with ids close to id or with peers + // for info-hash id. then send putData to them. + boost::intrusive_ptr ta(new dht_get(*this, username, resource, multi, + boost::bind(&nop), + boost::bind(&putData_fun, _1, boost::ref(*this), p, sig_p, sig_user), true)); + + if( local ) { + // store it locally so it will be automatically refreshed with the rest + std::vector pbuf; + bencode(std::back_inserter(pbuf), p); + std::string str_p = std::string(pbuf.data(),pbuf.size()); + + dht_storage_item item(str_p, sig_p, sig_user); + item.local_add_time = time(NULL); + std::vector vbuf; + bencode(std::back_inserter(vbuf), p["v"]); + std::pair bufv = std::make_pair(vbuf.data(), vbuf.size()); + + int seq = (seqEntry && seqEntry->type() == entry::int_t) ? seqEntry->integer() : -1; + int height = heightEntry->integer(); + store_dht_item(item, ta->target(), multi, seq, height, bufv); + } + + // now send it to the network (start transversal algorithm) + ta->start(); + } else { + printf("putDataSigned: consistency checks failed!\n"); + } } diff --git a/libtorrent/src/session.cpp b/libtorrent/src/session.cpp index a5bdea3c..a696097c 100644 --- a/libtorrent/src/session.cpp +++ b/libtorrent/src/session.cpp @@ -864,10 +864,10 @@ namespace libtorrent } void session::dht_putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user) + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local) { #ifndef TORRENT_DISABLE_DHT - TORRENT_ASYNC_CALL6(dht_putDataSigned, username, resource, multi, p, sig_p, sig_user); + TORRENT_ASYNC_CALL7(dht_putDataSigned, username, resource, multi, p, sig_p, sig_user, local); #endif } diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index 54ff8dc8..f6d4b3f9 100644 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -5789,9 +5789,9 @@ retry: } void session_impl::dht_putDataSigned(std::string const &username, std::string const &resource, bool multi, - entry const &p, std::string const &sig_p, std::string const &sig_user) + entry const &p, std::string const &sig_p, std::string const &sig_user, bool local) { - if (m_dht) m_dht->putDataSigned(username, resource, multi, p, sig_p, sig_user); + if (m_dht) m_dht->putDataSigned(username, resource, multi, p, sig_p, sig_user, local); } void post_dht_getData(aux::session_impl *si, entry::list_type const&lst) diff --git a/src/twister.cpp b/src/twister.cpp index 64747941..08bdc395 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -1365,7 +1365,7 @@ void dhtPutData(std::string const &username, std::string const &resource, bool m return; } - ses->dht_putDataSigned(username,resource,multi,p,sig_p,sig_user); + ses->dht_putDataSigned(username,resource,multi,p,sig_p,sig_user, true); } Value dhtput(const Array& params, bool fHelp)