From: Sebastian Harl Date: Sun, 14 Dec 2014 15:30:20 +0000 (+0100) Subject: utils/proto: Replaced sdb_proto_send_msg with sdb_proto_marshal. X-Git-Tag: sysdb-0.7.0~111 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=9610de881cfcd9bd15e064230714ae15cf621885 utils/proto: Replaced sdb_proto_send_msg with sdb_proto_marshal. The new function won't actually send the message but write it into a provided buffer in the wire format. This allows for more flexible use of the function. --- diff --git a/src/client/sock.c b/src/client/sock.c index 533e9e3..eaa10ff 100644 --- a/src/client/sock.c +++ b/src/client/sock.c @@ -248,10 +248,14 @@ ssize_t sdb_client_send(sdb_client_t *client, uint32_t cmd, uint32_t msg_len, const char *msg) { + char buf[2 * sizeof(uint32_t) + msg_len]; + if ((! client) || (! client->fd)) return -1; + if (sdb_proto_marshal(buf, sizeof(buf), cmd, msg_len, msg) < 0) + return -1; - return sdb_proto_send_msg(client->fd, cmd, msg_len, msg); + return sdb_proto_send(client->fd, sizeof(buf), buf); } /* sdb_client_send */ ssize_t diff --git a/src/frontend/connection.c b/src/frontend/connection.c index b27b3dd..66ccfd4 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -504,12 +504,15 @@ ssize_t sdb_connection_send(sdb_conn_t *conn, uint32_t code, uint32_t msg_len, const char *msg) { + char buf[2 * sizeof(uint32_t) + msg_len]; ssize_t status; if ((! conn) || (conn->fd < 0)) return -1; + if (sdb_proto_marshal(buf, sizeof(buf), code, msg_len, msg) < 0) + return -1; - status = sdb_proto_send_msg(conn->fd, code, msg_len, msg); + status = sdb_proto_send(conn->fd, sizeof(buf), buf); if (status < 0) { char errbuf[1024]; diff --git a/src/include/utils/proto.h b/src/include/utils/proto.h index f3f1a87..9d4748a 100644 --- a/src/include/utils/proto.h +++ b/src/include/utils/proto.h @@ -59,8 +59,19 @@ sdb_proto_select(int fd, int type); ssize_t sdb_proto_send(int fd, size_t msg_len, const char *msg); +/* + * sdb_proto_marshal: + * Encode the message into the wire format by adding an appropriate header. + * The encoded message is written to buf which has to be large enough to store + * the header (64 bits) and the entire message. + * + * Returns: + * - the number of bytes of the full encoded message on success (even if less + * than that fit into and was written to the buffer) + * - a negative value on error + */ ssize_t -sdb_proto_send_msg(int fd, uint32_t code, +sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code, uint32_t msg_len, const char *msg); uint32_t diff --git a/src/utils/proto.c b/src/utils/proto.c index 26c4826..3315e0c 100644 --- a/src/utils/proto.c +++ b/src/utils/proto.c @@ -125,24 +125,26 @@ sdb_proto_send(int fd, size_t msg_len, const char *msg) } /* sdb_proto_send */ ssize_t -sdb_proto_send_msg(int fd, uint32_t code, +sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code, uint32_t msg_len, const char *msg) { size_t len = 2 * sizeof(uint32_t) + msg_len; - char buffer[len]; - uint32_t tmp; + if (buf_len < 2 * sizeof(uint32_t)) + return -1; + if (buf_len < len) /* crop message */ + msg_len -= (uint32_t)(len - buf_len); + tmp = htonl(code); - memcpy(buffer, &tmp, sizeof(tmp)); + memcpy(buf, &tmp, sizeof(tmp)); tmp = htonl(msg_len); - memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp)); + memcpy(buf + sizeof(tmp), &tmp, sizeof(tmp)); if (msg_len) - memcpy(buffer + 2 * sizeof(tmp), msg, msg_len); - - return sdb_proto_send(fd, len, buffer); -} /* sdb_proto_send_msg */ + memcpy(buf + 2 * sizeof(tmp), msg, msg_len); + return len; +} /* sdb_proto_marshal */ uint32_t sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset)