You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/bootstrap.cpp

71 lines
1.4 KiB
C++

#include "bootstrap.hpp"
#include "util/bencode.hpp"
#include "util/logging.hpp"
#include "util/logging/buffer.hpp"
namespace llarp
{
bool
BootstrapList::bt_decode(std::string_view buf)
{
try
{
oxenc::bt_list_consumer btlc{buf};
while (not btlc.is_finished())
emplace(btlc.consume_dict_consumer());
}
catch (...)
{
log::warning(logcat, "Unable to decode bootstrap RemoteRC");
return false;
}
return true;
}
std::string_view
BootstrapList::bt_encode() const
{
oxenc::bt_list_producer btlp{};
for (const auto& it : *this)
btlp.append(it.view());
return btlp.view();
}
void
BootstrapList::read_from_file(const fs::path& fpath)
{
bool isListFile = false;
if (std::ifstream inf(fpath.c_str(), std::ios::binary); inf.is_open())
{
const char ch = inf.get();
isListFile = ch == 'l';
}
if (isListFile)
{
auto content = util::file_to_string(fpath);
if (not bt_decode(content))
{
throw std::runtime_error{fmt::format("failed to read bootstrap list file '{}'", fpath)};
}
}
else
{
RemoteRC rc;
if (not rc.read(fpath))
{
throw std::runtime_error{
fmt::format("failed to decode bootstrap RC, file='{}', rc={}", fpath, rc.to_string())};
}
insert(rc);
}
}
} // namespace llarp