Code

utils/proto: Replaced sdb_proto_send_msg with sdb_proto_marshal.
authorSebastian Harl <sh@tokkee.org>
Sun, 14 Dec 2014 15:30:20 +0000 (16:30 +0100)
committerSebastian Harl <sh@tokkee.org>
Sun, 14 Dec 2014 15:30:20 +0000 (16:30 +0100)
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.

src/client/sock.c
src/frontend/connection.c
src/include/utils/proto.h
src/utils/proto.c

index 533e9e31b62c16457f36ae1e844a450251c26c51..eaa10ffdb95307a31c0233d10d46b68f33d7f912 100644 (file)
@@ -248,10 +248,14 @@ ssize_t
 sdb_client_send(sdb_client_t *client,
                uint32_t cmd, uint32_t msg_len, const char *msg)
 {
 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 ((! 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
 } /* sdb_client_send */
 
 ssize_t
index b27b3dd28bfbfd0324941d1861bc2bdd5d60a8ca..66ccfd4c2fb727009063ddbb85288d3e2caa0310 100644 (file)
@@ -504,12 +504,15 @@ ssize_t
 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
                uint32_t msg_len, const char *msg)
 {
 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;
        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];
 
        if (status < 0) {
                char errbuf[1024];
 
index f3f1a871d83e48c28e96bee92fd8a40ac4e3188a..9d4748a43ce879a662c38624ffd01622b61e7ad3 100644 (file)
@@ -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);
 
 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
 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
                uint32_t msg_len, const char *msg);
 
 uint32_t
index 26c4826a7f949a003d67932bd283cc59eff2befd..3315e0c7878056393df7a581b558f4a70df8e417 100644 (file)
@@ -125,24 +125,26 @@ sdb_proto_send(int fd, size_t msg_len, const char *msg)
 } /* sdb_proto_send */
 
 ssize_t
 } /* 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;
                uint32_t msg_len, const char *msg)
 {
        size_t len = 2 * sizeof(uint32_t) + msg_len;
-       char buffer[len];
-
        uint32_t tmp;
 
        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);
        tmp = htonl(code);
-       memcpy(buffer, &tmp, sizeof(tmp));
+       memcpy(buf, &tmp, sizeof(tmp));
        tmp = htonl(msg_len);
        tmp = htonl(msg_len);
-       memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
+       memcpy(buf + sizeof(tmp), &tmp, sizeof(tmp));
 
        if (msg_len)
 
        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)
 
 uint32_t
 sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset)