2016-04-16 06:13:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2016-04-16 06:13:15 +02:00
|
|
|
// 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
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <fs.h>
|
|
|
|
#include <serialize.h>
|
2016-04-16 06:13:15 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
class CSubNet;
|
|
|
|
class CAddrMan;
|
2016-04-16 20:47:18 +02:00
|
|
|
class CDataStream;
|
2016-04-16 06:13:15 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-08-01 12:22:41 +02:00
|
|
|
explicit CBanEntry(int64_t nCreateTimeIn)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
|
|
|
SetNull();
|
|
|
|
nCreateTime = nCreateTimeIn;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:10 +01:00
|
|
|
explicit CBanEntry(int64_t n_create_time_in, BanReason ban_reason_in) : CBanEntry(n_create_time_in)
|
|
|
|
{
|
|
|
|
banReason = ban_reason_in;
|
|
|
|
}
|
|
|
|
|
2016-04-16 06:13:15 +02:00
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
2016-10-29 01:29:17 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2016-04-16 06:13:15 +02:00
|
|
|
READWRITE(this->nVersion);
|
|
|
|
READWRITE(nCreateTime);
|
|
|
|
READWRITE(nBanUntil);
|
|
|
|
READWRITE(banReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull()
|
|
|
|
{
|
|
|
|
nVersion = CBanEntry::CURRENT_VERSION;
|
|
|
|
nCreateTime = 0;
|
|
|
|
nBanUntil = 0;
|
|
|
|
banReason = BanReasonUnknown;
|
|
|
|
}
|
|
|
|
|
2017-03-09 13:34:54 +01:00
|
|
|
std::string banReasonToString() const
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
|
|
|
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:
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path pathAddr;
|
2016-04-16 06:13:15 +02:00
|
|
|
public:
|
|
|
|
CAddrDB();
|
|
|
|
bool Write(const CAddrMan& addr);
|
|
|
|
bool Read(CAddrMan& addr);
|
2017-04-21 13:50:55 +02:00
|
|
|
static bool Read(CAddrMan& addr, CDataStream& ssPeers);
|
2016-04-16 06:13:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Access to the banlist database (banlist.dat) */
|
|
|
|
class CBanDB
|
|
|
|
{
|
|
|
|
private:
|
2017-10-05 19:35:20 +02:00
|
|
|
const fs::path m_ban_list_path;
|
2016-04-16 06:13:15 +02:00
|
|
|
public:
|
2017-10-05 19:35:20 +02:00
|
|
|
explicit CBanDB(fs::path ban_list_path);
|
2016-04-16 06:13:15 +02:00
|
|
|
bool Write(const banmap_t& banSet);
|
|
|
|
bool Read(banmap_t& banSet);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BITCOIN_ADDRDB_H
|