Skip to content

Commit

Permalink
Merge pull request #48 from rmsacks/cross-seed
Browse files Browse the repository at this point in the history
Add flag to enable easier cross-seeding
  • Loading branch information
Rudde authored Jul 22, 2020
2 parents 5dfd415 + 9e8ccb4 commit 2ba2258
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
10 changes: 8 additions & 2 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ static void print_help()
"-v, --verbose : be verbose\n"
"-w, --web-seed=<url>[,<url>]* : add web seed URLs\n"
" additional -w adds more URLs\n"
"-x, --cross-seed : ensure info hash is unique for easier cross-seeding\n"
#else
"-a <url>[,<url>]* : specify the full announce URLs\n"
" additional -a adds backup trackers\n"
Expand All @@ -296,6 +297,7 @@ static void print_help()
"-v : be verbose\n"
"-w <url>[,<url>]* : add web seed URLs\n"
" additional -w adds more URLs\n"
"-x : ensure info hash is unique for easier cross-seeding\n"
#endif
"\nPlease send bug reports, patches, feature requests, praise and\n"
"general gossip about the program to: [email protected]\n");
Expand Down Expand Up @@ -427,6 +429,7 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
#endif
{"verbose", 0, NULL, 'v'},
{"web-seed", 1, NULL, 'w'},
{"cross-seed", 0, NULL, 'x'},
{NULL, 0, NULL, 0}
};
#endif
Expand All @@ -442,9 +445,9 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])

/* now parse the command line options given */
#ifdef USE_PTHREADS
#define OPT_STRING "a:c:dfhl:n:o:ps:t:vw:"
#define OPT_STRING "a:c:dfhl:n:o:ps:t:vw:x"
#else
#define OPT_STRING "a:c:dfhl:n:o:ps:vw:"
#define OPT_STRING "a:c:dfhl:n:o:ps:vw:x"
#endif
#ifdef USE_LONG_OPTIONS
while ((c = getopt_long(argc, argv, OPT_STRING,
Expand Down Expand Up @@ -497,6 +500,9 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
case 'w':
ll_extend(m->web_seed_list, get_slist(optarg));
break;
case 'x':
m->cross_seed = 1;
break;
case '?':
fatal("use -h for help.\n");
}
Expand Down
10 changes: 9 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/


#include <stdlib.h> /* exit() */
#include <stdlib.h> /* exit(), srandom() */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <sys/stat.h> /* S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */
#include <fcntl.h> /* open() */
#include <time.h> /* clock_gettime() */

#include "export.h"
#include "mktorrent.h"
Expand Down Expand Up @@ -121,6 +122,7 @@ int main(int argc, char *argv[])
0, /* no_creation_date */
0, /* private */
NULL, /* source string */
0, /* cross_seed */
0, /* verbose */
0, /* force_overwrite */
#ifdef USE_PTHREADS
Expand All @@ -136,6 +138,12 @@ int main(int argc, char *argv[])
/* print who we are */
printf("mktorrent " VERSION " (c) 2007, 2009 Emil Renner Berthing\n\n");

/* seed PRNG with current time */
struct timespec ts;
FATAL_IF(clock_gettime(CLOCK_REALTIME, &ts) == -1,
"failed to get time: %s\n", strerror(errno));
srandom(ts.tv_nsec ^ ts.tv_sec);

/* process options */
init(&m, argc, argv);

Expand Down
1 change: 1 addition & 0 deletions mktorrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct metafile {
int no_creation_date; /* don't write the creation date */
int private; /* set the private flag */
char *source; /* set source for private trackers */
int cross_seed; /* ensure info hash is unique for easier cross-seeding */
int verbose; /* be verbose */
int force_overwrite; /* overwrite existing output file */
#ifdef USE_PTHREADS
Expand Down
10 changes: 10 additions & 0 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <string.h> /* strlen() etc. */
#include <time.h> /* time() */
#include <inttypes.h> /* PRIuMAX */
#include <stdlib.h> /* random() */

#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA_DIGEST_LENGTH */
Expand Down Expand Up @@ -179,6 +180,15 @@ EXPORT void write_metainfo(FILE *f, struct metafile *m, unsigned char *hash_stri
else
write_file_list(f, m->file_list);

if (m->cross_seed) {
fprintf(f, "12:x_cross_seed%u:mktorrent-", CROSS_SEED_RAND_LENGTH * 2 + 10);
for (int i = 0; i < CROSS_SEED_RAND_LENGTH; i++) {
unsigned char rand_byte = random();
fputc("0123456789ABCDEF"[rand_byte >> 4], f);
fputc("0123456789ABCDEF"[rand_byte & 0x0F], f);
}
}

/* the info section also contains the name of the torrent,
the piece length and the hash string */
fprintf(f, "4:name%lu:%s12:piece lengthi%ue6:pieces%u:",
Expand Down
2 changes: 2 additions & 0 deletions output.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "export.h" /* EXPORT */
#include "mktorrent.h" /* struct metafile */

#define CROSS_SEED_RAND_LENGTH 16

EXPORT void write_metainfo(FILE *f, struct metafile *m,
unsigned char *hash_string);

Expand Down

0 comments on commit 2ba2258

Please sign in to comment.