Ignore:
Timestamp:
Dec 5, 2012, 5:29:46 PM (9 years ago)
Author:
jordan
Message:

Follow more common whitespace style conventions in the C code (libtransmission, daemon, utils, cli, gtk).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/announcer.c

    r13313 r13625  
    33 *
    44 * 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)
     5 * Transmission project are granted a special exemption to clause 2 (b)
    66 * so that the bulk of its code can remain under the MIT license.
    77 * This exemption does not extend to derived works not owned by
     
    1414#include <limits.h> /* INT_MAX */
    1515#include <stdio.h>
    16 #include <stdlib.h> /* qsort() */
    17 #include <string.h> /* strcmp(), memcpy() */
     16#include <stdlib.h> /* qsort () */
     17#include <string.h> /* strcmp (), memcpy () */
    1818
    1919#include <event2/buffer.h>
     
    2525#include "announcer.h"
    2626#include "announcer-common.h"
    27 #include "crypto.h" /* tr_cryptoRandInt(), tr_cryptoWeakRandInt() */
    28 #include "peer-mgr.h" /* tr_peerMgrCompactToPex() */
     27#include "crypto.h" /* tr_cryptoRandInt (), tr_cryptoWeakRandInt () */
     28#include "peer-mgr.h" /* tr_peerMgrCompactToPex () */
    2929#include "ptrarray.h"
    3030#include "session.h"
     
    3434struct tr_tier;
    3535
    36 static void tier_build_log_name( const struct tr_tier * tier,
    37                                  char * buf, size_t buflen );
    38 
    39 #define dbgmsg( tier, ... ) \
    40 if( tr_deepLoggingIsActive( ) ) do { \
     36static void tier_build_log_name (const struct tr_tier * tier,
     37                                 char * buf, size_t buflen);
     38
     39#define dbgmsg(tier, ...) \
     40if (tr_deepLoggingIsActive ()) do { \
    4141  char name[128]; \
    42   tier_build_log_name( tier, name, sizeof( name ) ); \
    43   tr_deepLog( __FILE__, __LINE__, name, __VA_ARGS__ ); \
    44 } while( 0 )
     42  tier_build_log_name (tier, name, sizeof (name)); \
     43  tr_deepLog (__FILE__, __LINE__, name, __VA_ARGS__); \
     44} while (0)
    4545
    4646enum
    4747{
    4848    /* unless the tracker says otherwise, rescrape this frequently */
    49     DEFAULT_SCRAPE_INTERVAL_SEC = ( 60 * 30 ),
     49    DEFAULT_SCRAPE_INTERVAL_SEC = (60 * 30),
    5050
    5151    /* unless the tracker says otherwise, this is the announce interval */
    52     DEFAULT_ANNOUNCE_INTERVAL_SEC = ( 60 * 10 ),
     52    DEFAULT_ANNOUNCE_INTERVAL_SEC = (60 * 10),
    5353
    5454    /* unless the tracker says otherwise, this is the announce min_interval */
    55     DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = ( 60 * 2 ),
     55    DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = (60 * 2),
    5656
    5757    /* how many web tasks we allow at one time */
     
    7272
    7373const char*
    74 tr_announce_event_get_string( tr_announce_event e )
    75 {
    76     switch( e )
     74tr_announce_event_get_string (tr_announce_event e)
     75{
     76    switch (e)
    7777    {
    7878        case TR_ANNOUNCE_EVENT_COMPLETED:  return "completed";
     
    8888
    8989static int
    90 compareTransfer( uint64_t a_uploaded, uint64_t a_downloaded,
    91                  uint64_t b_uploaded, uint64_t b_downloaded )
     90compareTransfer (uint64_t a_uploaded, uint64_t a_downloaded,
     91                 uint64_t b_uploaded, uint64_t b_downloaded)
    9292{
    9393    /* higher upload count goes first */
    94     if( a_uploaded != b_uploaded )
     94    if (a_uploaded != b_uploaded)
    9595        return a_uploaded > b_uploaded ? -1 : 1;
    9696
    9797    /* then higher download count goes first */
    98     if( a_downloaded != b_downloaded )
     98    if (a_downloaded != b_downloaded)
    9999        return a_downloaded > b_downloaded ? -1 : 1;
    100100
     
    110110 */
    111111static int
    112 compareStops( const void * va, const void * vb )
     112compareStops (const void * va, const void * vb)
    113113{
    114114    int i;
     
    117117
    118118    /* primary key: volume of data transferred. */
    119     if(( i = compareTransfer( a->up, a->down, b->up, b->down)))
     119    if ((i = compareTransfer (a->up, a->down, b->up, b->down)))
    120120        return i;
    121121
    122122    /* secondary key: the torrent's info_hash */
    123     if(( i = memcmp( a->info_hash, b->info_hash, SHA_DIGEST_LENGTH )))
     123    if ((i = memcmp (a->info_hash, b->info_hash, SHA_DIGEST_LENGTH)))
    124124        return i;
    125125
    126126    /* tertiary key: the tracker's announec url */
    127     return tr_strcmp0( a->url, b->url );
     127    return tr_strcmp0 (a->url, b->url);
    128128}
    129129
     
    148148
    149149bool
    150 tr_announcerHasBacklog( const struct tr_announcer * announcer )
     150tr_announcerHasBacklog (const struct tr_announcer * announcer)
    151151{
    152152    return announcer->slotsAvailable < 1;
     
    154154
    155155static void
    156 onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer );
     156onUpkeepTimer (int foo UNUSED, short bar UNUSED, void * vannouncer);
    157157
    158158void
    159 tr_announcerInit( tr_session * session )
     159tr_announcerInit (tr_session * session)
    160160{
    161161    tr_announcer * a;
    162162
    163     assert( tr_isSession( session ) );
    164 
    165     a = tr_new0( tr_announcer, 1 );
     163    assert (tr_isSession (session));
     164
     165    a = tr_new0 (tr_announcer, 1);
    166166    a->stops = TR_PTR_ARRAY_INIT;
    167     a->key = tr_cryptoRandInt( INT_MAX );
     167    a->key = tr_cryptoRandInt (INT_MAX);
    168168    a->session = session;
    169169    a->slotsAvailable = MAX_CONCURRENT_TASKS;
    170     a->upkeepTimer = evtimer_new( session->event_base, onUpkeepTimer, a );
    171     tr_timerAdd( a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
     170    a->upkeepTimer = evtimer_new (session->event_base, onUpkeepTimer, a);
     171    tr_timerAdd (a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0);
    172172
    173173    session->announcer = a;
    174174}
    175175
    176 static void flushCloseMessages( tr_announcer * announcer );
     176static void flushCloseMessages (tr_announcer * announcer);
    177177
    178178void
    179 tr_announcerClose( tr_session * session )
     179tr_announcerClose (tr_session * session)
    180180{
    181181    tr_announcer * announcer = session->announcer;
    182182
    183     flushCloseMessages( announcer );
    184 
    185     tr_tracker_udp_start_shutdown( session );
    186 
    187     event_free( announcer->upkeepTimer );
     183    flushCloseMessages (announcer);
     184
     185    tr_tracker_udp_start_shutdown (session);
     186
     187    event_free (announcer->upkeepTimer);
    188188    announcer->upkeepTimer = NULL;
    189189
    190     tr_ptrArrayDestruct( &announcer->stops, NULL );
     190    tr_ptrArrayDestruct (&announcer->stops, NULL);
    191191
    192192    session->announcer = NULL;
    193     tr_free( announcer );
     193    tr_free (announcer);
    194194}
    195195
     
    220220/* format: host+':'+ port */
    221221static char *
    222 getKey( const char * url )
     222getKey (const char * url)
    223223{
    224224    char * ret;
     
    227227    int port = 0;
    228228
    229     tr_urlParse( url, -1, &scheme, &host, &port, NULL );
    230     ret = tr_strdup_printf( "%s://%s:%d", (scheme?scheme:"invalid"), (host?host:"invalid"), port );
    231 
    232     tr_free( host );
    233     tr_free( scheme );
     229    tr_urlParse (url, -1, &scheme, &host, &port, NULL);
     230    ret = tr_strdup_printf ("%s://%s:%d", (scheme?scheme:"invalid"), (host?host:"invalid"), port);
     231
     232    tr_free (host);
     233    tr_free (scheme);
    234234    return ret;
    235235}
    236236
    237237static void
    238 trackerConstruct( tr_tracker * tracker, const tr_tracker_info * inf )
    239 {
    240     memset( tracker, 0, sizeof( tr_tracker ) );
    241     tracker->key = getKey( inf->announce );
    242     tracker->announce = tr_strdup( inf->announce );
    243     tracker->scrape = tr_strdup( inf->scrape );
     238trackerConstruct (tr_tracker * tracker, const tr_tracker_info * inf)
     239{
     240    memset (tracker, 0, sizeof (tr_tracker));
     241    tracker->key = getKey (inf->announce);
     242    tracker->announce = tr_strdup (inf->announce);
     243    tracker->scrape = tr_strdup (inf->scrape);
    244244    tracker->id = inf->id;
    245245    tracker->seederCount = -1;
     
    249249
    250250static void
    251 trackerDestruct( tr_tracker * tracker )
    252 {
    253     tr_free( tracker->tracker_id_str );
    254     tr_free( tracker->scrape );
    255     tr_free( tracker->announce );
    256     tr_free( tracker->key );
     251trackerDestruct (tr_tracker * tracker)
     252{
     253    tr_free (tracker->tracker_id_str);
     254    tr_free (tracker->scrape);
     255    tr_free (tracker->announce);
     256    tr_free (tracker->key);
    257257}
    258258
     
    314314
    315315static time_t
    316 get_next_scrape_time( const tr_session * session, const tr_tier * tier, int interval )
     316get_next_scrape_time (const tr_session * session, const tr_tier * tier, int interval)
    317317{
    318318    time_t ret;
    319     const time_t now = tr_time( );
     319    const time_t now = tr_time ();
    320320
    321321    /* Maybe don't scrape paused torrents */
    322     if( !tier->isRunning && !session->scrapePausedTorrents )
     322    if (!tier->isRunning && !session->scrapePausedTorrents)
    323323        ret = 0;
    324324
     
    328328    else {
    329329        ret = now + interval;
    330         while( ret % 10 )
     330        while (ret % 10)
    331331            ++ret;
    332332    }
     
    336336
    337337static void
    338 tierConstruct( tr_tier * tier, tr_torrent * tor )
     338tierConstruct (tr_tier * tier, tr_torrent * tor)
    339339{
    340340    static int nextKey = 1;
    341341
    342     memset( tier, 0, sizeof( tr_tier ) );
     342    memset (tier, 0, sizeof (tr_tier));
    343343
    344344    tier->key = nextKey++;
     
    347347    tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
    348348    tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
    349     tier->scrapeAt = get_next_scrape_time( tor->session, tier, tr_cryptoWeakRandInt( 180 ) );
     349    tier->scrapeAt = get_next_scrape_time (tor->session, tier, tr_cryptoWeakRandInt (180));
    350350    tier->tor = tor;
    351351}
    352352
    353353static void
    354 tierDestruct( tr_tier * tier )
    355 {
    356     tr_free( tier->announce_events );
    357 }
    358 
    359 static void
    360 tier_build_log_name( const tr_tier * tier, char * buf, size_t buflen )
    361 {
    362     tr_snprintf( buf, buflen, "[%s---%s]",
    363        ( tier && tier->tor ) ? tr_torrentName( tier->tor ) : "?",
    364        ( tier && tier->currentTracker ) ? tier->currentTracker->key : "?" );
    365 }
    366 
    367 static void
    368 tierIncrementTracker( tr_tier * tier )
     354tierDestruct (tr_tier * tier)
     355{
     356    tr_free (tier->announce_events);
     357}
     358
     359static void
     360tier_build_log_name (const tr_tier * tier, char * buf, size_t buflen)
     361{
     362    tr_snprintf (buf, buflen, "[%s---%s]",
     363     (tier && tier->tor) ? tr_torrentName (tier->tor) : "?",
     364     (tier && tier->currentTracker) ? tier->currentTracker->key : "?");
     365}
     366
     367static void
     368tierIncrementTracker (tr_tier * tier)
    369369{
    370370    /* move our index to the next tracker in the tier */
    371     const int i = ( tier->currentTracker == NULL )
     371    const int i = (tier->currentTracker == NULL)
    372372                ? 0
    373                 : ( tier->currentTrackerIndex + 1 ) % tier->tracker_count;
     373                : (tier->currentTrackerIndex + 1) % tier->tracker_count;
    374374    tier->currentTrackerIndex = i;
    375375    tier->currentTracker = &tier->trackers[i];
     
    408408
    409409static tr_torrent_tiers*
    410 tiersNew( void )
    411 {
    412     return tr_new0( tr_torrent_tiers, 1 );
    413 }
    414 
    415 static void
    416 tiersDestruct( tr_torrent_tiers * tt )
     410tiersNew (void)
     411{
     412    return tr_new0 (tr_torrent_tiers, 1);
     413}
     414
     415static void
     416tiersDestruct (tr_torrent_tiers * tt)
    417417{
    418418    int i;
    419419
    420     for( i=0; i<tt->tracker_count; ++i )
    421         trackerDestruct( &tt->trackers[i] );
    422     tr_free( tt->trackers );
    423 
    424     for( i=0; i<tt->tier_count; ++i )
    425         tierDestruct( &tt->tiers[i] );
    426     tr_free( tt->tiers );
    427 }
    428 
    429 static void
    430 tiersFree( tr_torrent_tiers * tt )
    431 {
    432     tiersDestruct( tt );
    433     tr_free( tt );
     420    for (i=0; i<tt->tracker_count; ++i)
     421        trackerDestruct (&tt->trackers[i]);
     422    tr_free (tt->trackers);
     423
     424    for (i=0; i<tt->tier_count; ++i)
     425        tierDestruct (&tt->tiers[i]);
     426    tr_free (tt->tiers);
     427}
     428
     429static void
     430tiersFree (tr_torrent_tiers * tt)
     431{
     432    tiersDestruct (tt);
     433    tr_free (tt);
    434434}
    435435
    436436static tr_tier*
    437 getTier( tr_announcer * announcer, const uint8_t * info_hash, int tierId )
     437getTier (tr_announcer * announcer, const uint8_t * info_hash, int tierId)
    438438{
    439439    tr_tier * tier = NULL;
    440440
    441     if( announcer != NULL )
     441    if (announcer != NULL)
    442442    {
    443443        tr_session * session = announcer->session;
    444         tr_torrent * tor = tr_torrentFindFromHash( session, info_hash );
    445 
    446         if( tor && tor->tiers )
     444        tr_torrent * tor = tr_torrentFindFromHash (session, info_hash);
     445
     446        if (tor && tor->tiers)
    447447        {
    448448            int i;
    449449            tr_torrent_tiers * tt = tor->tiers;
    450450
    451             for( i=0; !tier && i<tt->tier_count; ++i )
    452                 if( tt->tiers[i].key == tierId )
     451            for (i=0; !tier && i<tt->tier_count; ++i)
     452                if (tt->tiers[i].key == tierId)
    453453                    tier = &tt->tiers[i];
    454454        }
     
    465465
    466466static void
    467 publishMessage( tr_tier * tier, const char * msg, int type )
    468 {
    469     if( tier && tier->tor && tier->tor->tiers && tier->tor->tiers->callback )
     467publishMessage (tr_tier * tier, const char * msg, int type)
     468{
     469    if (tier && tier->tor && tier->tor->tiers && tier->tor->tiers->callback)
    470470    {
    471471        tr_torrent_tiers * tiers = tier->tor->tiers;
     
    473473        event.messageType = type;
    474474        event.text = msg;
    475         if( tier->currentTracker )
     475        if (tier->currentTracker)
    476476            event.tracker = tier->currentTracker->announce;
    477477
    478         tiers->callback( tier->tor, &event, tiers->callbackData );
    479     }
    480 }
    481 
    482 static void
    483 publishErrorClear( tr_tier * tier )
    484 {
    485     publishMessage( tier, NULL, TR_TRACKER_ERROR_CLEAR );
    486 }
    487 
    488 static void
    489 publishWarning( tr_tier * tier, const char * msg )
    490 {
    491     publishMessage( tier, msg, TR_TRACKER_WARNING );
    492 }
    493 
    494 static void
    495 publishError( tr_tier * tier, const char * msg )
    496 {
    497     publishMessage( tier, msg, TR_TRACKER_ERROR );
     478        tiers->callback (tier->tor, &event, tiers->callbackData);
     479    }
     480}
     481
     482static void
     483publishErrorClear (tr_tier * tier)
     484{
     485    publishMessage (tier, NULL, TR_TRACKER_ERROR_CLEAR);
     486}
     487
     488static void
     489publishWarning (tr_tier * tier, const char * msg)
     490{
     491    publishMessage (tier, msg, TR_TRACKER_WARNING);
     492}
     493
     494static void
     495publishError (tr_tier * tier, const char * msg)
     496{
     497    publishMessage (tier, msg, TR_TRACKER_ERROR);
    498498}
    499499
    500500static int8_t
    501 getSeedProbability( tr_tier * tier, int seeds, int leechers, int pex_count )
     501getSeedProbability (tr_tier * tier, int seeds, int leechers, int pex_count)
    502502{
    503503    /* special case optimization:
    504504       ocelot omits seeds from peer lists sent to seeds on private trackers.
    505505       so check for that case... */
    506     if( ( leechers == pex_count ) && tr_torrentIsPrivate( tier->tor )
    507                                   && tr_torrentIsSeed( tier->tor )
    508                                   && ( seeds + leechers < NUMWANT ) )
     506    if ((leechers == pex_count) && tr_torrentIsPrivate (tier->tor)
     507                                  && tr_torrentIsSeed (tier->tor)
     508                                  && (seeds + leechers < NUMWANT))
    509509        return 0;
    510510
    511     if( seeds>=0 && leechers>=0 && (seeds+leechers>0) )
    512         return (int8_t)((100.0*seeds)/(seeds+leechers));
     511    if (seeds>=0 && leechers>=0 && (seeds+leechers>0))
     512        return (int8_t)((100.0*seeds)/ (seeds+leechers));
    513513
    514514    return -1; /* unknown */
     
    516516
    517517static void
    518 publishPeersPex( tr_tier * tier, int seeds, int leechers,
    519                  const tr_pex * pex, int n )
    520 {
    521     if( tier->tor->tiers->callback )
     518publishPeersPex (tr_tier * tier, int seeds, int leechers,
     519                 const tr_pex * pex, int n)
     520{
     521    if (tier->tor->tiers->callback)
    522522    {
    523523        tr_tracker_event e = TRACKER_EVENT_INIT;
    524524        e.messageType = TR_TRACKER_PEERS;
    525         e.seedProbability = getSeedProbability( tier, seeds, leechers, n );
     525        e.seedProbability = getSeedProbability (tier, seeds, leechers, n);
    526526        e.pex = pex;
    527527        e.pexCount = n;
    528         dbgmsg( tier, "got %d peers; seed prob %d", n, (int)e.seedProbability );
    529 
    530         tier->tor->tiers->callback( tier->tor, &e, NULL );
     528        dbgmsg (tier, "got %d peers; seed prob %d", n, (int)e.seedProbability);
     529
     530        tier->tor->tiers->callback (tier->tor, &e, NULL);
    531531    }
    532532}
     
    550550 * secondary key: udp comes before http */
    551551static int
    552 filter_trackers_compare_func( const void * va, const void * vb )
     552filter_trackers_compare_func (const void * va, const void * vb)
    553553{
    554554    const struct ann_tracker_info * a = va;
    555555    const struct ann_tracker_info * b = vb;
    556     if( a->info.tier != b->info.tier )
     556    if (a->info.tier != b->info.tier)
    557557        return a->info.tier - b->info.tier;
    558     return -strcmp( a->scheme, b->scheme );
     558    return -strcmp (a->scheme, b->scheme);
    559559}
    560560
     
    563563 */
    564564static tr_tracker_info *
    565 filter_trackers( tr_tracker_info * input, int input_count, int * setme_count )
     565filter_trackers (tr_tracker_info * input, int input_count, int * setme_count)
    566566{
    567567    int i, in;
     
    569569    int n = 0;
    570570    struct tr_tracker_info * ret;
    571     struct ann_tracker_info * tmp = tr_new0( struct ann_tracker_info, input_count );
    572 
    573     /*for( i=0, in=input_count; i<in; ++i ) fprintf( stderr, "IN: [%d][%s]\n", input[i].tier, input[i].announce );*/
     571    struct ann_tracker_info * tmp = tr_new0 (struct ann_tracker_info, input_count);
     572
     573    /*for (i=0, in=input_count; i<in; ++i) fprintf (stderr, "IN: [%d][%s]\n", input[i].tier, input[i].announce);*/
    574574
    575575    /* build a list of valid trackers */
    576     for( i=0, in=input_count; i<in; ++i ) {
    577         if( tr_urlIsValidTracker( input[i].announce ) ) {
     576    for (i=0, in=input_count; i<in; ++i) {
     577        if (tr_urlIsValidTracker (input[i].announce)) {
    578578            int port;
    579579            char * scheme;
     
    581581            char * path;
    582582            bool is_duplicate = false;
    583             tr_urlParse( input[i].announce, -1, &scheme, &host, &port, &path );
     583            tr_urlParse (input[i].announce, -1, &scheme, &host, &port, &path);
    584584
    585585            /* weed out one common source of duplicates:
     
    587587             * "http://tracker:80/announce"
    588588             */
    589             for( j=0, jn=n; !is_duplicate && j<jn; ++j )
     589            for (j=0, jn=n; !is_duplicate && j<jn; ++j)
    590590                is_duplicate = (tmp[j].port==port)
    591                             && !strcmp(tmp[j].scheme,scheme)
    592                             && !strcmp(tmp[j].host,host)
    593                             && !strcmp(tmp[j].path,path);
    594 
    595             if( is_duplicate ) {
    596                 tr_free( path );
    597                 tr_free( host );
    598                 tr_free( scheme );
     591                            && !strcmp (tmp[j].scheme,scheme)
     592                            && !strcmp (tmp[j].host,host)
     593                            && !strcmp (tmp[j].path,path);
     594
     595            if (is_duplicate) {
     596                tr_free (path);
     597                tr_free (host);
     598                tr_free (scheme);
    599599                continue;
    600600            }
     
    611611     * (note: this can leave gaps in the `tier' values, but since the calling
    612612     * function doesn't care, there's no point in removing the gaps...) */
    613     for( i=0, in=n; i<in; ++i )
    614         for( j=i+1, jn=n; j<jn; ++j )
    615             if( (tmp[i].info.tier!=tmp[j].info.tier)
     613    for (i=0, in=n; i<in; ++i)
     614        for (j=i+1, jn=n; j<jn; ++j)
     615            if ((tmp[i].info.tier!=tmp[j].info.tier)
    616616                       && (tmp[i].port==tmp[j].port)
    617                        && !tr_strcmp0(tmp[i].host,tmp[j].host)
    618                        && !tr_strcmp0(tmp[i].path,tmp[j].path) )
     617                       && !tr_strcmp0 (tmp[i].host,tmp[j].host)
     618                       && !tr_strcmp0 (tmp[i].path,tmp[j].path))
    619619                tmp[j].info.tier = tmp[i].info.tier;
    620620
     
    622622     * (1) unjumble the tiers from the previous step
    623623     * (2) move the UDP trackers to the front of each tier */
    624     qsort( tmp, n, sizeof(struct ann_tracker_info), filter_trackers_compare_func );
     624    qsort (tmp, n, sizeof (struct ann_tracker_info), filter_trackers_compare_func);
    625625
    626626    /* build the output */
    627627    *setme_count = n;
    628     ret = tr_new0( tr_tracker_info, n );
    629     for( i=0, in=n; i<in; ++i )
     628    ret = tr_new0 (tr_tracker_info, n);
     629    for (i=0, in=n; i<in; ++i)
    630630        ret[i] = tmp[i].info;
    631631
    632632    /* cleanup */
    633     for( i=0, in=n; i<in; ++i ) {
    634         tr_free( tmp[i].path );
    635         tr_free( tmp[i].host );
    636         tr_free( tmp[i].scheme );
    637     }
    638     tr_free( tmp );
    639 
    640     /*for( i=0, in=n; i<in; ++i ) fprintf( stderr, "OUT: [%d][%s]\n", ret[i].tier, ret[i].announce );*/
     633    for (i=0, in=n; i<in; ++i) {
     634        tr_free (tmp[i].path);
     635        tr_free (tmp[i].host);
     636        tr_free (tmp[i].scheme);
     637    }
     638    tr_free (tmp);
     639
     640    /*for (i=0, in=n; i<in; ++i) fprintf (stderr, "OUT: [%d][%s]\n", ret[i].tier, ret[i].announce);*/
    641641    return ret;
    642642}
     
    644644
    645645static void
    646 addTorrentToTier( tr_torrent_tiers * tt, tr_torrent * tor )
     646addTorrentToTier (tr_torrent_tiers * tt, tr_torrent * tor)
    647647{
    648648    int i, n;
    649649    int tier_count;
    650650    tr_tier * tier;
    651     tr_tracker_info * infos = filter_trackers( tor->info.trackers,
    652                                                tor->info.trackerCount, &n );
     651    tr_tracker_info * infos = filter_trackers (tor->info.trackers,
     652                                               tor->info.trackerCount, &n);
    653653
    654654    /* build the array of trackers */
    655     tt->trackers = tr_new0( tr_tracker, n );
     655    tt->trackers = tr_new0 (tr_tracker, n);
    656656    tt->tracker_count = n;
    657     for( i=0; i<n; ++i )
    658         trackerConstruct( &tt->trackers[i], &infos[i] );
     657    for (i=0; i<n; ++i)
     658        trackerConstruct (&tt->trackers[i], &infos[i]);
    659659
    660660    /* count how many tiers there are */
    661661    tier_count = 0;
    662     for( i=0; i<n; ++i )
    663         if( !i || ( infos[i].tier != infos[i-1].tier ) )
     662    for (i=0; i<n; ++i)
     663        if (!i || (infos[i].tier != infos[i-1].tier))
    664664            ++tier_count;
    665665
    666666    /* build the array of tiers */
    667667    tier = NULL;
    668     tt->tiers = tr_new0( tr_tier, tier_count );
     668    tt->tiers = tr_new0 (tr_tier, tier_count);
    669669    tt->tier_count = 0;
    670     for( i=0; i<n; ++i ) {
    671         if( i && ( infos[i].tier == infos[i-1].tier ) )
     670    for (i=0; i<n; ++i) {
     671        if (i && (infos[i].tier == infos[i-1].tier))
    672672            ++tier->tracker_count;
    673673        else {
    674674            tier = &tt->tiers[tt->tier_count++];
    675             tierConstruct( tier, tor );
     675            tierConstruct (tier, tor);
    676676            tier->trackers = &tt->trackers[i];
    677677            tier->tracker_count = 1;
    678             tierIncrementTracker( tier );
     678            tierIncrementTracker (tier);
    679679        }
    680680    }
    681681
    682682    /* cleanup */
    683     tr_free( infos );
     683    tr_free (infos);
    684684}
    685685
    686686tr_torrent_tiers *
    687 tr_announcerAddTorrent( tr_torrent           * tor,
     687tr_announcerAddTorrent (tr_torrent           * tor,
    688688                        tr_tracker_callback  * callback,
    689                         void                 * callbackData )
     689                        void                 * callbackData)
    690690{
    691691    tr_torrent_tiers * tiers;
    692692
    693     assert( tr_isTorrent( tor ) );
    694 
    695     tiers = tiersNew( );
     693    assert (tr_isTorrent (tor));
     694
     695    tiers = tiersNew ();
    696696    tiers->callback = callback;
    697697    tiers->callbackData = callbackData;
    698698
    699     addTorrentToTier( tiers, tor );
     699    addTorrentToTier (tiers, tor);
    700700
    701701    return tiers;
     
    707707
    708708static bool
    709 tierCanManualAnnounce( const tr_tier * tier )
    710 {
    711     return tier->manualAnnounceAllowedAt <= tr_time( );
     709tierCanManualAnnounce (const tr_tier * tier)
     710{
     711    return tier->manualAnnounceAllowedAt <= tr_time ();
    712712}
    713713
    714714bool
    715 tr_announcerCanManualAnnounce( const tr_torrent * tor )
     715tr_announcerCanManualAnnounce (const tr_torrent * tor)
    716716{
    717717    int i;
    718718    struct tr_torrent_tiers * tt = NULL;
    719719
    720     assert( tr_isTorrent( tor ) );
    721     assert( tor->tiers != NULL );
    722 
    723     if( tor->isRunning )
     720    assert (tr_isTorrent (tor));
     721    assert (tor->tiers != NULL);
     722
     723    if (tor->isRunning)
    724724        tt = tor->tiers;
    725725
    726726    /* return true if any tier can manual announce */
    727     for( i=0; tt && i<tt->tier_count; ++i )
    728         if( tierCanManualAnnounce( &tt->tiers[i] ) )
     727    for (i=0; tt && i<tt->tier_count; ++i)
     728        if (tierCanManualAnnounce (&tt->tiers[i]))
    729729            return true;
    730730
     
    733733
    734734time_t
    735 tr_announcerNextManualAnnounce( const tr_torrent * tor )
     735tr_announcerNextManualAnnounce (const tr_torrent * tor)
    736736{
    737737    int i;
    738     time_t ret = ~(time_t)0;
     738    time_t ret = ~ (time_t)0;
    739739    struct tr_torrent_tiers * tt = tor->tiers;
    740740
    741741    /* find the earliest manual announce time from all peers */
    742     for( i=0; tt && i<tt->tier_count; ++i )
    743         if( tt->tiers[i].isRunning )
    744             ret = MIN( ret, tt->tiers[i].manualAnnounceAllowedAt );
     742    for (i=0; tt && i<tt->tier_count; ++i)
     743        if (tt->tiers[i].isRunning)
     744            ret = MIN (ret, tt->tiers[i].manualAnnounceAllowedAt);
    745745
    746746    return ret;
     
    748748
    749749static void
    750 dbgmsg_tier_announce_queue( const tr_tier * tier )
    751 {
    752     if( tr_deepLoggingIsActive( ) )
     750dbgmsg_tier_announce_queue (const tr_tier * tier)
     751{
     752    if (tr_deepLoggingIsActive ())
    753753    {
    754754        int i;
    755755        char name[128];
    756756        char * message;
    757         struct evbuffer * buf = evbuffer_new( );
    758 
    759         tier_build_log_name( tier, name, sizeof( name ) );
    760         for( i=0; i<tier->announce_event_count; ++i )
     757        struct evbuffer * buf = evbuffer_new ();
     758
     759        tier_build_log_name (tier, name, sizeof (name));
     760        for (i=0; i<tier->announce_event_count; ++i)
    761761        {
    762762            const tr_announce_event e = tier->announce_events[i];
    763             const char * str = tr_announce_event_get_string( e );
    764             evbuffer_add_printf( buf, "[%d:%s]", i, str );
     763            const char * str = tr_announce_event_get_string (e);
     764            evbuffer_add_printf (buf, "[%d:%s]", i, str);
    765765        }
    766766
    767         message = evbuffer_free_to_str( buf );
    768         tr_deepLog( __FILE__, __LINE__, name, "announce queue is %s", message );
    769         tr_free( message );
    770     }
    771 }
    772 
    773 static void
    774 tier_announce_remove_trailing( tr_tier * tier, tr_announce_event e )
    775 {
    776     while( ( tier->announce_event_count > 0 )
    777         && ( tier->announce_events[tier->announce_event_count-1] == e ) )
     767        message = evbuffer_free_to_str (buf);
     768        tr_deepLog (__FILE__, __LINE__, name, "announce queue is %s", message);
     769        tr_free (message);
     770    }
     771}
     772
     773static void
     774tier_announce_remove_trailing (tr_tier * tier, tr_announce_event e)
     775{
     776    while ((tier->announce_event_count > 0)
     777        && (tier->announce_events[tier->announce_event_count-1] == e))
    778778        --tier->announce_event_count;
    779779}
    780780
    781781static void
    782 tier_announce_event_push( tr_tier            * tier,
     782tier_announce_event_push (tr_tier            * tier,
    783783                          tr_announce_event    e,
    784                           time_t               announceAt )
     784                          time_t               announceAt)
    785785{
    786786    int i;
    787787
    788     assert( tier != NULL );
    789 
    790     dbgmsg_tier_announce_queue( tier );
    791     dbgmsg( tier, "queued \"%s\"", tr_announce_event_get_string( e ) );
    792 
    793     if( tier->announce_event_count > 0 )
     788    assert (tier != NULL);
     789
     790    dbgmsg_tier_announce_queue (tier);
     791    dbgmsg (tier, "queued \"%s\"", tr_announce_event_get_string (e));
     792
     793    if (tier->announce_event_count > 0)
    794794    {
    795795        /* special case #1: if we're adding a "stopped" event,
    796796         * dump everything leading up to it except "completed" */
    797         if( e == TR_ANNOUNCE_EVENT_STOPPED ) {
     797        if (e == TR_ANNOUNCE_EVENT_STOPPED) {
    798798            bool has_completed = false;
    799799            const tr_announce_event c = TR_ANNOUNCE_EVENT_COMPLETED;
    800             for( i=0; !has_completed && i<tier->announce_event_count; ++i )
     800            for (i=0; !has_completed && i<tier->announce_event_count; ++i)
    801801                has_completed = c == tier->announce_events[i];
    802802            tier->announce_event_count = 0;
    803             if( has_completed )
     803            if (has_completed)
    804804                tier->announce_events[tier->announce_event_count++] = c;
    805805        }
    806806
    807807        /* special case #2: dump all empty strings leading up to this event */
    808         tier_announce_remove_trailing( tier, TR_ANNOUNCE_EVENT_NONE );
     808        tier_announce_remove_trailing (tier, TR_ANNOUNCE_EVENT_NONE);
    809809
    810810        /* special case #3: no consecutive duplicates */
    811         tier_announce_remove_trailing( tier, e );
     811        tier_announce_remove_trailing (tier, e);
    812812    }
    813813
    814814    /* make room in the array for another event */
    815     if( tier->announce_event_alloc <= tier->announce_event_count ) {
     815    if (tier->announce_event_alloc <= tier->announce_event_count) {
    816816        tier->announce_event_alloc += 4;
    817         tier->announce_events = tr_renew( tr_announce_event,
     817        tier->announce_events = tr_renew (tr_announce_event,
    818818                                          tier->announce_events,
    819                                           tier->announce_event_alloc );
     819                                          tier->announce_event_alloc);
    820820    }
    821821
     
    824824    tier->announceAt = announceAt;
    825825
    826     dbgmsg_tier_announce_queue( tier );
    827     dbgmsg( tier, "announcing in %d seconds", (int)difftime(announceAt,tr_time()) );
     826    dbgmsg_tier_announce_queue (tier);
     827    dbgmsg (tier, "announcing in %d seconds", (int)difftime (announceAt,tr_time ()));
    828828}
    829829
    830830static tr_announce_event
    831 tier_announce_event_pull( tr_tier * tier )
     831tier_announce_event_pull (tr_tier * tier)
    832832{
    833833    const tr_announce_event e = tier->announce_events[0];
    834834
    835     tr_removeElementFromArray( tier->announce_events,
    836                                0, sizeof( tr_announce_event ),
    837                                tier->announce_event_count-- );
     835    tr_removeElementFromArray (tier->announce_events,
     836                               0, sizeof (tr_announce_event),
     837                               tier->announce_event_count--);
    838838
    839839    return e;
     
    841841
    842842static void
    843 torrentAddAnnounce( tr_torrent * tor, tr_announce_event e, time_t announceAt )
     843torrentAddAnnounce (tr_torrent * tor, tr_announce_event e, time_t announceAt)
    844844{
    845845    int i;
     
    847847
    848848    /* walk through each tier and tell them to announce */
    849     for( i=0; i<tt->tier_count; ++i )
    850         tier_announce_event_push( &tt->tiers[i], e, announceAt );
     849    for (i=0; i<tt->tier_count; ++i)
     850        tier_announce_event_push (&tt->tiers[i], e, announceAt);
    851851}
    852852
    853853void
    854 tr_announcerTorrentStarted( tr_torrent * tor )
    855 {
    856     torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_STARTED, tr_time( ) );
     854tr_announcerTorrentStarted (tr_torrent * tor)
     855{
     856    torrentAddAnnounce (tor, TR_ANNOUNCE_EVENT_STARTED, tr_time ());
    857857}
    858858void
    859 tr_announcerManualAnnounce( tr_torrent * tor )
    860 {
    861     torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_NONE, tr_time( ) );
     859tr_announcerManualAnnounce (tr_torrent * tor)
     860{
     861    torrentAddAnnounce (tor, TR_ANNOUNCE_EVENT_NONE, tr_time ());
    862862}
    863863void
    864 tr_announcerTorrentStopped( tr_torrent * tor )
    865 {
    866     torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_STOPPED, tr_time( ) );
     864tr_announcerTorrentStopped (tr_torrent * tor)
     865{
     866    torrentAddAnnounce (tor, TR_ANNOUNCE_EVENT_STOPPED, tr_time ());
    867867}
    868868void
    869 tr_announcerTorrentCompleted( tr_torrent * tor )
    870 {
    871     torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_COMPLETED, tr_time( ) );
     869tr_announcerTorrentCompleted (tr_torrent * tor)
     870{
     871    torrentAddAnnounce (tor, TR_ANNOUNCE_EVENT_COMPLETED, tr_time ());
    872872}
    873873void
    874 tr_announcerChangeMyPort( tr_torrent * tor )
    875 {
    876     tr_announcerTorrentStarted( tor );
     874tr_announcerChangeMyPort (tr_torrent * tor)
     875{
     876    tr_announcerTorrentStarted (tor);
    877877}
    878878
     
    882882
    883883void
    884 tr_announcerAddBytes( tr_torrent * tor, int type, uint32_t byteCount )
     884tr_announcerAddBytes (tr_torrent * tor, int type, uint32_t byteCount)
    885885{
    886886    int i;
    887887    struct tr_torrent_tiers * tt = tor->tiers;
    888888
    889     assert( tr_isTorrent( tor ) );
    890     assert( type==TR_ANN_UP || type==TR_ANN_DOWN || type==TR_ANN_CORRUPT );
    891 
    892     for( i=0; i<tt->tier_count; ++i )
     889    assert (tr_isTorrent (tor));
     890    assert (type==TR_ANN_UP || type==TR_ANN_DOWN || type==TR_ANN_CORRUPT);
     891
     892    for (i=0; i<tt->tier_count; ++i)
    893893        tt->tiers[i].byteCounts[ type ] += byteCount;
    894894}
     
    899899
    900900static tr_announce_request *
    901 announce_request_new( const tr_announcer  * announcer,
     901announce_request_new (const tr_announcer  * announcer,
    902902                      const tr_torrent    * tor,
    903903                      const tr_tier       * tier,
    904                       tr_announce_event     event )
    905 {
    906     tr_announce_request * req = tr_new0( tr_announce_request, 1 );
    907     req->port = tr_sessionGetPublicPeerPort( announcer->session );
    908     req->url = tr_strdup( tier->currentTracker->announce );
    909     req->tracker_id_str = tr_strdup( tier->currentTracker->tracker_id_str );
    910     memcpy( req->info_hash, tor->info.hash, SHA_DIGEST_LENGTH );
    911     memcpy( req->peer_id, tor->peer_id, PEER_ID_LEN );
     904                      tr_announce_event     event)
     905{
     906    tr_announce_request * req = tr_new0 (tr_announce_request, 1);
     907    req->port = tr_sessionGetPublicPeerPort (announcer->session);
     908    req->url = tr_strdup (tier->currentTracker->announce);
     909    req->tracker_id_str = tr_strdup (tier->currentTracker->tracker_id_str);
     910    memcpy (req->info_hash, tor->info.hash, SHA_DIGEST_LENGTH);
     911    memcpy (req->peer_id, tor->peer_id, PEER_ID_LEN);
    912912    req->up = tier->byteCounts[TR_ANN_UP];
    913913    req->down = tier->byteCounts[TR_ANN_DOWN];
    914914    req->corrupt = tier->byteCounts[TR_ANN_CORRUPT];
    915     req->leftUntilComplete = tr_torrentHasMetadata( tor )
    916             ? tor->info.totalSize - tr_cpHaveTotal( &tor->completion )
    917             : ~(uint64_t)0;
     915    req->leftUntilComplete = tr_torrentHasMetadata (tor)
     916            ? tor->info.totalSize - tr_cpHaveTotal (&tor->completion)
     917            : ~ (uint64_t)0;
    918918    req->event = event;
    919919    req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : NUMWANT;
    920920    req->key = announcer->key;
    921     req->partial_seed = tr_cpGetStatus( &tor->completion ) == TR_PARTIAL_SEED;
    922     tier_build_log_name( tier, req->log_name, sizeof( req->log_name ) );
     921    req->partial_seed = tr_cpGetStatus (&tor->completion) == TR_PARTIAL_SEED;
     922    tier_build_log_name (tier, req->log_name, sizeof (req->log_name));
    923923    return req;
    924924}
    925925
    926 static void announce_request_free( tr_announce_request * req );
     926static void announce_request_free (tr_announce_request * req);
    927927
    928928void
    929 tr_announcerRemoveTorrent( tr_announcer * announcer, tr_torrent * tor )
     929tr_announcerRemoveTorrent (tr_announcer * announcer, tr_torrent * tor)
    930930{
    931931    struct tr_torrent_tiers * tt = tor->tiers;
    932932
    933     if( tt != NULL )
     933    if (tt != NULL)
    934934    {
    935935        int i;
    936         for( i=0; i<tt->tier_count; ++i )
     936        for (i=0; i<tt->tier_count; ++i)
    937937        {
    938938            tr_tier * tier = &tt->tiers[i];
    939             if( tier->isRunning )
     939            if (tier->isRunning)
    940940            {
    941941                const tr_announce_event e = TR_ANNOUNCE_EVENT_STOPPED;
    942                 tr_announce_request * req = announce_request_new( announcer, tor, tier, e );
    943 
    944                 if( tr_ptrArrayFindSorted( &announcer->stops, req, compareStops ) != NULL )
    945                     announce_request_free( req );
     942                tr_announce_request * req = announce_request_new (announcer, tor, tier, e);
     943
     944                if (tr_ptrArrayFindSorted (&announcer->stops, req, compareStops) != NULL)
     945                    announce_request_free (req);
    946946                else
    947                     tr_ptrArrayInsertSorted( &announcer->stops, req, compareStops );
     947                    tr_ptrArrayInsertSorted (&announcer->stops, req, compareStops);
    948948            }
    949949        }
    950950
    951         tiersFree( tor->tiers );
     951        tiersFree (tor->tiers);
    952952        tor->tiers = NULL;
    953953    }
     
    955955
    956956static int
    957 getRetryInterval( const tr_tracker * t )
     957getRetryInterval (const tr_tracker * t)
    958958{
    959959    int minutes;
    960     const unsigned int jitter_seconds = tr_cryptoWeakRandInt( 60 );
    961     switch( t->consecutiveFailures ) {
     960    const unsigned int jitter_seconds = tr_cryptoWeakRandInt (60);
     961    switch (t->consecutiveFailures) {
    962962        case 0:  minutes =   1; break;
    963963        case 1:  minutes =   5; break;
     
    967967        default: minutes = 120; break;
    968968    }
    969     return ( minutes * 60 ) + jitter_seconds;
     969    return (minutes * 60) + jitter_seconds;
    970970}
    971971
     
    982982
    983983static void
    984 on_announce_error( tr_tier * tier, const char * err, tr_announce_event e )
     984on_announce_error (tr_tier * tier, const char * err, tr_announce_event e)
    985985{
    986986    int interval;
    987987
    988988    /* increment the error count */
    989     if( tier->currentTracker != NULL )
     989    if (tier->currentTracker != NULL)
    990990        ++tier->currentTracker->consecutiveFailures;
    991991
    992992    /* set the error message */
    993     dbgmsg( tier, "%s", err );
    994     tr_torinf( tier->tor, "%s", err );
    995     tr_strlcpy( tier->lastAnnounceStr, err, sizeof( tier->lastAnnounceStr ) );
     993    dbgmsg (tier, "%s", err);
     994    tr_torinf (tier->tor, "%s", err);
     995    tr_strlcpy (tier->lastAnnounceStr, err, sizeof (tier->lastAnnounceStr));
    996996
    997997    /* switch to the next tracker */
    998     tierIncrementTracker( tier );
     998    tierIncrementTracker (tier);
    999999
    10001000    /* schedule a reannounce */
    1001     interval = getRetryInterval( tier->currentTracker );
    1002     dbgmsg( tier, "Retrying announce in %d seconds.", interval );
    1003     tr_torinf( tier->tor, "Retrying announce in %d seconds.", interval );
    1004     tier_announce_event_push( tier, e, tr_time( ) + interval );
    1005 }
    1006 
    1007 static void
    1008 on_announce_done( const tr_announce_response  * response,
    1009                   void                        * vdata )
     1001    interval = getRetryInterval (tier->currentTracker);
     1002    dbgmsg (tier, "Retrying announce in %d seconds.", interval);
     1003    tr_torinf (tier->tor, "Retrying announce in %d seconds.", interval);
     1004    tier_announce_event_push (tier, e, tr_time () + interval);
     1005}
     1006
     1007static void
     1008on_announce_done (const tr_announce_response  * response,
     1009                  void                        * vdata)
    10101010{
    10111011    struct announce_data * data = vdata;
    10121012    tr_announcer * announcer = data->session->announcer;
    1013     tr_tier * tier = getTier( announcer, response->info_hash, data->tierId );
    1014     const time_t now = tr_time( );
     1013    tr_tier * tier = getTier (announcer, response->info_hash, data->tierId);
     1014    const time_t now = tr_time ();
    10151015    const tr_announce_event event = data->event;
    10161016
    1017     if( announcer )
     1017    if (announcer)
    10181018        ++announcer->slotsAvailable;
    10191019
    1020     if( tier != NULL )
     1020    if (tier != NULL)
    10211021    {
    10221022        tr_tracker * tracker;
    10231023
    1024         dbgmsg( tier, "Got announce response: "
     1024        dbgmsg (tier, "Got announce response: "
    10251025                      "connected:%d "
    10261026                      "timeout:%d "
     
    10351035                      "err:%s "
    10361036                      "warn:%s",
    1037                       (int)response->did_connect,
    1038                       (int)response->did_timeout,
     1037                    (int)response->did_connect,
     1038                    (int)response->did_timeout,
    10391039                      response->seeders,
    10401040                      response->leechers,
     
    10461046                      response->pex6_count,
    10471047                      response->errmsg ? response->errmsg : "none",
    1048                       response->warning ? response->warning : "none" );
     1048                      response->warning ? response->warning : "none");
    10491049
    10501050        tier->lastAnnounceTime = now;
     
    10541054        tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
    10551055
    1056         if( !response->did_connect )
     1056        if (!response->did_connect)
    10571057        {
    1058             on_announce_error( tier, _( "Could not connect to tracker" ), event );
     1058            on_announce_error (tier, _ ("Could not connect to tracker"), event);
    10591059        }
    1060         else if( response->did_timeout )
     1060        else if (response->did_timeout)
    10611061        {
    1062             on_announce_error( tier, _( "Tracker did not respond" ), event );
     1062            on_announce_error (tier, _ ("Tracker did not respond"), event);
    10631063        }
    1064         else if( response->errmsg )
     1064        else if (response->errmsg)
    10651065        {
    10661066            /* If the torrent's only tracker returned an error, publish it.
     
    10681068               all too common for people to load up dozens of dead trackers
    10691069               in a torrent's metainfo... */
    1070             if( tier->tor->info.trackerCount < 2 )
    1071                 publishError( tier, response->errmsg );
    1072 
    1073             on_announce_error( tier, response->errmsg, event );
     1070            if (tier->tor->info.trackerCount < 2)
     1071                publishError (tier, response->errmsg);
     1072
     1073            on_announce_error (tier, response->errmsg, event);
    10741074        }
    10751075        else
     
    10831083            const bool isStopped = event == TR_ANNOUNCE_EVENT_STOPPED;
    10841084
    1085             publishErrorClear( tier );
    1086 
    1087             if(( tracker = tier->currentTracker ))
     1085            publishErrorClear (tier);
     1086
     1087            if ((tracker = tier->currentTracker))
    10881088            {
    10891089                tracker->consecutiveFailures = 0;
    10901090
    1091                 if( response->seeders >= 0 )
     1091                if (response->seeders >= 0)
    10921092                {
    10931093                    tracker->seederCount = seeders = response->seeders;
     
    10951095                }
    10961096
    1097                 if( response->leechers >= 0 )
     1097                if (response->leechers >= 0)
    10981098                {
    10991099                    tracker->leecherCount = leechers = response->leechers;
    11001100                    ++scrape_fields;
    11011101                }
    1102                 if( response->downloads >= 0 )
     1102                if (response->downloads >= 0)
    11031103                {
    11041104                    tracker->downloadCount = downloads = response->downloads;
     
    11061106                }
    11071107
    1108                 if(( str = response->tracker_id_str ))
     1108                if ((str = response->tracker_id_str))
    11091109                {
    1110                     tr_free( tracker->tracker_id_str );
    1111                     tracker->tracker_id_str = tr_strdup( str );
     1110                    tr_free (tracker->tracker_id_str);
     1111                    tracker->tracker_id_str = tr_strdup (str);
    11121112                }
    11131113            }
    11141114
    1115             if(( str = response->warning ))
     1115            if ((str = response->warning))
    11161116            {
    1117                 tr_strlcpy( tier->lastAnnounceStr, str,
    1118                             sizeof( tier->lastAnnounceStr ) );
    1119                 dbgmsg( tier, "tracker gave \"%s\"", str );
    1120                 publishWarning( tier, str );
     1117                tr_strlcpy (tier->lastAnnounceStr, str,
     1118                            sizeof (tier->lastAnnounceStr));
     1119                dbgmsg (tier, "tracker gave \"%s\"", str);
     1120                publishWarning (tier, str);
    11211121            }
    11221122            else
    11231123            {
    1124                 tr_strlcpy( tier->lastAnnounceStr, _( "Success" ),
    1125                             sizeof( tier->lastAnnounceStr ) );
     1124                tr_strlcpy (tier->lastAnnounceStr, _ ("Success"),
     1125                            sizeof (tier->lastAnnounceStr));
    11261126            }
    11271127
    1128             if(( i = response->min_interval ))
     1128            if ((i = response->min_interval))
    11291129                tier->announceMinIntervalSec = i;
    11301130
    1131             if(( i = response->interval ))
     1131            if ((i = response->interval))
    11321132                tier->announceIntervalSec = i;
    11331133
    1134             if( response->pex_count > 0 )
    1135                 publishPeersPex( tier, seeders, leechers,
    1136                                  response->pex, response->pex_count );
    1137 
    1138             if( response->pex6_count > 0 )
    1139                 publishPeersPex( tier, seeders, leechers,
    1140                                  response->pex6, response->pex6_count );
     1134            if (response->pex_count > 0)
     1135                publishPeersPex (tier, seeders, leechers,
     1136                                 response->pex, response->pex_count);
     1137
     1138            if (response->pex6_count > 0)
     1139                publishPeersPex (tier, seeders, leechers,
     1140                                 response->pex6, response->pex6_count);
    11411141
    11421142            tier->isRunning = data->isRunningOnSuccess;
     
    11441144            /* if the tracker included scrape fields in its announce response,
    11451145               then a separate scrape isn't needed */
    1146             if( ( scrape_fields >= 3 ) || ( !tracker->scrape && ( scrape_fields >= 1 ) ) )
     1146            if ((scrape_fields >= 3) || (!tracker->scrape && (scrape_fields >= 1)))
    11471147            {
    1148                 tr_tordbg( tier->tor, "Announce response contained scrape info; "
     1148                tr_tordbg (tier->tor, "Announce response contained scrape info; "
    11491149                                      "rescheduling next scrape to %d seconds from now.",
    1150                                       tier->scrapeIntervalSec );
    1151                 tier->scrapeAt = get_next_scrape_time( announcer->session, tier, tier->scrapeIntervalSec );
     1150                                      tier->scrapeIntervalSec);
     1151                tier->scrapeAt = get_next_scrape_time (announcer->session, tier, tier->scrapeIntervalSec);
    11521152                tier->lastScrapeTime = now;
    11531153                tier->lastScrapeSucceeded = true;
    11541154            }
    1155             else if( tier->lastScrapeTime + tier->scrapeIntervalSec <= now )
     1155            else if (tier->lastScrapeTime + tier->scrapeIntervalSec <= now)
    11561156            {
    1157                 tier->scrapeAt = get_next_scrape_time( announcer->session, tier, 0 );
     1157                tier->scrapeAt = get_next_scrape_time (announcer->session, tier, 0);
    11581158            }
    11591159
     
    11621162                                        + response->pex6_count;
    11631163
    1164             if( isStopped )
     1164            if (isStopped)
    11651165            {
    11661166                /* now that we've successfully stopped the torrent,
     
    11721172            }
    11731173
    1174             if( !isStopped && !tier->announce_event_count )
     1174            if (!isStopped && !tier->announce_event_count)
    11751175            {
    11761176                /* the queue is empty, so enqueue a perodic update */
    11771177                i = tier->announceIntervalSec;
    1178                 dbgmsg( tier, "Sending periodic reannounce in %d seconds", i );
    1179                 tier_announce_event_push( tier, TR_ANNOUNCE_EVENT_NONE, now + i );
     1178                dbgmsg (tier, "Sending periodic reannounce in %d seconds", i);
     1179                tier_announce_event_push (tier, TR_ANNOUNCE_EVENT_NONE, now + i);
    11801180            }
    11811181        }
    11821182    }
    11831183
    1184     tr_free( data );
    1185 }
    1186 
    1187 static void
    1188 announce_request_free( tr_announce_request * req )
    1189 {
    1190     tr_free( req->tracker_id_str );
    1191     tr_free( req->url );
    1192     tr_free( req );
    1193 }
    1194 
    1195 static void
    1196 announce_request_delegate( tr_announcer               * announcer,
     1184    tr_free (data);
     1185}
     1186
     1187static void
     1188announce_request_free (tr_announce_request * req)
     1189{
     1190    tr_free (req->tracker_id_str);
     1191    tr_free (req->url);
     1192    tr_free (req);
     1193}
     1194
     1195static void
     1196announce_request_delegate (tr_announcer               * announcer,
    11971197                           tr_announce_request        * request,
    11981198                           tr_announce_response_func  * callback,
    1199                            void                       * callback_data )
     1199                           void                       * callback_data)
    12001200{
    12011201    tr_session * session = announcer->session;
    12021202
    1203     if( !memcmp( request->url, "http", 4 ) )
    1204         tr_tracker_http_announce( session, request, callback, callback_data );
    1205     else if( !memcmp( request->url, "udp://", 6 ) )
    1206         tr_tracker_udp_announce( session, request, callback, callback_data );
     1203    if (!memcmp (request->url, "http", 4))
     1204        tr_tracker_http_announce (session, request, callback, callback_data);
     1205    else if (!memcmp (request->url, "udp://", 6))
     1206        tr_tracker_udp_announce (session, request, callback, callback_data);
    12071207    else
    1208         tr_err( "Unsupported url: %s", request->url );
    1209 
    1210     announce_request_free( request );
    1211 }
    1212 
    1213 static void
    1214 tierAnnounce( tr_announcer * announcer, tr_tier * tier )
     1208        tr_err ("Unsupported url: %s", request->url);
     1209
     1210    announce_request_free (request);
     1211}
     1212
     1213static void
     1214tierAnnounce (tr_announcer * announcer, tr_tier * tier)
    12151215{
    12161216    tr_announce_event announce_event;
     
    12181218    struct announce_data * data;
    12191219    const tr_torrent * tor = tier->tor;
    1220     const time_t now = tr_time( );
    1221 
    1222     assert( !tier->isAnnouncing );
    1223     assert( tier->announce_event_count > 0 );
    1224 
    1225     announce_event = tier_announce_event_pull( tier );
    1226     req = announce_request_new( announcer, tor, tier, announce_event );
    1227 
    1228     data = tr_new0( struct announce_data, 1 );
     1220    const time_t now = tr_time ();
     1221
     1222    assert (!tier->isAnnouncing);
     1223    assert (tier->announce_event_count > 0);
     1224
     1225    announce_event = tier_announce_event_pull (tier);
     1226    req = announce_request_new (announcer, tor, tier, announce_event);
     1227
     1228    data = tr_new0 (struct announce_data, 1);
    12291229    data->session = announcer->session;
    12301230    data->tierId = tier->key;
     
    12371237    --announcer->slotsAvailable;
    12381238
    1239     announce_request_delegate( announcer, req, on_announce_done, data );
     1239    announce_request_delegate (announcer, req, on_announce_done, data);
    12401240}
    12411241
     
    12471247
    12481248static void
    1249 on_scrape_error( tr_session * session, tr_tier * tier, const char * errmsg )
     1249on_scrape_error (tr_session * session, tr_tier * tier, const char * errmsg)
    12501250{
    12511251    int interval;
    12521252
    12531253    /* increment the error count */
    1254     if( tier->currentTracker != NULL )
     1254    if (tier->currentTracker != NULL)
    12551255        ++tier->currentTracker->consecutiveFailures;
    12561256
    12571257    /* set the error message */
    1258     dbgmsg( tier, "Scrape error: %s", errmsg );
    1259     tr_torinf( tier->tor, "Scrape error: %s", errmsg );
    1260     tr_strlcpy( tier->lastScrapeStr, errmsg, sizeof( tier->lastScrapeStr ) );
     1258    dbgmsg (tier, "Scrape error: %s", errmsg);
     1259    tr_torinf (tier->tor, "Scrape error: %s", errmsg);
     1260    tr_strlcpy (tier->lastScrapeStr, errmsg, sizeof (tier->lastScrapeStr));
    12611261
    12621262    /* switch to the next tracker */
    1263     tierIncrementTracker( tier );
     1263    tierIncrementTracker (tier);
    12641264
    12651265    /* schedule a rescrape */
    1266     interval = getRetryInterval( tier->currentTracker );
    1267     dbgmsg( tier, "Retrying scrape in %zu seconds.", (size_t)interval );
    1268     tr_torinf( tier->tor, "Retrying scrape in %zu seconds.", (size_t)interval );
     1266    interval = getRetryInterval (tier->currentTracker);
     1267    dbgmsg (tier, "Retrying scrape in %zu seconds.", (size_t)interval);
     1268    tr_torinf (tier->tor, "Retrying scrape in %zu seconds.", (size_t)interval);
    12691269    tier->lastScrapeSucceeded = false;
    1270     tier->scrapeAt = get_next_scrape_time( session, tier, interval );
     1270    tier->scrapeAt = get_next_scrape_time (session, tier, interval);
    12711271}
    12721272
    12731273static tr_tier *
    1274 find_tier( tr_torrent * tor, const char * scrape )
     1274find_tier (tr_torrent * tor, const char * scrape)
    12751275{
    12761276    int i;
    12771277    struct tr_torrent_tiers * tt = tor->tiers;
    12781278
    1279     for( i=0; tt && i<tt->tier_count; ++i ) {
     1279    for (i=0; tt && i<tt->tier_count; ++i) {
    12801280        const tr_tracker * const tracker = tt->tiers[i].currentTracker;
    1281         if( tracker && !tr_strcmp0( scrape, tracker->scrape ) )
     1281        if (tracker && !tr_strcmp0 (scrape, tracker->scrape))
    12821282            return &tt->tiers[i];
    12831283    }
     
    12871287
    12881288static void
    1289 on_scrape_done( const tr_scrape_response * response, void * vsession )
     1289on_scrape_done (const tr_scrape_response * response, void * vsession)
    12901290{
    12911291    int i;
    1292     const time_t now = tr_time( );
     1292    const time_t now = tr_time ();
    12931293    tr_session * session = vsession;
    12941294    tr_announcer * announcer = session->announcer;
    12951295
    1296     for( i=0; i<response->row_count; ++i )
     1296    for (i=0; i<response->row_count; ++i)
    12971297    {
    12981298        const struct tr_scrape_response_row * row = &response->rows[i];
    1299         tr_torrent * tor = tr_torrentFindFromHash( session, row->info_hash );
    1300 
    1301         if( tor != NULL )
     1299        tr_torrent * tor = tr_torrentFindFromHash (session, row->info_hash);
     1300
     1301        if (tor != NULL)
    13021302        {
    1303             tr_tier * tier = find_tier( tor, response->url );
    1304 
    1305             if( tier != NULL )
     1303            tr_tier * tier = find_tier (tor, response->url);
     1304
     1305            if (tier != NULL)
    13061306            {
    1307                 dbgmsg( tier, "scraped url:%s -- "
     1307                dbgmsg (tier, "scraped url:%s -- "
    13081308                              "did_connect:%d "
    13091309                              "did_timeout:%d "
     
    13151315                              "err:%s ",
    13161316                              response->url,
    1317                               (int)response->did_connect,
    1318                               (int)response->did_timeout,
     1317                            (int)response->did_connect,
     1318                            (int)response->did_timeout,
    13191319                              row->seeders,
    13201320                              row->leechers,
     
    13221322                              row->downloaders,
    13231323                              response->min_request_interval,
    1324                               response->errmsg ? response->errmsg : "none" );
     1324                              response->errmsg ? response->errmsg : "none");
    13251325
    13261326                tier->isScraping = false;
     
    13291329                tier->lastScrapeTimedOut = response->did_timeout;
    13301330
    1331                 if( !response->did_connect )
     1331                if (!response->did_connect)
    13321332                {
    1333                     on_scrape_error( session, tier, _( "Could not connect to tracker" ) );
     1333                    on_scrape_error (session, tier, _ ("Could not connect to tracker"));
    13341334                }
    1335                 else if( response->did_timeout )
     1335                else if (response->did_timeout)
    13361336                {
    1337                     on_scrape_error( session, tier, _( "Tracker did not respond" ) );
     1337                    on_scrape_error (session, tier, _ ("Tracker did not respond"));
    13381338                }
    1339                 else if( response->errmsg )
     1339                else if (response->errmsg)
    13401340                {
    1341                     on_scrape_error( session, tier, response->errmsg );
     1341                    on_scrape_error (session, tier, response->errmsg);
    13421342                }
    13431343                else
     
    13461346
    13471347                    tier->lastScrapeSucceeded = true;
    1348                     tier->scrapeIntervalSec = MAX( DEFAULT_SCRAPE_INTERVAL_SEC,
    1349                                                    response->min_request_interval );
    1350                     tier->scrapeAt = get_next_scrape_time( session, tier, tier->scrapeIntervalSec );
    1351                     tr_tordbg( tier->tor, "Scrape successful. Rescraping in %d seconds.",
    1352                                tier->scrapeIntervalSec );
    1353 
    1354                     if(( tracker = tier->currentTracker ))
     1348                    tier->scrapeIntervalSec = MAX (DEFAULT_SCRAPE_INTERVAL_SEC,
     1349                                                   response->min_request_interval);
     1350                    tier->scrapeAt = get_next_scrape_time (session, tier, tier->scrapeIntervalSec);
     1351                    tr_tordbg (tier->tor, "Scrape successful. Rescraping in %d seconds.",
     1352                               tier->scrapeIntervalSec);
     1353
     1354                    if ((tracker = tier->currentTracker))
    13551355                    {
    1356                         if( row->seeders >= 0 )
     1356                        if (row->seeders >= 0)
    13571357                            tracker->seederCount = row->seeders;
    1358                         if( row->leechers >= 0 )
     1358                        if (row->leechers >= 0)
    13591359                            tracker->leecherCount = row->leechers;
    1360                         if( row->downloads >= 0 )
     1360                        if (row->downloads >= 0)
    13611361                            tracker->downloadCount = row->downloads;
    13621362                        tracker->downloaderCount = row->downloaders;
     
    13681368    }
    13691369
    1370     if( announcer )
     1370    if (announcer)
    13711371        ++announcer->slotsAvailable;
    13721372}
    13731373
    13741374static void
    1375 scrape_request_delegate( tr_announcer             * announcer,
     1375scrape_request_delegate (tr_announcer             * announcer,
    13761376                         const tr_scrape_request  * request,
    13771377                         tr_scrape_response_func  * callback,
    1378                          void                     * callback_data )
     1378                         void                     * callback_data)
    13791379{
    13801380    tr_session * session = announcer->session;
    13811381
    1382     if( !memcmp( request->url, "http", 4 ) )
    1383         tr_tracker_http_scrape( session, request, callback, callback_data );
    1384     else if( !memcmp( request->url, "udp://", 6 ) )
    1385         tr_tracker_udp_scrape( session, request, callback, callback_data );
     1382    if (!memcmp (request->url, "http", 4))
     1383        tr_tracker_http_scrape (session, request, callback, callback_data);
     1384    else if (!memcmp (request->url, "udp://", 6))
     1385        tr_tracker_udp_scrape (session, request, callback, callback_data);
    13861386    else
    1387         tr_err( "Unsupported url: %s", request->url );
    1388 }
    1389 
    1390 static void
    1391 multiscrape( tr_announcer * announcer, tr_ptrArray * tiers )
     1387        tr_err ("Unsupported url: %s", request->url);
     1388}
     1389
     1390static void
     1391multiscrape (tr_announcer * announcer, tr_ptrArray * tiers)
    13921392{
    13931393    int i;
    13941394    int request_count = 0;
    1395     const time_t now = tr_time( );
    1396     const int tier_count = tr_ptrArraySize( tiers );
    1397     const int max_request_count = MIN( announcer->slotsAvailable, tier_count );
    1398     tr_scrape_request * requests = tr_new0( tr_scrape_request, max_request_count );
     1395    const time_t now = tr_time ();
     1396    const int tier_count = tr_ptrArraySize (tiers);
     1397    const int max_request_count = MIN (announcer->slotsAvailable, tier_count);
     1398    tr_scrape_request * requests = tr_new0 (tr_scrape_request, max_request_count);
    13991399
    14001400    /* batch as many info_hashes into a request as we can */
    1401     for( i=0; i<tier_count; ++i )
     1401    for (i=0; i<tier_count; ++i)
    14021402    {
    14031403        int j;
    1404         tr_tier * tier = tr_ptrArrayNth( tiers, i );
     1404        tr_tier * tier = tr_ptrArrayNth (tiers, i);
    14051405        char * url = tier->currentTracker->scrape;
    14061406        const uint8_t * hash = tier->tor->info.hash;
    14071407
    14081408        /* if there's a request with this scrape URL and a free slot, use it */
    1409         for( j=0; j<request_count; ++j )
     1409        for (j=0; j<request_count; ++j)
    14101410        {
    14111411            tr_scrape_request * req = &requests[j];
    14121412
    1413             if( req->info_hash_count >= TR_MULTISCRAPE_MAX )
     1413            if (req->info_hash_count >= TR_MULTISCRAPE_MAX)
    14141414                continue;
    1415             if( tr_strcmp0( req->url, url ) )
     1415            if (tr_strcmp0 (req->url, url))
    14161416                continue;
    14171417
    1418             memcpy( req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH );
     1418            memcpy (req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH);
    14191419            tier->isScraping = true;
    14201420            tier->lastScrapeStartTime = now;
     
    14231423
    14241424        /* otherwise, if there's room for another request, build a new one */
    1425         if( ( j==request_count ) && ( request_count < max_request_count ) )
     1425        if ((j==request_count) && (request_count < max_request_count))
    14261426        {
    14271427            tr_scrape_request * req = &requests[request_count++];
    14281428            req->url = url;
    1429             tier_build_log_name( tier, req->log_name, sizeof( req->log_name ) );
    1430 
    1431             memcpy( req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH );
     1429            tier_build_log_name (tier, req->log_name, sizeof (req->log_name));
     1430
     1431            memcpy (req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH);
    14321432            tier->isScraping = true;
    14331433            tier->lastScrapeStartTime = now;
     
    14361436
    14371437    /* send the requests we just built */
    1438     for( i=0; i<request_count; ++i )
    1439         scrape_request_delegate( announcer, &requests[i], on_scrape_done, announcer->session );
     1438    for (i=0; i<request_count; ++i)
     1439        scrape_request_delegate (announcer, &requests[i], on_scrape_done, announcer->session);
    14401440
    14411441    /* cleanup */
    1442     tr_free( requests );
    1443 }
    1444 
    1445 static void
    1446 flushCloseMessages( tr_announcer * announcer )
     1442    tr_free (requests);
     1443}
     1444
     1445static void
     1446flushCloseMessages (tr_announcer * announcer)
    14471447{
    14481448    int i;
    1449     const int n = tr_ptrArraySize( &announcer->stops );
    1450 
    1451     for( i=0; i<n; ++i )
    1452         announce_request_delegate( announcer, tr_ptrArrayNth( &announcer->stops, i ), NULL, NULL );
    1453 
    1454     tr_ptrArrayClear( &announcer->stops );
     1449    const int n = tr_ptrArraySize (&announcer->stops);
     1450
     1451    for (i=0; i<n; ++i)
     1452        announce_request_delegate (announcer, tr_ptrArrayNth (&announcer->stops, i), NULL, NULL);
     1453
     1454    tr_ptrArrayClear (&announcer->stops);
    14551455}
    14561456
    14571457static bool
    1458 tierNeedsToAnnounce( const tr_tier * tier, const time_t now )
     1458tierNeedsToAnnounce (const tr_tier * tier, const time_t now)
    14591459{
    14601460    return !tier->isAnnouncing
    14611461        && !tier->isScraping
    1462         && ( tier->announceAt != 0 )
    1463         && ( tier->announceAt <= now )
    1464         && ( tier->announce_event_count > 0 );
     1462        && (tier->announceAt != 0)
     1463        && (tier->announceAt <= now)
     1464        && (tier->announce_event_count > 0);
    14651465}
    14661466
    14671467static bool
    1468 tierNeedsToScrape( const tr_tier * tier, const time_t now )
    1469 {
    1470     return ( !tier->isScraping )
    1471         && ( tier->scrapeAt != 0 )
    1472         && ( tier->scrapeAt <= now )
    1473         && ( tier->currentTracker != NULL )
    1474         && ( tier->currentTracker->scrape != NULL );
     1468tierNeedsToScrape (const tr_tier * tier, const time_t now)
     1469{
     1470    return (!tier->isScraping)
     1471        && (tier->scrapeAt != 0)
     1472        && (tier->scrapeAt <= now)
     1473        && (tier->currentTracker != NULL)
     1474        && (tier->currentTracker->scrape != NULL);
    14751475}
    14761476
    14771477static int
    1478 compareTiers( const void * va, const void * vb )
     1478compareTiers (const void * va, const void * vb)
    14791479{
    14801480    int ret;
    1481     const tr_tier * a = *(const tr_tier**)va;
    1482     const tr_tier * b = *(const tr_tier**)vb;
     1481    const tr_tier * a = * (const tr_tier**)va;
     1482    const tr_tier * b = * (const tr_tier**)vb;
    14831483
    14841484    /* primary key: larger stats come before smaller */
    1485     ret = compareTransfer( a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN],
    1486                            b->byteCounts[TR_ANN_UP], b->byteCounts[TR_ANN_DOWN] );
     1485    ret = compareTransfer (a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN],
     1486                           b->byteCounts[TR_ANN_UP], b->byteCounts[TR_ANN_DOWN]);
    14871487
    14881488    /* secondary key: announcements that have been waiting longer go first */
    1489     if( !ret && ( a->announceAt != b->announceAt ) )
     1489    if (!ret && (a->announceAt != b->announceAt))
    14901490        ret = a->announceAt < b->announceAt ? -1 : 1;
    14911491
     
    14941494
    14951495static void
    1496 announceMore( tr_announcer * announcer )
     1496announceMore (tr_announcer * announcer)
    14971497{
    14981498    int i;
     
    15011501    tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
    15021502    tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
    1503     const time_t now = tr_time( );
    1504 
    1505     dbgmsg( NULL, "announceMore: slotsAvailable is %d", announcer->slotsAvailable );
    1506 
    1507     if( announcer->slotsAvailable < 1 )
     1503    const time_t now = tr_time ();
     1504
     1505    dbgmsg (NULL, "announceMore: slotsAvailable is %d", announcer->slotsAvailable);
     1506
     1507    if (announcer->slotsAvailable < 1)
    15081508        return;
    15091509
    15101510    /* build a list of tiers that need to be announced */
    15111511    tor = NULL;
    1512     while(( tor = tr_torrentNext( announcer->session, tor ))) {
     1512    while ((tor = tr_torrentNext (announcer->session, tor))) {
    15131513        struct tr_torrent_tiers * tt = tor->tiers;
    1514         for( i=0; tt && i<tt->tier_count; ++i ) {
     1514        for (i=0; tt && i<tt->tier_count; ++i) {
    15151515            tr_tier * tier = &tt->tiers[i];
    1516             if( tierNeedsToAnnounce( tier, now ) )
    1517                 tr_ptrArrayAppend( &announceMe, tier );
    1518             else if( tierNeedsToScrape( tier, now ) )
    1519                 tr_ptrArrayAppend( &scrapeMe, tier );
     1516            if (tierNeedsToAnnounce (tier, now))
     1517                tr_ptrArrayAppend (&announceMe, tier);
     1518            else if (tierNeedsToScrape (tier, now))
     1519                tr_ptrArrayAppend (&scrapeMe, tier);
    15201520        }
    15211521    }
    15221522
    15231523    /* if there are more tiers than slots available, prioritize */
    1524     n = tr_ptrArraySize( &announceMe );
    1525     if( n > announcer->slotsAvailable )
    1526         qsort( tr_ptrArrayBase(&announceMe), n, sizeof(tr_tier*), compareTiers );
     1524    n = tr_ptrArraySize (&announceMe);
     1525    if (n > announcer->slotsAvailable)
     1526        qsort (tr_ptrArrayBase (&announceMe), n, sizeof (tr_tier*), compareTiers);
    15271527
    15281528    /* announce some */
    1529     n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
    1530     for( i=0; i<n; ++i ) {
    1531         tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
    1532         tr_tordbg( tier->tor, "%s", "Announcing to tracker" );
    1533         dbgmsg( tier, "announcing tier %d of %d", i, n );
    1534         tierAnnounce( announcer, tier );
     1529    n = MIN (tr_ptrArraySize (&announceMe), announcer->slotsAvailable);
     1530    for (i=0; i<n; ++i) {
     1531        tr_tier * tier = tr_ptrArrayNth (&announceMe, i);
     1532        tr_tordbg (tier->tor, "%s", "Announcing to tracker");
     1533        dbgmsg (tier, "announcing tier %d of %d", i, n);
     1534        tierAnnounce (announcer, tier);
    15351535    }
    15361536
    15371537    /* scrape some */
    1538     multiscrape( announcer, &scrapeMe );
     1538    multiscrape (announcer, &scrapeMe);
    15391539
    15401540    /* cleanup */
    1541     tr_ptrArrayDestruct( &scrapeMe, NULL );
    1542     tr_ptrArrayDestruct( &announceMe, NULL );
    1543 }
    1544 
    1545 static void
    1546 onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer )
     1541    tr_ptrArrayDestruct (&scrapeMe, NULL);
     1542    tr_ptrArrayDestruct (&announceMe, NULL);
     1543}
     1544
     1545static void
     1546onUpkeepTimer (int foo UNUSED, short bar UNUSED, void * vannouncer)
    15471547{
    15481548    tr_announcer * announcer = vannouncer;
    15491549    tr_session * session = announcer->session;
    15501550    const bool is_closing = session->isClosed;
    1551     const time_t now = tr_time( );
    1552 
    1553     tr_sessionLock( session );
     1551    const time_t now = tr_time ();
     1552
     1553    tr_sessionLock (session);
    15541554
    15551555    /* maybe send out some "stopped" messages for closed torrents */
    1556     flushCloseMessages( announcer );
     1556    flushCloseMessages (announcer);
    15571557
    15581558    /* maybe send out some announcements to trackers */
    1559     if( !is_closing )
    1560         announceMore( announcer );
     1559    if (!is_closing)
     1560        announceMore (announcer);
    15611561
    15621562    /* TAU upkeep */
    1563     if( announcer->tauUpkeepAt <= now ) {
     1563    if (announcer->tauUpkeepAt <= now) {
    15641564        announcer->tauUpkeepAt = now + TAU_UPKEEP_INTERVAL_SECS;
    1565         tr_tracker_udp_upkeep( session );
     1565        tr_tracker_udp_upkeep (session);
    15661566    }
    15671567
    15681568    /* set up the next timer */
    1569     tr_timerAdd( announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
    1570 
    1571     tr_sessionUnlock( session );
     1569    tr_timerAdd (announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0);
     1570
     1571    tr_sessionUnlock (session);
    15721572}
    15731573
     
    15771577
    15781578tr_tracker_stat *
    1579 tr_announcerStats( const tr_torrent * torrent, int * setmeTrackerCount )
     1579tr_announcerStats (const tr_torrent * torrent, int * setmeTrackerCount)
    15801580{
    15811581    int i;
     
    15831583    tr_tracker_stat * ret;
    15841584    struct tr_torrent_tiers * tt;
    1585     const time_t now = tr_time( );
    1586 
    1587     assert( tr_isTorrent( torrent ) );
    1588     assert( tr_torrentIsLocked( torrent ) );
     1585    const time_t now = tr_time ();
     1586
     1587    assert (tr_isTorrent (torrent));
     1588    assert (tr_torrentIsLocked (torrent));
    15891589
    15901590    tt = torrent->tiers;
     
    15921592    /* alloc the stats */
    15931593    *setmeTrackerCount = tt->tracker_count;
    1594     ret = tr_new0( tr_tracker_stat, tt->tracker_count );
     1594    ret = tr_new0 (tr_tracker_stat, tt->tracker_count);
    15951595
    15961596    /* populate the stats */
    1597     for( i=0; i<tt->tier_count; ++i )
     1597    for (i=0; i<tt->tier_count; ++i)
    15981598    {
    15991599        int j;
    16001600        const tr_tier * const tier = &tt->tiers[i];
    1601         for( j=0; j<tier->tracker_count; ++j )
     1601        for (j=0; j<tier->tracker_count; ++j)
    16021602        {
    16031603            const tr_tracker * const tracker = &tier->trackers[j];
     
    16051605
    16061606            st->id = tracker->id;
    1607             tr_strlcpy( st->host, tracker->key, sizeof( st->host ) );
    1608             tr_strlcpy( st->announce, tracker->announce, sizeof( st->announce ) );
     1607            tr_strlcpy (st->host, tracker->key, sizeof (st->host));
     1608            tr_strlcpy (st->announce, tracker->announce, sizeof (st->announce));
    16091609            st->tier = i;
    16101610            st->isBackup = tracker != tier->currentTracker;
    16111611            st->lastScrapeStartTime = tier->lastScrapeStartTime;
    1612             if( tracker->scrape )
    1613                 tr_strlcpy( st->scrape, tracker->scrape, sizeof( st->scrape ) );
     1612            if (tracker->scrape)
     1613                tr_strlcpy (st->scrape, tracker->scrape, sizeof (st->scrape));
    16141614            else
    16151615                st->scrape[0] = '\0';
     
    16191619            st->downloadCount = tracker->downloadCount;
    16201620
    1621             if( st->isBackup )
     1621            if (st->isBackup)
    16221622            {
    16231623                st->scrapeState = TR_TRACKER_INACTIVE;
     
    16281628            else
    16291629            {
    1630                 if(( st->hasScraped = tier->lastScrapeTime != 0 )) {
     1630                if ((st->hasScraped = tier->lastScrapeTime != 0)) {
    16311631                    st->lastScrapeTime = tier->lastScrapeTime;
    16321632                    st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
    16331633                    st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
    1634                     tr_strlcpy( st->lastScrapeResult, tier->lastScrapeStr,
    1635                                 sizeof( st->lastScrapeResult ) );
     1634                    tr_strlcpy (st->lastScrapeResult, tier->lastScrapeStr,
     1635                                sizeof (st->lastScrapeResult));
    16361636                }
    16371637
    1638                 if( tier->isScraping )
     1638                if (tier->isScraping)
    16391639                    st->scrapeState = TR_TRACKER_ACTIVE;
    1640                 else if( !tier->scrapeAt )
     1640                else if (!tier->scrapeAt)
    16411641                    st->scrapeState = TR_TRACKER_INACTIVE;
    1642                 else if( tier->scrapeAt > now )
     1642                else if (tier->scrapeAt > now)
    16431643                {
    16441644                    st->scrapeState = TR_TRACKER_WAITING;
     
    16501650                st->lastAnnounceStartTime = tier->lastAnnounceStartTime;
    16511651
    1652                 if(( st->hasAnnounced = tier->lastAnnounceTime != 0 )) {
     1652                if ((st->hasAnnounced = tier->lastAnnounceTime != 0)) {
    16531653                    st->lastAnnounceTime = tier->lastAnnounceTime;
    1654                     tr_strlcpy( st->lastAnnounceResult, tier->lastAnnounceStr,
    1655                                 sizeof( st->lastAnnounceResult ) );
     1654                    tr_strlcpy (st->lastAnnounceResult, tier->lastAnnounceStr,
     1655                                sizeof (st->lastAnnounceResult));
    16561656                    st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
    16571657                    st->lastAnnounceTimedOut = tier->lastAnnounceTimedOut;
     
    16591659                }
    16601660
    1661                 if( tier->isAnnouncing )
     1661                if (tier->isAnnouncing)
    16621662                    st->announceState = TR_TRACKER_ACTIVE;
    1663                 else if( !torrent->isRunning || !tier->announceAt )
     1663                else if (!torrent->isRunning || !tier->announceAt)
    16641664                    st->announceState = TR_TRACKER_INACTIVE;
    1665                 else if( tier->announceAt > now )
     1665                else if (tier->announceAt > now)
    16661666                {
    16671667                    st->announceState = TR_TRACKER_WAITING;
     
    16781678
    16791679void
    1680 tr_announcerStatsFree( tr_tracker_stat * trackers,
    1681                        int trackerCount UNUSED )
    1682 {
    1683     tr_free( trackers );
     1680tr_announcerStatsFree (tr_tracker_stat * trackers,
     1681                       int trackerCount UNUSED)
     1682{
     1683    tr_free (trackers);
    16841684}
    16851685
     
    16891689
    16901690static void
    1691 copy_tier_attributes_impl( struct tr_tier * tgt, int trackerIndex, const tr_tier * src )
     1691copy_tier_attributes_impl (struct tr_tier * tgt, int trackerIndex, const tr_tier * src)
    16921692{
    16931693    const tr_tier keep = *tgt;
    16941694
    16951695    /* sanity clause */
    1696     assert( trackerIndex < tgt->tracker_count );
    1697     assert( !tr_strcmp0( tgt->trackers[trackerIndex].announce, src->currentTracker->announce ) );
     1696    assert (trackerIndex < tgt->tracker_count);
     1697    assert (!tr_strcmp0 (tgt->trackers[trackerIndex].announce, src->currentTracker->announce));
    16981698
    16991699    /* bitwise copy will handle most of tr_tier's fields... */
     
    17041704    tgt->trackers = keep.trackers;
    17051705    tgt->tracker_count = keep.tracker_count;
    1706     tgt->announce_events = tr_memdup( src->announce_events, sizeof( tr_announce_event ) * src->announce_event_count );
     1706    tgt->announce_events = tr_memdup (src->announce_events, sizeof (tr_announce_event) * src->announce_event_count);
    17071707    tgt->announce_event_count = src->announce_event_count;
    17081708    tgt->announce_event_alloc = src->announce_event_count;
     
    17161716
    17171717static void
    1718 copy_tier_attributes( struct tr_torrent_tiers * tt, const tr_tier * src )
     1718copy_tier_attributes (struct tr_torrent_tiers * tt, const tr_tier * src)
    17191719{
    17201720    int i, j;
     
    17221722
    17231723    /* find a tier (if any) which has a match for src->currentTracker */
    1724     for( i=0; !found && i<tt->tier_count; ++i )
    1725         for( j=0; !found && j<tt->tiers[i].tracker_count; ++j )
    1726             if(( found = !tr_strcmp0( src->currentTracker->announce, tt->tiers[i].trackers[j].announce )))
    1727                 copy_tier_attributes_impl( &tt->tiers[i], j, src );
     1724    for (i=0; !found && i<tt->tier_count; ++i)
     1725        for (j=0; !found && j<tt->tiers[i].tracker_count; ++j)
     1726            if ((found = !tr_strcmp0 (src->currentTracker->announce, tt->tiers[i].trackers[j].announce)))
     1727                copy_tier_attributes_impl (&tt->tiers[i], j, src);
    17281728}
    17291729
    17301730void
    1731 tr_announcerResetTorrent( tr_announcer * announcer UNUSED, tr_torrent * tor )
     1731tr_announcerResetTorrent (tr_announcer * announcer UNUSED, tr_torrent * tor)
    17321732{
    17331733    int i;
    1734     const time_t now = tr_time( );
     1734    const time_t now = tr_time ();
    17351735    struct tr_torrent_tiers * tt = tor->tiers;
    17361736    tr_torrent_tiers old = *tt;
    17371737
    1738     assert( tt != NULL );
     1738    assert (tt != NULL);
    17391739
    17401740    /* remove the old tiers / trackers */
     
    17451745
    17461746    /* create the new tiers / trackers */
    1747     addTorrentToTier( tt, tor );
     1747    addTorrentToTier (tt, tor);
    17481748
    17491749    /* copy the old tiers' states into their replacements */
    1750     for( i=0; i<old.tier_count; ++i )
    1751         if( old.tiers[i].currentTracker != NULL )
    1752             copy_tier_attributes( tt, &old.tiers[i] );
     1750    for (i=0; i<old.tier_count; ++i)
     1751        if (old.tiers[i].currentTracker != NULL)
     1752            copy_tier_attributes (tt, &old.tiers[i]);
    17531753
    17541754    /* kickstart any tiers that didn't get started */
    1755     if( tor->isRunning )
    1756         for( i=0; i<tt->tier_count; ++i )
    1757             if( !tt->tiers[i].wasCopied )
    1758                 tier_announce_event_push( &tt->tiers[i], TR_ANNOUNCE_EVENT_STARTED, now );
     1755    if (tor->isRunning)
     1756        for (i=0; i<tt->tier_count; ++i)
     1757            if (!tt->tiers[i].wasCopied)
     1758                tier_announce_event_push (&tt->tiers[i], TR_ANNOUNCE_EVENT_STARTED, now);
    17591759
    17601760    /* cleanup */
    1761     tiersDestruct( &old );
    1762 }
     1761    tiersDestruct (&old);
     1762}
Note: See TracChangeset for help on using the changeset viewer.