lokinet/llarp/service/name.cpp
Jeff 21930cf667
LNS (#1342)
* initial relay side lns

* fix typo

* add reserved names and refactor test for dns

* lns name decryption

* all wired up (allegedly)

* refact to use service::EncryptedName for LNS responses to include nonce with ciphertext

* fully rwemove tag_lookup_job

* replace lns cache with DecayingHashTable

* check for lns name validity against the following rules:

* not localhost.loki, loki.loki, or snode.loki

* if it contains no dash then max 32 characters long, not including the .loki tld (and also assuming a leading subdomain has been stripped)

* These are from general DNS requirements, and also enforced in
registrations:

* Must be all [A-Za-z0-9-]. (A-Z will be lower-cased by the RPC call).

* cannot start or end with a -

* max 63 characters long if it does contain a dash

* cannot contain -- in the third and fourth characters unless it starts with xn--

* handle timeout in name lookup job by calling the right handler with std::nullopt
2020-09-17 15:18:08 -04:00

67 lines
1.8 KiB
C++

#include <service/name.hpp>
#include <crypto/crypto.hpp>
#include <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