2011-12-20 05:20:50 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "db.h"
|
|
|
|
#include "netbase.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "serialize.h"
|
|
|
|
#include "uint256.h"
|
|
|
|
|
|
|
|
#define BITCOIN_SEED_NONCE 0x0539a019ca550825
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class CNode {
|
|
|
|
SOCKET sock;
|
|
|
|
CDataStream vSend;
|
|
|
|
CDataStream vRecv;
|
|
|
|
unsigned int nHeaderStart;
|
|
|
|
unsigned int nMessageStart;
|
|
|
|
int nVersion;
|
|
|
|
string strSubVer;
|
|
|
|
int nStartingHeight;
|
|
|
|
vector<CAddress> *vAddr;
|
|
|
|
int ban;
|
|
|
|
int64 doneAfter;
|
|
|
|
CAddress you;
|
|
|
|
|
2012-09-29 18:08:21 +02:00
|
|
|
int GetTimeout() {
|
|
|
|
if (you.IsTor())
|
|
|
|
return 60;
|
|
|
|
else
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
2011-12-20 05:20:50 +01:00
|
|
|
void BeginMessage(const char *pszCommand) {
|
|
|
|
if (nHeaderStart != -1) AbortMessage();
|
|
|
|
nHeaderStart = vSend.size();
|
|
|
|
vSend << CMessageHeader(pszCommand, 0);
|
|
|
|
nMessageStart = vSend.size();
|
2011-12-20 14:29:21 +01:00
|
|
|
// printf("%s: SEND %s\n", ToString(you).c_str(), pszCommand);
|
2011-12-20 05:20:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbortMessage() {
|
|
|
|
if (nHeaderStart == -1) return;
|
|
|
|
vSend.resize(nHeaderStart);
|
|
|
|
nHeaderStart = -1;
|
|
|
|
nMessageStart = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndMessage() {
|
|
|
|
if (nHeaderStart == -1) return;
|
|
|
|
unsigned int nSize = vSend.size() - nMessageStart;
|
|
|
|
memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize));
|
|
|
|
if (vSend.GetVersion() >= 209) {
|
|
|
|
uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end());
|
|
|
|
unsigned int nChecksum = 0;
|
|
|
|
memcpy(&nChecksum, &hash, sizeof(nChecksum));
|
|
|
|
assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum));
|
|
|
|
memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum));
|
|
|
|
}
|
|
|
|
nHeaderStart = -1;
|
|
|
|
nMessageStart = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Send() {
|
|
|
|
if (sock == INVALID_SOCKET) return;
|
|
|
|
if (vSend.empty()) return;
|
|
|
|
int nBytes = send(sock, &vSend[0], vSend.size(), 0);
|
|
|
|
if (nBytes > 0) {
|
|
|
|
vSend.erase(vSend.begin(), vSend.begin() + nBytes);
|
|
|
|
} else {
|
|
|
|
close(sock);
|
|
|
|
sock = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushVersion() {
|
|
|
|
int64 nTime = time(NULL);
|
|
|
|
uint64 nLocalNonce = BITCOIN_SEED_NONCE;
|
|
|
|
int64 nLocalServices = 0;
|
2012-05-25 15:41:27 +02:00
|
|
|
CAddress me(CService("0.0.0.0"));
|
2011-12-20 05:20:50 +01:00
|
|
|
BeginMessage("version");
|
2013-08-09 09:25:54 +02:00
|
|
|
int nBestHeight = GetRequireHeight();
|
2011-12-20 05:20:50 +01:00
|
|
|
string ver = "/bitcoin-seeder:0.01/";
|
|
|
|
vSend << PROTOCOL_VERSION << nLocalServices << nTime << you << me << nLocalNonce << ver << nBestHeight;
|
|
|
|
EndMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GotVersion() {
|
2012-02-23 18:01:09 +01:00
|
|
|
// printf("\n%s: version %i\n", ToString(you).c_str(), nVersion);
|
2013-04-25 01:55:26 +02:00
|
|
|
if (vAddr) {
|
|
|
|
BeginMessage("getaddr");
|
|
|
|
EndMessage();
|
|
|
|
doneAfter = time(NULL) + GetTimeout();
|
|
|
|
} else {
|
|
|
|
doneAfter = time(NULL) + 1;
|
|
|
|
}
|
2011-12-20 05:20:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessMessage(string strCommand, CDataStream& vRecv) {
|
2011-12-20 14:29:21 +01:00
|
|
|
// printf("%s: RECV %s\n", ToString(you).c_str(), strCommand.c_str());
|
2011-12-20 05:20:50 +01:00
|
|
|
if (strCommand == "version") {
|
|
|
|
int64 nTime;
|
|
|
|
CAddress addrMe;
|
|
|
|
CAddress addrFrom;
|
|
|
|
uint64 nNonce = 1;
|
|
|
|
vRecv >> nVersion >> you.nServices >> nTime >> addrMe;
|
|
|
|
if (nVersion == 10300) nVersion = 300;
|
|
|
|
if (nVersion >= 106 && !vRecv.empty())
|
|
|
|
vRecv >> addrFrom >> nNonce;
|
|
|
|
if (nVersion >= 106 && !vRecv.empty())
|
|
|
|
vRecv >> strSubVer;
|
|
|
|
if (nVersion >= 209 && !vRecv.empty())
|
|
|
|
vRecv >> nStartingHeight;
|
2013-04-15 04:03:25 +02:00
|
|
|
|
2011-12-20 05:20:50 +01:00
|
|
|
if (nVersion >= 209) {
|
|
|
|
BeginMessage("verack");
|
|
|
|
EndMessage();
|
|
|
|
}
|
|
|
|
vSend.SetVersion(min(nVersion, PROTOCOL_VERSION));
|
|
|
|
if (nVersion < 209) {
|
|
|
|
this->vRecv.SetVersion(min(nVersion, PROTOCOL_VERSION));
|
|
|
|
GotVersion();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strCommand == "verack") {
|
|
|
|
this->vRecv.SetVersion(min(nVersion, PROTOCOL_VERSION));
|
|
|
|
GotVersion();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-25 01:55:26 +02:00
|
|
|
if (strCommand == "addr" && vAddr) {
|
2011-12-20 05:20:50 +01:00
|
|
|
vector<CAddress> vAddrNew;
|
|
|
|
vRecv >> vAddrNew;
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: got %i addresses\n", ToString(you).c_str(), (int)vAddrNew.size());
|
2011-12-20 05:20:50 +01:00
|
|
|
int64 now = time(NULL);
|
|
|
|
vector<CAddress>::iterator it = vAddrNew.begin();
|
|
|
|
if (doneAfter == 0 || doneAfter > now + 1) doneAfter = now + 1;
|
|
|
|
while (it != vAddrNew.end()) {
|
|
|
|
CAddress &addr = *it;
|
2011-12-20 14:29:21 +01:00
|
|
|
// printf("%s: got address %s\n", ToString(you).c_str(), addr.ToString().c_str(), (int)(vAddr->size()));
|
2011-12-20 05:20:50 +01:00
|
|
|
it++;
|
|
|
|
if (addr.nTime <= 100000000 || addr.nTime > now + 600)
|
|
|
|
addr.nTime = now - 5 * 86400;
|
2012-05-30 16:51:44 +02:00
|
|
|
if (addr.nTime > now - 604800)
|
|
|
|
vAddr->push_back(addr);
|
2011-12-20 14:29:21 +01:00
|
|
|
// printf("%s: added address %s (#%i)\n", ToString(you).c_str(), addr.ToString().c_str(), (int)(vAddr->size()));
|
2011-12-20 05:20:50 +01:00
|
|
|
if (vAddr->size() > 1000) {doneAfter = 1; return true; }
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessMessages() {
|
|
|
|
if (vRecv.empty()) return false;
|
|
|
|
do {
|
|
|
|
CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), END(pchMessageStart));
|
|
|
|
int nHeaderSize = vRecv.GetSerializeSize(CMessageHeader());
|
|
|
|
if (vRecv.end() - pstart < nHeaderSize) {
|
|
|
|
if (vRecv.size() > nHeaderSize) {
|
|
|
|
vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vRecv.erase(vRecv.begin(), pstart);
|
|
|
|
vector<char> vHeaderSave(vRecv.begin(), vRecv.begin() + nHeaderSize);
|
|
|
|
CMessageHeader hdr;
|
|
|
|
vRecv >> hdr;
|
|
|
|
if (!hdr.IsValid()) {
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: BAD (invalid header)\n", ToString(you).c_str());
|
2011-12-20 05:20:50 +01:00
|
|
|
ban = 100000; return true;
|
|
|
|
}
|
|
|
|
string strCommand = hdr.GetCommand();
|
|
|
|
unsigned int nMessageSize = hdr.nMessageSize;
|
|
|
|
if (nMessageSize > MAX_SIZE) {
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: BAD (message too large)\n", ToString(you).c_str());
|
2011-12-20 05:20:50 +01:00
|
|
|
ban = 100000;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nMessageSize > vRecv.size()) {
|
|
|
|
vRecv.insert(vRecv.begin(), vHeaderSave.begin(), vHeaderSave.end());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (vRecv.GetVersion() >= 209) {
|
|
|
|
uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
|
|
|
|
unsigned int nChecksum = 0;
|
|
|
|
memcpy(&nChecksum, &hash, sizeof(nChecksum));
|
|
|
|
if (nChecksum != hdr.nChecksum) continue;
|
|
|
|
}
|
|
|
|
CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion);
|
|
|
|
vRecv.ignore(nMessageSize);
|
|
|
|
if (ProcessMessage(strCommand, vMsg))
|
|
|
|
return true;
|
2011-12-20 14:29:21 +01:00
|
|
|
// printf("%s: done processing %s\n", ToString(you).c_str(), strCommand.c_str());
|
2011-12-20 05:20:50 +01:00
|
|
|
} while(1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-04-25 01:55:26 +02:00
|
|
|
CNode(const CService& ip, vector<CAddress>* vAddrIn) : you(ip), nHeaderStart(-1), nMessageStart(-1), vAddr(vAddrIn), ban(0), doneAfter(0), nVersion(0) {
|
2011-12-20 05:20:50 +01:00
|
|
|
vSend.SetType(SER_NETWORK);
|
|
|
|
vSend.SetVersion(0);
|
|
|
|
vRecv.SetType(SER_NETWORK);
|
|
|
|
vRecv.SetVersion(0);
|
|
|
|
if (time(NULL) > 1329696000) {
|
|
|
|
vSend.SetVersion(209);
|
|
|
|
vRecv.SetVersion(209);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool Run() {
|
|
|
|
bool res = true;
|
2012-05-25 15:41:27 +02:00
|
|
|
if (!ConnectSocket(you, sock)) return false;
|
2011-12-20 05:20:50 +01:00
|
|
|
PushVersion();
|
|
|
|
Send();
|
|
|
|
int64 now;
|
|
|
|
while (now = time(NULL), ban == 0 && (doneAfter == 0 || doneAfter > now) && sock != INVALID_SOCKET) {
|
|
|
|
char pchBuf[0x10000];
|
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(sock,&set);
|
|
|
|
struct timeval wa;
|
|
|
|
if (doneAfter) {
|
|
|
|
wa.tv_sec = doneAfter - now;
|
|
|
|
wa.tv_usec = 0;
|
|
|
|
} else {
|
2012-09-29 18:08:21 +02:00
|
|
|
wa.tv_sec = GetTimeout();
|
2011-12-20 05:20:50 +01:00
|
|
|
wa.tv_usec = 0;
|
|
|
|
}
|
|
|
|
int ret = select(sock+1, &set, NULL, &set, &wa);
|
|
|
|
if (ret != 1) {
|
|
|
|
if (!doneAfter) res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int nBytes = recv(sock, pchBuf, sizeof(pchBuf), 0);
|
|
|
|
int nPos = vRecv.size();
|
|
|
|
if (nBytes > 0) {
|
|
|
|
vRecv.resize(nPos + nBytes);
|
|
|
|
memcpy(&vRecv[nPos], pchBuf, nBytes);
|
|
|
|
} else if (nBytes == 0) {
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: BAD (connection closed prematurely)\n", ToString(you).c_str());
|
2011-12-20 14:29:21 +01:00
|
|
|
res = false;
|
|
|
|
break;
|
2011-12-20 05:20:50 +01:00
|
|
|
} else {
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: BAD (connection error)\n", ToString(you).c_str());
|
2011-12-20 14:29:21 +01:00
|
|
|
res = false;
|
2011-12-20 05:20:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ProcessMessages();
|
|
|
|
Send();
|
|
|
|
}
|
2011-12-20 06:42:48 +01:00
|
|
|
if (sock == INVALID_SOCKET) res = false;
|
2011-12-20 05:20:50 +01:00
|
|
|
close(sock);
|
|
|
|
sock = INVALID_SOCKET;
|
|
|
|
return (ban == 0) && res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetBan() {
|
|
|
|
return ban;
|
|
|
|
}
|
2011-12-23 02:43:32 +01:00
|
|
|
|
|
|
|
int GetClientVersion() {
|
|
|
|
return nVersion;
|
|
|
|
}
|
2012-02-23 18:01:09 +01:00
|
|
|
|
|
|
|
std::string GetClientSubVersion() {
|
|
|
|
return strSubVer;
|
|
|
|
}
|
2012-05-25 16:05:14 +02:00
|
|
|
|
|
|
|
int GetStartingHeight() {
|
|
|
|
return nStartingHeight;
|
|
|
|
}
|
2011-12-20 05:20:50 +01:00
|
|
|
};
|
|
|
|
|
2013-04-25 01:55:26 +02:00
|
|
|
bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, vector<CAddress>* vAddr) {
|
2012-01-10 01:20:22 +01:00
|
|
|
try {
|
|
|
|
CNode node(cip, vAddr);
|
|
|
|
bool ret = node.Run();
|
|
|
|
if (!ret) {
|
|
|
|
ban = node.GetBan();
|
|
|
|
} else {
|
|
|
|
ban = 0;
|
|
|
|
}
|
|
|
|
clientV = node.GetClientVersion();
|
2012-02-23 18:01:09 +01:00
|
|
|
clientSV = node.GetClientSubVersion();
|
2012-05-25 16:05:14 +02:00
|
|
|
blocks = node.GetStartingHeight();
|
2012-01-10 01:20:22 +01:00
|
|
|
// printf("%s: %s!!!\n", cip.ToString().c_str(), ret ? "GOOD" : "BAD");
|
|
|
|
return ret;
|
|
|
|
} catch(std::ios_base::failure& e) {
|
2011-12-20 05:20:50 +01:00
|
|
|
ban = 0;
|
2012-01-10 01:20:22 +01:00
|
|
|
return false;
|
2011-12-20 05:20:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
int main(void) {
|
2012-05-25 15:41:27 +02:00
|
|
|
CService ip("bitcoin.sipa.be", 8333, true);
|
2011-12-20 05:20:50 +01:00
|
|
|
vector<CAddress> vAddr;
|
|
|
|
vAddr.clear();
|
|
|
|
int ban = 0;
|
|
|
|
bool ret = TestNode(ip, ban, vAddr);
|
|
|
|
printf("ret=%s ban=%i vAddr.size()=%i\n", ret ? "good" : "bad", ban, (int)vAddr.size());
|
|
|
|
}
|
|
|
|
*/
|
2011-12-20 14:29:21 +01:00
|
|
|
|