Bitcoin Core  26.1.0
P2P Digital Currency
i2p.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 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 
5 #ifndef BITCOIN_I2P_H
6 #define BITCOIN_I2P_H
7 
8 #include <compat/compat.h>
9 #include <netaddress.h>
10 #include <sync.h>
11 #include <util/fs.h>
12 #include <util/sock.h>
13 #include <util/threadinterrupt.h>
14 
15 #include <memory>
16 #include <optional>
17 #include <string>
18 #include <unordered_map>
19 #include <vector>
20 
21 namespace i2p {
22 
26 using Binary = std::vector<uint8_t>;
27 
31 struct Connection {
33  std::unique_ptr<Sock> sock;
34 
37 
40 };
41 
42 namespace sam {
43 
50 static constexpr size_t MAX_MSG_SIZE{65536};
51 
55 class Session
56 {
57 public:
69  Session(const fs::path& private_key_file,
70  const CService& control_host,
71  CThreadInterrupt* interrupt);
72 
84  Session(const CService& control_host, CThreadInterrupt* interrupt);
85 
92  ~Session();
93 
101 
109 
119  bool Connect(const CService& to, Connection& conn, bool& proxy_error) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
120 
121 private:
125  struct Reply {
129  std::string full;
130 
134  std::string request;
135 
143  std::unordered_map<std::string, std::optional<std::string>> keys;
144 
155  std::string Get(const std::string& key) const;
156  };
157 
163  template <typename... Args>
164  void Log(const std::string& fmt, const Args&... args) const;
165 
174  Reply SendRequestAndGetReply(const Sock& sock,
175  const std::string& request,
176  bool check_result_ok = true) const;
177 
183  std::unique_ptr<Sock> Hello() const EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
184 
189 
196 
204 
211 
218 
226 
231 
235  const fs::path m_private_key_file;
236 
241 
246 
250  mutable Mutex m_mutex;
251 
256  Binary m_private_key GUARDED_BY(m_mutex);
257 
267  std::unique_ptr<Sock> m_control_sock GUARDED_BY(m_mutex);
268 
273  CService m_my_addr GUARDED_BY(m_mutex);
274 
278  std::string m_session_id GUARDED_BY(m_mutex);
279 
284  const bool m_transient;
285 };
286 
287 } // namespace sam
288 } // namespace i2p
289 
290 #endif // BITCOIN_I2P_H
std::string full
Full, unparsed reply.
Definition: i2p.h:129
void CheckControlSock() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check the control socket for errors and possibly disconnect.
Definition: i2p.cpp:344
void DestGenerate(const Sock &sock) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Generate a new destination with the SAM proxy and set m_private_key to it.
Definition: i2p.cpp:355
Binary MyDestination() const EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Derive own destination from m_private_key.
Definition: i2p.cpp:378
Binary m_private_key GUARDED_BY(m_mutex)
The private key of this peer.
void CreateIfNotCreatedAlready() EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Create the session if not already created.
Definition: i2p.cpp:410
std::unique_ptr< Sock > StreamAccept() EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Open a new connection to the SAM proxy and issue "STREAM ACCEPT" request using the existing session i...
Definition: i2p.cpp:463
Mutex m_mutex
Mutex protecting the members that can be concurrently accessed.
Definition: i2p.h:250
Reply SendRequestAndGetReply(const Sock &sock, const std::string &request, bool check_result_ok=true) const
Send request and get a reply from the SAM proxy.
Definition: i2p.cpp:291
CService me
Our I2P address.
Definition: i2p.h:36
I2P SAM session.
Definition: i2p.h:55
void Log(const std::string &fmt, const Args &... args) const
Log a message in the BCLog::I2P category.
Definition: i2p.cpp:286
An established connection with another peer.
Definition: i2p.h:31
A reply from the SAM proxy.
Definition: i2p.h:125
const fs::path m_private_key_file
The name of the file where this peer&#39;s private key is stored (in binary).
Definition: i2p.h:235
Filesystem operations and types.
std::string Get(const std::string &key) const
Get the value of a given key.
Definition: i2p.cpp:275
const bool m_transient
Whether this is a transient session (the I2P private key will not be read or written to disk)...
Definition: i2p.h:284
ArgsManager & args
Definition: bitcoind.cpp:269
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:534
Session(const fs::path &private_key_file, const CService &control_host, CThreadInterrupt *interrupt)
Construct a session.
Definition: i2p.cpp:117
A helper class for interruptible sleeps.
std::unique_ptr< Sock > Hello() const EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Open a new connection to the SAM proxy.
Definition: i2p.cpp:327
std::string request
Request, used for detailed error reporting.
Definition: i2p.h:134
static constexpr size_t MAX_MSG_SIZE
The maximum size of an incoming message from the I2P SAM proxy (in bytes).
Definition: i2p.h:50
void GenerateAndSavePrivateKey(const Sock &sock) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Generate a new destination with the SAM proxy, set m_private_key to it and save it on disk to m_priva...
Definition: i2p.cpp:366
Definition: i2p.cpp:29
bool Connect(const CService &to, Connection &conn, bool &proxy_error) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Connect to an I2P peer.
Definition: i2p.cpp:215
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
const CService m_control_host
The host and port of the SAM control service.
Definition: i2p.h:240
void Disconnect() EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Destroy the session, closing the internally used sockets.
Definition: i2p.cpp:484
std::unordered_map< std::string, std::optional< std::string > > keys
A map of keywords from the parsed reply.
Definition: i2p.h:143
CThreadInterrupt *const m_interrupt
Cease network activity when this is signaled.
Definition: i2p.h:245
~Session()
Destroy the session, closing the internally used sockets.
Definition: i2p.cpp:134
std::vector< uint8_t > Binary
Binary data.
Definition: i2p.h:26
bool Accept(Connection &conn) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Wait for and accept a new incoming connection.
Definition: i2p.cpp:155
RAII helper class that manages a socket and closes it automatically when it goes out of scope...
Definition: sock.h:26
bool Listen(Connection &conn) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Start listening for an incoming connection.
Definition: i2p.cpp:140
std::unique_ptr< Sock > sock
Connected socket.
Definition: i2p.h:33
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
CService peer
The peer&#39;s I2P address.
Definition: i2p.h:39