Skip to content

Commit a88bf9d

Browse files
committed
i2p: construct Session with Proxy instead of CService
1 parent d9318a3 commit a88bf9d

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/i2p.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static CNetAddr DestB64ToAddr(const std::string& dest)
115115
namespace sam {
116116

117117
Session::Session(const fs::path& private_key_file,
118-
const CService& control_host,
118+
const Proxy& control_host,
119119
CThreadInterrupt* interrupt)
120120
: m_private_key_file{private_key_file},
121121
m_control_host{control_host},
@@ -124,7 +124,7 @@ Session::Session(const fs::path& private_key_file,
124124
{
125125
}
126126

127-
Session::Session(const CService& control_host, CThreadInterrupt* interrupt)
127+
Session::Session(const Proxy& control_host, CThreadInterrupt* interrupt)
128128
: m_control_host{control_host},
129129
m_interrupt{interrupt},
130130
m_transient{true}
@@ -326,10 +326,10 @@ Session::Reply Session::SendRequestAndGetReply(const Sock& sock,
326326

327327
std::unique_ptr<Sock> Session::Hello() const
328328
{
329-
auto sock = ConnectDirectly(m_control_host, true);
329+
auto sock = m_control_host.Connect();
330330

331331
if (!sock) {
332-
throw std::runtime_error(strprintf("Cannot connect to %s", m_control_host.ToStringAddrPort()));
332+
throw std::runtime_error(strprintf("Cannot connect to %s", m_control_host.ToString()));
333333
}
334334

335335
SendRequestAndGetReply(*sock, "HELLO VERSION MIN=3.1 MAX=3.1");
@@ -413,7 +413,7 @@ void Session::CreateIfNotCreatedAlready()
413413
const auto session_type = m_transient ? "transient" : "persistent";
414414
const auto session_id = GetRandHash().GetHex().substr(0, 10); // full is overkill, too verbose in the logs
415415

416-
Log("Creating %s SAM session %s with %s", session_type, session_id, m_control_host.ToStringAddrPort());
416+
Log("Creating %s SAM session %s with %s", session_type, session_id, m_control_host.ToString());
417417

418418
auto sock = Hello();
419419

src/i2p.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <compat/compat.h>
99
#include <netaddress.h>
10+
#include <netbase.h>
1011
#include <sync.h>
1112
#include <util/fs.h>
1213
#include <util/sock.h>
@@ -67,7 +68,7 @@ class Session
6768
* `Session` object.
6869
*/
6970
Session(const fs::path& private_key_file,
70-
const CService& control_host,
71+
const Proxy& control_host,
7172
CThreadInterrupt* interrupt);
7273

7374
/**
@@ -81,7 +82,7 @@ class Session
8182
* `CThreadInterrupt` object is saved, so it must not be destroyed earlier than this
8283
* `Session` object.
8384
*/
84-
Session(const CService& control_host, CThreadInterrupt* interrupt);
85+
Session(const Proxy& control_host, CThreadInterrupt* interrupt);
8586

8687
/**
8788
* Destroy the session, closing the internally used sockets. The sockets that have been
@@ -235,9 +236,9 @@ class Session
235236
const fs::path m_private_key_file;
236237

237238
/**
238-
* The host and port of the SAM control service.
239+
* The SAM control service proxy.
239240
*/
240-
const CService m_control_host;
241+
const Proxy m_control_host;
241242

242243
/**
243244
* Cease network activity when this is signaled.

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
463463
LOCK(m_unused_i2p_sessions_mutex);
464464
if (m_unused_i2p_sessions.empty()) {
465465
i2p_transient_session =
466-
std::make_unique<i2p::sam::Session>(proxy.proxy, &interruptNet);
466+
std::make_unique<i2p::sam::Session>(proxy, &interruptNet);
467467
} else {
468468
i2p_transient_session.swap(m_unused_i2p_sessions.front());
469469
m_unused_i2p_sessions.pop();
@@ -3186,7 +3186,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
31863186
Proxy i2p_sam;
31873187
if (GetProxy(NET_I2P, i2p_sam) && connOptions.m_i2p_accept_incoming) {
31883188
m_i2p_sam_session = std::make_unique<i2p::sam::Session>(gArgs.GetDataDirNet() / "i2p_private_key",
3189-
i2p_sam.proxy, &interruptNet);
3189+
i2p_sam, &interruptNet);
31903190
}
31913191

31923192
for (const auto& strDest : connOptions.vSeedNodes) {

src/test/i2p_tests.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <i2p.h>
77
#include <logging.h>
88
#include <netaddress.h>
9+
#include <netbase.h>
910
#include <test/util/logging.h>
1011
#include <test/util/net.h>
1112
#include <test/util/setup_common.h>
@@ -51,7 +52,9 @@ BOOST_AUTO_TEST_CASE(unlimited_recv)
5152
};
5253

5354
CThreadInterrupt interrupt;
54-
i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key", CService{}, &interrupt);
55+
const std::optional<CService> addr{Lookup("127.0.0.1", 9000, false)};
56+
const Proxy sam_proxy(addr.value(), false);
57+
i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key", sam_proxy, &interrupt);
5558

5659
{
5760
ASSERT_DEBUG_LOG("Creating persistent SAM session");
@@ -111,8 +114,10 @@ BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)
111114
};
112115

113116
CThreadInterrupt interrupt;
117+
const CService addr{in6_addr(IN6ADDR_LOOPBACK_INIT), /*port=*/7656};
118+
const Proxy sam_proxy(addr, false);
114119
i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key",
115-
CService{in6_addr(IN6ADDR_LOOPBACK_INIT), /*port=*/7656},
120+
sam_proxy,
116121
&interrupt);
117122

118123
i2p::Connection conn;
@@ -154,7 +159,9 @@ BOOST_AUTO_TEST_CASE(damaged_private_key)
154159
BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents));
155160

156161
CThreadInterrupt interrupt;
157-
i2p::sam::Session session(i2p_private_key_file, CService{}, &interrupt);
162+
const CService addr{in6_addr(IN6ADDR_LOOPBACK_INIT), /*port=*/7656};
163+
const Proxy sam_proxy{addr, false};
164+
i2p::sam::Session session(i2p_private_key_file, sam_proxy, &interrupt);
158165

159166
{
160167
ASSERT_DEBUG_LOG("Creating persistent SAM session");

0 commit comments

Comments
 (0)