23 : nVersion(
json[
"version"].get_int()), nCreateTime(
json[
"ban_created"].get_int64()),
24 nBanUntil(
json[
"banned_until"].get_int64())
50 for (
const auto& it : bans) {
51 const auto& address = it.first;
52 const auto& ban_entry = it.second;
69 for (
const auto& ban_entry_json : bans_json.
getValues()) {
73 throw std::runtime_error(
74 strprintf(
"Cannot parse banned address or subnet: %s", subnet_str));
76 bans.insert_or_assign(subnet,
CBanEntry{ban_entry_json});
80 template <
typename Stream,
typename Data>
81 bool SerializeDB(Stream& stream,
const Data& data)
85 CHashWriter hasher(stream.GetType(), stream.GetVersion());
88 stream << hasher.GetHash();
89 }
catch (
const std::exception& e) {
90 return error(
"%s: Serialize or I/O error - %s", __func__, e.what());
96 template <
typename Data>
97 bool SerializeFileDB(
const std::string&
prefix,
const fs::path& path,
const Data& data,
int version)
108 if (fileout.IsNull()) {
111 return error(
"%s: Failed to open file %s", __func__, pathTmp.string());
115 if (!SerializeDB(fileout, data)) {
123 return error(
"%s: Failed to flush file %s", __func__, pathTmp.string());
130 return error(
"%s: Rename-into-place failed", __func__);
136 template <
typename Stream,
typename Data>
137 bool DeserializeDB(Stream& stream, Data& data,
bool fCheckSum =
true)
142 unsigned char pchMsgTmp[4];
143 verifier >> pchMsgTmp;
145 if (memcmp(pchMsgTmp,
Params().MessageStart(),
sizeof(pchMsgTmp)))
146 return error(
"%s: Invalid network magic number", __func__);
155 if (hashTmp != verifier.GetHash()) {
156 return error(
"%s: Checksum mismatch, data corrupted", __func__);
160 catch (
const std::exception& e) {
161 return error(
"%s: Deserialize or I/O error - %s", __func__, e.what());
167 template <
typename Data>
168 bool DeserializeFileDB(
const fs::path& path, Data& data,
int version)
173 if (filein.IsNull()) {
174 LogPrintf(
"Missing or invalid file %s\n", path.string());
177 return DeserializeDB(filein, data);
182 : m_banlist_dat(ban_list_path.string() +
".dat"),
183 m_banlist_json(ban_list_path.string() +
".json")
189 std::vector<std::string> errors;
194 for (
const auto& err : errors) {
211 std::map<std::string, util::SettingsValue> settings;
212 std::vector<std::string> errors;
215 for (
const auto& err : errors) {
222 BanMapFromJson(settings[
JSON_KEY], banSet);
223 }
catch (
const std::runtime_error& e) {
248 bool ret = DeserializeDB(ssPeers, addr,
false);
256 void DumpAnchors(
const fs::path& anchors_db_path,
const std::vector<CAddress>& anchors)
262 std::vector<CAddress>
ReadAnchors(
const fs::path& anchors_db_path)
264 std::vector<CAddress> anchors;
266 LogPrintf(
"Loaded %i addresses from %s\n", anchors.size(), anchors_db_path.filename());
271 fs::remove(anchors_db_path);
char const * json() noexcept
Template to generate JSON data.
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
const std::vector< UniValue > & getValues() const
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret, DNSLookupFn dns_lookup_function)
Parse and resolve a specified subnet string into the appropriate internal representation.
FILE * fopen(const fs::path &p, const char *mode)
static const char * BANMAN_JSON_ADDR_KEY
#define LOG_TIME_SECONDS(end_msg)
Double ended buffer combining vector and stream-like interfaces.
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!cs)
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
bool Read(banmap_t &banSet, bool &dirty)
Read the banlist from disk.
Reads data from an underlying stream, while hashing the read data.
void DumpAnchors(const fs::path &anchors_db_path, const std::vector< CAddress > &anchors)
Dump the anchor IP address database (anchors.dat)
Stochastical (IP) address manager.
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
bool push_back(const UniValue &val)
bool Write(const CAddrMan &addr)
CBanDB(fs::path ban_list_path)
std::vector< CAddress > ReadAnchors(const fs::path &anchors_db_path)
Read the anchor IP address database (anchors.dat)
const CMessageHeader::MessageStartChars & MessageStart() const
bool pushKV(const std::string &key, const UniValue &val)
static constexpr const char * JSON_KEY
JSON key under which the data is stored in the json database.
bool Write(const banmap_t &banSet)
bool RenameOver(fs::path src, fs::path dest)
const fs::path m_banlist_dat
const CChainParams & Params()
Return the currently selected parameters.
const fs::path m_banlist_json
A writer stream (for serialization) that computes a 256-bit hash.
std::map< CSubNet, CBanEntry > banmap_t
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
bool error(const char *fmt, const Args &... args)
UniValue ToJson() const
Generate a JSON representation of this ban entry.
bool Read(CAddrMan &addr)
Non-refcounted RAII wrapper for FILE*.
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.