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 inline void xreq_addref(struct afb_xreq *xreq)
45 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
48 static inline void xreq_unref(struct afb_xreq *xreq)
50 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
52 afb_xreq_fail(xreq, "error", "no reply");
54 afb_hook_xreq_end(xreq);
56 xreq_unref(xreq->caller);
57 xreq->queryitf->unref(xreq);
61 /******************************************************************************/
63 static inline struct afb_req to_req(struct afb_xreq *xreq)
65 return (struct afb_req){ .itf = xreq->itf, .closure = xreq };
68 /******************************************************************************/
74 void (*completion)(struct subcall*, int, struct json_object*);
78 struct jobloop *jobloop;
79 struct json_object *result;
84 void (*callback)(void*, int, struct json_object*);
85 void (*callback2)(void*, int, struct json_object*, struct afb_req);
92 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
94 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
96 return afb_xreq_subscribe(subcall->xreq.caller, event);
99 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
101 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
103 return afb_xreq_unsubscribe(subcall->xreq.caller, event);
106 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
108 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
110 subcall->completion(subcall, status, result);
111 json_object_put(result);
112 afb_xreq_unref(&subcall->xreq);
115 static void subcall_destroy_cb(struct afb_xreq *xreq)
117 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
119 json_object_put(subcall->xreq.json);
120 afb_cred_unref(subcall->xreq.cred);
124 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
125 .reply = subcall_reply_cb,
126 .unref = subcall_destroy_cb,
127 .subscribe = subcall_subscribe_cb,
128 .unsubscribe = subcall_unsubscribe_cb
131 static struct subcall *subcall_alloc(
132 struct afb_xreq *caller,
135 struct json_object *args
138 struct subcall *subcall;
139 size_t lenapi, lenverb;
142 lenapi = 1 + strlen(api);
143 lenverb = 1 + strlen(verb);
144 subcall = malloc(lenapi + lenverb + sizeof *subcall);
146 ERROR("out of memory");
148 copy = (char*)&subcall[1];
149 memcpy(copy, api, lenapi);
151 copy = ©[lenapi];
152 memcpy(copy, verb, lenverb);
155 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
156 afb_context_subinit(&subcall->xreq.context, &caller->context);
157 subcall->xreq.cred = afb_cred_addref(caller->cred);
158 subcall->xreq.json = args;
159 subcall->xreq.api = api;
160 subcall->xreq.verb = verb;
161 subcall->xreq.caller = caller;
168 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
170 subcall->callback(subcall->closure, status, result);
173 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
175 subcall->callback2(subcall->closure, status, result, to_req(subcall->xreq.caller));
178 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
180 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
181 subcall_on_reply(subcall, status, result);
184 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
186 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
187 subcall_req_on_reply(subcall, status, result);
190 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
192 struct afb_xreq *xreq = closure;
195 ERROR("subcall replied more than one time!!");
196 json_object_put(result);
199 subcall_reply_cb(xreq, status, result);
203 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
205 subcall->completion = completion;
206 if (subcall->xreq.caller->queryitf->subcall) {
207 subcall->xreq.caller->queryitf->subcall(
208 subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb,
209 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
211 afb_xreq_addref(&subcall->xreq);
212 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
216 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
218 subcall->callback = callback;
219 subcall->closure = cb_closure;
220 subcall_process(subcall, subcall_on_reply);
223 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
225 subcall->callback2 = callback;
226 subcall->closure = cb_closure;
227 subcall_process(subcall, subcall_req_on_reply);
230 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
232 subcall->callback = callback;
233 subcall->closure = cb_closure;
234 subcall_process(subcall, subcall_hooked_on_reply);
237 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
239 subcall->callback2 = callback;
240 subcall->closure = cb_closure;
241 subcall_process(subcall, subcall_req_hooked_on_reply);
244 static void subcall_sync_leave(struct subcall *subcall)
246 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
251 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
253 subcall->status = status;
254 subcall->result = json_object_get(result);
255 subcall_sync_leave(subcall);
258 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
260 struct subcall *subcall = closure;
263 subcall->jobloop = jobloop;
264 subcall->result = NULL;
266 subcall_process(subcall, subcall_sync_reply);
268 subcall->status = -1;
269 subcall_sync_leave(subcall);
273 static int subcallsync(struct subcall *subcall, struct json_object **result)
277 afb_xreq_addref(&subcall->xreq);
278 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
279 *result = subcall->result;
280 if (rc < 0 || subcall->status < 0) {
281 *result = *result ?: afb_msg_json_internal_error();
284 afb_xreq_unref(&subcall->xreq);
288 /******************************************************************************/
290 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
293 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
295 fun(first, second, info);
299 /******************************************************************************/
301 static struct json_object *xreq_json_cb(void *closure)
303 struct afb_xreq *xreq = closure;
304 if (!xreq->json && xreq->queryitf->json)
305 xreq->json = xreq->queryitf->json(xreq);
309 static struct afb_arg xreq_get_cb(void *closure, const char *name)
311 struct afb_xreq *xreq = closure;
313 struct json_object *object, *value;
315 if (xreq->queryitf->get)
316 arg = xreq->queryitf->get(xreq, name);
318 object = xreq_json_cb(closure);
319 if (json_object_object_get_ex(object, name, &value)) {
321 arg.value = json_object_get_string(value);
331 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
333 struct afb_xreq *xreq = closure;
336 ERROR("reply called more than one time!!");
337 json_object_put(obj);
340 if (xreq->queryitf->success)
341 xreq->queryitf->success(xreq, obj, info);
343 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
347 static void xreq_fail_cb(void *closure, const char *status, const char *info)
349 struct afb_xreq *xreq = closure;
352 ERROR("reply called more than one time!!");
355 if (xreq->queryitf->fail)
356 xreq->queryitf->fail(xreq, status, info);
358 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
362 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
364 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
367 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
369 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
372 static void *xreq_context_get_cb(void *closure)
374 struct afb_xreq *xreq = closure;
375 return afb_context_get(&xreq->context);
378 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
380 struct afb_xreq *xreq = closure;
381 afb_context_set(&xreq->context, value, free_value);
384 static void xreq_addref_cb(void *closure)
386 struct afb_xreq *xreq = closure;
390 static void xreq_unref_cb(void *closure)
392 struct afb_xreq *xreq = closure;
396 static void xreq_session_close_cb(void *closure)
398 struct afb_xreq *xreq = closure;
399 afb_context_close(&xreq->context);
402 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
404 struct afb_xreq *xreq = closure;
405 return afb_context_change_loa(&xreq->context, level);
408 static int xreq_subscribe_cb(void *closure, struct afb_event event)
410 struct afb_xreq *xreq = closure;
411 return afb_xreq_subscribe(xreq, event);
414 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
417 return afb_evt_add_watch(xreq->listener, event);
418 if (xreq->queryitf->subscribe)
419 return xreq->queryitf->subscribe(xreq, event);
420 ERROR("no event listener, subscription impossible");
425 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
427 struct afb_xreq *xreq = closure;
428 return afb_xreq_unsubscribe(xreq, event);
431 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
434 return afb_evt_remove_watch(xreq->listener, event);
435 if (xreq->queryitf->unsubscribe)
436 return xreq->queryitf->unsubscribe(xreq, event);
437 ERROR("no event listener, unsubscription impossible");
442 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)
444 struct afb_xreq *xreq = closure;
447 sc = subcall_alloc(xreq, api, verb, args);
450 callback(cb_closure, 1, afb_msg_json_internal_error());
451 json_object_put(args);
453 subcall(sc, callback, cb_closure);
457 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)
459 struct afb_xreq *xreq = closure;
462 sc = subcall_alloc(xreq, api, verb, args);
465 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
466 json_object_put(args);
468 subcall_req(sc, callback, cb_closure);
473 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
477 struct afb_xreq *xreq = closure;
478 struct json_object *resu;
480 sc = subcall_alloc(xreq, api, verb, args);
483 resu = afb_msg_json_internal_error();
484 json_object_put(args);
486 rc = subcallsync(sc, &resu);
491 json_object_put(resu);
495 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
498 struct afb_xreq *xreq = closure;
500 if (!fmt || vasprintf(&p, fmt, args) < 0)
501 vverbose(level, file, line, func, fmt, args);
503 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
508 static struct afb_stored_req *xreq_store_cb(void *closure)
510 xreq_addref_cb(closure);
514 static int xreq_has_permission_cb(void*closure, const char *permission)
516 struct afb_xreq *xreq = closure;
517 return afb_auth_has_permission(xreq, permission);
520 static char *xreq_get_application_id_cb(void*closure)
522 struct afb_xreq *xreq = closure;
523 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
526 /******************************************************************************/
528 static struct json_object *xreq_hooked_json_cb(void *closure)
530 struct json_object *r = xreq_json_cb(closure);
531 struct afb_xreq *xreq = closure;
532 return afb_hook_xreq_json(xreq, r);
535 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
537 struct afb_arg r = xreq_get_cb(closure, name);
538 struct afb_xreq *xreq = closure;
539 return afb_hook_xreq_get(xreq, name, r);
542 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
544 struct afb_xreq *xreq = closure;
545 afb_hook_xreq_success(xreq, obj, info);
546 xreq_success_cb(closure, obj, info);
549 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
551 struct afb_xreq *xreq = closure;
552 afb_hook_xreq_fail(xreq, status, info);
553 xreq_fail_cb(closure, status, info);
556 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
558 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
561 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
563 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
566 static void *xreq_hooked_context_get_cb(void *closure)
568 void *r = xreq_context_get_cb(closure);
569 struct afb_xreq *xreq = closure;
570 return afb_hook_xreq_context_get(xreq, r);
573 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
575 struct afb_xreq *xreq = closure;
576 afb_hook_xreq_context_set(xreq, value, free_value);
577 xreq_context_set_cb(closure, value, free_value);
580 static void xreq_hooked_addref_cb(void *closure)
582 struct afb_xreq *xreq = closure;
583 afb_hook_xreq_addref(xreq);
584 xreq_addref_cb(closure);
587 static void xreq_hooked_unref_cb(void *closure)
589 struct afb_xreq *xreq = closure;
590 afb_hook_xreq_unref(xreq);
591 xreq_unref_cb(closure);
594 static void xreq_hooked_session_close_cb(void *closure)
596 struct afb_xreq *xreq = closure;
597 afb_hook_xreq_session_close(xreq);
598 xreq_session_close_cb(closure);
601 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
603 int r = xreq_session_set_LOA_cb(closure, level);
604 struct afb_xreq *xreq = closure;
605 return afb_hook_xreq_session_set_LOA(xreq, level, r);
608 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
610 int r = xreq_subscribe_cb(closure, event);
611 struct afb_xreq *xreq = closure;
612 return afb_hook_xreq_subscribe(xreq, event, r);
615 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
617 int r = xreq_unsubscribe_cb(closure, event);
618 struct afb_xreq *xreq = closure;
619 return afb_hook_xreq_unsubscribe(xreq, event, r);
622 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)
624 struct afb_xreq *xreq = closure;
627 afb_hook_xreq_subcall(xreq, api, verb, args);
628 sc = subcall_alloc(xreq, api, verb, args);
631 callback(cb_closure, 1, afb_msg_json_internal_error());
632 json_object_put(args);
634 subcall_hooked(sc, callback, cb_closure);
638 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)
640 struct afb_xreq *xreq = closure;
643 afb_hook_xreq_subcall_req(xreq, api, verb, args);
644 sc = subcall_alloc(xreq, api, verb, args);
647 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
648 json_object_put(args);
650 subcall_req_hooked(sc, callback, cb_closure);
654 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
657 struct afb_xreq *xreq = closure;
658 afb_hook_xreq_subcallsync(xreq, api, verb, args);
659 r = xreq_subcallsync_cb(closure, api, verb, args, result);
660 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
663 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
665 struct afb_xreq *xreq = closure;
668 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
669 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
673 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
675 struct afb_xreq *xreq = closure;
676 struct afb_stored_req *r = xreq_store_cb(closure);
677 afb_hook_xreq_store(xreq, r);
681 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
683 struct afb_xreq *xreq = closure;
684 int r = xreq_has_permission_cb(closure, permission);
685 return afb_hook_xreq_has_permission(xreq, permission, r);
688 static char *xreq_hooked_get_application_id_cb(void*closure)
690 struct afb_xreq *xreq = closure;
691 char *r = xreq_get_application_id_cb(closure);
692 return afb_hook_xreq_get_application_id(xreq, r);
695 /******************************************************************************/
697 const struct afb_req_itf xreq_itf = {
698 .json = xreq_json_cb,
700 .success = xreq_success_cb,
701 .fail = xreq_fail_cb,
702 .vsuccess = xreq_vsuccess_cb,
703 .vfail = xreq_vfail_cb,
704 .context_get = xreq_context_get_cb,
705 .context_set = xreq_context_set_cb,
706 .addref = xreq_addref_cb,
707 .unref = xreq_unref_cb,
708 .session_close = xreq_session_close_cb,
709 .session_set_LOA = xreq_session_set_LOA_cb,
710 .subscribe = xreq_subscribe_cb,
711 .unsubscribe = xreq_unsubscribe_cb,
712 .subcall = xreq_subcall_cb,
713 .subcallsync = xreq_subcallsync_cb,
714 .vverbose = xreq_vverbose_cb,
715 .store = xreq_store_cb,
716 .subcall_req = xreq_subcall_req_cb,
717 .has_permission = xreq_has_permission_cb,
718 .get_application_id = xreq_get_application_id_cb
721 const struct afb_req_itf xreq_hooked_itf = {
722 .json = xreq_hooked_json_cb,
723 .get = xreq_hooked_get_cb,
724 .success = xreq_hooked_success_cb,
725 .fail = xreq_hooked_fail_cb,
726 .vsuccess = xreq_hooked_vsuccess_cb,
727 .vfail = xreq_hooked_vfail_cb,
728 .context_get = xreq_hooked_context_get_cb,
729 .context_set = xreq_hooked_context_set_cb,
730 .addref = xreq_hooked_addref_cb,
731 .unref = xreq_hooked_unref_cb,
732 .session_close = xreq_hooked_session_close_cb,
733 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
734 .subscribe = xreq_hooked_subscribe_cb,
735 .unsubscribe = xreq_hooked_unsubscribe_cb,
736 .subcall = xreq_hooked_subcall_cb,
737 .subcallsync = xreq_hooked_subcallsync_cb,
738 .vverbose = xreq_hooked_vverbose_cb,
739 .store = xreq_hooked_store_cb,
740 .subcall_req = xreq_hooked_subcall_req_cb,
741 .has_permission = xreq_hooked_has_permission_cb,
742 .get_application_id = xreq_hooked_get_application_id_cb
745 /******************************************************************************/
747 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
749 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
751 afb_hook_xreq_unstore(xreq);
755 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
757 return afb_req_json(to_req(xreq));
760 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
762 afb_req_success(to_req(xreq), obj, info);
765 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
769 va_start(args, info);
770 afb_req_success_v(to_req(xreq), obj, info, args);
774 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
776 afb_req_fail(to_req(xreq), status, info);
779 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
783 va_start(args, info);
784 afb_req_fail_v(to_req(xreq), status, info, args);
789 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
791 struct json_object *obj = xreq_json_cb(xreq);
792 const char *result = json_object_to_json_string(obj);
794 *size = strlen(result);
798 void afb_xreq_addref(struct afb_xreq *xreq)
800 afb_req_addref(to_req(xreq));
803 void afb_xreq_unref(struct afb_xreq *xreq)
805 afb_req_unref(to_req(xreq));
808 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)
810 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
813 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)
815 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
818 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
820 return xreq_subcallsync_cb(xreq, api, verb, args, result);
823 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
825 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
828 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
832 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
833 if (!afb_context_check(&xreq->context)) {
834 afb_context_close(&xreq->context);
835 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
841 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
842 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
843 if (!afb_context_check_loa(&xreq->context, loa)) {
844 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
850 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
851 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
852 if (afb_context_check_loa(&xreq->context, loa + 1)) {
853 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
859 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
860 afb_context_refresh(&xreq->context);
862 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
863 afb_context_change_loa(&xreq->context, 0);
864 afb_context_close(&xreq->context);
870 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
874 if (sessionflags != 0) {
875 if (!afb_context_check(&xreq->context)) {
876 afb_context_close(&xreq->context);
877 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
883 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
884 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
885 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
890 if (auth && !afb_auth_check(xreq, auth)) {
891 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
896 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
897 afb_context_refresh(&xreq->context);
899 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
900 afb_context_close(&xreq->context);
906 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
909 afb_xreq_fail_unknown_verb(xreq);
911 if (!xreq_session_check_apply_v1(xreq, verb->session))
912 verb->callback(to_req(xreq));
915 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
918 afb_xreq_fail_unknown_verb(xreq);
920 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
921 verb->callback(to_req(xreq));
924 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
926 memset(xreq, 0, sizeof *xreq);
927 xreq->itf = &xreq_hooked_itf; /* hook by default */
929 xreq->queryitf = queryitf;
932 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
934 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
937 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
939 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
942 static void init_hooking(struct afb_xreq *xreq)
944 afb_hook_init_xreq(xreq);
946 afb_hook_xreq_begin(xreq);
948 xreq->itf = &xreq_itf; /* unhook the interface */
952 * job callback for asynchronous and secured processing of the request.
954 static void process_async(int signum, void *arg)
956 struct afb_xreq *xreq = arg;
957 const struct afb_api *api;
960 /* emit the error (assumes that hooking is initialised) */
961 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
965 /* invoke api call method to process the reqiest */
966 api = (const struct afb_api*)xreq->context.api_key;
967 api->itf->call(api->closure, xreq);
969 /* release the request */
974 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
975 * The early failure occurs only in function 'afb_xreq_process' where normally,
976 * the hooking is not initialised. So this "early" failure takes care to initialise
977 * the hooking in first.
979 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
987 va_start(args, info);
988 afb_req_fail_v(to_req(xreq), status, info, args);
993 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
994 * Errors are reported as request failures.
996 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
998 const struct afb_api *api;
999 struct afb_xreq *caller;
1001 /* lookup at the api */
1002 xreq->apiset = apiset;
1003 api = afb_apiset_lookup_started(apiset, xreq->api, 1);
1005 if (errno == ENOENT)
1006 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
1008 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
1011 xreq->context.api_key = api;
1013 /* check self locking */
1014 if (!api->noconcurrency)
1017 caller = xreq->caller;
1019 if (caller->context.api_key == api) {
1020 /* noconcurrency lock detected */
1021 ERROR("self-lock detected in call stack for API %s", xreq->api);
1022 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api);
1025 caller = caller->caller;
1029 /* queue the request job */
1031 if (jobs_queue(api, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1032 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1033 ERROR("can't process job with threads: %m");
1034 early_failure(xreq, "cancelled", "not able to create a job for the task");