2021-03-09 22:24:35 +00:00
|
|
|
#include "intro.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2023-01-09 17:47:41 +00:00
|
|
|
#include <llarp/util/time.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
namespace llarp::service
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
util::StatusObject
|
|
|
|
Introduction::ExtractStatus() const
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
util::StatusObject obj{
|
|
|
|
{"router", router.ToHex()},
|
|
|
|
{"path", path_id.ToHex()},
|
|
|
|
{"expiresAt", to_json(expiry)},
|
|
|
|
{"latency", to_json(latency)},
|
|
|
|
{"version", uint64_t(version)}};
|
|
|
|
return obj;
|
|
|
|
}
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
Introduction::decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("k", router, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictInt("l", latency, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("p", path_id, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictInt("x", expiry, read, key, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
2019-01-11 00:12:43 +00:00
|
|
|
|
2023-10-03 20:00:23 +00:00
|
|
|
Introduction::Introduction(std::string buf)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_consumer btdc{std::move(buf)};
|
|
|
|
|
2023-12-06 19:34:37 +00:00
|
|
|
router.from_string(btdc.require<std::string>("k"));
|
2023-10-03 20:00:23 +00:00
|
|
|
latency = std::chrono::milliseconds{btdc.require<uint64_t>("l")};
|
|
|
|
path_id.from_string(btdc.require<std::string>("p"));
|
|
|
|
expiry = std::chrono::milliseconds{btdc.require<uint64_t>("x")};
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
log::critical(intro_cat, "Error: Introduction failed to populate with bt encoded contents");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
void
|
2023-10-03 20:00:23 +00:00
|
|
|
Introduction::bt_encode(oxenc::bt_list_producer& btlp) const
|
2023-08-31 16:28:02 +00:00
|
|
|
{
|
|
|
|
try
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2023-10-03 20:00:23 +00:00
|
|
|
auto subdict = btlp.append_dict();
|
|
|
|
|
|
|
|
subdict.append("k", router.ToView());
|
|
|
|
subdict.append("l", latency.count());
|
|
|
|
subdict.append("p", path_id.ToView());
|
|
|
|
subdict.append("x", expiry.count());
|
2019-01-11 00:12:43 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
catch (...)
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
log::critical(intro_cat, "Error: Introduction failed to bt encode contents!");
|
2019-01-11 00:12:43 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
2019-02-24 23:46:37 +00:00
|
|
|
|
2023-10-18 12:48:09 +00:00
|
|
|
void
|
|
|
|
Introduction::bt_encode(oxenc::bt_dict_producer& subdict) const
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
subdict.append("k", router.ToView());
|
|
|
|
subdict.append("l", latency.count());
|
|
|
|
subdict.append("p", path_id.ToView());
|
|
|
|
subdict.append("x", expiry.count());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
log::critical(intro_cat, "Error: Introduction failed to bt encode contents!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
void
|
|
|
|
Introduction::Clear()
|
|
|
|
{
|
|
|
|
router.Zero();
|
|
|
|
path_id.Zero();
|
|
|
|
latency = 0s;
|
|
|
|
expiry = 0s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Introduction::ToString() const
|
|
|
|
{
|
|
|
|
return fmt::format(
|
|
|
|
"[Intro k={} l={} p={} v={} x={}]",
|
|
|
|
RouterID{router},
|
|
|
|
latency.count(),
|
|
|
|
path_id,
|
|
|
|
version,
|
|
|
|
expiry.count());
|
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
} // namespace llarp::service
|