Changeset 5647
- Timestamp:
- Apr 19, 2008, 3:07:59 PM (14 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r5632 r5647 99 99 MAX_UNCHOKED_PEERS = 12, 100 100 101 /* corresponds to ut_pex's added.f flags */102 ADDED_F_ENCRYPTION_FLAG = 1,103 104 /* corresponds to ut_pex's added.f flags */105 ADDED_F_SEED_FLAG = 2,106 107 101 /* number of bad pieces a peer is allowed to send before we ban them */ 108 102 MAX_BAD_PIECES_PER_PEER = 3, … … 1169 1163 } 1170 1164 1171 void 1172 tr_peerMgrAddPeers( tr_peerMgr * manager, 1173 const uint8_t * torrentHash, 1174 uint8_t from, 1175 const uint8_t * peerCompact, 1176 int peerCount ) 1177 { 1178 int i; 1179 const uint8_t * walk = peerCompact; 1180 Torrent * t; 1181 1182 managerLock( manager ); 1183 1184 t = getExistingTorrent( manager, torrentHash ); 1185 for( i=0; t!=NULL && i<peerCount; ++i ) 1186 { 1187 struct in_addr addr; 1188 uint16_t port; 1189 memcpy( &addr, walk, 4 ); walk += 4; 1190 memcpy( &port, walk, 2 ); walk += 2; 1191 maybeEnsureAtomExists( t, &addr, port, 0, from ); 1192 } 1193 1194 managerUnlock( manager ); 1165 tr_pex * 1166 tr_peerMgrCompactToPex( const void * compact, 1167 size_t compactLen, 1168 const char * added_f, 1169 size_t * pexCount ) 1170 { 1171 size_t i; 1172 size_t n = compactLen / 6; 1173 const uint8_t * walk = compact; 1174 tr_pex * pex = tr_new0( tr_pex, n ); 1175 for( i=0; i<n; ++i ) { 1176 memcpy( &pex[i].in_addr, walk, 4 ); walk += 4; 1177 memcpy( &pex[i].port, walk, 2 ); walk += 2; 1178 if( added_f ) 1179 pex[i].flags = added_f[i]; 1180 } 1181 *pexCount = n; 1182 return pex; 1195 1183 } 1196 1184 -
trunk/libtransmission/peer-mgr.h
r5632 r5647 28 28 typedef struct tr_peerMgr tr_peerMgr; 29 29 30 enum 31 { 32 /* corresponds to ut_pex's added.f flags */ 33 ADDED_F_ENCRYPTION_FLAG = 1, 34 35 /* corresponds to ut_pex's added.f flags */ 36 ADDED_F_SEED_FLAG = 2, 37 }; 38 30 39 typedef struct tr_pex 31 40 { … … 51 60 int socket ); 52 61 53 void tr_peerMgrAddPeers( tr_peerMgr * manager, 54 const uint8_t * torrentHash, 55 uint8_t from, 56 const uint8_t * peerCompact, 57 int peerCount ); 58 62 tr_pex * tr_peerMgrCompactToPex( const void * compact, 63 size_t compactLen, 64 const char * added_f, 65 size_t * pexCount ); 66 59 67 void tr_peerMgrAddPex( tr_peerMgr * manager, 60 68 const uint8_t * torrentHash, -
trunk/libtransmission/peer-msgs.c
r5640 r5647 942 942 int loaded = 0; 943 943 uint8_t * tmp = tr_new( uint8_t, msglen ); 944 tr_benc val, * sub;944 tr_benc val, *added; 945 945 const tr_torrent * tor = msgs->torrent; 946 946 tr_peerIoReadBytes( msgs->io, inbuf, tmp, msglen ); … … 948 948 if( tr_torrentAllowsPex( tor ) 949 949 && (( loaded = !tr_bencLoad( tmp, msglen, &val, NULL ))) 950 && (( sub = tr_bencDictFindType( &val, "added", TYPE_STR )))) 951 { 952 const int n = sub->val.s.i / 6 ; 953 if( n ) 954 tr_tordbg( tor, "Got %d peers from peer exchange", n ); 955 tr_peerMgrAddPeers( msgs->handle->peerMgr, 956 tor->info.hash, 957 TR_PEER_FROM_PEX, 958 (uint8_t*)sub->val.s.s, n ); 950 && (( added = tr_bencDictFindType( &val, "added", TYPE_STR )))) 951 { 952 const char * added_f = NULL; 953 tr_pex * pex; 954 size_t i, n; 955 tr_bencDictFindStr( &val, "added.f", &added_f ); 956 pex = tr_peerMgrCompactToPex( added->val.s.s, added->val.s.i, added_f, &n ); 957 for( i=0; i<n; ++i ) 958 tr_peerMgrAddPex( msgs->handle->peerMgr, tor->info.hash, 959 TR_PEER_FROM_PEX, pex+i ); 960 tr_free( pex ); 959 961 } 960 962 … … 1798 1800 const int newCount = tr_peerMgrGetPeers( msgs->handle->peerMgr, msgs->torrent->info.hash, &newPex ); 1799 1801 PexDiffs diffs; 1800 tr_benc val, *added, *dropped , *flags;1802 tr_benc val, *added, *dropped; 1801 1803 uint8_t *tmp, *walk; 1802 1804 char * benc; … … 1835 1837 1836 1838 /* "added.f" */ 1837 flags = tr_bencDictAdd( &val, "added.f" );1838 1839 tmp = walk = tr_new( uint8_t, diffs.addedCount ); 1839 1840 for( i=0; i<diffs.addedCount; ++i ) 1840 1841 *walk++ = diffs.added[i].flags; 1841 1842 assert( ( walk - tmp ) == diffs.addedCount ); 1842 tr_benc InitStr( flags, tmp, walk-tmp, FALSE);1843 tr_bencDictAddRaw( &val, "added.f", tmp, walk-tmp ); 1843 1844 1844 1845 /* "dropped" */ -
trunk/libtransmission/torrent.c
r5627 r5647 153 153 switch( event->messageType ) 154 154 { 155 case TR_TRACKER_PEERS: 156 tr_peerMgrAddPeers( tor->handle->peerMgr, 157 tor->info.hash, 158 TR_PEER_FROM_TRACKER, 159 event->peerCompact, 160 event->peerCount ); 155 case TR_TRACKER_PEERS: { 156 size_t i, n; 157 tr_pex * pex = tr_peerMgrCompactToPex( event->compact, 158 event->compactLen, 159 NULL, &n ); 160 if( event->allAreSeeds ) 161 tr_tordbg( tor, "Got %d seeds from tracker", (int)n ); 162 else 163 tr_torinf( tor, _( "Got %d peers from tracker" ), (int)n ); 164 165 for( i=0; i<n; ++i ) { 166 if( event->allAreSeeds ) 167 pex[i].flags |= ADDED_F_SEED_FLAG; 168 tr_peerMgrAddPex( tor->handle->peerMgr, tor->info.hash, 169 TR_PEER_FROM_TRACKER, pex+i ); 170 } 171 172 tr_free( pex ); 161 173 break; 174 } 162 175 163 176 case TR_TRACKER_WARNING: -
trunk/libtransmission/tracker.c
r5626 r5647 222 222 ***/ 223 223 224 static const tr_tracker_event emptyEvent = { 0, NULL, NULL, NULL, 0 };224 static const tr_tracker_event emptyEvent = { 0, NULL, NULL, NULL, 0, 0 }; 225 225 226 226 static void … … 257 257 258 258 static void 259 publishNewPeers( tr_tracker * t, int count, uint8_t * peers)259 publishNewPeers( tr_tracker * t, int allAreSeeds, void * compact, int compactLen ) 260 260 { 261 261 tr_tracker_event event = emptyEvent; 262 262 event.hash = t->hash; 263 263 event.messageType = TR_TRACKER_PEERS; 264 event. peerCount = count;265 event. peerCompact = peers;266 tr_ndbg( t->name, "Torrent got %d new peers", count );267 if( co unt)264 event.allAreSeeds = allAreSeeds; 265 event.compact = compact; 266 event.compactLen = compactLen; 267 if( compactLen ) 268 268 tr_publisherPublish( t->publisher, t, &event ); 269 269 } … … 361 361 /* Convert to compact form */ 362 362 static uint8_t * 363 parseOldPeers( tr_benc * bePeers, int * setmePeerCount )363 parseOldPeers( tr_benc * bePeers, size_t * byteCount ) 364 364 { 365 365 int i; … … 394 394 } 395 395 396 * setmePeerCount = peerCount;396 *byteCount = peerCount * 6; 397 397 return compact; 398 398 } … … 433 433 { 434 434 tr_benc * tmp; 435 int incomplete = -1; 435 436 436 437 if(( tmp = tr_bencDictFind( &benc, "failure reason" ))) { … … 461 462 462 463 if(( tmp = tr_bencDictFind( &benc, "incomplete" ))) 463 t->leecherCount = tmp->val.i;464 t->leecherCount = incomplete = tmp->val.i; 464 465 465 466 if(( tmp = tr_bencDictFind( &benc, "peers" ))) 466 467 { 467 int peerCount = 0; 468 uint8_t * peerCompact = NULL; 469 470 if( tmp->type == TYPE_LIST ) /* original protocol */ 468 const int allAreSeeds = incomplete == 0; 469 470 if( tmp->type == TYPE_STR ) /* "compact" extension */ 471 471 { 472 if( tmp->val.l.count > 0 ) 473 peerCompact = parseOldPeers( tmp, &peerCount ); 472 publishNewPeers( t, allAreSeeds, tmp->val.s.s, tmp->val.s.i ); 474 473 } 475 else if( tmp->type == TYPE_ STR ) /* "compact" extension*/474 else if( tmp->type == TYPE_LIST ) /* original protocol */ 476 475 { 477 if( tmp->val.s.i >= 6 ) 478 { 479 peerCount = tmp->val.s.i / 6; 480 peerCompact = tr_new( uint8_t, tmp->val.s.i ); 481 memcpy( peerCompact, tmp->val.s.s, tmp->val.s.i ); 482 } 476 size_t byteCount = 0; 477 uint8_t * compact = parseOldPeers( tmp, &byteCount ); 478 publishNewPeers( t, allAreSeeds, compact, byteCount ); 479 tr_free( compact ); 483 480 } 484 485 publishNewPeers( t, peerCount, peerCompact );486 tr_free( peerCompact );487 481 } 488 482 } -
trunk/libtransmission/tracker.h
r5086 r5647 55 55 56 56 /* for TR_TRACKER_PEERS */ 57 const uint8_t * peerCompact; 58 int peerCount; 57 const uint8_t * compact; 58 int compactLen; 59 int allAreSeeds; 59 60 } 60 61 tr_tracker_event;
Note: See TracChangeset
for help on using the changeset viewer.