Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f9f8926
Do not shadow variables in networking code
paveljanik Aug 15, 2016
58044ac
Fix some locks
sipa Aug 26, 2016
10969f6
gui: add NodeID to the peer table
theuni Aug 19, 2016
66337dc
net: move CBanDB and CAddrDB out of net.h/cpp
Fuzzbawls Jul 31, 2020
f08e316
net: Create CConnman to encapsulate p2p connections
theuni Apr 16, 2016
2cd3d39
net: Add rpc error for missing/disabled p2p functionality
theuni May 26, 2016
00591b8
net: Pass CConnman around as needed
theuni May 26, 2016
7762b97
net: Pass CConnection to wallet rather than using the global
Fuzzbawls Aug 1, 2020
573200f
net: Move socket binding into CConnman
theuni Apr 16, 2016
1e9fa0f
net: move OpenNetworkConnection into CConnman
theuni Apr 16, 2016
091aaf2
net: handle nodesignals in CConnman
theuni May 24, 2016
facf878
net: move ban and addrman functions into CConnman
theuni Apr 16, 2016
37487ed
net: Add oneshot functions to CConnman
theuni Apr 16, 2016
5667e61
net: move added node functions to CConnman
theuni Apr 16, 2016
2e02467
net: Add most functions needed for vNodes to CConnman
theuni Apr 16, 2016
dbde9be
net: create generic functor accessors and move vNodes to CConnman
theuni Apr 16, 2016
3804c29
net: move whitelist functions into CConnman
theuni Apr 17, 2016
a1394ef
net: move nLastNodeId to CConnman
theuni Apr 18, 2016
9c5a0df
net: move nLocalHostNonce to CConnman
theuni Apr 18, 2016
2bb9dfa
net: move messageHandlerCondition to CConnman
theuni Apr 19, 2016
1cec418
net: SocketSendData returns written size
theuni May 21, 2016
6865469
net: move send/recv statistics to CConnman
theuni Apr 19, 2016
bcee6ae
net: move SendBufferSize/ReceiveFloodSize to CConnman
theuni Apr 19, 2016
481929f
net: move nLocalServices/nRelevantServices to CConnman
theuni Apr 19, 2016
2bf0921
net: move semOutbound to CConnman
theuni Apr 19, 2016
15eed91
net: move max/max-outbound to CConnman
theuni May 22, 2016
48de47e
net: Pass best block known height into CConnman
theuni May 24, 2016
431575c
net: pass CClientUIInterface into CConnman
theuni May 26, 2016
e4891bf
net: Drop StartNode/StopNode and use CConnman directly
theuni May 27, 2016
22a9aff
net: Introduce CConnection::Options to avoid passing so many params
theuni May 27, 2016
276c946
net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
theuni May 27, 2016
07ae004
net: move vNodesDisconnected into CConnman
theuni May 27, 2016
c1e59ad
minor net cleanups
Fuzzbawls Aug 14, 2020
9485ab0
Made the ForEachNode* functions in src/net.cpp more pragmatic and sel…
JeremyRubin Jun 15, 2016
874baba
Convert ForEachNode* functions to take a templated function argument …
JeremyRubin Jun 20, 2016
58eabe4
net: move MAX_FEELER_CONNECTIONS into connman
theuni Aug 31, 2016
1443f2a
net: a few small cleanups before replacing boost threads
theuni Dec 27, 2016
814d0de
net: add CThreadInterrupt and InterruptibleSleep
theuni Dec 27, 2016
e24b4cc
net: make net interruptible
theuni Dec 27, 2016
1bd97ef
net: make net processing interruptable
Fuzzbawls Aug 17, 2020
34ba0de
net: make proxy receives interruptible
theuni Dec 27, 2016
14d6917
net: misc header cleanups
theuni Dec 27, 2016
fe12ff9
net: No longer send local address in addrMe
laanwj Sep 15, 2016
9d5346a
Do not set an addr time penalty when a peer advertises itself.
gmaxwell Sep 3, 2016
92ab619
Address feedback
Fuzzbawls Aug 22, 2020
f38c9f9
miner: don't try to access connman if it doesn't exist
Fuzzbawls Aug 24, 2020
e1f3620
[Refactor] Proper CConnman encapsulation of proposals / votes sync
random-zebra Aug 25, 2020
219061e
[Refactor] Decouple SyncWithNode from CMasternodeSync::Process()
random-zebra Aug 25, 2020
b09da57
[Refactor] Proper CConnman encapsulation of mnsync
random-zebra Aug 25, 2020
e733939
[Cleanup] Remove CConnman::Copy(Release)NodeVector, now unused
random-zebra Aug 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ source_group("BitcoinHeaders" FILES
)

