Skip to content

Commit ec5c0b2

Browse files
committed
torcontrol: Define tor reply code as const to improve maintainability
Signed-off-by: Eval EXEC <[email protected]>
1 parent 4c1906a commit ec5c0b2

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/torcontrol.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const std::string DEFAULT_TOR_CONTROL = "127.0.0.1:" + ToString(DEFAULT_TOR_CONT
5353
static const int TOR_COOKIE_SIZE = 32;
5454
/** Size of client/server nonce for SAFECOOKIE */
5555
static const int TOR_NONCE_SIZE = 32;
56+
/** Tor control reply code. Ref: https://spec.torproject.org/control-spec/replies.html */
57+
static const int TOR_REPLY_OK = 250;
58+
static const int TOR_REPLY_UNRECOGNIZED = 510;
5659
/** For computing serverHash in SAFECOOKIE */
5760
static const std::string TOR_SAFE_SERVERKEY = "Tor safe cookie authentication server-to-controller hash";
5861
/** For computing clientHash in SAFECOOKIE */
@@ -357,7 +360,7 @@ void TorController::get_socks_cb(TorControlConnection& _conn, const TorControlRe
357360
{
358361
// NOTE: We can only get here if -onion is unset
359362
std::string socks_location;
360-
if (reply.code == 250) {
363+
if (reply.code == TOR_REPLY_OK) {
361364
for (const auto& line : reply.lines) {
362365
if (line.starts_with("net/listeners/socks=")) {
363366
const std::string port_list_str = line.substr(20);
@@ -382,7 +385,7 @@ void TorController::get_socks_cb(TorControlConnection& _conn, const TorControlRe
382385
} else {
383386
LogPrintf("tor: Get SOCKS port command returned nothing\n");
384387
}
385-
} else if (reply.code == 510) { // 510 Unrecognized command
388+
} else if (reply.code == TOR_REPLY_UNRECOGNIZED) {
386389
LogPrintf("tor: Get SOCKS port command failed with unrecognized command (You probably should upgrade Tor)\n");
387390
} else {
388391
LogPrintf("tor: Get SOCKS port command failed; error code %d\n", reply.code);
@@ -422,7 +425,7 @@ void TorController::get_socks_cb(TorControlConnection& _conn, const TorControlRe
422425

423426
void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlReply& reply)
424427
{
425-
if (reply.code == 250) {
428+
if (reply.code == TOR_REPLY_OK) {
426429
LogDebug(BCLog::TOR, "ADD_ONION successful\n");
427430
for (const std::string &s : reply.lines) {
428431
std::map<std::string,std::string> m = ParseTorReplyMapping(s);
@@ -448,7 +451,7 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
448451
}
449452
AddLocal(service, LOCAL_MANUAL);
450453
// ... onion requested - keep connection open
451-
} else if (reply.code == 510) { // 510 Unrecognized command
454+
} else if (reply.code == TOR_REPLY_UNRECOGNIZED) {
452455
LogPrintf("tor: Add onion failed with unrecognized command (You probably need to upgrade Tor)\n");
453456
} else {
454457
LogPrintf("tor: Add onion failed; error code %d\n", reply.code);
@@ -457,7 +460,7 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
457460

458461
void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply& reply)
459462
{
460-
if (reply.code == 250) {
463+
if (reply.code == TOR_REPLY_OK) {
461464
LogDebug(BCLog::TOR, "Authentication successful\n");
462465

463466
// Now that we know Tor is running setup the proxy for onion addresses
@@ -508,7 +511,7 @@ static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::v
508511

509512
void TorController::authchallenge_cb(TorControlConnection& _conn, const TorControlReply& reply)
510513
{
511-
if (reply.code == 250) {
514+
if (reply.code == TOR_REPLY_OK) {
512515
LogDebug(BCLog::TOR, "SAFECOOKIE authentication challenge successful\n");
513516
std::pair<std::string,std::string> l = SplitTorReplyLine(reply.lines[0]);
514517
if (l.first == "AUTHCHALLENGE") {
@@ -543,7 +546,7 @@ void TorController::authchallenge_cb(TorControlConnection& _conn, const TorContr
543546

544547
void TorController::protocolinfo_cb(TorControlConnection& _conn, const TorControlReply& reply)
545548
{
546-
if (reply.code == 250) {
549+
if (reply.code == TOR_REPLY_OK) {
547550
std::set<std::string> methods;
548551
std::string cookiefile;
549552
/*

0 commit comments

Comments
 (0)