Beispiel #1
0
/* Calculte runs
 *   - Stale runs, runs sitting in cache for a long time or runs not growing, get priority.
 *     Returns number of runs.
 */
static int
calcRuns (tr_cache * cache, struct run_info * runs)
{
  const int n = tr_ptrArraySize (&cache->blocks);
  int i = 0, pos;
  const time_t now = tr_time ();

  for (pos = 0; pos < n; pos += runs[i++].len)
    {
      int rank = getBlockRun (cache, pos, &runs[i]);

      /* This adds ~2 to the relative length of a run for every minute it has
       * languished in the cache. */
      rank += (now - runs[i].last_block_time) / 32;

      /* Flushing stale blocks should be a top priority as the probability of them
       * growing is very small, for blocks on piece boundaries, and nonexistant for
       * blocks inside pieces. */
      rank |= runs[i].is_piece_done ? DONEFLAG : 0;

      /* Move the multi piece runs higher */
      rank |= runs[i].is_multi_piece ? MULTIFLAG : 0;

      runs[i].rank = rank;

      //fprintf (stderr,"block run at pos %d of length %d and age %ld adjusted +%d\n",runs[i].pos,runs[i].len,now-runs[i].last_block_time,rank-runs[i].len);
    }

  //fprintf (stderr, "%d block runs\n", i);
  qsort (runs, i, sizeof (struct run_info), compareRuns);
  return i;
}
Beispiel #2
0
unsigned int tr_historyGet(tr_recentHistory const* h, time_t now, unsigned int sec)
{
    unsigned int n = 0;
    time_t const cutoff = (now != 0 ? now : tr_time()) - sec;
    int i = h->newest;

    for (;;)
    {
        if (h->slices[i].date <= cutoff)
        {
            break;
        }

        n += h->slices[i].n;

        if (--i == -1)
        {
            i = TR_RECENT_HISTORY_PERIOD_SEC - 1; /* circular history */
        }

        if (i == h->newest)
        {
            break; /* we've come all the way around */
        }
    }

    return n;
}
Beispiel #3
0
static const char*
dns_cache_set_name( struct tr_web_task * task, const char * host,
                   const char * resolved, int ttl )
{
    char * ret = NULL;

    ttl = MAX( MIN_DNS_CACHE_TIME, ttl );

    if( task->session->web != NULL )
    {
        struct dns_cache_item * item;
        tr_ptrArray * cache = &task->session->web->dns_cache;

        dns_cache_clear_entry( cache, host );

        item = tr_new( struct dns_cache_item, 1 );
        item->host = tr_strdup( host );
        item->resolved_host = tr_strdup( resolved );
        item->expiration = tr_time( ) + ttl;
        item->success = TRUE;
        tr_ptrArrayInsertSorted( cache, item, dns_cache_compare );
        ret = item->resolved_host;
        dbgmsg( "adding dns cache entry for \"%s\": %s", host, resolved );
    }
    return ret;
}
Beispiel #4
0
void tr_statsInit(tr_session* session)
{
    struct tr_stats_handle* stats = tr_new0(struct tr_stats_handle, 1);

    loadCumulativeStats(session, &stats->old);
    stats->single.sessionCount = 1;
    stats->startTime = tr_time();
    session->sessionStats = stats;
}
Beispiel #5
0
void tr_sessionGetStats(tr_session const* session, tr_session_stats* setme)
{
    struct tr_stats_handle const* stats = getStats(session);

    if (stats != NULL)
    {
        *setme = stats->single;
        setme->secondsActive = tr_time() - stats->startTime;
        updateRatio(setme);
    }
}
Beispiel #6
0
void tr_sessionClearStats(tr_session* session)
{
    tr_session_stats zero;

    zero.uploadedBytes = 0;
    zero.downloadedBytes = 0;
    zero.ratio = TR_RATIO_NA;
    zero.filesAdded = 0;
    zero.sessionCount = 0;
    zero.secondsActive = 0;

    session->sessionStats->isDirty = true;
    session->sessionStats->single = session->sessionStats->old = zero;
    session->sessionStats->startTime = tr_time();
}
Beispiel #7
0
static tr_peerIo*
tr_peerIoNew( tr_session       * session,
              tr_bandwidth     * parent,
              const tr_address * addr,
              tr_port            port,
              const uint8_t    * torrentHash,
              tr_bool            isIncoming,
              tr_bool            isSeed,
              int                socket )
{
    tr_peerIo * io;

    assert( session != NULL );
    assert( session->events != NULL );
    assert( tr_isBool( isIncoming ) );
    assert( tr_isBool( isSeed ) );
    assert( tr_amInEventThread( session ) );

    if( socket >= 0 ) {
        tr_netSetTOS( socket, session->peerSocketTOS );
        maybeSetCongestionAlgorithm( socket, session->peer_congestion_algorithm );
    }

    io = tr_new0( tr_peerIo, 1 );
    io->magicNumber = MAGIC_NUMBER;
    io->refCount = 1;
    io->crypto = tr_cryptoNew( torrentHash, isIncoming );
    io->session = session;
    io->addr = *addr;
    io->isSeed = isSeed;
    io->port = port;
    io->socket = socket;
    io->isIncoming = isIncoming != 0;
    io->hasFinishedConnecting = FALSE;
    io->timeCreated = tr_time( );
    io->inbuf = evbuffer_new( );
    io->outbuf = evbuffer_new( );
    tr_bandwidthConstruct( &io->bandwidth, session, parent );
    tr_bandwidthSetPeer( &io->bandwidth, io );
    dbgmsg( io, "bandwidth is %p; its parent is %p", &io->bandwidth, parent );

    event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io );
    event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io );

    return io;
}
Beispiel #8
0
/* Return our global IPv6 address, with caching. */
unsigned char const* tr_globalIPv6(void)
{
    static unsigned char ipv6[16];
    static time_t last_time = 0;
    static bool have_ipv6 = false;
    time_t const now = tr_time();

    /* Re-check every half hour */
    if (last_time < now - 1800)
    {
        int addrlen = 16;
        int const rc = tr_globalAddress(AF_INET6, ipv6, &addrlen);
        have_ipv6 = rc >= 0 && addrlen == 16;
        last_time = now;
    }

    return have_ipv6 ? ipv6 : NULL;
}
Beispiel #9
0
static void
dns_cache_set_fail( struct tr_web_task * task, const char * host )
{
    if( ( task->session->web != NULL ) && ( host != NULL ) )
    {
        struct dns_cache_item * item;
        tr_ptrArray * cache = &task->session->web->dns_cache;

        dns_cache_clear_entry( cache, host );

        item = tr_new( struct dns_cache_item, 1 );
        item->host = tr_strdup( host );
        item->resolved_host = NULL;
        item->expiration = tr_time( ) + MIN_DNS_CACHE_TIME;
        item->success = FALSE;
        tr_ptrArrayInsertSorted( cache, item, dns_cache_compare );
    }
}
Beispiel #10
0
int
tr_fdFileGetCached( tr_session       * session,
                    int                torrentId,
                    tr_file_index_t    fileNum,
                    tr_bool            doWrite )
{
    struct tr_openfile * match = NULL;
    struct tr_fdInfo * gFd;

    assert( tr_isSession( session ) );
    assert( session->fdInfo != NULL );
    assert( torrentId > 0 );
    assert( tr_isBool( doWrite ) );

    gFd = session->fdInfo;

    /* is it already open? */
    {
        int i;
        struct tr_openfile * o;
        for( i=0; i<gFd->openFileLimit; ++i )
        {
            o = &gFd->openFiles[i];

            if( torrentId != o->torrentId )
                continue;
            if( fileNum != o->fileNum )
                continue;
            if( !fileIsOpen( o ) )
                continue;

            match = o;
            break;
        }
    }

    if( ( match != NULL ) && ( !doWrite || match->isWritable ) )
    {
        match->date = tr_time( );
        return match->fd;
    }

    return -1;
}
Beispiel #11
0
static gboolean
core_watchdir_idle (gpointer gcore)
{
  GSList * l;
  GSList * changing = NULL;
  GSList * unchanging = NULL;
  TrCore * core = TR_CORE (gcore);
  const time_t now = tr_time ();
  struct TrCorePrivate * p = core->priv;

  /* separate the files into two lists: changing and unchanging */
  for (l=p->monitor_files; l!=NULL; l=l->next)
    {
      GFile * file = l->data;
      const time_t mtime = get_file_mtime (file);
      if (mtime + 2 >= now)
        changing = g_slist_prepend (changing, file);
      else
        unchanging = g_slist_prepend (unchanging, file);
    }

  /* add the files that have stopped changing */
  if (unchanging != NULL)
    {
      const gboolean do_start = gtr_pref_flag_get (TR_KEY_start_added_torrents);
      const gboolean do_prompt = gtr_pref_flag_get (TR_KEY_show_options_window);

      core->priv->adding_from_watch_dir = TRUE;
      gtr_core_add_files (core, unchanging, do_start, do_prompt, TRUE);
      g_slist_foreach (unchanging, (GFunc)rename_torrent_and_unref_file, NULL);
      g_slist_free (unchanging);
      core->priv->adding_from_watch_dir = FALSE;
    }

  /* keep monitoring the ones that are still changing */
  g_slist_free (p->monitor_files);
  p->monitor_files = changing;

  /* if monitor_files is nonempty, keep checking every second */
  if (core->priv->monitor_files)
    return G_SOURCE_CONTINUE;
  core->priv->monitor_idle_tag = 0;
  return G_SOURCE_REMOVE;
}
Beispiel #12
0
unsigned int
tr_historyGet( const tr_recentHistory * h, time_t now, unsigned int sec )
{
    unsigned int n = 0;
    const time_t cutoff = (now?now:tr_time()) - sec;
    int i = h->newest;

    for( ;; )
    {
        if( h->slices[i].date <= cutoff )
            break;

        n += h->slices[i].n;

        if( --i == -1 ) i = h->sliceCount - 1; /* circular history */
        if( i == h->newest ) break; /* we've come all the way around */
    }

    return n;
}
Beispiel #13
0
static tr_dns_result
dns_cache_lookup( struct tr_web_task * task, const char * host, const char ** resolved )
{
    tr_dns_result result = TR_DNS_UNTESTED;

    if( task->session->web != NULL )
    {
        struct dns_cache_item key;
        struct dns_cache_item * item;
        tr_ptrArray * cache = &task->session->web->dns_cache;

        key.host = (char*) host;
        item = tr_ptrArrayFindSorted( cache, &key, dns_cache_compare );

        /* has the ttl expired? */
        if( ( item != NULL ) && ( item->expiration <= tr_time( ) ) )
        {
            dns_cache_clear_entry( cache, host );
            item = NULL;
        }

        if( item != NULL )
        {
            result = item->success ? TR_DNS_OK : TR_DNS_FAIL;

            if( result == TR_DNS_OK )
            {
                *resolved = item->resolved_host;
            
                dbgmsg( "found cached dns entry for \"%s\": %s", host, *resolved );
            }
        }
    }

    return result;
}
Beispiel #14
0
static void saveProgress(tr_variant* dict, tr_torrent* tor)
{
    tr_variant* l;
    tr_variant* prog;
    tr_info const* inf = tr_torrentInfo(tor);
    time_t const now = tr_time();

    prog = tr_variantDictAddDict(dict, TR_KEY_progress, 3);

    /* add the file/piece check timestamps... */
    l = tr_variantDictAddList(prog, TR_KEY_time_checked, inf->fileCount);

    for (tr_file_index_t fi = 0; fi < inf->fileCount; ++fi)
    {
        time_t oldest_nonzero = now;
        time_t newest = 0;
        bool has_zero = false;
        time_t const mtime = tr_torrentGetFileMTime(tor, fi);
        tr_file const* f = &inf->files[fi];

        /* get the oldest and newest nonzero timestamps for pieces in this file */
        for (tr_piece_index_t i = f->firstPiece; i <= f->lastPiece; ++i)
        {
            tr_piece const* const p = &inf->pieces[i];

            if (p->timeChecked == 0)
            {
                has_zero = true;
            }
            else if (oldest_nonzero > p->timeChecked)
            {
                oldest_nonzero = p->timeChecked;
            }

            if (newest < p->timeChecked)
            {
                newest = p->timeChecked;
            }
        }

        /* If some of a file's pieces have been checked more recently than
           the file's mtime, and some less recently, then that file will
           have a list containing timestamps for each piece.

           However, the most common use case is that the file doesn't change
           after it's downloaded. To reduce overhead in the .resume file,
           only a single timestamp is saved for the file if *all* or *none*
           of the pieces were tested more recently than the file's mtime. */

        if (!has_zero && mtime <= oldest_nonzero) /* all checked */
        {
            tr_variantListAddInt(l, oldest_nonzero);
        }
        else if (newest < mtime) /* none checked */
        {
            tr_variantListAddInt(l, newest);
        }
        else /* some are checked, some aren't... so list piece by piece */
        {
            int const offset = oldest_nonzero - 1;
            tr_variant* ll = tr_variantListAddList(l, 2 + f->lastPiece - f->firstPiece);
            tr_variantListAddInt(ll, offset);

            for (tr_piece_index_t i = f->firstPiece; i <= f->lastPiece; ++i)
            {
                tr_piece const* const p = &inf->pieces[i];

                tr_variantListAddInt(ll, p->timeChecked != 0 ? p->timeChecked - offset : 0);
            }
        }
    }

    /* add the progress */
    if (tor->completeness == TR_SEED)
    {
        tr_variantDictAddStr(prog, TR_KEY_have, "all");
    }

    /* add the blocks bitfield */
    bitfieldToBenc(&tor->completion.blockBitfield, tr_variantDictAdd(prog, TR_KEY_blocks));
}
Beispiel #15
0
void
tr_msg( const char * file, int line,
        tr_msg_level level,
        const char * name,
        const char * fmt, ... )
{
    const int err = errno; /* message logging shouldn't affect errno */
    char buf[1024];
    va_list ap;
    tr_lockLock( getMessageLock( ) );

    /* build the text message */
    *buf = '\0';
    va_start( ap, fmt );
    evutil_vsnprintf( buf, sizeof( buf ), fmt, ap );
    va_end( ap );

    OutputDebugString( buf );

    if( *buf )
    {
        if( messageQueuing )
        {
            tr_msg_list * newmsg;
            newmsg = tr_new0( tr_msg_list, 1 );
            newmsg->level = level;
            newmsg->when = tr_time( );
            newmsg->message = tr_strdup( buf );
            newmsg->file = file;
            newmsg->line = line;
            newmsg->name = tr_strdup( name );

            *messageQueueTail = newmsg;
            messageQueueTail = &newmsg->next;
            ++messageQueueCount;

            if( messageQueueCount > TR_MAX_MSG_LOG )
            {
                tr_msg_list * old = messageQueue;
                messageQueue = old->next;
                old->next = NULL;
                tr_freeMessageList(old);

                --messageQueueCount;

                assert( messageQueueCount == TR_MAX_MSG_LOG );
            }
        }
        else
        {
            char timestr[64];
            FILE * fp;

            fp = tr_getLog( );
            if( fp == NULL )
                fp = stderr;

            tr_getLogTimeStr( timestr, sizeof( timestr ) );

            if( name )
                fprintf( fp, "[%s] %s: %s\n", timestr, name, buf );
            else
                fprintf( fp, "[%s] %s\n", timestr, buf );
            fflush( fp );
        }
    }

    tr_lockUnlock( getMessageLock( ) );
    errno = err;
}
Beispiel #16
0
int
tr_natpmpPulse( struct tr_natpmp * nat,
                int                port,
                int                isEnabled )
{
    int ret;

    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
    {
        int val = initnatpmp( &nat->natpmp );
        logVal( "initnatpmp", val );
        val = sendpublicaddressrequest( &nat->natpmp );
        logVal( "sendpublicaddressrequest", val );
        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
        nat->hasDiscovered = 1;
        setCommandTime( nat );
    }

    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
    {
        natpmpresp_t response;
        const int    val = readnatpmpresponseorretry( &nat->natpmp,
                                                      &response );
        logVal( "readnatpmpresponseorretry", val );
        if( val >= 0 )
        {
            tr_ninf( getKey( ), _(
                        "Found public address \"%s\"" ),
                    inet_ntoa( response.pnu.publicaddress.addr ) );
            nat->state = TR_NATPMP_IDLE;
        }
        else if( val != NATPMP_TRYAGAIN )
        {
            nat->state = TR_NATPMP_ERR;
        }
    }

    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
    {
        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
            nat->state = TR_NATPMP_SEND_UNMAP;
    }

    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
    {
        const int val =
            sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP,
                                       nat->port, nat->port,
                                       0 );
        logVal( "sendnewportmappingrequest", val );
        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
        setCommandTime( nat );
    }

    if( nat->state == TR_NATPMP_RECV_UNMAP )
    {
        natpmpresp_t resp;
        const int    val = readnatpmpresponseorretry( &nat->natpmp, &resp );
        logVal( "readnatpmpresponseorretry", val );
        if( val >= 0 )
        {
            const int p = resp.pnu.newportmapping.privateport;
            tr_ninf( getKey( ), _( "no longer forwarding port %d" ), p );
            if( nat->port == p )
            {
                nat->port = -1;
                nat->state = TR_NATPMP_IDLE;
                nat->isMapped = 0;
            }
        }
        else if( val != NATPMP_TRYAGAIN )
        {
            nat->state = TR_NATPMP_ERR;
        }
    }

    if( nat->state == TR_NATPMP_IDLE )
    {
        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
            nat->state = TR_NATPMP_SEND_MAP;

        else if( nat->isMapped && tr_time( ) >= nat->renewTime )
            nat->state = TR_NATPMP_SEND_MAP;
    }

    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
    {
        const int val =
            sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP,
                                       port,
                                       port,
                                       LIFETIME_SECS );
        logVal( "sendnewportmappingrequest", val );
        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
        setCommandTime( nat );
    }

    if( nat->state == TR_NATPMP_RECV_MAP )
    {
        natpmpresp_t resp;
        const int    val = readnatpmpresponseorretry( &nat->natpmp, &resp );
        logVal( "readnatpmpresponseorretry", val );
        if( val >= 0 )
        {
            nat->state = TR_NATPMP_IDLE;
            nat->isMapped = 1;
            nat->renewTime = tr_time( ) + LIFETIME_SECS;
            nat->port = resp.pnu.newportmapping.privateport;
            tr_ninf( getKey( ), _(
                         "Port %d forwarded successfully" ), nat->port );
        }
        else if( val != NATPMP_TRYAGAIN )
        {
            nat->state = TR_NATPMP_ERR;
        }
    }

    switch( nat->state )
    {
        case TR_NATPMP_IDLE:
            ret = nat->isMapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED; break;

        case TR_NATPMP_DISCOVER:
            ret = TR_PORT_UNMAPPED; break;

        case TR_NATPMP_RECV_PUB:
        case TR_NATPMP_SEND_MAP:
        case TR_NATPMP_RECV_MAP:
            ret = TR_PORT_MAPPING; break;

        case TR_NATPMP_SEND_UNMAP:
        case TR_NATPMP_RECV_UNMAP:
            ret = TR_PORT_UNMAPPING; break;

        default:
            ret = TR_PORT_ERROR; break;
    }
    return ret;
}
Beispiel #17
0
void
tr_logAddMessage (const char * file,
                  int line,
                  tr_log_level level,
                  const char * name,
                  const char * fmt,
                  ...)
{
  const int err = errno; /* message logging shouldn't affect errno */
  char buf[1024];
  int buf_len;
  va_list ap;
  tr_lockLock (getMessageLock ());

  /* build the text message */
  *buf = '\0';
  va_start (ap, fmt);
  buf_len = evutil_vsnprintf (buf, sizeof (buf), fmt, ap);
  va_end (ap);

  if (buf_len < 0)
    return;

#ifdef _WIN32
  if ((size_t) buf_len < sizeof (buf) - 3)
    {
      buf[buf_len + 0] = '\r';
      buf[buf_len + 1] = '\n';
      buf[buf_len + 2] = '\0';
      OutputDebugStringA (buf);
      buf[buf_len + 0] = '\0';
    }
  else
    {
      OutputDebugStringA (buf);
    }
#endif

  if (*buf)
    {
      if (tr_logGetQueueEnabled ())
        {
          tr_log_message * newmsg;
          newmsg = tr_new0 (tr_log_message, 1);
          newmsg->level = level;
          newmsg->when = tr_time ();
          newmsg->message = tr_strdup (buf);
          newmsg->file = file;
          newmsg->line = line;
          newmsg->name = tr_strdup (name);

          *myQueueTail = newmsg;
          myQueueTail = &newmsg->next;
          ++myQueueLength;

          if (myQueueLength > TR_LOG_MAX_QUEUE_LENGTH)
            {
              tr_log_message * old = myQueue;
              myQueue = old->next;
              old->next = NULL;
              tr_logFreeQueue (old);
              --myQueueLength;
              assert (myQueueLength == TR_LOG_MAX_QUEUE_LENGTH);
            }
        }
      else
        {
          tr_sys_file_t fp;
          char timestr[64];

          fp = tr_logGetFile ();
          if (fp == TR_BAD_SYS_FILE)
            fp = tr_sys_file_get_std (TR_STD_SYS_FILE_ERR, NULL);

          tr_logGetTimeStr (timestr, sizeof (timestr));

          if (name)
            tr_sys_file_write_fmt (fp, "[%s] %s: %s" TR_NATIVE_EOL_STR, NULL, timestr, name, buf);
          else
            tr_sys_file_write_fmt (fp, "[%s] %s" TR_NATIVE_EOL_STR, NULL, timestr, buf);
          tr_sys_file_flush (fp, NULL);
        }
    }

  tr_lockUnlock (getMessageLock ());
  errno = err;
}
Beispiel #18
0
static bool verifyTorrent(tr_torrent* tor, bool* stopFlag)
{
    time_t end;
    tr_sha1_ctx_t sha;
    tr_sys_file_t fd = TR_BAD_SYS_FILE;
    uint64_t filePos = 0;
    bool changed = false;
    bool hadPiece = false;
    time_t lastSleptAt = 0;
    uint32_t piecePos = 0;
    tr_file_index_t fileIndex = 0;
    tr_file_index_t prevFileIndex = !fileIndex;
    tr_piece_index_t pieceIndex = 0;
    time_t const begin = tr_time();
    size_t const buflen = 1024 * 128; /* 128 KiB buffer */
    uint8_t* buffer = tr_valloc(buflen);

    sha = tr_sha1_init();

    tr_logAddTorDbg(tor, "%s", "verifying torrent...");
    tr_torrentSetChecked(tor, 0);

    while (!*stopFlag && pieceIndex < tor->info.pieceCount)
    {
        uint64_t leftInPiece;
        uint64_t bytesThisPass;
        uint64_t leftInFile;
        tr_file const* file = &tor->info.files[fileIndex];

        /* if we're starting a new piece... */
        if (piecePos == 0)
        {
            hadPiece = tr_torrentPieceIsComplete(tor, pieceIndex);
        }

        /* if we're starting a new file... */
        if (filePos == 0 && fd == TR_BAD_SYS_FILE && fileIndex != prevFileIndex)
        {
            char* filename = tr_torrentFindFile(tor, fileIndex);
            fd = filename == NULL ? TR_BAD_SYS_FILE : tr_sys_file_open(filename, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0,
                NULL);
            tr_free(filename);
            prevFileIndex = fileIndex;
        }

        /* figure out how much we can read this pass */
        leftInPiece = tr_torPieceCountBytes(tor, pieceIndex) - piecePos;
        leftInFile = file->length - filePos;
        bytesThisPass = MIN(leftInFile, leftInPiece);
        bytesThisPass = MIN(bytesThisPass, buflen);

        /* read a bit */
        if (fd != TR_BAD_SYS_FILE)
        {
            uint64_t numRead;

            if (tr_sys_file_read_at(fd, buffer, bytesThisPass, filePos, &numRead, NULL) && numRead > 0)
            {
                bytesThisPass = numRead;
                tr_sha1_update(sha, buffer, bytesThisPass);
#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
                (void)posix_fadvise(fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED);
#endif
            }
        }

        /* move our offsets */
        leftInPiece -= bytesThisPass;
        leftInFile -= bytesThisPass;
        piecePos += bytesThisPass;
        filePos += bytesThisPass;

        /* if we're finishing a piece... */
        if (leftInPiece == 0)
        {
            time_t now;
            bool hasPiece;
            uint8_t hash[SHA_DIGEST_LENGTH];

            tr_sha1_final(sha, hash);
            hasPiece = memcmp(hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH) == 0;

            if (hasPiece || hadPiece)
            {
                tr_torrentSetHasPiece(tor, pieceIndex, hasPiece);
                changed |= hasPiece != hadPiece;
            }

            tr_torrentSetPieceChecked(tor, pieceIndex);
            now = tr_time();
            tor->anyDate = now;

            /* sleeping even just a few msec per second goes a long
             * way towards reducing IO load... */
            if (lastSleptAt != now)
            {
                lastSleptAt = now;
                tr_wait_msec(MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY);
            }

            sha = tr_sha1_init();
            pieceIndex++;
            piecePos = 0;
        }

        /* if we're finishing a file... */
        if (leftInFile == 0)
        {
            if (fd != TR_BAD_SYS_FILE)
            {
                tr_sys_file_close(fd, NULL);
                fd = TR_BAD_SYS_FILE;
            }

            fileIndex++;
            filePos = 0;
        }
    }

    /* cleanup */
    if (fd != TR_BAD_SYS_FILE)
    {
        tr_sys_file_close(fd, NULL);
    }

    tr_sha1_final(sha, NULL);
    free(buffer);

    /* stopwatch */
    end = tr_time();
    tr_logAddTorDbg(tor, "Verification is done. It took %d seconds to verify %" PRIu64 " bytes (%" PRIu64 " bytes per second)",
        (int)(end - begin), tor->info.totalSize, (uint64_t)(tor->info.totalSize / (1 + (end - begin))));

    return changed;
}
static void
setCommandTime( struct tr_natpmp * nat )
{
    nat->command_time = tr_time( ) + COMMAND_WAIT_SECS;
}
Beispiel #20
0
static tr_bool
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
{
    time_t end;
    SHA_CTX sha;
    int fd = -1;
    int64_t filePos = 0;
    tr_bool changed = 0;
    tr_bool hadPiece = 0;
    time_t lastSleptAt = 0;
    uint32_t piecePos = 0;
    tr_file_index_t fileIndex = 0;
    tr_file_index_t prevFileIndex = !fileIndex;
    tr_piece_index_t pieceIndex = 0;
    const time_t begin = tr_time( );
    const size_t buflen = 1024 * 128; /* 128 KiB buffer */
    uint8_t * buffer = tr_valloc( buflen );

    SHA1_Init( &sha );

    tr_tordbg( tor, "%s", "verifying torrent..." );
    tr_torrentSetChecked( tor, 0 );
    while( !*stopFlag && ( pieceIndex < tor->info.pieceCount ) )
    {
        uint32_t leftInPiece;
        uint32_t bytesThisPass;
        uint64_t leftInFile;
        const tr_file * file = &tor->info.files[fileIndex];

        /* if we're starting a new piece... */
        if( piecePos == 0 )
            hadPiece = tr_cpPieceIsComplete( &tor->completion, pieceIndex );

        /* if we're starting a new file... */
        if( !filePos && (fd<0) && (fileIndex!=prevFileIndex) )
        {
            char * filename = tr_torrentFindFile( tor, fileIndex );
            fd = filename == NULL ? -1 : tr_open_file_for_scanning( filename );
            tr_free( filename );
            prevFileIndex = fileIndex;
        }

        /* figure out how much we can read this pass */
        leftInPiece = tr_torPieceCountBytes( tor, pieceIndex ) - piecePos;
        leftInFile = file->length - filePos;
        bytesThisPass = MIN( leftInFile, leftInPiece );
        bytesThisPass = MIN( bytesThisPass, buflen );

        /* read a bit */
        if( fd >= 0 ) {
            const ssize_t numRead = tr_pread( fd, buffer, bytesThisPass, filePos );
            if( numRead > 0 ) {
                bytesThisPass = (uint32_t)numRead;
                SHA1_Update( &sha, buffer, bytesThisPass );
#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
                posix_fadvise( fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED );
#endif
            }
        }

        /* move our offsets */
        leftInPiece -= bytesThisPass;
        leftInFile -= bytesThisPass;
        piecePos += bytesThisPass;
        filePos += bytesThisPass;

        /* if we're finishing a piece... */
        if( leftInPiece == 0 )
        {
            time_t now;
            tr_bool hasPiece;
            uint8_t hash[SHA_DIGEST_LENGTH];

            SHA1_Final( hash, &sha );
            hasPiece = !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );

            if( hasPiece || hadPiece ) {
                tr_torrentSetHasPiece( tor, pieceIndex, hasPiece );
                changed |= hasPiece != hadPiece;
            }
            tr_torrentSetPieceChecked( tor, pieceIndex );
            now = tr_time( );
            tor->anyDate = now;

            /* sleeping even just a few msec per second goes a long
             * way towards reducing IO load... */
            if( lastSleptAt != now ) {
                lastSleptAt = now;
                tr_wait_msec( MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY );
            }

            SHA1_Init( &sha );
            ++pieceIndex;
            piecePos = 0;
        }

        /* if we're finishing a file... */
        if( leftInFile == 0 )
        {
            if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
            ++fileIndex;
            filePos = 0;
        }
    }

    /* cleanup */
    if( fd >= 0 )
        tr_close_file( fd );
    free( buffer );

    /* stopwatch */
    end = tr_time( );
    tr_tordbg( tor, "Verification is done. It took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)",
               (int)(end-begin), tor->info.totalSize,
               (uint64_t)(tor->info.totalSize/(1+(end-begin))) );

    return changed;
}
static int
canSendCommand( const struct tr_natpmp * nat )
{
    return tr_time( ) >= nat->command_time;
}
Beispiel #22
0
/* returns an fd on success, or a -1 on failure and sets errno */
int
tr_fdFileCheckout( tr_session             * session,
                   int                      torrentId,
                   tr_file_index_t          fileNum,
                   const char             * filename,
                   tr_bool                  doWrite,
                   tr_preallocation_mode    preallocationMode,
                   uint64_t                 desiredFileSize )
{
    int i, winner = -1;
    struct tr_fdInfo * gFd;
    struct tr_openfile * o;

    assert( tr_isSession( session ) );
    assert( session->fdInfo != NULL );
    assert( torrentId > 0 );
    assert( filename && *filename );
    assert( tr_isBool( doWrite ) );

    gFd = session->fdInfo;

    dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );

    /* is it already open? */
    for( i=0; i<gFd->openFileLimit; ++i )
    {
        o = &gFd->openFiles[i];

        if( torrentId != o->torrentId )
            continue;
        if( fileNum != o->fileNum )
            continue;
        if( !fileIsOpen( o ) )
            continue;

        if( doWrite && !o->isWritable )
        {
            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
            TrCloseFile( o );
            break;
        }

        dbgmsg( "found it!  it's ready for use!" );
        winner = i;
        break;
    }

    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
    while( winner < 0 )
    {
        time_t date = tr_time( ) + 1;

        /* look for the file that's been open longest */
        for( i=0; i<gFd->openFileLimit; ++i )
        {
            o = &gFd->openFiles[i];

            if( !fileIsOpen( o ) )
            {
                winner = i;
                dbgmsg( "found an empty slot in %d", winner );
                break;
            }

            if( date > o->date )
            {
                date = o->date;
                winner = i;
            }
        }

        assert( winner >= 0 );

        if( fileIsOpen( &gFd->openFiles[winner] ) )
        {
            dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename );
            TrCloseFile( &gFd->openFiles[winner] );
        }
    }

    assert( winner >= 0 );
    o = &gFd->openFiles[winner];
    if( !fileIsOpen( o ) )
    {
        const int err = TrOpenFile( session, winner, filename, doWrite,
                                    preallocationMode, desiredFileSize );
        if( err ) {
            errno = err;
            return -1;
        }

        dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
                doWrite ? 'y' : 'n' );
        tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
        o->isWritable = doWrite;
    }

    dbgmsg( "checking out '%s' in slot %d", filename, winner );
    o->torrentId = torrentId;
    o->fileNum = fileNum;
    o->date = tr_time( );
    return o->fd;
}
int
tr_natpmpPulse( struct tr_natpmp * nat, tr_port private_port, bool is_enabled, tr_port * public_port )
{
    int ret;

    if( is_enabled && ( nat->state == TR_NATPMP_DISCOVER ) )
    {
        int val = initnatpmp( &nat->natpmp );
        logVal( "initnatpmp", val );
        val = sendpublicaddressrequest( &nat->natpmp );
        logVal( "sendpublicaddressrequest", val );
        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
        nat->has_discovered = true;
        setCommandTime( nat );
    }

    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
    {
        natpmpresp_t response;
        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
        logVal( "readnatpmpresponseorretry", val );
        if( val >= 0 )
        {
            char str[128];
            evutil_inet_ntop( AF_INET, &response.pnu.publicaddress.addr, str, sizeof( str ) );
            tr_ninf( getKey( ), _( "Found public address \"%s\"" ), str );
            nat->state = TR_NATPMP_IDLE;
        }
        else if( val != NATPMP_TRYAGAIN )
        {
            nat->state = TR_NATPMP_ERR;
        }
    }

    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
    {
        if( nat->is_mapped && ( !is_enabled || ( nat->private_port != private_port ) ) )
            nat->state = TR_NATPMP_SEND_UNMAP;
    }

    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
    {
        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP,
                                                   nat->private_port,
                                                   nat->public_port,
                                                   0 );
        logVal( "sendnewportmappingrequest", val );
        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
        setCommandTime( nat );
    }

    if( nat->state == TR_NATPMP_RECV_UNMAP )
    {
        natpmpresp_t resp;
        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
        logVal( "readnatpmpresponseorretry", val );
        if( val >= 0 )
        {
            const int private_port = resp.pnu.newportmapping.privateport;

            tr_ninf( getKey( ), _( "no longer forwarding port %d" ), private_port );

            if( nat->private_port == private_port )
            {
                nat->private_port = 0;
                nat->public_port = 0;
                nat->state = TR_NATPMP_IDLE;
                nat->is_mapped = false;
            }
        }
        else if( val != NATPMP_TRYAGAIN )
        {
            nat->state = TR_NATPMP_ERR;
        }
    }

    if( nat->state == TR_NATPMP_IDLE )
    {
        if( is_enabled && !nat->is_mapped && nat->has_discovered )
            nat->state = TR_NATPMP_SEND_MAP;

        else if( nat->is_mapped && tr_time( ) >= nat->renew_time )
            nat->state = TR_NATPMP_SEND_MAP;
    }

    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
    {
        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, private_port, private_port, LIFETIME_SECS );
        logVal( "sendnewportmappingrequest", val );
        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
        setCommandTime( nat );
    }

    if( nat->state == TR_NATPMP_RECV_MAP )
    {
        natpmpresp_t resp;
        const int    val = readnatpmpresponseorretry( &nat->natpmp, &resp );
        logVal( "readnatpmpresponseorretry", val );
        if( val >= 0 )
        {
            nat->state = TR_NATPMP_IDLE;
            nat->is_mapped = true;
            nat->renew_time = tr_time( ) + ( resp.pnu.newportmapping.lifetime / 2 );
            nat->private_port = resp.pnu.newportmapping.privateport;
            nat->public_port = resp.pnu.newportmapping.mappedpublicport;
            tr_ninf( getKey( ), _( "Port %d forwarded successfully" ), nat->private_port );
        }
        else if( val != NATPMP_TRYAGAIN )
        {
            nat->state = TR_NATPMP_ERR;
        }
    }

    switch( nat->state )
    {
        case TR_NATPMP_IDLE:
            *public_port = nat->public_port;
            return nat->is_mapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED;
            break;

        case TR_NATPMP_DISCOVER:
            ret = TR_PORT_UNMAPPED; break;

        case TR_NATPMP_RECV_PUB:
        case TR_NATPMP_SEND_MAP:
        case TR_NATPMP_RECV_MAP:
            ret = TR_PORT_MAPPING; break;

        case TR_NATPMP_SEND_UNMAP:
        case TR_NATPMP_RECV_UNMAP:
            ret = TR_PORT_UNMAPPING; break;

        default:
            ret = TR_PORT_ERROR; break;
    }
    return ret;
}
Beispiel #24
0
  assert (tr_amInEventThread (torrent->session));

  if (cb == NULL)
    {
      cb = tr_new (struct cache_block, 1);
      cb->tor = torrent;
      cb->piece = piece;
      cb->offset = offset;
      cb->length = length;
      cb->block = _tr_block (torrent, piece, offset);
      cb->evbuf = evbuffer_new ();
      tr_ptrArrayInsertSorted (&cache->blocks, cb, cache_block_compare);
    }

  cb->time = tr_time ();

  assert (cb->length == length);
  evbuffer_drain (cb->evbuf, evbuffer_get_length (cb->evbuf));
  evbuffer_remove_buffer (writeme, cb->evbuf, cb->length);

  cache->cache_writes++;
  cache->cache_write_bytes += cb->length;

  return cacheTrim (cache);
}

