libubus: reduce code duplication and add stack depth protection for unsubscribe/notif...
[project/ubus.git] / libubus.c
index bcaf63d..e9809cf 100644 (file)
--- a/libubus.c
+++ b/libubus.c
@@ -214,6 +214,8 @@ void __hidden ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr
                break;
 
        case UBUS_MSG_INVOKE:
+       case UBUS_MSG_UNSUBSCRIBE:
+       case UBUS_MSG_NOTIFY:
                if (ctx->stack_depth > 2) {
                        pending = calloc(1, sizeof(*pending) +
                                blob_raw_len(hdr->data));
@@ -225,7 +227,7 @@ void __hidden ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr
                                blob_raw_len(hdr->data));
                        list_add(&pending->list, &ctx->pending);
                } else {
-                       ubus_process_invoke(ctx, hdr);
+                       ubus_process_obj_msg(ctx, hdr);
                }
                break;
        }
@@ -514,81 +516,6 @@ int ubus_register_event_handler(struct ubus_context *ctx,
                          NULL, NULL, 0);
 }
 
-enum {
-       WATCH_ID,
-       WATCH_NOTIFY,
-       __WATCH_MAX
-};
-
-static const struct blobmsg_policy watch_policy[] = {
-       [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
-       [WATCH_NOTIFY] = { .name = "notify", .type = BLOBMSG_TYPE_STRING },
-};
-
-
-static int ubus_watch_cb(struct ubus_context *ctx, struct ubus_object *obj,
-                        struct ubus_request_data *req,
-                        const char *method, struct blob_attr *msg)
-{
-       struct ubus_watch_object *w;
-       struct blob_attr *tb[__WATCH_MAX];
-
-       blobmsg_parse(watch_policy, ARRAY_SIZE(watch_policy), tb, blob_data(msg), blob_len(msg));
-
-       if (!tb[WATCH_ID] || !tb[WATCH_NOTIFY])
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       if (req->peer)
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       w = container_of(obj, struct ubus_watch_object, obj);
-       w->cb(ctx, w, blobmsg_get_u32(tb[WATCH_ID]));
-       return 0;
-}
-
-static const struct ubus_method watch_method = {
-       .name = NULL,
-       .handler = ubus_watch_cb,
-};
-
-int ubus_register_watch_object(struct ubus_context *ctx, struct ubus_watch_object *w_obj)
-{
-       struct ubus_object *obj = &w_obj->obj;
-
-       obj->methods = &watch_method;
-       obj->n_methods = 1;
-
-       return ubus_add_object(ctx, obj);
-}
-
-static int
-__ubus_watch_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, const char *method, int type)
-{
-       struct ubus_request req;
-
-       blob_buf_init(&b, 0);
-       blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
-       blob_put_int32(&b, UBUS_ATTR_TARGET, id);
-       if (method)
-               blob_put_string(&b, UBUS_ATTR_METHOD, method);
-
-       if (ubus_start_request(ctx, &req, b.head, type, 0) < 0)
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       return ubus_complete_request(ctx, &req, 0);
-
-}
-
-int ubus_watch_object_add(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id)
-{
-       return __ubus_watch_request(ctx, &obj->obj, id, "event", UBUS_MSG_ADD_WATCH);
-}
-
-int ubus_watch_object_remove(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id)
-{
-       return __ubus_watch_request(ctx, &obj->obj, id, NULL, UBUS_MSG_REMOVE_WATCH);
-}
-
 int ubus_send_event(struct ubus_context *ctx, const char *id,
                    struct blob_attr *data)
 {