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