diff --git a/cli/cli.c b/cli/cli.c index ea0b66b..9092477 100644 --- a/cli/cli.c +++ b/cli/cli.c @@ -57,11 +57,16 @@ static sig_atomic_t manualUpdate = 0; static const char * torrentPath = NULL; static const char * downloadDir = NULL; static const char * finishCall = NULL; -static const char * announce = NULL; static const char * configdir = NULL; static const char * sourceFile = NULL; static const char * comment = NULL; +struct announce_t { + int num; + tr_tracker_info *trackers; +}; +static struct announce_t announce = { 0, NULL }; + static const struct tr_option options[] = { { 'a', "announce", "Set the new torrent's announce URL", @@ -391,10 +396,7 @@ main( int argc, { int err; tr_metainfo_builder * b = tr_metaInfoBuilderCreate( h, sourceFile ); - tr_tracker_info ti; - ti.tier = 0; - ti.announce = (char*) announce; - tr_makeMetaInfo( b, torrentPath, &ti, 1, comment, isPrivate ); + tr_makeMetaInfo( b, torrentPath, announce.trackers, announce.num, comment, isPrivate ); while( !b->isDone ) { tr_wait( 1000 ); @@ -555,6 +557,33 @@ numarg( const char * arg ) return num; } +static void add_announce(const char * tracker_url) +{ + if ( tracker_url == NULL ) + return; + + // argc is an integer, also announce.num should not overflow + ++announce.num; + if ( (SIZE_MAX / sizeof(tr_tracker_info)) < (size_t)announce.num ) + { + puts("To many announces\n"); + exit(EXIT_FAILURE); + } + + tr_tracker_info *new_trackers = realloc(announce.trackers, sizeof(tr_tracker_info) * announce.num); + + if ( new_trackers == NULL ) + { + free(announce.trackers); + puts("Not enough ram\n"); + exit(EXIT_FAILURE); + } + announce.trackers = new_trackers; + + new_trackers[announce.num-1].tier = announce.num; + new_trackers[announce.num-1].announce = tracker_url; +} + static int parseCommandLine( int argc, const char ** argv ) @@ -567,7 +596,7 @@ parseCommandLine( int argc, switch( c ) { case 'a': - announce = optarg; break; + add_announce(optarg); break; case 'b': blocklistEnabled = 1; break;