]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Wed, 15 Dec 2021 05:41:31 +0000 (21:41 -0800)
committeralex <[email protected]>
Wed, 15 Dec 2021 05:41:31 +0000 (21:41 -0800)
Makefile.am
inc/bencode.h
inc/torrent.h
inc/tree.h
src/bencode/encode.c
src/torrent/file.c
src/torrent/info.c
src/torrent/piece.c
src/tree.c
test/unit/Makefile.am

index 483ce40db3eaf475a5c8094e2f9f306e602d6ad3..746c99b74d17860a4529c77ccd2c42438cb7f7b9 100644 (file)
@@ -46,6 +46,7 @@ seederd_SOURCES = \
        src/torrent/info.c \
        src/torrent/init.c \
        src/torrent/magnet.c \
+       src/torrent/piece.c \
        src/tree.c \
        src/usage.c \
        src/watch.c
index e0806759265ade1bc3407247cb2884fe897ff3f2..0d4ecffc933acbac74eedfd0ecd19066e4cc87cb 100644 (file)
@@ -22,6 +22,8 @@ ssize_t bencode_dict_start(uint8_t*,size_t);
 ssize_t bencode_int(long long int,uint8_t*,size_t);
 ssize_t bencode_list_end(uint8_t*,size_t);
 ssize_t bencode_list_start(uint8_t*,size_t);
+ssize_t bencode_size(size_t,uint8_t*,size_t);
+size_t bencode_size_int(int);
 ssize_t bencode_string(const uint8_t*,size_t,uint8_t*,size_t);
 
 #endif
index 24c67ab0cad0a6853ac59ce269c4fdeff1884dc2..939e0383d23c02bd930e3876b7309ee8ce30fad7 100644 (file)
@@ -37,6 +37,5 @@ int torrent_file_piece_layers(FILE*,struct torrent*);
 void torrent_free(struct torrent*);
 int torrent_init(struct torrent**,const char*,const char*,unsigned long long);
 char *torrent_magnet(struct torrent*);
-int torrent_bencode_piece_layers();
 
 #endif
