1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 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 CService& addr)
33 if (addr.GetInAddr(&tmp.ip))
35 tmp.port = htons(addr.GetPort());
37 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
38 return string("u") + EncodeBase58Check(vch);
43 bool DecodeAddress(string str, CService& addr)
45 vector<unsigned char> vch;
46 if (!DecodeBase58Check(str.substr(1), vch))
50 if (vch.size() != sizeof(tmp))
52 memcpy(&tmp, &vch[0], sizeof(tmp));
54 addr = CService(tmp.ip, ntohs(tmp.port));
63 static bool Send(SOCKET hSocket, const char* pszSend)
65 if (strstr(pszSend, "PONG") != pszSend)
66 printf("IRC SENDING: %s\n", pszSend);
67 const char* psz = pszSend;
68 const char* pszEnd = psz + strlen(psz);
71 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
79 bool RecvLine(SOCKET hSocket, string& strLine)
85 int nBytes = recv(hSocket, &c, 1, 0);
93 if (strLine.size() >= 9000)
102 int nErr = WSAGetLastError();
103 if (nErr == WSAEMSGSIZE)
105 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
111 if (!strLine.empty())
116 printf("IRC socket closed\n");
122 int nErr = WSAGetLastError();
123 printf("IRC recv failed: %d\n", nErr);
130 bool RecvLineIRC(SOCKET hSocket, string& strLine)
134 bool fRet = RecvLine(hSocket, strLine);
139 vector<string> vWords;
140 ParseString(strLine, ' ', vWords);
141 if (vWords.size() >= 1 && vWords[0] == "PING")
145 Send(hSocket, strLine.c_str());
153 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
158 strLine.reserve(10000);
159 if (!RecvLineIRC(hSocket, strLine))
161 printf("IRC %s\n", strLine.c_str());
162 if (psz1 && strLine.find(psz1) != -1)
164 if (psz2 && strLine.find(psz2) != -1)
166 if (psz3 && strLine.find(psz3) != -1)
168 if (psz4 && strLine.find(psz4) != -1)
173 bool Wait(int nSeconds)
177 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
178 for (int i = 0; i < nSeconds; i++)
187 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
193 if (!RecvLineIRC(hSocket, strLine))
196 vector<string> vWords;
197 ParseString(strLine, ' ', vWords);
198 if (vWords.size() < 2)
201 if (vWords[1] == psz1)
203 printf("IRC %s\n", strLine.c_str());
210 bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
212 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
215 if (!RecvCodeLine(hSocket, "302", strLine))
218 vector<string> vWords;
219 ParseString(strLine, ' ', vWords);
220 if (vWords.size() < 4)
223 string str = vWords[3];
224 if (str.rfind("@") == string::npos)
226 string strHost = str.substr(str.rfind("@")+1);
228 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
229 // but in case another IRC is ever used this should work.
230 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
233 CNetAddr addr(strHost, true);
243 void ThreadIRCSeed(void* parg)
245 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
248 ThreadIRCSeed2(parg);
250 catch (std::exception& e) {
251 PrintExceptionContinue(&e, "ThreadIRCSeed()");
253 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
255 printf("ThreadIRCSeed exiting\n");
258 void ThreadIRCSeed2(void* parg)
260 /* Dont advertise on IRC if we don't allow incoming connections */
261 if (mapArgs.count("-connect") || fNoListen)
264 if (GetBoolArg("-noirc"))
266 printf("ThreadIRCSeed started\n");
269 bool fNameInUse = false;
273 CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org
275 CService addrIRC("irc.lfnet.org", 6667, true);
276 if (addrIRC.IsValid())
277 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 CNetAddr addrFromIRC;
332 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
334 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
335 if (!fUseProxy && addrFromIRC.IsRoutable())
337 // IRC lets you to re-nick
338 fGotExternalIP = true;
339 addrLocalHost.SetIP(addrFromIRC);
340 strMyName = EncodeAddress(addrLocalHost);
341 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
346 Send(hSocket, "JOIN #bitcoinTEST\r");
347 Send(hSocket, "WHO #bitcoinTEST\r");
349 // randomly join #bitcoin00-#bitcoin99
350 int channel_number = GetRandInt(100);
351 Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
352 Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
355 int64 nStart = GetTime();
357 strLine.reserve(10000);
358 while (!fShutdown && RecvLineIRC(hSocket, strLine))
360 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
363 vector<string> vWords;
364 ParseString(strLine, ' ', vWords);
365 if (vWords.size() < 2)
371 if (vWords[1] == "352" && vWords.size() >= 8)
373 // index 7 is limited to 16 characters
374 // could get full length name at index 10, but would be different from join messages
375 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
376 printf("IRC got who\n");
379 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
381 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
382 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
383 if (strchr(pszName, '!'))
384 *strchr(pszName, '!') = '\0';
385 printf("IRC got join\n");
388 if (pszName[0] == 'u')
391 if (DecodeAddress(pszName, addr))
393 addr.nTime = GetAdjustedTime();
394 if (AddAddress(addr, 51 * 60))
395 printf("IRC got new address: %s\n", addr.ToString().c_str());
400 printf("IRC decode failed\n");
404 closesocket(hSocket);
405 hSocket = INVALID_SOCKET;
407 if (GetTime() - nStart > 20 * 60)
413 nRetryWait = nRetryWait * 11 / 10;
414 if (!Wait(nRetryWait += 60))
429 int main(int argc, char *argv[])
432 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
434 printf("Error at WSAStartup()\n");