2 * Copyright (C) 2015-2020 "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>
36 #include "afb-msg-json.h"
37 #include "afb-proto-ws.h"
43 /******** implementation of internal binder protocol per api **************/
46 This protocol is asymmetric: there is a client and a server
48 The client can require the following actions:
54 The server must reply to the previous actions by
56 - answering success or failure of the call
58 - answering the required description
60 The server can also within the context of a call
62 - subscribe or unsubscribe an event
64 For the purpose of handling events the server can:
66 - create/destroy an event
68 - push or broadcast data as an event
70 - signal unexpected event
73 /************** constants for protocol definition *************************/
75 #define CHAR_FOR_CALL 'K' /* client -> server */
76 #define CHAR_FOR_REPLY 'k' /* server -> client */
77 #define CHAR_FOR_EVT_BROADCAST 'B' /* server -> client */
78 #define CHAR_FOR_EVT_ADD 'E' /* server -> client */
79 #define CHAR_FOR_EVT_DEL 'e' /* server -> client */
80 #define CHAR_FOR_EVT_PUSH 'P' /* server -> client */
81 #define CHAR_FOR_EVT_SUBSCRIBE 'X' /* server -> client */
82 #define CHAR_FOR_EVT_UNSUBSCRIBE 'x' /* server -> client */
83 #define CHAR_FOR_EVT_UNEXPECTED 'U' /* client -> server */
84 #define CHAR_FOR_DESCRIBE 'D' /* client -> server */
85 #define CHAR_FOR_DESCRIPTION 'd' /* server -> client */
86 #define CHAR_FOR_TOKEN_ADD 'T' /* client -> server */
87 #define CHAR_FOR_TOKEN_DROP 't' /* client -> server */
88 #define CHAR_FOR_SESSION_ADD 'S' /* client -> server */
89 #define CHAR_FOR_SESSION_DROP 's' /* client -> server */
90 #define CHAR_FOR_VERSION_OFFER 'V' /* client -> server */
91 #define CHAR_FOR_VERSION_SET 'v' /* server -> client */
93 /******************* manage versions *****************************/
95 #define WSAPI_IDENTIFIER 02723012011 /* wsapi: 23.19.1.16.9 */
97 #define WSAPI_VERSION_UNSET 0
98 #define WSAPI_VERSION_1 1
100 #define WSAPI_VERSION_MIN WSAPI_VERSION_1
101 #define WSAPI_VERSION_MAX WSAPI_VERSION_1
103 /******************* maximum count of ids ***********************/
105 #define ACTIVE_ID_MAX 4095
107 /******************* handling calls *****************************/
110 * structure for recording calls on client side
113 struct client_call *next; /* the next call */
114 void *request; /* the request closure */
115 uint16_t callid; /* the message identifier */
119 * structure for a ws request
121 struct afb_proto_ws_call {
122 struct afb_proto_ws *protows; /* the client of the request */
123 char *buffer; /* the incoming buffer */
124 uint16_t refcount; /* reference count */
125 uint16_t callid; /* the incoming request callid */
129 * structure for recording describe requests
131 struct client_describe
133 struct client_describe *next;
134 void (*callback)(void*, struct json_object*);
140 * structure for jobs of describing
142 struct afb_proto_ws_describe
144 struct afb_proto_ws *protows;
148 /******************* proto description for client or servers ******************/
152 /* count of references */
158 /* count actives ids */
164 /* resource control */
165 pthread_mutex_t mutex;
170 /* the client closure */
173 /* the client side interface */
174 const struct afb_proto_ws_client_itf *client_itf;
176 /* the server side interface */
177 const struct afb_proto_ws_server_itf *server_itf;
179 /* emitted calls (client side) */
180 struct client_call *calls;
182 /* pending description (client side) */
183 struct client_describe *describes;
185 /* on hangup callback */
186 void (*on_hangup)(void *closure);
188 /* queuing facility for processing messages */
189 int (*queuing)(struct afb_proto_ws *proto, void (*process)(int s, void *c), void *closure);
192 /******************* streaming objects **********************************/
194 #define WRITEBUF_COUNT_MAX 32
195 #define WRITEBUF_BUFSZ (WRITEBUF_COUNT_MAX * sizeof(uint32_t))
199 int iovcount, bufcount;
200 struct iovec iovec[WRITEBUF_COUNT_MAX];
201 char buf[WRITEBUF_BUFSZ];
206 char *base, *head, *end;
211 struct afb_proto_ws *protows;
215 /******************* serialization part **********************************/
217 static char *readbuf_get(struct readbuf *rb, uint32_t length)
219 char *before = rb->head;
220 char *after = before + length;
227 static int readbuf_getat(struct readbuf *rb, void *to, uint32_t length)
229 char *head = readbuf_get(rb, length);
232 memcpy(to, head, length);
236 __attribute__((unused))
237 static int readbuf_char(struct readbuf *rb, char *value)
239 return readbuf_getat(rb, value, sizeof *value);
242 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
244 int r = readbuf_getat(rb, value, sizeof *value);
246 *value = le32toh(*value);
250 static int readbuf_uint16(struct readbuf *rb, uint16_t *value)
252 int r = readbuf_getat(rb, value, sizeof *value);
254 *value = le16toh(*value);
258 static int readbuf_uint8(struct readbuf *rb, uint8_t *value)
260 return readbuf_getat(rb, value, sizeof *value);
263 static int _readbuf_string_(struct readbuf *rb, const char **value, size_t *length, int nulok)
266 if (!readbuf_uint32(rb, &len))
277 *length = (size_t)(len - 1);
278 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
282 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
284 return _readbuf_string_(rb, value, length, 0);
287 static int readbuf_nullstring(struct readbuf *rb, const char **value, size_t *length)
289 return _readbuf_string_(rb, value, length, 1);
292 static int readbuf_object(struct readbuf *rb, struct json_object **object)
295 struct json_object *o;
296 enum json_tokener_error jerr;
297 int rc = readbuf_string(rb, &string, NULL);
299 o = json_tokener_parse_verbose(string, &jerr);
300 if (jerr != json_tokener_success)
301 o = json_object_new_string(string);
307 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
309 int i = wb->iovcount;
310 if (i == WRITEBUF_COUNT_MAX)
312 wb->iovec[i].iov_base = (void*)value;
313 wb->iovec[i].iov_len = length;
314 wb->iovcount = i + 1;
318 static int writebuf_putbuf(struct writebuf *wb, const void *value, int length)
321 int i = wb->iovcount, n = wb->bufcount, nafter;
323 /* check enough length */
325 if (nafter > WRITEBUF_BUFSZ)
328 /* get where to store */
330 if (i && p == (((char*)wb->iovec[i - 1].iov_base) + wb->iovec[i - 1].iov_len))
331 /* increase previous iovec */
332 wb->iovec[i - 1].iov_len += (size_t)length;
333 else if (i == WRITEBUF_COUNT_MAX)
338 wb->iovec[i].iov_base = p;
339 wb->iovec[i].iov_len = (size_t)length;
340 wb->iovcount = i + 1;
343 memcpy(p, value, (size_t)length);
344 wb->bufcount = nafter;
348 __attribute__((unused))
349 static int writebuf_char(struct writebuf *wb, char value)
351 return writebuf_putbuf(wb, &value, 1);
354 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
356 value = htole32(value);
357 return writebuf_putbuf(wb, &value, (int)sizeof value);
360 static int writebuf_uint16(struct writebuf *wb, uint16_t value)
362 value = htole16(value);
363 return writebuf_putbuf(wb, &value, (int)sizeof value);
366 static int writebuf_uint8(struct writebuf *wb, uint8_t value)
368 return writebuf_putbuf(wb, &value, (int)sizeof value);
371 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
373 uint32_t len = (uint32_t)++length;
374 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
377 static int writebuf_string(struct writebuf *wb, const char *value)
379 return writebuf_string_length(wb, value, strlen(value));
382 static int writebuf_nullstring(struct writebuf *wb, const char *value)
384 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
387 static int writebuf_object(struct writebuf *wb, struct json_object *object)
389 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
390 return string != NULL && writebuf_string(wb, string);
393 /******************* queuing of messages *****************/
395 /* queue the processing of the received message (except if size=0 cause it's not a valid message) */
396 static void queue_message_processing(struct afb_proto_ws *protows, char *data, size_t size, void (*processing)(int,void*))
398 struct binary *binary;
401 binary = malloc(sizeof *binary);
403 /* TODO process the problem */
406 binary->protows = protows;
407 binary->rb.base = data;
408 binary->rb.head = data;
409 binary->rb.end = data + size;
410 if (!protows->queuing
411 || protows->queuing(protows, processing, binary) < 0)
412 processing(0, binary);
419 /******************* sending messages *****************/
421 static int proto_write(struct afb_proto_ws *protows, struct writebuf *wb)
426 pthread_mutex_lock(&protows->mutex);
432 rc = afb_ws_binary_v(ws, wb->iovec, wb->iovcount);
436 pthread_mutex_unlock(&protows->mutex);
440 static int send_version_offer_1(struct afb_proto_ws *protows, uint8_t version)
443 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
445 if (writebuf_char(&wb, CHAR_FOR_VERSION_OFFER)
446 && writebuf_uint32(&wb, WSAPI_IDENTIFIER)
447 && writebuf_uint8(&wb, 1) /* offer one version */
448 && writebuf_uint8(&wb, version))
449 rc = proto_write(protows, &wb);
453 static int send_version_set(struct afb_proto_ws *protows, uint8_t version)
456 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
458 if (writebuf_char(&wb, CHAR_FOR_VERSION_SET)
459 && writebuf_uint8(&wb, version))
460 rc = proto_write(protows, &wb);
464 /******************* ws request part for server *****************/
466 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
468 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
471 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
473 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
476 afb_proto_ws_unref(call->protows);
481 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
484 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
485 struct afb_proto_ws *protows = call->protows;
487 if (writebuf_char(&wb, CHAR_FOR_REPLY)
488 && writebuf_uint16(&wb, call->callid)
489 && writebuf_nullstring(&wb, error)
490 && writebuf_nullstring(&wb, info)
491 && writebuf_object(&wb, obj))
492 rc = proto_write(protows, &wb);
496 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, uint16_t event_id)
499 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
500 struct afb_proto_ws *protows = call->protows;
502 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
503 && writebuf_uint16(&wb, call->callid)
504 && writebuf_uint16(&wb, event_id))
505 rc = proto_write(protows, &wb);
509 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, uint16_t event_id)
512 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
513 struct afb_proto_ws *protows = call->protows;
515 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
516 && writebuf_uint16(&wb, call->callid)
517 && writebuf_uint16(&wb, event_id))
518 rc = proto_write(protows, &wb);
522 /******************* client part **********************************/
524 /* search a memorized call */
525 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint16_t callid)
527 struct client_call *call;
529 call = protows->calls;
530 while (call != NULL && call->callid != callid)
536 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint16_t callid)
538 struct client_call *result;
540 pthread_mutex_lock(&protows->mutex);
541 result = client_call_search_locked(protows, callid);
542 pthread_mutex_unlock(&protows->mutex);
546 /* free and release the memorizing call */
547 static void client_call_destroy(struct afb_proto_ws *protows, struct client_call *call)
549 struct client_call **prv;
551 pthread_mutex_lock(&protows->mutex);
552 prv = &protows->calls;
553 while (*prv != NULL) {
557 pthread_mutex_unlock(&protows->mutex);
563 pthread_mutex_unlock(&protows->mutex);
566 /* get event from the message */
567 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
571 /* get event data from the message */
572 if (!readbuf_uint16(rb, &callid))
576 *call = client_call_search_unlocked(protows, callid);
577 return *call != NULL;
581 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
583 const char *event_name;
586 if (protows->client_itf->on_event_create
587 && readbuf_uint16(rb, &event_id)
588 && readbuf_string(rb, &event_name, NULL))
589 protows->client_itf->on_event_create(protows->closure, event_id, event_name);
591 ERROR("Ignoring creation of event");
594 /* removes an event */
595 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
599 if (protows->client_itf->on_event_remove && readbuf_uint16(rb, &event_id))
600 protows->client_itf->on_event_remove(protows->closure, event_id);
602 ERROR("Ignoring deletion of event");
605 /* subscribes an event */
606 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
609 struct client_call *call;
611 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && readbuf_uint16(rb, &event_id))
612 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_id);
614 ERROR("Ignoring subscription to event");
617 /* unsubscribes an event */
618 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
621 struct client_call *call;
623 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && readbuf_uint16(rb, &event_id))
624 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_id);
626 ERROR("Ignoring unsubscription to event");
629 /* receives broadcasted events */
630 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
632 const char *event_name, *uuid;
634 struct json_object *object;
636 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object) && (uuid = readbuf_get(rb, 16)) && readbuf_uint8(rb, &hop))
637 protows->client_itf->on_event_broadcast(protows->closure, event_name, object, (unsigned char*)uuid, hop);
639 ERROR("Ignoring broadcast of event");
643 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
646 struct json_object *object;
648 if (protows->client_itf->on_event_push && readbuf_uint16(rb, &event_id) && readbuf_object(rb, &object))
649 protows->client_itf->on_event_push(protows->closure, event_id, object);
651 ERROR("Ignoring push of event");
654 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
656 struct client_call *call;
657 struct json_object *object;
658 const char *error, *info;
660 if (!client_msg_call_get(protows, rb, &call))
663 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
664 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
666 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
668 client_call_destroy(protows, call);
671 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
674 struct client_describe *desc, **prv;
675 struct json_object *object;
677 if (readbuf_uint32(rb, &descid)) {
678 pthread_mutex_lock(&protows->mutex);
679 prv = &protows->describes;
680 while ((desc = *prv) && desc->descid != descid)
683 pthread_mutex_unlock(&protows->mutex);
687 pthread_mutex_unlock(&protows->mutex);
688 if (!readbuf_object(rb, &object))
690 desc->callback(desc->closure, object);
696 /* received a version set */
697 static void client_on_version_set(struct afb_proto_ws *protows, struct readbuf *rb)
701 /* reads the descid */
702 if (readbuf_uint8(rb, &version)
703 && WSAPI_VERSION_MIN <= version
704 && version <= WSAPI_VERSION_MAX) {
705 protows->version = version;
708 afb_proto_ws_hangup(protows);
712 /* callback when receiving binary data */
713 static void client_on_binary_job(int sig, void *closure)
715 struct binary *binary = closure;
718 switch (*binary->rb.head++) {
719 case CHAR_FOR_REPLY: /* reply */
720 client_on_reply(binary->protows, &binary->rb);
722 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
723 client_on_event_broadcast(binary->protows, &binary->rb);
725 case CHAR_FOR_EVT_ADD: /* creates the event */
726 client_on_event_create(binary->protows, &binary->rb);
728 case CHAR_FOR_EVT_DEL: /* removes the event */
729 client_on_event_remove(binary->protows, &binary->rb);
731 case CHAR_FOR_EVT_PUSH: /* pushs the event */
732 client_on_event_push(binary->protows, &binary->rb);
734 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
735 client_on_event_subscribe(binary->protows, &binary->rb);
737 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
738 client_on_event_unsubscribe(binary->protows, &binary->rb);
740 case CHAR_FOR_DESCRIPTION: /* description */
741 client_on_description(binary->protows, &binary->rb);
743 case CHAR_FOR_VERSION_SET: /* set the protocol version */
744 client_on_version_set(binary->protows, &binary->rb);
746 default: /* unexpected message */
747 /* TODO: close the connection */
751 free(binary->rb.base);
755 /* callback when receiving binary data */
756 static void client_on_binary(void *closure, char *data, size_t size)
758 struct afb_proto_ws *protows = closure;
760 queue_message_processing(protows, data, size, client_on_binary_job);
763 static int client_send_cmd_id16_optstr(struct afb_proto_ws *protows, char order, uint16_t id, const char *value)
765 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
768 if (writebuf_char(&wb, order)
769 && writebuf_uint16(&wb, id)
770 && (!value || writebuf_string(&wb, value)))
771 rc = proto_write(protows, &wb);
775 int afb_proto_ws_client_session_create(struct afb_proto_ws *protows, uint16_t sessionid, const char *sessionstr)
777 return client_send_cmd_id16_optstr(protows, CHAR_FOR_SESSION_ADD, sessionid, sessionstr);
780 int afb_proto_ws_client_session_remove(struct afb_proto_ws *protows, uint16_t sessionid)
782 return client_send_cmd_id16_optstr(protows, CHAR_FOR_SESSION_DROP, sessionid, NULL);
785 int afb_proto_ws_client_token_create(struct afb_proto_ws *protows, uint16_t tokenid, const char *tokenstr)
787 return client_send_cmd_id16_optstr(protows, CHAR_FOR_TOKEN_ADD, tokenid, tokenstr);
791 int afb_proto_ws_client_token_remove(struct afb_proto_ws *protows, uint16_t tokenid)
793 return client_send_cmd_id16_optstr(protows, CHAR_FOR_TOKEN_DROP, tokenid, NULL);
796 int afb_proto_ws_client_event_unexpected(struct afb_proto_ws *protows, uint16_t eventid)
798 return client_send_cmd_id16_optstr(protows, CHAR_FOR_EVT_UNEXPECTED, eventid, NULL);
801 int afb_proto_ws_client_call(
802 struct afb_proto_ws *protows,
804 struct json_object *args,
808 const char *user_creds
812 struct client_call *call;
813 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
816 /* allocate call data */
817 call = malloc(sizeof *call);
822 call->request = request;
825 pthread_mutex_lock(&protows->mutex);
826 if (protows->idcount >= ACTIVE_ID_MAX) {
827 pthread_mutex_unlock(&protows->mutex);
832 id = ++protows->genid;
833 while(!id || client_call_search_locked(protows, id) != NULL)
835 call->callid = protows->genid = id;
836 call->next = protows->calls;
837 protows->calls = call;
838 pthread_mutex_unlock(&protows->mutex);
840 /* creates the call message */
841 if (!writebuf_char(&wb, CHAR_FOR_CALL)
842 || !writebuf_uint16(&wb, call->callid)
843 || !writebuf_string(&wb, verb)
844 || !writebuf_uint16(&wb, sessionid)
845 || !writebuf_uint16(&wb, tokenid)
846 || !writebuf_object(&wb, args)
847 || !writebuf_nullstring(&wb, user_creds)) {
853 rc = proto_write(protows, &wb);
858 client_call_destroy(protows, call);
863 /* get the description */
864 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
866 struct client_describe *desc, *d;
867 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
870 desc = malloc(sizeof *desc);
876 /* fill in stack the description of the task */
877 pthread_mutex_lock(&protows->mutex);
878 if (protows->idcount >= ACTIVE_ID_MAX) {
883 id = ++protows->genid;
884 d = protows->describes;
886 if (id && d->descid != id)
890 d = protows->describes;
893 desc->descid = protows->genid = id;
894 desc->callback = callback;
895 desc->closure = closure;
896 desc->next = protows->describes;
897 protows->describes = desc;
898 pthread_mutex_unlock(&protows->mutex);
901 if (!writebuf_char(&wb, CHAR_FOR_DESCRIBE)
902 || !writebuf_uint16(&wb, desc->descid)) {
907 if (proto_write(protows, &wb) == 0)
911 pthread_mutex_lock(&protows->mutex);
912 d = protows->describes;
914 protows->describes = desc->next;
916 while(d && d->next != desc)
919 d->next = desc->next;
923 pthread_mutex_unlock(&protows->mutex);
926 /* TODO? callback(closure, NULL); */
930 /******************* client description part for server *****************************/
932 /* on call, propagate it to the ws service */
933 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
935 struct afb_proto_ws_call *call;
936 const char *verb, *user_creds;
937 uint16_t callid, sessionid, tokenid;
939 struct json_object *object;
941 afb_proto_ws_addref(protows);
943 /* reads the call message data */
944 if (!readbuf_uint16(rb, &callid)
945 || !readbuf_string(rb, &verb, &lenverb)
946 || !readbuf_uint16(rb, &sessionid)
947 || !readbuf_uint16(rb, &tokenid)
948 || !readbuf_object(rb, &object)
949 || !readbuf_nullstring(rb, &user_creds, NULL))
952 /* create the request */
953 call = malloc(sizeof *call);
957 call->protows = protows;
958 call->callid = callid;
960 call->buffer = rb->base;
961 rb->base = NULL; /* don't free the buffer */
963 protows->server_itf->on_call(protows->closure, call, verb, object, sessionid, tokenid, user_creds);
967 json_object_put(object);
970 afb_proto_ws_unref(protows);
973 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
976 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
978 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
979 && writebuf_uint32(&wb, descid)
980 && writebuf_object(&wb, descobj))
981 rc = proto_write(protows, &wb);
985 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
987 int rc = server_send_description(describe->protows, describe->descid, description);
988 afb_proto_ws_unref(describe->protows);
993 /* on describe, propagate it to the ws service */
994 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
997 struct afb_proto_ws_describe *desc;
999 /* reads the descid */
1000 if (readbuf_uint16(rb, &descid)) {
1001 if (protows->server_itf->on_describe) {
1002 /* create asynchronous job */
1003 desc = malloc(sizeof *desc);
1005 desc->descid = descid;
1006 desc->protows = protows;
1007 afb_proto_ws_addref(protows);
1008 protows->server_itf->on_describe(protows->closure, desc);
1012 server_send_description(protows, descid, NULL);
1016 static void server_on_session_add(struct afb_proto_ws *protows, struct readbuf *rb)
1019 const char *sessionstr;
1021 if (readbuf_uint16(rb, &sessionid) && readbuf_string(rb, &sessionstr, NULL))
1022 protows->server_itf->on_session_create(protows->closure, sessionid, sessionstr);
1025 static void server_on_session_drop(struct afb_proto_ws *protows, struct readbuf *rb)
1029 if (readbuf_uint16(rb, &sessionid))
1030 protows->server_itf->on_session_remove(protows->closure, sessionid);
1033 static void server_on_token_add(struct afb_proto_ws *protows, struct readbuf *rb)
1036 const char *tokenstr;
1038 if (readbuf_uint16(rb, &tokenid) && readbuf_string(rb, &tokenstr, NULL))
1039 protows->server_itf->on_token_create(protows->closure, tokenid, tokenstr);
1042 static void server_on_token_drop(struct afb_proto_ws *protows, struct readbuf *rb)
1046 if (readbuf_uint16(rb, &tokenid))
1047 protows->server_itf->on_token_remove(protows->closure, tokenid);
1050 static void server_on_event_unexpected(struct afb_proto_ws *protows, struct readbuf *rb)
1054 if (readbuf_uint16(rb, &eventid))
1055 protows->server_itf->on_event_unexpected(protows->closure, eventid);
1058 /* on version offer */
1059 static void server_on_version_offer(struct afb_proto_ws *protows, struct readbuf *rb)
1067 /* reads the descid */
1068 if (readbuf_uint32(rb, &id)
1069 && id == WSAPI_IDENTIFIER
1070 && readbuf_uint8(rb, &count)
1072 && (versions = (uint8_t*)readbuf_get(rb, (uint32_t)count))) {
1073 version = WSAPI_VERSION_UNSET;
1075 v = versions[--count];
1076 if (v >= WSAPI_VERSION_MIN
1077 && v <= WSAPI_VERSION_MAX
1078 && (version == WSAPI_VERSION_UNSET || version < v))
1081 if (version != WSAPI_VERSION_UNSET) {
1082 if (send_version_set(protows, version) >= 0) {
1083 protows->version = version;
1088 afb_proto_ws_hangup(protows);
1091 /* callback when receiving binary data */
1092 static void server_on_binary_job(int sig, void *closure)
1094 struct binary *binary = closure;
1097 switch (*binary->rb.head++) {
1099 server_on_call(binary->protows, &binary->rb);
1101 case CHAR_FOR_DESCRIBE:
1102 server_on_describe(binary->protows, &binary->rb);
1104 case CHAR_FOR_SESSION_ADD:
1105 server_on_session_add(binary->protows, &binary->rb);
1107 case CHAR_FOR_SESSION_DROP:
1108 server_on_session_drop(binary->protows, &binary->rb);
1110 case CHAR_FOR_TOKEN_ADD:
1111 server_on_token_add(binary->protows, &binary->rb);
1113 case CHAR_FOR_TOKEN_DROP:
1114 server_on_token_drop(binary->protows, &binary->rb);
1116 case CHAR_FOR_EVT_UNEXPECTED:
1117 server_on_event_unexpected(binary->protows, &binary->rb);
1119 case CHAR_FOR_VERSION_OFFER:
1120 server_on_version_offer(binary->protows, &binary->rb);
1122 default: /* unexpected message */
1123 /* TODO: close the connection */
1127 free(binary->rb.base);
1131 static void server_on_binary(void *closure, char *data, size_t size)
1133 struct afb_proto_ws *protows = closure;
1135 queue_message_processing(protows, data, size, server_on_binary_job);
1138 /******************* server part: manage events **********************************/
1140 static int server_event_send(struct afb_proto_ws *protows, char order, uint16_t event_id, const char *event_name, struct json_object *data)
1142 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
1145 if (writebuf_char(&wb, order)
1146 && writebuf_uint16(&wb, event_id)
1147 && (order != CHAR_FOR_EVT_ADD || writebuf_string(&wb, event_name))
1148 && (order != CHAR_FOR_EVT_PUSH || writebuf_object(&wb, data)))
1149 rc = proto_write(protows, &wb);
1153 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, uint16_t event_id, const char *event_name)
1155 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_id, event_name, NULL);
1158 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, uint16_t event_id)
1160 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_id, NULL, NULL);
1163 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, uint16_t event_id, struct json_object *data)
1165 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_id, NULL, data);
1168 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data, const unsigned char uuid[16], uint8_t hop)
1170 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
1176 if (writebuf_char(&wb, CHAR_FOR_EVT_BROADCAST)
1177 && writebuf_string(&wb, event_name)
1178 && writebuf_object(&wb, data)
1179 && writebuf_put(&wb, uuid, 16)
1180 && writebuf_uint8(&wb, (uint8_t)(hop - 1)))
1181 rc = proto_write(protows, &wb);
1185 /*****************************************************/
1187 /* callback when receiving a hangup */
1188 static void on_hangup(void *closure)
1190 struct afb_proto_ws *protows = closure;
1191 struct client_describe *cd, *ncd;
1192 struct client_call *call, *ncall;
1195 pthread_mutex_lock(&protows->mutex);
1196 ncd = protows->describes;
1197 protows->describes = NULL;
1198 ncall = protows->calls;
1199 protows->calls = NULL;
1202 protows->idcount = 0;
1203 pthread_mutex_unlock(&protows->mutex);
1208 protows->client_itf->on_reply(protows->closure, call->request, NULL, "disconnected", "server hung up");
1215 cd->callback(cd->closure, NULL);
1221 if (protows->on_hangup)
1222 protows->on_hangup(protows->closure);
1226 /*****************************************************/
1228 static const struct afb_ws_itf proto_ws_client_ws_itf =
1232 .on_binary = client_on_binary,
1234 .on_hangup = on_hangup
1237 static const struct afb_ws_itf server_ws_itf =
1241 .on_binary = server_on_binary,
1243 .on_hangup = on_hangup
1246 /*****************************************************/
1248 static struct afb_proto_ws *afb_proto_ws_create(struct fdev *fdev, const struct afb_proto_ws_server_itf *itfs, const struct afb_proto_ws_client_itf *itfc, void *closure, const struct afb_ws_itf *itf)
1250 struct afb_proto_ws *protows;
1252 protows = calloc(1, sizeof *protows);
1253 if (protows == NULL)
1256 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
1257 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
1258 protows->ws = afb_ws_create(fdev, itf, protows);
1259 if (protows->ws != NULL) {
1260 protows->refcount = 1;
1261 protows->version = WSAPI_VERSION_UNSET;
1262 protows->closure = closure;
1263 protows->server_itf = itfs;
1264 protows->client_itf = itfc;
1265 pthread_mutex_init(&protows->mutex, NULL);
1273 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1275 struct afb_proto_ws *protows;
1277 protows = afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1279 if (send_version_offer_1(protows, WSAPI_VERSION_1) != 0) {
1280 afb_proto_ws_unref(protows);
1287 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1289 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1292 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1294 if (protows && !__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1295 afb_proto_ws_hangup(protows);
1296 pthread_mutex_destroy(&protows->mutex);
1301 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1303 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1306 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1308 return !!protows->client_itf;
1311 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1313 return !!protows->server_itf;
1316 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1319 afb_ws_hangup(protows->ws);
1322 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1324 protows->on_hangup = on_hangup;
1327 void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(struct afb_proto_ws*, void (*)(int,void*), void*))
1329 protows->queuing = queuing;