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)
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)
}
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);
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);
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,
{
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)
#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"
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__)