2 * Copyright (C) 2016, 2017, 2018 "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_unref(ws->fdev);
124 websock_destroy(wsi);
125 free(ws->buffer.buffer);
127 if (call_on_hangup && ws->itf->on_hangup)
128 ws->itf->on_hangup(ws->closure);
132 static void fdevcb(void *ws, uint32_t revents, struct fdev *fdev)
134 if ((revents & EPOLLIN) != 0)
136 if ((revents & EPOLLHUP) != 0)
141 * Creates the afb_ws structure for the file descritor
142 * 'fd' and the callbacks described by the interface 'itf'
144 * When the creation is a success, the systemd event loop 'eloop' is
145 * used for handling event for 'fd'.
147 * Returns the handle for the afb_ws created or NULL on error.
149 struct afb_ws *afb_ws_create(struct fdev *fdev, const struct afb_ws_itf *itf, void *closure)
151 struct afb_ws *result;
156 result = malloc(sizeof * result);
162 result->fd = fdev_fd(fdev);
163 result->state = waiting;
165 result->closure = closure;
166 result->buffer.buffer = NULL;
167 result->buffer.size = 0;
169 /* creates the websocket */
170 result->ws = websock_create_v13(&aws_itf, result);
171 if (result->ws == NULL)
175 fdev_set_events(fdev, EPOLLIN);
176 fdev_set_callback(fdev, fdevcb, result);
187 * Destroys the websocket 'ws'
188 * It first hangup (but without calling on_hangup for safety reasons)
191 void afb_ws_destroy(struct afb_ws *ws)
193 aws_disconnect(ws, 0);
198 * Hangup the websocket 'ws'
200 void afb_ws_hangup(struct afb_ws *ws)
202 aws_disconnect(ws, 1);
206 * Is the websocket 'ws' still connected ?
208 int afb_ws_is_connected(struct afb_ws *ws)
210 return ws->ws != NULL;
214 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
215 * 'reason' (that can be NULL and that else should not be greater than 123
217 * Returns 0 on success or -1 in case of error.
219 int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason)
221 if (ws->ws == NULL) {
226 return websock_close(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
230 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
231 * 'reason' (that can be NULL and that else should not be greater than 123
233 * Raise an error after 'close' command is sent.
234 * Returns 0 on success or -1 in case of error.
236 int afb_ws_error(struct afb_ws *ws, uint16_t code, const char *reason)
238 if (ws->ws == NULL) {
243 return websock_error(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
247 * Sends a 'text' of 'length' to the endpoint of 'ws'.
248 * Returns 0 on success or -1 in case of error.
250 int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
252 if (ws->ws == NULL) {
257 return websock_text(ws->ws, 1, text, length);
261 * Sends a variable list of texts to the endpoint of 'ws'.
262 * Returns 0 on success or -1 in case of error.
264 int afb_ws_texts(struct afb_ws *ws, ...)
267 struct iovec ios[32];
271 if (ws->ws == NULL) {
279 s = va_arg(args, const char *);
285 ios[count].iov_base = (void*)s;
286 ios[count].iov_len = strlen(s);
288 s = va_arg(args, const char *);
291 return websock_text_v(ws->ws, 1, ios, count);
295 * Sends a text data described in the 'count' 'iovec' to the endpoint of 'ws'.
296 * Returns 0 on success or -1 in case of error.
298 int afb_ws_text_v(struct afb_ws *ws, const struct iovec *iovec, int count)
300 if (ws->ws == NULL) {
305 return websock_text_v(ws->ws, 1, iovec, count);
309 * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
310 * Returns 0 on success or -1 in case of error.
312 int afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
314 if (ws->ws == NULL) {
319 return websock_binary(ws->ws, 1, data, length);
323 * Sends a binary data described in the 'count' 'iovec' to the endpoint of 'ws'.
324 * Returns 0 on success or -1 in case of error.
326 int afb_ws_binary_v(struct afb_ws *ws, const struct iovec *iovec, int count)
328 if (ws->ws == NULL) {
333 return websock_binary_v(ws->ws, 1, iovec, count);
337 * callback for writing data
339 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
346 /* compute the size */
350 dsz += iov[i++].iov_len;
360 iov2 = (struct iovec*)iov;
363 rc = writev(ws->fd, iov2, iovcnt);
375 while (rc >= (ssize_t)iov2[i].iov_len)
376 rc -= (ssize_t)iov2[i++].iov_len;
383 iov2 = alloca(iovcnt * sizeof *iov2);
384 for (i = 0 ; i < iovcnt ; i++)
387 iov2->iov_base += rc;
391 pfd.events = POLLOUT;
397 * callback for reading data
399 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
403 rc = readv(ws->fd, iov, iovcnt);
404 } while(rc == -1 && errno == EINTR);
413 * callback on incoming data
415 static void aws_on_readable(struct afb_ws *ws)
419 assert(ws->ws != NULL);
420 rc = websock_dispatch(ws->ws, 0);
421 if (rc < 0 && errno == EPIPE)
426 * Reads from the websocket handled by 'ws' data of length 'size'
427 * and append it to the current buffer of 'ws'.
428 * Returns 0 in case of error or 1 in case of success.
430 static int aws_read(struct afb_ws *ws, size_t size)
436 if (size != 0 || ws->buffer.buffer == NULL) {
437 buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
440 ws->buffer.buffer = buffer;
442 sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
448 poll(&pfd, 1, 10); /* TODO: make fully asynchronous websockets */
450 ws->buffer.size += (size_t)sz;
459 * Callback when 'close' command received from 'ws' with 'code' and 'size'.
461 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
466 aws_clear_buffer(ws);
467 if (ws->itf->on_close == NULL) {
468 websock_drop(ws->ws);
470 } else if (!aws_read(ws, size))
471 ws->itf->on_close(ws->closure, code, NULL, 0);
473 b = aws_pick_buffer(ws);
474 ws->itf->on_close(ws->closure, code, b.buffer, b.size);
479 * Drops any incoming data and send an error of 'code'
481 static void aws_drop_error(struct afb_ws *ws, uint16_t code)
484 aws_clear_buffer(ws);
485 websock_drop(ws->ws);
486 websock_error(ws->ws, code, NULL, 0);
490 * Reads either text or binary data of 'size' from 'ws' eventually 'last'.
492 static void aws_continue(struct afb_ws *ws, int last, size_t size)
497 if (!aws_read(ws, size))
498 aws_drop_error(ws, WEBSOCKET_CODE_ABNORMAL);
500 istxt = ws->state == reading_text;
502 b = aws_pick_buffer(ws);
503 (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size);
508 * Callback when 'text' message received from 'ws' with 'size' and possibly 'last'.
510 static void aws_on_text(struct afb_ws *ws, int last, size_t size)
512 if (ws->state != waiting)
513 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
514 else if (ws->itf->on_text == NULL)
515 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
517 ws->state = reading_text;
518 aws_continue(ws, last, size);
523 * Callback when 'binary' message received from 'ws' with 'size' and possibly 'last'.
525 static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
527 if (ws->state != waiting)
528 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
529 else if (ws->itf->on_binary == NULL)
530 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
532 ws->state = reading_binary;
533 aws_continue(ws, last, size);
538 * Callback when 'continue' command received from 'ws' with 'code' and 'size'.
540 static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
542 if (ws->state == waiting)
543 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
545 aws_continue(ws, last, size);
549 * Callback when 'close' command is sent to 'ws' with 'code' and 'size'.
551 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size)
553 if (ws->itf->on_error != NULL)
554 ws->itf->on_error(ws->closure, code, data, size);