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 int nGotIRCAddresses = 0;
12 bool fGotExternalIP = false;
14 void ThreadIRCSeed2(void* parg);
27 string EncodeAddress(const CAddress& addr)
33 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
34 return string("u") + EncodeBase58Check(vch);
37 bool DecodeAddress(string str, CAddress& addr)
39 vector<unsigned char> vch;
40 if (!DecodeBase58Check(str.substr(1), vch))
44 if (vch.size() != sizeof(tmp))
46 memcpy(&tmp, &vch[0], sizeof(tmp));
48 addr = CAddress(tmp.ip, ntohs(tmp.port), NODE_NETWORK);
57 static bool Send(SOCKET hSocket, const char* pszSend)
59 if (strstr(pszSend, "PONG") != pszSend)
60 printf("IRC SENDING: %s\n", pszSend);
61 const char* psz = pszSend;
62 const char* pszEnd = psz + strlen(psz);
65 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
73 bool RecvLine(SOCKET hSocket, string& strLine)
79 int nBytes = recv(hSocket, &c, 1, 0);
87 if (strLine.size() >= 9000)
96 int nErr = WSAGetLastError();
97 if (nErr == WSAEMSGSIZE)
99 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
105 if (!strLine.empty())
110 printf("IRC socket closed\n");
116 int nErr = WSAGetLastError();
117 printf("IRC recv failed: %d\n", nErr);
124 bool RecvLineIRC(SOCKET hSocket, string& strLine)
128 bool fRet = RecvLine(hSocket, strLine);
133 vector<string> vWords;
134 ParseString(strLine, ' ', vWords);
135 if (vWords.size() >= 1 && vWords[0] == "PING")
139 Send(hSocket, strLine.c_str());
147 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
152 strLine.reserve(10000);
153 if (!RecvLineIRC(hSocket, strLine))
155 printf("IRC %s\n", strLine.c_str());
156 if (psz1 && strLine.find(psz1) != -1)
158 if (psz2 && strLine.find(psz2) != -1)
160 if (psz3 && strLine.find(psz3) != -1)
162 if (psz4 && strLine.find(psz4) != -1)
167 bool Wait(int nSeconds)
171 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
172 for (int i = 0; i < nSeconds; i++)
181 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
187 if (!RecvLineIRC(hSocket, strLine))
190 vector<string> vWords;
191 ParseString(strLine, ' ', vWords);
192 if (vWords.size() < 2)
195 if (vWords[1] == psz1)
197 printf("IRC %s\n", strLine.c_str());
204 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
206 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
209 if (!RecvCodeLine(hSocket, "302", strLine))
212 vector<string> vWords;
213 ParseString(strLine, ' ', vWords);
214 if (vWords.size() < 4)
217 string str = vWords[3];
218 if (str.rfind("@") == string::npos)
220 string strHost = str.substr(str.rfind("@")+1);
222 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
223 // but in case another IRC is ever used this should work.
224 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
227 CAddress addr(strHost, 0, true);
237 void ThreadIRCSeed(void* parg)
239 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
242 ThreadIRCSeed2(parg);
244 catch (std::exception& e) {
245 PrintExceptionContinue(&e, "ThreadIRCSeed()");
247 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
249 printf("ThreadIRCSeed exiting\n");
252 void ThreadIRCSeed2(void* parg)
254 /* Dont advertise on IRC if we don't allow incoming connections */
255 if (mapArgs.count("-connect") || fNoListen)
258 if (GetBoolArg("-noirc"))
260 printf("ThreadIRCSeed started\n");
263 bool fNameInUse = false;
264 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
268 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
269 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
272 //struct hostent* phostent = gethostbyname("chat.freenode.net");
273 CAddress addrIRC("irc.lfnet.org:6667", 0, true);
274 if (addrIRC.IsValid())
275 addrConnect = addrIRC;
279 if (!ConnectSocket(addrConnect, hSocket))
281 printf("IRC connect failed\n");
282 nErrorWait = nErrorWait * 11 / 10;
283 if (Wait(nErrorWait += 60))
289 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
291 closesocket(hSocket);
292 hSocket = INVALID_SOCKET;
293 nErrorWait = nErrorWait * 11 / 10;
294 if (Wait(nErrorWait += 60))
301 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
302 strMyName = EncodeAddress(addrLocalHost);
304 strMyName = strprintf("x%u", GetRand(1000000000));
306 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
307 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
309 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
312 closesocket(hSocket);
313 hSocket = INVALID_SOCKET;
316 printf("IRC name already in use\n");
321 nErrorWait = nErrorWait * 11 / 10;
322 if (Wait(nErrorWait += 60))
329 // Get our external IP from the IRC server and re-nick before joining the channel
330 CAddress addrFromIRC;
331 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
333 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
334 if (!fUseProxy && addrFromIRC.IsRoutable())
336 // IRC lets you to re-nick
337 fGotExternalIP = true;
338 addrLocalHost.ip = addrFromIRC.ip;
339 strMyName = EncodeAddress(addrLocalHost);
340 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
344 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");
345 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");
347 int64 nStart = GetTime();
349 strLine.reserve(10000);
350 while (!fShutdown && RecvLineIRC(hSocket, strLine))
352 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
355 vector<string> vWords;
356 ParseString(strLine, ' ', vWords);
357 if (vWords.size() < 2)
363 if (vWords[1] == "352" && vWords.size() >= 8)
365 // index 7 is limited to 16 characters
366 // could get full length name at index 10, but would be different from join messages
367 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
368 printf("IRC got who\n");
371 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
373 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
374 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
375 if (strchr(pszName, '!'))
376 *strchr(pszName, '!') = '\0';
377 printf("IRC got join\n");
380 if (pszName[0] == 'u')
383 if (DecodeAddress(pszName, addr))
385 addr.nTime = GetAdjustedTime();
386 if (AddAddress(addr, 51 * 60))
387 printf("IRC got new address: %s\n", addr.ToString().c_str());
392 printf("IRC decode failed\n");
396 closesocket(hSocket);
397 hSocket = INVALID_SOCKET;
399 // IRC usually blocks TOR, so only try once
403 if (GetTime() - nStart > 20 * 60)
409 nRetryWait = nRetryWait * 11 / 10;
410 if (!Wait(nRetryWait += 60))
425 int main(int argc, char *argv[])
428 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
430 printf("Error at WSAStartup()\n");