2 * Copyright (C) 2017, 2018 "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>
28 #include <afb/afb-request.h>
30 #include "afb-context.h"
33 #include "afb-msg-json.h"
37 #include "afb-api-dyn.h"
38 #include "afb-apiset.h"
43 /******************************************************************************/
45 static void xreq_finalize(struct afb_xreq *xreq)
48 afb_xreq_fail(xreq, "error", "no reply");
50 afb_hook_xreq_end(xreq);
52 afb_xreq_unhooked_unref(xreq->caller);
53 xreq->queryitf->unref(xreq);
56 inline void afb_xreq_unhooked_addref(struct afb_xreq *xreq)
58 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
61 inline void afb_xreq_unhooked_unref(struct afb_xreq *xreq)
63 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED))
67 /******************************************************************************/
73 void (*completion)(struct subcall*, int, struct json_object*);
77 struct jobloop *jobloop;
78 struct json_object *result;
83 void (*callback)(void*, int, struct json_object*);
84 void (*callback_req)(void*, int, struct json_object*, struct afb_req);
85 void (*callback_request)(void*, int, struct json_object*, struct afb_request*);
92 static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
94 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
96 return afb_xreq_subscribe(subcall->xreq.caller, eventid);
99 static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
101 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
103 return afb_xreq_unsubscribe(subcall->xreq.caller, eventid);
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_unhooked_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.request.api = api;
160 subcall->xreq.request.verb = verb;
161 subcall->xreq.caller = caller;
162 afb_xreq_unhooked_addref(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->callback_req(subcall->closure, status, result, xreq_to_req(subcall->xreq.caller));
178 static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result)
180 subcall->callback_request(subcall->closure, status, result, xreq_to_request(subcall->xreq.caller));
183 static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
185 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
186 subcall_on_reply(subcall, status, result);
189 static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
191 afb_hook_xreq_subcall_req_result(subcall->xreq.caller, status, result);
192 subcall_req_on_reply(subcall, status, result);
195 static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
197 afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
198 subcall_request_on_reply(subcall, status, result);
201 static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
203 struct afb_xreq *xreq = closure;
206 ERROR("subcall replied more than one time!!");
207 json_object_put(result);
210 subcall_reply_cb(xreq, status, result);
214 static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
216 subcall->completion = completion;
217 if (subcall->xreq.caller->queryitf->subcall) {
218 subcall->xreq.caller->queryitf->subcall(
219 subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb,
220 subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
222 afb_xreq_unhooked_addref(&subcall->xreq);
223 afb_xreq_process(&subcall->xreq, subcall->xreq.caller->apiset);
227 static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
229 subcall->callback = callback;
230 subcall->closure = cb_closure;
231 subcall_process(subcall, subcall_on_reply);
234 static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
236 subcall->callback_req = callback;
237 subcall->closure = cb_closure;
238 subcall_process(subcall, subcall_req_on_reply);
241 static void subcall_request(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
243 subcall->callback_request = callback;
244 subcall->closure = cb_closure;
245 subcall_process(subcall, subcall_request_on_reply);
248 static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
250 subcall->callback = callback;
251 subcall->closure = cb_closure;
252 subcall_process(subcall, subcall_hooked_on_reply);
255 static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
257 subcall->callback_req = callback;
258 subcall->closure = cb_closure;
259 subcall_process(subcall, subcall_req_hooked_on_reply);
262 static void subcall_request_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
264 subcall->callback_request = callback;
265 subcall->closure = cb_closure;
266 subcall_process(subcall, subcall_request_hooked_on_reply);
269 static void subcall_sync_leave(struct subcall *subcall)
271 struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
276 static void subcall_sync_reply(struct subcall *subcall, int status, struct json_object *result)
278 subcall->status = status;
279 subcall->result = json_object_get(result);
280 subcall_sync_leave(subcall);
283 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
285 struct subcall *subcall = closure;
288 subcall->jobloop = jobloop;
289 subcall->result = NULL;
291 subcall_process(subcall, subcall_sync_reply);
293 subcall->status = -1;
294 subcall_sync_leave(subcall);
298 static int subcallsync(struct subcall *subcall, struct json_object **result)
302 afb_xreq_unhooked_addref(&subcall->xreq);
303 rc = jobs_enter(NULL, 0, subcall_sync_enter, subcall);
304 *result = subcall->result;
305 if (rc < 0 || subcall->status < 0) {
306 *result = *result ?: afb_msg_json_internal_error();
309 afb_xreq_unhooked_unref(&subcall->xreq);
313 /******************************************************************************/
315 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
318 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
320 fun(first, second, info);
324 /******************************************************************************/
326 static struct json_object *xreq_json_cb(struct afb_request *closure)
328 struct afb_xreq *xreq = xreq_from_request(closure);
329 if (!xreq->json && xreq->queryitf->json)
330 xreq->json = xreq->queryitf->json(xreq);
334 static struct afb_arg xreq_get_cb(struct afb_request *closure, const char *name)
336 struct afb_xreq *xreq = xreq_from_request(closure);
338 struct json_object *object, *value;
340 if (xreq->queryitf->get)
341 arg = xreq->queryitf->get(xreq, name);
343 object = xreq_json_cb(closure);
344 if (json_object_object_get_ex(object, name, &value)) {
346 arg.value = json_object_get_string(value);
356 static void xreq_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
358 struct afb_xreq *xreq = xreq_from_request(closure);
361 ERROR("reply called more than one time!!");
362 json_object_put(obj);
365 if (xreq->queryitf->success)
366 xreq->queryitf->success(xreq, obj, info);
368 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
372 static void xreq_fail_cb(struct afb_request *closure, const char *status, const char *info)
374 struct afb_xreq *xreq = xreq_from_request(closure);
377 ERROR("reply called more than one time!!");
380 if (xreq->queryitf->fail)
381 xreq->queryitf->fail(xreq, status, info);
383 xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
387 static void xreq_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
389 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
392 static void xreq_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
394 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
397 static void *xreq_context_get_cb(struct afb_request *closure)
399 struct afb_xreq *xreq = xreq_from_request(closure);
400 return afb_context_get(&xreq->context);
403 static void xreq_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
405 struct afb_xreq *xreq = xreq_from_request(closure);
406 afb_context_set(&xreq->context, value, free_value);
409 static struct afb_request *xreq_addref_cb(struct afb_request *closure)
411 struct afb_xreq *xreq = xreq_from_request(closure);
412 afb_xreq_unhooked_addref(xreq);
416 static void xreq_unref_cb(struct afb_request *closure)
418 struct afb_xreq *xreq = xreq_from_request(closure);
419 afb_xreq_unhooked_unref(xreq);
422 static void xreq_session_close_cb(struct afb_request *closure)
424 struct afb_xreq *xreq = xreq_from_request(closure);
425 afb_context_close(&xreq->context);
428 static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level)
430 struct afb_xreq *xreq = xreq_from_request(closure);
431 return afb_context_change_loa(&xreq->context, level);
434 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
435 static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event)
437 return xreq_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
440 static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
442 struct afb_xreq *xreq = xreq_from_request(closure);
443 return afb_xreq_subscribe(xreq, eventid);
446 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
449 return afb_evt_eventid_add_watch(xreq->listener, eventid);
450 if (xreq->queryitf->subscribe)
451 return xreq->queryitf->subscribe(xreq, eventid);
452 ERROR("no event listener, subscription impossible");
457 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
458 static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
460 return xreq_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
463 static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
465 struct afb_xreq *xreq = xreq_from_request(closure);
466 return afb_xreq_unsubscribe(xreq, eventid);
469 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid)
472 return afb_evt_eventid_remove_watch(xreq->listener, eventid);
473 if (xreq->queryitf->unsubscribe)
474 return xreq->queryitf->unsubscribe(xreq, eventid);
475 ERROR("no event listener, unsubscription impossible");
480 static void xreq_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
482 struct afb_xreq *xreq = xreq_from_request(closure);
485 sc = subcall_alloc(xreq, api, verb, args);
488 callback(cb_closure, 1, afb_msg_json_internal_error());
489 json_object_put(args);
491 subcall(sc, callback, cb_closure);
495 static void xreq_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
497 struct afb_xreq *xreq = xreq_from_request(closure);
500 sc = subcall_alloc(xreq, api, verb, args);
503 callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_req(xreq));
504 json_object_put(args);
506 subcall_req(sc, callback, cb_closure);
510 static void xreq_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure)
512 struct afb_xreq *xreq = xreq_from_request(closure);
515 sc = subcall_alloc(xreq, api, verb, args);
518 callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_request(xreq));
519 json_object_put(args);
521 subcall_request(sc, callback, cb_closure);
526 static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
530 struct afb_xreq *xreq = xreq_from_request(closure);
531 struct json_object *resu;
533 sc = subcall_alloc(xreq, api, verb, args);
536 resu = afb_msg_json_internal_error();
537 json_object_put(args);
539 rc = subcallsync(sc, &resu);
544 json_object_put(resu);
548 static void xreq_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
551 struct afb_xreq *xreq = xreq_from_request(closure);
553 if (!fmt || vasprintf(&p, fmt, args) < 0)
554 vverbose(level, file, line, func, fmt, args);
556 verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p);
561 static struct afb_stored_req *xreq_store_cb(struct afb_request *closure)
563 xreq_addref_cb(closure);
564 return (struct afb_stored_req*)closure;
567 static int xreq_has_permission_cb(struct afb_request *closure, const char *permission)
569 struct afb_xreq *xreq = xreq_from_request(closure);
570 return afb_auth_has_permission(xreq, permission);
573 static char *xreq_get_application_id_cb(struct afb_request *closure)
575 struct afb_xreq *xreq = xreq_from_request(closure);
576 return xreq->cred && xreq->cred->id ? strdup(xreq->cred->id) : NULL;
579 static void *xreq_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
581 struct afb_xreq *xreq = xreq_from_request(closure);
582 return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure);
585 static int xreq_get_uid_cb(struct afb_request *closure)
587 struct afb_xreq *xreq = xreq_from_request(closure);
588 return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1;
591 /******************************************************************************/
593 static struct json_object *xreq_hooked_json_cb(struct afb_request *closure)
595 struct json_object *r = xreq_json_cb(closure);
596 struct afb_xreq *xreq = xreq_from_request(closure);
597 return afb_hook_xreq_json(xreq, r);
600 static struct afb_arg xreq_hooked_get_cb(struct afb_request *closure, const char *name)
602 struct afb_arg r = xreq_get_cb(closure, name);
603 struct afb_xreq *xreq = xreq_from_request(closure);
604 return afb_hook_xreq_get(xreq, name, r);
607 static void xreq_hooked_success_cb(struct afb_request *closure, struct json_object *obj, const char *info)
609 struct afb_xreq *xreq = xreq_from_request(closure);
610 afb_hook_xreq_success(xreq, obj, info);
611 xreq_success_cb(closure, obj, info);
614 static void xreq_hooked_fail_cb(struct afb_request *closure, const char *status, const char *info)
616 struct afb_xreq *xreq = xreq_from_request(closure);
617 afb_hook_xreq_fail(xreq, status, info);
618 xreq_fail_cb(closure, status, info);
621 static void xreq_hooked_vsuccess_cb(struct afb_request *closure, struct json_object *obj, const char *fmt, va_list args)
623 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
626 static void xreq_hooked_vfail_cb(struct afb_request *closure, const char *status, const char *fmt, va_list args)
628 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
631 static void *xreq_hooked_context_get_cb(struct afb_request *closure)
633 void *r = xreq_context_get_cb(closure);
634 struct afb_xreq *xreq = xreq_from_request(closure);
635 return afb_hook_xreq_context_get(xreq, r);
638 static void xreq_hooked_context_set_cb(struct afb_request *closure, void *value, void (*free_value)(void*))
640 struct afb_xreq *xreq = xreq_from_request(closure);
641 afb_hook_xreq_context_set(xreq, value, free_value);
642 xreq_context_set_cb(closure, value, free_value);
645 static struct afb_request *xreq_hooked_addref_cb(struct afb_request *closure)
647 struct afb_xreq *xreq = xreq_from_request(closure);
648 afb_hook_xreq_addref(xreq);
649 return xreq_addref_cb(closure);
652 static void xreq_hooked_unref_cb(struct afb_request *closure)
654 struct afb_xreq *xreq = xreq_from_request(closure);
655 afb_hook_xreq_unref(xreq);
656 xreq_unref_cb(closure);
659 static void xreq_hooked_session_close_cb(struct afb_request *closure)
661 struct afb_xreq *xreq = xreq_from_request(closure);
662 afb_hook_xreq_session_close(xreq);
663 xreq_session_close_cb(closure);
666 static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned level)
668 int r = xreq_session_set_LOA_cb(closure, level);
669 struct afb_xreq *xreq = xreq_from_request(closure);
670 return afb_hook_xreq_session_set_LOA(xreq, level, r);
673 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
674 static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event)
676 return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event));
679 static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
681 int r = xreq_subscribe_eventid_cb(closure, eventid);
682 struct afb_xreq *xreq = xreq_from_request(closure);
683 return afb_hook_xreq_subscribe(xreq, eventid, r);
686 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid);
687 static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event)
689 return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event));
692 static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid)
694 int r = xreq_unsubscribe_eventid_cb(closure, eventid);
695 struct afb_xreq *xreq = xreq_from_request(closure);
696 return afb_hook_xreq_unsubscribe(xreq, eventid, r);
699 static void xreq_hooked_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
701 struct afb_xreq *xreq = xreq_from_request(closure);
704 afb_hook_xreq_subcall(xreq, api, verb, args);
705 sc = subcall_alloc(xreq, api, verb, args);
708 callback(cb_closure, 1, afb_msg_json_internal_error());
709 json_object_put(args);
711 subcall_hooked(sc, callback, cb_closure);
715 static void xreq_hooked_subcall_req_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
717 struct afb_xreq *xreq = xreq_from_request(closure);
720 afb_hook_xreq_subcall_req(xreq, api, verb, args);
721 sc = subcall_alloc(xreq, api, verb, args);
724 callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_req(xreq));
725 json_object_put(args);
727 subcall_req_hooked(sc, callback, cb_closure);
731 static void xreq_hooked_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure)
733 struct afb_xreq *xreq = xreq_from_request(closure);
736 afb_hook_xreq_subcall(xreq, api, verb, args);
737 sc = subcall_alloc(xreq, api, verb, args);
740 callback(cb_closure, 1, afb_msg_json_internal_error(), xreq_to_request(xreq));
741 json_object_put(args);
743 subcall_request_hooked(sc, callback, cb_closure);
747 static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
750 struct afb_xreq *xreq = xreq_from_request(closure);
751 afb_hook_xreq_subcallsync(xreq, api, verb, args);
752 r = xreq_subcallsync_cb(closure, api, verb, args, result);
753 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
756 static void xreq_hooked_vverbose_cb(struct afb_request *closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
758 struct afb_xreq *xreq = xreq_from_request(closure);
761 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
762 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
766 static struct afb_stored_req *xreq_hooked_store_cb(struct afb_request *closure)
768 struct afb_xreq *xreq = xreq_from_request(closure);
769 struct afb_stored_req *r = xreq_store_cb(closure);
770 afb_hook_xreq_store(xreq, r);
774 static int xreq_hooked_has_permission_cb(struct afb_request *closure, const char *permission)
776 struct afb_xreq *xreq = xreq_from_request(closure);
777 int r = xreq_has_permission_cb(closure, permission);
778 return afb_hook_xreq_has_permission(xreq, permission, r);
781 static char *xreq_hooked_get_application_id_cb(struct afb_request *closure)
783 struct afb_xreq *xreq = xreq_from_request(closure);
784 char *r = xreq_get_application_id_cb(closure);
785 return afb_hook_xreq_get_application_id(xreq, r);
788 static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replace, void *(*create_value)(void*), void (*free_value)(void*), void *create_closure)
790 struct afb_xreq *xreq = xreq_from_request(closure);
791 void *result = xreq_context_make_cb(closure, replace, create_value, free_value, create_closure);
792 return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result);
795 static int xreq_hooked_get_uid_cb(struct afb_request *closure)
797 struct afb_xreq *xreq = xreq_from_request(closure);
798 int r = xreq_get_uid_cb(closure);
799 return afb_hook_xreq_get_uid(xreq, r);
802 /******************************************************************************/
804 const struct afb_request_itf xreq_itf = {
805 .json = xreq_json_cb,
807 .success = xreq_success_cb,
808 .fail = xreq_fail_cb,
809 .vsuccess = xreq_vsuccess_cb,
810 .vfail = xreq_vfail_cb,
811 .context_get = xreq_context_get_cb,
812 .context_set = xreq_context_set_cb,
813 .addref = xreq_addref_cb,
814 .unref = xreq_unref_cb,
815 .session_close = xreq_session_close_cb,
816 .session_set_LOA = xreq_session_set_LOA_cb,
817 .subscribe = xreq_subscribe_cb,
818 .unsubscribe = xreq_unsubscribe_cb,
819 .subcall = xreq_subcall_cb,
820 .subcallsync = xreq_subcallsync_cb,
821 .vverbose = xreq_vverbose_cb,
822 .store = xreq_store_cb,
823 .subcall_req = xreq_subcall_req_cb,
824 .has_permission = xreq_has_permission_cb,
825 .get_application_id = xreq_get_application_id_cb,
826 .context_make = xreq_context_make_cb,
827 .subscribe_eventid = xreq_subscribe_eventid_cb,
828 .unsubscribe_eventid = xreq_unsubscribe_eventid_cb,
829 .subcall_request = xreq_subcall_request_cb,
830 .get_uid = xreq_get_uid_cb,
833 const struct afb_request_itf xreq_hooked_itf = {
834 .json = xreq_hooked_json_cb,
835 .get = xreq_hooked_get_cb,
836 .success = xreq_hooked_success_cb,
837 .fail = xreq_hooked_fail_cb,
838 .vsuccess = xreq_hooked_vsuccess_cb,
839 .vfail = xreq_hooked_vfail_cb,
840 .context_get = xreq_hooked_context_get_cb,
841 .context_set = xreq_hooked_context_set_cb,
842 .addref = xreq_hooked_addref_cb,
843 .unref = xreq_hooked_unref_cb,
844 .session_close = xreq_hooked_session_close_cb,
845 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
846 .subscribe = xreq_hooked_subscribe_cb,
847 .unsubscribe = xreq_hooked_unsubscribe_cb,
848 .subcall = xreq_hooked_subcall_cb,
849 .subcallsync = xreq_hooked_subcallsync_cb,
850 .vverbose = xreq_hooked_vverbose_cb,
851 .store = xreq_hooked_store_cb,
852 .subcall_req = xreq_hooked_subcall_req_cb,
853 .has_permission = xreq_hooked_has_permission_cb,
854 .get_application_id = xreq_hooked_get_application_id_cb,
855 .context_make = xreq_hooked_context_make_cb,
856 .subscribe_eventid = xreq_hooked_subscribe_eventid_cb,
857 .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb,
858 .subcall_request = xreq_hooked_subcall_request_cb,
859 .get_uid = xreq_hooked_get_uid_cb,
862 /******************************************************************************/
864 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
866 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
868 afb_hook_xreq_unstore(xreq);
869 return xreq_to_req(xreq);
872 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
874 return afb_request_json(xreq_to_request(xreq));
877 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
879 afb_request_success(xreq_to_request(xreq), obj, info);
882 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
886 va_start(args, info);
887 afb_request_success_v(xreq_to_request(xreq), obj, info, args);
891 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
893 afb_request_fail(xreq_to_request(xreq), status, info);
896 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
900 va_start(args, info);
901 afb_request_fail_v(xreq_to_request(xreq), status, info, args);
906 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
908 struct json_object *obj = xreq_json_cb(xreq_to_request(xreq));
909 const char *result = json_object_to_json_string(obj);
911 *size = strlen(result);
915 void afb_xreq_addref(struct afb_xreq *xreq)
917 afb_request_addref(xreq_to_request(xreq));
920 void afb_xreq_unref(struct afb_xreq *xreq)
922 afb_request_unref(xreq_to_request(xreq));
925 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*, struct afb_request *), void *cb_closure)
927 xreq_subcall_request_cb(xreq_to_request(xreq), api, verb, args, callback, cb_closure);
930 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*, struct afb_request *), void *cb_closure)
932 afb_request_subcall(xreq_to_request(xreq), api, verb, args, callback, cb_closure);
935 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
937 return xreq_subcallsync_cb(xreq_to_request(xreq), api, verb, args, result);
940 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
942 return afb_request_subcall_sync(xreq_to_request(xreq), api, verb, args, result);
945 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
949 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
950 if (!afb_context_check(&xreq->context)) {
951 afb_context_close(&xreq->context);
952 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
958 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
959 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
960 if (!afb_context_check_loa(&xreq->context, loa)) {
961 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
967 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
968 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
969 if (afb_context_check_loa(&xreq->context, loa + 1)) {
970 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
976 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
977 afb_context_refresh(&xreq->context);
979 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
980 afb_context_change_loa(&xreq->context, 0);
981 afb_context_close(&xreq->context);
987 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
991 if (sessionflags != 0) {
992 if (!afb_context_check(&xreq->context)) {
993 afb_context_close(&xreq->context);
994 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
1000 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
1001 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
1002 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
1007 if (auth && !afb_auth_check(xreq, auth)) {
1008 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
1013 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
1014 afb_context_refresh(&xreq->context);
1016 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
1017 afb_context_close(&xreq->context);
1023 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
1026 afb_xreq_fail_unknown_verb(xreq);
1028 if (!xreq_session_check_apply_v1(xreq, verb->session))
1029 verb->callback(xreq_to_req(xreq));
1032 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
1035 afb_xreq_fail_unknown_verb(xreq);
1037 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
1038 verb->callback(xreq_to_req(xreq));
1041 void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb)
1044 afb_xreq_fail_unknown_verb(xreq);
1046 if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0)
1047 verb->callback(xreq_to_request(xreq));
1050 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
1052 memset(xreq, 0, sizeof *xreq);
1053 xreq->request.itf = &xreq_hooked_itf; /* hook by default */
1055 xreq->queryitf = queryitf;
1058 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
1060 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1063 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
1065 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api);
1068 static void init_hooking(struct afb_xreq *xreq)
1070 afb_hook_init_xreq(xreq);
1071 if (xreq->hookflags)
1072 afb_hook_xreq_begin(xreq);
1074 xreq->request.itf = &xreq_itf; /* unhook the interface */
1078 * job callback for asynchronous and secured processing of the request.
1080 static void process_async(int signum, void *arg)
1082 struct afb_xreq *xreq = arg;
1083 const struct afb_api *api;
1086 /* emit the error (assumes that hooking is initialised) */
1087 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
1091 /* invoke api call method to process the reqiest */
1092 api = (const struct afb_api*)xreq->context.api_key;
1093 api->itf->call(api->closure, xreq);
1095 /* release the request */
1096 afb_xreq_unhooked_unref(xreq);
1100 * Early request failure of the request 'xreq' with, as usual, 'status' and 'info'
1101 * The early failure occurs only in function 'afb_xreq_process' where normally,
1102 * the hooking is not initialised. So this "early" failure takes care to initialise
1103 * the hooking in first.
1105 static void early_failure(struct afb_xreq *xreq, const char *status, const char *info, ...)
1113 va_start(args, info);
1114 afb_request_fail_v(xreq_to_request(xreq), status, info, args);
1119 * Enqueue a job for processing the request 'xreq' using the given 'apiset'.
1120 * Errors are reported as request failures.
1122 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
1124 const struct afb_api *api;
1125 struct afb_xreq *caller;
1127 /* lookup at the api */
1128 xreq->apiset = apiset;
1129 api = afb_apiset_lookup_started(apiset, xreq->request.api, 1);
1131 if (errno == ENOENT)
1132 early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb);
1134 early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api);
1137 xreq->context.api_key = api;
1139 /* check self locking */
1141 caller = xreq->caller;
1143 if (((const struct afb_api *)caller->context.api_key)->group == api->group) {
1144 /* noconcurrency lock detected */
1145 ERROR("self-lock detected in call stack for API %s", xreq->request.api);
1146 early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api);
1149 caller = caller->caller;
1153 /* queue the request job */
1154 afb_xreq_unhooked_addref(xreq);
1155 if (jobs_queue(api->group, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
1156 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
1157 ERROR("can't process job with threads: %m");
1158 early_failure(xreq, "cancelled", "not able to create a job for the task");
1159 afb_xreq_unhooked_unref(xreq);
1162 afb_xreq_unhooked_unref(xreq);