enforce reset of xreq at init
[src/app-framework-binder.git] / src / afb-xreq.c
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <json-c/json.h>
26 #include <afb/afb-binding.h>
27
28 #include "afb-context.h"
29 #include "afb-xreq.h"
30 #include "afb-evt.h"
31 #include "afb-msg-json.h"
32 #include "afb-subcall.h"
33 #include "afb-hook.h"
34 #include "verbose.h"
35
36
37 static struct json_object *xreq_json_cb(void *closure)
38 {
39         struct afb_xreq *xreq = closure;
40         return xreq->json ? : (xreq->json = xreq->queryitf->json(xreq));
41 }
42
43 static struct afb_arg xreq_get_cb(void *closure, const char *name)
44 {
45         struct afb_xreq *xreq = closure;
46         if (xreq->queryitf->get)
47                 return xreq->queryitf->get(xreq, name);
48         else
49                 return afb_msg_json_get_arg(xreq_json_cb(closure), name);
50 }
51
52 static void xreq_success_cb(void *closure, struct json_object *obj, const char *info)
53 {
54         struct afb_xreq *xreq = closure;
55
56         if (xreq->replied) {
57                 ERROR("reply called more than one time!!");
58                 json_object_put(obj);
59         } else {
60                 xreq->replied = 1;
61                 if (xreq->queryitf->success)
62                         xreq->queryitf->success(xreq, obj, info);
63                 else
64                         xreq->queryitf->reply(xreq, 0, afb_msg_json_reply_ok(info, obj, &xreq->context, NULL));
65         }
66 }
67
68 static void xreq_fail_cb(void *closure, const char *status, const char *info)
69 {
70         struct afb_xreq *xreq = closure;
71
72         if (xreq->replied) {
73                 ERROR("reply called more than one time!!");
74         } else {
75                 xreq->replied = 1;
76                 if (xreq->queryitf->fail)
77                         xreq->queryitf->fail(xreq, status, info);
78                 else
79                         xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
80         }
81 }
82
83 static const char *xreq_raw_cb(void *closure, size_t *size)
84 {
85         struct afb_xreq *xreq = closure;
86         const char *result = json_object_to_json_string(xreq_json_cb(xreq));
87         if (size != NULL)
88                 *size = strlen(result);
89         return result;
90 }
91
92 static void xreq_send_cb(void *closure, const char *buffer, size_t size)
93 {
94         struct json_object *obj = json_tokener_parse(buffer);
95         if (!obj == !buffer)
96                 xreq_success_cb(closure, obj, "fake send");
97         else
98                 xreq_fail_cb(closure, "fake-send-failed", "fake send");
99 }
100
101 static void *xreq_context_get_cb(void *closure)
102 {
103         struct afb_xreq *xreq = closure;
104         return afb_context_get(&xreq->context);
105 }
106
107 static void xreq_context_set_cb(void *closure, void *value, void (*free_value)(void*))
108 {
109         struct afb_xreq *xreq = closure;
110         afb_context_set(&xreq->context, value, free_value);
111 }
112
113 static void xreq_addref_cb(void *closure)
114 {
115         struct afb_xreq *xreq = closure;
116         __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
117 }
118
119 static void xreq_unref_cb(void *closure)
120 {
121         struct afb_xreq *xreq = closure;
122         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
123                 xreq->queryitf->unref(xreq);
124         }
125 }
126
127 static void xreq_session_close_cb(void *closure)
128 {
129         struct afb_xreq *xreq = closure;
130         afb_context_close(&xreq->context);
131 }
132
133 static int xreq_session_set_LOA_cb(void *closure, unsigned level)
134 {
135         struct afb_xreq *xreq = closure;
136         return afb_context_change_loa(&xreq->context, level);
137 }
138
139 static int xreq_subscribe_cb(void *closure, struct afb_event event)
140 {
141         struct afb_xreq *xreq = closure;
142         return afb_xreq_subscribe(xreq, event);
143 }
144
145 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event)
146 {
147         if (xreq->listener)
148                 return afb_evt_add_watch(xreq->listener, event);
149         if (xreq->queryitf->subscribe)
150                 return xreq->queryitf->subscribe(xreq, event);
151         ERROR("no event listener, subscription impossible");
152         errno = EINVAL;
153         return -1;
154 }
155
156 static int xreq_unsubscribe_cb(void *closure, struct afb_event event)
157 {
158         struct afb_xreq *xreq = closure;
159         return afb_xreq_unsubscribe(xreq, event);
160 }
161
162 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
163 {
164         if (xreq->listener)
165                 return afb_evt_remove_watch(xreq->listener, event);
166         if (xreq->queryitf->unsubscribe)
167                 return xreq->queryitf->unsubscribe(xreq, event);
168         ERROR("no event listener, unsubscription impossible");
169         errno = EINVAL;
170         return -1;
171 }
172
173 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)
174 {
175         struct afb_xreq *xreq = closure;
176
177         if (xreq->queryitf->subcall)
178                 xreq->queryitf->subcall(xreq, api, verb, args, callback, cb_closure);
179         else
180                 afb_subcall(xreq, api, verb, args, callback, cb_closure);
181 }
182
183 static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
184 {
185         struct afb_xreq *xreq = closure;
186         return afb_subcall_sync(xreq, api, verb, args, result);
187 }
188
189 static struct json_object *xreq_hooked_json_cb(void *closure)
190 {
191         struct json_object *r = xreq_json_cb(closure);
192         struct afb_xreq *xreq = closure;
193         return afb_hook_xreq_json(xreq, r);
194 }
195
196 static struct afb_arg xreq_hooked_get_cb(void *closure, const char *name)
197 {
198         struct afb_arg r = xreq_get_cb(closure, name);
199         struct afb_xreq *xreq = closure;
200         return afb_hook_xreq_get(xreq, name, r);
201 }
202
203 static void xreq_hooked_success_cb(void *closure, struct json_object *obj, const char *info)
204 {
205         struct afb_xreq *xreq = closure;
206         afb_hook_xreq_success(xreq, obj, info);
207         xreq_success_cb(closure, obj, info);
208 }
209
210 static void xreq_hooked_fail_cb(void *closure, const char *status, const char *info)
211 {
212         struct afb_xreq *xreq = closure;
213         afb_hook_xreq_fail(xreq, status, info);
214         xreq_fail_cb(closure, status, info);
215 }
216
217 static const char *xreq_hooked_raw_cb(void *closure, size_t *size)
218 {
219         size_t s;
220         const char *r = xreq_raw_cb(closure, size ? : &s);
221         struct afb_xreq *xreq = closure;
222         return afb_hook_xreq_raw(xreq, r, *(size ? : &s));
223 }
224
225 static void xreq_hooked_send_cb(void *closure, const char *buffer, size_t size)
226 {
227         struct afb_xreq *xreq = closure;
228         afb_hook_xreq_send(xreq, buffer, size);
229         xreq_send_cb(closure, buffer, size);
230 }
231
232 static void *xreq_hooked_context_get_cb(void *closure)
233 {
234         void *r = xreq_context_get_cb(closure);
235         struct afb_xreq *xreq = closure;
236         return afb_hook_xreq_context_get(xreq, r);
237 }
238
239 static void xreq_hooked_context_set_cb(void *closure, void *value, void (*free_value)(void*))
240 {
241         struct afb_xreq *xreq = closure;
242         afb_hook_xreq_context_set(xreq, value, free_value);
243         xreq_context_set_cb(closure, value, free_value);
244 }
245
246 static void xreq_hooked_addref_cb(void *closure)
247 {
248         struct afb_xreq *xreq = closure;
249         afb_hook_xreq_addref(xreq);
250         xreq_addref_cb(closure);
251 }
252
253 static void xreq_hooked_unref_cb(void *closure)
254 {
255         struct afb_xreq *xreq = closure;
256         afb_hook_xreq_unref(xreq);
257         if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
258                 afb_hook_xreq_end(xreq);
259                 xreq->queryitf->unref(xreq);
260         }
261 }
262
263 static void xreq_hooked_session_close_cb(void *closure)
264 {
265         struct afb_xreq *xreq = closure;
266         afb_hook_xreq_session_close(xreq);
267         xreq_session_close_cb(closure);
268 }
269
270 static int xreq_hooked_session_set_LOA_cb(void *closure, unsigned level)
271 {
272         int r = xreq_session_set_LOA_cb(closure, level);
273         struct afb_xreq *xreq = closure;
274         return afb_hook_xreq_session_set_LOA(xreq, level, r);
275 }
276
277 static int xreq_hooked_subscribe_cb(void *closure, struct afb_event event)
278 {
279         int r = xreq_subscribe_cb(closure, event);
280         struct afb_xreq *xreq = closure;
281         return afb_hook_xreq_subscribe(xreq, event, r);
282 }
283
284 static int xreq_hooked_unsubscribe_cb(void *closure, struct afb_event event)
285 {
286         int r = xreq_unsubscribe_cb(closure, event);
287         struct afb_xreq *xreq = closure;
288         return afb_hook_xreq_unsubscribe(xreq, event, r);
289 }
290
291 struct reply
292 {
293         struct afb_xreq *xreq;
294         void (*callback)(void*, int, struct json_object*);
295         void *closure;
296 };
297
298 static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
299 {
300         struct reply *reply = closure;
301         
302         afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
303         reply->callback(reply->closure, iserror, result);
304         free(reply);
305 }
306
307 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)
308 {
309         struct reply *reply = malloc(sizeof *reply);
310         struct afb_xreq *xreq = closure;
311         afb_hook_xreq_subcall(xreq, api, verb, args);
312         if (reply) {
313                 reply->xreq = xreq;
314                 reply->callback = callback;
315                 reply->closure = cb_closure;
316                 xreq_subcall_cb(closure, api, verb, args, xreq_hooked_subcall_reply_cb, reply);
317         } else {
318                 ERROR("out of memory");
319                 xreq_subcall_cb(closure, api, verb, args, callback, cb_closure);
320         }
321 }
322
323 static int xreq_hooked_subcallsync_cb(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result)
324 {
325         int r;
326         struct afb_xreq *xreq = closure;
327         afb_hook_xreq_subcallsync(xreq, api, verb, args);
328         r = xreq_subcallsync_cb(closure, api, verb, args, result);
329         return afb_hook_xreq_subcallsync_result(xreq, r, *result);
330 }
331
332 const struct afb_req_itf xreq_itf = {
333         .json = xreq_json_cb,
334         .get = xreq_get_cb,
335         .success = xreq_success_cb,
336         .fail = xreq_fail_cb,
337         .raw = xreq_raw_cb,
338         .send = xreq_send_cb,
339         .context_get = xreq_context_get_cb,
340         .context_set = xreq_context_set_cb,
341         .addref = xreq_addref_cb,
342         .unref = xreq_unref_cb,
343         .session_close = xreq_session_close_cb,
344         .session_set_LOA = xreq_session_set_LOA_cb,
345         .subscribe = xreq_subscribe_cb,
346         .unsubscribe = xreq_unsubscribe_cb,
347         .subcall = xreq_subcall_cb,
348         .subcallsync = xreq_subcallsync_cb
349 };
350
351 const struct afb_req_itf xreq_hooked_itf = {
352         .json = xreq_hooked_json_cb,
353         .get = xreq_hooked_get_cb,
354         .success = xreq_hooked_success_cb,
355         .fail = xreq_hooked_fail_cb,
356         .raw = xreq_hooked_raw_cb,
357         .send = xreq_hooked_send_cb,
358         .context_get = xreq_hooked_context_get_cb,
359         .context_set = xreq_hooked_context_set_cb,
360         .addref = xreq_hooked_addref_cb,
361         .unref = xreq_hooked_unref_cb,
362         .session_close = xreq_hooked_session_close_cb,
363         .session_set_LOA = xreq_hooked_session_set_LOA_cb,
364         .subscribe = xreq_hooked_subscribe_cb,
365         .unsubscribe = xreq_hooked_unsubscribe_cb,
366         .subcall = xreq_hooked_subcall_cb,
367         .subcallsync = xreq_hooked_subcallsync_cb
368 };
369
370 static inline struct afb_req to_req(struct afb_xreq *xreq)
371 {
372         return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
373 }
374
375 void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info)
376 {
377         afb_req_success(to_req(xreq), obj, info);
378 }
379
380 void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...)
381 {
382         char *message;
383         va_list args;
384         va_start(args, info);
385         if (info == NULL || vasprintf(&message, info, args) < 0)
386                 message = NULL;
387         va_end(args);
388         afb_xreq_success(xreq, obj, message);
389         free(message);
390 }
391
392 void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info)
393 {
394         afb_req_fail(to_req(xreq), status, info);
395 }
396
397 void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...)
398 {
399         char *message;
400         va_list args;
401         va_start(args, info);
402         if (info == NULL || vasprintf(&message, info, args) < 0)
403                 message = NULL;
404         va_end(args);
405         afb_xreq_fail(xreq, status, message);
406         free(message);
407 }
408
409 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
410 {
411         return afb_req_raw(to_req(xreq), size);
412 }
413
414 void afb_xreq_addref(struct afb_xreq *xreq)
415 {
416         afb_req_addref(to_req(xreq));
417 }
418
419 void afb_xreq_unref(struct afb_xreq *xreq)
420 {
421         afb_req_unref(to_req(xreq));
422 }
423
424 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)
425 {
426         xreq_subcall_cb(xreq, api, verb, args, callback, cb_closure);
427 }
428
429 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)
430 {
431         afb_req_subcall(to_req(xreq), api, verb, args, callback, cb_closure);
432 }
433
434 static int xcheck(struct afb_xreq *xreq, int sessionflags)
435 {
436         if ((sessionflags & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
437                 if (!afb_context_check(&xreq->context)) {
438                         afb_context_close(&xreq->context);
439                         afb_xreq_fail_f(xreq, "failed", "invalid token's identity");
440                         return 0;
441                 }
442         }
443
444         if ((sessionflags & AFB_SESSION_CREATE) != 0) {
445                 if (afb_context_check_loa(&xreq->context, 1)) {
446                         afb_xreq_fail_f(xreq, "failed", "invalid creation state");
447                         return 0;
448                 }
449                 afb_context_change_loa(&xreq->context, 1);
450                 afb_context_refresh(&xreq->context);
451         }
452
453         if ((sessionflags & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
454                 afb_context_refresh(&xreq->context);
455
456         if ((sessionflags & AFB_SESSION_CLOSE) != 0) {
457                 afb_context_change_loa(&xreq->context, 0);
458                 afb_context_close(&xreq->context);
459         }
460
461         if ((sessionflags & AFB_SESSION_LOA_GE) != 0) {
462                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
463                 if (!afb_context_check_loa(&xreq->context, loa)) {
464                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
465                         return 0;
466                 }
467         }
468
469         if ((sessionflags & AFB_SESSION_LOA_LE) != 0) {
470                 int loa = (sessionflags >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
471                 if (afb_context_check_loa(&xreq->context, loa + 1)) {
472                         afb_xreq_fail_f(xreq, "failed", "invalid LOA");
473                         return 0;
474                 }
475         }
476         return 1;
477 }
478
479 void afb_xreq_call(struct afb_xreq *xreq, int sessionflags, void (*method)(struct afb_req req))
480 {
481         if (xcheck(xreq, sessionflags))
482                 method(to_req(xreq));
483 }
484
485 void afb_xreq_begin(struct afb_xreq *xreq)
486 {
487         afb_hook_init_xreq(xreq);
488         if (xreq->hookflags)
489                 afb_hook_xreq_begin(xreq);
490 }
491
492 void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf)
493 {
494         memset(xreq, 0, sizeof *xreq);
495         xreq->refcount = 1;
496         xreq->queryitf = queryitf;
497 }
498