From 6b59b063ecf29868bc6ea8c3734a671d1206ff1b Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 27 Jun 2018 21:49:48 +1200 Subject: Refactor libalarm --- alarm-tools/alarms-show.c | 2 +- alarmd/alarmd.c | 35 +++++++++++++--------- inc/alarmd_proto.h | 9 +++--- lib/libalarm.c | 76 ++++++++++++++++------------------------------- 4 files changed, 53 insertions(+), 69 deletions(-) diff --git a/alarm-tools/alarms-show.c b/alarm-tools/alarms-show.c index 3af493d..65d0e91 100644 --- a/alarm-tools/alarms-show.c +++ b/alarm-tools/alarms-show.c @@ -32,7 +32,7 @@ int dump_alarms(int sock) perror("recv"); return 1; } else if (nread < sizeof(count)) { - fprintf(stderr, "Alarm count too small\n"); + fprintf(stderr, "Alarm count size too small\n"); return 1; } diff --git a/alarmd/alarmd.c b/alarmd/alarmd.c index 93336b8..59942eb 100644 --- a/alarmd/alarmd.c +++ b/alarmd/alarmd.c @@ -15,7 +15,7 @@ struct alarm { UT_hash_handle hh; - char uuid[16]; + uuid_t uuid; char *name; bool is_raised; // time_t last_change; @@ -53,7 +53,7 @@ int handle_register(int c_sock) //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)) { + if (send(c_sock, uuid, sizeof(uuid_t), 0) != sizeof(uuid_t)) { perror("send"); return 1; } @@ -75,17 +75,19 @@ int handle_register(int c_sock) int handle_deregister(int c_sock) { - unsigned char buffer[128]; + uuid_t uuid; struct alarm *target = NULL; char uuid_str[37]; - /* FIXME 16 magic */ - if (recv(c_sock, &buffer, 16, 0) < 16) { + + if (recv(c_sock, &uuid, sizeof(uuid_t), 0) < sizeof(uuid_t)) { return 1; } - uuid_unparse(buffer, uuid_str); + + uuid_unparse(uuid, uuid_str); fprintf(stderr, "Deregistering alarm %s\n", uuid_str); + /* LOCK */ - HASH_FIND(hh, alarms, buffer, 16, target); + HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target); if (target != NULL) { HASH_DEL(alarms, target); free(target); @@ -97,18 +99,17 @@ int handle_deregister(int c_sock) int handle_set_raised_generic(int c_sock, int shall_raise) { - unsigned char buffer[128]; + uuid_t uuid; + size_t nread = 0; struct alarm *target = NULL; - char uuid_str[37]; - /* FIXME 16 magic */ - if (recv(c_sock, &buffer, 16, 0) < 16) { + + if ((nread = recv(c_sock, &uuid, 16, 0)) < 16) { + fprintf(stderr, "received not enough - %d\n", nread); return 1; } - uuid_unparse(buffer, uuid_str); - fprintf(stderr, "%sing alarm %s\n", shall_raise ? "Rais" : "Clear", uuid_str); /* LOCK */ - HASH_FIND(hh, alarms, buffer, 16, target); + HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target); if (target != NULL) { target->is_raised = shall_raise; } @@ -206,6 +207,7 @@ int main(int argc, char **argv) perror("recv"); return 1; } else if (nread == 0) { + printf("Zschero\n"); break; } else if (nread < sizeof(packet_type)) { fprintf(stderr, "Packet type too short\n"); @@ -216,26 +218,31 @@ int main(int argc, char **argv) switch(packet_type) { case ALARMD_PACKET_TYPE_REGISTER: if (handle_register(c_sock)) { + printf("reg fail\n"); close(c_sock); } break; case ALARMD_PACKET_TYPE_DEREGISTER: if (handle_deregister(c_sock)) { + printf("rereg fail\n"); close(c_sock); } break; case ALARMD_PACKET_TYPE_RAISE: if (handle_raise(c_sock)) { + printf("raise fail\n"); close(c_sock); } break; case ALARMD_PACKET_TYPE_CLEAR: if (handle_clear(c_sock)) { + printf("clear fail\n"); close(c_sock); } break; case ALARMD_PACKET_TYPE_QUERY: if (handle_query(c_sock)) { + printf("query fail\n"); close(c_sock); } break; diff --git a/inc/alarmd_proto.h b/inc/alarmd_proto.h index ebeddf1..3082424 100644 --- a/inc/alarmd_proto.h +++ b/inc/alarmd_proto.h @@ -2,6 +2,7 @@ #define ALARMD_PROTO_H #include +#include #define ALARMD_PORT "1433" @@ -15,9 +16,9 @@ enum alarmd_packet_type { int8_t recv_string(int sock, char (*buffer)[128]); int send_string(int sock, char *buffer); -int alarmd_register(int sock, char *desc, char (*uuid)[16]); -int alarmd_deregister(int sock, char uuid[16]); -int alarmd_raise(int sock, char uuid[16]); -int alarmd_clear(int sock, char uuid[16]); +int alarmd_register(int sock, char *desc, uuid_t *uuid); +int alarmd_deregister(int sock, uuid_t uuid); +int alarmd_raise(int sock, uuid_t uuid); +int alarmd_clear(int sock, uuid_t uuid); #endif /* ALARMD_PROTO_H */ diff --git a/lib/libalarm.c b/lib/libalarm.c index fb96e37..6140580 100644 --- a/lib/libalarm.c +++ b/lib/libalarm.c @@ -1,13 +1,15 @@ #include #include #include +#include #include #include -#include #include +#include +#include "alarmd_proto.h" - +/* Helper function */ int8_t recv_string(int sock, char (*buffer)[128]) { uint8_t length = 0; @@ -24,6 +26,7 @@ int8_t recv_string(int sock, char (*buffer)[128]) return length; } +/* Helper function */ int send_string(int sock, char *buffer) { ssize_t sent = 0; @@ -51,9 +54,21 @@ int send_string(int sock, char *buffer) return 0; } -#include "alarmd_proto.h" +/* Helper function */ +int send_packet_uuid(int sock, uint32_t packet_type, uuid_t uuid) +{ + uint32_t packet_type_n = 0; + + packet_type_n = htonl(packet_type); + + return (send(sock, &packet_type_n, sizeof(packet_type_n), 0) != sizeof(packet_type_n) + || send(sock, uuid, 16, 0) != 16); +} + +/** End helper functions ***********/ + -int alarmd_register(int sock, char *desc, char (*uuid)[16]) +int alarmd_register(int sock, char *desc, uuid_t *uuid) { uint8_t length = 0; uint32_t packet_type = 0; @@ -74,63 +89,24 @@ int alarmd_register(int sock, char *desc, char (*uuid)[16]) perror("send"); return 1; } - if (recv(sock, uuid, 16, 0) != 16) { + if (recv(sock, uuid, sizeof(*uuid), 0) != sizeof(*uuid)) { perror("recv"); return 1; } return 0; } -int alarmd_deregister(int sock, char uuid[16]) +int alarmd_deregister(int sock, uuid_t uuid) { - 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; + return send_packet_uuid(sock, ALARMD_PACKET_TYPE_DEREGISTER, uuid); } -int alarmd_raise(int sock, char uuid[16]) +int alarmd_raise(int sock, uuid_t uuid) { - 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; + return send_packet_uuid(sock, ALARMD_PACKET_TYPE_RAISE, uuid); } -int alarmd_clear(int sock, char uuid[16]) +int alarmd_clear(int sock, uuid_t uuid) { - 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; + return send_packet_uuid(sock, ALARMD_PACKET_TYPE_CLEAR, uuid); } -- cgit v1.1