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.
11 using namespace boost;
13 int nGotIRCAddresses = 0;
14 bool fGotExternalIP = false;
16 void ThreadIRCSeed2(void* parg);
29 string EncodeAddress(const CAddress& addr)
35 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
36 return string("u") + EncodeBase58Check(vch);
39 bool DecodeAddress(string str, CAddress& addr)
41 vector<unsigned char> vch;
42 if (!DecodeBase58Check(str.substr(1), vch))
46 if (vch.size() != sizeof(tmp))
48 memcpy(&tmp, &vch[0], sizeof(tmp));
50 addr = CAddress(tmp.ip, ntohs(tmp.port), NODE_NETWORK);
59 static bool Send(SOCKET hSocket, const char* pszSend)
61 if (strstr(pszSend, "PONG") != pszSend)
62 printf("IRC SENDING: %s\n", pszSend);
63 const char* psz = pszSend;
64 const char* pszEnd = psz + strlen(psz);
67 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
75 bool RecvLine(SOCKET hSocket, string& strLine)
81 int nBytes = recv(hSocket, &c, 1, 0);
89 if (strLine.size() >= 9000)
98 int nErr = WSAGetLastError();
99 if (nErr == WSAEMSGSIZE)
101 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
107 if (!strLine.empty())
112 printf("IRC socket closed\n");
118 int nErr = WSAGetLastError();
119 printf("IRC recv failed: %d\n", nErr);
126 bool RecvLineIRC(SOCKET hSocket, string& strLine)
130 bool fRet = RecvLine(hSocket, strLine);
135 vector<string> vWords;
136 ParseString(strLine, ' ', vWords);
137 if (vWords.size() >= 1 && vWords[0] == "PING")
141 Send(hSocket, strLine.c_str());
149 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
154 strLine.reserve(10000);
155 if (!RecvLineIRC(hSocket, strLine))
157 printf("IRC %s\n", strLine.c_str());
158 if (psz1 && strLine.find(psz1) != -1)
160 if (psz2 && strLine.find(psz2) != -1)
162 if (psz3 && strLine.find(psz3) != -1)
164 if (psz4 && strLine.find(psz4) != -1)
169 bool Wait(int nSeconds)
173 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
174 for (int i = 0; i < nSeconds; i++)
183 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
189 if (!RecvLineIRC(hSocket, strLine))
192 vector<string> vWords;
193 ParseString(strLine, ' ', vWords);
194 if (vWords.size() < 2)
197 if (vWords[1] == psz1)
199 printf("IRC %s\n", strLine.c_str());
206 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
208 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
211 if (!RecvCodeLine(hSocket, "302", strLine))
214 vector<string> vWords;
215 ParseString(strLine, ' ', vWords);
216 if (vWords.size() < 4)
219 string str = vWords[3];
220 if (str.rfind("@") == string::npos)
222 string strHost = str.substr(str.rfind("@")+1);
224 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
225 // but in case another IRC is ever used this should work.
226 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
229 CAddress addr(strHost, 0, true);
239 void ThreadIRCSeed(void* parg)
241 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
244 ThreadIRCSeed2(parg);
246 catch (std::exception& e) {
247 PrintExceptionContinue(&e, "ThreadIRCSeed()");
249 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
251 printf("ThreadIRCSeed exiting\n");
254 void ThreadIRCSeed2(void* parg)
256 /* Dont advertise on IRC if we don't allow incoming connections */
257 if (mapArgs.count("-connect") || fNoListen)
260 if (GetBoolArg("-noirc"))
262 printf("ThreadIRCSeed started\n");
265 bool fNameInUse = false;
266 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
270 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
271 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
274 //struct hostent* phostent = gethostbyname("chat.freenode.net");
275 CAddress addrIRC("irc.lfnet.org:6667", 0, true);
276 if (addrIRC.IsValid())
277 addrConnect = addrIRC;
281 if (!ConnectSocket(addrConnect, hSocket))
283 printf("IRC connect failed\n");
284 nErrorWait = nErrorWait * 11 / 10;
285 if (Wait(nErrorWait += 60))
291 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
293 closesocket(hSocket);
294 hSocket = INVALID_SOCKET;
295 nErrorWait = nErrorWait * 11 / 10;
296 if (Wait(nErrorWait += 60))
303 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
304 strMyName = EncodeAddress(addrLocalHost);
306 strMyName = strprintf("x%u", GetRand(1000000000));
308 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
309 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
311 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
314 closesocket(hSocket);
315 hSocket = INVALID_SOCKET;
318 printf("IRC name already in use\n");
323 nErrorWait = nErrorWait * 11 / 10;
324 if (Wait(nErrorWait += 60))
331 // Get our external IP from the IRC server and re-nick before joining the channel
332 CAddress addrFromIRC;
333 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
335 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
336 if (!fUseProxy && addrFromIRC.IsRoutable())
338 // IRC lets you to re-nick
339 fGotExternalIP = true;
340 addrLocalHost.ip = addrFromIRC.ip;
341 strMyName = EncodeAddress(addrLocalHost);
342 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
346 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");
347 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");
349 int64 nStart = GetTime();
351 strLine.reserve(10000);
352 while (!fShutdown && RecvLineIRC(hSocket, strLine))
354 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
357 vector<string> vWords;
358 ParseString(strLine, ' ', vWords);
359 if (vWords.size() < 2)
365 if (vWords[1] == "352" && vWords.size() >= 8)
367 // index 7 is limited to 16 characters
368 // could get full length name at index 10, but would be different from join messages
369 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
370 printf("IRC got who\n");
373 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
375 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
376 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
377 if (strchr(pszName, '!'))
378 *strchr(pszName, '!') = '\0';
379 printf("IRC got join\n");
382 if (pszName[0] == 'u')
385 if (DecodeAddress(pszName, addr))
387 addr.nTime = GetAdjustedTime();
388 if (AddAddress(addr, 51 * 60))
389 printf("IRC got new address: %s\n", addr.ToString().c_str());
394 printf("IRC decode failed\n");
398 closesocket(hSocket);
399 hSocket = INVALID_SOCKET;
401 // IRC usually blocks TOR, so only try once
405 if (GetTime() - nStart > 20 * 60)
411 nRetryWait = nRetryWait * 11 / 10;
412 if (!Wait(nRetryWait += 60))
427 int main(int argc, char *argv[])
430 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
432 printf("Error at WSAStartup()\n");