mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-07-24 15:05:35 -04:00
fixed a few bugs in torcontrol socket methods
This commit is contained in:
parent
24e862ae25
commit
d9368507a9
8 changed files with 56 additions and 25 deletions
|
@ -24,7 +24,7 @@
|
||||||
#include "pqi/pqifdbin.h"
|
#include "pqi/pqifdbin.h"
|
||||||
|
|
||||||
RsFdBinInterface::RsFdBinInterface(int file_descriptor)
|
RsFdBinInterface::RsFdBinInterface(int file_descriptor)
|
||||||
: mCLintConnt(file_descriptor),mIsActive(file_descriptor!=0)
|
: mCLintConnt(file_descriptor),mIsActive(false)
|
||||||
{
|
{
|
||||||
mTotalReadBytes=0;
|
mTotalReadBytes=0;
|
||||||
mTotalInBufferBytes=0;
|
mTotalInBufferBytes=0;
|
||||||
|
|
|
@ -16,8 +16,9 @@ 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)
|
||||||
{
|
{
|
||||||
|
if(mState == CONNECTED)
|
||||||
close();
|
close();
|
||||||
|
|
||||||
mConnectPort = tcp_port;
|
mConnectPort = tcp_port;
|
||||||
|
@ -25,7 +26,7 @@ int RsTcpSocket::connect(const std::string& tcp_address,uint16_t tcp_port)
|
||||||
|
|
||||||
return connect();
|
return connect();
|
||||||
}
|
}
|
||||||
int RsTcpSocket::connect()
|
bool RsTcpSocket::connect()
|
||||||
{
|
{
|
||||||
int CreateSocket = 0;
|
int CreateSocket = 0;
|
||||||
char dataReceived[1024];
|
char dataReceived[1024];
|
||||||
|
@ -38,18 +39,21 @@ int RsTcpSocket::connect()
|
||||||
printf("Socket not created \n");
|
printf("Socket not created \n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ipOfServer.sin_family = AF_INET;
|
ipOfServer.sin_family = AF_INET;
|
||||||
ipOfServer.sin_port = htons(mConnectPort);
|
ipOfServer.sin_port = htons(mConnectPort);
|
||||||
ipOfServer.sin_addr.s_addr = inet_addr(mConnectAddress.c_str());
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
mState = CONNECTED;
|
mState = CONNECTED;
|
||||||
setSocket(mSocket);
|
|
||||||
|
int flags = fcntl(CreateSocket,F_GETFL);
|
||||||
|
fcntl(CreateSocket, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
|
||||||
|
setSocket(CreateSocket);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,10 @@ public:
|
||||||
CONNECTED = 0x02
|
CONNECTED = 0x02
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return 1 when OK, 0 otherwise.
|
// Return true when OK, false otherwise.
|
||||||
int connect();
|
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.
|
// Returns 1 when OK, 0 otherwise.
|
||||||
int close();
|
int close();
|
||||||
|
|
|
@ -179,10 +179,10 @@ void TorControl::setStatus(TorControl::Status n)
|
||||||
}
|
}
|
||||||
mStatusChanged_callback(mStatus, old);
|
mStatusChanged_callback(mStatus, old);
|
||||||
|
|
||||||
if (mStatus == TorControl::Connected && old < TorControl::Connected)
|
// if (mStatus == TorControl::Connected && old < TorControl::Connected)
|
||||||
socketConnected();//mConnected_callback();
|
// socketConnected();//mConnected_callback();
|
||||||
else if (mStatus < TorControl::Connected && old >= TorControl::Connected)
|
// else if (mStatus < TorControl::Connected && old >= TorControl::Connected)
|
||||||
socketDisconnected();//mDisconnected_callback();
|
// socketDisconnected();//mDisconnected_callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TorControl::setTorStatus(TorControl::TorStatus n)
|
void TorControl::setTorStatus(TorControl::TorStatus n)
|
||||||
|
@ -282,11 +282,14 @@ void TorControl::connect(const std::string &address, uint16_t port)
|
||||||
setTorStatus(TorUnknown);
|
setTorStatus(TorUnknown);
|
||||||
|
|
||||||
//bool b = d->socket->blockSignals(true);
|
//bool b = d->socket->blockSignals(true);
|
||||||
|
if(mSocket->isRunning())
|
||||||
mSocket->fullstop();
|
mSocket->fullstop();
|
||||||
//d->socket->blockSignals(b);
|
//d->socket->blockSignals(b);
|
||||||
|
|
||||||
setStatus(Connecting);
|
setStatus(Connecting);
|
||||||
mSocket->connectToHost(address, port);
|
|
||||||
|
if(mSocket->connectToHost(address, port))
|
||||||
|
setStatus(SocketConnected);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TorControl::reconnect()
|
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;
|
torCtrlDebug() << "torctrl: Connected socket; querying information" << std::endl;
|
||||||
setStatus(TorControl::Authenticating);
|
setStatus(TorControl::Authenticating);
|
||||||
|
|
|
@ -67,8 +67,9 @@ public:
|
||||||
Error = -1,
|
Error = -1,
|
||||||
NotConnected = 0x00,
|
NotConnected = 0x00,
|
||||||
Connecting = 0x01,
|
Connecting = 0x01,
|
||||||
Authenticating = 0x02,
|
SocketConnected= 0x02,
|
||||||
Connected = 0x03
|
Authenticating = 0x03,
|
||||||
|
Connected = 0x04
|
||||||
};
|
};
|
||||||
|
|
||||||
enum TorStatus
|
enum TorStatus
|
||||||
|
@ -97,6 +98,7 @@ public:
|
||||||
/* Connection */
|
/* Connection */
|
||||||
bool isConnected() const { return status() == Connected; }
|
bool isConnected() const { return status() == Connected; }
|
||||||
void connect(const std::string &address, uint16_t port);
|
void connect(const std::string &address, uint16_t port);
|
||||||
|
void authenticate();
|
||||||
|
|
||||||
/* Ownership means that tor is managed by this socket, and we
|
/* Ownership means that tor is managed by this socket, and we
|
||||||
* can shut it down, own its configuration, etc. */
|
* can shut it down, own its configuration, etc. */
|
||||||
|
@ -159,7 +161,6 @@ private:
|
||||||
void publishServices();
|
void publishServices();
|
||||||
void protocolInfoReply(TorControlCommand *sender);
|
void protocolInfoReply(TorControlCommand *sender);
|
||||||
void socketDisconnected();
|
void socketDisconnected();
|
||||||
void socketConnected();
|
|
||||||
void authenticateReply(TorControlCommand *sender);
|
void authenticateReply(TorControlCommand *sender);
|
||||||
|
|
||||||
std::function<void(int,int)> mStatusChanged_callback;
|
std::function<void(int,int)> mStatusChanged_callback;
|
||||||
|
|
|
@ -49,9 +49,9 @@ TorControlSocket::~TorControlSocket()
|
||||||
clear();
|
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
|
std::string TorControlSocket::peerAddress() const
|
||||||
{
|
{
|
||||||
|
|
|
@ -54,7 +54,7 @@ public:
|
||||||
|
|
||||||
std::string errorMessage() const { return m_errorMessage; }
|
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 registerEvent(const ByteArray &event, TorControlCommand *handler);
|
||||||
|
|
||||||
void sendCommand(const ByteArray& data) { sendCommand(0, data); }
|
void sendCommand(const ByteArray& data) { sendCommand(0, data); }
|
||||||
|
|
|
@ -463,6 +463,29 @@ void TorManager::run()
|
||||||
void TorManager::threadTick()
|
void TorManager::threadTick()
|
||||||
{
|
{
|
||||||
d->process->tick();
|
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)
|
bool TorManager::getProxyServerInfo(std::string& proxy_server_adress,uint16_t& proxy_server_port)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue