From aeaea63a6c7f35db2b2ea4d92bf1e6c41aa6eb24 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 8 Dec 2021 17:06:57 -0800 Subject: [PATCH] ... --- Makefile.am | 1 + src/torrent/file.c | 51 -------------------------------------- src/torrent/info.c | 52 +++++++++++++++++++++++++++++++++++++++ src/torrent/magnet.c | 11 +++++---- test/unit/Makefile.am | 3 ++- test/unit/torrent.tests.c | 2 +- 6 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 src/torrent/info.c diff --git a/Makefile.am b/Makefile.am index c380572..483ce40 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,7 @@ seederd_SOURCES = \ src/torrent/add.c \ src/torrent/file.c \ src/torrent/free.c \ + src/torrent/info.c \ src/torrent/init.c \ src/torrent/magnet.c \ src/tree.c \ diff --git a/src/torrent/file.c b/src/torrent/file.c index a1763d7..aef04bd 100644 --- a/src/torrent/file.c +++ b/src/torrent/file.c @@ -66,57 +66,6 @@ static int torrent_file_announce(FILE *fp, struct torrent *p) { return 1; } -#define INITIAL_TORRENT_INFO_SIZE 100 - -ssize_t torrent_file_info(struct torrent *torrent_p, uint8_t **buf) { - uint8_t *p; - ssize_t i, len; - size_t buf_len; - - p = malloc(INITIAL_TORRENT_INFO_SIZE); - if(NULL==p) { return -1; } - - len = 0; - buf_len = INITIAL_TORRENT_INFO_SIZE; - if((i = bencode_dict_start(p,buf_len))<0) { goto clean; } - len += i; - - uint8_t file_tree_string[] = "file tree"; - if((i = bencode_string(file_tree_string,sizeof(file_tree_string),&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - if((i = tree_bencode(torrent_p->tree,&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - - uint8_t meta_version_string[] = "meta version"; - if((i = bencode_string(meta_version_string,sizeof(meta_version_string),&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - if((i = bencode_int(2,&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - - uint8_t name_string[] = "name"; - if((i = bencode_string(name_string,sizeof(name_string),&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - if((i = bencode_string((const uint8_t*)torrent_p->name,strlen(torrent_p->name),&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - - uint8_t piece_length_string[] = "piece length"; - if((i = bencode_string(piece_length_string,sizeof(piece_length_string),&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - if((i = bencode_int(torrent_p->piece_length,&(p[len]),buf_len-len))<0) { goto clean; } - len += i; - - *buf = malloc(len); - if(NULL==(*buf)) { goto clean; } - - memcpy(*buf,p,len); - free(p); - - return len; -clean: - free(p); - return -1; -} - #define TORRENT_FILE_DIRECTORY "/torrents" #define TORRENT_FILE_EXTENSION ".torrent" diff --git a/src/torrent/info.c b/src/torrent/info.c new file mode 100644 index 0000000..8d014a8 --- /dev/null +++ b/src/torrent/info.c @@ -0,0 +1,52 @@ +#include + +#define INITIAL_TORRENT_INFO_SIZE 16384 + +ssize_t torrent_file_info(struct torrent *torrent_p, uint8_t **buf) { + uint8_t *p; + ssize_t i, len; + size_t buf_len; + + p = malloc(INITIAL_TORRENT_INFO_SIZE); + if(NULL==p) { return -1; } + + len = 0; + buf_len = INITIAL_TORRENT_INFO_SIZE; + if((i = bencode_dict_start(p,buf_len))<0) { goto clean; } + len += i; + + uint8_t file_tree_string[] = "file tree"; + if((i = bencode_string(file_tree_string,sizeof(file_tree_string)-1,&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + if((i = tree_bencode(torrent_p->tree,&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + + uint8_t meta_version_string[] = "meta version"; + if((i = bencode_string(meta_version_string,sizeof(meta_version_string)-1,&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + if((i = bencode_int(2,&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + + uint8_t name_string[] = "name"; + if((i = bencode_string(name_string,sizeof(name_string)-1,&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + if((i = bencode_string((const uint8_t*)torrent_p->name,strlen(torrent_p->name),&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + + uint8_t piece_length_string[] = "piece length"; + if((i = bencode_string(piece_length_string,sizeof(piece_length_string)-1,&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + if((i = bencode_int(torrent_p->piece_length,&(p[len]),buf_len-len))<0) { goto clean; } + len += i; + + *buf = malloc(len); + if(NULL==(*buf)) { goto clean; } + + memcpy(*buf,p,len); + free(p); + + return len; +clean: + free(p); + return -1; +} diff --git a/src/torrent/magnet.c b/src/torrent/magnet.c index 3bce8e2..2a27ab6 100644 --- a/src/torrent/magnet.c +++ b/src/torrent/magnet.c @@ -14,19 +14,20 @@ char *torrent_magnet(struct torrent *torrent_p) { info = NULL; if((i = torrent_file_info(torrent_p,&info))<0) { goto clean; } - len = MAGNET_LINK_TEMPLATE_LENGTH+i+strlen(torrent_p->name)+1; - p = malloc(sizeof(char)*len); - if(NULL==p) { return NULL; } if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { goto clean; } free(info); info = 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); - len--; - if(snprintf(p,len,MAGNET_LINK_TEMPLATE,hex,torrent_p->name)!=len) { goto clean; } + // 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: diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index b975363..a542789 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -72,9 +72,10 @@ torrent_tests_SOURCES = \ $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/hashmap.c \ $(top_srcdir)/src/torrent/add.c \ - $(top_srcdir)/src/torrent/init.c \ $(top_srcdir)/src/torrent/file.c \ $(top_srcdir)/src/torrent/free.c \ + $(top_srcdir)/src/torrent/info.c \ + $(top_srcdir)/src/torrent/init.c \ $(top_srcdir)/src/torrent/magnet.c \ $(top_srcdir)/src/tree.c diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index 0a0ff31..a6d924e 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -111,7 +111,7 @@ static void torrent_magnet_basic_test() { p = torrent_magnet(torrent); assert(p!=NULL); - assert(strcmp(p,"magnet:?xt=urn:btmh:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&dn=" TEST_DIRECTORY)==0); + assert(strcmp(p,"magnet:?xt=urn:btmh:d2cb0c8d48b492e0407e2b24ae6b9c2e542d9addf08d71eef8db7008944def9e&dn=" TEST_DIRECTORY)==0); free(p); torrent_free(torrent); -- 2.39.5