From c3ed6d5c58dff662246bf11d6c216c964c436db4 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 28 Nov 2021 17:00:57 -0800 Subject: [PATCH] ... --- inc/torrent.h | 3 ++ src/torrent.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/inc/torrent.h b/inc/torrent.h index 2befb2e..36ae186 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -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 diff --git a/src/torrent.c b/src/torrent.c index bc67e5e..26ae50b 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -1,6 +1,9 @@ #include -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; +} -- 2.39.5