Bitcoin v0.1.0 · 2009-01-08
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}138139140141142bool fRestartIRCSeed = false;143144void ThreadIRCSeed(void* parg)145{146 loop147 {148 struct hostent* phostent = gethostbyname("chat.freenode.net");149 CAddress addrConnect(*(u_long*)phostent->h_addr_list[0], htons(6667));150151 SOCKET hSocket;152 if (!ConnectSocket(addrConnect, hSocket))153 {154 printf("IRC connect failed\n");155 return;156 }157158 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))159 {160 closesocket(hSocket);161 return;162 }163164 string strMyName = EncodeAddress(addrLocalHost);165166 if (!addrLocalHost.IsRoutable())167 strMyName = strprintf("x%u", GetRand(1000000000));168169 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());170 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());171172 if (!RecvUntil(hSocket, " 004 "))173 {174 closesocket(hSocket);175 return;176 }177 Sleep(500);178179 Send(hSocket, "JOIN #bitcoin\r");180 Send(hSocket, "WHO #bitcoin\r");181182 while (!fRestartIRCSeed)183 {184 string strLine;185 if (fShutdown || !RecvLineIRC(hSocket, strLine))186 {187 closesocket(hSocket);188 return;189 }190 if (strLine.empty() || strLine[0] != ':')191 continue;192 printf("IRC %s\n", strLine.c_str());193194 vector<string> vWords;195 ParseString(strLine, ' ', vWords);196 if (vWords.size() < 2)197 continue;198199 char pszName[10000];200 pszName[0] = '\0';201202 if (vWords[1] == "352" && vWords.size() >= 8)203 {204 // index 7 is limited to 16 characters205 // could get full length name at index 10, but would be different from join messages206 strcpy(pszName, vWords[7].c_str());207 printf("GOT WHO: [%s] ", pszName);208 }209210 if (vWords[1] == "JOIN")211 {212 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname213 strcpy(pszName, vWords[0].c_str() + 1);214 if (strchr(pszName, '!'))215 *strchr(pszName, '!') = '\0';216 printf("GOT JOIN: [%s] ", pszName);217 }218219 if (pszName[0] == 'u')220 {221 CAddress addr;222 if (DecodeAddress(pszName, addr))223 {224 CAddrDB addrdb;225 if (AddAddress(addrdb, addr))226 printf("new ");227 addr.print();228 }229 else230 {231 printf("decode failed\n");232 }233 }234 }235236 fRestartIRCSeed = false;237 closesocket(hSocket);238 }239}240241242243244245246247248249250#ifdef TEST251int main(int argc, char *argv[])252{253 WSADATA wsadata;254 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)255 {256 printf("Error at WSAStartup()\n");257 return false;258 }259260 ThreadIRCSeed(NULL);261262 WSACleanup();263 return 0;264}265#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.
