source: trunk/libtransmission/announcer.c @ 11333

Last change on this file since 11333 was 11333, checked in by charles, 11 years ago

(trunk libT) #3638 "fewer crypto calls when creating peer-ids, session ids, announcer keys, etc" -- fixed.

  • Property svn:keywords set to Date Rev Author Id
File size: 59.8 KB
Line 
1/*
2 * This file Copyright (C) 2009-2010 Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: announcer.c 11333 2010-10-17 18:32:55Z charles $
11 */
12
13#include <assert.h>
14#include <limits.h>
15
16#include <event.h>
17#include <evhttp.h> /* for HTTP_OK */
18
19#include "transmission.h"
20#include "announcer.h"
21#include "crypto.h"
22#include "net.h"
23#include "ptrarray.h"
24#include "session.h"
25#include "tr-dht.h"
26#include "tr-lpd.h"
27#include "torrent.h"
28#include "utils.h"
29#include "web.h"
30
31#define STARTED "started"
32
33#define dbgmsg( tier, ... ) \
34if( tr_deepLoggingIsActive( ) ) do { \
35  char name[128]; \
36  tr_snprintf( name, sizeof( name ), "[%s--%s]", tr_torrentName( tier->tor ), \
37      ( tier->currentTracker ? tier->currentTracker->host->name : "" ) ); \
38  tr_deepLog( __FILE__, __LINE__, name, __VA_ARGS__ ); \
39} while( 0 )
40
41enum
42{
43    /* unless the tracker says otherwise, rescrape this frequently */
44    DEFAULT_SCRAPE_INTERVAL_SEC = ( 60 * 30 ),
45
46    /* unless the tracker says otherwise, this is the announce interval */
47    DEFAULT_ANNOUNCE_INTERVAL_SEC = ( 60 * 10 ),
48
49    /* unless the tracker says otherwise, this is the announce min_interval */
50    DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = ( 60 * 2 ),
51
52    /* the length of the 'key' argument passed in tracker requests */
53    KEYLEN = 8,
54
55    /* how many web tasks we allow at one time */
56    MAX_CONCURRENT_TASKS = 48,
57
58    /* if a tracker takes more than this long to respond,
59     * we treat it as nonresponsive */
60    MAX_TRACKER_RESPONSE_TIME_SECS = ( 60 * 2 ),
61
62    /* the value of the 'numwant' argument passed in tracker requests. */
63    NUMWANT = 80,
64
65    /* how long to put slow (nonresponsive) trackers in the penalty box */
66    SLOW_HOST_PENALTY_SECS = ( 60 * 10 ),
67
68    UPKEEP_INTERVAL_SECS = 1,
69
70    /* this is an upper limit for the frequency of LDS announces */
71    LPD_HOUSEKEEPING_INTERVAL_SECS = 30
72
73};
74
75/***
76****
77***/
78
79static int
80compareTransfer( uint32_t a_uploaded, uint32_t a_downloaded,
81                 uint32_t b_uploaded, uint32_t b_downloaded )
82{
83    /* higher upload count goes first */
84    if( a_uploaded != b_uploaded )
85        return a_uploaded > b_uploaded ? -1 : 1;
86
87    /* then higher download count goes first */
88    if( a_downloaded != b_downloaded )
89        return a_downloaded > b_downloaded ? -1 : 1;
90
91    return 0;
92}
93
94/***
95****
96***/
97
98/**
99 * used by tr_announcer to recognize nonresponsive
100 * trackers and de-prioritize them
101 */
102typedef struct
103{
104    char * name;
105
106    /* how many seconds it took to get the last tracker response */
107    int lastResponseInterval;
108
109    /* the last time we sent an announce or scrape message */
110    time_t lastRequestTime;
111
112    /* the last successful announce/scrape time for this host */
113    time_t lastSuccessfulRequest;
114}
115tr_host;
116
117static int
118compareHosts( const void * va, const void * vb )
119{
120    const tr_host * a = va;
121    const tr_host * b = vb;
122    return strcmp( a->name, b->name );
123}
124
125static int
126compareHostToName( const void * va, const void * vb )
127{
128    const tr_host * a = va;
129    return strcmp( a->name, vb );
130}
131
132/* format: hostname + ':' + port */
133static char *
134getHostName( const char * url )
135{
136    int port = 0;
137    char * host = NULL;
138    char * ret;
139    tr_urlParse( url, -1, NULL, &host, &port, NULL );
140    ret = tr_strdup_printf( "%s:%d", ( host ? host : "invalid" ), port );
141    tr_free( host );
142    return ret;
143}
144
145static tr_host*
146hostNew( const char * name )
147{
148    tr_host * host = tr_new0( tr_host, 1 );
149    host->name = tr_strdup( name );
150    return host;
151}
152
153static void
154hostFree( void * vhost )
155{
156    tr_host * host = vhost;
157
158    tr_free( host->name );
159    tr_free( host );
160}
161
162/***
163****
164***/
165
166/**
167 * Since we can't poll a tr_torrent's fields after it's destroyed,
168 * we pre-build the "stop" announcement message when a torrent
169 * is removed from Transmission
170 */
171struct stop_message
172{
173    tr_host * host;
174    char * url;
175    uint32_t up;
176    uint32_t down;
177};
178
179static void
180stopFree( struct stop_message * stop )
181{
182    tr_free( stop->url );
183    tr_free( stop );
184}
185
186static int
187compareStops( const void * va, const void * vb )
188{
189    const struct stop_message * a = va;
190    const struct stop_message * b = vb;
191    return compareTransfer( a->up, a->down, b->up, b->down);
192}
193
194/***
195****
196***/
197
198/**
199 * "global" (per-tr_session) fields
200 */
201typedef struct tr_announcer
202{
203    tr_ptrArray hosts; /* tr_host */
204    tr_ptrArray stops; /* struct stop_message */
205    tr_session * session;
206    struct event * upkeepTimer;
207    int slotsAvailable;
208    time_t lpdHouseKeepingAt;
209}
210tr_announcer;
211
212tr_bool
213tr_announcerHasBacklog( const struct tr_announcer * announcer )
214{
215    return announcer->slotsAvailable < 1;
216}
217
218static tr_host *
219getHost( tr_announcer * announcer, const char * url )
220{
221    char * name = getHostName( url );
222    tr_host * host;
223
224    host = tr_ptrArrayFindSorted( &announcer->hosts, name, compareHostToName );
225    if( host == NULL )
226    {
227        host = hostNew( name );
228        tr_ptrArrayInsertSorted( &announcer->hosts, host, compareHosts );
229    }
230
231    tr_free( name );
232    return host;
233}
234
235static void
236onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer );
237
238static inline time_t
239calcRescheduleWithJitter( const int minPeriod )
240{
241    const double jitterFac = 0.1;
242
243    assert( minPeriod > 0 );
244
245    return tr_time()
246        + minPeriod
247        + tr_cryptoWeakRandInt( (int) ( minPeriod * jitterFac ) + 1 );
248}
249
250void
251tr_announcerInit( tr_session * session )
252{
253    tr_announcer * a;
254
255    const time_t relaxUntil =
256        calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS / 3 );
257
258    assert( tr_isSession( session ) );
259
260    a = tr_new0( tr_announcer, 1 );
261    a->hosts = TR_PTR_ARRAY_INIT;
262    a->stops = TR_PTR_ARRAY_INIT;
263    a->session = session;
264    a->slotsAvailable = MAX_CONCURRENT_TASKS;
265    a->lpdHouseKeepingAt = relaxUntil;
266    a->upkeepTimer = tr_new0( struct event, 1 );
267    evtimer_set( a->upkeepTimer, onUpkeepTimer, a );
268    tr_timerAdd( a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
269
270    session->announcer = a;
271}
272
273static void flushCloseMessages( tr_announcer * announcer );
274
275void
276tr_announcerClose( tr_session * session )
277{
278    tr_announcer * announcer = session->announcer;
279
280    flushCloseMessages( announcer );
281
282    evtimer_del( announcer->upkeepTimer );
283    tr_free( announcer->upkeepTimer );
284    announcer->upkeepTimer = NULL;
285
286    tr_ptrArrayDestruct( &announcer->stops, NULL );
287    tr_ptrArrayDestruct( &announcer->hosts, hostFree );
288
289    session->announcer = NULL;
290    tr_free( announcer );
291}
292
293/***
294****
295***/
296
297/* a row in tr_tier's list of trackers */
298typedef struct
299{
300    tr_host * host;
301
302    char * announce;
303    char * scrape;
304
305    char * tracker_id;
306
307    int seederCount;
308    int leecherCount;
309    int downloadCount;
310    int downloaderCount;
311
312    uint32_t id;
313
314    /* sent as the "key" argument in tracker requests
315     * to verify us if our IP address changes.
316     * This is immutable for the life of the tracker object.
317     * The +1 is for '\0' */
318    unsigned char key_param[KEYLEN + 1];
319}
320tr_tracker_item;
321
322static void
323trackerItemCopyAttributes( tr_tracker_item * t, const tr_tracker_item * o )
324{
325    assert( t != o );
326    assert( t != NULL );
327    assert( o != NULL );
328
329    t->seederCount = o->seederCount;
330    t->leecherCount = o->leecherCount;
331    t->downloadCount = o->downloadCount;
332    t->downloaderCount = o->downloaderCount;
333    memcpy( t->key_param, o->key_param, sizeof( t->key_param ) );
334}
335
336static void
337generateKeyParam( unsigned char * msg, size_t msglen )
338{
339    size_t i;
340    const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789";
341    const int poolSize = 36;
342
343    tr_cryptoRandBuf( msg, msglen );
344    for( i=0; i<msglen; ++i )
345        msg[i] = pool[ msg[i] % poolSize ];
346    msg[msglen] = '\0';
347}
348
349static tr_tracker_item*
350trackerNew( tr_announcer  * announcer,
351            const char    * announce,
352            const char    * scrape,
353            uint32_t        id )
354{
355    tr_tracker_item * tracker = tr_new0( tr_tracker_item, 1  );
356    tracker->host = getHost( announcer, announce );
357    tracker->announce = tr_strdup( announce );
358    tracker->scrape = tr_strdup( scrape );
359    tracker->id = id;
360    generateKeyParam( tracker->key_param, KEYLEN );
361    tracker->seederCount = -1;
362    tracker->leecherCount = -1;
363    tracker->downloadCount = -1;
364    return tracker;
365}
366
367static void
368trackerFree( void * vtracker )
369{
370    tr_tracker_item * tracker = vtracker;
371
372    tr_free( tracker->tracker_id );
373    tr_free( tracker->announce );
374    tr_free( tracker->scrape );
375    tr_free( tracker );
376}
377
378/***
379****
380***/
381
382struct tr_torrent_tiers;
383
384/** @brief A group of trackers in a single tier, as per the multitracker spec */
385typedef struct
386{
387    /* number of up/down/corrupt bytes since the last time we sent an
388     * "event=stopped" message that was acknowledged by the tracker */
389    uint32_t byteCounts[3];
390
391    tr_ptrArray trackers; /* tr_tracker_item */
392    tr_tracker_item * currentTracker;
393    int currentTrackerIndex;
394
395    tr_torrent * tor;
396
397    time_t scrapeAt;
398    time_t lastScrapeStartTime;
399    time_t lastScrapeTime;
400    tr_bool lastScrapeSucceeded;
401    tr_bool lastScrapeTimedOut;
402
403    time_t announceAt;
404    time_t manualAnnounceAllowedAt;
405    time_t lastAnnounceStartTime;
406    time_t lastAnnounceTime;
407    tr_bool lastAnnounceSucceeded;
408    tr_bool lastAnnounceTimedOut;
409
410    tr_ptrArray announceEvents; /* const char* */
411
412    /* unique lookup key */
413    int key;
414
415    int scrapeIntervalSec;
416    int announceIntervalSec;
417    int announceMinIntervalSec;
418
419    int lastAnnouncePeerCount;
420
421    tr_bool isRunning;
422    tr_bool isAnnouncing;
423    tr_bool isScraping;
424    tr_bool wasCopied;
425
426    char lastAnnounceStr[128];
427    char lastScrapeStr[128];
428}
429tr_tier;
430
431static tr_tier *
432tierNew( tr_torrent * tor )
433{
434    tr_tier * t;
435    static int nextKey = 1;
436    const time_t now = tr_time( );
437
438    t = tr_new0( tr_tier, 1 );
439    t->key = nextKey++;
440    t->announceEvents = TR_PTR_ARRAY_INIT;
441    t->trackers = TR_PTR_ARRAY_INIT;
442    t->currentTracker = NULL;
443    t->currentTrackerIndex = -1;
444    t->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
445    t->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
446    t->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
447    t->scrapeAt = now + tr_cryptoWeakRandInt( 60*5 );
448    t->tor = tor;
449
450    return t;
451}
452
453static void
454tierFree( void * vtier )
455{
456    tr_tier * tier = vtier;
457    tr_ptrArrayDestruct( &tier->trackers, trackerFree );
458    tr_ptrArrayDestruct( &tier->announceEvents, NULL );
459    tr_free( tier );
460}
461
462static void
463tierCopyAttributes( tr_tier * t, const tr_tier * o )
464{
465    int i, n;
466    tr_tier bak;
467
468    assert( t != NULL );
469    assert( o != NULL );
470    assert( t != o );
471
472    bak = *t;
473    *t = *o;
474    t->tor = bak.tor;
475    t->trackers = bak.trackers;
476    t->announceEvents = bak.announceEvents;
477    t->currentTracker = bak.currentTracker;
478    t->currentTrackerIndex = bak.currentTrackerIndex;
479
480    tr_ptrArrayClear( &t->announceEvents );
481    for( i=0, n=tr_ptrArraySize(&o->announceEvents); i<n; ++i )
482        tr_ptrArrayAppend( &t->announceEvents, tr_ptrArrayNth((tr_ptrArray*)&o->announceEvents,i) );
483}
484
485static void
486tierIncrementTracker( tr_tier * tier )
487{
488    /* move our index to the next tracker in the tier */
489    const int i = ( tier->currentTrackerIndex + 1 )
490                        % tr_ptrArraySize( &tier->trackers );
491    tier->currentTracker = tr_ptrArrayNth( &tier->trackers, i );
492    tier->currentTrackerIndex = i;
493
494    /* reset some of the tier's fields */
495    tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
496    tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
497    tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
498    tier->isAnnouncing = FALSE;
499    tier->isScraping = FALSE;
500    tier->lastAnnounceStartTime = 0;
501    tier->lastScrapeStartTime = 0;
502}
503
504static void
505tierAddTracker( tr_announcer * announcer,
506                tr_tier      * tier,
507                const char   * announce,
508                const char   * scrape,
509                uint32_t       id )
510{
511    tr_tracker_item * tracker = trackerNew( announcer, announce, scrape, id );
512
513    tr_ptrArrayAppend( &tier->trackers, tracker );
514    dbgmsg( tier, "adding tracker %s", announce );
515
516    if( !tier->currentTracker )
517        tierIncrementTracker( tier );
518}
519
520
521/***
522****
523***/
524
525/**
526 * @brief Opaque, per-torrent data structure for tracker announce information
527 *
528 * this opaque data structure can be found in tr_torrent.tiers
529 */
530typedef struct tr_torrent_tiers
531{
532    tr_ptrArray tiers;
533    tr_tracker_callback * callback;
534    void * callbackData;
535}
536tr_torrent_tiers;
537
538static tr_torrent_tiers*
539tiersNew( void )
540{
541    tr_torrent_tiers * tiers = tr_new0( tr_torrent_tiers, 1 );
542    tiers->tiers = TR_PTR_ARRAY_INIT;
543    return tiers;
544}
545
546static void
547tiersFree( tr_torrent_tiers * tiers )
548{
549    tr_ptrArrayDestruct( &tiers->tiers, tierFree );
550    tr_free( tiers );
551}
552
553static tr_tier*
554getTier( tr_announcer * announcer, int torrentId, int tierId )
555{
556    tr_tier * tier = NULL;
557
558    if( announcer )
559    {
560        tr_torrent * tor = tr_torrentFindFromId( announcer->session, torrentId );
561
562        if( tor && tor->tiers )
563        {
564            int i;
565            tr_ptrArray * tiers = &tor->tiers->tiers;
566            const int n = tr_ptrArraySize( tiers );
567            for( i=0; !tier && i<n; ++i )
568            {
569                tr_tier * tmp = tr_ptrArrayNth( tiers, i );
570                if( tmp->key == tierId )
571                    tier = tmp;
572            }
573        }
574    }
575
576    return tier;
577}
578
579/***
580****  PUBLISH
581***/
582
583static const tr_tracker_event emptyEvent = { 0, NULL, NULL, NULL, 0, 0 };
584
585static void
586publishMessage( tr_tier * tier, const char * msg, int type )
587{
588    if( tier && tier->tor && tier->tor->tiers )
589    {
590        tr_torrent_tiers * tiers = tier->tor->tiers;
591        tr_tracker_event event = emptyEvent;
592        event.messageType = type;
593        event.text = msg;
594        event.tracker = tier->currentTracker ? tier->currentTracker->announce : NULL;
595
596        if( tiers->callback != NULL )
597            tiers->callback( tier->tor, &event, tiers->callbackData );
598    }
599}
600
601static void
602publishErrorClear( tr_tier * tier )
603{
604    publishMessage( tier, NULL, TR_TRACKER_ERROR_CLEAR );
605}
606
607static void
608publishErrorMessageAndStop( tr_tier * tier, const char * msg )
609{
610    tier->isRunning = FALSE;
611
612    publishMessage( tier, msg, TR_TRACKER_ERROR );
613}
614
615static void
616publishWarning( tr_tier * tier, const char * msg )
617{
618    publishMessage( tier, msg, TR_TRACKER_WARNING );
619}
620
621static int8_t
622getSeedProbability( int seeds, int leechers )
623{
624    if( !seeds )
625        return 0;
626
627    if( seeds>=0 && leechers>=0 )
628        return (int8_t)((100.0*seeds)/(seeds+leechers));
629
630    return -1; /* unknown */
631}
632
633static int
634publishNewPeers( tr_tier * tier, int seeds, int leechers,
635                 const void * compact, int compactLen )
636{
637    tr_tracker_event e = emptyEvent;
638
639    e.messageType = TR_TRACKER_PEERS;
640    e.seedProbability = getSeedProbability( seeds, leechers );
641    e.compact = compact;
642    e.compactLen = compactLen;
643
644    if( tier->tor->tiers->callback != NULL )
645        tier->tor->tiers->callback( tier->tor, &e, NULL );
646
647    return compactLen / 6;
648}
649
650static int
651publishNewPeersCompact( tr_tier * tier, int seeds, int leechers,
652                        const void * compact, int compactLen )
653{
654    int i;
655    const uint8_t *compactWalk;
656    uint8_t *array, *walk;
657    const int peerCount = compactLen / 6;
658    const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
659    tr_address addr;
660    tr_port port;
661
662    addr.type = TR_AF_INET;
663    memset( &addr.addr, 0, sizeof( addr.addr ) );
664    array = tr_new( uint8_t, arrayLen );
665    for ( i=0, walk=array, compactWalk=compact ; i<peerCount ; ++i )
666    {
667        memcpy( &addr.addr.addr4, compactWalk, 4 );
668        memcpy( &port, compactWalk + 4, 2 );
669
670        memcpy( walk, &addr, sizeof( addr ) );
671        memcpy( walk + sizeof( addr ), &port, 2 );
672
673        walk += sizeof( tr_address ) + 2;
674        compactWalk += 6;
675    }
676
677    publishNewPeers( tier, seeds, leechers, array, arrayLen );
678
679    tr_free( array );
680
681    return peerCount;
682}
683
684static int
685publishNewPeersCompact6( tr_tier * tier, int seeds, int leechers,
686                         const void * compact, int compactLen )
687{
688    int i;
689    const uint8_t *compactWalk;
690    uint8_t *array, *walk;
691    const int peerCount = compactLen / 18;
692    const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
693    tr_address addr;
694    tr_port port;
695
696    addr.type = TR_AF_INET6;
697    memset( &addr.addr, 0, sizeof( addr.addr ) );
698    array = tr_new( uint8_t, arrayLen );
699    for ( i = 0, walk = array, compactWalk = compact ; i < peerCount ; ++i )
700    {
701        memcpy( &addr.addr.addr6, compactWalk, 16 );
702        memcpy( &port, compactWalk + 16, 2 );
703        compactWalk += 18;
704
705        memcpy( walk, &addr, sizeof( addr ) );
706        memcpy( walk + sizeof( addr ), &port, 2 );
707        walk += sizeof( tr_address ) + 2;
708    }
709
710    publishNewPeers( tier, seeds, leechers, array, arrayLen );
711    tr_free( array );
712
713    return peerCount;
714}
715
716static char*
717createAnnounceURL( const tr_announcer     * announcer,
718                   const tr_torrent       * torrent,
719                   const tr_tier          * tier,
720                   const char             * eventName )
721{
722    const int isStopping = !strcmp( eventName, "stopped" );
723    const int numwant = isStopping ? 0 : NUMWANT;
724    const tr_tracker_item  * tracker = tier->currentTracker;
725    const char * ann = tracker->announce;
726    struct evbuffer * buf = evbuffer_new( );
727    char * ret;
728    const char * str;
729    const unsigned char * ipv6;
730
731    evbuffer_expand( buf, 2048 );
732
733    evbuffer_add_printf( buf, "%s"
734                              "%c"
735                              "info_hash=%s"
736                              "&peer_id=%s"
737                              "&port=%d"
738                              "&uploaded=%" PRIu32
739                              "&downloaded=%" PRIu32
740                              "&left=%" PRIu64
741                              "&numwant=%d"
742                              "&key=%s"
743                              "&compact=1"
744                              "&supportcrypto=1",
745                              ann,
746                              strchr( ann, '?' ) ? '&' : '?',
747                              torrent->info.hashEscaped,
748                              torrent->peer_id,
749                              (int)tr_sessionGetPublicPeerPort( announcer->session ),
750                              tier->byteCounts[TR_ANN_UP],
751                              tier->byteCounts[TR_ANN_DOWN],
752                              tr_cpLeftUntilComplete( &torrent->completion ),
753                              numwant,
754                              tracker->key_param );
755
756    if( announcer->session->encryptionMode == TR_ENCRYPTION_REQUIRED )
757        evbuffer_add_printf( buf, "&requirecrypto=1" );
758
759    if( tier->byteCounts[TR_ANN_CORRUPT] )
760        evbuffer_add_printf( buf, "&corrupt=%" PRIu32, tier->byteCounts[TR_ANN_CORRUPT] );
761
762    str = eventName;
763    if( str && *str )
764        evbuffer_add_printf( buf, "&event=%s", str );
765
766    str = tracker->tracker_id;
767    if( str && *str )
768        evbuffer_add_printf( buf, "&trackerid=%s", str );
769
770    /* There are two incompatible techniques for announcing an IPv6 address.
771       BEP-7 suggests adding an "ipv6=" parameter to the announce URL,
772       while OpenTracker requires that peers announce twice, once over IPv4
773       and once over IPv6.
774
775       To be safe, we should do both: add the "ipv6=" parameter and
776       announce twice.  At any rate, we're already computing our IPv6
777       address (for the LTEP handshake), so this comes for free. */
778
779    ipv6 = tr_globalIPv6( );
780    if( ipv6 ) {
781        char ipv6_readable[INET6_ADDRSTRLEN];
782        inet_ntop( AF_INET6, ipv6, ipv6_readable, INET6_ADDRSTRLEN );
783        evbuffer_add_printf( buf, "&ipv6=");
784        tr_http_escape( buf, ipv6_readable, -1, TRUE );
785    }
786
787    ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
788    dbgmsg( tier, "announce URL is \"%s\"", ret );
789    evbuffer_free( buf );
790    return ret;
791}
792
793
794/***
795****
796***/
797
798static tr_bool
799announceURLIsSupported( const char * announce )
800{
801    return ( announce != NULL )
802        && ( ( strstr( announce, "http://" ) == announce ) ||
803             ( strstr( announce, "https://" ) == announce ) );
804}
805
806static void
807addTorrentToTier( tr_announcer * announcer, tr_torrent_tiers * tiers, tr_torrent * tor )
808{
809    int i, n;
810    const tr_tracker_info ** infos;
811    const int trackerCount = tor->info.trackerCount;
812    const tr_tracker_info  * trackers = tor->info.trackers;
813
814    /* get the trackers that we support... */
815    infos = tr_new0( const tr_tracker_info*, trackerCount );
816    for( i=n=0; i<trackerCount; ++i )
817        if( announceURLIsSupported( trackers[i].announce ) )
818            infos[n++] = &trackers[i];
819
820    /* build our private table of tiers... */
821    if( n > 0 )
822    {
823        int tierIndex = -1;
824        tr_tier * tier = NULL;
825
826        for( i=0; i<n; ++i )
827        {
828            const tr_tracker_info * info = infos[i];
829
830            if( info->tier != tierIndex )
831                tier = NULL;
832
833            tierIndex = info->tier;
834
835            if( tier == NULL ) {
836                tier = tierNew( tor );
837                dbgmsg( tier, "adding tier" );
838                tr_ptrArrayAppend( &tiers->tiers, tier );
839            }
840
841            tierAddTracker( announcer, tier, info->announce, info->scrape, info->id );
842        }
843    }
844
845    tr_free( infos );
846}
847
848tr_torrent_tiers *
849tr_announcerAddTorrent( tr_announcer * announcer, tr_torrent * tor,
850                        tr_tracker_callback * callback, void * callbackData )
851{
852    tr_torrent_tiers * tiers;
853
854    assert( announcer != NULL );
855    assert( tr_isTorrent( tor ) );
856
857    tiers = tiersNew( );
858    tiers->callback = callback;
859    tiers->callbackData = callbackData;
860
861    addTorrentToTier( announcer, tiers, tor );
862
863    return tiers;
864}
865
866static void
867tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt );
868
869void
870tr_announcerResetTorrent( tr_announcer * announcer, tr_torrent * tor )
871{
872    tr_ptrArray oldTiers = TR_PTR_ARRAY_INIT;
873
874    /* if we had tiers already, make a backup of them */
875    if( tor->tiers != NULL )
876    {
877        oldTiers = tor->tiers->tiers;
878        tor->tiers->tiers = TR_PTR_ARRAY_INIT;
879    }
880
881    /* create the new tier/tracker structs */
882    addTorrentToTier( announcer, tor->tiers, tor );
883
884    /* if we had tiers already, merge their state into the new structs */
885    if( !tr_ptrArrayEmpty( &oldTiers ) )
886    {
887        int i, in;
888        for( i=0, in=tr_ptrArraySize(&oldTiers); i<in; ++i )
889        {
890            int j, jn;
891            const tr_tier * o = tr_ptrArrayNth( &oldTiers, i );
892
893            if( o->currentTracker == NULL )
894                continue;
895
896            for( j=0, jn=tr_ptrArraySize(&tor->tiers->tiers); j<jn; ++j )
897            {
898                int k, kn;
899                tr_tier * t = tr_ptrArrayNth(&tor->tiers->tiers,j);
900
901                for( k=0, kn=tr_ptrArraySize(&t->trackers); k<kn; ++k )
902                {
903                    tr_tracker_item * item = tr_ptrArrayNth(&t->trackers,k);
904                    if( strcmp( o->currentTracker->announce, item->announce ) )
905                        continue;
906                    tierCopyAttributes( t, o );
907                    t->currentTracker = item;
908                    t->currentTrackerIndex = k;
909                    t->wasCopied = TRUE;
910                    trackerItemCopyAttributes( item, o->currentTracker );
911                    dbgmsg( t, "attributes copied to tier %d, tracker %d"
912                                               "from tier %d, tracker %d",
913                            i, o->currentTrackerIndex, j, k );
914
915                }
916            }
917        }
918    }
919
920    /* kickstart any tiers that didn't get started */
921    if( tor->isRunning )
922    {
923        int i, n;
924        const time_t now = tr_time( );
925        tr_tier ** tiers = (tr_tier**) tr_ptrArrayPeek( &tor->tiers->tiers, &n );
926        for( i=0; i<n; ++i ) {
927            tr_tier * tier = tiers[i];
928            if( !tier->wasCopied )
929                tierAddAnnounce( tier, STARTED, now );
930        }
931    }
932
933    /* cleanup */
934    tr_ptrArrayDestruct( &oldTiers, tierFree );
935}
936
937static tr_bool
938tierCanManualAnnounce( const tr_tier * tier )
939{
940    return tier->isRunning
941        && tier->manualAnnounceAllowedAt <= tr_time( );
942}
943
944tr_bool
945tr_announcerCanManualAnnounce( const tr_torrent * tor )
946{
947    int i;
948    int n;
949    const tr_tier ** tiers;
950
951    assert( tr_isTorrent( tor ) );
952    assert( tor->tiers != NULL );
953
954    n = tr_ptrArraySize( &tor->tiers->tiers );
955    tiers = (const tr_tier**) tr_ptrArrayBase( &tor->tiers->tiers );
956    for( i=0; i<n; ++i )
957        if( tierCanManualAnnounce( tiers[i] ) )
958            return TRUE;
959
960    return FALSE;
961}
962
963time_t
964tr_announcerNextManualAnnounce( const tr_torrent * tor )
965{
966    int i;
967    int n;
968    const tr_torrent_tiers * tiers;
969    time_t ret = ~(time_t)0;
970
971    assert( tr_isTorrent( tor  ) );
972
973    tiers = tor->tiers;
974    n = tr_ptrArraySize( &tiers->tiers );
975    for( i=0; i<n; ++i ) {
976        tr_tier * tier = tr_ptrArrayNth( (tr_ptrArray*)&tiers->tiers, i );
977        if( tier->isRunning )
978            ret = MIN( ret, tier->manualAnnounceAllowedAt );
979    }
980
981    return ret;
982}
983
984static void
985tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt )
986{
987    assert( tier != NULL );
988    assert( announceEvent != NULL );
989
990    tr_ptrArrayAppend( &tier->announceEvents, (void*)announceEvent );
991    tier->announceAt = announceAt;
992
993    dbgmsg( tier, "appended event \"%s\"; announcing in %d seconds\n", announceEvent, (int)difftime(announceAt,time(NULL)) );
994}
995
996static void
997torrentAddAnnounce( tr_torrent * tor, const char * announceEvent, time_t announceAt )
998{
999    int i;
1000    int n;
1001    tr_torrent_tiers * tiers;
1002
1003    assert( tr_isTorrent( tor ) );
1004
1005    tiers = tor->tiers;
1006    n = tr_ptrArraySize( &tiers->tiers );
1007    for( i=0; i<n; ++i )
1008        tierAddAnnounce( tr_ptrArrayNth( &tiers->tiers, i ), announceEvent, announceAt );
1009}
1010
1011void
1012tr_announcerTorrentStarted( tr_torrent * tor )
1013{
1014    torrentAddAnnounce( tor, STARTED, tr_time( ) );
1015}
1016void
1017tr_announcerManualAnnounce( tr_torrent * tor )
1018{
1019    torrentAddAnnounce( tor, "", tr_time( ) );
1020}
1021void
1022tr_announcerTorrentStopped( tr_torrent * tor )
1023{
1024    torrentAddAnnounce( tor, "stopped", tr_time( ) );
1025}
1026void
1027tr_announcerTorrentCompleted( tr_torrent * tor )
1028{
1029    torrentAddAnnounce( tor, "completed", tr_time( ) );
1030}
1031void
1032tr_announcerChangeMyPort( tr_torrent * tor )
1033{
1034    tr_announcerTorrentStarted( tor );
1035}
1036
1037/***
1038****
1039***/
1040
1041void
1042tr_announcerAddBytes( tr_torrent * tor, int type, uint32_t byteCount )
1043{
1044    int i, n;
1045    tr_torrent_tiers * tiers;
1046
1047    assert( tr_isTorrent( tor ) );
1048    assert( type==TR_ANN_UP || type==TR_ANN_DOWN || type==TR_ANN_CORRUPT );
1049
1050    tiers = tor->tiers;
1051    n = tr_ptrArraySize( &tiers->tiers );
1052    for( i=0; i<n; ++i )
1053    {
1054        tr_tier * tier = tr_ptrArrayNth( &tiers->tiers, i );
1055        tier->byteCounts[ type ] += byteCount;
1056    }
1057}
1058
1059/***
1060****
1061***/
1062
1063void
1064tr_announcerRemoveTorrent( tr_announcer * announcer, tr_torrent * tor )
1065{
1066    assert( tr_isTorrent( tor ) );
1067
1068    if( tor->tiers )
1069    {
1070        int i;
1071        const int n = tr_ptrArraySize( &tor->tiers->tiers );
1072        for( i=0; i<n; ++i )
1073        {
1074            tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
1075
1076            if( tier->isRunning )
1077            {
1078                struct stop_message * s = tr_new0( struct stop_message, 1 );
1079                s->up = tier->byteCounts[TR_ANN_UP];
1080                s->down = tier->byteCounts[TR_ANN_DOWN];
1081                s->url = createAnnounceURL( announcer, tor, tier, "stopped" );
1082                s->host = tier->currentTracker->host;
1083                tr_ptrArrayInsertSorted( &announcer->stops, s, compareStops );
1084            }
1085        }
1086
1087        tiersFree( tor->tiers );
1088        tor->tiers = NULL;
1089    }
1090}
1091
1092/* return true if (1) we've tried it recently AND (2) it didn't respond... */
1093static tr_bool
1094hostIsNotResponding( const tr_host * host, const time_t now )
1095{
1096    tr_bool b = ( host->lastRequestTime )
1097             && ( host->lastRequestTime >= ( now - SLOW_HOST_PENALTY_SECS ) )
1098             && ( host->lastResponseInterval > MAX_TRACKER_RESPONSE_TIME_SECS );
1099    return b;
1100}
1101
1102static tr_bool
1103tierIsNotResponding( const tr_tier * tier, const time_t now )
1104{
1105    return !tier->currentTracker
1106        || hostIsNotResponding( tier->currentTracker->host, now );
1107}
1108
1109static int
1110getRetryInterval( const tr_host * host )
1111{
1112    int interval;
1113    const int jitter = tr_cryptoWeakRandInt( 120 );
1114    const time_t timeSinceLastSuccess = tr_time() - host->lastSuccessfulRequest;
1115         if( timeSinceLastSuccess < 15*60 ) interval = 0;
1116    else if( timeSinceLastSuccess < 30*60 ) interval = 60*4;
1117    else if( timeSinceLastSuccess < 45*60 ) interval = 60*8;
1118    else if( timeSinceLastSuccess < 60*60 ) interval = 60*16;
1119    else                                    interval = 60*32;
1120    return interval + jitter;
1121}
1122
1123static int
1124compareTiers( const void * va, const void * vb )
1125{
1126    int ret = 0;
1127    tr_bool af, bf;
1128    const tr_tier * a = *(const tr_tier**)va;
1129    const tr_tier * b = *(const tr_tier**)vb;
1130
1131    /* working domains come before non-working */
1132    if( !ret ) {
1133        const time_t now = tr_time( );
1134        af = tierIsNotResponding( a, now );
1135        bf = tierIsNotResponding( b, now );
1136        if( af != bf )
1137            ret = !af ? -1 : 1;
1138    }
1139
1140    /* stops come before starts */
1141    if( !ret ) {
1142        af = a->tor->isRunning;
1143        bf = b->tor->isRunning;
1144        if( af != bf )
1145            ret = af ? 1 : -1;
1146    }
1147
1148    /* upload comes before download */
1149    if( !ret )
1150        ret = compareTransfer( a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN],
1151                               b->byteCounts[TR_ANN_UP], b->byteCounts[TR_ANN_DOWN] );
1152
1153    /* incomplete comes before complete */
1154    if( !ret ) {
1155        af = a->tor->completeness == TR_LEECH;
1156        bf = b->tor->completeness == TR_LEECH;
1157        if( af != bf )
1158            return af ? -1 : 1;
1159    }
1160
1161    /* private before public */
1162    if( !ret ) {
1163        af = tr_torrentIsPrivate( a->tor );
1164        bf = tr_torrentIsPrivate( b->tor );
1165        if( af != bf )
1166            ret = af ? -1 : 1;
1167    }
1168
1169    return ret;
1170}
1171
1172static uint8_t *
1173parseOldPeers( tr_benc * bePeers, size_t * byteCount )
1174{
1175    int       i;
1176    uint8_t * array, *walk;
1177    const int peerCount = bePeers->val.l.count;
1178
1179    assert( tr_bencIsList( bePeers ) );
1180
1181    array = tr_new( uint8_t, peerCount * ( sizeof( tr_address ) + 2 ) );
1182
1183    for( i = 0, walk = array; i < peerCount; ++i )
1184    {
1185        const char * s;
1186        int64_t      itmp;
1187        tr_address   addr;
1188        tr_port      port;
1189        tr_benc    * peer = &bePeers->val.l.vals[i];
1190
1191        if( tr_bencDictFindStr( peer, "ip", &s ) )
1192            if( tr_pton( s, &addr ) == NULL )
1193                continue;
1194
1195        if( !tr_bencDictFindInt( peer, "port", &itmp )
1196                || itmp < 0
1197                || itmp > USHRT_MAX )
1198            continue;
1199
1200        memcpy( walk, &addr, sizeof( tr_address ) );
1201        port = htons( (uint16_t)itmp );
1202        memcpy( walk + sizeof( tr_address ), &port, 2 );
1203        walk += sizeof( tr_address ) + 2;
1204    }
1205
1206    *byteCount = peerCount * sizeof( tr_address ) + 2;
1207    return array;
1208}
1209
1210static tr_bool
1211parseAnnounceResponse( tr_tier     * tier,
1212                       const char  * response,
1213                       size_t        responseLen,
1214                       tr_bool     * gotScrape )
1215{
1216    tr_benc benc;
1217    tr_bool success = FALSE;
1218    int scrapeFields = 0;
1219    const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
1220
1221    if( getenv( "TR_CURL_VERBOSE" ) != NULL )
1222    {
1223        char * str = tr_bencToStr( &benc, TR_FMT_JSON, NULL );
1224        fprintf( stderr, "Announce response:\n< %s\n", str );
1225        tr_free( str );
1226    }
1227
1228    dbgmsg( tier, "response len: %d, isBenc: %d", (int)responseLen, (int)bencLoaded );
1229    publishErrorClear( tier );
1230    if( bencLoaded && tr_bencIsDict( &benc ) )
1231    {
1232        int peerCount = 0;
1233        size_t rawlen;
1234        int64_t i;
1235        tr_benc * tmp;
1236        const char * str;
1237        const uint8_t * raw;
1238        tr_bool gotPeers = FALSE;
1239
1240        success = TRUE;
1241
1242        if( tr_bencDictFindStr( &benc, "failure reason", &str ) )
1243        {
1244            tr_strlcpy( tier->lastAnnounceStr, str,
1245                        sizeof( tier->lastAnnounceStr ) );
1246            dbgmsg( tier, "tracker gave \"%s\"", str );
1247            publishMessage( tier, str, TR_TRACKER_ERROR );
1248            success = FALSE;
1249        }
1250
1251        if( tr_bencDictFindStr( &benc, "warning message", &str ) )
1252        {
1253            tr_strlcpy( tier->lastAnnounceStr, str,
1254                        sizeof( tier->lastAnnounceStr ) );
1255            dbgmsg( tier, "tracker gave \"%s\"", str );
1256            publishWarning( tier, str );
1257        }
1258
1259        if( tr_bencDictFindInt( &benc, "interval", &i ) )
1260        {
1261            dbgmsg( tier, "setting interval to %d", (int)i );
1262            tier->announceIntervalSec = i;
1263        }
1264
1265        if( tr_bencDictFindInt( &benc, "min interval", &i ) )
1266        {
1267            dbgmsg( tier, "setting min interval to %d", (int)i );
1268            tier->announceMinIntervalSec = i;
1269        }
1270
1271        if( tr_bencDictFindStr( &benc, "tracker id", &str ) )
1272        {
1273            tier->currentTracker->tracker_id = tr_strdup( str );
1274        }
1275
1276        if( !tr_bencDictFindInt( &benc, "complete", &i ) )
1277            tier->currentTracker->seederCount = 0;
1278        else {
1279            ++scrapeFields;
1280            tier->currentTracker->seederCount = i;
1281        }
1282
1283        if( !tr_bencDictFindInt( &benc, "incomplete", &i ) )
1284            tier->currentTracker->leecherCount = 0;
1285        else {
1286            ++scrapeFields;
1287            tier->currentTracker->leecherCount = i;
1288        }
1289
1290        if( tr_bencDictFindInt( &benc, "downloaded", &i ) )
1291        {
1292            ++scrapeFields;
1293            tier->currentTracker->downloadCount = i;
1294        }
1295
1296        if( tr_bencDictFindRaw( &benc, "peers", &raw, &rawlen ) )
1297        {
1298            /* "compact" extension */
1299            const int seeders = tier->currentTracker->seederCount;
1300            const int leechers = tier->currentTracker->leecherCount;
1301            peerCount += publishNewPeersCompact( tier, seeders, leechers, raw, rawlen );
1302            gotPeers = TRUE;
1303        }
1304        else if( tr_bencDictFindList( &benc, "peers", &tmp ) )
1305        {
1306            /* original version of peers */
1307            const int seeders = tier->currentTracker->seederCount;
1308            const int leechers = tier->currentTracker->leecherCount;
1309            size_t byteCount = 0;
1310            uint8_t * array = parseOldPeers( tmp, &byteCount );
1311            peerCount += publishNewPeers( tier, seeders, leechers, array, byteCount );
1312            gotPeers = TRUE;
1313            tr_free( array );
1314        }
1315
1316        if( tr_bencDictFindRaw( &benc, "peers6", &raw, &rawlen ) )
1317        {
1318            /* "compact" extension */
1319            const int seeders = tier->currentTracker->seederCount;
1320            const int leechers = tier->currentTracker->leecherCount;
1321            peerCount += publishNewPeersCompact6( tier, seeders, leechers, raw, rawlen );
1322            gotPeers = TRUE;
1323        }
1324
1325        if( tier->lastAnnounceStr[0] == '\0' )
1326            tr_strlcpy( tier->lastAnnounceStr, _( "Success" ),
1327                        sizeof( tier->lastAnnounceStr ) );
1328
1329        if( gotPeers )
1330            tier->lastAnnouncePeerCount = peerCount;
1331    }
1332
1333    if( bencLoaded )
1334        tr_bencFree( &benc );
1335
1336    *gotScrape = scrapeFields >= 2;
1337
1338    return success;
1339}
1340
1341struct announce_data
1342{
1343    int torrentId;
1344    int tierId;
1345    time_t timeSent;
1346    const char * event;
1347
1348    /** If the request succeeds, the value for tier's "isRunning" flag */
1349    tr_bool isRunningOnSuccess;
1350};
1351
1352static void
1353onAnnounceDone( tr_session   * session,
1354                long           responseCode,
1355                const void   * response,
1356                size_t         responseLen,
1357                void         * vdata )
1358{
1359    tr_announcer * announcer = session->announcer;
1360    struct announce_data * data = vdata;
1361    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1362    tr_bool gotScrape = FALSE;
1363    tr_bool success = FALSE;
1364    const time_t now = time ( NULL );
1365    const char * announceEvent = data->event;
1366    const tr_bool isStopped = !strcmp( announceEvent, "stopped" );
1367
1368    if( announcer && tier )
1369    {
1370        if( tier->currentTracker->host )
1371        {
1372            tr_host * host = tier->currentTracker->host;
1373            host->lastRequestTime = data->timeSent;
1374            host->lastResponseInterval = now - data->timeSent;
1375        }
1376
1377        tier->lastAnnounceTime = now;
1378
1379        if( responseCode == HTTP_OK )
1380        {
1381            success = parseAnnounceResponse( tier, response, responseLen, &gotScrape );
1382            dbgmsg( tier, "success is %d", success );
1383
1384            if( isStopped )
1385            {
1386                /* now that we've successfully stopped the torrent,
1387                 * we can reset the up/down/corrupt count we've kept
1388                 * for this tracker */
1389                tier->byteCounts[ TR_ANN_UP ] = 0;
1390                tier->byteCounts[ TR_ANN_DOWN ] = 0;
1391                tier->byteCounts[ TR_ANN_CORRUPT ] = 0;
1392            }
1393        }
1394        else if( responseCode )
1395        {
1396            /* %1$ld - http status code, such as 404
1397             * %2$s - human-readable explanation of the http status code */
1398            char * buf = tr_strdup_printf(
1399                _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1400                responseCode,
1401                tr_webGetResponseStr( responseCode ) );
1402
1403            tr_strlcpy( tier->lastAnnounceStr, buf,
1404                        sizeof( tier->lastAnnounceStr ) );
1405
1406            /* if the response is serious, *and* if the response may require
1407             * human intervention, then notify the user... otherwise just log it */
1408            if( responseCode >= 400 )
1409                if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1410                    publishWarning( tier, buf );
1411            tr_torinf( tier->tor, "%s", buf );
1412            dbgmsg( tier, "%s", buf );
1413
1414            tr_free( buf );
1415        }
1416        else
1417        {
1418            tr_strlcpy( tier->lastAnnounceStr,
1419                        _( "tracker did not respond" ),
1420                        sizeof( tier->lastAnnounceStr ) );
1421            dbgmsg( tier, "%s", tier->lastAnnounceStr );
1422        }
1423    }
1424
1425    if( tier )
1426    {
1427        tier->isAnnouncing = FALSE;
1428
1429        if( responseCode == 0 )
1430        {
1431            const int interval = getRetryInterval( tier->currentTracker->host );
1432            dbgmsg( tier, "No response from tracker... retrying in %d seconds.", interval );
1433            tier->manualAnnounceAllowedAt = ~(time_t)0;
1434            tierAddAnnounce( tier, announceEvent, now + interval );
1435        }
1436        else if( 200 <= responseCode && responseCode <= 299 )
1437        {
1438            const int interval = tier->announceIntervalSec;
1439            dbgmsg( tier, "request succeeded. reannouncing in %d seconds", interval );
1440
1441            if( gotScrape )
1442            {
1443                tier->lastScrapeTime = now;
1444                tier->lastScrapeSucceeded = 1;
1445                tier->scrapeAt = now + tier->scrapeIntervalSec;
1446            }
1447
1448            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1449
1450            /* if we're running and the queue is empty, add the next update */
1451            if( !isStopped && !tr_ptrArraySize( &tier->announceEvents ) )
1452            {
1453                tierAddAnnounce( tier, "", now + interval );
1454            }
1455        }
1456        else if( 300 <= responseCode && responseCode <= 399 )
1457        {
1458            /* how did this get here?  libcurl handles this */
1459            const int interval = 5;
1460            dbgmsg( tier, "got a redirect. retrying in %d seconds", interval );
1461            tierAddAnnounce( tier, announceEvent, now + interval );
1462            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1463        }
1464        else if( ( responseCode == 404 ) || ( 500 <= responseCode && responseCode <= 599 ) )
1465        {
1466            /* 404: The requested resource could not be found but may be
1467             * available again in the future.  Subsequent requests by
1468             * the client are permissible. */
1469
1470            /* 5xx: indicate cases in which the server is aware that it
1471             * has erred or is incapable of performing the request.
1472             * So we pause a bit and try again. */
1473
1474            const int interval = getRetryInterval( tier->currentTracker->host );
1475            tier->manualAnnounceAllowedAt = ~(time_t)0;
1476            tierAddAnnounce( tier, announceEvent, now + interval );
1477        }
1478        else if( 400 <= responseCode && responseCode <= 499 )
1479        {
1480            /* The request could not be understood by the server due to
1481             * malformed syntax. The client SHOULD NOT repeat the
1482             * request without modifications. */
1483            if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1484                publishErrorMessageAndStop( tier, _( "Tracker returned a 4xx message" ) );
1485            tier->announceAt = 0;
1486            tier->manualAnnounceAllowedAt = ~(time_t)0;
1487        }
1488        else
1489        {
1490            /* WTF did we get?? */
1491            const int interval = 120;
1492            dbgmsg( tier, "Invalid response from tracker... retrying in two minutes." );
1493            tier->manualAnnounceAllowedAt = ~(time_t)0;
1494            tierAddAnnounce( tier, announceEvent, now + interval );
1495        }
1496
1497        tier->lastAnnounceSucceeded = success;
1498        tier->lastAnnounceTimedOut = responseCode == 0;
1499
1500        if( success )
1501        {
1502            tier->isRunning = data->isRunningOnSuccess;
1503
1504            if( tier->currentTracker->host )
1505                tier->currentTracker->host->lastSuccessfulRequest = now;
1506        }
1507        else if( responseCode != HTTP_OK )
1508        {
1509            tierIncrementTracker( tier );
1510
1511            tr_ptrArrayInsert( &tier->announceEvents, (void*)announceEvent, 0 );
1512        }
1513    }
1514
1515    if( announcer != NULL )
1516    {
1517        ++announcer->slotsAvailable;
1518    }
1519
1520    tr_free( data );
1521}
1522
1523static const char*
1524getNextAnnounceEvent( tr_tier * tier )
1525{
1526    int i, n;
1527    int pos = -1;
1528    tr_ptrArray tmp;
1529    const char ** events;
1530    const char * str = NULL;
1531
1532    assert( tier != NULL );
1533    assert( tr_isTorrent( tier->tor ) );
1534
1535    events = (const char**) tr_ptrArrayPeek( &tier->announceEvents, &n );
1536
1537    /* special case #1: if "stopped" is in the queue,
1538     * ignore everything before it except "completed" */
1539    if( pos == -1 ) {
1540        tr_bool completed = FALSE;
1541        for( i = 0; i < n; ++i ) {
1542            if( !strcmp( events[i], "completed" ) )
1543                completed = TRUE;
1544            if( !strcmp( events[i], "stopped" ) )
1545                break;
1546        }
1547        if( !completed && ( i <  n ) )
1548            pos = i;
1549    }
1550
1551    /* special case #2: don't use empty strings if something follows them */
1552    if( pos == -1 ) {
1553        for( i = 0; i < n; ++i )
1554            if( *events[i] )
1555                break;
1556        if( i < n )
1557            pos = i;
1558    }
1559
1560    /* default: use the next in the queue */
1561    if( ( pos == -1 ) && ( n > 0 ) )
1562        pos = 0;
1563
1564    /* special case #3: if there are duplicate requests in a row, skip to the last one */
1565    if( pos >= 0 ) {
1566        for( i=pos+1; i<n; ++i )
1567            if( strcmp( events[pos], events[i] ) )
1568                break;
1569        pos = i - 1;
1570    }
1571
1572    /* special case #4: BEP 21: "In order to tell the tracker that a peer is a
1573     * partial seed, it MUST send an event=paused parameter in every
1574     * announce while it is a partial seed." */
1575    str = pos>=0 ? events[pos] : NULL;
1576    if( tr_cpGetStatus( &tier->tor->completion ) == TR_PARTIAL_SEED )
1577        if( !str || strcmp( str, "stopped" ) )
1578            str = "paused";
1579
1580#if 0
1581for( i=0; i<n; ++i ) fprintf( stderr, "(%d)\"%s\" ", i, events[i] );
1582fprintf( stderr, "\n" );
1583fprintf( stderr, "using (%d)\"%s\"\n", pos, events[pos] );
1584if( strcmp( events[pos], str ) ) fprintf( stderr, "...but really using [%s]\n", str );
1585#endif
1586
1587    /* announceEvents array upkeep */
1588    tmp = TR_PTR_ARRAY_INIT;
1589    for( i=pos+1; i<n; ++i )
1590        tr_ptrArrayAppend( &tmp, (void*)events[i] );
1591    tr_ptrArrayDestruct( &tier->announceEvents, NULL );
1592    tier->announceEvents = tmp;
1593
1594    return str;
1595}
1596
1597static void
1598tierAnnounce( tr_announcer * announcer, tr_tier * tier )
1599{
1600    const char * announceEvent = getNextAnnounceEvent( tier );
1601
1602    assert( !tier->isAnnouncing );
1603
1604    if( announceEvent != NULL )
1605    {
1606        char * url;
1607        struct announce_data * data;
1608        const tr_torrent * tor = tier->tor;
1609        const time_t now = tr_time( );
1610
1611        data = tr_new0( struct announce_data, 1 );
1612        data->torrentId = tr_torrentId( tor );
1613        data->tierId = tier->key;
1614        data->isRunningOnSuccess = tor->isRunning;
1615        data->timeSent = now;
1616        data->event = announceEvent;
1617        url = createAnnounceURL( announcer, tor, tier, data->event );
1618
1619        tier->isAnnouncing = TRUE;
1620        tier->lastAnnounceStartTime = now;
1621        --announcer->slotsAvailable;
1622        tr_webRun( announcer->session, url, NULL, onAnnounceDone, data );
1623
1624        tr_free( url );
1625    }
1626}
1627
1628static tr_bool
1629parseScrapeResponse( tr_tier     * tier,
1630                     const char  * response,
1631                     size_t        responseLen,
1632                     char        * result,
1633                     size_t        resultlen )
1634{
1635    tr_bool success = FALSE;
1636    tr_benc benc, *files;
1637    const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
1638    if( bencLoaded && tr_bencDictFindDict( &benc, "files", &files ) )
1639    {
1640        const char * key;
1641        tr_benc * val;
1642        int i = 0;
1643        while( tr_bencDictChild( files, i++, &key, &val ))
1644        {
1645            int64_t intVal;
1646            tr_benc * flags;
1647
1648            if( memcmp( tier->tor->info.hash, key, SHA_DIGEST_LENGTH ) )
1649                continue;
1650
1651            success = TRUE;
1652            publishErrorClear( tier );
1653
1654            if( ( tr_bencDictFindInt( val, "complete", &intVal ) ) )
1655                tier->currentTracker->seederCount = intVal;
1656
1657            if( ( tr_bencDictFindInt( val, "incomplete", &intVal ) ) )
1658                tier->currentTracker->leecherCount = intVal;
1659
1660            if( ( tr_bencDictFindInt( val, "downloaded", &intVal ) ) )
1661                tier->currentTracker->downloadCount = intVal;
1662
1663            if( ( tr_bencDictFindInt( val, "downloaders", &intVal ) ) )
1664                tier->currentTracker->downloaderCount = intVal;
1665
1666            if( tr_bencDictFindDict( val, "flags", &flags ) )
1667                if( ( tr_bencDictFindInt( flags, "min_request_interval", &intVal ) ) )
1668                    tier->scrapeIntervalSec = MAX( DEFAULT_SCRAPE_INTERVAL_SEC, (int)intVal );
1669
1670            tr_tordbg( tier->tor,
1671                       "Scrape successful. Rescraping in %d seconds.",
1672                       tier->scrapeIntervalSec );
1673        }
1674    }
1675
1676    if( bencLoaded )
1677        tr_bencFree( &benc );
1678
1679    if( success )
1680        tr_strlcpy( result, _( "Success" ), resultlen );
1681    else
1682        tr_strlcpy( result, _( "Error parsing response" ), resultlen );
1683
1684    return success;
1685}
1686
1687static void
1688onScrapeDone( tr_session   * session,
1689              long           responseCode,
1690              const void   * response,
1691              size_t         responseLen,
1692              void         * vdata )
1693{
1694    tr_bool success = FALSE;
1695    tr_announcer * announcer = session->announcer;
1696    struct announce_data * data = vdata;
1697    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1698    const time_t now = tr_time( );
1699
1700    if( announcer )
1701        ++announcer->slotsAvailable;
1702
1703    if( announcer && tier )
1704    {
1705        tier->isScraping = FALSE;
1706        tier->lastScrapeTime = now;
1707
1708        if( tier->currentTracker->host )
1709        {
1710            tr_host * host = tier->currentTracker->host;
1711            host->lastRequestTime = data->timeSent;
1712            host->lastResponseInterval = now - data->timeSent;
1713        }
1714
1715        if( 200 <= responseCode && responseCode <= 299 )
1716        {
1717            const int interval = tier->scrapeIntervalSec;
1718            tier->scrapeAt = now + interval;
1719
1720            if( responseCode == HTTP_OK )
1721                success = parseScrapeResponse( tier, response, responseLen,
1722                                               tier->lastScrapeStr, sizeof( tier->lastScrapeStr ) );
1723            else
1724                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1725                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1726                             responseCode, tr_webGetResponseStr( responseCode ) );
1727            tr_tordbg( tier->tor, "%s", tier->lastScrapeStr );
1728        }
1729        else if( 300 <= responseCode && responseCode <= 399 )
1730        {
1731            /* this shouldn't happen; libcurl should handle this */
1732            const int interval = 5;
1733            tier->scrapeAt = now + interval;
1734            tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1735                         "Got a redirect. Retrying in %d seconds", interval );
1736            tr_tordbg( tier->tor, "%s", tier->lastScrapeStr );
1737        }
1738        else
1739        {
1740            const int interval = getRetryInterval( tier->currentTracker->host );
1741
1742            /* Don't retry on a 4xx.
1743             * Retry at growing intervals on a 5xx */
1744            if( 400 <= responseCode && responseCode <= 499 )
1745                tier->scrapeAt = 0;
1746            else
1747                tier->scrapeAt = now + interval;
1748
1749            /* %1$ld - http status code, such as 404
1750             * %2$s - human-readable explanation of the http status code */
1751            if( !responseCode )
1752                tr_strlcpy( tier->lastScrapeStr, _( "tracker did not respond" ),
1753                            sizeof( tier->lastScrapeStr ) );
1754            else
1755                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1756                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1757                             responseCode, tr_webGetResponseStr( responseCode ) );
1758        }
1759
1760        tier->lastScrapeSucceeded = success;
1761        tier->lastScrapeTimedOut = responseCode == 0;
1762
1763        if( success && tier->currentTracker->host )
1764            tier->currentTracker->host->lastSuccessfulRequest = now;
1765    }
1766
1767    tr_free( data );
1768}
1769
1770static void
1771tierScrape( tr_announcer * announcer, tr_tier * tier )
1772{
1773    char * url;
1774    const char * scrape;
1775    struct announce_data * data;
1776    const time_t now = tr_time( );
1777
1778    assert( tier );
1779    assert( !tier->isScraping );
1780    assert( tier->currentTracker != NULL );
1781    assert( tr_isTorrent( tier->tor ) );
1782
1783    data = tr_new0( struct announce_data, 1 );
1784    data->torrentId = tr_torrentId( tier->tor );
1785    data->tierId = tier->key;
1786
1787    scrape = tier->currentTracker->scrape;
1788
1789    url = tr_strdup_printf( "%s%cinfo_hash=%s",
1790                            scrape,
1791                            strchr( scrape, '?' ) ? '&' : '?',
1792                            tier->tor->info.hashEscaped );
1793
1794    tier->isScraping = TRUE;
1795    tier->lastScrapeStartTime = now;
1796    --announcer->slotsAvailable;
1797    dbgmsg( tier, "scraping \"%s\"", url );
1798    tr_webRun( announcer->session, url, NULL, onScrapeDone, data );
1799
1800    tr_free( url );
1801}
1802
1803static void
1804flushCloseMessages( tr_announcer * announcer )
1805{
1806    int i;
1807    const int n = tr_ptrArraySize( &announcer->stops );
1808
1809    for( i=0; i<n; ++i )
1810    {
1811        struct stop_message * stop = tr_ptrArrayNth( &announcer->stops, i );
1812        tr_webRun( announcer->session, stop->url, NULL, NULL, NULL );
1813        stopFree( stop );
1814    }
1815
1816    tr_ptrArrayClear( &announcer->stops );
1817}
1818
1819static tr_bool
1820tierNeedsToAnnounce( const tr_tier * tier, const time_t now )
1821{
1822    return !tier->isAnnouncing
1823        && !tier->isScraping
1824        && ( tier->announceAt != 0 )
1825        && ( tier->announceAt <= now )
1826        && ( tr_ptrArraySize( &tier->announceEvents ) != 0 );
1827}
1828
1829static tr_bool
1830tierNeedsToScrape( const tr_tier * tier, const time_t now )
1831{
1832    return ( !tier->isScraping )
1833        && ( tier->scrapeAt != 0 )
1834        && ( tier->scrapeAt <= now )
1835        && ( tier->currentTracker != NULL )
1836        && ( tier->currentTracker->scrape != NULL );
1837}
1838
1839static void
1840announceMore( tr_announcer * announcer )
1841{
1842    tr_torrent * tor = NULL;
1843    const time_t now = tr_time( );
1844
1845    if( announcer->slotsAvailable > 0 )
1846    {
1847        int i;
1848        int n;
1849        tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
1850        tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
1851
1852        /* build a list of tiers that need to be announced */
1853        while(( tor = tr_torrentNext( announcer->session, tor ))) {
1854            if( tor->tiers ) {
1855                n = tr_ptrArraySize( &tor->tiers->tiers );
1856                for( i=0; i<n; ++i ) {
1857                    tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
1858                    if( tierNeedsToAnnounce( tier, now ) )
1859                        tr_ptrArrayAppend( &announceMe, tier );
1860                    else if( tierNeedsToScrape( tier, now ) )
1861                        tr_ptrArrayAppend( &scrapeMe, tier );
1862                }
1863            }
1864        }
1865
1866        /* if there are more tiers than slots available, prioritize */
1867        n = tr_ptrArraySize( &announceMe );
1868        if( n > announcer->slotsAvailable )
1869            qsort( tr_ptrArrayBase( &announceMe ), n, sizeof( tr_tier * ), compareTiers );
1870
1871        /* announce some */
1872        n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
1873        for( i=0; i<n; ++i ) {
1874            tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
1875            dbgmsg( tier, "announcing tier %d of %d", i, n );
1876            tierAnnounce( announcer, tier );
1877        }
1878
1879        /* scrape some */
1880        n = MIN( tr_ptrArraySize( &scrapeMe ), announcer->slotsAvailable );
1881        for( i=0; i<n; ++i ) {
1882            tr_tier * tier = tr_ptrArrayNth( &scrapeMe, i );
1883            dbgmsg( tier, "scraping tier %d of %d", (i+1), n );
1884            tierScrape( announcer, tier );
1885        }
1886
1887#if 0
1888char timebuf[64];
1889tr_getLogTimeStr( timebuf, 64 );
1890fprintf( stderr, "[%s] announce.c has %d requests ready to send (announce: %d, scrape: %d)\n", timebuf, (int)(tr_ptrArraySize(&announceMe)+tr_ptrArraySize(&scrapeMe)), (int)tr_ptrArraySize(&announceMe), (int)tr_ptrArraySize(&scrapeMe) );
1891#endif
1892
1893        /* cleanup */
1894        tr_ptrArrayDestruct( &scrapeMe, NULL );
1895        tr_ptrArrayDestruct( &announceMe, NULL );
1896    }
1897
1898    tor = NULL;
1899    while(( tor = tr_torrentNext( announcer->session, tor ))) {
1900        if( tor->dhtAnnounceAt <= now ) {
1901            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1902                int rc;
1903                rc = tr_dhtAnnounce(tor, AF_INET, 1);
1904                if(rc == 0)
1905                    /* The DHT is not ready yet.  Try again soon. */
1906                    tor->dhtAnnounceAt = now + 5 + tr_cryptoWeakRandInt( 5 );
1907                else
1908                    /* We should announce at least once every 30 minutes. */
1909                    tor->dhtAnnounceAt =
1910                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1911            }
1912        }
1913
1914        if( tor->dhtAnnounce6At <= now ) {
1915            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1916                int rc;
1917                rc = tr_dhtAnnounce(tor, AF_INET6, 1);
1918                if(rc == 0)
1919                    tor->dhtAnnounce6At = now + 5 + tr_cryptoWeakRandInt( 5 );
1920                else
1921                    tor->dhtAnnounce6At =
1922                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1923            }
1924        }
1925    }
1926
1927    /* Local Peer Discovery */
1928    if( announcer->lpdHouseKeepingAt <= now )
1929    {
1930        tr_lpdAnnounceMore( now, LPD_HOUSEKEEPING_INTERVAL_SECS );
1931
1932        /* reschedule more LDS announces for ( the future + jitter ) */
1933        announcer->lpdHouseKeepingAt =
1934            calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS );
1935    }
1936}
1937
1938static void
1939onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer )
1940{
1941    tr_announcer * announcer = vannouncer;
1942    tr_sessionLock( announcer->session );
1943
1944    /* maybe send out some "stopped" messages for closed torrents */
1945    flushCloseMessages( announcer );
1946
1947    /* maybe send out some announcements to trackers */
1948    announceMore( announcer );
1949
1950    /* set up the next timer */
1951    tr_timerAdd( announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
1952
1953    tr_sessionUnlock( announcer->session );
1954}
1955
1956/***
1957****
1958***/
1959
1960tr_tracker_stat *
1961tr_announcerStats( const tr_torrent * torrent,
1962                   int              * setmeTrackerCount )
1963{
1964    int i;
1965    int n;
1966    int out = 0;
1967    int tierCount;
1968    tr_tracker_stat * ret;
1969    const time_t now = tr_time( );
1970
1971    assert( tr_isTorrent( torrent ) );
1972
1973    /* count the trackers... */
1974    for( i=n=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i ) {
1975        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1976        n += tr_ptrArraySize( &tier->trackers );
1977    }
1978
1979    /* alloc the stats */
1980    *setmeTrackerCount = n;
1981    ret = tr_new0( tr_tracker_stat, n );
1982
1983    /* populate the stats */
1984    for( i=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i )
1985    {
1986        int j;
1987        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1988        n = tr_ptrArraySize( &tier->trackers );
1989        for( j=0; j<n; ++j )
1990        {
1991            const tr_tracker_item * tracker = tr_ptrArrayNth( (tr_ptrArray*)&tier->trackers, j );
1992            tr_tracker_stat * st = ret + out++;
1993
1994            st->id = tracker->id;
1995            tr_strlcpy( st->host, tracker->host->name, sizeof( st->host ) );
1996            tr_strlcpy( st->announce, tracker->announce, sizeof( st->announce ) );
1997            st->tier = i;
1998            st->isBackup = tracker != tier->currentTracker;
1999            st->lastScrapeStartTime = tier->lastScrapeStartTime;
2000            if( tracker->scrape )
2001                tr_strlcpy( st->scrape, tracker->scrape, sizeof( st->scrape ) );
2002            else
2003                st->scrape[0] = '\0';
2004
2005            st->seederCount = tracker->seederCount;
2006            st->leecherCount = tracker->leecherCount;
2007            st->downloadCount = tracker->downloadCount;
2008
2009            if( st->isBackup )
2010            {
2011                st->scrapeState = TR_TRACKER_INACTIVE;
2012                st->announceState = TR_TRACKER_INACTIVE;
2013                st->nextScrapeTime = 0;
2014                st->nextAnnounceTime = 0;
2015            }
2016            else
2017            {
2018                if(( st->hasScraped = tier->lastScrapeTime != 0 )) {
2019                    st->lastScrapeTime = tier->lastScrapeTime;
2020                    st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
2021                    st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
2022                    tr_strlcpy( st->lastScrapeResult, tier->lastScrapeStr, sizeof( st->lastScrapeResult ) );
2023                }
2024
2025                if( tier->isScraping )
2026                    st->scrapeState = TR_TRACKER_ACTIVE;
2027                else if( !tier->scrapeAt )
2028                    st->scrapeState = TR_TRACKER_INACTIVE;
2029                else if( tier->scrapeAt > now )
2030                {
2031                    st->scrapeState = TR_TRACKER_WAITING;
2032                    st->nextScrapeTime = tier->scrapeAt;
2033                }
2034                else
2035                    st->scrapeState = TR_TRACKER_QUEUED;
2036
2037                st->lastAnnounceStartTime = tier->lastAnnounceStartTime;
2038
2039                if(( st->hasAnnounced = tier->lastAnnounceTime != 0 )) {
2040                    st->lastAnnounceTime = tier->lastAnnounceTime;
2041                    tr_strlcpy( st->lastAnnounceResult, tier->lastAnnounceStr, sizeof( st->lastAnnounceResult ) );
2042                    st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
2043                    st->lastAnnounceTimedOut = tier->lastAnnounceTimedOut;
2044                    st->lastAnnouncePeerCount = tier->lastAnnouncePeerCount;
2045                }
2046
2047                if( tier->isAnnouncing )
2048                    st->announceState = TR_TRACKER_ACTIVE;
2049                else if( !torrent->isRunning || !tier->announceAt )
2050                    st->announceState = TR_TRACKER_INACTIVE;
2051                else if( tier->announceAt > now )
2052                {
2053                    st->announceState = TR_TRACKER_WAITING;
2054                    st->nextAnnounceTime = tier->announceAt;
2055                }
2056                else
2057                    st->announceState = TR_TRACKER_QUEUED;
2058            }
2059        }
2060    }
2061
2062    return ret;
2063}
2064
2065void
2066tr_announcerStatsFree( tr_tracker_stat * trackers,
2067                       int trackerCount UNUSED )
2068{
2069    tr_free( trackers );
2070}
Note: See TracBrowser for help on using the repository browser.