Skip to content

Commit 9e8ccb4

Browse files
committed
Add flag to enable easier cross-seeding
1 parent 5dfd415 commit 9e8ccb4

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

init.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ static void print_help()
274274
"-v, --verbose : be verbose\n"
275275
"-w, --web-seed=<url>[,<url>]* : add web seed URLs\n"
276276
" additional -w adds more URLs\n"
277+
"-x, --cross-seed : ensure info hash is unique for easier cross-seeding\n"
277278
#else
278279
"-a <url>[,<url>]* : specify the full announce URLs\n"
279280
" additional -a adds backup trackers\n"
@@ -296,6 +297,7 @@ static void print_help()
296297
"-v : be verbose\n"
297298
"-w <url>[,<url>]* : add web seed URLs\n"
298299
" additional -w adds more URLs\n"
300+
"-x : ensure info hash is unique for easier cross-seeding\n"
299301
#endif
300302
"\nPlease send bug reports, patches, feature requests, praise and\n"
301303
"general gossip about the program to: [email protected]\n");
@@ -427,6 +429,7 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
427429
#endif
428430
{"verbose", 0, NULL, 'v'},
429431
{"web-seed", 1, NULL, 'w'},
432+
{"cross-seed", 0, NULL, 'x'},
430433
{NULL, 0, NULL, 0}
431434
};
432435
#endif
@@ -442,9 +445,9 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
442445

443446
/* now parse the command line options given */
444447
#ifdef USE_PTHREADS
445-
#define OPT_STRING "a:c:dfhl:n:o:ps:t:vw:"
448+
#define OPT_STRING "a:c:dfhl:n:o:ps:t:vw:x"
446449
#else
447-
#define OPT_STRING "a:c:dfhl:n:o:ps:vw:"
450+
#define OPT_STRING "a:c:dfhl:n:o:ps:vw:x"
448451
#endif
449452
#ifdef USE_LONG_OPTIONS
450453
while ((c = getopt_long(argc, argv, OPT_STRING,
@@ -497,6 +500,9 @@ EXPORT void init(struct metafile *m, int argc, char *argv[])
497500
case 'w':
498501
ll_extend(m->web_seed_list, get_slist(optarg));
499502
break;
503+
case 'x':
504+
m->cross_seed = 1;
505+
break;
500506
case '?':
501507
fatal("use -h for help.\n");
502508
}

main.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1818
*/
1919

2020

21-
#include <stdlib.h> /* exit() */
21+
#include <stdlib.h> /* exit(), srandom() */
2222
#include <errno.h> /* errno */
2323
#include <string.h> /* strerror() */
2424
#include <stdio.h> /* printf() etc. */
2525
#include <sys/stat.h> /* S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */
2626
#include <fcntl.h> /* open() */
27+
#include <time.h> /* clock_gettime() */
2728

2829
#include "export.h"
2930
#include "mktorrent.h"
@@ -121,6 +122,7 @@ int main(int argc, char *argv[])
121122
0, /* no_creation_date */
122123
0, /* private */
123124
NULL, /* source string */
125+
0, /* cross_seed */
124126
0, /* verbose */
125127
0, /* force_overwrite */
126128
#ifdef USE_PTHREADS
@@ -136,6 +138,12 @@ int main(int argc, char *argv[])
136138
/* print who we are */
137139
printf("mktorrent " VERSION " (c) 2007, 2009 Emil Renner Berthing\n\n");
138140

141+
/* seed PRNG with current time */
142+
struct timespec ts;
143+
FATAL_IF(clock_gettime(CLOCK_REALTIME, &ts) == -1,
144+
"failed to get time: %s\n", strerror(errno));
145+
srandom(ts.tv_nsec ^ ts.tv_sec);
146+
139147
/* process options */
140148
init(&m, argc, argv);
141149

mktorrent.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct metafile {
3131
int no_creation_date; /* don't write the creation date */
3232
int private; /* set the private flag */
3333
char *source; /* set source for private trackers */
34+
int cross_seed; /* ensure info hash is unique for easier cross-seeding */
3435
int verbose; /* be verbose */
3536
int force_overwrite; /* overwrite existing output file */
3637
#ifdef USE_PTHREADS

output.c

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2323
#include <string.h> /* strlen() etc. */
2424
#include <time.h> /* time() */
2525
#include <inttypes.h> /* PRIuMAX */
26+
#include <stdlib.h> /* random() */
2627

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

183+
if (m->cross_seed) {
184+
fprintf(f, "12:x_cross_seed%u:mktorrent-", CROSS_SEED_RAND_LENGTH * 2 + 10);
185+
for (int i = 0; i < CROSS_SEED_RAND_LENGTH; i++) {
186+
unsigned char rand_byte = random();
187+
fputc("0123456789ABCDEF"[rand_byte >> 4], f);
188+
fputc("0123456789ABCDEF"[rand_byte & 0x0F], f);
189+
}
190+
}
191+
182192
/* the info section also contains the name of the torrent,
183193
the piece length and the hash string */
184194
fprintf(f, "4:name%lu:%s12:piece lengthi%ue6:pieces%u:",

output.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "export.h" /* EXPORT */
77
#include "mktorrent.h" /* struct metafile */
88

9+
#define CROSS_SEED_RAND_LENGTH 16
10+
911
EXPORT void write_metainfo(FILE *f, struct metafile *m,
1012
unsigned char *hash_string);
1113

0 commit comments

Comments
 (0)