Skip to content

Commit b905363

Browse files
committed
net: accept incoming I2P connections from CConnman
1 parent 0635233 commit b905363

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/net.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <compat.h>
1515
#include <consensus/consensus.h>
1616
#include <crypto/sha256.h>
17+
#include <i2p.h>
1718
#include <net_permissions.h>
1819
#include <netbase.h>
1920
#include <node/ui_interface.h>
@@ -2187,6 +2188,45 @@ void CConnman::ThreadMessageHandler()
21872188
}
21882189
}
21892190

2191+
void CConnman::ThreadI2PAcceptIncoming()
2192+
{
2193+
static constexpr auto err_wait_begin = 1s;
2194+
static constexpr auto err_wait_cap = 5min;
2195+
auto err_wait = err_wait_begin;
2196+
2197+
bool advertising_listen_addr = false;
2198+
i2p::Connection conn;
2199+
2200+
while (!interruptNet) {
2201+
2202+
if (!m_i2p_sam_session->Listen(conn)) {
2203+
if (advertising_listen_addr && conn.me.IsValid()) {
2204+
RemoveLocal(conn.me);
2205+
advertising_listen_addr = false;
2206+
}
2207+
2208+
interruptNet.sleep_for(err_wait);
2209+
if (err_wait < err_wait_cap) {
2210+
err_wait *= 2;
2211+
}
2212+
2213+
continue;
2214+
}
2215+
2216+
if (!advertising_listen_addr) {
2217+
AddLocal(conn.me, LOCAL_BIND);
2218+
advertising_listen_addr = true;
2219+
}
2220+
2221+
if (!m_i2p_sam_session->Accept(conn)) {
2222+
continue;
2223+
}
2224+
2225+
CreateNodeFromAcceptedSocket(conn.sock.Release(), NetPermissionFlags::PF_NONE,
2226+
CAddress{conn.me, NODE_NONE}, CAddress{conn.peer, NODE_NONE});
2227+
}
2228+
}
2229+
21902230
bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError, NetPermissionFlags permissions)
21912231
{
21922232
int nOne = 1;
@@ -2472,6 +2512,12 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
24722512
// Process messages
24732513
threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
24742514

2515+
if (connOptions.m_i2p_accept_incoming && m_i2p_sam_session.get() != nullptr) {
2516+
threadI2PAcceptIncoming =
2517+
std::thread(&TraceThread<std::function<void()>>, "i2paccept",
2518+
std::function<void()>(std::bind(&CConnman::ThreadI2PAcceptIncoming, this)));
2519+
}
2520+
24752521
// Dump network addresses
24762522
scheduler.scheduleEvery([this] { DumpAddresses(); }, DUMP_PEERS_INTERVAL);
24772523

@@ -2519,6 +2565,9 @@ void CConnman::Interrupt()
25192565

25202566
void CConnman::StopThreads()
25212567
{
2568+
if (threadI2PAcceptIncoming.joinable()) {
2569+
threadI2PAcceptIncoming.join();
2570+
}
25222571
if (threadMessageHandler.joinable())
25232572
threadMessageHandler.join();
25242573
if (threadOpenConnections.joinable())

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ class CConnman
10501050
void ProcessAddrFetch();
10511051
void ThreadOpenConnections(std::vector<std::string> connect);
10521052
void ThreadMessageHandler();
1053+
void ThreadI2PAcceptIncoming();
10531054
void AcceptConnection(const ListenSocket& hListenSocket);
10541055

10551056
/**
@@ -1242,6 +1243,7 @@ class CConnman
12421243
std::thread threadOpenAddedConnections;
12431244
std::thread threadOpenConnections;
12441245
std::thread threadMessageHandler;
1246+
std::thread threadI2PAcceptIncoming;
12451247

12461248
/** flag for deciding to connect to an extra outbound peer,
12471249
* in excess of m_max_outbound_full_relay

0 commit comments

Comments
 (0)