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