From 51d617e70f90a972185917ea0096c1ecef2bfa8b Mon Sep 17 00:00:00 2001 From: Bitflash-sh Date: Fri, 24 Jul 2026 11:18:11 -0300 Subject: Bitflash: fair-launch release Satoshi's bitcoin-0.1.0 reborn with RandomX CPU proof of work, anonymous .btf addressing over Nostr with CGNAT rendezvous, automatic peer discovery, a headless Linux node, and Windows GUI wallet. Fair launch, no premine, 21M cap. --- src/net.cpp | 1207 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1207 insertions(+) create mode 100644 src/net.cpp (limited to 'src/net.cpp') diff --git a/src/net.cpp b/src/net.cpp new file mode 100644 index 0000000..8ab9ccc --- /dev/null +++ b/src/net.cpp @@ -0,0 +1,1207 @@ +// Copyright (c) 2009 Satoshi Nakamoto +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +#include "headers.h" +#ifdef _WIN32 +#include +#endif +#include +#include "btfaddr.h" +#include "btftunnel.h" + +void ThreadMessageHandler2(void* parg); +void ThreadSocketHandler2(void* parg); +void ThreadOpenConnections2(void* parg); + + + + + + +// +// Global state variables +// +bool fClient = false; +uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); +CAddress addrLocalHost(0, DEFAULT_PORT, nLocalServices); +unsigned short nListenPort = DEFAULT_PORT; // local P2P port (tunable via /port) +CNode nodeLocalHost(INVALID_SOCKET, CAddress("127.0.0.1", nLocalServices)); +CNode* pnodeLocalHost = &nodeLocalHost; +bool fShutdown = false; +array vfThreadRunning; +vector vNodes; +CCriticalSection cs_vNodes; +map, CAddress> mapAddresses; +CCriticalSection cs_mapAddresses; +map mapRelay; +deque > vRelayExpiration; +CCriticalSection cs_mapRelay; +map mapAlreadyAskedFor; +string strBtfConnect; // .btf peer to keep connected to (from /connectbtf) + + + +CAddress addrProxy; + +bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet) +{ + hSocketRet = INVALID_SOCKET; + + SOCKET hSocket = socket(AF_INET, SOCK_STREAM, 0); + if (hSocket == INVALID_SOCKET) + return false; + + bool fRoutable = !(addrConnect.GetByte(3) == 10 || (addrConnect.GetByte(3) == 192 && addrConnect.GetByte(2) == 168)); + bool fProxy = (addrProxy.ip && fRoutable); + struct sockaddr_in sockaddr = (fProxy ? addrProxy.GetSockAddr() : addrConnect.GetSockAddr()); + + if (connect(hSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR) + { + closesocket(hSocket); + return false; + } + + if (fProxy) + { + printf("Proxy connecting to %s\n", addrConnect.ToString().c_str()); + char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user"; + memcpy(pszSocks4IP + 2, &addrConnect.port, 2); + memcpy(pszSocks4IP + 4, &addrConnect.ip, 4); + char* pszSocks4 = pszSocks4IP; + int nSize = sizeof(pszSocks4IP); + + int ret = send(hSocket, pszSocks4, nSize, 0); + if (ret != nSize) + { + closesocket(hSocket); + return error("Error sending to proxy\n"); + } + char pchRet[8]; + if (recv(hSocket, pchRet, 8, 0) != 8) + { + closesocket(hSocket); + return error("Error reading proxy response\n"); + } + if (pchRet[1] != 0x5a) + { + closesocket(hSocket); + return error("Proxy returned error %d\n", pchRet[1]); + } + printf("Proxy connection established %s\n", addrConnect.ToString().c_str()); + } + + hSocketRet = hSocket; + return true; +} + + +bool GetMyExternalIP(unsigned int& ipRet) +{ + // The 2009 whatismyip.com service (fixed IP) no longer exists; this avoids + // stalling startup on TCP timeouts. The external IP may come from Nostr + // relays in the future. + return false; + + CAddress addrConnect("72.233.89.199:80"); // whatismyip.com 198-200 + + SOCKET hSocket; + if (!ConnectSocket(addrConnect, hSocket)) + return error("GetMyExternalIP() : connection to %s failed\n", addrConnect.ToString().c_str()); + + char* pszGet = + "GET /automation/n09230945.asp HTTP/1.1\r\n" + "Host: www.whatismyip.com\r\n" + "User-Agent: Bitcoin/0.1\r\n" + "Connection: close\r\n" + "\r\n"; + send(hSocket, pszGet, strlen(pszGet), 0); + + string strLine; + while (RecvLine(hSocket, strLine)) + { + if (strLine.empty()) + { + if (!RecvLine(hSocket, strLine)) + { + closesocket(hSocket); + return false; + } + closesocket(hSocket); + CAddress addr(strLine.c_str()); + printf("GetMyExternalIP() received [%s] %s\n", strLine.c_str(), addr.ToString().c_str()); + if (addr.ip == 0) + return false; + ipRet = addr.ip; + return true; + } + } + closesocket(hSocket); + return error("GetMyExternalIP() : connection closed\n"); +} + + + + + + +bool AddAddress(CAddrDB& addrdb, const CAddress& addr) +{ + if (!addr.IsRoutable()) + return false; + if (addr.ip == addrLocalHost.ip) + return false; + CRITICAL_BLOCK(cs_mapAddresses) + { + map, CAddress>::iterator it = mapAddresses.find(addr.GetKey()); + if (it == mapAddresses.end()) + { + // New address + mapAddresses.insert(make_pair(addr.GetKey(), addr)); + addrdb.WriteAddress(addr); + return true; + } + else + { + CAddress& addrFound = (*it).second; + if ((addrFound.nServices | addr.nServices) != addrFound.nServices) + { + // Services have been added + addrFound.nServices |= addr.nServices; + addrdb.WriteAddress(addrFound); + return true; + } + } + } + return false; +} + + + + + +void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1) +{ + // If the dialog might get closed before the reply comes back, + // call this in the destructor so it doesn't get called after it's deleted. + CRITICAL_BLOCK(cs_vNodes) + { + foreach(CNode* pnode, vNodes) + { + CRITICAL_BLOCK(pnode->cs_mapRequests) + { + for (map::iterator mi = pnode->mapRequests.begin(); mi != pnode->mapRequests.end();) + { + CRequestTracker& tracker = (*mi).second; + if (tracker.fn == fn && tracker.param1 == param1) + pnode->mapRequests.erase(mi++); + else + mi++; + } + } + } + } +} + + + + + + + +// +// Subscription methods for the broadcast and subscription system. +// Channel numbers are message numbers, i.e. MSG_TABLE and MSG_PRODUCT. +// +// The subscription system uses a meet-in-the-middle strategy. +// With 100,000 nodes, if senders broadcast to 1000 random nodes and receivers +// subscribe to 1000 random nodes, 99.995% (1 - 0.99^1000) of messages will get through. +// + +bool AnySubscribed(unsigned int nChannel) +{ + if (pnodeLocalHost->IsSubscribed(nChannel)) + return true; + CRITICAL_BLOCK(cs_vNodes) + foreach(CNode* pnode, vNodes) + if (pnode->IsSubscribed(nChannel)) + return true; + return false; +} + +bool CNode::IsSubscribed(unsigned int nChannel) +{ + if (nChannel >= vfSubscribe.size()) + return false; + return vfSubscribe[nChannel]; +} + +void CNode::Subscribe(unsigned int nChannel, unsigned int nHops) +{ + if (nChannel >= vfSubscribe.size()) + return; + + if (!AnySubscribed(nChannel)) + { + // Relay subscribe + CRITICAL_BLOCK(cs_vNodes) + foreach(CNode* pnode, vNodes) + if (pnode != this) + pnode->PushMessage("subscribe", nChannel, nHops); + } + + vfSubscribe[nChannel] = true; +} + +void CNode::CancelSubscribe(unsigned int nChannel) +{ + if (nChannel >= vfSubscribe.size()) + return; + + // Prevent from relaying cancel if wasn't subscribed + if (!vfSubscribe[nChannel]) + return; + vfSubscribe[nChannel] = false; + + if (!AnySubscribed(nChannel)) + { + // Relay subscription cancel + CRITICAL_BLOCK(cs_vNodes) + foreach(CNode* pnode, vNodes) + if (pnode != this) + pnode->PushMessage("sub-cancel", nChannel); + + // Clear memory, no longer subscribed + if (nChannel == MSG_PRODUCT) + CRITICAL_BLOCK(cs_mapProducts) + mapProducts.clear(); + } +} + + + + + + + + + +CNode* FindNode(unsigned int ip) +{ + CRITICAL_BLOCK(cs_vNodes) + { + foreach(CNode* pnode, vNodes) + if (pnode->addr.ip == ip) + return (pnode); + } + return NULL; +} + +CNode* FindNode(CAddress addr) +{ + CRITICAL_BLOCK(cs_vNodes) + { + foreach(CNode* pnode, vNodes) + if (pnode->addr == addr) + return (pnode); + } + return NULL; +} + +CNode* ConnectNode(CAddress addrConnect, int64 nTimeout) +{ + if (addrConnect.ip == addrLocalHost.ip) + return NULL; + + // Look for an existing connection + CNode* pnode = FindNode(addrConnect.ip); + if (pnode) + { + if (nTimeout != 0) + pnode->AddRef(nTimeout); + else + pnode->AddRef(); + return pnode; + } + + /// debug print + printf("trying %s\n", addrConnect.ToString().c_str()); + + // Connect + SOCKET hSocket; + if (ConnectSocket(addrConnect, hSocket)) + { + /// debug print + printf("connected %s\n", addrConnect.ToString().c_str()); + + // Add node + CNode* pnode = new CNode(hSocket, addrConnect, false); + if (nTimeout != 0) + pnode->AddRef(nTimeout); + else + pnode->AddRef(); + CRITICAL_BLOCK(cs_vNodes) + vNodes.push_back(pnode); + + CRITICAL_BLOCK(cs_mapAddresses) + mapAddresses[addrConnect.GetKey()].nLastFailed = 0; + return pnode; + } + else + { + CRITICAL_BLOCK(cs_mapAddresses) + mapAddresses[addrConnect.GetKey()].nLastFailed = GetTime(); + return NULL; + } +} + +// Synthetic marker address for a tunneled `.btf` peer. The real endpoint is +// unknown by design (the tunnel hides it), so tag the CNode with a non-routable +// 10.x.x.x address (fails IsRoutable, so it is never gossiped as a real peer) +// that is stable per identity, keeping FindNode and the duplicate check working. +static CAddress BtfMarkerAddr(const unsigned char b5[5]) +{ + unsigned int ip; + unsigned char b[4] = { 10, b5[0], b5[1], b5[2] }; + memcpy(&ip, b, 4); + unsigned short port = htons((unsigned short)((b5[3] << 8) | b5[4])); + return CAddress(ip, port, nLocalServices); +} + +// Connect to a peer by its `.btf` address: resolve the self-certified +// descriptor over Nostr, then tunnel through its meeting node with the +// end-to-end channel. Neither side ever learns the other's IP. +CNode* ConnectNodeBtf(const string& strBtfAddr) +{ + unsigned char pk[32]; + if (!btf::ParseAddress(strBtfAddr, pk)) + { + printf("ConnectNodeBtf: invalid address %s\n", strBtfAddr.c_str()); + return NULL; + } + if (BtfLocalAddress() == strBtfAddr) + return NULL; // ourselves + + CAddress addr = BtfMarkerAddr(pk); + CNode* pnode = FindNode(addr.ip); + if (pnode) + { + pnode->AddRef(); + return pnode; + } + + /// debug print + printf("trying %s\n", strBtfAddr.c_str()); + + string strMeeting; + unsigned char enc_pub[32]; + if (!BtfResolve(strBtfAddr, strMeeting, enc_pub)) + { + printf("ConnectNodeBtf: could not resolve a descriptor for %s\n", strBtfAddr.c_str()); + return NULL; + } + size_t colon = strMeeting.rfind(':'); + if (colon == string::npos) + return NULL; + string strHost = strMeeting.substr(0, colon); + int nPort = atoi(strMeeting.substr(colon + 1).c_str()); + if (nPort <= 0 || nPort > 65535) + return NULL; + + btf_socket_t hSocket = btf::BtfClientTunnel(strHost.c_str(), (unsigned short)nPort, pk, enc_pub); + if (hSocket == INVALID_SOCKET) + { + printf("ConnectNodeBtf: tunnel to %s via %s failed\n", strBtfAddr.c_str(), strMeeting.c_str()); + return NULL; + } + + /// debug print + printf("connected %s via rendezvous %s\n", strBtfAddr.c_str(), strMeeting.c_str()); + + // Add node + pnode = new CNode(hSocket, addr, false); + pnode->AddRef(); + CRITICAL_BLOCK(cs_vNodes) + vNodes.push_back(pnode); + return pnode; +} + +// Anonymous inbound listener (this node's `.btf` hidden service). Registers at +// the meeting relay and blocks until a client dials our address; each pairing +// becomes a normal inbound CNode riding the end-to-end channel, then we +// register again for the next caller. +void ThreadBtfAccept(void* parg) +{ + printf("ThreadBtfAccept started\n"); + unsigned char pk[32]; + unsigned char sk[32]; + while (!BtfGetIdentity(pk, sk)) + { + if (fShutdown) + return; + Sleep(5000); + } + loop + { + if (fShutdown) + return; + string strMeeting = strBtfMeetingRelay; + size_t colon = strMeeting.rfind(':'); + if (colon == string::npos) + { + Sleep(10000); // no meeting relay configured + continue; + } + string strHost = strMeeting.substr(0, colon); + int nPort = atoi(strMeeting.substr(colon + 1).c_str()); + + // Blocks until a client is paired to us (or the relay drops us) + btf::RvSocket rv = btf::RvServiceRegister(strHost.c_str(), (unsigned short)nPort, pk); + if (fShutdown) + { + if (rv != btf::RV_INVALID) + btf::RvClose(rv); + return; + } + if (rv == btf::RV_INVALID) + { + Sleep(10000); // relay unreachable; retry + continue; + } + btf_socket_t hSocket = btf::BtfServiceWrap(rv, sk); + if (hSocket == INVALID_SOCKET) + continue; + + // The dialer is anonymous (its pubkey never reaches us), so tag the + // connection with a random marker address. + unsigned char rnd[5]; + RAND_bytes(rnd, sizeof(rnd)); + CAddress addr = BtfMarkerAddr(rnd); + + printf("accepted .btf connection via rendezvous %s\n", strMeeting.c_str()); + CNode* pnode = new CNode(hSocket, addr, true); + pnode->AddRef(); + CRITICAL_BLOCK(cs_vNodes) + vNodes.push_back(pnode); + } +} + +// Keep an outbound connection to a specific `.btf` peer (from /connectbtf) -- +// the rendezvous analogue of addnode. Retries until connected, reconnects if +// the tunnel drops. +void ThreadBtfConnect(void* parg) +{ + printf("ThreadBtfConnect started\n"); + string strAddr = *(string*)parg; + delete (string*)parg; + loop + { + if (fShutdown) + return; + try + { + CNode* pnode = ConnectNodeBtf(strAddr); + if (pnode) + { + if (!pnode->fNetworkNode) + pnode->fNetworkNode = true; // keep the ref from ConnectNodeBtf + else + pnode->Release(); // already pinned; drop the extra ref + } + } + CATCH_PRINT_EXCEPTION("ThreadBtfConnect") + for (int i = 0; i < 30 && !fShutdown; i++) + Sleep(1000); + } +} + +void CNode::Disconnect() +{ + printf("disconnecting node %s\n", addr.ToString().c_str()); + + closesocket(hSocket); + + // All of a nodes broadcasts and subscriptions are automatically torn down + // when it goes down, so a node has to stay up to keep its broadcast going. + + CRITICAL_BLOCK(cs_mapProducts) + for (map::iterator mi = mapProducts.begin(); mi != mapProducts.end();) + AdvertRemoveSource(this, MSG_PRODUCT, 0, (*(mi++)).second); + + // Cancel subscriptions + for (unsigned int nChannel = 0; nChannel < vfSubscribe.size(); nChannel++) + if (vfSubscribe[nChannel]) + CancelSubscribe(nChannel); +} + + + + + + + + + + + + + +void ThreadSocketHandler(void* parg) +{ + IMPLEMENT_RANDOMIZE_STACK(ThreadSocketHandler(parg)); + + loop + { + vfThreadRunning[0] = true; + CheckForShutdown(0); + try + { + ThreadSocketHandler2(parg); + } + CATCH_PRINT_EXCEPTION("ThreadSocketHandler()") + vfThreadRunning[0] = false; + Sleep(5000); + } +} + +void ThreadSocketHandler2(void* parg) +{ + printf("ThreadSocketHandler started\n"); + SOCKET hListenSocket = *(SOCKET*)parg; + list vNodesDisconnected; + int nPrevNodeCount = 0; + + loop + { + // + // Disconnect nodes + // + CRITICAL_BLOCK(cs_vNodes) + { + // Disconnect duplicate connections + map mapFirst; + foreach(CNode* pnode, vNodes) + { + if (pnode->fDisconnect) + continue; + unsigned int ip = pnode->addr.ip; + if (mapFirst.count(ip) && addrLocalHost.ip < ip) + { + // In case two nodes connect to each other at once, + // the lower ip disconnects its outbound connection + CNode* pnodeExtra = mapFirst[ip]; + + if (pnodeExtra->GetRefCount() > (pnodeExtra->fNetworkNode ? 1 : 0)) + swap(pnodeExtra, pnode); + + if (pnodeExtra->GetRefCount() <= (pnodeExtra->fNetworkNode ? 1 : 0)) + { + printf("(%d nodes) disconnecting duplicate: %s\n", vNodes.size(), pnodeExtra->addr.ToString().c_str()); + if (pnodeExtra->fNetworkNode && !pnode->fNetworkNode) + { + pnode->AddRef(); + swap(pnodeExtra->fNetworkNode, pnode->fNetworkNode); + pnodeExtra->Release(); + } + pnodeExtra->fDisconnect = true; + } + } + mapFirst[ip] = pnode; + } + + // Disconnect unused nodes + vector vNodesCopy = vNodes; + foreach(CNode* pnode, vNodesCopy) + { + if (pnode->ReadyToDisconnect() && pnode->vRecv.empty() && pnode->vSend.empty()) + { + // remove from vNodes + vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end()); + pnode->Disconnect(); + + // hold in disconnected pool until all refs are released + pnode->nReleaseTime = max(pnode->nReleaseTime, GetTime() + 5 * 60); + if (pnode->fNetworkNode) + pnode->Release(); + vNodesDisconnected.push_back(pnode); + } + } + + // Delete disconnected nodes + list vNodesDisconnectedCopy = vNodesDisconnected; + foreach(CNode* pnode, vNodesDisconnectedCopy) + { + // wait until threads are done using it + if (pnode->GetRefCount() <= 0) + { + bool fDelete = false; + TRY_CRITICAL_BLOCK(pnode->cs_vSend) + TRY_CRITICAL_BLOCK(pnode->cs_vRecv) + TRY_CRITICAL_BLOCK(pnode->cs_mapRequests) + TRY_CRITICAL_BLOCK(pnode->cs_inventory) + fDelete = true; + if (fDelete) + { + vNodesDisconnected.remove(pnode); + delete pnode; + } + } + } + } + if (vNodes.size() != nPrevNodeCount) + { + nPrevNodeCount = vNodes.size(); + MainFrameRepaint(); + } + + + // + // Find which sockets have data to receive + // + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 50000; // frequency to poll pnode->vSend + + fd_set fdsetRecv; + fd_set fdsetSend; + FD_ZERO(&fdsetRecv); + FD_ZERO(&fdsetSend); + SOCKET hSocketMax = 0; + FD_SET(hListenSocket, &fdsetRecv); + hSocketMax = max(hSocketMax, hListenSocket); + CRITICAL_BLOCK(cs_vNodes) + { + foreach(CNode* pnode, vNodes) + { + FD_SET(pnode->hSocket, &fdsetRecv); + hSocketMax = max(hSocketMax, pnode->hSocket); + TRY_CRITICAL_BLOCK(pnode->cs_vSend) + if (!pnode->vSend.empty()) + FD_SET(pnode->hSocket, &fdsetSend); + } + } + + vfThreadRunning[0] = false; + int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, NULL, &timeout); + vfThreadRunning[0] = true; + CheckForShutdown(0); + if (nSelect == SOCKET_ERROR) + { + int nErr = WSAGetLastError(); + printf("select failed: %d\n", nErr); + for (int i = 0; i <= hSocketMax; i++) + { + FD_SET(i, &fdsetRecv); + FD_SET(i, &fdsetSend); + } + Sleep(timeout.tv_usec/1000); + } + RandAddSeed(); + + //// debug print + //foreach(CNode* pnode, vNodes) + //{ + // printf("vRecv = %-5d ", pnode->vRecv.size()); + // printf("vSend = %-5d ", pnode->vSend.size()); + //} + //printf("\n"); + + + // + // Accept new connections + // + if (FD_ISSET(hListenSocket, &fdsetRecv)) + { + struct sockaddr_in sockaddr; +#ifdef _WIN32 + int len = sizeof(sockaddr); +#else + socklen_t len = sizeof(sockaddr); +#endif + SOCKET hSocket = accept(hListenSocket, (struct sockaddr*)&sockaddr, &len); + CAddress addr(sockaddr); + if (hSocket == INVALID_SOCKET) + { + if (WSAGetLastError() != WSAEWOULDBLOCK) + printf("ERROR ThreadSocketHandler accept failed: %d\n", WSAGetLastError()); + } + else + { + printf("accepted connection from %s\n", addr.ToString().c_str()); + CNode* pnode = new CNode(hSocket, addr, true); + pnode->AddRef(); + CRITICAL_BLOCK(cs_vNodes) + vNodes.push_back(pnode); + } + } + + + // + // Service each socket + // + vector vNodesCopy; + CRITICAL_BLOCK(cs_vNodes) + vNodesCopy = vNodes; + foreach(CNode* pnode, vNodesCopy) + { + CheckForShutdown(0); + SOCKET hSocket = pnode->hSocket; + + // + // Receive + // + if (FD_ISSET(hSocket, &fdsetRecv)) + { + TRY_CRITICAL_BLOCK(pnode->cs_vRecv) + { + CDataStream& vRecv = pnode->vRecv; + unsigned int nPos = vRecv.size(); + + // typical socket buffer is 8K-64K + const unsigned int nBufSize = 0x10000; + vRecv.resize(nPos + nBufSize); + int nBytes = recv(hSocket, &vRecv[nPos], nBufSize, 0); + vRecv.resize(nPos + max(nBytes, 0)); + if (nBytes == 0) + { + // socket closed gracefully + if (!pnode->fDisconnect) + printf("recv: socket closed\n"); + pnode->fDisconnect = true; + } + else if (nBytes < 0) + { + // socket error + int nErr = WSAGetLastError(); + if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS) + { + if (!pnode->fDisconnect) + printf("recv failed: %d\n", nErr); + pnode->fDisconnect = true; + } + } + } + } + + // + // Send + // + if (FD_ISSET(hSocket, &fdsetSend)) + { + TRY_CRITICAL_BLOCK(pnode->cs_vSend) + { + CDataStream& vSend = pnode->vSend; + if (!vSend.empty()) + { + int nBytes = send(hSocket, &vSend[0], vSend.size(), 0); + if (nBytes > 0) + { + vSend.erase(vSend.begin(), vSend.begin() + nBytes); + } + else if (nBytes == 0) + { + if (pnode->ReadyToDisconnect()) + pnode->vSend.clear(); + } + else + { + printf("send error %d\n", nBytes); + if (pnode->ReadyToDisconnect()) + pnode->vSend.clear(); + } + } + } + } + } + + + Sleep(10); + } +} + + + + + + + + + + +void ThreadOpenConnections(void* parg) +{ + IMPLEMENT_RANDOMIZE_STACK(ThreadOpenConnections(parg)); + + loop + { + vfThreadRunning[1] = true; + CheckForShutdown(1); + try + { + ThreadOpenConnections2(parg); + } + CATCH_PRINT_EXCEPTION("ThreadOpenConnections()") + vfThreadRunning[1] = false; + Sleep(5000); + } +} + +void ThreadOpenConnections2(void* parg) +{ + printf("ThreadOpenConnections started\n"); + + // Initiate network connections + const int nMaxConnections = 15; + loop + { + // Wait + vfThreadRunning[1] = false; + Sleep(500); + while (vNodes.size() >= nMaxConnections || vNodes.size() >= mapAddresses.size()) + { + CheckForShutdown(1); + Sleep(2000); + } + vfThreadRunning[1] = true; + CheckForShutdown(1); + + + // Make a list of unique class C's + unsigned char pchIPCMask[4] = { 0xff, 0xff, 0xff, 0x00 }; + unsigned int nIPCMask = *(unsigned int*)pchIPCMask; + vector vIPC; + CRITICAL_BLOCK(cs_mapAddresses) + { + vIPC.reserve(mapAddresses.size()); + unsigned int nPrev = 0; + foreach(const PAIRTYPE(vector, CAddress)& item, mapAddresses) + { + const CAddress& addr = item.second; + if (!addr.IsIPv4()) + continue; + + // Taking advantage of mapAddresses being in sorted order, + // with IPs of the same class C grouped together. + unsigned int ipC = addr.ip & nIPCMask; + if (ipC != nPrev) + vIPC.push_back(nPrev = ipC); + } + } + + // + // The IP selection process is designed to limit vulnerability to address flooding. + // Any class C (a.b.c.?) has an equal chance of being chosen, then an IP is + // chosen within the class C. An attacker may be able to allocate many IPs, but + // they would normally be concentrated in blocks of class C's. They can hog the + // attention within their class C, but not the whole IP address space overall. + // A lone node in a class C will get as much attention as someone holding all 255 + // IPs in another class C. + // + bool fSuccess = false; + int nLimit = vIPC.size(); + while (!fSuccess && nLimit-- > 0) + { + // Choose a random class C + unsigned int ipC = vIPC[GetRand(vIPC.size())]; + + // Organize all addresses in the class C by IP + map > mapIP; + CRITICAL_BLOCK(cs_mapAddresses) + { + unsigned int nDelay = ((30 * 60) << vNodes.size()); + if (nDelay > 8 * 60 * 60) + nDelay = 8 * 60 * 60; + for (map, CAddress>::iterator mi = mapAddresses.lower_bound(CAddress(ipC, 0).GetKey()); + mi != mapAddresses.upper_bound(CAddress(ipC | ~nIPCMask, 0xffff).GetKey()); + ++mi) + { + const CAddress& addr = (*mi).second; + unsigned int nRandomizer = (addr.nLastFailed * addr.ip * 7777U) % 20000; + if (GetTime() - addr.nLastFailed > nDelay * nRandomizer / 10000) + mapIP[addr.ip].push_back(addr); + } + } + if (mapIP.empty()) + break; + + // Choose a random IP in the class C + map >::iterator mi = mapIP.begin(); + advance(mi, GetRand(mapIP.size())); + + // Once we've chosen an IP, we'll try every given port before moving on + foreach(const CAddress& addrConnect, (*mi).second) + { + if (addrConnect.ip == addrLocalHost.ip || !addrConnect.IsIPv4() || FindNode(addrConnect.ip)) + continue; + + CNode* pnode = ConnectNode(addrConnect); + if (!pnode) + continue; + pnode->fNetworkNode = true; + + if (addrLocalHost.IsRoutable()) + { + // Advertise our address + vector vAddrToSend; + vAddrToSend.push_back(addrLocalHost); + pnode->PushMessage("addr", vAddrToSend); + } + + // Get as many addresses as we can + pnode->PushMessage("getaddr"); + + ////// should the one on the receiving end do this too? + // Subscribe our local subscription list + const unsigned int nHops = 0; + for (unsigned int nChannel = 0; nChannel < pnodeLocalHost->vfSubscribe.size(); nChannel++) + if (pnodeLocalHost->vfSubscribe[nChannel]) + pnode->PushMessage("subscribe", nChannel, nHops); + + fSuccess = true; + break; + } + } + } +} + + + + + + + + +void ThreadMessageHandler(void* parg) +{ + IMPLEMENT_RANDOMIZE_STACK(ThreadMessageHandler(parg)); + + loop + { + vfThreadRunning[2] = true; + CheckForShutdown(2); + try + { + ThreadMessageHandler2(parg); + } + CATCH_PRINT_EXCEPTION("ThreadMessageHandler()") + vfThreadRunning[2] = false; + Sleep(5000); + } +} + +void ThreadMessageHandler2(void* parg) +{ + printf("ThreadMessageHandler started\n"); + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL); + loop + { + // Poll the connected nodes for messages + vector vNodesCopy; + CRITICAL_BLOCK(cs_vNodes) + vNodesCopy = vNodes; + foreach(CNode* pnode, vNodesCopy) + { + pnode->AddRef(); + + // Receive messages + TRY_CRITICAL_BLOCK(pnode->cs_vRecv) + ProcessMessages(pnode); + + // Send messages + TRY_CRITICAL_BLOCK(pnode->cs_vSend) + SendMessages(pnode); + + pnode->Release(); + } + + // Wait and allow messages to bunch up + vfThreadRunning[2] = false; + Sleep(100); + vfThreadRunning[2] = true; + CheckForShutdown(2); + } +} + + + + + + + + + +//// todo: start one thread per processor, use getenv("NUMBER_OF_PROCESSORS") +void ThreadBitcoinMiner(void* parg) +{ + vfThreadRunning[3] = true; + CheckForShutdown(3); + try + { + bool fRet = BitcoinMiner(); + printf("BitcoinMiner returned %s\n\n\n", fRet ? "true" : "false"); + } + CATCH_PRINT_EXCEPTION("BitcoinMiner()") + vfThreadRunning[3] = false; +} + + + + + + + + + + + +bool StartNode(string& strError) +{ + strError = ""; + + // Sockets startup + WSADATA wsadata; + int ret = WSAStartup(MAKEWORD(2,2), &wsadata); + if (ret != NO_ERROR) + { + strError = strprintf("Error: TCP/IP socket library failed to start (WSAStartup returned error %d)", ret); + printf("%s\n", strError.c_str()); + return false; + } + + // Get local host ip + char pszHostName[255]; + if (gethostname(pszHostName, 255) == SOCKET_ERROR) + { + strError = strprintf("Error: Unable to get IP address of this computer (gethostname returned error %d)", WSAGetLastError()); + printf("%s\n", strError.c_str()); + return false; + } + struct hostent* pHostEnt = gethostbyname(pszHostName); + if (!pHostEnt) + { + strError = strprintf("Error: Unable to get IP address of this computer (gethostbyname returned error %d)", WSAGetLastError()); + printf("%s\n", strError.c_str()); + return false; + } + addrLocalHost = CAddress(*(long*)(pHostEnt->h_addr_list[0]), + nListenPort, + nLocalServices); + printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str()); + + // Create socket for listening for incoming connections + SOCKET hListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (hListenSocket == INVALID_SOCKET) + { + strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %d)", WSAGetLastError()); + printf("%s\n", strError.c_str()); + return false; + } + + // Set to nonblocking, incoming connections will also inherit this + u_long nOne = 1; + if (ioctlsocket(hListenSocket, FIONBIO, &nOne) == SOCKET_ERROR) + { + strError = strprintf("Error: Couldn't set properties on socket for incoming connections (ioctlsocket returned error %d)", WSAGetLastError()); + printf("%s\n", strError.c_str()); + return false; + } + + // The sockaddr_in structure specifies the address family, + // IP address, and port for the socket that is being bound + int nRetryLimit = 15; + struct sockaddr_in sockaddr = addrLocalHost.GetSockAddr(); + if (bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR) + { + int nErr = WSAGetLastError(); + if (nErr == WSAEADDRINUSE) + strError = strprintf("Error: Unable to bind to port %s on this computer. The program is probably already running.", addrLocalHost.ToString().c_str()); + else + strError = strprintf("Error: Unable to bind to port %s on this computer (bind returned error %d)", addrLocalHost.ToString().c_str(), nErr); + printf("%s\n", strError.c_str()); + return false; + } + printf("bound to addrLocalHost = %s\n\n", addrLocalHost.ToString().c_str()); + + // Listen for incoming connections + if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR) + { + strError = strprintf("Error: Listening for incoming connections failed (listen returned error %d)", WSAGetLastError()); + printf("%s\n", strError.c_str()); + return false; + } + + // Get our external IP address for incoming connections + if (addrIncoming.ip) + addrLocalHost.ip = addrIncoming.ip; + + if (GetMyExternalIP(addrLocalHost.ip)) + { + addrIncoming = addrLocalHost; + CWalletDB().WriteSetting("addrIncoming", addrIncoming); + } + + // Peer discovery over Nostr relays (replaces the old IRC seed) + if (_beginthread(ThreadNostrSeed, 0, NULL) == -1) + printf("Error: _beginthread(ThreadNostrSeed) failed\n"); + + // Anonymous inbound: this node's .btf hidden service, reachable through the + // meeting relay without exposing our IP or needing a public port. + if (!strBtfMeetingRelay.empty()) + if (_beginthread(ThreadBtfAccept, 0, NULL) == -1) + printf("Error: _beginthread(ThreadBtfAccept) failed\n"); + + // Anonymous outbound: keep a connection to a specific .btf peer, if asked. + if (!strBtfConnect.empty()) + if (_beginthread(ThreadBtfConnect, 0, new string(strBtfConnect)) == -1) + printf("Error: _beginthread(ThreadBtfConnect) failed\n"); + + // + // Start threads + // + if (_beginthread(ThreadSocketHandler, 0, new SOCKET(hListenSocket)) == -1) + { + strError = "Error: _beginthread(ThreadSocketHandler) failed"; + printf("%s\n", strError.c_str()); + return false; + } + + if (_beginthread(ThreadOpenConnections, 0, NULL) == -1) + { + strError = "Error: _beginthread(ThreadOpenConnections) failed"; + printf("%s\n", strError.c_str()); + return false; + } + + if (_beginthread(ThreadMessageHandler, 0, NULL) == -1) + { + strError = "Error: _beginthread(ThreadMessageHandler) failed"; + printf("%s\n", strError.c_str()); + return false; + } + + return true; +} + +bool StopNode() +{ + printf("StopNode()\n"); + fShutdown = true; + nTransactionsUpdated++; + while (count(vfThreadRunning.begin(), vfThreadRunning.end(), true)) + Sleep(10); + Sleep(50); + + // Sockets shutdown + WSACleanup(); + return true; +} + +void CheckForShutdown(int n) +{ + if (fShutdown) + { + if (n != -1) + vfThreadRunning[n] = false; + _endthread(); + } +} -- cgit v1.2.3