ubus: increase message size limit and make it configurable at build-time
[project/ubus.git] / libubus-io.c
index 030d382..48bb72d 100644 (file)
@@ -212,7 +212,7 @@ static int recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
 
 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
 {
-       struct blob_attr *data = ubus_msghdr_data(hdr);
+       struct blob_attr *data = (struct blob_attr *) (hdr + 1);
 
        if (hdr->version != 0)
                return false;
@@ -228,11 +228,15 @@ static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
 
 static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
 {
-       struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr);
+       struct {
+               struct ubus_msghdr hdr;
+               struct blob_attr data;
+       } hdrbuf;
+       struct iovec iov = STATIC_IOV(hdrbuf);
+       int len;
        int r;
 
        /* receive header + start attribute */
-       iov.iov_len += sizeof(struct blob_attr);
        r = recv_retry(ctx->sock.fd, &iov, false, recv_fd);
        if (r <= 0) {
                if (r < 0)
@@ -241,11 +245,27 @@ static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
                return false;
        }
 
-       iov.iov_len = blob_len(ubus_msghdr_data(&ctx->msgbuf.hdr));
+       if (!ubus_validate_hdr(&hdrbuf.hdr))
+               return false;
+
+       len = blob_raw_len(&hdrbuf.data);
+       if (len > ctx->msgbuf_data_len) {
+               ctx->msgbuf.data = realloc(ctx->msgbuf.data, len * sizeof(char));
+               if (ctx->msgbuf.data)
+                       ctx->msgbuf_data_len = len;
+       }
+       if (!ctx->msgbuf.data)
+               return false;
+
+       memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr));
+       memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data));
+
+       iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data);
+       iov.iov_len = blob_len(ctx->msgbuf.data);
        if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true, NULL))
                return false;
 
-       return ubus_validate_hdr(&ctx->msgbuf.hdr);
+       return true;
 }
 
 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
@@ -264,6 +284,17 @@ void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
                ctx->connection_lost(ctx);
 }
 
+void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
+{
+       struct pollfd pfd = {
+               .fd = ctx->sock.fd,
+               .events = POLLIN | POLLERR,
+       };
+
+       poll(&pfd, 1, timeout);
+       ubus_handle_data(&ctx->sock, ULOOP_READ);
+}
+
 static void
 ubus_refresh_state(struct ubus_context *ctx)
 {