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 --- alarm-tools/alarms-show.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 alarm-tools/alarms-show.c (limited to 'alarm-tools/alarms-show.c') diff --git a/alarm-tools/alarms-show.c b/alarm-tools/alarms-show.c new file mode 100644 index 0000000..0d63d4a --- /dev/null +++ b/alarm-tools/alarms-show.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "alarmd_proto.h" + +int dump_alarms(int sock) +{ + ssize_t i = 0; + ssize_t nread = 0; + size_t length = 0; + uint32_t count = 0; + uint32_t packet_type = 0; + uint8_t is_raised = 0; + char buffer[128]; + + packet_type = htonl(ALARMD_PACKET_TYPE_QUERY); + + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + nread = recv(sock, &count, sizeof(count), 0); + if (nread < 0) { + perror("recv"); + return 1; + } else if (nread < sizeof(count)) { + fprintf(stderr, "Alarm count too small\n"); + return 1; + } + + count = ntohl(count); + + printf("Alarms\n" + "------\n"); + + for (i = 0; i < count; i++) { + if ((length = recv_string(sock, &buffer)) < 0) { + perror("recv"); + break; + } + if (recv(sock, &is_raised, sizeof(is_raised), 0) != sizeof(is_raised)) { + perror("recv"); + break; + } + printf("[%s] %s\n", is_raised ? "\x1b[1;31mRAISE\x1b[0m" : "\x1b[1;32mCLEAR\x1b[0m", buffer); + } + + if (i < count) { + fprintf(stderr, "Not all alarms received, output must be missing some\n"); + return 1; + } + + printf("\n"); + + return 0; +} + +int main(void) +{ + int sock = 0; + char buffer[128]; + struct addrinfo hints, *s_info, *p; + + 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"); + 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"); + return 1; + } + + + /**/ + char *name = "Some test alarm"; + char uuid[16]; + /**/ + + freeaddrinfo(s_info); + + dump_alarms(sock); + close(sock); +} -- cgit v1.1