2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
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
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.
14 #include <arpa/inet.h>
20 static struct ubus_msg_buf
*retmsg
;
21 static int *retmsg_data
;
22 static struct avl_tree clients
;
24 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
26 typedef int (*ubus_cmd_cb
)(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
);
28 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
29 [UBUS_ATTR_SIGNATURE
] = { .type
= BLOB_ATTR_NESTED
},
30 [UBUS_ATTR_OBJTYPE
] = { .type
= BLOB_ATTR_INT32
},
31 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
32 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
33 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
34 [UBUS_ATTR_METHOD
] = { .type
= BLOB_ATTR_STRING
},
37 struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
39 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
43 static void ubus_msg_close_fd(struct ubus_msg_buf
*ub
)
52 static void ubus_msg_init(struct ubus_msg_buf
*ub
, uint8_t type
, uint16_t seq
, uint32_t peer
)
60 static struct ubus_msg_buf
*ubus_msg_from_blob(bool shared
)
62 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), shared
);
65 static struct ubus_msg_buf
*ubus_reply_from_blob(struct ubus_msg_buf
*ub
, bool shared
)
67 struct ubus_msg_buf
*new;
69 new = ubus_msg_from_blob(shared
);
73 ubus_msg_init(new, UBUS_MSG_DATA
, ub
->hdr
.seq
, ub
->hdr
.peer
);
78 ubus_proto_send_msg_from_blob(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
,
81 ub
= ubus_reply_from_blob(ub
, true);
86 ubus_msg_send(cl
, ub
, true);
89 static bool ubusd_send_hello(struct ubus_client
*cl
)
91 struct ubus_msg_buf
*ub
;
94 ub
= ubus_msg_from_blob(true);
98 ubus_msg_init(ub
, UBUS_MSG_HELLO
, 0, cl
->id
.id
);
99 ubus_msg_send(cl
, ub
, true);
103 static int ubusd_send_pong(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
105 ub
->hdr
.type
= UBUS_MSG_DATA
;
106 ubus_msg_send(cl
, ub
, false);
110 static int ubusd_handle_remove_object(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
112 struct ubus_object
*obj
;
114 if (!attr
[UBUS_ATTR_OBJID
])
115 return UBUS_STATUS_INVALID_ARGUMENT
;
117 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
119 return UBUS_STATUS_NOT_FOUND
;
121 if (obj
->client
!= cl
)
122 return UBUS_STATUS_PERMISSION_DENIED
;
124 blob_buf_init(&b
, 0);
125 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
127 /* check if we're removing the object type as well */
128 if (obj
->type
&& obj
->type
->refcount
== 1)
129 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
.id
);
131 ubusd_free_object(obj
);
132 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
137 static int ubusd_handle_add_object(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
139 struct ubus_object
*obj
;
141 obj
= ubusd_create_object(cl
, attr
);
143 return UBUS_STATUS_INVALID_ARGUMENT
;
145 blob_buf_init(&b
, 0);
146 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
147 if (attr
[UBUS_ATTR_SIGNATURE
])
148 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
.id
);
150 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
154 static void ubusd_send_obj(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct ubus_object
*obj
)
156 struct ubus_method
*m
;
159 blob_buf_init(&b
, 0);
161 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->path
.key
);
162 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
163 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
.id
);
165 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
166 list_for_each_entry(m
, &obj
->type
->methods
, list
)
167 blobmsg_add_blob(&b
, m
->data
);
168 blob_nest_end(&b
, s
);
170 ubus_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
173 static int ubusd_handle_lookup(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
175 struct ubus_object
*obj
;
180 if (!attr
[UBUS_ATTR_OBJPATH
]) {
181 avl_for_each_element(&path
, obj
, path
)
182 ubusd_send_obj(cl
, ub
, obj
);
186 objpath
= blob_data(attr
[UBUS_ATTR_OBJPATH
]);
187 len
= strlen(objpath
);
188 if (objpath
[len
- 1] != '*') {
189 obj
= avl_find_element(&path
, objpath
, obj
, path
);
191 return UBUS_STATUS_NOT_FOUND
;
193 ubusd_send_obj(cl
, ub
, obj
);
199 obj
= avl_find_ge_element(&path
, objpath
, obj
, path
);
201 return UBUS_STATUS_NOT_FOUND
;
203 while (!strncmp(objpath
, obj
->path
.key
, len
)) {
205 ubusd_send_obj(cl
, ub
, obj
);
206 if (obj
== avl_last_element(&path
, obj
, path
))
208 obj
= avl_next_element(obj
, path
);
212 return UBUS_STATUS_NOT_FOUND
;
218 ubusd_forward_invoke(struct ubus_object
*obj
, const char *method
,
219 struct ubus_msg_buf
*ub
, struct blob_attr
*data
)
221 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
222 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
224 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(data
), blob_len(data
));
226 ubus_proto_send_msg_from_blob(obj
->client
, ub
, UBUS_MSG_INVOKE
);
229 static int ubusd_handle_invoke(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
231 struct ubus_object
*obj
= NULL
;
235 if (!attr
[UBUS_ATTR_METHOD
] || !attr
[UBUS_ATTR_OBJID
])
236 return UBUS_STATUS_INVALID_ARGUMENT
;
238 id
= ubus_find_id(&objects
, blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
240 return UBUS_STATUS_NOT_FOUND
;
242 obj
= container_of(id
, struct ubus_object
, id
);
244 method
= blob_data(attr
[UBUS_ATTR_METHOD
]);
247 return obj
->recv_msg(cl
, ub
, method
, attr
[UBUS_ATTR_DATA
]);
249 ub
->hdr
.peer
= cl
->id
.id
;
250 blob_buf_init(&b
, 0);
251 ubusd_forward_invoke(obj
, method
, ub
, attr
[UBUS_ATTR_DATA
]);
257 static int ubusd_handle_notify(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
259 struct ubus_object
*obj
= NULL
;
260 struct ubus_subscription
*s
;
263 bool no_reply
= false;
266 if (!attr
[UBUS_ATTR_METHOD
] || !attr
[UBUS_ATTR_OBJID
])
267 return UBUS_STATUS_INVALID_ARGUMENT
;
269 if (attr
[UBUS_ATTR_NO_REPLY
])
270 no_reply
= blob_get_int8(attr
[UBUS_ATTR_NO_REPLY
]);
272 id
= ubus_find_id(&objects
, blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
274 return UBUS_STATUS_NOT_FOUND
;
276 obj
= container_of(id
, struct ubus_object
, id
);
277 if (obj
->client
!= cl
)
278 return UBUS_STATUS_PERMISSION_DENIED
;
281 blob_buf_init(&b
, 0);
282 blob_put_int32(&b
, UBUS_ATTR_OBJID
, id
->id
);
283 c
= blob_nest_start(&b
, UBUS_ATTR_SUBSCRIBERS
);
284 list_for_each_entry(s
, &obj
->subscribers
, list
) {
285 blob_put_int32(&b
, 0, s
->subscriber
->id
.id
);
287 blob_nest_end(&b
, c
);
288 blob_put_int32(&b
, UBUS_ATTR_STATUS
, 0);
289 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_STATUS
);
292 ub
->hdr
.peer
= cl
->id
.id
;
293 method
= blob_data(attr
[UBUS_ATTR_METHOD
]);
294 list_for_each_entry(s
, &obj
->subscribers
, list
) {
295 blob_buf_init(&b
, 0);
297 blob_put_int8(&b
, UBUS_ATTR_NO_REPLY
, 1);
298 ubusd_forward_invoke(s
->subscriber
, method
, ub
, attr
[UBUS_ATTR_DATA
]);
305 static struct ubus_client
*ubusd_get_client_by_id(uint32_t id
)
307 struct ubus_id
*clid
;
309 clid
= ubus_find_id(&clients
, id
);
313 return container_of(clid
, struct ubus_client
, id
);
316 static int ubusd_handle_response(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
318 struct ubus_object
*obj
;
320 if (!attr
[UBUS_ATTR_OBJID
] ||
321 (ub
->hdr
.type
== UBUS_MSG_STATUS
&& !attr
[UBUS_ATTR_STATUS
]) ||
322 (ub
->hdr
.type
== UBUS_MSG_DATA
&& !attr
[UBUS_ATTR_DATA
]))
325 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
329 if (cl
!= obj
->client
)
332 cl
= ubusd_get_client_by_id(ub
->hdr
.peer
);
336 ub
->hdr
.peer
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
337 ubus_msg_send(cl
, ub
, true);
345 static int ubusd_handle_add_watch(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
347 struct ubus_object
*obj
, *target
;
349 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_TARGET
])
350 return UBUS_STATUS_INVALID_ARGUMENT
;
352 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
354 return UBUS_STATUS_NOT_FOUND
;
356 if (cl
!= obj
->client
)
357 return UBUS_STATUS_INVALID_ARGUMENT
;
359 target
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_TARGET
]));
361 return UBUS_STATUS_NOT_FOUND
;
363 if (cl
== target
->client
)
364 return UBUS_STATUS_INVALID_ARGUMENT
;
366 ubus_subscribe(obj
, target
);
370 static int ubusd_handle_remove_watch(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
)
372 struct ubus_object
*obj
;
373 struct ubus_subscription
*s
;
376 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_TARGET
])
377 return UBUS_STATUS_INVALID_ARGUMENT
;
379 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
381 return UBUS_STATUS_NOT_FOUND
;
383 if (cl
!= obj
->client
)
384 return UBUS_STATUS_INVALID_ARGUMENT
;
386 id
= blob_get_u32(attr
[UBUS_ATTR_TARGET
]);
387 list_for_each_entry(s
, &obj
->target_list
, target_list
) {
388 if (s
->target
->id
.id
!= id
)
395 return UBUS_STATUS_NOT_FOUND
;
398 static const ubus_cmd_cb handlers
[__UBUS_MSG_LAST
] = {
399 [UBUS_MSG_PING
] = ubusd_send_pong
,
400 [UBUS_MSG_ADD_OBJECT
] = ubusd_handle_add_object
,
401 [UBUS_MSG_REMOVE_OBJECT
] = ubusd_handle_remove_object
,
402 [UBUS_MSG_LOOKUP
] = ubusd_handle_lookup
,
403 [UBUS_MSG_INVOKE
] = ubusd_handle_invoke
,
404 [UBUS_MSG_STATUS
] = ubusd_handle_response
,
405 [UBUS_MSG_DATA
] = ubusd_handle_response
,
406 [UBUS_MSG_SUBSCRIBE
] = ubusd_handle_add_watch
,
407 [UBUS_MSG_UNSUBSCRIBE
] = ubusd_handle_remove_watch
,
408 [UBUS_MSG_NOTIFY
] = ubusd_handle_notify
,
411 void ubusd_proto_receive_message(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
413 ubus_cmd_cb cb
= NULL
;
416 retmsg
->hdr
.seq
= ub
->hdr
.seq
;
417 retmsg
->hdr
.peer
= ub
->hdr
.peer
;
419 if (ub
->hdr
.type
< __UBUS_MSG_LAST
)
420 cb
= handlers
[ub
->hdr
.type
];
422 if (ub
->hdr
.type
!= UBUS_MSG_STATUS
)
423 ubus_msg_close_fd(ub
);
426 ret
= cb(cl
, ub
, ubus_parse_msg(ub
->data
));
428 ret
= UBUS_STATUS_INVALID_COMMAND
;
435 *retmsg_data
= htonl(ret
);
436 ubus_msg_send(cl
, retmsg
, false);
439 struct ubus_client
*ubusd_proto_new_client(int fd
, uloop_fd_handler cb
)
441 struct ubus_client
*cl
;
443 cl
= calloc(1, sizeof(*cl
));
447 INIT_LIST_HEAD(&cl
->objects
);
450 cl
->pending_msg_fd
= -1;
452 if (!ubus_alloc_id(&clients
, &cl
->id
, 0))
455 if (!ubusd_send_hello(cl
))
461 ubus_free_id(&clients
, &cl
->id
);
467 void ubusd_proto_free_client(struct ubus_client
*cl
)
469 struct ubus_object
*obj
;
471 while (!list_empty(&cl
->objects
)) {
472 obj
= list_first_entry(&cl
->objects
, struct ubus_object
, list
);
473 ubusd_free_object(obj
);
476 ubus_free_id(&clients
, &cl
->id
);
479 void ubus_notify_subscription(struct ubus_object
*obj
)
481 bool active
= !list_empty(&obj
->subscribers
);
482 struct ubus_msg_buf
*ub
;
484 blob_buf_init(&b
, 0);
485 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
486 blob_put_int8(&b
, UBUS_ATTR_ACTIVE
, active
);
488 ub
= ubus_msg_from_blob(false);
492 ubus_msg_init(ub
, UBUS_MSG_NOTIFY
, ++obj
->invoke_seq
, 0);
493 ubus_msg_send(obj
->client
, ub
, true);
496 void ubus_notify_unsubscribe(struct ubus_subscription
*s
)
498 struct ubus_msg_buf
*ub
;
500 blob_buf_init(&b
, 0);
501 blob_put_int32(&b
, UBUS_ATTR_OBJID
, s
->subscriber
->id
.id
);
502 blob_put_int32(&b
, UBUS_ATTR_TARGET
, s
->target
->id
.id
);
504 ub
= ubus_msg_from_blob(false);
506 ubus_msg_init(ub
, UBUS_MSG_UNSUBSCRIBE
, ++s
->subscriber
->invoke_seq
, 0);
507 ubus_msg_send(s
->subscriber
->client
, ub
, true);
513 static void __constructor
ubusd_proto_init(void)
515 ubus_init_id_tree(&clients
);
517 blob_buf_init(&b
, 0);
518 blob_put_int32(&b
, UBUS_ATTR_STATUS
, 0);
520 retmsg
= ubus_msg_from_blob(false);
524 retmsg
->hdr
.type
= UBUS_MSG_STATUS
;
525 retmsg_data
= blob_data(blob_data(retmsg
->data
));