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);
72 lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
74 struct lws_context *context = wsi->context;
75 int n, m, ret, old_state;
76 struct lws_tokens eff_buf;
81 old_state = wsi->state;
83 if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
84 wsi->u.http.fd != LWS_INVALID_FILE) {
85 lwsl_debug("closing http file\n");
86 lws_plat_file_close(wsi, wsi->u.http.fd);
87 wsi->u.http.fd = LWS_INVALID_FILE;
88 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
89 wsi->user_space, NULL, 0);
91 if (wsi->socket_is_permanently_unusable ||
92 reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)
93 goto just_kill_connection;
96 case LWSS_DEAD_SOCKET:
99 /* we tried the polite way... */
100 case LWSS_AWAITING_CLOSE_ACK:
101 goto just_kill_connection;
103 case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
104 if (wsi->trunc_len) {
105 lws_callback_on_writable(wsi);
108 lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
109 goto just_kill_connection;
111 if (wsi->trunc_len) {
112 lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
113 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
114 lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
120 if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
121 wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
122 goto just_kill_connection;
124 if (wsi->mode == LWSCM_HTTP_SERVING)
125 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
126 wsi->user_space, NULL, 0);
129 * are his extensions okay with him closing? Eg he might be a mux
130 * parent and just his ch1 aspect is closing?
133 if (lws_ext_cb_wsi_active_exts(wsi,
134 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
135 lwsl_ext("extension vetoed close\n");
140 * flush any tx pending from extensions, since we may send close packet
141 * if there are problems with send, just nuke the connection
146 eff_buf.token = NULL;
147 eff_buf.token_len = 0;
149 /* show every extension the new incoming data */
151 m = lws_ext_cb_wsi_active_exts(wsi,
152 LWS_EXT_CALLBACK_FLUSH_PENDING_TX, &eff_buf, 0);
154 lwsl_ext("Extension reports fatal error\n");
155 goto just_kill_connection;
159 * at least one extension told us he has more
160 * to spill, so we will go around again after
164 /* assuming they left us something to send, send it */
166 if (eff_buf.token_len)
167 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
168 eff_buf.token_len) !=
170 lwsl_debug("close: ext spill failed\n");
171 goto just_kill_connection;
176 * signal we are closing, lws_write will
177 * add any necessary version-specific stuff. If the write fails,
178 * no worries we are closing anyway. If we didn't initiate this
179 * close, then our state has been changed to
180 * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
182 * Likewise if it's a second call to close this connection after we
183 * sent the close indication to the peer already, we are in state
184 * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
187 if (old_state == LWSS_ESTABLISHED &&
188 (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
189 (reason != LWS_CLOSE_STATUS_NOSTATUS &&
190 (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
191 lwsl_debug("sending close indication...\n");
193 /* if no prepared close reason, use 1000 and no aux data */
194 if (!wsi->u.ws.close_in_ping_buffer_len) {
195 wsi->u.ws.close_in_ping_buffer_len = 2;
196 wsi->u.ws.ping_payload_buf[LWS_SEND_BUFFER_PRE_PADDING] =
197 (reason >> 16) & 0xff;
198 wsi->u.ws.ping_payload_buf[LWS_SEND_BUFFER_PRE_PADDING + 1] =
202 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[
203 LWS_SEND_BUFFER_PRE_PADDING],
204 wsi->u.ws.close_in_ping_buffer_len,
208 * we have sent a nice protocol level indication we
209 * now wish to close, we should not send anything more
211 wsi->state = LWSS_AWAITING_CLOSE_ACK;
214 * ...and we should wait for a reply for a bit
217 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 1);
218 lwsl_debug("sent close indication, awaiting ack\n");
223 lwsl_info("close: sending close packet failed, hanging up\n");
225 /* else, the send failed and we should just hang up */
228 just_kill_connection:
230 lwsl_debug("close: just_kill_connection\n");
233 * we won't be servicing or receiving anything further from this guy
234 * delete socket from the internal poll list if still present
236 lws_ssl_remove_wsi_from_buffered_list(wsi);
238 /* checking return redundant since we anyway close */
239 remove_wsi_socket_from_fds(wsi);
241 wsi->state = LWSS_DEAD_SOCKET;
243 lws_free_set_NULL(wsi->rxflow_buffer);
245 if (old_state == LWSS_ESTABLISHED ||
246 wsi->mode == LWSCM_WS_SERVING ||
247 wsi->mode == LWSCM_WS_CLIENT) {
249 lws_free_set_NULL(wsi->u.ws.rx_user_buffer);
251 if (wsi->trunc_alloc)
252 /* not going to be completed... nuke it */
253 lws_free_set_NULL(wsi->trunc_alloc);
255 wsi->u.ws.ping_payload_len = 0;
256 wsi->u.ws.ping_pending_flag = 0;
259 /* tell the user it's all over for this guy */
261 if (wsi->protocol && wsi->protocol->callback &&
262 ((old_state == LWSS_ESTABLISHED) ||
263 (old_state == LWSS_RETURNED_CLOSE_ALREADY) ||
264 (old_state == LWSS_AWAITING_CLOSE_ACK) ||
265 (old_state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE))) {
266 lwsl_debug("calling back CLOSED\n");
267 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
268 wsi->user_space, NULL, 0);
269 } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
270 lwsl_debug("calling back CLOSED_HTTP\n");
271 context->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
272 wsi->user_space, NULL, 0 );
273 } else if (wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
274 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) {
275 lwsl_debug("Connection closed before server reply\n");
276 context->protocols[0].callback(wsi,
277 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
278 wsi->user_space, NULL, 0);
280 lwsl_debug("not calling back closed mode=%d state=%d\n",
281 wsi->mode, old_state);
283 /* deallocate any active extension contexts */
285 if (lws_ext_cb_wsi_active_exts(wsi, LWS_EXT_CALLBACK_DESTROY, NULL, 0) < 0)
286 lwsl_warn("extension destruction failed\n");
287 #ifndef LWS_NO_EXTENSIONS
288 for (n = 0; n < wsi->count_active_extensions; n++)
289 lws_free(wsi->active_extensions_user[n]);
292 * inform all extensions in case they tracked this guy out of band
293 * even though not active on him specifically
295 if (lws_ext_cb_all_exts(context, wsi,
296 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
297 lwsl_warn("ext destroy wsi failed\n");
299 if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) {
301 n = shutdown(wsi->sock, SHUT_RDWR);
303 lwsl_debug("closing: shutdown ret %d\n", LWS_ERRNO);
305 n = compatible_close(wsi->sock);
307 lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
310 compatible_close(wsi->sock);
312 wsi->sock = LWS_SOCK_INVALID;
315 /* outermost destroy notification for wsi (user_space still intact) */
316 context->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
317 wsi->user_space, NULL, 0);
323 lws_get_addresses(struct lws_context *context, void *ads, char *name,
324 int name_len, char *rip, int rip_len)
327 struct addrinfo ai, *res;
328 struct sockaddr_in addr4;
333 addr4.sin_family = AF_UNSPEC;
336 if (LWS_IPV6_ENABLED(context)) {
337 if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
338 lwsl_err("inet_ntop", strerror(LWS_ERRNO));
342 // Strip off the IPv4 to IPv6 header if one exists
343 if (strncmp(rip, "::ffff:", 7) == 0)
344 memmove(rip, rip + 7, strlen(rip) - 6);
346 getnameinfo((struct sockaddr *)ads,
347 sizeof(struct sockaddr_in6), name,
348 name_len, NULL, 0, 0);
354 struct addrinfo *result;
356 memset(&ai, 0, sizeof ai);
357 ai.ai_family = PF_UNSPEC;
358 ai.ai_socktype = SOCK_STREAM;
359 ai.ai_flags = AI_CANONNAME;
361 if (getnameinfo((struct sockaddr *)ads,
362 sizeof(struct sockaddr_in),
363 name, name_len, NULL, 0, 0))
369 if (getaddrinfo(name, NULL, &ai, &result))
373 while (addr4.sin_family == AF_UNSPEC && res) {
374 switch (res->ai_family) {
376 addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
377 addr4.sin_family = AF_INET;
383 freeaddrinfo(result);
386 if (addr4.sin_family == AF_UNSPEC)
389 lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len);
405 * lws_get_peer_addresses() - Get client address information
406 * @wsi: Local struct lws associated with
407 * @fd: Connection socket descriptor
408 * @name: Buffer to take client address name
409 * @name_len: Length of client address name buffer
410 * @rip: Buffer to take client address IP dotted quad
411 * @rip_len: Length of client address IP buffer
413 * This function fills in @name and @rip with the name and IP of
414 * the client connected with socket descriptor @fd. Names may be
415 * truncated if there is not enough room. If either cannot be
416 * determined, they will be returned as valid zero-length strings.
420 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
421 int name_len, char *rip, int rip_len)
426 struct sockaddr_in6 sin6;
428 struct sockaddr_in sin4;
429 struct lws_context *context = wsi->context;
436 lws_latency_pre(context, wsi);
439 if (LWS_IPV6_ENABLED(context)) {
449 if (getpeername(fd, p, &len) < 0) {
450 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
454 ret = lws_get_addresses(context, p, name, name_len, rip, rip_len);
457 lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
469 * lws_context_user() - get the user data associated with the context
470 * @context: Websocket context
472 * This returns the optional user allocation that can be attached to
473 * the context the sockets live in at context_create time. It's a way
474 * to let all sockets serviced in the same context share data without
475 * using globals statics in the user code.
478 lws_context_user(struct lws_context *context)
480 return context->user_space;
485 * lws_callback_all_protocol() - Callback all connections using
486 * the given protocol with the given reason
488 * @protocol: Protocol whose connections will get callbacks
489 * @reason: Callback reason index
493 lws_callback_all_protocol(struct lws_context *context,
494 const struct lws_protocols *protocol, int reason)
499 for (n = 0; n < context->fds_count; n++) {
500 wsi = wsi_from_fd(context, context->fds[n].fd);
503 if (wsi->protocol == protocol)
504 protocol->callback(wsi, reason, wsi->user_space, NULL, 0);
511 * lws_set_timeout() - marks the wsi as subject to a timeout
513 * You will not need this unless you are doing something special
515 * @wsi: Websocket connection instance
516 * @reason: timeout reason
517 * @secs: how many seconds
521 lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
527 wsi->pending_timeout_limit = now + secs;
528 wsi->pending_timeout = reason;
535 * lws_get_socket_fd() - returns the socket file descriptor
537 * You will not need this unless you are doing something special
539 * @wsi: Websocket connection instance
543 lws_get_socket_fd(struct lws *wsi)
552 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
553 int ret, int completed)
555 unsigned long long u;
558 u = time_in_microseconds();
561 wsi->latency_start = u;
562 if (!wsi->action_start)
563 wsi->action_start = u;
567 if (wsi->action_start == wsi->latency_start)
569 "Completion first try lat %lluus: %p: ret %d: %s\n",
570 u - wsi->latency_start,
571 (void *)wsi, ret, action);
574 "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
575 u - wsi->action_start,
576 u - wsi->latency_start,
577 (void *)wsi, ret, action);
578 wsi->action_start = 0;
580 sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
581 u - wsi->latency_start, (void *)wsi, ret, action);
583 if (u - wsi->latency_start > context->worst_latency) {
584 context->worst_latency = u - wsi->latency_start;
585 strcpy(context->worst_latency_info, buf);
587 lwsl_latency("%s", buf);
594 * lws_rx_flow_control() - Enable and disable socket servicing for
597 * If the output side of a server process becomes choked, this allows flow
598 * control for the input side.
600 * @wsi: Websocket connection instance to get callback for
601 * @enable: 0 = disable read servicing for this connection, 1 = enable
605 lws_rx_flow_control(struct lws *wsi, int enable)
607 if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
610 lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
611 wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
617 * lws_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
619 * When the user server code realizes it can accept more input, it can
620 * call this to have the RX flow restriction removed from all connections using
621 * the given protocol.
623 * @protocol: all connections using this protocol will be allowed to receive
627 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
628 const struct lws_protocols *protocol)
633 for (n = 0; n < context->fds_count; n++) {
634 wsi = wsi_from_fd(context, context->fds[n].fd);
637 if (wsi->protocol == protocol)
638 lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
644 * lws_canonical_hostname() - returns this host's hostname
646 * This is typically used by client code to fill in the host parameter
647 * when making a client connection. You can only call it after the context
650 * @context: Websocket context
652 LWS_VISIBLE extern const char *
653 lws_canonical_hostname(struct lws_context *context)
655 return (const char *)context->canonical_hostname;
658 int user_callback_handle_rxflow(callback_function callback_function,
660 enum lws_callback_reasons reason, void *user,
661 void *in, size_t len)
665 n = callback_function(wsi, reason, user, in, len);
667 n = _lws_rx_flow_control(wsi);
674 * lws_set_proxy() - Setups proxy to lws_context.
675 * @context: pointer to struct lws_context you want set proxy to
676 * @proxy: pointer to c string containing proxy in format address:port
678 * Returns 0 if proxy string was parsed and proxy was setup.
679 * Returns -1 if @proxy is NULL or has incorrect format.
681 * This is only required if your OS does not provide the http_proxy
682 * environment variable (eg, OSX)
684 * IMPORTANT! You should call this function right after creation of the
685 * lws_context and before call to connect. If you call this
686 * function after connect behavior is undefined.
687 * This function will override proxy settings made on lws_context
688 * creation with genenv() call.
692 lws_set_proxy(struct lws_context *context, const char *proxy)
700 p = strchr(proxy, '@');
701 if (p) { /* auth is around */
703 if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
706 strncpy(authstring, proxy, p - proxy);
707 // null termination not needed on input
708 if (lws_b64_encode_string(authstring, (p - proxy),
709 context->proxy_basic_auth_token,
710 sizeof context->proxy_basic_auth_token) < 0)
713 lwsl_notice(" Proxy auth in use\n");
717 context->proxy_basic_auth_token[0] = '\0';
719 strncpy(context->http_proxy_address, proxy,
720 sizeof(context->http_proxy_address) - 1);
721 context->http_proxy_address[
722 sizeof(context->http_proxy_address) - 1] = '\0';
724 p = strchr(context->http_proxy_address, ':');
725 if (!p && !context->http_proxy_port) {
726 lwsl_err("http_proxy needs to be ads:port\n");
732 context->http_proxy_port = atoi(p + 1);
736 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
737 context->http_proxy_port);
742 lwsl_err("proxy auth too long\n");
748 * lws_get_protocol() - Returns a protocol pointer from a websocket
750 * @wsi: pointer to struct websocket you want to know the protocol of
753 * Some apis can act on all live connections of a given protocol,
754 * this is how you can get a pointer to the active protocol if needed.
757 LWS_VISIBLE const struct lws_protocols *
758 lws_get_protocol(struct lws *wsi)
760 return wsi->protocol;
764 lws_is_final_fragment(struct lws *wsi)
766 return wsi->u.ws.final && !wsi->u.ws.rx_packet_length;
769 LWS_VISIBLE unsigned char
770 lws_get_reserved_bits(struct lws *wsi)
772 return wsi->u.ws.rsv;
776 lws_ensure_user_space(struct lws *wsi)
778 lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
782 /* allocate the per-connection user memory (if any) */
784 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
785 wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
786 if (wsi->user_space == NULL) {
787 lwsl_err("Out of memory for conn user space\n");
791 lwsl_info("%s: %p protocol pss %u, user_space=%d\n",
792 __func__, wsi, wsi->protocol->per_session_data_size,
797 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
799 unsigned long long now;
804 for (n = 0; n < LLL_COUNT; n++) {
805 if (level != (1 << n))
807 now = time_in_microseconds() / 100;
808 sprintf(buf, "[%llu:%04d] %s: ",
809 (unsigned long long) now / 10000,
810 (int)(now % 10000), log_level_names[n]);
814 fprintf(stderr, "%s%s", buf, line);
817 LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
821 if (!(log_level & filter))
824 vsnprintf(buf, sizeof(buf), format, vl);
825 buf[sizeof(buf) - 1] = '\0';
827 lwsl_emit(filter, buf);
830 LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
834 va_start(ap, format);
835 _lws_logv(filter, format, ap);
840 * lws_set_log_level() - Set the logging bitfield
841 * @level: OR together the LLL_ debug contexts you want output from
842 * @log_emit_function: NULL to leave it as it is, or a user-supplied
843 * function to perform log string emission instead of
844 * the default stderr one.
846 * log level defaults to "err", "warn" and "notice" contexts enabled and
847 * emission on stderr.
850 LWS_VISIBLE void lws_set_log_level(int level,
851 void (*func)(int level, const char *line))
859 * lws_use_ssl() - Find out if connection is using SSL
860 * @wsi: websocket connection to check
862 * Returns 0 if the connection is not using SSL, 1 if using SSL and
863 * using verified cert, and 2 if using SSL but the cert was not
864 * checked (appears for client wsi told to skip check on connection)
867 lws_is_ssl(struct lws *wsi)
869 #ifdef LWS_OPENSSL_SUPPORT
878 * lws_partial_buffered() - find out if lws buffered the last write
879 * @wsi: websocket connection to check
881 * Returns 1 if you cannot use lws_write because the last
882 * write on this connection is still buffered, and can't be cleared without
883 * returning to the service loop and waiting for the connection to be
886 * If you will try to do >1 lws_write call inside a single
887 * WRITEABLE callback, you must check this after every write and bail if
888 * set, ask for a new writeable callback and continue writing from there.
890 * This is never set at the start of a writeable callback, but any write
895 lws_partial_buffered(struct lws *wsi)
897 return !!wsi->trunc_len;
900 void lws_set_protocol_write_pending(struct lws *wsi,
901 enum lws_pending_protocol_send pend)
903 lwsl_info("setting pps %d\n", pend);
906 lwsl_err("pps overwrite\n");
908 lws_rx_flow_control(wsi, 0);
909 lws_callback_on_writable(wsi);
913 lws_get_peer_write_allowance(struct lws *wsi)
916 /* only if we are using HTTP2 on this connection */
917 if (wsi->mode != LWSCM_HTTP2_SERVING)
919 /* user is only interested in how much he can send, or that he can't */
920 if (wsi->u.http2.tx_credit <= 0)
923 return wsi->u.http2.tx_credit;
931 lws_union_transition(struct lws *wsi, enum connection_mode mode)
933 lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
934 memset(&wsi->u, 0, sizeof(wsi->u));
938 LWS_VISIBLE struct lws_plat_file_ops *
939 lws_get_fops(struct lws_context *context)
941 return &context->fops;
944 LWS_VISIBLE LWS_EXTERN struct lws_context *
945 lws_get_context(const struct lws *wsi)
950 LWS_VISIBLE LWS_EXTERN void *
951 lws_wsi_user(struct lws *wsi)
953 return wsi->user_space;
956 LWS_VISIBLE LWS_EXTERN void
957 lws_close_reason(struct lws *wsi, enum lws_close_status status,
958 unsigned char *buf, size_t len)
960 unsigned char *p, *start;
961 int budget = sizeof(wsi->u.ws.ping_payload_buf) -
962 LWS_SEND_BUFFER_PRE_PADDING;
964 assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
966 start = p = &wsi->u.ws.ping_payload_buf[LWS_SEND_BUFFER_PRE_PADDING];
968 *p++ = (((int)status) >> 8) & 0xff;
969 *p++ = ((int)status) & 0xff;
972 while (len-- && p < start + budget)
975 wsi->u.ws.close_in_ping_buffer_len = p - start;