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