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
25 #include <json-c/json.h>
26 #include <afb/afb-binding.h>
28 #include "afb-context.h"
31 #include "afb-msg-json.h"
32 #include "afb-subcall.h"
35 #include "afb-apiset.h"
40 /******************************************************************************/
42 static struct json_object *xreq_json_cb(void *closure)
44 struct afb_xreq *xreq = closure;
45 return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
48 static struct afb_arg xreq_get_cb(void *closure, const char *name)
50 struct afb_xreq *xreq = closure;
51 if (xreq->queryitf->get)
52 return xreq->queryitf->get(xreq, name);
54 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
57 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
59 struct afb_xreq *xreq = closure;
62 ERROR("reply called more than one time!!");
66 if (xreq->queryitf->success)
67 xreq->queryitf->success(xreq, obj, info);
69 xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
73 static void xreq_fail_cb(void *closure, const char *status, const char *info)
75 struct afb_xreq *xreq = closure;
78 ERROR("reply called more than one time!!");
81 if (xreq->queryitf->fail)
82 xreq->queryitf->fail(xreq, status, info);
84 xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
88 static const char *xreq_raw_cb(void *closure, size_t *size)
90 struct afb_xreq *xreq = closure;
91 const char *result = json_object_to_json_string(xreq_json_cb(xreq));
93 *size = strlen(result);
97 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
99 struct json_object *obj = json_tokener_parse(buffer);
101 xreq_success_cb(closure, obj, "fake send");
103 xreq_fail_cb(closure, "fake-send-failed", "fake send");
106 static void *xreq_context_get_cb(void *closure)
108 struct afb_xreq *xreq = closure;
109 return afb_context_get(&xreq->context);
112 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
114 struct afb_xreq *xreq = closure;
115 afb_context_set(&xreq->context, value, free_value);
118 static void xreq_addref_cb(void *closure)
120 struct afb_xreq *xreq = closure;
121 __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
124 static void xreq_unref_cb(void *closure)
126 struct afb_xreq *xreq = closure;
127 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
128 xreq->queryitf->unref(xreq);
132 static void xreq_session_close_cb(void *closure)
134 struct afb_xreq *xreq = closure;
135 afb_context_close(&xreq->context);
138 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
140 struct afb_xreq *xreq = closure;
141 return afb_context_change_loa(&xreq->context, level);
144 static int xreq_subscribe_cb(void *closure, struct afb_event event)
146 struct afb_xreq *xreq = closure;
147 return afb_xreq_subscribe(xreq, event);
150 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
153 return afb_evt_add_watch(xreq->listener, event);
154 if (xreq->queryitf->subscribe)
155 return xreq->queryitf->subscribe(xreq, event);
156 ERROR("no event listener, subscription impossible");
161 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
163 struct afb_xreq *xreq = closure;
164 return afb_xreq_unsubscribe(xreq, event);
167 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
170 return afb_evt_remove_watch(xreq->listener, event);
171 if (xreq->queryitf->unsubscribe)
172 return xreq->queryitf->unsubscribe(xreq, event);
173 ERROR("no event listener, unsubscription impossible");
178 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)
180 struct afb_xreq *xreq = closure;
182 if (xreq->queryitf->subcall)
183 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
185 afb_subcall(xreq, api, verb, args, callback, cb_closure);
190 struct afb_xreq *caller;
193 struct json_object *args;
194 struct jobloop *jobloop;
195 struct json_object *result;
199 static void xreq_sync_leave(struct xreq_sync *sync)
201 struct jobloop *jobloop = sync->jobloop;
203 sync->jobloop = NULL;
208 static void xreq_sync_reply(void *closure, int iserror, struct json_object *obj)
210 struct xreq_sync *sync = closure;
212 sync->iserror = iserror;
213 sync->result = json_object_get(obj);
214 xreq_sync_leave(sync);
217 static void xreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
219 struct xreq_sync *sync = closure;
222 sync->jobloop = jobloop;
223 xreq_subcall_cb(sync->caller, sync->api, sync->verb, sync->args, xreq_sync_reply, sync);
226 xreq_sync_leave(sync);
230 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
233 struct xreq_sync sync;
234 struct afb_xreq *xreq = closure;
244 rc = jobs_enter(NULL, 0, xreq_sync_enter, &sync);
245 if (rc < 0 || sync.iserror) {
246 *result = sync.result ? : afb_msg_json_internal_error();
249 *result = sync.result;
253 /******************************************************************************/
255 static struct json_object *xreq_hooked_json_cb(void *closure)
257 struct json_object *r = xreq_json_cb(closure);
258 struct afb_xreq *xreq = closure;
259 return afb_hook_xreq_json(xreq, r);
262 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
264 struct afb_arg r = xreq_get_cb(closure, name);
265 struct afb_xreq *xreq = closure;
266 return afb_hook_xreq_get(xreq, name, r);
269 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
271 struct afb_xreq *xreq = closure;
272 afb_hook_xreq_success(xreq, obj, info);
273 xreq_success_cb(closure, obj, info);
276 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
278 struct afb_xreq *xreq = closure;
279 afb_hook_xreq_fail(xreq, status, info);
280 xreq_fail_cb(closure, status, info);
283 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
286 const char *r = xreq_raw_cb(closure, size ? : &s);
287 struct afb_xreq *xreq = closure;
288 return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
291 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
293 struct afb_xreq *xreq = closure;
294 afb_hook_xreq_send(xreq, buffer, size);
295 xreq_send_cb(closure, buffer, size);
298 static void *xreq_hooked_context_get_cb(void *closure)
300 void *r = xreq_context_get_cb(closure);
301 struct afb_xreq *xreq = closure;
302 return afb_hook_xreq_context_get(xreq, r);
305 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
307 struct afb_xreq *xreq = closure;
308 afb_hook_xreq_context_set(xreq, value, free_value);
309 xreq_context_set_cb(closure, value, free_value);
312 static void xreq_hooked_addref_cb(void *closure)
314 struct afb_xreq *xreq = closure;
315 afb_hook_xreq_addref(xreq);
316 xreq_addref_cb(closure);
319 static void xreq_hooked_unref_cb(void *closure)
321 struct afb_xreq *xreq = closure;
322 afb_hook_xreq_unref(xreq);
323 if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
324 afb_hook_xreq_end(xreq);
325 xreq->queryitf->unref(xreq);
329 static void xreq_hooked_session_close_cb(void *closure)
331 struct afb_xreq *xreq = closure;
332 afb_hook_xreq_session_close(xreq);
333 xreq_session_close_cb(closure);
336 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
338 int r = xreq_session_set_LOA_cb(closure, level);
339 struct afb_xreq *xreq = closure;
340 return afb_hook_xreq_session_set_LOA(xreq, level, r);
343 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
345 int r = xreq_subscribe_cb(closure, event);
346 struct afb_xreq *xreq = closure;
347 return afb_hook_xreq_subscribe(xreq, event, r);
350 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
352 int r = xreq_unsubscribe_cb(closure, event);
353 struct afb_xreq *xreq = closure;
354 return afb_hook_xreq_unsubscribe(xreq, event, r);
359 struct afb_xreq *xreq;
360 void (*callback)(void*, int, struct json_object*);
364 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
366 struct reply *reply = closure;
368 afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
369 reply->callback(reply->closure, iserror, result);
373 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)
375 struct reply *reply = malloc(sizeof *reply);
376 struct afb_xreq *xreq = closure;
377 afb_hook_xreq_subcall(xreq, api, verb, args);
380 reply->callback = callback;
381 reply->closure = cb_closure;
382 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
384 ERROR("out of memory");
385 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
389 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
392 struct afb_xreq *xreq = closure;
393 afb_hook_xreq_subcallsync(xreq, api, verb, args);
394 r = xreq_subcallsync_cb(closure, api, verb, args, result);
395 return afb_hook_xreq_subcallsync_result(xreq, r, *result);
398 /******************************************************************************/
400 const struct afb_req_itf xreq_itf = {
401 .json = xreq_json_cb,
403 .success = xreq_success_cb,
404 .fail = xreq_fail_cb,
406 .send = xreq_send_cb,
407 .context_get = xreq_context_get_cb,
408 .context_set = xreq_context_set_cb,
409 .addref = xreq_addref_cb,
410 .unref = xreq_unref_cb,
411 .session_close = xreq_session_close_cb,
412 .session_set_LOA = xreq_session_set_LOA_cb,
413 .subscribe = xreq_subscribe_cb,
414 .unsubscribe = xreq_unsubscribe_cb,
415 .subcall = xreq_subcall_cb,
416 .subcallsync = xreq_subcallsync_cb
419 const struct afb_req_itf xreq_hooked_itf = {
420 .json = xreq_hooked_json_cb,
421 .get = xreq_hooked_get_cb,
422 .success = xreq_hooked_success_cb,
423 .fail = xreq_hooked_fail_cb,
424 .raw = xreq_hooked_raw_cb,
425 .send = xreq_hooked_send_cb,
426 .context_get = xreq_hooked_context_get_cb,
427 .context_set = xreq_hooked_context_set_cb,
428 .addref = xreq_hooked_addref_cb,
429 .unref = xreq_hooked_unref_cb,
430 .session_close = xreq_hooked_session_close_cb,
431 .session_set_LOA = xreq_hooked_session_set_LOA_cb,
432 .subscribe = xreq_hooked_subscribe_cb,
433 .unsubscribe = xreq_hooked_unsubscribe_cb,
434 .subcall = xreq_hooked_subcall_cb,
435 .subcallsync = xreq_hooked_subcallsync_cb
438 static inline struct afb_req to_req(struct afb_xreq *xreq)
440 return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
443 /******************************************************************************/
445 struct json_object *afb_xreq_json(struct afb_xreq *xreq)
447 return afb_req_json(to_req(xreq));
450 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
452 afb_req_success(to_req(xreq), obj, info);
455 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
459 va_start(args, info);
460 if (info == NULL || vasprintf(&message, info, args) < 0)
463 afb_xreq_success(xreq, obj, message);
467 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
469 afb_req_fail(to_req(xreq), status, info);
472 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
476 va_start(args, info);
477 if (info == NULL || vasprintf(&message, info, args) < 0)
480 afb_xreq_fail(xreq, status, message);
484 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
486 return afb_req_raw(to_req(xreq), size);
489 void afb_xreq_addref(struct afb_xreq *xreq)
491 afb_req_addref(to_req(xreq));
494 void afb_xreq_unref(struct afb_xreq *xreq)
496 afb_req_unref(to_req(xreq));
499 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)
501 xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
504 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)
506 afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
509 int afb_xreq_unhooked_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
511 return xreq_subcallsync_cb(xreq, api, verb, args, result);
514 int afb_xreq_subcall_sync(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, struct json_object **result)
516 return afb_req_subcall_sync(to_req(xreq), api, verb, args, result);
519 static int xreq_session_check_apply_v1(struct afb_xreq *xreq, int sessionflags)
523 if ((sessionflags & (AFB_SESSION_CLOSE_V1|AFB_SESSION_RENEW_V1|AFB_SESSION_CHECK_V1|AFB_SESSION_LOA_EQ_V1)) != 0) {
524 if (!afb_context_check(&xreq->context)) {
525 afb_context_close(&xreq->context);
526 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
532 if ((sessionflags & AFB_SESSION_LOA_GE_V1) != 0) {
533 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
534 if (!afb_context_check_loa(&xreq->context, loa)) {
535 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
541 if ((sessionflags & AFB_SESSION_LOA_LE_V1) != 0) {
542 loa = (sessionflags >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1;
543 if (afb_context_check_loa(&xreq->context, loa + 1)) {
544 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
550 if ((sessionflags & AFB_SESSION_RENEW_V1) != 0) {
551 afb_context_refresh(&xreq->context);
553 if ((sessionflags & AFB_SESSION_CLOSE_V1) != 0) {
554 afb_context_change_loa(&xreq->context, 0);
555 afb_context_close(&xreq->context);
561 static int xreq_session_check_apply_v2(struct afb_xreq *xreq, uint32_t sessionflags, const struct afb_auth *auth)
565 if (sessionflags != 0) {
566 if (!afb_context_check(&xreq->context)) {
567 afb_context_close(&xreq->context);
568 afb_xreq_fail_f(xreq, "denied", "invalid token's identity");
574 loa = (int)(sessionflags & AFB_SESSION_LOA_MASK_V2);
575 if (loa && !afb_context_check_loa(&xreq->context, loa)) {
576 afb_xreq_fail_f(xreq, "denied", "invalid LOA");
581 if (auth && !afb_auth_check(auth, xreq)) {
582 afb_xreq_fail_f(xreq, "denied", "authorisation refused");
587 if ((sessionflags & AFB_SESSION_REFRESH_V2) != 0) {
588 afb_context_refresh(&xreq->context);
590 if ((sessionflags & AFB_SESSION_CLOSE_V2) != 0) {
591 afb_context_close(&xreq->context);
597 void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb)
600 afb_xreq_fail_unknown_verb(xreq);
602 if (!xreq_session_check_apply_v1(xreq, verb->session))
603 verb->callback(to_req(xreq));
606 void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb)
609 afb_xreq_fail_unknown_verb(xreq);
611 if (!xreq_session_check_apply_v2(xreq, verb->session, verb->auth))
612 verb->callback(to_req(xreq));
615 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
617 memset(xreq, 0, sizeof *xreq);
619 xreq->queryitf = queryitf;
622 void afb_xreq_fail_unknown_api(struct afb_xreq *xreq)
624 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb);
627 void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
629 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api);
632 static void process_sync(struct afb_xreq *xreq)
637 afb_hook_init_xreq(xreq);
639 afb_hook_xreq_begin(xreq);
642 if (afb_apiset_get(xreq->apiset, xreq->api, &api) < 0) {
643 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
645 xreq->context.api_key = api.closure;
646 api.itf->call(api.closure, xreq);
650 static void process_async(int signum, void *arg)
652 struct afb_xreq *xreq = arg;
655 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
659 afb_xreq_unref(xreq);
662 void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset)
664 xreq->apiset = apiset;
666 afb_xreq_addref(xreq);
667 if (jobs_queue(NULL, afb_apiset_timeout_get(apiset), process_async, xreq) < 0) {
668 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
669 ERROR("can't process job with threads: %m");
670 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
671 afb_xreq_unref(xreq);
673 afb_xreq_unref(xreq);