fixed a few bugs in torcontrol socket methods

This commit is contained in:
csoler 2021-12-15 22:15:52 +01:00
parent 24e862ae25
commit d9368507a9
8 changed files with 56 additions and 25 deletions

View File

@ -24,7 +24,7 @@
#include "pqi/pqifdbin.h"
RsFdBinInterface::RsFdBinInterface(int file_descriptor)
: mCLintConnt(file_descriptor),mIsActive(file_descriptor!=0)
: mCLintConnt(file_descriptor),mIsActive(false)
{
mTotalReadBytes=0;
mTotalInBufferBytes=0;

View File

@ -16,16 +16,17 @@ RsTcpSocket::RsTcpSocket()
{
}
int RsTcpSocket::connect(const std::string& tcp_address,uint16_t tcp_port)
bool RsTcpSocket::connect(const std::string& tcp_address,uint16_t tcp_port)
{
close();
if(mState == CONNECTED)
close();
mConnectPort = tcp_port;
mConnectAddress = tcp_address;
return connect();
}
int RsTcpSocket::connect()
bool RsTcpSocket::connect()
{
int CreateSocket = 0;
char dataReceived[1024];
@ -38,18 +39,21 @@ int RsTcpSocket::connect()
printf("Socket not created \n");
return false;
}
ipOfServer.sin_family = AF_INET;
ipOfServer.sin_port = htons(mConnectPort);
ipOfServer.sin_addr.s_addr = inet_addr(mConnectAddress.c_str());
if(::connect(mSocket, (struct sockaddr *)&ipOfServer, sizeof(ipOfServer))<0)
if(::connect(CreateSocket, (struct sockaddr *)&ipOfServer, sizeof(ipOfServer))<0)
{
printf("Connection failed due to port and ip problems, or server is not available\n");
printf("Connection failed due to port and ip problems, or server is not available. Socket=%d,ConnectPort=%d,ConnectAddress=%s Errno=%d\n",mSocket,mConnectPort,mConnectAddress.c_str(),errno);
return false;
}
mState = CONNECTED;
setSocket(mSocket);
int flags = fcntl(CreateSocket,F_GETFL);
fcntl(CreateSocket, F_SETFL, flags | O_NONBLOCK);
setSocket(CreateSocket);
return true;
}

View File

@ -15,10 +15,10 @@ public:
CONNECTED = 0x02
};
// Return 1 when OK, 0 otherwise.
int connect();
// Return true when OK, false otherwise.
bool connect();
int connect(const std::string& tcp_address,uint16_t tcp_port);
bool connect(const std::string& tcp_address,uint16_t tcp_port);
// Returns 1 when OK, 0 otherwise.
int close();

View File

@ -179,10 +179,10 @@ void TorControl::setStatus(TorControl::Status n)
}
mStatusChanged_callback(mStatus, old);
if (mStatus == TorControl::Connected && old < TorControl::Connected)
socketConnected();//mConnected_callback();
else if (mStatus < TorControl::Connected && old >= TorControl::Connected)
socketDisconnected();//mDisconnected_callback();
// if (mStatus == TorControl::Connected && old < TorControl::Connected)
// socketConnected();//mConnected_callback();
// else if (mStatus < TorControl::Connected && old >= TorControl::Connected)
// socketDisconnected();//mDisconnected_callback();
}
void TorControl::setTorStatus(TorControl::TorStatus n)
@ -282,11 +282,14 @@ void TorControl::connect(const std::string &address, uint16_t port)
setTorStatus(TorUnknown);
//bool b = d->socket->blockSignals(true);
mSocket->fullstop();
if(mSocket->isRunning())
mSocket->fullstop();
//d->socket->blockSignals(b);
setStatus(Connecting);
mSocket->connectToHost(address, port);
if(mSocket->connectToHost(address, port))
setStatus(SocketConnected);
}
void TorControl::reconnect()
@ -336,9 +339,9 @@ void TorControl::authenticateReply(TorControlCommand *sender)
}
void TorControl::socketConnected()
void TorControl::authenticate()
{
assert(mStatus == TorControl::Connecting);
assert(mStatus == TorControl::SocketConnected);
torCtrlDebug() << "torctrl: Connected socket; querying information" << std::endl;
setStatus(TorControl::Authenticating);

View File

@ -67,8 +67,9 @@ public:
Error = -1,
NotConnected = 0x00,
Connecting = 0x01,
Authenticating = 0x02,
Connected = 0x03
SocketConnected= 0x02,
Authenticating = 0x03,
Connected = 0x04
};
enum TorStatus
@ -97,6 +98,7 @@ public:
/* Connection */
bool isConnected() const { return status() == Connected; }
void connect(const std::string &address, uint16_t port);
void authenticate();
/* Ownership means that tor is managed by this socket, and we
* can shut it down, own its configuration, etc. */
@ -159,7 +161,6 @@ private:
void publishServices();
void protocolInfoReply(TorControlCommand *sender);
void socketDisconnected();
void socketConnected();
void authenticateReply(TorControlCommand *sender);
std::function<void(int,int)> mStatusChanged_callback;

View File

@ -49,9 +49,9 @@ TorControlSocket::~TorControlSocket()
clear();
}
void TorControlSocket::connectToHost(const std::string& tcp_address,uint16_t tcp_port)
bool TorControlSocket::connectToHost(const std::string& tcp_address,uint16_t tcp_port)
{
RsTcpSocket::connect(tcp_address,tcp_port);
return RsTcpSocket::connect(tcp_address,tcp_port);
}
std::string TorControlSocket::peerAddress() const
{

View File

@ -54,7 +54,7 @@ public:
std::string errorMessage() const { return m_errorMessage; }
void connectToHost(const std::string& tcp_address,uint16_t tcp_port);
bool connectToHost(const std::string& tcp_address,uint16_t tcp_port);
void registerEvent(const ByteArray &event, TorControlCommand *handler);
void sendCommand(const ByteArray& data) { sendCommand(0, data); }

View File

@ -463,6 +463,29 @@ void TorManager::run()
void TorManager::threadTick()
{
d->process->tick();
if(d->process->state() != TorProcess::Ready)
return;
switch(d->control->status())
{
case TorControl::NotConnected:
RsDbg() << "Connecting to tor process at " << d->process->controlHost() << ":" << d->process->controlPort() << "..." ;
d->control->connect(d->process->controlHost(),d->process->controlPort());
break;
case TorControl::SocketConnected:
RsDbg() << "Connection established." ;
d->control->authenticate();
break;
case TorControl::Authenticating:
RsDbg() << "Authenticating..." ;
break;
case TorControl::Connected:;
break;
}
}
bool TorManager::getProxyServerInfo(std::string& proxy_server_adress,uint16_t& proxy_server_port)