Code

utils/proto: Let all unmarshal functions accept strings instead of strbufs.
authorSebastian Harl <sh@tokkee.org>
Wed, 17 Dec 2014 21:59:57 +0000 (22:59 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 18 Dec 2014 07:23:53 +0000 (08:23 +0100)
This is more consistent and flexible.

src/client/sock.c
src/frontend/connection.c
src/frontend/query.c
src/include/utils/proto.h
src/tools/sysdb/command.c
src/utils/proto.c

index a99abf10be889510541d57c25c74743842d0cfc7..6f231bd96a3e6823a405a7468a3cce6901465af4 100644 (file)
@@ -37,6 +37,7 @@
 
 #include <arpa/inet.h>
 
 
 #include <arpa/inet.h>
 
+#include <assert.h>
 #include <errno.h>
 #include <limits.h>
 
 #include <errno.h>
 #include <limits.h>
 
@@ -303,9 +304,14 @@ sdb_client_recv(sdb_client_t *client,
                        continue;
 
                if (rstatus == UINT32_MAX) {
                        continue;
 
                if (rstatus == UINT32_MAX) {
+                       const char *str = sdb_strbuf_string(buf) + data_offset;
+                       size_t len = sdb_strbuf_len(buf) - data_offset;
+
                        /* retrieve status and data len */
                        /* retrieve status and data len */
-                       rstatus = sdb_proto_unmarshal_int(buf, data_offset);
-                       rlen = sdb_proto_unmarshal_int(buf, data_offset + sizeof(rstatus));
+                       assert(len >= 2 * sizeof(uint32_t));
+                       rstatus = sdb_proto_unmarshal_int(str, len);
+                       rlen = sdb_proto_unmarshal_int(str + sizeof(rstatus),
+                                       len - sizeof(rstatus));
 
                        if (! rlen)
                                break;
 
                        if (! rlen)
                                break;
index 55843fd049d9e2569c24b8d42c6d20776039929d..14e53363b7589dd2d19ccbc8a566a740138ba0da 100644 (file)
@@ -356,7 +356,8 @@ command_init(sdb_conn_t *conn)
        /* reset */
        sdb_strbuf_clear(conn->errbuf);
 
        /* reset */
        sdb_strbuf_clear(conn->errbuf);
 
-       if (sdb_proto_unmarshal_header(conn->buf, &conn->cmd, &conn->cmd_len))
+       if (sdb_proto_unmarshal_header(SDB_STRBUF_STR(conn->buf),
+                               &conn->cmd, &conn->cmd_len))
                return -1;
        sdb_strbuf_skip(conn->buf, 0, 2 * sizeof(uint32_t));
 
                return -1;
        sdb_strbuf_skip(conn->buf, 0, 2 * sizeof(uint32_t));
 
index 4580e57e9714753682b55bb0bebd2de96f231647..1c5b1f293b76b83bff5f39dfde1b5baf0edcb5bd 100644 (file)
@@ -132,7 +132,7 @@ sdb_fe_fetch(sdb_conn_t *conn)
                return -1;
        }
 
                return -1;
        }
 
-       type = sdb_proto_unmarshal_int(conn->buf, 0);
+       type = sdb_proto_unmarshal_int(SDB_STRBUF_STR(conn->buf));
        strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
                        conn->cmd_len - sizeof(uint32_t));
        name[sizeof(name) - 1] = '\0';
        strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
                        conn->cmd_len - sizeof(uint32_t));
        name[sizeof(name) - 1] = '\0';
@@ -149,7 +149,7 @@ sdb_fe_list(sdb_conn_t *conn)
                return -1;
 
        if (conn->cmd_len == sizeof(uint32_t))
                return -1;
 
        if (conn->cmd_len == sizeof(uint32_t))
-               type = sdb_proto_unmarshal_int(conn->buf, 0);
+               type = sdb_proto_unmarshal_int(SDB_STRBUF_STR(conn->buf));
        else if (conn->cmd_len) {
                sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
                                "LIST command", conn->cmd_len);
        else if (conn->cmd_len) {
                sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
                                "LIST command", conn->cmd_len);
@@ -188,7 +188,7 @@ sdb_fe_lookup(sdb_conn_t *conn)
                                conn->cmd_len);
                return -1;
        }
                                conn->cmd_len);
                return -1;
        }
