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.h>
29 #include "afb-context.h"
32 #include "afb-msg-json.h"
33 #include "afb-subcall.h"
36 #include "afb-apiset.h"
41 /******************************************************************************/
43 static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
46 if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
48 fun(first, second, info);
52 /******************************************************************************/
54 static struct json_object *xreq_json_cb(void *closure)
56 struct afb_xreq *xreq = closure;
57 return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
60 static struct afb_arg xreq_get_cb(void *closure, const char *name)
62 struct afb_xreq *xreq = closure;
63 if (xreq->queryitf->get)
64 return xreq->queryitf->get(xreq, name);
66 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
69 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
71 struct afb_xreq *xreq = closure;
74 ERROR("reply called more than one time!!");
78 if (xreq->queryitf->success)
79 xreq->queryitf->success(xreq, obj, info);
81 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
85 static void xreq_fail_cb(void *closure, const char *status, const char *info)
87 struct afb_xreq *xreq = closure;
90 ERROR("reply called more than one time!!");
93 if (xreq->queryitf->fail)
94 xreq->queryitf->fail(xreq, status, info);
96 xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
100 static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
102 vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
105 static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
107 vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
110 static void *xreq_context_get_cb(void *closure)
112 struct afb_xreq *xreq = closure;
113 return afb_context_get(&xreq->context);
116 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
118 struct afb_xreq *xreq = closure;
119 afb_context_set(&xreq->context, value, free_value);
122 static void xreq_addref_cb(void *closure)
124 struct afb_xreq *xreq = closure;
125 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
128 static void xreq_unref_cb(void *closure)
130 struct afb_xreq *xreq = closure;
131 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
132 xreq->queryitf->unref(xreq);
136 static void xreq_session_close_cb(void *closure)
138 struct afb_xreq *xreq = closure;
139 afb_context_close(&xreq->context);
142 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
144 struct afb_xreq *xreq = closure;
145 return afb_context_change_loa(&xreq->context, level);
148 static int xreq_subscribe_cb(void *closure, struct afb_event event)
150 struct afb_xreq *xreq = closure;
151 return afb_xreq_subscribe(xreq, event);
154 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
157 return afb_evt_add_watch(xreq->listener, event);
158 if (xreq->queryitf->subscribe)
159 return xreq->queryitf->subscribe(xreq, event);
160 ERROR("no event listener, subscription impossible");
165 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
167 struct afb_xreq *xreq = closure;
168 return afb_xreq_unsubscribe(xreq, event);
171 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
174 return afb_evt_remove_watch(xreq->listener, event);
175 if (xreq->queryitf->unsubscribe)
176 return xreq->queryitf->unsubscribe(xreq, event);
177 ERROR("no event listener, unsubscription impossible");
182 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)
184 struct afb_xreq *xreq = closure;
186 if (xreq->queryitf->subcall) {
187 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
188 json_object_put(args);
190 afb_subcall(xreq, api, verb, args, callback, cb_closure);
196 struct afb_xreq *caller;
199 struct json_object *args;
200 struct jobloop *jobloop;
201 struct json_object *result;
205 static void xreq_sync_leave(struct xreq_sync *sync)
207 struct jobloop *jobloop = sync->jobloop;
209 sync->jobloop = NULL;
214 static void xreq_sync_reply(void *closure, int iserror, struct json_object *obj)
216 struct xreq_sync *sync = closure;
218 sync->iserror = iserror;
219 sync->result = json_object_get(obj);
220 xreq_sync_leave(sync);
223 static void xreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
225 struct xreq_sync *sync = closure;
228 sync->jobloop = jobloop;
229 xreq_subcall_cb(sync->caller, sync->api, sync->verb, json_object_get(sync->args), xreq_sync_reply, sync);
232 xreq_sync_leave(sync);
236 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
239 struct xreq_sync sync;
240 struct afb_xreq *xreq = closure;
250 rc = jobs_enter(NULL, 0, xreq_sync_enter, &sync);
251 json_object_put(args);
252 if (rc < 0 || sync.iserror) {
253 *result = sync.result ? : afb_msg_json_internal_error();
256 *result = sync.result;
260 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
262 /* TODO: improves the implementation. example: on condition make a list of log messages that will be returned */
263 vverbose(level, file, line, func, fmt, args);
266 /******************************************************************************/
268 static struct json_object *xreq_hooked_json_cb(void *closure)
270 struct json_object *r = xreq_json_cb(closure);
271 struct afb_xreq *xreq = closure;
272 return afb_hook_xreq_json(xreq, r);
275 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
277 struct afb_arg r = xreq_get_cb(closure, name);
278 struct afb_xreq *xreq = closure;
279 return afb_hook_xreq_get(xreq, name, r);
282 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
284 struct afb_xreq *xreq = closure;
285 afb_hook_xreq_success(xreq, obj, info);
286 xreq_success_cb(closure, obj, info);
289 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
291 struct afb_xreq *xreq = closure;
292 afb_hook_xreq_fail(xreq, status, info);
293 xreq_fail_cb(closure, status, info);
296 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
298 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
301 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
303 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
306 static void *xreq_hooked_context_get_cb(void *closure)
308 void *r = xreq_context_get_cb(closure);
309 struct afb_xreq *xreq = closure;
310 return afb_hook_xreq_context_get(xreq, r);
313 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
315 struct afb_xreq *xreq = closure;
316 afb_hook_xreq_context_set(xreq, value, free_value);
317 xreq_context_set_cb(closure, value, free_value);
320 static void xreq_hooked_addref_cb(void *closure)
322 struct afb_xreq *xreq = closure;
323 afb_hook_xreq_addref(xreq);
324 xreq_addref_cb(closure);
327 static void xreq_hooked_unref_cb(void *closure)
329 struct afb_xreq *xreq = closure;
330 afb_hook_xreq_unref(xreq);
331 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
332 afb_hook_xreq_end(xreq);
333 xreq->queryitf->unref(xreq);
337 static void xreq_hooked_session_close_cb(void *closure)
339 struct afb_xreq *xreq = closure;
340 afb_hook_xreq_session_close(xreq);
341 xreq_session_close_cb(closure);
344 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
346 int r = xreq_session_set_LOA_cb(closure, level);
347 struct afb_xreq *xreq = closure;
348 return afb_hook_xreq_session_set_LOA(xreq, level, r);
351 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
353 int r = xreq_subscribe_cb(closure, event);
354 struct afb_xreq *xreq = closure;
355 return afb_hook_xreq_subscribe(xreq, event, r);
358 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
360 int r = xreq_unsubscribe_cb(closure, event);
361 struct afb_xreq *xreq = closure;
362 return afb_hook_xreq_unsubscribe(xreq, event, r);
367 struct afb_xreq *xreq;
368 void (*callback)(void*, int, struct json_object*);
372 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
374 struct reply *reply = closure;
376 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
377 reply->callback(reply->closure, iserror, result);
381 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)
383 struct reply *reply = malloc(sizeof *reply);
384 struct afb_xreq *xreq = closure;
385 afb_hook_xreq_subcall(xreq, api, verb, args);
388 reply->callback = callback;
389 reply->closure = cb_closure;
390 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
392 ERROR("out of memory");
393 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
397 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
400 struct afb_xreq *xreq = closure;
401 afb_hook_xreq_subcallsync(xreq, api, verb, args);
402 r = xreq_subcallsync_cb(closure, api, verb, args, result);
403 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
406 static void xreq_hooked_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
408 struct afb_xreq *xreq = closure;
411 xreq_vverbose_cb(closure, level, file, line, func, fmt, args);
412 afb_hook_xreq_vverbose(xreq, level, file, line, func, fmt, ap);
416 /******************************************************************************/
418 const struct afb_req_itf xreq_itf = {
419 .json = xreq_json_cb,
421 .success = xreq_success_cb,
422 .fail = xreq_fail_cb,
423 .vsuccess = xreq_vsuccess_cb,
424 .vfail = xreq_vfail_cb,
425 .context_get = xreq_context_get_cb,
426 .context_set = xreq_context_set_cb,
427 .addref = xreq_addref_cb,
428 .unref = xreq_unref_cb,
429 .session_close = xreq_session_close_cb,
430 .session_set_LOA = xreq_session_set_LOA_cb,
431 .subscribe = xreq_subscribe_cb,
432 .unsubscribe = xreq_unsubscribe_cb,
433 .subcall = xreq_subcall_cb,
434 .subcallsync = xreq_subcallsync_cb,
435 .vverbose = xreq_vverbose_cb
438 const struct afb_req_itf xreq_hooked_itf = {
439 .json = xreq_hooked_json_cb,
440 .get = xreq_hooked_get_cb,
441 .success = xreq_hooked_success_cb,
442 .fail = xreq_hooked_fail_cb,
443 .vsuccess = xreq_hooked_vsuccess_cb,
444 .vfail = xreq_hooked_vfail_cb,
445 .context_get = xreq_hooked_context_get_cb,
446 .context_set = xreq_hooked_context_set_cb,
447 .addref = xreq_hooked_addref_cb,
448 .unref = xreq_hooked_unref_cb,
449 .session_close = xreq_hooked_session_close_cb,
450 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
451 .subscribe = xreq_hooked_subscribe_cb,
452 .unsubscribe = xreq_hooked_unsubscribe_cb,
453 .subcall = xreq_hooked_subcall_cb,
454 .subcallsync = xreq_hooked_subcallsync_cb,
455 .vverbose = xreq_hooked_vverbose_cb
458 static inline struct afb_req to_req(struct afb_xreq *xreq)
460 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
463 /******************************************************************************/
465 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
467 return afb_req_json(to_req(xreq));
470 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
472 afb_req_success(to_req(xreq), obj, info);
475 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
479 va_start(args, info);
480 if (info == NULL || vasprintf(&message, info, args) < 0)
483 afb_xreq_success(xreq, obj, message);
487 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
489 afb_req_fail(to_req(xreq), status, info);
492 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
496 va_start(args, info);
497 if (info == NULL || vasprintf(&message, info, args) < 0)
500 afb_xreq_fail(xreq, status, message);
504 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
506 struct json_object *obj = xreq_json_cb(xreq);
507 const char *result = json_object_to_json_string(obj);
509 *size = strlen(result);
513 void afb_xreq_addref(struct afb_xreq *xreq)
515 afb_req_addref(to_req(xreq));
518 void afb_xreq_unref(struct afb_xreq *xreq)
520 afb_req_unref(to_req(xreq));
523 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)
525 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
528 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)
530 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
533 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
535 return xreq_subcallsync_cb(xreq, api, verb, args, result);
538 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
540 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
543 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
547 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
548 if (!afb_context_check(&xreq->context)) {
549 afb_context_close(&xreq->context);
550 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
556 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
557 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
558 if (!afb_context_check_loa(&xreq->context, loa)) {
559 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
565 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
566 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
567 if (afb_context_check_loa(&xreq->context, loa + 1)) {
568 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
574 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
575 afb_context_refresh(&xreq->context);
577 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
578 afb_context_change_loa(&xreq->context, 0);
579 afb_context_close(&xreq->context);
585 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
589 if (sessionflags != 0) {
590 if (!afb_context_check(&xreq->context)) {
591 afb_context_close(&xreq->context);
592 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
598 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
599 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
600 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
605 if (auth && !afb_auth_check(auth, xreq)) {
606 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
611 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
612 afb_context_refresh(&xreq->context);
614 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
615 afb_context_close(&xreq->context);
621 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
624 afb_xreq_fail_unknown_verb(xreq);
626 if (!xreq_session_check_apply_v1(xreq, verb->session))
627 verb->callback(to_req(xreq));
630 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
633 afb_xreq_fail_unknown_verb(xreq);
635 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
636 verb->callback(to_req(xreq));
639 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
641 memset(xreq, 0, sizeof *xreq);
643 xreq->queryitf = queryitf;
646 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
648 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
651 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
653 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
656 static void process_sync(struct afb_xreq *xreq)
661 afb_hook_init_xreq(xreq);
663 afb_hook_xreq_begin(xreq);
666 if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
667 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
669 xreq->context.api_key = api.closure;
670 api.itf->call(api.closure, xreq);
674 static void process_async(int signum, void *arg)
676 struct afb_xreq *xreq = arg;
679 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
683 afb_xreq_unref(xreq);
686 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
688 xreq->apiset = apiset;
690 afb_xreq_addref(xreq);
691 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
692 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
693 ERROR("can't process job with threads: %m");
694 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
695 afb_xreq_unref(xreq);
697 afb_xreq_unref(xreq);