8a69541d0a6120a6d906031fc7072fbbcf8aca8b
[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 };
17
18 struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
19 {
20         blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
21         return attrbuf;
22 }
23
24 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
25 {
26         ub->hdr.version = 0;
27         ub->hdr.type = type;
28         ub->hdr.seq = seq;
29         ub->hdr.peer = peer;
30 }
31
32 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
33 {
34         return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
35 }
36
37 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
38 {
39         struct ubus_msg_buf *new;
40
41         new = ubus_msg_new(b.head, blob_raw_len(b.head), shared);
42         if (!new)
43                 return NULL;
44
45         ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
46         return new;
47 }
48
49 bool ubusd_send_hello(struct ubus_client *cl)
50 {
51         struct ubus_msg_buf *ub;
52
53         blob_buf_init(&b, 0);
54         ub = ubus_msg_from_blob(true);
55         if (!ub)
56                 return false;
57
58         ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
59         ubus_msg_send(cl, ub);
60         return true;
61 }
62
63 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub)
64 {
65         ub->hdr.type = UBUS_MSG_DATA;
66         ubus_msg_send(cl, ubus_msg_ref(ub));
67         return 0;
68 }
69
70 static int ubusd_handle_publish(struct ubus_client *cl, struct ubus_msg_buf *ub)
71 {
72         struct ubus_object *obj;
73         struct blob_attr **attr;
74
75         attr = ubus_parse_msg(ub->data);
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
104         s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
105         list_for_each_entry(m, &obj->type->methods, list)
106                 blob_put(&b, blob_id(m->data), blob_data(m->data), blob_len(m->data));
107         blob_nest_end(&b, s);
108
109         ub = ubus_reply_from_blob(ub, true);
110         if (!ub)
111                 return;
112
113         ubus_msg_send(cl, ub);
114 }
115
116 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub)
117 {
118         struct ubus_object *obj;
119         struct blob_attr **attr;
120         char *objpath;
121         bool wildcard = false;
122         bool found = false;
123         int len;
124
125         attr = ubus_parse_msg(ub->data);
126         if (!attr[UBUS_ATTR_OBJPATH]) {
127                 avl_for_each_element(&path, obj, path)
128                         ubusd_send_obj(cl, ub, obj);
129                 return 0;
130         }
131
132         objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
133         len = strlen(objpath);
134         if (objpath[len - 1] != '*') {
135                 obj = avl_find_element(&path, objpath, obj, path);
136                 if (!obj)
137                         return UBUS_STATUS_NOT_FOUND;
138
139                 ubusd_send_obj(cl, ub, obj);
140                 return 0;
141         }
142
143         objpath[--len] = 0;
144         wildcard = true;
145
146         obj = avl_find_ge_element(&path, objpath, obj, path);
147         if (!obj)
148                 return UBUS_STATUS_NOT_FOUND;
149
150         while (!strncmp(objpath, obj->path.key, len)) {
151                 found = true;
152                 ubusd_send_obj(cl, ub, obj);
153                 if (obj == avl_last_element(&path, obj, path))
154                         break;
155                 obj = avl_next_element(obj, path);
156         }
157
158         if (!found)
159                 return UBUS_STATUS_NOT_FOUND;
160
161         return 0;
162 }
163
164 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub)
165 {
166         return UBUS_STATUS_NOT_FOUND;
167 }
168
169 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
170         [UBUS_MSG_PING] = ubusd_send_pong,
171         [UBUS_MSG_PUBLISH] = ubusd_handle_publish,
172         [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
173         [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
174 };
175
176 void ubusd_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
177 {
178         ubus_cmd_cb cb = NULL;
179         int ret;
180
181         retmsg->hdr.seq = ub->hdr.seq;
182         retmsg->hdr.peer = ub->hdr.peer;
183
184         if (ub->hdr.type < __UBUS_MSG_LAST)
185                 cb = handlers[ub->hdr.type];
186
187         if (cb)
188                 ret = cb(cl, ub);
189         else
190                 ret = UBUS_STATUS_INVALID_COMMAND;
191
192         ubus_msg_free(ub);
193
194         *retmsg_data = htonl(ret);
195         ubus_msg_send(cl, ubus_msg_ref(retmsg));
196 }
197
198 static void __init ubusd_proto_init(void)
199 {
200         blob_buf_init(&b, 0);
201         blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
202
203         retmsg = ubus_msg_from_blob(false);
204         if (!retmsg)
205                 exit(1);
206
207         retmsg->hdr.type = UBUS_MSG_STATUS;
208         retmsg_data = blob_data(blob_data(retmsg->data));
209 }