#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;
}
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; }
torrent_free(*torrent_p);
return -1;
}
+
+char *torrent_magnet(struct torrent *p) {
+ return NULL;
+}