aboutsummaryrefslogtreecommitdiffstats
path: root/radharc-ipc.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-02-06 20:08:50 +0100
committerMattias Andrée <m@maandree.se>2025-02-06 20:08:50 +0100
commit42a2a579628d4aa8153775cdcd894b8b5c80e7da (patch)
tree992dd3b057a7a18d2258b131c83ff56fd84587a8 /radharc-ipc.c
parentm (diff)
downloadradharc-42a2a579628d4aa8153775cdcd894b8b5c80e7da.tar.gz
radharc-42a2a579628d4aa8153775cdcd894b8b5c80e7da.tar.bz2
radharc-42a2a579628d4aa8153775cdcd894b8b5c80e7da.tar.xz
m + libradharc for client-side protocol implementation
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'radharc-ipc.c')
-rw-r--r--radharc-ipc.c50
1 files changed, 36 insertions, 14 deletions
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 <string.h>
#include <unistd.h>
+#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: