Bitcoin Core  26.99.0
P2P Digital Currency
torcontrol.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2021 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 <netaddress.h>
12 #include <util/fs.h>
13 
14 #include <event2/util.h>
15 
16 #include <cstdint>
17 #include <deque>
18 #include <functional>
19 #include <string>
20 #include <vector>
21 
22 constexpr int DEFAULT_TOR_CONTROL_PORT = 9051;
23 extern const std::string DEFAULT_TOR_CONTROL;
24 static const bool DEFAULT_LISTEN_ONION = true;
25 
26 void StartTorControl(CService onion_service_target);
27 void InterruptTorControl();
28 void StopTorControl();
29 
31 
34 {
35 public:
37 
38  int code;
39  std::vector<std::string> lines;
40 
41  void Clear()
42  {
43  code = 0;
44  lines.clear();
45  }
46 };
47 
52 {
53 public:
54  typedef std::function<void(TorControlConnection&)> ConnectionCB;
55  typedef std::function<void(TorControlConnection &,const TorControlReply &)> ReplyHandlerCB;
56 
59  explicit TorControlConnection(struct event_base *base);
61 
69  bool Connect(const std::string& tor_control_center, const ConnectionCB& connected, const ConnectionCB& disconnected);
70 
74  void Disconnect();
75 
80  bool Command(const std::string &cmd, const ReplyHandlerCB& reply_handler);
81 
82 private:
84  std::function<void(TorControlConnection&)> connected;
86  std::function<void(TorControlConnection&)> disconnected;
88  struct event_base *base;
90  struct bufferevent* b_conn{nullptr};
94  std::deque<ReplyHandlerCB> reply_handlers;
95 
97  static void readcb(struct bufferevent *bev, void *ctx);
98  static void eventcb(struct bufferevent *bev, short what, void *ctx);
99 };
100 
101 /****** Bitcoin specific TorController implementation ********/
102 
107 {
108 public:
109  TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
110  TorController() : conn{nullptr} {
111  // Used for testing only.
112  }
113  ~TorController();
114 
117 
119  void Reconnect();
120 private:
121  struct event_base* base;
122  const std::string m_tor_control_center;
124  std::string private_key;
125  std::string service_id;
126  bool reconnect;
127  struct event *reconnect_ev = nullptr;
132  std::vector<uint8_t> cookie;
134  std::vector<uint8_t> clientNonce;
135 
136 public:
142  void auth_cb(TorControlConnection& conn, const TorControlReply& reply);
151 
153  static void reconnect_cb(evutil_socket_t fd, short what, void *arg);
154 };
155 
156 #endif // BITCOIN_TORCONTROL_H
const auto cmd
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Low-level handling for Tor control connection.
Definition: torcontrol.h:52
TorControlReply message
Message being received.
Definition: torcontrol.h:92
std::deque< ReplyHandlerCB > reply_handlers
Response handlers.
Definition: torcontrol.h:94
bool Connect(const std::string &tor_control_center, const ConnectionCB &connected, const ConnectionCB &disconnected)
Connect to a Tor control port.
Definition: torcontrol.cpp:141
TorControlConnection(struct event_base *base)
Create a new TorControlConnection.
Definition: torcontrol.cpp:68
bool Command(const std::string &cmd, const ReplyHandlerCB &reply_handler)
Send a command, register a handler for the reply.
Definition: torcontrol.cpp:185
std::function< void(TorControlConnection &)> connected
Callback when ready for use.
Definition: torcontrol.h:84
static void readcb(struct bufferevent *bev, void *ctx)
Libevent handlers: internal.
Definition: torcontrol.cpp:79
std::function< void(TorControlConnection &, const TorControlReply &)> ReplyHandlerCB
Definition: torcontrol.h:55
static void eventcb(struct bufferevent *bev, short what, void *ctx)
Definition: torcontrol.cpp:124
std::function< void(TorControlConnection &)> disconnected
Callback when connection lost.
Definition: torcontrol.h:86
std::function< void(TorControlConnection &)> ConnectionCB
Definition: torcontrol.h:54
void Disconnect()
Disconnect from Tor control port.
Definition: torcontrol.cpp:178
struct bufferevent * b_conn
Connection to control socket.
Definition: torcontrol.h:90
struct event_base * base
Libevent event base.
Definition: torcontrol.h:88
Reply from Tor, can be single or multi-line.
Definition: torcontrol.h:34
std::vector< std::string > lines
Definition: torcontrol.h:39
Controller that connects to Tor control socket, authenticate, then create and maintain an ephemeral o...
Definition: torcontrol.h:107
TorControlConnection conn
Definition: torcontrol.h:123
static void reconnect_cb(evutil_socket_t fd, short what, void *arg)
Callback for reconnect timer.
Definition: torcontrol.cpp:655
std::string service_id
Definition: torcontrol.h:125
struct event_base * base
Definition: torcontrol.h:121
fs::path GetPrivateKeyFile()
Get name of file to store private key in.
Definition: torcontrol.cpp:650
std::vector< uint8_t > clientNonce
ClientNonce for SAFECOOKIE auth.
Definition: torcontrol.h:134
void connected_cb(TorControlConnection &conn)
Callback after successful connection.
Definition: torcontrol.cpp:613
void get_socks_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for GETINFO net/listeners/socks result.
Definition: torcontrol.cpp:349
void add_onion_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for ADD_ONION result.
Definition: torcontrol.cpp:416
const std::string m_tor_control_center
Definition: torcontrol.h:122
void disconnected_cb(TorControlConnection &conn)
Callback after connection lost or failed connection attempt.
Definition: torcontrol.cpp:621
const CService m_target
Definition: torcontrol.h:130
void authchallenge_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHCHALLENGE result.
Definition: torcontrol.cpp:502
CService service
Definition: torcontrol.h:129
std::vector< uint8_t > cookie
Cookie for SAFECOOKIE auth.
Definition: torcontrol.h:132
struct event * reconnect_ev
Definition: torcontrol.h:127
float reconnect_timeout
Definition: torcontrol.h:128
void auth_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHENTICATE result.
Definition: torcontrol.cpp:451
void Reconnect()
Reconnect, after getting disconnected.
Definition: torcontrol.cpp:639
std::string private_key
Definition: torcontrol.h:124
void protocolinfo_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for PROTOCOLINFO result.
Definition: torcontrol.cpp:537
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
CService DefaultOnionServiceTarget()
Definition: torcontrol.cpp:710
const std::string DEFAULT_TOR_CONTROL
Default control ip and port.
Definition: torcontrol.cpp:46
static const bool DEFAULT_LISTEN_ONION
Definition: torcontrol.h:24
constexpr int DEFAULT_TOR_CONTROL_PORT
Functionality for communicating with Tor.
Definition: torcontrol.h:22
void InterruptTorControl()
Definition: torcontrol.cpp:691
void StartTorControl(CService onion_service_target)
Definition: torcontrol.cpp:672
void StopTorControl()
Definition: torcontrol.cpp:701