Bitcoin v0.1.3 · 2009-01-12
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"678910#pragma pack(1)11struct ircaddr12{13 int ip;14 short port;15};1617string EncodeAddress(const CAddress& addr)18{19 struct ircaddr tmp;20 tmp.ip = addr.ip;21 tmp.port = addr.port;2223 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));24 return string("u") + EncodeBase58Check(vch);25}2627bool DecodeAddress(string str, CAddress& addr)28{29 vector<unsigned char> vch;30 if (!DecodeBase58Check(str.substr(1), vch))31 return false;3233 struct ircaddr tmp;34 if (vch.size() != sizeof(tmp))35 return false;36 memcpy(&tmp, &vch[0], sizeof(tmp));3738 addr = CAddress(tmp.ip, tmp.port);39 return true;40}41424344454647static bool Send(SOCKET hSocket, const char* pszSend)48{49 if (strstr(pszSend, "PONG") != pszSend)50 printf("SENDING: %s\n", pszSend);51 const char* psz = pszSend;52 const char* pszEnd = psz + strlen(psz);53 while (psz < pszEnd)54 {55 int ret = send(hSocket, psz, pszEnd - psz, 0);56 if (ret < 0)57 return false;58 psz += ret;59 }60 return true;61}6263bool RecvLine(SOCKET hSocket, string& strLine)64{65 strLine = "";66 loop67 {68 char c;69 int nBytes = recv(hSocket, &c, 1, 0);70 if (nBytes > 0)71 {72 if (c == '\n')73 continue;74 if (c == '\r')75 return true;76 strLine += c;77 }78 else if (nBytes <= 0)79 {80 if (!strLine.empty())81 return true;82 // socket closed83 printf("IRC socket closed\n");84 return false;85 }86 else87 {88 // socket error89 int nErr = WSAGetLastError();90 if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)91 {92 printf("IRC recv failed: %d\n", nErr);93 return false;94 }95 }96 }97}9899bool RecvLineIRC(SOCKET hSocket, string& strLine)100{101 loop102 {103 bool fRet = RecvLine(hSocket, strLine);104 if (fRet)105 {106 if (fShutdown)107 return false;108 vector<string> vWords;109 ParseString(strLine, ' ', vWords);110 if (vWords[0] == "PING")111 {112 strLine[1] = 'O';113 strLine += '\r';114 Send(hSocket, strLine.c_str());115 continue;116 }117 }118 return fRet;119 }120}121122bool RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL)123{124 loop125 {126 string strLine;127 if (!RecvLineIRC(hSocket, strLine))128 return false;129 printf("IRC %s\n", strLine.c_str());130 if (psz1 && strLine.find(psz1) != -1)131 return true;132 if (psz2 && strLine.find(psz2) != -1)133 return true;134 if (psz3 && strLine.find(psz3) != -1)135 return true;136 }137}138139bool Wait(int nSeconds)140{141 if (fShutdown)142 return false;143 printf("Waiting %d seconds to reconnect to IRC\n", nSeconds);144 for (int i = 0; i < nSeconds; i++)145 {146 if (fShutdown)147 return false;148 Sleep(1000);149 }150 return true;151}152153154155void ThreadIRCSeed(void* parg)156{157 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);158 int nErrorWait = 30;159 int nRetryWait = 10;160161162 while (!fShutdown)163 {164 struct hostent* phostent = gethostbyname("chat.freenode.net");165 CAddress addrConnect(*(u_long*)phostent->h_addr_list[0], htons(6667));166167 SOCKET hSocket;168 if (!ConnectSocket(addrConnect, hSocket))169 {170 printf("IRC connect failed\n");171 if (Wait(nErrorWait += 60))172 continue;173 else174 return;175 }176177 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))178 {179 closesocket(hSocket);180 if (Wait(nErrorWait += 60))181 continue;182 else183 return;184 }185186 string strMyName = EncodeAddress(addrLocalHost);187188 if (!addrLocalHost.IsRoutable())189 strMyName = strprintf("x%u", GetRand(1000000000));190191192 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());193 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());194195 if (!RecvUntil(hSocket, " 004 "))196 {197 closesocket(hSocket);198 if (Wait(nErrorWait += 60))199 continue;200 else201 return;202 }203 Sleep(500);204205 Send(hSocket, "JOIN #bitcoin\r");206 Send(hSocket, "WHO #bitcoin\r");207208 string strLine;209 while (!fShutdown && RecvLineIRC(hSocket, strLine))210 {211 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')212 continue;213 printf("IRC %s\n", strLine.c_str());214215 vector<string> vWords;216 ParseString(strLine, ' ', vWords);217 if (vWords.size() < 2)218 continue;219220 char pszName[10000];221 pszName[0] = '\0';222223 if (vWords[1] == "352" && vWords.size() >= 8)224 {225 // index 7 is limited to 16 characters226 // could get full length name at index 10, but would be different from join messages227 strcpy(pszName, vWords[7].c_str());228 printf("GOT WHO: [%s] ", pszName);229 }230231 if (vWords[1] == "JOIN" && vWords[0].size() > 1)232 {233 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname234 strcpy(pszName, vWords[0].c_str() + 1);235 if (strchr(pszName, '!'))236 *strchr(pszName, '!') = '\0';237 printf("GOT JOIN: [%s] ", pszName);238 }239240 if (pszName[0] == 'u')241 {242 CAddress addr;243 if (DecodeAddress(pszName, addr))244 {245 CAddrDB addrdb;246 if (AddAddress(addrdb, addr))247 printf("new ");248 addr.print();249 }250 else251 {252 printf("decode failed\n");253 }254 }255256 }257 closesocket(hSocket);258259 if (!Wait(nRetryWait += 10))260 return;261 }262}263264265266267268269270271272273#ifdef TEST274int main(int argc, char *argv[])275{276 WSADATA wsadata;277 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)278 {279 printf("Error at WSAStartup()\n");280 return false;281 }282283 ThreadIRCSeed(NULL);284285 WSACleanup();286 return 0;287}288#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.
