From a513d9943fb71607ac8c0f2f3695dec9706f1ebb Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 2 May 2016 04:21:26 -0400 Subject: start working on a client/server model similar to urxvtd/urxvtc the code is still kind of a mess, and it doesn't quite work properly yet, but it's close enough to be a start, i think --- src/runesc.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/runesc.c (limited to 'src/runesc.c') diff --git a/src/runesc.c b/src/runesc.c new file mode 100644 index 0000000..04fc26b --- /dev/null +++ b/src/runesc.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +#include "runes.h" +#include "socket.h" + +static int runes_socket_open_client(char *name); + +int main (int argc, char *argv[]) +{ + char *name, *buf; + int s, i; + size_t len, offset; + uint32_t argc32 = argc, argv_len; + + name = runes_get_socket_name(); + s = runes_socket_open_client(name); + + len = sizeof(argc32) + sizeof(argv_len); + for (i = 0; i < argc; ++i) { + len += strlen(argv[i]) + 1; + } + + buf = malloc(len); + ((uint32_t*)buf)[0] = htonl(argc32); + offset = sizeof(argc32) + sizeof(argv_len); + ((uint32_t*)buf)[1] = htonl(len - offset); + + for (i = 0; i < argc; ++i) { + size_t arg_len = strlen(argv[i]) + 1; + memcpy(buf + offset, argv[i], arg_len); + offset += arg_len; + } + + send(s, buf, offset, 0); + free(buf); + shutdown(s, SHUT_RDWR); +} + +static int runes_socket_open_client(char *name) +{ + int s; + struct sockaddr_un client; + + if (strlen(name) + 1 > MAX_SOCKET_PATH_LEN) { + runes_die("socket path %s is too long\n", name); + } + + s = socket(AF_UNIX, SOCK_STREAM, 0); + if (s < 0) { + runes_die("couldn't create socket: %s\n", strerror(errno)); + } + + client.sun_family = AF_UNIX; + strcpy(client.sun_path, name); + if (connect(s, (struct sockaddr*)(&client), sizeof(struct sockaddr_un))) { + runes_die("couldn't connect to socket at %s: %s\n", name, + strerror(errno)); + } + + return s; +} -- cgit v1.2.3-54-g00ecf