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 using namespace boost;
12 int nGotIRCAddresses = 0;
13 bool fGotExternalIP = false;
15 void ThreadIRCSeed2(void* parg);
28 string EncodeAddress(const CAddress& addr)
34 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
35 return string("u") + EncodeBase58Check(vch);
38 bool DecodeAddress(string str, CAddress& addr)
40 vector<unsigned char> vch;
41 if (!DecodeBase58Check(str.substr(1), vch))
45 if (vch.size() != sizeof(tmp))
47 memcpy(&tmp, &vch[0], sizeof(tmp));
49 addr = CAddress(tmp.ip, ntohs(tmp.port), NODE_NETWORK);
58 static bool Send(SOCKET hSocket, const char* pszSend)
60 if (strstr(pszSend, "PONG") != pszSend)
61 printf("IRC SENDING: %s\n", pszSend);
62 const char* psz = pszSend;
63 const char* pszEnd = psz + strlen(psz);
66 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
74 bool RecvLine(SOCKET hSocket, string& strLine)
80 int nBytes = recv(hSocket, &c, 1, 0);
88 if (strLine.size() >= 9000)
97 int nErr = WSAGetLastError();
98 if (nErr == WSAEMSGSIZE)
100 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
106 if (!strLine.empty())
111 printf("IRC socket closed\n");
117 int nErr = WSAGetLastError();
118 printf("IRC recv failed: %d\n", nErr);
125 bool RecvLineIRC(SOCKET hSocket, string& strLine)
129 bool fRet = RecvLine(hSocket, strLine);
134 vector<string> vWords;
135 ParseString(strLine, ' ', vWords);
136 if (vWords.size() >= 1 && vWords[0] == "PING")
140 Send(hSocket, strLine.c_str());
148 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
153 strLine.reserve(10000);
154 if (!RecvLineIRC(hSocket, strLine))
156 printf("IRC %s\n", strLine.c_str());
157 if (psz1 && strLine.find(psz1) != -1)
159 if (psz2 && strLine.find(psz2) != -1)
161 if (psz3 && strLine.find(psz3) != -1)
163 if (psz4 && strLine.find(psz4) != -1)
168 bool Wait(int nSeconds)
172 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
173 for (int i = 0; i < nSeconds; i++)
182 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
188 if (!RecvLineIRC(hSocket, strLine))
191 vector<string> vWords;
192 ParseString(strLine, ' ', vWords);
193 if (vWords.size() < 2)
196 if (vWords[1] == psz1)
198 printf("IRC %s\n", strLine.c_str());
205 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
207 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
210 if (!RecvCodeLine(hSocket, "302", strLine))
213 vector<string> vWords;
214 ParseString(strLine, ' ', vWords);
215 if (vWords.size() < 4)
218 string str = vWords[3];
219 if (str.rfind("@") == string::npos)
221 string strHost = str.substr(str.rfind("@")+1);
223 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
224 // but in case another IRC is ever used this should work.
225 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
228 CAddress addr(strHost, 0, true);
238 void ThreadIRCSeed(void* parg)
240 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
243 ThreadIRCSeed2(parg);
245 catch (std::exception& e) {
246 PrintExceptionContinue(&e, "ThreadIRCSeed()");
248 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
250 printf("ThreadIRCSeed exiting\n");
253 void ThreadIRCSeed2(void* parg)
255 /* Dont advertise on IRC if we don't allow incoming connections */
256 if (mapArgs.count("-connect") || fNoListen)
259 if (GetBoolArg("-noirc"))
261 printf("ThreadIRCSeed started\n");
264 bool fNameInUse = false;
265 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
269 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
270 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
273 //struct hostent* phostent = gethostbyname("chat.freenode.net");
274 CAddress addrIRC("irc.lfnet.org:6667", 0, true);
275 if (addrIRC.IsValid())
276 addrConnect = addrIRC;
280 if (!ConnectSocket(addrConnect, hSocket))
282 printf("IRC connect failed\n");
283 nErrorWait = nErrorWait * 11 / 10;
284 if (Wait(nErrorWait += 60))
290 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
292 closesocket(hSocket);
293 hSocket = INVALID_SOCKET;
294 nErrorWait = nErrorWait * 11 / 10;
295 if (Wait(nErrorWait += 60))
302 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
303 strMyName = EncodeAddress(addrLocalHost);
305 strMyName = strprintf("x%u", GetRand(1000000000));
307 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
308 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
310 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
313 closesocket(hSocket);
314 hSocket = INVALID_SOCKET;
317 printf("IRC name already in use\n");
322 nErrorWait = nErrorWait * 11 / 10;
323 if (Wait(nErrorWait += 60))
330 // Get our external IP from the IRC server and re-nick before joining the channel
331 CAddress addrFromIRC;
332 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
334 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
335 if (!fUseProxy && addrFromIRC.IsRoutable())
337 // IRC lets you to re-nick
338 fGotExternalIP = true;
339 addrLocalHost.ip = addrFromIRC.ip;
340 strMyName = EncodeAddress(addrLocalHost);
341 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
345 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");
346 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");
348 int64 nStart = GetTime();
350 strLine.reserve(10000);
351 while (!fShutdown && RecvLineIRC(hSocket, strLine))
353 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
356 vector<string> vWords;
357 ParseString(strLine, ' ', vWords);
358 if (vWords.size() < 2)
364 if (vWords[1] == "352" && vWords.size() >= 8)
366 // index 7 is limited to 16 characters
367 // could get full length name at index 10, but would be different from join messages
368 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
369 printf("IRC got who\n");
372 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
374 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
375 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
376 if (strchr(pszName, '!'))
377 *strchr(pszName, '!') = '\0';
378 printf("IRC got join\n");
381 if (pszName[0] == 'u')
384 if (DecodeAddress(pszName, addr))
386 addr.nTime = GetAdjustedTime();
387 if (AddAddress(addr, 51 * 60))
388 printf("IRC got new address: %s\n", addr.ToString().c_str());
393 printf("IRC decode failed\n");
397 closesocket(hSocket);
398 hSocket = INVALID_SOCKET;
400 // IRC usually blocks TOR, so only try once
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");