From 5cff01b2a857bc009642ec19b5958b3d698c0324 Mon Sep 17 00:00:00 2001 From: Shane Bryldt Date: Wed, 22 Mar 2017 17:47:33 -0500 Subject: [PATCH] FS-10165: Removed dependancy and autoconf check for libsodium from libks. Moved DHT, bencode, and related tests into libblade. Still requires refactoring in blade from ks_* to blade_*. --- libs/libblade/Makefile.am | 6 +++++ libs/{libks => libblade}/src/bencode.c | 0 libs/{libks => libblade}/src/dht/ks_dht.c | 0 .../src/dht/ks_dht_bucket.c | 0 .../src/dht/ks_dht_datagram.c | 0 .../src/dht/ks_dht_distribute.c | 0 .../src/dht/ks_dht_endpoint.c | 0 libs/{libks => libblade}/src/dht/ks_dht_job.c | 0 .../src/dht/ks_dht_message.c | 0 .../src/dht/ks_dht_publish.c | 0 .../src/dht/ks_dht_search.c | 0 .../src/dht/ks_dht_storageitem.c | 0 .../src/dht/ks_dht_transaction.c | 0 libs/libblade/src/include/blade.h | 2 ++ .../src/include/ks_bencode.h | 0 .../src/include/ks_dht-int.h | 0 libs/{libks => libblade}/src/include/ks_dht.h | 0 libs/{libks => libblade}/src/ks_bencode.c | 12 +++++----- libs/libblade/test/Makefile.am | 15 ++++++++++++ libs/{libks => libblade}/test/nodeidgen.c | 0 libs/{libks => libblade}/test/testbuckets.c | 0 libs/{libks => libblade}/test/testdht2.c | 0 libs/libks/Makefile.am | 9 +++---- libs/libks/configure.ac | 1 - libs/libks/src/include/ks.h | 2 -- libs/libks/src/ks_rng.c | 21 +++------------- libs/libks/test/Makefile.am | 15 ------------ libs/libks/test/testhash.c | 24 ++++++++++--------- 28 files changed, 48 insertions(+), 59 deletions(-) rename libs/{libks => libblade}/src/bencode.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_bucket.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_datagram.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_distribute.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_endpoint.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_job.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_message.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_publish.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_search.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_storageitem.c (100%) rename libs/{libks => libblade}/src/dht/ks_dht_transaction.c (100%) rename libs/{libks => libblade}/src/include/ks_bencode.h (100%) rename libs/{libks => libblade}/src/include/ks_dht-int.h (100%) rename libs/{libks => libblade}/src/include/ks_dht.h (100%) rename libs/{libks => libblade}/src/ks_bencode.c (99%) rename libs/{libks => libblade}/test/nodeidgen.c (100%) rename libs/{libks => libblade}/test/testbuckets.c (100%) rename libs/{libks => libblade}/test/testdht2.c (100%) diff --git a/libs/libblade/Makefile.am b/libs/libblade/Makefile.am index f3ad18d3e0..0140b81233 100644 --- a/libs/libblade/Makefile.am +++ b/libs/libblade/Makefile.am @@ -16,15 +16,21 @@ libblade_la_SOURCES += src/blade_datastore.c libblade_la_SOURCES += src/blade_identity.c src/blade_module.c src/blade_connection.c libblade_la_SOURCES += src/blade_session.c src/blade_protocol.c src/blade_space.c src/blade_method.c libblade_la_SOURCES += src/blade_module_wss.c src/blade_module_chat.c +libblade_la_SOURCES += src/dht/ks_dht.c src/dht/ks_dht_datagram.c src/dht/ks_dht_endpoint.c src/dht/ks_dht_message.c src/dht/ks_dht_transaction.c +libblade_la_SOURCES += src/dht/ks_dht_job.c src/dht/ks_dht_search.c src/dht/ks_dht_publish.c src/dht/ks_dht_distribute.c src/dht/ks_dht_storageitem.c +libblade_la_SOURCES += src/dht/ks_dht_bucket.c src/ks_bencode.c + libblade_la_CFLAGS = $(AM_CFLAGS) $(AM_CPPFLAGS) libblade_la_LDFLAGS = -version-info 0:1:0 -lncurses -lpthread -lm -lconfig $(AM_LDFLAGS) libblade_la_LIBADD = libunqlite.la + library_includedir = $(prefix)/include library_include_HEADERS = src/include/blade.h src/include/blade_types.h src/include/blade_stack.h library_include_HEADERS += src/include/blade_datastore.h library_include_HEADERS += src/include/blade_identity.h src/include/blade_module.h src/include/blade_connection.h library_include_HEADERS += src/include/blade_session.h src/include/blade_protocol.h src/include/blade_space.h src/include/blade_method.h library_include_HEADERS += src/include/unqlite.h test/tap.h +library_include_HEADERS += src/include/ks_dht.h src/include/ks_bencode.h tests: libblade.la $(MAKE) -C test tests diff --git a/libs/libks/src/bencode.c b/libs/libblade/src/bencode.c similarity index 100% rename from libs/libks/src/bencode.c rename to libs/libblade/src/bencode.c diff --git a/libs/libks/src/dht/ks_dht.c b/libs/libblade/src/dht/ks_dht.c similarity index 100% rename from libs/libks/src/dht/ks_dht.c rename to libs/libblade/src/dht/ks_dht.c diff --git a/libs/libks/src/dht/ks_dht_bucket.c b/libs/libblade/src/dht/ks_dht_bucket.c similarity index 100% rename from libs/libks/src/dht/ks_dht_bucket.c rename to libs/libblade/src/dht/ks_dht_bucket.c diff --git a/libs/libks/src/dht/ks_dht_datagram.c b/libs/libblade/src/dht/ks_dht_datagram.c similarity index 100% rename from libs/libks/src/dht/ks_dht_datagram.c rename to libs/libblade/src/dht/ks_dht_datagram.c diff --git a/libs/libks/src/dht/ks_dht_distribute.c b/libs/libblade/src/dht/ks_dht_distribute.c similarity index 100% rename from libs/libks/src/dht/ks_dht_distribute.c rename to libs/libblade/src/dht/ks_dht_distribute.c diff --git a/libs/libks/src/dht/ks_dht_endpoint.c b/libs/libblade/src/dht/ks_dht_endpoint.c similarity index 100% rename from libs/libks/src/dht/ks_dht_endpoint.c rename to libs/libblade/src/dht/ks_dht_endpoint.c diff --git a/libs/libks/src/dht/ks_dht_job.c b/libs/libblade/src/dht/ks_dht_job.c similarity index 100% rename from libs/libks/src/dht/ks_dht_job.c rename to libs/libblade/src/dht/ks_dht_job.c diff --git a/libs/libks/src/dht/ks_dht_message.c b/libs/libblade/src/dht/ks_dht_message.c similarity index 100% rename from libs/libks/src/dht/ks_dht_message.c rename to libs/libblade/src/dht/ks_dht_message.c diff --git a/libs/libks/src/dht/ks_dht_publish.c b/libs/libblade/src/dht/ks_dht_publish.c similarity index 100% rename from libs/libks/src/dht/ks_dht_publish.c rename to libs/libblade/src/dht/ks_dht_publish.c diff --git a/libs/libks/src/dht/ks_dht_search.c b/libs/libblade/src/dht/ks_dht_search.c similarity index 100% rename from libs/libks/src/dht/ks_dht_search.c rename to libs/libblade/src/dht/ks_dht_search.c diff --git a/libs/libks/src/dht/ks_dht_storageitem.c b/libs/libblade/src/dht/ks_dht_storageitem.c similarity index 100% rename from libs/libks/src/dht/ks_dht_storageitem.c rename to libs/libblade/src/dht/ks_dht_storageitem.c diff --git a/libs/libks/src/dht/ks_dht_transaction.c b/libs/libblade/src/dht/ks_dht_transaction.c similarity index 100% rename from libs/libks/src/dht/ks_dht_transaction.c rename to libs/libblade/src/dht/ks_dht_transaction.c diff --git a/libs/libblade/src/include/blade.h b/libs/libblade/src/include/blade.h index c94c085511..9e41a1fc76 100644 --- a/libs/libblade/src/include/blade.h +++ b/libs/libblade/src/include/blade.h @@ -48,6 +48,8 @@ #include "blade_datastore.h" #include "blade_space.h" #include "blade_method.h" +#include "ks_dht.h" +#include "ks_bencode.h" KS_BEGIN_EXTERN_C diff --git a/libs/libks/src/include/ks_bencode.h b/libs/libblade/src/include/ks_bencode.h similarity index 100% rename from libs/libks/src/include/ks_bencode.h rename to libs/libblade/src/include/ks_bencode.h diff --git a/libs/libks/src/include/ks_dht-int.h b/libs/libblade/src/include/ks_dht-int.h similarity index 100% rename from libs/libks/src/include/ks_dht-int.h rename to libs/libblade/src/include/ks_dht-int.h diff --git a/libs/libks/src/include/ks_dht.h b/libs/libblade/src/include/ks_dht.h similarity index 100% rename from libs/libks/src/include/ks_dht.h rename to libs/libblade/src/include/ks_dht.h diff --git a/libs/libks/src/ks_bencode.c b/libs/libblade/src/ks_bencode.c similarity index 99% rename from libs/libks/src/ks_bencode.c rename to libs/libblade/src/ks_bencode.c index 2bf4cca7ff..24ff8fe7b6 100644 --- a/libs/libks/src/ks_bencode.c +++ b/libs/libblade/src/ks_bencode.c @@ -19,7 +19,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ -#include +#include #include #include @@ -350,7 +350,7 @@ struct bencode *ben_clone(const struct bencode *b) return clone_str(ben_str_const_cast(b)); default: die("Invalid type %c\n", b->type); - } + } } struct bencode *ben_shared_clone(const struct bencode *b) @@ -549,7 +549,7 @@ static int resize_dict(struct bencode_dict *d, size_t newalloc) assert(x >= newalloc); newalloc = x; if (newalloc > DICT_MAX_ALLOC) - return -1; + return -1; } /* size must be a power of two */ @@ -619,7 +619,7 @@ long long ben_hash(const struct bencode *b) return ben_str_hash(b); default: die("hash: Invalid type: %d\n", b->type); - } + } } static struct bencode *decode_dict(struct ben_decode_ctx *ctx) @@ -938,7 +938,7 @@ static struct bencode *decode_printed_bool(struct ben_decode_ctx *ctx) struct bencode *b; int bval = -1; - if (try_match(ctx, "True")) { + if (try_match(ctx, "True")) { if (ben_need_bytes(ctx, 4)) return ben_insufficient_ptr(ctx); } else { @@ -1868,7 +1868,7 @@ static void dict_unlink(struct bencode_dict *d, size_t bucket, size_t unlinkpos) } /* Remove node from the linked list, if found */ -static struct bencode *dict_pop(struct bencode_dict *d, +static struct bencode *dict_pop(struct bencode_dict *d, const struct bencode *key, long long hash) { struct bencode *value; diff --git a/libs/libblade/test/Makefile.am b/libs/libblade/test/Makefile.am index 514af0198c..04c7be4c69 100644 --- a/libs/libblade/test/Makefile.am +++ b/libs/libblade/test/Makefile.am @@ -18,6 +18,21 @@ blades_SOURCES = blades.c tap.c blades_CFLAGS = $(AM_CFLAGS) blades_LDADD = $(TEST_LDADD) +check_PROGRAMS += testdht2 +testdht2_SOURCES = testdht2.c tap.c +testdht2_CFLAGS = $(AM_CFLAGS) +testdht2_LDADD = $(TEST_LDADD) + +check_PROGRAMS += testbuckets +testbuckets_SOURCES = testbuckets.c tap.c +testbuckets_CFLAGS = $(AM_CFLAGS) +testbuckets_LDADD = $(TEST_LDADD) + +check_PROGRAMS += nodeidgen +nodeidgen_SOURCES = nodeidgen.c tap.c +nodeidgen_CFLAGS = $(AM_CFLAGS) +nodeidgen_LDADD = $(TEST_LDADD) + TESTS=$(check_PROGRAMS) diff --git a/libs/libks/test/nodeidgen.c b/libs/libblade/test/nodeidgen.c similarity index 100% rename from libs/libks/test/nodeidgen.c rename to libs/libblade/test/nodeidgen.c diff --git a/libs/libks/test/testbuckets.c b/libs/libblade/test/testbuckets.c similarity index 100% rename from libs/libks/test/testbuckets.c rename to libs/libblade/test/testbuckets.c diff --git a/libs/libks/test/testdht2.c b/libs/libblade/test/testdht2.c similarity index 100% rename from libs/libks/test/testdht2.c rename to libs/libblade/test/testdht2.c diff --git a/libs/libks/Makefile.am b/libs/libks/Makefile.am index 5a4bb8c850..1ce1bef723 100644 --- a/libs/libks/Makefile.am +++ b/libs/libks/Makefile.am @@ -9,13 +9,10 @@ AM_CPPFLAGS = $(AM_CFLAGS) lib_LTLIBRARIES = libks.la libks_la_SOURCES = src/ks.c src/ks_string.c src/ks_json.c src/cJSON.c src/cJSON_Utils.c src/ks_thread.c src/ks_thread_pool.c src/ks_mutex.c src/ks_config.c libks_la_SOURCES += src/ks_log.c src/ks_socket.c src/ks_buffer.c src/ks_pool.c src/simclist.c -libks_la_SOURCES += src/ks_time.c src/ks_printf.c src/ks_hash.c src/ks_q.c src/ks_dso.c # src/ks_dht.c +libks_la_SOURCES += src/ks_time.c src/ks_printf.c src/ks_hash.c src/ks_q.c src/ks_dso.c libks_la_SOURCES += src/ks_ssl.c src/kws.c src/ks_rng.c src/ks_base64.c libks_la_SOURCES += src/utp/utp_api.cpp src/utp/utp_callbacks.cpp src/utp/utp_hash.cpp src/utp/utp_internal.cpp -libks_la_SOURCES += src/utp/utp_packedsockaddr.cpp src/utp/utp_utils.cpp src/ks_bencode.c -libks_la_SOURCES += src/dht/ks_dht.c src/dht/ks_dht_datagram.c src/dht/ks_dht_endpoint.c src/dht/ks_dht_message.c src/dht/ks_dht_transaction.c -libks_la_SOURCES += src/dht/ks_dht_job.c src/dht/ks_dht_search.c src/dht/ks_dht_publish.c src/dht/ks_dht_distribute.c src/dht/ks_dht_storageitem.c -libks_la_SOURCES += src/dht/ks_dht_bucket.c +libks_la_SOURCES += src/utp/utp_packedsockaddr.cpp src/utp/utp_utils.cpp libks_la_SOURCES += crypt/aeskey.c crypt/aestab.c crypt/sha2.c crypt/twofish.c crypt/aes_modes.c crypt/aescrypt.c crypt/twofish_cfb.c #aes.h aescpp.h brg_endian.h aesopt.h aestab.h brg_types.h sha2.h twofish.h libks_la_SOURCES += src/ks_acl.c @@ -28,7 +25,7 @@ library_includedir = $(prefix)/include library_include_HEADERS = src/include/ks_config.h src/include/ks.h src/include/ks_threadmutex.h src/include/ks_json.h src/include/ks_buffer.h library_include_HEADERS += src/include/ks_thread_pool.h src/include/ks_cJSON.h src/include/ks_cJSON_Utils.h library_include_HEADERS += src/include/ks_pool.h src/include/simclist.h src/include/ks_time.h src/include/ks_q.h src/include/ks_socket.h -library_include_HEADERS += src/include/ks_dso.h src/include/ks_platform.h src/include/ks_types.h # src/include/ks_rng.h src/include/ks_dht.h +library_include_HEADERS += src/include/ks_dso.h src/include/ks_platform.h src/include/ks_types.h src/include/ks_rng.h library_include_HEADERS += src/include/ks_printf.h src/include/ks_hash.h src/include/ks_ssl.h src/include/kws.h library_include_HEADERS += src/include/ks_base64.h library_include_HEADERS += src/utp/utp_internal.h src/utp/utp.h src/utp/utp_types.h src/utp/utp_callbacks.h src/utp/utp_templates.h diff --git a/libs/libks/configure.ac b/libs/libks/configure.ac index ef8bce7134..94a2f08528 100644 --- a/libs/libks/configure.ac +++ b/libs/libks/configure.ac @@ -242,7 +242,6 @@ if test "${enable_address_sanitizer}" = "yes"; then AM_LDFLAGS="${AM_LDFLAGS} -fsanitize=address" fi -PKG_CHECK_MODULES([SODIUM], [libsodium >= 1.0.0],[AC_MSG_RESULT([yes])],[AC_MSG_ERROR([libsodium is required])]) PKG_CHECK_MODULES([UUID], [uuid >= 1.0.0],[AC_MSG_RESULT([yes])],[AC_MSG_ERROR([libuuid is required])]) AM_CFLAGS="${AM_CFLAGS} -Werror ${SODIUM_CFLAGS} ${UUID_CFLAGS}" diff --git a/libs/libks/src/include/ks.h b/libs/libks/src/include/ks.h index d5094f29a6..a03b753320 100644 --- a/libs/libks/src/include/ks.h +++ b/libs/libks/src/include/ks.h @@ -131,12 +131,10 @@ KS_DECLARE(void) ks_random_string(char *buf, uint16_t len, char *set); #include "ks_time.h" #include "ks_socket.h" #include "ks_dso.h" -//#include "ks_dht.h" #include "ks_utp.h" #include "simclist.h" #include "ks_ssl.h" #include "kws.h" -#include "ks_bencode.h" #include "ks_rng.h" #include "ks_acl.h" #include "ks_base64.h" diff --git a/libs/libks/src/ks_rng.c b/libs/libks/src/ks_rng.c index f98bf5cd5a..78a08b0f97 100644 --- a/libs/libks/src/ks_rng.c +++ b/libs/libks/src/ks_rng.c @@ -18,7 +18,6 @@ */ #include "ks.h" -#include "sodium.h" #include #include @@ -40,7 +39,7 @@ int fd = -1; * memset(buf, val, len), but the use of a volatile pointer * guarantees that the compiler will not optimise the call away. */ -//static void * (*volatile memset_volatile)(void *, int, size_t) = memset; +static void * (*volatile memset_volatile)(void *, int, size_t) = memset; KS_DECLARE(uuid_t *) ks_uuid(uuid_t *uuid) { @@ -71,16 +70,12 @@ KS_DECLARE(char *) ks_uuid_str(ks_pool_t *pool, uuid_t *uuid) KS_DECLARE(ks_status_t) ks_rng_init(void) { if (!initialized) { - if (sodium_init() == -1) { - abort(); - } - - randombytes_random(); + ks_aes_init(); ks_mutex_create(&rng_mutex, KS_MUTEX_FLAG_DEFAULT, ks_global_pool()); #ifdef __WINDOWS__ - if (!crypt_provider) { + if (!crypt_provider) { if (CryptAcquireContext(&crypt_provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == TRUE) { initialized = KS_TRUE; } else { @@ -174,14 +169,6 @@ KS_DECLARE(size_t) ks_rng_add_entropy(const uint8_t *buffer, size_t length) return length; } -KS_DECLARE(size_t) ks_rng_get_data(uint8_t* buffer, size_t length) { - randombytes_buf(buffer, length); - return length; -} - - -#if 0 - KS_DECLARE(size_t) ks_rng_get_data(uint8_t* buffer, size_t length) { aes_encrypt_ctx cx[1]; @@ -237,8 +224,6 @@ KS_DECLARE(size_t) ks_rng_get_data(uint8_t* buffer, size_t length) { return generated; } -#endif - /* For Emacs: * Local Variables: * mode:c diff --git a/libs/libks/test/Makefile.am b/libs/libks/test/Makefile.am index cffc18bdcd..12712cadfe 100644 --- a/libs/libks/test/Makefile.am +++ b/libs/libks/test/Makefile.am @@ -4,11 +4,6 @@ check_PROGRAMS = EXTRA_DIST = tap.h -check_PROGRAMS += testbuckets -testbuckets_SOURCES = testbuckets.c tap.c -testbuckets_CFLAGS = $(AM_CFLAGS) -testbuckets_LDADD = $(TEST_LDADD) - check_PROGRAMS += testpools testpools_SOURCES = testpools.c tap.c testpools_CFLAGS = $(AM_CFLAGS) @@ -64,16 +59,6 @@ testwebsock_LDADD = $(TEST_LDADD) #testdht_CFLAGS = $(AM_CFLAGS) #testdht_LDADD = $(TEST_LDADD) -check_PROGRAMS += testdht2 -testdht2_SOURCES = testdht2.c tap.c -testdht2_CFLAGS = $(AM_CFLAGS) -testdht2_LDADD = $(TEST_LDADD) - -check_PROGRAMS += nodeidgen -nodeidgen_SOURCES = nodeidgen.c tap.c -nodeidgen_CFLAGS = $(AM_CFLAGS) -nodeidgen_LDADD = $(TEST_LDADD) - #check_PROGRAMS += testdht_net #testdht_net_SOURCES = testdht-net.c tap.c #testdht_net_CFLAGS = $(AM_CFLAGS) diff --git a/libs/libks/test/testhash.c b/libs/libks/test/testhash.c index 7b7bdc24e2..c0987f28b9 100644 --- a/libs/libks/test/testhash.c +++ b/libs/libks/test/testhash.c @@ -18,7 +18,7 @@ int test1(void) } - + ks_hash_iterator_t *itt; ks_hash_write_lock(hash); @@ -53,9 +53,9 @@ static void *test2_thread(ks_thread_t *thread, void *data) for (itt = ks_hash_first(hash, KS_READLOCKED); itt; itt = ks_hash_next(&itt)) { const void *key; void *val; - + ks_hash_this(itt, &key, NULL, &val); - + printf("%p ITT %s=%s\n", (void *)(intptr_t)ks_thread_self(), (char *)key, (char *)val); } ks_sleep(100000); @@ -81,7 +81,7 @@ int test2(void) for (i = 0; i < ttl; i++) { ks_thread_create(&threads[i], test2_thread, hash, pool); } - + for(i = 0; i < runs; i++) { int x = rand() % 5; int j; @@ -93,21 +93,21 @@ int test2(void) } ks_sleep(x * 1000000); - + ks_hash_write_lock(hash); for (itt = ks_hash_first(hash, KS_UNLOCKED); itt; itt = ks_hash_next(&itt)) { const void *key; void *val; - + ks_hash_this(itt, &key, NULL, &val); - + printf("DEL %s=%s\n", (char *)key, (char *)val); ks_hash_remove(hash, (char *)key); } ks_hash_write_unlock(hash); } - + for (i = 0; i < ttl; i++) { threads[i]->running = 0; ks_thread_join(threads[i]); @@ -135,8 +135,10 @@ int test3(void) ks_hash_create(&hash, KS_HASH_MODE_ARBITRARY, KS_HASH_FLAG_NOLOCK, pool); ks_hash_set_keysize(hash, TEST3_SIZE); - randombytes_buf(data, sizeof(data)); - randombytes_buf(data2, sizeof(data2)); + ks_rng_get_data(data, sizeof(data)); + ks_rng_get_data(data2, sizeof(data)); + //randombytes_buf(data, sizeof(data)); + //randombytes_buf(data2, sizeof(data2)); ks_hash_insert(hash, data, "FOO"); ks_hash_insert(hash, data2, "BAR"); @@ -155,7 +157,7 @@ int test3(void) ks_pool_close(&pool); return !strcmp(A, "FOO") && !strcmp(B, "BAR") && !strcmp(C, "BAZ"); - + }