Changeset 11750


Ignore:
Timestamp:
Jan 22, 2011, 5:45:54 PM (11 years ago)
Author:
jordan
Message:

(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.

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/announcer.c

    r11730 r11750  
    2222#include "crypto.h"
    2323#include "net.h"
     24#include "peer-mgr.h" /* tr_peerMgrCompactToPex() */
    2425#include "ptrarray.h"
    2526#include "session.h"
     
    632633}
    633634
    634 static int
    635 publishNewPeers( tr_tier * tier, int seeds, int leechers,
    636                  const void * compact, int compactLen )
     635static void
     636publishPeersPex( tr_tier * tier, int seeds, int leechers,
     637                 const tr_pex * pex, int n )
    637638{
    638639    tr_tracker_event e = emptyEvent;
     
    640641    e.messageType = TR_TRACKER_PEERS;
    641642    e.seedProbability = getSeedProbability( seeds, leechers );
    642     e.compact = compact;
    643     e.compactLen = compactLen;
     643    e.pex = pex;
     644    e.pexCount = n;
    644645
    645646    if( tier->tor->tiers->callback != NULL )
    646647        tier->tor->tiers->callback( tier->tor, &e, NULL );
    647 
    648     return compactLen / 6;
    649 }
    650 
    651 static int
    652 publishNewPeersCompact( tr_tier * tier, int seeds, int leechers,
     648}
     649
     650static size_t
     651publishPeersCompact( tr_tier * tier, int seeds, int leechers,
    653652                        const void * compact, int compactLen )
    654653{
    655     int i;
    656     const uint8_t *compactWalk;
    657     uint8_t *array, *walk;
    658     const int peerCount = compactLen / 6;
    659     const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
    660     tr_address addr;
    661     tr_port port;
    662 
    663     addr.type = TR_AF_INET;
    664     memset( &addr.addr, 0, sizeof( addr.addr ) );
    665     array = tr_new( uint8_t, arrayLen );
    666     for ( i=0, walk=array, compactWalk=compact ; i<peerCount ; ++i )
    667     {
    668         memcpy( &addr.addr.addr4, compactWalk, 4 );
    669         memcpy( &port, compactWalk + 4, 2 );
    670 
    671         memcpy( walk, &addr, sizeof( addr ) );
    672         memcpy( walk + sizeof( addr ), &port, 2 );
    673 
    674         walk += sizeof( tr_address ) + 2;
    675         compactWalk += 6;
    676     }
    677 
    678     publishNewPeers( tier, seeds, leechers, array, arrayLen );
    679 
    680     tr_free( array );
    681 
    682     return peerCount;
    683 }
    684 
    685 static int
    686 publishNewPeersCompact6( tr_tier * tier, int seeds, int leechers,
     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,
    687664                         const void * compact, int compactLen )
    688665{
    689     int i;
    690     const uint8_t *compactWalk;
    691     uint8_t *array, *walk;
    692     const int peerCount = compactLen / 18;
    693     const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
    694     tr_address addr;
    695     tr_port port;
    696 
    697     addr.type = TR_AF_INET6;
    698     memset( &addr.addr, 0, sizeof( addr.addr ) );
    699     array = tr_new( uint8_t, arrayLen );
    700     for ( i = 0, walk = array, compactWalk = compact ; i < peerCount ; ++i )
    701     {
    702         memcpy( &addr.addr.addr6, compactWalk, 16 );
    703         memcpy( &port, compactWalk + 16, 2 );
    704         compactWalk += 18;
    705 
    706         memcpy( walk, &addr, sizeof( addr ) );
    707         memcpy( walk + sizeof( addr ), &port, 2 );
    708         walk += sizeof( tr_address ) + 2;
    709     }
    710 
    711     publishNewPeers( tier, seeds, leechers, array, arrayLen );
    712     tr_free( array );
    713 
    714     return peerCount;
     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;
    715711}
    716712
     
    11641160}
    11651161
    1166 static uint8_t *
    1167 parseOldPeers( tr_benc * bePeers, size_t * byteCount )
    1168 {
    1169     int       i;
    1170     uint8_t * array, *walk;
    1171     const int peerCount = bePeers->val.l.count;
    1172 
    1173     assert( tr_bencIsList( bePeers ) );
    1174 
    1175     array = tr_new( uint8_t, peerCount * ( sizeof( tr_address ) + 2 ) );
    1176 
    1177     for( i = 0, walk = array; i < peerCount; ++i )
    1178     {
    1179         const char * s;
    1180         int64_t      itmp;
    1181         tr_address   addr;
    1182         tr_port      port;
    1183         tr_benc    * peer = &bePeers->val.l.vals[i];
    1184 
    1185         if( tr_bencDictFindStr( peer, "ip", &s ) )
    1186             if( tr_pton( s, &addr ) == NULL )
    1187                 continue;
    1188 
    1189         if( !tr_bencDictFindInt( peer, "port", &itmp )
    1190                 || itmp < 0
    1191                 || itmp > USHRT_MAX )
    1192             continue;
    1193 
    1194         memcpy( walk, &addr, sizeof( tr_address ) );
    1195         port = htons( (uint16_t)itmp );
    1196         memcpy( walk + sizeof( tr_address ), &port, 2 );
    1197         walk += sizeof( tr_address ) + 2;
    1198     }
    1199 
    1200     *byteCount = peerCount * sizeof( tr_address ) + 2;
    1201     return array;
    1202 }
    12031162
    12041163static tr_bool
     
    12931252            const int seeders = tier->currentTracker->seederCount;
    12941253            const int leechers = tier->currentTracker->leecherCount;
    1295             peerCount += publishNewPeersCompact( tier, seeders, leechers, raw, rawlen );
     1254            peerCount += publishPeersCompact( tier, seeders, leechers, raw, rawlen );
    12961255            gotPeers = TRUE;
    12971256        }
     
    13011260            const int seeders = tier->currentTracker->seederCount;
    13021261            const int leechers = tier->currentTracker->leecherCount;
    1303             size_t byteCount = 0;
    1304             uint8_t * array = parseOldPeers( tmp, &byteCount );
    1305             peerCount += publishNewPeers( tier, seeders, leechers, array, byteCount );
     1262            peerCount += publishPeersDict( tier, seeders, leechers, tmp );
    13061263            gotPeers = TRUE;
    1307             tr_free( array );
    13081264        }
    13091265
     
    13131269            const int seeders = tier->currentTracker->seederCount;
    13141270            const int leechers = tier->currentTracker->leecherCount;
    1315             peerCount += publishNewPeersCompact6( tier, seeders, leechers, raw, rawlen );
     1271            peerCount += publishPeersCompact6( tier, seeders, leechers, raw, rawlen );
    13161272            gotPeers = TRUE;
    13171273        }
  • trunk/libtransmission/announcer.h

    r11709 r11750  
    3636TrackerEventType;
    3737
     38struct tr_pex;
     39
    3840/** @brief Notification object to tell listeners about announce or scrape occurences */
    3941typedef struct
     
    4749
    4850    /* for TR_TRACKER_PEERS */
    49     const uint8_t *  compact;
    50     int              compactLen;
     51    const struct tr_pex * pex;
     52    size_t pexCount;
    5153
    5254    /* [0...100] for probability a peer is a seed. calculated by the leecher/seeder ratio */
  • trunk/libtransmission/torrent.c

    r11746 r11750  
    494494        case TR_TRACKER_PEERS:
    495495        {
    496             size_t i, n;
     496            size_t i;
    497497            const int8_t seedProbability = event->seedProbability;
    498498            const tr_bool allAreSeeds = seedProbability == 100;
    499             tr_pex * pex = tr_peerMgrArrayToPex( event->compact,
    500                                                  event->compactLen, &n );
     499
    501500             if( allAreSeeds )
    502                 tr_tordbg( tor, "Got %d seeds from tracker", (int)n );
     501                tr_tordbg( tor, "Got %zu seeds from tracker", event->pexCount );
    503502            else
    504                 tr_tordbg( tor, "Got %d peers from tracker", (int)n );
    505 
    506             for( i = 0; i < n; ++i )
    507                 tr_peerMgrAddPex( tor, TR_PEER_FROM_TRACKER, pex+i, seedProbability );
     503                tr_tordbg( tor, "Got %zu peers from tracker", event->pexCount );
     504
     505            for( i = 0; i < event->pexCount; ++i )
     506                tr_peerMgrAddPex( tor, TR_PEER_FROM_TRACKER, &event->pex[i], seedProbability );
    508507
    509508            if( allAreSeeds && tr_torrentIsPrivate( tor ) )
    510509                tr_peerMgrMarkAllAsSeeds( tor );
    511510
    512             tr_free( pex );
    513511            break;
    514512        }
Note: See TracChangeset for help on using the changeset viewer.