Bitcoin Core  21.99.0
P2P Digital Currency
i2p.h
Go to the documentation of this file.
1 // Copyright (c) 2020-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 
5 #ifndef BITCOIN_I2P_H
6 #define BITCOIN_I2P_H
7 
8 #include <compat.h>
9 #include <fs.h>
10 #include <netaddress.h>
11 #include <sync.h>
12 #include <threadinterrupt.h>
13 #include <util/sock.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 
79  ~Session();
80 
87  bool Listen(Connection& conn);
88 
95  bool Accept(Connection& conn);
96 
106  bool Connect(const CService& to, Connection& conn, bool& proxy_error);
107 
108 private:
112  struct Reply {
116  std::string full;
117 
121  std::string request;
122 
130  std::unordered_map<std::string, std::optional<std::string>> keys;
131 
142  std::string Get(const std::string& key) const;
143  };
144 
150  template <typename... Args>
151  void Log(const std::string& fmt, const Args&... args) const;
152 
161  Reply SendRequestAndGetReply(const Sock& sock,
162  const std::string& request,
163  bool check_result_ok = true) const;
164 
170  std::unique_ptr<Sock> Hello() const EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
171 
175  void CheckControlSock();
176 
183 
191 
198 
205 
213 
218 
222  const fs::path m_private_key_file;
223 
228 
233 
237  mutable Mutex m_mutex;
238 
243  Binary m_private_key GUARDED_BY(m_mutex);
244 
253  std::unique_ptr<Sock> m_control_sock GUARDED_BY(m_mutex);
254 
259  CService m_my_addr GUARDED_BY(m_mutex);
260 
264  std::string m_session_id GUARDED_BY(m_mutex);
265 };
266 
267 } // namespace sam
268 } // namespace i2p
269 
270 #endif /* BITCOIN_I2P_H */
i2p::sam::Session::Reply::keys
std::unordered_map< std::string, std::optional< std::string > > keys
A map of keywords from the parsed reply.
Definition: i2p.h:130
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:551
threadinterrupt.h
i2p::sam::Session::m_private_key_file
const fs::path m_private_key_file
The name of the file where this peer's private key is stored (in binary).
Definition: i2p.h:222
i2p::sam::Session::Disconnect
void Disconnect() EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Destroy the session, closing the internally used sockets.
Definition: i2p.cpp:396
fs.h
i2p::sam::Session::Session
Session(const fs::path &private_key_file, const CService &control_host, CThreadInterrupt *interrupt)
Construct a session.
Definition: i2p.cpp:116
sync.h
i2p::sam::Session::Reply
A reply from the SAM proxy.
Definition: i2p.h:112
i2p::Connection::peer
CService peer
The peer's I2P address.
Definition: i2p.h:39
i2p::sam::Session::Hello
std::unique_ptr< Sock > Hello() const EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Open a new connection to the SAM proxy.
Definition: i2p.cpp:276
i2p::sam::Session::GenerateAndSavePrivateKey
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:314
AnnotatedMixin< std::mutex >
i2p
Definition: i2p.cpp:27
i2p::sam::Session::Reply::full
std::string full
Full, unparsed reply.
Definition: i2p.h:116
i2p::sam::Session::m_control_host
const CService m_control_host
The host and port of the SAM control service.
Definition: i2p.h:227
i2p::sam::Session::Listen
bool Listen(Connection &conn)
Start listening for an incoming connection.
Definition: i2p.cpp:130
Sock
RAII helper class that manages a socket.
Definition: sock.h:25
i2p::sam::Session::SendRequestAndGetReply
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:240
i2p::Binary
std::vector< uint8_t > Binary
Binary data.
Definition: i2p.h:26
i2p::sam::Session
I2P SAM session.
Definition: i2p.h:55
i2p::sam::Session::CreateIfNotCreatedAlready
void CreateIfNotCreatedAlready() EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Create the session if not already created.
Definition: i2p.cpp:343
i2p::sam::Session::m_mutex
Mutex m_mutex
Mutex protecting the members that can be concurrently accessed.
Definition: i2p.h:237
i2p::sam::Session::m_interrupt
CThreadInterrupt *const m_interrupt
Cease network activity when this is signaled.
Definition: i2p.h:232
compat.h
i2p::sam::Session::MyDestination
Binary MyDestination() const EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Derive own destination from m_private_key.
Definition: i2p.cpp:326
netaddress.h
i2p::sam::Session::Reply::request
std::string request
Request, used for detailed error reporting.
Definition: i2p.h:121
i2p::sam::Session::Reply::Get
std::string Get(const std::string &key) const
Get the value of a given key.
Definition: i2p.cpp:224
i2p::sam::Session::Connect
bool Connect(const CService &to, Connection &conn, bool &proxy_error)
Connect to an I2P peer.
Definition: i2p.cpp:171
i2p::sam::Session::StreamAccept
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:375
i2p::sam::Session::Log
void Log(const std::string &fmt, const Args &... args) const
Log a message in the BCLog::I2P category.
Definition: i2p.cpp:235
i2p::sam::Session::DestGenerate
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:304
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
i2p::sam::MAX_MSG_SIZE
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
i2p::Connection
An established connection with another peer.
Definition: i2p.h:31
i2p::Connection::me
CService me
Our I2P address.
Definition: i2p.h:36
i2p::sam::Session::CheckControlSock
void CheckControlSock()
Check the control socket for errors and possibly disconnect.
Definition: i2p.cpp:293
i2p::sam::Session::GUARDED_BY
Binary m_private_key GUARDED_BY(m_mutex)
The private key of this peer.
CThreadInterrupt
Definition: threadinterrupt.h:19
sock.h
i2p::sam::Session::Accept
bool Accept(Connection &conn)
Wait for and accept a new incoming connection.
Definition: i2p.cpp:145
i2p::Connection::sock
std::unique_ptr< Sock > sock
Connected socket.
Definition: i2p.h:33
i2p::sam::Session::~Session
~Session()
Destroy the session, closing the internally used sockets.
Definition: i2p.cpp:124