...
authoralex <[email protected]>
Sat, 23 Oct 2021 01:18:31 +0000 (18:18 -0700)
committeralex <[email protected]>
Sat, 23 Oct 2021 01:18:31 +0000 (18:18 -0700)
21 files changed:
Makefile.am
inc/add.h
inc/file.h
inc/hash.h
inc/init.h
inc/main.h
inc/peer.h [new file with mode: 0644]
inc/server.h [new file with mode: 0644]
inc/usage.h
inc/watch.h [new file with mode: 0644]
src/add.c
src/file.c
src/hash.c
src/init.c
src/main.c
src/server/start.c [new file with mode: 0644]
src/usage.c
src/watch.c [new file with mode: 0644]
test/unit/Makefile.am
test/unit/hash.tests.c [new file with mode: 0644]
test/unit/test_utils.c

index c9390b3ba6220db7c4216b1da9f4628a717b8657..4f480a2c72baa3e9367a7f35a3e6ce782943e525 100644 (file)
@@ -29,6 +29,7 @@ seederd_SOURCES = \
        src/opt/set.c \
        src/opt/watch.c \
        src/opt/worker.c \
+       src/server/start.c \
        src/session.c \
        src/setup.c \
        src/shutdown.c \
@@ -37,7 +38,8 @@ seederd_SOURCES = \
        src/usage.c \
        src/util/dir.c \
        src/util/file.c \
-       src/util/filter.c
+       src/util/filter.c \
+       src/watch.c
 
 seederd_SOURCES += \
        inc/add.h \
@@ -49,12 +51,14 @@ seederd_SOURCES += \
        inc/log.h \
        inc/main.h \
        inc/opt.h \
+       inc/server.h \
        inc/session.h \
        inc/setup.h \
        inc/shutdown.h \
        inc/torrent.h \
        inc/tree.h \
        inc/usage.h \
-       inc/util.h
+       inc/util.h \
+       inc/watch.h
 
 SUBDIRS = . test/unit
index 89c4d0a059284f3f0c7afc0a5db4f6fa7f526580..a9dc464c40c11fdb86e7d0474efb036c29ba33ee 100644 (file)
--- a/inc/add.h
+++ b/inc/add.h
@@ -9,6 +9,7 @@
 #include<util.h>
 
 #define ADD_MESSAGE_ADDING_TORRENT "adding all files in %s to torrent named %s\n"
+#define ADD_MESSAGE_ADDED_FILE "added file %s\n"
 
 #define ADD_QUEUE_INITIAL_SIZE 8
 
index 272852911981a303a1d184b0cc721cd5b8fc161b..ac254b045b3c977486f3faf55a3c1f6bedd9f514 100644 (file)
@@ -15,6 +15,7 @@ struct file {
 };
 
 void file_free(struct file*);
+int file_hash(struct file*);
 int file_init(struct file**,const char*);
 
 #endif
index c61ae6928c6b6231ff472f3577f0352ccd5c8c2a..38f04d556f68abda251a6829b5a2688a58242690 100644 (file)
@@ -3,7 +3,7 @@
 
 #include<sodium.h>
 
-int hash(void*,size_t,unsigned char*,size_t);
+int hash(const void*,size_t,unsigned char*,size_t);
 int hash_init(crypto_hash_sha256_state*);
 int hash_update(crypto_hash_sha256_state*,const void*,size_t);
 int hash_final(crypto_hash_sha256_state*,unsigned char*,size_t);
index f5f06c803598c43a4f41e959d1d6e70d3067ba27..59e1f910d7ce69239cb3010d5e46a68964466c71 100644 (file)
@@ -8,6 +8,8 @@
 #include<shutdown.h>
 #include<usage.h>
 
+extern struct option long_options[];
+
 int init(int,char**);
 
 #endif
index 25f7b40132b800b0a7144f0061662f0d60df01a8..8acd425a34f3c19766f703fc2bcf164c4428a35c 100644 (file)
@@ -5,6 +5,8 @@
 
 #include<add.h>
 #include<init.h>
+#include<server.h>
+#include<watch.h>
 
 int main(int,char**);
 
