From 50860ea97a1ceacc24271c02b41491a576f841e9 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 4 Feb 2022 15:37:19 -0800 Subject: [PATCH] ... --- Makefile.am | 1 + inc/torrent.h | 1 + src/torrent/file.c | 28 +++++++++++++--------------- src/torrent/infohash.c | 16 ++++++++++++++++ src/torrent/init.c | 2 ++ src/torrent/magnet.c | 13 ++----------- test/unit/Makefile.am | 1 + 7 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 src/torrent/infohash.c diff --git a/Makefile.am b/Makefile.am index 7fd0a10..2af3989 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,6 +57,7 @@ seederd_SOURCES = \ src/torrent/file.c \ src/torrent/free.c \ src/torrent/info.c \ + src/torrent/infohash.c \ src/torrent/init.c \ src/torrent/magnet.c \ src/torrent/path.c \ diff --git a/inc/torrent.h b/inc/torrent.h index cd43544..ee1c66f 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -47,6 +47,7 @@ char *torrent_file_path(unsigned char*,size_t); int torrent_file_piece_layers(FILE*,struct torrent*); void torrent_free(struct torrent*); int torrent_init(struct torrent**,unsigned long long); +int torrent_infohash(struct torrent*); char *torrent_magnet(struct torrent*); int torrent_remove(struct torrent*,const char*); diff --git a/src/torrent/file.c b/src/torrent/file.c index 7441463..cfdc971 100644 --- a/src/torrent/file.c +++ b/src/torrent/file.c @@ -2,18 +2,14 @@ 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; @@ -21,11 +17,9 @@ int torrent_file(struct torrent *torrent_p) { 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"); @@ -34,9 +28,7 @@ int torrent_file(struct torrent *torrent_p) { 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; } @@ -49,7 +41,6 @@ int torrent_file(struct torrent *torrent_p) { return 1; clean: - if(info!=NULL) { free(info); } if(fp!=NULL) { fclose(fp); } remove(path); if(path!=NULL) { free(path); } @@ -83,7 +74,8 @@ static int torrent_file_write_end(FILE *fp) { 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; @@ -91,9 +83,15 @@ static int torrent_file_write_info(FILE *fp, uint8_t *info, size_t info_len) { 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) { diff --git a/src/torrent/infohash.c b/src/torrent/infohash.c new file mode 100644 index 0000000..774625a --- /dev/null +++ b/src/torrent/infohash.c @@ -0,0 +1,16 @@ +#include + +int torrent_infohash(struct torrent *p) { + uint8_t *info; + ssize_t i; + + if((i = torrent_file_info(p,&info))<0) { return -1; } + if(hash(info,i,p->infohash,crypto_hash_sha256_BYTES)<0) { goto clean; } + + free(info); + + return 1; +clean: + free(info); + return -1; +} diff --git a/src/torrent/init.c b/src/torrent/init.c index c15a110..692c180 100644 --- a/src/torrent/init.c +++ b/src/torrent/init.c @@ -17,6 +17,8 @@ int torrent_init(struct torrent **torrent_p, unsigned long long piece_length) { (*torrent_p)->name = NULL; (*torrent_p)->feed_url = NULL; (*torrent_p)->piece_length = piece_length; + + memset((*torrent_p)->infohash,0,crypto_hash_sha256_BYTES); if(tree_init(&((*torrent_p)->tree))<0) { goto clean; } diff --git a/src/torrent/magnet.c b/src/torrent/magnet.c index f0eff34..d8c301a 100644 --- a/src/torrent/magnet.c +++ b/src/torrent/magnet.c @@ -4,33 +4,24 @@ #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; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 3942a6e..17fb531 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -33,6 +33,7 @@ torrent_SOURCES = \ $(top_srcdir)/src/torrent/file.c \ $(top_srcdir)/src/torrent/free.c \ $(top_srcdir)/src/torrent/init.c \ + $(top_srcdir)/src/torrent/infohash.c \ $(top_srcdir)/src/torrent/magnet.c \ $(top_srcdir)/src/torrent/path.c \ $(top_srcdir)/src/torrent/piece.c \ -- 2.39.5