2 * Copyright (C) 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.
29 #include <systemd/sd-event.h>
34 #include "afb-common.h"
37 * declaration of the websock interface for afb-ws
39 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
40 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
41 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size);
42 static void aws_on_text(struct afb_ws *ws, int last, size_t size);
43 static void aws_on_binary(struct afb_ws *ws, int last, size_t size);
44 static void aws_on_continue(struct afb_ws *ws, int last, size_t size);
45 static void aws_on_readable(struct afb_ws *ws);
46 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size);
48 static struct websock_itf aws_itf = {
49 .writev = (void*)aws_writev,
50 .readv = (void*)aws_readv,
54 .on_close = (void*)aws_on_close,
55 .on_text = (void*)aws_on_text,
56 .on_binary = (void*)aws_on_binary,
57 .on_continue = (void*)aws_on_continue,
60 .on_error = (void*)aws_on_error
64 * a common scheme of buffer handling
83 * the afb_ws structure
87 int fd; /* the socket file descriptor */
88 enum state state; /* current state */
89 const struct afb_ws_itf *itf; /* the callback interface */
90 void *closure; /* closure when calling the callbacks */
91 struct websock *ws; /* the websock handler */
92 sd_event_source *evsrc; /* the event source for the socket */
93 struct buf buffer; /* the last read fragment */
97 * Returns the current buffer of 'ws' that is reset.
99 static inline struct buf aws_pick_buffer(struct afb_ws *ws)
101 struct buf result = ws->buffer;
103 result.buffer[result.size] = 0;
104 ws->buffer.buffer = NULL;
110 * Clear the current buffer
112 static inline void aws_clear_buffer(struct afb_ws *ws)
118 * Disconnect the websocket 'ws' and calls on_hangup if
119 * 'call_on_hangup' is not null.
121 static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
123 struct websock *wsi = ws->ws;
126 sd_event_source_unref(ws->evsrc);
128 websock_destroy(wsi);
129 free(ws->buffer.buffer);
131 if (call_on_hangup && ws->itf->on_hangup)
132 ws->itf->on_hangup(ws->closure);
136 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *ws)
138 if ((revents & EPOLLIN) != 0)
140 if ((revents & EPOLLHUP) != 0)
146 * Creates the afb_ws structure for the file descritor
147 * 'fd' and the callbacks described by the interface 'itf'
149 * When the creation is a success, the systemd event loop 'eloop' is
150 * used for handling event for 'fd'.
152 * Returns the handle for the afb_ws created or NULL on error.
154 struct afb_ws *afb_ws_create(struct sd_event *eloop, int fd, const struct afb_ws_itf *itf, void *closure)
157 struct afb_ws *result;
162 result = malloc(sizeof * result);
168 result->state = waiting;
170 result->closure = closure;
171 result->buffer.buffer = NULL;
172 result->buffer.size = 0;
174 /* creates the websocket */
175 result->ws = websock_create_v13(&aws_itf, result);
176 if (result->ws == NULL)
179 /* creates the evsrc */
180 rc = sd_event_add_io(eloop, &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
188 websock_destroy(result->ws);
196 * Destroys the websocket 'ws'
197 * It first hangup (but without calling on_hangup for safety reasons)
200 void afb_ws_destroy(struct afb_ws *ws)
202 aws_disconnect(ws, 0);
207 * Hangup the websocket 'ws'
209 void afb_ws_hangup(struct afb_ws *ws)
211 aws_disconnect(ws, 1);
215 * Is the websocket 'ws' still connected ?
217 int afb_ws_is_connected(struct afb_ws *ws)
219 return ws->ws != NULL;
223 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
224 * 'reason' (that can be NULL and that else should not be greater than 123
226 * Returns 0 on success or -1 in case of error.
228 int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason)
230 if (ws->ws == NULL) {
235 return websock_close(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
239 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
240 * 'reason' (that can be NULL and that else should not be greater than 123
242 * Raise an error after 'close' command is sent.
243 * Returns 0 on success or -1 in case of error.
245 int afb_ws_error(struct afb_ws *ws, uint16_t code, const char *reason)
247 if (ws->ws == NULL) {
252 return websock_error(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
256 * Sends a 'text' of 'length' to the endpoint of 'ws'.
257 * Returns 0 on success or -1 in case of error.
259 int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
261 if (ws->ws == NULL) {
266 return websock_text(ws->ws, 1, text, length);
270 * Sends a variable list of texts to the endpoint of 'ws'.
271 * Returns 0 on success or -1 in case of error.
273 int afb_ws_texts(struct afb_ws *ws, ...)
276 struct iovec ios[32];
280 if (ws->ws == NULL) {
288 s = va_arg(args, const char *);
294 ios[count].iov_base = (void*)s;
295 ios[count].iov_len = strlen(s);
297 s = va_arg(args, const char *);
300 return websock_text_v(ws->ws, 1, ios, count);
304 * Sends a text data described in the 'count' 'iovec' to the endpoint of 'ws'.
305 * Returns 0 on success or -1 in case of error.
307 int afb_ws_text_v(struct afb_ws *ws, const struct iovec *iovec, int count)
309 if (ws->ws == NULL) {
314 return websock_text_v(ws->ws, 1, iovec, count);
318 * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
319 * Returns 0 on success or -1 in case of error.
321 int afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
323 if (ws->ws == NULL) {
328 return websock_binary(ws->ws, 1, data, length);
332 * Sends a binary data described in the 'count' 'iovec' to the endpoint of 'ws'.
333 * Returns 0 on success or -1 in case of error.
335 int afb_ws_binary_v(struct afb_ws *ws, const struct iovec *iovec, int count)
337 if (ws->ws == NULL) {
342 return websock_binary_v(ws->ws, 1, iovec, count);
346 * callback for writing data
348 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
352 rc = writev(ws->fd, iov, iovcnt);
356 else if (errno == EAGAIN) {
359 pfd.events = POLLOUT;
369 * callback for reading data
371 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
375 rc = readv(ws->fd, iov, iovcnt);
376 } while(rc == -1 && errno == EINTR);
385 * callback on incoming data
387 static void aws_on_readable(struct afb_ws *ws)
391 assert(ws->ws != NULL);
392 rc = websock_dispatch(ws->ws, 0);
393 if (rc < 0 && errno == EPIPE)
398 * Reads from the websocket handled by 'ws' data of length 'size'
399 * and append it to the current buffer of 'ws'.
400 * Returns 0 in case of error or 1 in case of success.
402 static int aws_read(struct afb_ws *ws, size_t size)
408 if (size != 0 || ws->buffer.buffer == NULL) {
409 buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
412 ws->buffer.buffer = buffer;
414 sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
420 poll(&pfd, 1, 10); /* TODO: make fully asynchronous websockets */
422 ws->buffer.size += (size_t)sz;
431 * Callback when 'close' command received from 'ws' with 'code' and 'size'.
433 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
438 aws_clear_buffer(ws);
439 if (ws->itf->on_close == NULL) {
440 websock_drop(ws->ws);
442 } else if (!aws_read(ws, size))
443 ws->itf->on_close(ws->closure, code, NULL, 0);
445 b = aws_pick_buffer(ws);
446 ws->itf->on_close(ws->closure, code, b.buffer, b.size);
451 * Drops any incoming data and send an error of 'code'
453 static void aws_drop_error(struct afb_ws *ws, uint16_t code)
456 aws_clear_buffer(ws);
457 websock_drop(ws->ws);
458 websock_error(ws->ws, code, NULL, 0);
462 * Reads either text or binary data of 'size' from 'ws' eventually 'last'.
464 static void aws_continue(struct afb_ws *ws, int last, size_t size)
469 if (!aws_read(ws, size))
470 aws_drop_error(ws, WEBSOCKET_CODE_ABNORMAL);
472 istxt = ws->state == reading_text;
474 b = aws_pick_buffer(ws);
475 (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size);
480 * Callback when 'text' message received from 'ws' with 'size' and possibly 'last'.
482 static void aws_on_text(struct afb_ws *ws, int last, size_t size)
484 if (ws->state != waiting)
485 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
486 else if (ws->itf->on_text == NULL)
487 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
489 ws->state = reading_text;
490 aws_continue(ws, last, size);
495 * Callback when 'binary' message received from 'ws' with 'size' and possibly 'last'.
497 static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
499 if (ws->state != waiting)
500 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
501 else if (ws->itf->on_binary == NULL)
502 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
504 ws->state = reading_binary;
505 aws_continue(ws, last, size);
510 * Callback when 'continue' command received from 'ws' with 'code' and 'size'.
512 static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
514 if (ws->state == waiting)
515 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
517 aws_continue(ws, last, size);
521 * Callback when 'close' command is sent to 'ws' with 'code' and 'size'.
523 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size)
525 if (ws->itf->on_error != NULL)
526 ws->itf->on_error(ws->closure, code, data, size);