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.
12 using namespace boost
;
14 int nGotIRCAddresses
= 0;
16 void ThreadIRCSeed2(void* parg
);
29 string
EncodeAddress(const CService
& addr
)
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
);
42 bool DecodeAddress(string str
, CService
& addr
)
44 vector
<unsigned char> vch
;
45 if (!DecodeBase58Check(str
.substr(1), vch
))
49 if (vch
.size() != sizeof(tmp
))
51 memcpy(&tmp
, &vch
[0], sizeof(tmp
));
53 addr
= CService(tmp
.ip
, ntohs(tmp
.port
));
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
);
70 int ret
= send(hSocket
, psz
, pszEnd
- psz
, MSG_NOSIGNAL
);
78 bool RecvLineIRC(SOCKET hSocket
, string
& strLine
)
82 bool fRet
= RecvLine(hSocket
, strLine
);
87 vector
<string
> vWords
;
88 ParseString(strLine
, ' ', vWords
);
89 if (vWords
.size() >= 1 && vWords
[0] == "PING")
93 Send(hSocket
, strLine
.c_str());
101 int RecvUntil(SOCKET hSocket
, const char* psz1
, const char* psz2
=NULL
, const char* psz3
=NULL
, const char* psz4
=NULL
)
106 strLine
.reserve(10000);
107 if (!RecvLineIRC(hSocket
, strLine
))
109 printf("IRC %s\n", strLine
.c_str());
110 if (psz1
&& strLine
.find(psz1
) != string::npos
)
112 if (psz2
&& strLine
.find(psz2
) != string::npos
)
114 if (psz3
&& strLine
.find(psz3
) != string::npos
)
116 if (psz4
&& strLine
.find(psz4
) != string::npos
)
121 bool Wait(int nSeconds
)
125 printf("IRC waiting %d seconds to reconnect\n", nSeconds
);
126 for (int i
= 0; i
< nSeconds
; i
++)
135 bool RecvCodeLine(SOCKET hSocket
, const char* psz1
, string
& strRet
)
141 if (!RecvLineIRC(hSocket
, strLine
))
144 vector
<string
> vWords
;
145 ParseString(strLine
, ' ', vWords
);
146 if (vWords
.size() < 2)
149 if (vWords
[1] == psz1
)
151 printf("IRC %s\n", strLine
.c_str());
158 bool GetIPFromIRC(SOCKET hSocket
, string strMyName
, CNetAddr
& ipRet
)
160 Send(hSocket
, strprintf("USERHOST %s\r", strMyName
.c_str()).c_str());
163 if (!RecvCodeLine(hSocket
, "302", strLine
))
166 vector
<string
> vWords
;
167 ParseString(strLine
, ' ', vWords
);
168 if (vWords
.size() < 4)
171 string str
= vWords
[3];
172 if (str
.rfind("@") == string::npos
)
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);
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()");
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
))
214 // ... or if we won't make outbound connections and won't accept inbound ones.
215 if (mapArgs
.count("-connect") && fNoListen
)
218 // ... or if IRC is not enabled.
219 if (!GetBoolArg("-irc", false))
222 printf("ThreadIRCSeed started\n");
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
;
236 if (!ConnectSocket(addrConnect
, hSocket
))
238 printf("IRC connect failed\n");
239 nErrorWait
= nErrorWait
* 11 / 10;
240 if (Wait(nErrorWait
+= 60))
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))
257 CNetAddr
addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
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
));
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 ");
273 closesocket(hSocket
);
274 hSocket
= INVALID_SOCKET
;
277 printf("IRC name already in use\n");
282 nErrorWait
= nErrorWait
* 11 / 10;
283 if (Wait(nErrorWait
+= 60))
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());
307 Send(hSocket
, "JOIN #bitcoinTEST3\r");
308 Send(hSocket
, "WHO #bitcoinTEST3\r");
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();
318 strLine
.reserve(10000);
319 while (!fShutdown
&& RecvLineIRC(hSocket
, strLine
))
321 if (strLine
.empty() || strLine
.size() > 900 || strLine
[0] != ':')
324 vector
<string
> vWords
;
325 ParseString(strLine
, ' ', vWords
);
326 if (vWords
.size() < 2)
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')
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());
361 printf("IRC decode failed\n");
365 closesocket(hSocket
);
366 hSocket
= INVALID_SOCKET
;
368 if (GetTime() - nStart
> 20 * 60)
374 nRetryWait
= nRetryWait
* 11 / 10;
375 if (!Wait(nRetryWait
+= 60))
390 int main(int argc
, char *argv
[])
393 if (WSAStartup(MAKEWORD(2,2), &wsadata
) != NO_ERROR
)
395 printf("Error at WSAStartup()\n");