source: trunk/libtransmission/announcer.c @ 11750

Last change on this file since 11750 was 11750, checked in by jordan, 11 years ago

(trunk libT) #3933 "announcer.c peer parsing could be simpler" -- fixed.

Remove redundant code by using tr_peerMgrCompactToPex() and tr_peerMgrCompact6ToPex() to parse compact ipv4 and ipv6 peer lists. Simplify the old-style benc peer list parsing and fix a bug that returned too few bytes in the old-style peer array.

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