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 int nGotIRCAddresses = 0;
14 void ThreadIRCSeed2(void* parg);
27 string EncodeAddress(const CService& addr)
30 if (addr.GetInAddr(&tmp.ip))
32 tmp.port = htons(addr.GetPort());
34 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
35 return string("u") + EncodeBase58Check(vch);
40 bool DecodeAddress(string str, CService& addr)
42 vector<unsigned char> vch;
43 if (!DecodeBase58Check(str.substr(1), vch))
47 if (vch.size() != sizeof(tmp))
49 memcpy(&tmp, &vch[0], sizeof(tmp));
51 addr = CService(tmp.ip, ntohs(tmp.port));
60 static bool Send(SOCKET hSocket, const char* pszSend)
62 if (strstr(pszSend, "PONG") != pszSend)
63 printf("IRC SENDING: %s\n", pszSend);
64 const char* psz = pszSend;
65 const char* pszEnd = psz + strlen(psz);
68 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
76 bool RecvLineIRC(SOCKET hSocket, string& strLine)
80 bool fRet = RecvLine(hSocket, strLine);
85 vector<string> vWords;
86 ParseString(strLine, ' ', vWords);
87 if (vWords.size() >= 1 && vWords[0] == "PING")
91 Send(hSocket, strLine.c_str());
99 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
104 strLine.reserve(10000);
105 if (!RecvLineIRC(hSocket, strLine))
107 printf("IRC %s\n", strLine.c_str());
108 if (psz1 && strLine.find(psz1) != string::npos)
110 if (psz2 && strLine.find(psz2) != string::npos)
112 if (psz3 && strLine.find(psz3) != string::npos)
114 if (psz4 && strLine.find(psz4) != string::npos)
119 bool Wait(int nSeconds)
123 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
124 for (int i = 0; i < nSeconds; i++)
133 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
139 if (!RecvLineIRC(hSocket, strLine))
142 vector<string> vWords;
143 ParseString(strLine, ' ', vWords);
144 if (vWords.size() < 2)
147 if (vWords[1] == psz1)
149 printf("IRC %s\n", strLine.c_str());
156 bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
158 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
161 if (!RecvCodeLine(hSocket, "302", strLine))
164 vector<string> vWords;
165 ParseString(strLine, ' ', vWords);
166 if (vWords.size() < 4)
169 string str = vWords[3];
170 if (str.rfind("@") == string::npos)
172 string strHost = str.substr(str.rfind("@")+1);
174 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
175 // but in case another IRC is ever used this should work.
176 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
177 CNetAddr addr(strHost, true);
187 void ThreadIRCSeed(void* parg)
189 // Make this thread recognisable as the IRC seeding thread
190 RenameThread("novacoin-ircseed");
192 printf("ThreadIRCSeed started\n");
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 // Don't connect to IRC if we won't use IPv4 connections.
209 if (IsLimited(NET_IPV4))
212 // ... or if we won't make outbound connections and won't accept inbound ones.
213 if (mapArgs.count("-connect") && fNoListen)
216 // ... or if IRC is not enabled.
217 if (!GetBoolArg("-irc", true))
220 printf("ThreadIRCSeed trying to connect...\n");
228 const uint16_t nIrcPort = 6667;
229 CService addrConnect("92.243.23.21", nIrcPort); // irc.lfnet.org
231 CService addrIRC("irc.lfnet.org", nIrcPort, true);
232 if (addrIRC.IsValid())
233 addrConnect = addrIRC;
236 if (!ConnectSocket(addrConnect, hSocket))
238 printf("IRC connect failed\n");
239 nErrorWait = nErrorWait * 11 / 10;
240 if (Wait(nErrorWait += 60))
246 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
248 CloseSocket(hSocket);
249 nErrorWait = nErrorWait * 11 / 10;
250 if (Wait(nErrorWait += 60))
256 CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
259 // Don't use our IP as our nick if we're not listening
260 // or if it keeps failing because the nick is already in use.
261 if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3)
262 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
263 if (strMyName.empty())
264 strMyName = strprintf("x%" PRIu64 "", GetRand(1000000000));
266 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
267 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
269 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
272 CloseSocket(hSocket);
275 printf("IRC name already in use\n");
280 nErrorWait = nErrorWait * 11 / 10;
281 if (Wait(nErrorWait += 60))
289 // Get our external IP from the IRC server and re-nick before joining the channel
290 CNetAddr addrFromIRC;
291 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
293 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
294 // Don't use our IP as our nick if we're not listening
295 if (!fNoListen && addrFromIRC.IsRoutable())
297 // IRC lets you to re-nick
298 AddLocal(addrFromIRC, LOCAL_IRC);
299 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
300 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
305 Send(hSocket, "JOIN #novacoinTEST2\r");
306 Send(hSocket, "WHO #novacoinTEST2\r");
308 // randomly join #novacoin00-#novacoin05
309 // int channel_number = GetRandInt(5);
311 // Channel number is always 0 for initial release
312 int channel_number = 0;
313 Send(hSocket, strprintf("JOIN #novacoin%02d\r", channel_number).c_str());
314 Send(hSocket, strprintf("WHO #novacoin%02d\r", channel_number).c_str());
317 int64_t nStart = GetTime();
319 strLine.reserve(10000);
320 while (!fShutdown && RecvLineIRC(hSocket, strLine))
322 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
325 vector<string> vWords;
326 ParseString(strLine, ' ', vWords);
327 if (vWords.size() < 2)
332 if (vWords[1] == "352" && vWords.size() >= 8)
334 // index 7 is limited to 16 characters
335 // could get full length name at index 10, but would be different from join messages
337 printf("IRC got who\n");
340 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
342 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
343 strName = vWords[0].substr(1, vWords[0].find('!', 1) - 1);
344 printf("IRC got join\n");
347 if (strName.compare(0,1, "u") == 0)
350 if (DecodeAddress(strName, addr))
352 addr.nTime = GetAdjustedTime();
353 if (addrman.Add(addr, addrConnect, 51 * 60))
354 printf("IRC got new address: %s\n", addr.ToString().c_str());
359 printf("IRC decode failed\n");
363 CloseSocket(hSocket);
365 if (GetTime() - nStart > 20 * 60)
371 nRetryWait = nRetryWait * 11 / 10;
372 if (!Wait(nRetryWait += 60))
387 int main(int argc, char *argv[])
390 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
392 printf("Error at WSAStartup()\n");