Code

proto: Let unmarshal functions return the number of bytes processed.
[sysdb.git] / src / utils / proto.c
index 5a7a2e31fe0ae83c96b6654e138fd578de0a6697..9cdc21603283e6a8e6a33e5dcc2b59a800429a3f 100644 (file)
@@ -162,7 +162,7 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
 } /* sdb_proto_marshal */
 
 ssize_t
-sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum)
+sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum)
 {
        ssize_t len = 0, n = 0;
        uint32_t tmp;
@@ -339,36 +339,79 @@ sdb_proto_marshal_metric(char *buf, size_t buf_len,
        return len;
 } /* sdb_proto_marshal_metric */
 
-int
+ssize_t
+sdb_proto_marshal_attribute(char *buf, size_t buf_len,
+               const sdb_proto_attribute_t *attr)
+{
+       size_t len;
+       ssize_t n;
+
+       if ((! attr) || (! attr->parent) || (! attr->key) || (! attr->value)
+                       || ((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);
+       if (n < 0)
+               return -1;
+
+       len = OBJ_HEADER_LEN
+               + strlen(attr->parent) + strlen(attr->key) + 2 + (size_t)n;
+       if (attr->parent_type != SDB_HOST)
+               len += strlen(attr->hostname) + 1;
+       if (buf_len < len)
+               return len;
+
+       n = marshal_obj_header(buf, buf_len,
+                       attr->parent_type | SDB_ATTRIBUTE, attr->last_update);
+       buf += n; buf_len -= n;
+       if (attr->parent_type != SDB_HOST) {
+               n = marshal_string(buf, buf_len, attr->hostname);
+               buf += n; buf_len -= n;
+       }
+       n = marshal_string(buf, buf_len, attr->parent);
+       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);
+       return len;
+} /* sdb_proto_marshal_attribute */
+
+ssize_t
 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
                uint32_t *code, uint32_t *msg_len)
 {
        uint32_t tmp;
+       ssize_t n;
 
        if (buf_len < 2 * sizeof(uint32_t))
                return -1;
 
-       tmp = sdb_proto_unmarshal_int32(buf, buf_len);
+       n = sdb_proto_unmarshal_int32(buf, buf_len, &tmp);
        if (code)
                *code = tmp;
-       tmp = sdb_proto_unmarshal_int32(buf + sizeof(uint32_t),
-                       buf_len - sizeof(uint32_t));
+       buf += n; buf_len -= n;
+       sdb_proto_unmarshal_int32(buf, buf_len, &tmp);
        if (msg_len)
                *msg_len = tmp;
-       return 0;
+       return 2 * sizeof(uint32_t);
 } /* sdb_proto_unmarshal_header */
 
-uint32_t
-sdb_proto_unmarshal_int32(const char *buf, size_t buf_len)
+ssize_t
+sdb_proto_unmarshal_int32(const char *buf, size_t buf_len, uint32_t *v)
 {
        uint32_t n;
 
        /* not enough data to read */
        if (buf_len < sizeof(n))
-               return UINT32_MAX;
+               return -1;
 
        memcpy(&n, buf, sizeof(n));
-       return ntohl(n);
+       if (v)
+               *v = ntohl(n);
+       return sizeof(n);
 } /* sdb_proto_unmarshal_int32 */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */