...
authoralex <[email protected]>
Mon, 29 Nov 2021 01:00:57 +0000 (17:00 -0800)
committeralex <[email protected]>
Mon, 29 Nov 2021 01:00:57 +0000 (17:00 -0800)
inc/torrent.h
src/torrent.c

index 2befb2eb988aa8cb5cf34f9ceb6142230ad66590..36ae186b2678beb66f61e9af4e11debd60bc4a14 100644 (file)
@@ -18,7 +18,10 @@ struct torrent {
 };
 
 int torrent_add(struct torrent*,struct file*);
+int torrent_file(struct torrent*);
+int torrent_file_info(struct torrent*);
 void torrent_free(struct torrent*);
 int torrent_init(struct torrent**,const char*,const char*);
+char *torrent_magnet(struct torrent*);
 
 #endif
index bc67e5e5008b7f8a42b819d3387dda1feb0cd83f..26ae50befed617863cc480e4bb1759ccfe152469 100644 (file)
@@ -1,6 +1,9 @@
 #include<torrent.h>
 
-static int torrent_add_file(struct torrent *p, struct file *f);
+#define BUF_SIZE 1024
+
+static int torrent_add_file(struct torrent*,struct file*);
+static char *torrent_file_path(unsigned char*,size_t);
 
 int torrent_add(struct torrent *p, struct file *f) {
        const char *path;
@@ -22,11 +25,111 @@ int torrent_add(struct torrent *p, struct file *f) {
 }
 
 static int torrent_add_file(struct torrent *p, struct file *f) {
-       if(hashmap_insert(p->files,f->path,strlen(f->path),f)<0) { return -1; }
+       if(hashmap_insert(p->files,f->root,crypto_hash_sha256_BYTES,f)<0) { return -1; }
+
+       return 1;
+}
+       
+int torrent_file(struct torrent *torrent_p) {
+       uint8_t *info;
+       FILE *fp;
+       unsigned char infohash[crypto_hash_sha256_BYTES];
+       char *path;
+       ssize_t i;
+
+       if((i = torrent_file_info(torrent_p,&info))<0) { return -1; }
+
+       if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { return -1; }
+
+       path = torrent_file_path(infohash);
+       if(NULL==path) { return -1; }
+       
+       fp = fopen(path,"w");
+       if(NULL==fp) {
+               free(path);
+               return -1;
+       }
+
+       if((i = bencode_dict_start(buf,BUF_SIZE))<0) { goto clean; }
+
+       if(torrent_file_announce(p)<0) { goto clean; }
+
+       if((i = bencode_string("info",4,buf,BUF_SIZE))<0) { goto clean; }
+
+       if((i = bencode_string("piece layers",12,buf,BUF_SIZE))<0) { goto clean; }
+       if(torrent_file_piece_layers(torrent_p)<0) { goto clean; }
+
+       if((i = bencode_dict_end(buf,BUF_SIZE))<0) { goto clean; }
+
+       return 1;
+clean:
+       fclose(fp);
+       remove(path);
+       free(path);
+       return -1;
+}
 
+static int torrent_file_announce(struct torrent *p) {
+       if((i = bencode_string("announce",8,buf,BUF_SIZE))<0) { goto clean; }
+       if((i = bencode_string("http://example.com/announce",28,buf,BUF_SIZE))<0) { goto clean; }
        return 1;
 }
 
+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;
+       
+       if((i = bencode_string("file tree",9,&(p[len]),buf_len-len))<0) { goto clean; }
+       len += i;
+       if((i = tree_bencode(p->file_tree,&(p[len]),buf_len-len))<0) { goto clean; }
+       len += i;
+       
+       if((i = bencode_string("meta version",12,&(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;
+
+       if((i = bencode_string("name",4,&(p[len]),buf_len-len))<0) { goto clean; }
+       len += i;
+       if((i = bencode_string(p->name,strlen(p->name),&(p[len]),buf_len-len))<0) { goto clean; }
+       len += i;
+       
+       if((i = bencode_string("piece length",12,&(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 char *torrent_file_path(unsigned char *infohash, size_t len) {
+       // this always returns hex, unnecessary to return
+       sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,infohash,crypto_hash_sha256_BYTES);
+       return NULL;
+}
+
+static int torrent_file_piece_layers(struct torrent *p) {
+       return -1;
+}
+
 void torrent_free(struct torrent *p) {
        if(NULL==p) { return; }
 
@@ -72,3 +175,7 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name)
                torrent_free(*torrent_p);
                return -1;
 }
+
+char *torrent_magnet(struct torrent *p) {
+       return NULL;
+}