...
authoralex <[email protected]>
Fri, 4 Feb 2022 23:37:19 +0000 (15:37 -0800)
committeralex <[email protected]>
Fri, 4 Feb 2022 23:37:19 +0000 (15:37 -0800)
Makefile.am
inc/torrent.h
src/torrent/file.c
src/torrent/infohash.c [new file with mode: 0644]
src/torrent/init.c
src/torrent/magnet.c
test/unit/Makefile.am

index 7fd0a108195b4268b7bea4b229e58492a55e8445..2af39890a193e3f8175123f339e747ca6912005e 100644 (file)
@@ -57,6 +57,7 @@ seederd_SOURCES = \
        src/torrent/file.c \
        src/torrent/free.c \
        src/torrent/info.c \
+       src/torrent/infohash.c \
        src/torrent/init.c \
        src/torrent/magnet.c \
        src/torrent/path.c \
index cd43544a2fa3a93144c1a487c8a52ac95b026c4f..ee1c66f84c71d66610bd180b46ede4e366954121 100644 (file)
@@ -47,6 +47,7 @@ char *torrent_file_path(unsigned char*,size_t);
 int torrent_file_piece_layers(FILE*,struct torrent*);
 void torrent_free(struct torrent*);
 int torrent_init(struct torrent**,unsigned long long);
+int torrent_infohash(struct torrent*);
 char *torrent_magnet(struct torrent*);
 int torrent_remove(struct torrent*,const char*);
 
index 74414638ed48e6e9fb41c539daa3d65b41498bda..cfdc971d1716704722c48bebf024e4f7eacf3ce6 100644 (file)
@@ -2,18 +2,14 @@
 
 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);
