aboutsummaryrefslogtreecommitdiff
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
parent737b8916cfa2ba16edeee3d9518e3b5715724c0d (diff)
downloadalarmd-4816ac8405f3f8a18f0e296b9a748a27d66fed0e.tar.xz
Change from TCP to UNIX sockets, massive clean up
-rw-r--r--Makefile12
-rw-r--r--alarm-tools/Makefile3
-rw-r--r--alarm-tools/alarms-show.c40
-rw-r--r--alarmd/Makefile3
-rw-r--r--alarmd/alarmd.c52
-rw-r--r--lib/Makefile7
-rw-r--r--lib/libalarm.c16
7 files changed, 57 insertions, 76 deletions
diff --git a/Makefile b/Makefile
index 7b4a567..cd2efa7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,12 @@
-CFLAGS += "-I./inc/"
+CFLAGS += "-I./inc/ -Wall -Wextra"
LDFLAGS += -L./lib/
-.PHONY: lib alarmd alarms-show
+all:
+ make -C lib
+ make -C alarmd
+ make -C alarm-tools
-all: lib alarmd alarm-tools
+clean:
+ make -C lib clean
+ make -C alarmd clean
+ make -C alarm-tools clean
diff --git a/alarm-tools/Makefile b/alarm-tools/Makefile
index 75f3c5b..56b3dff 100644
--- a/alarm-tools/Makefile
+++ b/alarm-tools/Makefile
@@ -2,3 +2,6 @@ LDFLAGS += -L../lib -lalarm
CFLAGS += -I../inc
all: alarms-show
+
+clean:
+ rm alarms-show
diff --git a/alarm-tools/alarms-show.c b/alarm-tools/alarms-show.c
index ba466a5..7613489 100644
--- a/alarm-tools/alarms-show.c
+++ b/alarm-tools/alarms-show.c
@@ -6,6 +6,7 @@
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
+#include <sys/un.h>
#include "alarmd_proto.h"
@@ -52,6 +53,10 @@ int dump_alarms(int sock)
printf("[%s] %s\n", is_raised ? "\x1b[1;31mRAISE\x1b[0m" : "\x1b[1;32mCLEAR\x1b[0m", buffer);
}
+ if (count == 0) {
+ printf("No alarms registered.\n");
+ }
+
if (i < count) {
fprintf(stderr, "Not all alarms received, output must be missing some\n");
return 1;
@@ -66,37 +71,22 @@ int main(void)
{
int sock = 0;
char buffer[128];
- struct addrinfo hints, *s_info, *p;
+ struct sockaddr_un server;
- bzero(&hints, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
-
- /* FIXME spec custom hostname on cmd line */
- if (getaddrinfo("localhost", 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 (connect(sock, p->ai_addr, p->ai_addrlen) < 0) {
- close(sock);
- perror("connect");
- continue;
- }
- break;
- }
-
- if (!p) {
- fprintf(stderr, "Connection to server failed\n");
+ server.sun_family = AF_UNIX;
+ strcpy(server.sun_path, "/home/david/alarmd_sock");
+ printf("Connecting...\n");
+ if (connect(sock, (struct sockaddr *)&server, strlen(server.sun_path) + sizeof(server.sun_family)) < 0) {
+ close(sock);
+ perror("connect");
return 1;
}
-
- freeaddrinfo(s_info);
+ printf("Connected.\n");
dump_alarms(sock);
close(sock);
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)) {
diff --git a/lib/Makefile b/lib/Makefile
index 52368e0..2c9ccc9 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -4,5 +4,8 @@ CFLAGS += "-I../inc/"
libalarm.so: libalarm.o
$(CC) -shared -o $@ $< $(LDFLAGS)
-.o.c:
- $(CC) -o $@ $< $(LDFLAGS)
+%.o: %.c
+ $(CC) -c -o $@ $< $(LDFLAGS) $(CFLAGS)
+
+clean:
+ rm libalarm.{s,}o
diff --git a/lib/libalarm.c b/lib/libalarm.c
index 856d8f6..fb96e37 100644
--- a/lib/libalarm.c
+++ b/lib/libalarm.c
@@ -1,7 +1,12 @@
#include <stdio.h>
#include <stdint.h>
#include <sys/socket.h>
-#include <strings.h>
+#include <unistd.h>
+#include <string.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+
+
int8_t recv_string(int sock, char (*buffer)[128])
{
@@ -46,15 +51,6 @@ int send_string(int sock, char *buffer)
return 0;
}
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <unistd.h>
-
#include "alarmd_proto.h"
int alarmd_register(int sock, char *desc, char (*uuid)[16])