lokinet/llarp/service/name.cpp
Thomas Winget 7caa87862e standardize include format and pragma once
All #ifndef guards on headers have been removed, I think,
in favor of #pragma once

Headers are now included as `#include "filename"` if the included file
resides in the same directory as the file including it, or any
subdirectory therein.  Otherwise they are included as
`#include <project/top/dir/relative/path/filename>`

The above does not include system/os headers.
2021-03-09 19:01:41 -05:00

67 lines
1.8 KiB
C++

#include "name.hpp"
#include <llarp/crypto/crypto.hpp>
#include <llarp/util/str.hpp>
namespace llarp::service
{
std::optional<Address>
EncryptedName::Decrypt(std::string_view name) const
{
if (ciphertext.empty())
return std::nullopt;
const auto crypto = CryptoManager::instance();
const auto maybe = crypto->maybe_decrypt_name(ciphertext, nonce, name);
if (maybe.has_value())
return Address{*maybe};
return std::nullopt;
}
bool
NameIsValid(std::string_view lnsName)
{
// strip off .loki suffix
lnsName = lnsName.substr(0, lnsName.find_last_of('.'));
// ensure chars are sane
for (const auto ch : lnsName)
{
if (ch == '-')
continue;
if (ch == '.')
continue;
if (ch >= 'a' and ch <= 'z')
continue;
if (ch >= '0' and ch <= '9')
continue;
return false;
}
// split into domain parts
const auto parts = split(lnsName, ".");
// get root domain
const auto primaryName = parts[parts.size() - 1];
constexpr size_t MaxNameLen = 32;
constexpr size_t MaxPunycodeNameLen = 63;
// check against lns name blacklist
if (primaryName == "localhost")
return false;
if (primaryName == "loki")
return false;
if (primaryName == "snode")
return false;
// check for dashes
if (primaryName.find("-") == std::string_view::npos)
return primaryName.size() <= MaxNameLen;
// check for dashes and end or beginning
if (*primaryName.begin() == '-' or *(primaryName.end() - 1) == '-')
return false;
// check for punycode name length
if (primaryName.size() > MaxPunycodeNameLen)
return false;
// check for xn--
return (primaryName[2] == '-' and primaryName[3] == '-')
? (primaryName[0] == 'x' and primaryName[1] == 'n')
: true;
}
} // namespace llarp::service