static int torrent_file_announce(FILE*,struct torrent*);
static int torrent_file_write_end(FILE*);
-static int torrent_file_write_info(FILE*,uint8_t*,size_t);
+static int torrent_file_write_info(FILE*,struct torrent*);
static int torrent_file_write_piece_layers(FILE*,struct torrent*);
static int torrent_file_write_start(FILE*);
int torrent_file(struct torrent *torrent_p) {
- uint8_t *info;
FILE *fp;
- unsigned char infohash[crypto_hash_sha256_BYTES];
char *path;
- ssize_t i;
- info = NULL;
fp = NULL;
path = NULL;
if(directory_create(TORRENT_FILE_DIRECTORY)<0) {goto clean; }
}
- if((i = torrent_file_info(torrent_p,&info))<0) { goto clean; }
-
- if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { goto clean; }
+ if(torrent_infohash(torrent_p)<0) { goto clean; }
- path = torrent_file_path(infohash,crypto_hash_sha256_BYTES);
+ path = torrent_file_path(torrent_p->infohash,crypto_hash_sha256_BYTES);
if(NULL==path) { goto clean; }
fp = fopen(path,"w");
if(torrent_file_write_start(fp)<0) { goto clean; }
if(torrent_file_announce(fp,torrent_p)<0) { goto clean; }
- if(torrent_file_write_info(fp,info,i)<0) { goto clean; }
- free(info);
- info = NULL;
+ if(torrent_file_write_info(fp,torrent_p)<0) { goto clean; }
if(torrent_file_write_piece_layers(fp,torrent_p)<0) { goto clean; }
return 1;
clean:
- if(info!=NULL) { free(info); }
if(fp!=NULL) { fclose(fp); }
remove(path);
if(path!=NULL) { free(path); }
return 1;
}
-static int torrent_file_write_info(FILE *fp, uint8_t *info, size_t info_len) {
+static int torrent_file_write_info(FILE *fp, struct torrent *p) {
+ uint8_t *info;
uint8_t info_string[] = "info";
uint8_t buf[6];
ssize_t i;
if((i = bencode_string(info_string,sizeof(info_string)-1,buf,6))<0) { return -1; }
if(i!=fwrite(buf,sizeof(uint8_t),i,fp)) { return -1; }
- if(info_len!=fwrite(info,sizeof(uint8_t),info_len,fp)) { return -1; }
+ if((i = torrent_file_info(p,&info))<0) { return -1; }
+ if(i!=fwrite(info,sizeof(uint8_t),i,fp)) { goto clean; }
+
+ free(info);
return 1;
+clean:
+ free(info);
+ return -1;
}
static int torrent_file_write_piece_layers(FILE *fp, struct torrent *p) {
#define MAGNET_LINK_TEMPLATE_LENGTH 24
char *torrent_magnet(struct torrent *torrent_p) {
- uint8_t *info;
char *p;
- unsigned char infohash[crypto_hash_sha256_BYTES];
char hex[(crypto_hash_sha256_BYTES<<1)+1];
size_t len;
- ssize_t i;
- info = NULL;
- if((i = torrent_file_info(torrent_p,&info))<0) { goto clean; }
-
- if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { goto clean; }
- free(info);
- info = NULL;
+ if(torrent_infohash(torrent_p)<0) { return NULL; }
len = MAGNET_LINK_TEMPLATE_LENGTH+(crypto_hash_sha256_BYTES<<1)+strlen(torrent_p->name)+1;
p = malloc(sizeof(char)*len);
if(NULL==p) { return NULL; }
// this always returns hex, unnecessary to return
- sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,infohash,crypto_hash_sha256_BYTES);
+ sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,torrent_p->infohash,crypto_hash_sha256_BYTES);
// snprintf returns number of characters written (excluding '\0')
if(snprintf(p,len,MAGNET_LINK_TEMPLATE,hex,torrent_p->name)!=(len-1)) { goto clean; }
return p;
clean:
- if(info!=NULL) { free(info); }
free(p);
return NULL;
}