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; }
void CAddrDb::Add_(const CAddress &addr) { if (!addr.IsRoutable()) return; CIPPort ipp(addr); if (banned.count(ipp)) { time_t bantime = banned[ipp]; if (bantime < time(NULL) && addr.nTime > bantime) banned.erase(ipp); else return; } if (ipToId.count(ipp)) { CAddrInfo &ai = idToInfo[ipToId[ipp]]; if (addr.nTime > ai.lastTry) ai.lastTry = addr.nTime; ai.services |= addr.nServices; printf("%s: updated\n", addr.ToString().c_str()); return; } CAddrInfo ai; ai.ip = ipp; ai.services = addr.nServices; ai.lastTry = addr.nTime; ai.ourLastTry = 0; ai.reliability = 0; ai.weight = 0; ai.total = 0; ai.success = 0; int id = nId++; idToInfo[id] = ai; ipToId[ipp] = id; printf("%s: added as id %i\n", ipp.ToString().c_str(), ipToId[ipp]); unkId.insert(id); }
bool GetMyExternalIP2(const CAddress& addrConnect, const char* pszGet, const char* pszKeyword, unsigned int& ipRet) { SOCKET hSocket; if (!ConnectSocket(addrConnect, hSocket)) return error("GetMyExternalIP() : connection to %s failed", addrConnect.ToString().c_str()); send(hSocket, pszGet, strlen(pszGet), MSG_NOSIGNAL); string strLine; while (RecvLine(hSocket, strLine)) { if (strLine.empty()) // HTTP response is separated from headers by blank line { loop { if (!RecvLine(hSocket, strLine)) { closesocket(hSocket); return false; } if (pszKeyword == NULL) break; if (strLine.find(pszKeyword) != -1) { strLine = strLine.substr(strLine.find(pszKeyword) + strlen(pszKeyword)); break; } } closesocket(hSocket); if (strLine.find("<") != -1) strLine = strLine.substr(0, strLine.find("<")); strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r")); while (strLine.size() > 0 && isspace(strLine[strLine.size()-1])) strLine.resize(strLine.size()-1); CAddress addr(strLine.c_str()); printf("GetMyExternalIP() received [%s] %s\n", strLine.c_str(), addr.ToString().c_str()); if (addr.ip == 0 || addr.ip == INADDR_NONE || !addr.IsRoutable()) return false; ipRet = addr.ip; return true; } }
void ThreadIRCSeed2(void* parg) { // Don't connect to IRC if we won't use IPv4 connections. if (IsLimited(NET_IPV4)) return; // ... or if we won't make outbound connections and won't accept inbound ones. if (mapArgs.count("-connect") && fNoListen) return; // ... or if IRC is not enabled. if (!GetBoolArg("-irc", false)) return; printf("ThreadIRCSeed started\n"); int nErrorWait = 10; int nRetryWait = 10; int nNameRetry = 0; while (!fShutdown) { CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org CService addrIRC("irc.lfnet.org", 6667, true); if (addrIRC.IsValid()) addrConnect = addrIRC; SOCKET hSocket; if (!ConnectSocket(addrConnect, hSocket)) { printf("IRC connect failed\n"); nErrorWait = nErrorWait * 11 / 10; if (Wait(nErrorWait += 60)) continue; else return; } if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname")) { closesocket(hSocket); hSocket = INVALID_SOCKET; nErrorWait = nErrorWait * 11 / 10; if (Wait(nErrorWait += 60)) continue; else return; } CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses CService addrLocal; string strMyName; // Don't use our IP as our nick if we're not listening // or if it keeps failing because the nick is already in use. if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3) strMyName = EncodeAddress(GetLocalAddress(&addrConnect)); if (strMyName == "") strMyName = strprintf("x%"PRIu64"", GetRand(1000000000)); Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str()); int nRet = RecvUntil(hSocket, " 004 ", " 433 "); if (nRet != 1) { closesocket(hSocket); hSocket = INVALID_SOCKET; if (nRet == 2) { printf("IRC name already in use\n"); nNameRetry++; Wait(10); continue; } nErrorWait = nErrorWait * 11 / 10; if (Wait(nErrorWait += 60)) continue; else return; } nNameRetry = 0; MilliSleep(500); // Get our external IP from the IRC server and re-nick before joining the channel CNetAddr addrFromIRC; if (GetIPFromIRC(hSocket, strMyName, addrFromIRC)) { printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str()); // Don't use our IP as our nick if we're not listening if (!fNoListen && addrFromIRC.IsRoutable()) { // IRC lets you to re-nick AddLocal(addrFromIRC, LOCAL_IRC); strMyName = EncodeAddress(GetLocalAddress(&addrConnect)); Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); } } if (fTestNet) { Send(hSocket, "JOIN #WebcoinTEST\r"); Send(hSocket, "WHO #WebcoinTEST\r"); } else { // randomly join #Webcoin00-#Webcoin05 //int channel_number = GetRandInt(5); int channel_number = 0; // Channel number is always 0 for initial release //int channel_number = 0; Send(hSocket, strprintf("JOIN #Webcoin%02d\r", channel_number).c_str()); Send(hSocket, strprintf("WHO #Webcoin%02d\r", channel_number).c_str()); } int64_t nStart = GetTime(); string strLine; strLine.reserve(10000); while (!fShutdown && RecvLineIRC(hSocket, strLine)) { if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':') continue; vector<string> vWords; ParseString(strLine, ' ', vWords); if (vWords.size() < 2) continue; char pszName[10000]; pszName[0] = '\0'; if (vWords[1] == "352" && vWords.size() >= 8) { // index 7 is limited to 16 characters // could get full length name at index 10, but would be different from join messages strlcpy(pszName, vWords[7].c_str(), sizeof(pszName)); printf("IRC got who\n"); } if (vWords[1] == "JOIN" && vWords[0].size() > 1) { // :[email protected] JOIN :#channelname strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName)); if (strchr(pszName, '!')) *strchr(pszName, '!') = '\0'; printf("IRC got join\n"); } if (pszName[0] == 'u') { CAddress addr; if (DecodeAddress(pszName, addr)) { addr.nTime = GetAdjustedTime(); if (addrman.Add(addr, addrConnect, 51 * 60)) printf("IRC got new address: %s\n", addr.ToString().c_str()); nGotIRCAddresses++; } else { printf("IRC decode failed\n"); } } } closesocket(hSocket); hSocket = INVALID_SOCKET; if (GetTime() - nStart > 20 * 60) { nErrorWait /= 3; nRetryWait /= 3; } nRetryWait = nRetryWait * 11 / 10; if (!Wait(nRetryWait += 60)) return; } }
void ThreadIRCSeed2(void* parg) { /* Dont advertise on IRC if we don't allow incoming connections */ if(mapArgs.count("-connect") || fNoListen) return; if(!GetBoolArg("-irc", false)) return; printf("ThreadIRCSeed started\n"); int nErrorWait = 10; int nRetryWait = 10; bool fNameInUse = false; while(!fShutdown) { CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org CService addrIRC("irc.lfnet.org", 6667, true); if(addrIRC.IsValid()) addrConnect = addrIRC; SOCKET hSocket; if(!ConnectSocket(addrConnect, hSocket)) { printf("IRC connect failed\n"); nErrorWait = nErrorWait * 11 / 10; if(Wait(nErrorWait += 60)) continue; else return; } if(!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname")) { closesocket(hSocket); hSocket = INVALID_SOCKET; nErrorWait = nErrorWait * 11 / 10; if(Wait(nErrorWait += 60)) continue; else return; } string strMyName; if(addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse) strMyName = EncodeAddress(addrLocalHost); else strMyName = strprintf("x%u", GetRand(1000000000)); Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str()); int nRet = RecvUntil(hSocket, " 004 ", " 433 "); if(nRet != 1) { closesocket(hSocket); hSocket = INVALID_SOCKET; if(nRet == 2) { printf("IRC name already in use\n"); fNameInUse = true; Wait(10); continue; } nErrorWait = nErrorWait * 11 / 10; if(Wait(nErrorWait += 60)) continue; else return; } Sleep(500); // Get our external IP from the IRC server and re-nick before joining the channel CNetAddr addrFromIRC; if(GetIPFromIRC(hSocket, strMyName, addrFromIRC)) { printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str()); if(!fUseProxy && addrFromIRC.IsRoutable()) { // IRC lets you to re-nick fGotExternalIP = true; addrLocalHost.SetIP(addrFromIRC); strMyName = EncodeAddress(addrLocalHost); Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); } } if(fTestNet) { Send(hSocket, "JOIN #bitcoinTEST\r"); Send(hSocket, "WHO #bitcoinTEST\r"); } else { // randomly join #bitcoin00-#bitcoin99 int channel_number = GetRandInt(100); Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str()); Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str()); } int64 nStart = GetTime(); string strLine; strLine.reserve(10000); while(!fShutdown && RecvLineIRC(hSocket, strLine)) { if(strLine.empty() || strLine.size() > 900 || strLine[0] != ':') continue; vector<string> vWords; ParseString(strLine, ' ', vWords); if(vWords.size() < 2) continue; char pszName[10000]; pszName[0] = '\0'; if(vWords[1] == "352" && vWords.size() >= 8) { // index 7 is limited to 16 characters // could get full length name at index 10, but would be different from join messages strlcpy(pszName, vWords[7].c_str(), sizeof(pszName)); printf("IRC got who\n"); } if(vWords[1] == "JOIN" && vWords[0].size() > 1) { // :[email protected] JOIN :#channelname strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName)); if(strchr(pszName, '!')) *strchr(pszName, '!') = '\0'; printf("IRC got join\n"); } if(pszName[0] == 'u') { CAddress addr; if(DecodeAddress(pszName, addr)) { addr.nTime = GetAdjustedTime(); if(addrman.Add(addr, addrConnect, 51 * 60)) printf("IRC got new address: %s\n", addr.ToString().c_str()); nGotIRCAddresses++; } else { printf("IRC decode failed\n"); } } } closesocket(hSocket); hSocket = INVALID_SOCKET; if(GetTime() - nStart > 20 * 60) { nErrorWait /= 3; nRetryWait /= 3; } nRetryWait = nRetryWait * 11 / 10; if(!Wait(nRetryWait += 60)) return; } }
bool CAddrMan::Add_(const CAddress& addrIn, const CNetAddr& source, int64_t nTimePenalty) { #ifdef I2PADDRMAN_EXTENSIONS //! We now need to check for an possibly modify the CAddress object for the garliccat field, so we make a local copy CAddress addr = addrIn; /** * Before we can add an address, even before we can test if its Routable, or use the Find command to match correctly, * we need to make sure that any I2P addresses have the GarlicCat field setup correctly in the IP area of the * CNetAddr portion of a given CAddress->CService->CNetAddr object, this should have already been done, but * double checking it here also insures we do not get a polluted b32 hash map */ if( addr.CheckAndSetGarlicCat() ) LogPrint( "addrman", "While adding an i2p destination, did not expect to need the garliccat fixed for %s\n", addr.ToString() ); #endif if( !addr.IsRoutable() ) { LogPrint( "addrman", "While adding an address, did not expect to find it unroutable: %s\n", addr.ToString() ); return false; } bool fNew = false; int nId; /** * Find Matches by CNetAddr objects, and returns the CAddrInfo object it finds, which is fine and what we want normally * however this means the ports can be different (CService), and other details in the CAddress portion, such as nServices * should not simply be 'or'd with what was found, sometimes we have to remove services in the version exchange that * peers report incorrectly, and having the port wrong means when Good_ is called that the objects do not match exactly. */ CAddrInfo* pinfo = Find(addr, &nId); if (pinfo) { // periodically update nTime bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60); int64_t nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60); if (addr.nTime && (!pinfo->nTime || pinfo->nTime < addr.nTime - nUpdateInterval - nTimePenalty)) pinfo->nTime = std::max((int64_t)0, addr.nTime - nTimePenalty); /** * Only do the following, IF the source of this information is the node itself (source), * otherwise we're just constantly changing the details, while getting addresses from peers. * * The call (to addrman.Add()) which puts us here, happens at the end of a version message exchange, * for inbound connections only. * For outbound connections, we only have a call to good, if the connection is made. * Other places addrman.Add() is called is for address seeding and user lookup, see net.cpp for those details */ if( (CNetAddr)addr == source ) { /** * add services, don't just 'or' them in here, hard set them to the correct value * original code: pinfo->nServices |= addr.nServices; * ToDo: Why this and the port value has not been fixed as standard procedure could be investigated in more detail * for now Anoncoin has so many unique problems with these 2 values, this should help correct allot of the * current network issues in regard to the values getting corrected over time. */ if( pinfo->nServices != addr.nServices ) { LogPrint( "addrman", "Updating peer record %s, the services listed needed to be changed. From 0x%016x To 0x%016x\n", pinfo->ToString(), pinfo->nServices, addr.nServices ); pinfo->nServices = addr.nServices; } if( pinfo->GetPort() != addr.GetPort() ) { LogPrint( "addrman", "Updating peer record %s, port %d was wrong, changed it to %d\n", pinfo->ToString(), pinfo->GetPort(), addr.GetPort() ); pinfo->SetPort( addr.GetPort() ); } } // do not update if no new information is present if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime)) return false; // do not update if the entry was already in the "tried" table if (pinfo->fInTried) return false; // do not update if the max reference count is reached if (pinfo->nRefCount == ADDRMAN_NEW_BUCKETS_PER_ADDRESS) return false; // stochastic test: previous nRefCount == N: 2^N times harder to increase it int nFactor = 1; for (int n = 0; n < pinfo->nRefCount; n++) nFactor *= 2; if (nFactor > 1 && (GetRandInt(nFactor) != 0)) return false; } else { pinfo = Create(addr, source, &nId); pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty); nNew++; fNew = true; } int nUBucket = pinfo->GetNewBucket(nKey, source); int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket); if (vvNew[nUBucket][nUBucketPos] != nId) { bool fInsert = vvNew[nUBucket][nUBucketPos] == -1; if (!fInsert) { CAddrInfo& infoExisting = mapInfo[vvNew[nUBucket][nUBucketPos]]; if (infoExisting.IsTerrible() || (infoExisting.nRefCount > 1 && pinfo->nRefCount == 0)) { // Overwrite the existing new table entry. fInsert = true; } } if (fInsert) { ClearNew(nUBucket, nUBucketPos); pinfo->nRefCount++; vvNew[nUBucket][nUBucketPos] = nId; } else { if (pinfo->nRefCount == 0) { Delete(nId); } } } return fNew; }
/** * The only time good is called, is during the version message exchange, for inbound it was added first and any address problems * looked over and fixed. For outbound, what we have, was likely what was used to make the connection, so it must have been good * or this would not have been called. * Still we may have services and port details incorrect in our database, and so any object differences can be corrected, if * a change is made to how this routine is called, from passing a CService to a CAddress object we can have the programmer fix * any problems before making this call, we'll look for, and fix the differences, if they show up here. */ void CAddrMan::Good_(const CAddress& addr, int64_t nTime) { bool fChanged = false; int nId; CAddrInfo* pinfo = Find(addr, &nId); // This matches on the CNetAddr portion of the object only aka the ip/i2p only // if not found, bail out if (!pinfo) { LogPrint( "addrman", "Marking as good failed, expected to find %s\n", addr.ToString() ); return; } CAddrInfo& info = *pinfo; // Only really for convenience, not really needed // check whether we are talking about the exact same CService (that includes the same port) if( (CService)info != (CService)addr) { assert( info.GetPort() != addr.GetPort() ); // The only reason which could cause a mismatch, or we have a serious programming problem LogPrint( "addrman", "While marking %s as good, it was found that port %d does not match our record, now updated.\n", info.ToString(), addr.GetPort() ); info.SetPort( addr.GetPort() ); fChanged = true; } // Ok so now we know that at least the CService details all match, lets check and fix the CAddress service bits for this peer, if its listed wrong, fix it. if( info.nServices != addr.nServices ) { LogPrint( "addrman", "While marking %s as good, the peer services was found to have changed from 0x%016x to 0x%016x, now updated.\n", info.ToString(), info.nServices, addr.nServices ); info.nServices = addr.nServices; fChanged = true; } // update info info.nLastSuccess = nTime; info.nLastTry = nTime; info.nAttempts = 0; // info.nTime = nTime; // nTime is not updated here, to avoid leaking information about // currently-connected peers. // if it is already in the tried set, don't do anything else, except report we were here if(info.fInTried ) { if( !fChanged ) LogPrint( "addrman", "Marked as good peer %s\n", info.ToString() ); return; } // find a bucket it is in now int nRnd = GetRandInt(ADDRMAN_NEW_BUCKET_COUNT); int nUBucket = -1; for (unsigned int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) { int nB = (n + nRnd) % ADDRMAN_NEW_BUCKET_COUNT; int nBpos = info.GetBucketPosition(nKey, true, nB); if (vvNew[nB][nBpos] == nId) { nUBucket = nB; break; } } // if no bucket is found, something bad happened; // TODO: maybe re-add the node, but for now, just bail out if( nUBucket == -1 ) { LogPrint( "addrman", "Fatal error while trying to add %s to tried, bucket not found\n", info.ToString() ); return; } LogPrint("addrman", "Moving %s to tried\n", addr.ToString()); // move nId to the tried tables MakeTried(info, nId); }