2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2015 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 #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
23 #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
25 /* old (pre 1.6) api name compatibility defines */
27 #define libwebsocket_create_context lws_create_context
28 #define libwebsocket_set_proxy lws_set_proxy
29 #define libwebsocket_context_destroy lws_context_destroy
30 #define libwebsocket_service lws_service
31 #define libwebsocket_cancel_service lws_cancel_service
32 #define libwebsocket_sigint_cfg lws_sigint_cfg
33 #define libwebsocket_initloop lws_initloop
34 #define libwebsocket_sigint_cb lws_sigint_cb
35 #define libwebsocket_service_fd lws_service_fd
36 #define libwebsocket_context_user lws_context_user
37 #define libwebsocket_set_timeout lws_set_timeout
38 #define libwebsocket_write lws_write
39 #define libwebsockets_serve_http_file_fragment lws_serve_http_file_fragment
40 #define libwebsockets_serve_http_file lws_serve_http_file
41 #define libwebsockets_return_http_status lws_return_http_status
42 #define libwebsockets_get_protocol lws_get_protocol
43 #define libwebsocket_callback_on_writable_all_protocol lws_callback_on_writable_all_protocol
44 #define libwebsocket_callback_on_writable lws_callback_on_writable
45 #define libwebsocket_callback_all_protocol lws_callback_all_protocol
46 #define libwebsocket_get_socket_fd lws_get_socket_fd
47 #define libwebsocket_is_final_fragment lws_is_final_fragment
48 #define libwebsocket_get_reserved_bits lws_get_reserved_bits
49 #define libwebsocket_rx_flow_control lws_rx_flow_control
50 #define libwebsocket_rx_flow_allow_all_protocol lws_rx_flow_allow_all_protocol
51 #define libwebsockets_remaining_packet_payload lws_remaining_packet_payload
52 #define libwebsocket_client_connect lws_client_connect
53 #define libwebsocket_canonical_hostname lws_canonical_hostname
54 #define libwebsockets_get_peer_addresses lws_get_peer_addresses
55 #define libwebsockets_get_random lws_get_random
56 #define libwebsockets_SHA1 lws_SHA1
57 #define libwebsocket_read lws_read
58 #define libwebsocket_get_internal_extensions lws_get_internal_extensions
59 #define libwebsocket_write_protocol lws_write_protocol
61 #define libwebsocket_protocols lws_protocols
62 #define libwebsocket_extension lws_extension
63 #define libwebsocket_context lws_context
64 #define libwebsocket_pollfd lws_pollfd
65 #define libwebsocket_callback_reasons lws_callback_reasons
66 #define libwebsocket lws
72 #include "mbed-drivers/mbed.h"
73 #include "sal-iface-eth/EthernetInterface.h"
74 #include "sockets/TCPListener.h"
75 #include "sal-stack-lwip/lwipv4_init.h"
78 const int SERVER_PORT = 80;
79 const int BUFFER_SIZE = 4096;
81 using namespace mbed::Sockets::v0;
92 awaiting_on_writeable(0)
97 void set_wsi(struct lws *_wsi) { wsi = _wsi; }
98 int actual_onRX(Socket *s);
100 void onError(Socket *s, socket_error_t err);
101 void onDisconnect(TCPStream *s);
102 void onSent(Socket *s, uint16_t len);
103 void serialized_writeable(struct lws *wsi);
110 char buffer[BUFFER_SIZE];
112 char awaiting_on_writeable;
115 class lws_conn_listener : lws_conn {
118 srv(SOCKET_STACK_LWIP_IPV4)
120 srv.setOnError(TCPStream::ErrorHandler_t(this, &lws_conn_listener::onError));
123 void start(const uint16_t port);
126 void onRX(Socket *s);
127 void onError(Socket *s, socket_error_t err);
128 void onIncoming(TCPListener *s, void *impl);
129 void onDisconnect(TCPStream *s);
142 #ifdef MBED_OPERATORS
148 #include "lws_config.h"
150 #if defined(WIN32) || defined(_WIN32)
151 #if (WINVER < 0x0501)
154 #define WINVER 0x0501
155 #define _WIN32_WINNT WINVER
157 #ifndef WIN32_LEAN_AND_MEAN
158 #define WIN32_LEAN_AND_MEAN
160 #include <winsock2.h>
161 #include <ws2tcpip.h>
166 #define strcasecmp stricmp
167 #define getdtablesize() 30000
173 #define LWS_EXTERN extern __declspec(dllexport)
175 #define LWS_EXTERN extern __declspec(dllimport)
184 #ifndef MBED_OPERATORS
188 #define getdtablesize() (20)
191 #if defined(__GNUC__)
192 #define LWS_VISIBLE __attribute__((visibility("default")))
197 #if defined(__ANDROID__)
198 #define getdtablesize() 1024
205 #endif /* LWS_USE_LIBEV */
208 #define LWS_EXTERN extern
214 #include <sys/time.h>
218 #ifdef LWS_OPENSSL_SUPPORT
220 #ifdef USE_OLD_CYASSL
221 #include <cyassl/openssl/ssl.h>
223 #include <wolfssl/openssl/ssl.h>
224 #endif /* not USE_OLD_CYASSL */
226 #include <openssl/ssl.h>
227 #endif /* not USE_WOLFSSL */
230 #define CONTEXT_PORT_NO_LISTEN -1
231 #define MAX_MUX_RECURSION 2
233 enum lws_log_levels {
243 LLL_LATENCY = 1 << 9,
245 LLL_COUNT = 10 /* set to count of valid flags */
248 LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
249 LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
251 /* notice, warn and log are always compiled in */
252 #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
253 #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
254 #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
256 * weaker logging can be deselected at configure time using --disable-debug
257 * that gets rid of the overhead of checking while keeping _warn and _err
262 #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
263 #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
264 #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
265 #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
266 #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
267 #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
268 #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
269 LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
273 #define lwsl_info(...) {}
274 #define lwsl_debug(...) {}
275 #define lwsl_parser(...) {}
276 #define lwsl_header(...) {}
277 #define lwsl_ext(...) {}
278 #define lwsl_client(...) {}
279 #define lwsl_latency(...) {}
280 #define lwsl_hexdump(a, b)
284 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
286 /* api change list for user code to test against */
288 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
290 /* the struct lws_protocols has the id field present */
291 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
293 /* you can call lws_get_peer_write_allowance */
294 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
296 /* extra parameter introduced in 917f43ab821 */
297 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
299 enum lws_context_options {
300 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
301 LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4,
302 LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = 8,
303 LWS_SERVER_OPTION_LIBEV = 16,
304 LWS_SERVER_OPTION_DISABLE_IPV6 = 32,
305 LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = 64,
306 LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = 128,
309 enum lws_callback_reasons {
310 LWS_CALLBACK_ESTABLISHED,
311 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
312 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
313 LWS_CALLBACK_CLIENT_ESTABLISHED,
315 LWS_CALLBACK_CLOSED_HTTP,
316 LWS_CALLBACK_RECEIVE,
317 LWS_CALLBACK_RECEIVE_PONG,
318 LWS_CALLBACK_CLIENT_RECEIVE,
319 LWS_CALLBACK_CLIENT_RECEIVE_PONG,
320 LWS_CALLBACK_CLIENT_WRITEABLE,
321 LWS_CALLBACK_SERVER_WRITEABLE,
323 LWS_CALLBACK_HTTP_BODY,
324 LWS_CALLBACK_HTTP_BODY_COMPLETION,
325 LWS_CALLBACK_HTTP_FILE_COMPLETION,
326 LWS_CALLBACK_HTTP_WRITEABLE,
327 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
328 LWS_CALLBACK_FILTER_HTTP_CONNECTION,
329 LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED,
330 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
331 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
332 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
333 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
334 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
335 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
336 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
337 LWS_CALLBACK_PROTOCOL_INIT,
338 LWS_CALLBACK_PROTOCOL_DESTROY,
339 LWS_CALLBACK_WSI_CREATE, /* always protocol[0] */
340 LWS_CALLBACK_WSI_DESTROY, /* always protocol[0] */
341 LWS_CALLBACK_GET_THREAD_ID,
343 /* external poll() management support */
344 LWS_CALLBACK_ADD_POLL_FD,
345 LWS_CALLBACK_DEL_POLL_FD,
346 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
347 LWS_CALLBACK_LOCK_POLL,
348 LWS_CALLBACK_UNLOCK_POLL,
350 LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
352 LWS_CALLBACK_USER = 1000, /* user code can use any including / above */
356 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
357 typedef SOCKET lws_sockfd_type;
358 #define lws_sockfd_valid(sfd) (!!sfd)
364 WINSOCK_API_LINKAGE int WSAAPI WSAPoll(struct lws_pollfd fdArray[], ULONG fds, INT timeout);
367 #if defined(MBED_OPERATORS)
368 /* it's a class lws_conn * */
369 typedef void * lws_sockfd_type;
370 #define lws_sockfd_valid(sfd) (!!sfd)
376 #define POLLIN 0x0001
377 #define POLLPRI 0x0002
378 #define POLLOUT 0x0004
379 #define POLLERR 0x0008
380 #define POLLHUP 0x0010
381 #define POLLNVAL 0x0020
385 void * mbed3_create_tcp_stream_socket(void);
386 void mbed3_delete_tcp_stream_socket(void *sockfd);
387 void mbed3_tcp_stream_bind(void *sock, int port, struct lws *);
388 void mbed3_tcp_stream_accept(void *sock, struct lws *);
390 typedef int lws_sockfd_type;
391 #define lws_sockfd_valid(sfd) (sfd >= 0)
394 #define lws_pollfd pollfd
397 // argument structure for all external poll related calls
398 // passed in via 'in'
399 struct lws_pollargs {
400 lws_sockfd_type fd; // applicable file descriptor
401 int events; // the new event mask
402 int prev_events; // the previous event mask
405 enum lws_extension_callback_reasons {
406 LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT,
407 LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT,
408 LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT,
409 LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT,
410 LWS_EXT_CALLBACK_CONSTRUCT,
411 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
412 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
413 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
414 LWS_EXT_CALLBACK_DESTROY,
415 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
416 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED,
417 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
418 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
419 LWS_EXT_CALLBACK_PACKET_TX_DO_SEND,
420 LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
421 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
422 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
423 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
424 LWS_EXT_CALLBACK_1HZ,
425 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
426 LWS_EXT_CALLBACK_IS_WRITEABLE,
427 LWS_EXT_CALLBACK_PAYLOAD_TX,
428 LWS_EXT_CALLBACK_PAYLOAD_RX,
431 enum lws_write_protocol {
434 LWS_WRITE_CONTINUATION,
437 /* special 04+ opcodes */
443 /* Same as write_http but we know this write ends the transaction */
444 LWS_WRITE_HTTP_FINAL,
448 LWS_WRITE_HTTP_HEADERS,
452 LWS_WRITE_NO_FIN = 0x40,
454 * client packet payload goes out on wire unmunged
455 * only useful for security tests since normal servers cannot
456 * decode the content if used
458 LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
462 * you need these to look at headers that have been parsed if using the
463 * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
464 * list below is absent, .token = NULL and token_len = 0. Otherwise .token
465 * points to .token_len chars containing that header content.
474 * don't forget to update test server header dump accordingly
476 * these have to be kept in sync with lextable.h / minilex.c
479 enum lws_token_indexes {
482 WSI_TOKEN_OPTIONS_URI,
484 WSI_TOKEN_CONNECTION,
489 WSI_TOKEN_EXTENSIONS,
496 WSI_TOKEN_HTTP2_SETTINGS,
497 WSI_TOKEN_HTTP_ACCEPT,
498 WSI_TOKEN_HTTP_AC_REQUEST_HEADERS,
499 WSI_TOKEN_HTTP_IF_MODIFIED_SINCE,
500 WSI_TOKEN_HTTP_IF_NONE_MATCH,
501 WSI_TOKEN_HTTP_ACCEPT_ENCODING,
502 WSI_TOKEN_HTTP_ACCEPT_LANGUAGE,
503 WSI_TOKEN_HTTP_PRAGMA,
504 WSI_TOKEN_HTTP_CACHE_CONTROL,
505 WSI_TOKEN_HTTP_AUTHORIZATION,
506 WSI_TOKEN_HTTP_COOKIE,
507 WSI_TOKEN_HTTP_CONTENT_LENGTH,
508 WSI_TOKEN_HTTP_CONTENT_TYPE,
510 WSI_TOKEN_HTTP_RANGE,
511 WSI_TOKEN_HTTP_REFERER,
516 WSI_TOKEN_HTTP_COLON_AUTHORITY,
517 WSI_TOKEN_HTTP_COLON_METHOD,
518 WSI_TOKEN_HTTP_COLON_PATH,
519 WSI_TOKEN_HTTP_COLON_SCHEME,
520 WSI_TOKEN_HTTP_COLON_STATUS,
522 WSI_TOKEN_HTTP_ACCEPT_CHARSET,
523 WSI_TOKEN_HTTP_ACCEPT_RANGES,
524 WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN,
526 WSI_TOKEN_HTTP_ALLOW,
527 WSI_TOKEN_HTTP_CONTENT_DISPOSITION,
528 WSI_TOKEN_HTTP_CONTENT_ENCODING,
529 WSI_TOKEN_HTTP_CONTENT_LANGUAGE,
530 WSI_TOKEN_HTTP_CONTENT_LOCATION,
531 WSI_TOKEN_HTTP_CONTENT_RANGE,
533 WSI_TOKEN_HTTP_EXPECT,
534 WSI_TOKEN_HTTP_EXPIRES,
536 WSI_TOKEN_HTTP_IF_MATCH,
537 WSI_TOKEN_HTTP_IF_RANGE,
538 WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE,
539 WSI_TOKEN_HTTP_LAST_MODIFIED,
541 WSI_TOKEN_HTTP_LOCATION,
542 WSI_TOKEN_HTTP_MAX_FORWARDS,
543 WSI_TOKEN_HTTP_PROXY_AUTHENTICATE,
544 WSI_TOKEN_HTTP_PROXY_AUTHORIZATION,
545 WSI_TOKEN_HTTP_REFRESH,
546 WSI_TOKEN_HTTP_RETRY_AFTER,
547 WSI_TOKEN_HTTP_SERVER,
548 WSI_TOKEN_HTTP_SET_COOKIE,
549 WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY,
550 WSI_TOKEN_HTTP_TRANSFER_ENCODING,
551 WSI_TOKEN_HTTP_USER_AGENT,
554 WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
559 WSI_TOKEN_DELETE_URI,
561 WSI_TOKEN_HTTP_URI_ARGS,
563 /* use token storage to stash these */
565 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
566 _WSI_TOKEN_CLIENT_PEER_ADDRESS,
567 _WSI_TOKEN_CLIENT_URI,
568 _WSI_TOKEN_CLIENT_HOST,
569 _WSI_TOKEN_CLIENT_ORIGIN,
571 /* always last real token index*/
573 /* parser state additions */
576 WSI_TOKEN_SKIPPING_SAW_CR,
577 WSI_PARSING_COMPLETE,
578 WSI_INIT_TOKEN_MUXURL,
581 struct lws_token_limits {
582 unsigned short token_limit[WSI_TOKEN_COUNT];
589 1000 indicates a normal closure, meaning that the purpose for
590 which the connection was established has been fulfilled.
594 1001 indicates that an endpoint is "going away", such as a server
595 going down or a browser having navigated away from a page.
599 1002 indicates that an endpoint is terminating the connection due
604 1003 indicates that an endpoint is terminating the connection
605 because it has received a type of data it cannot accept (e.g., an
606 endpoint that understands only text data MAY send this if it
607 receives a binary message).
611 Reserved. The specific meaning might be defined in the future.
615 1005 is a reserved value and MUST NOT be set as a status code in a
616 Close control frame by an endpoint. It is designated for use in
617 applications expecting a status code to indicate that no status
618 code was actually present.
622 1006 is a reserved value and MUST NOT be set as a status code in a
623 Close control frame by an endpoint. It is designated for use in
624 applications expecting a status code to indicate that the
625 connection was closed abnormally, e.g., without sending or
626 receiving a Close control frame.
630 1007 indicates that an endpoint is terminating the connection
631 because it has received data within a message that was not
632 consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
633 data within a text message).
637 1008 indicates that an endpoint is terminating the connection
638 because it has received a message that violates its policy. This
639 is a generic status code that can be returned when there is no
640 other more suitable status code (e.g., 1003 or 1009) or if there
641 is a need to hide specific details about the policy.
645 1009 indicates that an endpoint is terminating the connection
646 because it has received a message that is too big for it to
651 1010 indicates that an endpoint (client) is terminating the
652 connection because it has expected the server to negotiate one or
653 more extension, but the server didn't return them in the response
654 message of the WebSocket handshake. The list of extensions that
655 are needed SHOULD appear in the /reason/ part of the Close frame.
656 Note that this status code is not used by the server, because it
657 can fail the WebSocket handshake instead.
661 1011 indicates that a server is terminating the connection because
662 it encountered an unexpected condition that prevented it from
663 fulfilling the request.
667 1015 is a reserved value and MUST NOT be set as a status code in a
668 Close control frame by an endpoint. It is designated for use in
669 applications expecting a status code to indicate that the
670 connection was closed due to a failure to perform a TLS handshake
671 (e.g., the server certificate can't be verified).
674 enum lws_close_status {
675 LWS_CLOSE_STATUS_NOSTATUS = 0,
676 LWS_CLOSE_STATUS_NORMAL = 1000,
677 LWS_CLOSE_STATUS_GOINGAWAY = 1001,
678 LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
679 LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
680 LWS_CLOSE_STATUS_RESERVED = 1004,
681 LWS_CLOSE_STATUS_NO_STATUS = 1005,
682 LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
683 LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
684 LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
685 LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
686 LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
687 LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
688 LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
690 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
694 HTTP_STATUS_OK = 200,
695 HTTP_STATUS_NO_CONTENT = 204,
697 HTTP_STATUS_BAD_REQUEST = 400,
698 HTTP_STATUS_UNAUTHORIZED,
699 HTTP_STATUS_PAYMENT_REQUIRED,
700 HTTP_STATUS_FORBIDDEN,
701 HTTP_STATUS_NOT_FOUND,
702 HTTP_STATUS_METHOD_NOT_ALLOWED,
703 HTTP_STATUS_NOT_ACCEPTABLE,
704 HTTP_STATUS_PROXY_AUTH_REQUIRED,
705 HTTP_STATUS_REQUEST_TIMEOUT,
706 HTTP_STATUS_CONFLICT,
708 HTTP_STATUS_LENGTH_REQUIRED,
709 HTTP_STATUS_PRECONDITION_FAILED,
710 HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
711 HTTP_STATUS_REQ_URI_TOO_LONG,
712 HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
713 HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
714 HTTP_STATUS_EXPECTATION_FAILED,
716 HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
717 HTTP_STATUS_NOT_IMPLEMENTED,
718 HTTP_STATUS_BAD_GATEWAY,
719 HTTP_STATUS_SERVICE_UNAVAILABLE,
720 HTTP_STATUS_GATEWAY_TIMEOUT,
721 HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
726 /* needed even with extensions disabled for create context */
727 struct lws_extension;
730 * callback_function() - User server actions
731 * @context: Websockets context
732 * @wsi: Opaque websocket instance pointer
733 * @reason: The reason for the call
734 * @user: Pointer to per-session user data allocated by library
735 * @in: Pointer used for some callback reasons
736 * @len: Length set for some callback reasons
738 * This callback is the way the user controls what is served. All the
739 * protocol detail is hidden and handled by the library.
741 * For each connection / session there is user data allocated that is
742 * pointed to by "user". You set the size of this user data area when
743 * the library is initialized with lws_create_server.
745 * You get an opportunity to initialize user data when called back with
746 * LWS_CALLBACK_ESTABLISHED reason.
748 * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with
751 * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
752 * been unable to complete a handshake with the remote server. If
753 * in is non-NULL, you can find an error string of length len where
756 * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
757 * client user code to examine the http headers
758 * and decide to reject the connection. If the
759 * content in the headers is interesting to the
760 * client (url, etc) it needs to copy it out at
761 * this point since it will be destroyed before
762 * the CLIENT_ESTABLISHED call
764 * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
765 * a handshake with the remote server
767 * LWS_CALLBACK_CLOSED: when the websocket session ends
769 * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends
771 * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
772 * remote client, it can be found at *in and is
775 * LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
776 * they appear with this callback reason. PONG
777 * packets only exist in 04+ protocol
779 * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
780 * client connection, it can be found at *in and
783 * LWS_CALLBACK_HTTP: an http request has come from a client that is not
784 * asking to upgrade the connection to a websocket
785 * one. This is a chance to serve http content,
786 * for example, to send a script to the client
787 * which will then open the websockets connection.
788 * @in points to the URI path requested and
789 * lws_serve_http_file() makes it very
790 * simple to send back a file to the client.
791 * Normally after sending the file you are done
792 * with the http connection, since the rest of the
793 * activity will come by websockets from the script
794 * that was delivered by http, so you will want to
795 * return 1; to close and free up the connection.
796 * That's important because it uses a slot in the
797 * total number of client connections allowed set
800 * LWS_CALLBACK_HTTP_BODY: the next @len bytes data from the http
801 * request body HTTP connection is now available in @in.
803 * LWS_CALLBACK_HTTP_BODY_COMPLETION: the expected amount of http request
804 * body has been delivered
806 * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol
809 * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down
810 * http link has completed.
812 * LWS_CALLBACK_CLIENT_WRITEABLE:
813 * LWS_CALLBACK_SERVER_WRITEABLE: If you call
814 * lws_callback_on_writable() on a connection, you will
815 * get one of these callbacks coming when the connection socket
816 * is able to accept another write packet without blocking.
817 * If it already was able to take another packet without blocking,
818 * you'll get this callback at the next call to the service loop
819 * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
820 * and servers get LWS_CALLBACK_SERVER_WRITEABLE.
822 * LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
823 * the server at network level; the connection is accepted but then
824 * passed to this callback to decide whether to hang up immediately
825 * or not, based on the client IP. @in contains the connection
826 * socket's descriptor. Since the client connection information is
827 * not available yet, @wsi still pointing to the main server socket.
828 * Return non-zero to terminate the connection before sending or
829 * receiving anything. Because this happens immediately after the
830 * network connection from the client, there's no websocket protocol
831 * selected yet so this callback is issued only to protocol 0.
833 * LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED: A new client just had
834 * been connected, accepted, and instantiated into the pool. This
835 * callback allows setting any relevant property to it. Because this
836 * happens immediately after the instantiation of a new client,
837 * there's no websocket protocol selected yet so this callback is
838 * issued only to protocol 0. Only @wsi is defined, pointing to the
839 * new client, and the return value is ignored.
841 * LWS_CALLBACK_FILTER_HTTP_CONNECTION: called when the request has
842 * been received and parsed from the client, but the response is
843 * not sent yet. Return non-zero to disallow the connection.
844 * @user is a pointer to the connection user space allocation,
845 * @in is the URI, eg, "/"
846 * In your handler you can use the public APIs
847 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
848 * headers using the header enums lws_token_indexes from
849 * libwebsockets.h to check for and read the supported header
850 * presence and content before deciding to allow the http
851 * connection to proceed or to kill the connection.
853 * LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
854 * been received and parsed from the client, but the response is
855 * not sent yet. Return non-zero to disallow the connection.
856 * @user is a pointer to the connection user space allocation,
857 * @in is the requested protocol name
858 * In your handler you can use the public APIs
859 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
860 * headers using the header enums lws_token_indexes from
861 * libwebsockets.h to check for and read the supported header
862 * presence and content before deciding to allow the handshake
863 * to proceed or to kill the connection.
865 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for
866 * including OpenSSL support, this callback allows your user code
867 * to perform extra SSL_CTX_load_verify_locations() or similar
868 * calls to direct OpenSSL where to find certificates the client
869 * can use to confirm the remote server identity. @user is the
872 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for
873 * including OpenSSL support, this callback allows your user code
874 * to load extra certifcates into the server which allow it to
875 * verify the validity of certificates returned by clients. @user
876 * is the server's OpenSSL SSL_CTX*
878 * LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY: if configured for
879 * including OpenSSL support but no private key file has been specified
880 * (ssl_private_key_filepath is NULL), this callback is called to
881 * allow the user to set the private key directly via libopenssl
882 * and perform further operations if required; this might be useful
883 * in situations where the private key is not directly accessible by
884 * the OS, for example if it is stored on a smartcard
885 * @user is the server's OpenSSL SSL_CTX*
887 * LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the
888 * libwebsockets context was created with the option
889 * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
890 * callback is generated during OpenSSL verification of the cert
891 * sent from the client. It is sent to protocol[0] callback as
892 * no protocol has been negotiated on the connection yet.
893 * Notice that the libwebsockets context and wsi are both NULL
894 * during this callback. See
895 * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
896 * to understand more detail about the OpenSSL callback that
897 * generates this libwebsockets callback and the meanings of the
898 * arguments passed. In this callback, @user is the x509_ctx,
899 * @in is the ssl pointer and @len is preverify_ok
900 * Notice that this callback maintains libwebsocket return
901 * conventions, return 0 to mean the cert is OK or 1 to fail it.
902 * This also means that if you don't handle this callback then
903 * the default callback action of returning 0 allows the client
906 * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens
907 * when a client handshake is being compiled. @user is NULL,
908 * @in is a char **, it's pointing to a char * which holds the
909 * next location in the header buffer where you can add
910 * headers, and @len is the remaining space in the header buffer,
911 * which is typically some hundreds of bytes. So, to add a canned
912 * cookie, your handler code might look similar to:
914 * char **p = (char **)in;
919 * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
923 * Notice if you add anything, you just have to take care about
924 * the CRLF on the line you added. Obviously this callback is
925 * optional, if you don't handle it everything is fine.
927 * Notice the callback is coming to protocols[0] all the time,
928 * because there is no specific protocol handshook yet.
930 * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
931 * sees that it does support a requested extension, before
932 * accepting the extension by additing to the list sent back to
933 * the client it gives this callback just to check that it's okay
934 * to use that extension. It calls back to the requested protocol
935 * and with @in being the extension name, @len is 0 and @user is
936 * valid. Note though at this time the ESTABLISHED callback hasn't
937 * happened yet so if you initialize @user content there, @user
938 * content during this callback might not be useful for anything.
939 * Notice this callback comes to protocols[0].
941 * LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: When a client
942 * connection is being prepared to start a handshake to a server,
943 * each supported extension is checked with protocols[0] callback
944 * with this reason, giving the user code a chance to suppress the
945 * claim to support that extension by returning non-zero. If
946 * unhandled, by default 0 will be returned and the extension
947 * support included in the header to the server. Notice this
948 * callback comes to protocols[0].
950 * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can
951 * do initial setup / allocations etc
953 * LWS_CALLBACK_PROTOCOL_DESTROY: One-time call per protocol indicating
954 * this protocol won't get used at all after this callback, the
955 * context is getting destroyed. Take the opportunity to
956 * deallocate everything that was allocated by the protocol.
958 * LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification
960 * LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification
962 * The next five reasons are optional and only need taking care of if you
963 * will be integrating libwebsockets sockets into an external polling
966 * For these calls, @in points to a struct lws_pollargs that
967 * contains @fd, @events and @prev_events members
969 * LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
970 * internally, but in the case you are integrating with another
971 * server you will need to have libwebsocket sockets share a
972 * polling array with the other server. This and the other
973 * POLL_FD related callbacks let you put your specialized
974 * poll array interface code in the callback for protocol 0, the
975 * first protocol you support, usually the HTTP protocol in the
977 * This callback happens when a socket needs to be
978 * added to the polling loop: @in points to a struct
979 * lws_pollargs; the @fd member of the struct is the file
980 * descriptor, and @events contains the active events.
982 * If you are using the internal polling loop (the "service"
983 * callback), you can just ignore these callbacks.
985 * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
986 * needs to be removed from an external polling array. @in is
987 * again the struct lws_pollargs containing the @fd member
988 * to be removed. If you are using the internal polling
989 * loop, you can just ignore it.
991 * LWS_CALLBACK_CHANGE_MODE_POLL_FD: This callback happens when
992 * libwebsockets wants to modify the events for a connectiion.
993 * @in is the struct lws_pollargs with the @fd to change.
994 * The new event mask is in @events member and the old mask is in
995 * the @prev_events member.
996 * If you are using the internal polling loop, you can just ignore
999 * LWS_CALLBACK_LOCK_POLL:
1000 * LWS_CALLBACK_UNLOCK_POLL: These allow the external poll changes driven
1001 * by libwebsockets to participate in an external thread locking
1002 * scheme around the changes, so the whole thing is threadsafe.
1003 * These are called around three activities in the library,
1004 * - inserting a new wsi in the wsi / fd table (len=1)
1005 * - deleting a wsi from the wsi / fd table (len=1)
1006 * - changing a wsi's POLLIN/OUT state (len=0)
1007 * Locking and unlocking external synchronization objects when
1008 * len == 1 allows external threads to be synchronized against
1009 * wsi lifecycle changes if it acquires the same lock for the
1010 * duration of wsi dereference from the other thread context.
1012 LWS_VISIBLE LWS_EXTERN int
1013 callback(struct lws_context *context, struct lws *wsi,
1014 enum lws_callback_reasons reason, void *user, void *in, size_t len);
1016 typedef int (callback_function)(struct lws_context *context, struct lws *wsi,
1017 enum lws_callback_reasons reason, void *user,
1018 void *in, size_t len);
1020 #ifndef LWS_NO_EXTENSIONS
1022 * extension_callback_function() - Hooks to allow extensions to operate
1023 * @context: Websockets context
1024 * @ext: This extension
1025 * @wsi: Opaque websocket instance pointer
1026 * @reason: The reason for the call
1027 * @user: Pointer to per-session user data allocated by library
1028 * @in: Pointer used for some callback reasons
1029 * @len: Length set for some callback reasons
1031 * Each extension that is active on a particular connection receives
1032 * callbacks during the connection lifetime to allow the extension to
1033 * operate on websocket data and manage itself.
1035 * Libwebsockets takes care of allocating and freeing "user" memory for
1036 * each active extension on each connection. That is what is pointed to
1037 * by the @user parameter.
1039 * LWS_EXT_CALLBACK_CONSTRUCT: called when the server has decided to
1040 * select this extension from the list provided by the client,
1041 * just before the server will send back the handshake accepting
1042 * the connection with this extension active. This gives the
1043 * extension a chance to initialize its connection context found
1046 * LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT
1047 * but called when client is instantiating this extension. Some
1048 * extensions will work the same on client and server side and then
1049 * you can just merge handlers for both CONSTRUCTS.
1051 * LWS_EXT_CALLBACK_DESTROY: called when the connection the extension was
1052 * being used on is about to be closed and deallocated. It's the
1053 * last chance for the extension to deallocate anything it has
1054 * allocated in the user data (pointed to by @user) before the
1055 * user data is deleted. This same callback is used whether you
1056 * are in client or server instantiation context.
1058 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on
1059 * a connection, and a packet of data arrived at the connection,
1060 * it is passed to this callback to give the extension a chance to
1061 * change the data, eg, decompress it. @user is pointing to the
1062 * extension's private connection context data, @in is pointing
1063 * to an lws_tokens struct, it consists of a char * pointer called
1064 * token, and an int called token_len. At entry, these are
1065 * set to point to the received buffer and set to the content
1066 * length. If the extension will grow the content, it should use
1067 * a new buffer allocated in its private user context data and
1068 * set the pointed-to lws_tokens members to point to its buffer.
1070 * LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as
1071 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
1072 * extension a chance to change websocket data just before it will
1073 * be sent out. Using the same lws_token pointer scheme in @in,
1074 * the extension can change the buffer and the length to be
1075 * transmitted how it likes. Again if it wants to grow the
1076 * buffer safely, it should copy the data into its own buffer and
1077 * set the lws_tokens token pointer to it.
1079 LWS_VISIBLE LWS_EXTERN int
1080 extension_callback(struct lws_context *context, struct lws_extension *ext,
1081 struct lws *wsi, enum lws_extension_callback_reasons reason,
1082 void *user, void *in, size_t len);
1084 typedef int (extension_callback_function)(struct lws_context *context,
1085 struct lws_extension *ext, struct lws *wsi,
1086 enum lws_extension_callback_reasons reason,
1087 void *user, void *in, size_t len);
1091 * struct lws_protocols - List of protocols and handlers server
1093 * @name: Protocol name that must match the one given in the client
1094 * Javascript new WebSocket(url, 'protocol') name.
1095 * @callback: The service callback used for this protocol. It allows the
1096 * service action for an entire protocol to be encapsulated in
1097 * the protocol-specific callback
1098 * @per_session_data_size: Each new connection using this protocol gets
1099 * this much memory allocated on connection establishment and
1100 * freed on connection takedown. A pointer to this per-connection
1101 * allocation is passed into the callback in the 'user' parameter
1102 * @rx_buffer_size: if you want atomic frames delivered to the callback, you
1103 * should set this to the size of the biggest legal frame that
1104 * you support. If the frame size is exceeded, there is no
1105 * error, but the buffer will spill to the user callback when
1106 * full, which you can detect by using
1107 * lws_remaining_packet_payload(). Notice that you
1108 * just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING
1109 * and post-padding are automatically also allocated on top.
1110 * @id: ignored by lws, but useful to contain user information bound
1111 * to the selected protocol. For example if this protocol was
1112 * called "myprotocol-v2", you might set id to 2, and the user
1113 * code that acts differently according to the version can do so by
1114 * switch (wsi->protocol->id), user code might use some bits as
1115 * capability flags based on selected protocol version, etc.
1116 * @user: User provided context data at the protocol level.
1117 * Accessible via lws_get_protocol(wsi)->user
1118 * This should not be confused with wsi->user, it is not the same.
1119 * The library completely ignores any value in here.
1120 * @owning_server: the server init call fills in this opaque pointer when
1121 * registering this protocol with the server.
1122 * @protocol_index: which protocol we are starting from zero
1124 * This structure represents one protocol supported by the server. An
1125 * array of these structures is passed to lws_create_server()
1126 * allows as many protocols as you like to be handled by one server.
1128 * The first protocol given has its callback used for user callbacks when
1129 * there is no agreed protocol name, that's true during HTTP part of the
1130 * connection and true if the client did not send a Protocol: header.
1133 struct lws_protocols {
1135 callback_function *callback;
1136 size_t per_session_data_size;
1137 size_t rx_buffer_size;
1142 * below are filled in on server init and can be left uninitialized,
1143 * no need for user to use them directly either
1146 struct lws_context *owning_server;
1150 #ifndef LWS_NO_EXTENSIONS
1152 * struct lws_extension - An extension we know how to cope with
1154 * @name: Formal extension name, eg, "deflate-stream"
1155 * @callback: Service callback
1156 * @per_session_data_size: Libwebsockets will auto-malloc this much
1157 * memory for the use of the extension, a pointer
1158 * to it comes in the @user callback parameter
1159 * @per_context_private_data: Optional storage for this extension that
1160 * is per-context, so it can track stuff across
1161 * all sessions, etc, if it wants
1164 struct lws_extension {
1166 extension_callback_function *callback;
1167 size_t per_session_data_size;
1168 void *per_context_private_data;
1173 * struct lws_context_creation_info: parameters to create context with
1175 * @port: Port to listen on... you can use CONTEXT_PORT_NO_LISTEN to
1176 * suppress listening on any port, that's what you want if you are
1177 * not running a websocket server at all but just using it as a
1179 * @iface: NULL to bind the listen socket to all interfaces, or the
1180 * interface name, eg, "eth2"
1181 * @protocols: Array of structures listing supported protocols and a protocol-
1182 * specific callback for each one. The list is ended with an
1183 * entry that has a NULL callback pointer.
1184 * It's not const because we write the owning_server member
1185 * @extensions: NULL or array of lws_extension structs listing the
1186 * extensions this context supports. If you configured with
1187 * --without-extensions, you should give NULL here.
1188 * @token_limits: NULL or struct lws_token_limits pointer which is initialized
1189 * with a token length limit for each possible WSI_TOKEN_***
1190 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
1191 * to listen using SSL, set to the filepath to fetch the
1192 * server cert from, otherwise NULL for unencrypted
1193 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode;
1194 * if this is set to NULL but sll_cert_filepath is set, the
1195 * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called to allow
1196 * setting of the private key directly via openSSL library calls
1197 * @ssl_ca_filepath: CA certificate filepath or NULL
1198 * @ssl_cipher_list: List of valid ciphers to use (eg,
1199 * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
1200 * or you can leave it as NULL to get "DEFAULT"
1201 * @http_proxy_address: If non-NULL, attempts to proxy via the given address.
1202 * If proxy auth is required, use format
1203 * "username:password@server:port"
1204 * @http_proxy_port: If http_proxy_address was non-NULL, uses this port at the address
1205 * @gid: group id to change to after setting listen socket, or -1.
1206 * @uid: user id to change to after setting listen socket, or -1.
1207 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
1208 * @user: optional user pointer that can be recovered via the context
1209 * pointer using lws_context_user
1210 * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to
1211 * all libwebsocket sockets, client or server
1212 * @ka_probes: if ka_time was nonzero, after the timeout expires how many
1213 * times to try to get a response from the peer before giving up
1214 * and killing the connection
1215 * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes
1217 * @provided_client_ssl_ctx: If non-null, swap out libwebsockets ssl
1218 * implementation for the one provided by provided_ssl_ctx.
1219 * Libwebsockets no longer is responsible for freeing the context
1220 * if this option is selected.
1223 struct lws_context_creation_info {
1226 struct lws_protocols *protocols;
1227 struct lws_extension *extensions;
1228 struct lws_token_limits *token_limits;
1229 const char *ssl_private_key_password;
1230 const char *ssl_cert_filepath;
1231 const char *ssl_private_key_filepath;
1232 const char *ssl_ca_filepath;
1233 const char *ssl_cipher_list;
1234 const char *http_proxy_address;
1235 unsigned int http_proxy_port;
1238 unsigned int options;
1243 #ifdef LWS_OPENSSL_SUPPORT
1244 SSL_CTX *provided_client_ssl_ctx;
1245 #else /* maintain structure layout either way */
1246 void *provided_client_ssl_ctx;
1250 LWS_VISIBLE LWS_EXTERN void
1251 lws_set_log_level(int level,
1252 void (*log_emit_function)(int level, const char *line));
1254 LWS_VISIBLE LWS_EXTERN void
1255 lwsl_emit_syslog(int level, const char *line);
1257 LWS_VISIBLE LWS_EXTERN struct lws_context *
1258 lws_create_context(struct lws_context_creation_info *info);
1260 LWS_VISIBLE LWS_EXTERN int
1261 lws_set_proxy(struct lws_context *context, const char *proxy);
1263 LWS_VISIBLE LWS_EXTERN void
1264 lws_context_destroy(struct lws_context *context);
1266 LWS_VISIBLE LWS_EXTERN int
1267 lws_service(struct lws_context *context, int timeout_ms);
1269 LWS_VISIBLE LWS_EXTERN void
1270 lws_cancel_service(struct lws_context *context);
1272 LWS_VISIBLE LWS_EXTERN const unsigned char *
1273 lws_token_to_string(enum lws_token_indexes token);
1275 LWS_VISIBLE LWS_EXTERN int
1276 lws_add_http_header_by_name(struct lws_context *context,
1278 const unsigned char *name,
1279 const unsigned char *value,
1282 unsigned char *end);
1283 LWS_VISIBLE LWS_EXTERN int
1284 lws_finalize_http_header(struct lws_context *context,
1287 unsigned char *end);
1288 LWS_VISIBLE LWS_EXTERN int
1289 lws_add_http_header_by_token(struct lws_context *context,
1291 enum lws_token_indexes token,
1292 const unsigned char *value,
1295 unsigned char *end);
1296 LWS_VISIBLE LWS_EXTERN int
1297 lws_add_http_header_content_length(struct lws_context *context,
1299 unsigned long content_length,
1301 unsigned char *end);
1302 LWS_VISIBLE LWS_EXTERN int
1303 lws_add_http_header_status(struct lws_context *context,
1307 unsigned char *end);
1310 lws_http_transaction_completed(struct lws *wsi);
1312 #ifdef LWS_USE_LIBEV
1313 typedef void (lws_ev_signal_cb)(EV_P_ struct ev_signal *w, int revents);
1315 LWS_VISIBLE LWS_EXTERN int
1317 struct lws_context *context,
1319 lws_ev_signal_cb* cb);
1321 LWS_VISIBLE LWS_EXTERN int
1323 struct lws_context *context, struct ev_loop *loop);
1327 struct ev_loop *loop, struct ev_signal *watcher, int revents);
1328 #endif /* LWS_USE_LIBEV */
1330 LWS_VISIBLE LWS_EXTERN int
1331 lws_service_fd(struct lws_context *context,
1332 struct lws_pollfd *pollfd);
1334 LWS_VISIBLE LWS_EXTERN void *
1335 lws_context_user(struct lws_context *context);
1337 enum pending_timeout {
1338 NO_PENDING_TIMEOUT = 0,
1339 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
1340 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
1341 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
1342 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
1343 PENDING_TIMEOUT_AWAITING_PING,
1344 PENDING_TIMEOUT_CLOSE_ACK,
1345 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
1346 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
1347 PENDING_TIMEOUT_SSL_ACCEPT,
1348 PENDING_TIMEOUT_HTTP_CONTENT,
1349 PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
1350 PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE,
1353 LWS_VISIBLE LWS_EXTERN void
1354 lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
1359 * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
1360 * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
1361 * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
1363 * This allows us to add protocol info before and after the data, and send as
1364 * one packet on the network without payload copying, for maximum efficiency.
1366 * So for example you need this kind of code to use lws_write with a
1369 * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
1371 * // fill your part of the buffer... for example here it's all zeros
1372 * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
1374 * lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128,
1377 * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
1378 * use the whole buffer without taking care of the above.
1382 * this is the frame nonce plus two header plus 8 length
1383 * there's an additional two for mux extension per mux nesting level
1384 * 2 byte prepend on close will already fit because control frames cannot use
1385 * the big length style
1389 * Pad LWS_SEND_BUFFER_PRE_PADDING to the CPU word size, so that word references
1390 * to the address immediately after the padding won't cause an unaligned access
1391 * error. Sometimes for performance reasons the recommended padding is even
1392 * larger than sizeof(void *).
1395 #if !defined(LWS_SIZEOFPTR)
1396 #define LWS_SIZEOFPTR (sizeof (void *))
1398 #if !defined(u_int64_t)
1399 #define u_int64_t unsigned long long
1403 #define _LWS_PAD_SIZE 16 // Intel recommended for best performance.
1405 #define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target architecture */
1407 #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \
1408 ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
1409 #define LWS_SEND_BUFFER_PRE_PADDING _LWS_PAD(4 + 10 + (2 * MAX_MUX_RECURSION))
1410 #define LWS_SEND_BUFFER_POST_PADDING 4
1412 LWS_VISIBLE LWS_EXTERN int
1413 lws_write(struct lws *wsi, unsigned char *buf, size_t len,
1414 enum lws_write_protocol protocol);
1416 /* helper for case where buffer may be const */
1417 #define lws_write_http(wsi, buf, len) \
1418 lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
1420 LWS_VISIBLE LWS_EXTERN int
1421 lws_serve_http_file(struct lws_context *context, struct lws *wsi,
1422 const char *file, const char *content_type,
1423 const char *other_headers, int other_headers_len);
1424 LWS_VISIBLE LWS_EXTERN int
1425 lws_serve_http_file_fragment(struct lws_context *context, struct lws *wsi);
1427 LWS_VISIBLE LWS_EXTERN int
1428 lws_return_http_status(struct lws_context *context, struct lws *wsi,
1429 unsigned int code, const char *html_body);
1431 LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
1432 lws_get_protocol(struct lws *wsi);
1434 LWS_VISIBLE LWS_EXTERN int
1435 lws_callback_on_writable(struct lws_context *context, struct lws *wsi);
1437 LWS_VISIBLE LWS_EXTERN int
1438 lws_callback_on_writable_all_protocol(const struct lws_protocols *protocol);
1440 LWS_VISIBLE LWS_EXTERN int
1441 lws_callback_all_protocol(const struct lws_protocols *protocol, int reason);
1443 LWS_VISIBLE LWS_EXTERN int
1444 lws_get_socket_fd(struct lws *wsi);
1446 LWS_VISIBLE LWS_EXTERN int
1447 lws_is_final_fragment(struct lws *wsi);
1449 LWS_VISIBLE LWS_EXTERN unsigned char
1450 lws_get_reserved_bits(struct lws *wsi);
1452 LWS_VISIBLE LWS_EXTERN int
1453 lws_rx_flow_control(struct lws *wsi, int enable);
1455 LWS_VISIBLE LWS_EXTERN void
1456 lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol);
1458 LWS_VISIBLE LWS_EXTERN size_t
1459 lws_remaining_packet_payload(struct lws *wsi);
1462 * if the protocol does not have any guidance, returns -1. Currently only
1463 * http2 connections get send window information from this API. But your code
1464 * should use it so it can work properly with any protocol.
1466 * If nonzero return is the amount of payload data the peer or intermediary has
1467 * reported it has buffer space for. That has NO relationship with the amount
1468 * of buffer space your OS can accept on this connection for a write action.
1470 * This number represents the maximum you could send to the peer or intermediary
1471 * on this connection right now without it complaining.
1473 * lws manages accounting for send window updates and payload writes
1474 * automatically, so this number reflects the situation at the peer or
1475 * intermediary dynamically.
1477 LWS_VISIBLE LWS_EXTERN size_t
1478 lws_get_peer_write_allowance(struct lws *wsi);
1480 LWS_VISIBLE LWS_EXTERN struct lws *
1481 lws_client_connect(struct lws_context *clients, const char *address,
1482 int port, int ssl_connection, const char *path,
1483 const char *host, const char *origin, const char *protocol,
1484 int ietf_version_or_minus_one);
1486 LWS_VISIBLE LWS_EXTERN struct lws *
1487 lws_client_connect_extended(struct lws_context *clients, const char *address,
1488 int port, int ssl_connection, const char *path,
1489 const char *host, const char *origin,
1490 const char *protocol, int ietf_version_or_minus_one,
1493 LWS_VISIBLE LWS_EXTERN const char *
1494 lws_canonical_hostname(struct lws_context *context);
1497 LWS_VISIBLE LWS_EXTERN void
1498 lws_get_peer_addresses(struct lws_context *context, struct lws *wsi,
1499 lws_sockfd_type fd, char *name, int name_len,
1500 char *rip, int rip_len);
1502 LWS_VISIBLE LWS_EXTERN int
1503 lws_get_random(struct lws_context *context, void *buf, int len);
1505 LWS_VISIBLE LWS_EXTERN int
1506 lws_daemonize(const char *_lock_path);
1508 LWS_VISIBLE LWS_EXTERN int
1509 lws_send_pipe_choked(struct lws *wsi);
1511 LWS_VISIBLE LWS_EXTERN int
1512 lws_partial_buffered(struct lws *wsi);
1514 LWS_VISIBLE LWS_EXTERN int
1515 lws_frame_is_binary(struct lws *wsi);
1517 LWS_VISIBLE LWS_EXTERN int
1518 lws_is_ssl(struct lws *wsi);
1519 #ifdef LWS_SHA1_USE_OPENSSL_NAME
1520 #define lws_SHA1 SHA1
1522 LWS_VISIBLE LWS_EXTERN unsigned char *
1523 lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
1526 LWS_VISIBLE LWS_EXTERN int
1527 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
1529 LWS_VISIBLE LWS_EXTERN int
1530 lws_b64_decode_string(const char *in, char *out, int out_size);
1532 LWS_VISIBLE LWS_EXTERN const char *
1533 lws_get_library_version(void);
1535 /* access to headers... only valid while headers valid */
1537 LWS_VISIBLE LWS_EXTERN int
1538 lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
1540 LWS_VISIBLE LWS_EXTERN int
1541 lws_hdr_copy(struct lws *wsi, char *dest, int len,
1542 enum lws_token_indexes h);
1545 * Note: this is not normally needed as a user api. It's provided in case it is
1546 * useful when integrating with other app poll loop service code.
1549 LWS_VISIBLE LWS_EXTERN int
1550 lws_read(struct lws_context *context, struct lws *wsi,
1551 unsigned char *buf, size_t len);
1553 #ifndef LWS_NO_EXTENSIONS
1554 LWS_VISIBLE LWS_EXTERN struct lws_extension *lws_get_internal_extensions();
1558 * custom allocator support
1560 LWS_VISIBLE LWS_EXTERN void
1561 lws_set_allocator(void *(*realloc)(void *ptr, size_t size));