30 const char * host_service_separators =
":/." ;
36 m_address_valid(false) ,
37 m_address(
Address::defaultAddress()) ,
44 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" ) ;
48 m_host(head(sockless(location_string))) ,
49 m_service(tail(sockless(location_string))) ,
50 m_address_valid(false) ,
51 m_address(
Address::defaultAddress()) ,
58 m_socks = socked( location_string , m_socks_far_host , m_socks_far_port ) ;
59 if( m_host.empty() || m_service.empty() )
60 throw InvalidFormat( location_string ) ;
61 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" << (m_socks?
" (using socks)":
"") ) ;
64 std::string GNet::Location::sockless(
const std::string & s )
70 bool GNet::Location::socked(
const std::string & s , std::string & far_host ,
unsigned int & far_port )
72 std::string::size_type pos = s.find(
'@') ;
73 if( pos != std::string::npos )
76 far_host =
G::Str::head( ss , ss.find_last_of(host_service_separators) ) ;
79 return pos != std::string::npos ;
82 std::string GNet::Location::head(
const std::string & s )
84 return G::Str::head( s , s.find_last_of(host_service_separators) ) ;
87 std::string GNet::Location::tail(
const std::string & s )
89 return G::Str::tail( s , s.find_last_of(host_service_separators) ) ;
121 return m_address_valid ;
131 G_ASSERT( m_family == AF_UNSPEC || address.
domain() == m_family ) ;
132 m_address = address ;
133 m_family = address.
domain() ;
134 m_address_valid = true ;
135 m_canonical_name = name ;
137 G_DEBUG(
"GNet::Location::ctor: resolved location [" << displayString() <<
"]" ) ;
142 return m_canonical_name ;
147 const char * ipvx = m_family == AF_UNSPEC ?
"ip" : ( m_family == AF_INET ?
"ipv4" :
"ipv6" ) ;
148 return resolved() ? address().displayString() : (m_host+
"/"+m_service+
"/"+ipvx) ;
153 return m_update_time ;
163 return m_socks_far_port ;
168 return m_socks_far_host ;
A subsecond-resolution timestamp based on a time_t.
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 ip address string and port string is valid.
std::string displayString() const
Returns a string representation for logging and debug.
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
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.
static std::string head(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
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.
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.