diff --git a/inc/peer.h b/inc/peer.h
new file mode 100644 (file)
index 0000000..f99ac3c
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef __PEER_H_
+#define __PEER_H_
+
+enum peer_message {
+       PEER_MESSAGE_CHOKE = 0,
+       PEER_MESSAGE_UNCHOKE = 1,
+       PEER_MESSAGE_INTERESTED = 2,
+       PEER_MESSAGE_NOT_INTERESTED = 3,
+       PEER_MESSAGE_HAVE = 4,
+       PEER_MESSAGE_BITFIELD = 5,
+       PEER_MESSAGE_REQUEST = 6,
+       PEER_MESSAGE_PIECE = 7,
+       PEER_MESSAGE_CANCEL = 8,
+       PEER_MESSAGE_REJECT = 16,
+       PEER_MESSAGE_HASH_REQUEST = 21,
+       PEER_MESSAGE_HASHES = 22,
+       PEER_MESSAGE_HASH_REJECT = 23
+};
+
+#endif
diff --git a/inc/server.h b/inc/server.h
new file mode 100644 (file)
index 0000000..00d87c9
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef __SERVER_H_
+#define __SERVER_H_
+
+int server_start();
+
+#endif
index 77d4e41cb74799e10712d486e0effc66e53a3227..d709a3ab67f65457d8ddb6cb4b9f58111611903f 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef __USAGE_H_
 #define __USAGE_H_
 
+#include<init.h>
 #include<log.h>
 
 void usage();
diff --git a/inc/watch.h b/inc/watch.h
new file mode 100644 (file)
index 0000000..31f0d55
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef __WATCH_H_
+#define __WATCH_H_
+
+int watch();
+
+#endif
index 2b61a9c7b2a107ba6fea03d43e96bd5698197665..21035420b434033666fde918b354510b49002762 100644 (file)
--- a/src/add.c
+++ b/src/add.c
@@ -71,18 +71,11 @@ static void *add_hash(void *unused) {
                p = get_next();
                pthread_mutex_unlock(&adding_mutex);
 
-               if(hash(
-                       p->path,
-                       strlen(p->path),
-                       p->root,
-                       crypto_hash_sha256_BYTES
-                               )<0) {
-                       log_err("hash failed\n");
-                       return NULL;
-               }
-
                if(NULL==p) { return NULL; }
-               log_info("to add: %s\n",p->path);
+
+               if(file_hash(p)<0) { return NULL; }
+
+               log_info(ADD_MESSAGE_ADDED_FILE,p->path);
        }
 
        return NULL;
index c9866b5cd0cf03cf583c4184ec45441765336833..009d11bc2fefe03e9663c92491e0eb78d05624fb 100644 (file)
@@ -14,6 +14,10 @@ void file_free(struct file *p) {
        free(p);
 }
 
