Prepare bindings version 2
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <json-c/json.h>
26 #include <afb/afb-binding.h>
27
28 #include "afb-context.h"
29 #include "afb-xreq.h"
30 #include "afb-evt.h"
31 #include "afb-msg-json.h"
32 #include "afb-subcall.h"
33 #include "afb-hook.h"
34 #include "afb-api.h"
35 #include "afb-apiset.h"
36 #include "jobs.h"
37 #include "verbose.h"
38
39
40 static struct json_object *xreq_json_cb(void *closure)
41 {
42         struct afb_xreq *xreq = closure;
43         return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
44 }
45
46 static struct afb_arg xreq_get_cb(void *closure, const char *name)
47 {
48         struct afb_xreq *xreq = closure;
49         if (xreq->queryitf->get)
50                 return xreq->queryitf->get(xreq, name);
51         else
52                 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
53 }
54
55 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
56 {
57         struct afb_xreq *xreq = closure;
58
59         if (xreq->replied) {
60                 ERROR("reply called more than one time!!");
61                 json_object_put(obj);
62         } else {
63                 xreq->replied = 1;
64                 if (xreq->queryitf->success)
65                         xreq->queryitf->success(xreq, obj, info);
66                 else
67                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
68         }
69 }
70
71 static void xreq_fail_cb(void *closure, const char *status, const char *info)
72 {
73         struct afb_xreq *xreq = closure;
74
75         if (xreq->replied) {
76                 ERROR("reply called more than one time!!");
77         } else {
78                 xreq->replied = 1;
79                 if (xreq->queryitf->fail)
80                         xreq->queryitf->fail(xreq, status, info);
81                 else
82                         xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
83         }
84 }
85
86 static const char *xreq_raw_cb(void *closure, size_t *size)
87 {
88         struct afb_xreq *xreq = closure;
89         const char *result = json_object_to_json_string(xreq_json_cb(xreq));
90         if (size != NULL)
91                 *size = strlen(result);
92         return result;
93 }
94
95 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
96 {
97         struct json_object *obj = json_tokener_parse(buffer);
98         if (!obj == !buffer)
99                 xreq_success_cb(closure, obj, "fake send");
100         else
101                 xreq_fail_cb(closure, "fake-send-failed", "fake send");
102 }
103
104 static void *xreq_context_get_cb(void *closure)
105 {
106         struct afb_xreq *xreq = closure;
107         return afb_context_get(&xreq->context);
108 }
109
110 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
111 {
112         struct afb_xreq *xreq = closure;
113         afb_context_set(&xreq->context, value, free_value);
114 }
115
116 static void xreq_addref_cb(void *closure)
117 {
118         struct afb_xreq *xreq = closure;
119         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
120 }
121
122 static void xreq_unref_cb(void *closure)
123 {
124         struct afb_xreq *xreq = closure;
125         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
126                 xreq->queryitf->unref(xreq);
127         }
128 }
129
130 static void xreq_session_close_cb(void *closure)
131 {
132         struct afb_xreq *xreq = closure;
133         afb_context_close(&xreq->context);
134 }
135
136 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
137 {
138         struct afb_xreq *xreq = closure;
139         return afb_context_change_loa(&xreq->context, level);
140 }
141
142 static int xreq_subscribe_cb(void *closure, struct afb_event event)
143 {
144         struct afb_xreq *xreq = closure;
145         return afb_xreq_subscribe(xreq, event);
146 }
147
148 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
149 {
150         if (xreq->listener)
151                 return afb_evt_add_watch(xreq->listener, event);
152         if (xreq->queryitf->subscribe)
153                 return xreq->queryitf->subscribe(xreq, event);
154         ERROR("no event listener, subscription impossible");
155         errno = EINVAL;
156         return -1;
157 }
158
159 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
160 {
161         struct afb_xreq *xreq = closure;
162         return afb_xreq_unsubscribe(xreq, event);
163 }
164
165 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
166 {
167         if (xreq->listener)
168                 return afb_evt_remove_watch(xreq->listener, event);
169         if (xreq->queryitf->unsubscribe)
170                 return xreq->queryitf->unsubscribe(xreq, event);
171         ERROR("no event listener, unsubscription impossible");
172         errno = EINVAL;
173         return -1;
174 }
175
176 static void xreq_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
177 {
178         struct afb_xreq *xreq = closure;
179
180         if (xreq->queryitf->subcall)
181                 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
182         else
183                 afb_subcall(xreq, api, verb, args, callback, cb_closure);
184 }
185
186 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
187 {
188         struct afb_xreq *xreq = closure;
189         return afb_subcall_sync(xreq, api, verb, args, result);
190 }
191
192 static struct json_object *xreq_hooked_json_cb(void *closure)
193 {
194         struct json_object *r = xreq_json_cb(closure);
195         struct afb_xreq *xreq = closure;
196         return afb_hook_xreq_json(xreq, r);
197 }
198
199 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
200 {
201         struct afb_arg r = xreq_get_cb(closure, name);
202         struct afb_xreq *xreq = closure;
203         return afb_hook_xreq_get(xreq, name, r);
204 }
205
206 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
207 {
208         struct afb_xreq *xreq = closure;
209         afb_hook_xreq_success(xreq, obj, info);
210         xreq_success_cb(closure, obj, info);
211 }
212
213 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
214 {
215         struct afb_xreq *xreq = closure;
216         afb_hook_xreq_fail(xreq, status, info);
217         xreq_fail_cb(closure, status, info);
218 }
219
220 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
221 {
222         size_t s;
223         const char *r = xreq_raw_cb(closure, size ? : &s);
224         struct afb_xreq *xreq = closure;
225         return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
226 }
227
228 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
229 {
230         struct afb_xreq *xreq = closure;
231         afb_hook_xreq_send(xreq, buffer, size);
232         xreq_send_cb(closure, buffer, size);
233 }
234
235 static void *xreq_hooked_context_get_cb(void *closure)
236 {
237         void *r = xreq_context_get_cb(closure);
238         struct afb_xreq *xreq = closure;
239         return afb_hook_xreq_context_get(xreq, r);
240 }
241
242 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
243 {
244         struct afb_xreq *xreq = closure;
245         afb_hook_xreq_context_set(xreq, value, free_value);
246         xreq_context_set_cb(closure, value, free_value);
247 }
248
249 static void xreq_hooked_addref_cb(void *closure)
250 {
251         struct afb_xreq *xreq = closure;
252         afb_hook_xreq_addref(xreq);
253         xreq_addref_cb(closure);
254 }
255
256 static void xreq_hooked_unref_cb(void *closure)
257 {
258         struct afb_xreq *xreq = closure;
259         afb_hook_xreq_unref(xreq);
260         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
261                 afb_hook_xreq_end(xreq);
262                 xreq->queryitf->unref(xreq);
263         }
264 }
265
266 static void xreq_hooked_session_close_cb(void *closure)
267 {
268         struct afb_xreq *xreq = closure;
269         afb_hook_xreq_session_close(xreq);
270         xreq_session_close_cb(closure);
271 }
272
273 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
274 {
275         int r = xreq_session_set_LOA_cb(closure, level);
276         struct afb_xreq *xreq = closure;
277         return afb_hook_xreq_session_set_LOA(xreq, level, r);
278 }
279
280 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
281 {
282         int r = xreq_subscribe_cb(closure, event);
283         struct afb_xreq *xreq = closure;
284         return afb_hook_xreq_subscribe(xreq, event, r);
285 }
286
287 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
288 {
289         int r = xreq_unsubscribe_cb(closure, event);
290         struct afb_xreq *xreq = closure;
291         return afb_hook_xreq_unsubscribe(xreq, event, r);
292 }
293
294 struct reply
295 {
296         struct afb_xreq *xreq;
297         void (*callback)(void*, int, struct json_object*);
298         void *closure;
299 };
300
301 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
302 {
303         struct reply *reply = closure;
304         
305         afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
306         reply->callback(reply->closure, iserror, result);
307         free(reply);
308 }
309
310 static void xreq_hooked_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
311 {
312         struct reply *reply = malloc(sizeof *reply);
313         struct afb_xreq *xreq = closure;
314         afb_hook_xreq_subcall(xreq, api, verb, args);
315         if (reply) {
316                 reply->xreq = xreq;
317                 reply->callback = callback;
318                 reply->closure = cb_closure;
319                 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
320         } else {
321                 ERROR("out of memory");
322                 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
323         }
324 }
325
326 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
327 {
328         int r;
329         struct afb_xreq *xreq = closure;
330         afb_hook_xreq_subcallsync(xreq, api, verb, args);
331         r = xreq_subcallsync_cb(closure, api, verb, args, result);
332         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
333 }
334
335 const struct afb_req_itf xreq_itf = {
336         .json = xreq_json_cb,
337         .get = xreq_get_cb,
338         .success = xreq_success_cb,
339         .fail = xreq_fail_cb,
340         .raw = xreq_raw_cb,
341         .send = xreq_send_cb,
342         .context_get = xreq_context_get_cb,
343         .context_set = xreq_context_set_cb,
344         .addref = xreq_addref_cb,
345         .unref = xreq_unref_cb,
346         .session_close = xreq_session_close_cb,
347         .session_set_LOA = xreq_session_set_LOA_cb,
348         .subscribe = xreq_subscribe_cb,
349         .unsubscribe = xreq_unsubscribe_cb,
350         .subcall = xreq_subcall_cb,
351         .subcallsync = xreq_subcallsync_cb
352 };
353
354 const struct afb_req_itf xreq_hooked_itf = {
355         .json = xreq_hooked_json_cb,
356         .get = xreq_hooked_get_cb,
357         .success = xreq_hooked_success_cb,
358         .fail = xreq_hooked_fail_cb,
359         .raw = xreq_hooked_raw_cb,
360         .send = xreq_hooked_send_cb,
361         .context_get = xreq_hooked_context_get_cb,
362         .context_set = xreq_hooked_context_set_cb,
363         .addref = xreq_hooked_addref_cb,
364         .unref = xreq_hooked_unref_cb,
365         .session_close = xreq_hooked_session_close_cb,
366         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
367         .subscribe = xreq_hooked_subscribe_cb,
368         .unsubscribe = xreq_hooked_unsubscribe_cb,
369         .subcall = xreq_hooked_subcall_cb,
370         .subcallsync = xreq_hooked_subcallsync_cb
371 };
372
373 static inline struct afb_req to_req(struct afb_xreq *xreq)
374 {
375         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
376 }
377
378 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
379 {
380         return afb_req_json(to_req(xreq));
381 }
382
383 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
384 {
385         afb_req_success(to_req(xreq), obj, info);
386 }
387
388 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
389 {
390         char *message;
391         va_list args;
392         va_start(args, info);
393         if (info == NULL || vasprintf(&message, info, args) < 0)
394                 message = NULL;
395         va_end(args);
396         afb_xreq_success(xreq, obj, message);
397         free(message);
398 }
399
400 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
401 {
402         afb_req_fail(to_req(xreq), status, info);
403 }
404
405 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
406 {
407         char *message;
408         va_list args;
409         va_start(args, info);
410         if (info == NULL || vasprintf(&message, info, args) < 0)
411                 message = NULL;
412         va_end(args);
413         afb_xreq_fail(xreq, status, message);
414         free(message);
415 }
416
417 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
418 {
419         return afb_req_raw(to_req(xreq), size);
420 }
421
422 void afb_xreq_addref(struct afb_xreq *xreq)
423 {
424         afb_req_addref(to_req(xreq));
425 }
426
427 void afb_xreq_unref(struct afb_xreq *xreq)
428 {
429         afb_req_unref(to_req(xreq));
430 }
431
432 void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
433 {
434         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
435 }
436
437 void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
438 {
439         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
440 }
441
442 static int xreq_session_check_apply(struct afb_xreq *xreq, int sessionflags)
443 {
444         int loa;
445
446         if ((sessionflags & (AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
447                 if (!afb_context_check(&xreq->context)) {
448                         afb_context_close(&xreq->context);
449                         afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
450                         errno = EINVAL;
451                         return -1;
452                 }
453         }
454
455         if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
456                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
457                 if (!afb_context_check_loa(&xreq->context, loa)) {
458                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
459                         errno = EPERM;
460                         return -1;
461                 }
462         }
463
464         if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
465                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
466                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
467                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
468                         errno = EPERM;
469                         return -1;
470                 }
471         }
472
473         if ((sessionflags & AFB_SESSION_RENEW) != 0) {
474                 afb_context_refresh(&xreq->context);
475         }
476         if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
477                 afb_context_change_loa(&xreq->context, 0);
478                 afb_context_close(&xreq->context);
479         }
480
481         return 0;
482 }
483
484 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
485 {
486         if (!verb)
487                 afb_xreq_fail_unknown_verb(xreq);
488         else
489                 if (!xreq_session_check_apply(xreq, verb->session))
490                         verb->callback(to_req(xreq));
491 }
492
493 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
494 {
495         if (!verb)
496                 afb_xreq_fail_unknown_verb(xreq);
497         else
498                 if (!xreq_session_check_apply(xreq, verb->session))
499                         verb->callback(to_req(xreq));
500 }
501
502 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
503 {
504         memset(xreq, 0, sizeof *xreq);
505         xreq->refcount = 1;
506         xreq->queryitf = queryitf;
507 }
508
509 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
510 {
511         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
512 }
513
514 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
515 {
516         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
517 }
518
519 static void process_async(int signum, void *arg)
520 {
521         struct afb_xreq *xreq = arg;
522         struct afb_api api;
523
524         if (signum != 0) {
525                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
526         } else {
527                 /* init hooking */
528                 afb_hook_init_xreq(xreq);
529                 if (xreq->hookflags)
530                         afb_hook_xreq_begin(xreq);
531
532                 /* search the api */
533                 if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
534                         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
535                 } else {
536                         xreq->context.api_key = api.closure;
537                         api.itf->call(api.closure, xreq);
538                 }
539         }
540         afb_xreq_unref(xreq);
541 }
542
543 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
544 {
545         xreq->apiset = apiset;
546
547         afb_xreq_addref(xreq);
548         if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
549                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
550                 ERROR("can't process job with threads: %m");
551                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
552                 afb_xreq_unref(xreq);
553         }
554         afb_xreq_unref(xreq);
555 }
556