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 extern const struct afb_req_itf xreq_itf;
64 extern const struct afb_req_itf xreq_hooked_itf;
66 static inline struct afb_req to_req(struct afb_xreq *xreq)
68 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
71 /******************************************************************************/
77 void (*completion)(struct subcall*, int, struct json_object*);
81 struct jobloop *jobloop;
82 struct json_object *result;
87 void (*callback)(void*, int, struct json_object*);
88 void (*callback2)(void*, int, struct json_object*, struct afb_req);
95 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
97 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
99 return afb_xreq_subscribe(subcall->xreq.caller, event);
102 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
104 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
106 return afb_xreq_unsubscribe(subcall->xreq.caller, event);
109 static void subcall_reply_cb(struct afb_xreq *xreq, int status, struct json_object *result)
111 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
113 subcall->completion(subcall, status, result);
114 json_object_put(result);
115 afb_xreq_unref(&subcall->xreq);
118 static void subcall_destroy_cb(struct afb_xreq *xreq)
120 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
122 json_object_put(subcall->xreq.json);
123 afb_cred_unref(subcall->xreq.cred);
127 const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
128 .reply = subcall_reply_cb,
129 .unref = subcall_destroy_cb,
130 .subscribe = subcall_subscribe_cb,
131 .unsubscribe = subcall_unsubscribe_cb
134 static struct subcall *subcall_alloc(
135 struct afb_xreq *caller,
138 struct json_object *args
141 struct subcall *subcall;
142 size_t lenapi, lenverb;
145 lenapi = 1 + strlen(api);
146 lenverb = 1 + strlen(verb);
147 subcall = malloc(lenapi + lenverb + sizeof *subcall);
149 ERROR("out of memory");
151 copy = (char*)&subcall[1];
152 memcpy(copy, api, lenapi);
154 copy = ©[lenapi];
155 memcpy(copy, verb, lenverb);
158 afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
159 afb_context_subinit(&subcall->xreq.context, &caller->context);
160 subcall->xreq.cred = afb_cred_addref(caller->cred);
161 subcall->xreq.json = args;
162 subcall->xreq.api = api;
163 subcall->xreq.verb = verb;
164 subcall->xreq.caller = caller;
171 static void subcall_on_reply(struct subcall *subcall, int status, struct json_object *result)
173 subcall->callback(subcall->closure, status, result);
176 static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
178 subcall->callback2(subcall->closure, status, result, to_req(subcall->xreq.caller));
181 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
183 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
184 subcall_on_reply(subcall, status, result);
187 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
189 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
190 subcall_req_on_reply(subcall, status, result);
193 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
195 struct afb_xreq *xreq = closure;
198 ERROR("subcall replied more than one time!!");
199 json_object_put(result);
202 subcall_reply_cb(xreq, status, result);
206 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
208 subcall->completion = completion;
209 if (subcall->xreq.caller->queryitf->subcall) {
210 subcall->xreq.caller->queryitf->subcall(
211 subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb,
212 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
214 afb_xreq_addref(&subcall->xreq);
215 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
219 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
221 subcall->callback = callback;
222 subcall->closure = cb_closure;
223 subcall_process(subcall, subcall_on_reply);
226 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
228 subcall->callback2 = callback;
229 subcall->closure = cb_closure;
230 subcall_process(subcall, subcall_req_on_reply);
233 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
235 subcall->callback = callback;
236 subcall->closure = cb_closure;
237 subcall_process(subcall, subcall_hooked_on_reply);
240 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
242 subcall->callback2 = callback;
243 subcall->closure = cb_closure;
244 subcall_process(subcall, subcall_req_hooked_on_reply);
247 static void subcall_sync_leave(struct subcall *subcall)
249 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
254 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
256 subcall->status = status;
257 subcall->result = json_object_get(result);
258 subcall_sync_leave(subcall);
261 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
263 struct subcall *subcall = closure;
266 subcall->jobloop = jobloop;
267 subcall->result = NULL;
269 subcall_process(subcall, subcall_sync_reply);
271 subcall->status = -1;
272 subcall_sync_leave(subcall);
276 static int subcallsync(struct subcall *subcall, struct json_object **result)
280 afb_xreq_addref(&subcall->xreq);
281 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
282 *result = subcall->result;
283 if (rc < 0 || subcall->status < 0) {
284 *result = *result ?: afb_msg_json_internal_error();
287 afb_xreq_unref(&subcall->xreq);
291 /******************************************************************************/
293 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
296 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
298 fun(first, second, info);
302 /******************************************************************************/
304 static struct json_object *xreq_json_cb(void *closure)
306 struct afb_xreq *xreq = closure;
307 if (!xreq->json && xreq->queryitf->json)
308 xreq->json = xreq->queryitf->json(xreq);
312 static struct afb_arg xreq_get_cb(void *closure, const char *name)
314 struct afb_xreq *xreq = closure;
316 struct json_object *object, *value;
318 if (xreq->queryitf->get)
319 arg = xreq->queryitf->get(xreq, name);
321 object = xreq_json_cb(closure);
322 if (json_object_object_get_ex(object, name, &value)) {
324 arg.value = json_object_get_string(value);
334 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
336 struct afb_xreq *xreq = closure;
339 ERROR("reply called more than one time!!");
340 json_object_put(obj);
343 if (xreq->queryitf->success)
344 xreq->queryitf->success(xreq, obj, info);
346 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
350 static void xreq_fail_cb(void *closure, const char *status, const char *info)
352 struct afb_xreq *xreq = closure;
355 ERROR("reply called more than one time!!");
358 if (xreq->queryitf->fail)
359 xreq->queryitf->fail(xreq, status, info);
361 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
365 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
367 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
370 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
372 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
375 static void *xreq_context_get_cb(void *closure)
377 struct afb_xreq *xreq = closure;
378 return afb_context_get(&xreq->context);
381 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
383 struct afb_xreq *xreq = closure;
384 afb_context_set(&xreq->context, value, free_value);
387 static void xreq_addref_cb(void *closure)
389 struct afb_xreq *xreq = closure;
393 static void xreq_unref_cb(void *closure)
395 struct afb_xreq *xreq = closure;
399 static void xreq_session_close_cb(void *closure)
401 struct afb_xreq *xreq = closure;
402 afb_context_close(&xreq->context);
405 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
407 struct afb_xreq *xreq = closure;
408 return afb_context_change_loa(&xreq->context, level);
411 static int xreq_subscribe_cb(void *closure, struct afb_event event)
413 struct afb_xreq *xreq = closure;
414 return afb_xreq_subscribe(xreq, event);
417 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
420 return afb_evt_add_watch(xreq->listener, event);
421 if (xreq->queryitf->subscribe)
422 return xreq->queryitf->subscribe(xreq, event);
423 ERROR("no event listener, subscription impossible");
428 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
430 struct afb_xreq *xreq = closure;
431 return afb_xreq_unsubscribe(xreq, event);
434 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
437 return afb_evt_remove_watch(xreq->listener, event);
438 if (xreq->queryitf->unsubscribe)
439 return xreq->queryitf->unsubscribe(xreq, event);
440 ERROR("no event listener, unsubscription impossible");
445 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)
447 struct afb_xreq *xreq = closure;
450 sc = subcall_alloc(xreq, api, verb, args);
453 callback(cb_closure, 1, afb_msg_json_internal_error());
454 json_object_put(args);
456 subcall(sc, callback, cb_closure);
460 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)
462 struct afb_xreq *xreq = closure;
465 sc = subcall_alloc(xreq, api, verb, args);
468 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
469 json_object_put(args);
471 subcall_req(sc, callback, cb_closure);
476 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
480 struct afb_xreq *xreq = closure;
481 struct json_object *resu;
483 sc = subcall_alloc(xreq, api, verb, args);
486 resu = afb_msg_json_internal_error();
487 json_object_put(args);
489 rc = subcallsync(sc, &resu);
494 json_object_put(resu);
498 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
501 struct afb_xreq *xreq = closure;
503 if (!fmt || vasprintf(&p, fmt, args) < 0)
504 vverbose(level, file, line, func, fmt, args);
506 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p);
511 static struct afb_stored_req *xreq_store_cb(void *closure)
513 xreq_addref_cb(closure);
517 static int xreq_has_permission_cb(void*closure, const char *permission)
519 struct afb_xreq *xreq = closure;
520 return afb_auth_has_permission(xreq, permission);
523 static char *xreq_get_application_id_cb(void*closure)
525 struct afb_xreq *xreq = closure;
526 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
529 /******************************************************************************/
531 static struct json_object *xreq_hooked_json_cb(void *closure)
533 struct json_object *r = xreq_json_cb(closure);
534 struct afb_xreq *xreq = closure;
535 return afb_hook_xreq_json(xreq, r);
538 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
540 struct afb_arg r = xreq_get_cb(closure, name);
541 struct afb_xreq *xreq = closure;
542 return afb_hook_xreq_get(xreq, name, r);
545 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
547 struct afb_xreq *xreq = closure;
548 afb_hook_xreq_success(xreq, obj, info);
549 xreq_success_cb(closure, obj, info);
552 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
554 struct afb_xreq *xreq = closure;
555 afb_hook_xreq_fail(xreq, status, info);
556 xreq_fail_cb(closure, status, info);
559 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
561 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
564 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
566 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
569 static void *xreq_hooked_context_get_cb(void *closure)
571 void *r = xreq_context_get_cb(closure);
572 struct afb_xreq *xreq = closure;
573 return afb_hook_xreq_context_get(xreq, r);
576 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
578 struct afb_xreq *xreq = closure;
579 afb_hook_xreq_context_set(xreq, value, free_value);
580 xreq_context_set_cb(closure, value, free_value);
583 static void xreq_hooked_addref_cb(void *closure)
585 struct afb_xreq *xreq = closure;
586 afb_hook_xreq_addref(xreq);
587 xreq_addref_cb(closure);
590 static void xreq_hooked_unref_cb(void *closure)
592 struct afb_xreq *xreq = closure;
593 afb_hook_xreq_unref(xreq);
594 xreq_unref_cb(closure);
597 static void xreq_hooked_session_close_cb(void *closure)
599 struct afb_xreq *xreq = closure;
600 afb_hook_xreq_session_close(xreq);
601 xreq_session_close_cb(closure);
604 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
606 int r = xreq_session_set_LOA_cb(closure, level);
607 struct afb_xreq *xreq = closure;
608 return afb_hook_xreq_session_set_LOA(xreq, level, r);
611 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
613 int r = xreq_subscribe_cb(closure, event);
614 struct afb_xreq *xreq = closure;
615 return afb_hook_xreq_subscribe(xreq, event, r);
618 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
620 int r = xreq_unsubscribe_cb(closure, event);
621 struct afb_xreq *xreq = closure;
622 return afb_hook_xreq_unsubscribe(xreq, event, r);
625 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)
627 struct afb_xreq *xreq = closure;
630 afb_hook_xreq_subcall(xreq, api, verb, args);
631 sc = subcall_alloc(xreq, api, verb, args);
634 callback(cb_closure, 1, afb_msg_json_internal_error());
635 json_object_put(args);
637 subcall_hooked(sc, callback, cb_closure);
641 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)
643 struct afb_xreq *xreq = closure;
646 afb_hook_xreq_subcall_req(xreq, api, verb, args);
647 sc = subcall_alloc(xreq, api, verb, args);
650 callback(cb_closure, 1, afb_msg_json_internal_error(), to_req(xreq));
651 json_object_put(args);
653 subcall_req_hooked(sc, callback, cb_closure);
657 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
660 struct afb_xreq *xreq = closure;
661 afb_hook_xreq_subcallsync(xreq, api, verb, args);
662 r = xreq_subcallsync_cb(closure, api, verb, args, result);
663 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
666 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
668 struct afb_xreq *xreq = closure;
671 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
672 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
676 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
678 struct afb_xreq *xreq = closure;
679 struct afb_stored_req *r = xreq_store_cb(closure);
680 afb_hook_xreq_store(xreq, r);
684 static int xreq_hooked_has_permission_cb(void*closure, const char *permission)
686 struct afb_xreq *xreq = closure;
687 int r = xreq_has_permission_cb(closure, permission);
688 return afb_hook_xreq_has_permission(xreq, permission, r);
691 static char *xreq_hooked_get_application_id_cb(void*closure)
693 struct afb_xreq *xreq = closure;
694 char *r = xreq_get_application_id_cb(closure);
695 return afb_hook_xreq_get_application_id(xreq, r);
698 /******************************************************************************/
700 const struct afb_req_itf xreq_itf = {
701 .json = xreq_json_cb,
703 .success = xreq_success_cb,
704 .fail = xreq_fail_cb,
705 .vsuccess = xreq_vsuccess_cb,
706 .vfail = xreq_vfail_cb,
707 .context_get = xreq_context_get_cb,
708 .context_set = xreq_context_set_cb,
709 .addref = xreq_addref_cb,
710 .unref = xreq_unref_cb,
711 .session_close = xreq_session_close_cb,
712 .session_set_LOA = xreq_session_set_LOA_cb,
713 .subscribe = xreq_subscribe_cb,
714 .unsubscribe = xreq_unsubscribe_cb,
715 .subcall = xreq_subcall_cb,
716 .subcallsync = xreq_subcallsync_cb,
717 .vverbose = xreq_vverbose_cb,
718 .store = xreq_store_cb,
719 .subcall_req = xreq_subcall_req_cb,
720 .has_permission = xreq_has_permission_cb,
721 .get_application_id = xreq_get_application_id_cb
724 const struct afb_req_itf xreq_hooked_itf = {
725 .json = xreq_hooked_json_cb,
726 .get = xreq_hooked_get_cb,
727 .success = xreq_hooked_success_cb,
728 .fail = xreq_hooked_fail_cb,
729 .vsuccess = xreq_hooked_vsuccess_cb,
730 .vfail = xreq_hooked_vfail_cb,
731 .context_get = xreq_hooked_context_get_cb,
732 .context_set = xreq_hooked_context_set_cb,
733 .addref = xreq_hooked_addref_cb,
734 .unref = xreq_hooked_unref_cb,
735 .session_close = xreq_hooked_session_close_cb,
736 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
737 .subscribe = xreq_hooked_subscribe_cb,
738 .unsubscribe = xreq_hooked_unsubscribe_cb,
739 .subcall = xreq_hooked_subcall_cb,
740 .subcallsync = xreq_hooked_subcallsync_cb,
741 .vverbose = xreq_hooked_vverbose_cb,
742 .store = xreq_hooked_store_cb,
743 .subcall_req = xreq_hooked_subcall_req_cb,
744 .has_permission = xreq_hooked_has_permission_cb,
745 .get_application_id = xreq_hooked_get_application_id_cb
748 /******************************************************************************/
750 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
752 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
754 afb_hook_xreq_unstore(xreq);
758 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
760 return afb_req_json(to_req(xreq));
763 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
765 afb_req_success(to_req(xreq), obj, info);
768 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
772 va_start(args, info);
773 afb_req_success_v(to_req(xreq), obj, info, args);
777 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
779 afb_req_fail(to_req(xreq), status, info);
782 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
786 va_start(args, info);
787 afb_req_fail_v(to_req(xreq), status, info, args);
792 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
794 struct json_object *obj = xreq_json_cb(xreq);
795 const char *result = json_object_to_json_string(obj);
797 *size = strlen(result);
801 void afb_xreq_addref(struct afb_xreq *xreq)
803 afb_req_addref(to_req(xreq));
806 void afb_xreq_unref(struct afb_xreq *xreq)
808 afb_req_unref(to_req(xreq));
811 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)
813 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
816 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)
818 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
821 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
823 return xreq_subcallsync_cb(xreq, api, verb, args, result);
826 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
828 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
831 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
835 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
836 if (!afb_context_check(&xreq->context)) {
837 afb_context_close(&xreq->context);
838 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
844 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
845 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
846 if (!afb_context_check_loa(&xreq->context, loa)) {
847 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
853 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
854 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
855 if (afb_context_check_loa(&xreq->context, loa + 1)) {
856 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
862 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
863 afb_context_refresh(&xreq->context);
865 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
866 afb_context_change_loa(&xreq->context, 0);
867 afb_context_close(&xreq->context);
873 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
877 if (sessionflags != 0) {
878 if (!afb_context_check(&xreq->context)) {
879 afb_context_close(&xreq->context);
880 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
886 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
887 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
888 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
893 if (auth && !afb_auth_check(xreq, auth)) {
894 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
899 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
900 afb_context_refresh(&xreq->context);
902 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
903 afb_context_close(&xreq->context);
909 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
912 afb_xreq_fail_unknown_verb(xreq);
914 if (!xreq_session_check_apply_v1(xreq, verb->session))
915 verb->callback(to_req(xreq));
918 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
921 afb_xreq_fail_unknown_verb(xreq);
923 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
924 verb->callback(to_req(xreq));
927 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
929 memset(xreq, 0, sizeof *xreq);
931 xreq->queryitf = queryitf;
934 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
936 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
939 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
941 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
944 static void init_hooking(struct afb_xreq *xreq)
946 afb_hook_init_xreq(xreq);
948 afb_hook_xreq_begin(xreq);
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");