Skip to content

Commit a1580a0

Browse files
committed
net: store an optional I2P session in CNode
and destroy it when `CNode::m_sock` is closed. I2P transient sessions are created per connection (i.e. per `CNode`) and should be destroyed when the connection is closed. Storing the session in `CNode` is a convenient way to destroy it together with the connection socket (`CNode::m_sock`). An alternative approach would be to store a list of all I2P sessions in `CConnman` and from `CNode::CloseSocketDisconnect()` to somehow ask the `CConnman` to destroy the relevant session.
1 parent 2b781ad commit a1580a0

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/net.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ void CNode::CloseSocketDisconnect()
564564
LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
565565
m_sock.reset();
566566
}
567+
m_i2p_sam_session.reset();
567568
}
568569

569570
void CConnman::AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr) const {
@@ -2702,20 +2703,27 @@ ServiceFlags CConnman::GetLocalServices() const
27022703

27032704
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
27042705

2705-
CNode::CNode(NodeId idIn, std::shared_ptr<Sock> sock, const CAddress& addrIn,
2706-
uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
2707-
const CAddress& addrBindIn, const std::string& addrNameIn,
2708-
ConnectionType conn_type_in, bool inbound_onion)
2706+
CNode::CNode(NodeId idIn,
2707+
std::shared_ptr<Sock> sock,
2708+
const CAddress& addrIn,
2709+
uint64_t nKeyedNetGroupIn,
2710+
uint64_t nLocalHostNonceIn,
2711+
const CAddress& addrBindIn,
2712+
const std::string& addrNameIn,
2713+
ConnectionType conn_type_in,
2714+
bool inbound_onion,
2715+
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session)
27092716
: m_sock{sock},
27102717
m_connected{GetTime<std::chrono::seconds>()},
2711-
addr(addrIn),
2712-
addrBind(addrBindIn),
2718+
addr{addrIn},
2719+
addrBind{addrBindIn},
27132720
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
2714-
m_inbound_onion(inbound_onion),
2715-
nKeyedNetGroup(nKeyedNetGroupIn),
2716-
id(idIn),
2717-
nLocalHostNonce(nLocalHostNonceIn),
2718-
m_conn_type(conn_type_in)
2721+
m_inbound_onion{inbound_onion},
2722+
nKeyedNetGroup{nKeyedNetGroupIn},
2723+
id{idIn},
2724+
nLocalHostNonce{nLocalHostNonceIn},
2725+
m_conn_type{conn_type_in},
2726+
m_i2p_sam_session{std::move(i2p_sam_session)}
27192727
{
27202728
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
27212729

src/net.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,16 @@ class CNode
513513
* criterium in CConnman::AttemptToEvictConnection. */
514514
std::atomic<std::chrono::microseconds> m_min_ping_time{std::chrono::microseconds::max()};
515515

516-
CNode(NodeId id, std::shared_ptr<Sock> sock, const CAddress& addrIn,
517-
uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
518-
const CAddress& addrBindIn, const std::string& addrNameIn,
519-
ConnectionType conn_type_in, bool inbound_onion);
516+
CNode(NodeId id,
517+
std::shared_ptr<Sock> sock,
518+
const CAddress& addrIn,
519+
uint64_t nKeyedNetGroupIn,
520+
uint64_t nLocalHostNonceIn,
521+
const CAddress& addrBindIn,
522+
const std::string& addrNameIn,
523+
ConnectionType conn_type_in,
524+
bool inbound_onion,
525+
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session = nullptr);
520526
CNode(const CNode&) = delete;
521527
CNode& operator=(const CNode&) = delete;
522528

@@ -596,6 +602,18 @@ class CNode
596602

597603
mapMsgTypeSize mapSendBytesPerMsgType GUARDED_BY(cs_vSend);
598604
mapMsgTypeSize mapRecvBytesPerMsgType GUARDED_BY(cs_vRecv);
605+
606+
/**
607+
* If an I2P session is created per connection (for outbound transient I2P
608+
* connections) then it is stored here so that it can be destroyed when the
609+
* socket is closed. I2P sessions involve a data/transport socket (in `m_sock`)
610+
* and a control socket (in `m_i2p_sam_session`). For transient sessions, once
611+
* the data socket is closed, the control socket is not going to be used anymore
612+
* and is just taking up resources. So better close it as soon as `m_sock` is
613+
* closed.
614+
* Otherwise this unique_ptr is empty.
615+
*/
616+
std::unique_ptr<i2p::sam::Session> m_i2p_sam_session GUARDED_BY(m_sock_mutex);
599617
};
600618

601619
/**

0 commit comments

Comments
 (0)