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