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.
12 using namespace boost;
14 int nGotIRCAddresses = 0;
15 bool fGotExternalIP = false;
17 void ThreadIRCSeed2(void* parg);
30 string EncodeAddress(const CAddress& addr)
36 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
37 return string("u") + EncodeBase58Check(vch);
40 bool DecodeAddress(string str, CAddress& addr)
42 vector<unsigned char> vch;
43 if (!DecodeBase58Check(str.substr(1), vch))
47 if (vch.size() != sizeof(tmp))
49 memcpy(&tmp, &vch[0], sizeof(tmp));
51 addr = CAddress(tmp.ip, ntohs(tmp.port), NODE_NETWORK);
60 static bool Send(SOCKET hSocket, const char* pszSend)
62 if (strstr(pszSend, "PONG") != pszSend)
63 printf("IRC SENDING: %s\n", pszSend);
64 const char* psz = pszSend;
65 const char* pszEnd = psz + strlen(psz);
68 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
76 bool RecvLine(SOCKET hSocket, string& strLine)
82 int nBytes = recv(hSocket, &c, 1, 0);
90 if (strLine.size() >= 9000)
99 int nErr = WSAGetLastError();
100 if (nErr == WSAEMSGSIZE)
102 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
108 if (!strLine.empty())
113 printf("IRC socket closed\n");
119 int nErr = WSAGetLastError();
120 printf("IRC recv failed: %d\n", nErr);
127 bool RecvLineIRC(SOCKET hSocket, string& strLine)
131 bool fRet = RecvLine(hSocket, strLine);
136 vector<string> vWords;
137 ParseString(strLine, ' ', vWords);
138 if (vWords.size() >= 1 && vWords[0] == "PING")
142 Send(hSocket, strLine.c_str());
150 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
155 strLine.reserve(10000);
156 if (!RecvLineIRC(hSocket, strLine))
158 printf("IRC %s\n", strLine.c_str());
159 if (psz1 && strLine.find(psz1) != -1)
161 if (psz2 && strLine.find(psz2) != -1)
163 if (psz3 && strLine.find(psz3) != -1)
165 if (psz4 && strLine.find(psz4) != -1)
170 bool Wait(int nSeconds)
174 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
175 for (int i = 0; i < nSeconds; i++)
184 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
190 if (!RecvLineIRC(hSocket, strLine))
193 vector<string> vWords;
194 ParseString(strLine, ' ', vWords);
195 if (vWords.size() < 2)
198 if (vWords[1] == psz1)
200 printf("IRC %s\n", strLine.c_str());
207 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
209 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
212 if (!RecvCodeLine(hSocket, "302", strLine))
215 vector<string> vWords;
216 ParseString(strLine, ' ', vWords);
217 if (vWords.size() < 4)
220 string str = vWords[3];
221 if (str.rfind("@") == string::npos)
223 string strHost = str.substr(str.rfind("@")+1);
225 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
226 // but in case another IRC is ever used this should work.
227 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
230 CAddress addr(strHost, 0, true);
240 void ThreadIRCSeed(void* parg)
242 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
245 ThreadIRCSeed2(parg);
247 catch (std::exception& e) {
248 PrintExceptionContinue(&e, "ThreadIRCSeed()");
250 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
252 printf("ThreadIRCSeed exiting\n");
255 void ThreadIRCSeed2(void* parg)
257 /* Dont advertise on IRC if we don't allow incoming connections */
258 if (mapArgs.count("-connect") || fNoListen)
261 if (GetBoolArg("-noirc"))
263 printf("ThreadIRCSeed started\n");
266 bool fNameInUse = false;
267 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
271 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
272 CAddress addrConnect("92.243.23.21", 6667); // irc.lfnet.org
275 //struct hostent* phostent = gethostbyname("chat.freenode.net");
276 CAddress addrIRC("irc.lfnet.org", 6667, true);
277 if (addrIRC.IsValid())
278 addrConnect = addrIRC;
282 if (!ConnectSocket(addrConnect, hSocket))
284 printf("IRC connect failed\n");
285 nErrorWait = nErrorWait * 11 / 10;
286 if (Wait(nErrorWait += 60))
292 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
294 closesocket(hSocket);
295 hSocket = INVALID_SOCKET;
296 nErrorWait = nErrorWait * 11 / 10;
297 if (Wait(nErrorWait += 60))
304 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
305 strMyName = EncodeAddress(addrLocalHost);
307 strMyName = strprintf("x%u", GetRand(1000000000));
309 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
310 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
312 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
315 closesocket(hSocket);
316 hSocket = INVALID_SOCKET;
319 printf("IRC name already in use\n");
324 nErrorWait = nErrorWait * 11 / 10;
325 if (Wait(nErrorWait += 60))
332 // Get our external IP from the IRC server and re-nick before joining the channel
333 CAddress addrFromIRC;
334 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
336 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
337 if (!fUseProxy && addrFromIRC.IsRoutable())
339 // IRC lets you to re-nick
340 fGotExternalIP = true;
341 addrLocalHost.ip = addrFromIRC.ip;
342 strMyName = EncodeAddress(addrLocalHost);
343 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
348 Send(hSocket, "JOIN #bitcoinTEST\r");
349 Send(hSocket, "WHO #bitcoinTEST\r");
351 // randomly join #bitcoin00-#bitcoin99
352 int channel_number = GetRandInt(100);
353 Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
354 Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
357 int64 nStart = GetTime();
359 strLine.reserve(10000);
360 while (!fShutdown && RecvLineIRC(hSocket, strLine))
362 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
365 vector<string> vWords;
366 ParseString(strLine, ' ', vWords);
367 if (vWords.size() < 2)
373 if (vWords[1] == "352" && vWords.size() >= 8)
375 // index 7 is limited to 16 characters
376 // could get full length name at index 10, but would be different from join messages
377 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
378 printf("IRC got who\n");
381 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
383 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
384 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
385 if (strchr(pszName, '!'))
386 *strchr(pszName, '!') = '\0';
387 printf("IRC got join\n");
390 if (pszName[0] == 'u')
393 if (DecodeAddress(pszName, addr))
395 addr.nTime = GetAdjustedTime();
396 if (AddAddress(addr, 51 * 60))
397 printf("IRC got new address: %s\n", addr.ToString().c_str());
402 printf("IRC decode failed\n");
406 closesocket(hSocket);
407 hSocket = INVALID_SOCKET;
409 // IRC usually blocks TOR, so only try once
413 if (GetTime() - nStart > 20 * 60)
419 nRetryWait = nRetryWait * 11 / 10;
420 if (!Wait(nRetryWait += 60))
435 int main(int argc, char *argv[])
438 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
440 printf("Error at WSAStartup()\n");