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());
179 CNetAddr addr(strHost, true);
189 void ThreadIRCSeed(void* parg)
191 // Make this thread recognisable as the IRC seeding thread
192 RenameThread("novacoin-ircseed");
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 started\n");
227 CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org
229 CService addrIRC("irc.lfnet.org", 6667, true);
230 if (addrIRC.IsValid())
231 addrConnect = addrIRC;
234 if (!ConnectSocket(addrConnect, hSocket))
236 printf("IRC connect failed\n");
237 nErrorWait = nErrorWait * 11 / 10;
238 if (Wait(nErrorWait += 60))
244 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
246 closesocket(hSocket);
247 hSocket = INVALID_SOCKET;
248 nErrorWait = nErrorWait * 11 / 10;
249 if (Wait(nErrorWait += 60))
255 CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
258 // Don't use our IP as our nick if we're not listening
259 // or if it keeps failing because the nick is already in use.
260 if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3)
261 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
263 strMyName = strprintf("x%" PRIu64 "", GetRand(1000000000));
265 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
266 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
268 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
271 closesocket(hSocket);
272 hSocket = INVALID_SOCKET;
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)
333 if (vWords[1] == "352" && vWords.size() >= 8)
335 // index 7 is limited to 16 characters
336 // could get full length name at index 10, but would be different from join messages
337 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
338 printf("IRC got who\n");
341 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
343 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
344 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
345 if (strchr(pszName, '!'))
346 *strchr(pszName, '!') = '\0';
347 printf("IRC got join\n");
350 if (pszName[0] == 'u')
353 if (DecodeAddress(pszName, addr))
355 addr.nTime = GetAdjustedTime();
356 if (addrman.Add(addr, addrConnect, 51 * 60))
357 printf("IRC got new address: %s\n", addr.ToString().c_str());
362 printf("IRC decode failed\n");
366 closesocket(hSocket);
367 hSocket = INVALID_SOCKET;
369 if (GetTime() - nStart > 20 * 60)
375 nRetryWait = nRetryWait * 11 / 10;
376 if (!Wait(nRetryWait += 60))
391 int main(int argc, char *argv[])
394 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
396 printf("Error at WSAStartup()\n");