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
|
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#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;
}
freeaddrinfo(s_info);
dump_alarms(sock);
close(sock);
}
|