2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2014 Andy Green <andy@warmcat.com>
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.
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.
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,
22 #include "private-libwebsockets.h"
24 int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
25 static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
27 static const char * const log_level_names[] = {
41 lws_free_wsi(struct lws *wsi)
46 /* Protocol user data may be allocated either internally by lws
47 * or by specified the user.
48 * We should only free what we allocated. */
49 if (wsi->protocol && wsi->protocol->per_session_data_size &&
50 wsi->user_space && !wsi->user_space_externally_allocated)
51 lws_free(wsi->user_space);
53 lws_free_set_NULL(wsi->rxflow_buffer);
54 lws_free_set_NULL(wsi->trunc_alloc);
56 * These union members have an ah at the start
58 * struct _lws_http_mode_related http;
59 * struct _lws_http2_related http2;
60 * struct _lws_header_related hdr;
62 * basically ws-related union member does not
64 if (wsi->mode != LWSCM_WS_CLIENT &&
65 wsi->mode != LWSCM_WS_SERVING)
67 lws_free_header_table(wsi);
73 lws_remove_from_timeout_list(struct lws *wsi)
75 if (!wsi->timeout_list_prev)
78 if (wsi->timeout_list)
79 wsi->timeout_list->timeout_list_prev = wsi->timeout_list_prev;
80 *wsi->timeout_list_prev = wsi->timeout_list;
82 wsi->timeout_list_prev = NULL;
83 wsi->timeout_list = NULL;
88 lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
90 struct lws_context *context;
91 struct lws_context_per_thread *pt;
92 int n, m, ret, old_state;
93 struct lws_tokens eff_buf;
98 context = wsi->context;
99 pt = &context->pt[(int)wsi->tsi];
100 old_state = wsi->state;
102 if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
103 wsi->u.http.fd != LWS_INVALID_FILE) {
104 lwsl_debug("closing http file\n");
105 lws_plat_file_close(wsi, wsi->u.http.fd);
106 wsi->u.http.fd = LWS_INVALID_FILE;
107 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
108 wsi->user_space, NULL, 0);
110 if (wsi->socket_is_permanently_unusable ||
111 reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)
112 goto just_kill_connection;
115 case LWSS_DEAD_SOCKET:
118 /* we tried the polite way... */
119 case LWSS_AWAITING_CLOSE_ACK:
120 goto just_kill_connection;
122 case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
123 if (wsi->trunc_len) {
124 lws_callback_on_writable(wsi);
127 lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
128 goto just_kill_connection;
130 if (wsi->trunc_len) {
131 lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
132 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
133 lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
139 if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
140 wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
141 goto just_kill_connection;
143 if (wsi->mode == LWSCM_HTTP_SERVING)
144 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
145 wsi->user_space, NULL, 0);
148 * are his extensions okay with him closing? Eg he might be a mux
149 * parent and just his ch1 aspect is closing?
152 if (lws_ext_cb_active(wsi,
153 LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
154 lwsl_ext("extension vetoed close\n");
159 * flush any tx pending from extensions, since we may send close packet
160 * if there are problems with send, just nuke the connection
165 eff_buf.token = NULL;
166 eff_buf.token_len = 0;
168 /* show every extension the new incoming data */
170 m = lws_ext_cb_active(wsi,
171 LWS_EXT_CB_FLUSH_PENDING_TX, &eff_buf, 0);
173 lwsl_ext("Extension reports fatal error\n");
174 goto just_kill_connection;
178 * at least one extension told us he has more
179 * to spill, so we will go around again after
183 /* assuming they left us something to send, send it */
185 if (eff_buf.token_len)
186 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
187 eff_buf.token_len) !=
189 lwsl_debug("close: ext spill failed\n");
190 goto just_kill_connection;
195 * signal we are closing, lws_write will
196 * add any necessary version-specific stuff. If the write fails,
197 * no worries we are closing anyway. If we didn't initiate this
198 * close, then our state has been changed to
199 * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
201 * Likewise if it's a second call to close this connection after we
202 * sent the close indication to the peer already, we are in state
203 * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
206 if (old_state == LWSS_ESTABLISHED &&
207 (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
208 (reason != LWS_CLOSE_STATUS_NOSTATUS &&
209 (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
210 lwsl_debug("sending close indication...\n");
212 /* if no prepared close reason, use 1000 and no aux data */
213 if (!wsi->u.ws.close_in_ping_buffer_len) {
214 wsi->u.ws.close_in_ping_buffer_len = 2;
215 wsi->u.ws.ping_payload_buf[LWS_PRE] =
216 (reason >> 16) & 0xff;
217 wsi->u.ws.ping_payload_buf[LWS_PRE + 1] =
221 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[
223 wsi->u.ws.close_in_ping_buffer_len,
227 * we have sent a nice protocol level indication we
228 * now wish to close, we should not send anything more
230 wsi->state = LWSS_AWAITING_CLOSE_ACK;
233 * ...and we should wait for a reply for a bit
236 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 1);
237 lwsl_debug("sent close indication, awaiting ack\n");
242 lwsl_info("close: sending close packet failed, hanging up\n");
244 /* else, the send failed and we should just hang up */
247 just_kill_connection:
249 lwsl_debug("close: just_kill_connection: %p\n", wsi);
252 * we won't be servicing or receiving anything further from this guy
253 * delete socket from the internal poll list if still present
255 lws_ssl_remove_wsi_from_buffered_list(wsi);
256 lws_remove_from_timeout_list(wsi);
258 /* checking return redundant since we anyway close */
259 remove_wsi_socket_from_fds(wsi);
261 wsi->state = LWSS_DEAD_SOCKET;
263 lws_free_set_NULL(wsi->rxflow_buffer);
265 if (old_state == LWSS_ESTABLISHED ||
266 wsi->mode == LWSCM_WS_SERVING ||
267 wsi->mode == LWSCM_WS_CLIENT) {
269 if (wsi->u.ws.rx_draining_ext) {
270 struct lws **w = &pt->rx_draining_ext_list;
272 wsi->u.ws.rx_draining_ext = 0;
273 /* remove us from context draining ext list */
276 *w = wsi->u.ws.rx_draining_ext_list;
279 w = &((*w)->u.ws.rx_draining_ext_list);
281 wsi->u.ws.rx_draining_ext_list = NULL;
284 if (wsi->u.ws.tx_draining_ext) {
285 struct lws **w = &pt->tx_draining_ext_list;
287 wsi->u.ws.tx_draining_ext = 0;
288 /* remove us from context draining ext list */
291 *w = wsi->u.ws.tx_draining_ext_list;
294 w = &((*w)->u.ws.tx_draining_ext_list);
296 wsi->u.ws.tx_draining_ext_list = NULL;
298 lws_free_set_NULL(wsi->u.ws.rx_ubuf);
300 if (wsi->trunc_alloc)
301 /* not going to be completed... nuke it */
302 lws_free_set_NULL(wsi->trunc_alloc);
304 wsi->u.ws.ping_payload_len = 0;
305 wsi->u.ws.ping_pending_flag = 0;
308 /* tell the user it's all over for this guy */
310 if (wsi->protocol && wsi->protocol->callback &&
311 ((old_state == LWSS_ESTABLISHED) ||
312 (old_state == LWSS_RETURNED_CLOSE_ALREADY) ||
313 (old_state == LWSS_AWAITING_CLOSE_ACK) ||
314 (old_state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE))) {
315 lwsl_debug("calling back CLOSED\n");
316 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
317 wsi->user_space, NULL, 0);
318 } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
319 lwsl_debug("calling back CLOSED_HTTP\n");
320 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
321 wsi->user_space, NULL, 0 );
322 } else if (wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
323 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) {
324 lwsl_debug("Connection closed before server reply\n");
325 context->protocols[0].callback(wsi,
326 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
327 wsi->user_space, NULL, 0);
329 lwsl_debug("not calling back closed mode=%d state=%d\n",
330 wsi->mode, old_state);
332 /* deallocate any active extension contexts */
334 if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
335 lwsl_warn("extension destruction failed\n");
337 * inform all extensions in case they tracked this guy out of band
338 * even though not active on him specifically
340 if (lws_ext_cb_all_exts(context, wsi,
341 LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
342 lwsl_warn("ext destroy wsi failed\n");
344 if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) {
346 n = shutdown(wsi->sock, SHUT_RDWR);
348 lwsl_debug("closing: shutdown ret %d\n", LWS_ERRNO);
350 n = compatible_close(wsi->sock);
352 lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
355 compatible_close(wsi->sock);
357 wsi->sock = LWS_SOCK_INVALID;
360 /* outermost destroy notification for wsi (user_space still intact) */
361 context->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
362 wsi->user_space, NULL, 0);
369 interface_to_sa(struct lws_context *context, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
373 ipv6 = LWS_IPV6_ENABLED(context);
377 return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
382 lws_get_addresses(struct lws_context *context, void *ads, char *name,
383 int name_len, char *rip, int rip_len)
386 struct addrinfo ai, *res;
387 struct sockaddr_in addr4;
392 addr4.sin_family = AF_UNSPEC;
395 if (LWS_IPV6_ENABLED(context)) {
396 if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
397 lwsl_err("inet_ntop", strerror(LWS_ERRNO));
401 // Strip off the IPv4 to IPv6 header if one exists
402 if (strncmp(rip, "::ffff:", 7) == 0)
403 memmove(rip, rip + 7, strlen(rip) - 6);
405 getnameinfo((struct sockaddr *)ads,
406 sizeof(struct sockaddr_in6), name,
407 name_len, NULL, 0, 0);
413 struct addrinfo *result;
415 memset(&ai, 0, sizeof ai);
416 ai.ai_family = PF_UNSPEC;
417 ai.ai_socktype = SOCK_STREAM;
418 ai.ai_flags = AI_CANONNAME;
420 if (getnameinfo((struct sockaddr *)ads,
421 sizeof(struct sockaddr_in),
422 name, name_len, NULL, 0, 0))
428 if (getaddrinfo(name, NULL, &ai, &result))
432 while (addr4.sin_family == AF_UNSPEC && res) {
433 switch (res->ai_family) {
435 addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
436 addr4.sin_family = AF_INET;
442 freeaddrinfo(result);
445 if (addr4.sin_family == AF_UNSPEC)
448 if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len) == NULL)
465 * lws_get_peer_addresses() - Get client address information
466 * @wsi: Local struct lws associated with
467 * @fd: Connection socket descriptor
468 * @name: Buffer to take client address name
469 * @name_len: Length of client address name buffer
470 * @rip: Buffer to take client address IP dotted quad
471 * @rip_len: Length of client address IP buffer
473 * This function fills in @name and @rip with the name and IP of
474 * the client connected with socket descriptor @fd. Names may be
475 * truncated if there is not enough room. If either cannot be
476 * determined, they will be returned as valid zero-length strings.
480 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
481 int name_len, char *rip, int rip_len)
486 struct sockaddr_in6 sin6;
488 struct sockaddr_in sin4;
489 struct lws_context *context = wsi->context;
496 lws_latency_pre(context, wsi);
499 if (LWS_IPV6_ENABLED(context)) {
509 if (getpeername(fd, p, &len) < 0) {
510 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
514 ret = lws_get_addresses(context, p, name, name_len, rip, rip_len);
517 lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
529 * lws_context_user() - get the user data associated with the context
530 * @context: Websocket context
532 * This returns the optional user allocation that can be attached to
533 * the context the sockets live in at context_create time. It's a way
534 * to let all sockets serviced in the same context share data without
535 * using globals statics in the user code.
538 lws_context_user(struct lws_context *context)
540 return context->user_space;
545 * lws_callback_all_protocol() - Callback all connections using
546 * the given protocol with the given reason
548 * @protocol: Protocol whose connections will get callbacks
549 * @reason: Callback reason index
553 lws_callback_all_protocol(struct lws_context *context,
554 const struct lws_protocols *protocol, int reason)
556 struct lws_context_per_thread *pt = &context->pt[0];
558 int n, m = context->count_threads;
561 for (n = 0; n < pt->fds_count; n++) {
562 wsi = wsi_from_fd(context, pt->fds[n].fd);
565 if (wsi->protocol == protocol)
566 protocol->callback(wsi, reason, wsi->user_space, NULL, 0);
575 * lws_set_timeout() - marks the wsi as subject to a timeout
577 * You will not need this unless you are doing something special
579 * @wsi: Websocket connection instance
580 * @reason: timeout reason
581 * @secs: how many seconds
585 lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
591 if (!wsi->pending_timeout) {
592 wsi->timeout_list = wsi->context->timeout_list;
593 if (wsi->timeout_list)
594 wsi->timeout_list->timeout_list_prev = &wsi->timeout_list;
595 wsi->timeout_list_prev = &wsi->context->timeout_list;
596 *wsi->timeout_list_prev = wsi;
599 wsi->pending_timeout_limit = now + secs;
600 wsi->pending_timeout = reason;
603 lws_remove_from_timeout_list(wsi);
610 * lws_get_socket_fd() - returns the socket file descriptor
612 * You will not need this unless you are doing something special
614 * @wsi: Websocket connection instance
618 lws_get_socket_fd(struct lws *wsi)
627 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
628 int ret, int completed)
630 unsigned long long u;
633 u = time_in_microseconds();
636 wsi->latency_start = u;
637 if (!wsi->action_start)
638 wsi->action_start = u;
642 if (wsi->action_start == wsi->latency_start)
644 "Completion first try lat %lluus: %p: ret %d: %s\n",
645 u - wsi->latency_start,
646 (void *)wsi, ret, action);
649 "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
650 u - wsi->action_start,
651 u - wsi->latency_start,
652 (void *)wsi, ret, action);
653 wsi->action_start = 0;
655 sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
656 u - wsi->latency_start, (void *)wsi, ret, action);
658 if (u - wsi->latency_start > context->worst_latency) {
659 context->worst_latency = u - wsi->latency_start;
660 strcpy(context->worst_latency_info, buf);
662 lwsl_latency("%s", buf);
669 * lws_rx_flow_control() - Enable and disable socket servicing for
672 * If the output side of a server process becomes choked, this allows flow
673 * control for the input side.
675 * @wsi: Websocket connection instance to get callback for
676 * @enable: 0 = disable read servicing for this connection, 1 = enable
680 lws_rx_flow_control(struct lws *wsi, int enable)
682 if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
685 lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
686 wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
692 * lws_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
694 * When the user server code realizes it can accept more input, it can
695 * call this to have the RX flow restriction removed from all connections using
696 * the given protocol.
698 * @protocol: all connections using this protocol will be allowed to receive
702 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
703 const struct lws_protocols *protocol)
705 const struct lws_context_per_thread *pt = &context->pt[0];
707 int n, m = context->count_threads;
710 for (n = 0; n < pt->fds_count; n++) {
711 wsi = wsi_from_fd(context, pt->fds[n].fd);
714 if (wsi->protocol == protocol)
715 lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
723 * lws_canonical_hostname() - returns this host's hostname
725 * This is typically used by client code to fill in the host parameter
726 * when making a client connection. You can only call it after the context
729 * @context: Websocket context
731 LWS_VISIBLE extern const char *
732 lws_canonical_hostname(struct lws_context *context)
734 return (const char *)context->canonical_hostname;
737 int user_callback_handle_rxflow(lws_callback_function callback_function,
739 enum lws_callback_reasons reason, void *user,
740 void *in, size_t len)
744 n = callback_function(wsi, reason, user, in, len);
746 n = _lws_rx_flow_control(wsi);
753 * lws_set_proxy() - Setups proxy to lws_context.
754 * @context: pointer to struct lws_context you want set proxy to
755 * @proxy: pointer to c string containing proxy in format address:port
757 * Returns 0 if proxy string was parsed and proxy was setup.
758 * Returns -1 if @proxy is NULL or has incorrect format.
760 * This is only required if your OS does not provide the http_proxy
761 * environment variable (eg, OSX)
763 * IMPORTANT! You should call this function right after creation of the
764 * lws_context and before call to connect. If you call this
765 * function after connect behavior is undefined.
766 * This function will override proxy settings made on lws_context
767 * creation with genenv() call.
771 lws_set_proxy(struct lws_context *context, const char *proxy)
779 p = strchr(proxy, '@');
780 if (p) { /* auth is around */
782 if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
785 strncpy(authstring, proxy, p - proxy);
786 // null termination not needed on input
787 if (lws_b64_encode_string(authstring, (p - proxy),
788 context->proxy_basic_auth_token,
789 sizeof context->proxy_basic_auth_token) < 0)
792 lwsl_notice(" Proxy auth in use\n");
796 context->proxy_basic_auth_token[0] = '\0';
798 strncpy(context->http_proxy_address, proxy,
799 sizeof(context->http_proxy_address) - 1);
800 context->http_proxy_address[
801 sizeof(context->http_proxy_address) - 1] = '\0';
803 p = strchr(context->http_proxy_address, ':');
804 if (!p && !context->http_proxy_port) {
805 lwsl_err("http_proxy needs to be ads:port\n");
811 context->http_proxy_port = atoi(p + 1);
815 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
816 context->http_proxy_port);
821 lwsl_err("proxy auth too long\n");
827 * lws_get_protocol() - Returns a protocol pointer from a websocket
829 * @wsi: pointer to struct websocket you want to know the protocol of
832 * Some apis can act on all live connections of a given protocol,
833 * this is how you can get a pointer to the active protocol if needed.
836 LWS_VISIBLE const struct lws_protocols *
837 lws_get_protocol(struct lws *wsi)
839 return wsi->protocol;
843 lws_is_final_fragment(struct lws *wsi)
845 lwsl_info("%s: final %d, rx pk length %d, draining %d", __func__,
846 wsi->u.ws.final, wsi->u.ws.rx_packet_length, wsi->u.ws.rx_draining_ext);
847 return wsi->u.ws.final && !wsi->u.ws.rx_packet_length && !wsi->u.ws.rx_draining_ext;
850 LWS_VISIBLE unsigned char
851 lws_get_reserved_bits(struct lws *wsi)
853 return wsi->u.ws.rsv;
857 lws_ensure_user_space(struct lws *wsi)
859 lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
863 /* allocate the per-connection user memory (if any) */
865 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
866 wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
867 if (wsi->user_space == NULL) {
868 lwsl_err("Out of memory for conn user space\n");
872 lwsl_info("%s: %p protocol pss %u, user_space=%d\n",
873 __func__, wsi, wsi->protocol->per_session_data_size,
878 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
880 unsigned long long now;
885 for (n = 0; n < LLL_COUNT; n++) {
886 if (level != (1 << n))
888 now = time_in_microseconds() / 100;
889 sprintf(buf, "[%llu:%04d] %s: ",
890 (unsigned long long) now / 10000,
891 (int)(now % 10000), log_level_names[n]);
895 fprintf(stderr, "%s%s", buf, line);
898 LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
902 if (!(log_level & filter))
905 vsnprintf(buf, sizeof(buf), format, vl);
906 buf[sizeof(buf) - 1] = '\0';
908 lwsl_emit(filter, buf);
911 LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
915 va_start(ap, format);
916 _lws_logv(filter, format, ap);
921 * lws_set_log_level() - Set the logging bitfield
922 * @level: OR together the LLL_ debug contexts you want output from
923 * @log_emit_function: NULL to leave it as it is, or a user-supplied
924 * function to perform log string emission instead of
925 * the default stderr one.
927 * log level defaults to "err", "warn" and "notice" contexts enabled and
928 * emission on stderr.
931 LWS_VISIBLE void lws_set_log_level(int level,
932 void (*func)(int level, const char *line))
940 * lws_use_ssl() - Find out if connection is using SSL
941 * @wsi: websocket connection to check
943 * Returns 0 if the connection is not using SSL, 1 if using SSL and
944 * using verified cert, and 2 if using SSL but the cert was not
945 * checked (appears for client wsi told to skip check on connection)
948 lws_is_ssl(struct lws *wsi)
950 #ifdef LWS_OPENSSL_SUPPORT
959 * lws_partial_buffered() - find out if lws buffered the last write
960 * @wsi: websocket connection to check
962 * Returns 1 if you cannot use lws_write because the last
963 * write on this connection is still buffered, and can't be cleared without
964 * returning to the service loop and waiting for the connection to be
967 * If you will try to do >1 lws_write call inside a single
968 * WRITEABLE callback, you must check this after every write and bail if
969 * set, ask for a new writeable callback and continue writing from there.
971 * This is never set at the start of a writeable callback, but any write
976 lws_partial_buffered(struct lws *wsi)
978 return !!wsi->trunc_len;
981 void lws_set_protocol_write_pending(struct lws *wsi,
982 enum lws_pending_protocol_send pend)
984 lwsl_info("setting pps %d\n", pend);
987 lwsl_err("pps overwrite\n");
989 lws_rx_flow_control(wsi, 0);
990 lws_callback_on_writable(wsi);
994 lws_get_peer_write_allowance(struct lws *wsi)
997 /* only if we are using HTTP2 on this connection */
998 if (wsi->mode != LWSCM_HTTP2_SERVING)
1000 /* user is only interested in how much he can send, or that he can't */
1001 if (wsi->u.http2.tx_credit <= 0)
1004 return wsi->u.http2.tx_credit;
1012 lws_union_transition(struct lws *wsi, enum connection_mode mode)
1014 lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
1015 memset(&wsi->u, 0, sizeof(wsi->u));
1019 LWS_VISIBLE struct lws_plat_file_ops *
1020 lws_get_fops(struct lws_context *context)
1022 return &context->fops;
1025 LWS_VISIBLE LWS_EXTERN struct lws_context *
1026 lws_get_context(const struct lws *wsi)
1028 return wsi->context;
1031 LWS_VISIBLE LWS_EXTERN int
1032 lws_get_count_threads(struct lws_context *context)
1034 return context->count_threads;
1037 LWS_VISIBLE LWS_EXTERN void *
1038 lws_wsi_user(struct lws *wsi)
1040 return wsi->user_space;
1043 LWS_VISIBLE LWS_EXTERN void
1044 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1045 unsigned char *buf, size_t len)
1047 unsigned char *p, *start;
1048 int budget = sizeof(wsi->u.ws.ping_payload_buf) - LWS_PRE;
1050 assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
1052 start = p = &wsi->u.ws.ping_payload_buf[LWS_PRE];
1054 *p++ = (((int)status) >> 8) & 0xff;
1055 *p++ = ((int)status) & 0xff;
1058 while (len-- && p < start + budget)
1061 wsi->u.ws.close_in_ping_buffer_len = p - start;
1065 _lws_rx_flow_control(struct lws *wsi)
1067 /* there is no pending change */
1068 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) {
1069 lwsl_debug("%s: no pending change\n", __func__);
1073 /* stuff is still buffered, not ready to really accept new input */
1074 if (wsi->rxflow_buffer) {
1075 /* get ourselves called back to deal with stashed buffer */
1076 lws_callback_on_writable(wsi);
1080 /* pending is cleared, we can change rxflow state */
1082 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1084 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1085 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
1087 /* adjust the pollfd for this wsi */
1089 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
1090 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
1091 lwsl_info("%s: fail\n", __func__);
1095 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
1102 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
1104 static const unsigned char e0f4[] = {
1105 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
1106 0x80 | ((4 - 1) << 2) | 1, /* e1 */
1107 0x80 | ((4 - 1) << 2) | 1, /* e2 */
1108 0x80 | ((4 - 1) << 2) | 1, /* e3 */
1109 0x80 | ((4 - 1) << 2) | 1, /* e4 */
1110 0x80 | ((4 - 1) << 2) | 1, /* e5 */
1111 0x80 | ((4 - 1) << 2) | 1, /* e6 */
1112 0x80 | ((4 - 1) << 2) | 1, /* e7 */
1113 0x80 | ((4 - 1) << 2) | 1, /* e8 */
1114 0x80 | ((4 - 1) << 2) | 1, /* e9 */
1115 0x80 | ((4 - 1) << 2) | 1, /* ea */
1116 0x80 | ((4 - 1) << 2) | 1, /* eb */
1117 0x80 | ((4 - 1) << 2) | 1, /* ec */
1118 0x80 | ((2 - 1) << 2) | 1, /* ed */
1119 0x80 | ((4 - 1) << 2) | 1, /* ee */
1120 0x80 | ((4 - 1) << 2) | 1, /* ef */
1121 0x90 | ((3 - 1) << 2) | 2, /* f0 */
1122 0x80 | ((4 - 1) << 2) | 2, /* f1 */
1123 0x80 | ((4 - 1) << 2) | 2, /* f2 */
1124 0x80 | ((4 - 1) << 2) | 2, /* f3 */
1125 0x80 | ((1 - 1) << 2) | 2, /* f4 */
1128 0x80 | ((4 - 1) << 2) | 0, /* s2 */
1129 0x80 | ((4 - 1) << 2) | 1, /* s3 */
1131 unsigned char s = *state;
1134 unsigned char c = *buf++;
1138 if (c < 0xc2 || c > 0xf4)
1141 s = 0x80 | ((4 - 1) << 2);
1146 if (c < (s & 0xf0) ||
1147 c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
1149 s = e0f4[21 + (s & 3)];
1159 * lws_parse_uri: cut up https:/xxx:yyy/zzz into pieces
1160 * Notice it does so by dropping '\0' into input string
1162 * @p: incoming uri string.. will get written to
1163 * @prot: result pointer for protocol part (https://)
1164 * @ads: result pointer for address part
1165 * @port: result pointer for port part
1166 * @path: result pointer for path part
1169 LWS_VISIBLE LWS_EXTERN int
1170 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
1174 static const char *slash = "/";
1176 /* cut up the location into address, port and path */
1178 while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
1189 if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
1191 else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
1194 while (*p && *p != ':' && *p != '/')
1199 while (*p && *p != '/')
1212 #ifdef LWS_NO_EXTENSIONS
1214 /* we need to provide dummy callbacks for internal exts
1215 * so user code runs when faced with a lib compiled with
1216 * extensions disabled.
1220 lws_extension_callback_pm_deflate(struct lws_context *context,
1221 const struct lws_extension *ext,
1223 enum lws_extension_callback_reasons reason,
1224 void *user, void *in, size_t len)