2016-04-16 06:13:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2009-2017 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.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <addrdb.h>
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <addrman.h>
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <clientversion.h>
|
|
|
|
#include <hash.h>
|
|
|
|
#include <random.h>
|
|
|
|
#include <streams.h>
|
|
|
|
#include <tinyformat.h>
|
|
|
|
#include <util.h>
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
namespace {
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Stream, typename Data>
|
|
|
|
bool SerializeDB(Stream& stream, const Data& data)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
// Write and commit header, data
|
|
|
|
try {
|
|
|
|
CHashWriter hasher(SER_DISK, CLIENT_VERSION);
|
2018-03-21 01:37:32 +01:00
|
|
|
stream << Params().MessageStart() << data;
|
|
|
|
hasher << Params().MessageStart() << data;
|
2017-04-21 13:50:55 +02:00
|
|
|
stream << hasher.GetHash();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
return error("%s: Serialize or I/O error - %s", __func__, e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Data>
|
|
|
|
bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
|
|
|
// Generate random temporary filename
|
|
|
|
unsigned short randv = 0;
|
|
|
|
GetRandBytes((unsigned char*)&randv, sizeof(randv));
|
2017-04-21 13:50:55 +02:00
|
|
|
std::string tmpfn = strprintf("%s.%04x", prefix, randv);
|
2016-04-16 06:13:15 +02:00
|
|
|
|
|
|
|
// open temp output file, and associate with CAutoFile
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path pathTmp = GetDataDir() / tmpfn;
|
2017-03-01 17:28:39 +01:00
|
|
|
FILE *file = fsbridge::fopen(pathTmp, "wb");
|
2016-04-16 06:13:15 +02:00
|
|
|
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
|
|
|
|
if (fileout.IsNull())
|
|
|
|
return error("%s: Failed to open file %s", __func__, pathTmp.string());
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
// Serialize
|
|
|
|
if (!SerializeDB(fileout, data)) return false;
|
2018-04-20 11:21:08 +02:00
|
|
|
if (!FileCommit(fileout.Get()))
|
|
|
|
return error("%s: Failed to flush file %s", __func__, pathTmp.string());
|
2016-04-16 06:13:15 +02:00
|
|
|
fileout.fclose();
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
// replace existing file, if any, with new file
|
|
|
|
if (!RenameOver(pathTmp, path))
|
2016-04-16 06:13:15 +02:00
|
|
|
return error("%s: Rename-into-place failed", __func__);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Stream, typename Data>
|
|
|
|
bool DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
|
|
|
try {
|
2017-04-21 13:50:55 +02:00
|
|
|
CHashVerifier<Stream> verifier(&stream);
|
2016-04-16 06:13:15 +02:00
|
|
|
// de-serialize file header (network specific magic number) and ..
|
2017-04-21 13:50:55 +02:00
|
|
|
unsigned char pchMsgTmp[4];
|
2018-03-21 01:37:32 +01:00
|
|
|
verifier >> pchMsgTmp;
|
2016-04-16 06:13:15 +02:00
|
|
|
// ... verify the network matches ours
|
|
|
|
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
|
|
|
|
return error("%s: Invalid network magic number", __func__);
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
// de-serialize data
|
|
|
|
verifier >> data;
|
|
|
|
|
|
|
|
// verify checksum
|
|
|
|
if (fCheckSum) {
|
|
|
|
uint256 hashTmp;
|
|
|
|
stream >> hashTmp;
|
|
|
|
if (hashTmp != verifier.GetHash()) {
|
|
|
|
return error("%s: Checksum mismatch, data corrupted", __func__);
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
template <typename Data>
|
|
|
|
bool DeserializeFileDB(const fs::path& path, Data& data)
|
2016-04-16 06:13:15 +02:00
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
// open input file, and associate with CAutoFile
|
|
|
|
FILE *file = fsbridge::fopen(path, "rb");
|
|
|
|
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
|
|
|
|
if (filein.IsNull())
|
|
|
|
return error("%s: Failed to open file %s", __func__, path.string());
|
|
|
|
|
|
|
|
return DeserializeDB(filein, data);
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
CBanDB::CBanDB()
|
|
|
|
{
|
|
|
|
pathBanlist = GetDataDir() / "banlist.dat";
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
bool CBanDB::Write(const banmap_t& banSet)
|
|
|
|
{
|
|
|
|
return SerializeFileDB("banlist", pathBanlist, banSet);
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
bool CBanDB::Read(banmap_t& banSet)
|
|
|
|
{
|
|
|
|
return DeserializeFileDB(pathBanlist, banSet);
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
CAddrDB::CAddrDB()
|
|
|
|
{
|
|
|
|
pathAddr = GetDataDir() / "peers.dat";
|
|
|
|
}
|
2016-04-16 06:13:15 +02:00
|
|
|
|
2017-04-21 13:50:55 +02:00
|
|
|
bool CAddrDB::Write(const CAddrMan& addr)
|
|
|
|
{
|
|
|
|
return SerializeFileDB("peers", pathAddr, addr);
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CAddrDB::Read(CAddrMan& addr)
|
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
return DeserializeFileDB(pathAddr, addr);
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
|
|
|
|
{
|
2017-04-21 13:50:55 +02:00
|
|
|
bool ret = DeserializeDB(ssPeers, addr, false);
|
|
|
|
if (!ret) {
|
|
|
|
// Ensure addrman is left in a clean state
|
2016-04-16 06:13:15 +02:00
|
|
|
addr.Clear();
|
|
|
|
}
|
2017-04-21 13:50:55 +02:00
|
|
|
return ret;
|
2016-04-16 06:13:15 +02:00
|
|
|
}
|