aboutsummaryrefslogtreecommitdiff
path: root/lib/libalarm.c
blob: fb96e375bc3a8ce4afef7fc9f4e45b59815e8b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdint.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>



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 "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;
}