10b3fc7757794cf22f008f9b58df72b4e45b4a53
[src/app-framework-binder.git] / src / afb-stub-ws.c
1 /*
2  * Copyright (C) 2015-2018 "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
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <endian.h>
27 #include <netdb.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <pthread.h>
32
33 #include <json-c/json.h>
34
35 #include <afb/afb-event-x2.h>
36
37 #include "afb-session.h"
38 #include "afb-cred.h"
39 #include "afb-api.h"
40 #include "afb-apiset.h"
41 #include "afb-proto-ws.h"
42 #include "afb-stub-ws.h"
43 #include "afb-context.h"
44 #include "afb-evt.h"
45 #include "afb-xreq.h"
46 #include "verbose.h"
47 #include "fdev.h"
48 #include "jobs.h"
49
50 struct afb_stub_ws;
51
52
53 /*
54  * structure for a ws request
55  */
56 struct server_req {
57         struct afb_xreq xreq;           /* the xreq */
58         struct afb_stub_ws *stubws;     /* the client of the request */
59         struct afb_proto_ws_call *call; /* the incoming call */
60 };
61
62 /*
63  * structure for recording events on client side
64  */
65 struct client_event
66 {
67         struct client_event *next;
68         struct afb_event_x2 *event;
69         int id;
70         int refcount;
71 };
72
73 /*
74  * structure for recording describe requests
75  */
76 struct client_describe
77 {
78         struct afb_stub_ws *stubws;
79         struct jobloop *jobloop;
80         struct json_object *result;
81 };
82
83 /*
84  * structure for jobs of describing
85  */
86 struct server_describe
87 {
88         struct afb_stub_ws *stubws;
89         struct afb_proto_ws_describe *describe;
90 };
91
92 /*
93  * structure for recording sessions
94  */
95 struct server_session
96 {
97         struct server_session *next;
98         struct afb_session *session;
99 };
100
101 /******************* stub description for client or servers ******************/
102
103 struct afb_stub_ws
104 {
105         /* count of references */
106         int refcount;
107
108         /* resource control */
109         pthread_mutex_t mutex;
110
111         /* protocol */
112         struct afb_proto_ws *proto;
113
114         /* listener for events (server side) */
115         struct afb_evt_listener *listener;
116
117         /* event replica (client side) */
118         struct client_event *events;
119
120         /* credentials of the client (server side) */
121         struct afb_cred *cred;
122
123         /* sessions (server side) */
124         struct server_session *sessions;
125
126         /* apiset */
127         struct afb_apiset *apiset;
128
129         /* on hangup callback */
130         void (*on_hangup)(struct afb_stub_ws *);
131
132         /* the api name */
133         char apiname[1];
134 };
135
136 /******************* ws request part for server *****************/
137
138 /* decrement the reference count of the request and free/release it on falling to null */
139 static void server_req_destroy_cb(struct afb_xreq *xreq)
140 {
141         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
142
143         afb_context_disconnect(&wreq->xreq.context);
144         afb_cred_unref(wreq->xreq.cred);
145         json_object_put(wreq->xreq.json);
146         afb_proto_ws_call_unref(wreq->call);
147         afb_stub_ws_unref(wreq->stubws);
148         free(wreq);
149 }
150
151 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
152 {
153         int rc;
154         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
155
156         rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
157         if (rc < 0)
158                 ERROR("error while sending reply");
159         json_object_put(obj);
160 }
161
162 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
163 {
164         int rc;
165         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
166
167         rc = afb_evt_event_x2_add_watch(wreq->stubws->listener, event);
168         if (rc >= 0)
169                 rc = afb_proto_ws_call_subscribe(wreq->call,  afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
170         if (rc < 0)
171                 ERROR("error while subscribing event");
172         return rc;
173 }
174
175 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
176 {
177         int rc, rc2;
178         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
179
180         rc = afb_proto_ws_call_unsubscribe(wreq->call,  afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
181         rc2 = afb_evt_event_x2_remove_watch(wreq->stubws->listener, event);
182         if (rc >= 0 && rc2 < 0)
183                 rc = rc2;
184         if (rc < 0)
185                 ERROR("error while unsubscribing event");
186         return rc;
187 }
188
189 static const struct afb_xreq_query_itf server_req_xreq_itf = {
190         .reply = server_req_reply_cb,
191         .unref = server_req_destroy_cb,
192         .subscribe = server_req_subscribe_cb,
193         .unsubscribe = server_req_unsubscribe_cb
194 };
195
196 /******************* client part **********************************/
197
198 /* search the event */
199 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
200 {
201         struct client_event *ev;
202
203         ev = stubws->events;
204         while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
205                 ev = ev->next;
206
207         return ev;
208 }
209
210 /* on call, propagate it to the ws service */
211 static void client_call_cb(void * closure, struct afb_xreq *xreq)
212 {
213         struct afb_stub_ws *stubws = closure;
214
215         afb_proto_ws_client_call(
216                         stubws->proto,
217                         xreq->request.called_verb,
218                         afb_xreq_json(xreq),
219                         afb_session_uuid(xreq->context.session),
220                         xreq,
221                         xreq_on_behalf_cred_export(xreq));
222         afb_xreq_unhooked_addref(xreq);
223 }
224
225 static void client_on_description_cb(void *closure, struct json_object *data)
226 {
227         struct client_describe *desc = closure;
228
229         desc->result = data;
230         jobs_leave(desc->jobloop);
231 }
232
233 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
234 {
235         struct client_describe *desc = closure;
236
237         if (signum)
238                 jobs_leave(jobloop);
239         else {
240                 desc->jobloop = jobloop;
241                 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
242         }
243 }
244
245 /* get the description */
246 static struct json_object *client_describe_cb(void * closure)
247 {
248         struct client_describe desc;
249
250         /* synchronous job: send the request and wait its result */
251         desc.stubws = closure;
252         desc.result = NULL;
253         jobs_enter(NULL, 0, client_send_describe_cb, &desc);
254         return desc.result;
255 }
256
257 /******************* server part: manage events **********************************/
258
259 static void server_event_add(void *closure, const char *event, int eventid)
260 {
261         struct afb_stub_ws *stubws = closure;
262
263         afb_proto_ws_server_event_create(stubws->proto, event, eventid);
264 }
265
266 static void server_event_remove(void *closure, const char *event, int eventid)
267 {
268         struct afb_stub_ws *stubws = closure;
269
270         afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
271 }
272
273 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
274 {
275         struct afb_stub_ws *stubws = closure;
276
277         afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
278         json_object_put(object);
279 }
280
281 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
282 {
283         struct afb_stub_ws *stubws = closure;
284
285         afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
286         json_object_put(object);
287 }
288
289 /*****************************************************/
290
291 static void on_reply(void *closure, void *request, struct json_object *object, const char *error, const char *info)
292 {
293         struct afb_xreq *xreq = request;
294
295         afb_xreq_reply(xreq, object, error, info);
296         afb_xreq_unhooked_unref(xreq);
297 }
298
299 static void on_event_create(void *closure, const char *event_name, int event_id)
300 {
301         struct afb_stub_ws *stubws = closure;
302         struct client_event *ev;
303
304         /* check conflicts */
305         ev = client_event_search(stubws, event_id, event_name);
306         if (ev != NULL) {
307                 ev->refcount++;
308                 return;
309         }
310
311         /* no conflict, try to add it */
312         ev = malloc(sizeof *ev);
313         if (ev != NULL) {
314                 ev->event = afb_evt_event_x2_create(event_name);
315                 if (ev->event != NULL) {
316                         ev->refcount = 1;
317                         ev->id = event_id;
318                         ev->next = stubws->events;
319                         stubws->events = ev;
320                         return;
321                 }
322                 free(ev);
323         }
324         ERROR("can't create event %s, out of memory", event_name);
325 }
326
327 static void on_event_remove(void *closure, const char *event_name, int event_id)
328 {
329         struct afb_stub_ws *stubws = closure;
330         struct client_event *ev, **prv;
331
332         /* check conflicts */
333         ev = client_event_search(stubws, event_id, event_name);
334         if (ev == NULL)
335                 return;
336
337         /* decrease the reference count */
338         if (--ev->refcount)
339                 return;
340
341         /* unlinks the event */
342         prv = &stubws->events;
343         while (*prv != ev)
344                 prv = &(*prv)->next;
345         *prv = ev->next;
346
347         /* destroys the event */
348         afb_evt_event_x2_unref(ev->event);
349         free(ev);
350 }
351
352 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
353 {
354         struct afb_stub_ws *stubws = closure;
355         struct afb_xreq *xreq = request;
356         struct client_event *ev;
357
358         /* check conflicts */
359         ev = client_event_search(stubws, event_id, event_name);
360         if (ev == NULL)
361                 return;
362
363         if (afb_xreq_subscribe(xreq, ev->event) < 0)
364                 ERROR("can't subscribe: %m");
365 }
366
367 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
368 {
369         struct afb_stub_ws *stubws = closure;
370         struct afb_xreq *xreq = request;
371         struct client_event *ev;
372
373         /* check conflicts */
374         ev = client_event_search(stubws, event_id, event_name);
375         if (ev == NULL)
376                 return;
377
378         if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
379                 ERROR("can't unsubscribe: %m");
380 }
381
382 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
383 {
384         struct afb_stub_ws *stubws = closure;
385         struct client_event *ev;
386
387         /* check conflicts */
388         ev = client_event_search(stubws, event_id, event_name);
389         if (ev)
390                 afb_evt_event_x2_push(ev->event, data);
391         else
392                 ERROR("unreadable push event");
393 }
394
395 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
396 {
397         afb_evt_broadcast(event_name, data);
398 }
399
400 /*****************************************************/
401
402 static void record_session(struct afb_stub_ws *stubws, struct afb_session *session)
403 {
404         struct server_session *s, **prv;
405
406         /* search */
407         prv = &stubws->sessions;
408         while ((s = *prv)) {
409                 if (s->session == session)
410                         return;
411                 if (afb_session_is_closed(s->session)) {
412                         *prv = s->next;
413                         afb_session_unref(s->session);
414                         free(s);
415                 }
416                 else
417                         prv = &s->next;
418         }
419
420         /* create */
421         s = malloc(sizeof *s);
422         if (s) {
423                 s->session = afb_session_addref(session);
424                 s->next = stubws->sessions;
425                 stubws->sessions = s;
426         }
427 }
428
429 static void release_all_sessions(struct afb_stub_ws *stubws)
430 {
431         struct server_session *s, *n;
432
433         s = stubws->sessions;
434         stubws->sessions = NULL;
435         while(s) {
436                 n = s->next;
437                 afb_session_unref(s->session);
438                 free(s);
439                 s = n;
440         }
441 }
442
443 /*****************************************************/
444
445 static void on_call(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid, const char *user_creds)
446 {
447         struct afb_stub_ws *stubws = closure;
448         struct server_req *wreq;
449
450         afb_stub_ws_addref(stubws);
451
452         /* create the request */
453         wreq = malloc(sizeof *wreq);
454         if (wreq == NULL)
455                 goto out_of_memory;
456
457         afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
458         wreq->stubws = stubws;
459         wreq->call = call;
460
461         /* init the context */
462         if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
463                 goto unconnected;
464         wreq->xreq.context.validated = 1;
465         record_session(stubws, wreq->xreq.context.session);
466         if (wreq->xreq.context.created)
467                 afb_session_set_autoclose(wreq->xreq.context.session, 1);
468
469         /* makes the call */
470         wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
471         wreq->xreq.request.called_api = stubws->apiname;
472         wreq->xreq.request.called_verb = verb;
473         wreq->xreq.json = args;
474         afb_xreq_process(&wreq->xreq, stubws->apiset);
475         return;
476
477 unconnected:
478         free(wreq);
479 out_of_memory:
480         json_object_put(args);
481         afb_stub_ws_unref(stubws);
482         afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
483         afb_proto_ws_call_unref(call);
484 }
485
486 static void server_describe_sjob(int signum, void *closure)
487 {
488         struct json_object *obj;
489         struct server_describe *desc = closure;
490
491         /* get the description if possible */
492         obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
493
494         /* send it */
495         afb_proto_ws_describe_put(desc->describe, obj);
496         json_object_put(obj);
497         afb_stub_ws_unref(desc->stubws);
498 }
499
500 static void server_describe_job(int signum, void *closure)
501 {
502         server_describe_sjob(signum, closure);
503         free(closure);
504 }
505
506 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
507 {
508         struct server_describe *desc, sdesc;
509         struct afb_stub_ws *stubws = closure;
510
511         /* allocate (if possible) and init */
512         desc = malloc(sizeof *desc);
513         if (desc == NULL)
514                 desc = &sdesc;
515         desc->stubws = stubws;
516         desc->describe = describe;
517         afb_stub_ws_addref(stubws);
518
519         /* process */
520         if (desc == &sdesc)
521                 jobs_call(NULL, 0, server_describe_sjob, desc);
522         else {
523                 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
524                         jobs_call(NULL, 0, server_describe_job, desc);
525         }
526 }
527
528 /*****************************************************/
529
530 static const struct afb_proto_ws_client_itf client_itf =
531 {
532         .on_reply = on_reply,
533         .on_event_create = on_event_create,
534         .on_event_remove = on_event_remove,
535         .on_event_subscribe = on_event_subscribe,
536         .on_event_unsubscribe = on_event_unsubscribe,
537         .on_event_push = on_event_push,
538         .on_event_broadcast = on_event_broadcast,
539 };
540
541 static const struct afb_proto_ws_server_itf server_itf =
542 {
543         .on_call = on_call,
544         .on_describe = on_describe
545 };
546
547 static struct afb_api_itf ws_api_itf = {
548         .call = client_call_cb,
549         .describe = client_describe_cb
550 };
551
552 /* the interface for events pushing */
553 static const struct afb_evt_itf server_evt_itf = {
554         .broadcast = server_event_broadcast,
555         .push = server_event_push,
556         .add = server_event_add,
557         .remove = server_event_remove
558 };
559
560 /*****************************************************/
561
562 static void drop_all_events(struct afb_stub_ws *stubws)
563 {
564         struct client_event *ev, *nxt;
565
566         ev = stubws->events;
567         stubws->events = NULL;
568
569         while (ev) {
570                 nxt = ev->next;
571                 afb_evt_event_x2_unref(ev->event);
572                 free(ev);
573                 ev = nxt;
574         }
575 }
576
577 /* callback when receiving a hangup */
578 static void on_hangup(void *closure)
579 {
580         struct afb_stub_ws *stubws = closure;
581
582         afb_stub_ws_addref(stubws);
583         if (stubws->on_hangup)
584                 stubws->on_hangup(stubws);
585
586         release_all_sessions(stubws);
587         afb_stub_ws_unref(stubws);
588 }
589
590 static int enqueue_processing(void (*callback)(int signum, void* arg), void *arg)
591 {
592         return jobs_queue(NULL, 0, callback, arg);
593 }
594
595 /*****************************************************/
596
597 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, int client)
598 {
599         struct afb_stub_ws *stubws;
600
601         stubws = calloc(1, sizeof *stubws + strlen(apiname));
602         if (stubws == NULL)
603                 errno = ENOMEM;
604         else {
605                 if (client)
606                         stubws->proto = afb_proto_ws_create_client(fdev, &client_itf, stubws);
607                 else
608                         stubws->proto = afb_proto_ws_create_server(fdev, &server_itf, stubws);
609
610                 if (stubws->proto) {
611                         strcpy(stubws->apiname, apiname);
612                         stubws->apiset = afb_apiset_addref(apiset);
613                         stubws->refcount = 1;
614                         afb_proto_ws_on_hangup(stubws->proto, on_hangup);
615                         afb_proto_ws_set_queuing(stubws->proto, enqueue_processing);
616                         return stubws;
617                 }
618                 free(stubws);
619         }
620         fdev_unref(fdev);
621         return NULL;
622 }
623
624 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
625 {
626         return afb_stub_ws_create(fdev, apiname, apiset, 1);
627 }
628
629 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
630 {
631         struct afb_stub_ws *stubws;
632
633         stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
634         if (stubws) {
635                 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
636                 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
637                 if (stubws->listener != NULL)
638                         return stubws;
639                 afb_stub_ws_unref(stubws);
640         }
641         return NULL;
642 }
643
644 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
645 {
646         if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
647                 drop_all_events(stubws);
648                 if (stubws->listener)
649                         afb_evt_listener_unref(stubws->listener);
650                 release_all_sessions(stubws);
651                 afb_proto_ws_unref(stubws->proto);
652                 afb_cred_unref(stubws->cred);
653                 afb_apiset_unref(stubws->apiset);
654                 free(stubws);
655         }
656 }
657
658 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
659 {
660         __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
661 }
662
663 void afb_stub_ws_set_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
664 {
665         stubws->on_hangup = on_hangup;
666 }
667
668 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
669 {
670         return stubws->apiname;
671 }
672
673 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
674 {
675         struct afb_api_item api;
676
677         assert(!stubws->listener); /* check client */
678         api.closure = stubws;
679         api.itf = &ws_api_itf;
680         api.group = NULL;
681         return api;
682 }
683
684 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
685 {
686         return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
687 }
688