From 42a2a579628d4aa8153775cdcd894b8b5c80e7da Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 6 Feb 2025 20:08:50 +0100 Subject: m + libradharc for client-side protocol implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- radharc-ipc.c | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) (limited to 'radharc-ipc.c') diff --git a/radharc-ipc.c b/radharc-ipc.c index 83f436f..3787d2d 100644 --- a/radharc-ipc.c +++ b/radharc-ipc.c @@ -11,11 +11,13 @@ #include #include +#include "libradharc.h" + USAGE("[-R rule] (pid | @name | &fd | address) ..."); struct message { - char *text; + void *text; size_t len; }; @@ -46,7 +48,7 @@ main(int argc, char *argv[]) size_t i, j; struct sockaddr_un addr; size_t addrlen; - int sock; + int sock, *socks; struct message *messages = NULL; size_t nmessages = 0; @@ -70,9 +72,24 @@ main(int argc, char *argv[]) if (!addresses) goto fail; + sock = socket(PF_UNIX, SOCK_DGRAM, 0); + if (sock < 0) + goto fail; + + /* we need an address, otherwise the peer will not get an address to send the response to */ + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + if (bind(sock, (void *)&addr, (socklen_t)offsetof(struct sockaddr_un, sun_path))) + goto fail; + + socks = calloc((size_t)argc, sizeof(int)); + if (!socks) + goto fail; + for (i = 0; i < (size_t)argc; i++) { const char *arg = argv[i]; uintmax_t num; + socks[i] = sock; if (*arg == '@' || strchr(arg, '/')) { addresses[i] = strdup(arg); if (addresses[i]) @@ -83,11 +100,8 @@ main(int argc, char *argv[]) if (*arg == '&') { if (num > (uintmax_t)INT_MAX) usage(); - /* TODO whoops, this should be send with that fd, not to it */ - addresses[i] = malloc(sizeof("/proc/self/fd/") + strlen(&arg[1])); - if (!addresses[i]) - goto fail; - stpcpy(stpcpy(addresses[i], "/proc/self/fd/"), &arg[1]); + addresses[i] = NULL; + socks[i] = (int)num; } else { addresses[i] = malloc(sizeof("@/proc//") + strlen(arg) + strlen(class)); if (!addresses[i]) @@ -105,15 +119,11 @@ main(int argc, char *argv[]) } } - sock = socket(PF_UNIX, SOCK_DGRAM, 0); - if (sock < 0) - goto fail; - for (j = 0; j < nmessages; j++) { for (i = 0; i < (size_t)argc; i++) { size_t len; - if (!addresses[i]) + if (socks[i] < 0) continue; len = strlen(&addresses[i][1]) + 1U; @@ -122,15 +132,26 @@ main(int argc, char *argv[]) memcpy(addr.sun_path, addresses[i], len); addrlen = offsetof(struct sockaddr_un, sun_path) + len; - if (sendto(sock, messages[j].text, messages[j].len, MSG_NOSIGNAL, + if (socks[i] != sock) { + if (send(socks[i], messages[j].text, messages[j].len, MSG_NOSIGNAL) < 0) + goto send_failed; + } else if (sendto(sock, messages[j].text, messages[j].len, MSG_NOSIGNAL, (void *)&addr, (socklen_t)addrlen) < 0) { + send_failed: fprintf(stderr, "%s: error while sending to '%s': %s\n", argv0, argv[i], strerror(errno)); ret = 1; - addresses[i] = NULL; + socks[i] = -1; } } free(messages[j].text); + + for (i = 0; i < (size_t)argc; i++) { + if (socks[i] < 0) + continue; + + /* TODO get response */ + } } close(sock); @@ -140,6 +161,7 @@ main(int argc, char *argv[]) free(addresses); free(messages); free(class); + free(socks); return ret; fail: -- cgit v1.2.3-70-g09d2