aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-06-27 20:51:32 +1200
committerDavid Phillips <david@sighup.nz>2018-06-27 20:51:32 +1200
commitce3357fc9151e7b4f0d233b4accf6c7a780327d6 (patch)
treed1219ef18a4a2e4e56ba4387bb53ab47a310dfdc
parent4816ac8405f3f8a18f0e296b9a748a27d66fed0e (diff)
downloadalarmd-ce3357fc9151e7b4f0d233b4accf6c7a780327d6.tar.xz
Begin server refactor, remove hard-coded sockets
-rw-r--r--Makefile3
-rw-r--r--alarm-tools/alarms-show.c10
-rw-r--r--alarmd/alarmd.c229
3 files changed, 158 insertions, 84 deletions
diff --git a/Makefile b/Makefile
index cd2efa7..b18d07e 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,4 @@
-CFLAGS += "-I./inc/ -Wall -Wextra"
-LDFLAGS += -L./lib/
+export CFLAGS += -Wall
all:
make -C lib
diff --git a/alarm-tools/alarms-show.c b/alarm-tools/alarms-show.c
index 7613489..3af493d 100644
--- a/alarm-tools/alarms-show.c
+++ b/alarm-tools/alarms-show.c
@@ -67,19 +67,23 @@ int dump_alarms(int sock)
return 0;
}
-int main(void)
+int main(int argc, char **argv)
{
int sock = 0;
- char buffer[128];
struct sockaddr_un server;
+ if (argc != 2) {
+ fprintf(stderr, "Syntax: %s socket_name\n", argv[0]);
+ return 1;
+ }
+
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
return 1;
}
server.sun_family = AF_UNIX;
- strcpy(server.sun_path, "/home/david/alarmd_sock");
+ strcpy(server.sun_path, argv[1]);
printf("Connecting...\n");
if (connect(sock, (struct sockaddr *)&server, strlen(server.sun_path) + sizeof(server.sun_family)) < 0) {
close(sock);
diff --git a/alarmd/alarmd.c b/alarmd/alarmd.c
index 7a8b125..a0f650a 100644
--- a/alarmd/alarmd.c
+++ b/alarmd/alarmd.c
@@ -39,22 +39,150 @@ int8_t read_string(int sock, unsigned char (*buffer)[128])
return length;
}
-int main(void)
+int handle_register(int c_sock)
+{
+ uuid_t uuid;
+ unsigned char buffer[128];
+ struct alarm *new_alarm = NULL;
+
+ if (read_string(c_sock, &buffer) < 0) {
+ return 1;
+ }
+
+ uuid_generate(uuid);
+ //uuid_unparse(uuid, uuid_str);
+ //fprintf(stderr, "Registering alarm %s with uuid %s\n", buffer, uuid_str);
+
+ if (send(c_sock, uuid, sizeof(uuid), 0) != sizeof(uuid)) {
+ perror("send");
+ return 1;
+ }
+
+ if ((new_alarm = malloc(sizeof(*new_alarm))) == NULL) {
+ perror("malloc");
+ return 1;
+ }
+
+ new_alarm->name = strdup((const char*)buffer);
+ memcpy(new_alarm->uuid, uuid, 16);
+ new_alarm->is_raised = 0;
+
+ /* LOCK */
+ HASH_ADD(hh, alarms, uuid, 16, new_alarm);
+ /* UNLOCK */
+ return 0;
+}
+
+int handle_deregister(int c_sock)
+{
+ unsigned char buffer[128];
+ struct alarm *target = NULL;
+ char uuid_str[37];
+ /* FIXME 16 magic */
+ if (recv(c_sock, &buffer, 16, 0) < 16) {
+ return 1;
+ }
+ uuid_unparse(buffer, uuid_str);
+ fprintf(stderr, "Deregistering alarm %s\n", uuid_str);
+ /* LOCK */
+ HASH_FIND(hh, alarms, buffer, 16, target);
+ if (target != NULL) {
+ HASH_DEL(alarms, target);
+ free(target);
+ }
+ /* UNLOCK */
+
+ return 0;
+}
+
+int handle_raise(int c_sock)
+{
+ unsigned char buffer[128];
+ struct alarm *target = NULL;
+ char uuid_str[37];
+ /* FIXME 16 magic */
+ if (recv(c_sock, &buffer, 16, 0) < 16) {
+ return 1;
+ }
+ uuid_unparse(buffer, uuid_str);
+ fprintf(stderr, "Raising alarm %s\n", uuid_str);
+
+ /* LOCK */
+ HASH_FIND(hh, alarms, buffer, 16, target);
+ if (target != NULL) {
+ target->is_raised = 1;
+ }
+ /* UNLOCK */
+ return 0;
+}
+
+int handle_clear(int c_sock)
+{
+ unsigned char buffer[128];
+ struct alarm *target = NULL;
+ char uuid_str[37];
+ /* FIXME 16 magic */
+ if (recv(c_sock, &buffer, 16, 0) < 16) {
+ return 1;
+ }
+ uuid_unparse(buffer, uuid_str);
+ fprintf(stderr, "Clearing alarm %s\n", uuid_str);
+ /* LOCK */
+ HASH_FIND(hh, alarms, buffer, 16, target);
+ if (target != NULL) {
+ target->is_raised = 0;
+ }
+ /* UNLOCK */
+ return 0;
+}
+
+int handle_query(int c_sock)
+{
+ uint32_t count = 0;
+ uint8_t length = 0;
+ struct alarm *a = NULL;
+ struct alarm *tmp = NULL;
+
+ /* LOCK */
+ count = htonl(HASH_COUNT(alarms));
+
+ 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);
+ /* FIXME factor out, idiot. also bounds check on strlen */
+ length = strlen(a->name);
+ if (send(c_sock, &length, sizeof(length), 0) < sizeof(length)) {
+ perror("send");
+ }
+ if (send(c_sock, a->name, strlen(a->name), 0) < strlen(a->name)) {
+ perror("send");
+ }
+ if (send(c_sock, &(a->is_raised), sizeof(a->is_raised), 0) < sizeof(a->is_raised)) {
+ perror("send");
+ }
+ fprintf(stderr, "SENT.\n");
+ }
+ /* UNLOCK */
+ return 0;
+}
+
+
+int main(int argc, char **argv)
{
int sock = 0;
int c_sock = 0;
socklen_t c_addr_l = 0;
struct sockaddr_un local, c_addr;
- unsigned char buffer[128];
uint32_t packet_type = 0;
ssize_t nread = 0;
- uint32_t count = 0;
-
- int reuse = 1;
- uuid_t uuid;
- char uuid_str[37];
- struct alarm *target = NULL;
+ if (argc != 2) {
+ fprintf(stderr, "Syntax: %s socket_name\n", argv[0]);
+ return 1;
+ }
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
@@ -62,7 +190,7 @@ int main(void)
}
local.sun_family = AF_UNIX;
- strcpy(local.sun_path, "/home/david/alarmd_sock");
+ strcpy(local.sun_path, argv[1]);
unlink(local.sun_path);
if (bind(sock, (struct sockaddr *)&local, strlen(local.sun_path) + sizeof(local.sun_family)) < 0) {
perror("bind");
@@ -86,95 +214,38 @@ int main(void)
if (nread < 0) {
perror("recv");
return 1;
+ } else if (nread == 0) {
+ break;
} else if (nread < sizeof(packet_type)) {
- fprintf(stderr, "Packet type too short (%d)\n", nread);
+ fprintf(stderr, "Packet type too short\n");
return 1;
}
packet_type = ntohl(packet_type);
switch(packet_type) {
case ALARMD_PACKET_TYPE_REGISTER:
- if (read_string(c_sock, &buffer) < 0) {
- return 1;
- }
- uuid_generate(uuid);
- //uuid_unparse(uuid, uuid_str);
- //fprintf(stderr, "Registering alarm %s with uuid %s\n", buffer, uuid_str);
- if (send(c_sock, uuid, sizeof(uuid), 0) != sizeof(uuid)) {
- perror("send");
- return 1;
- }
- struct alarm *new_alarm = malloc(sizeof(*new_alarm));
- if (new_alarm == NULL) {
- perror("malloc");
- return 1;
+ if (handle_register(c_sock)) {
+ close(c_sock);;
}
- new_alarm->name = strdup(buffer);
- memcpy(new_alarm->uuid, uuid, 16);
- new_alarm->is_raised = 0;
- HASH_ADD(hh, alarms, uuid, 16, new_alarm);
break;
case ALARMD_PACKET_TYPE_DEREGISTER:
- /* FIXME 16 magic */
- if (recv(c_sock, &buffer, 16, 0) < 16) {
- return 1;
- }
- uuid_unparse(buffer, uuid_str);
- fprintf(stderr, "Deregistering alarm %s\n", uuid_str);
- HASH_FIND(hh, alarms, buffer, 16, target);
- if (target != NULL) {
- HASH_DEL(alarms, target);
- free(target);
+ if (handle_deregister(c_sock)) {
+ close(c_sock);
}
break;
case ALARMD_PACKET_TYPE_RAISE:
- /* FIXME 16 magic */
- if (recv(c_sock, &buffer, 16, 0) < 16) {
- return 1;
- }
- uuid_unparse(buffer, uuid_str);
- fprintf(stderr, "Raising alarm %s\n", uuid_str);
- HASH_FIND(hh, alarms, buffer, 16, target);
- if (target != NULL) {
- target->is_raised = 1;
+ if (handle_raise(c_sock)) {
+ close(c_sock);
}
break;
case ALARMD_PACKET_TYPE_CLEAR:
- /* FIXME 16 magic */
- if (recv(c_sock, &buffer, 16, 0) < 16) {
- return 1;
- }
- uuid_unparse(buffer, uuid_str);
- fprintf(stderr, "Clearing alarm %s\n", uuid_str);
- HASH_FIND(hh, alarms, buffer, 16, target);
- if (target != NULL) {
- target->is_raised = 0;
+ if (handle_clear(c_sock)) {
+ close(c_sock);
}
break;
case ALARMD_PACKET_TYPE_QUERY:
- /* bounds-check HASH_COUNT*/
- count = htonl(HASH_COUNT(alarms));
- uint8_t length = 0;
- /* 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);
- /* FIXME factor out, idiot. also bounds check on strlen */
- length = strlen(a->name);
- if (send(c_sock, &length, sizeof(length), 0) < sizeof(length)) {
- perror("send");
- }
- if (send(c_sock, a->name, strlen(a->name), 0) < strlen(a->name)) {
- perror("send");
- }
- if (send(c_sock, &(a->is_raised), sizeof(a->is_raised), 0) < sizeof(a->is_raised)) {
- perror("send");
- }
- fprintf(stderr, "SENT.\n");
+ if (handle_query(c_sock)) {
+ close(c_sock);
}
break;
default: