Bitcoin Core  21.99.0
P2P Digital Currency
torcontrol.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
8 #ifndef BITCOIN_TORCONTROL_H
9 #define BITCOIN_TORCONTROL_H
10 
11 #include <fs.h>
12 #include <netaddress.h>
13 
14 #include <boost/signals2/signal.hpp>
15 
16 #include <event2/bufferevent.h>
17 #include <event2/event.h>
18 
19 #include <cstdlib>
20 #include <deque>
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
25 class CService;
26 
27 extern const std::string DEFAULT_TOR_CONTROL;
28 static const bool DEFAULT_LISTEN_ONION = true;
29 
30 void StartTorControl(CService onion_service_target);
31 void InterruptTorControl();
32 void StopTorControl();
33 
35 
38 {
39 public:
41 
42  int code;
43  std::vector<std::string> lines;
44 
45  void Clear()
46  {
47  code = 0;
48  lines.clear();
49  }
50 };
51 
56 {
57 public:
58  typedef std::function<void(TorControlConnection&)> ConnectionCB;
59  typedef std::function<void(TorControlConnection &,const TorControlReply &)> ReplyHandlerCB;
60 
63  explicit TorControlConnection(struct event_base *base);
65 
73  bool Connect(const std::string& tor_control_center, const ConnectionCB& connected, const ConnectionCB& disconnected);
74 
78  void Disconnect();
79 
84  bool Command(const std::string &cmd, const ReplyHandlerCB& reply_handler);
85 
87  boost::signals2::signal<void(TorControlConnection &,const TorControlReply &)> async_handler;
88 private:
90  std::function<void(TorControlConnection&)> connected;
92  std::function<void(TorControlConnection&)> disconnected;
94  struct event_base *base;
96  struct bufferevent *b_conn;
100  std::deque<ReplyHandlerCB> reply_handlers;
101 
103  static void readcb(struct bufferevent *bev, void *ctx);
104  static void eventcb(struct bufferevent *bev, short what, void *ctx);
105 };
106 
107 /****** Bitcoin specific TorController implementation ********/
108 
113 {
114 public:
115  TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
116  TorController() : conn{nullptr} {
117  // Used for testing only.
118  }
119  ~TorController();
120 
122  fs::path GetPrivateKeyFile();
123 
125  void Reconnect();
126 private:
127  struct event_base* base;
128  const std::string m_tor_control_center;
130  std::string private_key;
131  std::string service_id;
132  bool reconnect;
133  struct event *reconnect_ev = nullptr;
138  std::vector<uint8_t> cookie;
140  std::vector<uint8_t> clientNonce;
141 
142 public:
146  void auth_cb(TorControlConnection& conn, const TorControlReply& reply);
155 
157  static void reconnect_cb(evutil_socket_t fd, short what, void *arg);
158 };
159 
160 #endif /* BITCOIN_TORCONTROL_H */
TorControlReply
Reply from Tor, can be single or multi-line.
Definition: torcontrol.h:37
TorController::conn
TorControlConnection conn
Definition: torcontrol.h:129
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:551
TorControlConnection::async_handler
boost::signals2::signal< void(TorControlConnection &, const TorControlReply &)> async_handler
Response handlers for async replies.
Definition: torcontrol.h:87
TorControlConnection::b_conn
struct bufferevent * b_conn
Connection to control socket.
Definition: torcontrol.h:96
TorControlReply::code
int code
Definition: torcontrol.h:42
TorController::m_tor_control_center
const std::string m_tor_control_center
Definition: torcontrol.h:128
fs.h
TorController::connected_cb
void connected_cb(TorControlConnection &conn)
Callback after successful connection.
Definition: torcontrol.cpp:526
TorController::protocolinfo_cb
void protocolinfo_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for PROTOCOLINFO result.
Definition: torcontrol.cpp:452
TorControlConnection::disconnected
std::function< void(TorControlConnection &)> disconnected
Callback when connection lost.
Definition: torcontrol.h:92
StopTorControl
void StopTorControl()
Definition: torcontrol.cpp:614
TorControlConnection::ConnectionCB
std::function< void(TorControlConnection &)> ConnectionCB
Definition: torcontrol.h:58
TorControlConnection
Low-level handling for Tor control connection.
Definition: torcontrol.h:55
TorController::cookie
std::vector< uint8_t > cookie
Cookie for SAFECOOKIE auth.
Definition: torcontrol.h:138
TorControlConnection::reply_handlers
std::deque< ReplyHandlerCB > reply_handlers
Response handlers.
Definition: torcontrol.h:100
DefaultOnionServiceTarget
CService DefaultOnionServiceTarget()
Definition: torcontrol.cpp:623
InterruptTorControl
void InterruptTorControl()
Definition: torcontrol.cpp:604
TorController::~TorController
~TorController()
Definition: torcontrol.cpp:317
TorControlReply::Clear
void Clear()
Definition: torcontrol.h:45
TorControlConnection::readcb
static void readcb(struct bufferevent *bev, void *ctx)
Libevent handlers: internal.
Definition: torcontrol.cpp:70
TorControlConnection::Connect
bool Connect(const std::string &tor_control_center, const ConnectionCB &connected, const ConnectionCB &disconnected)
Connect to a Tor control port.
Definition: torcontrol.cpp:132
TorController::disconnected_cb
void disconnected_cb(TorControlConnection &conn)
Callback after connection lost or failed connection attempt.
Definition: torcontrol.cpp:534
TorController::reconnect_ev
struct event * reconnect_ev
Definition: torcontrol.h:133
DEFAULT_LISTEN_ONION
static const bool DEFAULT_LISTEN_ONION
Definition: torcontrol.h:28
StartTorControl
void StartTorControl(CService onion_service_target)
Definition: torcontrol.cpp:585
TorController::add_onion_cb
void add_onion_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for ADD_ONION result.
Definition: torcontrol.cpp:328
TorController::service_id
std::string service_id
Definition: torcontrol.h:131
TorControlReply::TorControlReply
TorControlReply()
Definition: torcontrol.h:40
TorControlConnection::TorControlConnection
TorControlConnection(struct event_base *base)
Create a new TorControlConnection.
Definition: torcontrol.cpp:59
netaddress.h
TorControlConnection::ReplyHandlerCB
std::function< void(TorControlConnection &, const TorControlReply &)> ReplyHandlerCB
Definition: torcontrol.h:59
TorControlReply::lines
std::vector< std::string > lines
Definition: torcontrol.h:43
TorControlConnection::Disconnect
void Disconnect()
Disconnect from Tor control port.
Definition: torcontrol.cpp:162
TorController::service
CService service
Definition: torcontrol.h:135
TorController::base
struct event_base * base
Definition: torcontrol.h:127
TorController::auth_cb
void auth_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHENTICATE result.
Definition: torcontrol.cpp:363
TorController::reconnect
bool reconnect
Definition: torcontrol.h:132
TorController::Reconnect
void Reconnect()
Reconnect, after getting disconnected.
Definition: torcontrol.cpp:552
TorController::clientNonce
std::vector< uint8_t > clientNonce
ClientNonce for SAFECOOKIE auth.
Definition: torcontrol.h:140
TorControlConnection::Command
bool Command(const std::string &cmd, const ReplyHandlerCB &reply_handler)
Send a command, register a handler for the reply.
Definition: torcontrol.cpp:169
TorController::m_target
const CService m_target
Definition: torcontrol.h:136
TorController::reconnect_timeout
float reconnect_timeout
Definition: torcontrol.h:134
TorController::authchallenge_cb
void authchallenge_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHCHALLENGE result.
Definition: torcontrol.cpp:417
TorController
Controller that connects to Tor control socket, authenticate, then create and maintain an ephemeral o...
Definition: torcontrol.h:112
DEFAULT_TOR_CONTROL
const std::string DEFAULT_TOR_CONTROL
Default control port.
Definition: torcontrol.cpp:38
TorController::TorController
TorController()
Definition: torcontrol.h:116
TorController::private_key
std::string private_key
Definition: torcontrol.h:130
TorController::GetPrivateKeyFile
fs::path GetPrivateKeyFile()
Get name of file to store private key in.
Definition: torcontrol.cpp:563
TorControlConnection::~TorControlConnection
~TorControlConnection()
Definition: torcontrol.cpp:64
TorControlConnection::eventcb
static void eventcb(struct bufferevent *bev, short what, void *ctx)
Definition: torcontrol.cpp:115
TorControlConnection::message
TorControlReply message
Message being received.
Definition: torcontrol.h:98
TorControlConnection::base
struct event_base * base
Libevent event base.
Definition: torcontrol.h:94
TorControlConnection::connected
std::function< void(TorControlConnection &)> connected
Callback when ready for use.
Definition: torcontrol.h:90
TorController::reconnect_cb
static void reconnect_cb(evutil_socket_t fd, short what, void *arg)
Callback for reconnect timer.
Definition: torcontrol.cpp:568
ctx
static secp256k1_context * ctx
Definition: tests.c:36