[PATCH] ipcpd: Make the DHT a directory policy

  • From: Dimitri Staessens <dimitri@ouroboros.rocks>
  • To: ouroboros@xxxxxxxxxxxxx
  • Date: Sat, 4 Dec 2021 19:08:39 +0100

The DHT is now a proper directory policy instead of a unicast IPCP
component.

Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks>
---
 src/ipcpd/unicast/CMakeLists.txt              |  6 +--
 src/ipcpd/unicast/dir.c                       | 43 +++++++++----------
 src/ipcpd/unicast/{ => dir}/dht.c             | 11 +++++
 src/ipcpd/unicast/{ => dir}/dht.h             |  8 ++--
 src/ipcpd/unicast/{ => dir}/kademlia.proto    |  0
 src/ipcpd/unicast/dir/ops.h                   | 42 ++++++++++++++++++
 .../unicast/{ => dir}/tests/CMakeLists.txt    |  0
 src/ipcpd/unicast/{ => dir}/tests/dht_test.c  |  0
 8 files changed, 81 insertions(+), 29 deletions(-)
 rename src/ipcpd/unicast/{ => dir}/dht.c (99%)
 rename src/ipcpd/unicast/{ => dir}/dht.h (87%)
 rename src/ipcpd/unicast/{ => dir}/kademlia.proto (100%)
 create mode 100644 src/ipcpd/unicast/dir/ops.h
 rename src/ipcpd/unicast/{ => dir}/tests/CMakeLists.txt (100%)
 rename src/ipcpd/unicast/{ => dir}/tests/dht_test.c (100%)

diff --git a/src/ipcpd/unicast/CMakeLists.txt b/src/ipcpd/unicast/CMakeLists.txt
index a14f4e4..f488716 100644
--- a/src/ipcpd/unicast/CMakeLists.txt
+++ b/src/ipcpd/unicast/CMakeLists.txt
@@ -14,7 +14,7 @@ include_directories(${CMAKE_BINARY_DIR}/include)
 
 set(IPCP_UNICAST_TARGET ipcpd-unicast CACHE INTERNAL "")
 
