lokinet/llarp/service/address.hpp

109 lines
2.3 KiB
C++
Raw Normal View History

#ifndef LLARP_SERVICE_ADDRESS_HPP
#define LLARP_SERVICE_ADDRESS_HPP
2018-12-12 00:48:54 +00:00
#include <dht/key.hpp>
#include <router_id.hpp>
#include <util/aligned.hpp>
#include <functional>
#include <numeric>
#include <string>
namespace llarp
{
namespace service
{
2018-12-03 22:22:59 +00:00
/// Snapp/Snode Address
struct Address : public AlignedBuffer< 32 >
{
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
static const std::vector< std::string > AllowedTLDs;
/// return true if we permit using this tld
/// otherwise return false
static bool
PermitTLD(const char* tld);
std::string
2018-12-03 22:22:59 +00:00
ToString(const char* tld = ".loki") const;
bool
2018-12-03 22:22:59 +00:00
FromString(const std::string& str, const char* tld = ".loki");
Address() : AlignedBuffer< SIZE >()
2018-07-12 18:21:44 +00:00
{
2018-08-10 21:34:11 +00:00
}
explicit Address(const Data& buf) : AlignedBuffer< SIZE >(buf)
2018-08-10 21:34:11 +00:00
{
}
2019-04-26 12:11:34 +00:00
Address(const Address& other)
: AlignedBuffer< SIZE >(other.as_array()), subdomain(other.subdomain)
2018-08-10 21:34:11 +00:00
{
}
explicit Address(const AlignedBuffer< SIZE >& other)
: AlignedBuffer< SIZE >(other)
{
}
2018-08-10 21:34:11 +00:00
bool
operator<(const Address& other) const
{
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
{
return as_array() == other.as_array();
2018-08-10 21:34:11 +00:00
}
bool
operator!=(const Address& other) const
{
return as_array() != other.as_array();
2018-08-10 21:34:11 +00:00
}
Address&
operator=(const Address& other) = default;
2018-08-10 21:34:11 +00:00
dht::Key_t
2018-08-10 21:34:11 +00:00
ToKey() const
2018-07-17 04:37:50 +00:00
{
return dht::Key_t(as_array());
2018-07-17 04:37:50 +00:00
}
2018-08-10 21:34:11 +00:00
RouterID
2018-08-10 21:34:11 +00:00
ToRouter() const
{
return RouterID(as_array());
2018-08-10 21:34:11 +00:00
}
struct Hash
{
size_t
operator()(const Address& buf) const
{
return std::accumulate(buf.begin(), buf.end(), 0,
std::bit_xor< size_t >());
2018-08-10 21:34:11 +00:00
}
};
};
} // namespace service
} // namespace llarp
2018-09-19 16:20:34 +00:00
#endif