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.10 · 2010-08-15

irc.cpp

328 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    if (mapArgs.count("-connect"))162        return;163    if (mapArgs.count("-noirc"))164        return;165    printf("ThreadIRCSeed started\n");166    int nErrorWait = 10;167    int nRetryWait = 10;168    bool fNameInUse = false;169    bool fTOR = (fUseProxy && addrProxy.port == htons(9050));170171    while (!fShutdown)172    {173        //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net174        CAddress addrConnect("92.243.23.21:6667"); // irc.lfnet.org175        if (!fTOR)176        {177            //struct hostent* phostent = gethostbyname("chat.freenode.net");178            struct hostent* phostent = gethostbyname("irc.lfnet.org");179            if (phostent && phostent->h_addr_list && phostent->h_addr_list[0])180                addrConnect = CAddress(*(u_long*)phostent->h_addr_list[0], htons(6667));181        }182183        SOCKET hSocket;184        if (!ConnectSocket(addrConnect, hSocket))185        {186            printf("IRC connect failed\n");187            nErrorWait = nErrorWait * 11 / 10;188            if (Wait(nErrorWait += 60))189                continue;190            else191                return;192        }193194        if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))195        {196            closesocket(hSocket);197            hSocket = INVALID_SOCKET;198            nErrorWait = nErrorWait * 11 / 10;199            if (Wait(nErrorWait += 60))200                continue;201            else202                return;203        }204205        string strMyName;206        if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)207            strMyName = EncodeAddress(addrLocalHost);208        else209            strMyName = strprintf("x%u", GetRand(1000000000));210211        Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());212        Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());213214        int nRet = RecvUntil(hSocket, " 004 ", " 433 ");215        if (nRet != 1)216        {217            closesocket(hSocket);218            hSocket = INVALID_SOCKET;219            if (nRet == 2)220            {221                printf("IRC name already in use\n");222                fNameInUse = true;223                Wait(10);224                continue;225            }226            nErrorWait = nErrorWait * 11 / 10;227            if (Wait(nErrorWait += 60))228                continue;229            else230                return;231        }232        Sleep(500);233234        Send(hSocket, "JOIN #bitcoin\r");235        Send(hSocket, "WHO #bitcoin\r");236237        int64 nStart = GetTime();238        string strLine;239        while (!fShutdown && RecvLineIRC(hSocket, strLine))240        {241            if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')242                continue;243244            vector<string> vWords;245            ParseString(strLine, ' ', vWords);246            if (vWords.size() < 2)247                continue;248249            char pszName[10000];250            pszName[0] = '\0';251252            if (vWords[1] == "352" && vWords.size() >= 8)253            {254                // index 7 is limited to 16 characters255                // could get full length name at index 10, but would be different from join messages256                strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));257                printf("IRC got who\n");258            }259260            if (vWords[1] == "JOIN" && vWords[0].size() > 1)261            {262                // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname263                strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));264                if (strchr(pszName, '!'))265                    *strchr(pszName, '!') = '\0';266                printf("IRC got join\n");267            }268269            if (pszName[0] == 'u')270            {271                CAddress addr;272                if (DecodeAddress(pszName, addr))273                {274                    addr.nTime = GetAdjustedTime() - 51 * 60;275                    if (AddAddress(addr))276                        printf("IRC got new address\n");277                    nGotIRCAddresses++;278                }279                else280                {281                    printf("IRC decode failed\n");282                }283            }284        }285        closesocket(hSocket);286        hSocket = INVALID_SOCKET;287288        // IRC usually blocks TOR, so only try once289        if (fTOR)290            return;291292        if (GetTime() - nStart > 20 * 60)293        {294            nErrorWait /= 3;295            nRetryWait /= 3;296        }297298        nRetryWait = nRetryWait * 11 / 10;299        if (!Wait(nRetryWait += 60))300            return;301    }302}303304305306307308309310311312313#ifdef TEST314int main(int argc, char *argv[])315{316    WSADATA wsadata;317    if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)318    {319        printf("Error at WSAStartup()\n");320        return false;321    }322323    ThreadIRCSeed(NULL);324325    WSACleanup();326    return 0;327}328#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