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
65 #include "mbed-drivers/mbed.h"
66 #include "sal-iface-eth/EthernetInterface.h"
67 #include "sockets/TCPListener.h"
68 #include "sal-stack-lwip/lwipv4_init.h"
71 const int SERVER_PORT = 80;
72 const int BUFFER_SIZE = 4096;
74 using namespace mbed::Sockets::v0;
77 struct libwebsocket_context;
85 awaiting_on_writeable(0)
90 void set_wsi(struct libwebsocket *_wsi) { wsi = _wsi; }
91 int actual_onRX(Socket *s);
93 void onError(Socket *s, socket_error_t err);
94 void onDisconnect(TCPStream *s);
95 void onSent(Socket *s, uint16_t len);
96 void serialized_writeable(struct libwebsocket *wsi);
102 struct libwebsocket *wsi;
103 char buffer[BUFFER_SIZE];
105 char awaiting_on_writeable;
108 class lws_conn_listener : lws_conn {
111 srv(SOCKET_STACK_LWIP_IPV4)
113 srv.setOnError(TCPStream::ErrorHandler_t(this, &lws_conn_listener::onError));
116 void start(const uint16_t port);
119 void onRX(Socket *s);
120 void onError(Socket *s, socket_error_t err);
121 void onIncoming(TCPListener *s, void *impl);
122 void onDisconnect(TCPStream *s);
135 #ifdef MBED_OPERATORS
141 #include "lws_config.h"
143 #if defined(WIN32) || defined(_WIN32)
144 #if (WINVER < 0x0501)
147 #define WINVER 0x0501
148 #define _WIN32_WINNT WINVER
150 #ifndef WIN32_LEAN_AND_MEAN
151 #define WIN32_LEAN_AND_MEAN
153 #include <winsock2.h>
154 #include <ws2tcpip.h>
159 #define strcasecmp stricmp
160 #define getdtablesize() 30000
166 #define LWS_EXTERN extern __declspec(dllexport)
168 #define LWS_EXTERN extern __declspec(dllimport)
177 #ifndef MBED_OPERATORS
181 #define getdtablesize() (20)
184 #if defined(__GNUC__)
185 #define LWS_VISIBLE __attribute__((visibility("default")))
190 #if defined(__ANDROID__)
191 #define getdtablesize() 1024
198 #endif /* LWS_USE_LIBEV */
201 #define LWS_EXTERN extern
207 #include <sys/time.h>
211 #ifdef LWS_OPENSSL_SUPPORT
213 #ifdef USE_OLD_CYASSL
214 #include <cyassl/openssl/ssl.h>
216 #include <wolfssl/openssl/ssl.h>
217 #endif /* not USE_OLD_CYASSL */
219 #include <openssl/ssl.h>
220 #endif /* not USE_WOLFSSL */
223 #define CONTEXT_PORT_NO_LISTEN -1
224 #define MAX_MUX_RECURSION 2
226 enum lws_log_levels {
236 LLL_LATENCY = 1 << 9,
238 LLL_COUNT = 10 /* set to count of valid flags */
241 LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
242 LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
244 /* notice, warn and log are always compiled in */
245 #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
246 #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
247 #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
249 * weaker logging can be deselected at configure time using --disable-debug
250 * that gets rid of the overhead of checking while keeping _warn and _err
255 #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
256 #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
257 #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
258 #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
259 #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
260 #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
261 #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
262 LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
266 #define lwsl_info(...) {}
267 #define lwsl_debug(...) {}
268 #define lwsl_parser(...) {}
269 #define lwsl_header(...) {}
270 #define lwsl_ext(...) {}
271 #define lwsl_client(...) {}
272 #define lwsl_latency(...) {}
273 #define lwsl_hexdump(a, b)
277 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
279 /* api change list for user code to test against */
281 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
283 /* the struct libwebsocket_protocols has the id field present */
284 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
286 /* you can call lws_get_peer_write_allowance */
287 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
289 /* extra parameter introduced in 917f43ab821 */
290 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
292 enum libwebsocket_context_options {
293 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
294 LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4,
295 LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = 8,
296 LWS_SERVER_OPTION_LIBEV = 16,
297 LWS_SERVER_OPTION_DISABLE_IPV6 = 32,
298 LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = 64,
299 LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = 128,
302 enum libwebsocket_callback_reasons {
303 LWS_CALLBACK_ESTABLISHED,
304 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
305 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
306 LWS_CALLBACK_CLIENT_ESTABLISHED,
308 LWS_CALLBACK_CLOSED_HTTP,
309 LWS_CALLBACK_RECEIVE,
310 LWS_CALLBACK_RECEIVE_PONG,
311 LWS_CALLBACK_CLIENT_RECEIVE,
312 LWS_CALLBACK_CLIENT_RECEIVE_PONG,
313 LWS_CALLBACK_CLIENT_WRITEABLE,
314 LWS_CALLBACK_SERVER_WRITEABLE,
316 LWS_CALLBACK_HTTP_BODY,
317 LWS_CALLBACK_HTTP_BODY_COMPLETION,
318 LWS_CALLBACK_HTTP_FILE_COMPLETION,
319 LWS_CALLBACK_HTTP_WRITEABLE,
320 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
321 LWS_CALLBACK_FILTER_HTTP_CONNECTION,
322 LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED,
323 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
324 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
325 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
326 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
327 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
328 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
329 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
330 LWS_CALLBACK_PROTOCOL_INIT,
331 LWS_CALLBACK_PROTOCOL_DESTROY,
332 LWS_CALLBACK_WSI_CREATE, /* always protocol[0] */
333 LWS_CALLBACK_WSI_DESTROY, /* always protocol[0] */
334 LWS_CALLBACK_GET_THREAD_ID,
336 /* external poll() management support */
337 LWS_CALLBACK_ADD_POLL_FD,
338 LWS_CALLBACK_DEL_POLL_FD,
339 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
340 LWS_CALLBACK_LOCK_POLL,
341 LWS_CALLBACK_UNLOCK_POLL,
343 LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
345 LWS_CALLBACK_USER = 1000, /* user code can use any including / above */
349 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
350 typedef SOCKET lws_sockfd_type;
351 #define lws_sockfd_valid(sfd) (!!sfd)
352 struct libwebsocket_pollfd {
357 WINSOCK_API_LINKAGE int WSAAPI WSAPoll(struct libwebsocket_pollfd fdArray[], ULONG fds, INT timeout);
360 #if defined(MBED_OPERATORS)
361 /* it's a class lws_conn * */
362 typedef void * lws_sockfd_type;
363 #define lws_sockfd_valid(sfd) (!!sfd)
369 #define POLLIN 0x0001
370 #define POLLPRI 0x0002
371 #define POLLOUT 0x0004
372 #define POLLERR 0x0008
373 #define POLLHUP 0x0010
374 #define POLLNVAL 0x0020
378 void * mbed3_create_tcp_stream_socket(void);
379 void mbed3_delete_tcp_stream_socket(void *sockfd);
380 void mbed3_tcp_stream_bind(void *sock, int port, struct libwebsocket *);
381 void mbed3_tcp_stream_accept(void *sock, struct libwebsocket *);
383 typedef int lws_sockfd_type;
384 #define lws_sockfd_valid(sfd) (sfd >= 0)
387 #define libwebsocket_pollfd pollfd
390 // argument structure for all external poll related calls
391 // passed in via 'in'
392 struct libwebsocket_pollargs {
393 lws_sockfd_type fd; // applicable file descriptor
394 int events; // the new event mask
395 int prev_events; // the previous event mask
398 enum libwebsocket_extension_callback_reasons {
399 LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT,
400 LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT,
401 LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT,
402 LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT,
403 LWS_EXT_CALLBACK_CONSTRUCT,
404 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
405 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
406 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
407 LWS_EXT_CALLBACK_DESTROY,
408 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
409 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED,
410 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
411 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
412 LWS_EXT_CALLBACK_PACKET_TX_DO_SEND,
413 LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
414 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
415 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
416 LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION,
417 LWS_EXT_CALLBACK_1HZ,
418 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
419 LWS_EXT_CALLBACK_IS_WRITEABLE,
420 LWS_EXT_CALLBACK_PAYLOAD_TX,
421 LWS_EXT_CALLBACK_PAYLOAD_RX,
424 enum lws_write_protocol {
427 LWS_WRITE_CONTINUATION,
430 /* special 04+ opcodes */
436 /* Same as write_http but we know this write ends the transaction */
437 LWS_WRITE_HTTP_FINAL,
441 LWS_WRITE_HTTP_HEADERS,
445 LWS_WRITE_NO_FIN = 0x40,
447 * client packet payload goes out on wire unmunged
448 * only useful for security tests since normal servers cannot
449 * decode the content if used
451 LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
455 * you need these to look at headers that have been parsed if using the
456 * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
457 * list below is absent, .token = NULL and token_len = 0. Otherwise .token
458 * points to .token_len chars containing that header content.
467 * don't forget to update test server header dump accordingly
469 * these have to be kept in sync with lextable.h / minilex.c
472 enum lws_token_indexes {
475 WSI_TOKEN_OPTIONS_URI,
477 WSI_TOKEN_CONNECTION,
482 WSI_TOKEN_EXTENSIONS,
489 WSI_TOKEN_HTTP2_SETTINGS,
490 WSI_TOKEN_HTTP_ACCEPT,
491 WSI_TOKEN_HTTP_AC_REQUEST_HEADERS,
492 WSI_TOKEN_HTTP_IF_MODIFIED_SINCE,
493 WSI_TOKEN_HTTP_IF_NONE_MATCH,
494 WSI_TOKEN_HTTP_ACCEPT_ENCODING,
495 WSI_TOKEN_HTTP_ACCEPT_LANGUAGE,
496 WSI_TOKEN_HTTP_PRAGMA,
497 WSI_TOKEN_HTTP_CACHE_CONTROL,
498 WSI_TOKEN_HTTP_AUTHORIZATION,
499 WSI_TOKEN_HTTP_COOKIE,
500 WSI_TOKEN_HTTP_CONTENT_LENGTH,
501 WSI_TOKEN_HTTP_CONTENT_TYPE,
503 WSI_TOKEN_HTTP_RANGE,
504 WSI_TOKEN_HTTP_REFERER,
509 WSI_TOKEN_HTTP_COLON_AUTHORITY,
510 WSI_TOKEN_HTTP_COLON_METHOD,
511 WSI_TOKEN_HTTP_COLON_PATH,
512 WSI_TOKEN_HTTP_COLON_SCHEME,
513 WSI_TOKEN_HTTP_COLON_STATUS,
515 WSI_TOKEN_HTTP_ACCEPT_CHARSET,
516 WSI_TOKEN_HTTP_ACCEPT_RANGES,
517 WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN,
519 WSI_TOKEN_HTTP_ALLOW,
520 WSI_TOKEN_HTTP_CONTENT_DISPOSITION,
521 WSI_TOKEN_HTTP_CONTENT_ENCODING,
522 WSI_TOKEN_HTTP_CONTENT_LANGUAGE,
523 WSI_TOKEN_HTTP_CONTENT_LOCATION,
524 WSI_TOKEN_HTTP_CONTENT_RANGE,
526 WSI_TOKEN_HTTP_EXPECT,
527 WSI_TOKEN_HTTP_EXPIRES,
529 WSI_TOKEN_HTTP_IF_MATCH,
530 WSI_TOKEN_HTTP_IF_RANGE,
531 WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE,
532 WSI_TOKEN_HTTP_LAST_MODIFIED,
534 WSI_TOKEN_HTTP_LOCATION,
535 WSI_TOKEN_HTTP_MAX_FORWARDS,
536 WSI_TOKEN_HTTP_PROXY_AUTHENTICATE,
537 WSI_TOKEN_HTTP_PROXY_AUTHORIZATION,
538 WSI_TOKEN_HTTP_REFRESH,
539 WSI_TOKEN_HTTP_RETRY_AFTER,
540 WSI_TOKEN_HTTP_SERVER,
541 WSI_TOKEN_HTTP_SET_COOKIE,
542 WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY,
543 WSI_TOKEN_HTTP_TRANSFER_ENCODING,
544 WSI_TOKEN_HTTP_USER_AGENT,
547 WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
552 WSI_TOKEN_DELETE_URI,
554 WSI_TOKEN_HTTP_URI_ARGS,
556 /* use token storage to stash these */
558 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
559 _WSI_TOKEN_CLIENT_PEER_ADDRESS,
560 _WSI_TOKEN_CLIENT_URI,
561 _WSI_TOKEN_CLIENT_HOST,
562 _WSI_TOKEN_CLIENT_ORIGIN,
564 /* always last real token index*/
566 /* parser state additions */
569 WSI_TOKEN_SKIPPING_SAW_CR,
570 WSI_PARSING_COMPLETE,
571 WSI_INIT_TOKEN_MUXURL,
574 struct lws_token_limits {
575 unsigned short token_limit[WSI_TOKEN_COUNT];
582 1000 indicates a normal closure, meaning that the purpose for
583 which the connection was established has been fulfilled.
587 1001 indicates that an endpoint is "going away", such as a server
588 going down or a browser having navigated away from a page.
592 1002 indicates that an endpoint is terminating the connection due
597 1003 indicates that an endpoint is terminating the connection
598 because it has received a type of data it cannot accept (e.g., an
599 endpoint that understands only text data MAY send this if it
600 receives a binary message).
604 Reserved. The specific meaning might be defined in the future.
608 1005 is a reserved value and MUST NOT be set as a status code in a
609 Close control frame by an endpoint. It is designated for use in
610 applications expecting a status code to indicate that no status
611 code was actually present.
615 1006 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 the
618 connection was closed abnormally, e.g., without sending or
619 receiving a Close control frame.
623 1007 indicates that an endpoint is terminating the connection
624 because it has received data within a message that was not
625 consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
626 data within a text message).
630 1008 indicates that an endpoint is terminating the connection
631 because it has received a message that violates its policy. This
632 is a generic status code that can be returned when there is no
633 other more suitable status code (e.g., 1003 or 1009) or if there
634 is a need to hide specific details about the policy.
638 1009 indicates that an endpoint is terminating the connection
639 because it has received a message that is too big for it to
644 1010 indicates that an endpoint (client) is terminating the
645 connection because it has expected the server to negotiate one or
646 more extension, but the server didn't return them in the response
647 message of the WebSocket handshake. The list of extensions that
648 are needed SHOULD appear in the /reason/ part of the Close frame.
649 Note that this status code is not used by the server, because it
650 can fail the WebSocket handshake instead.
654 1011 indicates that a server is terminating the connection because
655 it encountered an unexpected condition that prevented it from
656 fulfilling the request.
660 1015 is a reserved value and MUST NOT be set as a status code in a
661 Close control frame by an endpoint. It is designated for use in
662 applications expecting a status code to indicate that the
663 connection was closed due to a failure to perform a TLS handshake
664 (e.g., the server certificate can't be verified).
667 enum lws_close_status {
668 LWS_CLOSE_STATUS_NOSTATUS = 0,
669 LWS_CLOSE_STATUS_NORMAL = 1000,
670 LWS_CLOSE_STATUS_GOINGAWAY = 1001,
671 LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
672 LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
673 LWS_CLOSE_STATUS_RESERVED = 1004,
674 LWS_CLOSE_STATUS_NO_STATUS = 1005,
675 LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
676 LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
677 LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
678 LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
679 LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
680 LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
681 LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
683 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
687 HTTP_STATUS_OK = 200,
688 HTTP_STATUS_NO_CONTENT = 204,
690 HTTP_STATUS_BAD_REQUEST = 400,
691 HTTP_STATUS_UNAUTHORIZED,
692 HTTP_STATUS_PAYMENT_REQUIRED,
693 HTTP_STATUS_FORBIDDEN,
694 HTTP_STATUS_NOT_FOUND,
695 HTTP_STATUS_METHOD_NOT_ALLOWED,
696 HTTP_STATUS_NOT_ACCEPTABLE,
697 HTTP_STATUS_PROXY_AUTH_REQUIRED,
698 HTTP_STATUS_REQUEST_TIMEOUT,
699 HTTP_STATUS_CONFLICT,
701 HTTP_STATUS_LENGTH_REQUIRED,
702 HTTP_STATUS_PRECONDITION_FAILED,
703 HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
704 HTTP_STATUS_REQ_URI_TOO_LONG,
705 HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
706 HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
707 HTTP_STATUS_EXPECTATION_FAILED,
709 HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
710 HTTP_STATUS_NOT_IMPLEMENTED,
711 HTTP_STATUS_BAD_GATEWAY,
712 HTTP_STATUS_SERVICE_UNAVAILABLE,
713 HTTP_STATUS_GATEWAY_TIMEOUT,
714 HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
718 struct libwebsocket_context;
719 /* needed even with extensions disabled for create context */
720 struct libwebsocket_extension;
723 * callback_function() - User server actions
724 * @context: Websockets context
725 * @wsi: Opaque websocket instance pointer
726 * @reason: The reason for the call
727 * @user: Pointer to per-session user data allocated by library
728 * @in: Pointer used for some callback reasons
729 * @len: Length set for some callback reasons
731 * This callback is the way the user controls what is served. All the
732 * protocol detail is hidden and handled by the library.
734 * For each connection / session there is user data allocated that is
735 * pointed to by "user". You set the size of this user data area when
736 * the library is initialized with libwebsocket_create_server.
738 * You get an opportunity to initialize user data when called back with
739 * LWS_CALLBACK_ESTABLISHED reason.
741 * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with
744 * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
745 * been unable to complete a handshake with the remote server. If
746 * in is non-NULL, you can find an error string of length len where
749 * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
750 * client user code to examine the http headers
751 * and decide to reject the connection. If the
752 * content in the headers is interesting to the
753 * client (url, etc) it needs to copy it out at
754 * this point since it will be destroyed before
755 * the CLIENT_ESTABLISHED call
757 * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
758 * a handshake with the remote server
760 * LWS_CALLBACK_CLOSED: when the websocket session ends
762 * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends
764 * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
765 * remote client, it can be found at *in and is
768 * LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
769 * they appear with this callback reason. PONG
770 * packets only exist in 04+ protocol
772 * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
773 * client connection, it can be found at *in and
776 * LWS_CALLBACK_HTTP: an http request has come from a client that is not
777 * asking to upgrade the connection to a websocket
778 * one. This is a chance to serve http content,
779 * for example, to send a script to the client
780 * which will then open the websockets connection.
781 * @in points to the URI path requested and
782 * lws_serve_http_file() makes it very
783 * simple to send back a file to the client.
784 * Normally after sending the file you are done
785 * with the http connection, since the rest of the
786 * activity will come by websockets from the script
787 * that was delivered by http, so you will want to
788 * return 1; to close and free up the connection.
789 * That's important because it uses a slot in the
790 * total number of client connections allowed set
793 * LWS_CALLBACK_HTTP_BODY: the next @len bytes data from the http
794 * request body HTTP connection is now available in @in.
796 * LWS_CALLBACK_HTTP_BODY_COMPLETION: the expected amount of http request
797 * body has been delivered
799 * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol
802 * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down
803 * http link has completed.
805 * LWS_CALLBACK_CLIENT_WRITEABLE:
806 * LWS_CALLBACK_SERVER_WRITEABLE: If you call
807 * lws_callback_on_writable() on a connection, you will
808 * get one of these callbacks coming when the connection socket
809 * is able to accept another write packet without blocking.
810 * If it already was able to take another packet without blocking,
811 * you'll get this callback at the next call to the service loop
812 * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
813 * and servers get LWS_CALLBACK_SERVER_WRITEABLE.
815 * LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
816 * the server at network level; the connection is accepted but then
817 * passed to this callback to decide whether to hang up immediately
818 * or not, based on the client IP. @in contains the connection
819 * socket's descriptor. Since the client connection information is
820 * not available yet, @wsi still pointing to the main server socket.
821 * Return non-zero to terminate the connection before sending or
822 * receiving anything. Because this happens immediately after the
823 * network connection from the client, there's no websocket protocol
824 * selected yet so this callback is issued only to protocol 0.
826 * LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED: A new client just had
827 * been connected, accepted, and instantiated into the pool. This
828 * callback allows setting any relevant property to it. Because this
829 * happens immediately after the instantiation of a new client,
830 * there's no websocket protocol selected yet so this callback is
831 * issued only to protocol 0. Only @wsi is defined, pointing to the
832 * new client, and the return value is ignored.
834 * LWS_CALLBACK_FILTER_HTTP_CONNECTION: called when the request has
835 * been received and parsed from the client, but the response is
836 * not sent yet. Return non-zero to disallow the connection.
837 * @user is a pointer to the connection user space allocation,
838 * @in is the URI, eg, "/"
839 * In your handler you can use the public APIs
840 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
841 * headers using the header enums lws_token_indexes from
842 * libwebsockets.h to check for and read the supported header
843 * presence and content before deciding to allow the http
844 * connection to proceed or to kill the connection.
846 * LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
847 * been received and parsed from the client, but the response is
848 * not sent yet. Return non-zero to disallow the connection.
849 * @user is a pointer to the connection user space allocation,
850 * @in is the requested protocol name
851 * In your handler you can use the public APIs
852 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
853 * headers using the header enums lws_token_indexes from
854 * libwebsockets.h to check for and read the supported header
855 * presence and content before deciding to allow the handshake
856 * to proceed or to kill the connection.
858 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for
859 * including OpenSSL support, this callback allows your user code
860 * to perform extra SSL_CTX_load_verify_locations() or similar
861 * calls to direct OpenSSL where to find certificates the client
862 * can use to confirm the remote server identity. @user is the
865 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for
866 * including OpenSSL support, this callback allows your user code
867 * to load extra certifcates into the server which allow it to
868 * verify the validity of certificates returned by clients. @user
869 * is the server's OpenSSL SSL_CTX*
871 * LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY: if configured for
872 * including OpenSSL support but no private key file has been specified
873 * (ssl_private_key_filepath is NULL), this callback is called to
874 * allow the user to set the private key directly via libopenssl
875 * and perform further operations if required; this might be useful
876 * in situations where the private key is not directly accessible by
877 * the OS, for example if it is stored on a smartcard
878 * @user is the server's OpenSSL SSL_CTX*
880 * LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the
881 * libwebsockets context was created with the option
882 * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
883 * callback is generated during OpenSSL verification of the cert
884 * sent from the client. It is sent to protocol[0] callback as
885 * no protocol has been negotiated on the connection yet.
886 * Notice that the libwebsockets context and wsi are both NULL
887 * during this callback. See
888 * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
889 * to understand more detail about the OpenSSL callback that
890 * generates this libwebsockets callback and the meanings of the
891 * arguments passed. In this callback, @user is the x509_ctx,
892 * @in is the ssl pointer and @len is preverify_ok
893 * Notice that this callback maintains libwebsocket return
894 * conventions, return 0 to mean the cert is OK or 1 to fail it.
895 * This also means that if you don't handle this callback then
896 * the default callback action of returning 0 allows the client
899 * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens
900 * when a client handshake is being compiled. @user is NULL,
901 * @in is a char **, it's pointing to a char * which holds the
902 * next location in the header buffer where you can add
903 * headers, and @len is the remaining space in the header buffer,
904 * which is typically some hundreds of bytes. So, to add a canned
905 * cookie, your handler code might look similar to:
907 * char **p = (char **)in;
912 * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
916 * Notice if you add anything, you just have to take care about
917 * the CRLF on the line you added. Obviously this callback is
918 * optional, if you don't handle it everything is fine.
920 * Notice the callback is coming to protocols[0] all the time,
921 * because there is no specific protocol handshook yet.
923 * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
924 * sees that it does support a requested extension, before
925 * accepting the extension by additing to the list sent back to
926 * the client it gives this callback just to check that it's okay
927 * to use that extension. It calls back to the requested protocol
928 * and with @in being the extension name, @len is 0 and @user is
929 * valid. Note though at this time the ESTABLISHED callback hasn't
930 * happened yet so if you initialize @user content there, @user
931 * content during this callback might not be useful for anything.
932 * Notice this callback comes to protocols[0].
934 * LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: When a client
935 * connection is being prepared to start a handshake to a server,
936 * each supported extension is checked with protocols[0] callback
937 * with this reason, giving the user code a chance to suppress the
938 * claim to support that extension by returning non-zero. If
939 * unhandled, by default 0 will be returned and the extension
940 * support included in the header to the server. Notice this
941 * callback comes to protocols[0].
943 * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can
944 * do initial setup / allocations etc
946 * LWS_CALLBACK_PROTOCOL_DESTROY: One-time call per protocol indicating
947 * this protocol won't get used at all after this callback, the
948 * context is getting destroyed. Take the opportunity to
949 * deallocate everything that was allocated by the protocol.
951 * LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification
953 * LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification
955 * The next five reasons are optional and only need taking care of if you
956 * will be integrating libwebsockets sockets into an external polling
959 * For these calls, @in points to a struct libwebsocket_pollargs that
960 * contains @fd, @events and @prev_events members
962 * LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
963 * internally, but in the case you are integrating with another
964 * server you will need to have libwebsocket sockets share a
965 * polling array with the other server. This and the other
966 * POLL_FD related callbacks let you put your specialized
967 * poll array interface code in the callback for protocol 0, the
968 * first protocol you support, usually the HTTP protocol in the
970 * This callback happens when a socket needs to be
971 * added to the polling loop: @in points to a struct
972 * libwebsocket_pollargs; the @fd member of the struct is the file
973 * descriptor, and @events contains the active events.
975 * If you are using the internal polling loop (the "service"
976 * callback), you can just ignore these callbacks.
978 * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
979 * needs to be removed from an external polling array. @in is
980 * again the struct libwebsocket_pollargs containing the @fd member
981 * to be removed. If you are using the internal polling
982 * loop, you can just ignore it.
984 * LWS_CALLBACK_CHANGE_MODE_POLL_FD: This callback happens when
985 * libwebsockets wants to modify the events for a connectiion.
986 * @in is the struct libwebsocket_pollargs with the @fd to change.
987 * The new event mask is in @events member and the old mask is in
988 * the @prev_events member.
989 * If you are using the internal polling loop, you can just ignore
992 * LWS_CALLBACK_LOCK_POLL:
993 * LWS_CALLBACK_UNLOCK_POLL: These allow the external poll changes driven
994 * by libwebsockets to participate in an external thread locking
995 * scheme around the changes, so the whole thing is threadsafe.
996 * These are called around three activities in the library,
997 * - inserting a new wsi in the wsi / fd table (len=1)
998 * - deleting a wsi from the wsi / fd table (len=1)
999 * - changing a wsi's POLLIN/OUT state (len=0)
1000 * Locking and unlocking external synchronization objects when
1001 * len == 1 allows external threads to be synchronized against
1002 * wsi lifecycle changes if it acquires the same lock for the
1003 * duration of wsi dereference from the other thread context.
1005 LWS_VISIBLE LWS_EXTERN int callback(struct libwebsocket_context *context,
1006 struct libwebsocket *wsi,
1007 enum libwebsocket_callback_reasons reason, void *user,
1008 void *in, size_t len);
1010 typedef int (callback_function)(struct libwebsocket_context *context,
1011 struct libwebsocket *wsi,
1012 enum libwebsocket_callback_reasons reason, void *user,
1013 void *in, size_t len);
1015 #ifndef LWS_NO_EXTENSIONS
1017 * extension_callback_function() - Hooks to allow extensions to operate
1018 * @context: Websockets context
1019 * @ext: This extension
1020 * @wsi: Opaque websocket instance pointer
1021 * @reason: The reason for the call
1022 * @user: Pointer to per-session user data allocated by library
1023 * @in: Pointer used for some callback reasons
1024 * @len: Length set for some callback reasons
1026 * Each extension that is active on a particular connection receives
1027 * callbacks during the connection lifetime to allow the extension to
1028 * operate on websocket data and manage itself.
1030 * Libwebsockets takes care of allocating and freeing "user" memory for
1031 * each active extension on each connection. That is what is pointed to
1032 * by the @user parameter.
1034 * LWS_EXT_CALLBACK_CONSTRUCT: called when the server has decided to
1035 * select this extension from the list provided by the client,
1036 * just before the server will send back the handshake accepting
1037 * the connection with this extension active. This gives the
1038 * extension a chance to initialize its connection context found
1041 * LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT
1042 * but called when client is instantiating this extension. Some
1043 * extensions will work the same on client and server side and then
1044 * you can just merge handlers for both CONSTRUCTS.
1046 * LWS_EXT_CALLBACK_DESTROY: called when the connection the extension was
1047 * being used on is about to be closed and deallocated. It's the
1048 * last chance for the extension to deallocate anything it has
1049 * allocated in the user data (pointed to by @user) before the
1050 * user data is deleted. This same callback is used whether you
1051 * are in client or server instantiation context.
1053 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on
1054 * a connection, and a packet of data arrived at the connection,
1055 * it is passed to this callback to give the extension a chance to
1056 * change the data, eg, decompress it. @user is pointing to the
1057 * extension's private connection context data, @in is pointing
1058 * to an lws_tokens struct, it consists of a char * pointer called
1059 * token, and an int called token_len. At entry, these are
1060 * set to point to the received buffer and set to the content
1061 * length. If the extension will grow the content, it should use
1062 * a new buffer allocated in its private user context data and
1063 * set the pointed-to lws_tokens members to point to its buffer.
1065 * LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as
1066 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
1067 * extension a chance to change websocket data just before it will
1068 * be sent out. Using the same lws_token pointer scheme in @in,
1069 * the extension can change the buffer and the length to be
1070 * transmitted how it likes. Again if it wants to grow the
1071 * buffer safely, it should copy the data into its own buffer and
1072 * set the lws_tokens token pointer to it.
1074 LWS_VISIBLE LWS_EXTERN int extension_callback(struct libwebsocket_context *context,
1075 struct libwebsocket_extension *ext,
1076 struct libwebsocket *wsi,
1077 enum libwebsocket_extension_callback_reasons reason,
1078 void *user, void *in, size_t len);
1080 typedef int (extension_callback_function)(struct libwebsocket_context *context,
1081 struct libwebsocket_extension *ext,
1082 struct libwebsocket *wsi,
1083 enum libwebsocket_extension_callback_reasons reason,
1084 void *user, void *in, size_t len);
1088 * struct libwebsocket_protocols - List of protocols and handlers server
1090 * @name: Protocol name that must match the one given in the client
1091 * Javascript new WebSocket(url, 'protocol') name.
1092 * @callback: The service callback used for this protocol. It allows the
1093 * service action for an entire protocol to be encapsulated in
1094 * the protocol-specific callback
1095 * @per_session_data_size: Each new connection using this protocol gets
1096 * this much memory allocated on connection establishment and
1097 * freed on connection takedown. A pointer to this per-connection
1098 * allocation is passed into the callback in the 'user' parameter
1099 * @rx_buffer_size: if you want atomic frames delivered to the callback, you
1100 * should set this to the size of the biggest legal frame that
1101 * you support. If the frame size is exceeded, there is no
1102 * error, but the buffer will spill to the user callback when
1103 * full, which you can detect by using
1104 * lws_remaining_packet_payload(). Notice that you
1105 * just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING
1106 * and post-padding are automatically also allocated on top.
1107 * @id: ignored by lws, but useful to contain user information bound
1108 * to the selected protocol. For example if this protocol was
1109 * called "myprotocol-v2", you might set id to 2, and the user
1110 * code that acts differently according to the version can do so by
1111 * switch (wsi->protocol->id), user code might use some bits as
1112 * capability flags based on selected protocol version, etc.
1113 * @user: User provided context data at the protocol level.
1114 * Accessible via lws_get_protocol(wsi)->user
1115 * This should not be confused with wsi->user, it is not the same.
1116 * The library completely ignores any value in here.
1117 * @owning_server: the server init call fills in this opaque pointer when
1118 * registering this protocol with the server.
1119 * @protocol_index: which protocol we are starting from zero
1121 * This structure represents one protocol supported by the server. An
1122 * array of these structures is passed to libwebsocket_create_server()
1123 * allows as many protocols as you like to be handled by one server.
1125 * The first protocol given has its callback used for user callbacks when
1126 * there is no agreed protocol name, that's true during HTTP part of the
1127 * connection and true if the client did not send a Protocol: header.
1130 struct libwebsocket_protocols {
1132 callback_function *callback;
1133 size_t per_session_data_size;
1134 size_t rx_buffer_size;
1139 * below are filled in on server init and can be left uninitialized,
1140 * no need for user to use them directly either
1143 struct libwebsocket_context *owning_server;
1147 #ifndef LWS_NO_EXTENSIONS
1149 * struct libwebsocket_extension - An extension we know how to cope with
1151 * @name: Formal extension name, eg, "deflate-stream"
1152 * @callback: Service callback
1153 * @per_session_data_size: Libwebsockets will auto-malloc this much
1154 * memory for the use of the extension, a pointer
1155 * to it comes in the @user callback parameter
1156 * @per_context_private_data: Optional storage for this extension that
1157 * is per-context, so it can track stuff across
1158 * all sessions, etc, if it wants
1161 struct libwebsocket_extension {
1163 extension_callback_function *callback;
1164 size_t per_session_data_size;
1165 void *per_context_private_data;
1170 * struct lws_context_creation_info: parameters to create context with
1172 * @port: Port to listen on... you can use CONTEXT_PORT_NO_LISTEN to
1173 * suppress listening on any port, that's what you want if you are
1174 * not running a websocket server at all but just using it as a
1176 * @iface: NULL to bind the listen socket to all interfaces, or the
1177 * interface name, eg, "eth2"
1178 * @protocols: Array of structures listing supported protocols and a protocol-
1179 * specific callback for each one. The list is ended with an
1180 * entry that has a NULL callback pointer.
1181 * It's not const because we write the owning_server member
1182 * @extensions: NULL or array of libwebsocket_extension structs listing the
1183 * extensions this context supports. If you configured with
1184 * --without-extensions, you should give NULL here.
1185 * @token_limits: NULL or struct lws_token_limits pointer which is initialized
1186 * with a token length limit for each possible WSI_TOKEN_***
1187 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
1188 * to listen using SSL, set to the filepath to fetch the
1189 * server cert from, otherwise NULL for unencrypted
1190 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode;
1191 * if this is set to NULL but sll_cert_filepath is set, the
1192 * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called to allow
1193 * setting of the private key directly via openSSL library calls
1194 * @ssl_ca_filepath: CA certificate filepath or NULL
1195 * @ssl_cipher_list: List of valid ciphers to use (eg,
1196 * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
1197 * or you can leave it as NULL to get "DEFAULT"
1198 * @http_proxy_address: If non-NULL, attempts to proxy via the given address.
1199 * If proxy auth is required, use format
1200 * "username:password@server:port"
1201 * @http_proxy_port: If http_proxy_address was non-NULL, uses this port at the address
1202 * @gid: group id to change to after setting listen socket, or -1.
1203 * @uid: user id to change to after setting listen socket, or -1.
1204 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
1205 * @user: optional user pointer that can be recovered via the context
1206 * pointer using lws_context_user
1207 * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to
1208 * all libwebsocket sockets, client or server
1209 * @ka_probes: if ka_time was nonzero, after the timeout expires how many
1210 * times to try to get a response from the peer before giving up
1211 * and killing the connection
1212 * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes
1214 * @provided_client_ssl_ctx: If non-null, swap out libwebsockets ssl
1215 * implementation for the one provided by provided_ssl_ctx.
1216 * Libwebsockets no longer is responsible for freeing the context
1217 * if this option is selected.
1220 struct lws_context_creation_info {
1223 struct libwebsocket_protocols *protocols;
1224 struct libwebsocket_extension *extensions;
1225 struct lws_token_limits *token_limits;
1226 const char *ssl_private_key_password;
1227 const char *ssl_cert_filepath;
1228 const char *ssl_private_key_filepath;
1229 const char *ssl_ca_filepath;
1230 const char *ssl_cipher_list;
1231 const char *http_proxy_address;
1232 unsigned int http_proxy_port;
1235 unsigned int options;
1240 #ifdef LWS_OPENSSL_SUPPORT
1241 SSL_CTX *provided_client_ssl_ctx;
1242 #else /* maintain structure layout either way */
1243 void *provided_client_ssl_ctx;
1247 LWS_VISIBLE LWS_EXTERN
1248 void lws_set_log_level(int level,
1249 void (*log_emit_function)(int level, const char *line));
1251 LWS_VISIBLE LWS_EXTERN void
1252 lwsl_emit_syslog(int level, const char *line);
1254 LWS_VISIBLE LWS_EXTERN struct libwebsocket_context *
1255 lws_create_context(struct lws_context_creation_info *info);
1257 LWS_VISIBLE LWS_EXTERN int
1258 lws_set_proxy(struct libwebsocket_context *context, const char *proxy);
1260 LWS_VISIBLE LWS_EXTERN void
1261 lws_context_destroy(struct libwebsocket_context *context);
1263 LWS_VISIBLE LWS_EXTERN int
1264 lws_service(struct libwebsocket_context *context, int timeout_ms);
1266 LWS_VISIBLE LWS_EXTERN void
1267 lws_cancel_service(struct libwebsocket_context *context);
1269 LWS_VISIBLE LWS_EXTERN const unsigned char *
1270 lws_token_to_string(enum lws_token_indexes token);
1272 LWS_VISIBLE LWS_EXTERN int
1273 lws_add_http_header_by_name(struct libwebsocket_context *context,
1274 struct libwebsocket *wsi,
1275 const unsigned char *name,
1276 const unsigned char *value,
1279 unsigned char *end);
1280 LWS_VISIBLE LWS_EXTERN int
1281 lws_finalize_http_header(struct libwebsocket_context *context,
1282 struct libwebsocket *wsi,
1284 unsigned char *end);
1285 LWS_VISIBLE LWS_EXTERN int
1286 lws_add_http_header_by_token(struct libwebsocket_context *context,
1287 struct libwebsocket *wsi,
1288 enum lws_token_indexes token,
1289 const unsigned char *value,
1292 unsigned char *end);
1293 LWS_VISIBLE LWS_EXTERN int
1294 lws_add_http_header_content_length(struct libwebsocket_context *context,
1295 struct libwebsocket *wsi,
1296 unsigned long content_length,
1298 unsigned char *end);
1299 LWS_VISIBLE LWS_EXTERN int
1300 lws_add_http_header_status(struct libwebsocket_context *context,
1301 struct libwebsocket *wsi,
1304 unsigned char *end);
1306 LWS_EXTERN int lws_http_transaction_completed(struct libwebsocket *wsi);
1308 #ifdef LWS_USE_LIBEV
1309 typedef void (lws_ev_signal_cb)(EV_P_ struct ev_signal *w, int revents);
1311 LWS_VISIBLE LWS_EXTERN int
1313 struct libwebsocket_context *context,
1315 lws_ev_signal_cb* cb);
1317 LWS_VISIBLE LWS_EXTERN int
1319 struct libwebsocket_context *context, struct ev_loop *loop);
1323 struct ev_loop *loop, struct ev_signal *watcher, int revents);
1324 #endif /* LWS_USE_LIBEV */
1326 LWS_VISIBLE LWS_EXTERN int
1327 lws_service_fd(struct libwebsocket_context *context,
1328 struct libwebsocket_pollfd *pollfd);
1330 LWS_VISIBLE LWS_EXTERN void *
1331 lws_context_user(struct libwebsocket_context *context);
1333 enum pending_timeout {
1334 NO_PENDING_TIMEOUT = 0,
1335 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
1336 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
1337 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
1338 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
1339 PENDING_TIMEOUT_AWAITING_PING,
1340 PENDING_TIMEOUT_CLOSE_ACK,
1341 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
1342 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
1343 PENDING_TIMEOUT_SSL_ACCEPT,
1344 PENDING_TIMEOUT_HTTP_CONTENT,
1345 PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
1346 PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE,
1349 LWS_VISIBLE LWS_EXTERN void
1350 lws_set_timeout(struct libwebsocket *wsi,
1351 enum pending_timeout reason, int secs);
1356 * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
1357 * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
1358 * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
1360 * This allows us to add protocol info before and after the data, and send as
1361 * one packet on the network without payload copying, for maximum efficiency.
1363 * So for example you need this kind of code to use lws_write with a
1366 * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
1368 * // fill your part of the buffer... for example here it's all zeros
1369 * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
1371 * lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128,
1374 * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
1375 * use the whole buffer without taking care of the above.
1379 * this is the frame nonce plus two header plus 8 length
1380 * there's an additional two for mux extension per mux nesting level
1381 * 2 byte prepend on close will already fit because control frames cannot use
1382 * the big length style
1385 // Pad LWS_SEND_BUFFER_PRE_PADDING to the CPU word size, so that word references
1386 // to the address immediately after the padding won't cause an unaligned access
1387 // error. Sometimes the recommended padding is even larger than the size of a void *.
1388 // For example, for the X86-64 architecture, in Intel's document
1389 // https://software.intel.com/en-us/articles/data-alignment-when-migrating-to-64-bit-intel-architecture
1390 // they recommend that structures larger than 16 bytes be aligned to 16-byte
1394 #if !defined(LWS_SIZEOFPTR)
1395 #define LWS_SIZEOFPTR (sizeof (void *))
1397 #if !defined(u_int64_t)
1398 #define u_int64_t unsigned long long
1402 #define _LWS_PAD_SIZE 16 // Intel recommended for best performance.
1404 #define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target architecture */
1406 #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
1407 #define LWS_SEND_BUFFER_PRE_PADDING _LWS_PAD(4 + 10 + (2 * MAX_MUX_RECURSION))
1408 #define LWS_SEND_BUFFER_POST_PADDING 4
1410 LWS_VISIBLE LWS_EXTERN int
1411 lws_write(struct libwebsocket *wsi, unsigned char *buf, size_t len,
1412 enum lws_write_protocol protocol);
1414 /* helper for case where buffer may be const */
1415 #define lws_write_http(wsi, buf, len) \
1416 lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
1418 LWS_VISIBLE LWS_EXTERN int
1419 lws_serve_http_file(struct libwebsocket_context *context,
1420 struct libwebsocket *wsi, const char *file,
1421 const char *content_type, const char *other_headers,
1422 int other_headers_len);
1423 LWS_VISIBLE LWS_EXTERN int
1424 lws_serve_http_file_fragment(struct libwebsocket_context *context,
1425 struct libwebsocket *wsi);
1427 LWS_VISIBLE LWS_EXTERN int lws_return_http_status(
1428 struct libwebsocket_context *context,
1429 struct libwebsocket *wsi, unsigned int code,
1430 const char *html_body);
1432 LWS_VISIBLE LWS_EXTERN const struct libwebsocket_protocols *
1433 lws_get_protocol(struct libwebsocket *wsi);
1435 LWS_VISIBLE LWS_EXTERN int
1436 lws_callback_on_writable(struct libwebsocket_context *context,
1437 struct libwebsocket *wsi);
1439 LWS_VISIBLE LWS_EXTERN int
1440 lws_callback_on_writable_all_protocol(
1441 const struct libwebsocket_protocols *protocol);
1443 LWS_VISIBLE LWS_EXTERN int
1444 lws_callback_all_protocol(
1445 const struct libwebsocket_protocols *protocol, int reason);
1447 LWS_VISIBLE LWS_EXTERN int
1448 lws_get_socket_fd(struct libwebsocket *wsi);
1450 LWS_VISIBLE LWS_EXTERN int
1451 lws_is_final_fragment(struct libwebsocket *wsi);
1453 LWS_VISIBLE LWS_EXTERN unsigned char
1454 lws_get_reserved_bits(struct libwebsocket *wsi);
1456 LWS_VISIBLE LWS_EXTERN int
1457 lws_rx_flow_control(struct libwebsocket *wsi, int enable);
1459 LWS_VISIBLE LWS_EXTERN void
1460 lws_rx_flow_allow_all_protocol(const struct libwebsocket_protocols *protocol);
1462 LWS_VISIBLE LWS_EXTERN size_t
1463 lws_remaining_packet_payload(struct libwebsocket *wsi);
1466 * if the protocol does not have any guidance, returns -1. Currently only
1467 * http2 connections get send window information from this API. But your code
1468 * should use it so it can work properly with any protocol.
1470 * If nonzero return is the amount of payload data the peer or intermediary has
1471 * reported it has buffer space for. That has NO relationship with the amount
1472 * of buffer space your OS can accept on this connection for a write action.
1474 * This number represents the maximum you could send to the peer or intermediary
1475 * on this connection right now without it complaining.
1477 * lws manages accounting for send window updates and payload writes
1478 * automatically, so this number reflects the situation at the peer or
1479 * intermediary dynamically.
1481 LWS_VISIBLE LWS_EXTERN size_t
1482 lws_get_peer_write_allowance(struct libwebsocket *wsi);
1484 LWS_VISIBLE LWS_EXTERN struct libwebsocket *
1485 lws_client_connect(struct libwebsocket_context *clients,
1486 const char *address,
1492 const char *protocol,
1493 int ietf_version_or_minus_one);
1495 LWS_VISIBLE LWS_EXTERN struct libwebsocket *
1496 lws_client_connect_extended(struct libwebsocket_context *clients,
1497 const char *address,
1503 const char *protocol,
1504 int ietf_version_or_minus_one,
1507 LWS_VISIBLE LWS_EXTERN const char *
1508 lws_canonical_hostname(struct libwebsocket_context *context);
1511 LWS_VISIBLE LWS_EXTERN void
1512 lws_get_peer_addresses(struct libwebsocket_context *context,
1513 struct libwebsocket *wsi, lws_sockfd_type fd,
1514 char *name, int name_len,
1515 char *rip, int rip_len);
1517 LWS_VISIBLE LWS_EXTERN int
1518 lws_get_random(struct libwebsocket_context *context, void *buf, int len);
1520 LWS_VISIBLE LWS_EXTERN int
1521 lws_daemonize(const char *_lock_path);
1523 LWS_VISIBLE LWS_EXTERN int
1524 lws_send_pipe_choked(struct libwebsocket *wsi);
1526 LWS_VISIBLE LWS_EXTERN int
1527 lws_partial_buffered(struct libwebsocket *wsi);
1529 LWS_VISIBLE LWS_EXTERN int
1530 lws_frame_is_binary(struct libwebsocket *wsi);
1532 LWS_VISIBLE LWS_EXTERN int
1533 lws_is_ssl(struct libwebsocket *wsi);
1534 #ifdef LWS_SHA1_USE_OPENSSL_NAME
1535 #define lws_SHA1 SHA1
1537 LWS_VISIBLE LWS_EXTERN unsigned char *
1538 lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
1541 LWS_VISIBLE LWS_EXTERN int
1542 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
1544 LWS_VISIBLE LWS_EXTERN int
1545 lws_b64_decode_string(const char *in, char *out, int out_size);
1547 LWS_VISIBLE LWS_EXTERN const char *
1548 lws_get_library_version(void);
1550 /* access to headers... only valid while headers valid */
1552 LWS_VISIBLE LWS_EXTERN int
1553 lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h);
1555 LWS_VISIBLE LWS_EXTERN int
1556 lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len,
1557 enum lws_token_indexes h);
1560 * Note: this is not normally needed as a user api. It's provided in case it is
1561 * useful when integrating with other app poll loop service code.
1564 LWS_VISIBLE LWS_EXTERN int
1565 lws_read(struct libwebsocket_context *context,
1566 struct libwebsocket *wsi,
1567 unsigned char *buf, size_t len);
1569 #ifndef LWS_NO_EXTENSIONS
1570 LWS_VISIBLE LWS_EXTERN struct libwebsocket_extension *lws_get_internal_extensions();
1574 * custom allocator support
1576 LWS_VISIBLE LWS_EXTERN void
1577 lws_set_allocator(void *(*realloc)(void *ptr, size_t size));