2014-02-28 05:02:48 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2014, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "libtorrent/session.hpp"
|
2015-03-15 19:04:45 +01:00
|
|
|
#include "libtorrent/hex.hpp" // for from_hex
|
2014-02-28 05:02:48 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp" // for bencode()
|
2014-03-03 00:35:35 +01:00
|
|
|
#include "libtorrent/kademlia/item.hpp" // for sign_mutable_item
|
2016-08-30 02:28:42 +02:00
|
|
|
#include "libtorrent/kademlia/ed25519.hpp"
|
2016-07-24 00:57:04 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2014-07-19 09:12:20 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <cstdio> // for snprintf
|
|
|
|
#include <cinttypes> // for PRId64 et.al.
|
|
|
|
#include <cstdlib>
|
2014-02-28 05:02:48 +01:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
2016-08-30 02:28:42 +02:00
|
|
|
using namespace libtorrent::dht;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace lt = libtorrent;
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
// TODO: don't use internal functions to libtorrent
|
|
|
|
using libtorrent::aux::from_hex;
|
|
|
|
using libtorrent::aux::to_hex;
|
|
|
|
|
2014-03-06 10:45:16 +01:00
|
|
|
#ifdef TORRENT_DISABLE_DHT
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "not built with DHT support\n");
|
2014-03-06 10:45:16 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
void usage()
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr,
|
2014-02-28 05:02:48 +01:00
|
|
|
"USAGE:\ndht <command> <arg>\n\nCOMMANDS:\n"
|
2014-03-03 00:35:35 +01:00
|
|
|
"get <hash> - retrieves and prints out the immutable\n"
|
|
|
|
" item stored under hash.\n"
|
|
|
|
"put <string> - puts the specified string as an immutable\n"
|
|
|
|
" item onto the DHT. The resulting target hash\n"
|
|
|
|
"gen-key <key-file> - generate ed25519 keypair and save it in\n"
|
|
|
|
" the specified file\n"
|
2015-09-22 18:24:03 +02:00
|
|
|
"dump-key <key-file> - dump ed25519 keypair from the specified key\n"
|
|
|
|
" file.\n"
|
2014-03-03 00:35:35 +01:00
|
|
|
"mput <key-file> <string> - puts the specified string as a mutable\n"
|
|
|
|
" object under the public key in key-file\n"
|
|
|
|
"mget <public-key> - get a mutable object under the specified\n"
|
|
|
|
" public key\n"
|
2014-02-28 05:02:48 +01:00
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
alert* wait_for_alert(lt::session& s, int alert_type)
|
2014-02-28 05:02:48 +01:00
|
|
|
{
|
2016-06-20 17:32:06 +02:00
|
|
|
alert* ret = nullptr;
|
2014-02-28 05:02:48 +01:00
|
|
|
bool found = false;
|
|
|
|
while (!found)
|
|
|
|
{
|
|
|
|
s.wait_for_alert(seconds(5));
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
std::vector<alert*> alerts;
|
2014-02-28 05:02:48 +01:00
|
|
|
s.pop_alerts(&alerts);
|
2015-04-03 22:15:48 +02:00
|
|
|
for (std::vector<alert*>::iterator i = alerts.begin()
|
2014-02-28 05:02:48 +01:00
|
|
|
, end(alerts.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->type() != alert_type)
|
|
|
|
{
|
|
|
|
static int spinner = 0;
|
|
|
|
static const char anim[] = {'-', '\\', '|', '/'};
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\r%c", anim[spinner]);
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fflush(stdout);
|
2014-02-28 05:02:48 +01:00
|
|
|
spinner = (spinner + 1) & 3;
|
|
|
|
//print some alerts?
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-03 22:15:48 +02:00
|
|
|
ret = *i;
|
2014-02-28 05:02:48 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\n");
|
2014-02-28 05:02:48 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
void put_string(entry& e, std::array<char, 64>& sig
|
|
|
|
, std::uint64_t& seq
|
|
|
|
, std::string const& salt
|
|
|
|
, std::array<char, 32> const& pk
|
|
|
|
, std::array<char, 64> const& sk
|
2014-03-03 00:35:35 +01:00
|
|
|
, char const* str)
|
|
|
|
{
|
|
|
|
using libtorrent::dht::sign_mutable_item;
|
|
|
|
|
|
|
|
e = std::string(str);
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), e);
|
2016-07-24 00:57:04 +02:00
|
|
|
dht::signature sign;
|
2014-03-03 00:35:35 +01:00
|
|
|
++seq;
|
2016-08-30 08:37:51 +02:00
|
|
|
sign = sign_mutable_item(buf, salt, dht::sequence_number(seq)
|
2016-07-24 00:57:04 +02:00
|
|
|
, dht::public_key(pk.data())
|
2016-08-30 08:37:51 +02:00
|
|
|
, dht::secret_key(sk.data()));
|
2016-07-24 00:57:04 +02:00
|
|
|
sig = sign.bytes;
|
2014-03-03 00:35:35 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void bootstrap(lt::session& s)
|
2014-03-03 00:35:35 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("bootstrapping\n");
|
2014-03-03 00:35:35 +01:00
|
|
|
wait_for_alert(s, dht_bootstrap_alert::alert_type);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("bootstrap done.\n");
|
2015-09-22 18:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int dump_key(char *filename)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* f = std::fopen(filename, "rb+");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr)
|
2015-09-22 18:24:03 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to open file \"%s\": (%d) %s\n"
|
2015-09-22 18:24:03 +02:00
|
|
|
, filename, errno, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-30 02:28:42 +02:00
|
|
|
std::array<char, 32> seed;
|
|
|
|
int size = int(fread(seed.data(), 1, 32, f));
|
2015-09-22 18:24:03 +02:00
|
|
|
if (size != 32)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "invalid key file.\n");
|
2015-09-22 18:24:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fclose(f);
|
2015-09-22 18:24:03 +02:00
|
|
|
|
2016-08-30 02:28:42 +02:00
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
2016-08-30 08:37:51 +02:00
|
|
|
std::tie(pk, sk) = ed25519_create_keypair(seed);
|
2015-09-22 18:24:03 +02:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
std::printf("public key: %s\nprivate key: %s\n"
|
2016-08-30 02:28:42 +02:00
|
|
|
, to_hex(pk.bytes).c_str()
|
|
|
|
, to_hex(sk.bytes).c_str());
|
2015-09-22 18:24:03 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int generate_key(char* filename)
|
|
|
|
{
|
2016-08-30 08:37:51 +02:00
|
|
|
std::array<char, 32> seed = ed25519_create_seed();
|
2015-09-22 18:24:03 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* f = std::fopen(filename, "wb+");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr)
|
2015-09-22 18:24:03 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to open file for writing \"%s\": (%d) %s\n"
|
2015-09-22 18:24:03 +02:00
|
|
|
, filename, errno, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-30 02:28:42 +02:00
|
|
|
int size = int(std::fwrite(seed.data(), 1, 32, f));
|
2015-09-22 18:24:03 +02:00
|
|
|
if (size != 32)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to write key file.\n");
|
2015-09-22 18:24:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fclose(f);
|
2015-09-22 18:24:03 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_dht_state(lt::session& s)
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* f = std::fopen(".dht", "rb");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr) return;
|
2015-09-22 18:24:03 +02:00
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
int size = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
std::vector<char> state;
|
|
|
|
state.resize(size);
|
|
|
|
fread(&state[0], 1, state.size(), f);
|
|
|
|
|
|
|
|
bdecode_node e;
|
|
|
|
error_code ec;
|
|
|
|
bdecode(&state[0], &state[0] + state.size(), e, ec);
|
|
|
|
if (ec)
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to parse .dht file: (%d) %s\n"
|
2015-09-22 18:24:03 +02:00
|
|
|
, ec.value(), ec.message().c_str());
|
|
|
|
else
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("load dht state from .dht\n");
|
2015-09-22 18:24:03 +02:00
|
|
|
s.load_state(e);
|
|
|
|
}
|
|
|
|
}
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fclose(f);
|
2015-09-22 18:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int save_dht_state(lt::session& s)
|
|
|
|
{
|
|
|
|
entry e;
|
|
|
|
s.save_state(e, session::save_dht_state);
|
|
|
|
std::vector<char> state;
|
|
|
|
bencode(std::back_inserter(state), e);
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* f = std::fopen(".dht", "wb+");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr)
|
2015-09-22 18:24:03 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to open file .dht for writing");
|
2015-09-22 18:24:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fwrite(&state[0], 1, state.size(), f);
|
|
|
|
std::fclose(f);
|
2015-09-22 18:24:03 +02:00
|
|
|
|
|
|
|
return 0;
|
2014-03-03 00:35:35 +01:00
|
|
|
}
|
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2014-03-03 00:35:35 +01:00
|
|
|
// skip pointer to self
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
|
|
|
|
if (argc < 1) usage();
|
|
|
|
|
2015-09-22 18:24:03 +02:00
|
|
|
if (strcmp(argv[0], "dump-key") == 0)
|
2014-03-03 00:35:35 +01:00
|
|
|
{
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) usage();
|
|
|
|
|
2015-09-22 18:24:03 +02:00
|
|
|
return dump_key(argv[0]);
|
|
|
|
}
|
2014-03-03 00:35:35 +01:00
|
|
|
|
2015-09-22 18:24:03 +02:00
|
|
|
if (strcmp(argv[0], "gen-key") == 0)
|
|
|
|
{
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) usage();
|
|
|
|
|
|
|
|
return generate_key(argv[0]);
|
2014-03-03 00:35:35 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
settings_pack sett;
|
2015-08-28 05:45:29 +02:00
|
|
|
sett.set_bool(settings_pack::enable_dht, false);
|
2014-07-06 21:18:00 +02:00
|
|
|
sett.set_int(settings_pack::alert_mask, 0xffffffff);
|
|
|
|
lt::session s(sett);
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2015-08-28 05:45:29 +02:00
|
|
|
sett.set_bool(settings_pack::enable_dht, true);
|
|
|
|
s.apply_settings(sett);
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2015-09-22 18:24:03 +02:00
|
|
|
load_dht_state(s);
|
2014-02-28 05:02:48 +01:00
|
|
|
|
|
|
|
if (strcmp(argv[0], "get") == 0)
|
|
|
|
{
|
|
|
|
++argv;
|
|
|
|
--argc;
|
2016-06-04 16:01:43 +02:00
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
if (argc < 1) usage();
|
|
|
|
|
|
|
|
if (strlen(argv[0]) != 40)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "the hash is expected to be 40 hex characters\n");
|
2014-02-28 05:02:48 +01:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
sha1_hash target;
|
2016-07-30 00:40:22 +02:00
|
|
|
bool ret = from_hex({argv[0], 40}, (char*)&target[0]);
|
2014-03-03 00:35:35 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "invalid hex encoding of target hash\n");
|
2014-03-03 00:35:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2014-03-03 00:35:35 +01:00
|
|
|
bootstrap(s);
|
2014-02-28 05:02:48 +01:00
|
|
|
s.dht_get_item(target);
|
|
|
|
|
2016-07-30 00:40:22 +02:00
|
|
|
std::printf("GET %s\n", to_hex(target).c_str());
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
alert* a = wait_for_alert(s, dht_immutable_item_alert::alert_type);
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
dht_immutable_item_alert* item = alert_cast<dht_immutable_item_alert>(a);
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2016-10-19 07:32:15 +02:00
|
|
|
std::string str = item->item.to_string();
|
|
|
|
std::printf("%s", str.c_str());
|
2014-02-28 05:02:48 +01:00
|
|
|
}
|
|
|
|
else if (strcmp(argv[0], "put") == 0)
|
|
|
|
{
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) usage();
|
|
|
|
|
|
|
|
entry data;
|
|
|
|
data = std::string(argv[0]);
|
|
|
|
|
2014-03-03 00:35:35 +01:00
|
|
|
bootstrap(s);
|
2016-07-30 00:40:22 +02:00
|
|
|
sha1_hash const target = s.dht_put_item(data);
|
2016-05-25 06:31:52 +02:00
|
|
|
|
2016-07-30 00:40:22 +02:00
|
|
|
std::printf("PUT %s\n", to_hex(target).c_str());
|
2014-02-28 05:02:48 +01:00
|
|
|
|
2015-09-22 20:10:57 +02:00
|
|
|
alert* a = wait_for_alert(s, dht_put_alert::alert_type);
|
|
|
|
dht_put_alert* pa = alert_cast<dht_put_alert>(a);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%s\n", pa->message().c_str());
|
2014-02-28 05:02:48 +01:00
|
|
|
}
|
2014-03-03 00:35:35 +01:00
|
|
|
else if (strcmp(argv[0], "mput") == 0)
|
|
|
|
{
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) usage();
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* f = std::fopen(argv[0], "rb+");
|
2016-06-20 17:32:06 +02:00
|
|
|
if (f == nullptr)
|
2014-03-03 00:35:35 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to open file \"%s\": (%d) %s\n"
|
2014-03-03 00:35:35 +01:00
|
|
|
, argv[0], errno, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-30 02:28:42 +02:00
|
|
|
std::array<char, 32> seed;
|
|
|
|
fread(seed.data(), 1, 32, f);
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fclose(f);
|
2014-03-03 00:35:35 +01:00
|
|
|
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) usage();
|
|
|
|
|
2016-08-30 02:28:42 +02:00
|
|
|
public_key pk;
|
|
|
|
secret_key sk;
|
2016-08-30 08:37:51 +02:00
|
|
|
std::tie(pk, sk) = ed25519_create_keypair(seed);
|
2016-05-25 06:31:52 +02:00
|
|
|
|
2014-03-03 00:35:35 +01:00
|
|
|
bootstrap(s);
|
2016-08-30 02:28:42 +02:00
|
|
|
s.dht_put_item(pk.bytes, std::bind(&put_string, _1, _2, _3, _4
|
|
|
|
, pk.bytes, sk.bytes, argv[0]));
|
2014-03-03 00:35:35 +01:00
|
|
|
|
2016-08-30 02:28:42 +02:00
|
|
|
std::printf("MPUT public key: %s\n", to_hex(pk.bytes).c_str());
|
2014-03-03 00:35:35 +01:00
|
|
|
|
2015-09-22 20:10:57 +02:00
|
|
|
alert* a = wait_for_alert(s, dht_put_alert::alert_type);
|
|
|
|
dht_put_alert* pa = alert_cast<dht_put_alert>(a);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%s\n", pa->message().c_str());
|
2014-03-03 00:35:35 +01:00
|
|
|
}
|
|
|
|
else if (strcmp(argv[0], "mget") == 0)
|
|
|
|
{
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc < 1) usage();
|
|
|
|
|
2016-07-30 00:40:22 +02:00
|
|
|
size_t len = strlen(argv[0]);
|
2014-03-03 00:35:35 +01:00
|
|
|
if (len != 64)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "public key is expected to be 64 hex digits\n");
|
2014-03-03 00:35:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-01 05:10:47 +02:00
|
|
|
std::array<char, 32> public_key;
|
2016-07-30 00:40:22 +02:00
|
|
|
bool ret = from_hex({argv[0], len}, &public_key[0]);
|
2014-03-03 00:35:35 +01:00
|
|
|
if (!ret)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "invalid hex encoding of public key\n");
|
2014-03-03 00:35:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bootstrap(s);
|
|
|
|
s.dht_get_item(public_key);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("MGET %s\n", argv[0]);
|
2014-03-03 00:35:35 +01:00
|
|
|
|
2015-08-28 18:27:48 +02:00
|
|
|
bool authoritative = false;
|
2014-03-03 00:35:35 +01:00
|
|
|
|
2015-08-28 18:27:48 +02:00
|
|
|
while (!authoritative)
|
|
|
|
{
|
|
|
|
alert* a = wait_for_alert(s, dht_mutable_item_alert::alert_type);
|
2014-03-03 00:35:35 +01:00
|
|
|
|
2015-08-28 18:27:48 +02:00
|
|
|
dht_mutable_item_alert* item = alert_cast<dht_mutable_item_alert>(a);
|
|
|
|
|
|
|
|
authoritative = item->authoritative;
|
2016-10-19 07:32:15 +02:00
|
|
|
std::string str = item->item.to_string();
|
|
|
|
std::printf("%s: %s", authoritative ? "auth" : "non-auth", str.c_str());
|
2015-08-28 18:27:48 +02:00
|
|
|
}
|
2014-03-03 00:35:35 +01:00
|
|
|
}
|
2014-02-28 05:02:48 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:24:03 +02:00
|
|
|
return save_dht_state(s);
|
2014-02-28 05:02:48 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 10:45:16 +01:00
|
|
|
#endif
|
|
|
|
|