Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/db.cpp
#include "db.h" | #include "db.h" | ||||
#include <stdlib.h> | |||||
using namespace std; | #include <cstdlib> | ||||
void CAddrInfo::Update(bool good) { | void CAddrInfo::Update(bool good) { | ||||
uint32_t now = time(NULL); | uint32_t now = time(nullptr); | ||||
if (ourLastTry == 0) ourLastTry = now - MIN_RETRY; | if (ourLastTry == 0) ourLastTry = now - MIN_RETRY; | ||||
int age = now - ourLastTry; | int age = now - ourLastTry; | ||||
lastTry = now; | lastTry = now; | ||||
ourLastTry = now; | ourLastTry = now; | ||||
total++; | total++; | ||||
if (good) { | if (good) { | ||||
success++; | success++; | ||||
ourLastSuccess = now; | ourLastSuccess = now; | ||||
Show All 15 Lines | void CAddrInfo::Update(bool good) { | ||||
// stat8H.weight), stat8H.count, | // stat8H.weight), stat8H.count, | ||||
// 100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - | // 100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - | ||||
// stat1D.weight), stat1D.count, | // stat1D.weight), stat1D.count, | ||||
// 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - | // 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - | ||||
// stat1W.weight), stat1W.count); | // stat1W.weight), stat1W.count); | ||||
} | } | ||||
bool CAddrDb::Get_(CServiceResult &ip, int &wait) { | bool CAddrDb::Get_(CServiceResult &ip, int &wait) { | ||||
int64 now = time(NULL); | int64 now = time(nullptr); | ||||
int cont = 0; | int cont = 0; | ||||
int tot = unkId.size() + ourId.size(); | int tot = unkId.size() + ourId.size(); | ||||
if (tot == 0) { | if (tot == 0) { | ||||
wait = 5; | wait = 5; | ||||
return false; | return false; | ||||
} | } | ||||
do { | do { | ||||
int rnd = rand() % tot; | int rnd = rand() % tot; | ||||
int ret; | int ret; | ||||
if (rnd < unkId.size()) { | if (rnd < unkId.size()) { | ||||
set<int>::iterator it = unkId.end(); | std::set<int>::iterator it = unkId.end(); | ||||
it--; | it--; | ||||
ret = *it; | ret = *it; | ||||
unkId.erase(it); | unkId.erase(it); | ||||
} else { | } else { | ||||
ret = ourId.front(); | ret = ourId.front(); | ||||
if (time(NULL) - idToInfo[ret].ourLastTry < MIN_RETRY) return false; | if (time(nullptr) - idToInfo[ret].ourLastTry < MIN_RETRY) | ||||
return false; | |||||
ourId.pop_front(); | ourId.pop_front(); | ||||
} | } | ||||
if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) { | if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) { | ||||
ourId.push_back(ret); | ourId.push_back(ret); | ||||
idToInfo[ret].ourLastTry = now; | idToInfo[ret].ourLastTry = now; | ||||
} else { | } else { | ||||
ip.service = idToInfo[ret].ip; | ip.service = idToInfo[ret].ip; | ||||
ip.ourLastSuccess = idToInfo[ret].ourLastSuccess; | ip.ourLastSuccess = idToInfo[ret].ourLastSuccess; | ||||
Show All 30 Lines | |||||
} | } | ||||
void CAddrDb::Bad_(const CService &addr, int ban) { | void CAddrDb::Bad_(const CService &addr, int ban) { | ||||
int id = Lookup_(addr); | int id = Lookup_(addr); | ||||
if (id == -1) return; | if (id == -1) return; | ||||
unkId.erase(id); | unkId.erase(id); | ||||
CAddrInfo &info = idToInfo[id]; | CAddrInfo &info = idToInfo[id]; | ||||
info.Update(false); | info.Update(false); | ||||
uint32_t now = time(NULL); | uint32_t now = time(nullptr); | ||||
int ter = info.GetBanTime(); | int ter = info.GetBanTime(); | ||||
if (ter) { | if (ter) { | ||||
// printf("%s: terrible\n", ToString(addr).c_str()); | // printf("%s: terrible\n", ToString(addr).c_str()); | ||||
if (ban < ter) ban = ter; | if (ban < ter) ban = ter; | ||||
} | } | ||||
if (ban > 0) { | if (ban > 0) { | ||||
// printf("%s: ban for %i seconds\n", ToString(addr).c_str(), ban); | // printf("%s: ban for %i seconds\n", ToString(addr).c_str(), ban); | ||||
banned[info.ip] = ban + now; | banned[info.ip] = ban + now; | ||||
Show All 20 Lines | void CAddrDb::Skipped_(const CService &addr) { | ||||
nDirty++; | nDirty++; | ||||
} | } | ||||
void CAddrDb::Add_(const CAddress &addr, bool force) { | void CAddrDb::Add_(const CAddress &addr, bool force) { | ||||
if (!force && !addr.IsRoutable()) return; | if (!force && !addr.IsRoutable()) return; | ||||
CService ipp(addr); | CService ipp(addr); | ||||
if (banned.count(ipp)) { | if (banned.count(ipp)) { | ||||
time_t bantime = banned[ipp]; | time_t bantime = banned[ipp]; | ||||
if (force || (bantime < time(NULL) && addr.nTime > bantime)) | if (force || (bantime < time(nullptr) && addr.nTime > bantime)) | ||||
banned.erase(ipp); | banned.erase(ipp); | ||||
else | else | ||||
return; | return; | ||||
} | } | ||||
if (ipToId.count(ipp)) { | if (ipToId.count(ipp)) { | ||||
CAddrInfo &ai = idToInfo[ipToId[ipp]]; | CAddrInfo &ai = idToInfo[ipToId[ipp]]; | ||||
if (addr.nTime > ai.lastTry || ai.services != addr.nServices) { | if (addr.nTime > ai.lastTry || ai.services != addr.nServices) { | ||||
ai.lastTry = addr.nTime; | ai.lastTry = addr.nTime; | ||||
Show All 15 Lines | void CAddrDb::Add_(const CAddress &addr, bool force) { | ||||
int id = nId++; | int id = nId++; | ||||
idToInfo[id] = ai; | idToInfo[id] = ai; | ||||
ipToId[ipp] = id; | ipToId[ipp] = id; | ||||
// printf("%s: added\n", ToString(ipp).c_str(), ipToId[ipp]); | // printf("%s: added\n", ToString(ipp).c_str(), ipToId[ipp]); | ||||
unkId.insert(id); | unkId.insert(id); | ||||
nDirty++; | nDirty++; | ||||
} | } | ||||
void CAddrDb::GetIPs_(set<CNetAddr> &ips, uint64_t requestedFlags, int max, | void CAddrDb::GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags, int max, | ||||
const bool *nets) { | const bool *nets) { | ||||
if (goodId.size() == 0) { | if (goodId.size() == 0) { | ||||
int id = -1; | int id = -1; | ||||
if (ourId.size() == 0) { | if (ourId.size() == 0) { | ||||
if (unkId.size() == 0) return; | if (unkId.size() == 0) return; | ||||
id = *unkId.begin(); | id = *unkId.begin(); | ||||
} else { | } else { | ||||
id = *ourId.begin(); | id = *ourId.begin(); | ||||
Show All 11 Lines | for (std::set<int>::const_iterator it = goodId.begin(); it != goodId.end(); | ||||
goodIdFiltered.push_back(*it); | goodIdFiltered.push_back(*it); | ||||
} | } | ||||
if (!goodIdFiltered.size()) return; | if (!goodIdFiltered.size()) return; | ||||
if (max > goodIdFiltered.size() / 2) max = goodIdFiltered.size() / 2; | if (max > goodIdFiltered.size() / 2) max = goodIdFiltered.size() / 2; | ||||
if (max < 1) max = 1; | if (max < 1) max = 1; | ||||
set<int> ids; | std::set<int> ids; | ||||
while (ids.size() < max) { | while (ids.size() < max) { | ||||
ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]); | ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]); | ||||
} | } | ||||
for (set<int>::const_iterator it = ids.begin(); it != ids.end(); it++) { | for (std::set<int>::const_iterator it = ids.begin(); it != ids.end(); | ||||
it++) { | |||||
CService &ip = idToInfo[*it].ip; | CService &ip = idToInfo[*it].ip; | ||||
if (nets[ip.GetNetwork()]) ips.insert(ip); | if (nets[ip.GetNetwork()]) ips.insert(ip); | ||||
} | } | ||||
} | } |