Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2015 The Bitcoin Core developers 3 : // Copyright (c) 2020 The PIVX Core developers 4 : // Distributed under the MIT software license, see the accompanying 5 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 : 7 : #ifndef PIVX_ADDRDB_H 8 : #define PIVX_ADDRDB_H 9 : 10 : #include "fs.h" 11 : #include "serialize.h" 12 : #include "streams.h" 13 : 14 : #include <string> 15 : #include <map> 16 : 17 : class CSubNet; 18 : class CAddrMan; 19 : class CDataStream; 20 : 21 : typedef enum BanReason 22 : { 23 : BanReasonUnknown = 0, 24 : BanReasonNodeMisbehaving = 1, 25 : BanReasonManuallyAdded = 2 26 : } BanReason; 27 : 28 : class CBanEntry 29 : { 30 : public: 31 : static const int CURRENT_VERSION=1; 32 : int nVersion; 33 : int64_t nCreateTime; 34 : int64_t nBanUntil; 35 : uint8_t banReason; 36 : 37 20 : CBanEntry() 38 20 : { 39 20 : SetNull(); 40 : } 41 : 42 17 : explicit CBanEntry(int64_t nCreateTimeIn) 43 17 : { 44 17 : SetNull(); 45 17 : nCreateTime = nCreateTimeIn; 46 : } 47 : 48 47 : SERIALIZE_METHODS(CBanEntry, obj) { READWRITE(obj.nVersion, obj.nCreateTime, obj.nBanUntil, obj.banReason); } 49 : 50 37 : void SetNull() 51 : { 52 37 : nVersion = CBanEntry::CURRENT_VERSION; 53 37 : nCreateTime = 0; 54 37 : nBanUntil = 0; 55 20 : banReason = BanReasonUnknown; 56 : } 57 : 58 20 : std::string banReasonToString() const 59 : { 60 20 : switch (banReason) { 61 0 : case BanReasonNodeMisbehaving: 62 0 : return "node misbehaving"; 63 20 : case BanReasonManuallyAdded: 64 20 : return "manually added"; 65 0 : default: 66 0 : return "unknown"; 67 : } 68 : } 69 : }; 70 : 71 : typedef std::map<CSubNet, CBanEntry> banmap_t; 72 : 73 : /** Access to the (IP) address database (peers.dat) */ 74 1962 : class CAddrDB 75 : { 76 : private: 77 : fs::path pathAddr; 78 : 79 : public: 80 : CAddrDB(); 81 : bool Write(const CAddrMan& addr); 82 : bool Read(CAddrMan& addr); 83 : static bool Read(CAddrMan& addr, CDataStream& ssPeers); 84 : }; 85 : 86 : /** Access to the banlist database (banlist.dat) */ 87 1304 : class CBanDB 88 : { 89 : private: 90 : fs::path pathBanlist; 91 : public: 92 : CBanDB(); 93 : bool Write(const banmap_t& banSet); 94 : bool Read(banmap_t& banSet); 95 : }; 96 : 97 : #endif // PIVX_ADDRDB_H