1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
7 int nGotIRCAddresses = 0;
8 bool fGotExternalIP = false;
10 void ThreadIRCSeed2(void* parg);
23 string EncodeAddress(const CAddress& addr)
29 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
30 return string("u") + EncodeBase58Check(vch);
33 bool DecodeAddress(string str, CAddress& addr)
35 vector<unsigned char> vch;
36 if (!DecodeBase58Check(str.substr(1), vch))
40 if (vch.size() != sizeof(tmp))
42 memcpy(&tmp, &vch[0], sizeof(tmp));
44 addr = CAddress(tmp.ip, tmp.port, NODE_NETWORK);
53 static bool Send(SOCKET hSocket, const char* pszSend)
55 if (strstr(pszSend, "PONG") != pszSend)
56 printf("IRC SENDING: %s\n", pszSend);
57 const char* psz = pszSend;
58 const char* pszEnd = psz + strlen(psz);
61 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
69 bool RecvLine(SOCKET hSocket, string& strLine)
75 int nBytes = recv(hSocket, &c, 1, 0);
83 if (strLine.size() >= 9000)
92 int nErr = WSAGetLastError();
93 if (nErr == WSAEMSGSIZE)
95 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
101 if (!strLine.empty())
106 printf("IRC socket closed\n");
112 int nErr = WSAGetLastError();
113 printf("IRC recv failed: %d\n", nErr);
120 bool RecvLineIRC(SOCKET hSocket, string& strLine)
124 bool fRet = RecvLine(hSocket, strLine);
129 vector<string> vWords;
130 ParseString(strLine, ' ', vWords);
131 if (vWords.size() >= 1 && vWords[0] == "PING")
135 Send(hSocket, strLine.c_str());
143 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
148 strLine.reserve(10000);
149 if (!RecvLineIRC(hSocket, strLine))
151 printf("IRC %s\n", strLine.c_str());
152 if (psz1 && strLine.find(psz1) != -1)
154 if (psz2 && strLine.find(psz2) != -1)
156 if (psz3 && strLine.find(psz3) != -1)
158 if (psz4 && strLine.find(psz4) != -1)
163 bool Wait(int nSeconds)
167 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
168 for (int i = 0; i < nSeconds; i++)
177 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
183 if (!RecvLineIRC(hSocket, strLine))
186 vector<string> vWords;
187 ParseString(strLine, ' ', vWords);
188 if (vWords.size() < 2)
191 if (vWords[1] == psz1)
193 printf("IRC %s\n", strLine.c_str());
200 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
202 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
205 if (!RecvCodeLine(hSocket, "302", strLine))
208 vector<string> vWords;
209 ParseString(strLine, ' ', vWords);
210 if (vWords.size() < 4)
213 string str = vWords[3];
214 if (str.rfind("@") == string::npos)
216 string strHost = str.substr(str.rfind("@")+1);
218 unsigned int a=0, b=0, c=0, d=0;
219 if (sscanf(strHost.c_str(), "%u.%u.%u.%u", &a, &b, &c, &d) == 4 &&
220 inet_addr(strHost.c_str()) != INADDR_NONE)
222 printf("GetIPFromIRC() userhost is IP %s\n", strHost.c_str());
223 ipRet = CAddress(strHost).ip;
227 // Hybrid IRC used by lfnet always returns IP when you userhost yourself,
228 // but in case another IRC is ever used this should work.
229 printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
232 struct hostent* phostent = gethostbyname(strHost.c_str());
233 if (!phostent || !phostent->h_addr_list || !phostent->h_addr_list[0])
235 ipRet = *(u_long*)phostent->h_addr_list[0];
243 void ThreadIRCSeed(void* parg)
245 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
248 ThreadIRCSeed2(parg);
250 catch (std::exception& e) {
251 PrintExceptionContinue(&e, "ThreadIRCSeed()");
253 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
255 printf("ThreadIRCSeed exiting\n");
258 void ThreadIRCSeed2(void* parg)
260 /* Dont advertise on IRC if we don't allow incoming connections */
261 if (mapArgs.count("-connect") || fNoListen)
264 if (GetBoolArg("-noirc"))
266 printf("ThreadIRCSeed started\n");
269 bool fNameInUse = false;
270 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
274 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
275 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
278 //struct hostent* phostent = gethostbyname("chat.freenode.net");
279 struct hostent* phostent = gethostbyname("irc.lfnet.org");
280 if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])
281 addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));
285 if (!ConnectSocket(addrConnect, hSocket))
287 printf("IRC connect failed\n");
288 nErrorWait = nErrorWait * 11 / 10;
289 if (Wait(nErrorWait += 60))
295 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
297 closesocket(hSocket);
298 hSocket = INVALID_SOCKET;
299 nErrorWait = nErrorWait * 11 / 10;
300 if (Wait(nErrorWait += 60))
307 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
308 strMyName = EncodeAddress(addrLocalHost);
310 strMyName = strprintf("x%u", GetRand(1000000000));
312 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
313 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
315 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
318 closesocket(hSocket);
319 hSocket = INVALID_SOCKET;
322 printf("IRC name already in use\n");
327 nErrorWait = nErrorWait * 11 / 10;
328 if (Wait(nErrorWait += 60))
335 // Get our external IP from the IRC server and re-nick before joining the channel
336 CAddress addrFromIRC;
337 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
339 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
340 if (!fUseProxy && addrFromIRC.IsRoutable())
342 // IRC lets you to re-nick
343 fGotExternalIP = true;
344 addrLocalHost.ip = addrFromIRC.ip;
345 strMyName = EncodeAddress(addrLocalHost);
346 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
350 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");
351 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");
353 int64 nStart = GetTime();
355 strLine.reserve(10000);
356 while (!fShutdown && RecvLineIRC(hSocket, strLine))
358 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
361 vector<string> vWords;
362 ParseString(strLine, ' ', vWords);
363 if (vWords.size() < 2)
369 if (vWords[1] == "352" && vWords.size() >= 8)
371 // index 7 is limited to 16 characters
372 // could get full length name at index 10, but would be different from join messages
373 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
374 printf("IRC got who\n");
377 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
380 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
381 if (strchr(pszName, '!'))
382 *strchr(pszName, '!') = '\0';
383 printf("IRC got join\n");
386 if (pszName[0] == 'u')
389 if (DecodeAddress(pszName, addr))
391 addr.nTime = GetAdjustedTime();
392 if (AddAddress(addr, 51 * 60))
393 printf("IRC got new address\n");
398 printf("IRC decode failed\n");
402 closesocket(hSocket);
403 hSocket = INVALID_SOCKET;
405 // IRC usually blocks TOR, so only try once
409 if (GetTime() - nStart > 20 * 60)
415 nRetryWait = nRetryWait * 11 / 10;
416 if (!Wait(nRetryWait += 60))
431 int main(int argc, char *argv[])
434 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
436 printf("Error at WSAStartup()\n");