2 * Copyright (C) 2015, 2016, 2017 "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.
19 #define NO_PLUGIN_VERBOSE_MACRO
29 #include <sys/types.h>
30 #include <sys/socket.h>
34 #include <json-c/json.h>
35 #include <systemd/sd-event.h>
37 #include <afb/afb-req-itf.h>
39 #include "afb-common.h"
41 #include "afb-session.h"
44 #include "afb-msg-json.h"
46 #include "afb-apiset.h"
47 #include "afb-api-so.h"
48 #include "afb-context.h"
58 #define CHAR_FOR_CALL 'C'
59 #define CHAR_FOR_ANSWER_SUCCESS 'T'
60 #define CHAR_FOR_ANSWER_FAIL 'F'
61 #define CHAR_FOR_EVT_BROADCAST '*'
62 #define CHAR_FOR_EVT_ADD '+'
63 #define CHAR_FOR_EVT_DEL '-'
64 #define CHAR_FOR_EVT_PUSH '!'
65 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
66 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
67 #define CHAR_FOR_SUBCALL_CALL 'B'
68 #define CHAR_FOR_SUBCALL_REPLY 'R'
74 char *path; /* path of the object for the API */
75 char *api; /* api name of the interface */
76 int fd; /* file descriptor */
77 pthread_mutex_t mutex; /**< resource control */
81 struct api_ws_event *events;
82 struct api_ws_memo *memos;
85 sd_event_source *listensrc; /**< systemd source for server socket */
86 struct afb_apiset *apiset;
95 /******************* common usefull tools **********************************/
98 * translate a pointer to some integer
99 * @param ptr the pointer to translate
102 static inline uint32_t ptr2id(void *ptr)
104 return (uint32_t)(((intptr_t)ptr) >> 6);
107 /******************* websocket interface for client part **********************************/
109 static void api_ws_client_on_binary(void *closure, char *data, size_t size);
111 static const struct afb_ws_itf api_ws_client_ws_itf =
115 .on_binary = api_ws_client_on_binary,
120 /******************* event structures for server part **********************************/
122 static void api_ws_server_event_add(void *closure, const char *event, int eventid);
123 static void api_ws_server_event_remove(void *closure, const char *event, int eventid);
124 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
125 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
127 /* the interface for events pushing */
128 static const struct afb_evt_itf api_ws_server_evt_itf = {
129 .broadcast = api_ws_server_event_broadcast,
130 .push = api_ws_server_event_push,
131 .add = api_ws_server_event_add,
132 .remove = api_ws_server_event_remove
135 /******************* handling subcalls *****************************/
138 * Structure on server side for recording pending
141 struct api_ws_subcall
143 struct api_ws_subcall *next; /**< next subcall for the client */
144 uint32_t subcallid; /**< the subcallid */
145 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
146 void *closure; /**< closure of the callback */
150 * Structure for sending back replies on client side
154 struct api_ws *apiws; /**< api descriptor */
155 uint32_t subcallid; /**< subcallid for the reply */
158 /******************* client description part for server *****************************/
162 /* the server ws-api */
165 /* count of references */
168 /* file descriptor */
171 /* resource control */
172 pthread_mutex_t mutex;
174 /* listener for events */
175 struct afb_evt_listener *listener;
181 struct afb_cred *cred;
183 /* pending subcalls */
184 struct api_ws_subcall *subcalls;
187 struct afb_apiset *apiset;
190 /******************* websocket interface for client part **********************************/
192 static void api_ws_server_on_binary(void *closure, char *data, size_t size);
193 static void api_ws_server_on_hangup(void *closure);
195 static const struct afb_ws_itf api_ws_server_ws_itf =
199 .on_binary = api_ws_server_on_binary,
201 .on_hangup = api_ws_server_on_hangup
204 /******************* ws request part for server *****************/
207 * structure for a ws request
209 struct api_ws_server_req {
210 struct afb_xreq xreq; /* the xreq */
211 struct api_ws_client *client; /* the client of the request */
212 uint32_t msgid; /* the incoming request msgid */
215 static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info);
216 static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info);
217 static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq);
218 static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event);
219 static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event);
220 static void api_ws_server_req_subcall_cb(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
222 const struct afb_xreq_query_itf afb_api_ws_xreq_itf = {
223 .success = api_ws_server_req_success_cb,
224 .fail = api_ws_server_req_fail_cb,
225 .unref = api_ws_server_req_destroy_cb,
226 .subcall = api_ws_server_req_subcall_cb,
227 .subscribe = api_ws_server_req_subscribe_cb,
228 .unsubscribe = api_ws_server_req_unsubscribe_cb
231 /******************* common part **********************************/
234 * create a structure api_ws not connected to the 'path'.
236 static struct api_ws *api_ws_make(const char *path)
241 /* allocates the structure */
242 length = strlen(path);
243 api = calloc(1, sizeof *api + 1 + length);
249 /* path is copied after the struct */
250 api->path = (char*)(api+1);
251 memcpy(api->path, path, length + 1);
253 /* api name is at the end of the path */
254 while (length && path[length - 1] != '/' && path[length - 1] != ':')
256 api->api = &api->path[length];
257 if (api->api == NULL || !afb_api_is_valid_name(api->api)) {
261 pthread_mutex_init(&api->mutex, NULL);
272 static int api_ws_socket_unix(const char *path, int server)
275 struct sockaddr_un addr;
278 length = strlen(path);
280 errno = ENAMETOOLONG;
284 if (server && path[0] != '@')
287 fd = socket(AF_UNIX, SOCK_STREAM, 0);
291 memset(&addr, 0, sizeof addr);
292 addr.sun_family = AF_UNIX;
293 strcpy(addr.sun_path, path);
294 if (addr.sun_path[0] == '@')
295 addr.sun_path[0] = 0; /* implement abstract sockets */
297 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
299 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
308 static int api_ws_socket_inet(const char *path, int server)
311 const char *service, *host, *api;
312 struct addrinfo hint, *rai, *iai;
315 api = strrchr(path, '/');
316 service = strrchr(path, ':');
317 if (api == NULL || service == NULL || api < service) {
321 host = strndupa(path, service++ - path);
322 service = strndupa(service, api - service);
325 memset(&hint, 0, sizeof hint);
326 hint.ai_family = AF_INET;
327 hint.ai_socktype = SOCK_STREAM;
328 rc = getaddrinfo(host, service, &hint, &rai);
336 while (iai != NULL) {
337 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
340 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
342 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
357 static int api_ws_socket(const char *path, int server)
361 /* check for systemd socket */
362 if (0 == strncmp(path, "sd:", 3))
363 fd = sd_fds_for(path + 3);
365 /* check for unix socket */
366 if (0 == strncmp(path, "unix:", 5))
368 fd = api_ws_socket_unix(path + 5, server);
371 fd = api_ws_socket_inet(path, server);
373 if (fd >= 0 && server) {
375 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
379 /* configure the socket */
381 fcntl(fd, F_SETFD, FD_CLOEXEC);
382 fcntl(fd, F_SETFL, O_NONBLOCK);
387 /******************* serialisation part **********************************/
394 #define WRITEBUF_COUNT_MAX 32
397 struct iovec iovec[WRITEBUF_COUNT_MAX];
398 uint32_t uints[WRITEBUF_COUNT_MAX];
402 static char *api_ws_read_get(struct readbuf *rb, uint32_t length)
404 char *before = rb->head;
405 char *after = before + length;
412 static int api_ws_read_char(struct readbuf *rb, char *value)
414 if (rb->head >= rb->end)
416 *value = *rb->head++;
420 static int api_ws_read_uint32(struct readbuf *rb, uint32_t *value)
422 char *after = rb->head + sizeof *value;
425 memcpy(value, rb->head, sizeof *value);
427 *value = le32toh(*value);
431 static int api_ws_read_string(struct readbuf *rb, const char **value, size_t *length)
434 if (!api_ws_read_uint32(rb, &len) || !len)
437 *length = (size_t)(len - 1);
438 return (*value = api_ws_read_get(rb, len)) != NULL && rb->head[-1] == 0;
441 static int api_ws_read_object(struct readbuf *rb, struct json_object **object)
444 struct json_object *o;
445 int rc = api_ws_read_string(rb, &string, NULL);
447 o = json_tokener_parse(string);
448 if (o == NULL && strcmp(string, "null"))
449 o = json_object_new_string(string);
455 static int api_ws_write_put(struct writebuf *wb, const void *value, size_t length)
458 if (i == WRITEBUF_COUNT_MAX)
460 wb->iovec[i].iov_base = (void*)value;
461 wb->iovec[i].iov_len = length;
466 static int api_ws_write_char(struct writebuf *wb, char value)
469 if (i == WRITEBUF_COUNT_MAX)
471 *(char*)&wb->uints[i] = value;
472 wb->iovec[i].iov_base = &wb->uints[i];
473 wb->iovec[i].iov_len = 1;
478 static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
481 if (i == WRITEBUF_COUNT_MAX)
483 wb->uints[i] = htole32(value);
484 wb->iovec[i].iov_base = &wb->uints[i];
485 wb->iovec[i].iov_len = sizeof wb->uints[i];
490 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
492 uint32_t len = (uint32_t)++length;
493 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
496 static int api_ws_write_string(struct writebuf *wb, const char *value)
498 return api_ws_write_string_length(wb, value, strlen(value));
501 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
503 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
504 return string != NULL && api_ws_write_string(wb, string);
507 /******************* client part **********************************/
510 * structure for recording query data
513 struct api_ws_memo *next; /* the next memo */
514 struct api_ws *api; /* the ws api */
515 struct afb_xreq *xreq; /* the request handle */
516 uint32_t msgid; /* the message identifier */
521 struct api_ws_event *next;
522 struct afb_event event;
527 /* search a memorized request */
528 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
530 struct api_ws_memo *memo;
532 memo = api->client.memos;
533 while (memo != NULL && memo->msgid != msgid)
539 /* search the event */
540 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
542 struct api_ws_event *ev;
544 ev = api->client.events;
545 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
552 /* allocates and init the memorizing data */
553 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_xreq *xreq)
555 struct api_ws_memo *memo;
557 memo = malloc(sizeof *memo);
559 afb_xreq_addref(xreq);
561 memo->msgid = ptr2id(memo);
562 while(api_ws_client_memo_search(api, memo->msgid) != NULL)
565 memo->next = api->client.memos;
566 api->client.memos = memo;
571 /* free and release the memorizing data */
572 static void api_ws_client_memo_destroy(struct api_ws_memo *memo)
574 struct api_ws_memo **prv;
576 prv = &memo->api->client.memos;
577 while (*prv != NULL) {
585 afb_xreq_unref(memo->xreq);
589 /* get event data from the message */
590 static int api_ws_client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
592 return api_ws_read_uint32(rb, eventid) && api_ws_read_string(rb, name, NULL);
595 /* get event from the message */
596 static int api_ws_client_msg_event_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev)
601 /* get event data from the message */
602 if (!api_ws_client_msg_event_read(rb, &eventid, &name)) {
603 ERROR("Invalid message");
607 /* check conflicts */
608 *ev = api_ws_client_event_search(api, eventid, name);
610 ERROR("event %s not found", name);
617 /* get event from the message */
618 static int api_ws_client_msg_memo_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo)
622 /* get event data from the message */
623 if (!api_ws_read_uint32(rb, &msgid)) {
624 ERROR("Invalid message");
629 *memo = api_ws_client_memo_search(api, msgid);
631 ERROR("message not found");
638 /* read a subscrition message */
639 static int api_ws_client_msg_subscription_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo, struct api_ws_event **ev)
641 return api_ws_client_msg_memo_get(api, rb, memo) && api_ws_client_msg_event_get(api, rb, ev);
645 static void api_ws_client_event_create(struct api_ws *api, struct readbuf *rb)
650 struct api_ws_event *ev;
652 /* get event data from the message */
653 offset = api_ws_client_msg_event_read(rb, &eventid, &name);
655 ERROR("Invalid message");
659 /* check conflicts */
660 ev = api_ws_client_event_search(api, eventid, name);
666 /* no conflict, try to add it */
667 ev = malloc(sizeof *ev);
669 ev->event = afb_evt_create_event(name);
670 if (ev->event.closure == NULL)
674 ev->eventid = eventid;
675 ev->next = api->client.events;
676 api->client.events = ev;
680 ERROR("can't create event %s, out of memory", name);
683 /* removes an event */
684 static void api_ws_client_event_drop(struct api_ws *api, struct readbuf *rb)
686 struct api_ws_event *ev, **prv;
688 /* retrieves the event */
689 if (!api_ws_client_msg_event_get(api, rb, &ev))
692 /* decrease the reference count */
696 /* unlinks the event */
697 prv = &api->client.events;
702 /* destroys the event */
703 afb_event_drop(ev->event);
707 /* subscribes an event */
708 static void api_ws_client_event_subscribe(struct api_ws *api, struct readbuf *rb)
710 struct api_ws_event *ev;
711 struct api_ws_memo *memo;
713 if (api_ws_client_msg_subscription_get(api, rb, &memo, &ev)) {
714 /* subscribe the request from the event */
715 if (afb_xreq_subscribe(memo->xreq, ev->event) < 0)
716 ERROR("can't subscribe: %m");
720 /* unsubscribes an event */
721 static void api_ws_client_event_unsubscribe(struct api_ws *api, struct readbuf *rb)
723 struct api_ws_event *ev;
724 struct api_ws_memo *memo;
726 if (api_ws_client_msg_subscription_get(api, rb, &memo, &ev)) {
727 /* unsubscribe the request from the event */
728 if (afb_xreq_unsubscribe(memo->xreq, ev->event) < 0)
729 ERROR("can't unsubscribe: %m");
733 /* receives broadcasted events */
734 static void api_ws_client_event_broadcast(struct api_ws *api, struct readbuf *rb)
736 struct json_object *object;
739 if (api_ws_read_string(rb, &event, NULL) && api_ws_read_object(rb, &object))
740 afb_evt_broadcast(event, object);
742 ERROR("unreadable broadcasted event");
746 static void api_ws_client_event_push(struct api_ws *api, struct readbuf *rb)
748 struct api_ws_event *ev;
749 struct json_object *object;
751 if (api_ws_client_msg_event_get(api, rb, &ev) && api_ws_read_object(rb, &object))
752 afb_event_push(ev->event, object);
754 ERROR("unreadable push event");
757 static void api_ws_client_reply_success(struct api_ws *api, struct readbuf *rb)
759 struct api_ws_memo *memo;
760 struct json_object *object;
764 /* retrieve the message data */
765 if (!api_ws_client_msg_memo_get(api, rb, &memo))
768 if (api_ws_read_uint32(rb, &flags)
769 && api_ws_read_string(rb, &info, NULL)
770 && api_ws_read_object(rb, &object)) {
771 memo->xreq->context.flags = (unsigned)flags;
772 afb_xreq_success(memo->xreq, object, *info ? info : NULL);
774 /* failing to have the answer */
775 afb_xreq_fail(memo->xreq, "error", "ws error");
777 api_ws_client_memo_destroy(memo);
780 static void api_ws_client_reply_fail(struct api_ws *api, struct readbuf *rb)
782 struct api_ws_memo *memo;
783 const char *info, *status;
786 /* retrieve the message data */
787 if (!api_ws_client_msg_memo_get(api, rb, &memo))
790 if (api_ws_read_uint32(rb, &flags)
791 && api_ws_read_string(rb, &status, NULL)
792 && api_ws_read_string(rb, &info, NULL)) {
793 memo->xreq->context.flags = (unsigned)flags;
794 afb_xreq_fail(memo->xreq, status, *info ? info : NULL);
796 /* failing to have the answer */
797 afb_xreq_fail(memo->xreq, "error", "ws error");
799 api_ws_client_memo_destroy(memo);
802 /* send a subcall reply */
803 static void api_ws_client_send_subcall_reply(struct api_ws_reply *reply, int iserror, json_object *object)
806 struct writebuf wb = { .count = 0 };
807 char ie = (char)!!iserror;
809 if (!api_ws_write_char(&wb, CHAR_FOR_SUBCALL_REPLY)
810 || !api_ws_write_uint32(&wb, reply->subcallid)
811 || !api_ws_write_char(&wb, ie)
812 || !api_ws_write_object(&wb, object)) {
817 rc = afb_ws_binary_v(reply->apiws->client.ws, wb.iovec, wb.count);
820 ERROR("error while sending subcall reply");
823 /* callback for subcall reply */
824 static void api_ws_client_subcall_reply_cb(void *closure, int iserror, json_object *object)
826 api_ws_client_send_subcall_reply(closure, iserror, object);
830 /* received a subcall request */
831 static void api_ws_client_subcall(struct api_ws *apiws, struct readbuf *rb)
833 struct api_ws_reply *reply;
834 struct api_ws_memo *memo;
835 const char *api, *verb;
837 struct json_object *object;
839 reply = malloc(sizeof *reply);
843 /* retrieve the message data */
844 if (!api_ws_client_msg_memo_get(apiws, rb, &memo))
847 if (api_ws_read_uint32(rb, &subcallid)
848 && api_ws_read_string(rb, &api, NULL)
849 && api_ws_read_string(rb, &verb, NULL)
850 && api_ws_read_object(rb, &object)) {
851 reply->apiws = apiws;
852 reply->subcallid = subcallid;
853 afb_xreq_subcall(memo->xreq, api, verb, object, api_ws_client_subcall_reply_cb, reply);
857 /* callback when receiving binary data */
858 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
861 struct api_ws *apiws = closure;
862 struct readbuf rb = { .head = data, .end = data + size };
864 pthread_mutex_lock(&apiws->mutex);
865 switch (*rb.head++) {
866 case CHAR_FOR_ANSWER_SUCCESS: /* success */
867 api_ws_client_reply_success(apiws, &rb);
869 case CHAR_FOR_ANSWER_FAIL: /* fail */
870 api_ws_client_reply_fail(apiws, &rb);
872 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
873 api_ws_client_event_broadcast(apiws, &rb);
875 case CHAR_FOR_EVT_ADD: /* creates the event */
876 api_ws_client_event_create(apiws, &rb);
878 case CHAR_FOR_EVT_DEL: /* drops the event */
879 api_ws_client_event_drop(apiws, &rb);
881 case CHAR_FOR_EVT_PUSH: /* pushs the event */
882 api_ws_client_event_push(apiws, &rb);
884 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
885 api_ws_client_event_subscribe(apiws, &rb);
887 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
888 api_ws_client_event_unsubscribe(apiws, &rb);
890 case CHAR_FOR_SUBCALL_CALL: /* subcall */
891 api_ws_client_subcall(apiws, &rb);
893 default: /* unexpected message */
894 /* TODO: close the connection */
897 pthread_mutex_unlock(&apiws->mutex);
902 /* on call, propagate it to the ws service */
903 static void api_ws_client_call_cb(void * closure, struct afb_xreq *xreq)
906 struct api_ws_memo *memo;
907 struct writebuf wb = { .count = 0 };
910 struct api_ws *apiws = closure;
912 pthread_mutex_lock(&apiws->mutex);
914 /* create the recording data */
915 memo = api_ws_client_memo_make(apiws, xreq);
917 afb_xreq_fail_f(xreq, "error", "out of memory");
921 /* creates the call message */
922 raw = afb_xreq_raw(xreq, &szraw);
925 if (!api_ws_write_char(&wb, CHAR_FOR_CALL)
926 || !api_ws_write_uint32(&wb, memo->msgid)
927 || !api_ws_write_uint32(&wb, (uint32_t)xreq->context.flags)
928 || !api_ws_write_string(&wb, xreq->verb)
929 || !api_ws_write_string(&wb, afb_session_uuid(xreq->context.session))
930 || !api_ws_write_string_length(&wb, raw, szraw))
934 rc = afb_ws_binary_v(apiws->client.ws, wb.iovec, wb.count);
938 afb_xreq_fail(xreq, "error", "websocket sending error");
942 afb_xreq_fail(xreq, "error", "internal: raw is NULL!");
946 afb_xreq_fail(xreq, "error", "overflow: size doesn't match 32 bits!");
949 api_ws_client_memo_destroy(memo);
951 pthread_mutex_unlock(&apiws->mutex);
955 static void api_ws_client_disconnect(struct api_ws *api)
958 afb_ws_destroy(api->client.ws);
959 api->client.ws = NULL;
966 static int api_ws_client_connect(struct api_ws *api)
971 fd = api_ws_socket(api->path, 0);
973 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
984 static struct afb_api_itf ws_api_itf = {
985 .call = api_ws_client_call_cb
988 /* adds a afb-ws-service client api */
989 int afb_api_ws_add_client(const char *path, struct afb_apiset *apiset)
993 struct afb_api afb_api;
995 /* create the ws client api */
996 api = api_ws_make(path);
1000 /* connect to the service */
1001 rc = api_ws_client_connect(api);
1003 ERROR("can't connect to ws service %s", api->path);
1007 /* record it as an API */
1008 afb_api.closure = api;
1009 afb_api.itf = &ws_api_itf;
1010 if (afb_apiset_add(apiset, api->api, afb_api) < 0)
1016 api_ws_client_disconnect(api);
1023 /******************* client description part for server *****************************/
1025 static void api_ws_server_client_unref(struct api_ws_client *client)
1027 struct api_ws_subcall *sc, *nsc;
1029 if (!__atomic_sub_fetch(&client->refcount, 1, __ATOMIC_RELAXED)) {
1030 afb_evt_listener_unref(client->listener);
1031 afb_ws_destroy(client->ws);
1032 nsc = client->subcalls;
1036 sc->callback(sc->closure, 1, NULL);
1039 afb_cred_unref(client->cred);
1040 afb_apiset_unref(client->apiset);
1045 static void api_ws_server_client_addref(struct api_ws_client *client)
1047 __atomic_add_fetch(&client->refcount, 1, __ATOMIC_RELAXED);
1050 /* on call, propagate it to the ws service */
1051 static void api_ws_server_on_call(struct api_ws_client *client, struct readbuf *rb)
1053 struct api_ws_server_req *wreq;
1055 const char *uuid, *verb;
1056 uint32_t flags, msgid;
1058 struct json_object *object;
1060 api_ws_server_client_addref(client);
1062 /* reads the call message data */
1063 if (!api_ws_read_uint32(rb, &msgid)
1064 || !api_ws_read_uint32(rb, &flags)
1065 || !api_ws_read_string(rb, &verb, &lenverb)
1066 || !api_ws_read_string(rb, &uuid, NULL)
1067 || !api_ws_read_object(rb, &object))
1070 /* create the request */
1071 wreq = malloc(++lenverb + sizeof *wreq);
1075 afb_xreq_init(&wreq->xreq, &afb_api_ws_xreq_itf);
1076 wreq->client = client;
1077 wreq->msgid = msgid;
1078 cverb = (char*)&wreq[1];
1079 memcpy(cverb, verb, lenverb);
1081 /* init the context */
1082 if (afb_context_connect(&wreq->xreq.context, uuid, NULL) < 0)
1084 wreq->xreq.context.flags = flags;
1086 /* makes the call */
1087 wreq->xreq.cred = afb_cred_addref(client->cred);
1088 wreq->xreq.api = client->api;
1089 wreq->xreq.verb = cverb;
1090 wreq->xreq.json = object;
1091 afb_xreq_process(&wreq->xreq, client->apiset);
1097 json_object_put(object);
1099 api_ws_server_client_unref(client);
1102 /* on subcall reply */
1103 static void api_ws_server_on_subcall_reply(struct api_ws_client *client, struct readbuf *rb)
1107 struct json_object *object;
1108 struct api_ws_subcall *sc, **psc;
1110 /* reads the call message data */
1111 if (!api_ws_read_uint32(rb, &subcallid)
1112 || !api_ws_read_char(rb, &iserror)
1113 || !api_ws_read_object(rb, &object)) {
1114 /* TODO bad protocol */
1118 /* search the subcall and unlink it */
1119 pthread_mutex_lock(&client->mutex);
1120 psc = &client->subcalls;
1121 while ((sc = *psc) && sc->subcallid != subcallid)
1124 pthread_mutex_unlock(&client->mutex);
1125 /* TODO subcall not found */
1128 pthread_mutex_unlock(&client->mutex);
1129 sc->callback(sc->closure, (int)iserror, object);
1132 json_object_put(object);
1135 /* callback when receiving binary data */
1136 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
1139 struct readbuf rb = { .head = data, .end = data + size };
1140 switch (*rb.head++) {
1142 api_ws_server_on_call(closure, &rb);
1144 case CHAR_FOR_SUBCALL_REPLY:
1145 api_ws_server_on_subcall_reply(closure, &rb);
1147 default: /* unexpected message */
1148 /* TODO: close the connection */
1155 /* callback when receiving a hangup */
1156 static void api_ws_server_on_hangup(void *closure)
1158 struct api_ws_client *client = closure;
1160 /* close the socket */
1161 if (client->fd >= 0) {
1166 /* release the client */
1167 api_ws_server_client_unref(client);
1170 static void api_ws_server_accept(struct api_ws *api)
1172 struct api_ws_client *client;
1173 struct sockaddr addr;
1176 client = calloc(1, sizeof *client);
1177 if (client != NULL) {
1178 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
1179 if (client->listener != NULL) {
1180 lenaddr = (socklen_t)sizeof addr;
1181 client->fd = accept(api->fd, &addr, &lenaddr);
1182 if (client->fd >= 0) {
1183 client->cred = afb_cred_create_for_socket(client->fd);
1184 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
1185 fcntl(client->fd, F_SETFL, O_NONBLOCK);
1186 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1187 if (client->ws != NULL) {
1188 client->api = api->api;
1189 client->apiset = afb_apiset_addref(api->server.apiset);
1190 client->refcount = 1;
1191 client->subcalls = NULL;
1194 afb_cred_unref(client->cred);
1197 afb_evt_listener_unref(client->listener);
1203 /******************* server part: manage events **********************************/
1205 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1208 struct writebuf wb = { .count = 0 };
1210 if (api_ws_write_char(&wb, order)
1211 && api_ws_write_uint32(&wb, eventid)
1212 && api_ws_write_string(&wb, event)
1213 && (data == NULL || api_ws_write_string(&wb, data))) {
1214 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1218 ERROR("error while sending %c for event %s", order, event);
1221 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1223 api_ws_server_event_send(closure, CHAR_FOR_EVT_ADD, event, eventid, NULL);
1226 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1228 api_ws_server_event_send(closure, CHAR_FOR_EVT_DEL, event, eventid, NULL);
1231 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1233 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1234 api_ws_server_event_send(closure, CHAR_FOR_EVT_PUSH, event, eventid, data ? : "null");
1235 json_object_put(object);
1238 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1241 struct api_ws_client *client = closure;
1243 struct writebuf wb = { .count = 0 };
1245 if (api_ws_write_char(&wb, CHAR_FOR_EVT_BROADCAST) && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1246 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1248 ERROR("error while broadcasting event %s", event);
1250 ERROR("error while broadcasting event %s", event);
1251 json_object_put(object);
1254 /******************* ws request part for server *****************/
1256 /* decrement the reference count of the request and free/release it on falling to null */
1257 static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq)
1259 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1261 afb_context_disconnect(&wreq->xreq.context);
1262 afb_cred_unref(wreq->xreq.cred);
1263 json_object_put(wreq->xreq.json);
1264 api_ws_server_client_unref(wreq->client);
1268 static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
1271 struct writebuf wb = { .count = 0 };
1272 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1274 if (api_ws_write_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
1275 && api_ws_write_uint32(&wb, wreq->msgid)
1276 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1277 && api_ws_write_string(&wb, info ? : "")
1278 && api_ws_write_object(&wb, obj)) {
1279 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1283 ERROR("error while sending success");
1285 json_object_put(obj);
1288 static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
1291 struct writebuf wb = { .count = 0 };
1292 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1294 if (api_ws_write_char(&wb, CHAR_FOR_ANSWER_FAIL)
1295 && api_ws_write_uint32(&wb, wreq->msgid)
1296 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1297 && api_ws_write_string(&wb, status)
1298 && api_ws_write_string(&wb, info ? : "")) {
1299 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1303 ERROR("error while sending fail");
1306 static void api_ws_server_req_subcall_cb(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
1309 struct writebuf wb = { .count = 0 };
1310 struct api_ws_subcall *sc, *osc;
1311 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1312 struct api_ws_client *client = wreq->client;
1314 sc = malloc(sizeof *sc);
1318 sc->callback = callback;
1319 sc->closure = cb_closure;
1321 pthread_mutex_unlock(&client->mutex);
1322 sc->subcallid = ptr2id(sc);
1325 osc = client->subcalls;
1326 while(osc && osc->subcallid != sc->subcallid)
1329 sc->next = client->subcalls;
1330 client->subcalls = sc;
1331 pthread_mutex_unlock(&client->mutex);
1333 if (api_ws_write_char(&wb, CHAR_FOR_SUBCALL_CALL)
1334 && api_ws_write_uint32(&wb, wreq->msgid)
1335 && api_ws_write_uint32(&wb, sc->subcallid)
1336 && api_ws_write_string(&wb, api)
1337 && api_ws_write_string(&wb, verb)
1338 && api_ws_write_object(&wb, args)) {
1339 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1343 ERROR("error while sending fail");
1347 static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
1350 struct writebuf wb = { .count = 0 };
1351 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1353 rc = afb_evt_add_watch(wreq->client->listener, event);
1357 if (api_ws_write_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
1358 && api_ws_write_uint32(&wb, wreq->msgid)
1359 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1360 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1361 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1365 ERROR("error while subscribing event");
1370 static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
1373 struct writebuf wb = { .count = 0 };
1374 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1376 if (api_ws_write_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
1377 && api_ws_write_uint32(&wb, wreq->msgid)
1378 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1379 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1380 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1384 ERROR("error while subscribing event");
1386 rc = afb_evt_remove_watch(wreq->client->listener, event);
1390 /******************* server part **********************************/
1392 static int api_ws_server_connect(struct api_ws *api);
1394 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1396 if ((revents & EPOLLIN) != 0)
1397 api_ws_server_accept(closure);
1398 if ((revents & EPOLLHUP) != 0)
1399 api_ws_server_connect(closure);
1403 static void api_ws_server_disconnect(struct api_ws *api)
1405 if (api->server.listensrc != NULL) {
1406 sd_event_source_unref(api->server.listensrc);
1407 api->server.listensrc = NULL;
1415 static int api_ws_server_connect(struct api_ws *api)
1419 /* ensure disconnected */
1420 api_ws_server_disconnect(api);
1422 /* request the service object name */
1423 api->fd = api_ws_socket(api->path, 1);
1425 ERROR("can't create socket %s", api->path);
1427 /* listen for service */
1428 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1433 ERROR("can't add ws object %s", api->path);
1438 /* create the service */
1439 int afb_api_ws_add_server(const char *path, struct afb_apiset *apiset)
1444 /* creates the ws api object */
1445 api = api_ws_make(path);
1449 /* connect for serving */
1450 rc = api_ws_server_connect(api);
1454 api->server.apiset = afb_apiset_addref(apiset);