Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 698 Lines • ▼ Show 20 Lines | |||||
#undef X | #undef X | ||||
static bool IsOversizedMessage(const Config &config, const CNetMessage &msg) { | static bool IsOversizedMessage(const Config &config, const CNetMessage &msg) { | ||||
if (!msg.in_data) { | if (!msg.in_data) { | ||||
// Header only, cannot be oversized. | // Header only, cannot be oversized. | ||||
return false; | return false; | ||||
} | } | ||||
// If the message doesn't not contain a block content, check against | return msg.hdr.IsOversized(config); | ||||
// MAX_PROTOCOL_MESSAGE_LENGTH. | |||||
if (msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH && | |||||
!NetMsgType::IsBlockLike(msg.hdr.GetCommand())) { | |||||
return true; | |||||
} | |||||
// Scale the maximum accepted size with the block size. | |||||
if (msg.hdr.nMessageSize > 2 * config.GetMaxBlockSize()) { | |||||
return true; | |||||
} | |||||
return false; | |||||
} | } | ||||
bool CNode::ReceiveMsgBytes(const Config &config, const char *pch, | bool CNode::ReceiveMsgBytes(const Config &config, const char *pch, | ||||
unsigned int nBytes, bool &complete) { | unsigned int nBytes, bool &complete) { | ||||
complete = false; | complete = false; | ||||
int64_t nTimeMicros = GetTimeMicros(); | int64_t nTimeMicros = GetTimeMicros(); | ||||
LOCK(cs_vRecv); | LOCK(cs_vRecv); | ||||
nLastRecv = nTimeMicros / 1000000; | nLastRecv = nTimeMicros / 1000000; | ||||
nRecvBytes += nBytes; | nRecvBytes += nBytes; | ||||
while (nBytes > 0) { | while (nBytes > 0) { | ||||
// Get current incomplete message, or create a new one. | // Get current incomplete message, or create a new one. | ||||
if (vRecvMsg.empty() || vRecvMsg.back().complete()) { | if (vRecvMsg.empty() || vRecvMsg.back().complete()) { | ||||
vRecvMsg.push_back(CNetMessage(Params().NetMagic(), SER_NETWORK, | vRecvMsg.push_back(CNetMessage(Params().NetMagic(), SER_NETWORK, | ||||
INIT_PROTO_VERSION)); | INIT_PROTO_VERSION)); | ||||
} | } | ||||
CNetMessage &msg = vRecvMsg.back(); | CNetMessage &msg = vRecvMsg.back(); | ||||
// Absorb network data. | // Absorb network data. | ||||
int handled; | int handled; | ||||
if (!msg.in_data) { | if (!msg.in_data) { | ||||
handled = msg.readHeader(pch, nBytes); | handled = msg.readHeader(config, pch, nBytes); | ||||
} else { | } else { | ||||
handled = msg.readData(pch, nBytes); | handled = msg.readData(pch, nBytes); | ||||
} | } | ||||
if (handled < 0) { | if (handled < 0) { | ||||
return false; | return false; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | int CNode::GetSendVersion() const { | ||||
if (nSendVersion == 0) { | if (nSendVersion == 0) { | ||||
error("Requesting unset send version for node: %i. Using %i", id, | error("Requesting unset send version for node: %i. Using %i", id, | ||||
INIT_PROTO_VERSION); | INIT_PROTO_VERSION); | ||||
return INIT_PROTO_VERSION; | return INIT_PROTO_VERSION; | ||||
} | } | ||||
return nSendVersion; | return nSendVersion; | ||||
} | } | ||||
int CNetMessage::readHeader(const char *pch, unsigned int nBytes) { | int CNetMessage::readHeader(const Config &config, const char *pch, | ||||
unsigned int nBytes) { | |||||
// copy data to temporary parsing buffer | // copy data to temporary parsing buffer | ||||
unsigned int nRemaining = 24 - nHdrPos; | unsigned int nRemaining = 24 - nHdrPos; | ||||
unsigned int nCopy = std::min(nRemaining, nBytes); | unsigned int nCopy = std::min(nRemaining, nBytes); | ||||
memcpy(&hdrbuf[nHdrPos], pch, nCopy); | memcpy(&hdrbuf[nHdrPos], pch, nCopy); | ||||
nHdrPos += nCopy; | nHdrPos += nCopy; | ||||
// if header incomplete, exit | // if header incomplete, exit | ||||
if (nHdrPos < 24) { | if (nHdrPos < 24) { | ||||
return nCopy; | return nCopy; | ||||
} | } | ||||
// deserialize to CMessageHeader | // deserialize to CMessageHeader | ||||
try { | try { | ||||
hdrbuf >> hdr; | hdrbuf >> hdr; | ||||
} catch (const std::exception &) { | } catch (const std::exception &) { | ||||
return -1; | return -1; | ||||
} | } | ||||
// reject messages larger than MAX_SIZE | // Reject oversized messages | ||||
if (hdr.nMessageSize > MAX_SIZE) { | if (hdr.IsOversized(config)) { | ||||
LogPrint(BCLog::NET, "Oversized header detected\n"); | |||||
return -1; | return -1; | ||||
} | } | ||||
// switch state to reading message data | // switch state to reading message data | ||||
in_data = true; | in_data = true; | ||||
return nCopy; | return nCopy; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 2,245 Lines • Show Last 20 Lines |