Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/batadv.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ int nw_get_batadv_clients() {
parse_clients_list_netlink_cb, NLM_F_DUMP,
&opts.query_opts);
if (ret < 0) {
log_error("Failed to query batman-adv clients: %d", ret);
return -1;
}

Expand Down
20 changes: 17 additions & 3 deletions src/information.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Expand All @@ -10,6 +11,7 @@
#include <linux/kernel.h>

#include "batadv.h"
#include "log.h"
#include "util.h"
#include "information.h"

Expand Down Expand Up @@ -41,6 +43,7 @@ int node_whisperer_information_hostname_collect(uint8_t *buffer, size_t buffer_s

ret = gethostname((char *)buffer, buffer_size);
if (ret) {
log_warning("gethostname failed: %s", strerror(errno));
return ret;
}

Expand All @@ -63,13 +66,15 @@ int node_whisperer_information_node_id_collect(uint8_t *buffer, size_t buffer_si
}

if (node_id_ascii_len < 18) {
log_warning("primary_mac too short: %zu bytes", node_id_ascii_len);
free(node_id_ascii);
return -1;
}

ret = nw_parse_mac_address_ascii(node_id_ascii, buffer);
free(node_id_ascii);
if (ret < 0) {
log_warning("Failed to parse primary_mac");
return -1;
}

Expand All @@ -87,15 +92,18 @@ int node_whisperer_information_batman_adv_collect(uint8_t *buffer, size_t buffer
}

ret = nw_get_batadv_neighbor_stats(&stats);
if (ret)
if (ret) {
log_warning("Failed to get batman-adv neighbor stats");
return -1;

}

ret = nw_get_batadv_clients();
if (ret < 0) {
num_clients = 0;
} else {
num_clients = (uint16_t)ret;
}
log_debug("nw_get_batadv_clients() found %d clients", num_clients);
}

buffer[0] = stats.vpn.connected ? 1 : 0;
buffer[1] = stats.vpn.tq;
Expand Down Expand Up @@ -138,18 +146,21 @@ int node_whisperer_information_site_code_collect(uint8_t *buffer, size_t buffer_

site = gluonutil_load_site_config();
if (!site) {
log_warning("Failed to load site config");
ret = -1;
goto out_free;
}

site_code_j = json_object_object_get(site, "site_code");
if (!site_code_j) {
log_warning("site_code not found in site config");
ret = -1;
goto out_free;
}

site_code = json_object_get_string(site_code_j);
if (!site_code) {
log_warning("site_code is not a string");
ret = -1;
goto out_free;
}
Expand All @@ -176,6 +187,7 @@ int node_whisperer_information_domain_collect(uint8_t *buffer, size_t buffer_siz

dom = gluonutil_get_domain();
if (!dom) {
log_warning("Failed to get domain");
ret = -1;
goto out_free;
}
Expand Down Expand Up @@ -203,6 +215,7 @@ int node_whisperer_information_system_load_collect(uint8_t *buffer, size_t buffe
}

if (getloadavg(samples, 3) < 3) {
log_warning("getloadavg failed");
return -1;
}

Expand All @@ -221,6 +234,7 @@ int node_whisperer_information_firmware_version_collect(uint8_t *buffer, size_t
}

if (firmware_version_len > buffer_size) {
log_warning("Firmware version too long: %zu bytes", firmware_version_len);
free(firmware_version);
return -1;
}
Expand Down
10 changes: 8 additions & 2 deletions src/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ int nw_interface_remove(struct ubus_context *ctx,
int nw_interface_add(struct ubus_context *ctx, int id, const char *name)
{
struct nw_interface *iface;
int ret;

iface = calloc(sizeof(*iface), 1);
if (!iface) {
Expand All @@ -121,8 +122,13 @@ int nw_interface_add(struct ubus_context *ctx, int id, const char *name)
/* Subscribe to node removal */
iface->ubus.subscriber.cb = nw_interface_handle_event;
iface->ubus.subscriber.remove_cb = nw_interface_handle_remove;
ubus_register_subscriber(ctx, &iface->ubus.subscriber);
ubus_subscribe(ctx, &iface->ubus.subscriber, iface->ubus.id);
ret = ubus_register_subscriber(ctx, &iface->ubus.subscriber);
if (ret)
log_error("Failed to register subscriber for %s: %s", name, ubus_strerror(ret));

ret = ubus_subscribe(ctx, &iface->ubus.subscriber, iface->ubus.id);
if (ret)
log_error("Failed to subscribe to %s: %s", name, ubus_strerror(ret));

log_info("Registered interface %s", iface->ubus.name);

Expand Down
7 changes: 7 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ void log_error(const char *fmt, ...) {
va_end(args);
}

void log_warning(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
log_vprintf(LL_WARNING, fmt, args);
va_end(args);
}

void log_info(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
Expand Down
1 change: 1 addition & 0 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ void log_set_level(enum log_level level);
void log_use_syslog(int use);

void log_error(const char *fmt, ...);
void log_warning(const char *fmt, ...);
void log_info(const char *fmt, ...);
void log_debug(const char *fmt, ...);
17 changes: 14 additions & 3 deletions src/ubus.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,11 @@ static void nw_register_events(struct ubus_context *ctx)
static struct ubus_event_handler handler = {
.cb = nw_ubus_event_handler
};
int ret;

ubus_register_event_handler(ctx, &handler, "ubus.object.add");
ret = ubus_register_event_handler(ctx, &handler, "ubus.object.add");
if (ret)
log_error("Failed to register ubus event handler: %s", ubus_strerror(ret));
}

static void nw_ubus_list_cb(struct ubus_context *ctx,
Expand All @@ -225,7 +228,15 @@ static void nw_ubus_list_cb(struct ubus_context *ctx,

void nw_ubus_init(struct ubus_context *ctx)
{
ubus_add_object(ctx, &nw_obj);
int ret;

ret = ubus_add_object(ctx, &nw_obj);
if (ret)
log_error("Failed to register ubus object: %s", ubus_strerror(ret));

nw_register_events(ctx);
ubus_lookup(ctx, "hostapd.*", nw_ubus_list_cb, NULL);

ret = ubus_lookup(ctx, "hostapd.*", nw_ubus_list_cb, NULL);
if (ret)
log_warning("Failed to enumerate existing hostapd interfaces: %s", ubus_strerror(ret));
}
12 changes: 10 additions & 2 deletions src/util.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include <string.h> // Add missing import
#include "log.h"

int nw_read_file(char *path, char **buf, size_t *len) {
FILE *f = fopen(path, "rb");
if (!f) {
log_warning("Failed to open file %s: %s", path, strerror(errno));
return -1;
}
fseek(f, 0, SEEK_END);
*len = ftell(f);
fseek(f, 0, SEEK_SET);
*buf = malloc(*len);
if (!*buf) {
log_error("Failed to allocate %zu bytes for file %s", *len, path);
fclose(f);
return -1;
}
fread(*buf, 1, *len, f);
size_t read_len = fread(*buf, 1, *len, f);
if (read_len != *len) {
log_warning("Short read for file %s: expected %zu bytes, got %zu", path, *len, read_len);
*len = read_len;
}
fclose(f);
return 0;
}
Expand Down