-       type = sdb_proto_unmarshal_int(conn->buf, 0);
+       type = sdb_proto_unmarshal_int(SDB_STRBUF_STR(conn->buf));
 
        matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
        matcher_len = conn->cmd_len - sizeof(uint32_t);
 
        matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
        matcher_len = conn->cmd_len - sizeof(uint32_t);
index 311162bb0682c09aa8acc11908e8b8bf2f7df4f1..a00101027cf9634aa875e53edf5f1b553477373d 100644 (file)
@@ -54,22 +54,22 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
 
 /*
  * sdb_proto_unmarshal_header:
 
 /*
  * sdb_proto_unmarshal_header:
- * Read and decode a message header from the specified string buffer.
+ * Read and decode a message header from the specified string.
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sdb_proto_unmarshal_header(sdb_strbuf_t *buf,
+sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
                uint32_t *code, uint32_t *msg_len);
 
 /*
  * sdb_proto_unmarshal_int:
                uint32_t *code, uint32_t *msg_len);
 
 /*
  * sdb_proto_unmarshal_int:
- * Read and decode an integer from the specified string buffer.
+ * Read and decode an integer from the specified string.
  */
 uint32_t
  */
 uint32_t
-sdb_proto_unmarshal_int(sdb_strbuf_t *buf, size_t offset);
+sdb_proto_unmarshal_int(const char *buf, size_t buf_len);
 
 #ifdef __cplusplus
 } /* extern "C" */
 
 #ifdef __cplusplus
 } /* extern "C" */
index e859bc795db43fc49d64c852fdd8f4f408c6e6e2..78deb717520be48a95d37808b8b3830e32ec73db 100644 (file)
@@ -49,7 +49,7 @@
 static void
 log_printer(sdb_strbuf_t *buf)
 {
 static void
 log_printer(sdb_strbuf_t *buf)
 {
-       uint32_t prio = sdb_proto_unmarshal_int(buf, 0);
+       uint32_t prio = sdb_proto_unmarshal_int(SDB_STRBUF_STR(buf));
 
        if (prio == UINT32_MAX) {
                sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid "
 
        if (prio == UINT32_MAX) {
                sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid "
index dc6ee715e721ea3ea5f8e60ffec85c8de484acbb..b5cfe04700c52793388f4d3d62bc40dec1674b83 100644 (file)
@@ -65,39 +65,34 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
 } /* sdb_proto_marshal */
 
 int
 } /* sdb_proto_marshal */
 
 int
-sdb_proto_unmarshal_header(sdb_strbuf_t *buf,
+sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
                uint32_t *code, uint32_t *msg_len)
 {
        uint32_t tmp;
 
                uint32_t *code, uint32_t *msg_len)
 {
        uint32_t tmp;
 
-       if (sdb_strbuf_len(buf) < 2 * sizeof(uint32_t))
+       if (buf_len < 2 * sizeof(uint32_t))
                return -1;
 
                return -1;
 
-       tmp = sdb_proto_unmarshal_int(buf, 0);
+       tmp = sdb_proto_unmarshal_int(buf, buf_len);
        if (code)
                *code = tmp;
        if (code)
                *code = tmp;
-       tmp = sdb_proto_unmarshal_int(buf, sizeof(uint32_t));
+       tmp = sdb_proto_unmarshal_int(buf + sizeof(uint32_t),
+                       buf_len - sizeof(uint32_t));
        if (msg_len)
                *msg_len = tmp;
        return 0;
 } /* sdb_proto_unmarshal_header */
 
 uint32_t
        if (msg_len)
                *msg_len = tmp;
        return 0;
 } /* sdb_proto_unmarshal_header */
 
 uint32_t
-sdb_proto_unmarshal_int(sdb_strbuf_t *buf, size_t offset)
+sdb_proto_unmarshal_int(const char *buf, size_t buf_len)
 {
 {
-       const char *data;
        uint32_t n;
 
        uint32_t n;
 
-       if (! buf)
-               return UINT32_MAX;
-
        /* not enough data to read */
        /* not enough data to read */
-       if (offset + sizeof(uint32_t) > sdb_strbuf_len(buf))
+       if (buf_len < sizeof(n))
                return UINT32_MAX;
 
                return UINT32_MAX;
 
-       data = sdb_strbuf_string(buf);
-       data += offset;
-       memcpy(&n, data, sizeof(n));
+       memcpy(&n, buf, sizeof(n));
        return ntohl(n);
 } /* sdb_proto_unmarshal_int */
 
        return ntohl(n);
 } /* sdb_proto_unmarshal_int */