Code

utils/proto: Support boolean values.
[sysdb.git] / src / utils / proto.c
index bf0d9dd0e8ba629cf01ac5aa1f4c26335063fc8a..f8265d6861da71335da53a88d3c932b713c718e6 100644 (file)
@@ -66,19 +66,43 @@ endian_swap64(uint64_t v)
                | (((v & 0x00000000000000ffLL) << 56) & 0xff00000000000000LL);
 } /* endian_swap64 */
 
+static unsigned char *
+memdup(const unsigned char *d, size_t length)
+{
+       unsigned char *v = malloc(length);
+       if (! v)
+               return NULL;
+       memcpy(v, d, length);
+       return v;
+} /* memdup */
+
 /* In case there's not enough buffer space, the marshal functions have to
  * return the number of bytes that would have been written if enough space had
  * been available. */
 
 static ssize_t
-marshal_int32(char *buf, size_t buf_len, uint32_t v)
+marshal_bool(char *buf, size_t buf_len, bool v)
 {
-       if (buf_len >= sizeof(v)) {
-               v = htonl(v);
-               memcpy(buf, &v, sizeof(v));
+       uint8_t t;
+       if (buf_len >= sizeof(t)) {
+               t = v ? 1 : 0;
+               memcpy(buf, &t, sizeof(t));
        }
-       return sizeof(v);
-} /* marshal_int32 */
+       return sizeof(t);
+} /* marshal_bool */
+
+static ssize_t
+unmarshal_bool(const char *buf, size_t len, bool *v)
+{
+       uint8_t t;
+       if (len < sizeof(t))
+               return -1;
+       if (v) {
+               memcpy(&t, buf, sizeof(t));
+               *v = t != 0;
+       }
+       return sizeof(t);
+} /* unmarshal_bool */
 
 static ssize_t
 marshal_int64(char *buf, size_t buf_len, int64_t v)
@@ -93,9 +117,9 @@ marshal_int64(char *buf, size_t buf_len, int64_t v)
 } /* marshal_int64 */
 
 static ssize_t
-unmarshal_int64(const char *buf, size_t buf_len, int64_t *v)
+unmarshal_int64(const char *buf, size_t len, int64_t *v)
 {
-       if (buf_len < sizeof(*v))
+       if (len < sizeof(*v))
                return -1;
        if (v) {
                memcpy(v, buf, sizeof(*v));
@@ -163,7 +187,8 @@ marshal_binary(char *buf, size_t buf_len, size_t len, const unsigned char *v)
 } /* marshal_binary */
 
 static ssize_t
-unmarshal_binary(const char *buf, size_t len, size_t *v_len, unsigned char **v)
+unmarshal_binary(const char *buf, size_t len,
+               size_t *v_len, const unsigned char **v)
 {
        uint32_t l;
        ssize_t n;
@@ -176,12 +201,8 @@ unmarshal_binary(const char *buf, size_t len, size_t *v_len, unsigned char **v)
 
        if (v_len)
                *v_len = (size_t)l;
-       if (v && (l > 0)) {
-               *v = malloc((size_t)l);
-               if (! *v)
-                       return -1;
-               memcpy(*v, buf, (size_t)l);
-       }
+       if (v && (l > 0))
+               *v = (const unsigned char *)buf;
        else if (v)
                *v = NULL;
        return sizeof(l) + (ssize_t)l;
@@ -198,21 +219,17 @@ marshal_string(char *buf, size_t buf_len, const char *v)
 } /* marshal_string */
 
 static ssize_t
-unmarshal_string(const char *buf, size_t len, char **v)
+unmarshal_string(const char *buf, size_t len, const char **v)
 {
        size_t l = 0;
 
        for (l = 0; l < len; ++l)
                if (buf[l] == '\0')
                        break;
-       if ((! len) || (buf[l] != '\0'))
+       if (l == len)
                return -1;
-       if (v) {
-               *v = malloc(l + 1);
-               if (! *v)
-                       return -1;
-               memcpy(*v, buf, l + 1);
-       }
+       if (v)
+               *v = buf;
        return l + 1;
 } /* unmarshal_string */
 
@@ -226,12 +243,28 @@ marshal_obj_header(char *buf, size_t buf_len,
        if (buf_len < OBJ_HEADER_LEN)
                return OBJ_HEADER_LEN;
 
-       n = marshal_int32(buf, buf_len, (uint32_t)type);
+       n = sdb_proto_marshal_int32(buf, buf_len, (uint32_t)type);
        buf += n; buf_len -= n;
        marshal_datetime(buf, buf_len, last_update);
        return OBJ_HEADER_LEN;
 } /* marshal_obj_header */
 
+static ssize_t
+unmarshal_obj_header(const char *buf, size_t len,
+               int *type, sdb_time_t *last_update)
+{
+       ssize_t n;
+
+       if (len < OBJ_HEADER_LEN)
+               return -1;
+
+       n = sdb_proto_unmarshal_int32(buf, len, (uint32_t *)type);
+       buf += n; len -= n;
+       if (unmarshal_datetime(buf, len, last_update) < 0)
+               return -1;
+       return OBJ_HEADER_LEN;
+} /* unmarshal_obj_header */
+
 /*
  * public API
  */
@@ -258,6 +291,16 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
        return len;
 } /* sdb_proto_marshal */
 
