Skip to content

Commit 9c94fb6

Browse files
committed
Merge #9930: Trivial: Correct indentation and bracing
31a14d4 Correct indentation and remove unnecessary braces (Matthias Grundmann) Tree-SHA512: c0e827ec4474133c7674254dfd13f59608820cd639debc7759bddae71d73451645fcfe14384f343d08f74d69ac3922bafc12a514f3b790ae2bf9271aa67d5f36
2 parents e0a7e19 + 31a14d4 commit 9c94fb6

File tree

3 files changed

+129
-133
lines changed

3 files changed

+129
-133
lines changed

src/net.cpp

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
708708
handled = msg.readData(pch, nBytes);
709709

710710
if (handled < 0)
711-
return false;
711+
return false;
712712

713713
if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
714714
LogPrint(BCLog::NET, "Oversized message from peer=%i, disconnecting\n", GetId());
@@ -786,7 +786,7 @@ int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
786786

787787
// reject messages larger than MAX_SIZE
788788
if (hdr.nMessageSize > MAX_SIZE)
789-
return -1;
789+
return -1;
790790

791791
// switch state to reading message data
792792
in_data = true;
@@ -1299,59 +1299,55 @@ void CConnman::ThreadSocketHandler()
12991299
}
13001300
if (recvSet || errorSet)
13011301
{
1302+
// typical socket buffer is 8K-64K
1303+
char pchBuf[0x10000];
1304+
int nBytes = 0;
13021305
{
1303-
{
1304-
// typical socket buffer is 8K-64K
1305-
char pchBuf[0x10000];
1306-
int nBytes = 0;
1307-
{
1308-
LOCK(pnode->cs_hSocket);
1309-
if (pnode->hSocket == INVALID_SOCKET)
1310-
continue;
1311-
nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1312-
}
1313-
if (nBytes > 0)
1314-
{
1315-
bool notify = false;
1316-
if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1317-
pnode->CloseSocketDisconnect();
1318-
RecordBytesRecv(nBytes);
1319-
if (notify) {
1320-
size_t nSizeAdded = 0;
1321-
auto it(pnode->vRecvMsg.begin());
1322-
for (; it != pnode->vRecvMsg.end(); ++it) {
1323-
if (!it->complete())
1324-
break;
1325-
nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
1326-
}
1327-
{
1328-
LOCK(pnode->cs_vProcessMsg);
1329-
pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1330-
pnode->nProcessQueueSize += nSizeAdded;
1331-
pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
1332-
}
1333-
WakeMessageHandler();
1334-
}
1335-
}
1336-
else if (nBytes == 0)
1337-
{
1338-
// socket closed gracefully
1339-
if (!pnode->fDisconnect) {
1340-
LogPrint(BCLog::NET, "socket closed\n");
1341-
}
1342-
pnode->CloseSocketDisconnect();
1306+
LOCK(pnode->cs_hSocket);
1307+
if (pnode->hSocket == INVALID_SOCKET)
1308+
continue;
1309+
nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1310+
}
1311+
if (nBytes > 0)
1312+
{
1313+
bool notify = false;
1314+
if (!pnode->ReceiveMsgBytes(pchBuf, nBytes, notify))
1315+
pnode->CloseSocketDisconnect();
1316+
RecordBytesRecv(nBytes);
1317+
if (notify) {
1318+
size_t nSizeAdded = 0;
1319+
auto it(pnode->vRecvMsg.begin());
1320+
for (; it != pnode->vRecvMsg.end(); ++it) {
1321+
if (!it->complete())
1322+
break;
1323+
nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
13431324
}
1344-
else if (nBytes < 0)
13451325
{
1346-
// error
1347-
int nErr = WSAGetLastError();
1348-
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1349-
{
1350-
if (!pnode->fDisconnect)
1351-
LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1352-
pnode->CloseSocketDisconnect();
1353-
}
1326+
LOCK(pnode->cs_vProcessMsg);
1327+
pnode->vProcessMsg.splice(pnode->vProcessMsg.end(), pnode->vRecvMsg, pnode->vRecvMsg.begin(), it);
1328+
pnode->nProcessQueueSize += nSizeAdded;
1329+
pnode->fPauseRecv = pnode->nProcessQueueSize > nReceiveFloodSize;
13541330
}
1331+
WakeMessageHandler();
1332+
}
1333+
}
1334+
else if (nBytes == 0)
1335+
{
1336+
// socket closed gracefully
1337+
if (!pnode->fDisconnect) {
1338+
LogPrint(BCLog::NET, "socket closed\n");
1339+
}
1340+
pnode->CloseSocketDisconnect();
1341+
}
1342+
else if (nBytes < 0)
1343+
{
1344+
// error
1345+
int nErr = WSAGetLastError();
1346+
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1347+
{
1348+
if (!pnode->fDisconnect)
1349+
LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
1350+
pnode->CloseSocketDisconnect();
13551351
}
13561352
}
13571353
}

src/net.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,15 @@ class CNode
699699
public:
700700

701701
NodeId GetId() const {
702-
return id;
702+
return id;
703703
}
704704

705705
uint64_t GetLocalNonce() const {
706-
return nLocalHostNonce;
706+
return nLocalHostNonce;
707707
}
708708

709709
int GetMyStartingHeight() const {
710-
return nMyStartingHeight;
710+
return nMyStartingHeight;
711711
}
712712

713713
int GetRefCount()

src/net_processing.cpp

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,100 +2681,100 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman, const std::atomic<bool>& i
26812681
// this maintains the order of responses
26822682
if (!pfrom->vRecvGetData.empty()) return true;
26832683

2684-
// Don't bother if send buffer is too full to respond anyway
2685-
if (pfrom->fPauseSend)
2686-
return false;
2684+
// Don't bother if send buffer is too full to respond anyway
2685+
if (pfrom->fPauseSend)
2686+
return false;
26872687

2688-
std::list<CNetMessage> msgs;
2689-
{
2690-
LOCK(pfrom->cs_vProcessMsg);
2691-
if (pfrom->vProcessMsg.empty())
2692-
return false;
2693-
// Just take one message
2694-
msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2695-
pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2696-
pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman.GetReceiveFloodSize();
2697-
fMoreWork = !pfrom->vProcessMsg.empty();
2698-
}
2699-
CNetMessage& msg(msgs.front());
2700-
2701-
msg.SetVersion(pfrom->GetRecvVersion());
2702-
// Scan for message start
2703-
if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
2704-
LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
2705-
pfrom->fDisconnect = true;
2688+
std::list<CNetMessage> msgs;
2689+
{
2690+
LOCK(pfrom->cs_vProcessMsg);
2691+
if (pfrom->vProcessMsg.empty())
27062692
return false;
2707-
}
2693+
// Just take one message
2694+
msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2695+
pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2696+
pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman.GetReceiveFloodSize();
2697+
fMoreWork = !pfrom->vProcessMsg.empty();
2698+
}
2699+
CNetMessage& msg(msgs.front());
2700+
2701+
msg.SetVersion(pfrom->GetRecvVersion());
2702+
// Scan for message start
2703+
if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
2704+
LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
2705+
pfrom->fDisconnect = true;
2706+
return false;
2707+
}
27082708

