fix max message length handling - exclude the header
[project/ubus.git] / libubus.c
index 223a6bb..93f8459 100644 (file)
--- a/libubus.c
+++ b/libubus.c
@@ -124,6 +124,8 @@ static bool recv_retry(int fd, struct iovec *iov, bool wait)
                bytes = read(fd, iov->iov_base, iov->iov_len);
                if (bytes < 0) {
                        bytes = 0;
+                       if (uloop_cancelled)
+                               return false;
                        if (errno == EINTR)
                                continue;
 
@@ -151,7 +153,7 @@ static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
        if (blob_raw_len(hdr->data) < sizeof(*hdr->data))
                return false;
 
-       if (blob_raw_len(hdr->data) + sizeof(*hdr) > UBUS_MAX_MSGLEN)
+       if (blob_pad_len(hdr->data) > UBUS_MAX_MSGLEN)
                return false;
 
        return true;
@@ -180,7 +182,7 @@ static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret)
        if (!attrbuf[UBUS_ATTR_STATUS])
                return false;
 
-       *ret = blob_get_int32(attrbuf[UBUS_ATTR_STATUS]);
+       *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
        return true;
 }
 
@@ -293,7 +295,7 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd
        if (!attrbuf[UBUS_ATTR_OBJID])
                return;
 
-       objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+       objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
 
        if (!attrbuf[UBUS_ATTR_METHOD]) {
                ret = UBUS_STATUS_INVALID_ARGUMENT;
@@ -307,7 +309,8 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd
        }
 
        for (method = 0; method < obj->n_methods; method++)
-               if (!strcmp(obj->methods[method].name,
+               if (!obj->methods[method].name ||
+                   !strcmp(obj->methods[method].name,
                            blob_data(attrbuf[UBUS_ATTR_METHOD])))
                        goto found;
 
@@ -320,7 +323,7 @@ found:
        req.peer = hdr->peer;
        req.seq = hdr->seq;
        ret = obj->methods[method].handler(ctx, obj, &req,
-                                          obj->methods[method].name,
+                                          blob_data(attrbuf[UBUS_ATTR_METHOD]),
                                           attrbuf[UBUS_ATTR_DATA]);
 
 send:
@@ -443,9 +446,9 @@ static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr
                return;
 
        memset(&obj, 0, sizeof(obj));
-       obj.id = blob_get_int32(attr[UBUS_ATTR_OBJID]);
+       obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
        obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
-       obj.type_id = blob_get_int32(attr[UBUS_ATTR_OBJTYPE]);
+       obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
        obj.signature = attr[UBUS_ATTR_SIGNATURE];
        req->cb(ureq->ctx, &obj, ureq->priv);
 }
@@ -475,7 +478,7 @@ static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_at
        if (!attr[UBUS_ATTR_OBJID])
                return;
 
-       *id = blob_get_int32(attr[UBUS_ATTR_OBJID]);
+       *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
 }
 
 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
@@ -530,7 +533,7 @@ int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
        return ubus_complete_request(ctx, &req);
 }
 
-static void ubus_publish_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
 {
        struct ubus_object *obj = req->priv;
 
@@ -539,10 +542,10 @@ static void ubus_publish_cb(struct ubus_request *req, int type, struct blob_attr
        if (!attrbuf[UBUS_ATTR_OBJID])
                return;
 
-       obj->id = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]);
+       obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
 
        if (attrbuf[UBUS_ATTR_OBJTYPE])
-               obj->type->id = blob_get_int32(attrbuf[UBUS_ATTR_OBJTYPE]);
+               obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
 
        obj->avl.key = &obj->id;
        avl_insert(&req->ctx->objects, &obj->avl);
@@ -606,7 +609,7 @@ static bool ubus_push_object_type(struct ubus_object_type *type)
        return true;
 }
 
-static int __ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
+static int __ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
 {
        struct ubus_request req;
        int ret;
@@ -622,8 +625,8 @@ static int __ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
                        return UBUS_STATUS_INVALID_ARGUMENT;
        }
 
-       ubus_start_request(ctx, &req, b.head, UBUS_MSG_PUBLISH, 0);
-       req.raw_data_cb = ubus_publish_cb;
+       ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0);
+       req.raw_data_cb = ubus_add_object_cb;
        req.priv = obj;
        ret = ubus_complete_request(ctx, &req);
        if (ret)
@@ -635,25 +638,83 @@ static int __ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
        return 0;
 }
 
-int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
+int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
 {
        if (!obj->name || !obj->type)
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       return __ubus_publish(ctx, obj);
+       return __ubus_add_object(ctx, obj);
+}
+
+static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       struct ubus_object *obj = req->priv;
+
+       ubus_parse_msg(msg);
+
+       if (!attrbuf[UBUS_ATTR_OBJID])
+               return;
+
+       obj->id = 0;
+
+       if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
+               obj->type->id = 0;
+
+       avl_delete(&req->ctx->objects, &obj->avl);
+}
+
+int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
+{
+       struct ubus_request req;
+       int ret;
+
+       blob_buf_init(&b, 0);
+       blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
+       ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0);
+       req.raw_data_cb = ubus_remove_object_cb;
+       req.priv = obj;
+       ret = ubus_complete_request(ctx, &req);
+       if (ret)
+               return ret;
+
+       if (obj->id)
+               return UBUS_STATUS_NO_DATA;
+
+       return 0;
+}
+
+static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
+                        struct ubus_request_data *req,
+                        const char *method, struct blob_attr *msg)
+{
+       struct ubus_event_handler *ev;
+
+       ev = container_of(obj, struct ubus_event_handler, obj);
+       ev->cb(ctx, ev, method, msg);
+       return 0;
 }
 
-int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_object *obj,
+static const struct ubus_method event_method = {
+       .name = NULL,
+       .handler = ubus_event_cb,
+};
+
+int ubus_register_event_handler(struct ubus_context *ctx,
+                               struct ubus_event_handler *ev,
                                const char *pattern)
 {
+       struct ubus_object *obj = &ev->obj;
        struct blob_buf b2;
        int ret;
 
        if (!obj->id) {
+               obj->methods = &event_method;
+               obj->n_methods = 1;
+
                if (!!obj->name ^ !!obj->type)
                        return UBUS_STATUS_INVALID_ARGUMENT;
 
-               ret = __ubus_publish(ctx, obj);
+               ret = __ubus_add_object(ctx, obj);
                if (ret)
                        return ret;
        }
@@ -671,8 +732,25 @@ int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_object *ob
        return 0;
 }
 
+int ubus_send_event(struct ubus_context *ctx, const char *id,
+                   struct blob_attr *data)
+{
+       struct ubus_request req;
+       void *s;
+
+       blob_buf_init(&b, 0);
+       blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
+       blob_put_string(&b, UBUS_ATTR_METHOD, "send");
+       s = blob_nest_start(&b, UBUS_ATTR_DATA);
+       blobmsg_add_string(&b, "id", id);
+       blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
+       blob_nest_end(&b, s);
+
+       ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT);
+       return ubus_complete_request(ctx, &req);
+}
 
-void ubus_default_connection_lost(struct ubus_context *ctx)
+static void ubus_default_connection_lost(struct ubus_context *ctx)
 {
        if (ctx->sock.registered)
                uloop_end();