plat-optee and boringssl adaptations
[platform/upstream/libwebsockets.git] / lib / service.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 static int
25 lws_calllback_as_writeable(struct lws *wsi)
26 {
27         int n;
28
29         switch (wsi->mode) {
30         case LWSCM_WS_CLIENT:
31                 n = LWS_CALLBACK_CLIENT_WRITEABLE;
32                 break;
33         case LWSCM_WSCL_ISSUE_HTTP_BODY:
34                 n = LWS_CALLBACK_CLIENT_HTTP_WRITEABLE;
35                 break;
36         case LWSCM_WS_SERVING:
37                 n = LWS_CALLBACK_SERVER_WRITEABLE;
38                 break;
39         default:
40                 n = LWS_CALLBACK_HTTP_WRITEABLE;
41                 break;
42         }
43         lwsl_debug("%s: %p (user=%p)\n", __func__, wsi, wsi->user_space);
44         return user_callback_handle_rxflow(wsi->protocol->callback,
45                                            wsi, (enum lws_callback_reasons) n,
46                                            wsi->user_space, NULL, 0);
47 }
48
49 int
50 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd)
51 {
52         int write_type = LWS_WRITE_PONG;
53         struct lws_tokens eff_buf;
54 #ifdef LWS_USE_HTTP2
55         struct lws *wsi2;
56 #endif
57         int ret, m, n;
58
59         //lwsl_err("%s: %p\n", __func__, wsi);
60
61         /*
62          * user callback is lowest priority to get these notifications
63          * actually, since other pending things cannot be disordered
64          */
65
66         /* Priority 1: pending truncated sends are incomplete ws fragments
67          *             If anything else sent first the protocol would be
68          *             corrupted.
69          */
70         if (wsi->trunc_len) {
71                 if (lws_issue_raw(wsi, wsi->trunc_alloc + wsi->trunc_offset,
72                                   wsi->trunc_len) < 0) {
73                         lwsl_info("%s signalling to close\n", __func__);
74                         return -1;
75                 }
76                 /* leave POLLOUT active either way */
77                 return 0;
78         } else
79                 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)
80                         return -1; /* retry closing now */
81
82         if (wsi->mode == LWSCM_WSCL_ISSUE_HTTP_BODY)
83                 goto user_service;
84
85
86 #ifdef LWS_USE_HTTP2
87         /* Priority 2: protocol packets
88          */
89         if (wsi->pps) {
90                 lwsl_info("servicing pps %d\n", wsi->pps);
91                 switch (wsi->pps) {
92                 case LWS_PPS_HTTP2_MY_SETTINGS:
93                 case LWS_PPS_HTTP2_ACK_SETTINGS:
94                         lws_http2_do_pps_send(lws_get_context(wsi), wsi);
95                         break;
96                 default:
97                         break;
98                 }
99                 wsi->pps = LWS_PPS_NONE;
100                 lws_rx_flow_control(wsi, 1);
101
102                 return 0; /* leave POLLOUT active */
103         }
104 #endif
105
106 #ifdef LWS_WITH_CGI
107         if (wsi->cgi)
108                 goto user_service_go_again;
109 #endif
110
111         /* Priority 3: pending control packets (pong or close)
112          */
113         if ((wsi->state == LWSS_ESTABLISHED &&
114              wsi->u.ws.ping_pending_flag) ||
115             (wsi->state == LWSS_RETURNED_CLOSE_ALREADY &&
116              wsi->u.ws.payload_is_close)) {
117
118                 if (wsi->u.ws.payload_is_close)
119                         write_type = LWS_WRITE_CLOSE;
120
121                 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
122                               wsi->u.ws.ping_payload_len, write_type);
123                 if (n < 0)
124                         return -1;
125
126                 /* well he is sent, mark him done */
127                 wsi->u.ws.ping_pending_flag = 0;
128                 if (wsi->u.ws.payload_is_close)
129                         /* oh... a close frame was it... then we are done */
130                         return -1;
131
132                 /* otherwise for PING, leave POLLOUT active either way */
133                 return 0;
134         }
135
136         if (wsi->state == LWSS_ESTABLISHED &&
137             !wsi->socket_is_permanently_unusable &&
138             wsi->u.ws.send_check_ping) {
139
140                 lwsl_info("issuing ping on wsi %p\n", wsi);
141                 wsi->u.ws.send_check_ping = 0;
142                 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
143                               0, LWS_WRITE_PING);
144                 if (n < 0)
145                         return -1;
146
147                 /*
148                  * we apparently were able to send the PING in a reasonable time
149                  * now reset the clock on our peer to be able to send the
150                  * PONG in a reasonable time.
151                  */
152
153                 lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG,
154                                 wsi->context->timeout_secs);
155
156                 return 0;
157         }
158
159         /* Priority 4: if we are closing, not allowed to send more data frags
160          *             which means user callback or tx ext flush banned now
161          */
162         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
163                 goto user_service;
164
165         /* Priority 5: Tx path extension with more to send
166          *
167          *             These are handled as new fragments each time around
168          *             So while we must block new writeable callback to enforce
169          *             payload ordering, but since they are always complete
170          *             fragments control packets can interleave OK.
171          */
172         if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
173                 lwsl_ext("SERVICING TX EXT DRAINING\n");
174                 if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
175                         return -1;
176                 /* leave POLLOUT active */
177                 return 0;
178         }
179
180         /* Priority 6: user can get the callback
181          */
182         m = lws_ext_cb_active(wsi, LWS_EXT_CB_IS_WRITEABLE, NULL, 0);
183         if (m)
184                 return -1;
185 #ifndef LWS_NO_EXTENSIONS
186         if (!wsi->extension_data_pending)
187                 goto user_service;
188 #endif
189         /*
190          * check in on the active extensions, see if they
191          * had pending stuff to spill... they need to get the
192          * first look-in otherwise sequence will be disordered
193          *
194          * NULL, zero-length eff_buf means just spill pending
195          */
196
197         ret = 1;
198         while (ret == 1) {
199
200                 /* default to nobody has more to spill */
201
202                 ret = 0;
203                 eff_buf.token = NULL;
204                 eff_buf.token_len = 0;
205
206                 /* give every extension a chance to spill */
207
208                 m = lws_ext_cb_active(wsi,
209                                         LWS_EXT_CB_PACKET_TX_PRESEND,
210                                                &eff_buf, 0);
211                 if (m < 0) {
212                         lwsl_err("ext reports fatal error\n");
213                         return -1;
214                 }
215                 if (m)
216                         /*
217                          * at least one extension told us he has more
218                          * to spill, so we will go around again after
219                          */
220                         ret = 1;
221
222                 /* assuming they gave us something to send, send it */
223
224                 if (eff_buf.token_len) {
225                         n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
226                                           eff_buf.token_len);
227                         if (n < 0) {
228                                 lwsl_info("closing from POLLOUT spill\n");
229                                 return -1;
230                         }
231                         /*
232                          * Keep amount spilled small to minimize chance of this
233                          */
234                         if (n != eff_buf.token_len) {
235                                 lwsl_err("Unable to spill ext %d vs %s\n",
236                                                           eff_buf.token_len, n);
237                                 return -1;
238                         }
239                 } else
240                         continue;
241
242                 /* no extension has more to spill */
243
244                 if (!ret)
245                         continue;
246
247                 /*
248                  * There's more to spill from an extension, but we just sent
249                  * something... did that leave the pipe choked?
250                  */
251
252                 if (!lws_send_pipe_choked(wsi))
253                         /* no we could add more */
254                         continue;
255
256                 lwsl_info("choked in POLLOUT service\n");
257
258                 /*
259                  * Yes, he's choked.  Leave the POLLOUT masked on so we will
260                  * come back here when he is unchoked.  Don't call the user
261                  * callback to enforce ordering of spilling, he'll get called
262                  * when we come back here and there's nothing more to spill.
263                  */
264
265                 return 0;
266         }
267 #ifndef LWS_NO_EXTENSIONS
268         wsi->extension_data_pending = 0;
269 #endif
270 user_service:
271         /* one shot */
272
273         if (pollfd)
274                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
275                         lwsl_info("failed at set pollfd\n");
276                         return 1;
277                 }
278
279         if (wsi->mode != LWSCM_WSCL_ISSUE_HTTP_BODY &&
280             !wsi->hdr_parsing_completed)
281                 return 0;
282
283 #ifdef LWS_WITH_CGI
284 user_service_go_again:
285 #endif
286
287 #ifdef LWS_USE_HTTP2
288         /*
289          * we are the 'network wsi' for potentially many muxed child wsi with
290          * no network connection of their own, who have to use us for all their
291          * network actions.  So we use a round-robin scheme to share out the
292          * POLLOUT notifications to our children.
293          *
294          * But because any child could exhaust the socket's ability to take
295          * writes, we can only let one child get notified each time.
296          *
297          * In addition children may be closed / deleted / added between POLLOUT
298          * notifications, so we can't hold pointers
299          */
300
301         if (wsi->mode != LWSCM_HTTP2_SERVING) {
302                 lwsl_info("%s: non http2\n", __func__);
303                 goto notify;
304         }
305
306         wsi->u.http2.requested_POLLOUT = 0;
307         if (!wsi->u.http2.initialized) {
308                 lwsl_info("pollout on uninitialized http2 conn\n");
309                 return 0;
310         }
311
312         lwsl_info("%s: doing children\n", __func__);
313
314         wsi2 = wsi;
315         do {
316                 wsi2 = wsi2->u.http2.next_child_wsi;
317                 lwsl_info("%s: child %p\n", __func__, wsi2);
318                 if (!wsi2)
319                         continue;
320                 if (!wsi2->u.http2.requested_POLLOUT)
321                         continue;
322                 wsi2->u.http2.requested_POLLOUT = 0;
323                 if (lws_calllback_as_writeable(wsi2)) {
324                         lwsl_debug("Closing POLLOUT child\n");
325                         lws_close_free_wsi(wsi2, LWS_CLOSE_STATUS_NOSTATUS);
326                 }
327                 wsi2 = wsi;
328         } while (wsi2 != NULL && !lws_send_pipe_choked(wsi));
329
330         lwsl_info("%s: completed\n", __func__);
331
332         return 0;
333 notify:
334 #endif
335         return lws_calllback_as_writeable(wsi);
336 }
337
338 int
339 lws_service_timeout_check(struct lws *wsi, unsigned int sec)
340 {
341 //#if LWS_POSIX
342         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
343         int n = 0;
344 //#endif
345
346         (void)n;
347
348         /*
349          * if extensions want in on it (eg, we are a mux parent)
350          * give them a chance to service child timeouts
351          */
352         if (lws_ext_cb_active(wsi, LWS_EXT_CB_1HZ, NULL, sec) < 0)
353                 return 0;
354
355         if (!wsi->pending_timeout)
356                 return 0;
357
358         /*
359          * if we went beyond the allowed time, kill the
360          * connection
361          */
362         if ((time_t)sec > wsi->pending_timeout_limit) {
363 //#if LWS_POSIX
364                 if (wsi->sock != LWS_SOCK_INVALID && wsi->position_in_fds_table >= 0)
365                         n = pt->fds[wsi->position_in_fds_table].events;
366
367                 /* no need to log normal idle keepalive timeout */
368                 if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
369                         lwsl_notice("wsi %p: TIMEDOUT WAITING on %d (did hdr %d, ah %p, wl %d, pfd events %d) %llu vs %llu\n",
370                             (void *)wsi, wsi->pending_timeout,
371                             wsi->hdr_parsing_completed, wsi->u.hdr.ah,
372                             pt->ah_wait_list_length, n, (unsigned long long)sec, (unsigned long long)wsi->pending_timeout_limit);
373 //#endif
374                 /*
375                  * Since he failed a timeout, he already had a chance to do
376                  * something and was unable to... that includes situations like
377                  * half closed connections.  So process this "failed timeout"
378                  * close as a violent death and don't try to do protocol
379                  * cleanup like flush partials.
380                  */
381                 wsi->socket_is_permanently_unusable = 1;
382                 if (wsi->mode == LWSCM_WSCL_WAITING_SSL)
383                         wsi->vhost->protocols[0].callback(wsi,
384                                 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
385                                 wsi->user_space, (void *)"Timed out waiting SSL", 21);
386
387                 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
388
389                 return 1;
390         }
391
392         return 0;
393 }
394
395 int lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len)
396 {
397         /* his RX is flowcontrolled, don't send remaining now */
398         if (wsi->rxflow_buffer) {
399                 /* rxflow while we were spilling prev rxflow */
400                 lwsl_info("stalling in existing rxflow buf\n");
401                 return 1;
402         }
403
404         /* a new rxflow, buffer it and warn caller */
405         lwsl_info("new rxflow input buffer len %d\n", len - n);
406         wsi->rxflow_buffer = lws_malloc(len - n);
407         if (!wsi->rxflow_buffer)
408                 return -1;
409         wsi->rxflow_len = len - n;
410         wsi->rxflow_pos = 0;
411         memcpy(wsi->rxflow_buffer, buf + n, len - n);
412
413         return 0;
414 }
415
416 /* this is used by the platform service code to stop us waiting for network
417  * activity in poll() when we have something that already needs service
418  */
419
420 LWS_VISIBLE LWS_EXTERN int
421 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi)
422 {
423         struct lws_context_per_thread *pt = &context->pt[tsi];
424         int n;
425
426         /* Figure out if we really want to wait in poll()
427          * We only need to wait if really nothing already to do and we have
428          * to wait for something from network
429          */
430
431         /* 1) if we know we are draining rx ext, do not wait in poll */
432         if (pt->rx_draining_ext_list)
433                 return 0;
434
435 #ifdef LWS_OPENSSL_SUPPORT
436         /* 2) if we know we have non-network pending data, do not wait in poll */
437         if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
438                 lwsl_info("ssl buffered read\n");
439                 return 0;
440         }
441 #endif
442
443         /* 3) if any ah has pending rx, do not wait in poll */
444         for (n = 0; n < context->max_http_header_pool; n++)
445                 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen) {
446                         /* any ah with pending rx must be attached to someone */
447                         if (!pt->ah_pool[n].wsi) {
448                                 lwsl_err("%s: assert: no wsi attached to ah\n", __func__);
449                                 assert(0);
450                         }
451                         return 0;
452                 }
453
454         return timeout_ms;
455 }
456
457 /*
458  * guys that need POLLIN service again without waiting for network action
459  * can force POLLIN here if not flowcontrolled, so they will get service.
460  *
461  * Return nonzero if anybody got their POLLIN faked
462  */
463 int
464 lws_service_flag_pending(struct lws_context *context, int tsi)
465 {
466         struct lws_context_per_thread *pt = &context->pt[tsi];
467 #ifdef LWS_OPENSSL_SUPPORT
468         struct lws *wsi_next;
469 #endif
470         struct lws *wsi;
471         int forced = 0;
472         int n;
473
474         /* POLLIN faking */
475
476         /*
477          * 1) For all guys with already-available ext data to drain, if they are
478          * not flowcontrolled, fake their POLLIN status
479          */
480         wsi = pt->rx_draining_ext_list;
481         while (wsi) {
482                 pt->fds[wsi->position_in_fds_table].revents |=
483                         pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
484                 if (pt->fds[wsi->position_in_fds_table].revents &
485                     LWS_POLLIN)
486                         forced = 1;
487                 wsi = wsi->u.ws.rx_draining_ext_list;
488         }
489
490 #ifdef LWS_OPENSSL_SUPPORT
491         /*
492          * 2) For all guys with buffered SSL read data already saved up, if they
493          * are not flowcontrolled, fake their POLLIN status so they'll get
494          * service to use up the buffered incoming data, even though their
495          * network socket may have nothing
496          */
497         wsi = pt->pending_read_list;
498         while (wsi) {
499                 wsi_next = wsi->pending_read_list_next;
500                 pt->fds[wsi->position_in_fds_table].revents |=
501                         pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
502                 if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
503                         forced = 1;
504                         /*
505                          * he's going to get serviced now, take him off the
506                          * list of guys with buffered SSL.  If he still has some
507                          * at the end of the service, he'll get put back on the
508                          * list then.
509                          */
510                         lws_ssl_remove_wsi_from_buffered_list(wsi);
511                 }
512
513                 wsi = wsi_next;
514         }
515 #endif
516         /*
517          * 3) For any wsi who have an ah with pending RX who did not
518          * complete their current headers, and are not flowcontrolled,
519          * fake their POLLIN status so they will be able to drain the
520          * rx buffered in the ah
521          */
522         for (n = 0; n < context->max_http_header_pool; n++)
523                 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen &&
524                     !pt->ah_pool[n].wsi->hdr_parsing_completed) {
525                         pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents |=
526                                 pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].events &
527                                         LWS_POLLIN;
528                         if (pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents &
529                             LWS_POLLIN)
530                                 forced = 1;
531                 }
532
533         return forced;
534 }
535
536 #ifndef LWS_NO_CLIENT
537
538 LWS_VISIBLE int
539 lws_http_client_read(struct lws *wsi, char **buf, int *len)
540 {
541         int rlen, n;
542
543         rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
544         *len = 0;
545
546         /* allow the source to signal he has data again next time */
547         lws_change_pollfd(wsi, 0, LWS_POLLIN);
548
549         if (rlen == LWS_SSL_CAPABLE_ERROR) {
550                 lwsl_notice("%s: SSL capable error\n", __func__);
551                 return -1;
552         }
553
554         if (rlen <= 0)
555                 return 0;
556
557         *len = rlen;
558         wsi->client_rx_avail = 0;
559
560         /*
561          * server may insist on transfer-encoding: chunked,
562          * so http client must deal with it
563          */
564 spin_chunks:
565         while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
566                 switch (wsi->chunk_parser) {
567                 case ELCP_HEX:
568                         if ((*buf)[0] == '\x0d') {
569                                 wsi->chunk_parser = ELCP_CR;
570                                 break;
571                         }
572                         n = char_to_hex((*buf)[0]);
573                         if (n < 0) {
574                                 lwsl_debug("chunking failure\n");
575                                 return -1;
576                         }
577                         wsi->chunk_remaining <<= 4;
578                         wsi->chunk_remaining |= n;
579                         break;
580                 case ELCP_CR:
581                         if ((*buf)[0] != '\x0a') {
582                                 lwsl_debug("chunking failure\n");
583                                 return -1;
584                         }
585                         wsi->chunk_parser = ELCP_CONTENT;
586                         lwsl_info("chunk %d\n", wsi->chunk_remaining);
587                         if (wsi->chunk_remaining)
588                                 break;
589                         lwsl_info("final chunk\n");
590                         goto completed;
591
592                 case ELCP_CONTENT:
593                         break;
594
595                 case ELCP_POST_CR:
596                         if ((*buf)[0] != '\x0d') {
597                                 lwsl_debug("chunking failure\n");
598
599                                 return -1;
600                         }
601
602                         wsi->chunk_parser = ELCP_POST_LF;
603                         break;
604
605                 case ELCP_POST_LF:
606                         if ((*buf)[0] != '\x0a')
607                                 return -1;
608
609                         wsi->chunk_parser = ELCP_HEX;
610                         wsi->chunk_remaining = 0;
611                         break;
612                 }
613                 (*buf)++;
614                 (*len)--;
615         }
616
617         if (wsi->chunked && !wsi->chunk_remaining)
618                 return 0;
619
620         if (wsi->u.http.content_remain &&
621             (int)wsi->u.http.content_remain < *len)
622                 n = wsi->u.http.content_remain;
623         else
624                 n = *len;
625
626         if (wsi->chunked && wsi->chunk_remaining &&
627             wsi->chunk_remaining < n)
628                 n = wsi->chunk_remaining;
629
630 #ifdef LWS_WITH_HTTP_PROXY
631         /* hubbub */
632         if (wsi->perform_rewrite)
633                 lws_rewrite_parse(wsi->rw, (unsigned char *)*buf, n);
634         else
635 #endif
636                 if (user_callback_handle_rxflow(wsi->protocol->callback,
637                                 wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
638                                 wsi->user_space, *buf, n)) {
639                         lwsl_debug("%s: LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ returned -1\n", __func__);
640
641                         return -1;
642                 }
643
644         if (wsi->chunked && wsi->chunk_remaining) {
645                 (*buf) += n;
646                 wsi->chunk_remaining -= n;
647                 *len -= n;
648         }
649
650         if (wsi->chunked && !wsi->chunk_remaining)
651                 wsi->chunk_parser = ELCP_POST_CR;
652
653         if (wsi->chunked && *len) {
654                 goto spin_chunks;
655         }
656
657         if (wsi->chunked)
658                 return 0;
659
660         wsi->u.http.content_remain -= n;
661         if (wsi->u.http.content_remain || !wsi->u.http.content_length)
662                 return 0;
663
664 completed:
665         if (user_callback_handle_rxflow(wsi->protocol->callback,
666                         wsi, LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
667                         wsi->user_space, NULL, 0)) {
668                 lwsl_debug("Completed call returned -1\n");
669                 return -1;
670         }
671
672         if (lws_http_transaction_completed_client(wsi)) {
673                 lwsl_notice("%s: transaction completed says -1\n", __func__);
674                 return -1;
675         }
676
677         return 0;
678 }
679 #endif
680
681 LWS_VISIBLE int
682 lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi)
683 {
684         struct lws_context_per_thread *pt = &context->pt[tsi];
685         lws_sockfd_type our_fd = 0, tmp_fd;
686         struct lws_tokens eff_buf;
687         unsigned int pending = 0;
688         struct lws *wsi, *wsi1;
689         char draining_flow = 0;
690         int timed_out = 0;
691         time_t now;
692         int n = 0, m;
693         int more;
694
695         if (!context->protocol_init_done)
696                 lws_protocol_init(context);
697
698         time(&now);
699
700         /*
701          * handle case that system time was uninitialized when lws started
702          * at boot, and got initialized a little later
703          */
704         if (context->time_up < 1464083026 && now > 1464083026)
705                 context->time_up = now;
706
707         /* TODO: if using libev, we should probably use timeout watchers... */
708         if (context->last_timeout_check_s != now) {
709                 context->last_timeout_check_s = now;
710
711                 lws_plat_service_periodic(context);
712
713                 /* retire unused deprecated context */
714 #ifndef LWS_PLAT_OPTEE
715 #if LWS_POSIX && !defined(_WIN32)
716                 if (context->deprecated && !context->count_wsi_allocated) {
717                         lwsl_notice("%s: ending deprecated context\n", __func__);
718                         kill(getpid(), SIGINT);
719                         return 0;
720                 }
721 #endif
722 #endif
723                 /* global timeout check once per second */
724
725                 if (pollfd)
726                         our_fd = pollfd->fd;
727
728                 wsi = context->pt[tsi].timeout_list;
729                 while (wsi) {
730                         /* we have to take copies, because he may be deleted */
731                         wsi1 = wsi->timeout_list;
732                         tmp_fd = wsi->sock;
733                         if (lws_service_timeout_check(wsi, (unsigned int)now)) {
734                                 /* he did time out... */
735                                 if (tmp_fd == our_fd)
736                                         /* it was the guy we came to service! */
737                                         timed_out = 1;
738                                         /* he's gone, no need to mark as handled */
739                         }
740                         wsi = wsi1;
741                 }
742 #ifdef LWS_WITH_CGI
743                 lws_cgi_kill_terminated(pt);
744 #endif
745 #if 0
746                 {
747                         char s[300], *p = s;
748
749                         for (n = 0; n < context->count_threads; n++)
750                                 p += sprintf(p, " %7lu (%5d), ",
751                                              context->pt[n].count_conns,
752                                              context->pt[n].fds_count);
753
754                         lwsl_notice("load: %s\n", s);
755                 }
756 #endif
757         }
758
759         /*
760          * at intervals, check for ws connections needing ping-pong checks
761          */
762
763         if (context->ws_ping_pong_interval &&
764             context->last_ws_ping_pong_check_s < now + 10) {
765                 struct lws_vhost *vh = context->vhost_list;
766                 context->last_ws_ping_pong_check_s = now;
767
768                 while (vh) {
769                         for (n = 0; n < vh->count_protocols; n++) {
770                                 wsi = vh->same_vh_protocol_list[n];
771
772                                 while (wsi) {
773                                         if (wsi->state == LWSS_ESTABLISHED &&
774                                             !wsi->socket_is_permanently_unusable &&
775                                             !wsi->u.ws.send_check_ping &&
776                                             wsi->u.ws.time_next_ping_check &&
777                                             wsi->u.ws.time_next_ping_check < now) {
778
779                                                 lwsl_info("requesting ping-pong on wsi %p\n", wsi);
780                                                 wsi->u.ws.send_check_ping = 1;
781                                                 lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING,
782                                                                 context->timeout_secs);
783                                                 lws_callback_on_writable(wsi);
784                                                 wsi->u.ws.time_next_ping_check = now +
785                                                                 wsi->context->ws_ping_pong_interval;
786                                         }
787                                         wsi = wsi->same_vh_protocol_next;
788                                 }
789                         }
790                         vh = vh->vhost_next;
791                 }
792         }
793
794         /* the socket we came to service timed out, nothing to do */
795         if (timed_out)
796                 return 0;
797
798         /* just here for timeout management? */
799         if (!pollfd)
800                 return 0;
801
802         /* no, here to service a socket descriptor */
803         wsi = wsi_from_fd(context, pollfd->fd);
804         if (!wsi)
805                 /* not lws connection ... leave revents alone and return */
806                 return 0;
807
808         /*
809          * so that caller can tell we handled, past here we need to
810          * zero down pollfd->revents after handling
811          */
812
813 #if LWS_POSIX
814         /* handle session socket closed */
815
816         if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
817             (pollfd->revents & LWS_POLLHUP)) {
818                 wsi->socket_is_permanently_unusable = 1;
819                 lwsl_debug("Session Socket %p (fd=%d) dead\n",
820                                                        (void *)wsi, pollfd->fd);
821
822                 goto close_and_handled;
823         }
824
825 #ifdef _WIN32
826         if (pollfd->revents & LWS_POLLOUT)
827                 wsi->sock_send_blocking = FALSE;
828 #endif
829
830 #endif
831
832         lwsl_debug("fd=%d, revents=%d\n", pollfd->fd, pollfd->revents);
833
834         /* okay, what we came here to do... */
835
836         switch (wsi->mode) {
837         case LWSCM_HTTP_SERVING:
838         case LWSCM_HTTP_CLIENT:
839         case LWSCM_HTTP_SERVING_ACCEPTED:
840         case LWSCM_SERVER_LISTENER:
841         case LWSCM_SSL_ACK_PENDING:
842                 if (wsi->state == LWSS_CLIENT_HTTP_ESTABLISHED)
843                         goto handled;
844
845 #ifdef LWS_WITH_CGI
846                 if (wsi->cgi && (pollfd->revents & LWS_POLLOUT)) {
847                         n = lws_handle_POLLOUT_event(wsi, pollfd);
848                         if (n)
849                                 goto close_and_handled;
850                         goto handled;
851                 }
852 #endif
853                 n = lws_server_socket_service(context, wsi, pollfd);
854                 if (n) /* closed by above */
855                         return 1;
856                 goto handled;
857
858         case LWSCM_WS_SERVING:
859         case LWSCM_WS_CLIENT:
860         case LWSCM_HTTP2_SERVING:
861         case LWSCM_HTTP_CLIENT_ACCEPTED:
862
863                 /* 1: something requested a callback when it was OK to write */
864
865                 if ((pollfd->revents & LWS_POLLOUT) &&
866                     (wsi->state == LWSS_ESTABLISHED ||
867                      wsi->state == LWSS_HTTP2_ESTABLISHED ||
868                      wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS ||
869                      wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
870                      wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) &&
871                     lws_handle_POLLOUT_event(wsi, pollfd)) {
872                         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
873                                 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
874                         lwsl_info("lws_service_fd: closing\n");
875                         goto close_and_handled;
876                 }
877
878                 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
879                     wsi->state == LWSS_AWAITING_CLOSE_ACK) {
880                         /*
881                          * we stopped caring about anything except control
882                          * packets.  Force flow control off, defeat tx
883                          * draining.
884                          */
885                         lws_rx_flow_control(wsi, 1);
886                         wsi->u.ws.tx_draining_ext = 0;
887                 }
888
889                 if (wsi->u.ws.tx_draining_ext)
890                         /* we cannot deal with new RX until the TX ext
891                          * path has been drained.  It's because new
892                          * rx will, eg, crap on the wsi rx buf that
893                          * may be needed to retain state.
894                          *
895                          * TX ext drain path MUST go through event loop
896                          * to avoid blocking.
897                          */
898                         break;
899
900                 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
901                         /* We cannot deal with any kind of new RX
902                          * because we are RX-flowcontrolled.
903                          */
904                         break;
905
906                 /* 2: RX Extension needs to be drained
907                  */
908
909                 if (wsi->state == LWSS_ESTABLISHED &&
910                     wsi->u.ws.rx_draining_ext) {
911
912                         lwsl_ext("%s: RX EXT DRAINING: Service\n", __func__);
913 #ifndef LWS_NO_CLIENT
914                         if (wsi->mode == LWSCM_WS_CLIENT) {
915                                 n = lws_client_rx_sm(wsi, 0);
916                                 if (n < 0)
917                                         /* we closed wsi */
918                                         n = 0;
919                         } else
920 #endif
921                                 n = lws_rx_sm(wsi, 0);
922
923                         goto handled;
924                 }
925
926                 if (wsi->u.ws.rx_draining_ext)
927                         /*
928                          * We have RX EXT content to drain, but can't do it
929                          * right now.  That means we cannot do anything lower
930                          * priority either.
931                          */
932                         break;
933
934                 /* 3: RX Flowcontrol buffer needs to be drained
935                  */
936
937                 if (wsi->rxflow_buffer) {
938                         lwsl_info("draining rxflow (len %d)\n",
939                                 wsi->rxflow_len - wsi->rxflow_pos
940                         );
941                         /* well, drain it */
942                         eff_buf.token = (char *)wsi->rxflow_buffer +
943                                                 wsi->rxflow_pos;
944                         eff_buf.token_len = wsi->rxflow_len - wsi->rxflow_pos;
945                         draining_flow = 1;
946                         goto drain;
947                 }
948
949                 /* 4: any incoming (or ah-stashed incoming rx) data ready?
950                  * notice if rx flow going off raced poll(), rx flow wins
951                  */
952
953                 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
954                         break;
955
956 read:
957                 /* all the union members start with hdr, so even in ws mode
958                  * we can deal with the ah via u.hdr
959                  */
960                 if (wsi->u.hdr.ah) {
961                         lwsl_info("%s: %p: inherited ah rx\n", __func__, wsi);
962                         eff_buf.token_len = wsi->u.hdr.ah->rxlen -
963                                             wsi->u.hdr.ah->rxpos;
964                         eff_buf.token = (char *)wsi->u.hdr.ah->rx +
965                                         wsi->u.hdr.ah->rxpos;
966                 } else {
967                         if (wsi->mode != LWSCM_HTTP_CLIENT_ACCEPTED) {
968                                 eff_buf.token_len = lws_ssl_capable_read(wsi,
969                                         pt->serv_buf, pending ? pending :
970                                         context->pt_serv_buf_size);
971                                 switch (eff_buf.token_len) {
972                                 case 0:
973                                         lwsl_info("%s: zero length read\n", __func__);
974                                         goto close_and_handled;
975                                 case LWS_SSL_CAPABLE_MORE_SERVICE:
976                                         lwsl_info("SSL Capable more service\n");
977                                         n = 0;
978                                         goto handled;
979                                 case LWS_SSL_CAPABLE_ERROR:
980                                         lwsl_info("Closing when error\n");
981                                         goto close_and_handled;
982                                 }
983
984                                 eff_buf.token = (char *)pt->serv_buf;
985                         }
986                 }
987
988 drain:
989 #ifndef LWS_NO_CLIENT
990                 if (wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED &&
991                     !wsi->told_user_closed) {
992
993                         /*
994                          * In SSL mode we get POLLIN notification about
995                          * encrypted data in.
996                          *
997                          * But that is not necessarily related to decrypted
998                          * data out becoming available; in may need to perform
999                          * other in or out before that happens.
1000                          *
1001                          * simply mark ourselves as having readable data
1002                          * and turn off our POLLIN
1003                          */
1004                         wsi->client_rx_avail = 1;
1005                         lws_change_pollfd(wsi, LWS_POLLIN, 0);
1006
1007                         /* let user code know, he'll usually ask for writeable
1008                          * callback and drain / re-enable it there
1009                          */
1010                         if (user_callback_handle_rxflow(
1011                                         wsi->protocol->callback,
1012                                         wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
1013                                         wsi->user_space, NULL, 0)) {
1014                                 lwsl_debug("LWS_CALLBACK_RECEIVE_CLIENT_HTTP closed it\n");
1015                                 goto close_and_handled;
1016                         }
1017                 }
1018 #endif
1019                 /*
1020                  * give any active extensions a chance to munge the buffer
1021                  * before parse.  We pass in a pointer to an lws_tokens struct
1022                  * prepared with the default buffer and content length that's in
1023                  * there.  Rather than rewrite the default buffer, extensions
1024                  * that expect to grow the buffer can adapt .token to
1025                  * point to their own per-connection buffer in the extension
1026                  * user allocation.  By default with no extensions or no
1027                  * extension callback handling, just the normal input buffer is
1028                  * used then so it is efficient.
1029                  */
1030                 do {
1031                         more = 0;
1032
1033                         m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_RX_PREPARSE,
1034                                               &eff_buf, 0);
1035                         if (m < 0)
1036                                 goto close_and_handled;
1037                         if (m)
1038                                 more = 1;
1039
1040                         /* service incoming data */
1041
1042                         if (eff_buf.token_len) {
1043                                 /*
1044                                  * if draining from rxflow buffer, not
1045                                  * critical to track what was used since at the
1046                                  * use it bumps wsi->rxflow_pos.  If we come
1047                                  * around again it will pick up from where it
1048                                  * left off.
1049                                  */
1050                                 n = lws_read(wsi, (unsigned char *)eff_buf.token,
1051                                              eff_buf.token_len);
1052                                 if (n < 0) {
1053                                         /* we closed wsi */
1054                                         n = 0;
1055                                         goto handled;
1056                                 }
1057                         }
1058
1059                         eff_buf.token = NULL;
1060                         eff_buf.token_len = 0;
1061                 } while (more);
1062
1063                 if (wsi->u.hdr.ah) {
1064                         lwsl_notice("%s: %p: detaching\n",
1065                                  __func__, wsi);
1066                         /* show we used all the pending rx up */
1067                         wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1068                         /* we can run the normal ah detach flow despite
1069                          * being in ws union mode, since all union members
1070                          * start with hdr */
1071                         lws_header_table_detach(wsi, 0);
1072                 }
1073
1074                 pending = lws_ssl_pending(wsi);
1075                 if (pending) {
1076                         pending = pending > context->pt_serv_buf_size ?
1077                                         context->pt_serv_buf_size : pending;
1078                         goto read;
1079                 }
1080
1081                 if (draining_flow && wsi->rxflow_buffer &&
1082                     wsi->rxflow_pos == wsi->rxflow_len) {
1083                         lwsl_info("flow buffer: drained\n");
1084                         lws_free_set_NULL(wsi->rxflow_buffer);
1085                         /* having drained the rxflow buffer, can rearm POLLIN */
1086 #ifdef LWS_NO_SERVER
1087                         n =
1088 #endif
1089                         _lws_rx_flow_control(wsi);
1090                         /* n ignored, needed for NO_SERVER case */
1091                 }
1092
1093                 break;
1094 #ifdef LWS_WITH_CGI
1095         case LWSCM_CGI: /* we exist to handle a cgi's stdin/out/err data...
1096                          * do the callback on our master wsi
1097                          */
1098                 {
1099                         struct lws_cgi_args args;
1100
1101                         if (wsi->cgi_channel >= LWS_STDOUT &&
1102                             !(pollfd->revents & pollfd->events & LWS_POLLIN))
1103                                 break;
1104                         if (wsi->cgi_channel == LWS_STDIN &&
1105                             !(pollfd->revents & pollfd->events & LWS_POLLOUT))
1106                                 break;
1107
1108                         if (wsi->cgi_channel == LWS_STDIN)
1109                                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
1110                                         lwsl_info("failed at set pollfd\n");
1111                                         return 1;
1112                                 }
1113
1114                         args.ch = wsi->cgi_channel;
1115                         args.stdwsi = &wsi->parent->cgi->stdwsi[0];
1116                         args.hdr_state = wsi->hdr_state;
1117
1118                         //lwsl_err("CGI LWS_STDOUT waiting wsi %p mode %d state %d\n",
1119                         //       wsi->parent, wsi->parent->mode, wsi->parent->state);
1120
1121                         if (user_callback_handle_rxflow(
1122                                         wsi->parent->protocol->callback,
1123                                         wsi->parent, LWS_CALLBACK_CGI,
1124                                         wsi->parent->user_space,
1125                                         (void *)&args, 0))
1126                                 return 1;
1127
1128                         break;
1129                 }
1130 #endif
1131         default:
1132 #ifdef LWS_NO_CLIENT
1133                 break;
1134 #else
1135                 if ((pollfd->revents & LWS_POLLOUT) &&
1136                     lws_handle_POLLOUT_event(wsi, pollfd)) {
1137                         lwsl_debug("POLLOUT event closed it\n");
1138                         goto close_and_handled;
1139                 }
1140
1141                 n = lws_client_socket_service(context, wsi, pollfd);
1142                 if (n)
1143                         return 1;
1144                 goto handled;
1145 #endif
1146         }
1147
1148         n = 0;
1149         goto handled;
1150
1151 close_and_handled:
1152         lwsl_debug("Close and handled\n");
1153         lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1154         /*
1155          * pollfd may point to something else after the close
1156          * due to pollfd swapping scheme on delete on some platforms
1157          * we can't clear revents now because it'd be the wrong guy's revents
1158          */
1159         return 1;
1160
1161 handled:
1162         pollfd->revents = 0;
1163         return n;
1164 }
1165
1166 LWS_VISIBLE int
1167 lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
1168 {
1169         return lws_service_fd_tsi(context, pollfd, 0);
1170 }
1171
1172 LWS_VISIBLE int
1173 lws_service(struct lws_context *context, int timeout_ms)
1174 {
1175         return lws_plat_service(context, timeout_ms);
1176 }
1177
1178 LWS_VISIBLE int
1179 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
1180 {
1181         return _lws_plat_service_tsi(context, timeout_ms, tsi);
1182 }
1183