From 4816ac8405f3f8a18f0e296b9a748a27d66fed0e Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 27 Jun 2018 19:50:25 +1200 Subject: Change from TCP to UNIX sockets, massive clean up --- alarmd/Makefile | 3 +++ alarmd/alarmd.c | 52 ++++++++++++++++------------------------------------ 2 files changed, 19 insertions(+), 36 deletions(-) (limited to 'alarmd') diff --git a/alarmd/Makefile b/alarmd/Makefile index b6c56d9..9c69056 100644 --- a/alarmd/Makefile +++ b/alarmd/Makefile @@ -2,3 +2,6 @@ LDFLAGS += -L../lib -lalarm -luuid CFLAGS += -I../inc all: alarmd + +clean: + rm alarmd diff --git a/alarmd/alarmd.c b/alarmd/alarmd.c index 239fc3b..7a8b125 100644 --- a/alarmd/alarmd.c +++ b/alarmd/alarmd.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "alarmd_proto.h" @@ -22,7 +23,7 @@ struct alarm { static struct alarm *alarms; -int8_t read_string(int sock, char (*buffer)[128]) +int8_t read_string(int sock, unsigned char (*buffer)[128]) { uint8_t length = 0; if (recv(sock, &length, sizeof(uint8_t), 0) < sizeof(uint8_t)) { @@ -43,9 +44,8 @@ int main(void) int sock = 0; int c_sock = 0; socklen_t c_addr_l = 0; - struct addrinfo hints, *s_info, *p; - struct sockaddr_storage c_addr; - char buffer[128]; + struct sockaddr_un local, c_addr; + unsigned char buffer[128]; uint32_t packet_type = 0; ssize_t nread = 0; uint32_t count = 0; @@ -56,39 +56,19 @@ int main(void) char uuid_str[37]; struct alarm *target = NULL; - bzero(&hints, sizeof(hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE; - - if (getaddrinfo(NULL, ALARMD_PORT, &hints, &s_info) < 0) { - perror("getaddrinfo"); + if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + perror("socket"); return 1; } - for (p = s_info; p != NULL; p = p->ai_next) { - if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) { - perror("socket"); - continue; - } - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) { - perror("setsockopt"); - return 1; - } - if (bind(sock, p->ai_addr, p->ai_addrlen) < 0) { - perror("bind"); - continue; - } - break; - } - - if (!p) { - fprintf(stderr, "No addresses to bind to\n"); + local.sun_family = AF_UNIX; + strcpy(local.sun_path, "/home/david/alarmd_sock"); + unlink(local.sun_path); + if (bind(sock, (struct sockaddr *)&local, strlen(local.sun_path) + sizeof(local.sun_family)) < 0) { + perror("bind"); return 1; } - freeaddrinfo(s_info); - if (listen(sock, 128) < 0) { perror("listen"); return 1; @@ -104,10 +84,10 @@ int main(void) /* read packet type */ nread = recv(c_sock, &packet_type, sizeof(packet_type), 0); if (nread < 0) { - perror("read"); + perror("recv"); return 1; } else if (nread < sizeof(packet_type)) { - fprintf(stderr, "Packet type too short\n"); + fprintf(stderr, "Packet type too short (%d)\n", nread); return 1; } packet_type = ntohl(packet_type); @@ -178,11 +158,11 @@ int main(void) /* FIXME */ struct alarm *a = NULL; struct alarm *tmp = NULL; + if (send(c_sock, &count, sizeof(count), 0) < sizeof(count)) { + perror("send"); + } HASH_ITER(hh, alarms, a, tmp) { fprintf(stderr, "DEBUG : [%s] %s\n", a->is_raised ? "RAISE" : "CLEAR" , a->name); - if (send(c_sock, &count, sizeof(count), 0) < sizeof(count)) { - perror("send"); - } /* FIXME factor out, idiot. also bounds check on strlen */ length = strlen(a->name); if (send(c_sock, &length, sizeof(length), 0) < sizeof(length)) { -- cgit v1.1