2 * Copyright (C) 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <json-c/json.h>
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
29 #include "afb-context.h"
32 #include "afb-msg-json.h"
36 #include "afb-apiset.h"
41 /******************************************************************************/
43 static void xreq_finalize(struct afb_xreq *xreq)
46 afb_xreq_fail(xreq, "error", "no reply");
48 afb_hook_xreq_end(xreq);
50 afb_xreq_unhooked_unref(xreq->caller);
51 xreq->queryitf->unref(xreq);
54 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
56 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
59 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
61 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
65 /******************************************************************************/
67 static inline struct afb_req to_req(struct afb_xreq *xreq)
69 return (struct afb_req){ .itf = xreq->itf, .closure = xreq };
72 /******************************************************************************/
78 void (*completion)(struct subcall*, int, struct json_object*);
82 struct jobloop *jobloop;
83 struct json_object *result;
88 void (*callback)(void*, int, struct json_object*);
89 void (*callback2)(void*, int, struct json_object*, struct afb_req);
96 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
98 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
100 return afb_xreq_subscribe(subcall->xreq.caller, event);
103 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
105 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
107 return afb_xreq_unsubscribe(subcall->xreq.caller, event);
110 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
112 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
114 subcall->completion(subcall, status, result);
115 json_object_put(result);
116 afb_xreq_unhooked_unref(&subcall->xreq);
119 static void subcall_destroy_cb(struct afb_xreq *xreq)
121 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
123 json_object_put(subcall->xreq.json);
124 afb_cred_unref(subcall->xreq.cred);
128 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
129 .reply = subcall_reply_cb,
130 .unref = subcall_destroy_cb,
131 .subscribe = subcall_subscribe_cb,
132 .unsubscribe = subcall_unsubscribe_cb
135 static struct subcall *subcall_alloc(
136 struct afb_xreq *caller,
139 struct json_object *args
142 struct subcall *subcall;
143 size_t lenapi, lenverb;
146 lenapi = 1 + strlen(api);
147 lenverb = 1 + strlen(verb);
148 subcall = malloc(lenapi + lenverb + sizeof *subcall);
150 ERROR("out of memory");
152 copy = (char*)&subcall[1];
153 memcpy(copy, api, lenapi);
155 copy = ©[lenapi];
156 memcpy(copy, verb, lenverb);
159 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
160 afb_context_subinit(&subcall->xreq.context, &caller->context);
161 subcall->xreq.cred = afb_cred_addref(caller->cred);
162 subcall->xreq.json = args;
163 subcall->xreq.api = api;
164 subcall->xreq.verb = verb;
165 subcall->xreq.caller = caller;
166 afb_xreq_unhooked_addref(caller);
172 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
174 subcall->callback(subcall->closure, status, result);
177 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
179 subcall->callback2(subcall->closure, status, result, to_req(subcall->xreq.caller));
182 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
184 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
185 subcall_on_reply(subcall, status, result);
188 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
190 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
191 subcall_req_on_reply(subcall, status, result);
194 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
196 struct afb_xreq *xreq = closure;
199 ERROR("subcall replied more than one time!!");
200 json_object_put(result);
203 subcall_reply_cb(xreq, status, result);
207 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
209 subcall->completion = completion;
210 if (subcall->xreq.caller->queryitf->subcall) {
211 subcall->xreq.caller->queryitf->subcall(
212 subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb,
213 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
215 afb_xreq_unhooked_addref(&subcall->xreq);
216 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
220 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
222 subcall->callback = callback;
223 subcall->closure = cb_closure;
224 subcall_process(subcall, subcall_on_reply);
227 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
229 subcall->callback2 = callback;
230 subcall->closure = cb_closure;
231 subcall_process(subcall, subcall_req_on_reply);
234 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
236 subcall->callback = callback;
237 subcall->closure = cb_closure;
238 subcall_process(subcall, subcall_hooked_on_reply);
241 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
243 subcall->callback2 = callback;
244 subcall->closure = cb_closure;
245 subcall_process(subcall, subcall_req_hooked_on_reply);
248 static void subcall_sync_leave(struct subcall *subcall)
250 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
255 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
257 subcall->status = status;
258 subcall->result = json_object_get(result);
259 subcall_sync_leave(subcall);
262 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
264 struct subcall *subcall = closure;
267 subcall->jobloop = jobloop;
268 subcall->result = NULL;
270 subcall_process(subcall, subcall_sync_reply);
272 subcall->status = -1;
273 subcall_sync_leave(subcall);
277 static int subcallsync(struct subcall *subcall, struct json_object **result)
281 afb_xreq_unhooked_addref(&subcall->xreq);
282 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
283 *result = subcall->result;
284 if (rc < 0 || subcall->status < 0) {
285 *result = *result ?: afb_msg_json_internal_error();
288 afb_xreq_unhooked_unref(&subcall->xreq);
292 /******************************************************************************/
294 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
297 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
299 fun(first, second, info);
303 /******************************************************************************/
305 static struct json_object *xreq_json_cb(void *closure)
307 struct afb_xreq *xreq = closure;
308 if (!xreq->json && xreq->queryitf->json)
309 xreq->json = xreq->queryitf->json(xreq);
313 static struct afb_arg xreq_get_cb(void *closure, const char *name)
315 struct afb_xreq *xreq = closure;
317 struct json_object *object, *value;
319 if (xreq->queryitf->get)
320 arg = xreq->queryitf->get(xreq, name);
322 object = xreq_json_cb(closure);
323 if (json_object_object_get_ex(object, name, &value)) {
325 arg.value = json_object_get_string(value);
335 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
337 struct afb_xreq *xreq = closure;
340 ERROR("reply called more than one time!!");
341 json_object_put(obj);
344 if (xreq->queryitf->success)
345 xreq->queryitf->success(xreq, obj, info);
347 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
351 static void xreq_fail_cb(void *closure, const char *status, const char *info)
353 struct afb_xreq *xreq = closure;
356 ERROR("reply called more than one time!!");
359 if (xreq->queryitf->fail)
360 xreq->queryitf->fail(xreq, status, info);
362 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
366 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
368 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
371 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
373 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
376 static void *xreq_context_get_cb(void *closure)
378 struct afb_xreq *xreq = closure;
379 return afb_context_get(&xreq->context);
382 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
384 struct afb_xreq *xreq = closure;
385 afb_context_set(&xreq->context, value, free_value);
388 static void xreq_addref_cb(void *closure)
390 struct afb_xreq *xreq = closure;
391 afb_xreq_unhooked_addref(xreq);
394 static void xreq_unref_cb(void *closure)
396 struct afb_xreq *xreq = closure;
397 afb_xreq_unhooked_unref(xreq);
400 static void xreq_session_close_cb(void *closure)
402 struct afb_xreq *xreq = closure;
403 afb_context_close(&xreq->context);
406 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
408 struct afb_xreq *xreq = closure;
409 return afb_context_change_loa(&xreq->context, level);
412 static int xreq_subscribe_cb(void *closure, struct afb_event event)
414 struct afb_xreq *xreq = closure;
415 return afb_xreq_subscribe(xreq, event);
418 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
421 return afb_evt_add_watch(xreq->listener, event);
422 if (xreq->queryitf->subscribe)
423 return xreq->queryitf->subscribe(xreq, event);
424 ERROR("no event listener, subscription impossible");
429 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
431 struct afb_xreq *xreq = closure;
432 return afb_xreq_unsubscribe(xreq, event);
435 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
438 return afb_evt_remove_watch(xreq->listener, event);
439 if (xreq->queryitf->unsubscribe)
440 return xreq->queryitf->unsubscribe(xreq, event);
441 ERROR("no event listener, unsubscription impossible");
446 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)
448 struct afb_xreq *xreq = closure;
451 sc = subcall_alloc(xreq, api, verb, args);
454 callback(cb_closure, 1, afb_msg_json_internal_error());
455 json_object_put(args);
457 subcall(sc, callback, cb_closure);
461 static void xreq_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
463 struct afb_xreq *xreq = closure;
466 sc = subcall_alloc(xreq, api, verb, args);
469 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
470 json_object_put(args);
472 subcall_req(sc, callback, cb_closure);
477 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
481 struct afb_xreq *xreq = closure;
482 struct json_object *resu;
484 sc = subcall_alloc(xreq, api, verb, args);
487 resu = afb_msg_json_internal_error();
488 json_object_put(args);
490 rc = subcallsync(sc, &resu);
495 json_object_put(resu);
499 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
502 struct afb_xreq *xreq = closure;
504 if (!fmt || vasprintf(&p, fmt, args) < 0)
505 vverbose(level, file, line, func, fmt, args);
507 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
512 static struct afb_stored_req *xreq_store_cb(void *closure)
514 xreq_addref_cb(closure);
518 static int xreq_has_permission_cb(void*closure, const char *permission)
520 struct afb_xreq *xreq = closure;
521 return afb_auth_has_permission(xreq, permission);
524 static char *xreq_get_application_id_cb(void*closure)
526 struct afb_xreq *xreq = closure;
527 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
530 /******************************************************************************/
532 static struct json_object *xreq_hooked_json_cb(void *closure)
534 struct json_object *r = xreq_json_cb(closure);
535 struct afb_xreq *xreq = closure;
536 return afb_hook_xreq_json(xreq, r);
539 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
541 struct afb_arg r = xreq_get_cb(closure, name);
542 struct afb_xreq *xreq = closure;
543 return afb_hook_xreq_get(xreq, name, r);
546 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
548 struct afb_xreq *xreq = closure;
549 afb_hook_xreq_success(xreq, obj, info);
550 xreq_success_cb(closure, obj, info);
553 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
555 struct afb_xreq *xreq = closure;
556 afb_hook_xreq_fail(xreq, status, info);
557 xreq_fail_cb(closure, status, info);
560 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
562 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
565 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
567 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
570 static void *xreq_hooked_context_get_cb(void *closure)
572 void *r = xreq_context_get_cb(closure);
573 struct afb_xreq *xreq = closure;
574 return afb_hook_xreq_context_get(xreq, r);
577 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
579 struct afb_xreq *xreq = closure;
580 afb_hook_xreq_context_set(xreq, value, free_value);
581 xreq_context_set_cb(closure, value, free_value);
584 static void xreq_hooked_addref_cb(void *closure)
586 struct afb_xreq *xreq = closure;
587 afb_hook_xreq_addref(xreq);
588 xreq_addref_cb(closure);
591 static void xreq_hooked_unref_cb(void *closure)
593 struct afb_xreq *xreq = closure;
594 afb_hook_xreq_unref(xreq);
595 xreq_unref_cb(closure);
598 static void xreq_hooked_session_close_cb(void *closure)
600 struct afb_xreq *xreq = closure;
601 afb_hook_xreq_session_close(xreq);
602 xreq_session_close_cb(closure);
605 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
607 int r = xreq_session_set_LOA_cb(closure, level);
608 struct afb_xreq *xreq = closure;
609 return afb_hook_xreq_session_set_LOA(xreq, level, r);
612 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
614 int r = xreq_subscribe_cb(closure, event);
615 struct afb_xreq *xreq = closure;
616 return afb_hook_xreq_subscribe(xreq, event, r);
619 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
621 int r = xreq_unsubscribe_cb(closure, event);
622 struct afb_xreq *xreq = closure;
623 return afb_hook_xreq_unsubscribe(xreq, event, r);
626 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)
628 struct afb_xreq *xreq = closure;
631 afb_hook_xreq_subcall(xreq, api, verb, args);
632 sc = subcall_alloc(xreq, api, verb, args);
635 callback(cb_closure, 1, afb_msg_json_internal_error());
636 json_object_put(args);
638 subcall_hooked(sc, callback, cb_closure);
642 static void xreq_hooked_subcall_req_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
644 struct afb_xreq *xreq = closure;
647 afb_hook_xreq_subcall_req(xreq, api, verb, args);
648 sc = subcall_alloc(xreq, api, verb, args);
651 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
652 json_object_put(args);
654 subcall_req_hooked(sc, callback, cb_closure);
658 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
661 struct afb_xreq *xreq = closure;
662 afb_hook_xreq_subcallsync(xreq, api, verb, args);
663 r = xreq_subcallsync_cb(closure, api, verb, args, result);
664 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
667 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
669 struct afb_xreq *xreq = closure;
672 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
673 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
677 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
679 struct afb_xreq *xreq = closure;
680 struct afb_stored_req *r = xreq_store_cb(closure);
681 afb_hook_xreq_store(xreq, r);
685 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
687 struct afb_xreq *xreq = closure;
688 int r = xreq_has_permission_cb(closure, permission);
689 return afb_hook_xreq_has_permission(xreq, permission, r);
692 static char *xreq_hooked_get_application_id_cb(void*closure)
694 struct afb_xreq *xreq = closure;
695 char *r = xreq_get_application_id_cb(closure);
696 return afb_hook_xreq_get_application_id(xreq, r);
699 /******************************************************************************/
701 const struct afb_req_itf xreq_itf = {
702 .json = xreq_json_cb,
704 .success = xreq_success_cb,
705 .fail = xreq_fail_cb,
706 .vsuccess = xreq_vsuccess_cb,
707 .vfail = xreq_vfail_cb,
708 .context_get = xreq_context_get_cb,
709 .context_set = xreq_context_set_cb,
710 .addref = xreq_addref_cb,
711 .unref = xreq_unref_cb,
712 .session_close = xreq_session_close_cb,
713 .session_set_LOA = xreq_session_set_LOA_cb,
714 .subscribe = xreq_subscribe_cb,
715 .unsubscribe = xreq_unsubscribe_cb,
716 .subcall = xreq_subcall_cb,
717 .subcallsync = xreq_subcallsync_cb,
718 .vverbose = xreq_vverbose_cb,
719 .store = xreq_store_cb,
720 .subcall_req = xreq_subcall_req_cb,
721 .has_permission = xreq_has_permission_cb,
722 .get_application_id = xreq_get_application_id_cb
725 const struct afb_req_itf xreq_hooked_itf = {
726 .json = xreq_hooked_json_cb,
727 .get = xreq_hooked_get_cb,
728 .success = xreq_hooked_success_cb,
729 .fail = xreq_hooked_fail_cb,
730 .vsuccess = xreq_hooked_vsuccess_cb,
731 .vfail = xreq_hooked_vfail_cb,
732 .context_get = xreq_hooked_context_get_cb,
733 .context_set = xreq_hooked_context_set_cb,
734 .addref = xreq_hooked_addref_cb,
735 .unref = xreq_hooked_unref_cb,
736 .session_close = xreq_hooked_session_close_cb,
737 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
738 .subscribe = xreq_hooked_subscribe_cb,
739 .unsubscribe = xreq_hooked_unsubscribe_cb,
740 .subcall = xreq_hooked_subcall_cb,
741 .subcallsync = xreq_hooked_subcallsync_cb,
742 .vverbose = xreq_hooked_vverbose_cb,
743 .store = xreq_hooked_store_cb,
744 .subcall_req = xreq_hooked_subcall_req_cb,
745 .has_permission = xreq_hooked_has_permission_cb,
746 .get_application_id = xreq_hooked_get_application_id_cb
749 /******************************************************************************/
751 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
753 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
755 afb_hook_xreq_unstore(xreq);
759 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
761 return afb_req_json(to_req(xreq));
764 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
766 afb_req_success(to_req(xreq), obj, info);
769 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
773 va_start(args, info);
774 afb_req_success_v(to_req(xreq), obj, info, args);
778 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
780 afb_req_fail(to_req(xreq), status, info);
783 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
787 va_start(args, info);
788 afb_req_fail_v(to_req(xreq), status, info, args);
793 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
795 struct json_object *obj = xreq_json_cb(xreq);
796 const char *result = json_object_to_json_string(obj);
798 *size = strlen(result);
802 void afb_xreq_addref(struct afb_xreq *xreq)
804 afb_req_addref(to_req(xreq));
807 void afb_xreq_unref(struct afb_xreq *xreq)
809 afb_req_unref(to_req(xreq));
812 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)
814 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
817 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)
819 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
822 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
824 return xreq_subcallsync_cb(xreq, api, verb, args, result);
827 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
829 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
832 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
836 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
837 if (!afb_context_check(&xreq->context)) {
838 afb_context_close(&xreq->context);
839 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
845 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
846 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
847 if (!afb_context_check_loa(&xreq->context, loa)) {
848 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
854 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
855 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
856 if (afb_context_check_loa(&xreq->context, loa + 1)) {
857 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
863 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
864 afb_context_refresh(&xreq->context);
866 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
867 afb_context_change_loa(&xreq->context, 0);
868 afb_context_close(&xreq->context);
874 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
878 if (sessionflags != 0) {
879 if (!afb_context_check(&xreq->context)) {
880 afb_context_close(&xreq->context);
881 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
887 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
888 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
889 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
894 if (auth && !afb_auth_check(xreq, auth)) {
895 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
900 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
901 afb_context_refresh(&xreq->context);
903 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
904 afb_context_close(&xreq->context);
910 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
913 afb_xreq_fail_unknown_verb(xreq);
915 if (!xreq_session_check_apply_v1(xreq, verb->session))
916 verb->callback(to_req(xreq));
919 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
922 afb_xreq_fail_unknown_verb(xreq);
924 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
925 verb->callback(to_req(xreq));
928 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
930 memset(xreq, 0, sizeof *xreq);
931 xreq->itf = &xreq_hooked_itf; /* hook by default */
933 xreq->queryitf = queryitf;
936 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
938 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
941 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
943 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
946 static void init_hooking(struct afb_xreq *xreq)
948 afb_hook_init_xreq(xreq);
950 afb_hook_xreq_begin(xreq);
952 xreq->itf = &xreq_itf; /* unhook the interface */
956 * job callback for asynchronous and secured processing of the request.
958 static void process_async(int signum, void *arg)
960 struct afb_xreq *xreq = arg;
961 const struct afb_api *api;
964 /* emit the error (assumes that hooking is initialised) */
965 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
969 /* invoke api call method to process the reqiest */
970 api = (const struct afb_api*)xreq->context.api_key;
971 api->itf->call(api->closure, xreq);
973 /* release the request */
974 afb_xreq_unhooked_unref(xreq);
978 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
979 * The early failure occurs only in function 'afb_xreq_process' where normally,
980 * the hooking is not initialised. So this "early" failure takes care to initialise
981 * the hooking in first.
983 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
991 va_start(args, info);
992 afb_req_fail_v(to_req(xreq), status, info, args);
997 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
998 * Errors are reported as request failures.
1000 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1002 const struct afb_api *api;
1003 struct afb_xreq *caller;
1005 /* lookup at the api */
1006 xreq->apiset = apiset;
1007 api = afb_apiset_lookup_started(apiset, xreq->api, 1);
1009 if (errno == ENOENT)
1010 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
1012 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
1015 xreq->context.api_key = api;
1017 /* check self locking */
1018 if (!api->noconcurrency)
1021 caller = xreq->caller;
1023 if (caller->context.api_key == api) {
1024 /* noconcurrency lock detected */
1025 ERROR("self-lock detected in call stack for API %s", xreq->api);
1026 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
1029 caller = caller->caller;
1033 /* queue the request job */
1034 afb_xreq_unhooked_addref(xreq);
1035 if (jobs_queue(api, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1036 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1037 ERROR("can't process job with threads: %m");
1038 early_failure(xreq, "cancelled", "not able to create a job for the task");
1039 afb_xreq_unhooked_unref(xreq);
1042 afb_xreq_unhooked_unref(xreq);