-protobuf_generate_c(KAD_PROTO_SRCS KAD_PROTO_HDRS kademlia.proto)
+protobuf_generate_c(KAD_PROTO_SRCS KAD_PROTO_HDRS dir/kademlia.proto)
 
 math(EXPR PFT_EXPR "1 << 12")
 set(PFT_SIZE ${PFT_EXPR} CACHE STRING
@@ -34,7 +34,6 @@ set(SOURCE_FILES
   addr-auth.c
   ca.c
   connmgr.c
-  dht.c
   dir.c
   dt.c
   enroll.c
@@ -47,6 +46,7 @@ set(SOURCE_FILES
   addr-auth/flat.c
   ca/mb-ecn.c
   ca/nop.c
+  dir/dht.c
   pff/simple.c
   pff/alternate.c
   pff/multipath.c
@@ -70,5 +70,5 @@ add_subdirectory(pff/tests)
 add_subdirectory(routing/tests)
 
 if (NOT GNU)
-  add_subdirectory(tests)
+  add_subdirectory(dir/tests)
 endif ()
diff --git a/src/ipcpd/unicast/dir.c b/src/ipcpd/unicast/dir.c
index d27cabf..984f1b6 100644
--- a/src/ipcpd/unicast/dir.c
+++ b/src/ipcpd/unicast/dir.c
@@ -34,7 +34,8 @@
 #include <ouroboros/utils.h>
 
 #include "dir.h"
-#include "dht.h"
+#include "dir/ops.h"
+#include "dir/dht.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -42,53 +43,49 @@
 #include <inttypes.h>
 #include <limits.h>
 
+struct {
+        struct dir_ops * ops;
+} dir;
+
 int dir_init(void)
 {
-        if (dht_init() < 0)
+        dir.ops = &dht_dir_ops;
+
+        if (dir.ops->init() < 0) {
+                dir.ops = NULL;
                 return -ENOMEM;
+        }
 
         return 0;
 }
 
 void dir_fini(void)
 {
-        dht_fini();
+        dir.ops->fini();
+        dir.ops = NULL;
 }
 
-int dir_bootstrap(void) {
-        log_dbg("Bootstrapping directory.");
-
-        if (dht_bootstrap()) {
-                dht_fini();
-                return -ENOMEM;
-        }
-
-        log_info("Directory bootstrapped.");
-
-        return 0;
+int dir_bootstrap(void)
+{
+        return dir.ops->bootstrap();
 }
 
 int dir_reg(const uint8_t * hash)
 {
-        return dht_reg(hash);
+        return dir.ops->reg(hash);
 }
 
 int dir_unreg(const uint8_t * hash)
 {
-        return dht_unreg(hash);
+        return dir.ops->unreg(hash);
 }
 
 uint64_t dir_query(const uint8_t * hash)
 {
-        return dht_query(hash);
+        return dir.ops->query(hash);
 }
 
 int dir_wait_running(void)
 {
-        if (dht_wait_running()) {
-                log_warn("Directory did not bootstrap.");
-                return -1;
-        }
-
-        return 0;
+        return dir.ops->wait_running();
 }
diff --git a/src/ipcpd/unicast/dht.c b/src/ipcpd/unicast/dir/dht.c
similarity index 99%
rename from src/ipcpd/unicast/dht.c
rename to src/ipcpd/unicast/dir/dht.c
index f7cb89f..ba4b897 100644
--- a/src/ipcpd/unicast/dht.c
+++ b/src/ipcpd/unicast/dir/dht.c
@@ -48,6 +48,7 @@
 #include "dht.h"
 #include "dt.h"
 #include "ipcp.h"
+#include "ops.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -209,6 +210,16 @@ struct cmd {
         struct shm_du_buff * sdb;
 };
 
+struct dir_ops dht_dir_ops = {
+        .init         = dht_init,
+        .fini         = dht_fini,
+        .bootstrap    = dht_bootstrap,
+        .reg          = dht_reg,
+        .unreg        = dht_unreg,
+        .query        = dht_query,
+        .wait_running = dht_wait_running
+};
+
 struct {
         size_t           alpha;
         size_t           b;
diff --git a/src/ipcpd/unicast/dht.h b/src/ipcpd/unicast/dir/dht.h
similarity index 87%
rename from src/ipcpd/unicast/dht.h
rename to src/ipcpd/unicast/dir/dht.h
index 29ab7ee..f6fb8e8 100644
--- a/src/ipcpd/unicast/dht.h
+++ b/src/ipcpd/unicast/dir/dht.h
@@ -20,8 +20,8 @@
  * Foundation, Inc., http://www.fsf.org/about/contact/.
  */
 
-#ifndef OUROBOROS_IPCPD_UNICAST_DHT_H
-#define OUROBOROS_IPCPD_UNICAST_DHT_H
+#ifndef OUROBOROS_IPCPD_UNICAST_DIR_DHT_H
+#define OUROBOROS_IPCPD_UNICAST_DIR_DHT_H
 
 #include <ouroboros/ipcp-dev.h>
 
@@ -42,4 +42,6 @@ uint64_t     dht_query(const uint8_t * key);
 
 int          dht_wait_running(void);
 
-#endif /* OUROBOROS_IPCPD_UNICAST_DHT_H */
+extern struct dir_ops dht_dir_ops;
+
+#endif /* OUROBOROS_IPCPD_UNICAST_DIR_DHT_H */
diff --git a/src/ipcpd/unicast/kademlia.proto 
b/src/ipcpd/unicast/dir/kademlia.proto
similarity index 100%
rename from src/ipcpd/unicast/kademlia.proto
rename to src/ipcpd/unicast/dir/kademlia.proto
diff --git a/src/ipcpd/unicast/dir/ops.h b/src/ipcpd/unicast/dir/ops.h
new file mode 100644
index 0000000..7eabb68
--- /dev/null
+++ b/src/ipcpd/unicast/dir/ops.h
@@ -0,0 +1,42 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2021
+ *
+ * Directory policy ops
+ *
+ *    Dimitri Staessens <dimitri@ouroboros.rocks>
+ *    Sander Vrijders   <sander@ouroboros.rocks>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., http://www.fsf.org/about/contact/.
+ */
+
+#ifndef OUROBOROS_IPCPD_UNICAST_DIR_OPS_H
+#define OUROBOROS_IPCPD_UNICAST_DIR_OPS_H
+
+struct dir_ops {
+        int      (* init)(void);
+
+        void     (* fini)(void);
+
+        int      (* bootstrap)(void);
+
+        int      (* reg)(const uint8_t * hash);
+
+        int      (* unreg)(const uint8_t * hash);
+
+        uint64_t (* query)(const uint8_t * hash);
+
+        int      (* wait_running)(void);
+};
+
+#endif /* OUROBOROS_IPCPD_UNICAST_DIR_OPS_H */
diff --git a/src/ipcpd/unicast/tests/CMakeLists.txt 
b/src/ipcpd/unicast/dir/tests/CMakeLists.txt
similarity index 100%
rename from src/ipcpd/unicast/tests/CMakeLists.txt
rename to src/ipcpd/unicast/dir/tests/CMakeLists.txt
diff --git a/src/ipcpd/unicast/tests/dht_test.c 
b/src/ipcpd/unicast/dir/tests/dht_test.c
similarity index 100%
rename from src/ipcpd/unicast/tests/dht_test.c
rename to src/ipcpd/unicast/dir/tests/dht_test.c
-- 
2.34.1


Other related posts:

  • » [PATCH] ipcpd: Make the DHT a directory policy - Dimitri Staessens