2 * Copyright 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.
27 #include <systemd/sd-event.h>
32 #include "afb-common.h"
35 * declaration of the websock interface for afb-ws
37 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
38 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
39 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size);
40 static void aws_on_text(struct afb_ws *ws, int last, size_t size);
41 static void aws_on_binary(struct afb_ws *ws, int last, size_t size);
42 static void aws_on_continue(struct afb_ws *ws, int last, size_t size);
43 static void aws_on_readable(struct afb_ws *ws);
44 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size);
46 static struct websock_itf aws_itf = {
47 .writev = (void*)aws_writev,
48 .readv = (void*)aws_readv,
52 .on_close = (void*)aws_on_close,
53 .on_text = (void*)aws_on_text,
54 .on_binary = (void*)aws_on_binary,
55 .on_continue = (void*)aws_on_continue,
58 .on_error = (void*)aws_on_error
62 * a common scheme of buffer handling
81 * the afb_ws structure
85 int fd; /* the socket file descriptor */
86 enum state state; /* current state */
87 const struct afb_ws_itf *itf; /* the callback interface */
88 void *closure; /* closure when calling the callbacks */
89 struct websock *ws; /* the websock handler */
90 sd_event_source *evsrc; /* the event source for the socket */
91 struct buf buffer; /* the last read fragment */
95 * Returns the current buffer of 'ws' that is reset.
97 static inline struct buf aws_pick_buffer(struct afb_ws *ws)
99 struct buf result = ws->buffer;
100 ws->buffer.buffer = NULL;
106 * Disconnect the websocket 'ws' and calls on_hangup if
107 * 'call_on_hangup' is not null.
109 static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
111 struct websock *wsi = ws->ws;
114 sd_event_source_unref(ws->evsrc);
116 websock_destroy(wsi);
117 free(aws_pick_buffer(ws).buffer);
119 if (call_on_hangup && ws->itf->on_hangup)
120 ws->itf->on_hangup(ws->closure);
124 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *ws)
126 if ((revents & EPOLLIN) != 0)
128 if ((revents & EPOLLHUP) != 0)
134 * Creates the afb_ws structure for the file descritor
135 * 'fd' and the callbacks described by the interface 'itf'
138 * Returns the handle for the afb_ws created or NULL on error.
140 struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure)
143 struct afb_ws *result;
148 result = malloc(sizeof * result);
154 result->state = waiting;
156 result->closure = closure;
157 result->buffer.buffer = NULL;
158 result->buffer.size = 0;
160 /* creates the websocket */
161 result->ws = websock_create_v13(&aws_itf, result);
162 if (result->ws == NULL)
165 /* creates the evsrc */
166 rc = sd_event_add_io(afb_common_get_event_loop(), &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
174 websock_destroy(result->ws);
182 * Destroys the websocket 'ws'
183 * It first hangup (but without calling on_hangup for safety reasons)
186 void afb_ws_destroy(struct afb_ws *ws)
188 aws_disconnect(ws, 0);
193 * Hangup the websocket 'ws'
195 void afb_ws_hangup(struct afb_ws *ws)
197 aws_disconnect(ws, 1);
201 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
202 * 'reason' (that can be NULL and that else should not be greater than 123
204 * Returns 0 on success or -1 in case of error.
206 int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason)
208 if (ws->ws == NULL) {
213 return websock_close(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
217 * Sends a 'close' command to the endpoint of 'ws' with the 'code' and the
218 * 'reason' (that can be NULL and that else should not be greater than 123
220 * Raise an error after 'close' command is sent.
221 * Returns 0 on success or -1 in case of error.
223 int afb_ws_error(struct afb_ws *ws, uint16_t code, const char *reason)
225 if (ws->ws == NULL) {
230 return websock_error(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
234 * Sends a 'text' of 'length' to the endpoint of 'ws'.
235 * Returns 0 on success or -1 in case of error.
237 int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
239 if (ws->ws == NULL) {
244 return websock_text(ws->ws, 1, text, length);
248 * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
249 * Returns 0 on success or -1 in case of error.
251 int afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
253 if (ws->ws == NULL) {
258 return websock_binary(ws->ws, 1, data, length);
262 * callback for writing data
264 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
268 rc = writev(ws->fd, iov, iovcnt);
269 } while(rc == -1 && errno == EINTR);
274 * callback for reading data
276 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
280 rc = readv(ws->fd, iov, iovcnt);
281 } while(rc == -1 && errno == EINTR);
290 * callback on incoming data
292 static void aws_on_readable(struct afb_ws *ws)
296 assert(ws->ws != NULL);
297 rc = websock_dispatch(ws->ws);
298 if (rc < 0 && errno == EPIPE)
303 * Reads from the websocket handled by 'ws' data of length 'size'
304 * and append it to the current buffer of 'ws'.
305 * Returns 0 in case of error or 1 in case of success.
307 static int aws_read(struct afb_ws *ws, size_t size)
313 buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
316 ws->buffer.buffer = buffer;
317 sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
318 if ((size_t)sz != size)
320 ws->buffer.size += size;
326 * Callback when 'close' command received from 'ws' with 'code' and 'size'.
328 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
333 free(aws_pick_buffer(ws).buffer);
334 if (ws->itf->on_close == NULL) {
335 websock_drop(ws->ws);
337 } else if (!aws_read(ws, size))
338 ws->itf->on_close(ws->closure, code, NULL, 0);
340 b = aws_pick_buffer(ws);
341 ws->itf->on_close(ws->closure, code, b.buffer, b.size);
346 * Drops any incoming data and send an error of 'code'
348 static void aws_drop_error(struct afb_ws *ws, uint16_t code)
351 free(aws_pick_buffer(ws).buffer);
352 websock_drop(ws->ws);
353 websock_error(ws->ws, code, NULL, 0);
357 * Reads either text or binary data of 'size' from 'ws' eventually 'last'.
359 static void aws_continue(struct afb_ws *ws, int last, size_t size)
364 if (!aws_read(ws, size))
365 aws_drop_error(ws, WEBSOCKET_CODE_ABNORMAL);
367 istxt = ws->state == reading_text;
369 b = aws_pick_buffer(ws);
370 b.buffer[b.size] = 0;
371 (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size);
376 * Callback when 'text' message received from 'ws' with 'size' and possibly 'last'.
378 static void aws_on_text(struct afb_ws *ws, int last, size_t size)
380 if (ws->state != waiting)
381 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
382 else if (ws->itf->on_text == NULL)
383 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
385 ws->state = reading_text;
386 aws_continue(ws, last, size);
391 * Callback when 'binary' message received from 'ws' with 'size' and possibly 'last'.
393 static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
395 if (ws->state != waiting)
396 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
397 else if (ws->itf->on_binary == NULL)
398 aws_drop_error(ws, WEBSOCKET_CODE_CANT_ACCEPT);
400 ws->state = reading_binary;
401 aws_continue(ws, last, size);
406 * Callback when 'close' command received from 'ws' with 'code' and 'size'.
408 static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
410 if (ws->state == waiting)
411 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
413 aws_continue(ws, last, size);
417 * Callback when 'close' command is sent to 'ws' with 'code' and 'size'.
419 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size)
421 if (ws->itf->on_error != NULL)
422 ws->itf->on_error(ws->closure, code, data, size);