ubusd_proto.c 6.5 KB

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