1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-24 08:43:27 +00:00

add populate and cleanup utils for tr_tracker_info_t, since bencode and the new tracker code will both use it

This commit is contained in:
Charles Kerr 2007-08-14 15:24:48 +00:00
parent 26dabf816e
commit 02606d6675
2 changed files with 49 additions and 26 deletions

View file

@ -45,6 +45,12 @@ typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
#define FALSE 0 #define FALSE 0
#endif #endif
int tr_trackerInfoInit( struct tr_tracker_info_s * info,
const char * address,
int address_len );
void tr_trackerInfoClear( struct tr_tracker_info_s * info );
struct tr_peer_s; struct tr_peer_s;
void tr_peerIdNew ( char* buf, int buflen ); void tr_peerIdNew ( char* buf, int buflen );

View file

@ -310,20 +310,15 @@ realparse( tr_info_t * inf, const uint8_t * buf, size_t size )
void tr_metainfoFree( tr_info_t * inf ) void tr_metainfoFree( tr_info_t * inf )
{ {
int ii, jj; int i, j;
tr_free( inf->pieces ); tr_free( inf->pieces );
tr_free( inf->files ); tr_free( inf->files );
for( ii = 0; ii < inf->trackerTiers; ii++ ) for( i=0; i<inf->trackerTiers; ++i ) {
{ for( j=0; j<inf->trackerList[i].count; ++j )
for( jj = 0; jj < inf->trackerList[ii].count; jj++ ) tr_trackerInfoClear( &inf->trackerList[i].list[j] );
{ tr_free( inf->trackerList[i].list );
tr_free( inf->trackerList[ii].list[jj].address );
tr_free( inf->trackerList[ii].list[jj].announce );
tr_free( inf->trackerList[ii].list[jj].scrape );
}
tr_free( inf->trackerList[ii].list );
} }
tr_free( inf->trackerList ); tr_free( inf->trackerList );
@ -416,10 +411,11 @@ static int getannounce( tr_info_t * inf, benc_val_t * meta )
/* iterate through the tier's items */ /* iterate through the tier's items */
for( jj = 0; jj < subval->val.l.count; jj++ ) for( jj = 0; jj < subval->val.l.count; jj++ )
{ {
tr_tracker_info_t tmp;
urlval = &subval->val.l.vals[jj]; urlval = &subval->val.l.vals[jj];
if( TYPE_STR != urlval->type || if( TYPE_STR != urlval->type ||
tr_httpParseUrl( urlval->val.s.s, urlval->val.s.i, tr_trackerInfoInit( &tmp, urlval->val.s.s, urlval->val.s.i ) )
&address, &port, &announce ) )
{ {
continue; continue;
} }
@ -427,13 +423,8 @@ static int getannounce( tr_info_t * inf, benc_val_t * meta )
/* place the item info in a random location in the sublist */ /* place the item info in a random location in the sublist */
random = tr_rand( subcount + 1 ); random = tr_rand( subcount + 1 );
if( random != subcount ) if( random != subcount )
{
sublist[subcount] = sublist[random]; sublist[subcount] = sublist[random];
} sublist[random] = tmp;
sublist[random].address = address;
sublist[random].port = port;
sublist[random].announce = announce;
sublist[random].scrape = announceToScrape( announce );
subcount++; subcount++;
} }
@ -481,14 +472,15 @@ static int getannounce( tr_info_t * inf, benc_val_t * meta )
} }
/* Regular announce value */ /* Regular announce value */
if( 0 == inf->trackerTiers ) val = tr_bencDictFind( meta, "announce" );
if( NULL == val || TYPE_STR != val->type )
{
tr_err( "No \"announce\" entry" );
return TR_EINVALID;
}
if( !inf->trackerTiers )
{ {
val = tr_bencDictFind( meta, "announce" );
if( NULL == val || TYPE_STR != val->type )
{
tr_err( "No \"announce\" entry" );
return TR_EINVALID;
}
if( tr_httpParseUrl( val->val.s.s, val->val.s.i, if( tr_httpParseUrl( val->val.s.s, val->val.s.i,
&address, &port, &announce ) ) &address, &port, &announce ) )
@ -553,6 +545,31 @@ static char * announceToScrape( const char * announce )
return scrape; return scrape;
} }
int
tr_trackerInfoInit( tr_tracker_info_t * info,
const char * address,
int address_len )
{
int ret = tr_httpParseUrl( address, address_len,
&info->address,
&info->port,
&info->announce );
if( !ret )
info->scrape = announceToScrape( info->announce );
return ret;
}
void
tr_trackerInfoClear( tr_tracker_info_t * info )
{
tr_free( info->address );
tr_free( info->announce );
tr_free( info->scrape );
memset( info, '\0', sizeof(tr_tracker_info_t) );
}
static void static void
savedname( char * name, size_t len, const char * hash, const char * tag ) savedname( char * name, size_t len, const char * hash, const char * tag )
{ {