28 m_host(head(sockless(spec))) ,
29 m_service(tail(sockless(spec))) ,
30 m_address_valid(false) ,
31 m_address(
Address::defaultAddress()) ,
36 m_using_socks = socksified( spec , m_socks_far_host , m_socks_far_port ) ;
38 throw InvalidFormat( spec ) ;
39 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" << (m_using_socks?
" (using socks)":
"") ) ;
44 m_service(tail(spec)) ,
45 m_address_valid(false) ,
46 m_address(
Address::defaultAddress()) ,
51 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" ) ;
55 m_host(head(socks_server)) ,
56 m_service(tail(socks_server)) ,
57 m_address_valid(false) ,
58 m_address(Address::defaultAddress()) ,
62 m_socks_far_host(head(far_server)) ,
63 m_socks_far_port(tail(far_server))
65 if( m_socks_far_host.empty() || m_socks_far_port.empty() )
66 throw InvalidFormat() ;
68 throw InvalidFormat(
"invalid port number: [" + m_socks_far_port +
"]" ) ;
69 G_DEBUG(
"GNet::Location::ctor: unresolved location [" <<
displayString() <<
"]" <<
" (using socks)" ) ;
74 return { spec , family , 1 } ;
79 return { socks_server , far_server , AF_UNSPEC } ;
82std::string GNet::Location::sockless(
const std::string & s )
88bool GNet::Location::socksified(
const std::string & s , std::string & far_host_out , std::string & far_port_out )
90 std::string::size_type pos = s.find(
'@') ;
91 if( pos != std::string::npos )
98 return pos != std::string::npos ;
101std::string GNet::Location::head(
const std::string & s )
103 std::size_t pos = s.rfind(
':' ) ;
104 std::string h = ( pos == std::string::npos && !s.empty() && s[0] ==
'/' ) ? s :
G::Str::head( s , pos ) ;
105 if( h.size() > 1U && h.at(0U) ==
'[' && h.at(h.size()-1U) ==
']' )
106 h = h.substr( 1U , h.size()-2U ) ;
110std::string GNet::Location::tail(
const std::string & s )
133 std::string address_string =
G::Str::join(
":" , m_host , m_service ) ;
137 update( address , std::string() ) ;
144 return m_address_valid ;
154 if( !update(address,name,std::nothrow) )
155 throw InvalidFamily() ;
161 address.
family() == Address::Family::ipv4 ||
162 address.
family() == Address::Family::ipv6 ||
163 address.
family() == Address::Family::local ;
165 if( !valid_family || ( m_family != AF_UNSPEC && address.
af() != m_family ) )
168 m_address = address ;
169 m_family = address.
af() ;
170 m_address_valid = true ;
171 m_canonical_name = name ;
173 G_DEBUG(
"GNet::Location::ctor: resolved location [" << displayString() <<
"]" ) ;
179 return m_canonical_name ;
186 return address().displayString() ;
188 else if( m_host.find(
'/') == 0U )
194 const char * ipvx = m_family == AF_UNSPEC ?
"ip" : ( m_family == AF_INET ?
"ipv4" :
"ipv6" ) ;
195 return m_host +
"/" + m_service +
"/" + ipvx ;
201 return m_update_time ;
206 return m_using_socks ;
211 G_ASSERT( m_socks_far_port.empty() ||
G::Str::isUInt(m_socks_far_port) ) ;
212 return m_socks_far_port.empty() ? 0U :
G::Str::toUInt(m_socks_far_port) ;
217 return m_socks_far_host ;
The GNet::Address class encapsulates a TCP/UDP transport address.
Family family() const
Returns the address family enumeration.
int af() const
Returns the address family number such as AF_INET or AFINET6.
static Address parse(const std::string &display_string)
Factory function for any address family.
static bool validString(const std::string &display_string, std::string *reason=nullptr)
Returns true if the transport-address display string is valid.
A class that represents the remote target for out-going client connections.
int family() const
Returns the preferred name resolution address family as passed to the constructor.
bool resolved() const
Returns true after update() has been called or resolveTrivially() succeeded.
bool resolveTrivially()
If host() and service() are already in address format then do a trivial update() so that the location...
std::string name() const
Returns the remote canonical name.
std::string displayString() const
Returns a string representation for logging and debug.
std::string socksFarHost() const
Returns the port for the socks far server.
G::SystemTime updateTime() const
Returns the time of the last update() or zero if never update()d.
bool socks() const
Returns true if a socks location.
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().
std::string service() const
Returns the remote service name derived from the constructor parameter.
unsigned int socksFarPort() const
Returns the port number for the socks far server.
Address address() const
Returns the remote address.
std::string host() const
Returns the remote host name derived from the constructor parameter.
Location(const std::string &spec, int family=AF_UNSPEC)
Constructor taking a formatted "host:service" string.
static Location nosocks(const std::string &spec, int family=AF_UNSPEC)
Factory function for a remote location but not allowing the extended syntax for socks.
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
static std::string tail(const std::string &in, std::size_t pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception.
static std::string head(const std::string &in, std::size_t pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
Represents a unix-epoch time with microsecond resolution.
static SystemTime now()
Factory function for the current time.