diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -133,7 +133,9 @@ : base(_base), b_conn(0) {} TorControlConnection::~TorControlConnection() { - if (b_conn) bufferevent_free(b_conn); + if (b_conn) { + bufferevent_free(b_conn); + } } void TorControlConnection::readcb(struct bufferevent *bev, void *ctx) { @@ -148,7 +150,9 @@ std::string s(line, n_read_out); free(line); // Short line - if (s.size() < 4) continue; + if (s.size() < 4) { + continue; + } // (-|+| ) self->message.code = atoi(s.substr(0, 3)); self->message.lines.push_back(s.substr(4)); @@ -191,11 +195,12 @@ LogPrint(BCLog::TOR, "tor: Successfully connected!\n"); self->connected(*self); } else if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { - if (what & BEV_EVENT_ERROR) + if (what & BEV_EVENT_ERROR) { LogPrint(BCLog::TOR, "tor: Error connecting to Tor control socket\n"); - else + } else { LogPrint(BCLog::TOR, "tor: End of stream\n"); + } self->Disconnect(); self->disconnected(*self); } @@ -204,7 +209,9 @@ bool TorControlConnection::Connect(const std::string &target, const ConnectionCB &_connected, const ConnectionCB &_disconnected) { - if (b_conn) Disconnect(); + if (b_conn) { + Disconnect(); + } // Parse target address:port struct sockaddr_storage connect_to_addr; int connect_to_addrlen = sizeof(connect_to_addr); @@ -217,7 +224,9 @@ // Create a new socket, set up callbacks and enable notification bits b_conn = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); - if (!b_conn) return false; + if (!b_conn) { + return false; + } bufferevent_setcb(b_conn, TorControlConnection::readcb, nullptr, TorControlConnection::eventcb, this); bufferevent_enable(b_conn, EV_READ | EV_WRITE); @@ -234,16 +243,22 @@ } bool TorControlConnection::Disconnect() { - if (b_conn) bufferevent_free(b_conn); + if (b_conn) { + bufferevent_free(b_conn); + } b_conn = 0; return true; } bool TorControlConnection::Command(const std::string &cmd, const ReplyHandlerCB &reply_handler) { - if (!b_conn) return false; + if (!b_conn) { + return false; + } struct evbuffer *buf = bufferevent_get_output(b_conn); - if (!buf) return false; + if (!buf) { + return false; + } evbuffer_add(buf, cmd.data(), cmd.size()); evbuffer_add(buf, "\r\n", 2); reply_handlers.push_back(reply_handler); @@ -263,7 +278,10 @@ type.push_back(s[ptr]); ++ptr; } - if (ptr < s.size()) ++ptr; // skip ' ' + if (ptr < s.size()) { + // skip ' ' + ++ptr; + } return make_pair(type, s.substr(ptr)); } @@ -282,7 +300,9 @@ ++ptr; } // unexpected end of line - if (ptr == s.size()) return std::map(); + if (ptr == s.size()) { + return std::map(); + } // skip '=' ++ptr; // Quoted string @@ -296,7 +316,9 @@ ++ptr; } // unexpected end of line - if (ptr == s.size()) return std::map(); + if (ptr == s.size()) { + return std::map(); + } // skip closing '"' ++ptr; /* TODO: unescape value - according to the spec this depends on the @@ -342,7 +364,9 @@ size_t n; while ((n = fread(buffer, 1, sizeof(buffer), f)) > 0) { retval.append(buffer, buffer + n); - if (retval.size() > maxsize) break; + if (retval.size() > maxsize) { + break; + } } fclose(f); return std::make_pair(true, retval); @@ -354,7 +378,9 @@ */ static bool WriteBinaryFile(const fs::path &filename, const std::string &data) { FILE *f = fsbridge::fopen(filename, "wb"); - if (f == nullptr) return false; + if (f == nullptr) { + return false; + } if (fwrite(data.data(), 1, data.size(), f) != data.size()) { fclose(f); return false; @@ -419,9 +445,10 @@ : base(_base), target(_target), conn(base), reconnect(true), reconnect_ev(0), reconnect_timeout(RECONNECT_TIMEOUT_START) { reconnect_ev = event_new(base, -1, 0, reconnect_cb, this); - if (!reconnect_ev) + if (!reconnect_ev) { LogPrintf( "tor: Failed to create event for reconnection: out of memory?\n"); + } // Start connection attempts immediately if (!conn.Connect(_target, boost::bind(&TorController::connected_cb, this, _1), @@ -455,8 +482,12 @@ for (const std::string &s : reply.lines) { std::map m = ParseTorReplyMapping(s); std::map::iterator i; - if ((i = m.find("ServiceID")) != m.end()) service_id = i->second; - if ((i = m.find("PrivateKey")) != m.end()) private_key = i->second; + if ((i = m.find("ServiceID")) != m.end()) { + service_id = i->second; + } + if ((i = m.find("PrivateKey")) != m.end()) { + private_key = i->second; + } } service = LookupNumeric(std::string(service_id + ".onion").c_str(), GetListenPort()); @@ -599,10 +630,12 @@ std::map m = ParseTorReplyMapping(l.second); std::map::iterator i; - if ((i = m.find("METHODS")) != m.end()) + if ((i = m.find("METHODS")) != m.end()) { boost::split(methods, i->second, boost::is_any_of(",")); - if ((i = m.find("COOKIEFILE")) != m.end()) + } + if ((i = m.find("COOKIEFILE")) != m.end()) { cookiefile = i->second; + } } else if (l.first == "VERSION") { std::map m = ParseTorReplyMapping(l.second); @@ -687,15 +720,20 @@ // expected if (!_conn.Command( "PROTOCOLINFO 1", - boost::bind(&TorController::protocolinfo_cb, this, _1, _2))) + boost::bind(&TorController::protocolinfo_cb, this, _1, _2))) { LogPrintf("tor: Error sending initial protocolinfo command\n"); + } } void TorController::disconnected_cb(TorControlConnection &_conn) { // Stop advertising service when disconnected - if (service.IsValid()) RemoveLocal(service); + if (service.IsValid()) { + RemoveLocal(service); + } service = CService(); - if (!reconnect) return; + if (!reconnect) { + return; + } LogPrint(BCLog::TOR, "tor: Not connected to Tor control port %s, trying to reconnect\n", @@ -703,7 +741,9 @@ // Single-shot timer for reconnect. Use exponential backoff. struct timeval time = MillisToTimeval(int64_t(reconnect_timeout * 1000.0)); - if (reconnect_ev) event_add(reconnect_ev, &time); + if (reconnect_ev) { + event_add(reconnect_ev, &time); + } reconnect_timeout *= RECONNECT_TIMEOUT_EXP; }