lokinet/llarp/router_contact.cpp

428 lines
9.7 KiB
C++
Raw Normal View History

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