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