From fb741a9dc0ee6f7c540ef84d529d478529e1dad9 Mon Sep 17 00:00:00 2001 From: hunbernd Date: Sat, 2 Jan 2021 18:44:32 +0100 Subject: [PATCH] Fix: DHT cannot bootstrap if bdboot.txt is corrupted. If the bdboot.txt file is broken in profile folder, attempt to load the data from the file installed with RS. --- src/bitdht/bdmanager.cc | 4 ++-- src/bitdht/bdmanager.h | 2 +- src/bitdht/bdnode.cc | 4 ++-- src/bitdht/bdnode.h | 2 +- src/bitdht/bdstore.cc | 19 ++++++++++++++++--- src/bitdht/bdstore.h | 6 ++++-- src/udp/udpbitdht.cc | 4 ++-- src/udp/udpbitdht.h | 2 +- 8 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/bitdht/bdmanager.cc b/src/bitdht/bdmanager.cc index 522b062..dadbfb2 100644 --- a/src/bitdht/bdmanager.cc +++ b/src/bitdht/bdmanager.cc @@ -64,8 +64,8 @@ #define QUERY_UPDATE_PERIOD 8 // under refresh period - so it'll happen at the MAX_REFRESH_PERIOD -bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string bootfile, const std::string& filterfile,bdDhtFunctions *fns) - :bdNode(id, dhtVersion, bootfile, filterfile, fns, this) +bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string bootfile, std::string bootfilebak, const std::string& filterfile, bdDhtFunctions *fns) + :bdNode(id, dhtVersion, bootfile, bootfilebak,filterfile, fns, this) { mMode = BITDHT_MGR_STATE_OFF; mDhtFns = fns; diff --git a/src/bitdht/bdmanager.h b/src/bitdht/bdmanager.h index 1a6a2ce..e1440c1 100644 --- a/src/bitdht/bdmanager.h +++ b/src/bitdht/bdmanager.h @@ -90,7 +90,7 @@ class bdQueryPeer class bdNodeManager: public bdNode, public BitDhtInterface { public: - bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string bootfile, const std::string &filterfile, bdDhtFunctions *fns); + bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string bootfile, std::string bootfilebak, const std::string &filterfile, bdDhtFunctions *fns); void iteration(); diff --git a/src/bitdht/bdnode.cc b/src/bitdht/bdnode.cc index 9adaaea..5d3e4fd 100644 --- a/src/bitdht/bdnode.cc +++ b/src/bitdht/bdnode.cc @@ -67,12 +67,12 @@ #define HISTORY_PERIOD 60 -bdNode::bdNode(bdNodeId *ownId, std::string dhtVersion, const std::string& bootfile, const std::string& filterfile, bdDhtFunctions *fns, bdNodeManager *manager) +bdNode::bdNode(bdNodeId *ownId, std::string dhtVersion, const std::string& bootfile, const std::string &bootfilebak, const std::string& filterfile, bdDhtFunctions *fns, bdNodeManager *manager) :mNodeSpace(ownId, fns), mFilterPeers(filterfile,ownId, BITDHT_FILTER_REASON_OWNID, fns, manager), mQueryMgr(NULL), mConnMgr(NULL), - mOwnId(*ownId), mDhtVersion(dhtVersion), mStore(bootfile, fns), mFns(fns), + mOwnId(*ownId), mDhtVersion(dhtVersion), mStore(bootfile, bootfilebak, fns), mFns(fns), mFriendList(ownId), mHistory(HISTORY_PERIOD) { init(); /* (uses this pointers) stuff it - do it here! */ diff --git a/src/bitdht/bdnode.h b/src/bitdht/bdnode.h index ad1ffe2..39abbb4 100644 --- a/src/bitdht/bdnode.h +++ b/src/bitdht/bdnode.h @@ -117,7 +117,7 @@ class bdNode: public bdNodePublisher { public: - bdNode(bdNodeId *id, std::string dhtVersion, const std::string& bootfile, const std::string& filterfile, + bdNode(bdNodeId *id, std::string dhtVersion, const std::string& bootfile, const std::string& bootfilebak, const std::string& filterfile, bdDhtFunctions *fns, bdNodeManager* manager); void init(); /* sets up the self referential classes (mQueryMgr & mConnMgr) */ diff --git a/src/bitdht/bdstore.cc b/src/bitdht/bdstore.cc index 40031ab..2cce684 100644 --- a/src/bitdht/bdstore.cc +++ b/src/bitdht/bdstore.cc @@ -29,7 +29,7 @@ //#define DEBUG_STORE 1 -bdStore::bdStore(std::string file, bdDhtFunctions *fns) +bdStore::bdStore(std::string file, std::string backupfile, bdDhtFunctions *fns) :mFns(fns) { #ifdef DEBUG_STORE @@ -39,6 +39,7 @@ bdStore::bdStore(std::string file, bdDhtFunctions *fns) /* read data from file */ mStoreFile = file; + mStoreFileBak = backupfile; reloadFromStore(); } @@ -51,13 +52,25 @@ int bdStore::clear() } int bdStore::reloadFromStore() +{ + int result = reloadFromStore(mStoreFile); + if( result != 0 && store.size() > 0){ + return result; + } else if(mStoreFileBak != "") { //Nothing loaded, try the backup file + return reloadFromStore(mStoreFileBak); + } else { + return 0; + } +} + +int bdStore::reloadFromStore(std::string file) { clear(); - FILE *fd = fopen(mStoreFile.c_str(), "r"); + FILE *fd = fopen(file.c_str(), "r"); if (!fd) { - fprintf(stderr, "Failed to Open File: %s ... No Peers\n", mStoreFile.c_str()); + fprintf(stderr, "Failed to Open File: %s ... No Peers\n", file.c_str()); return 0; } diff --git a/src/bitdht/bdstore.h b/src/bitdht/bdstore.h index a01f5aa..2202f05 100644 --- a/src/bitdht/bdstore.h +++ b/src/bitdht/bdstore.h @@ -30,10 +30,11 @@ class bdStore { public: - bdStore(std::string file, bdDhtFunctions *fns); + bdStore(std::string file, std::string backupfile, bdDhtFunctions *fns); int reloadFromStore(); /* for restarts */ - int filterIpList(const std::list &filteredIPs); + int reloadFromStore(std::string file); + int filterIpList(const std::list &filteredIPs); int clear(); int getPeer(bdPeer *peer); @@ -43,6 +44,7 @@ public: protected: std::string mStoreFile; + std::string mStoreFileBak; std::list store; int mIndex; bdDhtFunctions *mFns; diff --git a/src/udp/udpbitdht.cc b/src/udp/udpbitdht.cc index 72357c9..066faf5 100644 --- a/src/udp/udpbitdht.cc +++ b/src/udp/udpbitdht.cc @@ -53,7 +53,7 @@ /*************************************/ -UdpBitDht::UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string appVersion, std::string bootstrapfile, const std::string& filteredipfile, bdDhtFunctions *fns) +UdpBitDht::UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string appVersion, std::string bootstrapfile, std::string bootstrapfilebak, const std::string& filteredipfile, bdDhtFunctions *fns) :UdpSubReceiver(pub), dhtMtx(true)//, mFns(fns) { std::string usedVersion; @@ -72,7 +72,7 @@ UdpBitDht::UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string appVersion, st /* setup nodeManager */ bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ - mBitDhtManager = new bdNodeManager(id, usedVersion, bootstrapfile, filteredipfile, fns); + mBitDhtManager = new bdNodeManager(id, usedVersion, bootstrapfile, bootstrapfilebak, filteredipfile, fns); } diff --git a/src/udp/udpbitdht.h b/src/udp/udpbitdht.h index 5cbf062..38088ed 100644 --- a/src/udp/udpbitdht.h +++ b/src/udp/udpbitdht.h @@ -45,7 +45,7 @@ class UdpBitDht: public UdpSubReceiver, public bdThread, public BitDhtInterface { public: - UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string dhtVersion, std::string bootstrapfile, const std::string& filteredipfile,bdDhtFunctions *fns); + UdpBitDht(UdpPublisher *pub, bdNodeId *id, std::string dhtVersion, std::string bootstrapfile, std::string bootstrapfilebak, const std::string& filteredipfile,bdDhtFunctions *fns); virtual ~UdpBitDht();