index b2893bb3f5d8f24f3ca3a67018ba3560c441af9b..f315945a4a82a9cbd685cd7f3d5cccd9461472ad 100644 (file)
@@ -22,6 +22,7 @@ struct tree {
 
 int tree_add(struct tree*,const char*,struct file*);
 ssize_t tree_bencode(struct tree*,uint8_t*,size_t);
+size_t tree_bencode_length(struct tree*);
 void tree_entry_free(struct tree_entry*);
 int tree_entry_init(struct tree_entry**, struct file*);
 void tree_free(struct tree*);
index 536532a5df4cfee2f832f70d8df22dbe8dcd715d..c7dc994bd760c2c3167cde54760b0732f5477766 100644 (file)
@@ -1,7 +1,5 @@
 #include<bencode.h>
 
-static size_t size_int(int i);
-
 ssize_t bencode_dict_end(uint8_t *output, size_t output_size) {
        if(NULL==output) { return -1; }
        if(output_size<=1) { return -1; }
@@ -26,7 +24,7 @@ ssize_t bencode_int(long long int to_encode, uint8_t *output, size_t output_size
 
        if(NULL==output) { return -1; }
 
-       i = size_int(to_encode);
+       i = bencode_size_int(to_encode);
        i += 2;
 
        if((ret = snprintf((char*)output,output_size,"i%llue",to_encode))<0) { return -1; }
@@ -53,26 +51,20 @@ ssize_t bencode_list_start(uint8_t *output, size_t output_size) {
        return 1;
 }
 
-ssize_t bencode_string(const uint8_t *str, size_t len, uint8_t *output, size_t output_size) {
+ssize_t bencode_size(size_t len, uint8_t *output, size_t output_size) {
        size_t i;
-       
-       if(NULL==str) { return -1; }
-       if(NULL==output) { return -1; }
 
-       i = size_int(len);
+       i = bencode_size_int(len);
        i++; // account for ':'
-       if(i+len>output_size) { return -1; }
+       if(i>output_size) { return -1; }
 
        // snprintf requires space for '\0' otherwise will truncate
        if(snprintf((char*)output,i+1,"%lu:",len)!=i) { return -1; }
 
-       memcpy(&(output[i]),str,len);
-
-       return i+len;
+       return i;
 }
 
-
-static size_t size_int(int i) {
+size_t bencode_size_int(int i) {
        size_t j = (i<0)?1:0;
        
        i = abs(i);
@@ -84,3 +76,18 @@ static size_t size_int(int i) {
        }
        return j;
 }
+
+ssize_t bencode_string(const uint8_t *str, size_t len, uint8_t *output, size_t output_size) {
+       size_t i;
+       
+       if(NULL==str) { return -1; }
+       if(NULL==output) { return -1; }
+
+       if((i = bencode_size(len,output,output_size))<0) { return -1; }
+       
+       if(i+len>output_size) { return -1; }
+
+       memcpy(&(output[i]),str,len);
+
+       return i+len;
+}
index c0ed1457e741ba65318952b258a136bb5d988fca..dc1e0a7573d990922b0368da96e2401b76c1eaa2 100644 (file)
@@ -1,6 +1,5 @@
 #include<torrent.h>
 
-static int piece_layer_sort(const void*,const void*);
 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);
index 99347e711e92c435666f181a36f15921731b5ea1..71f84743be05b5bdf8824b9f4bfd31fbf5e43176 100644 (file)
@@ -1,54 +1,64 @@
 #include<torrent.h>
 
-#define INITIAL_TORRENT_INFO_SIZE 16384
+static size_t torrent_file_info_length(struct tree *tree);
+
+static uint8_t file_tree_string[] = "file tree";
+static uint8_t meta_version_string[] = "meta version";
+static uint8_t name_string[] = "name";
+static uint8_t piece_length_string[] = "piece length";
 
 ssize_t torrent_file_info(struct torrent *torrent_p, uint8_t **buf) {
        uint8_t *p;
-       ssize_t i, len;
-       size_t buf_len;
+       ssize_t i, len, buf_len;
 
        if(NULL==buf) { return -1; }
 
-       p = malloc(INITIAL_TORRENT_INFO_SIZE);
+       buf_len = torrent_file_info_length(torrent_p->tree);
+       p = malloc(sizeof(uint8_t)*buf_len);
        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;
 }
+
+static size_t torrent_file_info_length(struct tree *tree) {
+       size_t i;
+
+       i = 0;
+
+       // string length = sizeof()-1, +1 for ':' included
+       i += sizeof(file_tree_string)+bencode_size_int(sizeof(file_tree_string));
+       i += sizeof(meta_version_string)+bencode_size_int(sizeof(file_tree_string));
+       i += sizeof(name_string)+bencode_size_int(sizeof(file_tree_string));
+       i += sizeof(piece_length_string)+bencode_size_int(sizeof(file_tree_string));
+
+       i += tree_bencode_length(tree);
+
+       return i;
+}
index 94725f74996a6ae66b9b02215758b0cbec2c5825..07f6ed1ef5c3d011d5cc5976ceb82ae986792133 100644 (file)
@@ -1,37 +1,38 @@
 #include<torrent.h>
 
-ssize_t torrent_bencode_piece_layers(struct torrent *torrent_p, unsigned char *hash) {
-       struct file *file_p;
+static int piece_layer_sort(const void*, const void*);
+static int torrent_bencode_piece_layers(FILE*,struct file*);
+
+#define PIECE_LAYER_BENCODED_LENGTH 2+1+32+2+1+32
+
+static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) {
        struct block *p;
-       size_t buf_len, len;
+       size_t blocks;
        ssize_t i;
-       uint8_t *buf;
-       
-       file_p = hashmap_find(torrent_p->files.roots,hash,crypto_hash_sha256_BYTES)
-       if(NULL==file_p) { return -1; }
-
-       // more lenght
+       uint8_t buf[PIECE_LAYER_BENCODED_LENGTH];
 
-       buf = malloc(sizeof(uint8_t)*len);
-       if(NULL==buf) { return -1; }
+       if((i = bencode_string(file_p->root,crypto_hash_sha256_BYTES,buf,PIECE_LAYER_BENCODED_LENGTH))<0) { return -1; }
+       if(fwrite(buf,sizeof(uint8_t),i,fp)!=i) { return -1; }
 
-       buf_len = len;
+       blocks = block_length(file_p->piece_layers);
+       if((i = bencode_size(blocks*crypto_hash_sha256_BYTES,buf,PIECE_LAYER_BENCODED_LENGTH))<0) { return -1; }
 
-       if((i = bencode_string(file_p->root,crypto_hash_sha256_BYTES,buf,len))<0) { goto clean; }
-       buf += i;
-       len -= i;
+       if(fwrite(buf,sizeof(uint8_t),i,fp)!=i) { return -1; }
 
        p = file_p->piece_layers;
-       while(
+       while(p!=NULL) {
+               if(fwrite(p->hash,sizeof(uint8_t),crypto_hash_sha256_BYTES,fp)!=crypto_hash_sha256_BYTES) { return -1; }
 
-       return -1;
+               p = p->next;
+       }
+
+       return 1;
 }
 
 int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
        unsigned char **hashes;
        size_t index, pieces;
        struct file *file_p;
-       struct block **piece_layers, *block_p;
 
        pieces = 0;
        for(size_t i=0;i<p->files.roots->size;i++) {
@@ -54,19 +55,30 @@ int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
 
        qsort(hashes,pieces,sizeof(unsigned char*),&piece_layer_sort);
 
-       // NOT DONE
+       // hashes[0] should never be NULL
+       if(NULL==hashes[0]) { return -1; }
+
+       file_p = hashmap_find(p->files.roots,hashes[0],crypto_hash_sha256_BYTES);
+       if(NULL==file_p) { return -1; }
+
        for(size_t i=0;i<pieces;i++) {
-               file_p = (struct file*)p->files.roots->map[i];
+               file_p = hashmap_find(p->files.roots,hash,crypto_hash_sha256_BYTES);
+               if(NULL==file_p) { break; } // sorting guarantees all NULLs at end
+
+               if(torrent_bencode_piece_layers(fp,file_p)<0) { goto clean; }
+       }
+
+       free(hashes);
 
        return 1;
-//clean:
-//     free(piece_layers);
-//     return -1;
+clean:
+       free(hashes);
+       return -1;
 }
 
 static int piece_layer_sort(const void *p1, const void *p2) {
-       if(NULL==a) { return 1; }
-       if(NULL==b) { return -1; }
+       if(NULL==p1) { return 1; }
+       if(NULL==p2) { return -1; }
 
        return memcmp(p1,p2,crypto_hash_sha256_BYTES);
 }
index 10322aa7abecfb7c0e7c1f6136598cce744d6921..6503b082ff3fd4e71f97eff7ac80c8bfe6684a67 100644 (file)
@@ -151,6 +151,10 @@ ssize_t tree_bencode(struct tree *tree, uint8_t *buf, size_t len) {
        return buf_len - len;
 }
 
+size_t tree_bencode_length(struct tree *p) {
+       return 0;
+}
+
 void tree_entry_free(struct tree_entry *p) {
        if(NULL==p) { return; }
        
index a542789244c17e155088b892db69394b54e91000..82ec0ae53cb5d6a0eebcb95548b75a9bca0857ea 100644 (file)
@@ -77,6 +77,7 @@ torrent_tests_SOURCES = \
        $(top_srcdir)/src/torrent/info.c \
        $(top_srcdir)/src/torrent/init.c \
        $(top_srcdir)/src/torrent/magnet.c \
+       $(top_srcdir)/src/torrent/piece.c \
        $(top_srcdir)/src/tree.c
 
 tree_tests_SOURCES = \