1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2013 NovaCoin Developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
13 using namespace boost;
15 int nGotIRCAddresses = 0;
16 bool fGotExternalIP = false;
18 void ThreadIRCSeed2(void* parg);
31 string EncodeAddress(const CService& addr)
34 if (addr.GetInAddr(&tmp.ip))
36 tmp.port = htons(addr.GetPort());
38 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
39 return string("u") + EncodeBase58Check(vch);
44 bool DecodeAddress(string str, CService& addr)
46 vector<unsigned char> vch;
47 if (!DecodeBase58Check(str.substr(1), vch))
51 if (vch.size() != sizeof(tmp))
53 memcpy(&tmp, &vch[0], sizeof(tmp));
55 addr = CService(tmp.ip, ntohs(tmp.port));
64 static bool Send(SOCKET hSocket, const char* pszSend)
66 if (strstr(pszSend, "PONG") != pszSend)
67 printf("IRC SENDING: %s\n", pszSend);
68 const char* psz = pszSend;
69 const char* pszEnd = psz + strlen(psz);
72 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
80 bool RecvLineIRC(SOCKET hSocket, string& strLine)
84 bool fRet = RecvLine(hSocket, strLine);
89 vector<string> vWords;
90 ParseString(strLine, ' ', vWords);
91 if (vWords.size() >= 1 && vWords[0] == "PING")
95 Send(hSocket, strLine.c_str());
103 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
108 strLine.reserve(10000);
109 if (!RecvLineIRC(hSocket, strLine))
111 printf("IRC %s\n", strLine.c_str());
112 if (psz1 && strLine.find(psz1) != string::npos)
114 if (psz2 && strLine.find(psz2) != string::npos)
116 if (psz3 && strLine.find(psz3) != string::npos)
118 if (psz4 && strLine.find(psz4) != string::npos)
123 bool Wait(int nSeconds)
127 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
128 for (int i = 0; i < nSeconds; i++)
137 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
143 if (!RecvLineIRC(hSocket, strLine))
146 vector<string> vWords;
147 ParseString(strLine, ' ', vWords);
148 if (vWords.size() < 2)
151 if (vWords[1] == psz1)
153 printf("IRC %s\n", strLine.c_str());
160 bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
162 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
165 if (!RecvCodeLine(hSocket, "302", strLine))
168 vector<string> vWords;
169 ParseString(strLine, ' ', vWords);
170 if (vWords.size() < 4)
173 string str = vWords[3];
174 if (str.rfind("@") == string::npos)
176 string strHost = str.substr(str.rfind("@")+1);
178 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
179 // but in case another IRC is ever used this should work.
180 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
183 CNetAddr addr(strHost, true);
193 void ThreadIRCSeed(void* parg)
195 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
198 ThreadIRCSeed2(parg);
200 catch (std::exception& e) {
201 PrintExceptionContinue(&e, "ThreadIRCSeed()");
203 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
205 printf("ThreadIRCSeed exiting\n");
208 void ThreadIRCSeed2(void* parg)
210 /* Dont advertise on IRC if we don't allow incoming connections */
211 if (mapArgs.count("-connect") || fNoListen)
214 if (!GetBoolArg("-irc", true))
217 printf("ThreadIRCSeed started\n");
220 bool fNameInUse = false;
224 CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org
226 CService addrIRC("irc.lfnet.org", 6667, true);
227 if (addrIRC.IsValid())
228 addrConnect = addrIRC;
231 if (!ConnectSocket(addrConnect, hSocket))
233 printf("IRC connect failed\n");
234 nErrorWait = nErrorWait * 11 / 10;
235 if (Wait(nErrorWait += 60))
241 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
243 closesocket(hSocket);
244 hSocket = INVALID_SOCKET;
245 nErrorWait = nErrorWait * 11 / 10;
246 if (Wait(nErrorWait += 60))
253 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
254 strMyName = EncodeAddress(addrLocalHost);
256 strMyName = strprintf("x%u", GetRand(1000000000));
258 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
259 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
261 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
264 closesocket(hSocket);
265 hSocket = INVALID_SOCKET;
268 printf("IRC name already in use\n");
273 nErrorWait = nErrorWait * 11 / 10;
274 if (Wait(nErrorWait += 60))
281 // Get our external IP from the IRC server and re-nick before joining the channel
282 CNetAddr addrFromIRC;
283 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
285 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
286 if (!fUseProxy && addrFromIRC.IsRoutable())
288 // IRC lets you to re-nick
289 fGotExternalIP = true;
290 addrLocalHost.SetIP(addrFromIRC);
291 strMyName = EncodeAddress(addrLocalHost);
292 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
297 Send(hSocket, "JOIN #novacoinTEST\r");
298 Send(hSocket, "WHO #novacoinTEST\r");
300 // randomly join #novacoin00-#novacoin05
301 // int channel_number = GetRandInt(5);
303 // Channel number is always 0 for initial release
304 int channel_number = 0;
305 Send(hSocket, strprintf("JOIN #novacoin%02d\r", channel_number).c_str());
306 Send(hSocket, strprintf("WHO #novacoin%02d\r", channel_number).c_str());
309 int64 nStart = GetTime();
311 strLine.reserve(10000);
312 while (!fShutdown && RecvLineIRC(hSocket, strLine))
314 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
317 vector<string> vWords;
318 ParseString(strLine, ' ', vWords);
319 if (vWords.size() < 2)
325 if (vWords[1] == "352" && vWords.size() >= 8)
327 // index 7 is limited to 16 characters
328 // could get full length name at index 10, but would be different from join messages
329 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
330 printf("IRC got who\n");
333 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
335 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
336 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
337 if (strchr(pszName, '!'))
338 *strchr(pszName, '!') = '\0';
339 printf("IRC got join\n");
342 if (pszName[0] == 'u')
345 if (DecodeAddress(pszName, addr))
347 addr.nTime = GetAdjustedTime();
348 if (addrman.Add(addr, addrConnect, 51 * 60))
349 printf("IRC got new address: %s\n", addr.ToString().c_str());
354 printf("IRC decode failed\n");
358 closesocket(hSocket);
359 hSocket = INVALID_SOCKET;
361 if (GetTime() - nStart > 20 * 60)
367 nRetryWait = nRetryWait * 11 / 10;
368 if (!Wait(nRetryWait += 60))
383 int main(int argc, char *argv[])
386 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
388 printf("Error at WSAStartup()\n");