int
tr_cacheReadBlock (tr_cache         * cache,
                   tr_torrent       * torrent,
                   tr_piece_index_t   piece,
Beispiel #25
0
static void
saveProgress( tr_benc * dict, tr_torrent * tor )
{
    tr_benc * l;
    tr_benc * prog;
    tr_file_index_t fi;
    const tr_info * inf = tr_torrentInfo( tor );
    const time_t now = tr_time( );

    prog = tr_bencDictAddDict( dict, KEY_PROGRESS, 3 );

    /* add the file/piece check timestamps... */
    l = tr_bencDictAddList( prog, KEY_PROGRESS_CHECKTIME, inf->fileCount );
    for( fi=0; fi<inf->fileCount; ++fi )
    {
        const tr_piece * p;
        const tr_piece * pend;
        time_t oldest_nonzero = now;
        time_t newest = 0;
        tr_bool has_zero = FALSE;
        const time_t mtime = tr_torrentGetFileMTime( tor, fi );
        const tr_file * f = &inf->files[fi];

        /* get the oldest and newest nonzero timestamps for pieces in this file */
        for( p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]; p!=pend; ++p )
        {
            if( !p->timeChecked )
                has_zero = TRUE;
            else if( oldest_nonzero > p->timeChecked )
                oldest_nonzero = p->timeChecked;
            if( newest < p->timeChecked )
                newest = p->timeChecked;
        }

        /* If some of a file's pieces have been checked more recently than
           the file's mtime, and some lest recently, then that file will
           have a list containing timestamps for each piece.
           
           However, the most common use case is that the file doesn't change
           after it's downloaded. To reduce overhead in the .resume file,
           only a single timestamp is saved for the file if *all* or *none*
           of the pieces were tested more recently than the file's mtime. */

        if( !has_zero && ( mtime <= oldest_nonzero ) ) /* all checked */
            tr_bencListAddInt( l, oldest_nonzero );
        else if( newest < mtime ) /* none checked */
            tr_bencListAddInt( l, newest );
        else { /* some are checked, some aren't... so list piece by piece */
            const int offset = oldest_nonzero - 1;
            tr_benc * ll = tr_bencListAddList( l, 2 + f->lastPiece - f->firstPiece );
            tr_bencListAddInt( ll, offset );
            for( p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]+1; p!=pend; ++p )
                tr_bencListAddInt( ll, p->timeChecked ? p->timeChecked - offset : 0 );
        }
    }

    /* add the progress */
    if( tor->completeness == TR_SEED )
        tr_bencDictAddStr( prog, KEY_PROGRESS_HAVE, "all" );

    /* add the blocks bitfield */
    tr_bitsetToBenc( tr_cpBlockBitset( &tor->completion ),
                     tr_bencDictAdd( prog, KEY_PROGRESS_BLOCKS ) );
}
Beispiel #26
0
void
tr_logAddMessage (const char * file,
                  int line,
                  tr_log_level level,
                  const char * name,
                  const char * fmt,
                  ...)
{
  const int err = errno; /* message logging shouldn't affect errno */
  char buf[1024];
  va_list ap;
  tr_lockLock (getMessageLock ());

  /* build the text message */
  *buf = '\0';
  va_start (ap, fmt);
  evutil_vsnprintf (buf, sizeof (buf), fmt, ap);
  va_end (ap);

  OutputDebugStringA (buf);

  if (*buf)
    {
      if (tr_logGetQueueEnabled ())
        {
          tr_log_message * newmsg;
          newmsg = tr_new0 (tr_log_message, 1);
          newmsg->level = level;
          newmsg->when = tr_time ();
          newmsg->message = tr_strdup (buf);
          newmsg->file = file;
          newmsg->line = line;
          newmsg->name = tr_strdup (name);

          *myQueueTail = newmsg;
          myQueueTail = &newmsg->next;
          ++myQueueLength;

          if (myQueueLength > TR_LOG_MAX_QUEUE_LENGTH)
            {
              tr_log_message * old = myQueue;
              myQueue = old->next;
              old->next = NULL;
              tr_logFreeQueue (old);
              --myQueueLength;
              assert (myQueueLength == TR_LOG_MAX_QUEUE_LENGTH);
            }
        }
      else
        {
          FILE * fp;
          char timestr[64];

          fp = tr_logGetFile ();
          if (fp == NULL)
            fp = stderr;

          tr_logGetTimeStr (timestr, sizeof (timestr));

          if (name)
            fprintf (fp, "[%s] %s: %s\n", timestr, name, buf);
          else
            fprintf (fp, "[%s] %s\n", timestr, buf);
          fflush (fp);
        }
    }

  tr_lockUnlock (getMessageLock ());
  errno = err;
}