2018-07-11 13:20:14 +00:00
|
|
|
#ifndef LLARP_SERVICE_ADDRESS_HPP
|
|
|
|
#define LLARP_SERVICE_ADDRESS_HPP
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/key.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <router_id.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/aligned.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-12-20 14:18:03 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <numeric>
|
2018-07-11 13:20:14 +00:00
|
|
|
#include <string>
|
2019-04-26 13:02:08 +00:00
|
|
|
#include <set>
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2020-01-27 21:30:41 +00:00
|
|
|
/// Snapp Address
|
2020-04-07 18:38:56 +00:00
|
|
|
struct Address : public AlignedBuffer<32>
|
2018-07-12 13:43:37 +00:00
|
|
|
{
|
2019-04-26 12:11:34 +00:00
|
|
|
/// if parsed using FromString this contains the subdomain
|
|
|
|
/// this member is not used when comparing it's extra data for dns
|
|
|
|
std::string subdomain;
|
|
|
|
|
|
|
|
/// list of whitelisted gtld to permit
|
2020-04-07 18:38:56 +00:00
|
|
|
static const std::set<std::string> AllowedTLDs;
|
2019-04-26 12:11:34 +00:00
|
|
|
|
|
|
|
/// return true if we permit using this tld
|
|
|
|
/// otherwise return false
|
|
|
|
static bool
|
|
|
|
PermitTLD(const char* tld);
|
|
|
|
|
2018-07-12 13:43:37 +00:00
|
|
|
std::string
|
2018-12-03 22:22:59 +00:00
|
|
|
ToString(const char* tld = ".loki") const;
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
bool
|
2018-12-03 22:22:59 +00:00
|
|
|
FromString(const std::string& str, const char* tld = ".loki");
|
2018-08-02 00:48:43 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
Address() : AlignedBuffer<32>()
|
2018-07-12 18:21:44 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
explicit Address(const std::string& str) : AlignedBuffer<32>()
|
2020-02-28 16:29:15 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not FromString(str))
|
2020-02-28 16:29:15 +00:00
|
|
|
throw std::runtime_error("invalid address");
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
explicit Address(const Data& buf) : AlignedBuffer<32>(buf)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-26 12:11:34 +00:00
|
|
|
Address(const Address& other)
|
2020-04-07 18:38:56 +00:00
|
|
|
: AlignedBuffer<32>(other.as_array()), subdomain(other.subdomain)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
explicit Address(const AlignedBuffer<32>& other) : AlignedBuffer<32>(other)
|
2019-01-02 01:04:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
bool
|
|
|
|
operator<(const Address& other) const
|
|
|
|
{
|
2019-01-02 01:03:53 +00:00
|
|
|
return as_array() < other.as_array();
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const Address& self)
|
|
|
|
{
|
|
|
|
return out << self.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Address& other) const
|
|
|
|
{
|
2019-01-02 01:03:53 +00:00
|
|
|
return as_array() == other.as_array();
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const Address& other) const
|
|
|
|
{
|
2019-01-02 01:03:53 +00:00
|
|
|
return as_array() != other.as_array();
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Address&
|
2018-12-20 14:18:03 +00:00
|
|
|
operator=(const Address& other) = default;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2019-01-02 01:03:53 +00:00
|
|
|
dht::Key_t
|
2020-01-27 21:30:41 +00:00
|
|
|
ToKey() const;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2019-01-02 01:03:53 +00:00
|
|
|
RouterID
|
2018-08-10 21:34:11 +00:00
|
|
|
ToRouter() const
|
|
|
|
{
|
2019-01-02 01:03:53 +00:00
|
|
|
return RouterID(as_array());
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const Address& buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return std::accumulate(buf.begin(), buf.end(), 0, std::bit_xor<size_t>());
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
};
|
2018-07-12 13:43:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
2018-07-11 13:20:14 +00:00
|
|
|
|
2018-09-19 16:20:34 +00:00
|
|
|
#endif
|