summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Fietkau2025-02-28 13:30:53 +0000
committerFelix Fietkau2025-02-28 13:30:56 +0000
commit3fab99eab4d5fe29280babcfa5d6b86e43b88cad (patch)
treec98fb457e8c268656adabdc791afdb54de96854c
parentf5341f3275394504a1d5a86ea3db817029f9e2f2 (diff)
downloadunetd-3fab99eab4d5fe29280babcfa5d6b86e43b88cad.tar.gz
add udebug support
Signed-off-by: Felix Fietkau <nbd@nbd.name>
-rw-r--r--CMakeLists.txt4
-rw-r--r--main.c77
-rw-r--r--network.c2
-rw-r--r--ubus.c2
-rw-r--r--unetd.h19
5 files changed, 95 insertions, 9 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8f7ef9f..aba9787 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -40,15 +40,17 @@ IF(UBUS_SUPPORT)
SET(DHT_SOURCES ${DHT_SOURCES} udht-ubus.c)
ADD_DEFINITIONS(-DUBUS_SUPPORT=1)
FIND_LIBRARY(ubus ubus)
+ FIND_LIBRARY(udebug NAMES udebug)
ELSE()
SET(ubus "")
+ SET(udebug "")
ENDIF()
ADD_LIBRARY(unet SHARED curve25519.c siphash.c sha512.c fprime.c f25519.c ed25519.c edsign.c auth-data.c chacha20.c pex-msg.c utils.c stun.c)
TARGET_LINK_LIBRARIES(unet ubox)
ADD_EXECUTABLE(unetd ${SOURCES})
-TARGET_LINK_LIBRARIES(unetd unet ubox ${ubus} blobmsg_json ${libjson} ${nl} ${bpf} ${elf} ${zlib})
+TARGET_LINK_LIBRARIES(unetd unet ubox ${ubus} blobmsg_json ${libjson} ${nl} ${bpf} ${elf} ${zlib} ${udebug})
ADD_EXECUTABLE(unet-tool cli.c)
TARGET_LINK_LIBRARIES(unet-tool unet blobmsg_json ${libjson} ubox)
diff --git a/main.c b/main.c
index be24db7..7014af8 100644
--- a/main.c
+++ b/main.c
@@ -19,7 +19,75 @@ static const char *hosts_file;
const char *mssfix_path = UNETD_MSS_BPF_PATH;
const char *data_dir = UNETD_DATA_DIR;
int global_pex_port = UNETD_GLOBAL_PEX_PORT;
-bool debug;
+
+static bool debug;
+
+#ifdef UBUS_SUPPORT
+static struct udebug ud;
+static struct udebug_buf udb_log;
+
+static const struct udebug_buf_meta meta_log = {
+ .name = "log",
+ .format = UDEBUG_FORMAT_STRING
+};
+
+static struct udebug_ubus_ring rings[] = {
+ {
+ .buf = &udb_log,
+ .meta = &meta_log,
+ .default_entries = 1024,
+ .default_size = 64 * 1024,
+ }
+};
+#endif
+
+bool unetd_debug_active(void)
+{
+#ifdef UBUS_SUPPORT
+ if (udebug_buf_valid(&udb_log))
+ return true;
+#endif
+ return debug;
+}
+
+static void __attribute__((format (printf, 1, 0)))
+unetd_udebug_vprintf(const char *format, va_list ap)
+{
+#ifdef UBUS_SUPPORT
+ if (!udebug_buf_valid(&udb_log))
+ return;
+
+ udebug_entry_init(&udb_log);
+ udebug_entry_vprintf(&udb_log, format, ap);
+ udebug_entry_add(&udb_log);
+#endif
+}
+
+void unetd_debug_printf(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+
+ if (debug) {
+ va_list ap2;
+
+ va_copy(ap2, ap);
+ vfprintf(stderr, format, ap2);
+ va_end(ap2);
+ }
+
+ unetd_udebug_vprintf(format, ap);
+ va_end(ap);
+}
+
+#ifdef UBUS_SUPPORT
+void unetd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+ bool enabled)
+{
+ udebug_ubus_apply_config(&ud, rings, ARRAY_SIZE(rings), data, enabled);
+}
+#endif
static void
network_write_hosts(struct network *net, FILE *f)
@@ -134,6 +202,13 @@ int main(int argc, char **argv)
}
uloop_init();
+#ifdef UBUS_SUPPORT
+ udebug_init(&ud);
+ udebug_auto_connect(&ud, NULL);
+ for (size_t i = 0; i < ARRAY_SIZE(rings); i++)
+ udebug_ubus_ring_init(&ud, &rings[i]);
+#endif
+
unetd_ubus_init();
unetd_write_hosts();
global_pex_open(unix_socket);
diff --git a/network.c b/network.c
index 09fd01b..c5de02c 100644
--- a/network.c
+++ b/network.c
@@ -459,7 +459,7 @@ network_do_update(struct network *net, bool up)
network_fill_subnets(net, &b);
}
- if (debug) {
+ if (unetd_debug_active()) {
char *s = blobmsg_format_json(b.head, true);
D_NET(net, "update: %s", s);
free(s);
diff --git a/ubus.c b/ubus.c
index 6e32b53..ba6b3de 100644
--- a/ubus.c
+++ b/ubus.c
@@ -11,6 +11,7 @@
static struct ubus_auto_conn conn;
static struct ubus_subscriber sub;
static struct blob_buf b;
+static struct udebug_ubus udebug;
static int
ubus_network_add(struct ubus_context *ctx, struct ubus_object *obj,
@@ -561,6 +562,7 @@ ubus_connect_handler(struct ubus_context *ctx)
{
int ret;
+ udebug_ubus_init(&udebug, ctx, "unetd", unetd_udebug_config);
ubus_register_subscriber(ctx, &sub);
ret = ubus_add_object(ctx, &unetd_object);
if (ret)
diff --git a/unetd.h b/unetd.h
index 56e8d2d..a218d0b 100644
--- a/unetd.h
+++ b/unetd.h
@@ -10,6 +10,9 @@
#include <libubox/vlist.h>
#include <libubox/blobmsg.h>
#include <libubox/utils.h>
+#ifdef UBUS_SUPPORT
+#include <udebug.h>
+#endif
#include "utils.h"
#include "siphash.h"
#include "wg.h"
@@ -25,14 +28,18 @@
extern const char *mssfix_path;
extern const char *data_dir;
-extern bool debug;
extern int global_pex_port;
+bool unetd_debug_active(void);
+void unetd_debug_printf(const char *format, ...);
+#ifdef UBUS_SUPPORT
+void unetd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+ bool enabled);
+#endif
-#define D(format, ...) \
- do { \
- if (debug) \
- fprintf(stderr, "%s(%d) " format "\n", \
- __func__, __LINE__, ##__VA_ARGS__); \
+#define D(format, ...) \
+ do { \
+ unetd_debug_printf("%s(%d) " format "\n", \
+ __func__, __LINE__, ##__VA_ARGS__); \
} while (0)
#define D_NET(net, format, ...) D("network %s " format, network_name(net), ##__VA_ARGS__)