+int file_hash(struct file *p) {
+       return 1;
+}
+
 int file_init(struct file **p, const char *path) {
        char *b;
 
index 71f4a91b840223bb67a3d8c44456897ca5a8be16..a745eb63e26e5f1ce0ad869713f070dcca173604 100644 (file)
@@ -1,6 +1,6 @@
 #include<hash.h>
 
-int hash(void *data, size_t data_len, unsigned char *out, size_t out_size) {
+int hash(const void *data, size_t data_len, unsigned char *out, size_t out_size) {
        if(crypto_hash_sha256(out,data,data_len)!=0) { return -1; }
 
        return 1;
index 8f4ca50d50e6e8f3530b32b1ab66cc039ec58516..5d6c93c00549b2fc854ab804f33422d9ddbd7010 100644 (file)
@@ -1,6 +1,6 @@
 #include<init.h>
 
-static struct option long_options[] = {
+struct option long_options[] = {
        {"config-file", required_argument, 0, 'c'},
        {"daemon", no_argument, 0, 'd'},
        {"file-filter", required_argument, 0, 'f'},
@@ -8,7 +8,7 @@ static struct option long_options[] = {
        {"log-file", required_argument, 0, 'l'},
        {"quiet", no_argument, 0, 'q'},
        {"verbose", no_argument, 0, 'v'},
-       {"watch", required_argument, 0, 'w'},
+       {"watch-directory", required_argument, 0, 'w'},
        {"worker-threads", required_argument, 0, 1},
        {0,0,0,0}
 };
index eb0ff1f0457805468c570c7cd0fea2dfce053d7a..e761aba37e7ca3a40458876c05e2ca2d127c9be7 100644 (file)
@@ -6,8 +6,10 @@ int main(int argc, char **argv) {
        if(setup_logging()<0) { return EXIT_FAILURE; }
 
        if(add()<0) { return EXIT_FAILURE; }
+       //if(feed()<0) { return EXIT_FAILURE; }
+       if(watch()<0) { return EXIT_FAILURE; }
 
-       log_err("this is a test %d %s\n",10,"what?");
+       if(server_start()<0) { return EXIT_FAILURE; }
 
-       return EXIT_FAILURE;
+       return EXIT_SUCCESS;
 }
diff --git a/src/server/start.c b/src/server/start.c
new file mode 100644 (file)
index 0000000..66124ac
--- /dev/null
@@ -0,0 +1,5 @@
+#include<server.h>
+
+int server_start() {
+       return -1;
+}
index 68c209ba875800a8ee2d1d7b93e858c51dac1d7f..96eeee983e0917156f3eb65de9e70c48af7892ee 100644 (file)
@@ -1,17 +1,53 @@
 #include<usage.h>
 
+static void usage_print_options();
+
 void usage() {
-       log_err("Usage:\n");
-       log_err("\tseederd [options]\n");
-       log_err("\n");
-       log_err("Options:\n");
-       log_err("\t--config-file=<path>, -c <path>\n");
-       log_err("\t--daemon, -d\n");
-       log_err("\t--file-filter=<filter>, -f <filter>\n");
-       log_err("\t--help, -h\n");
-       log_err("\t--log-file=<file>, -l <file>\n");
-       log_err("\t--quiet, -q\n");
-       log_err("\t--verbose, -v\n");
-       log_err("\t--watch=<directory>, -w <directory>\n");
-       log_err("\t--worker-threads=<number of threads>\n");
+       fprintf(stderr,"Usage:\n");
+       fprintf(stderr,"\tseederd [options]\n");
+       fprintf(stderr,"\n");
+       fprintf(stderr,"Options:\n");
+
+       usage_print_options();
+
+       fprintf(stderr,"\n");
+       fprintf(stderr,"See `man seederd` for more information\n");
+}
+
+static void usage_print_options() {
+       size_t i;
+       char *p;
+
+       i = 0;
+       while(
+               !((0==long_options[i].name) &&
+               (0==long_options[i].has_arg) &&
+               (0==long_options[i].flag) &&
+               (0==long_options[i].val))) {
+
+               fprintf(stderr,"\t%s",long_options[i].name);
+
+               p = strrchr(long_options[i].name,'-');
+               if(p!=NULL) {
+                       p++;
+                       if('\0'==*p) {
+                               p = NULL;
+                       }
+               }
+
+               if((required_argument==long_options[i].has_arg)&&(p!=NULL)) {
+                       fprintf(stderr,"=<%s>",p);
+               }
+
+               if(97 <= long_options[i].val && long_options[i].val <= 122) {
+                       fprintf(stderr,", -%c",long_options[i].val);
+
+                       if((required_argument==long_options[i].has_arg)&&(p!=NULL)) {
+                               fprintf(stderr," <%s>",p);
+                       }
+               }
+
+               fprintf(stderr,"\n");
+               i++;
+       }
 }
diff --git a/src/watch.c b/src/watch.c
new file mode 100644 (file)
index 0000000..3977127
--- /dev/null
@@ -0,0 +1,5 @@
+#include<watch.h>
+
+int watch() {
+       return -1;
+}
index fd1acd0a8258570388acc228f78acb34958d25dd..41d1d2daefc4f75e22dac7c35c3965fe8fdb4361 100644 (file)
@@ -12,7 +12,7 @@ AM_CPPFLAGS += \
        -DNDEBUG
 endif
 
-check_PROGRAMS = file.tests hashmap.tests torrent.tests tree.tests util.filter.tests 
+check_PROGRAMS = file.tests hash.tests hashmap.tests torrent.tests tree.tests util.filter.tests 
 TESTS = $(check_PROGRAMS)
 
 if ENABLE_MEMCHECK
@@ -27,6 +27,11 @@ file_tests_SOURCES = \
        file.tests.c \
        $(top_srcdir)/src/file.c
 
+hash_tests_SOURCES = \
+       $(common_SOURCES) \
+       hash.tests.c \
+       $(top_srcdir)/src/hash.c
+
 hashmap_tests_SOURCES = \
        $(common_SOURCES) \
        hashmap.tests.c \
diff --git a/test/unit/hash.tests.c b/test/unit/hash.tests.c
new file mode 100644 (file)
index 0000000..620339b
--- /dev/null
@@ -0,0 +1,98 @@
+#include<test_utils.h>
+
+#include<hash.h>
+
+int main();
+static void hash_basic_tests();
+static void hash_multi_step_conformance_test();
+
+int main() {
+       setup_env();
+
+       hash_basic_tests();
+       hash_multi_step_conformance_test();
+
+       clean_env();
+
+       return EXIT_SUCCESS;
+}
+
+static void hash_basic_tests() {
+       unsigned char expected[crypto_hash_sha256_BYTES];
+       unsigned char out[crypto_hash_sha256_BYTES];
+       size_t i;
+
+       const char str1[] = "test\n";
+       const char expected1[] = "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2";
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,expected1,strlen(expected1),NULL,&i,NULL)==0);
+       assert(i==32);
+
+       assert(hash(str1,strlen(str1),out,crypto_hash_sha256_BYTES)==1);
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+
+       const char expected2[] = "ae49be25bdf4ec71127aff1405e54d97bb481b4bcb20199a51f9131d7607e4e5";
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,expected2,strlen(expected2),NULL,&i,NULL)==0);
+       assert(i==32);
+       
+       assert(hash(TEST_FILE_1_CONTENTS,strlen(TEST_FILE_1_CONTENTS),out,crypto_hash_sha256_BYTES)==1);
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+
+       const char expected3[] = "477e8d55511501d1fd3f0ac6e3872c70d38071f0f304b948058b7959299bf505";
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,expected3,strlen(expected3),NULL,&i,NULL)==0);
+       assert(i==32);
+       
+       assert(hash(TEST_FILE_2_CONTENTS,strlen(TEST_FILE_2_CONTENTS),out,crypto_hash_sha256_BYTES)==1);
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+
+
+       const char expected4[] = "2f293857380375ececc0af34acce4469d7662f932fd5d85c0d0c75df1cb3a99d";
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,expected4,strlen(expected4),NULL,&i,NULL)==0);
+       assert(i==32);
+       
+       assert(hash(TEST_FILE_3_CONTENTS,strlen(TEST_FILE_3_CONTENTS),out,crypto_hash_sha256_BYTES)==1);
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+       
+       const char expected5[] = "0db92f82ba8d282b6c04c40c94a584b6804c532414bf1f3bda58d584e40d8b9f";
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,expected5,strlen(expected5),NULL,&i,NULL)==0);
+       assert(i==32);
+       
+       assert(hash(TEST_FILE_4_CONTENTS,strlen(TEST_FILE_4_CONTENTS),out,crypto_hash_sha256_BYTES)==1);
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+}
+
+static void hash_multi_step_conformance_test() {
+       unsigned char expected[crypto_hash_sha256_BYTES];
+       unsigned char out[crypto_hash_sha256_BYTES];
+       crypto_hash_sha256_state state;
+       size_t i, len;
+       const char *p;
+
+       const char str[] = "alskdjflkasdfjasdfjalskdjflkasjdflkasjdlkfjaslkdfjklasdjfklajsdfkljaslkdfjlaksjdfklajsdklfjaskldfj";
+       const char expected_hash[] = "5deca19259f33b54ca65a61da595af75ac66209c4a851176a01ef201bced1cf3";
+       
+       assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,expected_hash,strlen(expected_hash),NULL,&i,NULL)==0);
+       assert(i==32);
+       
+       len = strlen(str);
+       assert(hash(str,len,out,crypto_hash_sha256_BYTES)==1);
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+
+       assert(hash_init(&state)==1);
+       p = str;
+       while(len>0) {
+               i = rand()%len;
+               if(i==0) {
+                       i = len;
+               }
+
+               if(i>0) {
+                       assert(hash_update(&state,p,i)==1);
+                       p += i;
+                       len -= i;
+               }
+       }
+
+       memset(out,0,crypto_hash_sha256_BYTES);
+       assert(hash_final(&state,out,crypto_hash_sha256_BYTES)==1);
+       assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
+}
index fe98e51102e85a88f91d1c2e85954fc61a469638..a79c9d1d61c5bc6ecfc3f057f093838f31ccf994 100644 (file)
@@ -20,6 +20,8 @@ void setup_env() {
        create_test_directory(TEST_DIRECTORY);
        create_test_file(TEST_FILE_1,TEST_FILE_1_CONTENTS);
        create_test_file(TEST_FILE_2,TEST_FILE_2_CONTENTS);
+       create_test_file(TEST_FILE_3,TEST_FILE_3_CONTENTS);
+       create_test_file(TEST_FILE_4,TEST_FILE_4_CONTENTS);
 }
 
 static void create_test_directory(const char *directory) {