aboutsummaryrefslogtreecommitdiff
path: root/alarmd
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-06-27 19:50:25 +1200
committerDavid Phillips <david@sighup.nz>2018-06-27 19:56:24 +1200
commit4816ac8405f3f8a18f0e296b9a748a27d66fed0e (patch)
tree37124c1d7786147c7a76030e47f44ddab8873be1 /alarmd
parent737b8916cfa2ba16edeee3d9518e3b5715724c0d (diff)
downloadalarmd-4816ac8405f3f8a18f0e296b9a748a27d66fed0e.tar.xz
Change from TCP to UNIX sockets, massive clean up
Diffstat (limited to 'alarmd')
-rw-r--r--alarmd/Makefile3
-rw-r--r--alarmd/alarmd.c52
2 files changed, 19 insertions, 36 deletions
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 <netdb.h>
#include <strings.h>
#include <uuid/uuid.h>
+#include <sys/un.h>
#include <uthash.h>
#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)) {