afb-stub-ws: Process error on call to remote
[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         int rc;
214         struct afb_stub_ws *stubws = closure;
215
216         rc = afb_proto_ws_client_call(
217                         stubws->proto,
218                         xreq->request.called_verb,
219                         afb_xreq_json(xreq),
220                         afb_session_uuid(xreq->context.session),
221                         xreq,
222                         xreq_on_behalf_cred_export(xreq));
223         if (rc >= 0)
224                 afb_xreq_unhooked_addref(xreq);
225         else
226                 afb_xreq_reply(xreq, NULL, "internal", "can't send message");
227 }
228
229 static void client_on_description_cb(void *closure, struct json_object *data)
230 {
231         struct client_describe *desc = closure;
232
233         desc->result = data;
234         jobs_leave(desc->jobloop);
235 }
236
237 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
238 {
239         struct client_describe *desc = closure;
240
241         if (signum)
242                 jobs_leave(jobloop);
243         else {
244                 desc->jobloop = jobloop;
245                 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
246         }
247 }
248
249 /* get the description */
250 static struct json_object *client_describe_cb(void * closure)
251 {
252         struct client_describe desc;
253
254         /* synchronous job: send the request and wait its result */
255         desc.stubws = closure;
256         desc.result = NULL;
257         jobs_enter(NULL, 0, client_send_describe_cb, &desc);
258         return desc.result;
259 }
260
261 /******************* server part: manage events **********************************/
262
263 static void server_event_add(void *closure, const char *event, int eventid)
264 {
265         struct afb_stub_ws *stubws = closure;
266
267         afb_proto_ws_server_event_create(stubws->proto, event, eventid);
268 }
269
270 static void server_event_remove(void *closure, const char *event, int eventid)
271 {
272         struct afb_stub_ws *stubws = closure;
273
274         afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
275 }
276
277 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
278 {
279         struct afb_stub_ws *stubws = closure;
280
281         afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
282         json_object_put(object);
283 }
284
285 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
286 {
287         struct afb_stub_ws *stubws = closure;
288
289         afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
290         json_object_put(object);
291 }
292
293 /*****************************************************/
294
295 static void on_reply(void *closure, void *request, struct json_object *object, const char *error, const char *info)
296 {
297         struct afb_xreq *xreq = request;
298
299         afb_xreq_reply(xreq, object, error, info);
300         afb_xreq_unhooked_unref(xreq);
301 }
302
303 static void on_event_create(void *closure, const char *event_name, int event_id)
304 {
305         struct afb_stub_ws *stubws = closure;
306         struct client_event *ev;
307
308         /* check conflicts */
309         ev = client_event_search(stubws, event_id, event_name);
310         if (ev != NULL) {
311                 ev->refcount++;
312                 return;
313         }
314
315         /* no conflict, try to add it */
316         ev = malloc(sizeof *ev);
317         if (ev != NULL) {
318                 ev->event = afb_evt_event_x2_create(event_name);
319                 if (ev->event != NULL) {
320                         ev->refcount = 1;
321                         ev->id = event_id;
322                         ev->next = stubws->events;
323                         stubws->events = ev;
324                         return;
325                 }
326                 free(ev);
327         }
328         ERROR("can't create event %s, out of memory", event_name);
329 }
330
331 static void on_event_remove(void *closure, const char *event_name, int event_id)
332 {
333         struct afb_stub_ws *stubws = closure;
334         struct client_event *ev, **prv;
335
336         /* check conflicts */
337         ev = client_event_search(stubws, event_id, event_name);
338         if (ev == NULL)
339                 return;
340
341         /* decrease the reference count */
342         if (--ev->refcount)
343                 return;
344
345         /* unlinks the event */
346         prv = &stubws->events;
347         while (*prv != ev)
348                 prv = &(*prv)->next;
349         *prv = ev->next;
350
351         /* destroys the event */
352         afb_evt_event_x2_unref(ev->event);
353         free(ev);
354 }
355
356 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
357 {
358         struct afb_stub_ws *stubws = closure;
359         struct afb_xreq *xreq = request;
360         struct client_event *ev;
361
362         /* check conflicts */
363         ev = client_event_search(stubws, event_id, event_name);
364         if (ev == NULL)
365                 return;
366
367         if (afb_xreq_subscribe(xreq, ev->event) < 0)
368                 ERROR("can't subscribe: %m");
369 }
370
371 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
372 {
373         struct afb_stub_ws *stubws = closure;
374         struct afb_xreq *xreq = request;
375         struct client_event *ev;
376
377         /* check conflicts */
378         ev = client_event_search(stubws, event_id, event_name);
379         if (ev == NULL)
380                 return;
381
382         if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
383                 ERROR("can't unsubscribe: %m");
384 }
385
386 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
387 {
388         struct afb_stub_ws *stubws = closure;
389         struct client_event *ev;
390
391         /* check conflicts */
392         ev = client_event_search(stubws, event_id, event_name);
393         if (ev)
394                 afb_evt_event_x2_push(ev->event, data);
395         else
396                 ERROR("unreadable push event");
397 }
398
399 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
400 {
401         afb_evt_broadcast(event_name, data);
402 }
403
404 /*****************************************************/
405
406 static void record_session(struct afb_stub_ws *stubws, struct afb_session *session)
407 {
408         struct server_session *s, **prv;
409
410         /* search */
411         prv = &stubws->sessions;
412         while ((s = *prv)) {
413                 if (s->session == session)
414                         return;
415                 if (afb_session_is_closed(s->session)) {
416                         *prv = s->next;
417                         afb_session_unref(s->session);
418                         free(s);
419                 }
420                 else
421                         prv = &s->next;
422         }
423
424         /* create */
425         s = malloc(sizeof *s);
426         if (s) {
427                 s->session = afb_session_addref(session);
428                 s->next = stubws->sessions;
429                 stubws->sessions = s;
430         }
431 }
432
433 static void release_all_sessions(struct afb_stub_ws *stubws)
434 {
435         struct server_session *s, *n;
436
437         s = __atomic_exchange_n(&stubws->sessions, NULL, __ATOMIC_RELAXED);
438         while(s) {
439                 n = s->next;
440                 afb_session_unref(s->session);
441                 free(s);
442                 s = n;
443         }
444 }
445
446 /*****************************************************/
447
448 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)
449 {
450         struct afb_stub_ws *stubws = closure;
451         struct server_req *wreq;
452
453         afb_stub_ws_addref(stubws);
454
455         /* create the request */
456         wreq = malloc(sizeof *wreq);
457         if (wreq == NULL)
458                 goto out_of_memory;
459
460         afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
461         wreq->stubws = stubws;
462         wreq->call = call;
463
464         /* init the context */
465         if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
466                 goto unconnected;
467         wreq->xreq.context.validated = 1;
468         record_session(stubws, wreq->xreq.context.session);
469         if (wreq->xreq.context.created)
470                 afb_session_set_autoclose(wreq->xreq.context.session, 1);
471
472         /* makes the call */
473         wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
474         wreq->xreq.request.called_api = stubws->apiname;
475         wreq->xreq.request.called_verb = verb;
476         wreq->xreq.json = args;
477         afb_xreq_process(&wreq->xreq, stubws->apiset);
478         return;
479
480 unconnected:
481         free(wreq);
482 out_of_memory:
483         json_object_put(args);
484         afb_stub_ws_unref(stubws);
485         afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
486         afb_proto_ws_call_unref(call);
487 }
488
489 static void server_describe_sjob(int signum, void *closure)
490 {
491         struct json_object *obj;
492         struct server_describe *desc = closure;
493
494         /* get the description if possible */
495         obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
496
497         /* send it */
498         afb_proto_ws_describe_put(desc->describe, obj);
499         json_object_put(obj);
500         afb_stub_ws_unref(desc->stubws);
501 }
502
503 static void server_describe_job(int signum, void *closure)
504 {
505         server_describe_sjob(signum, closure);
506         free(closure);
507 }
508
509 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
510 {
511         struct server_describe *desc, sdesc;
512         struct afb_stub_ws *stubws = closure;
513
514         /* allocate (if possible) and init */
515         desc = malloc(sizeof *desc);
516         if (desc == NULL)
517                 desc = &sdesc;
518         desc->stubws = stubws;
519         desc->describe = describe;
520         afb_stub_ws_addref(stubws);
521
522         /* process */
523         if (desc == &sdesc)
524                 jobs_call(NULL, 0, server_describe_sjob, desc);
525         else {
526                 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
527                         jobs_call(NULL, 0, server_describe_job, desc);
528         }
529 }
530
531 /*****************************************************/
532
533 static const struct afb_proto_ws_client_itf client_itf =
534 {
535         .on_reply = on_reply,
536         .on_event_create = on_event_create,
537         .on_event_remove = on_event_remove,
538         .on_event_subscribe = on_event_subscribe,
539         .on_event_unsubscribe = on_event_unsubscribe,
540         .on_event_push = on_event_push,
541         .on_event_broadcast = on_event_broadcast,
542 };
543
544 static const struct afb_proto_ws_server_itf server_itf =
545 {
546         .on_call = on_call,
547         .on_describe = on_describe
548 };
549
550 static struct afb_api_itf ws_api_itf = {
551         .call = client_call_cb,
552         .describe = client_describe_cb
553 };
554
555 /* the interface for events pushing */
556 static const struct afb_evt_itf server_evt_itf = {
557         .broadcast = server_event_broadcast,
558         .push = server_event_push,
559         .add = server_event_add,
560         .remove = server_event_remove
561 };
562
563 /*****************************************************/
564
565 static void drop_all_events(struct afb_stub_ws *stubws)
566 {
567         struct client_event *ev, *nxt;
568
569         ev = stubws->events;
570         stubws->events = NULL;
571
572         while (ev) {
573                 nxt = ev->next;
574                 afb_evt_event_x2_unref(ev->event);
575                 free(ev);
576                 ev = nxt;
577         }
578 }
579
580 /* callback when receiving a hangup */
581 static void on_hangup(void *closure)
582 {
583         struct afb_stub_ws *stubws = closure;
584
585         afb_stub_ws_addref(stubws);
586         if (stubws->on_hangup)
587                 stubws->on_hangup(stubws);
588
589         release_all_sessions(stubws);
590         afb_stub_ws_unref(stubws);
591 }
592
593 static int enqueue_processing(void (*callback)(int signum, void* arg), void *arg)
594 {
595         return jobs_queue(NULL, 0, callback, arg);
596 }
597
598 /*****************************************************/
599
600 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, int client)
601 {
602         struct afb_stub_ws *stubws;
603
604         stubws = calloc(1, sizeof *stubws + strlen(apiname));
605         if (stubws == NULL)
606                 errno = ENOMEM;
607         else {
608                 if (client)
609                         stubws->proto = afb_proto_ws_create_client(fdev, &client_itf, stubws);
610                 else
611                         stubws->proto = afb_proto_ws_create_server(fdev, &server_itf, stubws);
612
613                 if (stubws->proto) {
614                         strcpy(stubws->apiname, apiname);
615                         stubws->apiset = afb_apiset_addref(apiset);
616                         stubws->refcount = 1;
617                         afb_proto_ws_on_hangup(stubws->proto, on_hangup);
618                         afb_proto_ws_set_queuing(stubws->proto, enqueue_processing);
619                         return stubws;
620                 }
621                 free(stubws);
622         }
623         fdev_unref(fdev);
624         return NULL;
625 }
626
627 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
628 {
629         return afb_stub_ws_create(fdev, apiname, apiset, 1);
630 }
631
632 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
633 {
634         struct afb_stub_ws *stubws;
635
636         stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
637         if (stubws) {
638                 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
639                 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
640                 if (stubws->listener != NULL)
641                         return stubws;
642                 afb_stub_ws_unref(stubws);
643         }
644         return NULL;
645 }
646
647 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
648 {
649         if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
650                 drop_all_events(stubws);
651                 if (stubws->listener)
652                         afb_evt_listener_unref(stubws->listener);
653                 release_all_sessions(stubws);
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_set_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_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
677 {
678         struct afb_api_item api;
679
680         assert(!stubws->listener); /* check client */
681         api.closure = stubws;
682         api.itf = &ws_api_itf;
683         api.group = NULL;
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