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 \
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"
--- /dev/null
+#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;
+}
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:
$(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
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);