2 * Copyright (C) 2016-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.
34 * declaration of the websock interface for afb-ws
36 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
37 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
38 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size);
39 static void aws_on_text(struct afb_ws *ws, int last, size_t size);
40 static void aws_on_binary(struct afb_ws *ws, int last, size_t size);
41 static void aws_on_continue(struct afb_ws *ws, int last, size_t size);
42 static void aws_on_readable(struct afb_ws *ws);
43 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size);
45 static struct websock_itf aws_itf = {
46 .writev = (void*)aws_writev,
47 .readv = (void*)aws_readv,
51 .on_close = (void*)aws_on_close,
52 .on_text = (void*)aws_on_text,
53 .on_binary = (void*)aws_on_binary,
54 .on_continue = (void*)aws_on_continue,
57 .on_error = (void*)aws_on_error
61 * a common scheme of buffer handling
80 * the afb_ws structure
84 int fd; /* the socket file descriptor */
85 enum state state; /* current state */
86 const struct afb_ws_itf *itf; /* the callback interface */
87 void *closure; /* closure when calling the callbacks */
88 struct websock *ws; /* the websock handler */
89 struct fdev *fdev; /* the fdev for the socket */
90 struct buf buffer; /* the last read fragment */
94 * Returns the current buffer of 'ws' that is reset.
96 static inline struct buf aws_pick_buffer(struct afb_ws *ws)
98 struct buf result = ws->buffer;
100 result.buffer[result.size] = 0;
101 ws->buffer.buffer = NULL;
107 * Clear the current buffer
109 static inline void aws_clear_buffer(struct afb_ws *ws)
115 * Disconnect the websocket 'ws' and calls on_hangup if
116 * 'call_on_hangup' is not null.
118 static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
120 struct websock *wsi = ws->ws;
123 fdev_set_callback(ws->fdev, NULL, 0);
124 fdev_unref(ws->fdev);
125 websock_destroy(wsi);
126 free(ws->buffer.buffer);
128 if (call_on_hangup && ws->itf->on_hangup)
129 ws->itf->on_hangup(ws->closure);
133 static void fdevcb(void *ws, uint32_t revents, struct fdev *fdev)
135 if ((revents & EPOLLIN) != 0)
137 else if ((revents & EPOLLHUP) != 0)
142 * Creates the afb_ws structure for the file descritor
143 * 'fd' and the callbacks described by the interface 'itf'
145 * When the creation is a success, the systemd event loop 'eloop' is
146 * used for handling event for 'fd'.
148 * Returns the handle for the afb_ws created or NULL on error.
150 struct afb_ws *afb_ws_create(struct fdev *fdev, const struct afb_ws_itf *itf, void *closure)
152 struct afb_ws *result;
157 result = malloc(sizeof * result);
163 result->fd = fdev_fd(fdev);
164 result->state = waiting;
166 result->closure = closure;
167 result->buffer.buffer = NULL;
168 result->buffer.size = 0;
170 /* creates the websocket */
171 result->ws = websock_create_v13(&aws_itf, result);
172 if (result->ws == NULL)
176 fdev_set_events(fdev, EPOLLIN);
177 fdev_set_callback(fdev, fdevcb, result);
188 * Destroys the websocket 'ws'
189 * It first hangup (but without calling on_hangup for safety reasons)
192 void afb_ws_destroy(struct afb_ws *ws)
194 aws_disconnect(ws, 0);
199 * Hangup the websocket 'ws'
201 void afb_ws_hangup(struct afb_ws *ws)
203 aws_disconnect(ws, 1);
207 * Is the websocket 'ws' still connected ?
209 int afb_ws_is_connected(struct afb_ws *ws)
211 return ws->ws != NULL;
215 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
216 * 'reason' (that can be NULL and that else should not be greater than 123
218 * Returns 0 on success or -1 in case of error.
220 int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason)
222 if (ws->ws == NULL) {
227 return websock_close(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
231 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
232 * 'reason' (that can be NULL and that else should not be greater than 123
234 * Raise an error after 'close' command is sent.
235 * Returns 0 on success or -1 in case of error.
237 int afb_ws_error(struct afb_ws *ws, uint16_t code, const char *reason)
239 if (ws->ws == NULL) {
244 return websock_error(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
248 * Sends a 'text' of 'length' to the endpoint of 'ws'.
249 * Returns 0 on success or -1 in case of error.
251 int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
253 if (ws->ws == NULL) {
258 return websock_text(ws->ws, 1, text, length);
262 * Sends a variable list of texts to the endpoint of 'ws'.
263 * Returns 0 on success or -1 in case of error.
265 int afb_ws_texts(struct afb_ws *ws, ...)
268 struct iovec ios[32];
272 if (ws->ws == NULL) {
280 s = va_arg(args, const char *);
286 ios[count].iov_base = (void*)s;
287 ios[count].iov_len = strlen(s);
289 s = va_arg(args, const char *);
292 return websock_text_v(ws->ws, 1, ios, count);
296 * Sends a text data described in the 'count' 'iovec' to the endpoint of 'ws'.
297 * Returns 0 on success or -1 in case of error.
299 int afb_ws_text_v(struct afb_ws *ws, const struct iovec *iovec, int count)
301 if (ws->ws == NULL) {
306 return websock_text_v(ws->ws, 1, iovec, count);
310 * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
311 * Returns 0 on success or -1 in case of error.
313 int afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
315 if (ws->ws == NULL) {
320 return websock_binary(ws->ws, 1, data, length);
324 * Sends a binary data described in the 'count' 'iovec' to the endpoint of 'ws'.
325 * Returns 0 on success or -1 in case of error.
327 int afb_ws_binary_v(struct afb_ws *ws, const struct iovec *iovec, int count)
329 if (ws->ws == NULL) {
334 return websock_binary_v(ws->ws, 1, iovec, count);
338 * callback for writing data
340 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
347 /* compute the size */
351 dsz += iov[i++].iov_len;
361 iov2 = (struct iovec*)iov;
364 rc = writev(ws->fd, iov2, iovcnt);
376 while (rc >= (ssize_t)iov2[i].iov_len)
377 rc -= (ssize_t)iov2[i++].iov_len;
384 iov2 = alloca(iovcnt * sizeof *iov2);
385 for (i = 0 ; i < iovcnt ; i++)
388 iov2->iov_base += rc;
392 pfd.events = POLLOUT;
398 * callback for reading data
400 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
404 rc = readv(ws->fd, iov, iovcnt);
405 } while(rc == -1 && errno == EINTR);
414 * callback on incoming data
416 static void aws_on_readable(struct afb_ws *ws)
420 assert(ws->ws != NULL);
421 rc = websock_dispatch(ws->ws, 0);
422 if (rc < 0 && errno == EPIPE)
427 * Reads from the websocket handled by 'ws' data of length 'size'
428 * and append it to the current buffer of 'ws'.
429 * Returns 0 in case of error or 1 in case of success.
431 static int aws_read(struct afb_ws *ws, size_t size)
437 if (size != 0 || ws->buffer.buffer == NULL) {
438 buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
441 ws->buffer.buffer = buffer;
443 sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
449 poll(&pfd, 1, 10); /* TODO: make fully asynchronous websockets */
451 ws->buffer.size += (size_t)sz;
460 * Callback when 'close' command received from 'ws' with 'code' and 'size'.
462 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
467 aws_clear_buffer(ws);
468 if (ws->itf->on_close == NULL) {
469 websock_drop(ws->ws);
471 } else if (!aws_read(ws, size))
472 ws->itf->on_close(ws->closure, code, NULL, 0);
474 b = aws_pick_buffer(ws);
475 ws->itf->on_close(ws->closure, code, b.buffer, b.size);
480 * Drops any incoming data and send an error of 'code'
482 static void aws_drop_error(struct afb_ws *ws, uint16_t code)
485 aws_clear_buffer(ws);
486 websock_drop(ws->ws);
487 websock_error(ws->ws, code, NULL, 0);
491 * Reads either text or binary data of 'size' from 'ws' eventually 'last'.
493 static void aws_continue(struct afb_ws *ws, int last, size_t size)
498 if (!aws_read(ws, size))
499 aws_drop_error(ws, WEBSOCKET_CODE_ABNORMAL);
501 istxt = ws->state == reading_text;
503 b = aws_pick_buffer(ws);
504 (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size);
509 * Callback when 'text' message received from 'ws' with 'size' and possibly 'last'.
511 static void aws_on_text(struct afb_ws *ws, int last, size_t size)
513 if (ws->state != waiting)
514 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
515 else if (ws->itf->on_text == NULL)
516 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
518 ws->state = reading_text;
519 aws_continue(ws, last, size);
524 * Callback when 'binary' message received from 'ws' with 'size' and possibly 'last'.
526 static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
528 if (ws->state != waiting)
529 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
530 else if (ws->itf->on_binary == NULL)
531 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
533 ws->state = reading_binary;
534 aws_continue(ws, last, size);
539 * Callback when 'continue' command received from 'ws' with 'code' and 'size'.
541 static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
543 if (ws->state == waiting)
544 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
546 aws_continue(ws, last, size);
550 * Callback when 'close' command is sent to 'ws' with 'code' and 'size'.
552 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size)
554 if (ws->itf->on_error != NULL)
555 ws->itf->on_error(ws->closure, code, data, size);