@ -13,117 +13,187 @@
namespace llarp
{
template < typename pubkey_t = PubKey , std : : enable_if_t < std : : is_base_of_v < PubKey , pubkey_t > , int > = 0 >
struct RemoteAddress
{
pubkey_t _pubkey ;
std : : optional < std : : string > _name = std : : nullopt ;
protected :
std : : string _tld ;
bool is_ons { false } ;
explicit RemoteAddress ( std : : string_view tld ) : _tld { std : : move ( tld ) }
{ }
public :
RemoteAddress ( ) = default ;
virtual ~ RemoteAddress ( ) = default ;
explicit RemoteAddress ( std : : string addr , bool _is_ons = false ) : _name { std : : move ( addr ) } , is_ons { _is_ons }
std : : string to_string ( ) const
{
if ( not is_ons )
_pubkey . from_string ( * _name ) ;
if constexpr ( std : : is_same_v < pubkey_t , ClientPubKey > )
_tld = std : : string { TLD : : CLIENT } ;
else if constexpr ( std : : is_same_v < pubkey_t , RelayPubKey > )
_tld = std : : string { TLD : : RELAY } ;
else
throw std : : invalid_argument { " Something seriously weird just happened. " } ;
return remote_name ( ) ;
}
explicit RemoteAddress ( PubKey pk , std : : string_view tld , std : : optional < std : : string > n = std : : nullopt )
: _pubkey { pk . data ( ) } , _name { std : : move ( n ) } , _tld { tld }
{ }
RemoteAddress ( const RemoteAddress & other ) : RemoteAddress { other . _pubkey , other . _tld , other . _name }
{ }
RemoteAddress ( RemoteAddress & & other )
: RemoteAddress { std : : move ( other . _pubkey ) , std : : move ( other . _tld ) , std : : move ( other . _name ) }
{ }
virtual std : : string name ( ) const = 0 ;
RemoteAddress & operator = ( const RemoteAddress & other ) = default ;
RemoteAddress & operator = ( RemoteAddress & & other )
std : : string tld ( ) const
{
_pubkey = std : : move ( other . _pubkey ) ;
_name = std : : move ( other . _name ) ;
_tld = std : : move ( other . _tld ) ;
return * this ;
return _tld ;
}
bool operator < ( const RemoteAddress & other ) const
{
return std : : tie ( _pubkey , _name , _tld ) < std : : tie ( other . _pubkey , other . _name , other . _tld ) ;
}
bool operator = = ( const RemoteAddress & other ) const
{
return _pubkey = = other . _pubkey and _name = = other . _name & & _tld = = other . _tld ;
}
bool operator ! = ( const RemoteAddress & other ) const
std : : string remote_name ( ) const
{
return n ot( * this = = other ) ;
return name ( ) + tld ( ) ;
}
} ;
~ RemoteAddress ( ) = default ;
struct ClientAddress final : public RemoteAddress
{
private :
std : : optional < ClientPubKey > _pubkey { std : : nullopt } ;
std : : optional < std : : string > _name { std : : nullopt } ;
bool _is_ons { false } ;
std : : string to_string ( ) const
{
return remote_name ( ) ;
}
explicit ClientAddress ( std : : string_view addr , bool is_ons ) ;
public :
ClientAddress ( ) = default ;
~ ClientAddress ( ) override = default ;
explicit ClientAddress ( ClientPubKey cpk , std : : optional < std : : string > n = std : : nullopt )
: RemoteAddress { TLD : : CLIENT } , _pubkey { std : : move ( cpk ) } , _name { std : : move ( n ) }
{ }
ClientAddress ( const ClientAddress & other )
: RemoteAddress { TLD : : RELAY } , _pubkey { other . _pubkey } , _name { other . _name }
{ }
ClientAddress ( ClientAddress & & other )
: RemoteAddress { TLD : : RELAY } , _pubkey { std : : move ( other . _pubkey ) } , _name { std : : move ( other . _name ) }
{ }
ClientAddress & operator = ( const ClientAddress & other ) = default ;
ClientAddress & operator = ( ClientAddress & & other ) = default ;
std : : string name ( ) const
bool operator < ( const ClientAddress & other ) const ;
bool operator = = ( const ClientAddress & other ) const ;
bool operator ! = ( const ClientAddress & other ) const ;
static std : : optional < ClientAddress > from_client_addr ( std : : string arg ) ;
std : : optional < ClientPubKey > pubkey ( )
{
return _name . value_or ( _pubkey . to_string ( ) ) ;
return _ pubkey;
}
std : : string tld ( ) const
bool is_ons ( ) const
{
return _tld ;
return _ is_ons ;
}
std : : string remote_name ( ) const
std : : string name( ) const override
{
return name ( ) + tld ( ) ;
return _name . value_or ( _pubkey - > to_string ( ) ) ;
}
} ;
template < typename pubkey_t = PubKey , std : : enable_if_t < std : : is_base_of_v < PubKey , pubkey_t > , int > = 0 >
std : : optional < RemoteAddress < pubkey_t > > from_pubkey_addr ( const std : : string & arg )
struct RelayAddress final : public RemoteAddress
{
if constexpr ( std : : is_same_v < pubkey_t , ClientPubKey > | | std : : is_same_v < pubkey_t , PubKey > )
private :
RelayPubKey _pubkey ;
explicit RelayAddress ( std : : string_view addr ) ;
public :
RelayAddress ( ) = default ;
~ RelayAddress ( ) override = default ;
explicit RelayAddress ( RelayPubKey cpk ) : RemoteAddress { TLD : : CLIENT } , _pubkey { std : : move ( cpk ) }
{ }
RelayAddress ( const RelayAddress & other ) : RemoteAddress { TLD : : RELAY } , _pubkey { other . _pubkey }
{ }
RelayAddress ( RelayAddress & & other ) : RemoteAddress { TLD : : RELAY } , _pubkey { std : : move ( other . _pubkey ) }
{ }
RelayAddress & operator = ( const RelayAddress & other ) = default ;
RelayAddress & operator = ( RelayAddress & & other ) = default ;
bool operator < ( const RelayAddress & other ) const ;
bool operator = = ( const RelayAddress & other ) const ;
bool operator ! = ( const RelayAddress & other ) const ;
static std : : optional < RelayAddress > from_relay_addr ( std : : string arg ) ;
const RelayPubKey & pubkey ( )
{
if ( service : : is_valid_ons ( arg ) )
{
return std : : make_optional ( RemoteAddress < ClientPubKey > ( arg , true ) ) ;
}
if ( auto maybe_addr = parse_addr_string ( arg , TLD : : CLIENT ) )
{
return std : : make_optional ( RemoteAddress < ClientPubKey > ( * maybe_addr ) ) ;
}
return _pubkey ;
}
if ( auto maybe_addr = parse_addr_string ( arg , TLD : : RELAY ) )
std : : string name ( ) const override
{
return std : : make_optional ( RemoteAddress < RelayPubKey > ( * maybe_addr ) ) ;
return _pubkey . to_string ( ) ;
}
} ;
return std : : nullopt ;
}
template < typename pk_t >
inline constexpr bool IsToStringFormattable < RemoteAddress < pk_t > > = true ;
// template <typename pubkey_t = PubKey, std::enable_if_t<std::is_base_of_v<PubKey, pubkey_t>, int> = 0>
// std::optional<RemoteAddress<pubkey_t>> from_pubkey_addr(const std::string& arg)
// {
// if constexpr (std::is_same_v<pubkey_t, ClientPubKey> || std::is_same_v<pubkey_t, PubKey>)
// {
// if (service::is_valid_ons(arg))
// {
// return std::make_optional(RemoteAddress<ClientPubKey>(arg, true));
// }
// if (auto maybe_addr = parse_addr_string(arg, TLD::CLIENT))
// {
// return std::make_optional(RemoteAddress<ClientPubKey>(*maybe_addr));
// }
// }
// if (auto maybe_addr = parse_addr_string(arg, TLD::RELAY))
// {
// return std::make_optional(RemoteAddress<RelayPubKey>(*maybe_addr));
// }
// return std::nullopt;
// }
// template <typename pubkey_t, std::enable_if_t<std::is_base_of_v<PubKey, pubkey_t>, int> = 0>
// std::optional<RemoteAddress<pubkey_t>> from_pubkey_addr(const std::string& arg)
// // auto from_pubkey_addr(const std::string& arg)
// {
// if (arg.ends_with(".loki"))
// {
// return RemoteAddress<ClientPubKey>(arg, service::is_valid_ons(arg));
// }
// if (arg.ends_with(".snode"))
// {
// return RemoteAddress<RelayPubKey>(arg);
// }
// return std::nullopt;
// }
template < >
inline constexpr bool IsToStringFormattable < RemoteAddress > = true ;
template < typename T >
inline constexpr bool IsToStringFormattable < T , std : : enable_if_t < std : : is_base_of_v < RemoteAddress , T > > > = true ;
} // namespace llarp
namespace std
{
template < typename pk_t >
struct hash < llarp : : RemoteAddress < pk_t > >
template < >
struct hash < llarp : : RemoteAddress >
{
virtual size_t operator ( ) ( const llarp : : RemoteAddress < pk_t > & r ) const
virtual size_t operator ( ) ( const llarp : : RemoteAddress & r ) const
{
return std : : hash < std : : string > { } ( r . to_string ( ) ) ;
}
} ;
template < >
struct hash < llarp : : RelayAddress > : public hash < llarp : : RemoteAddress >
{ } ;
template < >
struct hash < llarp : : ClientAddress > : public hash < llarp : : RemoteAddress >
{ } ;
} // namespace std