2709-
// Read header
2710-
CMessageHeader& hdr = msg.hdr;
2711-
if (!hdr.IsValid(chainparams.MessageStart()))
2712-
{
2713-
LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
2714-
return fMoreWork;
2715-
}
2716-
std::string strCommand = hdr.GetCommand();
2709+
// Read header
2710+
CMessageHeader& hdr = msg.hdr;
2711+
if (!hdr.IsValid(chainparams.MessageStart()))
2712+
{
2713+
LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
2714+
return fMoreWork;
2715+
}
2716+
std::string strCommand = hdr.GetCommand();
2717+
2718+
// Message size
2719+
unsigned int nMessageSize = hdr.nMessageSize;
27172720

2718-
// Message size
2719-
unsigned int nMessageSize = hdr.nMessageSize;
2721+
// Checksum
2722+
CDataStream& vRecv = msg.vRecv;
2723+
const uint256& hash = msg.GetMessageHash();
2724+
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2725+
{
2726+
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2727+
SanitizeString(strCommand), nMessageSize,
2728+
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
2729+
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
2730+
return fMoreWork;
2731+
}
27202732

2721-
// Checksum
2722-
CDataStream& vRecv = msg.vRecv;
2723-
const uint256& hash = msg.GetMessageHash();
2724-
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2733+
// Process message
2734+
bool fRet = false;
2735+
try
2736+
{
2737+
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
2738+
if (interruptMsgProc)
2739+
return false;
2740+
if (!pfrom->vRecvGetData.empty())
2741+
fMoreWork = true;
2742+
}
2743+
catch (const std::ios_base::failure& e)
2744+
{
2745+
connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
2746+
if (strstr(e.what(), "end of data"))
27252747
{
2726-
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2727-
SanitizeString(strCommand), nMessageSize,
2728-
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
2729-
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
2730-
return fMoreWork;
2748+
// Allow exceptions from under-length message on vRecv
2749+
LogPrintf("%s(%s, %u bytes): Exception '%s' caught, normally caused by a message being shorter than its stated length\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
27312750
}
2732-
2733-
// Process message
2734-
bool fRet = false;
2735-
try
2751+
else if (strstr(e.what(), "size too large"))
27362752
{
2737-
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
2738-
if (interruptMsgProc)
2739-
return false;
2740-
if (!pfrom->vRecvGetData.empty())
2741-
fMoreWork = true;
2753+
// Allow exceptions from over-long size
2754+
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
27422755
}
2743-
catch (const std::ios_base::failure& e)
2756+
else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
27442757
{
2745-
connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
2746-
if (strstr(e.what(), "end of data"))
2747-
{
2748-
// Allow exceptions from under-length message on vRecv
2749-
LogPrintf("%s(%s, %u bytes): Exception '%s' caught, normally caused by a message being shorter than its stated length\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2750-
}
2751-
else if (strstr(e.what(), "size too large"))
2752-
{
2753-
// Allow exceptions from over-long size
2754-
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2755-
}
2756-
else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
2757-
{
2758-
// Allow exceptions from non-canonical encoding
2759-
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2760-
}
2761-
else
2762-
{
2763-
PrintExceptionContinue(&e, "ProcessMessages()");
2764-
}
2758+
// Allow exceptions from non-canonical encoding
2759+
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
27652760
}
2766-
catch (const std::exception& e) {
2761+
else
2762+
{
27672763
PrintExceptionContinue(&e, "ProcessMessages()");
2768-
} catch (...) {
2769-
PrintExceptionContinue(NULL, "ProcessMessages()");
27702764
}
2765+
}
2766+
catch (const std::exception& e) {
2767+
PrintExceptionContinue(&e, "ProcessMessages()");
2768+
} catch (...) {
2769+
PrintExceptionContinue(NULL, "ProcessMessages()");
2770+
}
27712771

2772-
if (!fRet) {
2773-
LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
2774-
}
2772+
if (!fRet) {
2773+
LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
2774+
}
27752775

2776-
LOCK(cs_main);
2777-
SendRejectsAndCheckIfBanned(pfrom, connman);
2776+
LOCK(cs_main);
2777+
SendRejectsAndCheckIfBanned(pfrom, connman);
27782778

27792779
return fMoreWork;
27802780
}

0 commit comments

Comments
 (0)