//Edited, immutable items are working. Mutable not yet.
I'm trying to put some data under DHT (mutable or immutable) but when I receive the alarm with the get response the message says that the item is uninitialized. Am I doing something wrong?
std::string cadenaStr = "Hello world"; //The string I want to save on DHT
lt::entry data; // The entry to store the message
data = cadenaStr;
std::vector<char> buf; //The buffer where the bencoded message will be
lt::bencode(std::back_inserter(buf), data);
lt::sha1_hash hash = ses.dht_put_item(buf); //Finally I put the bencoded message on DHT
Then I use ses.dht_get_item(hash) and wait for the alarm with the item but the message says that the item is uninitialized.
I get the same response when trying to get a mutable item.
Where is the mistake?
Thanks a lot.
Here is the complete code after successfully using immutable items. Mutable items still won't work.
namespace lt = libtorrent;
unsigned char signature[64];
unsigned char seed[32];
unsigned char public_key[32];
boost::array<char, 32> key;
unsigned char private_key[64];
void cb(lt::entry& e, boost::array<char,64>& sig, boost::uint64_t& seq,
std::string const& salt){
printf("Entrando en callback function\n");
e = std::string("Hello world from Libtorrent!");
std::vector<char> buf;
lt::bencode(std::back_inserter(buf), e);
++seq;
std::pair<char const *, int> pairStr;
pairStr = std::make_pair(e.to_string().c_str(),
sizeof(e.to_string().c_str()));
std::pair<char const *, int> pairSalt;
pairSalt = std::make_pair(salt.c_str(), sizeof(salt.c_str()));
lt::dht::sign_mutable_item(pairStr, pairSalt, seq, public_key,
private_key, signature);
for(int i=0; i<64; i++)
sig[i] = signature[i];
}
int main(int argc, char const* argv[])
{
if (argc != 2) {
std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl;
return 1;
}
ed25519_create_seed(seed);
ed25519_create_keypair(public_key, private_key, seed);
for(int i=0;i<32;i++){
key[i] = public_key[i];
}
lt::settings_pack sett;
sett.set_bool(lt::settings_pack::enable_dht, true);
sett.set_int(lt::settings_pack::alert_mask, 0xffffffff);
lt::session ses(sett);
ses.apply_settings(sett);
while(!ses.is_dht_running()){
printf("DHT is not running\r");
}
printf("DHT is running\t\t\n");
lt::add_torrent_params atp;
atp.url = argv[1];
atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(atp);
for (;;) {
std::vector<lt::alert*> alerts;
ses.pop_alerts(&alerts);
for (lt::alert const* a : alerts) {
if(lt::alert_cast<lt::dht_bootstrap_alert>(a)){
std::cout << a->message() << std::endl;
ses.dht_put_item(key, std::bind(&cb,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4), std::string("salt"));
}
if(lt::alert_cast<lt::dht_mutable_item_alert>(a)){
std::cout << a->message() << std::endl;
lt::dht_mutable_item_alert* item =
lt::alert_cast<lt::dht_mutable_item_alert>(a);
std::string str = item->item.to_string();
std::printf("%s", str.c_str());
printf("Mutable item alert successfull\n");
exit(0);
}
if(lt::alert_cast<lt::dht_put_alert>(a)){
std::cout << a->message() << std::endl;
lt::dht_put_alert* item = lt::alert_cast<lt::dht_put_alert>(a);
int num_success = item->num_success;
if(num_success==0)
ses.dht_put_item(key, std::bind(&cb,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4), "salt");
else
ses.dht_get_item(key);
}
}
}
}
it's hard to say without more information. You can get a better understanding of what's going on by enabling dht logging in the alert_mask
, and log those alerts.
For instance, perhaps you're trying to store the data before the DHT node is done bootstrapping. See the example in the repository, how it waits for bootstrap before storing.
It's also possible the node fails to bootstrap entirely, if you only have a single node to introduce you to the network. I would recommend saving the DHT state in between sessions to improve your chances to join the network again.
User contributions licensed under CC BY-SA 3.0