Bitcoin v0.2.0 · 2009-12-16
irc.cpp
1// Copyright (c) 2009 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;891011#pragma pack(push, 1)12struct ircaddr13{14 int ip;15 short port;16};17#pragma pack(pop)1819string EncodeAddress(const CAddress& addr)20{21 struct ircaddr tmp;22 tmp.ip = addr.ip;23 tmp.port = addr.port;2425 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));26 return string("u") + EncodeBase58Check(vch);27}2829bool DecodeAddress(string str, CAddress& addr)30{31 vector<unsigned char> vch;32 if (!DecodeBase58Check(str.substr(1), vch))33 return false;3435 struct ircaddr tmp;36 if (vch.size() != sizeof(tmp))37 return false;38 memcpy(&tmp, &vch[0], sizeof(tmp));3940 addr = CAddress(tmp.ip, tmp.port, NODE_NETWORK);41 return true;42}43444546474849static bool Send(SOCKET hSocket, const char* pszSend)50{51 if (strstr(pszSend, "PONG") != pszSend)52 printf("IRC SENDING: %s\n", pszSend);53 const char* psz = pszSend;54 const char* pszEnd = psz + strlen(psz);55 while (psz < pszEnd)56 {57 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);58 if (ret < 0)59 return false;60 psz += ret;61 }62 return true;63}6465bool RecvLine(SOCKET hSocket, string& strLine)66{67 strLine = "";68 loop69 {70 char c;71 int nBytes = recv(hSocket, &c, 1, 0);72 if (nBytes > 0)73 {74 if (c == '\n')75 continue;76 if (c == '\r')77 return true;78 strLine += c;79 }80 else if (nBytes <= 0)81 {82 if (!strLine.empty())83 return true;84 // socket closed85 printf("IRC socket closed\n");86 return false;87 }88 else89 {90 // socket error91 int nErr = WSAGetLastError();92 if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)93 {94 printf("IRC recv failed: %d\n", nErr);95 return false;96 }97 }98 }99}100101bool RecvLineIRC(SOCKET hSocket, string& strLine)102{103 loop104 {105 bool fRet = RecvLine(hSocket, strLine);106 if (fRet)107 {108 if (fShutdown)109 return false;110 vector<string> vWords;111 ParseString(strLine, ' ', vWords);112 if (vWords[0] == "PING")113 {114 strLine[1] = 'O';115 strLine += '\r';116 Send(hSocket, strLine.c_str());117 continue;118 }119 }120 return fRet;121 }122}123124int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL)125{126 loop127 {128 string strLine;129 if (!RecvLineIRC(hSocket, strLine))130 return 0;131 printf("IRC %s\n", strLine.c_str());132 if (psz1 && strLine.find(psz1) != -1)133 return 1;134 if (psz2 && strLine.find(psz2) != -1)135 return 2;136 if (psz3 && strLine.find(psz3) != -1)137 return 3;138 }139}140141bool Wait(int nSeconds)142{143 if (fShutdown)144 return false;145 printf("IRC waiting %d seconds to reconnect\n", nSeconds);146 for (int i = 0; i < nSeconds; i++)147 {148 if (fShutdown)149 return false;150 Sleep(1000);151 }152 return true;153}154155156157void ThreadIRCSeed(void* parg)158{159 SetThreadPriority(THREAD_PRIORITY_NORMAL);160 int nErrorWait = 10;161 int nRetryWait = 10;162 bool fNameInUse = false;163 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));164165 while (!fShutdown)166 {167 CAddress addrConnect("216.155.130.130:6667");168 if (!fTOR)169 {170 struct hostent* phostent = gethostbyname("chat.freenode.net");171 if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])172 addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));173 }174175 SOCKET hSocket;176 if (!ConnectSocket(addrConnect, hSocket))177 {178 printf("IRC connect failed\n");179 nErrorWait = nErrorWait * 11 / 10;180 if (Wait(nErrorWait += 60))181 continue;182 else183 return;184 }185186 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))187 {188 closesocket(hSocket);189 hSocket = INVALID_SOCKET;190 nErrorWait = nErrorWait * 11 / 10;191 if (Wait(nErrorWait += 60))192 continue;193 else194 return;195 }196197 string strMyName;198 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)199 strMyName = EncodeAddress(addrLocalHost);200 else201 strMyName = strprintf("x%u", GetRand(1000000000));202203204 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());205 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());206207 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");208 if (nRet != 1)209 {210 closesocket(hSocket);211 hSocket = INVALID_SOCKET;212 if (nRet == 2)213 {214 printf("IRC name already in use\n");215 fNameInUse = true;216 Wait(10);217 continue;218 }219 nErrorWait = nErrorWait * 11 / 10;220 if (Wait(nErrorWait += 60))221 continue;222 else223 return;224 }225 Sleep(500);226227 Send(hSocket, "JOIN #bitcoin\r");228 Send(hSocket, "WHO #bitcoin\r");229230 int64 nStart = GetTime();231 string strLine;232 while (!fShutdown && RecvLineIRC(hSocket, strLine))233 {234 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')235 continue;236237 vector<string> vWords;238 ParseString(strLine, ' ', vWords);239 if (vWords.size() < 2)240 continue;241242 char pszName[10000];243 pszName[0] = '\0';244245 if (vWords[1] == "352" && vWords.size() >= 8)246 {247 // index 7 is limited to 16 characters248 // could get full length name at index 10, but would be different from join messages249 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));250 printf("IRC got who\n");251 }252253 if (vWords[1] == "JOIN" && vWords[0].size() > 1)254 {255 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname256 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));257 if (strchr(pszName, '!'))258 *strchr(pszName, '!') = '\0';259 printf("IRC got join\n");260 }261262 if (pszName[0] == 'u')263 {264 CAddress addr;265 if (DecodeAddress(pszName, addr))266 {267 addr.nTime = GetAdjustedTime() - 51 * 60;268 CAddrDB addrdb;269 if (AddAddress(addrdb, addr))270 printf("IRC got new address\n");271 nGotIRCAddresses++;272 }273 else274 {275 printf("IRC decode failed\n");276 }277 }278 }279 closesocket(hSocket);280 hSocket = INVALID_SOCKET;281282 // IRC usually blocks TOR, so only try once283 if (fTOR)284 return;285286 if (GetTime() - nStart > 20 * 60)287 {288 nErrorWait /= 3;289 nRetryWait /= 3;290 }291292 nRetryWait = nRetryWait * 11 / 10;293 if (!Wait(nRetryWait += 60))294 return;295 }296}297298299300301302303304305306307#ifdef TEST308int main(int argc, char *argv[])309{310 WSADATA wsadata;311 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)312 {313 printf("Error at WSAStartup()\n");314 return false;315 }316317 ThreadIRCSeed(NULL);318319 WSACleanup();320 return 0;321}322#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.
