summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 4b1c3e7)
raw | patch | inline | side by side (parent: 4b1c3e7)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 29 Dec 2014 15:53:44 +0000 (16:53 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 29 Dec 2014 15:53:44 +0000 (16:53 +0100) |
That's a more flexible and straight-forward interface allowing callers to not
care about the size of the expected data.
care about the size of the expected data.
diff --git a/src/client/sock.c b/src/client/sock.c
index bed6fc0ff0b6d20aff605f4f0a1c4b466435b3e8..0d734f3a9462ba74bb7405beb94c9fdeecb67d8a 100644 (file)
--- a/src/client/sock.c
+++ b/src/client/sock.c
if (rstatus == UINT32_MAX) {
const char *str = sdb_strbuf_string(buf) + data_offset;
size_t len = sdb_strbuf_len(buf) - data_offset;
+ ssize_t n;
/* retrieve status and data len */
assert(len >= 2 * sizeof(uint32_t));
- rstatus = sdb_proto_unmarshal_int32(str, len);
- rlen = sdb_proto_unmarshal_int32(str + sizeof(rstatus),
- len - sizeof(rstatus));
+ n = sdb_proto_unmarshal_int32(str, len, &rstatus);
+ str += n; len -= (size_t)n;
+ sdb_proto_unmarshal_int32(str, len, &rlen);
if (! rlen)
break;
index 14e53363b7589dd2d19ccbc8a566a740138ba0da..91a9710e6b8e4789b625bcab1448343a6770e410 100644 (file)
sdb_strbuf_clear(conn->errbuf);
if (sdb_proto_unmarshal_header(SDB_STRBUF_STR(conn->buf),
- &conn->cmd, &conn->cmd_len))
+ &conn->cmd, &conn->cmd_len) < 0)
return -1;
sdb_strbuf_skip(conn->buf, 0, 2 * sizeof(uint32_t));
diff --git a/src/frontend/query.c b/src/frontend/query.c
index 933ca04cae5a887156400b27fb78c086d0fc6c33..63ec226c6dc17cea869c3187a66908f28b26117f 100644 (file)
--- a/src/frontend/query.c
+++ b/src/frontend/query.c
sdb_fe_fetch(sdb_conn_t *conn)
{
char name[conn->cmd_len + 1];
- int type;
+ uint32_t type;
if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH))
return -1;
return -1;
}
- type = sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf));
+ sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
conn->cmd_len - sizeof(uint32_t));
name[sizeof(name) - 1] = '\0';
/* TODO: support other types besides hosts */
- return sdb_fe_exec_fetch(conn, type, name, NULL, /* filter = */ NULL);
+ return sdb_fe_exec_fetch(conn, (int)type, name, NULL, /* filter = */ NULL);
} /* sdb_fe_fetch */
int
sdb_fe_list(sdb_conn_t *conn)
{
- int type = SDB_HOST;
+ uint32_t type = SDB_HOST;
if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
return -1;
if (conn->cmd_len == sizeof(uint32_t))
- type = sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf));
+ sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
else if (conn->cmd_len) {
sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
"LIST command", conn->cmd_len);
conn->cmd_len);
return -1;
}
- return sdb_fe_exec_list(conn, type, /* filter = */ NULL);
+ return sdb_fe_exec_list(conn, (int)type, /* filter = */ NULL);
} /* sdb_fe_list */
int
const char *matcher;
size_t matcher_len;
- int type;
+ uint32_t type;
int status;
conn_matcher_t m_node = {
conn->cmd_len);
return -1;
}
- type = sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf));
+ sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
matcher_len = conn->cmd_len - sizeof(uint32_t);
return -1;
}
- node.type = type;
+ node.type = (int)type;
m_node.matcher = m;
/* run analyzer separately; parse_matcher is missing
status = -1;
}
else
- status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
+ status = sdb_fe_exec_lookup(conn, (int)type, m, /* filter = */ NULL);
sdb_object_deref(SDB_OBJ(m));
return status;
} /* sdb_fe_lookup */
index fd842c1b0caf8c6f48b64b03a7c7d1c2db9f5488..a872dd51b309c6a05a659d9ea4e70a70933fdf35 100644 (file)
* Read and decode a message header from the specified string.
*
* Returns:
- * - 0 on success
+ * - the number of bytes read on success
* - a negative value else
*/
-int
+ssize_t
sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
uint32_t *code, uint32_t *msg_len);
/*
* sdb_proto_unmarshal_int32:
* Read and decode a 32-bit integer from the specified string.
+ *
+ * Returns:
+ * - the number of bytes read on success
+ * - a negative value else
*/
-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);
#ifdef __cplusplus
} /* extern "C" */
index 2706dd220e12fbe8e5bacb0af539b313d08a8a8a..c809c1f8850db46109e7f06b7afd6c99127858cd 100644 (file)
static void
log_printer(sdb_strbuf_t *buf)
{
- uint32_t prio = sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf));
+ uint32_t prio = 0;
- if (prio == UINT32_MAX) {
+ if (sdb_proto_unmarshal_int32(SDB_STRBUF_STR(buf), &prio) < 0) {
sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid "
"or missing priority");
prio = (uint32_t)SDB_LOG_ERR;
diff --git a/src/utils/proto.c b/src/utils/proto.c
index cd4e050969e6dab401742adb368ca59ec657c007..9cdc21603283e6a8e6a33e5dcc2b59a800429a3f 100644 (file)
--- a/src/utils/proto.c
+++ b/src/utils/proto.c
return len;
} /* sdb_proto_marshal_attribute */
-int
+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 : */