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 *wsi->timeout_list_prev = wsi->timeout_list;
79 wsi->timeout_list_prev = NULL;
80 wsi->timeout_list = NULL;
85 lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
87 struct lws_context *context;
88 struct lws_context_per_thread *pt;
89 int n, m, ret, old_state;
90 struct lws_tokens eff_buf;
95 context = wsi->context;
96 pt = &context->pt[(int)wsi->tsi];
97 old_state = wsi->state;
99 if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
100 wsi->u.http.fd != LWS_INVALID_FILE) {
101 lwsl_debug("closing http file\n");
102 lws_plat_file_close(wsi, wsi->u.http.fd);
103 wsi->u.http.fd = LWS_INVALID_FILE;
104 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
105 wsi->user_space, NULL, 0);
107 if (wsi->socket_is_permanently_unusable ||
108 reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)
109 goto just_kill_connection;
112 case LWSS_DEAD_SOCKET:
115 /* we tried the polite way... */
116 case LWSS_AWAITING_CLOSE_ACK:
117 goto just_kill_connection;
119 case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
120 if (wsi->trunc_len) {
121 lws_callback_on_writable(wsi);
124 lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
125 goto just_kill_connection;
127 if (wsi->trunc_len) {
128 lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
129 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
130 lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
136 if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
137 wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
138 goto just_kill_connection;
140 if (wsi->mode == LWSCM_HTTP_SERVING)
141 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
142 wsi->user_space, NULL, 0);
145 * are his extensions okay with him closing? Eg he might be a mux
146 * parent and just his ch1 aspect is closing?
149 if (lws_ext_cb_active(wsi,
150 LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
151 lwsl_ext("extension vetoed close\n");
156 * flush any tx pending from extensions, since we may send close packet
157 * if there are problems with send, just nuke the connection
162 eff_buf.token = NULL;
163 eff_buf.token_len = 0;
165 /* show every extension the new incoming data */
167 m = lws_ext_cb_active(wsi,
168 LWS_EXT_CB_FLUSH_PENDING_TX, &eff_buf, 0);
170 lwsl_ext("Extension reports fatal error\n");
171 goto just_kill_connection;
175 * at least one extension told us he has more
176 * to spill, so we will go around again after
180 /* assuming they left us something to send, send it */
182 if (eff_buf.token_len)
183 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
184 eff_buf.token_len) !=
186 lwsl_debug("close: ext spill failed\n");
187 goto just_kill_connection;
192 * signal we are closing, lws_write will
193 * add any necessary version-specific stuff. If the write fails,
194 * no worries we are closing anyway. If we didn't initiate this
195 * close, then our state has been changed to
196 * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
198 * Likewise if it's a second call to close this connection after we
199 * sent the close indication to the peer already, we are in state
200 * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
203 if (old_state == LWSS_ESTABLISHED &&
204 (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
205 (reason != LWS_CLOSE_STATUS_NOSTATUS &&
206 (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
207 lwsl_debug("sending close indication...\n");
209 /* if no prepared close reason, use 1000 and no aux data */
210 if (!wsi->u.ws.close_in_ping_buffer_len) {
211 wsi->u.ws.close_in_ping_buffer_len = 2;
212 wsi->u.ws.ping_payload_buf[LWS_PRE] =
213 (reason >> 16) & 0xff;
214 wsi->u.ws.ping_payload_buf[LWS_PRE + 1] =
218 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[
220 wsi->u.ws.close_in_ping_buffer_len,
224 * we have sent a nice protocol level indication we
225 * now wish to close, we should not send anything more
227 wsi->state = LWSS_AWAITING_CLOSE_ACK;
230 * ...and we should wait for a reply for a bit
233 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 1);
234 lwsl_debug("sent close indication, awaiting ack\n");
239 lwsl_info("close: sending close packet failed, hanging up\n");
241 /* else, the send failed and we should just hang up */
244 just_kill_connection:
246 lwsl_debug("close: just_kill_connection: %p\n", wsi);
249 * we won't be servicing or receiving anything further from this guy
250 * delete socket from the internal poll list if still present
252 lws_ssl_remove_wsi_from_buffered_list(wsi);
253 lws_remove_from_timeout_list(wsi);
255 /* checking return redundant since we anyway close */
256 remove_wsi_socket_from_fds(wsi);
258 wsi->state = LWSS_DEAD_SOCKET;
260 lws_free_set_NULL(wsi->rxflow_buffer);
262 if (old_state == LWSS_ESTABLISHED ||
263 wsi->mode == LWSCM_WS_SERVING ||
264 wsi->mode == LWSCM_WS_CLIENT) {
266 if (wsi->u.ws.rx_draining_ext) {
267 struct lws **w = &pt->rx_draining_ext_list;
269 wsi->u.ws.rx_draining_ext = 0;
270 /* remove us from context draining ext list */
273 *w = wsi->u.ws.rx_draining_ext_list;
276 w = &((*w)->u.ws.rx_draining_ext_list);
278 wsi->u.ws.rx_draining_ext_list = NULL;
281 if (wsi->u.ws.tx_draining_ext) {
282 struct lws **w = &pt->tx_draining_ext_list;
284 wsi->u.ws.tx_draining_ext = 0;
285 /* remove us from context draining ext list */
288 *w = wsi->u.ws.tx_draining_ext_list;
291 w = &((*w)->u.ws.tx_draining_ext_list);
293 wsi->u.ws.tx_draining_ext_list = NULL;
295 lws_free_set_NULL(wsi->u.ws.rx_ubuf);
297 if (wsi->trunc_alloc)
298 /* not going to be completed... nuke it */
299 lws_free_set_NULL(wsi->trunc_alloc);
301 wsi->u.ws.ping_payload_len = 0;
302 wsi->u.ws.ping_pending_flag = 0;
305 /* tell the user it's all over for this guy */
307 if (wsi->protocol && wsi->protocol->callback &&
308 ((old_state == LWSS_ESTABLISHED) ||
309 (old_state == LWSS_RETURNED_CLOSE_ALREADY) ||
310 (old_state == LWSS_AWAITING_CLOSE_ACK) ||
311 (old_state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE))) {
312 lwsl_debug("calling back CLOSED\n");
313 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
314 wsi->user_space, NULL, 0);
315 } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
316 lwsl_debug("calling back CLOSED_HTTP\n");
317 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
318 wsi->user_space, NULL, 0 );
319 } else if (wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
320 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) {
321 lwsl_debug("Connection closed before server reply\n");
322 context->protocols[0].callback(wsi,
323 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
324 wsi->user_space, NULL, 0);
326 lwsl_debug("not calling back closed mode=%d state=%d\n",
327 wsi->mode, old_state);
329 /* deallocate any active extension contexts */
331 if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
332 lwsl_warn("extension destruction failed\n");
334 * inform all extensions in case they tracked this guy out of band
335 * even though not active on him specifically
337 if (lws_ext_cb_all_exts(context, wsi,
338 LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
339 lwsl_warn("ext destroy wsi failed\n");
341 if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) {
343 n = shutdown(wsi->sock, SHUT_RDWR);
345 lwsl_debug("closing: shutdown ret %d\n", LWS_ERRNO);
347 n = compatible_close(wsi->sock);
349 lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
352 compatible_close(wsi->sock);
354 wsi->sock = LWS_SOCK_INVALID;
357 /* outermost destroy notification for wsi (user_space still intact) */
358 context->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
359 wsi->user_space, NULL, 0);
366 interface_to_sa(struct lws_context *context, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
370 ipv6 = LWS_IPV6_ENABLED(context);
374 return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
379 lws_get_addresses(struct lws_context *context, void *ads, char *name,
380 int name_len, char *rip, int rip_len)
383 struct addrinfo ai, *res;
384 struct sockaddr_in addr4;
389 addr4.sin_family = AF_UNSPEC;
392 if (LWS_IPV6_ENABLED(context)) {
393 if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
394 lwsl_err("inet_ntop", strerror(LWS_ERRNO));
398 // Strip off the IPv4 to IPv6 header if one exists
399 if (strncmp(rip, "::ffff:", 7) == 0)
400 memmove(rip, rip + 7, strlen(rip) - 6);
402 getnameinfo((struct sockaddr *)ads,
403 sizeof(struct sockaddr_in6), name,
404 name_len, NULL, 0, 0);
410 struct addrinfo *result;
412 memset(&ai, 0, sizeof ai);
413 ai.ai_family = PF_UNSPEC;
414 ai.ai_socktype = SOCK_STREAM;
415 ai.ai_flags = AI_CANONNAME;
417 if (getnameinfo((struct sockaddr *)ads,
418 sizeof(struct sockaddr_in),
419 name, name_len, NULL, 0, 0))
425 if (getaddrinfo(name, NULL, &ai, &result))
429 while (addr4.sin_family == AF_UNSPEC && res) {
430 switch (res->ai_family) {
432 addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
433 addr4.sin_family = AF_INET;
439 freeaddrinfo(result);
442 if (addr4.sin_family == AF_UNSPEC)
445 lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len);
461 * lws_get_peer_addresses() - Get client address information
462 * @wsi: Local struct lws associated with
463 * @fd: Connection socket descriptor
464 * @name: Buffer to take client address name
465 * @name_len: Length of client address name buffer
466 * @rip: Buffer to take client address IP dotted quad
467 * @rip_len: Length of client address IP buffer
469 * This function fills in @name and @rip with the name and IP of
470 * the client connected with socket descriptor @fd. Names may be
471 * truncated if there is not enough room. If either cannot be
472 * determined, they will be returned as valid zero-length strings.
476 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
477 int name_len, char *rip, int rip_len)
482 struct sockaddr_in6 sin6;
484 struct sockaddr_in sin4;
485 struct lws_context *context = wsi->context;
492 lws_latency_pre(context, wsi);
495 if (LWS_IPV6_ENABLED(context)) {
505 if (getpeername(fd, p, &len) < 0) {
506 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
510 ret = lws_get_addresses(context, p, name, name_len, rip, rip_len);
513 lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
525 * lws_context_user() - get the user data associated with the context
526 * @context: Websocket context
528 * This returns the optional user allocation that can be attached to
529 * the context the sockets live in at context_create time. It's a way
530 * to let all sockets serviced in the same context share data without
531 * using globals statics in the user code.
534 lws_context_user(struct lws_context *context)
536 return context->user_space;
541 * lws_callback_all_protocol() - Callback all connections using
542 * the given protocol with the given reason
544 * @protocol: Protocol whose connections will get callbacks
545 * @reason: Callback reason index
549 lws_callback_all_protocol(struct lws_context *context,
550 const struct lws_protocols *protocol, int reason)
552 struct lws_context_per_thread *pt = &context->pt[0];
554 int n, m = context->count_threads;
557 for (n = 0; n < pt->fds_count; n++) {
558 wsi = wsi_from_fd(context, pt->fds[n].fd);
561 if (wsi->protocol == protocol)
562 protocol->callback(wsi, reason, wsi->user_space, NULL, 0);
571 * lws_set_timeout() - marks the wsi as subject to a timeout
573 * You will not need this unless you are doing something special
575 * @wsi: Websocket connection instance
576 * @reason: timeout reason
577 * @secs: how many seconds
581 lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
587 if (!wsi->pending_timeout) {
588 wsi->timeout_list = wsi->context->timeout_list;
589 if (wsi->timeout_list)
590 wsi->timeout_list->timeout_list_prev = &wsi->timeout_list;
591 wsi->timeout_list_prev = &wsi->context->timeout_list;
592 *wsi->timeout_list_prev = wsi;
595 wsi->pending_timeout_limit = now + secs;
596 wsi->pending_timeout = reason;
599 lws_remove_from_timeout_list(wsi);
606 * lws_get_socket_fd() - returns the socket file descriptor
608 * You will not need this unless you are doing something special
610 * @wsi: Websocket connection instance
614 lws_get_socket_fd(struct lws *wsi)
623 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
624 int ret, int completed)
626 unsigned long long u;
629 u = time_in_microseconds();
632 wsi->latency_start = u;
633 if (!wsi->action_start)
634 wsi->action_start = u;
638 if (wsi->action_start == wsi->latency_start)
640 "Completion first try lat %lluus: %p: ret %d: %s\n",
641 u - wsi->latency_start,
642 (void *)wsi, ret, action);
645 "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
646 u - wsi->action_start,
647 u - wsi->latency_start,
648 (void *)wsi, ret, action);
649 wsi->action_start = 0;
651 sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
652 u - wsi->latency_start, (void *)wsi, ret, action);
654 if (u - wsi->latency_start > context->worst_latency) {
655 context->worst_latency = u - wsi->latency_start;
656 strcpy(context->worst_latency_info, buf);
658 lwsl_latency("%s", buf);
665 * lws_rx_flow_control() - Enable and disable socket servicing for
668 * If the output side of a server process becomes choked, this allows flow
669 * control for the input side.
671 * @wsi: Websocket connection instance to get callback for
672 * @enable: 0 = disable read servicing for this connection, 1 = enable
676 lws_rx_flow_control(struct lws *wsi, int enable)
678 if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
681 lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
682 wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
688 * lws_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
690 * When the user server code realizes it can accept more input, it can
691 * call this to have the RX flow restriction removed from all connections using
692 * the given protocol.
694 * @protocol: all connections using this protocol will be allowed to receive
698 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
699 const struct lws_protocols *protocol)
701 const struct lws_context_per_thread *pt = &context->pt[0];
703 int n, m = context->count_threads;
706 for (n = 0; n < pt->fds_count; n++) {
707 wsi = wsi_from_fd(context, pt->fds[n].fd);
710 if (wsi->protocol == protocol)
711 lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
719 * lws_canonical_hostname() - returns this host's hostname
721 * This is typically used by client code to fill in the host parameter
722 * when making a client connection. You can only call it after the context
725 * @context: Websocket context
727 LWS_VISIBLE extern const char *
728 lws_canonical_hostname(struct lws_context *context)
730 return (const char *)context->canonical_hostname;
733 int user_callback_handle_rxflow(lws_callback_function callback_function,
735 enum lws_callback_reasons reason, void *user,
736 void *in, size_t len)
740 n = callback_function(wsi, reason, user, in, len);
742 n = _lws_rx_flow_control(wsi);
749 * lws_set_proxy() - Setups proxy to lws_context.
750 * @context: pointer to struct lws_context you want set proxy to
751 * @proxy: pointer to c string containing proxy in format address:port
753 * Returns 0 if proxy string was parsed and proxy was setup.
754 * Returns -1 if @proxy is NULL or has incorrect format.
756 * This is only required if your OS does not provide the http_proxy
757 * environment variable (eg, OSX)
759 * IMPORTANT! You should call this function right after creation of the
760 * lws_context and before call to connect. If you call this
761 * function after connect behavior is undefined.
762 * This function will override proxy settings made on lws_context
763 * creation with genenv() call.
767 lws_set_proxy(struct lws_context *context, const char *proxy)
775 p = strchr(proxy, '@');
776 if (p) { /* auth is around */
778 if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
781 strncpy(authstring, proxy, p - proxy);
782 // null termination not needed on input
783 if (lws_b64_encode_string(authstring, (p - proxy),
784 context->proxy_basic_auth_token,
785 sizeof context->proxy_basic_auth_token) < 0)
788 lwsl_notice(" Proxy auth in use\n");
792 context->proxy_basic_auth_token[0] = '\0';
794 strncpy(context->http_proxy_address, proxy,
795 sizeof(context->http_proxy_address) - 1);
796 context->http_proxy_address[
797 sizeof(context->http_proxy_address) - 1] = '\0';
799 p = strchr(context->http_proxy_address, ':');
800 if (!p && !context->http_proxy_port) {
801 lwsl_err("http_proxy needs to be ads:port\n");
807 context->http_proxy_port = atoi(p + 1);
811 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
812 context->http_proxy_port);
817 lwsl_err("proxy auth too long\n");
823 * lws_get_protocol() - Returns a protocol pointer from a websocket
825 * @wsi: pointer to struct websocket you want to know the protocol of
828 * Some apis can act on all live connections of a given protocol,
829 * this is how you can get a pointer to the active protocol if needed.
832 LWS_VISIBLE const struct lws_protocols *
833 lws_get_protocol(struct lws *wsi)
835 return wsi->protocol;
839 lws_is_final_fragment(struct lws *wsi)
841 lwsl_info("%s: final %d, rx pk length %d, draining %d", __func__,
842 wsi->u.ws.final, wsi->u.ws.rx_packet_length, wsi->u.ws.rx_draining_ext);
843 return wsi->u.ws.final && !wsi->u.ws.rx_packet_length && !wsi->u.ws.rx_draining_ext;
846 LWS_VISIBLE unsigned char
847 lws_get_reserved_bits(struct lws *wsi)
849 return wsi->u.ws.rsv;
853 lws_ensure_user_space(struct lws *wsi)
855 lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
859 /* allocate the per-connection user memory (if any) */
861 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
862 wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
863 if (wsi->user_space == NULL) {
864 lwsl_err("Out of memory for conn user space\n");
868 lwsl_info("%s: %p protocol pss %u, user_space=%d\n",
869 __func__, wsi, wsi->protocol->per_session_data_size,
874 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
876 unsigned long long now;
881 for (n = 0; n < LLL_COUNT; n++) {
882 if (level != (1 << n))
884 now = time_in_microseconds() / 100;
885 sprintf(buf, "[%llu:%04d] %s: ",
886 (unsigned long long) now / 10000,
887 (int)(now % 10000), log_level_names[n]);
891 fprintf(stderr, "%s%s", buf, line);
894 LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
898 if (!(log_level & filter))
901 vsnprintf(buf, sizeof(buf), format, vl);
902 buf[sizeof(buf) - 1] = '\0';
904 lwsl_emit(filter, buf);
907 LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
911 va_start(ap, format);
912 _lws_logv(filter, format, ap);
917 * lws_set_log_level() - Set the logging bitfield
918 * @level: OR together the LLL_ debug contexts you want output from
919 * @log_emit_function: NULL to leave it as it is, or a user-supplied
920 * function to perform log string emission instead of
921 * the default stderr one.
923 * log level defaults to "err", "warn" and "notice" contexts enabled and
924 * emission on stderr.
927 LWS_VISIBLE void lws_set_log_level(int level,
928 void (*func)(int level, const char *line))
936 * lws_use_ssl() - Find out if connection is using SSL
937 * @wsi: websocket connection to check
939 * Returns 0 if the connection is not using SSL, 1 if using SSL and
940 * using verified cert, and 2 if using SSL but the cert was not
941 * checked (appears for client wsi told to skip check on connection)
944 lws_is_ssl(struct lws *wsi)
946 #ifdef LWS_OPENSSL_SUPPORT
955 * lws_partial_buffered() - find out if lws buffered the last write
956 * @wsi: websocket connection to check
958 * Returns 1 if you cannot use lws_write because the last
959 * write on this connection is still buffered, and can't be cleared without
960 * returning to the service loop and waiting for the connection to be
963 * If you will try to do >1 lws_write call inside a single
964 * WRITEABLE callback, you must check this after every write and bail if
965 * set, ask for a new writeable callback and continue writing from there.
967 * This is never set at the start of a writeable callback, but any write
972 lws_partial_buffered(struct lws *wsi)
974 return !!wsi->trunc_len;
977 void lws_set_protocol_write_pending(struct lws *wsi,
978 enum lws_pending_protocol_send pend)
980 lwsl_info("setting pps %d\n", pend);
983 lwsl_err("pps overwrite\n");
985 lws_rx_flow_control(wsi, 0);
986 lws_callback_on_writable(wsi);
990 lws_get_peer_write_allowance(struct lws *wsi)
993 /* only if we are using HTTP2 on this connection */
994 if (wsi->mode != LWSCM_HTTP2_SERVING)
996 /* user is only interested in how much he can send, or that he can't */
997 if (wsi->u.http2.tx_credit <= 0)
1000 return wsi->u.http2.tx_credit;
1008 lws_union_transition(struct lws *wsi, enum connection_mode mode)
1010 lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
1011 memset(&wsi->u, 0, sizeof(wsi->u));
1015 LWS_VISIBLE struct lws_plat_file_ops *
1016 lws_get_fops(struct lws_context *context)
1018 return &context->fops;
1021 LWS_VISIBLE LWS_EXTERN struct lws_context *
1022 lws_get_context(const struct lws *wsi)
1024 return wsi->context;
1027 LWS_VISIBLE LWS_EXTERN int
1028 lws_get_count_threads(struct lws_context *context)
1030 return context->count_threads;
1033 LWS_VISIBLE LWS_EXTERN void *
1034 lws_wsi_user(struct lws *wsi)
1036 return wsi->user_space;
1039 LWS_VISIBLE LWS_EXTERN void
1040 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1041 unsigned char *buf, size_t len)
1043 unsigned char *p, *start;
1044 int budget = sizeof(wsi->u.ws.ping_payload_buf) - LWS_PRE;
1046 assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
1048 start = p = &wsi->u.ws.ping_payload_buf[LWS_PRE];
1050 *p++ = (((int)status) >> 8) & 0xff;
1051 *p++ = ((int)status) & 0xff;
1054 while (len-- && p < start + budget)
1057 wsi->u.ws.close_in_ping_buffer_len = p - start;
1061 _lws_rx_flow_control(struct lws *wsi)
1063 /* there is no pending change */
1064 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) {
1065 lwsl_debug("%s: no pending change\n", __func__);
1069 /* stuff is still buffered, not ready to really accept new input */
1070 if (wsi->rxflow_buffer) {
1071 /* get ourselves called back to deal with stashed buffer */
1072 lws_callback_on_writable(wsi);
1076 /* pending is cleared, we can change rxflow state */
1078 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1080 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1081 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
1083 /* adjust the pollfd for this wsi */
1085 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
1086 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
1087 lwsl_info("%s: fail\n", __func__);
1091 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
1098 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
1100 static const unsigned char e0f4[] = {
1101 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
1102 0x80 | ((4 - 1) << 2) | 1, /* e1 */
1103 0x80 | ((4 - 1) << 2) | 1, /* e2 */
1104 0x80 | ((4 - 1) << 2) | 1, /* e3 */
1105 0x80 | ((4 - 1) << 2) | 1, /* e4 */
1106 0x80 | ((4 - 1) << 2) | 1, /* e5 */
1107 0x80 | ((4 - 1) << 2) | 1, /* e6 */
1108 0x80 | ((4 - 1) << 2) | 1, /* e7 */
1109 0x80 | ((4 - 1) << 2) | 1, /* e8 */
1110 0x80 | ((4 - 1) << 2) | 1, /* e9 */
1111 0x80 | ((4 - 1) << 2) | 1, /* ea */
1112 0x80 | ((4 - 1) << 2) | 1, /* eb */
1113 0x80 | ((4 - 1) << 2) | 1, /* ec */
1114 0x80 | ((2 - 1) << 2) | 1, /* ed */
1115 0x80 | ((4 - 1) << 2) | 1, /* ee */
1116 0x80 | ((4 - 1) << 2) | 1, /* ef */
1117 0x90 | ((3 - 1) << 2) | 2, /* f0 */
1118 0x80 | ((4 - 1) << 2) | 2, /* f1 */
1119 0x80 | ((4 - 1) << 2) | 2, /* f2 */
1120 0x80 | ((4 - 1) << 2) | 2, /* f3 */
1121 0x80 | ((1 - 1) << 2) | 2, /* f4 */
1124 0x80 | ((4 - 1) << 2) | 0, /* s2 */
1125 0x80 | ((4 - 1) << 2) | 1, /* s3 */
1127 unsigned char s = *state;
1130 unsigned char c = *buf++;
1134 if (c < 0xc2 || c > 0xf4)
1137 s = 0x80 | ((4 - 1) << 2);
1142 if (c < (s & 0xf0) ||
1143 c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
1145 s = e0f4[21 + (s & 3)];
1155 * lws_parse_uri: cut up https:/xxx:yyy/zzz into pieces
1156 * Notice it does so by dropping '\0' into input string
1158 * @p: incoming uri string.. will get written to
1159 * @prot: result pointer for protocol part (https://)
1160 * @ads: result pointer for address part
1161 * @port: result pointer for port part
1162 * @path: result pointer for path part
1165 LWS_VISIBLE LWS_EXTERN int
1166 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
1170 static const char *slash = "/";
1172 /* cut up the location into address, port and path */
1174 while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
1185 if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
1187 else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
1190 while (*p && *p != ':' && *p != '/')
1195 while (*p && *p != '/')
1208 #ifdef LWS_NO_EXTENSIONS
1210 /* we need to provide dummy callbacks for internal exts
1211 * so user code runs when faced with a lib compiled with
1212 * extensions disabled.
1216 lws_extension_callback_pm_deflate(struct lws_context *context,
1217 const struct lws_extension *ext,
1219 enum lws_extension_callback_reasons reason,
1220 void *user, void *in, size_t len)