]> git.ipfire.org Git - collecty.git/commitdiff
netlink: Add a helper function to enumerate all wireless interfaces
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 May 2026 08:41:30 +0000 (08:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 May 2026 08:41:30 +0000 (08:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/netlink.c
src/daemon/netlink.h

index 2ae14b719cc5577011151c7d58a42a467685291d..e78474349a2a1d6cabfefb7a5085e3c61ee586a2 100644 (file)
@@ -21,6 +21,7 @@
 #ifdef HAVE_LIBNL3
 
 #include <errno.h>
+#include <linux/nl80211.h>
 #include <netlink/socket.h>
 #include <stdlib.h>
 
@@ -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 */
index 39b895dd97e6ce0e34d6367a2e295b64ed22c214..85715fe81daf8c938ef740e506c9b322e35476d4 100644 (file)
@@ -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 */