2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2012-02-07 17:28:30 +01:00
|
|
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
2010-08-29 18:58:15 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-05-14 21:30:15 +02:00
|
|
|
#include "irc.h"
|
2011-05-14 23:20:30 +02:00
|
|
|
#include "net.h"
|
2011-05-16 00:08:35 +02:00
|
|
|
#include "strlcpy.h"
|
2012-04-15 22:10:54 +02:00
|
|
|
#include "base58.h"
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
|
2010-08-29 18:58:15 +02:00
|
|
|
int nGotIRCAddresses = 0;
|
|
|
|
|
2010-09-07 03:12:53 +02:00
|
|
|
void ThreadIRCSeed2(void* parg);
|
|
|
|
|
|
|
|
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct ircaddr
|
|
|
|
{
|
2012-01-03 23:33:31 +01:00
|
|
|
struct in_addr ip;
|
2010-08-29 18:58:15 +02:00
|
|
|
short port;
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
string EncodeAddress(const CService& addr)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
struct ircaddr tmp;
|
2012-01-03 23:33:31 +01:00
|
|
|
if (addr.GetInAddr(&tmp.ip))
|
|
|
|
{
|
|
|
|
tmp.port = htons(addr.GetPort());
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
|
|
|
|
return string("u") + EncodeBase58Check(vch);
|
|
|
|
}
|
|
|
|
return "";
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
bool DecodeAddress(string str, CService& addr)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
vector<unsigned char> vch;
|
|
|
|
if (!DecodeBase58Check(str.substr(1), vch))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct ircaddr tmp;
|
|
|
|
if (vch.size() != sizeof(tmp))
|
|
|
|
return false;
|
|
|
|
memcpy(&tmp, &vch[0], sizeof(tmp));
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
addr = CService(tmp.ip, ntohs(tmp.port));
|
2010-08-29 18:58:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool Send(SOCKET hSocket, const char* pszSend)
|
|
|
|
{
|
|
|
|
if (strstr(pszSend, "PONG") != pszSend)
|
|
|
|
printf("IRC SENDING: %s\n", pszSend);
|
|
|
|
const char* psz = pszSend;
|
|
|
|
const char* pszEnd = psz + strlen(psz);
|
|
|
|
while (psz < pszEnd)
|
|
|
|
{
|
|
|
|
int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);
|
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
psz += ret;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecvLineIRC(SOCKET hSocket, string& strLine)
|
|
|
|
{
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
bool fRet = RecvLine(hSocket, strLine);
|
|
|
|
if (fRet)
|
|
|
|
{
|
|
|
|
if (fShutdown)
|
|
|
|
return false;
|
|
|
|
vector<string> vWords;
|
|
|
|
ParseString(strLine, ' ', vWords);
|
|
|
|
if (vWords.size() >= 1 && vWords[0] == "PING")
|
|
|
|
{
|
|
|
|
strLine[1] = 'O';
|
|
|
|
strLine += '\r';
|
|
|
|
Send(hSocket, strLine.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fRet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-09 21:33:35 +02:00
|
|
|
int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL, const char* psz4=NULL)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
string strLine;
|
2010-09-07 03:12:53 +02:00
|
|
|
strLine.reserve(10000);
|
2010-08-29 18:58:15 +02:00
|
|
|
if (!RecvLineIRC(hSocket, strLine))
|
|
|
|
return 0;
|
|
|
|
printf("IRC %s\n", strLine.c_str());
|
2012-04-15 22:47:24 +02:00
|
|
|
if (psz1 && strLine.find(psz1) != string::npos)
|
2010-08-29 18:58:15 +02:00
|
|
|
return 1;
|
2012-04-15 22:47:24 +02:00
|
|
|
if (psz2 && strLine.find(psz2) != string::npos)
|
2010-08-29 18:58:15 +02:00
|
|
|
return 2;
|
2012-04-15 22:47:24 +02:00
|
|
|
if (psz3 && strLine.find(psz3) != string::npos)
|
2010-08-29 18:58:15 +02:00
|
|
|
return 3;
|
2012-04-15 22:47:24 +02:00
|
|
|
if (psz4 && strLine.find(psz4) != string::npos)
|
2010-10-09 21:33:35 +02:00
|
|
|
return 4;
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Wait(int nSeconds)
|
|
|
|
{
|
|
|
|
if (fShutdown)
|
|
|
|
return false;
|
|
|
|
printf("IRC waiting %d seconds to reconnect\n", nSeconds);
|
|
|
|
for (int i = 0; i < nSeconds; i++)
|
|
|
|
{
|
|
|
|
if (fShutdown)
|
|
|
|
return false;
|
|
|
|
Sleep(1000);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-05 10:29:30 +01:00
|
|
|
bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet)
|
|
|
|
{
|
|
|
|
strRet.clear();
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
string strLine;
|
|
|
|
if (!RecvLineIRC(hSocket, strLine))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vector<string> vWords;
|
|
|
|
ParseString(strLine, ' ', vWords);
|
|
|
|
if (vWords.size() < 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (vWords[1] == psz1)
|
|
|
|
{
|
|
|
|
printf("IRC %s\n", strLine.c_str());
|
|
|
|
strRet = strLine;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
|
2010-12-05 10:29:30 +01:00
|
|
|
{
|
|
|
|
Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str());
|
|
|
|
|
|
|
|
string strLine;
|
|
|
|
if (!RecvCodeLine(hSocket, "302", strLine))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vector<string> vWords;
|
|
|
|
ParseString(strLine, ' ', vWords);
|
|
|
|
if (vWords.size() < 4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
string str = vWords[3];
|
|
|
|
if (str.rfind("@") == string::npos)
|
|
|
|
return false;
|
|
|
|
string strHost = str.substr(str.rfind("@")+1);
|
|
|
|
|
2011-05-02 15:34:42 +02:00
|
|
|
// Hybrid IRC used by lfnet always returns IP when you userhost yourself,
|
|
|
|
// but in case another IRC is ever used this should work.
|
|
|
|
printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
|
2012-01-03 23:33:31 +01:00
|
|
|
CNetAddr addr(strHost, true);
|
2011-05-02 15:34:42 +02:00
|
|
|
if (!addr.IsValid())
|
|
|
|
return false;
|
2012-01-03 23:33:31 +01:00
|
|
|
ipRet = addr;
|
2010-12-05 10:29:30 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
void ThreadIRCSeed(void* parg)
|
2010-09-07 03:12:53 +02:00
|
|
|
{
|
|
|
|
IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
|
2012-06-24 17:03:57 +02:00
|
|
|
|
|
|
|
// Make this thread recognisable as the IRC seeding thread
|
|
|
|
RenameThread("bitcoin-ircseed");
|
|
|
|
|
2010-09-07 03:12:53 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ThreadIRCSeed2(parg);
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
|
|
|
PrintExceptionContinue(&e, "ThreadIRCSeed()");
|
|
|
|
} catch (...) {
|
|
|
|
PrintExceptionContinue(NULL, "ThreadIRCSeed()");
|
|
|
|
}
|
2012-05-17 19:52:38 +02:00
|
|
|
printf("ThreadIRCSeed exited\n");
|
2010-09-07 03:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadIRCSeed2(void* parg)
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
2012-07-26 02:48:39 +02:00
|
|
|
/* Don't advertise on IRC if we don't allow incoming connections */
|
2010-12-24 10:25:21 +01:00
|
|
|
if (mapArgs.count("-connect") || fNoListen)
|
2010-08-29 18:58:15 +02:00
|
|
|
return;
|
2010-12-24 10:25:21 +01:00
|
|
|
|
2012-02-06 20:35:57 +01:00
|
|
|
if (!GetBoolArg("-irc", false))
|
2010-08-29 18:58:15 +02:00
|
|
|
return;
|
2012-02-06 20:35:57 +01:00
|
|
|
|
2010-08-29 18:58:15 +02:00
|
|
|
printf("ThreadIRCSeed started\n");
|
|
|
|
int nErrorWait = 10;
|
|
|
|
int nRetryWait = 10;
|
|
|
|
|
|
|
|
while (!fShutdown)
|
|
|
|
{
|
2012-01-03 23:33:31 +01:00
|
|
|
CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org
|
2012-01-03 16:14:22 +01:00
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
CService addrIRC("irc.lfnet.org", 6667, true);
|
2012-01-03 16:14:22 +01:00
|
|
|
if (addrIRC.IsValid())
|
|
|
|
addrConnect = addrIRC;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
SOCKET hSocket;
|
|
|
|
if (!ConnectSocket(addrConnect, hSocket))
|
|
|
|
{
|
|
|
|
printf("IRC connect failed\n");
|
|
|
|
nErrorWait = nErrorWait * 11 / 10;
|
|
|
|
if (Wait(nErrorWait += 60))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-09 21:33:35 +02:00
|
|
|
if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
closesocket(hSocket);
|
|
|
|
hSocket = INVALID_SOCKET;
|
|
|
|
nErrorWait = nErrorWait * 11 / 10;
|
|
|
|
if (Wait(nErrorWait += 60))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-13 15:06:16 +02:00
|
|
|
CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
|
2012-05-10 20:35:13 +02:00
|
|
|
CService addrLocal;
|
2010-08-29 18:58:15 +02:00
|
|
|
string strMyName;
|
2012-05-13 15:06:16 +02:00
|
|
|
if (GetLocal(addrLocal, &addrIPv4))
|
2012-02-12 13:45:24 +01:00
|
|
|
strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
|
2012-05-13 15:06:16 +02:00
|
|
|
if (strMyName == "")
|
2010-08-29 18:58:15 +02:00
|
|
|
strMyName = strprintf("x%u", GetRand(1000000000));
|
|
|
|
|
|
|
|
Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
|
|
|
|
Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
|
|
|
|
|
|
|
|
int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
|
|
|
|
if (nRet != 1)
|
|
|
|
{
|
|
|
|
closesocket(hSocket);
|
|
|
|
hSocket = INVALID_SOCKET;
|
|
|
|
if (nRet == 2)
|
|
|
|
{
|
|
|
|
printf("IRC name already in use\n");
|
|
|
|
Wait(10);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nErrorWait = nErrorWait * 11 / 10;
|
|
|
|
if (Wait(nErrorWait += 60))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Sleep(500);
|
|
|
|
|
2010-12-15 23:43:51 +01:00
|
|
|
// Get our external IP from the IRC server and re-nick before joining the channel
|
2012-01-03 23:33:31 +01:00
|
|
|
CNetAddr addrFromIRC;
|
|
|
|
if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
|
2010-12-05 10:29:30 +01:00
|
|
|
{
|
2012-01-03 23:33:31 +01:00
|
|
|
printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
|
2012-05-24 19:02:21 +02:00
|
|
|
if (addrFromIRC.IsRoutable())
|
2010-12-05 10:29:30 +01:00
|
|
|
{
|
2010-12-15 23:43:51 +01:00
|
|
|
// IRC lets you to re-nick
|
2012-02-12 13:45:24 +01:00
|
|
|
AddLocal(addrFromIRC, LOCAL_IRC);
|
|
|
|
strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
|
2010-12-05 10:29:30 +01:00
|
|
|
Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
|
|
|
|
}
|
|
|
|
}
|
2011-05-21 02:54:51 +02:00
|
|
|
|
|
|
|
if (fTestNet) {
|
2012-04-13 03:26:35 +02:00
|
|
|
Send(hSocket, "JOIN #bitcoinTEST3\r");
|
|
|
|
Send(hSocket, "WHO #bitcoinTEST3\r");
|
2011-05-21 02:54:51 +02:00
|
|
|
} else {
|
|
|
|
// randomly join #bitcoin00-#bitcoin99
|
|
|
|
int channel_number = GetRandInt(100);
|
|
|
|
Send(hSocket, strprintf("JOIN #bitcoin%02d\r", channel_number).c_str());
|
|
|
|
Send(hSocket, strprintf("WHO #bitcoin%02d\r", channel_number).c_str());
|
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
int64 nStart = GetTime();
|
2010-08-29 18:58:15 +02:00
|
|
|
string strLine;
|
2010-09-07 03:12:53 +02:00
|
|
|
strLine.reserve(10000);
|
2010-08-29 18:58:15 +02:00
|
|
|
while (!fShutdown && RecvLineIRC(hSocket, strLine))
|
|
|
|
{
|
|
|
|
if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
vector<string> vWords;
|
|
|
|
ParseString(strLine, ' ', vWords);
|
|
|
|
if (vWords.size() < 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char pszName[10000];
|
|
|
|
pszName[0] = '\0';
|
|
|
|
|
|
|
|
if (vWords[1] == "352" && vWords.size() >= 8)
|
|
|
|
{
|
|
|
|
// index 7 is limited to 16 characters
|
|
|
|
// could get full length name at index 10, but would be different from join messages
|
|
|
|
strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
|
|
|
|
printf("IRC got who\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vWords[1] == "JOIN" && vWords[0].size() > 1)
|
|
|
|
{
|
|
|
|
// :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
|
|
|
|
strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
|
|
|
|
if (strchr(pszName, '!'))
|
|
|
|
*strchr(pszName, '!') = '\0';
|
|
|
|
printf("IRC got join\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pszName[0] == 'u')
|
|
|
|
{
|
|
|
|
CAddress addr;
|
|
|
|
if (DecodeAddress(pszName, addr))
|
|
|
|
{
|
2010-10-23 19:43:53 +02:00
|
|
|
addr.nTime = GetAdjustedTime();
|
2012-01-04 23:39:45 +01:00
|
|
|
if (addrman.Add(addr, addrConnect, 51 * 60))
|
2011-05-02 15:34:42 +02:00
|
|
|
printf("IRC got new address: %s\n", addr.ToString().c_str());
|
2010-08-29 18:58:15 +02:00
|
|
|
nGotIRCAddresses++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("IRC decode failed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closesocket(hSocket);
|
|
|
|
hSocket = INVALID_SOCKET;
|
|
|
|
|
|
|
|
if (GetTime() - nStart > 20 * 60)
|
|
|
|
{
|
|
|
|
nErrorWait /= 3;
|
|
|
|
nRetryWait /= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
nRetryWait = nRetryWait * 11 / 10;
|
|
|
|
if (!Wait(nRetryWait += 60))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
WSADATA wsadata;
|
|
|
|
if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
|
|
|
|
{
|
|
|
|
printf("Error at WSAStartup()\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadIRCSeed(NULL);
|
|
|
|
|
|
|
|
WSACleanup();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|