-
Notifications
You must be signed in to change notification settings - Fork 1
/
devices.c
205 lines (165 loc) · 4.17 KB
/
devices.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* xenoeye
*
* Copyright (c) 2021, Vladimir Misyurov, Michael Kogan
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <arpa/inet.h>
#include <stdlib.h>
#include "devices.h"
#include "aajson/aajson.h"
struct devices_info
{
struct device *devices;
size_t n_devices;
};
static struct devices_info devices = {NULL, 0};
static int
config_adjust_devs_size(struct devices_info *devs, size_t idx)
{
struct device *tmp;
if (devs->n_devices >= (idx + 1)) {
return 1;
}
tmp = realloc(devs->devices, (idx + 1) * sizeof(struct device));
if (!tmp) {
LOG("realloc() failed");
return 0;
}
devs->devices = tmp;
devs->n_devices = idx + 1;
/* init new device */
memset(&devs->devices[devs->n_devices - 1], 0, sizeof(struct device));
return 1;
}
#define STRCMP(A, I, S) strcmp(A->path_stack[I].data.path_item, S)
static int
config_callback(struct aajson *a, aajson_val *value, void *user)
{
struct devices_info *devs = user;
size_t idx;
if (a->path_stack_pos < 2) {
return 1;
}
if (a->path_stack[1].type != AAJSON_PATH_ITEM_ARRAY) {
return 1;
}
idx = a->path_stack[1].data.array_idx;
if (!config_adjust_devs_size(devs, idx)) {
return 0;
}
if (STRCMP(a, 2, "ip") == 0) {
unsigned char buf[sizeof(struct in6_addr)];
int rc;
devs->devices[devs->n_devices - 1].use_ip = 1;
/* FIXME: add IPv6 */
rc = inet_pton(AF_INET, value->str, buf);
if (rc <= 0) {
LOG("Can't parse IP '%s'", value->str);
return 0;
}
devs->devices[devs->n_devices - 1].ip_ver = 4;
devs->devices[devs->n_devices - 1].ip = 0;
memcpy(&devs->devices[devs->n_devices - 1].ip, buf, 4);
}
if (STRCMP(a, 2, "id") == 0) {
devs->devices[devs->n_devices - 1].use_id = 1;
devs->devices[devs->n_devices - 1].id
= htonl(atoi(value->str));
}
if (STRCMP(a, 2, "sampling-rate") == 0) {
devs->devices[devs->n_devices - 1].sampling_rate
= atoi(value->str);
}
return 1;
}
#undef STRCMP
int
devices_load(const char *filename)
{
FILE *f;
long len;
struct aajson conf_json;
int ret = 0;
char *file;
f = fopen(filename, "rb");
if (!f) {
LOG("Can't open config file '%s'", filename);
goto fail_open;
}
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
file = malloc(len);
if (!file) {
LOG("Can't allocate %ld bytes", len);
goto fail_alloc;
}
if (fread(file, 1, len, f) != (size_t)len) {
LOG("Can't read config file '%s'", filename);
goto fail_read;
}
aajson_init(&conf_json, file);
aajson_parse(&conf_json, &config_callback, &devices);
if (conf_json.error) {
LOG("Can't parse config file '%s': line %lu, col %lu: %s",
filename, conf_json.line, conf_json.col,
conf_json.errmsg);
free(devices.devices);
devices.devices = NULL;
devices.n_devices = 0;
goto fail_parse;
}
ret = 1;
fail_parse:
fail_read:
free(file);
fail_alloc:
fclose(f);
fail_open:
return ret;
}
int
device_get_sampling_rate(struct device *d)
{
size_t i;
int found = 0;
for (i=0; i<devices.n_devices; i++) {
struct device *db = &devices.devices[i];
if (db->use_ip && db->use_id) {
/* check all fields */
if ((d->ip_ver == db->ip_ver)
&& (d->ip == db->ip) && (d->id == db->id)) {
found = 1;
d->sampling_rate = db->sampling_rate;
break;
}
} else if (db->use_ip) {
/* only IP */
if ((d->ip_ver == db->ip_ver) && (d->ip == db->ip)) {
found = 1;
d->sampling_rate = db->sampling_rate;
break;
}
} if (db->use_id) {
/* only ID */
if (d->id == db->id) {
found = 1;
d->sampling_rate = db->sampling_rate;
break;
}
}
}
return found;
}