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.
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
28 #include <afb/afb-binding-v2.h>
30 #include "afb-context.h"
33 #include "afb-msg-json.h"
34 #include "afb-subcall.h"
37 #include "afb-apiset.h"
42 /******************************************************************************/
44 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
47 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
49 fun(first, second, info);
53 /******************************************************************************/
55 static struct json_object *xreq_json_cb(void *closure)
57 struct afb_xreq *xreq = closure;
58 if (!xreq->json && xreq->queryitf->json)
59 xreq->json = xreq->queryitf->json(xreq);
63 static struct afb_arg xreq_get_cb(void *closure, const char *name)
65 struct afb_xreq *xreq = closure;
67 struct json_object *object, *value;
69 if (xreq->queryitf->get)
70 arg = xreq->queryitf->get(xreq, name);
72 object = xreq_json_cb(closure);
73 if (json_object_object_get_ex(object, name, &value)) {
75 arg.value = json_object_get_string(value);
85 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
87 struct afb_xreq *xreq = closure;
90 ERROR("reply called more than one time!!");
94 if (xreq->queryitf->success)
95 xreq->queryitf->success(xreq, obj, info);
97 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
101 static void xreq_fail_cb(void *closure, const char *status, const char *info)
103 struct afb_xreq *xreq = closure;
106 ERROR("reply called more than one time!!");
109 if (xreq->queryitf->fail)
110 xreq->queryitf->fail(xreq, status, info);
112 xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
116 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
118 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
121 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
123 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
126 static void *xreq_context_get_cb(void *closure)
128 struct afb_xreq *xreq = closure;
129 return afb_context_get(&xreq->context);
132 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
134 struct afb_xreq *xreq = closure;
135 afb_context_set(&xreq->context, value, free_value);
138 static void xreq_addref_cb(void *closure)
140 struct afb_xreq *xreq = closure;
141 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
144 static void xreq_unref_cb(void *closure)
146 struct afb_xreq *xreq = closure;
147 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
148 xreq->queryitf->unref(xreq);
152 static void xreq_session_close_cb(void *closure)
154 struct afb_xreq *xreq = closure;
155 afb_context_close(&xreq->context);
158 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
160 struct afb_xreq *xreq = closure;
161 return afb_context_change_loa(&xreq->context, level);
164 static int xreq_subscribe_cb(void *closure, struct afb_event event)
166 struct afb_xreq *xreq = closure;
167 return afb_xreq_subscribe(xreq, event);
170 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
173 return afb_evt_add_watch(xreq->listener, event);
174 if (xreq->queryitf->subscribe)
175 return xreq->queryitf->subscribe(xreq, event);
176 ERROR("no event listener, subscription impossible");
181 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
183 struct afb_xreq *xreq = closure;
184 return afb_xreq_unsubscribe(xreq, event);
187 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
190 return afb_evt_remove_watch(xreq->listener, event);
191 if (xreq->queryitf->unsubscribe)
192 return xreq->queryitf->unsubscribe(xreq, event);
193 ERROR("no event listener, unsubscription impossible");
198 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)
200 struct afb_xreq *xreq = closure;
202 if (xreq->queryitf->subcall) {
203 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
204 json_object_put(args);
206 afb_subcall(xreq, api, verb, args, callback, cb_closure);
212 struct afb_xreq *caller;
215 struct json_object *args;
216 struct jobloop *jobloop;
217 struct json_object *result;
221 static void xreq_sync_leave(struct xreq_sync *sync)
223 struct jobloop *jobloop = sync->jobloop;
225 sync->jobloop = NULL;
230 static void xreq_sync_reply(void *closure, int iserror, struct json_object *obj)
232 struct xreq_sync *sync = closure;
234 sync->iserror = iserror;
235 sync->result = json_object_get(obj);
236 xreq_sync_leave(sync);
239 static void xreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
241 struct xreq_sync *sync = closure;
244 sync->jobloop = jobloop;
245 xreq_subcall_cb(sync->caller, sync->api, sync->verb, json_object_get(sync->args), xreq_sync_reply, sync);
248 xreq_sync_leave(sync);
252 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
255 struct xreq_sync sync;
256 struct afb_xreq *xreq = closure;
266 rc = jobs_enter(NULL, 0, xreq_sync_enter, &sync);
267 json_object_put(args);
268 if (rc < 0 || sync.iserror) {
269 *result = sync.result ? : afb_msg_json_internal_error();
272 *result = sync.result;
276 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
278 /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
279 vverbose(level, file, line, func, fmt, args);
282 static struct afb_stored_req *xreq_store_cb(void *closure)
284 xreq_addref_cb(closure);
288 /******************************************************************************/
290 static struct json_object *xreq_hooked_json_cb(void *closure)
292 struct json_object *r = xreq_json_cb(closure);
293 struct afb_xreq *xreq = closure;
294 return afb_hook_xreq_json(xreq, r);
297 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
299 struct afb_arg r = xreq_get_cb(closure, name);
300 struct afb_xreq *xreq = closure;
301 return afb_hook_xreq_get(xreq, name, r);
304 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
306 struct afb_xreq *xreq = closure;
307 afb_hook_xreq_success(xreq, obj, info);
308 xreq_success_cb(closure, obj, info);
311 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
313 struct afb_xreq *xreq = closure;
314 afb_hook_xreq_fail(xreq, status, info);
315 xreq_fail_cb(closure, status, info);
318 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
320 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
323 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
325 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
328 static void *xreq_hooked_context_get_cb(void *closure)
330 void *r = xreq_context_get_cb(closure);
331 struct afb_xreq *xreq = closure;
332 return afb_hook_xreq_context_get(xreq, r);
335 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
337 struct afb_xreq *xreq = closure;
338 afb_hook_xreq_context_set(xreq, value, free_value);
339 xreq_context_set_cb(closure, value, free_value);
342 static void xreq_hooked_addref_cb(void *closure)
344 struct afb_xreq *xreq = closure;
345 afb_hook_xreq_addref(xreq);
346 xreq_addref_cb(closure);
349 static void xreq_hooked_unref_cb(void *closure)
351 struct afb_xreq *xreq = closure;
352 afb_hook_xreq_unref(xreq);
353 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
354 afb_hook_xreq_end(xreq);
355 xreq->queryitf->unref(xreq);
359 static void xreq_hooked_session_close_cb(void *closure)
361 struct afb_xreq *xreq = closure;
362 afb_hook_xreq_session_close(xreq);
363 xreq_session_close_cb(closure);
366 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
368 int r = xreq_session_set_LOA_cb(closure, level);
369 struct afb_xreq *xreq = closure;
370 return afb_hook_xreq_session_set_LOA(xreq, level, r);
373 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
375 int r = xreq_subscribe_cb(closure, event);
376 struct afb_xreq *xreq = closure;
377 return afb_hook_xreq_subscribe(xreq, event, r);
380 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
382 int r = xreq_unsubscribe_cb(closure, event);
383 struct afb_xreq *xreq = closure;
384 return afb_hook_xreq_unsubscribe(xreq, event, r);
389 struct afb_xreq *xreq;
390 void (*callback)(void*, int, struct json_object*);
394 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
396 struct reply *reply = closure;
398 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
399 reply->callback(reply->closure, iserror, result);
403 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)
405 struct reply *reply = malloc(sizeof *reply);
406 struct afb_xreq *xreq = closure;
407 afb_hook_xreq_subcall(xreq, api, verb, args);
410 reply->callback = callback;
411 reply->closure = cb_closure;
412 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
414 ERROR("out of memory");
415 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
419 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
422 struct afb_xreq *xreq = closure;
423 afb_hook_xreq_subcallsync(xreq, api, verb, args);
424 r = xreq_subcallsync_cb(closure, api, verb, args, result);
425 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
428 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
430 struct afb_xreq *xreq = closure;
433 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
434 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
438 static struct afb_stored_req *xreq_hooked_store_cb(void *closure)
440 struct afb_xreq *xreq = closure;
441 struct afb_stored_req *r = xreq_store_cb(closure);
442 afb_hook_xreq_store(xreq, r);
446 /******************************************************************************/
448 const struct afb_req_itf xreq_itf = {
449 .json = xreq_json_cb,
451 .success = xreq_success_cb,
452 .fail = xreq_fail_cb,
453 .vsuccess = xreq_vsuccess_cb,
454 .vfail = xreq_vfail_cb,
455 .context_get = xreq_context_get_cb,
456 .context_set = xreq_context_set_cb,
457 .addref = xreq_addref_cb,
458 .unref = xreq_unref_cb,
459 .session_close = xreq_session_close_cb,
460 .session_set_LOA = xreq_session_set_LOA_cb,
461 .subscribe = xreq_subscribe_cb,
462 .unsubscribe = xreq_unsubscribe_cb,
463 .subcall = xreq_subcall_cb,
464 .subcallsync = xreq_subcallsync_cb,
465 .vverbose = xreq_vverbose_cb,
466 .store = xreq_store_cb
469 const struct afb_req_itf xreq_hooked_itf = {
470 .json = xreq_hooked_json_cb,
471 .get = xreq_hooked_get_cb,
472 .success = xreq_hooked_success_cb,
473 .fail = xreq_hooked_fail_cb,
474 .vsuccess = xreq_hooked_vsuccess_cb,
475 .vfail = xreq_hooked_vfail_cb,
476 .context_get = xreq_hooked_context_get_cb,
477 .context_set = xreq_hooked_context_set_cb,
478 .addref = xreq_hooked_addref_cb,
479 .unref = xreq_hooked_unref_cb,
480 .session_close = xreq_hooked_session_close_cb,
481 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
482 .subscribe = xreq_hooked_subscribe_cb,
483 .unsubscribe = xreq_hooked_unsubscribe_cb,
484 .subcall = xreq_hooked_subcall_cb,
485 .subcallsync = xreq_hooked_subcallsync_cb,
486 .vverbose = xreq_hooked_vverbose_cb,
487 .store = xreq_hooked_store_cb
490 static inline struct afb_req to_req(struct afb_xreq *xreq)
492 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
495 /******************************************************************************/
497 struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq)
499 struct afb_xreq *xreq = (struct afb_xreq *)sreq;
501 afb_hook_xreq_unstore(xreq);
505 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
507 return afb_req_json(to_req(xreq));
510 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
512 afb_req_success(to_req(xreq), obj, info);
515 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
519 va_start(args, info);
520 if (info == NULL || vasprintf(&message, info, args) < 0)
523 afb_xreq_success(xreq, obj, message);
527 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
529 afb_req_fail(to_req(xreq), status, info);
532 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
536 va_start(args, info);
537 if (info == NULL || vasprintf(&message, info, args) < 0)
540 afb_xreq_fail(xreq, status, message);
544 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
546 struct json_object *obj = xreq_json_cb(xreq);
547 const char *result = json_object_to_json_string(obj);
549 *size = strlen(result);
553 void afb_xreq_addref(struct afb_xreq *xreq)
555 afb_req_addref(to_req(xreq));
558 void afb_xreq_unref(struct afb_xreq *xreq)
560 afb_req_unref(to_req(xreq));
563 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)
565 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
568 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)
570 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
573 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
575 return xreq_subcallsync_cb(xreq, api, verb, args, result);
578 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
580 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
583 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
587 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
588 if (!afb_context_check(&xreq->context)) {
589 afb_context_close(&xreq->context);
590 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
596 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
597 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
598 if (!afb_context_check_loa(&xreq->context, loa)) {
599 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
605 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
606 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
607 if (afb_context_check_loa(&xreq->context, loa + 1)) {
608 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
614 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
615 afb_context_refresh(&xreq->context);
617 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
618 afb_context_change_loa(&xreq->context, 0);
619 afb_context_close(&xreq->context);
625 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
629 if (sessionflags != 0) {
630 if (!afb_context_check(&xreq->context)) {
631 afb_context_close(&xreq->context);
632 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
638 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
639 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
640 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
645 if (auth && !afb_auth_check(auth, xreq)) {
646 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
651 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
652 afb_context_refresh(&xreq->context);
654 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
655 afb_context_close(&xreq->context);
661 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
664 afb_xreq_fail_unknown_verb(xreq);
666 if (!xreq_session_check_apply_v1(xreq, verb->session))
667 verb->callback(to_req(xreq));
670 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
673 afb_xreq_fail_unknown_verb(xreq);
675 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
676 verb->callback(to_req(xreq));
679 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
681 memset(xreq, 0, sizeof *xreq);
683 xreq->queryitf = queryitf;
686 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
688 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
691 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
693 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
696 static void process_sync(struct afb_xreq *xreq)
701 afb_hook_init_xreq(xreq);
703 afb_hook_xreq_begin(xreq);
706 if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
708 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
710 afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
712 xreq->context.api_key = api.closure;
713 api.itf->call(api.closure, xreq);
717 static void process_async(int signum, void *arg)
719 struct afb_xreq *xreq = arg;
722 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
726 afb_xreq_unref(xreq);
729 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
731 xreq->apiset = apiset;
733 afb_xreq_addref(xreq);
734 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
735 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
736 ERROR("can't process job with threads: %m");
737 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
738 afb_xreq_unref(xreq);
740 afb_xreq_unref(xreq);