use int8 as boolean
[project/ubus.git] / ubusd_proto.c
1 #include <arpa/inet.h>
2 #include "ubusd.h"
3
4 struct blob_buf b;
5 static struct ubus_msg_buf *retmsg;
6 static int *retmsg_data;
7 static struct avl_tree clients;
8
9 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
10
11 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
12
13 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
14 [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
15 [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
16 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
17 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
18 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
19 };
20
21 static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
22 {
23 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
24 return attrbuf;
25 }
26
27 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
28 {
29 ub->hdr.version = 0;
30 ub->hdr.type = type;
31 ub->hdr.seq = seq;
32 ub->hdr.peer = peer;
33 }
34
35 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
36 {
37 return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
38 }
39
40 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
41 {
42 struct ubus_msg_buf *new;
43
44 new = ubus_msg_from_blob(shared);
45 if (!new)
46 return NULL;
47
48 ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
49 return new;
50 }
51
52 static bool ubusd_send_hello(struct ubus_client *cl)
53 {
54 struct ubus_msg_buf *ub;
55
56 blob_buf_init(&b, 0);
57 ub = ubus_msg_from_blob(true);
58 if (!ub)
59 return false;
60
61 ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
62 ubus_msg_send(cl, ub, true);
63 return true;
64 }
65
66 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
67 {
68 ub->hdr.type = UBUS_MSG_DATA;
69 ubus_msg_send(cl, ub, false);
70 return 0;
71 }
72
73 static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
74 {
75 struct ubus_object *obj;
76
77 if (!attr[UBUS_ATTR_OBJID])
78 return UBUS_STATUS_INVALID_ARGUMENT;
79
80 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
81 if (!obj)
82 return UBUS_STATUS_NOT_FOUND;
83
84 if (obj->client != cl)
85 return UBUS_STATUS_PERMISSION_DENIED;
86
87 blob_buf_init(&b, 0);
88 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
89
90 /* check if we're removing the object type as well */
91 if (obj->type && obj->type->refcount == 1)
92 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
93
94 ubusd_free_object(obj);
95
96 ub = ubus_reply_from_blob(ub, true);
97 if (!ub)
98 return UBUS_STATUS_NO_DATA;
99
100 ubus_msg_send(cl, ub, true);
101 return 0;
102 }
103
104 static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
105 {
106 struct ubus_object *obj;
107
108 obj = ubusd_create_object(cl, attr);
109 if (!obj)
110 return UBUS_STATUS_INVALID_ARGUMENT;
111
112 blob_buf_init(&b, 0);
113 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
114 if (attr[UBUS_ATTR_SIGNATURE])
115 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
116
117 ub = ubus_reply_from_blob(ub, true);
118 if (!ub)
119 return UBUS_STATUS_NO_DATA;
120
121 ubus_msg_send(cl, ub, true);
122 return 0;
123 }
124
125 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
126 {
127 struct ubus_method *m;
128 void *s;
129
130 blob_buf_init(&b, 0);
131
132 if (obj->path.key)
133 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
134 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
135 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
136
137 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
138 list_for_each_entry(m, &obj->type->methods, list)
139 blob_put(&b, blob_id(m->data), blob_data(m->data), blob_len(m->data));
140 blob_nest_end(&b, s);
141
142 ub = ubus_reply_from_blob(ub, true);
143 if (!ub)
144 return;
145
146 ubus_msg_send(cl, ub, true);
147 }
148
149 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
150 {
151 struct ubus_object *obj;
152 char *objpath;
153 bool wildcard = false;
154 bool found = false;
155 int len;
156
157 if (!attr[UBUS_ATTR_OBJPATH]) {
158 avl_for_each_element(&path, obj, path)
159 ubusd_send_obj(cl, ub, obj);
160 return 0;
161 }
162
163 objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
164 len = strlen(objpath);
165 if (objpath[len - 1] != '*') {
166 obj = avl_find_element(&path, objpath, obj, path);
167 if (!obj)
168 return UBUS_STATUS_NOT_FOUND;
169
170 ubusd_send_obj(cl, ub, obj);
171 return 0;
172 }
173
174 objpath[--len] = 0;
175 wildcard = true;
176
177 obj = avl_find_ge_element(&path, objpath, obj, path);
178 if (!obj)
179 return UBUS_STATUS_NOT_FOUND;
180
181 while (!strncmp(objpath, obj->path.key, len)) {
182 found = true;
183 ubusd_send_obj(cl, ub, obj);
184 if (obj == avl_last_element(&path, obj, path))
185 break;
186 obj = avl_next_element(obj, path);
187 }
188
189 if (!found)
190 return UBUS_STATUS_NOT_FOUND;
191
192 return 0;
193 }
194
195 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
196 {
197 struct ubus_object *obj = NULL;
198 struct ubus_id *id;
199 const char *method;
200
201 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
202 return UBUS_STATUS_INVALID_ARGUMENT;
203
204 id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
205 if (!id)
206 return UBUS_STATUS_NOT_FOUND;
207
208 obj = container_of(id, struct ubus_object, id);
209
210 method = blob_data(attr[UBUS_ATTR_METHOD]);
211
212 if (!obj->client)
213 return obj->recv_msg(cl, method, attr[UBUS_ATTR_DATA]);
214
215 blob_buf_init(&b, 0);
216 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
217 blob_put_string(&b, UBUS_ATTR_METHOD, method);
218 if (attr[UBUS_ATTR_DATA])
219 blob_put(&b, UBUS_ATTR_DATA, blob_data(attr[UBUS_ATTR_DATA]),
220 blob_len(attr[UBUS_ATTR_DATA]));
221
222 ubus_msg_free(ub);
223
224 ub = ubus_reply_from_blob(ub, true);
225 if (!ub)
226 return UBUS_STATUS_NO_DATA;
227
228 ub->hdr.type = UBUS_MSG_INVOKE;
229 ub->hdr.peer = cl->id.id;
230 ubus_msg_send(obj->client, ub, true);
231
232 return -1;
233 }
234
235 static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
236 {
237 struct ubus_id *clid;
238
239 clid = ubus_find_id(&clients, id);
240 if (!clid)
241 return NULL;
242
243 return container_of(clid, struct ubus_client, id);
244 }
245
246 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
247 {
248 struct ubus_object *obj;
249
250 if (!attr[UBUS_ATTR_OBJID] ||
251 (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
252 (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
253 goto error;
254
255 obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
256 if (!obj)
257 goto error;
258
259 if (cl != obj->client)
260 goto error;
261
262 cl = ubusd_get_client_by_id(ub->hdr.peer);
263 if (!cl)
264 goto error;
265
266 ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
267 ubus_msg_send(cl, ub, true);
268 return -1;
269
270 error:
271 ubus_msg_free(ub);
272 return -1;
273 }
274
275 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
276 [UBUS_MSG_PING] = ubusd_send_pong,
277 [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
278 [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
279 [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
280 [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
281 [UBUS_MSG_STATUS] = ubusd_handle_response,
282 [UBUS_MSG_DATA] = ubusd_handle_response,
283 };
284
285 void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
286 {
287 ubus_cmd_cb cb = NULL;
288 int ret;
289
290 retmsg->hdr.seq = ub->hdr.seq;
291 retmsg->hdr.peer = ub->hdr.peer;
292
293 if (ub->hdr.type < __UBUS_MSG_LAST)
294 cb = handlers[ub->hdr.type];
295
296 if (cb)
297 ret = cb(cl, ub, ubus_parse_msg(ub->data));
298 else
299 ret = UBUS_STATUS_INVALID_COMMAND;
300
301 if (ret == -1)
302 return;
303
304 ubus_msg_free(ub);
305
306 *retmsg_data = htonl(ret);
307 ubus_msg_send(cl, retmsg, false);
308 }
309
310 struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
311 {
312 struct ubus_client *cl;
313
314 cl = calloc(1, sizeof(*cl));
315 if (!cl)
316 return NULL;
317
318 INIT_LIST_HEAD(&cl->objects);
319 cl->sock.fd = fd;
320 cl->sock.cb = cb;
321
322 if (!ubus_alloc_id(&clients, &cl->id, 0))
323 goto free;
324
325 if (!ubusd_send_hello(cl))
326 goto delete;
327
328 return cl;
329
330 delete:
331 ubus_free_id(&clients, &cl->id);
332 free:
333 free(cl);
334 return NULL;
335 }
336
337 void ubusd_proto_free_client(struct ubus_client *cl)
338 {
339 struct ubus_object *obj;
340
341 while (!list_empty(&cl->objects)) {
342 obj = list_first_entry(&cl->objects, struct ubus_object, list);
343 ubusd_free_object(obj);
344 }
345
346 ubus_free_id(&clients, &cl->id);
347 }
348
349 static void __init ubusd_proto_init(void)
350 {
351 ubus_init_id_tree(&clients);
352
353 blob_buf_init(&b, 0);
354 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
355
356 retmsg = ubus_msg_from_blob(false);
357 if (!retmsg)
358 exit(1);
359
360 retmsg->hdr.type = UBUS_MSG_STATUS;
361 retmsg_data = blob_data(blob_data(retmsg->data));
362 }