From c40ba83cb3277926e71e346fc7f010f0dac3fd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Mon, 11 Apr 2022 17:16:29 -0400 Subject: [PATCH] c: support building with autotools --- Makefile.am | 4 ++++ c/Makefile.am | 12 ++++++++++++ c/opendht_c.h | 2 +- configure.ac | 8 ++++++++ tools/Makefile.am | 7 +++++++ tools/dhtcnode.c | 36 ++++++++++++++++++++---------------- 6 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 c/Makefile.am diff --git a/Makefile.am b/Makefile.am index 7ba6efbb..498329ba 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,6 +4,10 @@ SUBDIRS = SUBDIRS += src +if ENABLE_C +SUBDIRS += c +endif + if ENABLE_TOOLS SUBDIRS += tools endif diff --git a/c/Makefile.am b/c/Makefile.am new file mode 100644 index 00000000..4e690d79 --- /dev/null +++ b/c/Makefile.am @@ -0,0 +1,12 @@ +lib_LTLIBRARIES = libopendht-c.la +noinst_HEADERS = opendht_c.h + +AM_CPPFLAGS = -isystem @top_srcdir@/include @JsonCpp_CFLAGS@ @MsgPack_CFLAGS@ + +libopendht_c_la_LIBADD = $(LIBOBJS) ../src/.libs/libopendht.la + +libopendht_c_la_SOURCES = \ + opendht.cpp + +nobase_include_HEADERS = \ + opendht_c.h diff --git a/c/opendht_c.h b/c/opendht_c.h index 3d2c72ab..4b3bb729 100644 --- a/c/opendht_c.h +++ b/c/opendht_c.h @@ -22,7 +22,7 @@ extern "C" { #endif -#include "def.h" +#include <opendht/def.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdbool.h> diff --git a/configure.ac b/configure.ac index 1e006df7..b424b802 100644 --- a/configure.ac +++ b/configure.ac @@ -32,6 +32,13 @@ AS_IF([test "x$enable_logs" != "xno"], [ AC_DEFINE([OPENDHT_LOG], [false], [Define if DHT logs are enabled]) ]) +dnl Check for C binding +AC_ARG_ENABLE([c], [AS_HELP_STRING([--disable-c], [Disable DHT C binding])]) +AM_CONDITIONAL(ENABLE_C, test x$enable_c != "xno") +AM_COND_IF(ENABLE_C, [ + AC_DEFINE([OPENDHT_C], [], [Define if DHT C biding is enabled]) +]) + dnl Check for indexation AC_ARG_ENABLE([indexation], [AS_HELP_STRING([--disable-indexation], [Disable DHT indexation])]) AM_CONDITIONAL(ENABLE_INDEXATION, test x$enable_indexation != "xno") @@ -198,6 +205,7 @@ AC_SUBST(PROJECT_BINARY_DIR, "../src/.libs") AC_CONFIG_FILES([Makefile src/Makefile + c/Makefile tools/Makefile python/Makefile python/setup.py diff --git a/tools/Makefile.am b/tools/Makefile.am index 14284d02..0e8ec15a 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -11,3 +11,10 @@ dhtchat_LDFLAGS = -lopendht -lreadline -L@top_builddir@/src/.libs @GnuTLS_LIBS@ dhtscanner_SOURCES = dhtscanner.cpp dhtscanner_LDFLAGS = -lopendht -lreadline -L@top_builddir@/src/.libs @GnuTLS_LIBS@ + +if ENABLE_C +bin_PROGRAMS += dhtcnode +dhtcnode_CFLAGS = -isystem @top_srcdir@/c -isystem @top_srcdir@/include +dhtcnode_SOURCES = dhtcnode.c +dhtcnode_LDFLAGS = -lopendht-c -L@top_builddir@/c/.libs +endif diff --git a/tools/dhtcnode.c b/tools/dhtcnode.c index 7a7df77c..21d9da86 100644 --- a/tools/dhtcnode.c +++ b/tools/dhtcnode.c @@ -75,37 +75,41 @@ int main() pk_id = dht_publickey_get_id(pk); printf("Key ID: %s\n", dht_infohash_print(&pk_id)); dht_publickey_delete(pk); - dht_identity_delete(&id); - dht_runner* runner = dht_runner_new(); - dht_runner_run(runner, 4040); - dht_infohash h; - dht_infohash_random(&h); - - printf("random hash: %s\n", dht_infohash_print(&h)); + //dht_infohash_random(&h); + dht_infohash_get_from_string(&h, "bad_actors_test"); + printf("hash: %s\n", dht_infohash_print(&h)); // Put data const char* data_str = "yo, this is some data"; dht_value* val = dht_value_new_from_string(data_str); - dht_runner_put(runner, &h, val, dht_done_callback, runner, false); - dht_value_unref(val); - // Get data - dht_runner_get(runner, &h, dht_get_callback, dht_done_callback, runner); - - // Listen for data struct op_context* ctx = malloc(sizeof(struct op_context)); ctx->runner = runner; ctx->d = 42; - dht_op_token* token = dht_runner_listen(runner, &h, dht_value_callback, op_context_free, ctx); - sleep(1); + dht_runner* runner = dht_runner_new(); + dht_runner_config dht_config; + dht_runner_config_default(&dht_config); + dht_config.peer_discovery = true; // Look for other peers on the network + dht_config.peer_publish = true; // Publish our own peer info + dht_runner_run_config(runner, 4040, &dht_config); + + + // Get data + //dht_runner_get(runner, &h, dht_get_callback, dht_done_callback, runner); + // Listen for data + dht_op_token* token = dht_runner_listen(runner, &h, dht_value_callback, op_context_free, ctx); dht_runner_bootstrap(runner, "bootstrap.jami.net", NULL); + dht_runner_put(runner, &h, val, dht_done_callback, runner, true); + dht_runner_get(runner, &h, dht_get_callback, dht_done_callback, runner); + dht_value_unref(val); - sleep(2); + //sleep(1); + //sleep(20); struct sockaddr** addrs = dht_runner_get_public_address(runner); for (struct sockaddr** addrIt = addrs; *addrIt; addrIt++) { -- GitLab