mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
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.
This commit is contained in:
30
src/net.cpp
30
src/net.cpp
@@ -564,6 +564,7 @@ void CNode::CloseSocketDisconnect()
|
|||||||
LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
|
LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
|
||||||
m_sock.reset();
|
m_sock.reset();
|
||||||
}
|
}
|
||||||
|
m_i2p_sam_session.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CConnman::AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr) const {
|
void CConnman::AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr) const {
|
||||||
@@ -2702,20 +2703,27 @@ ServiceFlags CConnman::GetLocalServices() const
|
|||||||
|
|
||||||
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
|
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
|
||||||
|
|
||||||
CNode::CNode(NodeId idIn, std::shared_ptr<Sock> sock, const CAddress& addrIn,
|
CNode::CNode(NodeId idIn,
|
||||||
uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
|
std::shared_ptr<Sock> sock,
|
||||||
const CAddress& addrBindIn, const std::string& addrNameIn,
|
const CAddress& addrIn,
|
||||||
ConnectionType conn_type_in, bool inbound_onion)
|
uint64_t nKeyedNetGroupIn,
|
||||||
|
uint64_t nLocalHostNonceIn,
|
||||||
|
const CAddress& addrBindIn,
|
||||||
|
const std::string& addrNameIn,
|
||||||
|
ConnectionType conn_type_in,
|
||||||
|
bool inbound_onion,
|
||||||
|
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session)
|
||||||
: m_sock{sock},
|
: m_sock{sock},
|
||||||
m_connected{GetTime<std::chrono::seconds>()},
|
m_connected{GetTime<std::chrono::seconds>()},
|
||||||
addr(addrIn),
|
addr{addrIn},
|
||||||
addrBind(addrBindIn),
|
addrBind{addrBindIn},
|
||||||
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
|
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
|
||||||
m_inbound_onion(inbound_onion),
|
m_inbound_onion{inbound_onion},
|
||||||
nKeyedNetGroup(nKeyedNetGroupIn),
|
nKeyedNetGroup{nKeyedNetGroupIn},
|
||||||
id(idIn),
|
id{idIn},
|
||||||
nLocalHostNonce(nLocalHostNonceIn),
|
nLocalHostNonce{nLocalHostNonceIn},
|
||||||
m_conn_type(conn_type_in)
|
m_conn_type{conn_type_in},
|
||||||
|
m_i2p_sam_session{std::move(i2p_sam_session)}
|
||||||
{
|
{
|
||||||
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
|
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
|
||||||
|
|
||||||
|
|||||||
26
src/net.h
26
src/net.h
@@ -513,10 +513,16 @@ public:
|
|||||||
* criterium in CConnman::AttemptToEvictConnection. */
|
* criterium in CConnman::AttemptToEvictConnection. */
|
||||||
std::atomic<std::chrono::microseconds> m_min_ping_time{std::chrono::microseconds::max()};
|
std::atomic<std::chrono::microseconds> m_min_ping_time{std::chrono::microseconds::max()};
|
||||||
|
|
||||||
CNode(NodeId id, std::shared_ptr<Sock> sock, const CAddress& addrIn,
|
CNode(NodeId id,
|
||||||
uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
|
std::shared_ptr<Sock> sock,
|
||||||
const CAddress& addrBindIn, const std::string& addrNameIn,
|
const CAddress& addrIn,
|
||||||
ConnectionType conn_type_in, bool inbound_onion);
|
uint64_t nKeyedNetGroupIn,
|
||||||
|
uint64_t nLocalHostNonceIn,
|
||||||
|
const CAddress& addrBindIn,
|
||||||
|
const std::string& addrNameIn,
|
||||||
|
ConnectionType conn_type_in,
|
||||||
|
bool inbound_onion,
|
||||||
|
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session = nullptr);
|
||||||
CNode(const CNode&) = delete;
|
CNode(const CNode&) = delete;
|
||||||
CNode& operator=(const CNode&) = delete;
|
CNode& operator=(const CNode&) = delete;
|
||||||
|
|
||||||
@@ -596,6 +602,18 @@ private:
|
|||||||
|
|
||||||
mapMsgTypeSize mapSendBytesPerMsgType GUARDED_BY(cs_vSend);
|
mapMsgTypeSize mapSendBytesPerMsgType GUARDED_BY(cs_vSend);
|
||||||
mapMsgTypeSize mapRecvBytesPerMsgType GUARDED_BY(cs_vRecv);
|
mapMsgTypeSize mapRecvBytesPerMsgType GUARDED_BY(cs_vRecv);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If an I2P session is created per connection (for outbound transient I2P
|
||||||
|
* connections) then it is stored here so that it can be destroyed when the
|
||||||
|
* socket is closed. I2P sessions involve a data/transport socket (in `m_sock`)
|
||||||
|
* and a control socket (in `m_i2p_sam_session`). For transient sessions, once
|
||||||
|
* the data socket is closed, the control socket is not going to be used anymore
|
||||||
|
* and is just taking up resources. So better close it as soon as `m_sock` is
|
||||||
|
* closed.
|
||||||
|
* Otherwise this unique_ptr is empty.
|
||||||
|
*/
|
||||||
|
std::unique_ptr<i2p::sam::Session> m_i2p_sam_session GUARDED_BY(m_sock_mutex);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user