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 /******************************************************************************/
284 static struct json_object *xreq_hooked_json_cb(void *closure)
286 struct json_object *r = xreq_json_cb(closure);
287 struct afb_xreq *xreq = closure;
288 return afb_hook_xreq_json(xreq, r);
291 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
293 struct afb_arg r = xreq_get_cb(closure, name);
294 struct afb_xreq *xreq = closure;
295 return afb_hook_xreq_get(xreq, name, r);
298 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
300 struct afb_xreq *xreq = closure;
301 afb_hook_xreq_success(xreq, obj, info);
302 xreq_success_cb(closure, obj, info);
305 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
307 struct afb_xreq *xreq = closure;
308 afb_hook_xreq_fail(xreq, status, info);
309 xreq_fail_cb(closure, status, info);
312 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
314 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
317 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
319 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
322 static void *xreq_hooked_context_get_cb(void *closure)
324 void *r = xreq_context_get_cb(closure);
325 struct afb_xreq *xreq = closure;
326 return afb_hook_xreq_context_get(xreq, r);
329 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
331 struct afb_xreq *xreq = closure;
332 afb_hook_xreq_context_set(xreq, value, free_value);
333 xreq_context_set_cb(closure, value, free_value);
336 static void xreq_hooked_addref_cb(void *closure)
338 struct afb_xreq *xreq = closure;
339 afb_hook_xreq_addref(xreq);
340 xreq_addref_cb(closure);
343 static void xreq_hooked_unref_cb(void *closure)
345 struct afb_xreq *xreq = closure;
346 afb_hook_xreq_unref(xreq);
347 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
348 afb_hook_xreq_end(xreq);
349 xreq->queryitf->unref(xreq);
353 static void xreq_hooked_session_close_cb(void *closure)
355 struct afb_xreq *xreq = closure;
356 afb_hook_xreq_session_close(xreq);
357 xreq_session_close_cb(closure);
360 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
362 int r = xreq_session_set_LOA_cb(closure, level);
363 struct afb_xreq *xreq = closure;
364 return afb_hook_xreq_session_set_LOA(xreq, level, r);
367 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
369 int r = xreq_subscribe_cb(closure, event);
370 struct afb_xreq *xreq = closure;
371 return afb_hook_xreq_subscribe(xreq, event, r);
374 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
376 int r = xreq_unsubscribe_cb(closure, event);
377 struct afb_xreq *xreq = closure;
378 return afb_hook_xreq_unsubscribe(xreq, event, r);
383 struct afb_xreq *xreq;
384 void (*callback)(void*, int, struct json_object*);
388 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
390 struct reply *reply = closure;
392 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
393 reply->callback(reply->closure, iserror, result);
397 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)
399 struct reply *reply = malloc(sizeof *reply);
400 struct afb_xreq *xreq = closure;
401 afb_hook_xreq_subcall(xreq, api, verb, args);
404 reply->callback = callback;
405 reply->closure = cb_closure;
406 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
408 ERROR("out of memory");
409 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
413 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
416 struct afb_xreq *xreq = closure;
417 afb_hook_xreq_subcallsync(xreq, api, verb, args);
418 r = xreq_subcallsync_cb(closure, api, verb, args, result);
419 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
422 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
424 struct afb_xreq *xreq = closure;
427 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
428 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
432 /******************************************************************************/
434 const struct afb_req_itf xreq_itf = {
435 .json = xreq_json_cb,
437 .success = xreq_success_cb,
438 .fail = xreq_fail_cb,
439 .vsuccess = xreq_vsuccess_cb,
440 .vfail = xreq_vfail_cb,
441 .context_get = xreq_context_get_cb,
442 .context_set = xreq_context_set_cb,
443 .addref = xreq_addref_cb,
444 .unref = xreq_unref_cb,
445 .session_close = xreq_session_close_cb,
446 .session_set_LOA = xreq_session_set_LOA_cb,
447 .subscribe = xreq_subscribe_cb,
448 .unsubscribe = xreq_unsubscribe_cb,
449 .subcall = xreq_subcall_cb,
450 .subcallsync = xreq_subcallsync_cb,
451 .vverbose = xreq_vverbose_cb
454 const struct afb_req_itf xreq_hooked_itf = {
455 .json = xreq_hooked_json_cb,
456 .get = xreq_hooked_get_cb,
457 .success = xreq_hooked_success_cb,
458 .fail = xreq_hooked_fail_cb,
459 .vsuccess = xreq_hooked_vsuccess_cb,
460 .vfail = xreq_hooked_vfail_cb,
461 .context_get = xreq_hooked_context_get_cb,
462 .context_set = xreq_hooked_context_set_cb,
463 .addref = xreq_hooked_addref_cb,
464 .unref = xreq_hooked_unref_cb,
465 .session_close = xreq_hooked_session_close_cb,
466 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
467 .subscribe = xreq_hooked_subscribe_cb,
468 .unsubscribe = xreq_hooked_unsubscribe_cb,
469 .subcall = xreq_hooked_subcall_cb,
470 .subcallsync = xreq_hooked_subcallsync_cb,
471 .vverbose = xreq_hooked_vverbose_cb
474 static inline struct afb_req to_req(struct afb_xreq *xreq)
476 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
479 /******************************************************************************/
481 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
483 return afb_req_json(to_req(xreq));
486 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
488 afb_req_success(to_req(xreq), obj, info);
491 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
495 va_start(args, info);
496 if (info == NULL || vasprintf(&message, info, args) < 0)
499 afb_xreq_success(xreq, obj, message);
503 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
505 afb_req_fail(to_req(xreq), status, info);
508 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
512 va_start(args, info);
513 if (info == NULL || vasprintf(&message, info, args) < 0)
516 afb_xreq_fail(xreq, status, message);
520 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
522 struct json_object *obj = xreq_json_cb(xreq);
523 const char *result = json_object_to_json_string(obj);
525 *size = strlen(result);
529 void afb_xreq_addref(struct afb_xreq *xreq)
531 afb_req_addref(to_req(xreq));
534 void afb_xreq_unref(struct afb_xreq *xreq)
536 afb_req_unref(to_req(xreq));
539 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)
541 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
544 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)
546 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
549 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
551 return xreq_subcallsync_cb(xreq, api, verb, args, result);
554 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
556 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
559 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
563 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
564 if (!afb_context_check(&xreq->context)) {
565 afb_context_close(&xreq->context);
566 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
572 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
573 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
574 if (!afb_context_check_loa(&xreq->context, loa)) {
575 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
581 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
582 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
583 if (afb_context_check_loa(&xreq->context, loa + 1)) {
584 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
590 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
591 afb_context_refresh(&xreq->context);
593 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
594 afb_context_change_loa(&xreq->context, 0);
595 afb_context_close(&xreq->context);
601 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
605 if (sessionflags != 0) {
606 if (!afb_context_check(&xreq->context)) {
607 afb_context_close(&xreq->context);
608 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
614 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
615 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
616 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
621 if (auth && !afb_auth_check(auth, xreq)) {
622 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
627 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
628 afb_context_refresh(&xreq->context);
630 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
631 afb_context_close(&xreq->context);
637 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
640 afb_xreq_fail_unknown_verb(xreq);
642 if (!xreq_session_check_apply_v1(xreq, verb->session))
643 verb->callback(to_req(xreq));
646 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
649 afb_xreq_fail_unknown_verb(xreq);
651 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
652 verb->callback(to_req(xreq));
655 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
657 memset(xreq, 0, sizeof *xreq);
659 xreq->queryitf = queryitf;
662 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
664 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
667 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
669 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
672 static void process_sync(struct afb_xreq *xreq)
677 afb_hook_init_xreq(xreq);
679 afb_hook_xreq_begin(xreq);
682 if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
684 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
686 afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
688 xreq->context.api_key = api.closure;
689 api.itf->call(api.closure, xreq);
693 static void process_async(int signum, void *arg)
695 struct afb_xreq *xreq = arg;
698 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
702 afb_xreq_unref(xreq);
705 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
707 xreq->apiset = apiset;
709 afb_xreq_addref(xreq);
710 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
711 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
712 ERROR("can't process job with threads: %m");
713 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
714 afb_xreq_unref(xreq);
716 afb_xreq_unref(xreq);