...
authoralex <[email protected]>
Thu, 9 Dec 2021 01:06:57 +0000 (17:06 -0800)
committeralex <[email protected]>
Thu, 9 Dec 2021 01:06:57 +0000 (17:06 -0800)
Makefile.am
src/torrent/file.c
src/torrent/info.c [new file with mode: 0644]
src/torrent/magnet.c
test/unit/Makefile.am
test/unit/torrent.tests.c

index c380572530fad4758631e0aeb54e81fcfb7d4485..483ce40db3eaf475a5c8094e2f9f306e602d6ad3 100644 (file)
@@ -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 \
index a1763d761d121589faf01779660da96e72c65b51..aef04bdeb0271783c3c7a9bf2124bc5afbe2e01c 100644 (file)
@@ -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 (file)
index 0000000..8d014a8
--- /dev/null
@@ -0,0 +1,52 @@
+#include<torrent.h>
+
+#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;
+}
index 3bce8e2eaa7bbfb69fd3b64e083f99abf6256a9d..2a27ab607ffac2e67b36f83ffe3c3a33a934d0ca 100644 (file)
@@ -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:
index b97536359a39c5ef4ae85698dfe84d8036c87a6f..a542789244c17e155088b892db69394b54e91000 100644 (file)
@@ -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
 
index 0a0ff3103dc2d5ff3ee37fdd4ae423e838ca1b78..a6d924e9cdd38fb4bc6b5d681a9bdd1494039ee7 100644 (file)
@@ -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);