Update Bugreport Links
[bitcoinplatinum.git] / src / irc.cpp
blob6991e6ee7e704cc34e0b46328d60d5dccc5ebe42
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.
6 #include "irc.h"
7 #include "net.h"
8 #include "strlcpy.h"
9 #include "base58.h"
11 using namespace std;
12 using namespace boost;
14 int nGotIRCAddresses = 0;
16 void ThreadIRCSeed2(void* parg);
21 #pragma pack(push, 1)
22 struct ircaddr
24 struct in_addr ip;
25 short port;
27 #pragma pack(pop)
29 string EncodeAddress(const CService& addr)
31 struct ircaddr tmp;
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);
39 return "";
42 bool DecodeAddress(string str, CService& addr)
44 vector<unsigned char> vch;
45 if (!DecodeBase58Check(str.substr(1), vch))
46 return false;
48 struct ircaddr tmp;
49 if (vch.size() != sizeof(tmp))
50 return false;
51 memcpy(&tmp, &vch[0], sizeof(tmp));
53 addr = CService(tmp.ip, ntohs(tmp.port));
54 return true;
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);
68 while (psz < pszEnd)
70 int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
71 if (ret < 0)
72 return false;
73 psz += ret;
75 return true;
78 bool RecvLineIRC(SOCKET hSocket, string& strLine)
80 loop
82 bool fRet = RecvLine(hSocket, strLine);
83 if (fRet)
85 if (fShutdown)
86 return false;
87 vector<string> vWords;
88 ParseString(strLine, ' ', vWords);
89 if (vWords.size() >= 1 && vWords[0] == "PING")
91 strLine[1] = 'O';
92 strLine += '\r';
93 Send(hSocket, strLine.c_str());
94 continue;
97 return fRet;
101 int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
103 loop
105 string strLine;
106 strLine.reserve(10000);
107 if (!RecvLineIRC(hSocket, strLine))
108 return 0;
109 printf("IRC %s\n", strLine.c_str());
110 if (psz1 && strLine.find(psz1) != string::npos)
111 return 1;
112 if (psz2 && strLine.find(psz2) != string::npos)
113 return 2;
114 if (psz3 && strLine.find(psz3) != string::npos)
115 return 3;
116 if (psz4 && strLine.find(psz4) != string::npos)
117 return 4;
121 bool Wait(int nSeconds)
123 if (fShutdown)
124 return false;
125 printf("IRC waiting %d seconds to reconnect\n", nSeconds);
126 for (int i = 0; i < nSeconds; i++)
128 if (fShutdown)
129 return false;
130 Sleep(1000);
132 return true;
135 bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
137 strRet.clear();
138 loop
140 string strLine;
141 if (!RecvLineIRC(hSocket, strLine))
142 return false;
144 vector<string> vWords;
145 ParseString(strLine, ' ', vWords);
146 if (vWords.size() < 2)
147 continue;
149 if (vWords[1] == psz1)
151 printf("IRC %s\n", strLine.c_str());
152 strRet = strLine;
153 return true;
158 bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
160 Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
162 string strLine;
163 if (!RecvCodeLine(hSocket, "302", strLine))
164 return false;
166 vector<string> vWords;
167 ParseString(strLine, ' ', vWords);
168 if (vWords.size() < 4)
169 return false;
171 string str = vWords[3];
172 if (str.rfind("@") == string::npos)
173 return false;
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);
180 if (!addr.IsValid())
181 return false;
182 ipRet = addr;
184 return true;
189 void ThreadIRCSeed(void* parg)
191 IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
193 // Make this thread recognisable as the IRC seeding thread
194 RenameThread("bitcoin-ircseed");
198 ThreadIRCSeed2(parg);
200 catch (std::exception& e) {
201 PrintExceptionContinue(&e, "ThreadIRCSeed()");
202 } catch (...) {
203 PrintExceptionContinue(NULL, "ThreadIRCSeed()");
205 printf("ThreadIRCSeed exited\n");
208 void ThreadIRCSeed2(void* parg)
210 // Don't connect to IRC if we won't use IPv4 connections.
211 if (IsLimited(NET_IPV4))
212 return;
214 // ... or if we won't make outbound connections and won't accept inbound ones.
215 if (mapArgs.count("-connect") && fNoListen)
216 return;
218 // ... or if IRC is not enabled.
219 if (!GetBoolArg("-irc", false))
220 return;
222 printf("ThreadIRCSeed started\n");
223 int nErrorWait = 10;
224 int nRetryWait = 10;
225 int nNameRetry = 0;
227 while (!fShutdown)
229 CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org
231 CService addrIRC("irc.lfnet.org", 6667, true);
232 if (addrIRC.IsValid())
233 addrConnect = addrIRC;
235 SOCKET hSocket;
236 if (!ConnectSocket(addrConnect, hSocket))
238 printf("IRC connect failed\n");
239 nErrorWait = nErrorWait * 11 / 10;
240 if (Wait(nErrorWait += 60))
241 continue;
242 else
243 return;
246 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
248 closesocket(hSocket);
249 hSocket = INVALID_SOCKET;
250 nErrorWait = nErrorWait * 11 / 10;
251 if (Wait(nErrorWait += 60))
252 continue;
253 else
254 return;
257 CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
258 CService addrLocal;
259 string strMyName;
260 // Don't use our IP as our nick if we're not listening
261 // or if it keeps failing because the nick is already in use.
262 if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3)
263 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
264 if (strMyName == "")
265 strMyName = strprintf("x%u", GetRand(1000000000));
267 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
268 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
270 int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
271 if (nRet != 1)
273 closesocket(hSocket);
274 hSocket = INVALID_SOCKET;
275 if (nRet == 2)
277 printf("IRC name already in use\n");
278 nNameRetry++;
279 Wait(10);
280 continue;
282 nErrorWait = nErrorWait * 11 / 10;
283 if (Wait(nErrorWait += 60))
284 continue;
285 else
286 return;
288 nNameRetry = 0;
289 Sleep(500);
291 // Get our external IP from the IRC server and re-nick before joining the channel
292 CNetAddr addrFromIRC;
293 if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
295 printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
296 // Don't use our IP as our nick if we're not listening
297 if (!fNoListen && addrFromIRC.IsRoutable())
299 // IRC lets you to re-nick
300 AddLocal(addrFromIRC, LOCAL_IRC);
301 strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
302 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
306 if (fTestNet) {
307 Send(hSocket, "JOIN #bitcoinTEST3\r");
308 Send(hSocket, "WHO #bitcoinTEST3\r");
309 } else {
310 // randomly join #bitcoin00-#bitcoin99
311 int channel_number = GetRandInt(100);
312 Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
313 Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
316 int64 nStart = GetTime();
317 string strLine;
318 strLine.reserve(10000);
319 while (!fShutdown && RecvLineIRC(hSocket, strLine))
321 if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
322 continue;
324 vector<string> vWords;
325 ParseString(strLine, ' ', vWords);
326 if (vWords.size() < 2)
327 continue;
329 char pszName[10000];
330 pszName[0] = '\0';
332 if (vWords[1] == "352" && vWords.size() >= 8)
334 // index 7 is limited to 16 characters
335 // could get full length name at index 10, but would be different from join messages
336 strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
337 printf("IRC got who\n");
340 if (vWords[1] == "JOIN" && vWords[0].size() > 1)
342 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
343 strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
344 if (strchr(pszName, '!'))
345 *strchr(pszName, '!') = '\0';
346 printf("IRC got join\n");
349 if (pszName[0] == 'u')
351 CAddress addr;
352 if (DecodeAddress(pszName, addr))
354 addr.nTime = GetAdjustedTime();
355 if (addrman.Add(addr, addrConnect, 51 * 60))
356 printf("IRC got new address: %s\n", addr.ToString().c_str());
357 nGotIRCAddresses++;
359 else
361 printf("IRC decode failed\n");
365 closesocket(hSocket);
366 hSocket = INVALID_SOCKET;
368 if (GetTime() - nStart > 20 * 60)
370 nErrorWait /= 3;
371 nRetryWait /= 3;
374 nRetryWait = nRetryWait * 11 / 10;
375 if (!Wait(nRetryWait += 60))
376 return;
389 #ifdef TEST
390 int main(int argc, char *argv[])
392 WSADATA wsadata;
393 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
395 printf("Error at WSAStartup()\n");
396 return false;
399 ThreadIRCSeed(NULL);
401 WSACleanup();
402 return 0;
404 #endif