예제 #1
0
파일: irc.cpp 프로젝트: d5000/slimcoin
void ThreadIRCSeed2(void* parg)
{
  /* Dont advertise on IRC if we don't allow incoming connections */
  if(mapArgs.count("-connect") || fNoListen)
    return;

  if(!GetBoolArg("-irc", false))
    return;

  printf("ThreadIRCSeed started\n");
  int nErrorWait = 10;
  int nRetryWait = 10;
  bool fNameInUse = false;

  while(!fShutdown)
  {
    CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org

    CService addrIRC("irc.lfnet.org", 6667, true);
    if(addrIRC.IsValid())
      addrConnect = addrIRC;

    SOCKET hSocket;
    if(!ConnectSocket(addrConnect, hSocket))
    {
      printf("IRC connect failed\n");
      nErrorWait = nErrorWait * 11 / 10;
      if(Wait(nErrorWait += 60))
        continue;
      else
        return;
    }

    if(!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
    {
      closesocket(hSocket);
      hSocket = INVALID_SOCKET;
      nErrorWait = nErrorWait * 11 / 10;
      if(Wait(nErrorWait += 60))
        continue;
      else
        return;
    }

    string strMyName;
    if(addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse)
      strMyName = EncodeAddress(addrLocalHost);
    else
      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");
        fNameInUse = true;
        Wait(10);
        continue;
      }
      nErrorWait = nErrorWait * 11 / 10;
      if(Wait(nErrorWait += 60))
        continue;
      else
        return;
    }
    Sleep(500);

    // Get our external IP from the IRC server and re-nick before joining the channel
    CNetAddr addrFromIRC;
    if(GetIPFromIRC(hSocket, strMyName, addrFromIRC))
    {
      printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
      if(!fUseProxy && addrFromIRC.IsRoutable())
      {
        // IRC lets you to re-nick
        fGotExternalIP = true;
        addrLocalHost.SetIP(addrFromIRC);
        strMyName = EncodeAddress(addrLocalHost);
        Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
      }
    }
        
    if(fTestNet) {
      Send(hSocket, "JOIN #bitcoinTEST\r");
      Send(hSocket, "WHO #bitcoinTEST\r");
    } 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());
    }

    int64 nStart = GetTime();
    string strLine;
    strLine.reserve(10000);
    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)
      {
        // :[email protected] 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))
        {
          addr.nTime = GetAdjustedTime();
          if(addrman.Add(addr, addrConnect, 51 * 60))
            printf("IRC got new address: %s\n", addr.ToString().c_str());
          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;
  }
}
예제 #2
0
파일: irc.cpp 프로젝트: webcoinx14/Webcoin
void ThreadIRCSeed2(void* parg)
{
    // Don't connect to IRC if we won't use IPv4 connections.
    if (IsLimited(NET_IPV4))
        return;

    // ... or if we won't make outbound connections and won't accept inbound ones.
    if (mapArgs.count("-connect") && fNoListen)
        return;

    // ... or if IRC is not enabled.
    if (!GetBoolArg("-irc", false))
        return;

    printf("ThreadIRCSeed started\n");
    int nErrorWait = 10;
    int nRetryWait = 10;
    int nNameRetry = 0;

    while (!fShutdown)
    {
        CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org

        CService addrIRC("irc.lfnet.org", 6667, true);
        if (addrIRC.IsValid())
            addrConnect = addrIRC;

        SOCKET hSocket;
        if (!ConnectSocket(addrConnect, hSocket))
        {
            printf("IRC connect failed\n");
            nErrorWait = nErrorWait * 11 / 10;
            if (Wait(nErrorWait += 60))
                continue;
            else
                return;
        }

        if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
        {
            closesocket(hSocket);
            hSocket = INVALID_SOCKET;
            nErrorWait = nErrorWait * 11 / 10;
            if (Wait(nErrorWait += 60))
                continue;
            else
                return;
        }

        CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
        CService addrLocal;
        string strMyName;
        // Don't use our IP as our nick if we're not listening
        // or if it keeps failing because the nick is already in use.
        if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3)
            strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
        if (strMyName == "")
            strMyName = strprintf("x%"PRIu64"", 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");
                nNameRetry++;
                Wait(10);
                continue;
            }
            nErrorWait = nErrorWait * 11 / 10;
            if (Wait(nErrorWait += 60))
                continue;
            else
                return;
        }
        nNameRetry = 0;
        MilliSleep(500);

        // Get our external IP from the IRC server and re-nick before joining the channel
        CNetAddr addrFromIRC;
        if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
        {
            printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
            // Don't use our IP as our nick if we're not listening
            if (!fNoListen && addrFromIRC.IsRoutable())
            {
                // IRC lets you to re-nick
                AddLocal(addrFromIRC, LOCAL_IRC);
                strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
                Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
            }
        }

        if (fTestNet) {
            Send(hSocket, "JOIN #WebcoinTEST\r");
            Send(hSocket, "WHO #WebcoinTEST\r");
        } else {
            // randomly join #Webcoin00-#Webcoin05
            //int channel_number = GetRandInt(5);
            int channel_number = 0;
            // Channel number is always 0 for initial release
            //int channel_number = 0;
            Send(hSocket, strprintf("JOIN #Webcoin%02d\r", channel_number).c_str());
            Send(hSocket, strprintf("WHO #Webcoin%02d\r", channel_number).c_str());
        }

        int64_t nStart = GetTime();
        string strLine;
        strLine.reserve(10000);
        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)
            {
                // :[email protected] 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))
                {
                    addr.nTime = GetAdjustedTime();
                    if (addrman.Add(addr, addrConnect, 51 * 60))
                        printf("IRC got new address: %s\n", addr.ToString().c_str());
                    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;
    }
}
예제 #3
0
파일: ntp.cpp 프로젝트: FikiHafana/chipcoin
int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {


#ifdef WIN32
    u_long nOne = 1;
    if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR) {
        printf("ConnectSocket() : ioctlsocket non-blocking setting failed, error %d\n", WSAGetLastError());
#else
    if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == SOCKET_ERROR) {
        printf("ConnectSocket() : fcntl non-blocking setting failed, error %d\n", errno);
#endif
        return -2;
    }

    struct timeval timeout = {10, 0};
    struct pkt *msg = new pkt;
    struct pkt *prt  = new pkt;
    time_t seconds_transmit;
    int len = 48;

    msg->li_vn_mode=227;
    msg->stratum=0;
    msg->ppoll=4;
    msg->precision=0;
    msg->rootdelay=0;
    msg->rootdispersion=0;

    msg->ref.Ul_i.Xl_i=0;
    msg->ref.Ul_f.Xl_f=0;
    msg->org.Ul_i.Xl_i=0;
    msg->org.Ul_f.Xl_f=0;
    msg->rec.Ul_i.Xl_i=0;
    msg->rec.Ul_f.Xl_f=0;
    msg->xmt.Ul_i.Xl_i=0;
    msg->xmt.Ul_f.Xl_f=0;

    int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen);
    if (retcode < 0) {
        printf("sendto() failed: %d\n", retcode);
        seconds_transmit = -3;
        goto _end;
    }

    fd_set fdset;
    FD_ZERO(&fdset);
    FD_SET(sockfd, &fdset);

    retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
    if (retcode <= 0) {
        printf("recvfrom() error\n");
        seconds_transmit = -4;
        goto _end;
    }

    recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL);
    ntohl_fp(&msg->xmt, &prt->xmt);
    Ntp2Unix(prt->xmt.Ul_i.Xl_ui, seconds_transmit);

    _end:

    delete msg;
    delete prt;

    return seconds_transmit;
}

