ubusd: fix issue caused by an implicit cast
[project/ubus.git] / ubusd.c
diff --git a/ubusd.c b/ubusd.c
index f1f8ac7..5409b7f 100644 (file)
--- a/ubusd.c
+++ b/ubusd.c
@@ -110,8 +110,15 @@ static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
        }
 
        if (offset < sizeof(ub->hdr)) {
-               iov[0].iov_base = ((char *) &ub->hdr) + offset;
-               iov[0].iov_len = sizeof(ub->hdr) - offset;
+               struct ubus_msghdr hdr;
+
+               hdr.version = ub->hdr.version;
+               hdr.type = ub->hdr.type;
+               hdr.seq = cpu_to_be16(ub->hdr.seq);
+               hdr.peer = cpu_to_be32(ub->hdr.peer);
+
+               iov[0].iov_base = ((char *) &hdr) + offset;
+               iov[0].iov_len = sizeof(hdr) - offset;
                iov[1].iov_base = (char *) ub->data;
                iov[1].iov_len = ub->len;
 
@@ -141,12 +148,13 @@ void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
 
        if (!cl->tx_queue[cl->txq_cur]) {
                written = ubus_msg_writev(cl->sock.fd, ub, 0);
-               if (written >= ub->len + sizeof(ub->hdr))
-                       goto out;
 
                if (written < 0)
                        written = 0;
 
+               if (written >= ub->len + sizeof(ub->hdr))
+                       goto out;
+
                cl->txq_ofs = written;
 
                /* get an event once we can write to the socket again */
@@ -275,6 +283,9 @@ retry:
                if (!cl->pending_msg)
                        goto disconnect;
 
+               cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
+               cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
+
                memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
                memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
        }