Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

torcontrol.cpp File Reference
#include <torcontrol.h>
#include <utilstrencodings.h>
#include <netbase.h>
#include <net.h>
#include <util.h>
#include <crypto/hmac_sha256.h>
#include <vector>
#include <deque>
#include <set>
#include <stdlib.h>
#include <boost/bind.hpp>
#include <boost/signals2/signal.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/util.h>
#include <event2/event.h>
#include <event2/thread.h>
+ Include dependency graph for torcontrol.cpp:

Go to the source code of this file.

Classes

class  TorControlReply
 Reply from Tor, can be single or multi-line. More...
 
class  TorControlConnection
 Low-level handling for Tor control connection. More...
 
class  TorController
 Controller that connects to Tor control socket, authenticate, then create and maintain an ephemeral hidden service. More...
 

Functions

static std::pair< std::string, std::string > SplitTorReplyLine (const std::string &s)
 
static std::map< std::string, std::string > ParseTorReplyMapping (const std::string &s)
 Parse reply arguments in the form 'METHODS=COOKIE,SAFECOOKIE COOKIEFILE=".../control_auth_cookie"'. More...
 
static std::pair< bool, std::string > ReadBinaryFile (const fs::path &filename, size_t maxsize=std::numeric_limits< size_t >::max())
 Read full contents of a file and return them in a std::string. More...
 
static bool WriteBinaryFile (const fs::path &filename, const std::string &data)
 Write contents of std::string to a file. More...
 
static std::vector< uint8_t > ComputeResponse (const std::string &key, const std::vector< uint8_t > &cookie, const std::vector< uint8_t > &clientNonce, const std::vector< uint8_t > &serverNonce)
 Compute Tor SAFECOOKIE response. More...
 
static void TorControlThread ()
 
void StartTorControl (boost::thread_group &threadGroup, CScheduler &scheduler)
 
void InterruptTorControl ()
 
void StopTorControl ()
 

Variables

const std::string DEFAULT_TOR_CONTROL = "127.0.0.1:9051"
 Default control port. More...
 
static const int TOR_COOKIE_SIZE = 32
 Tor cookie size (from control-spec.txt) More...
 
static const int TOR_NONCE_SIZE = 32
 Size of client/server nonce for SAFECOOKIE. More...
 
static const std::string TOR_SAFE_SERVERKEY = "Tor safe cookie authentication server-to-controller hash"
 For computing serverHash in SAFECOOKIE. More...
 
static const std::string TOR_SAFE_CLIENTKEY = "Tor safe cookie authentication controller-to-server hash"
 For computing clientHash in SAFECOOKIE. More...
 
static const float RECONNECT_TIMEOUT_START = 1.0
 Exponential backoff configuration - initial timeout in seconds. More...
 
static const float RECONNECT_TIMEOUT_EXP = 1.5
 Exponential backoff configuration - growth factor. More...
 
static const int MAX_LINE_LENGTH = 100000
 Maximum length for lines received on TorControlConnection. More...
 
static struct event_base * gBase
 
static boost::thread torControlThread
 

Function Documentation

◆ ComputeResponse()

static std::vector<uint8_t> ComputeResponse ( const std::string &  key,
const std::vector< uint8_t > &  cookie,
const std::vector< uint8_t > &  clientNonce,
const std::vector< uint8_t > &  serverNonce 
)
static

Compute Tor SAFECOOKIE response.

ServerHash is computed as: HMAC-SHA256("Tor safe cookie authentication server-to-controller hash", CookieString | ClientNonce | ServerNonce) (with the HMAC key as its first argument)

After a controller sends a successful AUTHCHALLENGE command, the next command sent on the connection must be an AUTHENTICATE command, and the only authentication string which that AUTHENTICATE command will accept is:

HMAC-SHA256("Tor safe cookie authentication controller-to-server hash", CookieString | ClientNonce | ServerNonce)

Definition at line 564 of file torcontrol.cpp.

References CHMAC_SHA256::OUTPUT_SIZE.

Referenced by TorController::authchallenge_cb().

◆ InterruptTorControl()

void InterruptTorControl ( )

Definition at line 760 of file torcontrol.cpp.

References gBase, and LogPrintf.

Referenced by Interrupt().

◆ ParseTorReplyMapping()

static std::map<std::string,std::string> ParseTorReplyMapping ( const std::string &  s)
static

Parse reply arguments in the form 'METHODS=COOKIE,SAFECOOKIE COOKIEFILE=".../control_auth_cookie"'.

Returns a map of keys to values, or an empty map if there was an error. Grammar is implicitly defined in https://spec.torproject.org/control-spec by the server reply formats for PROTOCOLINFO (S3.21), AUTHCHALLENGE (S3.24), and ADD_ONION (S3.27). See also sections 2.1 and 2.3.

Unescape value. Per https://spec.torproject.org/control-spec section 2.1.1:

For future-proofing, controller implementors MAY use the following rules to be compatible with buggy Tor implementations and with future ones that implement the spec as intended:

Read
and \0 ... \377 as C escapes. Treat a backslash followed by any other character as that character.

Definition at line 273 of file torcontrol.cpp.

Referenced by TorController::add_onion_cb(), TorController::authchallenge_cb(), and TorController::protocolinfo_cb().

◆ ReadBinaryFile()

static std::pair<bool,std::string> ReadBinaryFile ( const fs::path &  filename,
size_t  maxsize = std::numeric_limits<size_t>::max() 
)
static

Read full contents of a file and return them in a std::string.

Returns a pair <status, string>. If an error occurred, status will be false, otherwise status will be true and the data will be returned in string.

Parameters
maxsizePuts a maximum size limit on the file that is read. If the file is larger than this, truncated data (with len > maxsize) will be returned.

Definition at line 367 of file torcontrol.cpp.

References fsbridge::fopen().

Referenced by TorController::protocolinfo_cb(), and TorController::TorController().

◆ SplitTorReplyLine()

static std::pair<std::string,std::string> SplitTorReplyLine ( const std::string &  s)
static

◆ StartTorControl()

void StartTorControl ( boost::thread_group &  threadGroup,
CScheduler scheduler 
)

Definition at line 743 of file torcontrol.cpp.

References gBase, LogPrintf, torControlThread, TorControlThread(), and TraceThread().

Referenced by AppInitMain().

◆ StopTorControl()

void StopTorControl ( )

Definition at line 768 of file torcontrol.cpp.

References gBase, and torControlThread.

Referenced by Shutdown().

◆ TorControlThread()

static void TorControlThread ( )
static

Definition at line 736 of file torcontrol.cpp.

References DEFAULT_TOR_CONTROL, gArgs, gBase, and ArgsManager::GetArg().

Referenced by StartTorControl().

◆ WriteBinaryFile()

static bool WriteBinaryFile ( const fs::path &  filename,
const std::string &  data 
)
static

Write contents of std::string to a file.

Returns
true on success.

Definition at line 393 of file torcontrol.cpp.

References fsbridge::fopen().

Referenced by TorController::add_onion_cb().

Variable Documentation

◆ DEFAULT_TOR_CONTROL

const std::string DEFAULT_TOR_CONTROL = "127.0.0.1:9051"

Default control port.

Functionality for communicating with Tor.

Definition at line 31 of file torcontrol.cpp.

Referenced by HelpMessage(), and TorControlThread().

◆ gBase

struct event_base* gBase
static

◆ MAX_LINE_LENGTH

const int MAX_LINE_LENGTH = 100000
static

Maximum length for lines received on TorControlConnection.

tor-control-spec.txt mentions that there is explicitly no limit defined to line length, this is belt-and-suspenders sanity limit to prevent memory exhaustion.

Definition at line 48 of file torcontrol.cpp.

Referenced by TorControlConnection::readcb().

◆ RECONNECT_TIMEOUT_EXP

const float RECONNECT_TIMEOUT_EXP = 1.5
static

Exponential backoff configuration - growth factor.

Definition at line 43 of file torcontrol.cpp.

Referenced by TorController::disconnected_cb().

◆ RECONNECT_TIMEOUT_START

const float RECONNECT_TIMEOUT_START = 1.0
static

Exponential backoff configuration - initial timeout in seconds.

Definition at line 41 of file torcontrol.cpp.

Referenced by TorController::connected_cb().

◆ TOR_COOKIE_SIZE

const int TOR_COOKIE_SIZE = 32
static

Tor cookie size (from control-spec.txt)

Definition at line 33 of file torcontrol.cpp.

Referenced by TorController::protocolinfo_cb().

◆ TOR_NONCE_SIZE

const int TOR_NONCE_SIZE = 32
static

Size of client/server nonce for SAFECOOKIE.

Definition at line 35 of file torcontrol.cpp.

Referenced by TorController::protocolinfo_cb().

◆ TOR_SAFE_CLIENTKEY

const std::string TOR_SAFE_CLIENTKEY = "Tor safe cookie authentication controller-to-server hash"
static

For computing clientHash in SAFECOOKIE.

Definition at line 39 of file torcontrol.cpp.

Referenced by TorController::authchallenge_cb().

◆ TOR_SAFE_SERVERKEY

const std::string TOR_SAFE_SERVERKEY = "Tor safe cookie authentication server-to-controller hash"
static

For computing serverHash in SAFECOOKIE.

Definition at line 37 of file torcontrol.cpp.

Referenced by TorController::authchallenge_cb().

◆ torControlThread

boost::thread torControlThread
static

Definition at line 734 of file torcontrol.cpp.

Referenced by StartTorControl(), and StopTorControl().

Released under the MIT license