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.
10 int nGotIRCAddresses = 0;
11 bool fGotExternalIP = false;
13 void ThreadIRCSeed2(void* parg);
26 string EncodeAddress(const CAddress& addr)
32 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
33 return string("u") + EncodeBase58Check(vch);
36 bool DecodeAddress(string str, CAddress& addr)
38 vector<unsigned char> vch;
39 if (!DecodeBase58Check(str.substr(1), vch))
43 if (vch.size() != sizeof(tmp))
45 memcpy(&tmp, &vch[0], sizeof(tmp));
47 addr = CAddress(tmp.ip, ntohs(tmp.port), NODE_NETWORK);
56 static bool Send(SOCKET hSocket, const char* pszSend)
58 if (strstr(pszSend, "PONG") != pszSend)
59 printf("IRC SENDING: %s\n", pszSend);
60 const char* psz = pszSend;
61 const char* pszEnd = psz + strlen(psz);
64 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
72 bool RecvLine(SOCKET hSocket, string& strLine)
78 int nBytes = recv(hSocket, &c, 1, 0);
86 if (strLine.size() >= 9000)
95 int nErr = WSAGetLastError();
96 if (nErr == WSAEMSGSIZE)
98 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
104 if (!strLine.empty())
109 printf("IRC socket closed\n");
115 int nErr = WSAGetLastError();
116 printf("IRC recv failed: %d\n", nErr);
123 bool RecvLineIRC(SOCKET hSocket, string& strLine)
127 bool fRet = RecvLine(hSocket, strLine);
132 vector<string> vWords;
133 ParseString(strLine, ' ', vWords);
134 if (vWords.size() >= 1 && vWords[0] == "PING")
138 Send(hSocket, strLine.c_str());
146 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
151 strLine.reserve(10000);
152 if (!RecvLineIRC(hSocket, strLine))
154 printf("IRC %s\n", strLine.c_str());
155 if (psz1 && strLine.find(psz1) != -1)
157 if (psz2 && strLine.find(psz2) != -1)
159 if (psz3 && strLine.find(psz3) != -1)
161 if (psz4 && strLine.find(psz4) != -1)
166 bool Wait(int nSeconds)
170 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
171 for (int i = 0; i < nSeconds; i++)
180 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
186 if (!RecvLineIRC(hSocket, strLine))
189 vector<string> vWords;
190 ParseString(strLine, ' ', vWords);
191 if (vWords.size() < 2)
194 if (vWords[1] == psz1)
196 printf("IRC %s\n", strLine.c_str());
203 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
205 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
208 if (!RecvCodeLine(hSocket, "302", strLine))
211 vector<string> vWords;
212 ParseString(strLine, ' ', vWords);
213 if (vWords.size() < 4)
216 string str = vWords[3];
217 if (str.rfind("@") == string::npos)
219 string strHost = str.substr(str.rfind("@")+1);
221 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
222 // but in case another IRC is ever used this should work.
223 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
226 CAddress addr(strHost, 0, true);
236 void ThreadIRCSeed(void* parg)
238 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
241 ThreadIRCSeed2(parg);
243 catch (std::exception& e) {
244 PrintExceptionContinue(&e, "ThreadIRCSeed()");
246 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
248 printf("ThreadIRCSeed exiting\n");
251 void ThreadIRCSeed2(void* parg)
253 /* Dont advertise on IRC if we don't allow incoming connections */
254 if (mapArgs.count("-connect") || fNoListen)
257 if (GetBoolArg("-noirc"))
259 printf("ThreadIRCSeed started\n");
262 bool fNameInUse = false;
263 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
267 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
268 CAddress addrConnect("92.243.23.21", 6667); // irc.lfnet.org
271 //struct hostent* phostent = gethostbyname("chat.freenode.net");
272 CAddress addrIRC("irc.lfnet.org", 6667, true);
273 if (addrIRC.IsValid())
274 addrConnect = addrIRC;
278 if (!ConnectSocket(addrConnect, hSocket))
280 printf("IRC connect failed\n");
281 nErrorWait = nErrorWait * 11 / 10;
282 if (Wait(nErrorWait += 60))
288 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
290 closesocket(hSocket);
291 hSocket = INVALID_SOCKET;
292 nErrorWait = nErrorWait * 11 / 10;
293 if (Wait(nErrorWait += 60))
300 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
301 strMyName = EncodeAddress(addrLocalHost);
303 strMyName = strprintf("x%u", GetRand(1000000000));
305 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
306 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
308 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
311 closesocket(hSocket);
312 hSocket = INVALID_SOCKET;
315 printf("IRC name already in use\n");
320 nErrorWait = nErrorWait * 11 / 10;
321 if (Wait(nErrorWait += 60))
328 // Get our external IP from the IRC server and re-nick before joining the channel
329 CAddress addrFromIRC;
330 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
332 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
333 if (!fUseProxy && addrFromIRC.IsRoutable())
335 // IRC lets you to re-nick
336 fGotExternalIP = true;
337 addrLocalHost.ip = addrFromIRC.ip;
338 strMyName = EncodeAddress(addrLocalHost);
339 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
344 Send(hSocket, "JOIN #bitcoinTEST\r");
345 Send(hSocket, "WHO #bitcoinTEST\r");
347 // randomly join #bitcoin00-#bitcoin99
348 int channel_number = GetRandInt(100);
349 Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
350 Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
353 int64 nStart = GetTime();
355 strLine.reserve(10000);
356 while (!fShutdown && RecvLineIRC(hSocket, strLine))
358 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
361 vector<string> vWords;
362 ParseString(strLine, ' ', vWords);
363 if (vWords.size() < 2)
369 if (vWords[1] == "352" && vWords.size() >= 8)
371 // index 7 is limited to 16 characters
372 // could get full length name at index 10, but would be different from join messages
373 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
374 printf("IRC got who\n");
377 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
379 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
380 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
381 if (strchr(pszName, '!'))
382 *strchr(pszName, '!') = '\0';
383 printf("IRC got join\n");
386 if (pszName[0] == 'u')
389 if (DecodeAddress(pszName, addr))
391 addr.nTime = GetAdjustedTime();
392 if (AddAddress(addr, 51 * 60))
393 printf("IRC got new address: %s\n", addr.ToString().c_str());
398 printf("IRC decode failed\n");
402 closesocket(hSocket);
403 hSocket = INVALID_SOCKET;
405 // IRC usually blocks TOR, so only try once
409 if (GetTime() - nStart > 20 * 60)
415 nRetryWait = nRetryWait * 11 / 10;
416 if (!Wait(nRetryWait += 60))
431 int main(int argc, char *argv[])
434 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
436 printf("Error at WSAStartup()\n");