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