2019-01-05 01:49:06 +01:00
|
|
|
#include <router_contact.hpp>
|
|
|
|
|
2019-01-10 20:41:51 +01:00
|
|
|
#include <constants/version.hpp>
|
2019-01-13 15:00:50 +01:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-11 02:42:02 +01:00
|
|
|
#include <net/net.hpp>
|
2019-01-10 20:41:51 +01:00
|
|
|
#include <util/bencode.hpp>
|
|
|
|
#include <util/buffer.hpp>
|
2019-09-01 14:10:49 +02:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-01-10 20:41:51 +01:00
|
|
|
#include <util/mem.hpp>
|
2019-02-25 00:46:37 +01:00
|
|
|
#include <util/printer.hpp>
|
2019-01-10 20:41:51 +01:00
|
|
|
#include <util/time.hpp>
|
2018-05-27 15:42:55 +02:00
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
#include <fstream>
|
2019-06-24 18:39:03 +02:00
|
|
|
#include <util/fs.hpp>
|
2018-08-31 14:46:54 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
namespace llarp
|
2018-07-08 15:26:24 +02:00
|
|
|
{
|
2019-01-05 01:49:06 +01:00
|
|
|
NetID &
|
|
|
|
NetID::DefaultValue()
|
|
|
|
{
|
|
|
|
static NetID defaultID(
|
2019-12-12 03:32:27 +01:00
|
|
|
reinterpret_cast< const byte_t * >(llarp::DEFAULT_NETID));
|
2019-01-05 01:49:06 +01:00
|
|
|
return defaultID;
|
|
|
|
}
|
2018-12-21 14:08:01 +01:00
|
|
|
|
2019-08-27 01:29:17 +02:00
|
|
|
bool RouterContact::BlockBogons = true;
|
2018-12-17 21:46:08 +01:00
|
|
|
|
2018-12-19 17:19:16 +01:00
|
|
|
#ifdef TESTNET
|
|
|
|
// 1 minute for testnet
|
2020-02-24 20:40:45 +01:00
|
|
|
llarp_time_t RouterContact::Lifetime = 1min;
|
2018-12-19 17:19:16 +01:00
|
|
|
#else
|
2018-12-19 18:48:29 +01:00
|
|
|
/// 1 day for real network
|
2020-02-24 20:40:45 +01:00
|
|
|
llarp_time_t RouterContact::Lifetime = 24h;
|
2018-12-19 17:19:16 +01:00
|
|
|
#endif
|
2020-01-18 22:07:21 +01:00
|
|
|
/// an RC inserted long enough ago (4 hrs) is considered stale and is removed
|
2020-02-24 20:40:45 +01:00
|
|
|
llarp_time_t RouterContact::StaleInsertionAge = 4h;
|
2020-01-18 22:37:42 +01:00
|
|
|
/// update RCs shortly before they are about to expire
|
|
|
|
llarp_time_t RouterContact::UpdateInterval =
|
2020-02-24 20:40:45 +01:00
|
|
|
RouterContact::StaleInsertionAge - 5min;
|
2019-01-02 23:21:29 +01:00
|
|
|
|
2019-08-27 01:29:17 +02:00
|
|
|
NetID::NetID(const byte_t *val)
|
2019-01-05 01:49:06 +01:00
|
|
|
{
|
|
|
|
size_t len = strnlen(reinterpret_cast< const char * >(val), size());
|
|
|
|
std::copy(val, val + len, begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
NetID::NetID() : NetID(DefaultValue().data())
|
2018-12-19 17:17:41 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NetID::operator==(const NetID &other) const
|
|
|
|
{
|
2018-12-28 16:04:05 +01:00
|
|
|
return ToString() == other.ToString();
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
NetID::ToString() const
|
|
|
|
{
|
2019-01-02 02:03:53 +01:00
|
|
|
auto term = std::find(begin(), end(), '\0');
|
|
|
|
return std::string(begin(), term);
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NetID::BDecode(llarp_buffer_t *buf)
|
|
|
|
{
|
|
|
|
Zero();
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if(!bencode_read_string(buf, &strbuf))
|
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
|
2018-12-28 16:04:05 +01:00
|
|
|
if(strbuf.sz > size())
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2019-01-02 02:03:53 +01:00
|
|
|
|
2019-01-02 02:04:04 +01:00
|
|
|
std::copy(strbuf.base, strbuf.base + strbuf.sz, begin());
|
2018-12-19 17:17:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NetID::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
2019-01-02 02:03:53 +01:00
|
|
|
auto term = std::find(begin(), end(), '\0');
|
2019-04-19 20:24:33 +02:00
|
|
|
return bencode_write_bytestring(buf, data(), std::distance(begin(), term));
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
bool
|
|
|
|
RouterContact::BEncode(llarp_buffer_t *buf) const
|
2018-05-13 20:07:36 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write dict begin */
|
|
|
|
if(!bencode_start_dict(buf))
|
2018-06-10 16:05:48 +02:00
|
|
|
return false;
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write ai if they exist */
|
|
|
|
if(!bencode_write_bytestring(buf, "a", 1))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!BEncodeWriteList(addrs.begin(), addrs.end(), buf))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
|
2018-12-19 17:17:41 +01:00
|
|
|
/* write netid */
|
|
|
|
if(!bencode_write_bytestring(buf, "i", 1))
|
|
|
|
return false;
|
2018-12-21 14:08:01 +01:00
|
|
|
if(!netID.BEncode(buf))
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write signing pubkey */
|
|
|
|
if(!bencode_write_bytestring(buf, "k", 1))
|
2018-05-22 20:41:38 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!pubkey.BEncode(buf))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
std::string nick = Nick();
|
2019-08-27 01:29:17 +02:00
|
|
|
if(!nick.empty())
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write nickname */
|
|
|
|
if(!bencode_write_bytestring(buf, "n", 1))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!bencode_write_bytestring(buf, nick.c_str(), nick.size()))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-05-22 17:54:19 +02:00
|
|
|
}
|
2018-05-13 20:07:36 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write encryption pubkey */
|
|
|
|
if(!bencode_write_bytestring(buf, "p", 1))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!enckey.BEncode(buf))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
2020-01-25 17:28:07 +01:00
|
|
|
// write router version if present
|
|
|
|
if(routerVersion.has_value())
|
|
|
|
{
|
|
|
|
if(not BEncodeWriteDictEntry("r", routerVersion.value(), buf))
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write last updated */
|
2019-01-24 14:18:15 +01:00
|
|
|
if(!bencode_write_bytestring(buf, "u", 1))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2020-02-24 20:40:45 +01:00
|
|
|
if(!bencode_write_uint64(buf, last_updated.count()))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2018-05-13 20:07:36 +02:00
|
|
|
|
2019-11-22 18:39:35 +01:00
|
|
|
/* write versions */
|
|
|
|
if(!bencode_write_uint64_entry(buf, "v", 1, version))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2018-05-25 19:52:10 +02:00
|
|
|
|
2018-11-28 15:58:38 +01:00
|
|
|
/* write xi if they exist */
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!bencode_write_bytestring(buf, "x", 1))
|
2018-05-22 17:54:19 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!BEncodeWriteList(exits.begin(), exits.end(), buf))
|
2018-05-22 17:54:19 +02:00
|
|
|
return false;
|
2018-06-10 16:05:48 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write signature */
|
|
|
|
if(!bencode_write_bytestring(buf, "z", 1))
|
|
|
|
return false;
|
|
|
|
if(!signature.BEncode(buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
2018-05-11 01:32:46 +02:00
|
|
|
|
2018-08-31 15:51:24 +02:00
|
|
|
void
|
|
|
|
RouterContact::Clear()
|
|
|
|
{
|
|
|
|
addrs.clear();
|
|
|
|
exits.clear();
|
|
|
|
signature.Zero();
|
|
|
|
nickname.Zero();
|
|
|
|
enckey.Zero();
|
|
|
|
pubkey.Zero();
|
2020-02-19 22:58:01 +01:00
|
|
|
routerVersion = nonstd::optional< RouterVersion >{};
|
2020-02-24 20:40:45 +01:00
|
|
|
last_updated = 0s;
|
2018-08-31 15:51:24 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 18:14:43 +01:00
|
|
|
util::StatusObject
|
|
|
|
RouterContact::ExtractStatus() const
|
2019-02-08 20:43:25 +01:00
|
|
|
{
|
2020-02-24 20:40:45 +01:00
|
|
|
util::StatusObject obj{{"lastUpdated", last_updated.count()},
|
2019-02-11 18:14:43 +01:00
|
|
|
{"exit", IsExit()},
|
|
|
|
{"publicRouter", IsPublicRouter()},
|
2019-08-20 00:25:40 +02:00
|
|
|
{"identity", pubkey.ToString()},
|
|
|
|
{"addresses", addrs}};
|
|
|
|
|
2019-02-08 20:43:25 +01:00
|
|
|
if(HasNick())
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2019-08-19 11:33:26 +02:00
|
|
|
obj["nickname"] = Nick();
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2020-01-25 17:28:07 +01:00
|
|
|
if(routerVersion.has_value())
|
|
|
|
{
|
|
|
|
obj["routerVersion"] = routerVersion->ToString();
|
|
|
|
}
|
2019-02-11 18:14:43 +01:00
|
|
|
return obj;
|
2019-02-08 20:43:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
bool
|
2019-02-01 02:58:06 +01:00
|
|
|
RouterContact::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
|
2018-08-09 17:36:58 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictList("a", addrs, read, key, buf))
|
2018-08-09 17:36:58 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
2018-12-19 17:17:41 +01:00
|
|
|
if(!BEncodeMaybeReadDictEntry("i", netID, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf))
|
2018-08-09 17:36:58 +02:00
|
|
|
return false;
|
2018-08-02 02:48:43 +02:00
|
|
|
|
2020-01-25 17:28:07 +01:00
|
|
|
if(key == "r")
|
|
|
|
{
|
|
|
|
RouterVersion r;
|
|
|
|
if(not r.BDecode(buf))
|
|
|
|
return false;
|
|
|
|
routerVersion = r;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:13:34 +01:00
|
|
|
if(key == "n")
|
2018-08-31 15:51:24 +02:00
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if(!bencode_read_string(buf, &strbuf))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-31 15:51:24 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
|
|
|
if(strbuf.sz > llarp::AlignedBuffer< (32) >::size())
|
|
|
|
{
|
2018-08-31 15:51:24 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-08-31 15:51:24 +02:00
|
|
|
nickname.Zero();
|
2019-01-02 23:21:29 +01:00
|
|
|
std::copy(strbuf.base, strbuf.base + strbuf.sz, nickname.begin());
|
2018-08-31 15:51:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-12 14:49:23 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!BEncodeMaybeReadDictEntry("p", enckey, read, key, buf))
|
|
|
|
return false;
|
2018-05-21 14:44:50 +02:00
|
|
|
|
2019-01-24 14:18:15 +01:00
|
|
|
if(!BEncodeMaybeReadDictInt("u", last_updated, read, key, buf))
|
2018-08-31 15:51:24 +02:00
|
|
|
return false;
|
|
|
|
|
2019-01-24 14:18:15 +01:00
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2018-05-11 01:32:46 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
if(!BEncodeMaybeReadDictList("x", exits, read, key, buf))
|
2018-05-22 17:54:19 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictEntry("z", signature, read, key, buf))
|
2018-05-22 17:54:19 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
|
|
|
return read;
|
2018-05-11 01:32:46 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
bool
|
|
|
|
RouterContact::IsPublicRouter() const
|
|
|
|
{
|
2020-02-19 21:23:46 +01:00
|
|
|
if(not routerVersion.has_value())
|
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
return !addrs.empty();
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::HasNick() const
|
|
|
|
{
|
|
|
|
return nickname[0] != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-02 23:28:28 +02:00
|
|
|
RouterContact::SetNick(string_view nick)
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
|
|
|
nickname.Zero();
|
2019-01-02 02:04:03 +01:00
|
|
|
std::copy(nick.begin(),
|
|
|
|
nick.begin() + std::min(nick.size(), nickname.size()),
|
|
|
|
nickname.begin());
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 17:17:41 +01:00
|
|
|
bool
|
|
|
|
RouterContact::IsExpired(llarp_time_t now) const
|
2019-07-15 18:56:09 +02:00
|
|
|
{
|
2019-09-09 13:36:21 +02:00
|
|
|
(void)now;
|
2019-09-09 12:37:26 +02:00
|
|
|
return false;
|
|
|
|
// return Age(now) >= Lifetime;
|
2019-07-15 18:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llarp_time_t
|
|
|
|
RouterContact::TimeUntilExpires(llarp_time_t now) const
|
2018-12-19 17:17:41 +01:00
|
|
|
{
|
2019-06-04 15:19:45 +02:00
|
|
|
const auto expiresAt = last_updated + Lifetime;
|
2020-02-24 20:40:45 +01:00
|
|
|
return now < expiresAt ? expiresAt - now : 0s;
|
2019-07-15 18:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llarp_time_t
|
|
|
|
RouterContact::Age(llarp_time_t now) const
|
|
|
|
{
|
2020-02-24 20:40:45 +01:00
|
|
|
return now > last_updated ? now - last_updated : 0s;
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const
|
|
|
|
{
|
2019-07-15 18:56:09 +02:00
|
|
|
return TimeUntilExpires(now) <= dlt;
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
std::string
|
|
|
|
RouterContact::Nick() const
|
|
|
|
{
|
2019-01-02 02:03:53 +01:00
|
|
|
auto term = std::find(nickname.begin(), nickname.end(), '\0');
|
|
|
|
return std::string(nickname.begin(), term);
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 21:45:08 +02:00
|
|
|
RouterContact::Sign(const SecretKey &secretkey)
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2019-02-03 00:12:42 +01:00
|
|
|
pubkey = llarp::seckey_topublic(secretkey);
|
|
|
|
std::array< byte_t, MAX_RC_SIZE > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-08-31 14:46:54 +02:00
|
|
|
signature.Zero();
|
2018-11-19 23:45:37 +01:00
|
|
|
last_updated = time_now_ms();
|
2018-08-31 14:46:54 +02:00
|
|
|
if(!BEncode(&buf))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-31 14:46:54 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-08-31 14:46:54 +02:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
2019-05-28 21:45:08 +02:00
|
|
|
return CryptoManager::instance()->sign(signature, secretkey, buf);
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:02:32 +02:00
|
|
|
bool
|
2019-06-04 15:19:45 +02:00
|
|
|
RouterContact::Verify(llarp_time_t now, bool allowExpired) const
|
2018-10-15 14:02:32 +02:00
|
|
|
{
|
2019-01-17 16:11:17 +01:00
|
|
|
if(netID != NetID::DefaultValue())
|
2018-12-28 16:04:05 +01:00
|
|
|
{
|
2019-01-17 16:11:17 +01:00
|
|
|
llarp::LogError("netid missmatch: '", netID, "' != '",
|
|
|
|
NetID::DefaultValue(), "'");
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2018-12-28 16:04:05 +01:00
|
|
|
}
|
2018-12-19 17:17:41 +01:00
|
|
|
if(IsExpired(now))
|
2018-12-28 16:04:05 +01:00
|
|
|
{
|
2019-06-04 15:19:45 +02:00
|
|
|
if(!allowExpired)
|
|
|
|
{
|
|
|
|
llarp::LogError("RC is expired");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp::LogWarn("RC is expired");
|
2018-12-28 16:04:05 +01:00
|
|
|
}
|
2018-10-15 14:02:32 +02:00
|
|
|
for(const auto &a : addrs)
|
|
|
|
{
|
2019-08-27 01:29:17 +02:00
|
|
|
if(IsBogon(a.ip) && BlockBogons)
|
2018-10-15 14:02:32 +02:00
|
|
|
{
|
|
|
|
llarp::LogError("invalid address info: ", a);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(const auto &exit : exits)
|
|
|
|
{
|
|
|
|
if(IsBogonRange(exit.address, exit.netmask))
|
2019-04-08 19:40:51 +02:00
|
|
|
{
|
|
|
|
llarp::LogError("bogon exit: ", exit);
|
2018-10-15 14:02:32 +02:00
|
|
|
return false;
|
2019-04-08 19:40:51 +02:00
|
|
|
}
|
2018-10-15 14:02:32 +02:00
|
|
|
}
|
2019-05-28 21:45:08 +02:00
|
|
|
if(!VerifySignature())
|
2018-12-28 16:04:05 +01:00
|
|
|
{
|
2020-01-15 16:42:28 +01:00
|
|
|
llarp::LogError("invalid signature: ", *this);
|
2018-12-28 16:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-10-15 14:02:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
bool
|
2019-05-28 21:45:08 +02:00
|
|
|
RouterContact::VerifySignature() const
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2018-08-31 15:51:24 +02:00
|
|
|
RouterContact copy;
|
|
|
|
copy = *this;
|
2018-08-31 14:46:54 +02:00
|
|
|
copy.signature.Zero();
|
2019-02-03 00:12:42 +01:00
|
|
|
std::array< byte_t, MAX_RC_SIZE > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-08-31 14:46:54 +02:00
|
|
|
if(!copy.BEncode(&buf))
|
2018-08-31 15:51:24 +02:00
|
|
|
{
|
|
|
|
llarp::LogError("bencode failed");
|
2018-08-31 14:46:54 +02:00
|
|
|
return false;
|
2018-08-31 15:51:24 +02:00
|
|
|
}
|
2018-08-31 14:46:54 +02:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
2019-05-28 21:45:08 +02:00
|
|
|
return CryptoManager::instance()->verify(pubkey, buf, signature);
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::Write(const char *fname) const
|
|
|
|
{
|
2019-02-03 00:12:42 +01:00
|
|
|
std::array< byte_t, MAX_RC_SIZE > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-08-31 14:46:54 +02:00
|
|
|
if(!BEncode(&buf))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-31 14:46:54 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2019-06-24 18:39:03 +02:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
const fs::path fpath = std::string(fname); /* */
|
|
|
|
auto optional_f =
|
|
|
|
llarp::util::OpenFileStream< std::ofstream >(fpath, std::ios::binary);
|
|
|
|
if(!optional_f)
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2019-06-24 18:39:03 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2019-06-24 18:39:03 +02:00
|
|
|
auto &f = optional_f.value();
|
2019-01-11 02:44:45 +01:00
|
|
|
if(!f.is_open())
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2019-01-11 02:44:45 +01:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2019-01-11 02:44:45 +01:00
|
|
|
f.write((char *)buf.base, buf.sz);
|
2018-08-31 14:46:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::Read(const char *fname)
|
|
|
|
{
|
2019-02-03 00:12:42 +01:00
|
|
|
std::array< byte_t, MAX_RC_SIZE > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-11 02:44:45 +01:00
|
|
|
std::ifstream f;
|
|
|
|
f.open(fname, std::ios::binary);
|
|
|
|
if(!f.is_open())
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2019-01-11 02:44:45 +01:00
|
|
|
llarp::LogError("Failed to open ", fname);
|
|
|
|
return false;
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
2019-01-11 02:44:45 +01:00
|
|
|
f.seekg(0, std::ios::end);
|
2019-01-26 12:12:48 +01:00
|
|
|
auto l = f.tellg();
|
|
|
|
if(l > static_cast< std::streamoff >(sizeof tmp))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2019-01-24 16:13:41 +01:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2019-01-11 02:44:45 +01:00
|
|
|
f.seekg(0, std::ios::beg);
|
2019-02-03 00:12:42 +01:00
|
|
|
f.read((char *)tmp.data(), l);
|
2018-08-31 14:46:54 +02:00
|
|
|
return BDecode(&buf);
|
|
|
|
}
|
|
|
|
|
2019-02-25 00:46:37 +01:00
|
|
|
std::ostream &
|
|
|
|
RouterContact::print(std::ostream &stream, int level, int spaces) const
|
|
|
|
{
|
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
printer.printAttribute("k", pubkey);
|
2020-02-24 20:40:45 +01:00
|
|
|
printer.printAttribute("updated", last_updated.count());
|
2019-02-25 00:46:37 +01:00
|
|
|
printer.printAttribute("netid", netID);
|
|
|
|
printer.printAttribute("v", version);
|
|
|
|
printer.printAttribute("ai", addrs);
|
|
|
|
printer.printAttribute("xi", exits);
|
|
|
|
printer.printAttribute("e", enckey);
|
|
|
|
printer.printAttribute("z", signature);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
} // namespace llarp
|