void init_lws(void) { struct lws_context_creation_info info; lws_set_log_level(7, lwsl_emit_syslog); os_memset(&info, 0, sizeof(info)); info.port = 80; info.fd_limit_per_thread = 12; info.max_http_header_pool = 1; info.max_http_header_data = 512; info.pt_serv_buf_size = 1500; info.keepalive_timeout = 5; //info.ws_ping_pong_interval = 30; info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS; context = lws_create_context(&info); if (context == NULL) return; lws_get_fops(context)->open = esp8266_lws_fops_open; lws_get_fops(context)->close = esp8266_lws_fops_close; lws_get_fops(context)->read = esp8266_lws_fops_read; lws_get_fops(context)->seek_cur = esp8266_lws_fops_seek_cur; if (!lwsesp_is_booting_in_ap_mode()) { info.vhost_name = "station"; info.protocols = protocols_station; info.mounts = &mount_station; if (!lws_create_vhost(context, &info)) lwsl_err("Failed to create vhost station\n"); } else { info.vhost_name = "ap"; info.protocols = protocols_ap; info.mounts = &mount_ap; if (!lws_create_vhost(context, &info)) lwsl_err("Failed to create vhost ap\n"); } lws_protocol_init(context); }
LWS_VISIBLE struct lws * lws_client_connect_via_info(struct lws_client_connect_info *i) { struct lws *wsi; int v = SPEC_LATEST_SUPPORTED; const struct lws_protocols *p; if (i->context->requested_kill) return NULL; if (!i->context->protocol_init_done) lws_protocol_init(i->context); wsi = lws_zalloc(sizeof(struct lws)); if (wsi == NULL) goto bail; wsi->context = i->context; /* assert the mode and union status (hdr) clearly */ lws_union_transition(wsi, LWSCM_HTTP_CLIENT); wsi->desc.sockfd = LWS_SOCK_INVALID; /* 1) fill up the wsi with stuff from the connect_info as far as it * can go. It's because not only is our connection async, we might * not even be able to get ahold of an ah at this point. */ /* -1 means just use latest supported */ if (i->ietf_version_or_minus_one != -1 && i->ietf_version_or_minus_one) v = i->ietf_version_or_minus_one; wsi->ietf_spec_revision = v; wsi->user_space = NULL; wsi->state = LWSS_CLIENT_UNCONNECTED; wsi->pending_timeout = NO_PENDING_TIMEOUT; wsi->position_in_fds_table = -1; wsi->c_port = i->port; wsi->vhost = i->vhost; if (!wsi->vhost) wsi->vhost = i->context->vhost_list; wsi->protocol = &wsi->vhost->protocols[0]; /* for http[s] connection, allow protocol selection by name */ if (i->method && i->vhost && i->protocol) { p = lws_vhost_name_to_protocol(i->vhost, i->protocol); if (p) wsi->protocol = p; } if (wsi && !wsi->user_space && i->userdata) { wsi->user_space_externally_allocated = 1; wsi->user_space = i->userdata; } else /* if we stay in http, we can assign the user space now, * otherwise do it after the protocol negotiated */ if (i->method) if (lws_ensure_user_space(wsi)) goto bail; #ifdef LWS_OPENSSL_SUPPORT wsi->use_ssl = i->ssl_connection; #else if (i->ssl_connection) { lwsl_err("libwebsockets not configured for ssl\n"); goto bail; } #endif /* 2) stash the things from connect_info that we can't process without * an ah. Because if no ah, we will go on the ah waiting list and * process those things later (after the connect_info and maybe the * things pointed to have gone out of scope. */ wsi->u.hdr.stash = lws_malloc(sizeof(*wsi->u.hdr.stash)); if (!wsi->u.hdr.stash) { lwsl_err("%s: OOM\n", __func__); goto bail; } wsi->u.hdr.stash->origin[0] = '\0'; wsi->u.hdr.stash->protocol[0] = '\0'; wsi->u.hdr.stash->method[0] = '\0'; wsi->u.hdr.stash->iface[0] = '\0'; strncpy(wsi->u.hdr.stash->address, i->address, sizeof(wsi->u.hdr.stash->address) - 1); strncpy(wsi->u.hdr.stash->path, i->path, sizeof(wsi->u.hdr.stash->path) - 1); strncpy(wsi->u.hdr.stash->host, i->host, sizeof(wsi->u.hdr.stash->host) - 1); if (i->origin) strncpy(wsi->u.hdr.stash->origin, i->origin, sizeof(wsi->u.hdr.stash->origin) - 1); if (i->protocol) strncpy(wsi->u.hdr.stash->protocol, i->protocol, sizeof(wsi->u.hdr.stash->protocol) - 1); if (i->method) strncpy(wsi->u.hdr.stash->method, i->method, sizeof(wsi->u.hdr.stash->method) - 1); if (i->iface) strncpy(wsi->u.hdr.stash->iface, i->iface, sizeof(wsi->u.hdr.stash->iface) - 1); wsi->u.hdr.stash->address[sizeof(wsi->u.hdr.stash->address) - 1] = '\0'; wsi->u.hdr.stash->path[sizeof(wsi->u.hdr.stash->path) - 1] = '\0'; wsi->u.hdr.stash->host[sizeof(wsi->u.hdr.stash->host) - 1] = '\0'; wsi->u.hdr.stash->origin[sizeof(wsi->u.hdr.stash->origin) - 1] = '\0'; wsi->u.hdr.stash->protocol[sizeof(wsi->u.hdr.stash->protocol) - 1] = '\0'; wsi->u.hdr.stash->method[sizeof(wsi->u.hdr.stash->method) - 1] = '\0'; wsi->u.hdr.stash->iface[sizeof(wsi->u.hdr.stash->iface) - 1] = '\0'; if (i->pwsi) *i->pwsi = wsi; /* if we went on the waiting list, no probs just return the wsi * when we get the ah, now or later, he will call * lws_client_connect_via_info2() below. */ if (lws_header_table_attach(wsi, 0) < 0) { /* * if we failed here, the connection is already closed * and freed. */ goto bail1; } if (i->parent_wsi) { lwsl_info("%s: created child %p of parent %p\n", __func__, wsi, i->parent_wsi); wsi->parent = i->parent_wsi; wsi->sibling_list = i->parent_wsi->child_list; i->parent_wsi->child_list = wsi; } #ifdef LWS_WITH_HTTP_PROXY if (i->uri_replace_to) wsi->rw = lws_rewrite_create(wsi, html_parser_cb, i->uri_replace_from, i->uri_replace_to); #endif return wsi; bail: lws_free(wsi); bail1: if (i->pwsi) *i->pwsi = NULL; return NULL; }
LWS_VISIBLE int lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi) { struct lws_context_per_thread *pt = &context->pt[tsi]; lws_sockfd_type our_fd = 0, tmp_fd; struct lws_tokens eff_buf; unsigned int pending = 0; struct lws *wsi, *wsi1; char draining_flow = 0; int timed_out = 0; time_t now; int n = 0, m; int more; if (!context->protocol_init_done) lws_protocol_init(context); /* * you can call us with pollfd = NULL to just allow the once-per-second * global timeout checks; if less than a second since the last check * it returns immediately then. */ time(&now); /* TODO: if using libev, we should probably use timeout watchers... */ if (context->last_timeout_check_s != now) { context->last_timeout_check_s = now; lws_plat_service_periodic(context); /* global timeout check once per second */ if (pollfd) our_fd = pollfd->fd; wsi = context->pt[tsi].timeout_list; while (wsi) { /* we have to take copies, because he may be deleted */ wsi1 = wsi->timeout_list; tmp_fd = wsi->sock; if (lws_service_timeout_check(wsi, (unsigned int)now)) { /* he did time out... */ if (tmp_fd == our_fd) /* it was the guy we came to service! */ timed_out = 1; /* he's gone, no need to mark as handled */ } wsi = wsi1; } #ifdef LWS_WITH_CGI lws_cgi_kill_terminated(pt); #endif #if 0 { char s[300], *p = s; for (n = 0; n < context->count_threads; n++) p += sprintf(p, " %7lu (%5d), ", context->pt[n].count_conns, context->pt[n].fds_count); lwsl_notice("load: %s\n", s); } #endif } /* the socket we came to service timed out, nothing to do */ if (timed_out) return 0; /* just here for timeout management? */ if (!pollfd) return 0; /* no, here to service a socket descriptor */ wsi = wsi_from_fd(context, pollfd->fd); if (!wsi) /* not lws connection ... leave revents alone and return */ return 0; /* * so that caller can tell we handled, past here we need to * zero down pollfd->revents after handling */ #if LWS_POSIX /* handle session socket closed */ if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) && (pollfd->revents & LWS_POLLHUP)) { wsi->socket_is_permanently_unusable = 1; lwsl_debug("Session Socket %p (fd=%d) dead\n", (void *)wsi, pollfd->fd); goto close_and_handled; } #ifdef _WIN32 if (pollfd->revents & LWS_POLLOUT) wsi->sock_send_blocking = FALSE; #endif #endif lwsl_debug("fd=%d, revents=%d\n", pollfd->fd, pollfd->revents); /* okay, what we came here to do... */ switch (wsi->mode) { case LWSCM_HTTP_SERVING: case LWSCM_HTTP_CLIENT: case LWSCM_HTTP_SERVING_ACCEPTED: case LWSCM_SERVER_LISTENER: case LWSCM_SSL_ACK_PENDING: if (wsi->state == LWSS_CLIENT_HTTP_ESTABLISHED) goto handled; #ifdef LWS_WITH_CGI if (wsi->cgi && (pollfd->revents & LWS_POLLOUT)) { n = lws_handle_POLLOUT_event(wsi, pollfd); if (n) goto close_and_handled; goto handled; } #endif n = lws_server_socket_service(context, wsi, pollfd); if (n) /* closed by above */ return 1; goto handled; case LWSCM_WS_SERVING: case LWSCM_WS_CLIENT: case LWSCM_HTTP2_SERVING: case LWSCM_HTTP_CLIENT_ACCEPTED: /* 1: something requested a callback when it was OK to write */ if ((pollfd->revents & LWS_POLLOUT) && (wsi->state == LWSS_ESTABLISHED || wsi->state == LWSS_HTTP2_ESTABLISHED || wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS || wsi->state == LWSS_RETURNED_CLOSE_ALREADY || wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) && lws_handle_POLLOUT_event(wsi, pollfd)) { if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY) wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE; lwsl_info("lws_service_fd: closing\n"); goto close_and_handled; } if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY || wsi->state == LWSS_AWAITING_CLOSE_ACK) { /* * we stopped caring about anything except control * packets. Force flow control off, defeat tx * draining. */ lws_rx_flow_control(wsi, 1); wsi->u.ws.tx_draining_ext = 0; } if (wsi->u.ws.tx_draining_ext) /* we cannot deal with new RX until the TX ext * path has been drained. It's because new * rx will, eg, crap on the wsi rx buf that * may be needed to retain state. * * TX ext drain path MUST go through event loop * to avoid blocking. */ break; if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) /* We cannot deal with any kind of new RX * because we are RX-flowcontrolled. */ break; /* 2: RX Extension needs to be drained */ if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.rx_draining_ext) { lwsl_ext("%s: RX EXT DRAINING: Service\n", __func__); #ifndef LWS_NO_CLIENT if (wsi->mode == LWSCM_WS_CLIENT) { n = lws_client_rx_sm(wsi, 0); if (n < 0) /* we closed wsi */ n = 0; } else #endif n = lws_rx_sm(wsi, 0); goto handled; } if (wsi->u.ws.rx_draining_ext) /* * We have RX EXT content to drain, but can't do it * right now. That means we cannot do anything lower * priority either. */ break; /* 3: RX Flowcontrol buffer needs to be drained */ if (wsi->rxflow_buffer) { lwsl_info("draining rxflow (len %d)\n", wsi->rxflow_len - wsi->rxflow_pos ); /* well, drain it */ eff_buf.token = (char *)wsi->rxflow_buffer + wsi->rxflow_pos; eff_buf.token_len = wsi->rxflow_len - wsi->rxflow_pos; draining_flow = 1; goto drain; } /* 4: any incoming (or ah-stashed incoming rx) data ready? * notice if rx flow going off raced poll(), rx flow wins */ if (!(pollfd->revents & pollfd->events & LWS_POLLIN)) break; read: /* all the union members start with hdr, so even in ws mode * we can deal with the ah via u.hdr */ if (wsi->u.hdr.ah) { lwsl_info("%s: %p: inherited ah rx\n", __func__, wsi); eff_buf.token_len = wsi->u.hdr.ah->rxlen - wsi->u.hdr.ah->rxpos; eff_buf.token = (char *)wsi->u.hdr.ah->rx + wsi->u.hdr.ah->rxpos; } else { if (wsi->mode != LWSCM_HTTP_CLIENT_ACCEPTED) { eff_buf.token_len = lws_ssl_capable_read(wsi, pt->serv_buf, pending ? pending : LWS_MAX_SOCKET_IO_BUF); switch (eff_buf.token_len) { case 0: lwsl_info("%s: zero length read\n", __func__); goto close_and_handled; case LWS_SSL_CAPABLE_MORE_SERVICE: lwsl_info("SSL Capable more service\n"); n = 0; goto handled; case LWS_SSL_CAPABLE_ERROR: lwsl_info("Closing when error\n"); goto close_and_handled; } eff_buf.token = (char *)pt->serv_buf; } } drain: #ifndef LWS_NO_CLIENT if (wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED) { /* * simply mark ourselves as having readable data * and turn off our POLLIN */ wsi->client_rx_avail = 1; lws_change_pollfd(wsi, LWS_POLLIN, 0); /* let user code know, he'll usually ask for writeable * callback and drain / reenable it there */ if (user_callback_handle_rxflow( wsi->protocol->callback, wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP, wsi->user_space, NULL, 0)) goto close_and_handled; } #endif /* * give any active extensions a chance to munge the buffer * before parse. We pass in a pointer to an lws_tokens struct * prepared with the default buffer and content length that's in * there. Rather than rewrite the default buffer, extensions * that expect to grow the buffer can adapt .token to * point to their own per-connection buffer in the extension * user allocation. By default with no extensions or no * extension callback handling, just the normal input buffer is * used then so it is efficient. */ do { more = 0; m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_RX_PREPARSE, &eff_buf, 0); if (m < 0) goto close_and_handled; if (m) more = 1; /* service incoming data */ if (eff_buf.token_len) { /* * if draining from rxflow buffer, not * critical to track what was used since at the * use it bumps wsi->rxflow_pos. If we come * around again it will pick up from where it * left off. */ n = lws_read(wsi, (unsigned char *)eff_buf.token, eff_buf.token_len); if (n < 0) { /* we closed wsi */ n = 0; goto handled; } } eff_buf.token = NULL; eff_buf.token_len = 0; } while (more); if (wsi->u.hdr.ah) { lwsl_info("%s: %p: detaching inherited used ah\n", __func__, wsi); /* show we used all the pending rx up */ wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen; /* we can run the normal ah detach flow despite * being in ws union mode, since all union members * start with hdr */ lws_header_table_detach(wsi, 0); } pending = lws_ssl_pending(wsi); if (pending) { pending = pending > LWS_MAX_SOCKET_IO_BUF ? LWS_MAX_SOCKET_IO_BUF : pending; goto read; } if (draining_flow && wsi->rxflow_buffer && wsi->rxflow_pos == wsi->rxflow_len) { lwsl_info("flow buffer: drained\n"); lws_free_set_NULL(wsi->rxflow_buffer); /* having drained the rxflow buffer, can rearm POLLIN */ #ifdef LWS_NO_SERVER n = #endif _lws_rx_flow_control(wsi); /* n ignored, needed for NO_SERVER case */ } break; #ifdef LWS_WITH_CGI case LWSCM_CGI: /* we exist to handle a cgi's stdin/out/err data... * do the callback on our master wsi */ { struct lws_cgi_args args; if (wsi->cgi_channel >= LWS_STDOUT && !(pollfd->revents & pollfd->events & LWS_POLLIN)) break; if (wsi->cgi_channel == LWS_STDIN && !(pollfd->revents & pollfd->events & LWS_POLLOUT)) break; if (wsi->cgi_channel == LWS_STDIN) if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) { lwsl_info("failed at set pollfd\n"); return 1; } args.ch = wsi->cgi_channel; args.stdwsi = &wsi->parent->cgi->stdwsi[0]; args.hdr_state = wsi->hdr_state; //lwsl_err("CGI LWS_STDOUT waiting wsi %p mode %d state %d\n", // wsi->parent, wsi->parent->mode, wsi->parent->state); if (user_callback_handle_rxflow( wsi->parent->protocol->callback, wsi->parent, LWS_CALLBACK_CGI, wsi->parent->user_space, (void *)&args, 0)) return 1; break; } #endif default: #ifdef LWS_NO_CLIENT break; #else n = lws_client_socket_service(context, wsi, pollfd); if (n) return 1; goto handled; #endif } n = 0; goto handled; close_and_handled: lwsl_debug("Close and handled\n"); lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS); /* * pollfd may point to something else after the close * due to pollfd swapping scheme on delete on some platforms * we can't clear revents now because it'd be the wrong guy's revents */ return 1; handled: pollfd->revents = 0; return n; }
static int lws_service_periodic_checks(struct lws_context *context, struct lws_pollfd *pollfd, int tsi) { struct lws_context_per_thread *pt = &context->pt[tsi]; lws_sockfd_type our_fd = 0, tmp_fd; struct lws *wsi; int timed_out = 0; time_t now; #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) struct allocated_headers *ah; int m; #endif if (!context->protocol_init_done) if (lws_protocol_init(context)) return -1; time(&now); /* * handle case that system time was uninitialized when lws started * at boot, and got initialized a little later */ if (context->time_up < 1464083026 && now > 1464083026) context->time_up = now; if (context->last_timeout_check_s && now - context->last_timeout_check_s > 100) { /* * There has been a discontiguity. Any stored time that is * less than context->time_discontiguity should have context-> * time_fixup added to it. * * Some platforms with no RTC will experience this as a normal * event when ntp sets their clock, but we can have started * long before that with a 0-based unix time. */ context->time_discontiguity = now; context->time_fixup = now - context->last_timeout_check_s; lwsl_notice("time discontiguity: at old time %llus, " "new time %llus: +%llus\n", (unsigned long long)context->last_timeout_check_s, (unsigned long long)context->time_discontiguity, (unsigned long long)context->time_fixup); context->last_timeout_check_s = now - 1; } if (!lws_compare_time_t(context, context->last_timeout_check_s, now)) return 0; context->last_timeout_check_s = now; #if defined(LWS_WITH_STATS) if (!tsi && now - context->last_dump > 10) { lws_stats_log_dump(context); context->last_dump = now; } #endif lws_plat_service_periodic(context); lws_check_deferred_free(context, 0); #if defined(LWS_WITH_PEER_LIMITS) lws_peer_cull_peer_wait_list(context); #endif /* retire unused deprecated context */ #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_ESP32) #if !defined(_WIN32) if (context->deprecated && !context->count_wsi_allocated) { lwsl_notice("%s: ending deprecated context\n", __func__); kill(getpid(), SIGINT); return 0; } #endif #endif /* global timeout check once per second */ if (pollfd) our_fd = pollfd->fd; /* * Phase 1: check every wsi on the timeout check list */ lws_pt_lock(pt, __func__); lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1, context->pt[tsi].dll_head_timeout.next) { wsi = lws_container_of(d, struct lws, dll_timeout); tmp_fd = wsi->desc.sockfd; if (__lws_service_timeout_check(wsi, now)) { /* he did time out... */ if (tmp_fd == our_fd) /* it was the guy we came to service! */ timed_out = 1; /* he's gone, no need to mark as handled */ } } lws_end_foreach_dll_safe(d, d1); #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) /* * Phase 2: double-check active ah timeouts independent of wsi * timeout status */ ah = pt->http.ah_list; while (ah) { int len; char buf[256]; const unsigned char *c; if (!ah->in_use || !ah->wsi || !ah->assigned || (ah->wsi->vhost && lws_compare_time_t(context, now, ah->assigned) < ah->wsi->vhost->timeout_secs_ah_idle + 360)) { ah = ah->next; continue; } /* * a single ah session somehow got held for * an unreasonable amount of time. * * Dump info on the connection... */ wsi = ah->wsi; buf[0] = '\0'; #if !defined(LWS_PLAT_OPTEE) lws_get_peer_simple(wsi, buf, sizeof(buf)); #else buf[0] = '\0'; #endif lwsl_notice("ah excessive hold: wsi %p\n" " peer address: %s\n" " ah pos %u\n", wsi, buf, ah->pos); buf[0] = '\0'; m = 0; do { c = lws_token_to_string(m); if (!c) break; if (!(*c)) break; len = lws_hdr_total_length(wsi, m); if (!len || len > (int)sizeof(buf) - 1) { m++; continue; } if (lws_hdr_copy(wsi, buf, sizeof buf, m) > 0) { buf[sizeof(buf) - 1] = '\0'; lwsl_notice(" %s = %s\n", (const char *)c, buf); } m++; } while (1); /* explicitly detach the ah */ lws_header_table_detach(wsi, 0); /* ... and then drop the connection */ m = 0; if (wsi->desc.sockfd == our_fd) { m = timed_out; /* it was the guy we came to service! */ timed_out = 1; } if (!m) /* if he didn't already timeout */ __lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "excessive ah"); ah = pt->http.ah_list; } #endif lws_pt_unlock(pt); #if 0 { char s[300], *p = s; for (n = 0; n < context->count_threads; n++) p += sprintf(p, " %7lu (%5d), ", context->pt[n].count_conns, context->pt[n].fds_count); lwsl_notice("load: %s\n", s); } #endif /* * Phase 3: vhost / protocol timer callbacks */ wsi = NULL; lws_start_foreach_ll(struct lws_vhost *, v, context->vhost_list) { struct lws_timed_vh_protocol *nx; if (v->timed_vh_protocol_list) { lws_start_foreach_ll(struct lws_timed_vh_protocol *, q, v->timed_vh_protocol_list) { if (now >= q->time) { if (!wsi) wsi = lws_zalloc(sizeof(*wsi), "cbwsi"); wsi->context = context; wsi->vhost = v; wsi->protocol = q->protocol; lwsl_debug("timed cb: vh %s, protocol %s, reason %d\n", v->name, q->protocol->name, q->reason); q->protocol->callback(wsi, q->reason, NULL, NULL, 0); nx = q->next; lws_timed_callback_remove(v, q); q = nx; continue; /* we pointed ourselves to the next from the now-deleted guy */ } } lws_end_foreach_ll(q, next); } } lws_end_foreach_ll(v, vhost_next);