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 /******************************************************************************/
262 static struct json_object *xreq_hooked_json_cb(void *closure)
264 struct json_object *r = xreq_json_cb(closure);
265 struct afb_xreq *xreq = closure;
266 return afb_hook_xreq_json(xreq, r);
269 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
271 struct afb_arg r = xreq_get_cb(closure, name);
272 struct afb_xreq *xreq = closure;
273 return afb_hook_xreq_get(xreq, name, r);
276 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
278 struct afb_xreq *xreq = closure;
279 afb_hook_xreq_success(xreq, obj, info);
280 xreq_success_cb(closure, obj, info);
283 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
285 struct afb_xreq *xreq = closure;
286 afb_hook_xreq_fail(xreq, status, info);
287 xreq_fail_cb(closure, status, info);
290 static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
292 vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
295 static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
297 vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
300 static void *xreq_hooked_context_get_cb(void *closure)
302 void *r = xreq_context_get_cb(closure);
303 struct afb_xreq *xreq = closure;
304 return afb_hook_xreq_context_get(xreq, r);
307 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
309 struct afb_xreq *xreq = closure;
310 afb_hook_xreq_context_set(xreq, value, free_value);
311 xreq_context_set_cb(closure, value, free_value);
314 static void xreq_hooked_addref_cb(void *closure)
316 struct afb_xreq *xreq = closure;
317 afb_hook_xreq_addref(xreq);
318 xreq_addref_cb(closure);
321 static void xreq_hooked_unref_cb(void *closure)
323 struct afb_xreq *xreq = closure;
324 afb_hook_xreq_unref(xreq);
325 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
326 afb_hook_xreq_end(xreq);
327 xreq->queryitf->unref(xreq);
331 static void xreq_hooked_session_close_cb(void *closure)
333 struct afb_xreq *xreq = closure;
334 afb_hook_xreq_session_close(xreq);
335 xreq_session_close_cb(closure);
338 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
340 int r = xreq_session_set_LOA_cb(closure, level);
341 struct afb_xreq *xreq = closure;
342 return afb_hook_xreq_session_set_LOA(xreq, level, r);
345 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
347 int r = xreq_subscribe_cb(closure, event);
348 struct afb_xreq *xreq = closure;
349 return afb_hook_xreq_subscribe(xreq, event, r);
352 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
354 int r = xreq_unsubscribe_cb(closure, event);
355 struct afb_xreq *xreq = closure;
356 return afb_hook_xreq_unsubscribe(xreq, event, r);
361 struct afb_xreq *xreq;
362 void (*callback)(void*, int, struct json_object*);
366 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
368 struct reply *reply = closure;
370 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
371 reply->callback(reply->closure, iserror, result);
375 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)
377 struct reply *reply = malloc(sizeof *reply);
378 struct afb_xreq *xreq = closure;
379 afb_hook_xreq_subcall(xreq, api, verb, args);
382 reply->callback = callback;
383 reply->closure = cb_closure;
384 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
386 ERROR("out of memory");
387 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
391 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
394 struct afb_xreq *xreq = closure;
395 afb_hook_xreq_subcallsync(xreq, api, verb, args);
396 r = xreq_subcallsync_cb(closure, api, verb, args, result);
397 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
400 /******************************************************************************/
402 const struct afb_req_itf xreq_itf = {
403 .json = xreq_json_cb,
405 .success = xreq_success_cb,
406 .fail = xreq_fail_cb,
407 .vsuccess = xreq_vsuccess_cb,
408 .vfail = xreq_vfail_cb,
409 .context_get = xreq_context_get_cb,
410 .context_set = xreq_context_set_cb,
411 .addref = xreq_addref_cb,
412 .unref = xreq_unref_cb,
413 .session_close = xreq_session_close_cb,
414 .session_set_LOA = xreq_session_set_LOA_cb,
415 .subscribe = xreq_subscribe_cb,
416 .unsubscribe = xreq_unsubscribe_cb,
417 .subcall = xreq_subcall_cb,
418 .subcallsync = xreq_subcallsync_cb
421 const struct afb_req_itf xreq_hooked_itf = {
422 .json = xreq_hooked_json_cb,
423 .get = xreq_hooked_get_cb,
424 .success = xreq_hooked_success_cb,
425 .fail = xreq_hooked_fail_cb,
426 .vsuccess = xreq_hooked_vsuccess_cb,
427 .vfail = xreq_hooked_vfail_cb,
428 .context_get = xreq_hooked_context_get_cb,
429 .context_set = xreq_hooked_context_set_cb,
430 .addref = xreq_hooked_addref_cb,
431 .unref = xreq_hooked_unref_cb,
432 .session_close = xreq_hooked_session_close_cb,
433 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
434 .subscribe = xreq_hooked_subscribe_cb,
435 .unsubscribe = xreq_hooked_unsubscribe_cb,
436 .subcall = xreq_hooked_subcall_cb,
437 .subcallsync = xreq_hooked_subcallsync_cb
440 static inline struct afb_req to_req(struct afb_xreq *xreq)
442 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
445 /******************************************************************************/
447 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
449 return afb_req_json(to_req(xreq));
452 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
454 afb_req_success(to_req(xreq), obj, info);
457 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
461 va_start(args, info);
462 if (info == NULL || vasprintf(&message, info, args) < 0)
465 afb_xreq_success(xreq, obj, message);
469 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
471 afb_req_fail(to_req(xreq), status, info);
474 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
478 va_start(args, info);
479 if (info == NULL || vasprintf(&message, info, args) < 0)
482 afb_xreq_fail(xreq, status, message);
486 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
488 struct json_object *obj = xreq_json_cb(xreq);
489 const char *result = json_object_to_json_string(obj);
491 *size = strlen(result);
495 void afb_xreq_addref(struct afb_xreq *xreq)
497 afb_req_addref(to_req(xreq));
500 void afb_xreq_unref(struct afb_xreq *xreq)
502 afb_req_unref(to_req(xreq));
505 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)
507 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
510 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)
512 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
515 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
517 return xreq_subcallsync_cb(xreq, api, verb, args, result);
520 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
522 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
525 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
529 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
530 if (!afb_context_check(&xreq->context)) {
531 afb_context_close(&xreq->context);
532 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
538 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
539 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
540 if (!afb_context_check_loa(&xreq->context, loa)) {
541 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
547 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
548 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
549 if (afb_context_check_loa(&xreq->context, loa + 1)) {
550 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
556 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
557 afb_context_refresh(&xreq->context);
559 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
560 afb_context_change_loa(&xreq->context, 0);
561 afb_context_close(&xreq->context);
567 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
571 if (sessionflags != 0) {
572 if (!afb_context_check(&xreq->context)) {
573 afb_context_close(&xreq->context);
574 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
580 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
581 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
582 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
587 if (auth && !afb_auth_check(auth, xreq)) {
588 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
593 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
594 afb_context_refresh(&xreq->context);
596 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
597 afb_context_close(&xreq->context);
603 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
606 afb_xreq_fail_unknown_verb(xreq);
608 if (!xreq_session_check_apply_v1(xreq, verb->session))
609 verb->callback(to_req(xreq));
612 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
615 afb_xreq_fail_unknown_verb(xreq);
617 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
618 verb->callback(to_req(xreq));
621 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
623 memset(xreq, 0, sizeof *xreq);
625 xreq->queryitf = queryitf;
628 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
630 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
633 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
635 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
638 static void process_sync(struct afb_xreq *xreq)
643 afb_hook_init_xreq(xreq);
645 afb_hook_xreq_begin(xreq);
648 if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
649 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
651 xreq->context.api_key = api.closure;
652 api.itf->call(api.closure, xreq);
656 static void process_async(int signum, void *arg)
658 struct afb_xreq *xreq = arg;
661 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
665 afb_xreq_unref(xreq);
668 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
670 xreq->apiset = apiset;
672 afb_xreq_addref(xreq);
673 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
674 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
675 ERROR("can't process job with threads: %m");
676 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
677 afb_xreq_unref(xreq);
679 afb_xreq_unref(xreq);