commit a573f216c5e47fed94c48a7ccf879e67400bd9ba
parent bcfc411bb8f60ca18b84d9b08e6e02fe31adf7cc
Author: cowmonk <cowmonk@based.pt>
Date: Fri, 20 Nov 2026 13:08:38 -0700
iwctl: implemenent device list
Diffstat:
6 files changed, 71 insertions(+), 22 deletions(-)
diff --git a/ctl/README b/ctl/README
@@ -17,6 +17,13 @@ Then run the client in another (also as root):
./ctl/iwctl
+To print one wireless interface name per line:
+
+ ./ctl/iwctl device list
+
+An empty list prints nothing. Each name comes in its own packet with a
+trailing NUL. An empty name ends the list.
+
The default socket is /run/eiwd.sock, set by CONTROLSOCKET in config.mk.
IWD_CONTROL_SOCKET overrides the path at runtime; set the same value for
both processes. The socket is created with mode 0600, so the client needs
@@ -42,7 +49,6 @@ be good.
TODOs (in no particular order)
------------------------------
- - ability to get devices (probably first thing)
- list networks && scanning
- connect to WPA/psk network
- misc options and commands
diff --git a/ctl/main.c b/ctl/main.c
@@ -3,6 +3,7 @@
#include <ell/ell.h>
#include <errno.h>
+#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -12,17 +13,23 @@
#include <unistd.h>
int
-main(void)
+main(int argc, char **argv)
{
struct sockaddr_un address = { .sun_family = AF_UNIX };
struct timeval timeout = { .tv_sec = 3 };
const char *path = getenv("IWD_CONTROL_SOCKET");
- char reply[sizeof(CONTROL_PONG)];
+ bool list = argc == 3 && !strcmp(argv[1], "device") && !strcmp(argv[2], "list");
+ const char *request = list ? CONTROL_DEVICE_LIST : CONTROL_PING;
+ char reply[IF_NAMESIZE];
ssize_t len;
int fd;
l_log_set_stderr();
l_log_set_ident("iwctl");
+ if (argc != 1 && !list) {
+ l_error("usage: iwctl [device list]");
+ return EXIT_FAILURE;
+ }
if (!path)
path = CONTROL_SOCKET_PATH;
if (!*path || strlen(path) >= sizeof(address.sun_path)) {
@@ -37,23 +44,29 @@ main(void)
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0 ||
connect(fd, (struct sockaddr *)&address, sizeof(address)) < 0)
goto error;
- do {
- len = send(fd, CONTROL_PING, sizeof(CONTROL_PING) - 1, MSG_NOSIGNAL);
- } while (len < 0 && errno == EINTR);
- if (len < 0)
- goto error;
- do {
- len = recv(fd, reply, sizeof(reply), MSG_TRUNC);
- } while (len < 0 && errno == EINTR);
- if (len < 0)
- goto error;
- if (len != sizeof(CONTROL_PONG) - 1 ||
- memcmp(reply, CONTROL_PONG, sizeof(CONTROL_PONG) - 1)) {
- errno = EPROTO;
+ if (L_TFR(send(fd, request, strlen(request), MSG_NOSIGNAL)) < 0)
goto error;
+ for (;;) {
+ len = L_TFR(recv(fd, reply, sizeof(reply), MSG_TRUNC));
+ if (len < 0)
+ goto error;
+ if (!list && len == sizeof(CONTROL_PONG) - 1 &&
+ !memcmp(reply, CONTROL_PONG, len)) {
+ puts("ping! pong!");
+ break;
+ }
+ if (!list || len < 1 || (size_t)len > sizeof(reply) ||
+ strnlen(reply, len) != (size_t)len - 1) {
+ errno = EPROTO;
+ goto error;
+ }
+ if (!reply[0])
+ break;
+ if (puts(reply) == EOF)
+ break;
}
close(fd);
- return puts("ping! pong!") == EOF ? EXIT_FAILURE : EXIT_SUCCESS;
+ return fflush(stdout) || ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS;
error:
l_error("%s: %s", path, strerror(errno));
diff --git a/shared/control.h b/shared/control.h
@@ -4,5 +4,6 @@
/* very extensive and feature complete shared protocol */
#define CONTROL_PING "ping!"
#define CONTROL_PONG "pong!"
+#define CONTROL_DEVICE_LIST "device list"
#endif
diff --git a/src/control.c b/src/control.c
@@ -10,6 +10,8 @@
#include <sys/un.h>
#include <unistd.h>
+#include "src/netdev.h"
+
struct ControlClient {
struct l_io *io;
struct l_timeout *timeout;
@@ -45,9 +47,18 @@ client_timeout(struct l_timeout *timeout, void *data)
}
static bool
+send_name(const char *name, void *data)
+{
+ int fd = *(int *)data;
+ size_t size = strlen(name) + 1;
+
+ return L_TFR(send(fd, name, size, MSG_NOSIGNAL)) == (ssize_t)size;
+}
+
+static bool
client_read(struct l_io *io, void *data)
{
- char request[sizeof(CONTROL_PING)];
+ char request[sizeof(CONTROL_DEVICE_LIST)];
ssize_t len;
int fd = l_io_get_fd(io);
@@ -56,10 +67,11 @@ client_read(struct l_io *io, void *data)
return true;
if (len == sizeof(CONTROL_PING) - 1 &&
!memcmp(request, CONTROL_PING, len)) {
- do {
- len = send(fd, CONTROL_PONG, sizeof(CONTROL_PONG) - 1,
- MSG_NOSIGNAL);
- } while (len < 0 && errno == EINTR);
+ L_TFR(send(fd, CONTROL_PONG, sizeof(CONTROL_PONG) - 1, MSG_NOSIGNAL));
+ } else if (len == sizeof(CONTROL_DEVICE_LIST) - 1 &&
+ !memcmp(request, CONTROL_DEVICE_LIST, len)) {
+ if (netdev_foreach_name(send_name, &fd))
+ send_name("", &fd);
}
/* Let the disconnect handler release this client's watch. */
diff --git a/src/netdev.c b/src/netdev.c
@@ -425,6 +425,22 @@ const char *netdev_get_name(struct netdev *netdev)
return netdev->name;
}
+bool netdev_foreach_name(bool (*visit)(const char *, void *), void *data)
+{
+ const struct l_queue_entry *e;
+
+ for (e = l_queue_get_entries(netdev_list); e; e = e->next) {
+ struct netdev *netdev = e->data;
+
+ if (!netdev->events_ready || netdev->type == NETDEV_IFTYPE_P2P_CLIENT ||
+ netdev->type == NETDEV_IFTYPE_P2P_GO)
+ continue;
+ if (!visit(netdev->name, data))
+ return false;
+ }
+ return true;
+}
+
bool netdev_get_is_up(struct netdev *netdev)
{
bool powered = (netdev->ifi_flags & IFF_UP) != 0;
diff --git a/src/netdev.h b/src/netdev.h
@@ -155,6 +155,7 @@ int netdev_set_4addr(struct netdev *netdev, bool use_4addr,
netdev_destroy_func_t destroy);
bool netdev_get_4addr(struct netdev *netdev);
const char *netdev_get_name(struct netdev *netdev);
+bool netdev_foreach_name(bool (*visit)(const char *, void *), void *data);
bool netdev_get_is_up(struct netdev *netdev);
const char *netdev_get_path(struct netdev *netdev);
uint8_t netdev_get_rssi_level_idx(struct netdev *netdev);