1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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("socket closed\n");
119 int nErr = WSAGetLastError();
120 printf("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) != string::npos)
161 if (psz2 && strLine.find(psz2) != string::npos)
163 if (psz3 && strLine.find(psz3) != string::npos)
165 if (psz4 && strLine.find(psz4) != string::npos)
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;
270 CAddress addrConnect("92.243.23.21", 6667); // irc.lfnet.org
272 CAddress addrIRC("irc.lfnet.org", 6667, true);
273 if (addrIRC.IsValid())
274 addrConnect = addrIRC;
277 if (!ConnectSocket(addrConnect, hSocket))
279 printf("IRC connect failed\n");
280 nErrorWait = nErrorWait * 11 / 10;
281 if (Wait(nErrorWait += 60))
287 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
289 closesocket(hSocket);
290 hSocket = INVALID_SOCKET;
291 nErrorWait = nErrorWait * 11 / 10;
292 if (Wait(nErrorWait += 60))
299 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
300 strMyName = EncodeAddress(addrLocalHost);
302 strMyName = strprintf("x%u", GetRand(1000000000));
304 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
305 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
307 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
310 closesocket(hSocket);
311 hSocket = INVALID_SOCKET;
314 printf("IRC name already in use\n");
319 nErrorWait = nErrorWait * 11 / 10;
320 if (Wait(nErrorWait += 60))
327 // Get our external IP from the IRC server and re-nick before joining the channel
328 CAddress addrFromIRC;
329 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
331 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
332 if (!fUseProxy && addrFromIRC.IsRoutable())
334 // IRC lets you to re-nick
335 fGotExternalIP = true;
336 addrLocalHost.ip = addrFromIRC.ip;
337 strMyName = EncodeAddress(addrLocalHost);
338 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
343 Send(hSocket, "JOIN #bitcoinTEST\r");
344 Send(hSocket, "WHO #bitcoinTEST\r");
346 // randomly join #bitcoin00-#bitcoin99
347 int channel_number = GetRandInt(100);
348 Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
349 Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
352 int64 nStart = GetTime();
354 strLine.reserve(10000);
355 while (!fShutdown && RecvLineIRC(hSocket, strLine))
357 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
360 vector<string> vWords;
361 ParseString(strLine, ' ', vWords);
362 if (vWords.size() < 2)
368 if (vWords[1] == "352" && vWords.size() >= 8)
370 // index 7 is limited to 16 characters
371 // could get full length name at index 10, but would be different from join messages
372 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
373 printf("IRC got who\n");
376 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
378 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
379 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
380 if (strchr(pszName, '!'))
381 *strchr(pszName, '!') = '\0';
382 printf("IRC got join\n");
385 if (pszName[0] == 'u')
388 if (DecodeAddress(pszName, addr))
390 addr.nTime = GetAdjustedTime();
391 if (AddAddress(addr, 51 * 60))
392 printf("IRC got new address: %s\n", addr.ToString().c_str());
397 printf("IRC decode failed\n");
401 closesocket(hSocket);
402 hSocket = INVALID_SOCKET;
404 if (GetTime() - nStart > 20 * 60)
410 nRetryWait = nRetryWait * 11 / 10;
411 if (!Wait(nRetryWait += 60))
426 int main(int argc, char *argv[])
429 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
431 printf("Error at WSAStartup()\n");