nicer output

This commit is contained in:
Pieter Wuille 2011-12-26 15:53:22 +01:00
parent 3cb418361b
commit f7ce03cf70
6 changed files with 36 additions and 31 deletions

View file

@ -81,7 +81,7 @@ class CNode {
} }
void GotVersion() { void GotVersion() {
printf("%s: version %i\n", ToString(you).c_str(), nVersion); // printf("%s: version %i\n", ToString(you).c_str(), nVersion);
BeginMessage("getaddr"); BeginMessage("getaddr");
EndMessage(); EndMessage();
doneAfter = time(NULL) + 10; doneAfter = time(NULL) + 10;
@ -124,7 +124,7 @@ class CNode {
if (strCommand == "addr") { if (strCommand == "addr") {
vector<CAddress> vAddrNew; vector<CAddress> vAddrNew;
vRecv >> vAddrNew; vRecv >> vAddrNew;
printf("%s: got %i addresses\n", ToString(you).c_str(), (int)vAddrNew.size()); // printf("%s: got %i addresses\n", ToString(you).c_str(), (int)vAddrNew.size());
int64 now = time(NULL); int64 now = time(NULL);
vector<CAddress>::iterator it = vAddrNew.begin(); vector<CAddress>::iterator it = vAddrNew.begin();
if (doneAfter == 0 || doneAfter > now + 1) doneAfter = now + 1; if (doneAfter == 0 || doneAfter > now + 1) doneAfter = now + 1;
@ -162,13 +162,13 @@ class CNode {
CMessageHeader hdr; CMessageHeader hdr;
vRecv >> hdr; vRecv >> hdr;
if (!hdr.IsValid()) { if (!hdr.IsValid()) {
printf("%s: BAD (invalid header)\n", ToString(you).c_str()); // printf("%s: BAD (invalid header)\n", ToString(you).c_str());
ban = 100000; return true; ban = 100000; return true;
} }
string strCommand = hdr.GetCommand(); string strCommand = hdr.GetCommand();
unsigned int nMessageSize = hdr.nMessageSize; unsigned int nMessageSize = hdr.nMessageSize;
if (nMessageSize > MAX_SIZE) { if (nMessageSize > MAX_SIZE) {
printf("%s: BAD (message too large)\n", ToString(you).c_str()); // printf("%s: BAD (message too large)\n", ToString(you).c_str());
ban = 100000; ban = 100000;
return true; return true;
} }
@ -232,11 +232,11 @@ public:
vRecv.resize(nPos + nBytes); vRecv.resize(nPos + nBytes);
memcpy(&vRecv[nPos], pchBuf, nBytes); memcpy(&vRecv[nPos], pchBuf, nBytes);
} else if (nBytes == 0) { } else if (nBytes == 0) {
printf("%s: BAD (connection closed prematurely)\n", ToString(you).c_str()); // printf("%s: BAD (connection closed prematurely)\n", ToString(you).c_str());
res = false; res = false;
break; break;
} else { } else {
printf("%s: BAD (connection error)\n", ToString(you).c_str()); // printf("%s: BAD (connection error)\n", ToString(you).c_str());
res = false; res = false;
break; break;
} }

20
db.cpp
View file

@ -18,11 +18,11 @@ void CAddrInfo::Update(bool good) {
stat1W.Update(good, age, 3600*24*7); stat1W.Update(good, age, 3600*24*7);
int ign = GetIgnoreTime(); int ign = GetIgnoreTime();
if (ign && (ignoreTill==0 || ignoreTill < ign+now)) ignoreTill = ign+now; if (ign && (ignoreTill==0 || ignoreTill < ign+now)) ignoreTill = ign+now;
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, // 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 * 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 * 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 * 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); // 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - stat1W.weight), stat1W.count);
} }
bool CAddrDb::Get_(CIPPort &ip, int &wait) { bool CAddrDb::Get_(CIPPort &ip, int &wait) {
@ -92,7 +92,7 @@ void CAddrDb::Good_(const CIPPort &addr, int clientV) {
info.Update(true); info.Update(true);
if (info.IsGood() && goodId.count(id)==0) { if (info.IsGood() && goodId.count(id)==0) {
goodId.insert(id); goodId.insert(id);
printf("%s: good; %i good nodes now\n", ToString(addr).c_str(), (int)goodId.size()); // printf("%s: good; %i good nodes now\n", ToString(addr).c_str(), (int)goodId.size());
} }
nDirty++; nDirty++;
ourId.push_back(id); ourId.push_back(id);
@ -108,11 +108,11 @@ void CAddrDb::Bad_(const CIPPort &addr, int ban)
uint32_t now = time(NULL); uint32_t now = time(NULL);
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;
ipToId.erase(info.ip); ipToId.erase(info.ip);
goodId.erase(id); goodId.erase(id);
@ -120,7 +120,7 @@ void CAddrDb::Bad_(const CIPPort &addr, int ban)
} else { } else {
if (!info.IsGood() && goodId.count(id)==1) { if (!info.IsGood() && goodId.count(id)==1) {
goodId.erase(id); goodId.erase(id);
printf("%s: not good; %i good nodes left\n", ToString(addr).c_str(), (int)goodId.size()); // printf("%s: not good; %i good nodes left\n", ToString(addr).c_str(), (int)goodId.size());
} }
ourId.push_back(id); ourId.push_back(id);
} }
@ -133,7 +133,7 @@ void CAddrDb::Skipped_(const CIPPort &addr)
if (id == -1) return; if (id == -1) return;
unkId.erase(id); unkId.erase(id);
ourId.push_back(id); ourId.push_back(id);
printf("%s: skipped\n", ToString(addr).c_str()); // printf("%s: skipped\n", ToString(addr).c_str());
nDirty++; nDirty++;
} }

2
db.h
View file

@ -123,6 +123,7 @@ public:
int nTracked; int nTracked;
int nNew; int nNew;
int nGood; int nGood;
int nAge;
}; };
// seen nodes // seen nodes
@ -164,6 +165,7 @@ public:
stats.nTracked = ourId.size(); stats.nTracked = ourId.size();
stats.nGood = goodId.size(); stats.nGood = goodId.size();
stats.nNew = unkId.size(); stats.nNew = unkId.size();
stats.nAge = time(NULL) - idToInfo[ourId[0]].ourLastTry;
} }
} }

