commit 2b4cf0409bff96f384093a0a9c2c4ada06fbe52a
parent a573f216c5e47fed94c48a7ccf879e67400bd9ba
Author: xplshn <anto@xplshn.com.ar>
Date: Sun, 22 Nov 2026 17:47:22 -0700
iwctl: add status, networks, connect and disconnect commands
Diffstat:
5 files changed, 289 insertions(+), 29 deletions(-)
diff --git a/ctl/main.c b/ctl/main.c
@@ -3,7 +3,6 @@
#include <ell/ell.h>
#include <errno.h>
-#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -12,22 +11,55 @@
#include <sys/un.h>
#include <unistd.h>
+static void
+usage(void)
+{
+ l_error("usage: iwctl [device list | status | networks | "
+ "connect SSID [PASSPHRASE] | disconnect]");
+}
+
+/* the request the words make: tabs between them, but "device list", which
+ * is one word of the protocol written as two */
+static bool
+make_request(int argc, char **argv, char *request, size_t size)
+{
+ int n;
+
+ if (argc == 1) {
+ snprintf(request, size, "%s", CONTROL_PING);
+ return true;
+ }
+ if (argc == 3 && !strcmp(argv[1], "device") && !strcmp(argv[2], "list")) {
+ snprintf(request, size, "%s", CONTROL_DEVICE_LIST);
+ return true;
+ }
+ if ((argc == 2 && (!strcmp(argv[1], CONTROL_STATUS) ||
+ !strcmp(argv[1], CONTROL_NETWORKS) ||
+ !strcmp(argv[1], CONTROL_DISCONNECT))) ||
+ ((argc == 3 || argc == 4) && !strcmp(argv[1], CONTROL_CONNECT))) {
+ n = snprintf(request, size, "%s", argv[1]);
+ for (int i = 2; i < argc && n > 0 && (size_t)n < size; i++)
+ n += snprintf(request + n, size - n, "\t%s", argv[i]);
+ return n > 0 && (size_t)n < size;
+ }
+ return false;
+}
+
int
main(int argc, char **argv)
{
struct sockaddr_un address = { .sun_family = AF_UNIX };
- struct timeval timeout = { .tv_sec = 3 };
+ struct timeval timeout = { .tv_sec = 5 };
const char *path = getenv("IWD_CONTROL_SOCKET");
- 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];
+ char request[CONTROL_REQUEST_MAX], reply[CONTROL_REPLY_MAX];
+ bool ping = argc == 1, failed = false;
ssize_t len;
- int fd;
+ int fd = -1;
l_log_set_stderr();
l_log_set_ident("iwctl");
- if (argc != 1 && !list) {
- l_error("usage: iwctl [device list]");
+ if (!make_request(argc, argv, request, sizeof(request))) {
+ usage();
return EXIT_FAILURE;
}
if (!path)
@@ -47,26 +79,28 @@ main(int argc, char **argv)
if (L_TFR(send(fd, request, strlen(request), MSG_NOSIGNAL)) < 0)
goto error;
for (;;) {
- len = L_TFR(recv(fd, reply, sizeof(reply), MSG_TRUNC));
+ len = L_TFR(recv(fd, reply, sizeof(reply) - 1, MSG_TRUNC));
if (len < 0)
goto error;
- if (!list && len == sizeof(CONTROL_PONG) - 1 &&
- !memcmp(reply, CONTROL_PONG, len)) {
- puts("ping! pong!");
+ if (len == 0)
break;
- }
- if (!list || len < 1 || (size_t)len > sizeof(reply) ||
- strnlen(reply, len) != (size_t)len - 1) {
+ if ((size_t)len >= sizeof(reply) || reply[len - 1]) {
errno = EPROTO;
goto error;
}
+ if (ping && !strcmp(reply, CONTROL_PONG)) {
+ puts("ping! pong!");
+ break;
+ }
if (!reply[0])
break;
+ if (!strncmp(reply, "error ", 6))
+ failed = true;
if (puts(reply) == EOF)
break;
}
close(fd);
- return fflush(stdout) || ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS;
+ return fflush(stdout) || ferror(stdout) || failed ? EXIT_FAILURE : EXIT_SUCCESS;
error:
l_error("%s: %s", path, strerror(errno));
diff --git a/shared/control.h b/shared/control.h
@@ -1,9 +1,33 @@
#ifndef EIWD_CONTROL_PROTOCOL_H
#define EIWD_CONTROL_PROTOCOL_H
-/* very extensive and feature complete shared protocol */
+/*
+ * One request a connection, answered by packets of text each ended by a NUL,
+ * and a packet with nothing in it to end the answer. A request's words are
+ * separated by a tab, so a network's name may hold spaces.
+ *
+ * ping! pong!
+ * device list a wireless interface a packet
+ * status "state ssid signal" of the first one,
+ * state being connected, connecting,
+ * disconnecting, roaming or disconnected,
+ * signal in dBm, ssid empty when none
+ * networks "ssid security signal flag" a packet,
+ * strongest first; security is open, wep,
+ * psk or 8021x, flag connected, known or
+ * empty
+ * connect <ssid> [passphrase] ok, or "error why". a passphrase is
+ * kept as the network's own once it works
+ * disconnect ok, or "error why"
+ */
#define CONTROL_PING "ping!"
#define CONTROL_PONG "pong!"
#define CONTROL_DEVICE_LIST "device list"
+#define CONTROL_STATUS "status"
+#define CONTROL_NETWORKS "networks"
+#define CONTROL_CONNECT "connect"
+#define CONTROL_DISCONNECT "disconnect"
+#define CONTROL_REQUEST_MAX 512
+#define CONTROL_REPLY_MAX 256
#endif
diff --git a/src/control.c b/src/control.c
@@ -4,13 +4,21 @@
#include <ell/ell.h>
#include <errno.h>
+#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
+#ifndef CONTROL_SOCKET_GID
+#define CONTROL_SOCKET_GID 10
+#endif
+
+#include "src/common.h"
#include "src/netdev.h"
+#include "src/network.h"
+#include "src/station.h"
struct ControlClient {
struct l_io *io;
@@ -47,33 +55,205 @@ client_timeout(struct l_timeout *timeout, void *data)
}
static bool
+send_text(int fd, const char *text)
+{
+ size_t size = strlen(text) + 1;
+
+ return L_TFR(send(fd, text, size, MSG_NOSIGNAL)) == (ssize_t)size;
+}
+
+static bool
send_name(const char *name, void *data)
{
- int fd = *(int *)data;
- size_t size = strlen(name) + 1;
+ return send_text(*(int *)data, name);
+}
+
+/* every request is answered for the first wireless interface there is */
+static void
+first_station_cb(struct station *station, void *data)
+{
+ struct station **first = data;
+
+ if (!*first)
+ *first = station;
+}
+
+static struct station *
+first_station(void)
+{
+ struct station *station = NULL;
- return L_TFR(send(fd, name, size, MSG_NOSIGNAL)) == (ssize_t)size;
+ station_foreach(first_station_cb, &station);
+ return station;
+}
+
+static const char *
+state_name(enum station_state state)
+{
+ switch (state) {
+ case STATION_STATE_CONNECTED:
+ case STATION_STATE_NETCONFIG:
+ return "connected";
+ case STATION_STATE_CONNECTING:
+ case STATION_STATE_CONNECTING_AUTO:
+ return "connecting";
+ case STATION_STATE_DISCONNECTING:
+ return "disconnecting";
+ case STATION_STATE_ROAMING:
+ case STATION_STATE_FT_ROAMING:
+ case STATION_STATE_FW_ROAMING:
+ return "roaming";
+ default:
+ return "disconnected";
+ }
+}
+
+static const char *
+security_name(enum security security)
+{
+ switch (security) {
+ case SECURITY_NONE:
+ return "open";
+ case SECURITY_WEP:
+ return "wep";
+ case SECURITY_PSK:
+ return "psk";
+ default:
+ return "8021x";
+ }
+}
+
+static void
+send_status(int fd, struct station *station)
+{
+ char line[CONTROL_REPLY_MAX];
+ struct network *network = station_get_connected_network(station);
+
+ snprintf(line, sizeof(line), "%s\t%s\t%d",
+ state_name(station_get_state(station)),
+ network ? network_get_ssid(network) : "",
+ network ? network_get_signal_strength(network) / 100 : 0);
+ send_text(fd, line);
+}
+
+struct list_state {
+ int fd;
+ struct network *connected;
+};
+
+static void
+send_network(struct network *network, void *data)
+{
+ struct list_state *list = data;
+ char line[CONTROL_REPLY_MAX];
+
+ snprintf(line, sizeof(line), "%s\t%s\t%d\t%s",
+ network_get_ssid(network),
+ security_name(network_get_security(network)),
+ network_get_signal_strength(network) / 100,
+ network == list->connected ? "connected" :
+ network_get_settings(network) ? "known" : "");
+ send_text(list->fd, line);
+}
+
+struct find_state {
+ const char *ssid;
+ struct network *found;
+};
+
+static void
+find_network(struct network *network, void *data)
+{
+ struct find_state *find = data;
+
+ if (!find->found && !strcmp(network_get_ssid(network), find->ssid))
+ find->found = network;
+}
+
+/* connect <ssid> [passphrase] */
+static void
+do_connect(int fd, struct station *station, char *args)
+{
+ struct find_state find = { .ssid = args };
+ char *passphrase = strchr(args, '\t');
+ struct scan_bss *bss;
+
+ if (passphrase)
+ *passphrase++ = '\0';
+ station_foreach_network(station, find_network, &find);
+ if (!find.found) {
+ send_text(fd, "error no such network");
+ return;
+ }
+ if (network_get_security(find.found) == SECURITY_8021X ||
+ network_get_security(find.found) == SECURITY_WEP) {
+ send_text(fd, "error that kind of security is not supported");
+ return;
+ }
+ if (passphrase && *passphrase &&
+ network_get_security(find.found) == SECURITY_PSK &&
+ !network_set_passphrase(find.found, passphrase)) {
+ send_text(fd, "error not a usable passphrase");
+ return;
+ }
+ if (network_get_security(find.found) == SECURITY_PSK &&
+ !network_get_settings(find.found) &&
+ (!passphrase || !*passphrase)) {
+ send_text(fd, "error that network needs a passphrase");
+ return;
+ }
+ bss = network_bss_select(find.found, true);
+ if (!bss) {
+ send_text(fd, "error the network is not in range");
+ return;
+ }
+ station_connect_network(station, find.found, bss, NULL);
+ send_text(fd, "ok");
}
static bool
client_read(struct l_io *io, void *data)
{
- char request[sizeof(CONTROL_DEVICE_LIST)];
+ char request[CONTROL_REQUEST_MAX + 1];
ssize_t len;
int fd = l_io_get_fd(io);
+ struct station *station;
- len = recv(fd, request, sizeof(request), MSG_TRUNC);
+ len = recv(fd, request, CONTROL_REQUEST_MAX, MSG_TRUNC);
if (len < 0 && (errno == EINTR || errno == EAGAIN))
return true;
- if (len == sizeof(CONTROL_PING) - 1 &&
- !memcmp(request, CONTROL_PING, len)) {
- 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);
+ if (len < 0 || len > CONTROL_REQUEST_MAX)
+ goto done;
+ request[len] = '\0';
+ station = first_station();
+ if (!strcmp(request, CONTROL_PING)) {
+ send_text(fd, CONTROL_PONG);
+ goto done;
+ }
+ if (!strcmp(request, CONTROL_DEVICE_LIST)) {
+ if (!netdev_foreach_name(send_name, &fd))
+ goto done;
+ } else if (!station) {
+ send_text(fd, "error no wireless interface");
+ } else if (!strcmp(request, CONTROL_STATUS)) {
+ send_status(fd, station);
+ } else if (!strcmp(request, CONTROL_NETWORKS)) {
+ struct list_state list = { fd, station_get_connected_network(station) };
+
+ station_foreach_network(station, send_network, &list);
+ } else if (!strncmp(request, CONTROL_CONNECT "\t",
+ sizeof(CONTROL_CONNECT))) {
+ do_connect(fd, station, request + sizeof(CONTROL_CONNECT));
+ } else if (!strcmp(request, CONTROL_DISCONNECT)) {
+ int err = station_disconnect(station);
+
+ send_text(fd, err < 0 && err != -ENOTCONN ? "error not connected" : "ok");
+ } else {
+ send_text(fd, "error unknown request");
}
+ send_text(fd, "");
+done:
/* Let the disconnect handler release this client's watch. */
shutdown(fd, SHUT_RDWR);
return false;
@@ -140,6 +320,13 @@ control_init(const char *path)
umask(mask);
if (err < 0)
goto close_fd;
+ /*
+ * Whoever may use the network is whoever is in the group that runs the
+ * machine, wheel (10) by default: the socket is root's alone only
+ * until here.
+ */
+ if (chown(path, (uid_t)-1, CONTROL_SOCKET_GID) == 0)
+ chmod(path, 0660);
if (listen(fd, 16) < 0) {
err = -errno;
goto unlink_socket;
diff --git a/src/station.c b/src/station.c
@@ -5037,6 +5037,17 @@ void station_network_foreach(struct station *station,
l_hashmap_foreach(station->networks, network_foreach, &data);
}
+void station_foreach_network(struct station *station,
+ void (*func)(struct network *, void *),
+ void *user_data)
+{
+ const struct l_queue_entry *entry;
+
+ for (entry = l_queue_get_entries(station->networks_sorted); entry;
+ entry = entry->next)
+ func(entry->data, user_data);
+}
+
struct l_queue *station_get_bss_list(struct station *station)
{
return station->bss_list;
diff --git a/src/station.h b/src/station.h
@@ -104,6 +104,10 @@ void station_network_foreach(struct station *station,
station_network_foreach_func_t func,
void *user_data);
struct l_queue *station_get_bss_list(struct station *station);
+/* the networks seen by the station, strongest first */
+void station_foreach_network(struct station *station,
+ void (*func)(struct network *, void *),
+ void *user_data);
struct scan_bss *station_get_connected_bss(struct station *station);
int station_hide_network(struct station *station, struct network *network);