38 const char * c_cannot_connect_to =
"cannot connect to " ;
44 const Location & remote ,
bool bind_local_address ,
const Address & local_address ,
45 bool sync_dns ,
unsigned int secure_connection_timeout ) :
47 m_resolver(*this,eh) ,
48 m_remote_location(remote) ,
49 m_bind_local_address(bind_local_address) ,
50 m_local_address(local_address) ,
52 m_sync_dns(sync_dns) ,
53 m_secure_connection_timeout(secure_connection_timeout) ,
56 G_DEBUG(
"SimpleClient::ctor" ) ;
69 std::string s = m_remote_location.displayString() ;
70 if( m_socket.get() != nullptr )
71 s.append( std::string() +
"@" + m_socket->asString() ) ;
77 return m_remote_location ;
82 if( m_remote_location.host() == update.
host() && m_remote_location.service() == update.
service() && update.
resolved() )
84 G_DEBUG(
"GNet::SimpleClient::updateLocation: reusing dns lookup for " << update.
displayString() ) ;
85 m_remote_location = update ;
91 if( m_socket.get() == nullptr )
92 throw NotConnected() ;
93 return *m_socket.get() ;
98 if( m_socket.get() == nullptr )
99 throw NotConnected() ;
100 return *m_socket.get() ;
105 G_DEBUG(
"GNet::SimpleClient::connect: [" << m_remote_location.displayString() <<
"]" ) ;
106 if( m_state != Idle )
107 throw ConnectError(
"wrong state" ) ;
109 m_remote_location.resolveTrivially() ;
110 if( m_remote_location.resolved() )
112 setState( Connecting ) ;
119 throw DnsError( error ) ;
121 setState( Connecting ) ;
126 setState( Resolving ) ;
127 m_resolver.start( m_remote_location ) ;
131 void GNet::SimpleClient::onResolved( std::string error ,
Location location )
134 throw DnsError( error ) ;
136 G_DEBUG(
"GNet::SimpleClient::onResolved: " << location.
displayString() ) ;
137 m_remote_location.update( location.
address() , location.
name() ) ;
138 setState( Connecting ) ;
142 void GNet::SimpleClient::startConnecting()
144 G_DEBUG(
"GNet::SimpleClient::startConnecting: local: " << m_local_address.displayString() ) ;
145 G_DEBUG(
"GNet::SimpleClient::startConnecting: remote: " << m_remote_location.displayString() ) ;
147 setState( Testing ) ;
152 m_socket.reset(
new StreamSocket(m_remote_location.address().domain()) ) ;
153 socket().addWriteHandler( *
this , m_eh ) ;
157 m_sp.reset(
new SocketProtocol(*
this,m_eh,*
this,*m_socket.get(),m_secure_connection_timeout) ) ;
161 if( m_bind_local_address )
162 bindLocalAddress( m_local_address ) ;
166 bool immediate = false ;
167 if( !socket().connect( m_remote_location.address() , &immediate ) )
168 throw ConnectError( c_cannot_connect_to + m_remote_location.address().displayString() ) ;
174 socket().dropWriteHandler() ;
175 m_on_connect_timer.startTimer( 0U ) ;
179 void GNet::SimpleClient::onConnectTimer()
181 G_DEBUG(
"GNet::SimpleClient::onConnectTimer: immediate connection" ) ;
187 G_DEBUG(
"GNet::SimpleClient::writeEvent" ) ;
191 void GNet::SimpleClient::onWriteable()
193 if( m_state == Connected )
195 if( m_sp->writeEvent() )
198 else if( m_state == Testing )
200 socket().dropWriteHandler() ;
201 setState( Connecting ) ;
202 m_on_connect_timer.startTimer( 2U , 100000U ) ;
204 else if( m_state == Connecting && socket().hasPeer() && m_remote_location.socks() )
206 setState( Socksing ) ;
207 m_socks.reset(
new Socks(m_remote_location) ) ;
208 if( m_socks->send( socket() ) )
210 socket().addOtherHandler( *
this , m_eh ) ;
211 socket().dropWriteHandler() ;
212 socket().addReadHandler( *
this , m_eh ) ;
216 socket().addOtherHandler( *
this , m_eh ) ;
217 socket().addWriteHandler( *
this , m_eh ) ;
218 socket().dropReadHandler() ;
221 else if( m_state == Connecting && socket().hasPeer() )
223 socket().dropWriteHandler() ;
224 socket().addReadHandler( *
this , m_eh ) ;
225 socket().addOtherHandler( *
this , m_eh ) ;
227 setState( Connected ) ;
231 else if( m_state == Connecting )
233 socket().dropWriteHandler() ;
234 throw ConnectError( c_cannot_connect_to + m_remote_location.address().displayString() ) ;
236 else if( m_state == Socksing )
238 G_ASSERT( m_socks.get() != nullptr ) ;
239 if( m_socks->send( socket() ) )
241 socket().dropWriteHandler() ;
242 socket().addReadHandler( *
this , m_eh ) ;
244 setState( Connected ) ;
253 if( m_state == Socksing || m_sp.get() == nullptr )
256 m_sp->otherEvent( reason ) ;
261 G_ASSERT( m_sp.get() != nullptr ) ;
262 if( m_state == Socksing )
264 G_ASSERT( m_socks.get() != nullptr ) ;
265 bool complete = m_socks->read( socket() ) ;
268 setState( Connected ) ;
275 if( m_sp.get() != nullptr )
282 return error.find( c_cannot_connect_to ) == 0U ;
285 void GNet::SimpleClient::close()
293 return m_state == Connected ;
296 void GNet::SimpleClient::bindLocalAddress(
const Address & local_address )
300 socket().bind( local_address ) ;
303 if( local_address.
isLoopback() && !m_remote_location.address().isLoopback() )
304 G_WARNING_ONCE(
"GNet::SimpleClient::bindLocalAddress: binding the loopback address for "
305 "outgoing connections may result in connection failures" ) ;
308 void GNet::SimpleClient::setState( State new_state )
310 m_state = new_state ;
316 m_socket.get() !=
nullptr ?
317 socket().getLocalAddress() :
324 m_socket.get() !=
nullptr ?
325 socket().getPeerAddress() :
331 std::pair<bool,Address> pair =
332 m_socket.get() !=
nullptr ?
333 socket().getPeerAddress() :
338 pair.second.displayString() :
339 (
"("+m_remote_location.displayString()+
")") ;
344 return m_sp->peerCertificate() ;
349 if( m_sp.get() == nullptr )
350 throw NotConnected(
"for secure-connect" ) ;
351 m_sp->secureConnect() ;
360 bool rc = m_sp->send( data , offset ) ;
virtual void writeEvent() override
Override from GNet::EventHandler.
virtual void readEvent() override
Override from GNet::EventHandler.
A class for making an outgoing connection to a remote server, with support for socket-level protocols...
bool send(const std::string &data, std::string::size_type offset=0)
Sends data to the peer.
void secureConnect()
Starts TLS/SSL client-side negotiation.
The GNet::Address class encapsulates a TCP/UDP transport address.
bool isLoopback() const
Returns true if this is a loopback address.
virtual std::string peerCertificate() const override
Returns the peer's TLS certificate.
A class which acquires the process's special privileges on construction and releases them on destruct...
void addClient(const Connection &simple_client)
Adds a client connection.
bool connected() const
Returns true if connected to the peer.
virtual void onConnectImp()
An alternative to onConnect() for private implementation classes.
std::string displayString() const
Returns a string representation for logging and debug.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
void connect()
Initates a connection to the remote server.
A derivation of GNet::Socket for a stream socket.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
std::string name() const
Returns the remote canonical name.
static bool enabled()
Returns true if test features are enabled.
static Monitor * instance()
Returns the singleton pointer. Returns null if none.
Location remoteLocation() const
Returns a Location structure containing the result of host() and service() name lookup if available...
virtual std::pair< bool, Address > localAddress() const override
Override from Connection.
Address address() const
Returns the remote address.
bool resolved() const
Returns true after update() has been called.
virtual std::string connectionState() const override
Returns the connection state display string.
std::string logId() const
Returns a identification string for logging purposes.
std::string service() const
Returns the remote service name, as passed in to the constructor.
void removeClient(const Connection &simple_client)
Removes a client connection.
static bool synchronousDnsDefault()
Returns true if DNS queries should normally be synchronous on this platform.
virtual void onSendImp()
Called from within send().
static bool async()
Returns true if the resolver supports asynchronous operation.
virtual ~SimpleClient()
Destructor.
virtual void otherEvent(Reason)
Called for a socket-exception event or a socket-close event on windows.
virtual std::pair< bool, Address > peerAddress() const override
Override from Connection.
virtual void otherEvent(EventHandler::Reason) override
Override from GNet::EventHandler.
static bool connectError(const std::string &reason)
Returns true if the reason string implies the SimpleClient::connect() failed.
static std::string resolve(Location &)
Does synchronous name resolution.
void updateLocation(const Location &)
Updates the constructor's Location object with the given one as long as both objects have the same ho...
SimpleClient(ExceptionHandler &, const Location &remote_info, bool bind_local_address=false, const Address &local_address=Address::defaultAddress(), bool sync_dns=synchronousDnsDefault(), unsigned int secure_connection_timeout=0U)
Constructor. Call connect() to start connecting.
StreamSocket & socket()
Returns a reference to the socket. Throws if not connected.
std::string host() const
Returns the remote host name, as passed in to the constructor.