From: Michael Tremer Date: Fri, 29 May 2026 08:41:30 +0000 (+0000) Subject: netlink: Add a helper function to enumerate all wireless interfaces X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=705be8a98afaa7766629c8486606c3fdafde7cce;p=telemetry.git netlink: Add a helper function to enumerate all wireless interfaces Signed-off-by: Michael Tremer --- diff --git a/src/daemon/netlink.c b/src/daemon/netlink.c index 2ae14b7..e784743 100644 --- a/src/daemon/netlink.c +++ b/src/daemon/netlink.c @@ -21,6 +21,7 @@ #ifdef HAVE_LIBNL3 #include +#include #include #include @@ -332,6 +333,60 @@ static int td_netlink_get_nl80211_id(td_netlink* self) { return self->nl80211.id; } + +int td_netlink_enumerate_wireless_interfaces(td_netlink* self, + td_netlink_enumerate_wireless_interfaces_callback callback, void* data) { + struct nl_sock* sock = NULL; + struct nl_msg* msg = NULL; + int nl80211_id; + int r; + + // Fetch the nl80211 socket + sock = td_netlink_get_nl80211_socket(self); + if (!sock) + return -errno; + + // Fetch the ID + nl80211_id = td_netlink_get_nl80211_id(self); + if (!nl80211_id) + return -errno; + + // Allocate a new message + msg = nlmsg_alloc(); + if (!msg) { + ERROR(self->ctx, "Failed to allocate a netlink message: %m\n"); + r = -errno; + goto ERROR; + } + + // Request a dump of all wireless interfaces + genlmsg_put(msg, 0, 0, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_INTERFACE, 0); + + // Set the callback and send + nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, + NULL, data); + + // Send the message + r = nl_send_auto(sock, msg); + if (r < 0) { + ERROR(self->ctx, "Failed to send nl80211 request: %s\n", nl_geterror(r)); + goto ERROR; + } + + // Receive the response + r = nl_recvmsgs_default(sock); + if (r < 0) { + ERROR(self->ctx, "Failed to receive nl80211 response: %s\n", nl_geterror(r)); + goto ERROR; + } + +ERROR: + if (msg) + nlmsg_free(msg); + + return r; +} + # endif /* HAVE_LIBNL3_GENL */ #endif /* HAVE_LIBNL3 */ diff --git a/src/daemon/netlink.h b/src/daemon/netlink.h index 39b895d..85715fe 100644 --- a/src/daemon/netlink.h +++ b/src/daemon/netlink.h @@ -46,6 +46,16 @@ int td_netlink_get_default_gateway(td_netlink* self, #endif /* HAVE_LIBNL3_ROUTE */ +#ifdef HAVE_LIBNL3_GENL + +typedef int (*td_netlink_enumerate_wireless_interfaces_callback) + (td_ctx* ctx, const char* name, struct rtnl_link* link, void* data); + +int td_netlink_enumerate_wireless_interfaces(td_netlink* self, + td_netlink_enumerate_wireless_interfaces_callback callback, void* data); + +#endif /* HAVE_LIBNL3_GENL */ + #endif /* HAVE_LIBNL3 */ #endif /* TELEMETRY_NETLINK_H */