+static int torrent_file_write_info(FILE*,struct torrent*);
 static int torrent_file_write_piece_layers(FILE*,struct torrent*);
 static int torrent_file_write_start(FILE*);
 
 int torrent_file(struct torrent *torrent_p) {
-       uint8_t *info;
        FILE *fp;
-       unsigned char infohash[crypto_hash_sha256_BYTES];
        char *path;
-       ssize_t i;
 
-       info = NULL;
        fp = NULL;
        path = NULL;
 
@@ -21,11 +17,9 @@ int torrent_file(struct torrent *torrent_p) {
                if(directory_create(TORRENT_FILE_DIRECTORY)<0) {goto clean; }
        }
 
-       if((i = torrent_file_info(torrent_p,&info))<0) { goto clean; }
-
-       if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { goto clean; }
+       if(torrent_infohash(torrent_p)<0) { goto clean; }
        
-       path = torrent_file_path(infohash,crypto_hash_sha256_BYTES);
+       path = torrent_file_path(torrent_p->infohash,crypto_hash_sha256_BYTES);
        if(NULL==path) { goto clean; }
        
        fp = fopen(path,"w");
@@ -34,9 +28,7 @@ int torrent_file(struct torrent *torrent_p) {
        if(torrent_file_write_start(fp)<0) { goto clean; }
        if(torrent_file_announce(fp,torrent_p)<0) { goto clean; }
 
-       if(torrent_file_write_info(fp,info,i)<0) { goto clean; }
-       free(info);
-       info = NULL;
+       if(torrent_file_write_info(fp,torrent_p)<0) { goto clean; }
 
        if(torrent_file_write_piece_layers(fp,torrent_p)<0) { goto clean; }
 
@@ -49,7 +41,6 @@ int torrent_file(struct torrent *torrent_p) {
 
        return 1;
 clean:
-       if(info!=NULL) { free(info); }
        if(fp!=NULL) { fclose(fp); }
        remove(path);
        if(path!=NULL) { free(path); }
@@ -83,7 +74,8 @@ static int torrent_file_write_end(FILE *fp) {
        return 1;
 }
 
-static int torrent_file_write_info(FILE *fp, uint8_t *info, size_t info_len) {
+static int torrent_file_write_info(FILE *fp, struct torrent *p) {
+       uint8_t *info;
        uint8_t info_string[] = "info";
        uint8_t buf[6];
        ssize_t i;
@@ -91,9 +83,15 @@ static int torrent_file_write_info(FILE *fp, uint8_t *info, size_t info_len) {
        if((i = bencode_string(info_string,sizeof(info_string)-1,buf,6))<0) { return -1; }
        if(i!=fwrite(buf,sizeof(uint8_t),i,fp)) { return -1; }
 
-       if(info_len!=fwrite(info,sizeof(uint8_t),info_len,fp)) { return -1; }
+       if((i = torrent_file_info(p,&info))<0) { return -1; }
+       if(i!=fwrite(info,sizeof(uint8_t),i,fp)) { goto clean; }
+
+       free(info);
 
        return 1;
+clean:
+       free(info);
+       return -1;
 }
 
 static int torrent_file_write_piece_layers(FILE *fp, struct torrent *p) {
diff --git a/src/torrent/infohash.c b/src/torrent/infohash.c
new file mode 100644 (file)
index 0000000..774625a
--- /dev/null
@@ -0,0 +1,16 @@
+#include<torrent.h>
+
+int torrent_infohash(struct torrent *p) {
+       uint8_t *info;
+       ssize_t i;
+
+       if((i = torrent_file_info(p,&info))<0) { return -1; }
+       if(hash(info,i,p->infohash,crypto_hash_sha256_BYTES)<0) { goto clean; }
+
+       free(info);
+
+       return 1;
+clean:
+       free(info);
+       return -1;
+}
index c15a110a95da7678d1e69a4da4fb8b0e555faa62..692c1802745f5a625a5bde88df6770acd58726fc 100644 (file)
@@ -17,6 +17,8 @@ int torrent_init(struct torrent **torrent_p, unsigned long long piece_length) {
        (*torrent_p)->name = NULL;
        (*torrent_p)->feed_url = NULL;
        (*torrent_p)->piece_length = piece_length;
+       
+       memset((*torrent_p)->infohash,0,crypto_hash_sha256_BYTES);
 
        if(tree_init(&((*torrent_p)->tree))<0) { goto clean; }
 
index f0eff3499cc7462b2cf7cbbf3a6131f91f752ab3..d8c301a59dcc1ede6cb9bb42efb362c5f334c5dd 100644 (file)
@@ -4,33 +4,24 @@
 #define MAGNET_LINK_TEMPLATE_LENGTH 24
 
 char *torrent_magnet(struct torrent *torrent_p) {
-       uint8_t *info;
        char *p;
-       unsigned char infohash[crypto_hash_sha256_BYTES];
        char hex[(crypto_hash_sha256_BYTES<<1)+1];
        size_t len;
-       ssize_t i;
 
-       info = NULL;
-       if((i = torrent_file_info(torrent_p,&info))<0) { goto clean; }
-
-       if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { goto clean; }
-       free(info);
-       info = NULL;
+       if(torrent_infohash(torrent_p)<0) { return 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);
+       sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,torrent_p->infohash,crypto_hash_sha256_BYTES);
 
        // 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:
-       if(info!=NULL) { free(info); }
        free(p);
        return NULL;
 }
index 3942a6eabfa4101876050d2041251102d93ab666..17fb5313bcb7580555de96adb958f7f0ddc4a1ea 100644 (file)
@@ -33,6 +33,7 @@ torrent_SOURCES = \
        $(top_srcdir)/src/torrent/file.c \
        $(top_srcdir)/src/torrent/free.c \
        $(top_srcdir)/src/torrent/init.c \
+       $(top_srcdir)/src/torrent/infohash.c \
        $(top_srcdir)/src/torrent/magnet.c \
        $(top_srcdir)/src/torrent/path.c \
        $(top_srcdir)/src/torrent/piece.c \