1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
7 int nGotIRCAddresses = 0;
9 void ThreadIRCSeed2(void* parg);
22 string EncodeAddress(const CAddress& addr)
28 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
29 return string("u") + EncodeBase58Check(vch);
32 bool DecodeAddress(string str, CAddress& addr)
34 vector<unsigned char> vch;
35 if (!DecodeBase58Check(str.substr(1), vch))
39 if (vch.size() != sizeof(tmp))
41 memcpy(&tmp, &vch[0], sizeof(tmp));
43 addr = CAddress(tmp.ip, tmp.port, NODE_NETWORK);
52 static bool Send(SOCKET hSocket, const char* pszSend)
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);
60 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
68 bool RecvLine(SOCKET hSocket, string& strLine)
74 int nBytes = recv(hSocket, &c, 1, 0);
82 if (strLine.size() >= 9000)
90 printf("IRC socket closed\n");
96 int nErr = WSAGetLastError();
97 if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
99 printf("IRC recv failed: %d\n", nErr);
106 bool RecvLineIRC(SOCKET hSocket, string& strLine)
110 bool fRet = RecvLine(hSocket, strLine);
115 vector<string> vWords;
116 ParseString(strLine, ' ', vWords);
117 if (vWords.size() >= 1 && vWords[0] == "PING")
121 Send(hSocket, strLine.c_str());
129 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL)
134 strLine.reserve(10000);
135 if (!RecvLineIRC(hSocket, strLine))
137 printf("IRC %s\n", strLine.c_str());
138 if (psz1 && strLine.find(psz1) != -1)
140 if (psz2 && strLine.find(psz2) != -1)
142 if (psz3 && strLine.find(psz3) != -1)
147 bool Wait(int nSeconds)
151 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
152 for (int i = 0; i < nSeconds; i++)
163 void ThreadIRCSeed(void* parg)
165 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
168 ThreadIRCSeed2(parg);
170 catch (std::exception& e) {
171 PrintExceptionContinue(&e, "ThreadIRCSeed()");
173 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
175 printf("ThreadIRCSeed exiting\n");
178 void ThreadIRCSeed2(void* parg)
180 if (mapArgs.count("-connect"))
182 if (mapArgs.count("-noirc"))
184 printf("ThreadIRCSeed started\n");
187 bool fNameInUse = false;
188 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
192 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
193 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
196 //struct hostent* phostent = gethostbyname("chat.freenode.net");
197 struct hostent* phostent = gethostbyname("irc.lfnet.org");
198 if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])
199 addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));
203 if (!ConnectSocket(addrConnect, hSocket))
205 printf("IRC connect failed\n");
206 nErrorWait = nErrorWait * 11 / 10;
207 if (Wait(nErrorWait += 60))
213 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))
215 closesocket(hSocket);
216 hSocket = INVALID_SOCKET;
217 nErrorWait = nErrorWait * 11 / 10;
218 if (Wait(nErrorWait += 60))
225 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
226 strMyName = EncodeAddress(addrLocalHost);
228 strMyName = strprintf("x%u", GetRand(1000000000));
230 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
231 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
233 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
236 closesocket(hSocket);
237 hSocket = INVALID_SOCKET;
240 printf("IRC name already in use\n");
245 nErrorWait = nErrorWait * 11 / 10;
246 if (Wait(nErrorWait += 60))
253 Send(hSocket, "JOIN #bitcoin\r");
254 Send(hSocket, "WHO #bitcoin\r");
256 int64 nStart = GetTime();
258 strLine.reserve(10000);
259 while (!fShutdown && RecvLineIRC(hSocket, strLine))
261 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
264 vector<string> vWords;
265 ParseString(strLine, ' ', vWords);
266 if (vWords.size() < 2)
272 if (vWords[1] == "352" && vWords.size() >= 8)
274 // index 7 is limited to 16 characters
275 // could get full length name at index 10, but would be different from join messages
276 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
277 printf("IRC got who\n");
280 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
282 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
283 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
284 if (strchr(pszName, '!'))
285 *strchr(pszName, '!') = '\0';
286 printf("IRC got join\n");
289 if (pszName[0] == 'u')
292 if (DecodeAddress(pszName, addr))
294 addr.nTime = GetAdjustedTime() - 51 * 60;
295 if (AddAddress(addr))
296 printf("IRC got new address\n");
301 printf("IRC decode failed\n");
305 closesocket(hSocket);
306 hSocket = INVALID_SOCKET;
308 // IRC usually blocks TOR, so only try once
312 if (GetTime() - nStart > 20 * 60)
318 nRetryWait = nRetryWait * 11 / 10;
319 if (!Wait(nRetryWait += 60))
334 int main(int argc, char *argv[])
337 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
339 printf("Error at WSAStartup()\n");