eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

main.c (2945B)


      1 #include "config.h"
      2 #include "shared/control.h"
      3 
      4 #include <ell/ell.h>
      5 #include <errno.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <sys/socket.h>
     10 #include <sys/time.h>
     11 #include <sys/un.h>
     12 #include <unistd.h>
     13 
     14 static void
     15 usage(void)
     16 {
     17 	l_error("usage: iwctl [device list | status | networks | "
     18 		"connect SSID [PASSPHRASE] | disconnect]");
     19 }
     20 
     21 /* the request the words make: tabs between them, but "device list", which
     22  * is one word of the protocol written as two */
     23 static bool
     24 make_request(int argc, char **argv, char *request, size_t size)
     25 {
     26 	int n;
     27 
     28 	if (argc == 1) {
     29 		snprintf(request, size, "%s", CONTROL_PING);
     30 		return true;
     31 	}
     32 	if (argc == 3 && !strcmp(argv[1], "device") && !strcmp(argv[2], "list")) {
     33 		snprintf(request, size, "%s", CONTROL_DEVICE_LIST);
     34 		return true;
     35 	}
     36 	if ((argc == 2 && (!strcmp(argv[1], CONTROL_STATUS) ||
     37 				!strcmp(argv[1], CONTROL_NETWORKS) ||
     38 				!strcmp(argv[1], CONTROL_DISCONNECT))) ||
     39 			((argc == 3 || argc == 4) && !strcmp(argv[1], CONTROL_CONNECT))) {
     40 		n = snprintf(request, size, "%s", argv[1]);
     41 		for (int i = 2; i < argc && n > 0 && (size_t)n < size; i++)
     42 			n += snprintf(request + n, size - n, "\t%s", argv[i]);
     43 		return n > 0 && (size_t)n < size;
     44 	}
     45 	return false;
     46 }
     47 
     48 int
     49 main(int argc, char **argv)
     50 {
     51 	struct sockaddr_un address = { .sun_family = AF_UNIX };
     52 	struct timeval timeout = { .tv_sec = 5 };
     53 	const char *path = getenv("IWD_CONTROL_SOCKET");
     54 	char request[CONTROL_REQUEST_MAX], reply[CONTROL_REPLY_MAX];
     55 	bool ping = argc == 1, failed = false;
     56 	ssize_t len;
     57 	int fd = -1;
     58 
     59 	l_log_set_stderr();
     60 	l_log_set_ident("iwctl");
     61 	if (!make_request(argc, argv, request, sizeof(request))) {
     62 		usage();
     63 		return EXIT_FAILURE;
     64 	}
     65 	if (!path)
     66 		path = CONTROL_SOCKET_PATH;
     67 	if (!*path || strlen(path) >= sizeof(address.sun_path)) {
     68 		l_error("Invalid socket path");
     69 		return EXIT_FAILURE;
     70 	}
     71 	memcpy(address.sun_path, path, strlen(path) + 1);
     72 	fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
     73 	if (fd < 0)
     74 		goto error;
     75 	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0 ||
     76 			setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0 ||
     77 			connect(fd, (struct sockaddr *)&address, sizeof(address)) < 0)
     78 		goto error;
     79 	if (L_TFR(send(fd, request, strlen(request), MSG_NOSIGNAL)) < 0)
     80 		goto error;
     81 	for (;;) {
     82 		len = L_TFR(recv(fd, reply, sizeof(reply) - 1, MSG_TRUNC));
     83 		if (len < 0)
     84 			goto error;
     85 		if (len == 0)
     86 			break;
     87 		if ((size_t)len >= sizeof(reply) || reply[len - 1]) {
     88 			errno = EPROTO;
     89 			goto error;
     90 		}
     91 		if (ping && !strcmp(reply, CONTROL_PONG)) {
     92 			puts("ping! pong!");
     93 			break;
     94 		}
     95 		if (!reply[0])
     96 			break;
     97 		if (!strncmp(reply, "error ", 6))
     98 			failed = true;
     99 		if (puts(reply) == EOF)
    100 			break;
    101 	}
    102 	close(fd);
    103 	return fflush(stdout) || ferror(stdout) || failed ? EXIT_FAILURE : EXIT_SUCCESS;
    104 
    105 error:
    106 	l_error("%s: %s", path, strerror(errno));
    107 	if (fd >= 0)
    108 		close(fd);
    109 	return EXIT_FAILURE;
    110 }