add deferred reply to the ubus server example
[project/ubus.git] / ubusd_proto.c
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <arpa/inet.h>
15 #include "ubusd.h"
16
17 struct blob_buf b;
18 static struct ubus_msg_buf *retmsg;
19 static int *retmsg_data;
20 static struct avl_tree clients;
21
22 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
23
24 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
25
26 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
27         [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
28         [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
29         [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
30         [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
31         [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
32 };
33
34 static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
35 {
36         blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
37         return attrbuf;
38 }
39
40 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
41 {
42         ub->hdr.version = 0;
43         ub->hdr.type = type;
44         ub->hdr.seq = seq;
45         ub->hdr.peer = peer;
46 }
47
48 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
49 {
50         return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
51 }
52
53 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
54 {
55         struct ubus_msg_buf *new;
56
57         new = ubus_msg_from_blob(shared);
58         if (!new)
59                 return NULL;
60
61         ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
62         return new;
63 }
64
65 static bool ubusd_send_hello(struct ubus_client *cl)
66 {
67         struct ubus_msg_buf *ub;
68
69         blob_buf_init(&b, 0);
70         ub = ubus_msg_from_blob(true);
71         if (!ub)
72                 return false;
73
74         ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
75         ubus_msg_send(cl, ub, true);
76         return true;
77 }
78
79 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
80 {
81         ub->hdr.type = UBUS_MSG_DATA;
82         ubus_msg_send(cl, ub, false);
83         return 0;
84 }
85
86 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
87 {
88         struct ubus_object *obj;
89
90         if (!attr[UBUS_ATTR_OBJID])
91                 return UBUS_STATUS_INVALID_ARGUMENT;
92
93         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
94         if (!obj)
95                 return UBUS_STATUS_NOT_FOUND;
96
97         if (obj->client != cl)
98                 return UBUS_STATUS_PERMISSION_DENIED;
99
100         blob_buf_init(&b, 0);
101         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
102
103         /* check if we're removing the object type as well */
104         if (obj->type && obj->type->refcount == 1)
105                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
106
107         ubusd_free_object(obj);
108
109         ub = ubus_reply_from_blob(ub, true);
110         if (!ub)
111                 return UBUS_STATUS_NO_DATA;
112
113         ubus_msg_send(cl, ub, true);
114         return 0;
115 }
116
117 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
118 {
119         struct ubus_object *obj;
120
121         obj = ubusd_create_object(cl, attr);
122         if (!obj)
123                 return UBUS_STATUS_INVALID_ARGUMENT;
124
125         blob_buf_init(&b, 0);
126         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
127         if (attr[UBUS_ATTR_SIGNATURE])
128                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
129
130         ub = ubus_reply_from_blob(ub, true);
131         if (!ub)
132                 return UBUS_STATUS_NO_DATA;
133
134         ubus_msg_send(cl, ub, true);
135         return 0;
136 }
137
138 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
139 {
140         struct ubus_method *m;
141         void *s;
142
143         blob_buf_init(&b, 0);
144
145         if (obj->path.key)
146                 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
147         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
148         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
149
150         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
151         list_for_each_entry(m, &obj->type->methods, list)
152                 blob_put(&b, blob_id(m->data), blob_data(m->data), blob_len(m->data));
153         blob_nest_end(&b, s);
154
155         ub = ubus_reply_from_blob(ub, true);
156         if (!ub)
157                 return;
158
159         ubus_msg_send(cl, ub, true);
160 }
161
162 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
163 {
164         struct ubus_object *obj;
165         char *objpath;
166         bool found = false;
167         int len;
168
169         if (!attr[UBUS_ATTR_OBJPATH]) {
170                 avl_for_each_element(&path, obj, path)
171                         ubusd_send_obj(cl, ub, obj);
172                 return 0;
173         }
174
175         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
176         len = strlen(objpath);
177         if (objpath[len - 1] != '*') {
178                 obj = avl_find_element(&path, objpath, obj, path);
179                 if (!obj)
180                         return UBUS_STATUS_NOT_FOUND;
181
182                 ubusd_send_obj(cl, ub, obj);
183                 return 0;
184         }
185
186         objpath[--len] = 0;
187
188         obj = avl_find_ge_element(&path, objpath, obj, path);
189         if (!obj)
190                 return UBUS_STATUS_NOT_FOUND;
191
192         while (!strncmp(objpath, obj->path.key, len)) {
193                 found = true;
194                 ubusd_send_obj(cl, ub, obj);
195                 if (obj == avl_last_element(&path, obj, path))
196                         break;
197                 obj = avl_next_element(obj, path);
198         }
199
200         if (!found)
201                 return UBUS_STATUS_NOT_FOUND;
202
203         return 0;
204 }
205
206 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
207 {
208         struct ubus_msg_buf *ub_new;
209         struct ubus_object *obj = NULL;
210         struct ubus_id *id;
211         const char *method;
212
213         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
214                 return UBUS_STATUS_INVALID_ARGUMENT;
215
216         id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
217         if (!id)
218                 return UBUS_STATUS_NOT_FOUND;
219
220         obj = container_of(id, struct ubus_object, id);
221
222         method = blob_data(attr[UBUS_ATTR_METHOD]);
223
224         if (!obj->client)
225                 return obj->recv_msg(cl, method, attr[UBUS_ATTR_DATA]);
226
227         blob_buf_init(&b, 0);
228         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
229         blob_put_string(&b, UBUS_ATTR_METHOD, method);
230         if (attr[UBUS_ATTR_DATA])
231                 blob_put(&b, UBUS_ATTR_DATA, blob_data(attr[UBUS_ATTR_DATA]),
232                          blob_len(attr[UBUS_ATTR_DATA]));
233
234         ub_new = ubus_reply_from_blob(ub, true);
235         ubus_msg_free(ub);
236         ub = ub_new;
237
238         if (!ub)
239                 return UBUS_STATUS_NO_DATA;
240
241         ub->hdr.type = UBUS_MSG_INVOKE;
242         ub->hdr.peer = cl->id.id;
243         ubus_msg_send(obj->client, ub, true);
244
245         return -1;
246 }
247
248 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
249 {
250         struct ubus_id *clid;
251
252         clid = ubus_find_id(&clients, id);
253         if (!clid)
254                 return NULL;
255
256         return container_of(clid, struct ubus_client, id);
257 }
258
259 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
260 {
261         struct ubus_object *obj;
262
263         if (!attr[UBUS_ATTR_OBJID] ||
264             (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
265             (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
266                 goto error;
267
268         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
269         if (!obj)
270                 goto error;
271
272         if (cl != obj->client)
273                 goto error;
274
275         cl = ubusd_get_client_by_id(ub->hdr.peer);
276         if (!cl)
277                 goto error;
278
279         ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
280         ubus_msg_send(cl, ub, true);
281         return -1;
282
283 error:
284         ubus_msg_free(ub);
285         return -1;
286 }
287
288 static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
289 {
290         struct ubus_object *obj, *target;
291
292         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET] ||
293             !attr[UBUS_ATTR_METHOD]) {
294                 return UBUS_STATUS_INVALID_ARGUMENT;
295         }
296
297         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
298         if (!obj)
299                 return UBUS_STATUS_NOT_FOUND;
300
301         if (cl != obj->client)
302                 return UBUS_STATUS_INVALID_ARGUMENT;
303
304         target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
305         if (!target)
306                 return UBUS_STATUS_NOT_FOUND;
307
308         if (cl == target->client)
309                 return UBUS_STATUS_INVALID_ARGUMENT;
310
311         ubus_watch_new(obj, target, blob_data(attr[UBUS_ATTR_METHOD]));
312         return 0;
313 }
314
315 static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
316 {
317         struct ubus_object *obj;
318         struct ubus_watch *w;
319         uint32_t id;
320
321         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
322                 return UBUS_STATUS_INVALID_ARGUMENT;
323
324         obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
325         if (!obj)
326                 return UBUS_STATUS_NOT_FOUND;
327
328         if (cl != obj->client)
329                 return UBUS_STATUS_INVALID_ARGUMENT;
330
331         id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
332         list_for_each_entry(w, &obj->watched, watched_list) {
333                 if (w->watched->id.id != id)
334                         continue;
335
336                 ubus_watch_free(w);
337                 return 0;
338         }
339
340         return UBUS_STATUS_NOT_FOUND;
341 }
342
343 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
344         [UBUS_MSG_PING] = ubusd_send_pong,
345         [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
346         [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
347         [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
348         [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
349         [UBUS_MSG_STATUS] = ubusd_handle_response,
350         [UBUS_MSG_DATA] = ubusd_handle_response,
351         [UBUS_MSG_ADD_WATCH] = ubusd_handle_add_watch,
352         [UBUS_MSG_REMOVE_WATCH] = ubusd_handle_remove_watch,
353 };
354
355 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
356 {
357         ubus_cmd_cb cb = NULL;
358         int ret;
359
360         retmsg->hdr.seq = ub->hdr.seq;
361         retmsg->hdr.peer = ub->hdr.peer;
362
363         if (ub->hdr.type < __UBUS_MSG_LAST)
364                 cb = handlers[ub->hdr.type];
365
366         if (cb)
367                 ret = cb(cl, ub, ubus_parse_msg(ub->data));
368         else
369                 ret = UBUS_STATUS_INVALID_COMMAND;
370
371         if (ret == -1)
372                 return;
373
374         ubus_msg_free(ub);
375
376         *retmsg_data = htonl(ret);
377         ubus_msg_send(cl, retmsg, false);
378 }
379
380 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
381 {
382         struct ubus_client *cl;
383
384         cl = calloc(1, sizeof(*cl));
385         if (!cl)
386                 return NULL;
387
388         INIT_LIST_HEAD(&cl->objects);
389         cl->sock.fd = fd;
390         cl->sock.cb = cb;
391
392         if (!ubus_alloc_id(&clients, &cl->id, 0))
393                 goto free;
394
395         if (!ubusd_send_hello(cl))
396                 goto delete;
397
398         return cl;
399
400 delete:
401         ubus_free_id(&clients, &cl->id);
402 free:
403         free(cl);
404         return NULL;
405 }
406
407 void ubusd_proto_free_client(struct ubus_client *cl)
408 {
409         struct ubus_object *obj;
410
411         while (!list_empty(&cl->objects)) {
412                 obj = list_first_entry(&cl->objects, struct ubus_object, list);
413                 ubusd_free_object(obj);
414         }
415
416         ubus_free_id(&clients, &cl->id);
417 }
418
419 void ubus_proto_notify_watch(struct ubus_watch *w)
420 {
421         struct ubus_msg_buf *ub;
422         void *data;
423
424         blob_buf_init(&b, 0);
425         blob_put_int32(&b, UBUS_ATTR_OBJID, w->watcher->id.id);
426         blob_put_string(&b, UBUS_ATTR_METHOD, w->method);
427
428         data = blob_nest_start(&b, UBUS_ATTR_DATA);
429         blobmsg_add_string(&b, "notify", "remove");
430         blobmsg_add_u32(&b, "id", w->watched->id.id);
431         blobmsg_add_u32(&b, "peer", w->watched->client->id.id);
432         blob_nest_end(&b, data);
433
434         ub = ubus_msg_from_blob(false);
435         ubus_msg_init(ub, UBUS_MSG_INVOKE, ++w->watcher->invoke_seq, 0);
436         ubus_msg_send(w->watcher->client, ub, true);
437
438         ubus_watch_free(w);
439 }
440
441 static void __init ubusd_proto_init(void)
442 {
443         ubus_init_id_tree(&clients);
444
445         blob_buf_init(&b, 0);
446         blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
447
448         retmsg = ubus_msg_from_blob(false);
449         if (!retmsg)
450                 exit(1);
451
452         retmsg->hdr.type = UBUS_MSG_STATUS;
453         retmsg_data = blob_data(blob_data(retmsg->data));
454 }