test-client: fix broken protocol names
[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_WS_SERVING:
34                 n = LWS_CALLBACK_SERVER_WRITEABLE;
35                 break;
36         default:
37                 n = LWS_CALLBACK_HTTP_WRITEABLE;
38                 break;
39         }
40         lwsl_debug("%s: %p (user=%p)\n", __func__, wsi, wsi->user_space);
41         return user_callback_handle_rxflow(wsi->protocol->callback,
42                                            wsi, (enum lws_callback_reasons) n,
43                                            wsi->user_space, NULL, 0);
44 }
45
46 int
47 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd)
48 {
49         int write_type = LWS_WRITE_PONG;
50         struct lws_tokens eff_buf;
51 #ifdef LWS_USE_HTTP2
52         struct lws *wsi2;
53 #endif
54         int ret, m, n;
55
56         /*
57          * user callback is lowest priority to get these notifications
58          * actually, since other pending things cannot be disordered
59          */
60
61         /* Priority 1: pending truncated sends are incomplete ws fragments
62          *             If anything else sent first the protocol would be
63          *             corrupted.
64          */
65         if (wsi->trunc_len) {
66                 if (lws_issue_raw(wsi, wsi->trunc_alloc + wsi->trunc_offset,
67                                   wsi->trunc_len) < 0) {
68                         lwsl_info("%s signalling to close\n", __func__);
69                         return -1;
70                 }
71                 /* leave POLLOUT active either way */
72                 return 0;
73         } else
74                 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)
75                         return -1; /* retry closing now */
76
77
78 #ifdef LWS_USE_HTTP2
79         /* Priority 2: protocol packets
80          */
81         if (wsi->pps) {
82                 lwsl_info("servicing pps %d\n", wsi->pps);
83                 switch (wsi->pps) {
84                 case LWS_PPS_HTTP2_MY_SETTINGS:
85                 case LWS_PPS_HTTP2_ACK_SETTINGS:
86                         lws_http2_do_pps_send(lws_get_context(wsi), wsi);
87                         break;
88                 default:
89                         break;
90                 }
91                 wsi->pps = LWS_PPS_NONE;
92                 lws_rx_flow_control(wsi, 1);
93
94                 return 0; /* leave POLLOUT active */
95         }
96 #endif
97         /* Priority 3: pending control packets (pong or close)
98          */
99         if ((wsi->state == LWSS_ESTABLISHED &&
100              wsi->u.ws.ping_pending_flag) ||
101             (wsi->state == LWSS_RETURNED_CLOSE_ALREADY &&
102              wsi->u.ws.payload_is_close)) {
103
104                 if (wsi->u.ws.payload_is_close)
105                         write_type = LWS_WRITE_CLOSE;
106
107                 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
108                               wsi->u.ws.ping_payload_len, write_type);
109                 if (n < 0)
110                         return -1;
111
112                 /* well he is sent, mark him done */
113                 wsi->u.ws.ping_pending_flag = 0;
114                 if (wsi->u.ws.payload_is_close)
115                         /* oh... a close frame was it... then we are done */
116                         return -1;
117
118                 /* otherwise for PING, leave POLLOUT active either way */
119                 return 0;
120         }
121
122         /* Priority 4: if we are closing, not allowed to send more data frags
123          *             which means user callback or tx ext flush banned now
124          */
125         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
126                 goto user_service;
127
128         /* Priority 5: Tx path extension with more to send
129          *
130          *             These are handled as new fragments each time around
131          *             So while we must block new writeable callback to enforce
132          *             payload ordering, but since they are always complete
133          *             fragments control packets can interleave OK.
134          */
135         if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
136                 lwsl_ext("SERVICING TX EXT DRAINING\n");
137                 if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
138                         return -1;
139                 /* leave POLLOUT active */
140                 return 0;
141         }
142
143         /* Priority 6: user can get the callback
144          */
145         m = lws_ext_cb_active(wsi, LWS_EXT_CB_IS_WRITEABLE, NULL, 0);
146         if (m)
147                 return -1;
148 #ifndef LWS_NO_EXTENSIONS
149         if (!wsi->extension_data_pending)
150                 goto user_service;
151 #endif
152         /*
153          * check in on the active extensions, see if they
154          * had pending stuff to spill... they need to get the
155          * first look-in otherwise sequence will be disordered
156          *
157          * NULL, zero-length eff_buf means just spill pending
158          */
159
160         ret = 1;
161         while (ret == 1) {
162
163                 /* default to nobody has more to spill */
164
165                 ret = 0;
166                 eff_buf.token = NULL;
167                 eff_buf.token_len = 0;
168
169                 /* give every extension a chance to spill */
170
171                 m = lws_ext_cb_active(wsi,
172                                         LWS_EXT_CB_PACKET_TX_PRESEND,
173                                                &eff_buf, 0);
174                 if (m < 0) {
175                         lwsl_err("ext reports fatal error\n");
176                         return -1;
177                 }
178                 if (m)
179                         /*
180                          * at least one extension told us he has more
181                          * to spill, so we will go around again after
182                          */
183                         ret = 1;
184
185                 /* assuming they gave us something to send, send it */
186
187                 if (eff_buf.token_len) {
188                         n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
189                                           eff_buf.token_len);
190                         if (n < 0) {
191                                 lwsl_info("closing from POLLOUT spill\n");
192                                 return -1;
193                         }
194                         /*
195                          * Keep amount spilled small to minimize chance of this
196                          */
197                         if (n != eff_buf.token_len) {
198                                 lwsl_err("Unable to spill ext %d vs %s\n",
199                                                           eff_buf.token_len, n);
200                                 return -1;
201                         }
202                 } else
203                         continue;
204
205                 /* no extension has more to spill */
206
207                 if (!ret)
208                         continue;
209
210                 /*
211                  * There's more to spill from an extension, but we just sent
212                  * something... did that leave the pipe choked?
213                  */
214
215                 if (!lws_send_pipe_choked(wsi))
216                         /* no we could add more */
217                         continue;
218
219                 lwsl_info("choked in POLLOUT service\n");
220
221                 /*
222                  * Yes, he's choked.  Leave the POLLOUT masked on so we will
223                  * come back here when he is unchoked.  Don't call the user
224                  * callback to enforce ordering of spilling, he'll get called
225                  * when we come back here and there's nothing more to spill.
226                  */
227
228                 return 0;
229         }
230 #ifndef LWS_NO_EXTENSIONS
231         wsi->extension_data_pending = 0;
232 #endif
233 user_service:
234         /* one shot */
235
236         if (pollfd)
237                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
238                         lwsl_info("failed at set pollfd\n");
239                         return 1;
240                 }
241
242 #ifdef LWS_USE_HTTP2
243         /*
244          * we are the 'network wsi' for potentially many muxed child wsi with
245          * no network connection of their own, who have to use us for all their
246          * network actions.  So we use a round-robin scheme to share out the
247          * POLLOUT notifications to our children.
248          *
249          * But because any child could exhaust the socket's ability to take
250          * writes, we can only let one child get notified each time.
251          *
252          * In addition children may be closed / deleted / added between POLLOUT
253          * notifications, so we can't hold pointers
254          */
255
256         if (wsi->mode != LWSCM_HTTP2_SERVING) {
257                 lwsl_info("%s: non http2\n", __func__);
258                 goto notify;
259         }
260
261         wsi->u.http2.requested_POLLOUT = 0;
262         if (!wsi->u.http2.initialized) {
263                 lwsl_info("pollout on uninitialized http2 conn\n");
264                 return 0;
265         }
266
267         lwsl_info("%s: doing children\n", __func__);
268
269         wsi2 = wsi;
270         do {
271                 wsi2 = wsi2->u.http2.next_child_wsi;
272                 lwsl_info("%s: child %p\n", __func__, wsi2);
273                 if (!wsi2)
274                         continue;
275                 if (!wsi2->u.http2.requested_POLLOUT)
276                         continue;
277                 wsi2->u.http2.requested_POLLOUT = 0;
278                 if (lws_calllback_as_writeable(wsi2)) {
279                         lwsl_debug("Closing POLLOUT child\n");
280                         lws_close_free_wsi(wsi2, LWS_CLOSE_STATUS_NOSTATUS);
281                 }
282                 wsi2 = wsi;
283         } while (wsi2 != NULL && !lws_send_pipe_choked(wsi));
284
285         lwsl_info("%s: completed\n", __func__);
286
287         return 0;
288 notify:
289 #endif
290         return lws_calllback_as_writeable(wsi);
291 }
292
293 int
294 lws_service_timeout_check(struct lws *wsi, unsigned int sec)
295 {
296         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
297
298         /*
299          * if extensions want in on it (eg, we are a mux parent)
300          * give them a chance to service child timeouts
301          */
302         if (lws_ext_cb_active(wsi, LWS_EXT_CB_1HZ, NULL, sec) < 0)
303                 return 0;
304
305         if (!wsi->pending_timeout)
306                 return 0;
307
308         /*
309          * if we went beyond the allowed time, kill the
310          * connection
311          */
312         if ((time_t)sec > wsi->pending_timeout_limit) {
313 #if LWS_POSIX
314                 lwsl_notice("wsi %p: TIMEDOUT WAITING on %d (did hdr %d, ah %p, wl %d, pfd events %d)\n",
315                             (void *)wsi, wsi->pending_timeout,
316                             wsi->hdr_parsing_completed, wsi->u.hdr.ah,
317                             pt->ah_wait_list_length,
318                             pt->fds[wsi->sock].events);
319 #endif
320                 /*
321                  * Since he failed a timeout, he already had a chance to do
322                  * something and was unable to... that includes situations like
323                  * half closed connections.  So process this "failed timeout"
324                  * close as a violent death and don't try to do protocol
325                  * cleanup like flush partials.
326                  */
327                 wsi->socket_is_permanently_unusable = 1;
328                 if (wsi->mode == LWSCM_WSCL_WAITING_SSL)
329                         wsi->context->protocols[0].callback(wsi,
330                                         LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
331                                         wsi->user_space, NULL, 0);
332                 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
333
334                 return 1;
335         }
336
337         return 0;
338 }
339
340 int lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len)
341 {
342         /* his RX is flowcontrolled, don't send remaining now */
343         if (wsi->rxflow_buffer) {
344                 /* rxflow while we were spilling prev rxflow */
345                 lwsl_info("stalling in existing rxflow buf\n");
346                 return 1;
347         }
348
349         /* a new rxflow, buffer it and warn caller */
350         lwsl_info("new rxflow input buffer len %d\n", len - n);
351         wsi->rxflow_buffer = lws_malloc(len - n);
352         if (!wsi->rxflow_buffer)
353                 return -1;
354         wsi->rxflow_len = len - n;
355         wsi->rxflow_pos = 0;
356         memcpy(wsi->rxflow_buffer, buf + n, len - n);
357
358         return 0;
359 }
360
361 /* this is used by the platform service code to stop us waiting for network
362  * activity in poll() when we have something that already needs service
363  */
364
365 int
366 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi)
367 {
368         struct lws_context_per_thread *pt = &context->pt[tsi];
369         int n;
370
371         /* Figure out if we really want to wait in poll()
372          * We only need to wait if really nothing already to do and we have
373          * to wait for something from network
374          */
375
376         /* 1) if we know we are draining rx ext, do not wait in poll */
377         if (pt->rx_draining_ext_list)
378                 return 0;
379
380 #ifdef LWS_OPENSSL_SUPPORT
381         /* 2) if we know we have non-network pending data, do not wait in poll */
382         if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
383                 lwsl_info("ssl buffered read\n");
384                 return 0;
385         }
386 #endif
387
388         /* 3) if any ah has pending rx, do not wait in poll */
389         for (n = 0; n < context->max_http_header_pool; n++)
390                 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen) {
391                         /* any ah with pending rx must be attached to someone */
392                         assert(pt->ah_pool[n].wsi);
393                         return 0;
394                 }
395
396         return timeout_ms;
397 }
398
399 /*
400  * guys that need POLLIN service again without waiting for network action
401  * can force POLLIN here if not flowcontrolled, so they will get service.
402  *
403  * Return nonzero if anybody got their POLLIN faked
404  */
405 int
406 lws_service_flag_pending(struct lws_context *context, int tsi)
407 {
408         struct lws_context_per_thread *pt = &context->pt[tsi];
409 #ifdef LWS_OPENSSL_SUPPORT
410         struct lws *wsi_next;
411 #endif
412         struct lws *wsi;
413         int forced = 0;
414         int n;
415
416         /* POLLIN faking */
417
418         /*
419          * 1) For all guys with already-available ext data to drain, if they are
420          * not flowcontrolled, fake their POLLIN status
421          */
422         wsi = pt->rx_draining_ext_list;
423         while (wsi) {
424                 pt->fds[wsi->position_in_fds_table].revents |=
425                         pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
426                 if (pt->fds[wsi->position_in_fds_table].revents &
427                     LWS_POLLIN)
428                         forced = 1;
429                 wsi = wsi->u.ws.rx_draining_ext_list;
430         }
431
432 #ifdef LWS_OPENSSL_SUPPORT
433         /*
434          * 2) For all guys with buffered SSL read data already saved up, if they
435          * are not flowcontrolled, fake their POLLIN status so they'll get
436          * service to use up the buffered incoming data, even though their
437          * network socket may have nothing
438          */
439         wsi = pt->pending_read_list;
440         while (wsi) {
441                 wsi_next = wsi->pending_read_list_next;
442                 pt->fds[wsi->position_in_fds_table].revents |=
443                         pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
444                 if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
445                         forced = 1;
446                         /*
447                          * he's going to get serviced now, take him off the
448                          * list of guys with buffered SSL.  If he still has some
449                          * at the end of the service, he'll get put back on the
450                          * list then.
451                          */
452                         lws_ssl_remove_wsi_from_buffered_list(wsi);
453                 }
454
455                 wsi = wsi_next;
456         }
457 #endif
458         /*
459          * 3) For any wsi who have an ah with pending RX who did not
460          * complete their current headers, and are not flowcontrolled,
461          * fake their POLLIN status so they will be able to drain the
462          * rx buffered in the ah
463          */
464         for (n = 0; n < context->max_http_header_pool; n++)
465                 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen &&
466                     !pt->ah_pool[n].wsi->hdr_parsing_completed) {
467                         pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents |=
468                                 pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].events &
469                                         LWS_POLLIN;
470                         if (pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents &
471                             LWS_POLLIN)
472                                 forced = 1;
473                 }
474
475         return forced;
476 }
477
478 /**
479  * lws_service_fd() - Service polled socket with something waiting
480  * @context:    Websocket context
481  * @pollfd:     The pollfd entry describing the socket fd and which events
482  *              happened.
483  *
484  *      This function takes a pollfd that has POLLIN or POLLOUT activity and
485  *      services it according to the state of the associated
486  *      struct lws.
487  *
488  *      The one call deals with all "service" that might happen on a socket
489  *      including listen accepts, http files as well as websocket protocol.
490  *
491  *      If a pollfd says it has something, you can just pass it to
492  *      lws_service_fd() whether it is a socket handled by lws or not.
493  *      If it sees it is a lws socket, the traffic will be handled and
494  *      pollfd->revents will be zeroed now.
495  *
496  *      If the socket is foreign to lws, it leaves revents alone.  So you can
497  *      see if you should service yourself by checking the pollfd revents
498  *      after letting lws try to service it.
499  */
500
501 LWS_VISIBLE int
502 lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi)
503 {
504         struct lws_context_per_thread *pt = &context->pt[tsi];
505         lws_sockfd_type our_fd = 0, tmp_fd;
506         struct lws_tokens eff_buf;
507         unsigned int pending = 0;
508         struct lws *wsi, *wsi1;
509         char draining_flow = 0;
510         int timed_out = 0;
511         time_t now;
512         int n, m;
513         int more;
514
515         /*
516          * you can call us with pollfd = NULL to just allow the once-per-second
517          * global timeout checks; if less than a second since the last check
518          * it returns immediately then.
519          */
520
521         time(&now);
522
523         /* TODO: if using libev, we should probably use timeout watchers... */
524         if (context->last_timeout_check_s != now) {
525                 context->last_timeout_check_s = now;
526
527                 lws_plat_service_periodic(context);
528
529                 /* global timeout check once per second */
530
531                 if (pollfd)
532                         our_fd = pollfd->fd;
533
534                 wsi = context->pt[tsi].timeout_list;
535                 while (wsi) {
536                         /* we have to take copies, because he may be deleted */
537                         wsi1 = wsi->timeout_list;
538                         tmp_fd = wsi->sock;
539                         if (lws_service_timeout_check(wsi, (unsigned int)now)) {
540                                 /* he did time out... */
541                                 if (tmp_fd == our_fd)
542                                         /* it was the guy we came to service! */
543                                         timed_out = 1;
544                                         /* he's gone, no need to mark as handled */
545                         }
546                         wsi = wsi1;
547                 }
548 #if 0
549                 {
550                         char s[300], *p = s;
551
552                         for (n = 0; n < context->count_threads; n++)
553                                 p += sprintf(p, " %7lu (%5d), ",
554                                              context->pt[n].count_conns,
555                                              context->pt[n].fds_count);
556
557                         lwsl_notice("load: %s\n", s);
558                 }
559 #endif
560         }
561
562         /* the socket we came to service timed out, nothing to do */
563         if (timed_out)
564                 return 0;
565
566         /* just here for timeout management? */
567         if (!pollfd)
568                 return 0;
569
570         /* no, here to service a socket descriptor */
571         wsi = wsi_from_fd(context, pollfd->fd);
572         if (!wsi)
573                 /* not lws connection ... leave revents alone and return */
574                 return 0;
575
576         /*
577          * so that caller can tell we handled, past here we need to
578          * zero down pollfd->revents after handling
579          */
580
581 #if LWS_POSIX
582
583         /* handle session socket closed */
584
585         if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
586             (pollfd->revents & LWS_POLLHUP)) {
587                 wsi->socket_is_permanently_unusable = 1;
588                 lwsl_debug("Session Socket %p (fd=%d) dead\n",
589                                                        (void *)wsi, pollfd->fd);
590
591                 goto close_and_handled;
592         }
593
594 #ifdef _WIN32
595         if (pollfd->revents & LWS_POLLOUT)
596                 wsi->sock_send_blocking = FALSE;
597 #endif
598
599 #endif
600
601         /* okay, what we came here to do... */
602
603         switch (wsi->mode) {
604         case LWSCM_HTTP_SERVING:
605         case LWSCM_HTTP_SERVING_ACCEPTED:
606         case LWSCM_SERVER_LISTENER:
607         case LWSCM_SSL_ACK_PENDING:
608                 n = lws_server_socket_service(context, wsi, pollfd);
609                 if (n) /* closed by above */
610                         return 1;
611                 goto handled;
612
613         case LWSCM_WS_SERVING:
614         case LWSCM_WS_CLIENT:
615         case LWSCM_HTTP2_SERVING:
616
617                 /* 1: something requested a callback when it was OK to write */
618
619                 if ((pollfd->revents & LWS_POLLOUT) &&
620                     (wsi->state == LWSS_ESTABLISHED ||
621                      wsi->state == LWSS_HTTP2_ESTABLISHED ||
622                      wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS ||
623                      wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
624                      wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) &&
625                     lws_handle_POLLOUT_event(wsi, pollfd)) {
626                         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
627                                 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
628                         lwsl_info("lws_service_fd: closing\n");
629                         goto close_and_handled;
630                 }
631
632                 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
633                     wsi->state == LWSS_AWAITING_CLOSE_ACK) {
634                         /*
635                          * we stopped caring about anything except control
636                          * packets.  Force flow control off, defeat tx
637                          * draining.
638                          */
639                         lws_rx_flow_control(wsi, 1);
640                         wsi->u.ws.tx_draining_ext = 0;
641                 }
642
643                 if (wsi->u.ws.tx_draining_ext) {
644                         /* we cannot deal with new RX until the TX ext
645                          * path has been drained.  It's because new
646                          * rx will, eg, crap on the wsi rx buf that
647                          * may be needed to retain state.
648                          *
649                          * TX ext drain path MUST go through event loop
650                          * to avoid blocking.
651                          */
652                         break;
653                 }
654
655                 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
656                         /* We cannot deal with any kind of new RX
657                          * because we are RX-flowcontrolled.
658                          */
659                         break;
660
661                 /* 2: RX Extension needs to be drained
662                  */
663
664                 if (wsi->state == LWSS_ESTABLISHED &&
665                     wsi->u.ws.rx_draining_ext) {
666
667                         lwsl_ext("%s: RX EXT DRAINING: Service\n", __func__);
668 #ifndef LWS_NO_CLIENT
669                         if (wsi->mode == LWSCM_WS_CLIENT) {
670                                 n = lws_client_rx_sm(wsi, 0);
671                                 if (n < 0)
672                                         /* we closed wsi */
673                                         n = 0;
674                         } else
675 #endif
676                                 n = lws_rx_sm(wsi, 0);
677
678                         goto handled;
679                 }
680
681                 if (wsi->u.ws.rx_draining_ext)
682                         /*
683                          * We have RX EXT content to drain, but can't do it
684                          * right now.  That means we cannot do anything lower
685                          * priority either.
686                          */
687                         break;
688
689                 /* 3: RX Flowcontrol buffer needs to be drained
690                  */
691
692                 if (wsi->rxflow_buffer) {
693                         lwsl_info("draining rxflow (len %d)\n",
694                                 wsi->rxflow_len - wsi->rxflow_pos
695                         );
696                         /* well, drain it */
697                         eff_buf.token = (char *)wsi->rxflow_buffer +
698                                                 wsi->rxflow_pos;
699                         eff_buf.token_len = wsi->rxflow_len - wsi->rxflow_pos;
700                         draining_flow = 1;
701                         goto drain;
702                 }
703
704                 /* 4: any incoming (or ah-stashed incoming rx) data ready?
705                  * notice if rx flow going off raced poll(), rx flow wins
706                  */
707
708                 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
709                         break;
710 read:
711
712                 /* all the union members start with hdr, so even in ws mode
713                  * we can deal with the ah via u.hdr
714                  */
715                 if (wsi->u.hdr.ah) {
716                         lwsl_err("%s: %p: using inherited ah rx\n", __func__, wsi);
717                         eff_buf.token_len = wsi->u.hdr.ah->rxlen -
718                                             wsi->u.hdr.ah->rxpos;
719                         eff_buf.token = (char *)wsi->u.hdr.ah->rx +
720                                         wsi->u.hdr.ah->rxpos;
721                 } else {
722
723                         eff_buf.token_len = lws_ssl_capable_read(wsi, pt->serv_buf,
724                                              pending ? pending : LWS_MAX_SOCKET_IO_BUF);
725                         switch (eff_buf.token_len) {
726                         case 0:
727                                 lwsl_info("service_fd: closing due to 0 length read\n");
728                                 goto close_and_handled;
729                         case LWS_SSL_CAPABLE_MORE_SERVICE:
730                                 lwsl_info("SSL Capable more service\n");
731                                 n = 0;
732                                 goto handled;
733                         case LWS_SSL_CAPABLE_ERROR:
734                                 lwsl_info("Closing when error\n");
735                                 goto close_and_handled;
736                         }
737
738                         eff_buf.token = (char *)pt->serv_buf;
739                 }
740                 /*
741                  * give any active extensions a chance to munge the buffer
742                  * before parse.  We pass in a pointer to an lws_tokens struct
743                  * prepared with the default buffer and content length that's in
744                  * there.  Rather than rewrite the default buffer, extensions
745                  * that expect to grow the buffer can adapt .token to
746                  * point to their own per-connection buffer in the extension
747                  * user allocation.  By default with no extensions or no
748                  * extension callback handling, just the normal input buffer is
749                  * used then so it is efficient.
750                  */
751 drain:
752                 do {
753                         more = 0;
754
755                         m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_RX_PREPARSE,
756                                               &eff_buf, 0);
757                         if (m < 0)
758                                 goto close_and_handled;
759                         if (m)
760                                 more = 1;
761
762                         /* service incoming data */
763
764                         if (eff_buf.token_len) {
765                                 /*
766                                  * if draining from rxflow buffer, not
767                                  * critical to track what was used since at the
768                                  * use it bumps wsi->rxflow_pos.  If we come
769                                  * around again it will pick up from where it
770                                  * left off.
771                                  */
772                                 n = lws_read(wsi, (unsigned char *)eff_buf.token,
773                                              eff_buf.token_len);
774                                 if (n < 0) {
775                                         /* we closed wsi */
776                                         n = 0;
777                                         goto handled;
778                                 }
779                         }
780
781                         eff_buf.token = NULL;
782                         eff_buf.token_len = 0;
783                 } while (more);
784
785                 if (wsi->u.hdr.ah) {
786                         lwsl_err("%s: %p: detaching inherited used ah\n", __func__, wsi);
787                         /* show we used all the pending rx up */
788                         wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
789                         /* we can run the normal ah detach flow despite
790                          * being in ws union mode, since all union members
791                          * start with hdr */
792                         lws_header_table_detach(wsi);
793                 }
794
795                 pending = lws_ssl_pending(wsi);
796                 if (pending) {
797                         pending = pending > LWS_MAX_SOCKET_IO_BUF ?
798                                         LWS_MAX_SOCKET_IO_BUF : pending;
799                         goto read;
800                 }
801
802                 if (draining_flow && wsi->rxflow_buffer &&
803                     wsi->rxflow_pos == wsi->rxflow_len) {
804                         lwsl_info("flow buffer: drained\n");
805                         lws_free_set_NULL(wsi->rxflow_buffer);
806                         /* having drained the rxflow buffer, can rearm POLLIN */
807 #ifdef LWS_NO_SERVER
808                         n =
809 #endif
810                         _lws_rx_flow_control(wsi);
811                         /* n ignored, needed for NO_SERVER case */
812                 }
813
814                 break;
815
816         default:
817 #ifdef LWS_NO_CLIENT
818                 break;
819 #else
820                 n = lws_client_socket_service(context, wsi, pollfd);
821                 if (n)
822                         return 1;
823                 goto handled;
824 #endif
825         }
826
827         n = 0;
828         goto handled;
829
830 close_and_handled:
831         lwsl_debug("Close and handled\n");
832         lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
833         /*
834          * pollfd may point to something else after the close
835          * due to pollfd swapping scheme on delete on some platforms
836          * we can't clear revents now because it'd be the wrong guy's revents
837          */
838         return 1;
839
840 handled:
841         pollfd->revents = 0;
842         return n;
843 }
844
845 LWS_VISIBLE int
846 lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
847 {
848         return lws_service_fd_tsi(context, pollfd, 0);
849 }
850
851 /**
852  * lws_service() - Service any pending websocket activity
853  * @context:    Websocket context
854  * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
855  *              service otherwise block and service immediately, returning
856  *              after the timeout if nothing needed service.
857  *
858  *      This function deals with any pending websocket traffic, for three
859  *      kinds of event.  It handles these events on both server and client
860  *      types of connection the same.
861  *
862  *      1) Accept new connections to our context's server
863  *
864  *      2) Call the receive callback for incoming frame data received by
865  *          server or client connections.
866  *
867  *      You need to call this service function periodically to all the above
868  *      functions to happen; if your application is single-threaded you can
869  *      just call it in your main event loop.
870  *
871  *      Alternatively you can fork a new process that asynchronously handles
872  *      calling this service in a loop.  In that case you are happy if this
873  *      call blocks your thread until it needs to take care of something and
874  *      would call it with a large nonzero timeout.  Your loop then takes no
875  *      CPU while there is nothing happening.
876  *
877  *      If you are calling it in a single-threaded app, you don't want it to
878  *      wait around blocking other things in your loop from happening, so you
879  *      would call it with a timeout_ms of 0, so it returns immediately if
880  *      nothing is pending, or as soon as it services whatever was pending.
881  */
882
883 LWS_VISIBLE int
884 lws_service(struct lws_context *context, int timeout_ms)
885 {
886         return lws_plat_service(context, timeout_ms);
887 }
888
889 LWS_VISIBLE int
890 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
891 {
892         return lws_plat_service_tsi(context, timeout_ms, tsi);
893 }
894