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)
90 printf("IRC socket closed\n");
96 int nErr = WSAGetLastError();
97 if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
99 printf("IRC recv failed: %d\n", nErr);
106 bool RecvLineIRC(SOCKET hSocket, string& strLine)
110 bool fRet = RecvLine(hSocket, strLine);
115 vector<string> vWords;
116 ParseString(strLine, ' ', vWords);
117 if (vWords.size() >= 1 && vWords[0] == "PING")
121 Send(hSocket, strLine.c_str());
129 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
134 strLine.reserve(10000);
135 if (!RecvLineIRC(hSocket, strLine))
137 printf("IRC %s\n", strLine.c_str());
138 if (psz1 && strLine.find(psz1) != -1)
140 if (psz2 && strLine.find(psz2) != -1)
142 if (psz3 && strLine.find(psz3) != -1)
144 if (psz4 && strLine.find(psz4) != -1)
149 bool Wait(int nSeconds)
153 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
154 for (int i = 0; i < nSeconds; i++)
165 void ThreadIRCSeed(void* parg)
167 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
170 ThreadIRCSeed2(parg);
172 catch (std::exception& e) {
173 PrintExceptionContinue(&e, "ThreadIRCSeed()");
175 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
177 printf("ThreadIRCSeed exiting\n");
180 void ThreadIRCSeed2(void* parg)
182 if (mapArgs.count("-connect"))
184 if (mapArgs.count("-noirc"))
186 printf("ThreadIRCSeed started\n");
189 bool fNameInUse = false;
190 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
194 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
195 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
198 //struct hostent* phostent = gethostbyname("chat.freenode.net");
199 struct hostent* phostent = gethostbyname("irc.lfnet.org");
200 if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])
201 addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));
205 if (!ConnectSocket(addrConnect, hSocket))
207 printf("IRC connect failed\n");
208 nErrorWait = nErrorWait * 11 / 10;
209 if (Wait(nErrorWait += 60))
215 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
217 closesocket(hSocket);
218 hSocket = INVALID_SOCKET;
219 nErrorWait = nErrorWait * 11 / 10;
220 if (Wait(nErrorWait += 60))
227 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
228 strMyName = EncodeAddress(addrLocalHost);
230 strMyName = strprintf("x%u", GetRand(1000000000));
232 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
233 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
235 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
238 closesocket(hSocket);
239 hSocket = INVALID_SOCKET;
242 printf("IRC name already in use\n");
247 nErrorWait = nErrorWait * 11 / 10;
248 if (Wait(nErrorWait += 60))
255 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");
256 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");
258 int64 nStart = GetTime();
260 strLine.reserve(10000);
261 while (!fShutdown && RecvLineIRC(hSocket, strLine))
263 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
266 vector<string> vWords;
267 ParseString(strLine, ' ', vWords);
268 if (vWords.size() < 2)
274 if (vWords[1] == "352" && vWords.size() >= 8)
276 // index 7 is limited to 16 characters
277 // could get full length name at index 10, but would be different from join messages
278 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
279 printf("IRC got who\n");
282 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
284 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
285 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
286 if (strchr(pszName, '!'))
287 *strchr(pszName, '!') = '\0';
288 printf("IRC got join\n");
291 if (pszName[0] == 'u')
294 if (DecodeAddress(pszName, addr))
296 addr.nTime = GetAdjustedTime() - 51 * 60;
297 if (AddAddress(addr))
298 printf("IRC got new address\n");
303 printf("IRC decode failed\n");
307 closesocket(hSocket);
308 hSocket = INVALID_SOCKET;
310 // IRC usually blocks TOR, so only try once
314 if (GetTime() - nStart > 20 * 60)
320 nRetryWait = nRetryWait * 11 / 10;
321 if (!Wait(nRetryWait += 60))
336 int main(int argc, char *argv[])
339 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
341 printf("Error at WSAStartup()\n");