afb-xreq: integration of subcalls
[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 <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
28 #include <afb/afb-binding-v2.h>
29
30 #include "afb-context.h"
31 #include "afb-xreq.h"
32 #include "afb-evt.h"
33 #include "afb-msg-json.h"
34 #include "afb-cred.h"
35 #include "afb-hook.h"
36 #include "afb-api.h"
37 #include "afb-apiset.h"
38 #include "afb-auth.h"
39 #include "jobs.h"
40 #include "verbose.h"
41
42 /******************************************************************************/
43
44 static inline void xreq_addref(struct afb_xreq *xreq)
45 {
46         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
47 }
48
49 static inline void xreq_unref(struct afb_xreq *xreq)
50 {
51         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
52                 if (xreq->hookflags)
53                         afb_hook_xreq_end(xreq);
54                 xreq->queryitf->unref(xreq);
55         }
56 }
57
58 /******************************************************************************/
59
60 struct subcall
61 {
62         struct afb_xreq xreq;
63         struct afb_xreq *caller;
64         void (*callback)(void*, int, struct json_object*);
65         void *closure;
66         union {
67                 struct {
68                         struct jobloop *jobloop;
69                         struct json_object *result;
70                         int status;
71                 };
72                 struct {
73                                 void (*callback)(void*, int, struct json_object*);
74                                 void *closure;
75                 } hooked;
76         };
77 };
78
79 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
80 {
81         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
82
83         return afb_xreq_subscribe(subcall->caller, event);
84 }
85
86 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
87 {
88         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
89
90         return afb_xreq_unsubscribe(subcall->caller, event);
91 }
92
93 static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
94 {
95         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
96
97         if (subcall->callback)
98                 subcall->callback(subcall->closure, status, obj);
99         json_object_put(obj);
100 }
101
102 static void subcall_destroy(struct afb_xreq *xreq)
103 {
104         struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
105
106         json_object_put(subcall->xreq.json);
107         afb_cred_unref(subcall->xreq.cred);
108         xreq_unref(subcall->caller);
109         free(subcall);
110 }
111
112 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
113         .reply = subcall_reply,
114         .unref = subcall_destroy,
115         .subscribe = subcall_subscribe,
116         .unsubscribe = subcall_unsubscribe
117 };
118
119 static struct subcall *subcall_alloc(
120                 struct afb_xreq *caller,
121                 const char *api,
122                 const char *verb,
123                 struct json_object *args
124 )
125 {
126         struct subcall *subcall;
127         size_t lenapi, lenverb;
128         char *copy;
129
130         lenapi = 1 + strlen(api);
131         lenverb = 1 + strlen(verb);
132         subcall = malloc(lenapi + lenverb + sizeof *subcall);
133         if (!subcall)
134                 ERROR("out of memory");
135         else {
136                 copy = (char*)&subcall[1];
137                 memcpy(copy, api, lenapi);
138                 api = copy;
139                 copy = &copy[lenapi];
140                 memcpy(copy, verb, lenverb);
141                 verb = copy;
142
143                 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
144                 afb_context_subinit(&subcall->xreq.context, &caller->context);
145                 subcall->xreq.cred = afb_cred_addref(caller->cred);
146                 subcall->xreq.json = args;
147                 subcall->xreq.api = api;
148                 subcall->xreq.verb = verb;
149                 subcall->caller = caller;
150                 xreq_addref(caller);
151         }
152         return subcall;
153 }
154
155 static void subcall_process(struct subcall *subcall)
156 {
157         if (subcall->caller->queryitf->subcall) {
158                 subcall->caller->queryitf->subcall(
159                         subcall->caller, subcall->xreq.api, subcall->xreq.verb,
160                         subcall->xreq.json, subcall->callback, subcall->closure);
161                 xreq_unref(&subcall->xreq);
162         } else
163                 afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
164 }
165
166 static void subcall_sync_leave(struct subcall *subcall)
167 {
168         struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
169         if (jobloop)
170                 jobs_leave(jobloop);
171 }
172
173 static void subcall_sync_reply(void *closure, int status, struct json_object *obj)
174 {
175         struct subcall *subcall = closure;
176
177         subcall->status = status;
178         subcall->result = json_object_get(obj);
179         subcall_sync_leave(subcall);
180 }
181
182 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
183 {
184         struct subcall *subcall = closure;
185
186         if (!signum) {
187                 subcall->jobloop = jobloop;
188                 subcall_process(subcall);
189         } else {
190                 subcall->status = -1;
191                 subcall_sync_leave(subcall);
192         }
193 }
194
195 /******************************************************************************/
196
197 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
198 {
199         char *info;
200         if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
201                 info = NULL;
202         fun(first, second, info);
203         free(info);
204 }
205
206 /******************************************************************************/
207
208 static struct json_object *xreq_json_cb(void *closure)
209 {
210         struct afb_xreq *xreq = closure;
211         if (!xreq->json && xreq->queryitf->json)
212                 xreq->json = xreq->queryitf->json(xreq);
213         return xreq->json;
214 }
215
216 static struct afb_arg xreq_get_cb(void *closure, const char *name)
217 {
218         struct afb_xreq *xreq = closure;
219         struct afb_arg arg;
220         struct json_object *object, *value;
221
222         if (xreq->queryitf->get)
223                 arg = xreq->queryitf->get(xreq, name);
224         else {
225                 object = xreq_json_cb(closure);
226                 if (json_object_object_get_ex(object, name, &value)) {
227                         arg.name = name;
228                         arg.value = json_object_get_string(value);
229                 } else {
230                         arg.name = NULL;
231                         arg.value = NULL;
232                 }
233                 arg.path = NULL;
234         }
235         return arg;
236 }
237
238 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
239 {
240         struct afb_xreq *xreq = closure;
241
242         if (xreq->replied) {
243                 ERROR("reply called more than one time!!");
244                 json_object_put(obj);
245         } else {
246                 xreq->replied = 1;
247                 if (xreq->queryitf->success)
248                         xreq->queryitf->success(xreq, obj, info);
249                 else
250                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
251         }
252 }
253
254 static void xreq_fail_cb(void *closure, const char *status, const char *info)
255 {
256         struct afb_xreq *xreq = closure;
257
258         if (xreq->replied) {
259                 ERROR("reply called more than one time!!");
260         } else {
261                 xreq->replied = 1;
262                 if (xreq->queryitf->fail)
263                         xreq->queryitf->fail(xreq, status, info);
264                 else
265                         xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
266         }
267 }
268
269 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
270 {
271         vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
272 }
273
274 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
275 {
276         vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
277 }
278
279 static void *xreq_context_get_cb(void *closure)
280 {
281         struct afb_xreq *xreq = closure;
282         return afb_context_get(&xreq->context);
283 }
284
285 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
286 {
287         struct afb_xreq *xreq = closure;
288         afb_context_set(&xreq->context, value, free_value);
289 }
290
291 static void xreq_addref_cb(void *closure)
292 {
293         struct afb_xreq *xreq = closure;
294         xreq_addref(xreq);
295 }
296
297 static void xreq_unref_cb(void *closure)
298 {
299         struct afb_xreq *xreq = closure;
300         xreq_unref(xreq);
301 }
302
303 static void xreq_session_close_cb(void *closure)
304 {
305         struct afb_xreq *xreq = closure;
306         afb_context_close(&xreq->context);
307 }
308
309 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
310 {
311         struct afb_xreq *xreq = closure;
312         return afb_context_change_loa(&xreq->context, level);
313 }
314
315 static int xreq_subscribe_cb(void *closure, struct afb_event event)
316 {
317         struct afb_xreq *xreq = closure;
318         return afb_xreq_subscribe(xreq, event);
319 }
320
321 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
322 {
323         if (xreq->listener)
324                 return afb_evt_add_watch(xreq->listener, event);
325         if (xreq->queryitf->subscribe)
326                 return xreq->queryitf->subscribe(xreq, event);
327         ERROR("no event listener, subscription impossible");
328         errno = EINVAL;
329         return -1;
330 }
331
332 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
333 {
334         struct afb_xreq *xreq = closure;
335         return afb_xreq_unsubscribe(xreq, event);
336 }
337
338 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
339 {
340         if (xreq->listener)
341                 return afb_evt_remove_watch(xreq->listener, event);
342         if (xreq->queryitf->unsubscribe)
343                 return xreq->queryitf->unsubscribe(xreq, event);
344         ERROR("no event listener, unsubscription impossible");
345         errno = EINVAL;
346         return -1;
347 }
348
349 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)
350 {
351         struct afb_xreq *xreq = closure;
352         struct subcall *subcall;
353
354         subcall = subcall_alloc(xreq, api, verb, args);
355         if (subcall == NULL) {
356                 if (callback)
357                         callback(cb_closure, 1, afb_msg_json_internal_error());
358                 json_object_put(args);
359         } else {
360                 subcall->callback = callback;
361                 subcall->closure = cb_closure;
362                 subcall_process(subcall);
363         }
364 }
365
366 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
367 {
368         int rc;
369         struct subcall *subcall;
370         struct afb_xreq *xreq = closure;
371         struct json_object *resu;
372
373         subcall = subcall_alloc(xreq, api, verb, args);
374         if (!subcall) {
375                 rc = -1;
376                 resu = afb_msg_json_internal_error();
377                 json_object_put(args);
378         } else {
379                 subcall->callback = subcall_sync_reply;
380                 subcall->closure = subcall;
381                 subcall->jobloop = NULL;
382                 subcall->result = NULL;
383                 subcall->status = 0;
384                 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
385                 resu = subcall->result;
386                 if (rc < 0 || subcall->status < 0) {
387                         resu = resu ?: afb_msg_json_internal_error();
388                         rc = -1;
389                 }
390         }
391         if (result)
392                 *result = resu;
393         else
394                 json_object_put(resu);
395         return rc;
396 }
397
398 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
399 {
400         /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
401         vverbose(level, file, line, func, fmt, args);
402 }
403
404 static struct afb_stored_req *xreq_store_cb(void *closure)
405 {
406         xreq_addref_cb(closure);
407         return closure;
408 }
409
410 /******************************************************************************/
411
412 static struct json_object *xreq_hooked_json_cb(void *closure)
413 {
414         struct json_object *r = xreq_json_cb(closure);
415         struct afb_xreq *xreq = closure;
416         return afb_hook_xreq_json(xreq, r);
417 }
418
419 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
420 {
421         struct afb_arg r = xreq_get_cb(closure, name);
422         struct afb_xreq *xreq = closure;
423         return afb_hook_xreq_get(xreq, name, r);
424 }
425
426 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
427 {
428         struct afb_xreq *xreq = closure;
429         afb_hook_xreq_success(xreq, obj, info);
430         xreq_success_cb(closure, obj, info);
431 }
432
433 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
434 {
435         struct afb_xreq *xreq = closure;
436         afb_hook_xreq_fail(xreq, status, info);
437         xreq_fail_cb(closure, status, info);
438 }
439
440 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
441 {
442         vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
443 }
444
445 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
446 {
447         vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
448 }
449
450 static void *xreq_hooked_context_get_cb(void *closure)
451 {
452         void *r = xreq_context_get_cb(closure);
453         struct afb_xreq *xreq = closure;
454         return afb_hook_xreq_context_get(xreq, r);
455 }
456
457 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
458 {
459         struct afb_xreq *xreq = closure;
460         afb_hook_xreq_context_set(xreq, value, free_value);
461         xreq_context_set_cb(closure, value, free_value);
462 }
463
464 static void xreq_hooked_addref_cb(void *closure)
465 {
466         struct afb_xreq *xreq = closure;
467         afb_hook_xreq_addref(xreq);
468         xreq_addref_cb(closure);
469 }
470
471 static void xreq_hooked_unref_cb(void *closure)
472 {
473         struct afb_xreq *xreq = closure;
474         afb_hook_xreq_unref(xreq);
475         xreq_unref_cb(closure);
476 }
477
478 static void xreq_hooked_session_close_cb(void *closure)
479 {
480         struct afb_xreq *xreq = closure;
481         afb_hook_xreq_session_close(xreq);
482         xreq_session_close_cb(closure);
483 }
484
485 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
486 {
487         int r = xreq_session_set_LOA_cb(closure, level);
488         struct afb_xreq *xreq = closure;
489         return afb_hook_xreq_session_set_LOA(xreq, level, r);
490 }
491
492 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
493 {
494         int r = xreq_subscribe_cb(closure, event);
495         struct afb_xreq *xreq = closure;
496         return afb_hook_xreq_subscribe(xreq, event, r);
497 }
498
499 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
500 {
501         int r = xreq_unsubscribe_cb(closure, event);
502         struct afb_xreq *xreq = closure;
503         return afb_hook_xreq_unsubscribe(xreq, event, r);
504 }
505
506 static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
507 {
508         struct subcall *subcall = closure;
509         
510         afb_hook_xreq_subcall_result(subcall->caller, status, result);
511         subcall->hooked.callback(subcall->hooked.closure, status, result);
512 }
513
514 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)
515 {
516         struct afb_xreq *xreq = closure;
517         struct subcall *subcall;
518
519         afb_hook_xreq_subcall(xreq, api, verb, args);
520         subcall = subcall_alloc(xreq, api, verb, args);
521         if (subcall == NULL) {
522                 if (callback)
523                         callback(cb_closure, 1, afb_msg_json_internal_error());
524                 json_object_put(args);
525         } else {
526                 subcall->callback = xreq_hooked_subcall_reply_cb;
527                 subcall->closure = subcall;
528                 subcall->hooked.callback = callback;
529                 subcall->hooked.closure = cb_closure;
530                 subcall_process(subcall);
531         }
532 }
533
534 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
535 {
536         int r;
537         struct afb_xreq *xreq = closure;
538         afb_hook_xreq_subcallsync(xreq, api, verb, args);
539         r = xreq_subcallsync_cb(closure, api, verb, args, result);
540         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
541 }
542
543 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
544 {
545         struct afb_xreq *xreq = closure;
546         va_list ap;
547         va_copy(ap, args);
548         xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
549         afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
550         va_end(ap);
551 }
552
553 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
554 {
555         struct afb_xreq *xreq = closure;
556         struct afb_stored_req *r = xreq_store_cb(closure);
557         afb_hook_xreq_store(xreq, r);
558         return r;
559 }
560
561 /******************************************************************************/
562
563 const struct afb_req_itf xreq_itf = {
564         .json = xreq_json_cb,
565         .get = xreq_get_cb,
566         .success = xreq_success_cb,
567         .fail = xreq_fail_cb,
568         .vsuccess = xreq_vsuccess_cb,
569         .vfail = xreq_vfail_cb,
570         .context_get = xreq_context_get_cb,
571         .context_set = xreq_context_set_cb,
572         .addref = xreq_addref_cb,
573         .unref = xreq_unref_cb,
574         .session_close = xreq_session_close_cb,
575         .session_set_LOA = xreq_session_set_LOA_cb,
576         .subscribe = xreq_subscribe_cb,
577         .unsubscribe = xreq_unsubscribe_cb,
578         .subcall = xreq_subcall_cb,
579         .subcallsync = xreq_subcallsync_cb,
580         .vverbose = xreq_vverbose_cb,
581         .store = xreq_store_cb
582 };
583
584 const struct afb_req_itf xreq_hooked_itf = {
585         .json = xreq_hooked_json_cb,
586         .get = xreq_hooked_get_cb,
587         .success = xreq_hooked_success_cb,
588         .fail = xreq_hooked_fail_cb,
589         .vsuccess = xreq_hooked_vsuccess_cb,
590         .vfail = xreq_hooked_vfail_cb,
591         .context_get = xreq_hooked_context_get_cb,
592         .context_set = xreq_hooked_context_set_cb,
593         .addref = xreq_hooked_addref_cb,
594         .unref = xreq_hooked_unref_cb,
595         .session_close = xreq_hooked_session_close_cb,
596         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
597         .subscribe = xreq_hooked_subscribe_cb,
598         .unsubscribe = xreq_hooked_unsubscribe_cb,
599         .subcall = xreq_hooked_subcall_cb,
600         .subcallsync = xreq_hooked_subcallsync_cb,
601         .vverbose = xreq_hooked_vverbose_cb,
602         .store = xreq_hooked_store_cb
603 };
604
605 static inline struct afb_req to_req(struct afb_xreq *xreq)
606 {
607         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
608 }
609
610 /******************************************************************************/
611
612 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
613 {
614         struct afb_xreq *xreq = (struct afb_xreq *)sreq;
615         if (xreq->hookflags)
616                 afb_hook_xreq_unstore(xreq);
617         return to_req(xreq);
618 }
619
620 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
621 {
622         return afb_req_json(to_req(xreq));
623 }
624
625 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
626 {
627         afb_req_success(to_req(xreq), obj, info);
628 }
629
630 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
631 {
632         char *message;
633         va_list args;
634         va_start(args, info);
635         if (info == NULL || vasprintf(&message, info, args) < 0)
636                 message = NULL;
637         va_end(args);
638         afb_xreq_success(xreq, obj, message);
639         free(message);
640 }
641
642 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
643 {
644         afb_req_fail(to_req(xreq), status, info);
645 }
646
647 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
648 {
649         char *message;
650         va_list args;
651         va_start(args, info);
652         if (info == NULL || vasprintf(&message, info, args) < 0)
653                 message = NULL;
654         va_end(args);
655         afb_xreq_fail(xreq, status, message);
656         free(message);
657 }
658
659 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
660 {
661         struct json_object *obj = xreq_json_cb(xreq);
662         const char *result = json_object_to_json_string(obj);
663         if (size != NULL)
664                 *size = strlen(result);
665         return result;
666 }
667
668 void afb_xreq_addref(struct afb_xreq *xreq)
669 {
670         afb_req_addref(to_req(xreq));
671 }
672
673 void afb_xreq_unref(struct afb_xreq *xreq)
674 {
675         afb_req_unref(to_req(xreq));
676 }
677
678 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)
679 {
680         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
681 }
682
683 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)
684 {
685         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
686 }
687
688 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
689 {
690         return xreq_subcallsync_cb(xreq, api, verb, args, result);
691 }
692
693 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
694 {
695         return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
696 }
697
698 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
699 {
700         int loa;
701
702         if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
703                 if (!afb_context_check(&xreq->context)) {
704                         afb_context_close(&xreq->context);
705                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
706                         errno = EINVAL;
707                         return -1;
708                 }
709         }
710
711         if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
712                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
713                 if (!afb_context_check_loa(&xreq->context, loa)) {
714                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
715                         errno = EPERM;
716                         return -1;
717                 }
718         }
719
720         if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
721                 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
722                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
723                         afb_xreq_fail_f(xreq, "denied", "invalid LOA");
724                         errno = EPERM;
725                         return -1;
726                 }
727         }
728
729         if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
730                 afb_context_refresh(&xreq->context);
731         }
732         if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
733                 afb_context_change_loa(&xreq->context, 0);
734                 afb_context_close(&xreq->context);
735         }
736
737         return 0;
738 }
739
740 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
741 {
742         int loa;
743
744         if (sessionflags != 0) {
745                 if (!afb_context_check(&xreq->context)) {
746                         afb_context_close(&xreq->context);
747                         afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
748                         errno = EINVAL;
749                         return -1;
750                 }
751         }
752
753         loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
754         if (loa && !afb_context_check_loa(&xreq->context, loa)) {
755                 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
756                 errno = EPERM;
757                 return -1;
758         }
759
760         if (auth && !afb_auth_check(auth, xreq)) {
761                 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
762                 errno = EPERM;
763                 return -1;
764         }
765
766         if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
767                 afb_context_refresh(&xreq->context);
768         }
769         if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
770                 afb_context_close(&xreq->context);
771         }
772
773         return 0;
774 }
775
776 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
777 {
778         if (!verb)
779                 afb_xreq_fail_unknown_verb(xreq);
780         else
781                 if (!xreq_session_check_apply_v1(xreq, verb->session))
782                         verb->callback(to_req(xreq));
783 }
784
785 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
786 {
787         if (!verb)
788                 afb_xreq_fail_unknown_verb(xreq);
789         else
790                 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
791                         verb->callback(to_req(xreq));
792 }
793
794 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
795 {
796         memset(xreq, 0, sizeof *xreq);
797         xreq->refcount = 1;
798         xreq->queryitf = queryitf;
799 }
800
801 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
802 {
803         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
804 }
805
806 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
807 {
808         afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
809 }
810
811 static void process_sync(struct afb_xreq *xreq)
812 {
813         struct afb_api api;
814
815         /* init hooking */
816         afb_hook_init_xreq(xreq);
817         if (xreq->hookflags)
818                 afb_hook_xreq_begin(xreq);
819
820         /* search the api */
821         if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
822                 if (errno == ENOENT)
823                         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
824                 else
825                         afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
826         } else {
827                 xreq->context.api_key = api.closure;
828                 api.itf->call(api.closure, xreq);
829         }
830 }
831
832 static void process_async(int signum, void *arg)
833 {
834         struct afb_xreq *xreq = arg;
835
836         if (signum != 0) {
837                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
838         } else {
839                 process_sync(xreq);
840         }
841         xreq_unref(xreq);
842 }
843
844 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
845 {
846         xreq->apiset = apiset;
847
848         xreq_addref(xreq);
849         if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
850                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
851                 ERROR("can't process job with threads: %m");
852                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
853                 xreq_unref(xreq);
854         }
855         xreq_unref(xreq);
856 }
857