set(SERVER_SOURCES
./src/addrdb.cpp
./src/addrman.cpp
./src/bloom.cpp
./src/blocksignature.cpp
Expand Down Expand Up @@ -415,6 +416,7 @@ set(UTIL_SOURCES
./src/random.cpp
./src/rpc/protocol.cpp
./src/sync.cpp
./src/threadinterrupt.cpp
./src/blob_uint256.cpp
./src/arith_uint256.cpp
./src/uint256.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ LIBSAPLING_H = \
# pivx core #
BITCOIN_CORE_H = \
activemasternode.h \
addrdb.h \
addrman.h \
allocators.h \
arith_uint256.h \
Expand Down Expand Up @@ -243,6 +244,7 @@ BITCOIN_CORE_H = \
support/cleanse.h \
sync.h \
threadsafety.h \
threadinterrupt.h \
timedata.h \
tinyformat.h \
torcontrol.h \
Expand Down Expand Up @@ -292,6 +294,7 @@ libbitcoin_util_a-clientversion.$(OBJEXT): obj/build.h
libbitcoin_server_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
libbitcoin_server_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_server_a_SOURCES = \
addrdb.cpp \
addrman.cpp \
bloom.cpp \
blocksignature.cpp \
Expand Down Expand Up @@ -511,6 +514,7 @@ libbitcoin_util_a_SOURCES = \
rpc/protocol.cpp \
support/cleanse.cpp \
sync.cpp \
threadinterrupt.cpp \
uint256.cpp \
blob_uint256.cpp \
util.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void CActiveMasternode::ManageStatus()
LogPrintf("CActiveMasternode::ManageStatus() - Checking inbound connection to '%s'\n", service.ToString());

CAddress addr(service, NODE_NETWORK);
if (!OpenNetworkConnection(addr, true, nullptr)) {
if (!g_connman->OpenNetworkConnection(addr, true, nullptr)) {
notCapableReason = "Could not connect to " + service.ToString();
LogPrintf("CActiveMasternode::ManageStatus() - not capable: %s\n", notCapableReason);
return;
Expand Down
216 changes: 216 additions & 0 deletions src/addrdb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2020 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "addrdb.h"

#include "addrman.h"
#include "chainparams.h"
#include "clientversion.h"
#include "fs.h"
#include "hash.h"
#include "random.h"
#include "streams.h"
#include "tinyformat.h"
#include "util.h"

CBanDB::CBanDB()
{
pathBanlist = GetDataDir() / "banlist.dat";
}

bool CBanDB::Write(const banmap_t& banSet)
{
// Generate random temporary filename
unsigned short randv = 0;
GetRandBytes((unsigned char*)&randv, sizeof(randv));
std::string tmpfn = strprintf("banlist.dat.%04x", randv);

// serialize banlist, checksum data up to that point, then append csum
CDataStream ssBanlist(SER_DISK, CLIENT_VERSION);
ssBanlist << FLATDATA(Params().MessageStart());
ssBanlist << banSet;
uint256 hash = Hash(ssBanlist.begin(), ssBanlist.end());
ssBanlist << hash;

// open temp output file, and associate with CAutoFile
fs::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fsbridge::fopen(pathTmp, "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
return error("%s: Failed to open file %s", __func__, pathTmp.string());

// Write and commit header, data
try {
fileout << ssBanlist;
}
catch (const std::exception& e) {
return error("%s: Serialize or I/O error - %s", __func__, e.what());
}
FileCommit(fileout.Get());
fileout.fclose();

// replace existing banlist.dat, if any, with new banlist.dat.XXXX
if (!RenameOver(pathTmp, pathBanlist))
return error("%s: Rename-into-place failed", __func__);

return true;
}

bool CBanDB::Read(banmap_t& banSet)
{
// open input file, and associate with CAutoFile
FILE *file = fsbridge::fopen(pathBanlist, "rb");
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull())
return error("%s: Failed to open file %s", __func__, pathBanlist.string());

// use file size to size memory buffer
uint64_t fileSize = fs::file_size(pathBanlist);
uint64_t dataSize = 0;
// Don't try to resize to a negative number if file is small
if (fileSize >= sizeof(uint256))
dataSize = fileSize - sizeof(uint256);
std::vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;

// read data and checksum from file
try {
filein.read((char *)&vchData[0], dataSize);
filein >> hashIn;
}
catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
}
filein.fclose();

CDataStream ssBanlist(vchData, SER_DISK, CLIENT_VERSION);

// verify stored checksum matches input data
uint256 hashTmp = Hash(ssBanlist.begin(), ssBanlist.end());
if (hashIn != hashTmp)
return error("%s: Checksum mismatch, data corrupted", __func__);

unsigned char pchMsgTmp[4];
try {
// de-serialize file header (network specific magic number) and ..
ssBanlist >> FLATDATA(pchMsgTmp);

// ... verify the network matches ours
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
return error("%s: Invalid network magic number", __func__);

// de-serialize address data into one CAddrMan object
ssBanlist >> banSet;
}
catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
}

return true;
}

CAddrDB::CAddrDB()
{
pathAddr = GetDataDir() / "peers.dat";
}

bool CAddrDB::Write(const CAddrMan& addr)
{
// Generate random temporary filename
unsigned short randv = 0;
GetRandBytes((unsigned char*)&randv, sizeof(randv));
std::string tmpfn = strprintf("peers.dat.%04x", randv);

// serialize addresses, checksum data up to that point, then append csum
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
ssPeers << FLATDATA(Params().MessageStart());
ssPeers << addr;
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
ssPeers << hash;

// open output file, and associate with CAutoFile
fs::path _pathAddr = GetDataDir() / tmpfn;
FILE* file = fsbridge::fopen(_pathAddr, "wb");
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
return error("%s : Failed to open file %s", __func__, _pathAddr.string());

// Write and commit header, data
try {
fileout << ssPeers;
} catch (const std::exception& e) {
return error("%s : Serialize or I/O error - %s", __func__, e.what());
}
FileCommit(fileout.Get());
fileout.fclose();

// replace existing peers.dat, if any, with new peers.dat.XXXX
if (!RenameOver(_pathAddr, pathAddr))
return error("%s: Rename-into-place failed", __func__);


return true;
}

bool CAddrDB::Read(CAddrMan& addr)
{
// open input file, and associate with CAutoFile
FILE* file = fsbridge::fopen(pathAddr, "rb");
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull())
return error("%s : Failed to open file %s", __func__, pathAddr.string());

// use file size to size memory buffer
uint64_t fileSize = fs::file_size(pathAddr);
uint64_t dataSize = fileSize - sizeof(uint256);
// Don't try to resize to a negative number if file is small
if (fileSize >= sizeof(uint256))
dataSize = fileSize - sizeof(uint256);
std::vector<unsigned char> vchData;
vchData.resize(dataSize);
uint256 hashIn;

// read data and checksum from file
try {
filein.read((char*)&vchData[0], dataSize);
filein >> hashIn;
} catch (const std::exception& e) {
return error("%s : Deserialize or I/O error - %s", __func__, e.what());
}
filein.fclose();

CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);

// verify stored checksum matches input data
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
if (hashIn != hashTmp)
return error("%s : Checksum mismatch, data corrupted", __func__);

return Read(addr, ssPeers);
}

bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
{
unsigned char pchMsgTmp[4];
try {
// de-serialize file header (network specific magic number) and ..
ssPeers >> FLATDATA(pchMsgTmp);

// ... verify the network matches ours
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
return error("%s : Invalid network magic number", __func__);

// de-serialize address data into one CAddrMan object
ssPeers >> addr;
} catch (const std::exception& e) {
// de-serialization has failed, ensure addrman is left in a clean state
addr.Clear();
return error("%s : Deserialize or I/O error - %s", __func__, e.what());
}

return true;
}
105 changes: 105 additions & 0 deletions src/addrdb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2020 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_ADDRDB_H
#define BITCOIN_ADDRDB_H

#include "fs.h"
#include "serialize.h"
#include "streams.h"

#include <string>
#include <map>

class CSubNet;
class CAddrMan;
class CDataStream;

typedef enum BanReason
{
BanReasonUnknown = 0,
BanReasonNodeMisbehaving = 1,
BanReasonManuallyAdded = 2
} BanReason;

class CBanEntry
{
public:
static const int CURRENT_VERSION=1;
int nVersion;
int64_t nCreateTime;
int64_t nBanUntil;
uint8_t banReason;

CBanEntry()
{
SetNull();
}

CBanEntry(int64_t nCreateTimeIn)
{
SetNull();
nCreateTime = nCreateTimeIn;
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nVersion);
READWRITE(nCreateTime);
READWRITE(nBanUntil);
READWRITE(banReason);
}

void SetNull()
{
nVersion = CBanEntry::CURRENT_VERSION;
nCreateTime = 0;
nBanUntil = 0;
banReason = BanReasonUnknown;
}

std::string banReasonToString()
{
switch (banReason) {
case BanReasonNodeMisbehaving:
return "node misbehaving";
case BanReasonManuallyAdded:
return "manually added";
default:
return "unknown";
}
}
};

typedef std::map<CSubNet, CBanEntry> banmap_t;

/** Access to the (IP) address database (peers.dat) */
class CAddrDB
{
private:
fs::path pathAddr;

public:
CAddrDB();
bool Write(const CAddrMan& addr);
bool Read(CAddrMan& addr);
bool Read(CAddrMan& addr, CDataStream& ssPeers);
};

/** Access to the banlist database (banlist.dat) */
class CBanDB
{
private:
fs::path pathBanlist;
public:
CBanDB();
bool Write(const banmap_t& banSet);
bool Read(banmap_t& banSet);
};

#endif //BITCOIN_ADDRDB_H
5 changes: 5 additions & 0 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
int nId;
CAddrInfo* pinfo = Find(addr, &nId);

// Do not set a penalty for a source's self-announcement
if (addr == source) {
nTimePenalty = 0;
}

if (pinfo) {
// periodically update nTime
bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
Expand Down
Loading