2023-10-31 20:49:01 +00:00
|
|
|
#include "constants/version.hpp"
|
|
|
|
#include "crypto/crypto.hpp"
|
|
|
|
#include "net/net.hpp"
|
|
|
|
#include "router_contact.hpp"
|
|
|
|
#include "util/bencode.hpp"
|
|
|
|
#include "util/buffer.hpp"
|
|
|
|
#include "util/file.hpp"
|
|
|
|
#include "util/time.hpp"
|
|
|
|
|
|
|
|
#include <oxenc/bt_serialize.h>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2023-11-02 12:39:20 +00:00
|
|
|
LocalRC
|
|
|
|
LocalRC::make(const SecretKey secret, oxen::quic::Address local)
|
|
|
|
{
|
|
|
|
return *new LocalRC{secret, local};
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalRC::LocalRC(const SecretKey secret, oxen::quic::Address local)
|
|
|
|
: _secret_key{std::move(secret)}
|
|
|
|
{
|
|
|
|
_router_id = llarp::seckey_to_pubkey(_secret_key);
|
|
|
|
_addr = std::move(local);
|
2023-12-06 21:54:51 +00:00
|
|
|
if (_addr.is_ipv6())
|
|
|
|
_addr6.emplace(&_addr.in6());
|
2023-11-02 12:39:20 +00:00
|
|
|
resign();
|
|
|
|
}
|
|
|
|
|
2023-12-11 15:08:02 +00:00
|
|
|
RemoteRC
|
|
|
|
LocalRC::to_remote()
|
|
|
|
{
|
2023-12-11 17:29:56 +00:00
|
|
|
resign();
|
2023-12-11 15:08:02 +00:00
|
|
|
return RemoteRC{view()};
|
|
|
|
}
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
void
|
2023-11-02 12:30:38 +00:00
|
|
|
LocalRC::bt_sign(oxenc::bt_dict_producer& btdp)
|
2023-10-31 20:49:01 +00:00
|
|
|
{
|
|
|
|
_signature.clear();
|
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
btdp.append_signature("~", [this](ustring_view to_sign) {
|
|
|
|
std::array<unsigned char, 64> sig;
|
|
|
|
|
|
|
|
if (!crypto::sign(const_cast<unsigned char*>(sig.data()), _secret_key, to_sign))
|
2023-10-31 20:49:01 +00:00
|
|
|
throw std::runtime_error{"Failed to sign RC"};
|
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
_signature = {sig.data(), sig.size()};
|
|
|
|
return sig;
|
2023-10-31 20:49:01 +00:00
|
|
|
});
|
2023-11-02 12:30:38 +00:00
|
|
|
|
2023-12-07 22:39:39 +00:00
|
|
|
_payload = ustring{btdp.view<unsigned char>()};
|
2023-11-02 12:30:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
void
|
2023-11-02 12:39:20 +00:00
|
|
|
LocalRC::bt_encode(oxenc::bt_dict_producer& btdp)
|
2023-10-31 20:49:01 +00:00
|
|
|
{
|
|
|
|
btdp.append("", RC_VERSION);
|
|
|
|
|
|
|
|
std::array<unsigned char, 18> buf;
|
|
|
|
|
|
|
|
{
|
|
|
|
if (not _addr.is_ipv4())
|
|
|
|
throw std::runtime_error{"Unable to encode RC: addr is not IPv4"};
|
|
|
|
|
|
|
|
auto in4 = _addr.in4();
|
|
|
|
|
|
|
|
std::memcpy(buf.data(), &in4.sin_addr.s_addr, 4);
|
|
|
|
std::memcpy(buf.data() + 4, &in4.sin_port, 2);
|
|
|
|
|
|
|
|
btdp.append("4", ustring_view{buf.data(), 6});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_addr6)
|
|
|
|
{
|
|
|
|
if (not _addr.is_ipv6())
|
|
|
|
throw std::runtime_error{"Unable to encode RC: addr6 is set but is not IPv6"};
|
|
|
|
|
|
|
|
auto in6 = _addr.in6();
|
|
|
|
|
|
|
|
std::memcpy(buf.data(), &in6.sin6_addr.s6_addr, 16);
|
|
|
|
std::memcpy(buf.data() + 16, &in6.sin6_port, 2);
|
|
|
|
|
|
|
|
btdp.append("6", ustring_view{buf.data(), 18});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ACTIVE_NETID != llarp::LOKINET_DEFAULT_NETID)
|
|
|
|
btdp.append("i", ACTIVE_NETID);
|
|
|
|
|
|
|
|
btdp.append("p", _router_id.ToView());
|
|
|
|
|
|
|
|
btdp.append("t", _timestamp.time_since_epoch().count());
|
|
|
|
|
|
|
|
static_assert(llarp::LOKINET_VERSION.size() == 3);
|
|
|
|
btdp.append(
|
|
|
|
"v", std::string_view{reinterpret_cast<const char*>(llarp::LOKINET_VERSION.data()), 3});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LocalRC::resign()
|
|
|
|
{
|
2023-11-02 12:30:38 +00:00
|
|
|
set_systime_timestamp();
|
|
|
|
oxenc::bt_dict_producer btdp;
|
|
|
|
bt_encode(btdp);
|
|
|
|
bt_sign(btdp);
|
2023-10-31 20:49:01 +00:00
|
|
|
}
|
|
|
|
} // namespace llarp
|