1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
14 using namespace boost;
16 int nGotIRCAddresses = 0;
17 bool fGotExternalIP = false;
19 void ThreadIRCSeed2(void* parg);
32 string EncodeAddress(const CAddress& addr)
38 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
39 return string("u") + EncodeBase58Check(vch);
42 bool DecodeAddress(string str, CAddress& addr)
44 vector<unsigned char> vch;
45 if (!DecodeBase58Check(str.substr(1), vch))
49 if (vch.size() != sizeof(tmp))
51 memcpy(&tmp, &vch[0], sizeof(tmp));
53 addr = CAddress(tmp.ip, ntohs(tmp.port), NODE_NETWORK);
62 static bool Send(SOCKET hSocket, const char* pszSend)
64 if (strstr(pszSend, "PONG") != pszSend)
65 printf("IRC SENDING: %s\n", pszSend);
66 const char* psz = pszSend;
67 const char* pszEnd = psz + strlen(psz);
70 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
78 bool RecvLine(SOCKET hSocket, string& strLine)
84 int nBytes = recv(hSocket, &c, 1, 0);
92 if (strLine.size() >= 9000)
101 int nErr = WSAGetLastError();
102 if (nErr == WSAEMSGSIZE)
104 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
110 if (!strLine.empty())
115 printf("IRC socket closed\n");
121 int nErr = WSAGetLastError();
122 printf("IRC recv failed: %d\n", nErr);
129 bool RecvLineIRC(SOCKET hSocket, string& strLine)
133 bool fRet = RecvLine(hSocket, strLine);
138 vector<string> vWords;
139 ParseString(strLine, ' ', vWords);
140 if (vWords.size() >= 1 && vWords[0] == "PING")
144 Send(hSocket, strLine.c_str());
152 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
157 strLine.reserve(10000);
158 if (!RecvLineIRC(hSocket, strLine))
160 printf("IRC %s\n", strLine.c_str());
161 if (psz1 && strLine.find(psz1) != -1)
163 if (psz2 && strLine.find(psz2) != -1)
165 if (psz3 && strLine.find(psz3) != -1)
167 if (psz4 && strLine.find(psz4) != -1)
172 bool Wait(int nSeconds)
176 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
177 for (int i = 0; i < nSeconds; i++)
186 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
192 if (!RecvLineIRC(hSocket, strLine))
195 vector<string> vWords;
196 ParseString(strLine, ' ', vWords);
197 if (vWords.size() < 2)
200 if (vWords[1] == psz1)
202 printf("IRC %s\n", strLine.c_str());
209 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
211 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
214 if (!RecvCodeLine(hSocket, "302", strLine))
217 vector<string> vWords;
218 ParseString(strLine, ' ', vWords);
219 if (vWords.size() < 4)
222 string str = vWords[3];
223 if (str.rfind("@") == string::npos)
225 string strHost = str.substr(str.rfind("@")+1);
227 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
228 // but in case another IRC is ever used this should work.
229 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
232 CAddress addr(strHost, 0, true);
242 void ThreadIRCSeed(void* parg)
244 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
247 ThreadIRCSeed2(parg);
249 catch (std::exception& e) {
250 PrintExceptionContinue(&e, "ThreadIRCSeed()");
252 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
254 printf("ThreadIRCSeed exiting\n");
257 void ThreadIRCSeed2(void* parg)
259 /* Dont advertise on IRC if we don't allow incoming connections */
260 if (mapArgs.count("-connect") || fNoListen)
263 if (GetBoolArg("-noirc"))
265 printf("ThreadIRCSeed started\n");
268 bool fNameInUse = false;
269 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
273 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
274 CAddress addrConnect("92.243.23.21", 6667); // irc.lfnet.org
277 //struct hostent* phostent = gethostbyname("chat.freenode.net");
278 CAddress addrIRC("irc.lfnet.org", 6667, true);
279 if (addrIRC.IsValid())
280 addrConnect = addrIRC;
284 if (!ConnectSocket(addrConnect, hSocket))
286 printf("IRC connect failed\n");
287 nErrorWait = nErrorWait * 11 / 10;
288 if (Wait(nErrorWait += 60))
294 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
296 closesocket(hSocket);
297 hSocket = INVALID_SOCKET;
298 nErrorWait = nErrorWait * 11 / 10;
299 if (Wait(nErrorWait += 60))
306 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
307 strMyName = EncodeAddress(addrLocalHost);
309 strMyName = strprintf("x%u", GetRand(1000000000));
311 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
312 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
314 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
317 closesocket(hSocket);
318 hSocket = INVALID_SOCKET;
321 printf("IRC name already in use\n");
326 nErrorWait = nErrorWait * 11 / 10;
327 if (Wait(nErrorWait += 60))
334 // Get our external IP from the IRC server and re-nick before joining the channel
335 CAddress addrFromIRC;
336 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
338 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
339 if (!fUseProxy && addrFromIRC.IsRoutable())
341 // IRC lets you to re-nick
342 fGotExternalIP = true;
343 addrLocalHost.ip = addrFromIRC.ip;
344 strMyName = EncodeAddress(addrLocalHost);
345 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
350 Send(hSocket, "JOIN #bitcoinTEST\r");
351 Send(hSocket, "WHO #bitcoinTEST\r");
353 // randomly join #bitcoin00-#bitcoin99
354 int channel_number = GetRandInt(100);
355 Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
356 Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
359 int64_t nStart = GetTime();
361 strLine.reserve(10000);
362 while (!fShutdown && RecvLineIRC(hSocket, strLine))
364 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
367 vector<string> vWords;
368 ParseString(strLine, ' ', vWords);
369 if (vWords.size() < 2)
375 if (vWords[1] == "352" && vWords.size() >= 8)
377 // index 7 is limited to 16 characters
378 // could get full length name at index 10, but would be different from join messages
379 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
380 printf("IRC got who\n");
383 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
385 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
386 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
387 if (strchr(pszName, '!'))
388 *strchr(pszName, '!') = '\0';
389 printf("IRC got join\n");
392 if (pszName[0] == 'u')
395 if (DecodeAddress(pszName, addr))
397 addr.nTime = GetAdjustedTime();
398 if (AddAddress(addr, 51 * 60))
399 printf("IRC got new address: %s\n", addr.ToString().c_str());
404 printf("IRC decode failed\n");
408 closesocket(hSocket);
409 hSocket = INVALID_SOCKET;
411 // IRC usually blocks TOR, so only try once
415 if (GetTime() - nStart > 20 * 60)
421 nRetryWait = nRetryWait * 11 / 10;
422 if (!Wait(nRetryWait += 60))
437 int main(int argc, char *argv[])
440 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
442 printf("Error at WSAStartup()\n");