Fedora 30 packaging fix issu
[src/app-framework-binder.git] / src / afb-ws.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "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 #include <poll.h>
28
29 #include "websock.h"
30 #include "afb-ws.h"
31 #include "fdev.h"
32
33 /*
34  * declaration of the websock interface for afb-ws
35  */
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);
44
45 static struct websock_itf aws_itf = {
46         .writev = (void*)aws_writev,
47         .readv = (void*)aws_readv,
48
49         .on_ping = NULL,
50         .on_pong = NULL,
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,
55         .on_extension = NULL,
56
57         .on_error = (void*)aws_on_error
58 };
59
60 /*
61  * a common scheme of buffer handling
62  */
63 struct buf
64 {
65         char *buffer;
66         size_t size;
67 };
68
69 /*
70  * the state
71  */
72 enum state
73 {
74         waiting,
75         reading_text,
76         reading_binary
77 };
78
79 /*
80  * the afb_ws structure
81  */
82 struct afb_ws
83 {
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 */
91 };
92
93 /*
94  * Returns the current buffer of 'ws' that is reset.
95  */
96 static inline struct buf aws_pick_buffer(struct afb_ws *ws)
97 {
98         struct buf result = ws->buffer;
99         if (result.buffer)
100                 result.buffer[result.size] = 0;
101         ws->buffer.buffer = NULL;
102         ws->buffer.size = 0;
103         return result;
104 }
105
106 /*
107  * Clear the current buffer
108  */
109 static inline void aws_clear_buffer(struct afb_ws *ws)
110 {
111         ws->buffer.size = 0;
112 }
113
114 /*
115  * Disconnect the websocket 'ws' and calls on_hangup if
116  * 'call_on_hangup' is not null.
117  */
118 static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
119 {
120         struct websock *wsi = ws->ws;
121         if (wsi != NULL) {
122                 ws->ws = NULL;
123                 fdev_unref(ws->fdev);
124                 websock_destroy(wsi);
125                 free(ws->buffer.buffer);
126                 ws->state = waiting;
127                 if (call_on_hangup && ws->itf->on_hangup)
128                         ws->itf->on_hangup(ws->closure);
129         }
130 }
131
132 static void fdevcb(void *ws, uint32_t revents, struct fdev *fdev)
133 {
134         if ((revents & EPOLLIN) != 0)
135                 aws_on_readable(ws);
136         if ((revents & EPOLLHUP) != 0)
137                 afb_ws_hangup(ws);
138 }
139
140 /*
141  * Creates the afb_ws structure for the file descritor
142  * 'fd' and the callbacks described by the interface 'itf'
143  * and its 'closure'.
144  * When the creation is a success, the systemd event loop 'eloop' is
145  * used for handling event for 'fd'.
146  *
147  * Returns the handle for the afb_ws created or NULL on error.
148  */
149 struct afb_ws *afb_ws_create(struct fdev *fdev, const struct afb_ws_itf *itf, void *closure)
150 {
151         struct afb_ws *result;
152
153         assert(fdev);
154
155         /* allocation */
156         result = malloc(sizeof * result);
157         if (result == NULL)
158                 goto error;
159
160         /* init */
161         result->fdev = fdev;
162         result->fd = fdev_fd(fdev);
163         result->state = waiting;
164         result->itf = itf;
165         result->closure = closure;
166         result->buffer.buffer = NULL;
167         result->buffer.size = 0;
168
169         /* creates the websocket */
170         result->ws = websock_create_v13(&aws_itf, result);
171         if (result->ws == NULL)
172                 goto error2;
173
174         /* finalize */
175         fdev_set_events(fdev, EPOLLIN);
176         fdev_set_callback(fdev, fdevcb, result);
177         return result;
178
179 error2:
180         free(result);
181 error:
182         fdev_unref(fdev);
183         return NULL;
184 }
185
186 /*
187  * Destroys the websocket 'ws'
188  * It first hangup (but without calling on_hangup for safety reasons)
189  * if needed.
190  */
191 void afb_ws_destroy(struct afb_ws *ws)
192 {
193         aws_disconnect(ws, 0);
194         free(ws);
195 }
196
197 /*
198  * Hangup the websocket 'ws'
199  */
200 void afb_ws_hangup(struct afb_ws *ws)
201 {
202         aws_disconnect(ws, 1);
203 }
204
205 /*
206  * Is the websocket 'ws' still connected ?
207  */
208 int afb_ws_is_connected(struct afb_ws *ws)
209 {
210         return ws->ws != NULL;
211 }
212
213 /*
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
216  * characters).
217  * Returns 0 on success or -1 in case of error.
218  */
219 int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason)
220 {
221         if (ws->ws == NULL) {
222                 /* disconnected */
223                 errno = EPIPE;
224                 return -1;
225         }
226         return websock_close(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
227 }
228
229 /*
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
232  * characters).
233  * Raise an error after 'close' command is sent.
234  * Returns 0 on success or -1 in case of error.
235  */
236 int afb_ws_error(struct afb_ws *ws, uint16_t code, const char *reason)
237 {
238         if (ws->ws == NULL) {
239                 /* disconnected */
240                 errno = EPIPE;
241                 return -1;
242         }
243         return websock_error(ws->ws, code, reason, reason == NULL ? 0 : strlen(reason));
244 }
245
246 /*
247  * Sends a 'text' of 'length' to the endpoint of 'ws'.
248  * Returns 0 on success or -1 in case of error.
249  */
250 int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
251 {
252         if (ws->ws == NULL) {
253                 /* disconnected */
254                 errno = EPIPE;
255                 return -1;
256         }
257         return websock_text(ws->ws, 1, text, length);
258 }
259
260 /*
261  * Sends a variable list of texts to the endpoint of 'ws'.
262  * Returns 0 on success or -1 in case of error.
263  */
264 int afb_ws_texts(struct afb_ws *ws, ...)
265 {
266         va_list args;
267         struct iovec ios[32];
268         int count;
269         const char *s;
270
271         if (ws->ws == NULL) {
272                 /* disconnected */
273                 errno = EPIPE;
274                 return -1;
275         }
276
277         count = 0;
278         va_start(args, ws);
279         s = va_arg(args, const char *);
280         while (s != NULL) {
281                 if (count == 32) {
282                         errno = EINVAL;
283                         return -1;
284                 }
285                 ios[count].iov_base = (void*)s;
286                 ios[count].iov_len = strlen(s);
287                 count++;
288                 s = va_arg(args, const char *);
289         }
290         va_end(args);
291         return websock_text_v(ws->ws, 1, ios, count);
292 }
293
294 /*
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.
297  */
298 int afb_ws_text_v(struct afb_ws *ws, const struct iovec *iovec, int count)
299 {
300         if (ws->ws == NULL) {
301                 /* disconnected */
302                 errno = EPIPE;
303                 return -1;
304         }
305         return websock_text_v(ws->ws, 1, iovec, count);
306 }
307
308 /*
309  * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
310  * Returns 0 on success or -1 in case of error.
311  */
312 int afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
313 {
314         if (ws->ws == NULL) {
315                 /* disconnected */
316                 errno = EPIPE;
317                 return -1;
318         }
319         return websock_binary(ws->ws, 1, data, length);
320 }
321
322 /*
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.
325  */
326 int afb_ws_binary_v(struct afb_ws *ws, const struct iovec *iovec, int count)
327 {
328         if (ws->ws == NULL) {
329                 /* disconnected */
330                 errno = EPIPE;
331                 return -1;
332         }
333         return websock_binary_v(ws->ws, 1, iovec, count);
334 }
335
336 /*
337  * callback for writing data
338  */
339 static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
340 {
341         int i;
342         ssize_t rc, sz, dsz;
343         struct iovec *iov2;
344         struct pollfd pfd;
345
346         /* compute the size */
347         dsz = 0;
348         i = 0;
349         while (i < iovcnt) {
350                 dsz += iov[i++].iov_len;
351                 if (dsz < 0) {
352                         errno = EINVAL;
353                         return -1;
354                 }
355         }
356         if (dsz == 0)
357                 return 0;
358
359         /* write the data */
360         iov2 = (struct iovec*)iov;
361         sz = dsz;
362         for (;;) {
363                 rc = writev(ws->fd, iov2, iovcnt);
364                 if (rc < 0) {
365                         if (errno == EINTR)
366                                 continue;
367                         if (errno != EAGAIN)
368                                 return -1;
369                 } else {
370                         dsz -= rc;
371                         if (dsz == 0)
372                                 return sz;
373
374                         i = 0;
375                         while (rc >= (ssize_t)iov2[i].iov_len)
376                                 rc -= (ssize_t)iov2[i++].iov_len;
377
378                         iovcnt -= i;
379                         if (iov2 != iov)
380                                 iov2 += i;
381                         else {
382                                 iov += i;
383                                 iov2 = alloca(iovcnt * sizeof *iov2);
384                                 for (i = 0 ; i < iovcnt ; i++)
385                                         iov2[i] = iov[i];
386                         }
387                         iov2->iov_base += rc;
388                         iov2->iov_len -= rc;
389                 }
390                 pfd.fd = ws->fd;
391                 pfd.events = POLLOUT;
392                 poll(&pfd, 1, 10);
393         }
394 }
395
396 /*
397  * callback for reading data
398  */
399 static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
400 {
401         ssize_t rc;
402         do {
403                 rc = readv(ws->fd, iov, iovcnt);
404         } while(rc == -1 && errno == EINTR);
405         if (rc == 0) {
406                 errno = EPIPE;
407                 rc = -1;
408         }
409         return rc;
410 }
411
412 /*
413  * callback on incoming data
414  */
415 static void aws_on_readable(struct afb_ws *ws)
416 {
417         int rc;
418
419         assert(ws->ws != NULL);
420         rc = websock_dispatch(ws->ws, 0);
421         if (rc < 0 && errno == EPIPE)
422                 afb_ws_hangup(ws);
423 }
424
425 /*
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.
429  */
430 static int aws_read(struct afb_ws *ws, size_t size)
431 {
432         struct pollfd pfd;
433         ssize_t sz;
434         char *buffer;
435
436         if (size != 0 || ws->buffer.buffer == NULL) {
437                 buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
438                 if (buffer == NULL)
439                         return 0;
440                 ws->buffer.buffer = buffer;
441                 while (size != 0) {
442                         sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
443                         if (sz < 0) {
444                                 if (errno != EAGAIN)
445                                         return 0;
446                                 pfd.fd = ws->fd;
447                                 pfd.events = POLLIN;
448                                 poll(&pfd, 1, 10); /* TODO: make fully asynchronous websockets */
449                         } else {
450                                 ws->buffer.size += (size_t)sz;
451                                 size -= (size_t)sz;
452                         }
453                 }
454         }
455         return 1;
456 }
457
458 /*
459  * Callback when 'close' command received from 'ws' with 'code' and 'size'.
460  */
461 static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
462 {
463         struct buf b;
464
465         ws->state = waiting;
466         aws_clear_buffer(ws);
467         if (ws->itf->on_close == NULL) {
468                 websock_drop(ws->ws);
469                 afb_ws_hangup(ws);
470         } else if (!aws_read(ws, size))
471                 ws->itf->on_close(ws->closure, code, NULL, 0);
472         else {
473                 b = aws_pick_buffer(ws);
474                 ws->itf->on_close(ws->closure, code, b.buffer, b.size);
475         }
476 }
477
478 /*
479  * Drops any incoming data and send an error of 'code'
480  */
481 static void aws_drop_error(struct afb_ws *ws, uint16_t code)
482 {
483         ws->state = waiting;
484         aws_clear_buffer(ws);
485         websock_drop(ws->ws);
486         websock_error(ws->ws, code, NULL, 0);
487 }
488
489 /*
490  * Reads either text or binary data of 'size' from 'ws' eventually 'last'.
491  */
492 static void aws_continue(struct afb_ws *ws, int last, size_t size)
493 {
494         struct buf b;
495         int istxt;
496
497         if (!aws_read(ws, size))
498                 aws_drop_error(ws, WEBSOCKET_CODE_ABNORMAL);
499         else if (last) {
500                 istxt = ws->state == reading_text;
501                 ws->state = waiting;
502                 b = aws_pick_buffer(ws);
503                 (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size);
504         }
505 }
506
507 /*
508  * Callback when 'text' message received from 'ws' with 'size' and possibly 'last'.
509  */
510 static void aws_on_text(struct afb_ws *ws, int last, size_t size)
511 {
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);
516         else {
517                 ws->state = reading_text;
518                 aws_continue(ws, last, size);
519         }
520 }
521
522 /*
523  * Callback when 'binary' message received from 'ws' with 'size' and possibly 'last'.
524  */
525 static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
526 {
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);
531         else {
532                 ws->state = reading_binary;
533                 aws_continue(ws, last, size);
534         }
535 }
536
537 /*
538  * Callback when 'continue' command received from 'ws' with 'code' and 'size'.
539  */
540 static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
541 {
542         if (ws->state == waiting)
543                 aws_drop_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
544         else
545                 aws_continue(ws, last, size);
546 }
547
548 /*
549  * Callback when 'close' command is sent to 'ws' with 'code' and 'size'.
550  */
551 static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, size_t size)
552 {
553         if (ws->itf->on_error != NULL)
554                 ws->itf->on_error(ws->closure, code, data, size);
555         else
556                 afb_ws_hangup(ws);
557 }
558
559