From: alex Date: Sat, 23 Oct 2021 01:18:31 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=35f12155d6742efdfdca4ce7ebdcb24fb81c4731;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index c9390b3..4f480a2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/inc/add.h b/inc/add.h index 89c4d0a..a9dc464 100644 --- a/inc/add.h +++ b/inc/add.h @@ -9,6 +9,7 @@ #include #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 diff --git a/inc/file.h b/inc/file.h index 2728529..ac254b0 100644 --- a/inc/file.h +++ b/inc/file.h @@ -15,6 +15,7 @@ struct file { }; void file_free(struct file*); +int file_hash(struct file*); int file_init(struct file**,const char*); #endif diff --git a/inc/hash.h b/inc/hash.h index c61ae69..38f04d5 100644 --- a/inc/hash.h +++ b/inc/hash.h @@ -3,7 +3,7 @@ #include -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); diff --git a/inc/init.h b/inc/init.h index f5f06c8..59e1f91 100644 --- a/inc/init.h +++ b/inc/init.h @@ -8,6 +8,8 @@ #include #include +extern struct option long_options[]; + int init(int,char**); #endif diff --git a/inc/main.h b/inc/main.h index 25f7b40..8acd425 100644 --- a/inc/main.h +++ b/inc/main.h @@ -5,6 +5,8 @@ #include #include +#include +#include int main(int,char**); diff --git a/inc/peer.h b/inc/peer.h new file mode 100644 index 0000000..f99ac3c --- /dev/null +++ b/inc/peer.h @@ -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 index 0000000..00d87c9 --- /dev/null +++ b/inc/server.h @@ -0,0 +1,6 @@ +#ifndef __SERVER_H_ +#define __SERVER_H_ + +int server_start(); + +#endif diff --git a/inc/usage.h b/inc/usage.h index 77d4e41..d709a3a 100644 --- a/inc/usage.h +++ b/inc/usage.h @@ -1,6 +1,7 @@ #ifndef __USAGE_H_ #define __USAGE_H_ +#include #include void usage(); diff --git a/inc/watch.h b/inc/watch.h new file mode 100644 index 0000000..31f0d55 --- /dev/null +++ b/inc/watch.h @@ -0,0 +1,6 @@ +#ifndef __WATCH_H_ +#define __WATCH_H_ + +int watch(); + +#endif diff --git a/src/add.c b/src/add.c index 2b61a9c..2103542 100644 --- 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; diff --git a/src/file.c b/src/file.c index c9866b5..009d11b 100644 --- a/src/file.c +++ b/src/file.c @@ -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; diff --git a/src/hash.c b/src/hash.c index 71f4a91..a745eb6 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1,6 +1,6 @@ #include -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; diff --git a/src/init.c b/src/init.c index 8f4ca50..5d6c93c 100644 --- a/src/init.c +++ b/src/init.c @@ -1,6 +1,6 @@ #include -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} }; diff --git a/src/main.c b/src/main.c index eb0ff1f..e761aba 100644 --- a/src/main.c +++ b/src/main.c @@ -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 index 0000000..66124ac --- /dev/null +++ b/src/server/start.c @@ -0,0 +1,5 @@ +#include + +int server_start() { + return -1; +} diff --git a/src/usage.c b/src/usage.c index 68c209b..96eeee9 100644 --- a/src/usage.c +++ b/src/usage.c @@ -1,17 +1,53 @@ #include +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=, -c \n"); - log_err("\t--daemon, -d\n"); - log_err("\t--file-filter=, -f \n"); - log_err("\t--help, -h\n"); - log_err("\t--log-file=, -l \n"); - log_err("\t--quiet, -q\n"); - log_err("\t--verbose, -v\n"); - log_err("\t--watch=, -w \n"); - log_err("\t--worker-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 index 0000000..3977127 --- /dev/null +++ b/src/watch.c @@ -0,0 +1,5 @@ +#include + +int watch() { + return -1; +} diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index fd1acd0..41d1d2d 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -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 index 0000000..620339b --- /dev/null +++ b/test/unit/hash.tests.c @@ -0,0 +1,98 @@ +#include + +#include + +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); +} diff --git a/test/unit/test_utils.c b/test/unit/test_utils.c index fe98e51..a79c9d1 100644 --- a/test/unit/test_utils.c +++ b/test/unit/test_utils.c @@ -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) {