Skip to content

Commit

Permalink
fix: tor: Call event_base_loopbreak from the event's callback
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Aug 9, 2021
1 parent c0c0d5d commit 6e75b36
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/torcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TorControlConnection

/** Create a new TorControlConnection.
*/
TorControlConnection(struct event_base *base);
explicit TorControlConnection(struct event_base *base);
~TorControlConnection();

/**
Expand All @@ -93,7 +93,7 @@ class TorControlConnection
/**
* Disconnect from Tor control port.
*/
bool Disconnect();
void Disconnect();

/** Send a command, register a handler for the reply.
* A trailing CRLF is automatically added.
Expand Down Expand Up @@ -123,7 +123,7 @@ class TorControlConnection
};

TorControlConnection::TorControlConnection(struct event_base *_base):
base(_base), b_conn(0)
base(_base), b_conn(nullptr)
{
}

Expand All @@ -140,8 +140,8 @@ void TorControlConnection::readcb(struct bufferevent *bev, void *ctx)
size_t n_read_out = 0;
char *line;
assert(input);
// If there is not a whole line to read, evbuffer_readln returns NULL
while((line = evbuffer_readln(input, &n_read_out, EVBUFFER_EOL_CRLF)) != NULL)
// If there is not a whole line to read, evbuffer_readln returns nullptr
while((line = evbuffer_readln(input, &n_read_out, EVBUFFER_EOL_CRLF)) != nullptr)
{
std::string s(line, n_read_out);
free(line);
Expand Down Expand Up @@ -212,7 +212,7 @@ bool TorControlConnection::Connect(const std::string &target, const ConnectionCB
b_conn = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
if (!b_conn)
return false;
bufferevent_setcb(b_conn, TorControlConnection::readcb, NULL, TorControlConnection::eventcb, this);
bufferevent_setcb(b_conn, TorControlConnection::readcb, nullptr, TorControlConnection::eventcb, this);
bufferevent_enable(b_conn, EV_READ|EV_WRITE);
this->connected = _connected;
this->disconnected = _disconnected;
Expand All @@ -225,12 +225,11 @@ bool TorControlConnection::Connect(const std::string &target, const ConnectionCB
return true;
}

bool TorControlConnection::Disconnect()
void TorControlConnection::Disconnect()
{
if (b_conn)
bufferevent_free(b_conn);
b_conn = 0;
return true;
b_conn = nullptr;
}

bool TorControlConnection::Command(const std::string &cmd, const ReplyHandlerCB& reply_handler)
Expand Down Expand Up @@ -335,7 +334,7 @@ static std::map<std::string,std::string> ParseTorReplyMapping(const std::string
if (j == 3 && value[i] > '3') {
j--;
}
escaped_value.push_back(strtol(value.substr(i, j).c_str(), NULL, 8));
escaped_value.push_back(strtol(value.substr(i, j).c_str(), nullptr, 8));
// Account for automatic incrementing at loop end
i += j - 1;
} else {
Expand Down Expand Up @@ -369,7 +368,7 @@ static std::map<std::string,std::string> ParseTorReplyMapping(const std::string
static std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize=std::numeric_limits<size_t>::max())
{
FILE *f = fsbridge::fopen(filename, "rb");
if (f == NULL)
if (f == nullptr)
return std::make_pair(false,"");
std::string retval;
char buffer[128];
Expand All @@ -395,7 +394,7 @@ static std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size
static bool WriteBinaryFile(const fs::path &filename, const std::string &data)
{
FILE *f = fsbridge::fopen(filename, "wb");
if (f == NULL)
if (f == nullptr)
return false;
if (fwrite(data.data(), 1, data.size(), f) != data.size()) {
fclose(f);
Expand All @@ -408,15 +407,15 @@ static bool WriteBinaryFile(const fs::path &filename, const std::string &data)
/****** Bitcoin specific TorController implementation ********/

/** Controller that connects to Tor control socket, authenticate, then create
* and maintain a ephemeral hidden service.
* and maintain an ephemeral onion service.
*/
class TorController
{
public:
TorController(struct event_base* base, const std::string& target);
~TorController();

/** Get name fo file to store private key in */
/** Get name of file to store private key in */
fs::path GetPrivateKeyFile();

/** Reconnect, after getting disconnected */
Expand Down Expand Up @@ -478,7 +477,7 @@ TorController::~TorController()
{
if (reconnect_ev) {
event_free(reconnect_ev);
reconnect_ev = 0;
reconnect_ev = nullptr;
}
if (service.IsValid()) {
RemoveLocal(service);
Expand Down Expand Up @@ -763,7 +762,9 @@ void InterruptTorControl()
{
if (gBase) {
LogPrintf("tor: Thread interrupt\n");
event_base_loopbreak(gBase);
event_base_once(gBase, -1, EV_TIMEOUT, [](evutil_socket_t, short, void*) {
event_base_loopbreak(gBase);
}, nullptr, nullptr);
}
}

Expand Down

0 comments on commit 6e75b36

Please sign in to comment.