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.
10 #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
13 using namespace boost;
15 int nGotIRCAddresses = 0;
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 RecvLineIRC(SOCKET hSocket, string& strLine)
83 bool fRet = RecvLine(hSocket, strLine);
88 vector<string> vWords;
89 ParseString(strLine, ' ', vWords);
90 if (vWords.size() >= 1 && vWords[0] == "PING")
94 Send(hSocket, strLine.c_str());
102 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
107 strLine.reserve(10000);
108 if (!RecvLineIRC(hSocket, strLine))
110 printf("IRC %s\n", strLine.c_str());
111 if (psz1 && strLine.find(psz1) != string::npos)
113 if (psz2 && strLine.find(psz2) != string::npos)
115 if (psz3 && strLine.find(psz3) != string::npos)
117 if (psz4 && strLine.find(psz4) != string::npos)
122 bool Wait(int nSeconds)
126 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
127 for (int i = 0; i < nSeconds; i++)
136 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
142 if (!RecvLineIRC(hSocket, strLine))
145 vector<string> vWords;
146 ParseString(strLine, ' ', vWords);
147 if (vWords.size() < 2)
150 if (vWords[1] == psz1)
152 printf("IRC %s\n", strLine.c_str());
159 bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
161 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
164 if (!RecvCodeLine(hSocket, "302", strLine))
167 vector<string> vWords;
168 ParseString(strLine, ' ', vWords);
169 if (vWords.size() < 4)
172 string str = vWords[3];
173 if (str.rfind("@") == string::npos)
175 string strHost = str.substr(str.rfind("@")+1);
177 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
178 // but in case another IRC is ever used this should work.
179 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
180 CNetAddr addr(strHost, true);
190 void ThreadIRCSeed(void* parg)
192 // Make this thread recognisable as the IRC seeding thread
193 RenameThread("novacoin-ircseed");
195 printf("ThreadIRCSeed started\n");
199 ThreadIRCSeed2(parg);
201 catch (std::exception& e) {
202 PrintExceptionContinue(&e, "ThreadIRCSeed()");
204 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
206 printf("ThreadIRCSeed exited\n");
209 void ThreadIRCSeed2(void* parg)
211 // Don't connect to IRC if we won't use IPv4 connections.
212 if (IsLimited(NET_IPV4))
215 // ... or if we won't make outbound connections and won't accept inbound ones.
216 if (mapArgs.count("-connect") && fNoListen)
219 // ... or if IRC is not enabled.
220 if (!GetBoolArg("-irc", true))
223 printf("ThreadIRCSeed trying to connect...\n");
231 const uint16_t nIrcPort = 6667;
232 CService addrConnect("92.243.23.21", nIrcPort); // irc.lfnet.org
234 CService addrIRC("irc.lfnet.org", nIrcPort, true);
235 if (addrIRC.IsValid())
236 addrConnect = addrIRC;
239 if (!ConnectSocket(addrConnect, hSocket))
241 printf("IRC connect failed\n");
242 nErrorWait = nErrorWait * 11 / 10;
243 if (Wait(nErrorWait += 60))
249 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
251 closesocket(hSocket);
252 hSocket = INVALID_SOCKET;
253 nErrorWait = nErrorWait * 11 / 10;
254 if (Wait(nErrorWait += 60))
260 CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
263 // Don't use our IP as our nick if we're not listening
264 // or if it keeps failing because the nick is already in use.
265 if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3)
266 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
268 strMyName = strprintf("x%" PRIu64 "", GetRand(1000000000));
270 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
271 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
273 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
276 closesocket(hSocket);
277 hSocket = INVALID_SOCKET;
280 printf("IRC name already in use\n");
285 nErrorWait = nErrorWait * 11 / 10;
286 if (Wait(nErrorWait += 60))
294 // Get our external IP from the IRC server and re-nick before joining the channel
295 CNetAddr addrFromIRC;
296 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
298 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
299 // Don't use our IP as our nick if we're not listening
300 if (!fNoListen && addrFromIRC.IsRoutable())
302 // IRC lets you to re-nick
303 AddLocal(addrFromIRC, LOCAL_IRC);
304 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
305 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
310 Send(hSocket, "JOIN #novacoinTEST2\r");
311 Send(hSocket, "WHO #novacoinTEST2\r");
313 // randomly join #novacoin00-#novacoin05
314 // int channel_number = GetRandInt(5);
316 // Channel number is always 0 for initial release
317 int channel_number = 0;
318 Send(hSocket, strprintf("JOIN #novacoin%02d\r", channel_number).c_str());
319 Send(hSocket, strprintf("WHO #novacoin%02d\r", channel_number).c_str());
322 int64_t nStart = GetTime();
324 strLine.reserve(10000);
325 while (!fShutdown && RecvLineIRC(hSocket, strLine))
327 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
330 vector<string> vWords;
331 ParseString(strLine, ' ', vWords);
332 if (vWords.size() < 2)
337 if (vWords[1] == "352" && vWords.size() >= 8)
339 // index 7 is limited to 16 characters
340 // could get full length name at index 10, but would be different from join messages
341 strName = vWords[7].c_str();
342 printf("IRC got who\n");
345 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
347 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
348 strName = vWords[0].substr(1, vWords[0].find('!', 1) - 1);
349 printf("IRC got join\n");
352 if (boost::algorithm::starts_with(strName, "u"))
355 if (DecodeAddress(strName, addr))
357 addr.nTime = GetAdjustedTime();
358 if (addrman.Add(addr, addrConnect, 51 * 60))
359 printf("IRC got new address: %s\n", addr.ToString().c_str());
364 printf("IRC decode failed\n");
368 closesocket(hSocket);
369 hSocket = INVALID_SOCKET;
371 if (GetTime() - nStart > 20 * 60)
377 nRetryWait = nRetryWait * 11 / 10;
378 if (!Wait(nRetryWait += 60))
393 int main(int argc, char *argv[])
396 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
398 printf("Error at WSAStartup()\n");