29 const char * host_service_separators =
":/" ;
35 m_address_valid(false) ,
36 m_address(
Address::defaultAddress()) ,
43 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" ) ;
47 const std::string & far_host ,
const std::string & far_service ,
int family ) :
49 m_service(socks_service) ,
50 m_address_valid(false) ,
51 m_address(
Address::defaultAddress()) ,
55 m_socks(!far_host.empty()) ,
56 m_socks_far_host(far_host) ,
59 if( far_host.empty() != far_service.empty() )
60 throw InvalidFormat() ;
62 throw InvalidFormat(
"invalid port number: [" + far_service +
"]" ) ;
65 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" << (m_socks?
" (using socks)":
"") ) ;
69 m_host(head(sockless(location_string))) ,
70 m_service(tail(sockless(location_string))) ,
71 m_address_valid(false) ,
72 m_address(
Address::defaultAddress()) ,
79 m_socks = socksified( location_string , m_socks_far_host , m_socks_far_port ) ;
80 if( m_host.empty() || m_service.empty() )
81 throw InvalidFormat( location_string ) ;
82 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" << (m_socks?
" (using socks)":
"") ) ;
85 std::string GNet::Location::sockless(
const std::string & s )
91 bool GNet::Location::socksified(
const std::string & s , std::string & far_host ,
unsigned int & far_port )
93 std::string::size_type pos = s.find(
'@') ;
94 if( pos != std::string::npos )
97 far_host =
G::Str::head( ss , ss.find_last_of(host_service_separators) ) ;
100 return pos != std::string::npos ;
103 std::string GNet::Location::head(
const std::string & s )
105 std::string result =
G::Str::head( s , s.find_last_of(host_service_separators) ) ;
106 if( result.size() > 1U && result.at(0U) ==
'[' && result.at(result.size()-1U) ==
']' )
107 result = result.substr( 1U , result.size()-2U ) ;
111 std::string GNet::Location::tail(
const std::string & s )
113 return G::Str::tail( s , s.find_last_of(host_service_separators) ) ;
145 return m_address_valid ;
155 G_ASSERT( m_family == AF_UNSPEC || address.
domain() == m_family ) ;
156 m_address = address ;
157 m_family = address.
domain() ;
158 m_address_valid = true ;
159 m_canonical_name = name ;
161 G_DEBUG(
"GNet::Location::ctor: resolved location [" << displayString() <<
"]" ) ;
166 return m_canonical_name ;
171 const char * ipvx = m_family == AF_UNSPEC ?
"ip" : ( m_family == AF_INET ?
"ipv4" :
"ipv6" ) ;
172 return resolved() ? address().displayString() : (m_host+
"/"+m_service+
"/"+ipvx) ;
177 return m_update_time ;
187 return m_socks_far_port ;
192 return m_socks_far_host ;
A subsecond-resolution timestamp based on a time_t.
static std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static std::string head(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
The GNet::Address class encapsulates a TCP/UDP transport address.
bool socks() const
Returns true if a socks location.
static bool validStrings(const std::string &ip, const std::string &port_string, std::string *reason=nullptr)
Returns true if the combined network-address string and port string is valid.
std::string displayString() const
Returns a string representation for logging and debug.
static EpochTime now()
Returns the current epoch time.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
bool dgram() const
Returns true if the name resolution should be specifically for datagram sockets.
std::string name() const
Returns the remote canonical name.
void update(const Address &address, const std::string &canonical_name)
Updates the address and canonical name, typically after doing a name lookup on host() and service()...
Address address() const
Returns the remote address.
std::string socksFarHost() const
Returns the port for the socks far server.
bool resolved() const
Returns true after update() has been called.
int domain() const
Returns the address 'domain', eg. PF_INET.
std::string service() const
Returns the remote service name, as passed in to the constructor.
unsigned int socksFarPort() const
Returns the port number for the socks far server.
int family() const
Returns the preferred name resolution address family, or AF_UNSPEC.
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
Location(const std::string &host, const std::string &service, int family=AF_UNSPEC)
Constructor taking a host/service string.
void resolveTrivially()
If host() and service() are already in address format then do a trivial update() so that the location...
std::string host() const
Returns the remote host name, as passed in to the constructor.
G::EpochTime updateTime() const
Returns the time of the last update() or zero if never update()d.