Browse Source

move dhtput signing to outside libtorrent.

old function (ses->dht_putData) is likely to be removed, i can't think of any use for it right now.
miguelfreitas
Miguel Freitas 11 years ago
parent
commit
7651134016
  1. 3
      libtorrent/include/libtorrent/aux_/session_impl.hpp
  2. 3
      libtorrent/include/libtorrent/kademlia/dht_tracker.hpp
  3. 3
      libtorrent/include/libtorrent/kademlia/node.hpp
  4. 7
      libtorrent/include/libtorrent/session.hpp
  5. 6
      libtorrent/src/kademlia/dht_tracker.cpp
  6. 17
      libtorrent/src/kademlia/node.cpp
  7. 11
      libtorrent/src/session.cpp
  8. 6
      libtorrent/src/session_impl.cpp
  9. 69
      src/twister.cpp

3
libtorrent/include/libtorrent/aux_/session_impl.hpp

@ -311,6 +311,9 @@ namespace libtorrent @@ -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);

3
libtorrent/include/libtorrent/kademlia/dht_tracker.hpp

@ -99,6 +99,9 @@ namespace libtorrent { namespace dht @@ -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<void(entry::list_type const&)> fdata,

3
libtorrent/include/libtorrent/kademlia/node.hpp

@ -227,6 +227,9 @@ public: @@ -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<void(entry::list_type const&)> fdata,
boost::function<void(bool, bool)> fdone);

7
libtorrent/include/libtorrent/session.hpp

@ -439,8 +439,11 @@ namespace libtorrent @@ -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);

6
libtorrent/src/kademlia/dht_tracker.cpp

@ -440,6 +440,12 @@ namespace libtorrent { namespace dht @@ -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<void(entry::list_type const&)> fdata,
boost::function<void(bool, bool)> fdone)

17
libtorrent/src/kademlia/node.cpp

@ -474,6 +474,23 @@ void node_impl::putData(std::string const &username, std::string const &resource @@ -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<dht_get> 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<void(entry::list_type const&)> fdata,
boost::function<void(bool, bool)> fdone)

11
libtorrent/src/session.cpp

@ -332,6 +332,9 @@ namespace libtorrent @@ -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 @@ -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

6
libtorrent/src/session_impl.cpp

@ -5788,6 +5788,12 @@ retry: @@ -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<dht_reply_data_alert>() ) {

69
src/twister.cpp

@ -1333,6 +1333,41 @@ entry formatSpamPost(const string &msg, const string &username, uint64_t utcTime @@ -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<session> 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<char> 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) @@ -1340,10 +1375,6 @@ Value dhtput(const Array& params, bool fHelp)
"dhtput <username> <resource> <s(ingle)/m(ulti)> <value> <sig_user> <seq>\n"
"Store resource in dht network");
boost::shared_ptr<session> 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) @@ -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) @@ -1495,10 +1526,6 @@ Value newpostmsg(const Array& params, bool fHelp)
"newpostmsg <username> <k> <msg> [reply_n] [reply_k]\n"
"Post a new message to swarm");
boost::shared_ptr<session> 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) @@ -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) @@ -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) @@ -1636,10 +1663,6 @@ Value newrtmsg(const Array& params, bool fHelp)
"newrtmsg <username> <k> <rt_v_object>\n"
"Post a new RT to swarm");
boost::shared_ptr<session> 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) @@ -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<std::string>(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);
}

Loading…
Cancel
Save