diff --git a/libtorrent/include/libtorrent/aux_/session_impl.hpp b/libtorrent/include/libtorrent/aux_/session_impl.hpp index e65abb32..b43d9611 100644 --- a/libtorrent/include/libtorrent/aux_/session_impl.hpp +++ b/libtorrent/include/libtorrent/aux_/session_impl.hpp @@ -311,6 +311,9 @@ namespace libtorrent entry const &value, std::string const &sig_user, 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); + 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 af580286..78a8092c 100644 --- a/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp +++ b/libtorrent/include/libtorrent/kademlia/dht_tracker.hpp @@ -99,6 +99,9 @@ namespace libtorrent { namespace dht void putData(std::string const &username, std::string const &resource, bool multi, entry const &value, std::string const &sig_user, 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); 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 5906b1ca..6cc344c2 100644 --- a/libtorrent/include/libtorrent/kademlia/node.hpp +++ b/libtorrent/include/libtorrent/kademlia/node.hpp @@ -227,6 +227,9 @@ public: entry const &value, std::string const &sig_user, 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); + void getData(std::string const &username, std::string const &resource, bool multi, boost::function fdata, boost::function fdone); diff --git a/libtorrent/include/libtorrent/session.hpp b/libtorrent/include/libtorrent/session.hpp index 73b8551d..3b174d73 100644 --- a/libtorrent/include/libtorrent/session.hpp +++ b/libtorrent/include/libtorrent/session.hpp @@ -439,8 +439,11 @@ namespace libtorrent // [MF] twister void dht_putData(std::string const &username, std::string const &resource, bool multi, - entry const &value, std::string const &sig_user, - boost::int64_t timeutc, int seq); + entry const &value, std::string const &sig_user, + 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); 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 f6f451c0..be1dc8ac 100644 --- a/libtorrent/src/kademlia/dht_tracker.cpp +++ b/libtorrent/src/kademlia/dht_tracker.cpp @@ -440,6 +440,12 @@ namespace libtorrent { namespace dht m_dht.putData(username,resource, multi, value, sig_user, timeutc, seq); } + 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) + { + m_dht.putDataSigned(username,resource, multi, p, sig_p, sig_user); + } + void dht_tracker::getData(std::string const &username, std::string const &resource, bool multi, boost::function fdata, boost::function fdone) diff --git a/libtorrent/src/kademlia/node.cpp b/libtorrent/src/kademlia/node.cpp index 1f92bb8c..e39af8ac 100644 --- a/libtorrent/src/kademlia/node.cpp +++ b/libtorrent/src/kademlia/node.cpp @@ -474,6 +474,23 @@ void node_impl::putData(std::string const &username, std::string const &resource ta->start(); } +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) +{ + 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(); +} + + void node_impl::getData(std::string const &username, std::string const &resource, bool multi, boost::function fdata, boost::function fdone) diff --git a/libtorrent/src/session.cpp b/libtorrent/src/session.cpp index d5be0268..a5bdea3c 100644 --- a/libtorrent/src/session.cpp +++ b/libtorrent/src/session.cpp @@ -332,6 +332,9 @@ namespace libtorrent #define TORRENT_ASYNC_CALL3(x, a1, a2, a3) \ m_impl->m_io_service.dispatch(boost::bind(&session_impl:: x, m_impl.get(), a1, a2, a3)) +#define TORRENT_ASYNC_CALL6(x, a1, a2, a3, a4, a5, a6) \ + m_impl->m_io_service.dispatch(boost::bind(&session_impl:: x, m_impl.get(), a1, a2, a3, a4, a5, a6)) + #define TORRENT_ASYNC_CALL7(x, a1, a2, a3, a4, a5, a6, a7) \ m_impl->m_io_service.dispatch(boost::bind(&session_impl:: x, m_impl.get(), a1, a2, a3, a4, a5, a6, a7)) @@ -860,6 +863,14 @@ namespace libtorrent #endif } + 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) + { +#ifndef TORRENT_DISABLE_DHT + TORRENT_ASYNC_CALL6(dht_putDataSigned, username, resource, multi, p, sig_p, sig_user); +#endif + } + void session::dht_getData(std::string const &username, std::string const &resource, bool multi) { #ifndef TORRENT_DISABLE_DHT diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index be204333..54ff8dc8 100644 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -5788,6 +5788,12 @@ retry: if (m_dht) m_dht->putData(username, resource, multi, value, sig_user, timeutc, seq); } + 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) + { + if (m_dht) m_dht->putDataSigned(username, resource, multi, p, sig_p, sig_user); + } + void post_dht_getData(aux::session_impl *si, entry::list_type const&lst) { if( si->m_alerts.should_post() ) { diff --git a/src/twister.cpp b/src/twister.cpp index 86826055..64747941 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -1333,6 +1333,41 @@ entry formatSpamPost(const string &msg, const string &username, uint64_t utcTime return v; } + +void dhtPutData(std::string const &username, std::string const &resource, bool multi, + entry const &value, std::string const &sig_user, + boost::int64_t timeutc, int seq) +{ + boost::shared_ptr ses(m_ses); + if( !ses ) { + printf("dhtPutData: libtorrent session not ready\n"); + return; + } + + // construct p dictionary and sign it + entry p; + entry& target = p["target"]; + target["n"] = username; + target["r"] = resource; + target["t"] = (multi) ? "m" : "s"; + if (seq >= 0 && !multi) p["seq"] = seq; + p["v"] = value; + p["time"] = timeutc; + int height = getBestHeight()-1; // be conservative + p["height"] = height; + + std::vector pbuf; + bencode(std::back_inserter(pbuf), p); + std::string str_p = std::string(pbuf.data(),pbuf.size()); + std::string sig_p = createSignature(str_p, sig_user); + if( !sig_p.size() ) { + printf("dhtPutData: createSignature error for user '%s'\n", sig_user.c_str()); + return; + } + + ses->dht_putDataSigned(username,resource,multi,p,sig_p,sig_user); +} + Value dhtput(const Array& params, bool fHelp) { if (fHelp || params.size() < 5 || params.size() > 6) @@ -1340,10 +1375,6 @@ Value dhtput(const Array& params, bool fHelp) "dhtput \n" "Store resource in dht network"); - boost::shared_ptr ses(m_ses); - if( !ses ) - return Value(); - EnsureWalletIsUnlocked(); string strUsername = params[0].get_str(); @@ -1375,7 +1406,7 @@ Value dhtput(const Array& params, bool fHelp) boost::int64_t timeutc = GetAdjustedTime(); - ses->dht_putData(strUsername, strResource, multi, value, strSigUser, timeutc, seq); + dhtPutData(strUsername, strResource, multi, value, strSigUser, timeutc, seq); return Value(); } @@ -1495,10 +1526,6 @@ Value newpostmsg(const Array& params, bool fHelp) "newpostmsg [reply_n] [reply_k]\n" "Post a new message to swarm"); - boost::shared_ptr ses(m_ses); - if( !ses ) - return Array(); - EnsureWalletIsUnlocked(); string strUsername = params[0].get_str(); @@ -1538,19 +1565,19 @@ Value newpostmsg(const Array& params, bool fHelp) h.add_piece(k,buf.data(),buf.size()); } else { // TODO: swarm resource forwarding not implemented - ses->dht_putData(strUsername, "swarm", false, + dhtPutData(strUsername, "swarm", false, v, strUsername, GetAdjustedTime(), 1); } // post to dht as well - ses->dht_putData(strUsername, string("post")+strK, false, + dhtPutData(strUsername, string("post")+strK, false, v, strUsername, GetAdjustedTime(), 1); - ses->dht_putData(strUsername, string("status"), false, + dhtPutData(strUsername, string("status"), false, v, strUsername, GetAdjustedTime(), k); // is this a reply? notify if( strReplyN.length() ) { - ses->dht_putData(strReplyN, string("replies")+strReplyK, true, + dhtPutData(strReplyN, string("replies")+strReplyK, true, v, strUsername, GetAdjustedTime(), 0); } @@ -1567,10 +1594,10 @@ Value newpostmsg(const Array& params, bool fHelp) boost::algorithm::to_lower(word); #endif if( token.at(0) == '#') { - ses->dht_putData(word, "hashtag", true, + dhtPutData(word, "hashtag", true, v, strUsername, GetAdjustedTime(), 0); } else if( token.at(0) == '@') { - ses->dht_putData(word, "mention", true, + dhtPutData(word, "mention", true, v, strUsername, GetAdjustedTime(), 0); } } @@ -1636,10 +1663,6 @@ Value newrtmsg(const Array& params, bool fHelp) "newrtmsg \n" "Post a new RT to swarm"); - boost::shared_ptr ses(m_ses); - if( !ses ) - return Array(); - EnsureWalletIsUnlocked(); string strUsername = params[0].get_str(); @@ -1674,21 +1697,21 @@ Value newrtmsg(const Array& params, bool fHelp) h.add_piece(k,buf.data(),buf.size()); } else { // TODO: swarm resource forwarding not implemented - ses->dht_putData(strUsername, "swarm", false, + dhtPutData(strUsername, "swarm", false, v, strUsername, GetAdjustedTime(), 1); } // post to dht as well - ses->dht_putData(strUsername, string("post")+strK, false, + dhtPutData(strUsername, string("post")+strK, false, v, strUsername, GetAdjustedTime(), 1); - ses->dht_putData(strUsername, string("status"), false, + dhtPutData(strUsername, string("status"), false, v, strUsername, GetAdjustedTime(), k); // notification to keep track of RTs of the original post if( rt ) { string rt_user = rt->find_key("n")->string(); string rt_k = boost::lexical_cast(rt->find_key("k")->integer()); - ses->dht_putData(rt_user, string("rts")+rt_k, true, + dhtPutData(rt_user, string("rts")+rt_k, true, v, strUsername, GetAdjustedTime(), 0); }