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