Research
Timechain
Search Donate
Sponsors
Cake Wallet, private Bitcoin and Monero walletSideShift.ai, no-signup crypto exchangeOrangeRock, non-custodial bitcoin spot and perps tradingBraiins Hashpower, buy and sell Bitcoin hashrateeCash.com
Bitcoin v0.3.0 · 2010-07-06

irc.cpp

324 lines · plain text · GitHub · MIT license (license.txt)
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45#include "headers.h"67int nGotIRCAddresses = 0;891011#pragma pack(push, 1)12struct ircaddr13{14    int ip;15    short port;16};17#pragma pack(pop)1819string EncodeAddress(const CAddress& addr)20{21    struct ircaddr tmp;22    tmp.ip    = addr.ip;23    tmp.port  = addr.port;2425    vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));26    return string("u") + EncodeBase58Check(vch);27}2829bool DecodeAddress(string str, CAddress& addr)30{31    vector<unsigned char> vch;32    if (!DecodeBase58Check(str.substr(1), vch))33        return false;3435    struct ircaddr tmp;36    if (vch.size() != sizeof(tmp))37        return false;38    memcpy(&tmp, &vch[0], sizeof(tmp));3940    addr = CAddress(tmp.ip, tmp.port, NODE_NETWORK);41    return true;42}43444546474849static bool Send(SOCKET hSocket, const char* pszSend)50{51    if (strstr(pszSend, "PONG") != pszSend)52        printf("IRC SENDING: %s\n", pszSend);53    const char* psz = pszSend;54    const char* pszEnd = psz + strlen(psz);55    while (psz < pszEnd)56    {57        int ret = send(hSocket, psz, pszEnd - psz, MSG_NOSIGNAL);58        if (ret < 0)59            return false;60        psz += ret;61    }62    return true;63}6465bool RecvLine(SOCKET hSocket, string& strLine)66{67    strLine = "";68    loop69    {70        char c;71        int nBytes = recv(hSocket, &c, 1, 0);72        if (nBytes > 0)73        {74            if (c == '\n')75                continue;76            if (c == '\r')77                return true;78            strLine += c;79            if (strLine.size() >= 9000)80                return true;81        }82        else if (nBytes <= 0)83        {84            if (!strLine.empty())85                return true;86            // socket closed87            printf("IRC socket closed\n");88            return false;89        }90        else91        {92            // socket error93            int nErr = WSAGetLastError();94            if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)95            {96                printf("IRC recv failed: %d\n", nErr);97                return false;98            }99        }100    }101}102103bool RecvLineIRC(SOCKET hSocket, string& strLine)104{105    loop106    {107        bool fRet = RecvLine(hSocket, strLine);108        if (fRet)109        {110            if (fShutdown)111                return false;112            vector<string> vWords;113            ParseString(strLine, ' ', vWords);114            if (vWords.size() >= 1 && vWords[0] == "PING")115            {116                strLine[1] = 'O';117                strLine += '\r';118                Send(hSocket, strLine.c_str());119                continue;120            }121        }122        return fRet;123    }124}125126int RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL)127{128    loop129    {130        string strLine;131        if (!RecvLineIRC(hSocket, strLine))132            return 0;133        printf("IRC %s\n", strLine.c_str());134        if (psz1 && strLine.find(psz1) != -1)135            return 1;136        if (psz2 && strLine.find(psz2) != -1)137            return 2;138        if (psz3 && strLine.find(psz3) != -1)139            return 3;140    }141}142143bool Wait(int nSeconds)144{145    if (fShutdown)146        return false;147    printf("IRC waiting %d seconds to reconnect\n", nSeconds);148    for (int i = 0; i < nSeconds; i++)149    {150        if (fShutdown)151            return false;152        Sleep(1000);153    }154    return true;155}156157158159void ThreadIRCSeed(void* parg)160{161    printf("ThreadIRCSeed started\n");162    int nErrorWait = 10;163    int nRetryWait = 10;164    bool fNameInUse = false;165    bool fTOR = (fUseProxy && addrProxy.port == htons(9050));166167    while (!fShutdown)168    {169        //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net170        CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org171        if (!fTOR)172        {173            //struct hostent* phostent = gethostbyname("chat.freenode.net");174            struct hostent* phostent = gethostbyname("irc.lfnet.org");175            if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])176                addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));177        }178179        SOCKET hSocket;180        if (!ConnectSocket(addrConnect, hSocket))181        {182            printf("IRC connect failed\n");183            nErrorWait = nErrorWait * 11 / 10;184            if (Wait(nErrorWait += 60))185                continue;186            else187                return;188        }189190        if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))191        {192            closesocket(hSocket);193            hSocket = INVALID_SOCKET;194            nErrorWait = nErrorWait * 11 / 10;195            if (Wait(nErrorWait += 60))196                continue;197            else198                return;199        }200201        string strMyName;202        if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)203            strMyName = EncodeAddress(addrLocalHost);204        else205            strMyName = strprintf("x%u", GetRand(1000000000));206207        Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());208        Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());209210        int nRet = RecvUntil(hSocket, " 004 ", " 433 ");211        if (nRet != 1)212        {213            closesocket(hSocket);214            hSocket = INVALID_SOCKET;215            if (nRet == 2)216            {217                printf("IRC name already in use\n");218                fNameInUse = true;219                Wait(10);220                continue;221            }222            nErrorWait = nErrorWait * 11 / 10;223            if (Wait(nErrorWait += 60))224                continue;225            else226                return;227        }228        Sleep(500);229230        Send(hSocket, "JOIN #bitcoin\r");231        Send(hSocket, "WHO #bitcoin\r");232233        int64 nStart = GetTime();234        string strLine;235        while (!fShutdown && RecvLineIRC(hSocket, strLine))236        {237            if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')238                continue;239240            vector<string> vWords;241            ParseString(strLine, ' ', vWords);242            if (vWords.size() < 2)243                continue;244245            char pszName[10000];246            pszName[0] = '\0';247248            if (vWords[1] == "352" && vWords.size() >= 8)249            {250                // index 7 is limited to 16 characters251                // could get full length name at index 10, but would be different from join messages252                strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));253                printf("IRC got who\n");254            }255256            if (vWords[1] == "JOIN" && vWords[0].size() > 1)257            {258                // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname259                strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));260                if (strchr(pszName, '!'))261                    *strchr(pszName, '!') = '\0';262                printf("IRC got join\n");263            }264265            if (pszName[0] == 'u')266            {267                CAddress addr;268                if (DecodeAddress(pszName, addr))269                {270                    addr.nTime = GetAdjustedTime() - 51 * 60;271                    if (AddAddress(addr))272                        printf("IRC got new address\n");273                    nGotIRCAddresses++;274                }275                else276                {277                    printf("IRC decode failed\n");278                }279            }280        }281        closesocket(hSocket);282        hSocket = INVALID_SOCKET;283284        // IRC usually blocks TOR, so only try once285        if (fTOR)286            return;287288        if (GetTime() - nStart > 20 * 60)289        {290            nErrorWait /= 3;291            nRetryWait /= 3;292        }293294        nRetryWait = nRetryWait * 11 / 10;295        if (!Wait(nRetryWait += 60))296            return;297    }298}299300301302303304305306307308309#ifdef TEST310int main(int argc, char *argv[])311{312    WSADATA wsadata;313    if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)314    {315        printf("Error at WSAStartup()\n");316        return false;317    }318319    ThreadIRCSeed(NULL);320321    WSACleanup();322    return 0;323}324#endif

Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.

0:000:00