seeder/db.cpp

214 lines
5.6 KiB
C++
Raw Normal View History

2011-12-19 23:18:06 +01:00
#include "db.h"
#include <stdlib.h>
using namespace std;
2011-12-19 23:45:18 +01:00
void CAddrInfo::Update(bool good) {
2011-12-23 02:43:32 +01:00
uint32_t now = time(NULL);
if (ourLastTry == 0)
ourLastTry = now - MIN_RETRY;
int age = now - ourLastTry;
lastTry = now;
ourLastTry = now;
total++;
if (good) success++;
stat2H.Update(good, age, 3600*2);
stat8H.Update(good, age, 3600*8);
stat1D.Update(good, age, 3600*24);
stat1W.Update(good, age, 3600*24*7);
int ign = GetIgnoreTime();
if (ign && (ignoreTill==0 || ignoreTill < ign+now)) ignoreTill = ign+now;
2011-12-26 15:53:22 +01:00
// printf("%s: got %s result: success=%i/%i; 2H:%.2f%%-%.2f%%(%.2f) 8H:%.2f%%-%.2f%%(%.2f) 1D:%.2f%%-%.2f%%(%.2f) 1W:%.2f%%-%.2f%%(%.2f) \n", ToString(ip).c_str(), good ? "good" : "bad", success, total,
// 100.0 * stat2H.reliability, 100.0 * (stat2H.reliability + 1.0 - stat2H.weight), stat2H.count,
// 100.0 * stat8H.reliability, 100.0 * (stat8H.reliability + 1.0 - stat8H.weight), stat8H.count,
// 100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - stat1D.weight), stat1D.count,
// 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - stat1W.weight), stat1W.count);
2011-12-19 23:45:18 +01:00
}
2011-12-20 06:42:48 +01:00
bool CAddrDb::Get_(CIPPort &ip, int &wait) {
int64 now = time(NULL);
2011-12-23 02:43:32 +01:00
int cont = 0;
2011-12-20 14:29:21 +01:00
int tot = unkId.size();
2011-12-23 02:43:32 +01:00
do {
deque<int>::iterator it = ourId.begin();
while (it < ourId.end()) {
if (now - idToInfo[*it].ourLastTry > MIN_RETRY) {
tot++;
it++;
} else {
break;
}
}
if (tot == 0) {
if (ourId.size() > 0) {
wait = MIN_RETRY - (now - idToInfo[ourId.front()].ourLastTry);
} else {
wait = 5;
}
return false;
}
int rnd = rand() % tot;
int ret;
if (rnd < unkId.size()) {
if (rnd*10 < unkId.size()) {
// once every 10 attempts, restart with the oldest unknown IP
set<int>::iterator it = unkId.begin();
ret = *it;
} else {
// 90% of the time try the last learned IP
set<int>::reverse_iterator it = unkId.rbegin();
ret = *it;
}
unkId.erase(ret);
2011-12-20 05:20:50 +01:00
} else {
ret = ourId.front();
2011-12-23 02:43:32 +01:00
if (time(NULL) - idToInfo[ret].ourLastTry < MIN_RETRY) return false;
ourId.pop_front();
2011-12-20 14:29:21 +01:00
}
2011-12-23 02:43:32 +01:00
if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) {
ourId.push_back(ret);
2011-12-26 18:27:02 +01:00
idToInfo[ret].ourLastTry = now;
2011-12-23 02:43:32 +01:00
} else {
ip = idToInfo[ret].ip;
break;
2011-12-20 05:20:50 +01:00
}
2011-12-23 02:43:32 +01:00
} while(1);
nDirty++;
2011-12-20 05:20:50 +01:00
return true;
2011-12-19 23:18:06 +01:00
}
int CAddrDb::Lookup_(const CIPPort &ip) {
if (ipToId.count(ip))
return ipToId[ip];
return -1;
}
2011-12-23 02:43:32 +01:00
void CAddrDb::Good_(const CIPPort &addr, int clientV) {
2011-12-19 23:18:06 +01:00
int id = Lookup_(addr);
if (id == -1) return;
unkId.erase(id);
banned.erase(addr);
CAddrInfo &info = idToInfo[id];
2011-12-23 02:43:32 +01:00
info.clientVersion = clientV;
2011-12-19 23:18:06 +01:00
info.Update(true);
2011-12-20 14:29:21 +01:00
if (info.IsGood() && goodId.count(id)==0) {
2011-12-19 23:45:18 +01:00
goodId.insert(id);
2011-12-26 15:53:22 +01:00
// printf("%s: good; %i good nodes now\n", ToString(addr).c_str(), (int)goodId.size());
2011-12-19 23:45:18 +01:00
}
2011-12-23 02:43:32 +01:00
nDirty++;
2011-12-19 23:18:06 +01:00
ourId.push_back(id);
}
void CAddrDb::Bad_(const CIPPort &addr, int ban)
{
int id = Lookup_(addr);
if (id == -1) return;
unkId.erase(id);
CAddrInfo &info = idToInfo[id];
info.Update(false);
2011-12-19 23:45:18 +01:00
uint32_t now = time(NULL);
2011-12-23 02:43:32 +01:00
int ter = info.GetBanTime();
if (ter) {
2011-12-26 15:53:22 +01:00
// printf("%s: terrible\n", ToString(addr).c_str());
2011-12-23 02:43:32 +01:00
if (ban < ter) ban = ter;
2011-12-19 23:45:18 +01:00
}
2011-12-20 06:42:48 +01:00
if (ban > 0) {
2011-12-26 15:53:22 +01:00
// printf("%s: ban for %i seconds\n", ToString(addr).c_str(), ban);
2011-12-20 06:42:48 +01:00
banned[info.ip] = ban + now;
2011-12-19 23:18:06 +01:00
ipToId.erase(info.ip);
goodId.erase(id);
idToInfo.erase(id);
} else {
2012-01-01 19:18:56 +01:00
if (/*!info.IsGood() && */ goodId.count(id)==1) {
2011-12-19 23:18:06 +01:00
goodId.erase(id);
2011-12-26 15:53:22 +01:00
// printf("%s: not good; %i good nodes left\n", ToString(addr).c_str(), (int)goodId.size());
2011-12-19 23:45:18 +01:00
}
2011-12-19 23:18:06 +01:00
ourId.push_back(id);
}
2011-12-23 02:43:32 +01:00
nDirty++;
2011-12-19 23:18:06 +01:00
}
void CAddrDb::Skipped_(const CIPPort &addr)
{
int id = Lookup_(addr);
if (id == -1) return;
unkId.erase(id);
ourId.push_back(id);
2011-12-26 15:53:22 +01:00
// printf("%s: skipped\n", ToString(addr).c_str());
2011-12-23 02:43:32 +01:00
nDirty++;
2011-12-19 23:18:06 +01:00
}
2011-12-26 01:04:24 +01:00
void CAddrDb::Add_(const CAddress &addr, bool force) {
if (!force && !addr.IsRoutable())
2011-12-19 23:18:06 +01:00
return;
CIPPort ipp(addr);
if (banned.count(ipp)) {
time_t bantime = banned[ipp];
2011-12-26 01:04:24 +01:00
if (force || (bantime < time(NULL) && addr.nTime > bantime))
2011-12-19 23:18:06 +01:00
banned.erase(ipp);
else
return;
}
if (ipToId.count(ipp)) {
CAddrInfo &ai = idToInfo[ipToId[ipp]];
2011-12-20 14:29:21 +01:00
if (addr.nTime > ai.lastTry || ai.services != addr.nServices)
{
2011-12-19 23:18:06 +01:00
ai.lastTry = addr.nTime;
2011-12-20 14:29:21 +01:00
ai.services |= addr.nServices;
// printf("%s: updated\n", ToString(addr).c_str());
}
2011-12-26 01:04:24 +01:00
if (force) {
ai.ignoreTill = 0;
}
2011-12-19 23:18:06 +01:00
return;
}
CAddrInfo ai;
ai.ip = ipp;
ai.services = addr.nServices;
ai.lastTry = addr.nTime;
ai.ourLastTry = 0;
ai.total = 0;
ai.success = 0;
int id = nId++;
idToInfo[id] = ai;
ipToId[ipp] = id;
// printf("%s: added\n", ToString(ipp).c_str(), ipToId[ipp]);
2011-12-19 23:18:06 +01:00
unkId.insert(id);
2011-12-23 02:43:32 +01:00
nDirty++;
2011-12-19 23:18:06 +01:00
}
void CAddrDb::GetIPs_(set<CIP>& ips, int max, bool fOnlyIPv4) {
if (goodId.size() == 0) {
int id = -1;
if (ourId.size() == 0) {
if (unkId.size() == 0) return;
id = *unkId.begin();
} else {
id = *ourId.begin();
}
if (id >= 0) {
ips.insert(idToInfo[id].ip);
}
return;
}
if (max > goodId.size() / 2)
max = goodId.size() / 2;
if (max < 1)
max = 1;
int low = *goodId.begin();
int high = *goodId.rbegin();
set<int> ids;
while (ids.size() < max) {
int range = high-low+1;
int pos = low + (rand() % range);
int id = *(goodId.lower_bound(pos));
ids.insert(id);
}
for (set<int>::const_iterator it = ids.begin(); it != ids.end(); it++) {
CIPPort &ip = idToInfo[*it].ip;
if (ip.IsValid() && (!fOnlyIPv4 || ip.IsIPv4()))
ips.insert(ip);
}
}