Bitcoin v0.3.19 · 2010-12-13
irc.cpp
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45#include "headers.h"67int nGotIRCAddresses = 0;89void ThreadIRCSeed2(void* parg);1011121314#pragma pack(push, 1)15struct ircaddr16{17 int ip;18 short port;19};20#pragma pack(pop)2122string EncodeAddress(const CAddress& addr)23{24 struct ircaddr tmp;25 tmp.ip = addr.ip;26 tmp.port = addr.port;2728 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));29 return string("u") + EncodeBase58Check(vch);30}3132bool DecodeAddress(string str, CAddress& addr)33{34 vector<unsigned char> vch;35 if (!DecodeBase58Check(str.substr(1), vch))36 return false;3738 struct ircaddr tmp;39 if (vch.size() != sizeof(tmp))40 return false;41 memcpy(&tmp, &vch[0], sizeof(tmp));4243 addr = CAddress(tmp.ip, tmp.port, NODE_NETWORK);44 return true;45}46474849505152static bool Send(SOCKET hSocket, const char* pszSend)53{54 if (strstr(pszSend, "PONG") != pszSend)55 printf("IRC SENDING: %s\n", pszSend);56 const char* psz = pszSend;57 const char* pszEnd = psz + strlen(psz);58 while (psz < pszEnd)59 {60 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);61 if (ret < 0)62 return false;63 psz += ret;64 }65 return true;66}6768bool RecvLine(SOCKET hSocket, string& strLine)69{70 strLine = "";71 loop72 {73 char c;74 int nBytes = recv(hSocket, &c, 1, 0);75 if (nBytes > 0)76 {77 if (c == '\n')78 continue;79 if (c == '\r')80 return true;81 strLine += c;82 if (strLine.size() >= 9000)83 return true;84 }85 else if (nBytes <= 0)86 {87 if (fShutdown)88 return false;89 if (nBytes < 0)90 {91 int nErr = WSAGetLastError();92 if (nErr == WSAEMSGSIZE)93 continue;94 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)95 {96 Sleep(10);97 continue;98 }99 }100 if (!strLine.empty())101 return true;102 if (nBytes == 0)103 {104 // socket closed105 printf("IRC socket closed\n");106 return false;107 }108 else109 {110 // socket error111 int nErr = WSAGetLastError();112 printf("IRC recv failed: %d\n", nErr);113 return false;114 }115 }116 }117}118119bool RecvLineIRC(SOCKET hSocket, string& strLine)120{121 loop122 {123 bool fRet = RecvLine(hSocket, strLine);124 if (fRet)125 {126 if (fShutdown)127 return false;128 vector<string> vWords;129 ParseString(strLine, ' ', vWords);130 if (vWords.size() >= 1 && vWords[0] == "PING")131 {132 strLine[1] = 'O';133 strLine += '\r';134 Send(hSocket, strLine.c_str());135 continue;136 }137 }138 return fRet;139 }140}141142int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)143{144 loop145 {146 string strLine;147 strLine.reserve(10000);148 if (!RecvLineIRC(hSocket, strLine))149 return 0;150 printf("IRC %s\n", strLine.c_str());151 if (psz1 && strLine.find(psz1) != -1)152 return 1;153 if (psz2 && strLine.find(psz2) != -1)154 return 2;155 if (psz3 && strLine.find(psz3) != -1)156 return 3;157 if (psz4 && strLine.find(psz4) != -1)158 return 4;159 }160}161162bool Wait(int nSeconds)163{164 if (fShutdown)165 return false;166 printf("IRC waiting %d seconds to reconnect\n", nSeconds);167 for (int i = 0; i < nSeconds; i++)168 {169 if (fShutdown)170 return false;171 Sleep(1000);172 }173 return true;174}175176bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)177{178 strRet.clear();179 loop180 {181 string strLine;182 if (!RecvLineIRC(hSocket, strLine))183 return false;184185 vector<string> vWords;186 ParseString(strLine, ' ', vWords);187 if (vWords.size() < 2)188 continue;189190 if (vWords[1] == psz1)191 {192 printf("IRC %s\n", strLine.c_str());193 strRet = strLine;194 return true;195 }196 }197}198199bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)200{201 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());202203 string strLine;204 if (!RecvCodeLine(hSocket, "302", strLine))205 return false;206207 vector<string> vWords;208 ParseString(strLine, ' ', vWords);209 if (vWords.size() < 4)210 return false;211212 string str = vWords[3];213 if (str.rfind("@") == string::npos)214 return false;215 string strHost = str.substr(str.rfind("@")+1);216217 unsigned int a=0, b=0, c=0, d=0;218 if (sscanf(strHost.c_str(), "%u.%u.%u.%u", &a, &b, &c, &d) == 4 &&219 inet_addr(strHost.c_str()) != INADDR_NONE)220 {221 printf("GetIPFromIRC() userhost is IP %s\n", strHost.c_str());222 ipRet = CAddress(strHost).ip;223 }224 else225 {226 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());227 if (fUseProxy)228 return false;229 struct hostent* phostent = gethostbyname(strHost.c_str());230 if (!phostent || !phostent->h_addr_list || !phostent->h_addr_list[0])231 return false;232 ipRet = *(u_long*)phostent->h_addr_list[0];233 }234235 return true;236}237238239240void ThreadIRCSeed(void* parg)241{242 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));243 try244 {245 ThreadIRCSeed2(parg);246 }247 catch (std::exception& e) {248 PrintExceptionContinue(&e, "ThreadIRCSeed()");249 } catch (...) {250 PrintExceptionContinue(NULL, "ThreadIRCSeed()");251 }252 printf("ThreadIRCSeed exiting\n");253}254255void ThreadIRCSeed2(void* parg)256{257 if (mapArgs.count("-connect"))258 return;259 if (GetBoolArg("-noirc"))260 return;261 printf("ThreadIRCSeed started\n");262 int nErrorWait = 10;263 int nRetryWait = 10;264 bool fNameInUse = false;265 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));266267 while (!fShutdown)268 {269 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net270 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org271 if (!fTOR)272 {273 //struct hostent* phostent = gethostbyname("chat.freenode.net");274 struct hostent* phostent = gethostbyname("irc.lfnet.org");275 if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])276 addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));277 }278279 SOCKET hSocket;280 if (!ConnectSocket(addrConnect, hSocket))281 {282 printf("IRC connect failed\n");283 nErrorWait = nErrorWait * 11 / 10;284 if (Wait(nErrorWait += 60))285 continue;286 else287 return;288 }289290 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))291 {292 closesocket(hSocket);293 hSocket = INVALID_SOCKET;294 nErrorWait = nErrorWait * 11 / 10;295 if (Wait(nErrorWait += 60))296 continue;297 else298 return;299 }300301 string strMyName;302 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)303 strMyName = EncodeAddress(addrLocalHost);304 else305 strMyName = strprintf("x%u", GetRand(1000000000));306307 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());308 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());309310 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");311 if (nRet != 1)312 {313 closesocket(hSocket);314 hSocket = INVALID_SOCKET;315 if (nRet == 2)316 {317 printf("IRC name already in use\n");318 fNameInUse = true;319 Wait(10);320 continue;321 }322 nErrorWait = nErrorWait * 11 / 10;323 if (Wait(nErrorWait += 60))324 continue;325 else326 return;327 }328 Sleep(500);329330 // Get my external IP from IRC server331 CAddress addrFromIRC;332 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))333 {334 // Just using it as a backup for now335 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());336 if (addrFromIRC.IsRoutable() && !fUseProxy && !addrLocalHost.IsRoutable())337 {338 addrLocalHost.ip = addrFromIRC.ip;339 strMyName = EncodeAddress(addrLocalHost);340 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());341 }342 }343344 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");345 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");346347 int64 nStart = GetTime();348 string strLine;349 strLine.reserve(10000);350 while (!fShutdown && RecvLineIRC(hSocket, strLine))351 {352 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')353 continue;354355 vector<string> vWords;356 ParseString(strLine, ' ', vWords);357 if (vWords.size() < 2)358 continue;359360 char pszName[10000];361 pszName[0] = '\0';362363 if (vWords[1] == "352" && vWords.size() >= 8)364 {365 // index 7 is limited to 16 characters366 // could get full length name at index 10, but would be different from join messages367 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));368 printf("IRC got who\n");369 }370371 if (vWords[1] == "JOIN" && vWords[0].size() > 1)372 {373 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname374 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));375 if (strchr(pszName, '!'))376 *strchr(pszName, '!') = '\0';377 printf("IRC got join\n");378 }379380 if (pszName[0] == 'u')381 {382 CAddress addr;383 if (DecodeAddress(pszName, addr))384 {385 addr.nTime = GetAdjustedTime();386 if (AddAddress(addr, 51 * 60))387 printf("IRC got new address\n");388 nGotIRCAddresses++;389 }390 else391 {392 printf("IRC decode failed\n");393 }394 }395 }396 closesocket(hSocket);397 hSocket = INVALID_SOCKET;398399 // IRC usually blocks TOR, so only try once400 if (fTOR)401 return;402403 if (GetTime() - nStart > 20 * 60)404 {405 nErrorWait /= 3;406 nRetryWait /= 3;407 }408409 nRetryWait = nRetryWait * 11 / 10;410 if (!Wait(nRetryWait += 60))411 return;412 }413}414415416417418419420421422423424#ifdef TEST425int main(int argc, char *argv[])426{427 WSADATA wsadata;428 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)429 {430 printf("Error at WSAStartup()\n");431 return false;432 }433434 ThreadIRCSeed(NULL);435436 WSACleanup();437 return 0;438}439#endif
Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.
