add some stub functionality for the ubus event switch
[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, struct blob_attr **attr);
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, struct blob_attr **attr)
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, struct blob_attr **attr)
73 {
74         struct ubus_object *obj;
75
76         obj = ubusd_create_object(cl, attr);
77         if (!obj)
78                 return UBUS_STATUS_INVALID_ARGUMENT;
79
80         blob_buf_init(&b, 0);
81         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
82         if (attr[UBUS_ATTR_SIGNATURE])
83                 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
84
85         ub = ubus_reply_from_blob(ub, true);
86         if (!ub)
87                 return UBUS_STATUS_NO_DATA;
88
89         ubus_msg_send(cl, ub);
90         return 0;
91 }
92
93 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
94 {
95         struct ubus_method *m;
96         void *s;
97
98         blob_buf_init(&b, 0);
99
100         if (obj->path.key)
101                 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
102         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
103         blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
104
105         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
106         list_for_each_entry(m, &obj->type->methods, list)
107                 blob_put(&b, blob_id(m->data), blob_data(m->data), blob_len(m->data));
108         blob_nest_end(&b, s);
109
110         ub = ubus_reply_from_blob(ub, true);
111         if (!ub)
112                 return;
113
114         ubus_msg_send(cl, ub);
115 }
116
117 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
118 {
119         struct ubus_object *obj;
120         char *objpath;
121         bool wildcard = false;
122         bool found = false;
123         int len;
124
125         if (!attr[UBUS_ATTR_OBJPATH]) {
126                 avl_for_each_element(&path, obj, path)
127                         ubusd_send_obj(cl, ub, obj);
128                 return 0;
129         }
130
131         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
132         len = strlen(objpath);
133         if (objpath[len - 1] != '*') {
134                 obj = avl_find_element(&path, objpath, obj, path);
135                 if (!obj)
136                         return UBUS_STATUS_NOT_FOUND;
137
138                 ubusd_send_obj(cl, ub, obj);
139                 return 0;
140         }
141
142         objpath[--len] = 0;
143         wildcard = true;
144
145         obj = avl_find_ge_element(&path, objpath, obj, path);
146         if (!obj)
147                 return UBUS_STATUS_NOT_FOUND;
148
149         while (!strncmp(objpath, obj->path.key, len)) {
150                 found = true;
151                 ubusd_send_obj(cl, ub, obj);
152                 if (obj == avl_last_element(&path, obj, path))
153                         break;
154                 obj = avl_next_element(obj, path);
155         }
156
157         if (!found)
158                 return UBUS_STATUS_NOT_FOUND;
159
160         return 0;
161 }
162
163 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
164 {
165         struct ubus_object *obj = NULL;
166         struct ubus_id *id;
167         const char *method;
168
169         if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
170                 return UBUS_STATUS_INVALID_ARGUMENT;
171
172         id = ubus_find_id(&objects, blob_get_int32(attr[UBUS_ATTR_OBJID]));
173         if (!id)
174                 return UBUS_STATUS_NOT_FOUND;
175
176         obj = container_of(id, struct ubus_object, id);
177
178         method = blob_data(attr[UBUS_ATTR_METHOD]);
179
180         if (!obj->client)
181                 return obj->recv_msg(cl, method, attr[UBUS_ATTR_DATA]);
182
183         blob_buf_init(&b, 0);
184         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
185         blob_put_string(&b, UBUS_ATTR_METHOD, method);
186         if (attr[UBUS_ATTR_DATA])
187                 blob_put(&b, UBUS_ATTR_DATA, blob_data(attr[UBUS_ATTR_DATA]),
188                          blob_len(attr[UBUS_ATTR_DATA]));
189
190         ubus_msg_free(ub);
191
192         ub = ubus_reply_from_blob(ub, true);
193         if (!ub)
194                 return UBUS_STATUS_NO_DATA;
195
196         ub->hdr.type = UBUS_MSG_INVOKE;
197         ub->hdr.peer = cl->id.id;
198         ubus_msg_send(obj->client, ub);
199
200         return -1;
201 }
202
203 static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
204 {
205         struct ubus_object *obj;
206         struct ubus_id *id;
207
208         if (!attr[UBUS_ATTR_OBJID] ||
209             (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
210             (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
211                 goto error;
212
213         id = ubus_find_id(&objects, blob_get_int32(attr[UBUS_ATTR_OBJID]));
214         if (!id)
215                 goto error;
216
217         obj = container_of(id, struct ubus_object, id);
218         if (cl != obj->client)
219                 goto error;
220
221         cl = ubusd_get_client_by_id(ub->hdr.peer);
222         if (!cl)
223                 goto error;
224
225         ub->hdr.peer = blob_get_int32(attr[UBUS_ATTR_OBJID]);
226         ubus_msg_send(cl, ub);
227         return -1;
228
229 error:
230         ubus_msg_free(ub);
231         return -1;
232 }
233
234 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
235         [UBUS_MSG_PING] = ubusd_send_pong,
236         [UBUS_MSG_PUBLISH] = ubusd_handle_publish,
237         [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
238         [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
239         [UBUS_MSG_STATUS] = ubusd_handle_response,
240         [UBUS_MSG_DATA] = ubusd_handle_response,
241 };
242
243 void ubusd_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
244 {
245         ubus_cmd_cb cb = NULL;
246         int ret;
247
248         retmsg->hdr.seq = ub->hdr.seq;
249         retmsg->hdr.peer = ub->hdr.peer;
250
251         if (ub->hdr.type < __UBUS_MSG_LAST)
252                 cb = handlers[ub->hdr.type];
253
254         if (cb)
255                 ret = cb(cl, ub, ubus_parse_msg(ub->data));
256         else
257                 ret = UBUS_STATUS_INVALID_COMMAND;
258
259         if (ret == -1)
260                 return;
261
262         ubus_msg_free(ub);
263
264         *retmsg_data = htonl(ret);
265         ubus_msg_send(cl, ubus_msg_ref(retmsg));
266 }
267
268 static void __init ubusd_proto_init(void)
269 {
270         blob_buf_init(&b, 0);
271         blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
272
273         retmsg = ubus_msg_from_blob(false);
274         if (!retmsg)
275                 exit(1);
276
277         retmsg->hdr.type = UBUS_MSG_STATUS;
278         retmsg_data = blob_data(blob_data(retmsg->data));
279 }