Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 326 Lines • ▼ Show 20 Lines | for (CNode *pnode : vNodes) { | ||||
} | } | ||||
} | } | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
CNode *CConnman::FindNode(const std::string &addrName) { | CNode *CConnman::FindNode(const std::string &addrName) { | ||||
LOCK(cs_vNodes); | LOCK(cs_vNodes); | ||||
for (CNode *pnode : vNodes) { | for (CNode *pnode : vNodes) { | ||||
if (pnode->GetAddrName() == addrName) { | if (pnode->m_addr_name == addrName) { | ||||
return pnode; | return pnode; | ||||
} | } | ||||
} | } | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
CNode *CConnman::FindNode(const CService &addr) { | CNode *CConnman::FindNode(const CService &addr) { | ||||
LOCK(cs_vNodes); | LOCK(cs_vNodes); | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | if (pszDest) { | ||||
if (!addrConnect.IsValid()) { | if (!addrConnect.IsValid()) { | ||||
LogPrint(BCLog::NET, | LogPrint(BCLog::NET, | ||||
"Resolver returned invalid address %s for %s\n", | "Resolver returned invalid address %s for %s\n", | ||||
addrConnect.ToString(), pszDest); | addrConnect.ToString(), pszDest); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
// It is possible that we already have a connection to the IP/port | // It is possible that we already have a connection to the IP/port | ||||
// pszDest resolved to. In that case, drop the connection that was | // pszDest resolved to. In that case, drop the connection that was | ||||
// just created, and return the existing CNode instead. Also store | // just created. | ||||
// the name we used to connect in that CNode, so that future | |||||
// FindNode() calls to that name catch this early. | |||||
LOCK(cs_vNodes); | LOCK(cs_vNodes); | ||||
CNode *pnode = FindNode(static_cast<CService>(addrConnect)); | CNode *pnode = FindNode(static_cast<CService>(addrConnect)); | ||||
if (pnode) { | if (pnode) { | ||||
pnode->MaybeSetAddrName(std::string(pszDest)); | |||||
LogPrintf("Failed to open new connection, already connected\n"); | LogPrintf("Failed to open new connection, already connected\n"); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// Connect | // Connect | ||||
bool connected = false; | bool connected = false; | ||||
▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | switch (m_conn_type) { | ||||
return "addr-fetch"; | return "addr-fetch"; | ||||
case ConnectionType::AVALANCHE_OUTBOUND: | case ConnectionType::AVALANCHE_OUTBOUND: | ||||
return "avalanche"; | return "avalanche"; | ||||
} // no default case, so the compiler can warn about missing cases | } // no default case, so the compiler can warn about missing cases | ||||
assert(false); | assert(false); | ||||
} | } | ||||
std::string CNode::GetAddrName() const { | |||||
LOCK(cs_addrName); | |||||
return addrName; | |||||
} | |||||
void CNode::MaybeSetAddrName(const std::string &addrNameIn) { | |||||
LOCK(cs_addrName); | |||||
if (addrName.empty()) { | |||||
addrName = addrNameIn; | |||||
} | |||||
} | |||||
CService CNode::GetAddrLocal() const { | CService CNode::GetAddrLocal() const { | ||||
LOCK(cs_addrLocal); | LOCK(cs_addrLocal); | ||||
return addrLocal; | return addrLocal; | ||||
} | } | ||||
void CNode::SetAddrLocal(const CService &addrLocalIn) { | void CNode::SetAddrLocal(const CService &addrLocalIn) { | ||||
LOCK(cs_addrLocal); | LOCK(cs_addrLocal); | ||||
if (addrLocal.IsValid()) { | if (addrLocal.IsValid()) { | ||||
Show All 23 Lines | void CNode::copyStats(CNodeStats &stats) { | ||||
} | } | ||||
stats.m_last_send = m_last_send; | stats.m_last_send = m_last_send; | ||||
stats.m_last_recv = m_last_recv; | stats.m_last_recv = m_last_recv; | ||||
stats.m_last_tx_time = m_last_tx_time; | stats.m_last_tx_time = m_last_tx_time; | ||||
stats.m_last_proof_time = m_last_proof_time; | stats.m_last_proof_time = m_last_proof_time; | ||||
stats.m_last_block_time = m_last_block_time; | stats.m_last_block_time = m_last_block_time; | ||||
stats.m_connected = m_connected; | stats.m_connected = m_connected; | ||||
stats.nTimeOffset = nTimeOffset; | stats.nTimeOffset = nTimeOffset; | ||||
stats.addrName = GetAddrName(); | stats.m_addr_name = m_addr_name; | ||||
stats.nVersion = nVersion; | stats.nVersion = nVersion; | ||||
{ | { | ||||
LOCK(cs_SubVer); | LOCK(cs_SubVer); | ||||
stats.cleanSubVer = cleanSubVer; | stats.cleanSubVer = cleanSubVer; | ||||
} | } | ||||
stats.fInbound = IsInboundConn(); | stats.fInbound = IsInboundConn(); | ||||
stats.m_manual_connection = IsManualConn(); | stats.m_manual_connection = IsManualConn(); | ||||
stats.m_bip152_highbandwidth_to = m_bip152_highbandwidth_to; | stats.m_bip152_highbandwidth_to = m_bip152_highbandwidth_to; | ||||
▲ Show 20 Lines • Show All 1,877 Lines • ▼ Show 20 Lines | std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() { | ||||
std::map<CService, bool> mapConnected; | std::map<CService, bool> mapConnected; | ||||
std::map<std::string, std::pair<bool, CService>> mapConnectedByName; | std::map<std::string, std::pair<bool, CService>> mapConnectedByName; | ||||
{ | { | ||||
LOCK(cs_vNodes); | LOCK(cs_vNodes); | ||||
for (const CNode *pnode : vNodes) { | for (const CNode *pnode : vNodes) { | ||||
if (pnode->addr.IsValid()) { | if (pnode->addr.IsValid()) { | ||||
mapConnected[pnode->addr] = pnode->IsInboundConn(); | mapConnected[pnode->addr] = pnode->IsInboundConn(); | ||||
} | } | ||||
std::string addrName = pnode->GetAddrName(); | std::string addrName{pnode->m_addr_name}; | ||||
if (!addrName.empty()) { | if (!addrName.empty()) { | ||||
mapConnectedByName[std::move(addrName)] = | mapConnectedByName[std::move(addrName)] = | ||||
std::make_pair(pnode->IsInboundConn(), | std::make_pair(pnode->IsInboundConn(), | ||||
static_cast<const CService &>(pnode->addr)); | static_cast<const CService &>(pnode->addr)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 933 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, | CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, | ||||
const CAddress &addrIn, uint64_t nKeyedNetGroupIn, | const CAddress &addrIn, uint64_t nKeyedNetGroupIn, | ||||
uint64_t nLocalHostNonceIn, uint64_t nLocalExtraEntropyIn, | uint64_t nLocalHostNonceIn, uint64_t nLocalExtraEntropyIn, | ||||
const CAddress &addrBindIn, const std::string &addrNameIn, | const CAddress &addrBindIn, const std::string &addrNameIn, | ||||
ConnectionType conn_type_in, bool inbound_onion) | ConnectionType conn_type_in, bool inbound_onion) | ||||
: m_connected(GetTime<std::chrono::seconds>()), addr(addrIn), | : m_connected(GetTime<std::chrono::seconds>()), addr(addrIn), | ||||
addrBind(addrBindIn), m_inbound_onion(inbound_onion), | addrBind(addrBindIn), m_addr_name{addrNameIn.empty() | ||||
nKeyedNetGroup(nKeyedNetGroupIn), | ? addr.ToStringIPPort() | ||||
: addrNameIn}, | |||||
m_inbound_onion(inbound_onion), nKeyedNetGroup(nKeyedNetGroupIn), | |||||
m_tx_relay(conn_type_in != ConnectionType::BLOCK_RELAY | m_tx_relay(conn_type_in != ConnectionType::BLOCK_RELAY | ||||
? std::make_unique<TxRelay>() | ? std::make_unique<TxRelay>() | ||||
: nullptr), | : nullptr), | ||||
m_proof_relay(isAvalancheEnabled(gArgs) ? std::make_unique<ProofRelay>() | m_proof_relay(isAvalancheEnabled(gArgs) ? std::make_unique<ProofRelay>() | ||||
: nullptr), | : nullptr), | ||||
// Don't relay addr messages to peers that we connect to as | // Don't relay addr messages to peers that we connect to as | ||||
// block-relay-only peers (to prevent adversaries from inferring these | // block-relay-only peers (to prevent adversaries from inferring these | ||||
// links from addr traffic). | // links from addr traffic). | ||||
id(idIn), nLocalHostNonce(nLocalHostNonceIn), | id(idIn), nLocalHostNonce(nLocalHostNonceIn), | ||||
nLocalExtraEntropy(nLocalExtraEntropyIn), m_conn_type(conn_type_in), | nLocalExtraEntropy(nLocalExtraEntropyIn), m_conn_type(conn_type_in), | ||||
nLocalServices(nLocalServicesIn) { | nLocalServices(nLocalServicesIn) { | ||||
if (inbound_onion) { | if (inbound_onion) { | ||||
assert(conn_type_in == ConnectionType::INBOUND); | assert(conn_type_in == ConnectionType::INBOUND); | ||||
} | } | ||||
hSocket = hSocketIn; | hSocket = hSocketIn; | ||||
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn; | |||||
for (const std::string &msg : getAllNetMessageTypes()) { | for (const std::string &msg : getAllNetMessageTypes()) { | ||||
mapRecvBytesPerMsgCmd[msg] = 0; | mapRecvBytesPerMsgCmd[msg] = 0; | ||||
} | } | ||||
mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0; | mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0; | ||||
if (fLogIPs) { | if (fLogIPs) { | ||||
LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", addrName, id); | LogPrint(BCLog::NET, "Added connection to %s peer=%d\n", m_addr_name, | ||||
id); | |||||
} else { | } else { | ||||
LogPrint(BCLog::NET, "Added connection peer=%d\n", id); | LogPrint(BCLog::NET, "Added connection peer=%d\n", id); | ||||
} | } | ||||
m_deserializer = std::make_unique<V1TransportDeserializer>( | m_deserializer = std::make_unique<V1TransportDeserializer>( | ||||
V1TransportDeserializer(GetConfig().GetChainParams().NetMagic(), | V1TransportDeserializer(GetConfig().GetChainParams().NetMagic(), | ||||
SER_NETWORK, INIT_PROTO_VERSION)); | SER_NETWORK, INIT_PROTO_VERSION)); | ||||
m_serializer = | m_serializer = | ||||
Show All 11 Lines | |||||
void CConnman::PushMessage(CNode *pnode, CSerializedNetMsg &&msg) { | void CConnman::PushMessage(CNode *pnode, CSerializedNetMsg &&msg) { | ||||
size_t nMessageSize = msg.data.size(); | size_t nMessageSize = msg.data.size(); | ||||
LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", msg.m_type, | LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", msg.m_type, | ||||
nMessageSize, pnode->GetId()); | nMessageSize, pnode->GetId()); | ||||
if (gArgs.GetBoolArg("-capturemessages", false)) { | if (gArgs.GetBoolArg("-capturemessages", false)) { | ||||
CaptureMessage(pnode->addr, msg.m_type, msg.data, /*incoming=*/false); | CaptureMessage(pnode->addr, msg.m_type, msg.data, /*incoming=*/false); | ||||
} | } | ||||
TRACE6(net, outbound_message, pnode->GetId(), pnode->GetAddrName().c_str(), | TRACE6(net, outbound_message, pnode->GetId(), pnode->m_addr_name.c_str(), | ||||
pnode->ConnectionTypeAsString().c_str(), msg.m_type.c_str(), | pnode->ConnectionTypeAsString().c_str(), msg.m_type.c_str(), | ||||
msg.data.size(), msg.data.data()); | msg.data.size(), msg.data.data()); | ||||
// make sure we use the appropriate network transport format | // make sure we use the appropriate network transport format | ||||
std::vector<uint8_t> serializedHeader; | std::vector<uint8_t> serializedHeader; | ||||
pnode->m_serializer->prepareForTransport(*config, msg, serializedHeader); | pnode->m_serializer->prepareForTransport(*config, msg, serializedHeader); | ||||
size_t nTotalSize = nMessageSize + serializedHeader.size(); | size_t nTotalSize = nMessageSize + serializedHeader.size(); | ||||
▲ Show 20 Lines • Show All 149 Lines • Show Last 20 Lines |