Fix bug in session and token removal
[src/app-framework-binder.git] / src / afb-stub-ws.c
1 /*
2  * Copyright (C) 2015-2020 "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 "afb-token.h"
47 #include "afb-error-text.h"
48 #include "verbose.h"
49 #include "fdev.h"
50 #include "jobs.h"
51 #include "u16id.h"
52
53 struct afb_stub_ws;
54
55 /**
56  * structure for a ws request: requests on server side
57  */
58 struct server_req {
59         struct afb_xreq xreq;           /**< the xreq */
60         struct afb_stub_ws *stubws;     /**< the client of the request */
61         struct afb_proto_ws_call *call; /**< the incoming call */
62 };
63
64 /**
65  * structure for jobs of describing
66  */
67 struct server_describe
68 {
69         struct afb_stub_ws *stubws;
70         struct afb_proto_ws_describe *describe;
71 };
72
73 /******************* stub description for client or servers ******************/
74
75 struct afb_stub_ws
76 {
77         /* protocol */
78         struct afb_proto_ws *proto;
79
80         /* apiset */
81         struct afb_apiset *apiset;
82
83         /* on hangup callback */
84         void (*on_hangup)(struct afb_stub_ws *);
85
86         union {
87                 /* server side */
88                 struct {
89                         /* listener for events */
90                         struct afb_evt_listener *listener;
91
92                         /* sessions */
93                         struct server_session *sessions;
94
95                         /* credentials of the client */
96                         struct afb_cred *cred;
97
98                         /* event from server */
99                         struct u16id2bool *event_flags;
100
101                         /* transmitted sessions */
102                         struct u16id2ptr *session_proxies;
103
104                         /* transmitted tokens */
105                         struct u16id2ptr *token_proxies;
106                 };
107
108                 /* client side */
109                 struct {
110                         /* event from server */
111                         struct u16id2ptr *event_proxies;
112
113                         /* transmitted sessions */
114                         struct u16id2bool *session_flags;
115
116                         /* transmitted tokens */
117                         struct u16id2bool *token_flags;
118
119                         /* robustify */
120                         struct {
121                                 struct fdev *(*reopen)(void*);
122                                 void *closure;
123                                 void (*release)(void*);
124                         } robust;
125                 };
126         };
127
128         /* count of references */
129         unsigned refcount;
130
131         /* type of the stub: 0=server, 1=client */
132         uint8_t is_client;
133
134         /* the api name */
135         char apiname[];
136 };
137
138 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t server);
139
140 /******************* ws request part for server *****************/
141
142 /* decrement the reference count of the request and free/release it on falling to null */
143 static void server_req_destroy_cb(struct afb_xreq *xreq)
144 {
145         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
146
147         afb_context_disconnect(&wreq->xreq.context);
148         json_object_put(wreq->xreq.json);
149         afb_proto_ws_call_unref(wreq->call);
150         afb_stub_ws_unref(wreq->stubws);
151         free(wreq);
152 }
153
154 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
155 {
156         int rc;
157         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
158
159         rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
160         if (rc < 0)
161                 ERROR("error while sending reply");
162         json_object_put(obj);
163 }
164
165 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
166 {
167         int rc;
168         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
169
170         rc = afb_evt_listener_watch_x2(wreq->stubws->listener, event);
171         if (rc >= 0)
172                 rc = afb_proto_ws_call_subscribe(wreq->call,  afb_evt_event_x2_id(event));
173         if (rc < 0)
174                 ERROR("error while subscribing event");
175         return rc;
176 }
177
178 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
179 {
180         int rc;
181         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
182
183         rc = afb_proto_ws_call_unsubscribe(wreq->call,  afb_evt_event_x2_id(event));
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 static struct afb_proto_ws *client_get_proto(struct afb_stub_ws *stubws)
199 {
200         struct fdev *fdev;
201         struct afb_proto_ws *proto;
202
203         proto = stubws->proto;
204         if (proto == NULL && stubws->robust.reopen) {
205                 fdev = stubws->robust.reopen(stubws->robust.closure);
206                 if (fdev != NULL)
207                         proto = afb_stub_ws_create_proto(stubws, fdev, 0);
208         }
209         return proto;
210 }
211
212 static int client_make_ids(struct afb_stub_ws *stubws, struct afb_proto_ws *proto, struct afb_context *context, uint16_t *sessionid, uint16_t *tokenid)
213 {
214         int rc, rc2;
215         uint16_t sid, tid;
216
217         rc = 0;
218
219         /* get the session */
220         if (!context->session)
221                 sid = 0;
222         else {
223                 sid = afb_session_id(context->session);
224                 rc2 = u16id2bool_set(&stubws->session_flags, sid, 1);
225                 if (rc2 < 0)
226                         rc = rc2;
227                 else if (rc2 == 0)
228                         rc = afb_proto_ws_client_session_create(proto, sid, afb_session_uuid(context->session));
229         }
230
231         /* get the token */
232         if (!context->token)
233                 tid = 0;
234         else {
235                 tid = afb_token_id(context->token);
236                 rc2 = u16id2bool_set(&stubws->token_flags, tid, 1);
237                 if (rc2 < 0)
238                         rc = rc2;
239                 else if (rc2 == 0) {
240                         rc2 = afb_proto_ws_client_token_create(proto, tid, afb_token_string(context->token));
241                         if (rc2 < 0)
242                                 rc = rc2;
243                 }
244         }
245
246         *sessionid = sid;
247         *tokenid = tid;
248         return rc;
249 }
250
251 /* on call, propagate it to the ws service */
252 static void client_api_call_cb(void * closure, struct afb_xreq *xreq)
253 {
254         int rc;
255         struct afb_stub_ws *stubws = closure;
256         struct afb_proto_ws *proto;
257         uint16_t sessionid;
258         uint16_t tokenid;
259
260         proto = client_get_proto(stubws);
261         if (proto == NULL) {
262                 afb_xreq_reply(xreq, NULL, afb_error_text_disconnected, NULL);
263                 return;
264         }
265
266         rc = client_make_ids(stubws, proto, &xreq->context, &sessionid, &tokenid);
267         if (rc >= 0) {
268                 afb_xreq_unhooked_addref(xreq);
269                 rc = afb_proto_ws_client_call(
270                                 proto,
271                                 xreq->request.called_verb,
272                                 afb_xreq_json(xreq),
273                                 sessionid,
274                                 tokenid,
275                                 xreq,
276                                 xreq_on_behalf_cred_export(xreq));
277         }
278         if (rc < 0) {
279                 afb_xreq_reply(xreq, NULL, afb_error_text_internal_error, "can't send message");
280                 afb_xreq_unhooked_unref(xreq);
281         }
282 }
283
284 /* get the description */
285 static void client_api_describe_cb(void * closure, void (*describecb)(void *, struct json_object *), void *clocb)
286 {
287         struct afb_stub_ws *stubws = closure;
288         struct afb_proto_ws *proto;
289
290         proto = client_get_proto(stubws);
291         if (proto)
292                 afb_proto_ws_client_describe(proto, describecb, clocb);
293         else
294                 describecb(clocb, NULL);
295 }
296
297 /******************* server part: manage events **********************************/
298
299 static void server_event_add_cb(void *closure, const char *event, uint16_t eventid)
300 {
301         int rc;
302         struct afb_stub_ws *stubws = closure;
303
304         if (stubws->proto != NULL) {
305                 rc = u16id2bool_set(&stubws->event_flags, eventid, 1);
306                 if (rc == 0) {
307                         rc = afb_proto_ws_server_event_create(stubws->proto, eventid, event);
308                         if (rc < 0)
309                                 u16id2bool_set(&stubws->event_flags, eventid, 0);
310                 }
311         }
312 }
313
314 static void server_event_remove_cb(void *closure, const char *event, uint16_t eventid)
315 {
316         struct afb_stub_ws *stubws = closure;
317
318         if (stubws->proto != NULL) {
319                 if (u16id2bool_set(&stubws->event_flags, eventid, 0))
320                         afb_proto_ws_server_event_remove(stubws->proto, eventid);
321         }
322 }
323
324 static void server_event_push_cb(void *closure, const char *event, uint16_t eventid, struct json_object *object)
325 {
326         struct afb_stub_ws *stubws = closure;
327
328         if (stubws->proto != NULL && u16id2bool_get(stubws->event_flags, eventid))
329                 afb_proto_ws_server_event_push(stubws->proto, eventid, object);
330         json_object_put(object);
331 }
332
333 static void server_event_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
334 {
335         struct afb_stub_ws *stubws = closure;
336
337         if (stubws->proto != NULL)
338                 afb_proto_ws_server_event_broadcast(stubws->proto, event, object, uuid, hop);
339         json_object_put(object);
340 }
341
342 /*****************************************************/
343
344 static void client_on_reply_cb(void *closure, void *request, struct json_object *object, const char *error, const char *info)
345 {
346         struct afb_xreq *xreq = request;
347
348         afb_xreq_reply(xreq, object, error, info);
349         afb_xreq_unhooked_unref(xreq);
350 }
351
352 static void client_on_event_create_cb(void *closure, uint16_t event_id, const char *event_name)
353 {
354         struct afb_stub_ws *stubws = closure;
355         struct afb_event_x2 *event;
356         int rc;
357         
358         /* check conflicts */
359         event = afb_evt_event_x2_create(event_name);
360         if (event == NULL)
361                 ERROR("can't create event %s, out of memory", event_name);
362         else {
363                 rc = u16id2ptr_add(&stubws->event_proxies, event_id, event);
364                 if (rc < 0) {
365                         ERROR("can't record event %s", event_name);
366                         afb_evt_event_x2_unref(event);
367                 }
368         }
369 }
370
371 static void client_on_event_remove_cb(void *closure, uint16_t event_id)
372 {
373         struct afb_stub_ws *stubws = closure;
374         struct afb_event_x2 *event;
375         int rc;
376
377         rc = u16id2ptr_drop(&stubws->event_proxies, event_id, (void**)&event);
378         if (rc == 0 && event)
379                 afb_evt_event_x2_unref(event);
380 }
381
382 static void client_on_event_subscribe_cb(void *closure, void *request, uint16_t event_id)
383 {
384         struct afb_stub_ws *stubws = closure;
385         struct afb_xreq *xreq = request;
386         struct afb_event_x2 *event;
387         int rc;
388
389         rc = u16id2ptr_get(stubws->event_proxies, event_id, (void**)&event);
390         if (rc < 0 || !event || afb_xreq_subscribe(xreq, event) < 0)
391                 ERROR("can't subscribe: %m");
392 }
393
394 static void client_on_event_unsubscribe_cb(void *closure, void *request, uint16_t event_id)
395 {
396         struct afb_stub_ws *stubws = closure;
397         struct afb_xreq *xreq = request;
398         struct afb_event_x2 *event;
399         int rc;
400
401         rc = u16id2ptr_get(stubws->event_proxies, event_id, (void**)&event);
402         if (rc < 0 || !event || afb_xreq_unsubscribe(xreq, event) < 0)
403                 ERROR("can't unsubscribe: %m");
404 }
405
406 static void client_on_event_push_cb(void *closure, uint16_t event_id, struct json_object *data)
407 {
408         struct afb_stub_ws *stubws = closure;
409         struct afb_event_x2 *event;
410         int rc;
411
412         rc = u16id2ptr_get(stubws->event_proxies, event_id, (void**)&event);
413         if (rc >= 0 && event)
414                 rc = afb_evt_event_x2_push(event, data);
415         else
416                 ERROR("unreadable push event");
417         if (rc <= 0)
418                 afb_proto_ws_client_event_unexpected(stubws->proto, event_id);
419 }
420
421 static void client_on_event_broadcast_cb(void *closure, const char *event_name, struct json_object *data, const uuid_binary_t uuid, uint8_t hop)
422 {
423         afb_evt_rebroadcast(event_name, data, uuid, hop);
424 }
425
426 /*****************************************************/
427
428 static struct afb_session *server_add_session(struct afb_stub_ws *stubws, uint16_t sessionid, const char *sessionstr)
429 {
430         struct afb_session *session;
431         int rc, created;
432
433         session = afb_session_get(sessionstr, AFB_SESSION_TIMEOUT_DEFAULT, &created);
434         if (session == NULL)
435                 ERROR("can't create session %s, out of memory", sessionstr);
436         else {
437                 afb_session_set_autoclose(session, 1);
438                 rc = u16id2ptr_add(&stubws->session_proxies, sessionid, session);
439                 if (rc < 0) {
440                         ERROR("can't record session %s", sessionstr);
441                         afb_session_unref(session);
442                         session = NULL;
443                 }
444         }
445         return session;
446 }
447
448 static void server_on_session_create_cb(void *closure, uint16_t sessionid, const char *sessionstr)
449 {
450         struct afb_stub_ws *stubws = closure;
451
452         server_add_session(stubws, sessionid, sessionstr);
453 }
454
455 static void server_on_session_remove_cb(void *closure, uint16_t sessionid)
456 {
457         struct afb_stub_ws *stubws = closure;
458         struct afb_session *session;
459         int rc;
460         
461         rc = u16id2ptr_drop(&stubws->session_proxies, sessionid, (void**)&session);
462         if (rc == 0 && session)
463                 afb_session_unref(session);
464 }
465
466 static void server_on_token_create_cb(void *closure, uint16_t tokenid, const char *tokenstr)
467 {
468         struct afb_stub_ws *stubws = closure;
469         struct afb_token *token;
470         int rc;
471
472         rc = afb_token_get(&token, tokenstr);
473         if (rc < 0)
474                 ERROR("can't create token %s, out of memory", tokenstr);
475         else {
476                 rc = u16id2ptr_add(&stubws->token_proxies, tokenid, token);
477                 if (rc < 0) {
478                         ERROR("can't record token %s", tokenstr);
479                         afb_token_unref(token);
480                 }
481         }
482 }
483
484 static void server_on_token_remove_cb(void *closure, uint16_t tokenid)
485 {
486         struct afb_stub_ws *stubws = closure;
487         struct afb_token *token;
488         int rc;
489         
490         rc = u16id2ptr_drop(&stubws->token_proxies, tokenid, (void**)&token);
491         if (rc == 0 && token)
492                 afb_token_unref(token);
493 }
494
495 static void server_on_event_unexpected_cb(void *closure, uint16_t eventid)
496 {
497         struct afb_stub_ws *stubws = closure;
498
499         afb_evt_listener_unwatch_id(stubws->listener, eventid);
500 }
501
502 static void server_on_call_cb(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, uint16_t sessionid, uint16_t tokenid, const char *user_creds)
503 {
504         const char *errstr = afb_error_text_internal_error;
505         struct afb_stub_ws *stubws = closure;
506         struct server_req *wreq;
507         struct afb_session *session;
508         struct afb_token *token;
509         int rc;
510
511         afb_stub_ws_addref(stubws);
512
513         /* get tokens and sessions */
514         rc = u16id2ptr_get(stubws->session_proxies, sessionid, (void**)&session);
515         if (rc < 0) {
516                 if (sessionid != 0)
517                         goto no_session;
518                 session = server_add_session(stubws, sessionid, NULL);
519                 if (!session)
520                         goto out_of_memory;
521         }
522         if (!tokenid || u16id2ptr_get(stubws->token_proxies, tokenid, (void**)&token) < 0)
523                 token = NULL;
524
525         /* create the request */
526         wreq = malloc(sizeof *wreq);
527         if (wreq == NULL)
528                 goto out_of_memory;
529
530         afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
531         wreq->stubws = stubws;
532         wreq->call = call;
533
534         /* init the context */
535         afb_context_init(&wreq->xreq.context, session, token, stubws->cred);
536         afb_context_on_behalf_import(&wreq->xreq.context, user_creds);
537
538         /* makes the call */
539         wreq->xreq.request.called_api = stubws->apiname;
540         wreq->xreq.request.called_verb = verb;
541         wreq->xreq.json = args;
542         afb_xreq_process(&wreq->xreq, stubws->apiset);
543         return;
544
545 no_session:
546         errstr = afb_error_text_unknown_session;
547 out_of_memory:
548         json_object_put(args);
549         afb_stub_ws_unref(stubws);
550         afb_proto_ws_call_reply(call, NULL, errstr, NULL);
551         afb_proto_ws_call_unref(call);
552 }
553
554 static void server_on_description_cb(void *closure, struct json_object *description)
555 {
556         struct afb_proto_ws_describe *describe = closure;
557         afb_proto_ws_describe_put(describe, description);
558         json_object_put(description);
559 }
560
561
562 static void server_on_describe_cb(void *closure, struct afb_proto_ws_describe *describe)
563 {
564         struct afb_stub_ws *stubws = closure;
565
566         afb_apiset_describe(stubws->apiset, stubws->apiname, server_on_description_cb, describe);
567 }
568
569 /*****************************************************/
570
571 static const struct afb_proto_ws_client_itf client_itf =
572 {
573         .on_reply = client_on_reply_cb,
574         .on_event_create = client_on_event_create_cb,
575         .on_event_remove = client_on_event_remove_cb,
576         .on_event_subscribe = client_on_event_subscribe_cb,
577         .on_event_unsubscribe = client_on_event_unsubscribe_cb,
578         .on_event_push = client_on_event_push_cb,
579         .on_event_broadcast = client_on_event_broadcast_cb,
580 };
581
582 static struct afb_api_itf client_api_itf = {
583         .call = client_api_call_cb,
584         .describe = client_api_describe_cb
585 };
586
587 static const struct afb_proto_ws_server_itf server_itf =
588 {
589         .on_session_create = server_on_session_create_cb,
590         .on_session_remove = server_on_session_remove_cb,
591         .on_token_create = server_on_token_create_cb,
592         .on_token_remove = server_on_token_remove_cb,
593         .on_call = server_on_call_cb,
594         .on_describe = server_on_describe_cb,
595         .on_event_unexpected = server_on_event_unexpected_cb
596 };
597
598 /* the interface for events pushing */
599 static const struct afb_evt_itf server_event_itf = {
600         .broadcast = server_event_broadcast_cb,
601         .push = server_event_push_cb,
602         .add = server_event_add_cb,
603         .remove = server_event_remove_cb
604 };
605
606 /*****************************************************/
607 /*****************************************************/
608
609 static void release_all_sessions_cb(void*closure, uint16_t id, void *ptr)
610 {
611         struct afb_session *session = ptr;
612         afb_session_unref(session);
613 }
614
615 static void release_all_tokens_cb(void*closure, uint16_t id, void *ptr)
616 {
617         struct afb_token *token = ptr;
618         afb_token_unref(token);
619 }
620
621 static void release_all_events_cb(void*closure, uint16_t id, void *ptr)
622 {
623         struct afb_event_x2 *eventid = ptr;
624         afb_evt_event_x2_unref(eventid);
625 }
626
627 /* disconnect */
628 static void disconnect(struct afb_stub_ws *stubws)
629 {
630         struct u16id2ptr *i2p;
631         struct u16id2bool *i2b;
632
633         afb_proto_ws_unref(__atomic_exchange_n(&stubws->proto, NULL, __ATOMIC_RELAXED));
634         if (stubws->is_client) {
635                 i2p = __atomic_exchange_n(&stubws->event_proxies, NULL, __ATOMIC_RELAXED);
636                 if (i2p) {
637                         u16id2ptr_forall(i2p, release_all_events_cb, NULL);
638                         u16id2ptr_destroy(&i2p);
639                 }
640                 i2b = __atomic_exchange_n(&stubws->session_flags, NULL, __ATOMIC_RELAXED);
641                 u16id2bool_destroy(&i2b);
642                 i2b = __atomic_exchange_n(&stubws->token_flags, NULL, __ATOMIC_RELAXED);
643                 u16id2bool_destroy(&i2b);
644         } else {
645                 afb_evt_listener_unref(__atomic_exchange_n(&stubws->listener, NULL, __ATOMIC_RELAXED));
646                 afb_cred_unref(__atomic_exchange_n(&stubws->cred, NULL, __ATOMIC_RELAXED));
647                 i2b = __atomic_exchange_n(&stubws->event_flags, NULL, __ATOMIC_RELAXED);
648                 u16id2bool_destroy(&i2b);
649                 i2p = __atomic_exchange_n(&stubws->session_proxies, NULL, __ATOMIC_RELAXED);
650                 if (i2p) {
651                         u16id2ptr_forall(i2p, release_all_sessions_cb, NULL);
652                         u16id2ptr_destroy(&i2p);
653                 }
654                 i2p = __atomic_exchange_n(&stubws->token_proxies, NULL, __ATOMIC_RELAXED);
655                 if (i2p) {
656                         u16id2ptr_forall(i2p, release_all_tokens_cb, NULL);
657                         u16id2ptr_destroy(&i2p);
658                 }
659         }
660 }
661
662 /* callback when receiving a hangup */
663 static void on_hangup(void *closure)
664 {
665         struct afb_stub_ws *stubws = closure;
666
667         if (stubws->proto) {
668                 afb_stub_ws_addref(stubws);
669                 disconnect(stubws);
670                 if (stubws->on_hangup)
671                         stubws->on_hangup(stubws);
672                 afb_stub_ws_unref(stubws);
673         }
674 }
675
676 static int enqueue_processing(struct afb_proto_ws *proto, void (*callback)(int signum, void* arg), void *arg)
677 {
678         return jobs_queue(proto, 0, callback, arg);
679 }
680
681 /*****************************************************/
682
683 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t is_client)
684 {
685         struct afb_proto_ws *proto;
686
687         stubws->proto = proto = is_client
688                   ? afb_proto_ws_create_client(fdev, &client_itf, stubws)
689                   : afb_proto_ws_create_server(fdev, &server_itf, stubws);
690         if (proto) {
691                 afb_proto_ws_on_hangup(proto, on_hangup);
692                 afb_proto_ws_set_queuing(proto, enqueue_processing);
693         }
694
695         return proto;
696 }
697
698 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, uint8_t is_client)
699 {
700         struct afb_stub_ws *stubws;
701
702         stubws = calloc(1, sizeof *stubws + 1 + strlen(apiname));
703         if (stubws == NULL)
704                 errno = ENOMEM;
705         else {
706                 if (afb_stub_ws_create_proto(stubws, fdev, is_client)) {
707                         stubws->refcount = 1;
708                         stubws->is_client = is_client;
709                         strcpy(stubws->apiname, apiname);
710                         stubws->apiset = afb_apiset_addref(apiset);
711                         return stubws;
712                 }
713                 free(stubws);
714         }
715         fdev_unref(fdev);
716         return NULL;
717 }
718
719 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
720 {
721         return afb_stub_ws_create(fdev, apiname, apiset, 1);
722 }
723
724 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
725 {
726         struct afb_stub_ws *stubws;
727
728         stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
729         if (stubws) {
730                 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
731                 stubws->listener = afb_evt_listener_create(&server_event_itf, stubws);
732                 if (stubws->listener != NULL)
733                         return stubws;
734                 afb_stub_ws_unref(stubws);
735         }
736         return NULL;
737 }
738
739 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
740 {
741         if (stubws && !__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
742
743                 if (stubws->is_client) {
744                         stubws->robust.reopen = NULL;
745                         if (stubws->robust.release)
746                                 stubws->robust.release(stubws->robust.closure);
747                 }
748
749                 disconnect(stubws);
750                 afb_apiset_unref(stubws->apiset);
751                 free(stubws);
752         }
753 }
754
755 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
756 {
757         __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
758 }
759
760 void afb_stub_ws_set_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
761 {
762         stubws->on_hangup = on_hangup;
763 }
764
765 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
766 {
767         return stubws->apiname;
768 }
769
770 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
771 {
772         struct afb_api_item api;
773
774         assert(stubws->is_client); /* check client */
775         api.closure = stubws;
776         api.itf = &client_api_itf;
777         api.group = stubws; /* serialize for reconnections */
778         return api;
779 }
780
781 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
782 {
783         return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
784 }
785
786 void afb_stub_ws_client_robustify(struct afb_stub_ws *stubws, struct fdev *(*reopen)(void*), void *closure, void (*release)(void*))
787 {
788         assert(stubws->is_client); /* check client */
789
790         if (stubws->robust.release)
791                 stubws->robust.release(stubws->robust.closure);
792
793         stubws->robust.reopen = reopen;
794         stubws->robust.closure = closure;
795         stubws->robust.release = release;
796 }