2 * Copyright (C) 2016 "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 <systemd/sd-event.h>
33 #include "afb-common.h"
36 * declaration of the websock interface for afb-ws
38 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
39 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
40 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size);
41 static void aws_on_text(struct afb_ws *ws, int last, size_t size);
42 static void aws_on_binary(struct afb_ws *ws, int last, size_t size);
43 static void aws_on_continue(struct afb_ws *ws, int last, size_t size);
44 static void aws_on_readable(struct afb_ws *ws);
45 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size);
47 static struct websock_itf aws_itf = {
48 .writev = (void*)aws_writev,
49 .readv = (void*)aws_readv,
53 .on_close = (void*)aws_on_close,
54 .on_text = (void*)aws_on_text,
55 .on_binary = (void*)aws_on_binary,
56 .on_continue = (void*)aws_on_continue,
59 .on_error = (void*)aws_on_error
63 * a common scheme of buffer handling
82 * the afb_ws structure
86 int fd; /* the socket file descriptor */
87 enum state state; /* current state */
88 const struct afb_ws_itf *itf; /* the callback interface */
89 void *closure; /* closure when calling the callbacks */
90 struct websock *ws; /* the websock handler */
91 sd_event_source *evsrc; /* the event source for the socket */
92 struct buf buffer; /* the last read fragment */
96 * Returns the current buffer of 'ws' that is reset.
98 static inline struct buf aws_pick_buffer(struct afb_ws *ws)
100 struct buf result = ws->buffer;
101 ws->buffer.buffer = NULL;
107 * Disconnect the websocket 'ws' and calls on_hangup if
108 * 'call_on_hangup' is not null.
110 static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
112 struct websock *wsi = ws->ws;
115 sd_event_source_unref(ws->evsrc);
117 websock_destroy(wsi);
118 free(aws_pick_buffer(ws).buffer);
120 if (call_on_hangup && ws->itf->on_hangup)
121 ws->itf->on_hangup(ws->closure);
125 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *ws)
127 if ((revents & EPOLLIN) != 0)
129 if ((revents & EPOLLHUP) != 0)
135 * Creates the afb_ws structure for the file descritor
136 * 'fd' and the callbacks described by the interface 'itf'
138 * When the creation is a success, the systemd event loop 'eloop' is
139 * used for handling event for 'fd'.
141 * Returns the handle for the afb_ws created or NULL on error.
143 struct afb_ws *afb_ws_create(struct sd_event *eloop, int fd, const struct afb_ws_itf *itf, void *closure)
146 struct afb_ws *result;
151 result = malloc(sizeof * result);
157 result->state = waiting;
159 result->closure = closure;
160 result->buffer.buffer = NULL;
161 result->buffer.size = 0;
163 /* creates the websocket */
164 result->ws = websock_create_v13(&aws_itf, result);
165 if (result->ws == NULL)
168 /* creates the evsrc */
169 rc = sd_event_add_io(eloop, &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
177 websock_destroy(result->ws);
185 * Destroys the websocket 'ws'
186 * It first hangup (but without calling on_hangup for safety reasons)
189 void afb_ws_destroy(struct afb_ws *ws)
191 aws_disconnect(ws, 0);
196 * Hangup the websocket 'ws'
198 void afb_ws_hangup(struct afb_ws *ws)
200 aws_disconnect(ws, 1);
204 * Is the websocket 'ws' still connected ?
206 int afb_ws_is_connected(struct afb_ws *ws)
208 return ws->ws != NULL;
212 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
213 * 'reason' (that can be NULL and that else should not be greater than 123
215 * Returns 0 on success or -1 in case of error.
217 int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason)
219 if (ws->ws == NULL) {
224 return websock_close(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
228 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
229 * 'reason' (that can be NULL and that else should not be greater than 123
231 * Raise an error after 'close' command is sent.
232 * Returns 0 on success or -1 in case of error.
234 int afb_ws_error(struct afb_ws *ws, uint16_t code, const char *reason)
236 if (ws->ws == NULL) {
241 return websock_error(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
245 * Sends a 'text' of 'length' to the endpoint of 'ws'.
246 * Returns 0 on success or -1 in case of error.
248 int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
250 if (ws->ws == NULL) {
255 return websock_text(ws->ws, 1, text, length);
259 * Sends a variable list of texts to the endpoint of 'ws'.
260 * Returns 0 on success or -1 in case of error.
262 int afb_ws_texts(struct afb_ws *ws, ...)
265 struct iovec ios[32];
269 if (ws->ws == NULL) {
277 s = va_arg(args, const char *);
283 ios[count].iov_base = (void*)s;
284 ios[count].iov_len = strlen(s);
286 s = va_arg(args, const char *);
289 return websock_text_v(ws->ws, 1, ios, count);
293 * Sends a text data described in the 'count' 'iovec' to the endpoint of 'ws'.
294 * Returns 0 on success or -1 in case of error.
296 int afb_ws_text_v(struct afb_ws *ws, const struct iovec *iovec, int count)
298 if (ws->ws == NULL) {
303 return websock_text_v(ws->ws, 1, iovec, count);
307 * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
308 * Returns 0 on success or -1 in case of error.
310 int afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
312 if (ws->ws == NULL) {
317 return websock_binary(ws->ws, 1, data, length);
321 * Sends a binary data described in the 'count' 'iovec' to the endpoint of 'ws'.
322 * Returns 0 on success or -1 in case of error.
324 int afb_ws_binary_v(struct afb_ws *ws, const struct iovec *iovec, int count)
326 if (ws->ws == NULL) {
331 return websock_binary_v(ws->ws, 1, iovec, count);
335 * callback for writing data
337 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
341 rc = writev(ws->fd, iov, iovcnt);
342 } while(rc == -1 && errno == EINTR);
347 * callback for reading data
349 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
353 rc = readv(ws->fd, iov, iovcnt);
354 } while(rc == -1 && errno == EINTR);
363 * callback on incoming data
365 static void aws_on_readable(struct afb_ws *ws)
369 assert(ws->ws != NULL);
370 rc = websock_dispatch(ws->ws);
371 if (rc < 0 && errno == EPIPE)
376 * Reads from the websocket handled by 'ws' data of length 'size'
377 * and append it to the current buffer of 'ws'.
378 * Returns 0 in case of error or 1 in case of success.
380 static int aws_read(struct afb_ws *ws, size_t size)
386 buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
389 ws->buffer.buffer = buffer;
390 sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
391 if ((size_t)sz != size)
393 ws->buffer.size += size;
399 * Callback when 'close' command received from 'ws' with 'code' and 'size'.
401 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
406 free(aws_pick_buffer(ws).buffer);
407 if (ws->itf->on_close == NULL) {
408 websock_drop(ws->ws);
410 } else if (!aws_read(ws, size))
411 ws->itf->on_close(ws->closure, code, NULL, 0);
413 b = aws_pick_buffer(ws);
414 ws->itf->on_close(ws->closure, code, b.buffer, b.size);
419 * Drops any incoming data and send an error of 'code'
421 static void aws_drop_error(struct afb_ws *ws, uint16_t code)
424 free(aws_pick_buffer(ws).buffer);
425 websock_drop(ws->ws);
426 websock_error(ws->ws, code, NULL, 0);
430 * Reads either text or binary data of 'size' from 'ws' eventually 'last'.
432 static void aws_continue(struct afb_ws *ws, int last, size_t size)
437 if (!aws_read(ws, size))
438 aws_drop_error(ws, WEBSOCKET_CODE_ABNORMAL);
440 istxt = ws->state == reading_text;
442 b = aws_pick_buffer(ws);
443 b.buffer[b.size] = 0;
444 (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size);
449 * Callback when 'text' message received from 'ws' with 'size' and possibly 'last'.
451 static void aws_on_text(struct afb_ws *ws, int last, size_t size)
453 if (ws->state != waiting)
454 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
455 else if (ws->itf->on_text == NULL)
456 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
458 ws->state = reading_text;
459 aws_continue(ws, last, size);
464 * Callback when 'binary' message received from 'ws' with 'size' and possibly 'last'.
466 static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
468 if (ws->state != waiting)
469 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
470 else if (ws->itf->on_binary == NULL)
471 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
473 ws->state = reading_binary;
474 aws_continue(ws, last, size);
479 * Callback when 'close' command received from 'ws' with 'code' and 'size'.
481 static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
483 if (ws->state == waiting)
484 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
486 aws_continue(ws, last, size);
490 * Callback when 'close' command is sent to 'ws' with 'code' and 'size'.
492 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size)
494 if (ws->itf->on_error != NULL)
495 ws->itf->on_error(ws->closure, code, data, size);