From 5f5316a034d68f8825a16e7b866c8e8b2b6c6bd8 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 4 Jun 2018 00:09:02 +1200 Subject: Initial dump of prototype --- lib/Makefile | 8 ++++ lib/libalarm.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 lib/Makefile create mode 100644 lib/libalarm.c (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..52368e0 --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,8 @@ + +CFLAGS += "-I../inc/" + +libalarm.so: libalarm.o + $(CC) -shared -o $@ $< $(LDFLAGS) + +.o.c: + $(CC) -o $@ $< $(LDFLAGS) diff --git a/lib/libalarm.c b/lib/libalarm.c new file mode 100644 index 0000000..856d8f6 --- /dev/null +++ b/lib/libalarm.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include + +int8_t recv_string(int sock, char (*buffer)[128]) +{ + uint8_t length = 0; + if (recv(sock, &length, sizeof(uint8_t), 0) < sizeof(uint8_t)) { + return -1; + } + if (length >= 128) { + return -1; + } + if (recv(sock, buffer, length, 0) < 0) { + return -1; + } + (*buffer)[length] = '\0'; + return length; +} + +int send_string(int sock, char *buffer) +{ + ssize_t sent = 0; + size_t length = 0; + uint8_t length8 = 0; + + /* Protocol allows strings of length < 128 */ + if (length >= 128) { + return 1; + } + + /* bitmasking is unnecessary by now aside from clarity */ + length8 = length8 & 0xFF; + + if (send(sock, &length8, sizeof(length8), 0) < sizeof(length8)) { + return 1; + } + sent = send(sock, buffer, length8, 0); + if (sent < 0) { + perror("send"); + return 1; + } else if (sent < length8) { + return 1; + } + return 0; +} + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "alarmd_proto.h" + +int alarmd_register(int sock, char *desc, char (*uuid)[16]) +{ + uint8_t length = 0; + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_REGISTER); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + length = strlen(desc); + if (send(sock, &length, sizeof(length), 0) != sizeof(length)) { + perror("send"); + return 1; + } + + if (send(sock, desc, strlen(desc), 0) != strlen(desc)) { + perror("send"); + return 1; + } + if (recv(sock, uuid, 16, 0) != 16) { + perror("recv"); + return 1; + } + return 0; +} + +int alarmd_deregister(int sock, char uuid[16]) +{ + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_DEREGISTER); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + /* FIXME 16 magic */ + if (send(sock, uuid, 16, 0) != 16) { + perror("send"); + return 1; + } + return 0; +} + +int alarmd_raise(int sock, char uuid[16]) +{ + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_RAISE); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + /* FIXME 16 magic */ + if (send(sock, uuid, 16, 0) != 16) { + perror("send"); + return 1; + } + return 0; +} + +int alarmd_clear(int sock, char uuid[16]) +{ + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_CLEAR); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + /* FIXME 16 magic */ + if (send(sock, uuid, 16, 0) != 16) { + perror("send"); + return 1; + } + return 0; +} -- cgit v1.1