mirror of https://github.com/oxen-io/lokinet
it compiles
This commit is contained in:
parent
5228a81bae
commit
973f86c900
|
@ -28,44 +28,20 @@ handle_signal(int sig)
|
|||
#define TESTNET 0
|
||||
#endif
|
||||
|
||||
bool
|
||||
printNode(struct llarp_nodedb_iter *iter)
|
||||
{
|
||||
char ftmp[68] = {0};
|
||||
const char *hexname =
|
||||
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(iter->rc->pubkey, ftmp);
|
||||
|
||||
printf("[%zu]=>[%s]\n", iter->index, hexname);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
aiLister(struct llarp_ai_list_iter *request, struct llarp_ai *addr)
|
||||
{
|
||||
static size_t count = 0;
|
||||
count++;
|
||||
llarp::Addr a(*addr);
|
||||
std::cout << "AddressInfo " << count << ": " << a << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
displayRC(llarp_rc *rc)
|
||||
displayRC(const llarp::RouterContact &rc)
|
||||
{
|
||||
char ftmp[68] = {0};
|
||||
const char *hexPubSigKey =
|
||||
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(rc->pubkey, ftmp);
|
||||
printf("PubSigKey [%s]\n", hexPubSigKey);
|
||||
|
||||
struct llarp_ai_list_iter iter;
|
||||
// iter.user
|
||||
iter.visit = &aiLister;
|
||||
llarp_ai_list_iterate(rc->addrs, &iter);
|
||||
std::cout << rc.pubkey << std::endl;
|
||||
for(const auto &addr : rc.addrs)
|
||||
{
|
||||
std::cout << "AddressInfo: " << addr << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// fwd declr
|
||||
struct check_online_request;
|
||||
|
||||
/*
|
||||
void
|
||||
HandleDHTLocate(llarp_router_lookup_job *job)
|
||||
{
|
||||
|
@ -88,6 +64,7 @@ HandleDHTLocate(llarp_router_lookup_job *job)
|
|||
// still need to exit this logic thread...
|
||||
llarp_main_abort(ctx);
|
||||
}
|
||||
*/
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
|
@ -135,6 +112,8 @@ main(int argc, char *argv[])
|
|||
conffname = defaultConfName;
|
||||
char *rcfname = nullptr;
|
||||
char *nodesdir = nullptr;
|
||||
|
||||
llarp::RouterContact rc;
|
||||
while(1)
|
||||
{
|
||||
static struct option long_options[] = {
|
||||
|
@ -243,62 +222,54 @@ main(int argc, char *argv[])
|
|||
{
|
||||
llarp_crypto crypto;
|
||||
llarp_crypto_libsodium_init(&crypto);
|
||||
llarp_rc rc;
|
||||
if(!llarp_rc_read(rcfname, &rc))
|
||||
if(!rc.Read(rcfname))
|
||||
{
|
||||
std::cout << "failed to read " << rcfname << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if(!llarp_rc_verify_sig(&crypto, &rc))
|
||||
if(!rc.VerifySignature(&crypto))
|
||||
{
|
||||
std::cout << rcfname << " has invalid signature" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if(!llarp_rc_is_public_router(&rc))
|
||||
if(!rc.IsPublicRouter())
|
||||
{
|
||||
std::cout << rcfname << " is not a public router";
|
||||
if(llarp_ai_list_size(rc.addrs) == 0)
|
||||
if(rc.addrs.size() == 0)
|
||||
{
|
||||
std::cout << " because it has no public addresses";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return 1;
|
||||
}
|
||||
llarp::PubKey pubkey(rc.pubkey);
|
||||
llarp::PubKey enckey(rc.enckey);
|
||||
|
||||
std::cout << "router identity and dht routing key: " << pubkey << std::endl;
|
||||
std::cout << "router encryption key: " << enckey << std::endl;
|
||||
std::cout << "router identity and dht routing key: " << rc.pubkey
|
||||
<< std::endl;
|
||||
|
||||
std::cout << "router encryption key: " << rc.enckey << std::endl;
|
||||
|
||||
if(rc.HasNick())
|
||||
std::cout << "router nickname: " << rc.Nick() << std::endl;
|
||||
|
||||
std::cout << "advertised addresses: ";
|
||||
llarp_ai_list_iter a_itr;
|
||||
a_itr.user = nullptr;
|
||||
a_itr.visit = [](llarp_ai_list_iter *, llarp_ai *addrInfo) -> bool {
|
||||
llarp::Addr addr(*addrInfo);
|
||||
std::cout << addr << " ";
|
||||
return true;
|
||||
};
|
||||
llarp_ai_list_iterate(rc.addrs, &a_itr);
|
||||
std::cout << "advertised addresses: " << std::endl;
|
||||
for(const auto &addr : rc.addrs)
|
||||
{
|
||||
std::cout << addr << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "advertised exits: ";
|
||||
|
||||
if(llarp_xi_list_size(rc.exits))
|
||||
if(rc.exits.size())
|
||||
{
|
||||
llarp_xi_list_iter e_itr;
|
||||
e_itr.user = nullptr;
|
||||
e_itr.visit = [](llarp_xi_list_iter *, llarp_xi *xi) -> bool {
|
||||
std::cout << *xi << " ";
|
||||
return true;
|
||||
};
|
||||
llarp_xi_list_iterate(rc.exits, &e_itr);
|
||||
for(const auto &exit : rc.exits)
|
||||
{
|
||||
std::cout << exit << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "none";
|
||||
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
@ -310,7 +281,7 @@ main(int argc, char *argv[])
|
|||
auto nodedb = llarp_nodedb_new(&crypto);
|
||||
llarp_nodedb_iter itr;
|
||||
itr.visit = [](llarp_nodedb_iter *i) -> bool {
|
||||
std::cout << llarp::PubKey(i->rc->pubkey) << std::endl;
|
||||
std::cout << i->rc->pubkey << std::endl;
|
||||
return true;
|
||||
};
|
||||
if(llarp_nodedb_load_dir(nodedb, nodesdir) > 0)
|
||||
|
@ -336,27 +307,26 @@ main(int argc, char *argv[])
|
|||
return 1;
|
||||
}
|
||||
llarp_nodedb_set_dir(nodedb, nodesdir);
|
||||
llarp_rc rc;
|
||||
if(!llarp_rc_read(rcfname, &rc))
|
||||
if(!rc.Read(rcfname))
|
||||
{
|
||||
std::cout << "failed to read " << rcfname << " " << strerror(errno)
|
||||
<< std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!llarp_rc_verify_sig(&crypto, &rc))
|
||||
if(!rc.VerifySignature(&crypto))
|
||||
{
|
||||
std::cout << rcfname << " has invalid signature" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!llarp_nodedb_put_rc(nodedb, &rc))
|
||||
if(!llarp_nodedb_put_rc(nodedb, rc))
|
||||
{
|
||||
std::cout << "failed to store " << strerror(errno) << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "imported " << llarp::PubKey(rc.pubkey) << std::endl;
|
||||
std::cout << "imported " << rc.pubkey << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -376,20 +346,12 @@ main(int argc, char *argv[])
|
|||
return 0;
|
||||
}
|
||||
signal(SIGINT, handle_signal);
|
||||
|
||||
llarp_rc tmp;
|
||||
if(genMode)
|
||||
{
|
||||
printf("Creating [%s]\n", rcfname);
|
||||
// Jeff wanted tmp to be stack created
|
||||
// do we still need to zero it out?
|
||||
llarp_rc_clear(&tmp);
|
||||
// if we zero it out then
|
||||
// allocate fresh pointers that the bencoder can expect to be ready
|
||||
tmp.addrs = llarp_ai_list_new();
|
||||
tmp.exits = llarp_xi_list_new();
|
||||
// set updated timestamp
|
||||
tmp.last_updated = llarp_time_now_ms();
|
||||
rc.last_updated = llarp_time_now_ms();
|
||||
// load longterm identity
|
||||
llarp_crypto crypt;
|
||||
llarp_crypto_libsodium_init(&crypt);
|
||||
|
@ -400,123 +362,32 @@ main(int argc, char *argv[])
|
|||
llarp::SecretKey encryption;
|
||||
|
||||
llarp_findOrCreateEncryption(&crypt, encryption_keyfile.string().c_str(),
|
||||
&encryption);
|
||||
encryption);
|
||||
|
||||
llarp_rc_set_pubenckey(&tmp, llarp::seckey_topublic(encryption));
|
||||
rc.enckey = llarp::seckey_topublic(encryption);
|
||||
|
||||
// get identity public sig key
|
||||
fs::path ident_keyfile = "identity.key";
|
||||
byte_t identity[SECKEYSIZE];
|
||||
llarp::SecretKey identity;
|
||||
llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(),
|
||||
identity);
|
||||
|
||||
llarp_rc_set_pubsigkey(&tmp, llarp::seckey_topublic(identity));
|
||||
rc.pubkey = llarp::seckey_topublic(identity);
|
||||
|
||||
// this causes a segfault
|
||||
llarp_rc_sign(&crypt, identity, &tmp);
|
||||
if(!rc.Sign(&crypt, identity))
|
||||
{
|
||||
std::cout << "failed to sign" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
// set filename
|
||||
fs::path our_rc_file = rcfname;
|
||||
// write file
|
||||
llarp_rc_write(&tmp, our_rc_file.string().c_str());
|
||||
|
||||
// release memory for tmp lists
|
||||
llarp_rc_free(&tmp);
|
||||
rc.Write(our_rc_file.string().c_str());
|
||||
}
|
||||
if(updMode)
|
||||
{
|
||||
printf("rcutil.cpp - Loading [%s]\n", rcfname);
|
||||
llarp_rc rc;
|
||||
llarp_rc_clear(&rc);
|
||||
llarp_rc_read(rcfname, &rc);
|
||||
|
||||
// set updated timestamp
|
||||
rc.last_updated = llarp_time_now_ms();
|
||||
// load longterm identity
|
||||
llarp_crypto crypt;
|
||||
llarp_crypto_libsodium_init(&crypt);
|
||||
fs::path ident_keyfile = "identity.key";
|
||||
byte_t identity[SECKEYSIZE];
|
||||
llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(),
|
||||
identity);
|
||||
// get identity public key
|
||||
const uint8_t *pubkey = llarp::seckey_topublic(identity);
|
||||
llarp_rc_set_pubsigkey(&rc, pubkey);
|
||||
llarp_rc_sign(&crypt, identity, &rc);
|
||||
|
||||
// set filename
|
||||
fs::path our_rc_file_out = "update_debug.rc";
|
||||
// write file
|
||||
llarp_rc_write(&tmp, our_rc_file_out.string().c_str());
|
||||
if(rc.Read(rcfname))
|
||||
displayRC(rc);
|
||||
}
|
||||
if(listMode)
|
||||
{
|
||||
llarp_main_loadDatabase(ctx);
|
||||
llarp_nodedb_iter iter;
|
||||
iter.visit = printNode;
|
||||
llarp_main_iterateDatabase(ctx, iter);
|
||||
}
|
||||
|
||||
if(exportMode)
|
||||
{
|
||||
llarp_main_loadDatabase(ctx);
|
||||
// llarp::LogInfo("Looking for string: ", rcfname);
|
||||
|
||||
llarp::PubKey binaryPK;
|
||||
llarp::HexDecode(rcfname, binaryPK.data());
|
||||
|
||||
llarp::LogInfo("Looking for binary: ", binaryPK);
|
||||
struct llarp_rc *rc = llarp_main_getDatabase(ctx, binaryPK.data());
|
||||
if(!rc)
|
||||
{
|
||||
llarp::LogError("Can't load RC from database");
|
||||
}
|
||||
std::string filename(rcfname);
|
||||
filename.append(".signed");
|
||||
llarp::LogInfo("Writing out: ", filename);
|
||||
llarp_rc_write(rc, filename.c_str());
|
||||
}
|
||||
if(locateMode)
|
||||
{
|
||||
llarp::LogInfo("Going online");
|
||||
llarp_main_setup(ctx);
|
||||
|
||||
llarp::PubKey binaryPK;
|
||||
llarp::HexDecode(rcfname, binaryPK.data());
|
||||
|
||||
llarp::LogInfo("Queueing job");
|
||||
llarp_router_lookup_job *job = new llarp_router_lookup_job;
|
||||
job->iterative = true;
|
||||
job->found = false;
|
||||
job->hook = &HandleDHTLocate;
|
||||
llarp_rc_new(&job->result);
|
||||
memcpy(job->target, binaryPK, PUBKEYSIZE); // set job's target
|
||||
|
||||
// create query DHT request
|
||||
check_online_request *request = new check_online_request;
|
||||
request->ptr = ctx;
|
||||
request->job = job;
|
||||
request->online = false;
|
||||
request->nodes = 0;
|
||||
request->first = false;
|
||||
llarp_main_queryDHT(request);
|
||||
|
||||
llarp::LogInfo("Processing");
|
||||
// run system and wait
|
||||
llarp_main_run(ctx);
|
||||
}
|
||||
if(localMode)
|
||||
{
|
||||
llarp_rc *rc = llarp_main_getLocalRC(ctx);
|
||||
displayRC(rc);
|
||||
}
|
||||
if(readMode)
|
||||
{
|
||||
llarp_rc result;
|
||||
llarp_rc_clear(&result);
|
||||
llarp_rc_read(rcfname, &result);
|
||||
displayRC(&result);
|
||||
}
|
||||
// it's a unique_ptr, should clean up itself
|
||||
// llarp_main_free(ctx);
|
||||
return 1; // success
|
||||
return 0; // success
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <llarp/mem.h>
|
||||
#include <llarp/net.h>
|
||||
#include <stdbool.h>
|
||||
#include <llarp/bencode.hpp>
|
||||
#include <llarp/crypto.hpp>
|
||||
|
||||
#include <string>
|
||||
|
@ -16,7 +17,7 @@
|
|||
/// address information model
|
||||
namespace llarp
|
||||
{
|
||||
struct AddressInfo
|
||||
struct AddressInfo : public IBEncodeMessage
|
||||
{
|
||||
uint16_t rank;
|
||||
std::string dialect;
|
||||
|
@ -24,11 +25,21 @@ namespace llarp
|
|||
struct in6_addr ip;
|
||||
uint16_t port;
|
||||
|
||||
bool
|
||||
BEncode(llarp_buffer_t *buf) const;
|
||||
~AddressInfo();
|
||||
|
||||
bool
|
||||
BDecode(llarp_buffer_t *buf);
|
||||
BEncode(llarp_buffer_t* buf) const;
|
||||
|
||||
bool
|
||||
DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf);
|
||||
|
||||
friend std::ostream&
|
||||
operator<<(std::ostream& out, const AddressInfo& a)
|
||||
{
|
||||
char tmp[128] = {0};
|
||||
inet_ntop(AF_INET6, &a.ip, tmp, sizeof(tmp));
|
||||
return out << tmp << "." << std::to_string(a.port);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace llarp
|
||||
|
|
|
@ -57,7 +57,7 @@ typedef bool (*llarp_hmac_func)(byte_t *, llarp_buffer_t, const byte_t *);
|
|||
/// S(sig, secretkey, body)
|
||||
typedef bool (*llarp_sign_func)(byte_t *, const byte_t *, llarp_buffer_t);
|
||||
|
||||
/// V(sig, body, secretkey)
|
||||
/// V(sig, body, pubkey)
|
||||
typedef bool (*llarp_verify_func)(const byte_t *, llarp_buffer_t,
|
||||
const byte_t *);
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef LLARP_XI_HPP
|
||||
#define LLARP_XI_HPP
|
||||
#include <llarp/buffer.h>
|
||||
#include <llarp/bencode.hpp>
|
||||
#include <llarp/crypto.hpp>
|
||||
#include <llarp/net.h>
|
||||
#include <iostream>
|
||||
|
@ -15,17 +15,19 @@
|
|||
/// Exit info model
|
||||
namespace llarp
|
||||
{
|
||||
struct ExitInfo
|
||||
struct ExitInfo : public IBEncodeMessage
|
||||
{
|
||||
struct in6_addr address;
|
||||
struct in6_addr netmask;
|
||||
PubKey pubkey;
|
||||
|
||||
~ExitInfo();
|
||||
|
||||
bool
|
||||
BEncode(llarp_buffer_t *buf) const;
|
||||
|
||||
bool
|
||||
BDecode(llarp_buffer_t *buf);
|
||||
DecodeKey(llarp_buffer_t k, llarp_buffer_t *buf);
|
||||
|
||||
friend std::ostream &
|
||||
operator<<(std::ostream &out, const ExitInfo &xi)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "address_info.hpp"
|
||||
#include <llarp/address_info.hpp>
|
||||
#ifndef _WIN32
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
@ -6,274 +6,138 @@
|
|||
#include <llarp/mem.h>
|
||||
#include <llarp/string.h>
|
||||
|
||||
static bool
|
||||
llarp_ai_decode_key(struct dict_reader *r, llarp_buffer_t *key)
|
||||
namespace llarp
|
||||
{
|
||||
uint64_t i;
|
||||
char tmp[128] = {0};
|
||||
|
||||
llarp_buffer_t strbuf;
|
||||
llarp_ai *ai = static_cast< llarp_ai * >(r->user);
|
||||
|
||||
// done
|
||||
if(!key)
|
||||
return true;
|
||||
|
||||
// rank
|
||||
if(llarp_buffer_eq(*key, "c"))
|
||||
AddressInfo::~AddressInfo()
|
||||
{
|
||||
if(!bencode_read_integer(r->buffer, &i))
|
||||
return false;
|
||||
|
||||
if(i > 65536 || i <= 0)
|
||||
return false;
|
||||
|
||||
ai->rank = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
// dialect
|
||||
if(llarp_buffer_eq(*key, "d"))
|
||||
bool
|
||||
AddressInfo::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
||||
{
|
||||
if(!bencode_read_string(r->buffer, &strbuf))
|
||||
return false;
|
||||
uint64_t i;
|
||||
char tmp[128] = {0};
|
||||
|
||||
if(strbuf.sz >= sizeof(ai->dialect))
|
||||
return false;
|
||||
llarp_buffer_t strbuf;
|
||||
|
||||
memcpy(ai->dialect, strbuf.base, strbuf.sz);
|
||||
ai->dialect[strbuf.sz] = 0;
|
||||
return true;
|
||||
// rank
|
||||
if(llarp_buffer_eq(key, "c"))
|
||||
{
|
||||
if(!bencode_read_integer(buf, &i))
|
||||
return false;
|
||||
|
||||
if(i > 65536 || i <= 0)
|
||||
return false;
|
||||
|
||||
rank = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
// dialect
|
||||
if(llarp_buffer_eq(key, "d"))
|
||||
{
|
||||
if(!bencode_read_string(buf, &strbuf))
|
||||
return false;
|
||||
if(strbuf.sz > sizeof(tmp))
|
||||
return false;
|
||||
memcpy(tmp, strbuf.base, strbuf.sz);
|
||||
tmp[strbuf.sz] = 0;
|
||||
dialect = std::string(tmp);
|
||||
return true;
|
||||
}
|
||||
|
||||
// encryption public key
|
||||
if(llarp_buffer_eq(key, "e"))
|
||||
{
|
||||
if(!bencode_read_string(buf, &strbuf))
|
||||
return false;
|
||||
|
||||
if(strbuf.sz != PUBKEYSIZE)
|
||||
return false;
|
||||
|
||||
pubkey = strbuf.base;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ip address
|
||||
if(llarp_buffer_eq(key, "i"))
|
||||
{
|
||||
if(!bencode_read_string(buf, &strbuf))
|
||||
return false;
|
||||
|
||||
if(strbuf.sz >= sizeof(tmp))
|
||||
return false;
|
||||
|
||||
memcpy(tmp, strbuf.base, strbuf.sz);
|
||||
tmp[strbuf.sz] = 0;
|
||||
return inet_pton(AF_INET6, tmp, &ip.s6_addr[0]) == 1;
|
||||
}
|
||||
|
||||
// port
|
||||
if(llarp_buffer_eq(key, "p"))
|
||||
{
|
||||
if(!bencode_read_integer(buf, &i))
|
||||
return false;
|
||||
|
||||
if(i > 65536 || i <= 0)
|
||||
return false;
|
||||
|
||||
port = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
// version
|
||||
if(llarp_buffer_eq(key, "v"))
|
||||
{
|
||||
if(!bencode_read_integer(buf, &i))
|
||||
return false;
|
||||
return i == LLARP_PROTO_VERSION;
|
||||
}
|
||||
|
||||
// bad key
|
||||
return false;
|
||||
}
|
||||
|
||||
// encryption public key
|
||||
if(llarp_buffer_eq(*key, "e"))
|
||||
bool
|
||||
AddressInfo::BEncode(llarp_buffer_t *buff) const
|
||||
{
|
||||
if(!bencode_read_string(r->buffer, &strbuf))
|
||||
char ipbuff[128] = {0};
|
||||
const char *ipstr;
|
||||
if(!bencode_start_dict(buff))
|
||||
return false;
|
||||
/* rank */
|
||||
if(!bencode_write_bytestring(buff, "c", 1))
|
||||
return false;
|
||||
if(!bencode_write_uint64(buff, rank))
|
||||
return false;
|
||||
/* dialect */
|
||||
if(!bencode_write_bytestring(buff, "d", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, dialect.c_str(), dialect.size()))
|
||||
return false;
|
||||
/* encryption key */
|
||||
if(!bencode_write_bytestring(buff, "e", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, pubkey, PUBKEYSIZE))
|
||||
return false;
|
||||
/** ip */
|
||||
ipstr = inet_ntop(AF_INET6, &ip, ipbuff, sizeof(ipbuff));
|
||||
if(!ipstr)
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, "i", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff))))
|
||||
return false;
|
||||
/** port */
|
||||
if(!bencode_write_bytestring(buff, "p", 1))
|
||||
return false;
|
||||
if(!bencode_write_uint64(buff, port))
|
||||
return false;
|
||||
|
||||
if(strbuf.sz != PUBKEYSIZE)
|
||||
/** version */
|
||||
if(!bencode_write_version_entry(buff))
|
||||
return false;
|
||||
|
||||
memcpy(ai->enc_key, strbuf.base, PUBKEYSIZE);
|
||||
return true;
|
||||
/** end */
|
||||
return bencode_end(buff);
|
||||
}
|
||||
|
||||
// ip address
|
||||
if(llarp_buffer_eq(*key, "i"))
|
||||
{
|
||||
if(!bencode_read_string(r->buffer, &strbuf))
|
||||
return false;
|
||||
|
||||
if(strbuf.sz >= sizeof(tmp))
|
||||
return false;
|
||||
|
||||
memcpy(tmp, strbuf.base, strbuf.sz);
|
||||
tmp[strbuf.sz] = 0;
|
||||
return inet_pton(AF_INET6, tmp, &ai->ip.s6_addr[0]) == 1;
|
||||
}
|
||||
|
||||
// port
|
||||
if(llarp_buffer_eq(*key, "p"))
|
||||
{
|
||||
if(!bencode_read_integer(r->buffer, &i))
|
||||
return false;
|
||||
|
||||
if(i > 65536 || i <= 0)
|
||||
return false;
|
||||
|
||||
ai->port = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
// version
|
||||
if(llarp_buffer_eq(*key, "v"))
|
||||
{
|
||||
if(!bencode_read_integer(r->buffer, &i))
|
||||
return false;
|
||||
return i == LLARP_PROTO_VERSION;
|
||||
}
|
||||
|
||||
// bad key
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
llarp_ai_list_bdecode_item(struct list_reader *r, bool more)
|
||||
{
|
||||
if(!more)
|
||||
return true;
|
||||
llarp_ai_list *l = static_cast< llarp_ai_list * >(r->user);
|
||||
llarp_ai ai;
|
||||
|
||||
if(!llarp_ai_bdecode(&ai, r->buffer))
|
||||
return false;
|
||||
|
||||
llarp_ai_list_pushback(l, &ai);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
llarp_ai_list_iter_bencode(struct llarp_ai_list_iter *iter, struct llarp_ai *ai)
|
||||
{
|
||||
return llarp_ai_bencode(ai, static_cast< llarp_buffer_t * >(iter->user));
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t *buff)
|
||||
{
|
||||
#ifndef _MSC_VER
|
||||
struct dict_reader reader = {
|
||||
.buffer = nullptr, .user = ai, .on_key = &llarp_ai_decode_key};
|
||||
#else
|
||||
struct dict_reader reader = {nullptr, ai, &llarp_ai_decode_key};
|
||||
#endif
|
||||
return bencode_read_dict(buff, &reader);
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff)
|
||||
{
|
||||
char ipbuff[128] = {0};
|
||||
const char *ipstr;
|
||||
if(!bencode_start_dict(buff))
|
||||
return false;
|
||||
/* rank */
|
||||
if(!bencode_write_bytestring(buff, "c", 1))
|
||||
return false;
|
||||
if(!bencode_write_uint64(buff, ai->rank))
|
||||
return false;
|
||||
/* dialect */
|
||||
if(!bencode_write_bytestring(buff, "d", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, ai->dialect,
|
||||
strnlen(ai->dialect, sizeof(ai->dialect))))
|
||||
return false;
|
||||
/* encryption key */
|
||||
if(!bencode_write_bytestring(buff, "e", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, ai->enc_key, PUBKEYSIZE))
|
||||
return false;
|
||||
/** ip */
|
||||
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
|
||||
if(!ipstr)
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, "i", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff))))
|
||||
return false;
|
||||
/** port */
|
||||
if(!bencode_write_bytestring(buff, "p", 1))
|
||||
return false;
|
||||
if(!bencode_write_uint64(buff, ai->port))
|
||||
return false;
|
||||
|
||||
/** version */
|
||||
if(!bencode_write_version_entry(buff))
|
||||
return false;
|
||||
/** end */
|
||||
return bencode_end(buff);
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_ai_list_bencode(struct llarp_ai_list *l, llarp_buffer_t *buff)
|
||||
{
|
||||
if(!bencode_start_list(buff))
|
||||
return false;
|
||||
#ifndef _MSC_VER
|
||||
struct llarp_ai_list_iter ai_itr = {
|
||||
.user = buff, .list = nullptr, .visit = &llarp_ai_list_iter_bencode};
|
||||
#else
|
||||
struct llarp_ai_list_iter ai_itr = {buff, nullptr,
|
||||
&llarp_ai_list_iter_bencode};
|
||||
#endif
|
||||
llarp_ai_list_iterate(l, &ai_itr);
|
||||
return bencode_end(buff);
|
||||
}
|
||||
|
||||
struct llarp_ai_list *
|
||||
llarp_ai_list_new()
|
||||
{
|
||||
return new llarp_ai_list();
|
||||
}
|
||||
|
||||
void
|
||||
llarp_ai_list_free(struct llarp_ai_list *l)
|
||||
{
|
||||
if(l)
|
||||
{
|
||||
l->list.clear();
|
||||
delete l;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
llarp_ai_copy(struct llarp_ai *dst, struct llarp_ai *src)
|
||||
{
|
||||
memcpy(dst, src, sizeof(struct llarp_ai));
|
||||
}
|
||||
|
||||
void
|
||||
llarp_ai_list_copy(struct llarp_ai_list *dst, struct llarp_ai_list *src)
|
||||
{
|
||||
dst->list.clear();
|
||||
for(auto &itr : src->list)
|
||||
dst->list.emplace_back(itr);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_ai_list_pushback(struct llarp_ai_list *l, struct llarp_ai *ai)
|
||||
{
|
||||
l->list.push_back(*ai);
|
||||
}
|
||||
|
||||
size_t
|
||||
llarp_ai_list_size(struct llarp_ai_list *l)
|
||||
{
|
||||
if(l)
|
||||
{
|
||||
return l->list.size();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
llarp_ai_list_iterate(struct llarp_ai_list *l, struct llarp_ai_list_iter *itr)
|
||||
{
|
||||
if(l)
|
||||
{
|
||||
itr->list = l;
|
||||
for(auto &ai : l->list)
|
||||
if(!itr->visit(itr, &ai))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_ai_list_index(struct llarp_ai_list *l, ssize_t idx, struct llarp_ai *dst)
|
||||
{
|
||||
// TODO: implement negative indexes
|
||||
if(idx < 0)
|
||||
return false;
|
||||
|
||||
size_t i = idx;
|
||||
|
||||
if(l->list.size() > i)
|
||||
{
|
||||
llarp_ai_copy(dst, &l->list[i]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_ai_list_bdecode(struct llarp_ai_list *l, llarp_buffer_t *buff)
|
||||
{
|
||||
#ifndef _MSC_VER
|
||||
struct list_reader r = {
|
||||
.buffer = nullptr, .user = l, .on_item = &llarp_ai_list_bdecode_item};
|
||||
#else
|
||||
struct list_reader r = {nullptr, l, &llarp_ai_list_bdecode_item};
|
||||
#endif
|
||||
return bencode_read_list(buff, &r);
|
||||
}
|
||||
} // namespace llarp
|
||||
|
|
|
@ -329,76 +329,6 @@ extern "C"
|
|||
llarp_logic_stop_timer(ptr->ctx->router->logic);
|
||||
}
|
||||
|
||||
int
|
||||
llarp_main_loadDatabase(struct llarp_main *ptr)
|
||||
{
|
||||
return ptr->ctx->LoadDatabase();
|
||||
}
|
||||
|
||||
int
|
||||
llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i)
|
||||
{
|
||||
return ptr->ctx->IterateDatabase(i);
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc)
|
||||
{
|
||||
return ptr->ctx->PutDatabase(rc);
|
||||
}
|
||||
|
||||
struct llarp_rc *
|
||||
llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk)
|
||||
{
|
||||
return ptr->ctx->GetDatabase(pk);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_main_checkOnline(void *u, uint64_t orig, uint64_t left)
|
||||
{
|
||||
// llarp::LogInfo("checkOnline - check ", left);
|
||||
if(left)
|
||||
return;
|
||||
struct check_online_request *request =
|
||||
static_cast< struct check_online_request * >(u);
|
||||
// llarp::LogDebug("checkOnline - running");
|
||||
// llarp::LogInfo("checkOnline - DHT nodes ",
|
||||
// request->ptr->ctx->router->dht->impl.nodes->nodes.size());
|
||||
request->online = false;
|
||||
request->nodes = request->ptr->ctx->router->dht->impl.nodes->nodes.size();
|
||||
if(request->ptr->ctx->router->dht->impl.nodes->nodes.size())
|
||||
{
|
||||
// llarp::LogInfo("checkOnline - Going to say we're online");
|
||||
request->online = true;
|
||||
}
|
||||
request->hook(request);
|
||||
// reschedue our self
|
||||
llarp_main_queryDHT(request);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_main_queryDHT_online(struct check_online_request *request)
|
||||
{
|
||||
// Info("llarp_main_queryDHT_online: ", request->online ? "online" :
|
||||
// "offline");
|
||||
if(request->online && !request->first)
|
||||
{
|
||||
request->first = true;
|
||||
llarp::LogInfo("llarp_main_queryDHT_online - We're online");
|
||||
llarp::LogInfo("llarp_main_queryDHT_online - Querying DHT");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
llarp_main_queryDHT(struct check_online_request *request)
|
||||
{
|
||||
// llarp::LogInfo("llarp_main_queryDHT - setting up timer");
|
||||
request->hook = &llarp_main_queryDHT_online;
|
||||
llarp_logic_call_later(request->ptr->ctx->router->logic,
|
||||
{1000, request, &llarp_main_checkOnline});
|
||||
// llarp_dht_lookup_router(ptr->ctx->router->dht, job);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_main_free(struct llarp_main *ptr)
|
||||
{
|
||||
|
|
|
@ -636,7 +636,7 @@ namespace llarp
|
|||
return parent->DHTSendTo(peer, msg);
|
||||
}
|
||||
|
||||
void
|
||||
virtual void
|
||||
SendReply()
|
||||
{
|
||||
if(resultHandler)
|
||||
|
@ -652,6 +652,56 @@ namespace llarp
|
|||
}
|
||||
};
|
||||
|
||||
struct LocalRouterLookup : public RecursiveRouterLookup
|
||||
{
|
||||
PathID_t localPath;
|
||||
|
||||
LocalRouterLookup(const PathID_t &path, uint64_t txid,
|
||||
const RouterID &target, Context *ctx)
|
||||
: RecursiveRouterLookup(TXOwner{ctx->OurKey(), txid}, target, ctx,
|
||||
nullptr)
|
||||
, localPath(path)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
SendReply()
|
||||
{
|
||||
auto path =
|
||||
parent->router->paths.GetByUpstream(parent->OurKey(), localPath);
|
||||
if(!path)
|
||||
{
|
||||
llarp::LogWarn(
|
||||
"did not send reply for relayed dht request, no such local path "
|
||||
"for pathid=",
|
||||
localPath);
|
||||
return;
|
||||
}
|
||||
routing::DHTMessage msg;
|
||||
msg.M.push_back(new GotRouterMessage(parent->OurKey(), whoasked.txid,
|
||||
valuesFound, true));
|
||||
if(!path->SendRoutingMessage(&msg, parent->router))
|
||||
{
|
||||
llarp::LogWarn(
|
||||
"failed to send routing message when informing result of dht "
|
||||
"request, pathid=",
|
||||
localPath);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
Context::LookupRouterForPath(const RouterID &target, uint64_t txid,
|
||||
const llarp::PathID_t &path,
|
||||
const Key_t &askpeer)
|
||||
|
||||
{
|
||||
TXOwner peer(askpeer, ++ids);
|
||||
auto tx = pendingRouterLookups.NewTX(
|
||||
peer, target, new LocalRouterLookup(path, txid, target, this));
|
||||
tx->Start(peer);
|
||||
}
|
||||
|
||||
void
|
||||
Context::LookupRouterRecursive(const RouterID &target,
|
||||
const Key_t &whoasked, uint64_t txid,
|
||||
|
|
|
@ -3,197 +3,49 @@
|
|||
#endif
|
||||
|
||||
#include <llarp/bencode.h>
|
||||
#include <llarp/exit_info.h>
|
||||
#include <llarp/exit_info.hpp>
|
||||
#include <llarp/mem.h>
|
||||
#include <llarp/string.h>
|
||||
#include <list>
|
||||
|
||||
struct llarp_xi_list
|
||||
namespace llarp
|
||||
{
|
||||
std::list< llarp_xi > list;
|
||||
};
|
||||
|
||||
struct llarp_xi_list *
|
||||
llarp_xi_list_new()
|
||||
{
|
||||
return new llarp_xi_list;
|
||||
}
|
||||
|
||||
void
|
||||
llarp_xi_list_free(struct llarp_xi_list *l)
|
||||
{
|
||||
if(l)
|
||||
ExitInfo::~ExitInfo()
|
||||
{
|
||||
delete l;
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
llarp_xi_iter_bencode(struct llarp_xi_list_iter *iter, struct llarp_xi *xi)
|
||||
{
|
||||
return llarp_xi_bencode(xi, static_cast< llarp_buffer_t * >(iter->user));
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_xi_list_bencode(struct llarp_xi_list *l, llarp_buffer_t *buff)
|
||||
{
|
||||
if(!bencode_start_list(buff))
|
||||
return false;
|
||||
struct llarp_xi_list_iter xi_itr = {buff, nullptr, &llarp_xi_iter_bencode};
|
||||
llarp_xi_list_iterate(l, &xi_itr);
|
||||
return bencode_end(buff);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_xi_list_iterate(struct llarp_xi_list *l, struct llarp_xi_list_iter *iter)
|
||||
{
|
||||
iter->list = l;
|
||||
for(auto &item : l->list)
|
||||
if(!iter->visit(iter, &item))
|
||||
return;
|
||||
}
|
||||
|
||||
size_t
|
||||
llarp_xi_list_size(const struct llarp_xi_list *l)
|
||||
{
|
||||
if(l)
|
||||
return l->list.size();
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_xi_bencode(const struct llarp_xi *xi, llarp_buffer_t *buff)
|
||||
{
|
||||
char addr_buff[128] = {0};
|
||||
const char *addr;
|
||||
if(!bencode_start_dict(buff))
|
||||
return false;
|
||||
|
||||
/** address */
|
||||
addr = inet_ntop(AF_INET6, &xi->address, addr_buff, sizeof(addr_buff));
|
||||
if(!addr)
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, "a", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
|
||||
return false;
|
||||
|
||||
/** netmask */
|
||||
addr = inet_ntop(AF_INET6, &xi->netmask, addr_buff, sizeof(addr_buff));
|
||||
if(!addr)
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, "b", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
|
||||
return false;
|
||||
|
||||
/** public key */
|
||||
if(!bencode_write_bytestring(buff, "k", 1))
|
||||
return false;
|
||||
if(!bencode_write_bytestring(buff, xi->pubkey, PUBKEYSIZE))
|
||||
return false;
|
||||
|
||||
/** version */
|
||||
if(!bencode_write_version_entry(buff))
|
||||
return false;
|
||||
|
||||
return bencode_end(buff);
|
||||
}
|
||||
|
||||
static bool
|
||||
llarp_xi_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
|
||||
{
|
||||
if(!key)
|
||||
return true;
|
||||
|
||||
llarp_xi *xi = static_cast< llarp_xi * >(r->user);
|
||||
llarp_buffer_t strbuf;
|
||||
uint64_t v;
|
||||
char tmp[128] = {0};
|
||||
|
||||
// address
|
||||
if(llarp_buffer_eq(*key, "a"))
|
||||
{
|
||||
if(!bencode_read_string(r->buffer, &strbuf))
|
||||
return false;
|
||||
if(strbuf.sz >= sizeof(tmp))
|
||||
return false;
|
||||
memcpy(tmp, strbuf.base, strbuf.sz);
|
||||
return inet_pton(AF_INET6, tmp, xi->address.s6_addr) == 1;
|
||||
}
|
||||
|
||||
if(llarp_buffer_eq(*key, "b"))
|
||||
bool
|
||||
ExitInfo::BEncode(llarp_buffer_t* buf) const
|
||||
{
|
||||
if(!bencode_read_string(r->buffer, &strbuf))
|
||||
char tmp[128] = {0};
|
||||
if(!bencode_start_dict(buf))
|
||||
return false;
|
||||
if(strbuf.sz >= sizeof(tmp))
|
||||
|
||||
if(!inet_ntop(AF_INET6, &address, tmp, sizeof(tmp)))
|
||||
return false;
|
||||
memcpy(tmp, strbuf.base, strbuf.sz);
|
||||
return inet_pton(AF_INET6, tmp, xi->netmask.s6_addr) == 1;
|
||||
if(!BEncodeWriteDictString("a", std::string(tmp), buf))
|
||||
return false;
|
||||
|
||||
if(!inet_ntop(AF_INET6, &netmask, tmp, sizeof(tmp)))
|
||||
return false;
|
||||
if(!BEncodeWriteDictString("b", std::string(tmp), buf))
|
||||
return false;
|
||||
|
||||
if(!BEncodeWriteDictEntry("k", pubkey, buf))
|
||||
return false;
|
||||
|
||||
if(!BEncodeWriteDictInt("v", version, buf))
|
||||
return false;
|
||||
|
||||
return bencode_end(buf);
|
||||
}
|
||||
|
||||
if(llarp_buffer_eq(*key, "k"))
|
||||
bool
|
||||
ExitInfo::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
|
||||
{
|
||||
if(!bencode_read_string(r->buffer, &strbuf))
|
||||
return false;
|
||||
if(strbuf.sz != PUBKEYSIZE)
|
||||
return false;
|
||||
memcpy(xi->pubkey, strbuf.base, PUBKEYSIZE);
|
||||
return true;
|
||||
bool read = false;
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
if(llarp_buffer_eq(*key, "v"))
|
||||
{
|
||||
if(!bencode_read_integer(r->buffer, &v))
|
||||
return false;
|
||||
return v == LLARP_PROTO_VERSION;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_xi_bdecode(struct llarp_xi *xi, llarp_buffer_t *buf)
|
||||
{
|
||||
struct dict_reader r = {buf, xi, &llarp_xi_decode_dict};
|
||||
return bencode_read_dict(buf, &r);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_xi_list_pushback(struct llarp_xi_list *l, struct llarp_xi *xi)
|
||||
{
|
||||
l->list.emplace_back();
|
||||
llarp_xi_copy(&l->list.back(), xi);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_xi_copy(struct llarp_xi *dst, struct llarp_xi *src)
|
||||
{
|
||||
memcpy(dst, src, sizeof(struct llarp_xi));
|
||||
}
|
||||
|
||||
static bool
|
||||
llarp_xi_list_decode_item(struct list_reader *r, bool more)
|
||||
{
|
||||
if(!more)
|
||||
return true;
|
||||
|
||||
llarp_xi_list *l = static_cast< llarp_xi_list * >(r->user);
|
||||
l->list.emplace_back();
|
||||
return llarp_xi_bdecode(&l->list.back(), r->buffer);
|
||||
}
|
||||
|
||||
void
|
||||
llarp_xi_list_copy(struct llarp_xi_list *dst, struct llarp_xi_list *src)
|
||||
{
|
||||
dst->list.clear();
|
||||
for(auto &itr : src->list)
|
||||
dst->list.emplace_back(itr);
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_xi_list_bdecode(struct llarp_xi_list *l, llarp_buffer_t *buff)
|
||||
{
|
||||
list_reader r = {buff, l, &llarp_xi_list_decode_item};
|
||||
return bencode_read_list(buff, &r);
|
||||
}
|
||||
} // namespace llarp
|
||||
|
|
|
@ -276,6 +276,12 @@ llarp_nodedb_free(struct llarp_nodedb **n)
|
|||
}
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_nodedb_put_rc(struct llarp_nodedb *n, const llarp::RouterContact &rc)
|
||||
{
|
||||
return n->Insert(rc);
|
||||
}
|
||||
|
||||
bool
|
||||
llarp_nodedb_ensure_dir(const char *dir)
|
||||
{
|
||||
|
|
|
@ -906,7 +906,7 @@ llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath,
|
|||
// C++ ...
|
||||
bool
|
||||
llarp_findOrCreateEncryption(llarp_crypto *crypto, const char *fpath,
|
||||
llarp::SecretKey *encryption)
|
||||
llarp::SecretKey &encryption)
|
||||
{
|
||||
llarp::LogDebug("find or create ", fpath);
|
||||
fs::path path(fpath);
|
||||
|
@ -914,18 +914,18 @@ llarp_findOrCreateEncryption(llarp_crypto *crypto, const char *fpath,
|
|||
if(!fs::exists(path, ec))
|
||||
{
|
||||
llarp::LogInfo("generating new encryption key");
|
||||
crypto->encryption_keygen(*encryption);
|
||||
crypto->encryption_keygen(encryption);
|
||||
std::ofstream f(path.string(), std::ios::binary);
|
||||
if(f.is_open())
|
||||
{
|
||||
f.write((char *)encryption, SECKEYSIZE);
|
||||
f.write((char *)encryption.data(), SECKEYSIZE);
|
||||
}
|
||||
}
|
||||
|
||||
std::ifstream f(path.string(), std::ios::binary);
|
||||
if(f.is_open())
|
||||
{
|
||||
f.read((char *)encryption, SECKEYSIZE);
|
||||
f.read((char *)encryption.data(), SECKEYSIZE);
|
||||
return true;
|
||||
}
|
||||
llarp::LogInfo("failed to get encryption key");
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include "logger.hpp"
|
||||
#include "mem.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
bool
|
||||
|
@ -95,4 +97,107 @@ namespace llarp
|
|||
return read;
|
||||
}
|
||||
|
||||
bool
|
||||
RouterContact::IsPublicRouter() const
|
||||
{
|
||||
return addrs.size() > 0;
|
||||
}
|
||||
|
||||
bool
|
||||
RouterContact::HasNick() const
|
||||
{
|
||||
return nickname[0] != 0;
|
||||
}
|
||||
|
||||
void
|
||||
RouterContact::SetNick(const std::string &nick)
|
||||
{
|
||||
nickname.Zero();
|
||||
memcpy(nickname, nick.c_str(), std::min(nick.size(), nickname.size()));
|
||||
}
|
||||
|
||||
std::string
|
||||
RouterContact::Nick() const
|
||||
{
|
||||
const char *n = (const char *)nickname.data();
|
||||
return std::string(n, strnlen(n, nickname.size()));
|
||||
}
|
||||
|
||||
bool
|
||||
RouterContact::Sign(llarp_crypto *crypto, const SecretKey &secretkey)
|
||||
{
|
||||
byte_t tmp[MAX_RC_SIZE] = {0};
|
||||
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
||||
signature.Zero();
|
||||
if(!BEncode(&buf))
|
||||
return false;
|
||||
buf.sz = buf.cur - buf.base;
|
||||
buf.cur = buf.base;
|
||||
return crypto->sign(signature, secretkey, buf);
|
||||
}
|
||||
|
||||
bool
|
||||
RouterContact::VerifySignature(llarp_crypto *crypto) const
|
||||
{
|
||||
RouterContact copy = *this;
|
||||
copy.signature.Zero();
|
||||
byte_t tmp[MAX_RC_SIZE] = {0};
|
||||
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
||||
if(!copy.BEncode(&buf))
|
||||
return false;
|
||||
buf.sz = buf.cur - buf.base;
|
||||
buf.cur = buf.base;
|
||||
return crypto->verify(signature, buf, pubkey);
|
||||
}
|
||||
|
||||
bool
|
||||
RouterContact::Write(const char *fname) const
|
||||
{
|
||||
byte_t tmp[MAX_RC_SIZE] = {0};
|
||||
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
||||
if(!BEncode(&buf))
|
||||
return false;
|
||||
buf.sz = buf.cur - buf.base;
|
||||
buf.cur = buf.base;
|
||||
{
|
||||
std::ofstream f;
|
||||
f.open(fname);
|
||||
if(!f.is_open())
|
||||
return false;
|
||||
f.write((char *)buf.base, buf.sz);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
RouterContact::Read(const char *fname)
|
||||
{
|
||||
byte_t tmp[MAX_RC_SIZE] = {0};
|
||||
{
|
||||
std::ifstream f;
|
||||
f.open(fname);
|
||||
if(!f.is_open())
|
||||
return false;
|
||||
f.seekg(0, std::ios::end);
|
||||
auto l = f.tellg();
|
||||
f.seekg(0, std::ios::beg);
|
||||
f.read((char *)tmp, l);
|
||||
}
|
||||
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
||||
return BDecode(&buf);
|
||||
}
|
||||
|
||||
RouterContact &
|
||||
RouterContact::operator=(const RouterContact &other)
|
||||
{
|
||||
addrs = other.addrs;
|
||||
signature = other.signature;
|
||||
exits = other.exits;
|
||||
last_updated = other.last_updated;
|
||||
enckey = other.enckey;
|
||||
pubkey = other.pubkey;
|
||||
nickname = other.nickname;
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace llarp
|
||||
|
|
Loading…
Reference in New Issue