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;
9 void ThreadIRCSeed2(void* parg);
22 string EncodeAddress(const CAddress& addr)
28 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
29 return string("u") + EncodeBase58Check(vch);
32 bool DecodeAddress(string str, CAddress& addr)
34 vector<unsigned char> vch;
35 if (!DecodeBase58Check(str.substr(1), vch))
39 if (vch.size() != sizeof(tmp))
41 memcpy(&tmp, &vch[0], sizeof(tmp));
43 addr = CAddress(tmp.ip, tmp.port, NODE_NETWORK);
52 static bool Send(SOCKET hSocket, const char* pszSend)
54 if (strstr(pszSend, "PONG") != pszSend)
55 printf("IRC SENDING: %s\n", pszSend);
56 const char* psz = pszSend;
57 const char* pszEnd = psz + strlen(psz);
60 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
68 bool RecvLine(SOCKET hSocket, string& strLine)
74 int nBytes = recv(hSocket, &c, 1, 0);
82 if (strLine.size() >= 9000)
91 int nErr = WSAGetLastError();
92 if (nErr == WSAEMSGSIZE)
94 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
100 if (!strLine.empty())
105 printf("IRC socket closed\n");
111 int nErr = WSAGetLastError();
112 printf("IRC recv failed: %d\n", nErr);
119 bool RecvLineIRC(SOCKET hSocket, string& strLine)
123 bool fRet = RecvLine(hSocket, strLine);
128 vector<string> vWords;
129 ParseString(strLine, ' ', vWords);
130 if (vWords.size() >= 1 && vWords[0] == "PING")
134 Send(hSocket, strLine.c_str());
142 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
147 strLine.reserve(10000);
148 if (!RecvLineIRC(hSocket, strLine))
150 printf("IRC %s\n", strLine.c_str());
151 if (psz1 && strLine.find(psz1) != -1)
153 if (psz2 && strLine.find(psz2) != -1)
155 if (psz3 && strLine.find(psz3) != -1)
157 if (psz4 && strLine.find(psz4) != -1)
162 bool Wait(int nSeconds)
166 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
167 for (int i = 0; i < nSeconds; i++)
178 void ThreadIRCSeed(void* parg)
180 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
183 ThreadIRCSeed2(parg);
185 catch (std::exception& e) {
186 PrintExceptionContinue(&e, "ThreadIRCSeed()");
188 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
190 printf("ThreadIRCSeed exiting\n");
193 void ThreadIRCSeed2(void* parg)
195 if (mapArgs.count("-connect"))
197 if (mapArgs.count("-noirc"))
199 printf("ThreadIRCSeed started\n");
202 bool fNameInUse = false;
203 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
207 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
208 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
211 //struct hostent* phostent = gethostbyname("chat.freenode.net");
212 struct hostent* phostent = gethostbyname("irc.lfnet.org");
213 if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])
214 addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));
218 if (!ConnectSocket(addrConnect, hSocket))
220 printf("IRC connect failed\n");
221 nErrorWait = nErrorWait * 11 / 10;
222 if (Wait(nErrorWait += 60))
228 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
230 closesocket(hSocket);
231 hSocket = INVALID_SOCKET;
232 nErrorWait = nErrorWait * 11 / 10;
233 if (Wait(nErrorWait += 60))
240 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
241 strMyName = EncodeAddress(addrLocalHost);
243 strMyName = strprintf("x%u", GetRand(1000000000));
245 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
246 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
248 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
251 closesocket(hSocket);
252 hSocket = INVALID_SOCKET;
255 printf("IRC name already in use\n");
260 nErrorWait = nErrorWait * 11 / 10;
261 if (Wait(nErrorWait += 60))
268 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");
269 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");
271 int64 nStart = GetTime();
273 strLine.reserve(10000);
274 while (!fShutdown && RecvLineIRC(hSocket, strLine))
276 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
279 vector<string> vWords;
280 ParseString(strLine, ' ', vWords);
281 if (vWords.size() < 2)
287 if (vWords[1] == "352" && vWords.size() >= 8)
289 // index 7 is limited to 16 characters
290 // could get full length name at index 10, but would be different from join messages
291 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
292 printf("IRC got who\n");
295 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
297 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
298 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
299 if (strchr(pszName, '!'))
300 *strchr(pszName, '!') = '\0';
301 printf("IRC got join\n");
304 if (pszName[0] == 'u')
307 if (DecodeAddress(pszName, addr))
309 addr.nTime = GetAdjustedTime();
310 if (AddAddress(addr, 51 * 60))
311 printf("IRC got new address\n");
316 printf("IRC decode failed\n");
320 closesocket(hSocket);
321 hSocket = INVALID_SOCKET;
323 // IRC usually blocks TOR, so only try once
327 if (GetTime() - nStart > 20 * 60)
333 nRetryWait = nRetryWait * 11 / 10;
334 if (!Wait(nRetryWait += 60))
349 int main(int argc, char *argv[])
352 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
354 printf("Error at WSAStartup()\n");