1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
12 using namespace boost;
14 int nGotIRCAddresses = 0;
16 void ThreadIRCSeed2(void* parg);
29 string EncodeAddress(const CService& addr)
32 if (addr.GetInAddr(&tmp.ip))
34 tmp.port = htons(addr.GetPort());
36 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
37 return string("u") + EncodeBase58Check(vch);
42 bool DecodeAddress(string str, CService& addr)
44 vector<unsigned char> vch;
45 if (!DecodeBase58Check(str.substr(1), vch))
49 if (vch.size() != sizeof(tmp))
51 memcpy(&tmp, &vch[0], sizeof(tmp));
53 addr = CService(tmp.ip, ntohs(tmp.port));
62 static bool Send(SOCKET hSocket, const char* pszSend)
64 if (strstr(pszSend, "PONG") != pszSend)
65 printf("IRC SENDING: %s\n", pszSend);
66 const char* psz = pszSend;
67 const char* pszEnd = psz + strlen(psz);
70 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
78 bool RecvLineIRC(SOCKET hSocket, string& strLine)
82 bool fRet = RecvLine(hSocket, strLine);
87 vector<string> vWords;
88 ParseString(strLine, ' ', vWords);
89 if (vWords.size() >= 1 && vWords[0] == "PING")
93 Send(hSocket, strLine.c_str());
101 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
106 strLine.reserve(10000);
107 if (!RecvLineIRC(hSocket, strLine))
109 printf("IRC %s\n", strLine.c_str());
110 if (psz1 && strLine.find(psz1) != string::npos)
112 if (psz2 && strLine.find(psz2) != string::npos)
114 if (psz3 && strLine.find(psz3) != string::npos)
116 if (psz4 && strLine.find(psz4) != string::npos)
121 bool Wait(int nSeconds)
125 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
126 for (int i = 0; i < nSeconds; i++)
135 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
141 if (!RecvLineIRC(hSocket, strLine))
144 vector<string> vWords;
145 ParseString(strLine, ' ', vWords);
146 if (vWords.size() < 2)
149 if (vWords[1] == psz1)
151 printf("IRC %s\n", strLine.c_str());
158 bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
160 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
163 if (!RecvCodeLine(hSocket, "302", strLine))
166 vector<string> vWords;
167 ParseString(strLine, ' ', vWords);
168 if (vWords.size() < 4)
171 string str = vWords[3];
172 if (str.rfind("@") == string::npos)
174 string strHost = str.substr(str.rfind("@")+1);
176 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
177 // but in case another IRC is ever used this should work.
178 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
181 CNetAddr addr(strHost, true);
191 void ThreadIRCSeed(void* parg)
193 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
196 ThreadIRCSeed2(parg);
198 catch (std::exception& e) {
199 PrintExceptionContinue(&e, "ThreadIRCSeed()");
201 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
203 printf("ThreadIRCSeed exited\n");
206 void ThreadIRCSeed2(void* parg)
208 /* Dont advertise on IRC if we don't allow incoming connections */
209 if (mapArgs.count("-connect") || fNoListen)
212 if (!GetBoolArg("-irc", false))
215 printf("ThreadIRCSeed started\n");
221 CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org
223 CService addrIRC("irc.lfnet.org", 6667, true);
224 if (addrIRC.IsValid())
225 addrConnect = addrIRC;
228 if (!ConnectSocket(addrConnect, hSocket))
230 printf("IRC connect failed\n");
231 nErrorWait = nErrorWait * 11 / 10;
232 if (Wait(nErrorWait += 60))
238 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
240 closesocket(hSocket);
241 hSocket = INVALID_SOCKET;
242 nErrorWait = nErrorWait * 11 / 10;
243 if (Wait(nErrorWait += 60))
249 CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
252 if (GetLocal(addrLocal, &addrIPv4))
253 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
255 strMyName = strprintf("x%u", GetRand(1000000000));
257 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
258 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
260 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
263 closesocket(hSocket);
264 hSocket = INVALID_SOCKET;
267 printf("IRC name already in use\n");
271 nErrorWait = nErrorWait * 11 / 10;
272 if (Wait(nErrorWait += 60))
279 // Get our external IP from the IRC server and re-nick before joining the channel
280 CNetAddr addrFromIRC;
281 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
283 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
284 if (!fUseProxy && addrFromIRC.IsRoutable())
286 // IRC lets you to re-nick
287 AddLocal(addrFromIRC, LOCAL_IRC);
288 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
289 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
294 Send(hSocket, "JOIN #bitcoinTEST\r");
295 Send(hSocket, "WHO #bitcoinTEST\r");
297 // randomly join #bitcoin00-#bitcoin99
298 int channel_number = GetRandInt(100);
299 Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
300 Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
303 int64 nStart = GetTime();
305 strLine.reserve(10000);
306 while (!fShutdown && RecvLineIRC(hSocket, strLine))
308 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
311 vector<string> vWords;
312 ParseString(strLine, ' ', vWords);
313 if (vWords.size() < 2)
319 if (vWords[1] == "352" && vWords.size() >= 8)
321 // index 7 is limited to 16 characters
322 // could get full length name at index 10, but would be different from join messages
323 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
324 printf("IRC got who\n");
327 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
330 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
331 if (strchr(pszName, '!'))
332 *strchr(pszName, '!') = '\0';
333 printf("IRC got join\n");
336 if (pszName[0] == 'u')
339 if (DecodeAddress(pszName, addr))
341 addr.nTime = GetAdjustedTime();
342 if (addrman.Add(addr, addrConnect, 51 * 60))
343 printf("IRC got new address: %s\n", addr.ToString().c_str());
348 printf("IRC decode failed\n");
352 closesocket(hSocket);
353 hSocket = INVALID_SOCKET;
355 if (GetTime() - nStart > 20 * 60)
361 nRetryWait = nRetryWait * 11 / 10;
362 if (!Wait(nRetryWait += 60))
377 int main(int argc, char *argv[])
380 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
382 printf("Error at WSAStartup()\n");