Merge remote branch 'refs/remotes/svn/trunk' into svn
[bitcoinplatinum.git] / irc.cpp
blobaad9beb76e97bdcba643f32696b44ea4681bab95
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.
5 #include "headers.h"
7 int nGotIRCAddresses = 0;
8 bool fGotExternalIP = false;
10 void ThreadIRCSeed2(void* parg);
15 #pragma pack(push, 1)
16 struct ircaddr
18 int ip;
19 short port;
21 #pragma pack(pop)
23 string EncodeAddress(const CAddress& addr)
25 struct ircaddr tmp;
26 tmp.ip = addr.ip;
27 tmp.port = addr.port;
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))
37 return false;
39 struct ircaddr tmp;
40 if (vch.size() != sizeof(tmp))
41 return false;
42 memcpy(&tmp, &vch[0], sizeof(tmp));
44 addr = CAddress(tmp.ip, tmp.port, NODE_NETWORK);
45 return true;
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);
59 while (psz < pszEnd)
61 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
62 if (ret < 0)
63 return false;
64 psz += ret;
66 return true;
69 bool RecvLine(SOCKET hSocket, string& strLine)
71 strLine = "";
72 loop
74 char c;
75 int nBytes = recv(hSocket, &c, 1, 0);
76 if (nBytes > 0)
78 if (c == '\n')
79 continue;
80 if (c == '\r')
81 return true;
82 strLine += c;
83 if (strLine.size() >= 9000)
84 return true;
86 else if (nBytes <= 0)
88 if (fShutdown)
89 return false;
90 if (nBytes < 0)
92 int nErr = WSAGetLastError();
93 if (nErr == WSAEMSGSIZE)
94 continue;
95 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
97 Sleep(10);
98 continue;
101 if (!strLine.empty())
102 return true;
103 if (nBytes == 0)
105 // socket closed
106 printf("IRC socket closed\n");
107 return false;
109 else
111 // socket error
112 int nErr = WSAGetLastError();
113 printf("IRC recv failed: %d\n", nErr);
114 return false;
120 bool RecvLineIRC(SOCKET hSocket, string& strLine)
122 loop
124 bool fRet = RecvLine(hSocket, strLine);
125 if (fRet)
127 if (fShutdown)
128 return false;
129 vector<string> vWords;
130 ParseString(strLine, ' ', vWords);
131 if (vWords.size() >= 1 && vWords[0] == "PING")
133 strLine[1] = 'O';
134 strLine += '\r';
135 Send(hSocket, strLine.c_str());
136 continue;
139 return fRet;
143 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
145 loop
147 string strLine;
148 strLine.reserve(10000);
149 if (!RecvLineIRC(hSocket, strLine))
150 return 0;
151 printf("IRC %s\n", strLine.c_str());
152 if (psz1 && strLine.find(psz1) != -1)
153 return 1;
154 if (psz2 && strLine.find(psz2) != -1)
155 return 2;
156 if (psz3 && strLine.find(psz3) != -1)
157 return 3;
158 if (psz4 && strLine.find(psz4) != -1)
159 return 4;
163 bool Wait(int nSeconds)
165 if (fShutdown)
166 return false;
167 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
168 for (int i = 0; i < nSeconds; i++)
170 if (fShutdown)
171 return false;
172 Sleep(1000);
174 return true;
177 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
179 strRet.clear();
180 loop
182 string strLine;
183 if (!RecvLineIRC(hSocket, strLine))
184 return false;
186 vector<string> vWords;
187 ParseString(strLine, ' ', vWords);
188 if (vWords.size() < 2)
189 continue;
191 if (vWords[1] == psz1)
193 printf("IRC %s\n", strLine.c_str());
194 strRet = strLine;
195 return true;
200 bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
202 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
204 string strLine;
205 if (!RecvCodeLine(hSocket, "302", strLine))
206 return false;
208 vector<string> vWords;
209 ParseString(strLine, ' ', vWords);
210 if (vWords.size() < 4)
211 return false;
213 string str = vWords[3];
214 if (str.rfind("@") == string::npos)
215 return false;
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;
225 else
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());
230 if (fUseProxy)
231 return false;
232 struct hostent* phostent = gethostbyname(strHost.c_str());
233 if (!phostent || !phostent->h_addr_list || !phostent->h_addr_list[0])
234 return false;
235 ipRet = *(u_long*)phostent->h_addr_list[0];
238 return true;
243 void ThreadIRCSeed(void* parg)
245 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
248 ThreadIRCSeed2(parg);
250 catch (std::exception& e) {
251 PrintExceptionContinue(&e, "ThreadIRCSeed()");
252 } catch (...) {
253 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
255 printf("ThreadIRCSeed exiting\n");
258 void ThreadIRCSeed2(void* parg)
260 if (mapArgs.count("-connect"))
261 return;
262 if (GetBoolArg("-noirc"))
263 return;
264 printf("ThreadIRCSeed started\n");
265 int nErrorWait = 10;
266 int nRetryWait = 10;
267 bool fNameInUse = false;
268 bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
270 while (!fShutdown)
272 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
273 CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org
274 if (!fTOR)
276 //struct hostent* phostent = gethostbyname("chat.freenode.net");
277 struct hostent* phostent = gethostbyname("irc.lfnet.org");
278 if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])
279 addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));
282 SOCKET hSocket;
283 if (!ConnectSocket(addrConnect, hSocket))
285 printf("IRC connect failed\n");
286 nErrorWait = nErrorWait * 11 / 10;
287 if (Wait(nErrorWait += 60))
288 continue;
289 else
290 return;
293 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
295 closesocket(hSocket);
296 hSocket = INVALID_SOCKET;
297 nErrorWait = nErrorWait * 11 / 10;
298 if (Wait(nErrorWait += 60))
299 continue;
300 else
301 return;
304 string strMyName;
305 if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
306 strMyName = EncodeAddress(addrLocalHost);
307 else
308 strMyName = strprintf("x%u", GetRand(1000000000));
310 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
311 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
313 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
314 if (nRet != 1)
316 closesocket(hSocket);
317 hSocket = INVALID_SOCKET;
318 if (nRet == 2)
320 printf("IRC name already in use\n");
321 fNameInUse = true;
322 Wait(10);
323 continue;
325 nErrorWait = nErrorWait * 11 / 10;
326 if (Wait(nErrorWait += 60))
327 continue;
328 else
329 return;
331 Sleep(500);
333 // Get our external IP from the IRC server and re-nick before joining the channel
334 CAddress addrFromIRC;
335 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
337 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
338 if (!fUseProxy && addrFromIRC.IsRoutable())
340 // IRC lets you to re-nick
341 fGotExternalIP = true;
342 addrLocalHost.ip = addrFromIRC.ip;
343 strMyName = EncodeAddress(addrLocalHost);
344 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
348 Send(hSocket, fTestNet ? "JOIN #bitcoinTEST\r" : "JOIN #bitcoin\r");
349 Send(hSocket, fTestNet ? "WHO #bitcoinTEST\r" : "WHO #bitcoin\r");
351 int64 nStart = GetTime();
352 string strLine;
353 strLine.reserve(10000);
354 while (!fShutdown && RecvLineIRC(hSocket, strLine))
356 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
357 continue;
359 vector<string> vWords;
360 ParseString(strLine, ' ', vWords);
361 if (vWords.size() < 2)
362 continue;
364 char pszName[10000];
365 pszName[0] = '\0';
367 if (vWords[1] == "352" && vWords.size() >= 8)
369 // index 7 is limited to 16 characters
370 // could get full length name at index 10, but would be different from join messages
371 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
372 printf("IRC got who\n");
375 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
377 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
378 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
379 if (strchr(pszName, '!'))
380 *strchr(pszName, '!') = '\0';
381 printf("IRC got join\n");
384 if (pszName[0] == 'u')
386 CAddress addr;
387 if (DecodeAddress(pszName, addr))
389 addr.nTime = GetAdjustedTime();
390 if (AddAddress(addr, 51 * 60))
391 printf("IRC got new address\n");
392 nGotIRCAddresses++;
394 else
396 printf("IRC decode failed\n");
400 closesocket(hSocket);
401 hSocket = INVALID_SOCKET;
403 // IRC usually blocks TOR, so only try once
404 if (fTOR)
405 return;
407 if (GetTime() - nStart > 20 * 60)
409 nErrorWait /= 3;
410 nRetryWait /= 3;
413 nRetryWait = nRetryWait * 11 / 10;
414 if (!Wait(nRetryWait += 60))
415 return;
428 #ifdef TEST
429 int main(int argc, char *argv[])
431 WSADATA wsadata;
432 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
434 printf("Error at WSAStartup()\n");
435 return false;
438 ThreadIRCSeed(NULL);
440 WSACleanup();
441 return 0;
443 #endif