+ssize_t
+sdb_proto_marshal_int32(char *buf, size_t buf_len, uint32_t v)
+{
+       if (buf_len >= sizeof(v)) {
+               v = htonl(v);
+               memcpy(buf, &v, sizeof(v));
+       }
+       return sizeof(v);
+} /* sdb_proto_marshal_int32 */
+
 ssize_t
 sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum)
 {
@@ -279,7 +322,9 @@ sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum)
        if (datum->type == SDB_TYPE_NULL)
                return len;
 
-       if (datum->type == SDB_TYPE_INTEGER)
+       if (datum->type == SDB_TYPE_BOOLEAN)
+               n = marshal_bool(buf, buf_len, datum->data.boolean);
+       else if (datum->type == SDB_TYPE_INTEGER)
                n = marshal_int64(buf, buf_len, datum->data.integer);
        else if (datum->type == SDB_TYPE_DECIMAL)
                n = marshal_double(buf, buf_len, datum->data.decimal);
@@ -316,7 +361,11 @@ sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum)
 
        type = datum->type & 0xff;
        for (i = 0; i < datum->data.array.length; ++i) {
-               if (type == SDB_TYPE_INTEGER) {
+               if (type == SDB_TYPE_BOOLEAN) {
+                       bool *v = datum->data.array.values;
+                       n = marshal_bool(buf, buf_len, v[i]);
+               }
+               else if (type == SDB_TYPE_INTEGER) {
                        int64_t *v = datum->data.array.values;
                        n = marshal_int64(buf, buf_len, v[i]);
                }
@@ -443,14 +492,14 @@ sdb_proto_marshal_attribute(char *buf, size_t buf_len,
        size_t len;
        ssize_t n;
 
-       if ((! attr) || (! attr->parent) || (! attr->key) || (! attr->value)
+       if ((! attr) || (! attr->parent) || (! attr->key)
                        || ((attr->parent_type != SDB_HOST) && (! attr->hostname))
                        || ((attr->parent_type != SDB_HOST)
                                && (attr->parent_type != SDB_SERVICE)
                                && (attr->parent_type != SDB_METRIC)))
                return -1;
 
-       n = sdb_proto_marshal_data(NULL, 0, attr->value);
+       n = sdb_proto_marshal_data(NULL, 0, &attr->value);
        if (n < 0)
                return -1;
 
@@ -472,7 +521,7 @@ sdb_proto_marshal_attribute(char *buf, size_t buf_len,
        buf += n; buf_len -= n;
        n = marshal_string(buf, buf_len, attr->key);
        buf += n; buf_len -= n;
-       sdb_proto_marshal_data(buf, buf_len, attr->value);
+       sdb_proto_marshal_data(buf, buf_len, &attr->value);
        return len;
 } /* sdb_proto_marshal_attribute */
 
@@ -528,23 +577,34 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
 
 /* Don't populate 'd' if 'datum' is NULL. */
 #define D(field) (datum ? &d.data.field : NULL)
-       if (d.type == SDB_TYPE_INTEGER)
+       if (d.type == SDB_TYPE_BOOLEAN)
+               n = unmarshal_bool(buf, len, D(boolean));
+       else if (d.type == SDB_TYPE_INTEGER)
                n = unmarshal_int64(buf, len, D(integer));
        else if (d.type == SDB_TYPE_DECIMAL)
                n = unmarshal_double(buf, len, D(decimal));
-       else if (d.type == SDB_TYPE_STRING)
-               n = unmarshal_string(buf, len, D(string));
+       else if (d.type == SDB_TYPE_STRING) {
+               const char *str = NULL;
+               n = unmarshal_string(buf, len, &str);
+               if ((n > 0) && datum)
+                       if (! (d.data.string = strdup(str)))
+                               n = -1;
+       }
        else if (d.type == SDB_TYPE_DATETIME)
                n = unmarshal_datetime(buf, len, D(datetime));
-       else if (d.type == SDB_TYPE_BINARY)
-               n = unmarshal_binary(buf, len, D(binary.length), D(binary.datum));
+       else if (d.type == SDB_TYPE_BINARY) {
+               const unsigned char *data = NULL;
+               n = unmarshal_binary(buf, len, D(binary.length), &data);
+               if ((n > 0) && datum)
+                       if (! (d.data.binary.datum = memdup(data, d.data.binary.length)))
+                               n = -1;
+       }
        else if (d.type == SDB_TYPE_REGEX) {
                if (datum) {
-                       char *str = NULL;
+                       const char *str = NULL;
                        n = unmarshal_string(buf, len, &str);
                        if (sdb_data_parse(str, SDB_TYPE_REGEX, &d))
                                n = -1;
-                       free(str);
                }
                else
                        n = unmarshal_string(buf, len, NULL);
@@ -577,7 +637,11 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
                d.data.array.values = calloc(d.data.array.length,
                                sdb_data_sizeof(d.type & 0xff));
        for (i = 0; i < d.data.array.length; ++i) {
-               if ((d.type & 0xff) == SDB_TYPE_INTEGER) {
+               if ((d.type & 0xff) == SDB_TYPE_BOOLEAN) {
+                       bool *v = d.data.array.values;
+                       n = unmarshal_bool(buf, len, V());
+               }
+               else if ((d.type & 0xff) == SDB_TYPE_INTEGER) {
                        int64_t *v = d.data.array.values;
                        n = unmarshal_int64(buf, len, V());
                }
@@ -587,7 +651,11 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
                }
                else if ((d.type & 0xff) == SDB_TYPE_STRING) {
                        char **v = d.data.array.values;
-                       n = unmarshal_string(buf, len, V());
+                       const char *str = NULL;
+                       n = unmarshal_string(buf, len, &str);
+                       if ((n > 0) && datum)
+                               if (! (v[i] = strdup(str)))
+                                       n = -1;
                }
                else if ((d.type & 0xff) == SDB_TYPE_DATETIME) {
                        sdb_time_t *v = d.data.array.values;
@@ -598,7 +666,12 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
                                size_t length;
                                unsigned char *datum;
                        } *v = d.data.array.values;
-                       n = unmarshal_binary(buf, len, V(.length), V(.datum));
+                       const unsigned char *data = NULL;
+
+                       n = unmarshal_binary(buf, len, V(.length), &data);
+                       if ((n > 0) && datum)
+                               if (! (v[i].datum = memdup(data, v[i].length)))
+                                       n = -1;
                }
                else if ((d.type & 0xff) == SDB_TYPE_REGEX) {
                        struct {
@@ -607,7 +680,7 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
                        } *v = d.data.array.values;
                        if (datum) {
                                sdb_data_t t = SDB_DATA_INIT;
-                               char *str = NULL;
+                               const char *str = NULL;
                                n = unmarshal_string(buf, len, &str);
                                if (! sdb_data_parse(str, SDB_TYPE_REGEX, &t)) {
                                        v[i].raw = t.data.re.raw;
@@ -615,7 +688,6 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
                                }
                                else
                                        n = -1;
-                               free(str);
                        }
                        else
                                n = unmarshal_string(buf, len, NULL);
@@ -632,13 +704,7 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
                                sdb_data_free_datum(&d);
                        return -1;
                }
-               if (len >= (size_t)n) {
-                       buf += n;
-                       len -= n;
-                       l += n;
-               }
-               else
-                       return -1;
+               buf += n; len -= n; l += n;
        }
 #undef V
 
@@ -647,5 +713,120 @@ sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum)
        return l;
 } /* sdb_proto_unmarshal_data */
 
+ssize_t
+sdb_proto_unmarshal_host(const char *buf, size_t len,
+               sdb_proto_host_t *host)
+{
+       int type = 0;
+       ssize_t l = 0, n;
+
+       if ((n = unmarshal_obj_header(buf, len,
+                                       &type, host ? &host->last_update : NULL)) < 0)
+               return n;
+       if (type != SDB_HOST)
+               return -1;
+       buf += n; len -= n; l += n;
+       if ((n = unmarshal_string(buf, len, host ? &host->name : NULL)) < 0)
+               return n;
+       return l + n;
+} /* sdb_proto_unmarshal_host */
+
+ssize_t
+sdb_proto_unmarshal_service(const char *buf, size_t len,
+               sdb_proto_service_t *svc)
+{
+       int type = 0;
+       ssize_t l = 0, n;
+
+       if ((n = unmarshal_obj_header(buf, len,
+                                       &type, svc ? &svc->last_update : NULL)) < 0)
+               return n;
+       if (type != SDB_SERVICE)
+               return -1;
+       buf += n; len -= n; l += n;
+       if ((n = unmarshal_string(buf, len, svc ? &svc->hostname : NULL)) < 0)
+               return n;
+       buf += n; len -= n; l += n;
+       if ((n = unmarshal_string(buf, len, svc ? &svc->name : NULL)) < 0)
+               return n;
+       return l + n;
+} /* sdb_proto_unmarshal_service */
+
+ssize_t
+sdb_proto_unmarshal_metric(const char *buf, size_t len,
+               sdb_proto_metric_t *metric)
+{
+       int type = 0;
+       ssize_t l = 0, n;
+
+       if ((n = unmarshal_obj_header(buf, len,
+                                       &type, metric ? &metric->last_update : NULL)) < 0)
+               return n;
+       if (type != SDB_METRIC)
+               return -1;
+       buf += n; len -= n; l += n;
+       if ((n = unmarshal_string(buf, len,
+                                       metric ? &metric->hostname : NULL)) < 0)
+               return n;
+       buf += n; len -= n; l += n;
+       if ((n = unmarshal_string(buf, len, metric ? &metric->name : NULL)) < 0)
+               return n;
+       if (len > (size_t)n) {
+               buf += n; len -= n; l += n;
+               if ((n = unmarshal_string(buf, len,
+                                               metric ? &metric->store_type : NULL)) < 0)
+                       return n;
+               buf += n; len -= n; l += n;
+               if ((n = unmarshal_string(buf, len,
+                                               metric ? &metric->store_id : NULL)) < 0)
+                       return n;
+       }
+       else if (metric) {
+               metric->store_type = NULL;
+               metric->store_id = NULL;
+       }
+       return l + n;
+} /* sdb_proto_unmarshal_metric */
+
+ssize_t
+sdb_proto_unmarshal_attribute(const char *buf, size_t len,
+               sdb_proto_attribute_t *attr)
+{
+       int parent_type, type = 0;
+       ssize_t l = 0, n;
+
+       if ((n = unmarshal_obj_header(buf, len,
+                                       &type, attr ? &attr->last_update : NULL)) < 0)
+               return n;
+       buf += n; len -= n; l += n;
+       parent_type = type & 0xf;
+       if (type != (parent_type | SDB_ATTRIBUTE))
+               return -1;
+       if ((parent_type != SDB_HOST) && (parent_type != SDB_SERVICE)
+                               && (parent_type != SDB_METRIC))
+               return -1;
+       if (attr)
+               attr->parent_type = parent_type;
+
+       if (parent_type != SDB_HOST) {
+               if ((n = unmarshal_string(buf, len,
+                                               attr ? &attr->hostname : NULL)) < 0)
+                       return n;
+               buf += n; len -= n; l += n;
+       }
+       else if (attr)
+               attr->hostname = NULL;
+       if ((n = unmarshal_string(buf, len, attr ? &attr->parent : NULL)) < 0)
+               return n;
+       buf += n; len -= n; l += n;
+       if ((n = unmarshal_string(buf, len, attr ? &attr->key : NULL)) < 0)
+               return n;
+       buf += n; len -= n; l += n;
+       if ((n = sdb_proto_unmarshal_data(buf, len,
+                                       attr ? &attr->value : NULL)) < 0)
+               return n;
+       return l + n;
+} /* sdb_proto_unmarshal_attribute */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */