Code

proto: Let unmarshal functions return the number of bytes processed.
[sysdb.git] / src / utils / proto.c
1 /*
2  * SysDB - src/utils/proto.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "core/data.h"
33 #include "core/store.h"
34 #include "core/time.h"
35 #include "utils/error.h"
36 #include "utils/proto.h"
38 #include <assert.h>
39 #include <errno.h>
41 #include <arpa/inet.h>
42 #include <limits.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include <sys/select.h>
49 /*
50  * private helper functions
51  */
53 /* In case there's not enough buffer space, the marshal functions have to
54  * return the number of bytes that would have been written if enough space had
55  * been available. */
57 static ssize_t
58 marshal_int32(char *buf, size_t buf_len, uint32_t v)
59 {
60         if (buf_len >= sizeof(v)) {
61                 v = htonl(v);
62                 memcpy(buf, &v, sizeof(v));
63         }
64         return sizeof(v);
65 } /* marshal_int32 */
67 static ssize_t
68 marshal_int64(char *buf, size_t buf_len, int64_t v)
69 {
70         if (buf_len >= sizeof(v)) {
71 #if __BYTE_ORDER != __BIG_ENDIAN
72                 v = (((int64_t)htonl((int32_t)v)) << 32)
73                         + ((int64_t)htonl((int32_t)(v >> 32)));
74 #endif
75                 memcpy(buf, &v, sizeof(v));
76         }
77         return sizeof(v);
78 } /* marshal_int64 */
80 static ssize_t
81 marshal_double(char *buf, size_t buf_len, double v)
82 {
83         uint64_t t = 0;
84         assert(sizeof(v) == sizeof(t));
85         memcpy(&t, &v, sizeof(v));
86 #if IEEE754_DOUBLE_BYTE_ORDER != IEEE754_DOUBLE_BIG_ENDIAN
87         t = (((int64_t)htonl((int32_t)t)) << 32)
88                 + ((int64_t)htonl((int32_t)(t >> 32)));
89 #endif
90         if (buf_len >= sizeof(t))
91                 memcpy(buf, &t, sizeof(t));
92         return sizeof(t);
93 } /* marshal_double */
95 static ssize_t
96 marshal_datetime(char *buf, size_t buf_len, sdb_time_t v)
97 {
98         return marshal_int64(buf, buf_len, (int64_t)v);
99 } /* marshal_datetime */
101 static ssize_t
102 marshal_binary(char *buf, size_t buf_len, size_t len, const unsigned char *v)
104         uint32_t tmp = htonl((uint32_t)len);
105         if (buf_len >= sizeof(tmp) + len) {
106                 memcpy(buf, &tmp, sizeof(tmp));
107                 memcpy(buf + sizeof(tmp), v, len);
108         }
109         return sizeof(tmp) + len;
110 } /* marshal_binary */
112 static ssize_t
113 marshal_string(char *buf, size_t buf_len, const char *v)
115         /* The actual string including the terminating null byte. */
116         size_t len = strlen(v) + 1;
117         if (buf_len >= len)
118                 memcpy(buf, v, len);
119         return len;
120 } /* marshal_string */
122 #define OBJ_HEADER_LEN (sizeof(uint32_t) + sizeof(sdb_time_t))
123 static ssize_t
124 marshal_obj_header(char *buf, size_t buf_len,
125                 int type, sdb_time_t last_update)
127         ssize_t n;
129         if (buf_len < OBJ_HEADER_LEN)
130                 return OBJ_HEADER_LEN;
132         n = marshal_int32(buf, buf_len, (uint32_t)type);
133         buf += n; buf_len -= n;
134         marshal_datetime(buf, buf_len, last_update);
135         return OBJ_HEADER_LEN;
136 } /* marshal_obj_header */
138 /*
139  * public API
140  */
142 ssize_t
143 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
144                 uint32_t msg_len, const char *msg)
146         size_t len = 2 * sizeof(uint32_t) + msg_len;
147         uint32_t tmp;
149         if (buf_len < 2 * sizeof(uint32_t))
150                 return -1;
151         if (buf_len < len) /* crop message */
152                 msg_len -= (uint32_t)(len - buf_len);
154         tmp = htonl(code);
155         memcpy(buf, &tmp, sizeof(tmp));
156         tmp = htonl(msg_len);
157         memcpy(buf + sizeof(tmp), &tmp, sizeof(tmp));
159         if (msg_len)
160                 memcpy(buf + 2 * sizeof(tmp), msg, msg_len);
161         return len;
162 } /* sdb_proto_marshal */
164 ssize_t
165 sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum)
167         ssize_t len = 0, n = 0;
168         uint32_t tmp;
169         size_t i;
170         int type;
172         if (buf_len >= sizeof(tmp)) {
173                 tmp = htonl((uint32_t)datum->type);
174                 memcpy(buf, &tmp, sizeof(tmp));
175                 buf += sizeof(tmp);
176                 buf_len -= sizeof(tmp);
177         }
178         else
179                 buf_len = 0;
180         len += sizeof(tmp);
182         if (datum->type == SDB_TYPE_NULL)
183                 return len;
185         if (datum->type == SDB_TYPE_INTEGER)
186                 n = marshal_int64(buf, buf_len, datum->data.integer);
187         else if (datum->type == SDB_TYPE_DECIMAL)
188                 n = marshal_double(buf, buf_len, datum->data.decimal);
189         else if (datum->type == SDB_TYPE_STRING)
190                 n = marshal_string(buf, buf_len, datum->data.string);
191         else if (datum->type == SDB_TYPE_DATETIME)
192                 n = marshal_datetime(buf, buf_len, datum->data.datetime);
193         else if (datum->type == SDB_TYPE_BINARY)
194                 n = marshal_binary(buf, buf_len,
195                                 datum->data.binary.length, datum->data.binary.datum);
196         else if (datum->type == SDB_TYPE_REGEX)
197                 n = marshal_string(buf, buf_len, datum->data.re.raw);
199         if (n < 0)
200                 return n;
201         else if (n > 0)
202                 return len + n;
204         if (! (datum->type & SDB_TYPE_ARRAY)) {
205                 errno = EINVAL;
206                 return -1;
207         }
209         /* arrays */
210         if (buf_len >= sizeof(tmp)) {
211                 tmp = htonl((uint32_t)datum->data.array.length);
212                 memcpy(buf, &tmp, sizeof(tmp));
213                 buf += sizeof(tmp);
214                 buf_len -= sizeof(tmp);
215         }
216         else
217                 buf_len = 0;
218         len += sizeof(tmp);
220         type = datum->type & 0xff;
221         for (i = 0; i < datum->data.array.length; ++i) {
222                 if (type == SDB_TYPE_INTEGER) {
223                         int64_t *v = datum->data.array.values;
224                         n = marshal_int64(buf, buf_len, v[i]);
225                 }
226                 else if (type == SDB_TYPE_DECIMAL) {
227                         double *v = datum->data.array.values;
228                         n = marshal_double(buf, buf_len, v[i]);
229                 }
230                 else if (type == SDB_TYPE_STRING) {
231                         char **v = datum->data.array.values;
232                         n = marshal_string(buf, buf_len, v[i]);
233                 }
234                 else if (type == SDB_TYPE_DATETIME) {
235                         sdb_time_t *v = datum->data.array.values;
236                         n = marshal_datetime(buf, buf_len, v[i]);
237                 }
238                 else if (type == SDB_TYPE_BINARY) {
239                         struct {
240                                 size_t length;
241                                 unsigned char *datum;
242                         } *v = datum->data.array.values;
243                         n = marshal_binary(buf, buf_len, v[i].length, v[i].datum);
244                 }
245                 else if (type == SDB_TYPE_REGEX) {
246                         struct {
247                                 char *raw;
248                                 regex_t regex;
249                         } *v = datum->data.array.values;
250                         n = marshal_string(buf, buf_len, v[i].raw);
251                 }
252                 else {
253                         errno = EINVAL;
254                         return -1;
255                 }
257                 if (n < 0)
258                         return -1;
259                 if (buf_len >= (size_t)n) {
260                         buf += n;
261                         buf_len -= n;
262                 }
263                 else
264                         buf_len = 0;
265                 len += n;
266         }
267         return len;
268 } /* sdb_proto_marshal_data */
270 ssize_t
271 sdb_proto_marshal_host(char *buf, size_t buf_len,
272                 const sdb_proto_host_t *host)
274         size_t len;
275         ssize_t n;
277         if ((! host) || (! host->name))
278                 return -1;
280         len = OBJ_HEADER_LEN + strlen(host->name) + 1;
281         if (buf_len < len)
282                 return len;
284         n = marshal_obj_header(buf, buf_len, SDB_HOST, host->last_update);
285         buf += n; buf_len -= n;
286         marshal_string(buf, buf_len, host->name);
287         return len;
288 } /* sdb_proto_marshal_host */
290 ssize_t
291 sdb_proto_marshal_service(char *buf, size_t buf_len,
292                 const sdb_proto_service_t *svc)
294         size_t len;
295         ssize_t n;
297         if ((! svc) || (! svc->hostname) || (! svc->name))
298                 return -1;
300         len = OBJ_HEADER_LEN + strlen(svc->hostname) + strlen(svc->name) + 2;
301         if (buf_len < len)
302                 return len;
304         n = marshal_obj_header(buf, buf_len, SDB_SERVICE, svc->last_update);
305         buf += n; buf_len -= n;
306         n = marshal_string(buf, buf_len, svc->hostname);
307         buf += n; buf_len -= n;
308         marshal_string(buf, buf_len, svc->name);
309         return len;
310 } /* sdb_proto_marshal_service */
312 ssize_t
313 sdb_proto_marshal_metric(char *buf, size_t buf_len,
314                 const sdb_proto_metric_t *metric)
316         size_t len;
317         ssize_t n;
319         if ((! metric) || (! metric->hostname) || (! metric->name))
320                 return -1;
322         len = OBJ_HEADER_LEN + strlen(metric->hostname) + strlen(metric->name) + 2;
323         if (metric->store_type && metric->store_id)
324                 len += strlen(metric->store_type) + strlen(metric->store_id) + 2;
325         if (buf_len < len)
326                 return len;
328         n = marshal_obj_header(buf, buf_len, SDB_METRIC, metric->last_update);
329         buf += n; buf_len -= n;
330         n = marshal_string(buf, buf_len, metric->hostname);
331         buf += n; buf_len -= n;
332         n = marshal_string(buf, buf_len, metric->name);
333         buf += n; buf_len -= n;
334         if (metric->store_type && metric->store_id) {
335                 n = marshal_string(buf, buf_len, metric->store_type);
336                 buf += n; buf_len -= n;
337                 marshal_string(buf, buf_len, metric->store_id);
338         }
339         return len;
340 } /* sdb_proto_marshal_metric */
342 ssize_t
343 sdb_proto_marshal_attribute(char *buf, size_t buf_len,
344                 const sdb_proto_attribute_t *attr)
346         size_t len;
347         ssize_t n;
349         if ((! attr) || (! attr->parent) || (! attr->key) || (! attr->value)
350                         || ((attr->parent_type != SDB_HOST) && (! attr->hostname))
351                         || ((attr->parent_type != SDB_HOST)
352                                 && (attr->parent_type != SDB_SERVICE)
353                                 && (attr->parent_type != SDB_METRIC)))
354                 return -1;
356         n = sdb_proto_marshal_data(NULL, 0, attr->value);
357         if (n < 0)
358                 return -1;
360         len = OBJ_HEADER_LEN
361                 + strlen(attr->parent) + strlen(attr->key) + 2 + (size_t)n;
362         if (attr->parent_type != SDB_HOST)
363                 len += strlen(attr->hostname) + 1;
364         if (buf_len < len)
365                 return len;
367         n = marshal_obj_header(buf, buf_len,
368                         attr->parent_type | SDB_ATTRIBUTE, attr->last_update);
369         buf += n; buf_len -= n;
370         if (attr->parent_type != SDB_HOST) {
371                 n = marshal_string(buf, buf_len, attr->hostname);
372                 buf += n; buf_len -= n;
373         }
374         n = marshal_string(buf, buf_len, attr->parent);
375         buf += n; buf_len -= n;
376         n = marshal_string(buf, buf_len, attr->key);
377         buf += n; buf_len -= n;
378         sdb_proto_marshal_data(buf, buf_len, attr->value);
379         return len;
380 } /* sdb_proto_marshal_attribute */
382 ssize_t
383 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
384                 uint32_t *code, uint32_t *msg_len)
386         uint32_t tmp;
387         ssize_t n;
389         if (buf_len < 2 * sizeof(uint32_t))
390                 return -1;
392         n = sdb_proto_unmarshal_int32(buf, buf_len, &tmp);
393         if (code)
394                 *code = tmp;
395         buf += n; buf_len -= n;
396         sdb_proto_unmarshal_int32(buf, buf_len, &tmp);
397         if (msg_len)
398                 *msg_len = tmp;
399         return 2 * sizeof(uint32_t);
400 } /* sdb_proto_unmarshal_header */
402 ssize_t
403 sdb_proto_unmarshal_int32(const char *buf, size_t buf_len, uint32_t *v)
405         uint32_t n;
407         /* not enough data to read */
408         if (buf_len < sizeof(n))
409                 return -1;
411         memcpy(&n, buf, sizeof(n));
412         if (v)
413                 *v = ntohl(n);
414         return sizeof(n);
415 } /* sdb_proto_unmarshal_int32 */
417 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */