afb-ws: adds function afb_ws_is_connected
[src/app-framework-binder.git] / src / afb-ws.c
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdint.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <sys/uio.h>
25 #include <string.h>
26 #include <stdarg.h>
27
28 #include <systemd/sd-event.h>
29
30 #include "websock.h"
31 #include "afb-ws.h"
32
33 #include "afb-common.h"
34
35 /*
36  * declaration of the websock interface for afb-ws
37  */
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);
46
47 static struct websock_itf aws_itf = {
48         .writev = (void*)aws_writev,
49         .readv = (void*)aws_readv,
50
51         .on_ping = NULL,
52         .on_pong = NULL,
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,
57         .on_extension = NULL,
58
59         .on_error = (void*)aws_on_error
60 };
61
62 /*
63  * a common scheme of buffer handling
64  */
65 struct buf
66 {
67         char *buffer;
68         size_t size;
69 };
70
71 /*
72  * the state
73  */
74 enum state
75 {
76         waiting,
77         reading_text,
78         reading_binary
79 };
80
81 /*
82  * the afb_ws structure
83  */
84 struct afb_ws
85 {
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 */
93 };
94
95 /*
96  * Returns the current buffer of 'ws' that is reset.
97  */
98 static inline struct buf aws_pick_buffer(struct afb_ws *ws)
99 {
100         struct buf result = ws->buffer;
101         ws->buffer.buffer = NULL;
102         ws->buffer.size = 0;
103         return result;
104 }
105
106 /*
107  * Disconnect the websocket 'ws' and calls on_hangup if
108  * 'call_on_hangup' is not null.
109  */
110 static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
111 {
112         struct websock *wsi = ws->ws;
113         if (wsi != NULL) {
114                 ws->ws = NULL;
115                 sd_event_source_unref(ws->evsrc);
116                 ws->evsrc = NULL;
117                 websock_destroy(wsi);
118                 free(aws_pick_buffer(ws).buffer);
119                 ws->state = waiting;
120                 if (call_on_hangup && ws->itf->on_hangup)
121                         ws->itf->on_hangup(ws->closure);
122         }
123 }
124
125 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *ws)
126 {
127         if ((revents & EPOLLIN) != 0)
128                 aws_on_readable(ws);
129         if ((revents & EPOLLHUP) != 0)
130                 afb_ws_hangup(ws);
131         return 0;
132 }
133
134 /*
135  * Creates the afb_ws structure for the file descritor
136  * 'fd' and the callbacks described by the interface 'itf'
137  * and its 'closure'.
138  * When the creation is a success, the systemd event loop 'eloop' is
139  * used for handling event for 'fd'.
140  *
141  * Returns the handle for the afb_ws created or NULL on error.
142  */
143 struct afb_ws *afb_ws_create(struct sd_event *eloop, int fd, const struct afb_ws_itf *itf, void *closure)
144 {
145         int rc;
146         struct afb_ws *result;
147
148         assert(fd >= 0);
149
150         /* allocation */
151         result = malloc(sizeof * result);
152         if (result == NULL)
153                 goto error;
154
155         /* init */
156         result->fd = fd;
157         result->state = waiting;
158         result->itf = itf;
159         result->closure = closure;
160         result->buffer.buffer = NULL;
161         result->buffer.size = 0;
162
163         /* creates the websocket */
164         result->ws = websock_create_v13(&aws_itf, result);
165         if (result->ws == NULL)
166                 goto error2;
167
168         /* creates the evsrc */
169         rc = sd_event_add_io(eloop, &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
170         if (rc < 0) {
171                 errno = -rc;
172                 goto error3;
173         }
174         return result;
175
176 error3:
177         websock_destroy(result->ws);
178 error2:
179         free(result);
180 error:
181         return NULL;
182 }
183
184 /*
185  * Destroys the websocket 'ws'
186  * It first hangup (but without calling on_hangup for safety reasons)
187  * if needed.
188  */
189 void afb_ws_destroy(struct afb_ws *ws)
190 {
191         aws_disconnect(ws, 0);
192         free(ws);
193 }
194
195 /*
196  * Hangup the websocket 'ws'
197  */
198 void afb_ws_hangup(struct afb_ws *ws)
199 {
200         aws_disconnect(ws, 1);
201 }
202
203 /*
204  * Is the websocket 'ws' still connected ?
205  */
206 int afb_ws_is_connected(struct afb_ws *ws)
207 {
208         return ws->ws != NULL;
209 }
210
211 /*
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
214  * characters).
215  * Returns 0 on success or -1 in case of error.
216  */
217 int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason)
218 {
219         if (ws->ws == NULL) {
220                 /* disconnected */
221                 errno = EPIPE;
222                 return -1;
223         }
224         return websock_close(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
225 }
226
227 /*
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
230  * characters).
231  * Raise an error after 'close' command is sent.
232  * Returns 0 on success or -1 in case of error.
233  */
234 int afb_ws_error(struct afb_ws *ws, uint16_t code, const char *reason)
235 {
236         if (ws->ws == NULL) {
237                 /* disconnected */
238                 errno = EPIPE;
239                 return -1;
240         }
241         return websock_error(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
242 }
243
244 /*
245  * Sends a 'text' of 'length' to the endpoint of 'ws'.
246  * Returns 0 on success or -1 in case of error.
247  */
248 int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
249 {
250         if (ws->ws == NULL) {
251                 /* disconnected */
252                 errno = EPIPE;
253                 return -1;
254         }
255         return websock_text(ws->ws, 1, text, length);
256 }
257
258 /*
259  * Sends a variable list of texts to the endpoint of 'ws'.
260  * Returns 0 on success or -1 in case of error.
261  */
262 int afb_ws_texts(struct afb_ws *ws, ...)
263 {
264         va_list args;
265         struct iovec ios[32];
266         int count;
267         const char *s;
268
269         if (ws->ws == NULL) {
270                 /* disconnected */
271                 errno = EPIPE;
272                 return -1;
273         }
274
275         count = 0;
276         va_start(args, ws);
277         s = va_arg(args, const char *);
278         while (s != NULL) {
279                 if (count == 32) {
280                         errno = EINVAL;
281                         return -1;
282                 }
283                 ios[count].iov_base = (void*)s;
284                 ios[count].iov_len = strlen(s);
285                 count++;
286                 s = va_arg(args, const char *);
287         }
288         va_end(args);
289         return websock_text_v(ws->ws, 1, ios, count);
290 }
291
292 /*
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.
295  */
296 int afb_ws_text_v(struct afb_ws *ws, const struct iovec *iovec, int count)
297 {
298         if (ws->ws == NULL) {
299                 /* disconnected */
300                 errno = EPIPE;
301                 return -1;
302         }
303         return websock_text_v(ws->ws, 1, iovec, count);
304 }
305
306 /*
307  * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
308  * Returns 0 on success or -1 in case of error.
309  */
310 int afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
311 {
312         if (ws->ws == NULL) {
313                 /* disconnected */
314                 errno = EPIPE;
315                 return -1;
316         }
317         return websock_binary(ws->ws, 1, data, length);
318 }
319
320 /*
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.
323  */
324 int afb_ws_binary_v(struct afb_ws *ws, const struct iovec *iovec, int count)
325 {
326         if (ws->ws == NULL) {
327                 /* disconnected */
328                 errno = EPIPE;
329                 return -1;
330         }
331         return websock_binary_v(ws->ws, 1, iovec, count);
332 }
333
334 /*
335  * callback for writing data
336  */
337 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
338 {
339         ssize_t rc;
340         do {
341                 rc = writev(ws->fd, iov, iovcnt);
342         } while(rc == -1 && errno == EINTR);
343         return rc;
344 }
345
346 /*
347  * callback for reading data
348  */
349 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
350 {
351         ssize_t rc;
352         do {
353                 rc = readv(ws->fd, iov, iovcnt);
354         } while(rc == -1 && errno == EINTR);
355         if (rc == 0) {
356                 errno = EPIPE;
357                 rc = -1;
358         }
359         return rc;
360 }
361
362 /*
363  * callback on incoming data
364  */
365 static void aws_on_readable(struct afb_ws *ws)
366 {
367         int rc;
368
369         assert(ws->ws != NULL);
370         rc = websock_dispatch(ws->ws);
371         if (rc < 0 && errno == EPIPE)
372                 afb_ws_hangup(ws);
373 }
374
375 /*
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.
379  */
380 static int aws_read(struct afb_ws *ws, size_t size)
381 {
382         ssize_t sz;
383         char *buffer;
384
385         if (size != 0) {
386                 buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
387                 if (buffer == NULL)
388                         return 0;
389                 ws->buffer.buffer = buffer;
390                 sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
391                 if ((size_t)sz != size)
392                         return 0;
393                 ws->buffer.size += size;
394         }
395         return 1;
396 }
397
398 /*
399  * Callback when 'close' command received from 'ws' with 'code' and 'size'.
400  */
401 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
402 {
403         struct buf b;
404
405         ws->state = waiting;
406         free(aws_pick_buffer(ws).buffer);
407         if (ws->itf->on_close == NULL) {
408                 websock_drop(ws->ws);
409                 afb_ws_hangup(ws);
410         } else if (!aws_read(ws, size))
411                 ws->itf->on_close(ws->closure, code, NULL, 0);
412         else {
413                 b = aws_pick_buffer(ws);
414                 ws->itf->on_close(ws->closure, code, b.buffer, b.size);
415         }
416 }
417
418 /*
419  * Drops any incoming data and send an error of 'code'
420  */
421 static void aws_drop_error(struct afb_ws *ws, uint16_t code)
422 {
423         ws->state = waiting;
424         free(aws_pick_buffer(ws).buffer);
425         websock_drop(ws->ws);
426         websock_error(ws->ws, code, NULL, 0);
427 }
428
429 /*
430  * Reads either text or binary data of 'size' from 'ws' eventually 'last'.
431  */
432 static void aws_continue(struct afb_ws *ws, int last, size_t size)
433 {
434         struct buf b;
435         int istxt;
436
437         if (!aws_read(ws, size))
438                 aws_drop_error(ws, WEBSOCKET_CODE_ABNORMAL);
439         else if (last) {
440                 istxt = ws->state == reading_text;
441                 ws->state = waiting;
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);
445         }
446 }
447
448 /*
449  * Callback when 'text' message received from 'ws' with 'size' and possibly 'last'.
450  */
451 static void aws_on_text(struct afb_ws *ws, int last, size_t size)
452 {
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);
457         else {
458                 ws->state = reading_text;
459                 aws_continue(ws, last, size);
460         }
461 }
462
463 /*
464  * Callback when 'binary' message received from 'ws' with 'size' and possibly 'last'.
465  */
466 static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
467 {
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);
472         else {
473                 ws->state = reading_binary;
474                 aws_continue(ws, last, size);
475         }
476 }
477
478 /*
479  * Callback when 'close' command received from 'ws' with 'code' and 'size'.
480  */
481 static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
482 {
483         if (ws->state == waiting)
484                 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
485         else
486                 aws_continue(ws, last, size);
487 }
488
489 /*
490  * Callback when 'close' command is sent to 'ws' with 'code' and 'size'.
491  */
492 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size)
493 {
494         if (ws->itf->on_error != NULL)
495                 ws->itf->on_error(ws->closure, code, data, size);
496         else
497                 afb_ws_hangup(ws);
498 }
499
500