2 * Copyright (C) 2015-2019 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <sys/types.h>
29 #include <sys/socket.h>
33 #include <json-c/json.h>
35 #include <afb/afb-event-x2.h>
37 #include "afb-session.h"
40 #include "afb-apiset.h"
41 #include "afb-proto-ws.h"
42 #include "afb-stub-ws.h"
43 #include "afb-context.h"
46 #include "afb-token.h"
47 #include "afb-error-text.h"
56 * structure for a ws request: requests on server side
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 */
65 * structure for jobs of describing
67 struct server_describe
69 struct afb_stub_ws *stubws;
70 struct afb_proto_ws_describe *describe;
73 /******************* stub description for client or servers ******************/
78 struct afb_proto_ws *proto;
81 struct afb_apiset *apiset;
83 /* on hangup callback */
84 void (*on_hangup)(struct afb_stub_ws *);
89 /* listener for events */
90 struct afb_evt_listener *listener;
93 struct server_session *sessions;
95 /* credentials of the client */
96 struct afb_cred *cred;
98 /* event from server */
99 struct u16id2bool *event_flags;
101 /* transmitted sessions */
102 struct u16id2ptr *session_proxies;
104 /* transmitted tokens */
105 struct u16id2ptr *token_proxies;
110 /* event from server */
111 struct u16id2ptr *event_proxies;
113 /* transmitted sessions */
114 struct u16id2bool *session_flags;
116 /* transmitted tokens */
117 struct u16id2bool *token_flags;
121 struct fdev *(*reopen)(void*);
123 void (*release)(void*);
128 /* count of references */
131 /* type of the stub: 0=server, 1=client */
138 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t server);
140 /******************* ws request part for server *****************/
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)
145 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
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);
154 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
157 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
159 rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
161 ERROR("error while sending reply");
162 json_object_put(obj);
165 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
168 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
170 rc = afb_evt_listener_watch_x2(wreq->stubws->listener, event);
172 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_event_x2_id(event));
174 ERROR("error while subscribing event");
178 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
181 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
183 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_event_x2_id(event));
184 rc2 = afb_evt_listener_unwatch_x2(wreq->stubws->listener, event);
185 if (rc >= 0 && rc2 < 0)
188 ERROR("error while unsubscribing event");
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
199 /******************* client part **********************************/
201 static struct afb_proto_ws *client_get_proto(struct afb_stub_ws *stubws)
204 struct afb_proto_ws *proto;
206 proto = stubws->proto;
207 if (proto == NULL && stubws->robust.reopen) {
208 fdev = stubws->robust.reopen(stubws->robust.closure);
210 proto = afb_stub_ws_create_proto(stubws, fdev, 0);
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)
222 /* get the session */
223 if (!context->session)
226 sid = afb_session_id(context->session);
227 rc2 = u16id2bool_set(&stubws->session_flags, sid, 1);
231 rc = afb_proto_ws_client_session_create(proto, sid, afb_session_uuid(context->session));
238 tid = afb_token_id(context->token);
239 rc2 = u16id2bool_set(&stubws->token_flags, tid, 1);
243 rc2 = afb_proto_ws_client_token_create(proto, tid, afb_token_string(context->token));
254 /* on call, propagate it to the ws service */
255 static void client_api_call_cb(void * closure, struct afb_xreq *xreq)
258 struct afb_stub_ws *stubws = closure;
259 struct afb_proto_ws *proto;
263 proto = client_get_proto(stubws);
265 afb_xreq_reply(xreq, NULL, afb_error_text_disconnected, NULL);
269 rc = client_make_ids(stubws, proto, &xreq->context, &sessionid, &tokenid);
271 afb_xreq_unhooked_addref(xreq);
272 rc = afb_proto_ws_client_call(
274 xreq->request.called_verb,
279 xreq_on_behalf_cred_export(xreq));
282 afb_xreq_reply(xreq, NULL, afb_error_text_internal_error, "can't send message");
283 afb_xreq_unhooked_unref(xreq);
287 /* get the description */
288 static void client_api_describe_cb(void * closure, void (*describecb)(void *, struct json_object *), void *clocb)
290 struct afb_stub_ws *stubws = closure;
291 struct afb_proto_ws *proto;
293 proto = client_get_proto(stubws);
295 afb_proto_ws_client_describe(proto, describecb, clocb);
297 describecb(clocb, NULL);
300 /******************* server part: manage events **********************************/
302 static void server_event_add_cb(void *closure, const char *event, uint16_t eventid)
305 struct afb_stub_ws *stubws = closure;
307 if (stubws->proto != NULL) {
308 rc = u16id2bool_set(&stubws->event_flags, eventid, 1);
310 rc = afb_proto_ws_server_event_create(stubws->proto, eventid, event);
312 u16id2bool_set(&stubws->event_flags, eventid, 0);
317 static void server_event_remove_cb(void *closure, const char *event, uint16_t eventid)
319 struct afb_stub_ws *stubws = closure;
321 if (stubws->proto != NULL) {
322 if (u16id2bool_set(&stubws->event_flags, eventid, 0))
323 afb_proto_ws_server_event_remove(stubws->proto, eventid);
327 static void server_event_push_cb(void *closure, const char *event, uint16_t eventid, struct json_object *object)
329 struct afb_stub_ws *stubws = closure;
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);
336 static void server_event_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
338 struct afb_stub_ws *stubws = closure;
340 if (stubws->proto != NULL)
341 afb_proto_ws_server_event_broadcast(stubws->proto, event, object, uuid, hop);
342 json_object_put(object);
345 /*****************************************************/
347 static void client_on_reply_cb(void *closure, void *request, struct json_object *object, const char *error, const char *info)
349 struct afb_xreq *xreq = request;
351 afb_xreq_reply(xreq, object, error, info);
352 afb_xreq_unhooked_unref(xreq);
355 static void client_on_event_create_cb(void *closure, uint16_t event_id, const char *event_name)
357 struct afb_stub_ws *stubws = closure;
358 struct afb_event_x2 *event;
361 /* check conflicts */
362 event = afb_evt_event_x2_create(event_name);
364 ERROR("can't create event %s, out of memory", event_name);
366 rc = u16id2ptr_add(&stubws->event_proxies, event_id, event);
368 ERROR("can't record event %s", event_name);
369 afb_evt_event_x2_unref(event);
374 static void client_on_event_remove_cb(void *closure, uint16_t event_id)
376 struct afb_stub_ws *stubws = closure;
377 struct afb_event_x2 *event;
380 rc = u16id2ptr_drop(&stubws->event_proxies, event_id, (void**)&event);
381 if (rc == 0 && event)
382 afb_evt_event_x2_unref(event);
385 static void client_on_event_subscribe_cb(void *closure, void *request, uint16_t event_id)
387 struct afb_stub_ws *stubws = closure;
388 struct afb_xreq *xreq = request;
389 struct afb_event_x2 *event;
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");
397 static void client_on_event_unsubscribe_cb(void *closure, void *request, uint16_t event_id)
399 struct afb_stub_ws *stubws = closure;
400 struct afb_xreq *xreq = request;
401 struct afb_event_x2 *event;
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");
409 static void client_on_event_push_cb(void *closure, uint16_t event_id, struct json_object *data)
411 struct afb_stub_ws *stubws = closure;
412 struct afb_event_x2 *event;
415 rc = u16id2ptr_get(stubws->event_proxies, event_id, (void**)&event);
416 if (rc >= 0 && event)
417 rc = afb_evt_event_x2_push(event, data);
419 ERROR("unreadable push event");
421 afb_proto_ws_client_event_unexpected(stubws->proto, event_id);
424 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)
426 afb_evt_rebroadcast(event_name, data, uuid, hop);
429 /*****************************************************/
431 static struct afb_session *server_add_session(struct afb_stub_ws *stubws, uint16_t sessionid, const char *sessionstr)
433 struct afb_session *session;
436 session = afb_session_get(sessionstr, AFB_SESSION_TIMEOUT_DEFAULT, &created);
438 ERROR("can't create session %s, out of memory", sessionstr);
440 afb_session_set_autoclose(session, 1);
441 rc = u16id2ptr_add(&stubws->session_proxies, sessionid, session);
443 ERROR("can't record session %s", sessionstr);
444 afb_session_unref(session);
451 static void server_on_session_create_cb(void *closure, uint16_t sessionid, const char *sessionstr)
453 struct afb_stub_ws *stubws = closure;
455 server_add_session(stubws, sessionid, sessionstr);
458 static void server_on_session_remove_cb(void *closure, uint16_t sessionid)
460 struct afb_stub_ws *stubws = closure;
461 struct afb_session *session;
464 rc = u16id2ptr_drop(&stubws->event_proxies, sessionid, (void**)&session);
465 if (rc == 0 && session)
466 afb_session_unref(session);
469 static void server_on_token_create_cb(void *closure, uint16_t tokenid, const char *tokenstr)
471 struct afb_stub_ws *stubws = closure;
472 struct afb_token *token;
475 rc = afb_token_get(&token, tokenstr);
477 ERROR("can't create token %s, out of memory", tokenstr);
479 rc = u16id2ptr_add(&stubws->token_proxies, tokenid, token);
481 ERROR("can't record token %s", tokenstr);
482 afb_token_unref(token);
487 static void server_on_token_remove_cb(void *closure, uint16_t tokenid)
489 struct afb_stub_ws *stubws = closure;
490 struct afb_token *token;
493 rc = u16id2ptr_drop(&stubws->event_proxies, tokenid, (void**)&token);
494 if (rc == 0 && token)
495 afb_token_unref(token);
498 static void server_on_event_unexpected_cb(void *closure, uint16_t eventid)
500 struct afb_stub_ws *stubws = closure;
502 afb_evt_listener_unwatch_id(stubws->listener, eventid);
505 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)
507 struct afb_stub_ws *stubws = closure;
508 struct server_req *wreq;
509 struct afb_session *session;
510 struct afb_token *token;
513 afb_stub_ws_addref(stubws);
515 /* get tokens and sessions */
516 rc = u16id2ptr_get(stubws->session_proxies, sessionid, (void**)&session);
520 session = server_add_session(stubws, sessionid, NULL);
524 if (!tokenid || u16id2ptr_get(stubws->token_proxies, tokenid, (void**)&token) < 0)
527 /* create the request */
528 wreq = malloc(sizeof *wreq);
532 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
533 wreq->stubws = stubws;
536 /* init the context */
537 afb_context_init(&wreq->xreq.context, session, token, stubws->cred);
538 afb_context_on_behalf_import(&wreq->xreq.context, user_creds);
541 wreq->xreq.request.called_api = stubws->apiname;
542 wreq->xreq.request.called_verb = verb;
543 wreq->xreq.json = args;
544 afb_xreq_process(&wreq->xreq, stubws->apiset);
549 json_object_put(args);
550 afb_stub_ws_unref(stubws);
551 afb_proto_ws_call_reply(call, NULL, afb_error_text_internal_error, NULL);
552 afb_proto_ws_call_unref(call);
555 static void server_on_description_cb(void *closure, struct json_object *description)
557 struct afb_proto_ws_describe *describe = closure;
558 afb_proto_ws_describe_put(describe, description);
559 json_object_put(description);
563 static void server_on_describe_cb(void *closure, struct afb_proto_ws_describe *describe)
565 struct afb_stub_ws *stubws = closure;
567 afb_apiset_describe(stubws->apiset, stubws->apiname, server_on_description_cb, describe);
570 /*****************************************************/
572 static const struct afb_proto_ws_client_itf client_itf =
574 .on_reply = client_on_reply_cb,
575 .on_event_create = client_on_event_create_cb,
576 .on_event_remove = client_on_event_remove_cb,
577 .on_event_subscribe = client_on_event_subscribe_cb,
578 .on_event_unsubscribe = client_on_event_unsubscribe_cb,
579 .on_event_push = client_on_event_push_cb,
580 .on_event_broadcast = client_on_event_broadcast_cb,
583 static struct afb_api_itf client_api_itf = {
584 .call = client_api_call_cb,
585 .describe = client_api_describe_cb
588 static const struct afb_proto_ws_server_itf server_itf =
590 .on_session_create = server_on_session_create_cb,
591 .on_session_remove = server_on_session_remove_cb,
592 .on_token_create = server_on_token_create_cb,
593 .on_token_remove = server_on_token_remove_cb,
594 .on_call = server_on_call_cb,
595 .on_describe = server_on_describe_cb,
596 .on_event_unexpected = server_on_event_unexpected_cb
599 /* the interface for events pushing */
600 static const struct afb_evt_itf server_event_itf = {
601 .broadcast = server_event_broadcast_cb,
602 .push = server_event_push_cb,
603 .add = server_event_add_cb,
604 .remove = server_event_remove_cb
607 /*****************************************************/
608 /*****************************************************/
610 static void release_all_sessions_cb(void*closure, uint16_t id, void *ptr)
612 struct afb_session *session = ptr;
613 afb_session_unref(session);
616 static void release_all_tokens_cb(void*closure, uint16_t id, void *ptr)
618 struct afb_token *token = ptr;
619 afb_token_unref(token);
622 static void release_all_events_cb(void*closure, uint16_t id, void *ptr)
624 struct afb_event_x2 *eventid = ptr;
625 afb_evt_event_x2_unref(eventid);
629 static void disconnect(struct afb_stub_ws *stubws)
631 struct u16id2ptr *i2p;
632 struct u16id2bool *i2b;
634 afb_proto_ws_unref(__atomic_exchange_n(&stubws->proto, NULL, __ATOMIC_RELAXED));
635 if (stubws->is_client) {
636 i2p = __atomic_exchange_n(&stubws->event_proxies, NULL, __ATOMIC_RELAXED);
638 u16id2ptr_forall(i2p, release_all_events_cb, NULL);
639 u16id2ptr_destroy(&i2p);
641 i2b = __atomic_exchange_n(&stubws->session_flags, NULL, __ATOMIC_RELAXED);
642 u16id2bool_destroy(&i2b);
643 i2b = __atomic_exchange_n(&stubws->token_flags, NULL, __ATOMIC_RELAXED);
644 u16id2bool_destroy(&i2b);
646 afb_evt_listener_unref(__atomic_exchange_n(&stubws->listener, NULL, __ATOMIC_RELAXED));
647 afb_cred_unref(__atomic_exchange_n(&stubws->cred, NULL, __ATOMIC_RELAXED));
648 i2b = __atomic_exchange_n(&stubws->event_flags, NULL, __ATOMIC_RELAXED);
649 u16id2bool_destroy(&i2b);
650 i2p = __atomic_exchange_n(&stubws->session_proxies, NULL, __ATOMIC_RELAXED);
652 u16id2ptr_forall(i2p, release_all_sessions_cb, NULL);
653 u16id2ptr_destroy(&i2p);
655 i2p = __atomic_exchange_n(&stubws->token_proxies, NULL, __ATOMIC_RELAXED);
657 u16id2ptr_forall(i2p, release_all_tokens_cb, NULL);
658 u16id2ptr_destroy(&i2p);
663 /* callback when receiving a hangup */
664 static void on_hangup(void *closure)
666 struct afb_stub_ws *stubws = closure;
669 afb_stub_ws_addref(stubws);
671 if (stubws->on_hangup)
672 stubws->on_hangup(stubws);
673 afb_stub_ws_unref(stubws);
677 static int enqueue_processing(struct afb_proto_ws *proto, void (*callback)(int signum, void* arg), void *arg)
679 return jobs_queue(proto, 0, callback, arg);
682 /*****************************************************/
684 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t is_client)
686 struct afb_proto_ws *proto;
688 stubws->proto = proto = is_client
689 ? afb_proto_ws_create_client(fdev, &client_itf, stubws)
690 : afb_proto_ws_create_server(fdev, &server_itf, stubws);
692 afb_proto_ws_on_hangup(proto, on_hangup);
693 afb_proto_ws_set_queuing(proto, enqueue_processing);
699 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, uint8_t is_client)
701 struct afb_stub_ws *stubws;
703 stubws = calloc(1, sizeof *stubws + 1 + strlen(apiname));
707 if (afb_stub_ws_create_proto(stubws, fdev, is_client)) {
708 stubws->refcount = 1;
709 stubws->is_client = is_client;
710 strcpy(stubws->apiname, apiname);
711 stubws->apiset = afb_apiset_addref(apiset);
720 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
722 return afb_stub_ws_create(fdev, apiname, apiset, 1);
725 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
727 struct afb_stub_ws *stubws;
729 stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
731 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
732 stubws->listener = afb_evt_listener_create(&server_event_itf, stubws);
733 if (stubws->listener != NULL)
735 afb_stub_ws_unref(stubws);
740 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
742 if (stubws && !__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
744 if (stubws->is_client) {
745 stubws->robust.reopen = NULL;
746 if (stubws->robust.release)
747 stubws->robust.release(stubws->robust.closure);
751 afb_apiset_unref(stubws->apiset);
756 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
758 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
761 void afb_stub_ws_set_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
763 stubws->on_hangup = on_hangup;
766 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
768 return stubws->apiname;
771 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
773 struct afb_api_item api;
775 assert(stubws->is_client); /* check client */
776 api.closure = stubws;
777 api.itf = &client_api_itf;
778 api.group = stubws; /* serialize for reconnections */
782 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
784 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
787 void afb_stub_ws_client_robustify(struct afb_stub_ws *stubws, struct fdev *(*reopen)(void*), void *closure, void (*release)(void*))
789 assert(stubws->is_client); /* check client */
791 if (stubws->robust.release)
792 stubws->robust.release(stubws->robust.closure);
794 stubws->robust.reopen = reopen;
795 stubws->robust.closure = closure;
796 stubws->robust.release = release;