Skip to content

Commit 31a14d4

Browse files
committed
Correct indentation and remove unnecessary braces
1 parent 12af74b commit 31a14d4

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
@@ -701,15 +701,15 @@ class CNode
701701
public:
702702

703703
NodeId GetId() const {
704-
return id;
704+
return id;
705705
}
706706

707707
uint64_t GetLocalNonce() const {
708-
return nLocalHostNonce;
708+
return nLocalHostNonce;
709709
}
710710

711711
int GetMyStartingHeight() const {
712-
return nMyStartingHeight;
712+
return nMyStartingHeight;
713713
}
714714

715715
int GetRefCount()

src/net_processing.cpp

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

2664-
// Don't bother if send buffer is too full to respond anyway
2665-
if (pfrom->fPauseSend)
2666-
return false;
2664+
// Don't bother if send buffer is too full to respond anyway
2665+
if (pfrom->fPauseSend)
2666+
return false;
26672667

2668-
std::list<CNetMessage> msgs;
2669-
{
2670-
LOCK(pfrom->cs_vProcessMsg);
2671-
if (pfrom->vProcessMsg.empty())
2672-
return false;
2673-
// Just take one message
2674-
msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2675-
pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2676-
pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman.GetReceiveFloodSize();
2677-
fMoreWork = !pfrom->vProcessMsg.empty();
2678-
}
2679-
CNetMessage& msg(msgs.front());
2680-
2681-
msg.SetVersion(pfrom->GetRecvVersion());
2682-
// Scan for message start
2683-
if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
2684-
LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
2685-
pfrom->fDisconnect = true;
2668+
std::list<CNetMessage> msgs;
2669+
{
2670+
LOCK(pfrom->cs_vProcessMsg);
2671+
if (pfrom->vProcessMsg.empty())
26862672
return false;
2687-
}
2673+
// Just take one message
2674+
msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2675+
pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2676+
pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman.GetReceiveFloodSize();
2677+
fMoreWork = !pfrom->vProcessMsg.empty();
2678+
}
2679+
CNetMessage& msg(msgs.front());
2680+
2681+
msg.SetVersion(pfrom->GetRecvVersion());
2682+
// Scan for message start
2683+
if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
2684+
LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
2685+
pfrom->fDisconnect = true;
2686+
return false;
2687+
}
26882688

2689-
// Read header
2690-
CMessageHeader& hdr = msg.hdr;
2691-
if (!hdr.IsValid(chainparams.MessageStart()))
2692-
{
2693-
LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
2694-
return fMoreWork;
2695-
}
2696-
std::string strCommand = hdr.GetCommand();
2689+
// Read header
2690+
CMessageHeader& hdr = msg.hdr;
2691+
if (!hdr.IsValid(chainparams.MessageStart()))
2692+
{
2693+
LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
2694+
return fMoreWork;
2695+
}
2696+
std::string strCommand = hdr.GetCommand();
2697+
2698+
// Message size
2699+
unsigned int nMessageSize = hdr.nMessageSize;
26972700

2698-
// Message size
2699-
unsigned int nMessageSize = hdr.nMessageSize;
2701+
// Checksum
2702+
CDataStream& vRecv = msg.vRecv;
2703+
const uint256& hash = msg.GetMessageHash();
2704+
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2705+
{
2706+
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2707+
SanitizeString(strCommand), nMessageSize,
2708+
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
2709+
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
2710+
return fMoreWork;
2711+
}
27002712

2701-
// Checksum
2702-
CDataStream& vRecv = msg.vRecv;
2703-
const uint256& hash = msg.GetMessageHash();
2704-
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2713+
// Process message
2714+
bool fRet = false;
2715+
try
2716+
{
2717+
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
2718+
if (interruptMsgProc)
2719+
return false;
2720+
if (!pfrom->vRecvGetData.empty())
2721+
fMoreWork = true;
2722+
}
2723+
catch (const std::ios_base::failure& e)
2724+
{
2725+
connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
2726+
if (strstr(e.what(), "end of data"))
27052727
{
2706-
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2707-
SanitizeString(strCommand), nMessageSize,
2708-
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
2709-
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
2710-
return fMoreWork;
2728+
// Allow exceptions from under-length message on vRecv
2729+
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());
27112730
}
2712-
2713-
// Process message
2714-
bool fRet = false;
2715-
try
2731+
else if (strstr(e.what(), "size too large"))
27162732
{
2717-
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
2718-
if (interruptMsgProc)
2719-
return false;
2720-
if (!pfrom->vRecvGetData.empty())
2721-
fMoreWork = true;
2733+
// Allow exceptions from over-long size
2734+
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
27222735
}
2723-
catch (const std::ios_base::failure& e)
2736+
else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
27242737
{
2725-
connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
2726-
if (strstr(e.what(), "end of data"))
2727-
{
2728-
// Allow exceptions from under-length message on vRecv
2729-
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());
2730-
}
2731-
else if (strstr(e.what(), "size too large"))
2732-
{
2733-
// Allow exceptions from over-long size
2734-
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2735-
}
2736-
else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
2737-
{
2738-
// Allow exceptions from non-canonical encoding
2739-
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2740-
}
2741-
else
2742-
{
2743-
PrintExceptionContinue(&e, "ProcessMessages()");
2744-
}
2738+
// Allow exceptions from non-canonical encoding
2739+
LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
27452740
}
2746-
catch (const std::exception& e) {
2741+
else
2742+
{
27472743
PrintExceptionContinue(&e, "ProcessMessages()");
2748-
} catch (...) {
2749-
PrintExceptionContinue(NULL, "ProcessMessages()");
27502744
}
2745+
}
2746+
catch (const std::exception& e) {
2747+
PrintExceptionContinue(&e, "ProcessMessages()");
2748+
} catch (...) {
2749+
PrintExceptionContinue(NULL, "ProcessMessages()");
2750+
}
27512751

2752-
if (!fRet) {
2753-
LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
2754-
}
2752+
if (!fRet) {
2753+
LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
2754+
}
27552755

2756-
LOCK(cs_main);
2757-
SendRejectsAndCheckIfBanned(pfrom, connman);
2756+
LOCK(cs_main);
2757+
SendRejectsAndCheckIfBanned(pfrom, connman);
27582758

27592759
return fMoreWork;
27602760
}

0 commit comments

Comments
 (0)