5
dns.c
View file

@ -261,7 +261,7 @@ ssize_t static dnshandle(dns_opt_t *opt, const unsigned char *inbuf, size_t insi
unsigned char *outpos = outbuf+(inpos-inbuf); unsigned char *outpos = outbuf+(inpos-inbuf);
unsigned char *outend = outbuf + BUFLEN; unsigned char *outend = outbuf + BUFLEN;
printf("DNS: Request host='%s' type=%i class=%i\n", name, typ, cls); // printf("DNS: Request host='%s' type=%i class=%i\n", name, typ, cls);
// calculate size of authority section // calculate size of authority section
@ -348,7 +348,8 @@ int dnsserver(dns_opt_t *opt) {
do { do {
ssize_t insize = recvfrom(s, inbuf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen); ssize_t insize = recvfrom(s, inbuf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen);
unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr; unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr;
printf("DNS: Request %llu from %i.%i.%i.%i:%i of %i bytes\n", (unsigned long long)(++opt->nRequests), addr[0], addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize); // printf("DNS: Request %llu from %i.%i.%i.%i:%i of %i bytes\n", (unsigned long long)(opt->nRequests), addr[0], addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize);
opt->nRequests++;
if (insize > 0) { if (insize > 0) {
ssize_t ret = dnshandle(opt, inbuf, insize, outbuf); ssize_t ret = dnshandle(opt, inbuf, insize, outbuf);
if (ret > 0) if (ret > 0)

View file

@ -65,7 +65,7 @@ extern "C" void* ThreadDNS(void*) {
dns_opt.datattl = 60; dns_opt.datattl = 60;
dns_opt.nsttl = 40000; dns_opt.nsttl = 40000;
dns_opt.cb = GetIPList; dns_opt.cb = GetIPList;
dns_opt.port = 53; dns_opt.port = 5353;
dns_opt.nRequests = 0; dns_opt.nRequests = 0;
dnsserver(&dns_opt); dnsserver(&dns_opt);
} }
@ -87,8 +87,9 @@ extern "C" void* ThreadStats(void*) {
do { do {
CAddrDbStats stats; CAddrDbStats stats;
db.GetStats(stats); db.GetStats(stats);
printf("*** %i available (%i tracked, %i new, %i active), %i banned; %i good; %llu DNS requests\n", stats.nAvail, stats.nTracked, stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, stats.nBanned, stats.nGood, (unsigned long long)dns_opt.nRequests); printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
Sleep(10000); printf("*** %i available (%i tracked (%is old), %i new, %i active), %i banned; %i good; %llu DNS requests", stats.nAvail, stats.nTracked, stats.nAge, stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, stats.nBanned, stats.nGood, (unsigned long long)dns_opt.nRequests);
Sleep(1000);
} while(1); } while(1);
} }
@ -108,6 +109,7 @@ extern "C" void* ThreadSeeder(void*) {
} }
int main(void) { int main(void) {
setbuf(stdout, NULL);
FILE *f = fopen("dnsseed.dat","r"); FILE *f = fopen("dnsseed.dat","r");
if (f) { if (f) {
CAutoFile cf(f); CAutoFile cf(f);

View file

@ -178,7 +178,7 @@ bool CIPPort::ConnectSocket(SOCKET& hSocketRet, int nTimeout) const
SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSocket == INVALID_SOCKET) if (hSocket == INVALID_SOCKET)
{ {
printf("Failed to create socket: %s\n", strerror(errno)); // printf("Failed to create socket: %s\n", strerror(errno));
return false; return false;
} }
@ -198,7 +198,7 @@ bool CIPPort::ConnectSocket(SOCKET& hSocketRet, int nTimeout) const
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1) if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
#endif #endif
{ {
printf("Failed to set socket NONBLOCK\n"); // printf("Failed to set socket NONBLOCK\n");
closesocket(hSocket); closesocket(hSocket);
} }
@ -218,13 +218,13 @@ bool CIPPort::ConnectSocket(SOCKET& hSocketRet, int nTimeout) const
int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout); int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
if (nRet == 0) if (nRet == 0)
{ {
printf("connection timeout\n"); // printf("connection timeout\n");
closesocket(hSocket); closesocket(hSocket);
return false; return false;
} }
if (nRet == SOCKET_ERROR) if (nRet == SOCKET_ERROR)
{ {
printf("select() for connection failed: %s\n",strerror(WSAGetLastError())); // printf("select() for connection failed: %s\n",strerror(WSAGetLastError()));
closesocket(hSocket); closesocket(hSocket);
return false; return false;
} }
@ -235,13 +235,13 @@ bool CIPPort::ConnectSocket(SOCKET& hSocketRet, int nTimeout) const
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR) if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
#endif #endif
{ {
printf("getsockopt() for connection failed: %s\n",strerror(WSAGetLastError())); // printf("getsockopt() for connection failed: %s\n",strerror(WSAGetLastError()));
closesocket(hSocket); closesocket(hSocket);
return false; return false;
} }
if (nRet != 0) if (nRet != 0)
{ {
printf("connect() failed after select(): %s\n",strerror(nRet)); // printf("connect() failed after select(): %s\n",strerror(nRet));
closesocket(hSocket); closesocket(hSocket);
return false; return false;
} }
@ -252,7 +252,7 @@ bool CIPPort::ConnectSocket(SOCKET& hSocketRet, int nTimeout) const
else else
#endif #endif
{ {
printf("connect() failed: %i\n",WSAGetLastError()); // printf("connect() failed: %i\n",WSAGetLastError());
closesocket(hSocket); closesocket(hSocket);
return false; return false;
} }
@ -269,7 +269,7 @@ bool CIPPort::ConnectSocket(SOCKET& hSocketRet, int nTimeout) const
if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR) if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
#endif #endif
{ {
printf("Failed to set socket blocking\n"); // printf("Failed to set socket blocking\n");
closesocket(hSocket); closesocket(hSocket);
return false; return false;
} }
@ -571,7 +571,7 @@ int64 CIP::GetHash() const
void CIP::print() const void CIP::print() const
{ {
printf("CIP(%s)\n", ToString().c_str()); // printf("CIP(%s)\n", ToString().c_str());
} }
void CIPPort::Init() void CIPPort::Init()
@ -699,7 +699,7 @@ std::string CIPPort::ToString() const
void CIPPort::print() const void CIPPort::print() const
{ {
printf("CIPPort(%s)\n", ToString().c_str()); // printf("CIPPort(%s)\n", ToString().c_str());
} }
void CIPPort::SetPort(unsigned short portIn) void CIPPort::SetPort(unsigned short portIn)