libubus: keep objects in an avl tree
[project/ubus.git] / ubusd_proto.c
1 #include <arpa/inet.h>
2 #include "ubusd.h"
3
4 static struct blob_buf b;
5 static struct ubus_msg_buf *retmsg;
6 static int *retmsg_data;
7
8 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
9
10 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub);
11
12 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
13         [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
14         [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
15         [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
16         [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
17         [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
18 };
19
20 struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
21 {
22         blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
23         return attrbuf;
24 }
25
26 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
27 {
28         ub->hdr.version = 0;
29         ub->hdr.type = type;
30         ub->hdr.seq = seq;
31         ub->hdr.peer = peer;
32 }
33
34 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
35 {
36         return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
37 }
38
39 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
40 {
41         struct ubus_msg_buf *new;
42
43         new = ubus_msg_new(b.head, blob_raw_len(b.head), shared);
44         if (!new)
45                 return NULL;
46
47         ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
48         return new;
49 }
50
51 bool ubusd_send_hello(struct ubus_client *cl)
52 {
53         struct ubus_msg_buf *ub;
54
55         blob_buf_init(&b, 0);
56         ub = ubus_msg_from_blob(true);
57         if (!ub)
58                 return false;
59
60         ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
61         ubus_msg_send(cl, ub);
62         return true;
63 }
64
65 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub)
66 {
67         ub->hdr.type = UBUS_MSG_DATA;
68         ubus_msg_send(cl, ubus_msg_ref(ub));
69         return 0;
70 }
71
72 static int ubusd_handle_publish(struct ubus_client *cl, struct ubus_msg_buf *ub)
73 {
74         struct ubus_object *obj;
75         struct blob_attr **attr;
76
77         attr = ubus_parse_msg(ub->data);
78         obj = ubusd_create_object(cl, attr);
79         if (!obj)
80                 return UBUS_STATUS_INVALID_ARGUMENT;
81
82         blob_buf_init(&b, 0);
83         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
84         if (attr[UBUS_ATTR_SIGNATURE])
85                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
86
87         ub = ubus_reply_from_blob(ub, true);
88         if (!ub)
89                 return UBUS_STATUS_NO_DATA;
90
91         ubus_msg_send(cl, ub);
92         return 0;
93 }
94
95 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
96 {
97         struct ubus_method *m;
98         void *s;
99
100         blob_buf_init(&b, 0);
101
102         if (obj->path.key)
103                 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
104         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
105
106         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
107         list_for_each_entry(m, &obj->type->methods, list)
108                 blob_put(&b, blob_id(m->data), blob_data(m->data), blob_len(m->data));
109         blob_nest_end(&b, s);
110
111         ub = ubus_reply_from_blob(ub, true);
112         if (!ub)
113                 return;
114
115         ubus_msg_send(cl, ub);
116 }
117
118 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub)
119 {
120         struct ubus_object *obj;
121         struct blob_attr **attr;
122         char *objpath;
123         bool wildcard = false;
124         bool found = false;
125         int len;
126
127         attr = ubus_parse_msg(ub->data);
128         if (!attr[UBUS_ATTR_OBJPATH]) {
129                 avl_for_each_element(&path, obj, path)
130                         ubusd_send_obj(cl, ub, obj);
131                 return 0;
132         }
133
134         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
135         len = strlen(objpath);
136         if (objpath[len - 1] != '*') {
137                 obj = avl_find_element(&path, objpath, obj, path);
138                 if (!obj)
139                         return UBUS_STATUS_NOT_FOUND;
140
141                 ubusd_send_obj(cl, ub, obj);
142                 return 0;
143         }
144
145         objpath[--len] = 0;
146         wildcard = true;
147
148         obj = avl_find_ge_element(&path, objpath, obj, path);
149         if (!obj)
150                 return UBUS_STATUS_NOT_FOUND;
151
152         while (!strncmp(objpath, obj->path.key, len)) {
153                 found = true;
154                 ubusd_send_obj(cl, ub, obj);
155                 if (obj == avl_last_element(&path, obj, path))
156                         break;
157                 obj = avl_next_element(obj, path);
158         }
159
160         if (!found)
161                 return UBUS_STATUS_NOT_FOUND;
162
163         return 0;
164 }
165
166 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub)
167 {
168         struct ubus_object *obj = NULL;
169         struct blob_attr **attr;
170         const char *method;
171
172         attr = ubus_parse_msg(ub->data);
173         if (!attr[UBUS_ATTR_METHOD])
174                 return UBUS_STATUS_INVALID_ARGUMENT;
175
176         if (attr[UBUS_ATTR_OBJID]) {
177                 struct ubus_id *id;
178                 id = ubus_find_id(&objects, blob_get_int32(attr[UBUS_ATTR_OBJID]));
179                 if (id)
180                         obj = container_of(id, struct ubus_object, id);
181         } else if (attr[UBUS_ATTR_OBJPATH]) {
182                 const char *objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
183                 obj = avl_find_element(&path, objpath, obj, path);
184         }
185         if (!obj)
186                 return UBUS_STATUS_NOT_FOUND;
187
188         method = blob_data(attr[UBUS_ATTR_METHOD]);
189         blob_buf_init(&b, 0);
190         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
191         blob_put_string(&b, UBUS_ATTR_METHOD, method);
192         if (attr[UBUS_ATTR_DATA])
193                 blob_put(&b, UBUS_ATTR_DATA, blob_data(attr[UBUS_ATTR_DATA]),
194                          blob_len(attr[UBUS_ATTR_DATA]));
195
196         ubus_msg_free(ub);
197
198         ub = ubus_reply_from_blob(ub, true);
199         if (!ub)
200                 return UBUS_STATUS_NO_DATA;
201
202         ub->hdr.type = UBUS_MSG_INVOKE;
203         ub->hdr.peer = cl->id.id;
204         ubus_msg_send(obj->client, ub);
205
206         return -1;
207 }
208
209 static int ubusd_handle_status(struct ubus_client *cl, struct ubus_msg_buf *ub)
210 {
211         struct blob_attr **attr;
212
213         attr = ubus_parse_msg(ub->data);
214         if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_STATUS])
215                 goto error;
216
217         cl = ubusd_get_client_by_id(ub->hdr.peer);
218         if (!cl)
219                 goto error;
220
221         ub->hdr.peer = blob_get_int32(attr[UBUS_ATTR_OBJID]);
222         ubus_msg_send(cl, ub);
223         return -1;
224
225 error:
226         ubus_msg_free(ub);
227         return -1;
228 }
229
230 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
231         [UBUS_MSG_PING] = ubusd_send_pong,
232         [UBUS_MSG_PUBLISH] = ubusd_handle_publish,
233         [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
234         [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
235         [UBUS_MSG_STATUS] = ubusd_handle_status,
236 };
237
238 void ubusd_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
239 {
240         ubus_cmd_cb cb = NULL;
241         int ret;
242
243         retmsg->hdr.seq = ub->hdr.seq;
244         retmsg->hdr.peer = ub->hdr.peer;
245
246         if (ub->hdr.type < __UBUS_MSG_LAST)
247                 cb = handlers[ub->hdr.type];
248
249         if (cb)
250                 ret = cb(cl, ub);
251         else
252                 ret = UBUS_STATUS_INVALID_COMMAND;
253
254         if (ret == -1)
255                 return;
256
257         ubus_msg_free(ub);
258
259         *retmsg_data = htonl(ret);
260         ubus_msg_send(cl, ubus_msg_ref(retmsg));
261 }
262
263 static void __init ubusd_proto_init(void)
264 {
265         blob_buf_init(&b, 0);
266         blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
267
268         retmsg = ubus_msg_from_blob(false);
269         if (!retmsg)
270                 exit(1);
271
272         retmsg->hdr.type = UBUS_MSG_STATUS;
273         retmsg_data = blob_data(blob_data(retmsg->data));
274 }