int64_t NtpGetTime(CNetAddr& ip) {
    struct sockaddr cliaddr;

    SOCKET sockfd;
    socklen_t servlen;

    if (!InitWithRandom(sockfd, servlen, &cliaddr))
        return -1;

    ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr);
    int64_t nTime = DoReq(sockfd, servlen, cliaddr);

    closesocket(sockfd);

    return nTime;
}

int64_t NtpGetTime(const std::string &strHostName)
{
    struct sockaddr cliaddr;

    SOCKET sockfd;
    socklen_t servlen;

    if (!InitWithHost(strHostName, sockfd, servlen, &cliaddr))
        return -1;

    int64_t nTime = DoReq(sockfd, servlen, cliaddr);

    closesocket(sockfd);

    return nTime;
}

// NTP server, which we unconditionally trust. This may be your own installation of ntpd somewhere, for example. 
// "localhost" means "trust no one"
std::string strTrustedUpstream = "localhost";

// Current offset
int64_t nNtpOffset = INT64_MAX;

int64_t GetNtpOffset() {
    return nNtpOffset;
}

void ThreadNtpSamples(void* parg) {
    const int64_t nMaxOffset = 86400; // Not a real limit, just sanity threshold.

    printf("Trying to find NTP server at localhost...\n");

    std::string strLocalHost = "127.0.0.1";
    if (NtpGetTime(strLocalHost) == GetTime()) {
        printf("There is NTP server active at localhost,  we don't need NTP thread.\n");

        nNtpOffset = 0;
        return;
    }

    printf("ThreadNtpSamples started\n");
    vnThreadsRunning[THREAD_NTP]++;

    // Make this thread recognisable as time synchronization thread
    RenameThread("Chipcoin-ntp-samples");

    CMedianFilter<int64_t> vTimeOffsets(200,0);

    while (!fShutdown) {
        if (strTrustedUpstream != "localhost") {
            // Trying to get new offset sample from trusted NTP server.
            int64_t nClockOffset = NtpGetTime(strTrustedUpstream) - GetTime();

            if (abs64(nClockOffset) < nMaxOffset) {
                // Everything seems right, remember new trusted offset.
                printf("ThreadNtpSamples: new offset sample from %s, offset=%" PRId64 ".\n", strTrustedUpstream.c_str(), nClockOffset);
                nNtpOffset = nClockOffset;
            }
            else {
                // Something went wrong, disable trusted offset sampling.
                nNtpOffset = INT64_MAX;
                strTrustedUpstream = "localhost";

                int nSleepMinutes = 1 + GetRandInt(9); // Sleep for 1-10 minutes.
                for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++)
                    MilliSleep(1000);

                continue;
            }
        }
        else {
            // Now, trying to get 2-4 samples from random NTP servers.
            int nSamplesCount = 2 + GetRandInt(2);

            for (int i = 0; i < nSamplesCount; i++) {
                CNetAddr ip;
                int64_t nClockOffset = NtpGetTime(ip) - GetTime();

                if (abs64(nClockOffset) < nMaxOffset) { // Skip the deliberately wrong timestamps
                    printf("ThreadNtpSamples: new offset sample from %s, offset=%" PRId64 ".\n", ip.ToString().c_str(), nClockOffset);
                    vTimeOffsets.input(nClockOffset);
                }
            }

            if (vTimeOffsets.size() > 1) {
                nNtpOffset = vTimeOffsets.median();
            }
            else {
                // Not enough offsets yet, try to collect additional samples later.
                nNtpOffset = INT64_MAX;
                int nSleepMinutes = 1 + GetRandInt(4); // Sleep for 1-5 minutes.
                for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++) 
                    MilliSleep(1000);
                continue;
            }
        }

        if (GetNodesOffset() == INT_MAX && abs64(nNtpOffset) > 40 * 60)
        {
            // If there is not enough node offsets data and NTP time offset is greater than 40 minutes then give a warning.
            std::string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Chipcoin will not work properly.");
            strMiscWarning = strMessage;
            printf("*** %s\n", strMessage.c_str());
            uiInterface.ThreadSafeMessageBox(strMessage+" ", std::string("Chipcoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
        }

        printf("nNtpOffset = %+" PRId64 "  (%+" PRId64 " minutes)\n", nNtpOffset, nNtpOffset/60);

        int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours.
        for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++)
            MilliSleep(1000);
    }

    vnThreadsRunning[THREAD_NTP]--;
    printf("ThreadNtpSamples exited\n");
}