blob: 6b3112d927ddb6e6d1b4755200d1857a63a817fb [file] [log] [blame]
Andy Greena0da8a82010-11-08 17:12:19 +00001/*
2 * libwebsockets - small server side websockets and web server implementation
Andy Greene77ddd82010-11-13 10:03:47 +00003 *
Andy Green7e37d102015-01-28 21:03:49 +08004 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
Andy Greena0da8a82010-11-08 17:12:19 +00005 *
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.
10 *
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.
15 *
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,
19 * MA 02110-1301 USA
20 */
21
Markus Elfring75212332013-10-26 20:23:00 +080022#ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
23#define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
Andy Greenab990e42010-10-31 12:42:52 +000024
Andy Green29a44cf2015-12-04 07:55:17 +080025/* old (pre 1.6) api name compatibility defines */
26
27#define libwebsocket_create_context lws_create_context
28#define libwebsocket_set_proxy lws_set_proxy
Andy Green4b85c1d2015-12-04 11:08:32 +080029#define lws_context_destroy lws_context_destroy
Andy Green29a44cf2015-12-04 07:55:17 +080030#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
Andy Green4b85c1d2015-12-04 11:08:32 +080036#define lws_context_user lws_context_user
Andy Green29a44cf2015-12-04 07:55:17 +080037#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
Andy Green62304762015-12-04 08:43:54 +080059#define libwebsocket_write_protocol lws_write_protocol
Andy Green29a44cf2015-12-04 07:55:17 +080060
Andy Green4b85c1d2015-12-04 11:08:32 +080061#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 lws_callback_reasons lws_callback_reasons
66#define libwebsocket lws
67
Andy Greena11fe942011-09-25 10:30:26 +010068#ifdef __cplusplus
Paulo Roberto Urio4144e832012-06-04 09:04:33 +080069#include <cstddef>
José Luis Millán080e6dd2014-12-16 12:54:33 +010070#include <cstdarg>
Andy Green2cd30742015-11-02 13:10:33 +080071#ifdef MBED_OPERATORS
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"
Andy Green8c0d3c02015-11-02 20:34:12 +080076
77namespace {
78 const int SERVER_PORT = 80;
79 const int BUFFER_SIZE = 4096;
80}
81using namespace mbed::Sockets::v0;
82
Andy Green4b85c1d2015-12-04 11:08:32 +080083struct lws;
84struct lws_context;
Andy Green8c0d3c02015-11-02 20:34:12 +080085
86class lws_conn {
87 public:
88 lws_conn():
89 ts(NULL),
Andy Green87eeb0a2015-11-25 08:22:08 +080090 wsi(NULL),
91 writeable(1),
92 awaiting_on_writeable(0)
Andy Green8c0d3c02015-11-02 20:34:12 +080093 {
94 }
95
96public:
Andy Green4b85c1d2015-12-04 11:08:32 +080097 void set_wsi(struct lws *_wsi) { wsi = _wsi; }
Andy Green11f27342015-11-08 12:10:26 +080098 int actual_onRX(Socket *s);
99 void 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);
Andy Green4b85c1d2015-12-04 11:08:32 +0800103 void serialized_writeable(struct lws *wsi);
Andy Green11f27342015-11-08 12:10:26 +0800104
Andy Green8c0d3c02015-11-02 20:34:12 +0800105public:
106 TCPStream *ts;
107
108public:
Andy Green4b85c1d2015-12-04 11:08:32 +0800109 struct lws *wsi;
Andy Green8c0d3c02015-11-02 20:34:12 +0800110 char buffer[BUFFER_SIZE];
Andy Green87eeb0a2015-11-25 08:22:08 +0800111 char writeable;
112 char awaiting_on_writeable;
Andy Green8c0d3c02015-11-02 20:34:12 +0800113};
114
115class lws_conn_listener : lws_conn {
116public:
117 lws_conn_listener():
118 srv(SOCKET_STACK_LWIP_IPV4)
119 {
120 srv.setOnError(TCPStream::ErrorHandler_t(this, &lws_conn_listener::onError));
121 }
122
123 void start(const uint16_t port);
124
125protected:
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);
Andy Green8c0d3c02015-11-02 20:34:12 +0800130
131public:
132 TCPListener srv;
133};
134
Andy Green2cd30742015-11-02 13:10:33 +0800135#endif
136
Neil Hormanfc98f9a2014-12-13 14:15:48 +0800137extern "C" {
Johan Simonssonbb87ac82015-01-04 11:15:23 +0000138#else
139#include <stdarg.h>
Andy Greena11fe942011-09-25 10:30:26 +0100140#endif
wonder-mice41802c72015-04-22 00:16:57 -0700141
Andy Green11f27342015-11-08 12:10:26 +0800142#ifdef MBED_OPERATORS
143#define LWS_POSIX 0
144#else
145#define LWS_POSIX 1
146#endif
147
Andy Green158e8042014-04-02 14:25:10 +0800148#include "lws_config.h"
Andy Greena11fe942011-09-25 10:30:26 +0100149
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100150#if defined(WIN32) || defined(_WIN32)
Stephan Eberleb820e2c2015-10-23 08:10:55 +0200151#if (WINVER < 0x0501)
152#undef WINVER
153#undef _WIN32_WINNT
154#define WINVER 0x0501
155#define _WIN32_WINNT WINVER
156#endif
Peter Hinz56885f32011-03-02 22:03:47 +0000157#ifndef WIN32_LEAN_AND_MEAN
158#define WIN32_LEAN_AND_MEAN
159#endif
160#include <winsock2.h>
161#include <ws2tcpip.h>
Paulo Roberto Urio4144e832012-06-04 09:04:33 +0800162#include <stddef.h>
Andy Greena3439032014-11-30 13:17:35 +0800163#include <stdint.h>
pmcdona0a0a7eb2013-12-18 10:17:25 +0800164#include <basetsd.h>
Peter Hinz56885f32011-03-02 22:03:47 +0000165
Oleg Golosovskiy2ef75052011-10-04 20:20:14 +0800166#define strcasecmp stricmp
Andy Greendfb23042013-01-17 12:26:48 +0800167#define getdtablesize() 30000
Oleg Golosovskiy2ef75052011-10-04 20:20:14 +0800168
Andy Green3661d7b2013-04-26 07:37:16 +0800169#define LWS_VISIBLE
170
David Galeano9454e212011-09-26 12:17:20 +0100171#ifdef LWS_DLL
172#ifdef LWS_INTERNAL
173#define LWS_EXTERN extern __declspec(dllexport)
174#else
175#define LWS_EXTERN extern __declspec(dllimport)
176#endif
Andy Green3661d7b2013-04-26 07:37:16 +0800177#else
178#define LWS_EXTERN
David Galeano9454e212011-09-26 12:17:20 +0100179#endif
180
Joakim Soderbergf272cb02013-02-13 09:29:26 +0800181#else // NOT WIN32
Andy Greendfb23042013-01-17 12:26:48 +0800182#include <unistd.h>
Andy Green5f2a8152015-11-02 08:21:08 +0800183
184#ifndef MBED_OPERATORS
185#include <poll.h>
Andy Green7e37d102015-01-28 21:03:49 +0800186#include <netdb.h>
Andy Green8c0d3c02015-11-02 20:34:12 +0800187#else
Andy Green87eeb0a2015-11-25 08:22:08 +0800188#define getdtablesize() (20)
Andy Green5f2a8152015-11-02 08:21:08 +0800189#endif
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800190
191#if defined(__GNUC__)
192#define LWS_VISIBLE __attribute__((visibility("default")))
193#else
194#define LWS_VISIBLE
195#endif
196
Krishnaraj R Bhat1662c622015-10-28 21:02:04 +0530197#if defined(__ANDROID__)
198#define getdtablesize() 1024
199#endif
200
Peter Hinz56885f32011-03-02 22:03:47 +0000201#endif
Andy Green9f990342011-02-12 11:57:45 +0000202
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800203#ifdef LWS_USE_LIBEV
204#include <ev.h>
205#endif /* LWS_USE_LIBEV */
206
David Galeano9454e212011-09-26 12:17:20 +0100207#ifndef LWS_EXTERN
208#define LWS_EXTERN extern
209#endif
Andy Green158e8042014-04-02 14:25:10 +0800210
211#ifdef _WIN32
212#define random rand
213#else
214#include <sys/time.h>
215#include <unistd.h>
216#endif
David Galeano9454e212011-09-26 12:17:20 +0100217
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +0800218#ifdef LWS_OPENSSL_SUPPORT
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200219#ifdef USE_WOLFSSL
ABruines80a70682015-08-09 22:56:32 +0200220#ifdef USE_OLD_CYASSL
221#include <cyassl/openssl/ssl.h>
222#else
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200223#include <wolfssl/openssl/ssl.h>
ABruines80a70682015-08-09 22:56:32 +0200224#endif /* not USE_OLD_CYASSL */
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +0800225#else
226#include <openssl/ssl.h>
Alexander Bruinesc3bcb892015-08-08 18:54:49 +0200227#endif /* not USE_WOLFSSL */
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +0800228#endif
229
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800230#define CONTEXT_PORT_NO_LISTEN -1
Andy Greena41314f2011-05-23 10:00:03 +0100231#define MAX_MUX_RECURSION 2
Andy Greenbfb051f2011-02-09 08:49:14 +0000232
Andy Green43db0452013-01-10 19:50:35 +0800233enum lws_log_levels {
234 LLL_ERR = 1 << 0,
235 LLL_WARN = 1 << 1,
Andy Green7c19c342013-01-19 12:18:07 +0800236 LLL_NOTICE = 1 << 2,
237 LLL_INFO = 1 << 3,
238 LLL_DEBUG = 1 << 4,
239 LLL_PARSER = 1 << 5,
240 LLL_HEADER = 1 << 6,
241 LLL_EXT = 1 << 7,
242 LLL_CLIENT = 1 << 8,
Andy Greend636e352013-01-29 12:36:17 +0800243 LLL_LATENCY = 1 << 9,
Andy Green43db0452013-01-10 19:50:35 +0800244
Andy Greend636e352013-01-29 12:36:17 +0800245 LLL_COUNT = 10 /* set to count of valid flags */
Andy Green43db0452013-01-10 19:50:35 +0800246};
247
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800248LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
Neal Horman98e491f2014-12-10 18:50:28 -0600249LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
Andy Green69e43642013-01-19 11:58:07 +0800250
Andy Greend636e352013-01-29 12:36:17 +0800251/* notice, warn and log are always compiled in */
Andy Green69e43642013-01-19 11:58:07 +0800252#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__)
Andy Green69e43642013-01-19 11:58:07 +0800255/*
Andy Greend636e352013-01-29 12:36:17 +0800256 * weaker logging can be deselected at configure time using --disable-debug
Andy Green69e43642013-01-19 11:58:07 +0800257 * that gets rid of the overhead of checking while keeping _warn and _err
258 * active
259 */
260#ifdef _DEBUG
Andy Greend636e352013-01-29 12:36:17 +0800261
Andy Green69e43642013-01-19 11:58:07 +0800262#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__)
Andy Greend636e352013-01-29 12:36:17 +0800266#define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
Andy Green69e43642013-01-19 11:58:07 +0800267#define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
Andy Greend636e352013-01-29 12:36:17 +0800268#define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800269LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
Andy Greend636e352013-01-29 12:36:17 +0800270
Andy Green69e43642013-01-19 11:58:07 +0800271#else /* no debug */
Andy Greend636e352013-01-29 12:36:17 +0800272
Andy Green2cd30742015-11-02 13:10:33 +0800273#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(...) {}
Andy Green69e43642013-01-19 11:58:07 +0800280#define lwsl_hexdump(a, b)
Andy Greend636e352013-01-29 12:36:17 +0800281
Andy Green69e43642013-01-19 11:58:07 +0800282#endif
283
Andy Green4e7a1332013-11-11 07:30:33 +0800284#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
285
Andy Green6ea337a2014-04-21 08:27:19 +0900286/* api change list for user code to test against */
287
288#define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
289
Andy Green4b85c1d2015-12-04 11:08:32 +0800290/* the struct lws_protocols has the id field present */
Michael Haberlera2d3bf12014-08-11 14:36:57 +0200291#define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
292
Andy Green97ee57f2014-10-29 09:39:08 +0800293/* you can call lws_get_peer_write_allowance */
294#define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
Andy Green6ea337a2014-04-21 08:27:19 +0900295
Michael Haberler49d0ce12014-12-01 14:05:51 +0100296/* extra parameter introduced in 917f43ab821 */
297#define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
298
Andy Green4b85c1d2015-12-04 11:08:32 +0800299enum lws_context_options {
Andy Greenc6bf2c22011-02-20 11:10:47 +0000300 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2,
Andy Green788c4a82012-10-22 12:29:57 +0100301 LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4,
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800302 LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = 8,
James Devine3f13ea22014-03-24 16:09:25 +0800303 LWS_SERVER_OPTION_LIBEV = 16,
304 LWS_SERVER_OPTION_DISABLE_IPV6 = 32,
Andy Greenf55b2ef2014-07-05 10:59:59 +0800305 LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = 64,
Andy Green6d59f592015-10-15 09:12:58 +0800306 LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = 128,
Andy Greenbfb051f2011-02-09 08:49:14 +0000307};
Andy Green8014b292011-01-30 20:57:25 +0000308
Andy Green4b85c1d2015-12-04 11:08:32 +0800309enum lws_callback_reasons {
Andy Green775c0dd2010-10-29 14:15:22 +0100310 LWS_CALLBACK_ESTABLISHED,
David Brooks80a44972012-04-20 12:18:47 +0800311 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
Andy Green2b57a342013-02-06 15:15:25 +0900312 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
Andy Green90c7cbc2011-01-27 06:26:52 +0000313 LWS_CALLBACK_CLIENT_ESTABLISHED,
Andy Green775c0dd2010-10-29 14:15:22 +0100314 LWS_CALLBACK_CLOSED,
Andy Green0c9563b2013-06-10 22:54:40 +0800315 LWS_CALLBACK_CLOSED_HTTP,
Andy Green775c0dd2010-10-29 14:15:22 +0100316 LWS_CALLBACK_RECEIVE,
John Tarlton05fc6ba2015-10-05 11:35:52 +0100317 LWS_CALLBACK_RECEIVE_PONG,
Andy Green4739e5c2011-01-22 12:51:57 +0000318 LWS_CALLBACK_CLIENT_RECEIVE,
Andy Greena6cbece2011-01-27 20:06:03 +0000319 LWS_CALLBACK_CLIENT_RECEIVE_PONG,
Andy Green90c7cbc2011-01-27 06:26:52 +0000320 LWS_CALLBACK_CLIENT_WRITEABLE,
Andy Green9e4c2b62011-03-07 20:47:39 +0000321 LWS_CALLBACK_SERVER_WRITEABLE,
Andy Greena2b0ab02010-11-11 12:28:29 +0000322 LWS_CALLBACK_HTTP,
kapejodce64fb02013-11-19 13:38:16 +0100323 LWS_CALLBACK_HTTP_BODY,
324 LWS_CALLBACK_HTTP_BODY_COMPLETION,
Andy Greend280b6e2013-01-15 13:40:23 +0800325 LWS_CALLBACK_HTTP_FILE_COMPLETION,
Andy Green54cb3462013-02-14 22:23:54 +0800326 LWS_CALLBACK_HTTP_WRITEABLE,
Andy Green07034092011-02-13 08:37:12 +0000327 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
Andy Green19895bc2013-11-09 11:59:56 +0800328 LWS_CALLBACK_FILTER_HTTP_CONNECTION,
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200329 LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED,
Andy Greenc85619d2011-02-13 08:25:26 +0000330 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
Andy Green0894bda2011-02-19 09:09:11 +0000331 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
Andy Greenc6bf2c22011-02-20 11:10:47 +0000332 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Andy Green6901cb32011-02-21 08:06:47 +0000333 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
Andy Green385e7ad2011-03-01 21:06:02 +0000334 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
Andy Greenc5114822011-03-06 10:29:35 +0000335 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
Andy Greenc6517fa2011-03-06 13:15:29 +0000336 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
Andy Greena7109e62013-02-11 12:05:54 +0800337 LWS_CALLBACK_PROTOCOL_INIT,
338 LWS_CALLBACK_PROTOCOL_DESTROY,
Andy Green76b6ea12014-02-15 19:25:50 +0800339 LWS_CALLBACK_WSI_CREATE, /* always protocol[0] */
340 LWS_CALLBACK_WSI_DESTROY, /* always protocol[0] */
Andy Green3b3fa9e2013-12-25 16:34:37 +0800341 LWS_CALLBACK_GET_THREAD_ID,
342
Andy Green3221f922011-02-12 13:14:11 +0000343 /* external poll() management support */
344 LWS_CALLBACK_ADD_POLL_FD,
345 LWS_CALLBACK_DEL_POLL_FD,
Michael Haberlerb3093052014-02-15 20:18:24 +0800346 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
Andy Green7a132792013-12-18 09:48:26 +0800347 LWS_CALLBACK_LOCK_POLL,
348 LWS_CALLBACK_UNLOCK_POLL,
Andy Green09f00582014-02-15 16:49:41 +0800349
Octav Zlatiorcf518962014-12-15 16:29:15 +0100350 LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
351
Andy Green09f00582014-02-15 16:49:41 +0800352 LWS_CALLBACK_USER = 1000, /* user code can use any including / above */
Andy Green5fd8a5e2010-10-31 11:57:17 +0000353};
354
Michael Haberlercb733062014-02-15 20:15:55 +0800355
Andy Green3b193862015-11-02 13:13:44 +0800356#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
357typedef SOCKET lws_sockfd_type;
Andy Green8c0d3c02015-11-02 20:34:12 +0800358#define lws_sockfd_valid(sfd) (!!sfd)
Andy Green4b85c1d2015-12-04 11:08:32 +0800359struct lws_pollfd {
Andy Green3b193862015-11-02 13:13:44 +0800360 lws_sockfd_type fd;
Patrick Gansterer73882e42014-03-29 08:25:58 +0100361 SHORT events;
362 SHORT revents;
363};
Andy Green4b85c1d2015-12-04 11:08:32 +0800364WINSOCK_API_LINKAGE int WSAAPI WSAPoll(struct lws_pollfd fdArray[], ULONG fds, INT timeout);
Patrick Gansterer73882e42014-03-29 08:25:58 +0100365#else
Andy Green3b193862015-11-02 13:13:44 +0800366
367#if defined(MBED_OPERATORS)
Andy Green11f27342015-11-08 12:10:26 +0800368/* it's a class lws_conn * */
Andy Green8c0d3c02015-11-02 20:34:12 +0800369typedef void * lws_sockfd_type;
370#define lws_sockfd_valid(sfd) (!!sfd)
Andy Green3b193862015-11-02 13:13:44 +0800371struct pollfd {
Andy Green8c0d3c02015-11-02 20:34:12 +0800372 lws_sockfd_type fd;
Andy Green3b193862015-11-02 13:13:44 +0800373 short events;
374 short revents;
375};
376#define POLLIN 0x0001
377#define POLLPRI 0x0002
378#define POLLOUT 0x0004
379#define POLLERR 0x0008
380#define POLLHUP 0x0010
381#define POLLNVAL 0x0020
Andy Green8c0d3c02015-11-02 20:34:12 +0800382
Andy Green4b85c1d2015-12-04 11:08:32 +0800383struct lws;
Andy Green8c0d3c02015-11-02 20:34:12 +0800384
385void * mbed3_create_tcp_stream_socket(void);
386void mbed3_delete_tcp_stream_socket(void *sockfd);
Andy Green4b85c1d2015-12-04 11:08:32 +0800387void mbed3_tcp_stream_bind(void *sock, int port, struct lws *);
388void mbed3_tcp_stream_accept(void *sock, struct lws *);
Andy Green3b193862015-11-02 13:13:44 +0800389#else
Andy Green11f27342015-11-08 12:10:26 +0800390typedef int lws_sockfd_type;
Andy Green8c0d3c02015-11-02 20:34:12 +0800391#define lws_sockfd_valid(sfd) (sfd >= 0)
Andy Green3b193862015-11-02 13:13:44 +0800392#endif
393
Andy Green4b85c1d2015-12-04 11:08:32 +0800394#define lws_pollfd pollfd
Patrick Gansterer73882e42014-03-29 08:25:58 +0100395#endif
Michael Haberlercb733062014-02-15 20:15:55 +0800396
Andy Green3b193862015-11-02 13:13:44 +0800397// argument structure for all external poll related calls
398// passed in via 'in'
Andy Green4b85c1d2015-12-04 11:08:32 +0800399struct lws_pollargs {
Andy Green3b193862015-11-02 13:13:44 +0800400 lws_sockfd_type fd; // applicable file descriptor
401 int events; // the new event mask
402 int prev_events; // the previous event mask
403};
404
Andy Green4b85c1d2015-12-04 11:08:32 +0800405enum lws_extension_callback_reasons {
Andy Greena41314f2011-05-23 10:00:03 +0100406 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,
Andy Greenc5114822011-03-06 10:29:35 +0000410 LWS_EXT_CALLBACK_CONSTRUCT,
Andy Green2366b1c2011-03-06 13:15:31 +0000411 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
Andy Green68b45042011-05-25 21:41:57 +0100412 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
Andy Green09226502011-05-28 10:19:19 +0100413 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
Andy Greenc5114822011-03-06 10:29:35 +0000414 LWS_EXT_CALLBACK_DESTROY,
Andy Greena41314f2011-05-23 10:00:03 +0100415 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
416 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED,
Andy Green98a717c2011-03-06 13:14:15 +0000417 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green3b84c002011-03-06 13:14:42 +0000418 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
Andy Greena41314f2011-05-23 10:00:03 +0100419 LWS_EXT_CALLBACK_PACKET_TX_DO_SEND,
420 LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
Andy Greenc44159f2011-03-07 07:08:18 +0000421 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
Andy Greena41314f2011-05-23 10:00:03 +0100422 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,
David Galeanoe2cf9922013-01-09 18:06:55 +0800427 LWS_EXT_CALLBACK_PAYLOAD_TX,
428 LWS_EXT_CALLBACK_PAYLOAD_RX,
Andy Greenc5114822011-03-06 10:29:35 +0000429};
430
Andy Green62304762015-12-04 08:43:54 +0800431enum lws_write_protocol {
Andy Green5fd8a5e2010-10-31 11:57:17 +0000432 LWS_WRITE_TEXT,
433 LWS_WRITE_BINARY,
Andy Green5d9d94b2011-03-07 20:47:41 +0000434 LWS_WRITE_CONTINUATION,
Andy Green38e57bb2011-01-19 12:20:27 +0000435 LWS_WRITE_HTTP,
436
Andy Green5e1fa172011-02-10 09:07:05 +0000437 /* special 04+ opcodes */
Andy Green38e57bb2011-01-19 12:20:27 +0000438
439 LWS_WRITE_CLOSE,
440 LWS_WRITE_PING,
Andy Green90c7cbc2011-01-27 06:26:52 +0000441 LWS_WRITE_PONG,
442
Andy Green200f3852014-10-18 12:23:05 +0800443 /* Same as write_http but we know this write ends the transaction */
444 LWS_WRITE_HTTP_FINAL,
445
Andy Green024eb6c2014-10-08 12:00:53 +0800446 /* HTTP2 */
447
448 LWS_WRITE_HTTP_HEADERS,
449
Andy Greenbd96d802011-01-30 08:24:31 +0000450 /* flags */
451
452 LWS_WRITE_NO_FIN = 0x40,
453 /*
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
457 */
458 LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
Andy Greenff95d7a2010-10-28 22:36:01 +0100459};
460
Andy Greenc85619d2011-02-13 08:25:26 +0000461/*
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.
466 */
467
468struct lws_tokens {
469 char *token;
470 int token_len;
471};
472
Andy Green1ee42a52014-02-23 14:38:59 +0800473/*
474 * don't forget to update test server header dump accordingly
475 *
476 * these have to be kept in sync with lextable.h / minilex.c
477 */
Andy Green9887bdb2014-09-30 08:23:06 +0800478
Andy Greenc85619d2011-02-13 08:25:26 +0000479enum lws_token_indexes {
480 WSI_TOKEN_GET_URI,
kapejodce64fb02013-11-19 13:38:16 +0100481 WSI_TOKEN_POST_URI,
Andrew Canadayda55fb52014-07-05 10:04:19 +0800482 WSI_TOKEN_OPTIONS_URI,
Andy Greenc85619d2011-02-13 08:25:26 +0000483 WSI_TOKEN_HOST,
484 WSI_TOKEN_CONNECTION,
Andy Greenc85619d2011-02-13 08:25:26 +0000485 WSI_TOKEN_UPGRADE,
486 WSI_TOKEN_ORIGIN,
487 WSI_TOKEN_DRAFT,
488 WSI_TOKEN_CHALLENGE,
Andy Greenc85619d2011-02-13 08:25:26 +0000489 WSI_TOKEN_EXTENSIONS,
Andy Green1ee42a52014-02-23 14:38:59 +0800490 WSI_TOKEN_KEY1,
491 WSI_TOKEN_KEY2,
492 WSI_TOKEN_PROTOCOL,
Andy Greenc85619d2011-02-13 08:25:26 +0000493 WSI_TOKEN_ACCEPT,
494 WSI_TOKEN_NONCE,
495 WSI_TOKEN_HTTP,
Andy Green1ee42a52014-02-23 14:38:59 +0800496 WSI_TOKEN_HTTP2_SETTINGS,
Andy Greencc13c6f2013-11-09 10:09:09 +0800497 WSI_TOKEN_HTTP_ACCEPT,
Andrew Canaday42203272014-06-28 20:19:57 -0400498 WSI_TOKEN_HTTP_AC_REQUEST_HEADERS,
Andy Greencc13c6f2013-11-09 10:09:09 +0800499 WSI_TOKEN_HTTP_IF_MODIFIED_SINCE,
Andrew Canaday42203272014-06-28 20:19:57 -0400500 WSI_TOKEN_HTTP_IF_NONE_MATCH,
Andy Greencc13c6f2013-11-09 10:09:09 +0800501 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,
kapejodce64fb02013-11-19 13:38:16 +0100507 WSI_TOKEN_HTTP_CONTENT_LENGTH,
Andy Greencc13c6f2013-11-09 10:09:09 +0800508 WSI_TOKEN_HTTP_CONTENT_TYPE,
509 WSI_TOKEN_HTTP_DATE,
510 WSI_TOKEN_HTTP_RANGE,
511 WSI_TOKEN_HTTP_REFERER,
Andy Green1ee42a52014-02-23 14:38:59 +0800512 WSI_TOKEN_KEY,
513 WSI_TOKEN_VERSION,
514 WSI_TOKEN_SWORIGIN,
Andy Greencc13c6f2013-11-09 10:09:09 +0800515
Andy Greenecc2e722014-10-09 16:57:47 +0800516 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,
521
522 WSI_TOKEN_HTTP_ACCEPT_CHARSET,
523 WSI_TOKEN_HTTP_ACCEPT_RANGES,
524 WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN,
525 WSI_TOKEN_HTTP_AGE,
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,
532 WSI_TOKEN_HTTP_ETAG,
533 WSI_TOKEN_HTTP_EXPECT,
534 WSI_TOKEN_HTTP_EXPIRES,
535 WSI_TOKEN_HTTP_FROM,
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,
540 WSI_TOKEN_HTTP_LINK,
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,
552 WSI_TOKEN_HTTP_VARY,
553 WSI_TOKEN_HTTP_VIA,
554 WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=fdde0f02014-12-14 12:13:43 +0800555 WSI_TOKEN_PROXY,
Andy Green917f43a2014-10-12 14:31:47 +0800556
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800557 WSI_TOKEN_PATCH_URI,
558 WSI_TOKEN_PUT_URI,
559 WSI_TOKEN_DELETE_URI,
560
Andy Green917f43a2014-10-12 14:31:47 +0800561 WSI_TOKEN_HTTP_URI_ARGS,
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800562
Andy Green917f43a2014-10-12 14:31:47 +0800563 /* use token storage to stash these */
564
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,
Andy Greenecc2e722014-10-09 16:57:47 +0800570
Andy Greenc85619d2011-02-13 08:25:26 +0000571 /* always last real token index*/
572 WSI_TOKEN_COUNT,
573 /* parser state additions */
574 WSI_TOKEN_NAME_PART,
575 WSI_TOKEN_SKIPPING,
576 WSI_TOKEN_SKIPPING_SAW_CR,
Andy Greena41314f2011-05-23 10:00:03 +0100577 WSI_PARSING_COMPLETE,
578 WSI_INIT_TOKEN_MUXURL,
Andy Greenc85619d2011-02-13 08:25:26 +0000579};
580
Andrew Canaday74b4a652014-06-29 00:25:19 -0400581struct lws_token_limits {
582 unsigned short token_limit[WSI_TOKEN_COUNT];
583};
584
Andy Green687b0182011-02-26 11:04:01 +0000585/*
David Galeano2915a802013-01-09 15:26:08 +0800586 * From RFC 6455
Andy Green687b0182011-02-26 11:04:01 +0000587 1000
588
David Galeano2915a802013-01-09 15:26:08 +0800589 1000 indicates a normal closure, meaning that the purpose for
590 which the connection was established has been fulfilled.
Andy Green687b0182011-02-26 11:04:01 +0000591
592 1001
593
594 1001 indicates that an endpoint is "going away", such as a server
David Galeano2915a802013-01-09 15:26:08 +0800595 going down or a browser having navigated away from a page.
Andy Green687b0182011-02-26 11:04:01 +0000596
597 1002
598
599 1002 indicates that an endpoint is terminating the connection due
600 to a protocol error.
601
602 1003
603
604 1003 indicates that an endpoint is terminating the connection
David Galeano2915a802013-01-09 15:26:08 +0800605 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).
Andy Green687b0182011-02-26 11:04:01 +0000608
609 1004
610
David Galeano2915a802013-01-09 15:26:08 +0800611 Reserved. The specific meaning might be defined in the future.
612
613 1005
614
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.
619
620 1006
621
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.
627
628 1007
629
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).
634
635 1008
636
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.
642
643 1009
644
645 1009 indicates that an endpoint is terminating the connection
646 because it has received a message that is too big for it to
647 process.
648
649 1010
650
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.
658
659 1011
660
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.
664
665 1015
666
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).
Andy Green687b0182011-02-26 11:04:01 +0000672*/
673
674enum 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,
David Galeano2915a802013-01-09 15:26:08 +0800680 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,
Andy Greenb5b23192013-02-11 17:13:32 +0800687 LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
688 LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
Andy Green8ec78022015-03-25 01:53:58 +0800689
690 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
Andy Green687b0182011-02-26 11:04:01 +0000691};
692
Andy Green4e7a1332013-11-11 07:30:33 +0800693enum http_status {
kapejodce64fb02013-11-19 13:38:16 +0100694 HTTP_STATUS_OK = 200,
Andrew Canadayb57e7a02014-06-29 19:14:43 -0400695 HTTP_STATUS_NO_CONTENT = 204,
kapejodce64fb02013-11-19 13:38:16 +0100696
Andy Green4e7a1332013-11-11 07:30:33 +0800697 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,
707 HTTP_STATUS_GONE,
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,
715
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,
722};
723
Andy Green4b85c1d2015-12-04 11:08:32 +0800724struct lws;
725struct lws_context;
Andy Green3182ece2013-01-20 17:08:31 +0800726/* needed even with extensions disabled for create context */
Andy Green4b85c1d2015-12-04 11:08:32 +0800727struct lws_extension;
Andy Greenff95d7a2010-10-28 22:36:01 +0100728
Andy Green8f037e42010-12-19 22:13:26 +0000729/**
David Brooks6c6a3d32012-04-20 12:13:50 +0800730 * callback_function() - User server actions
Andy Green32375b72011-02-19 08:32:53 +0000731 * @context: Websockets context
Andy Green8f037e42010-12-19 22:13:26 +0000732 * @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
737 *
738 * This callback is the way the user controls what is served. All the
739 * protocol detail is hidden and handled by the library.
Andy Green6964bb52011-01-23 16:50:33 +0000740 *
Andy Green8f037e42010-12-19 22:13:26 +0000741 * 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
Andy Green4b85c1d2015-12-04 11:08:32 +0800743 * the library is initialized with lws_create_server.
Andy Green6964bb52011-01-23 16:50:33 +0000744 *
Andy Green8f037e42010-12-19 22:13:26 +0000745 * You get an opportunity to initialize user data when called back with
746 * LWS_CALLBACK_ESTABLISHED reason.
747 *
Andy Green2b57a342013-02-06 15:15:25 +0900748 * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with
Andy Green90c7cbc2011-01-27 06:26:52 +0000749 * an incoming client
750 *
David Brooks80a44972012-04-20 12:18:47 +0800751 * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
MGadkari020c53c2015-08-21 16:15:36 +0530752 * 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
754 * it points to.
David Brooks80a44972012-04-20 12:18:47 +0800755 *
Andy Green2b57a342013-02-06 15:15:25 +0900756 * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
Andy Greenb5b23192013-02-11 17:13:32 +0800757 * 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
Andy Green2b57a342013-02-06 15:15:25 +0900763 *
David Brooks80a44972012-04-20 12:18:47 +0800764 * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
Andy Green90c7cbc2011-01-27 06:26:52 +0000765 * a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000766 *
767 * LWS_CALLBACK_CLOSED: when the websocket session ends
768 *
Andy Green0c9563b2013-06-10 22:54:40 +0800769 * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends
770 *
Andy Green90c7cbc2011-01-27 06:26:52 +0000771 * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
772 * remote client, it can be found at *in and is
773 * len bytes long
774 *
Andy Greena6cbece2011-01-27 20:06:03 +0000775 * 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
778 *
Andy Green90c7cbc2011-01-27 06:26:52 +0000779 * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
780 * client connection, it can be found at *in and
781 * is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000782 *
Andy Green6964bb52011-01-23 16:50:33 +0000783 * LWS_CALLBACK_HTTP: an http request has come from a client that is not
Andy Green8f037e42010-12-19 22:13:26 +0000784 * 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.
Andy Green6964bb52011-01-23 16:50:33 +0000788 * @in points to the URI path requested and
Andy Green62304762015-12-04 08:43:54 +0800789 * lws_serve_http_file() makes it very
Andy Green8f037e42010-12-19 22:13:26 +0000790 * simple to send back a file to the client.
Andy Green24b588b2013-01-13 09:53:18 +0800791 * 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
798 * by MAX_CLIENTS.
Andy Green90c7cbc2011-01-27 06:26:52 +0000799 *
kapejodce64fb02013-11-19 13:38:16 +0100800 * LWS_CALLBACK_HTTP_BODY: the next @len bytes data from the http
801 * request body HTTP connection is now available in @in.
802 *
803 * LWS_CALLBACK_HTTP_BODY_COMPLETION: the expected amount of http request
804 * body has been delivered
805 *
Andy Green54cb3462013-02-14 22:23:54 +0800806 * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol
807 * link now.
808 *
Andy Greend280b6e2013-01-15 13:40:23 +0800809 * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down
810 * http link has completed.
811 *
Andy Green9e4c2b62011-03-07 20:47:39 +0000812 * LWS_CALLBACK_CLIENT_WRITEABLE:
813 * LWS_CALLBACK_SERVER_WRITEABLE: If you call
Andy Green62304762015-12-04 08:43:54 +0800814 * lws_callback_on_writable() on a connection, you will
Andy Green9e4c2b62011-03-07 20:47:39 +0000815 * 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.
Andy Greene7981dc2011-02-12 21:24:03 +0000821 *
Andy Green07034092011-02-13 08:37:12 +0000822 * 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
Edwin van den Oetelaar8c8a8e12013-02-20 20:56:59 +0800825 * or not, based on the client IP. @in contains the connection
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200826 * 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.
Andy Green19895bc2013-11-09 11:59:56 +0800832 *
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200833 * 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.
840 *
Andy Green19895bc2013-11-09 11:59:56 +0800841 * 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.
Andy Green07034092011-02-13 08:37:12 +0000852 *
Andy Greenb5b23192013-02-11 17:13:32 +0800853 * 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.
Andy Green96d48fd2013-09-18 08:32:55 +0800856 * @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.
Andy Greenc85619d2011-02-13 08:25:26 +0000864 *
Andy Greenb5b23192013-02-11 17:13:32 +0800865 * 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
Andy Green0894bda2011-02-19 09:09:11 +0000868 * calls to direct OpenSSL where to find certificates the client
869 * can use to confirm the remote server identity. @user is the
870 * OpenSSL SSL_CTX*
Andy Greenc85619d2011-02-13 08:25:26 +0000871 *
Andy Greenc6bf2c22011-02-20 11:10:47 +0000872 * 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*
877 *
Octav Zlatiorcf518962014-12-15 16:29:15 +0100878 * 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*
886 *
Andy Green6901cb32011-02-21 08:06:47 +0000887 * 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
904 * certificates.
905 *
Andy Green385e7ad2011-03-01 21:06:02 +0000906 * 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:
913 *
914 * char **p = (char **)in;
915 *
Andy Greenb5b23192013-02-11 17:13:32 +0800916 * if (len < 100)
917 * return 1;
Andy Green385e7ad2011-03-01 21:06:02 +0000918 *
919 * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
920 *
921 * return 0;
922 *
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.
926 *
Andy Greenb5b23192013-02-11 17:13:32 +0800927 * Notice the callback is coming to protocols[0] all the time,
Andy Green385e7ad2011-03-01 21:06:02 +0000928 * because there is no specific protocol handshook yet.
929 *
Andy Greenb5b23192013-02-11 17:13:32 +0800930 * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
Andy Greenc5114822011-03-06 10:29:35 +0000931 * 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].
940 *
Andy Greenc6517fa2011-03-06 13:15:29 +0000941 * 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].
949 *
Andy Greena7109e62013-02-11 12:05:54 +0800950 * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can
951 * do initial setup / allocations etc
952 *
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.
957 *
Andy Green76b6ea12014-02-15 19:25:50 +0800958 * LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification
959 *
960 * LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification
961 *
Michael Haberler8284cf12014-02-15 20:33:46 +0800962 * The next five reasons are optional and only need taking care of if you
Andy Greenb5b23192013-02-11 17:13:32 +0800963 * will be integrating libwebsockets sockets into an external polling
964 * array.
965 *
Andy Green4b85c1d2015-12-04 11:08:32 +0800966 * For these calls, @in points to a struct lws_pollargs that
Michael Haberler8284cf12014-02-15 20:33:46 +0800967 * contains @fd, @events and @prev_events members
968 *
Andy Greenb5b23192013-02-11 17:13:32 +0800969 * 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
Michael Haberler8284cf12014-02-15 20:33:46 +0800976 * serving case.
977 * This callback happens when a socket needs to be
978 * added to the polling loop: @in points to a struct
Andy Green4b85c1d2015-12-04 11:08:32 +0800979 * lws_pollargs; the @fd member of the struct is the file
Michael Haberler8284cf12014-02-15 20:33:46 +0800980 * descriptor, and @events contains the active events.
981 *
982 * If you are using the internal polling loop (the "service"
983 * callback), you can just ignore these callbacks.
Andy Greene7981dc2011-02-12 21:24:03 +0000984 *
Andy Greenb5b23192013-02-11 17:13:32 +0800985 * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
Andy Green50097dd2013-02-15 22:36:30 +0800986 * needs to be removed from an external polling array. @in is
Andy Green4b85c1d2015-12-04 11:08:32 +0800987 * again the struct lws_pollargs containing the @fd member
Michael Haberler8284cf12014-02-15 20:33:46 +0800988 * to be removed. If you are using the internal polling
Andy Greenb5b23192013-02-11 17:13:32 +0800989 * loop, you can just ignore it.
Andy Greene7981dc2011-02-12 21:24:03 +0000990 *
Michael Haberler8284cf12014-02-15 20:33:46 +0800991 * LWS_CALLBACK_CHANGE_MODE_POLL_FD: This callback happens when
992 * libwebsockets wants to modify the events for a connectiion.
Andy Green4b85c1d2015-12-04 11:08:32 +0800993 * @in is the struct lws_pollargs with the @fd to change.
Michael Haberler8284cf12014-02-15 20:33:46 +0800994 * 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
997 * it.
Andy Greene7981dc2011-02-12 21:24:03 +0000998 *
Michael Haberler8284cf12014-02-15 20:33:46 +0800999 * 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.
Andy Green5a3b1d32015-11-20 09:51:18 +08001003 * 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.
Andy Green8f037e42010-12-19 22:13:26 +00001011 */
Andy Green4b85c1d2015-12-04 11:08:32 +08001012LWS_VISIBLE LWS_EXTERN int callback(struct lws_context *context,
1013 struct lws *wsi,
1014 enum lws_callback_reasons reason, void *user,
Andy Green8f037e42010-12-19 22:13:26 +00001015 void *in, size_t len);
1016
Andy Green4b85c1d2015-12-04 11:08:32 +08001017typedef int (callback_function)(struct lws_context *context,
1018 struct lws *wsi,
1019 enum lws_callback_reasons reason, void *user,
David Brooks6c6a3d32012-04-20 12:13:50 +08001020 void *in, size_t len);
1021
Andy Green3182ece2013-01-20 17:08:31 +08001022#ifndef LWS_NO_EXTENSIONS
Andy Green57b4e9a2011-03-06 13:14:46 +00001023/**
David Brooks6c6a3d32012-04-20 12:13:50 +08001024 * extension_callback_function() - Hooks to allow extensions to operate
Andy Green57b4e9a2011-03-06 13:14:46 +00001025 * @context: Websockets context
Andy Green46c2ea02011-03-22 09:04:01 +00001026 * @ext: This extension
Andy Green57b4e9a2011-03-06 13:14:46 +00001027 * @wsi: Opaque websocket instance pointer
1028 * @reason: The reason for the call
1029 * @user: Pointer to per-session user data allocated by library
1030 * @in: Pointer used for some callback reasons
1031 * @len: Length set for some callback reasons
1032 *
1033 * Each extension that is active on a particular connection receives
1034 * callbacks during the connection lifetime to allow the extension to
1035 * operate on websocket data and manage itself.
1036 *
1037 * Libwebsockets takes care of allocating and freeing "user" memory for
1038 * each active extension on each connection. That is what is pointed to
1039 * by the @user parameter.
1040 *
1041 * LWS_EXT_CALLBACK_CONSTRUCT: called when the server has decided to
1042 * select this extension from the list provided by the client,
1043 * just before the server will send back the handshake accepting
1044 * the connection with this extension active. This gives the
1045 * extension a chance to initialize its connection context found
1046 * in @user.
1047 *
Andy Greenb5b23192013-02-11 17:13:32 +08001048 * LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT
Andy Green2366b1c2011-03-06 13:15:31 +00001049 * but called when client is instantiating this extension. Some
1050 * extensions will work the same on client and server side and then
1051 * you can just merge handlers for both CONSTRUCTS.
1052 *
Andy Green57b4e9a2011-03-06 13:14:46 +00001053 * LWS_EXT_CALLBACK_DESTROY: called when the connection the extension was
1054 * being used on is about to be closed and deallocated. It's the
1055 * last chance for the extension to deallocate anything it has
1056 * allocated in the user data (pointed to by @user) before the
Andy Green2366b1c2011-03-06 13:15:31 +00001057 * user data is deleted. This same callback is used whether you
1058 * are in client or server instantiation context.
Andy Green57b4e9a2011-03-06 13:14:46 +00001059 *
1060 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on
1061 * a connection, and a packet of data arrived at the connection,
1062 * it is passed to this callback to give the extension a chance to
1063 * change the data, eg, decompress it. @user is pointing to the
1064 * extension's private connection context data, @in is pointing
1065 * to an lws_tokens struct, it consists of a char * pointer called
1066 * token, and an int called token_len. At entry, these are
1067 * set to point to the received buffer and set to the content
1068 * length. If the extension will grow the content, it should use
1069 * a new buffer allocated in its private user context data and
1070 * set the pointed-to lws_tokens members to point to its buffer.
1071 *
1072 * LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as
1073 * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
1074 * extension a chance to change websocket data just before it will
1075 * be sent out. Using the same lws_token pointer scheme in @in,
1076 * the extension can change the buffer and the length to be
1077 * transmitted how it likes. Again if it wants to grow the
1078 * buffer safely, it should copy the data into its own buffer and
1079 * set the lws_tokens token pointer to it.
1080 */
Andy Green4b85c1d2015-12-04 11:08:32 +08001081LWS_VISIBLE LWS_EXTERN int extension_callback(struct lws_context *context,
1082 struct lws_extension *ext,
1083 struct lws *wsi,
1084 enum lws_extension_callback_reasons reason,
Andy Greenb5b23192013-02-11 17:13:32 +08001085 void *user, void *in, size_t len);
Andy Green57b4e9a2011-03-06 13:14:46 +00001086
Andy Green4b85c1d2015-12-04 11:08:32 +08001087typedef int (extension_callback_function)(struct lws_context *context,
1088 struct lws_extension *ext,
1089 struct lws *wsi,
1090 enum lws_extension_callback_reasons reason,
Andy Greenb5b23192013-02-11 17:13:32 +08001091 void *user, void *in, size_t len);
Andy Green3182ece2013-01-20 17:08:31 +08001092#endif
Andy Green57b4e9a2011-03-06 13:14:46 +00001093
Andy Green4f3943a2010-11-12 10:44:16 +00001094/**
Andy Green4b85c1d2015-12-04 11:08:32 +08001095 * struct lws_protocols - List of protocols and handlers server
Andy Green6964bb52011-01-23 16:50:33 +00001096 * supports.
Andy Green4f3943a2010-11-12 10:44:16 +00001097 * @name: Protocol name that must match the one given in the client
Andy Greena40760a2014-08-07 16:52:15 +08001098 * Javascript new WebSocket(url, 'protocol') name.
Andy Green4f3943a2010-11-12 10:44:16 +00001099 * @callback: The service callback used for this protocol. It allows the
Andy Green6964bb52011-01-23 16:50:33 +00001100 * service action for an entire protocol to be encapsulated in
1101 * the protocol-specific callback
Andy Green4f3943a2010-11-12 10:44:16 +00001102 * @per_session_data_size: Each new connection using this protocol gets
Andy Green6964bb52011-01-23 16:50:33 +00001103 * this much memory allocated on connection establishment and
1104 * freed on connection takedown. A pointer to this per-connection
1105 * allocation is passed into the callback in the 'user' parameter
Andy Green54495112013-02-06 21:10:16 +09001106 * @rx_buffer_size: if you want atomic frames delivered to the callback, you
Andy Greenb5b23192013-02-11 17:13:32 +08001107 * should set this to the size of the biggest legal frame that
1108 * you support. If the frame size is exceeded, there is no
1109 * error, but the buffer will spill to the user callback when
1110 * full, which you can detect by using
Andy Green62304762015-12-04 08:43:54 +08001111 * lws_remaining_packet_payload(). Notice that you
Andy Greenb5b23192013-02-11 17:13:32 +08001112 * just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING
1113 * and post-padding are automatically also allocated on top.
Andy Greeneabed8d2014-08-11 12:11:36 +08001114 * @id: ignored by lws, but useful to contain user information bound
1115 * to the selected protocol. For example if this protocol was
1116 * called "myprotocol-v2", you might set id to 2, and the user
1117 * code that acts differently according to the version can do so by
1118 * switch (wsi->protocol->id), user code might use some bits as
1119 * capability flags based on selected protocol version, etc.
Neal Hormanb3992372014-12-10 18:59:04 -06001120 * @user: User provided context data at the protocol level.
Andy Green62304762015-12-04 08:43:54 +08001121 * Accessible via lws_get_protocol(wsi)->user
Neal Hormanb3992372014-12-10 18:59:04 -06001122 * This should not be confused with wsi->user, it is not the same.
1123 * The library completely ignores any value in here.
Andy Greenb45993c2010-12-18 15:13:50 +00001124 * @owning_server: the server init call fills in this opaque pointer when
Andy Green6964bb52011-01-23 16:50:33 +00001125 * registering this protocol with the server.
Andy Greenb45993c2010-12-18 15:13:50 +00001126 * @protocol_index: which protocol we are starting from zero
Andy Greene77ddd82010-11-13 10:03:47 +00001127 *
Andy Green6964bb52011-01-23 16:50:33 +00001128 * This structure represents one protocol supported by the server. An
Andy Green4b85c1d2015-12-04 11:08:32 +08001129 * array of these structures is passed to lws_create_server()
Andy Green6964bb52011-01-23 16:50:33 +00001130 * allows as many protocols as you like to be handled by one server.
Andy Greena40760a2014-08-07 16:52:15 +08001131 *
1132 * The first protocol given has its callback used for user callbacks when
1133 * there is no agreed protocol name, that's true during HTTP part of the
1134 * connection and true if the client did not send a Protocol: header.
Andy Green4f3943a2010-11-12 10:44:16 +00001135 */
1136
Andy Green4b85c1d2015-12-04 11:08:32 +08001137struct lws_protocols {
Andy Green4f3943a2010-11-12 10:44:16 +00001138 const char *name;
David Brooks6c6a3d32012-04-20 12:13:50 +08001139 callback_function *callback;
Andy Green4f3943a2010-11-12 10:44:16 +00001140 size_t per_session_data_size;
Andy Green54495112013-02-06 21:10:16 +09001141 size_t rx_buffer_size;
Andy Greeneabed8d2014-08-11 12:11:36 +08001142 unsigned int id;
Neal Hormanb3992372014-12-10 18:59:04 -06001143 void *user;
Andy Greenb45993c2010-12-18 15:13:50 +00001144
1145 /*
1146 * below are filled in on server init and can be left uninitialized,
1147 * no need for user to use them directly either
1148 */
Andy Green6964bb52011-01-23 16:50:33 +00001149
Andy Green4b85c1d2015-12-04 11:08:32 +08001150 struct lws_context *owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +00001151 int protocol_index;
Andy Green4f3943a2010-11-12 10:44:16 +00001152};
1153
Andy Green3182ece2013-01-20 17:08:31 +08001154#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001155/**
Andy Green4b85c1d2015-12-04 11:08:32 +08001156 * struct lws_extension - An extension we know how to cope with
Andy Greend6e09112011-03-05 16:12:15 +00001157 *
1158 * @name: Formal extension name, eg, "deflate-stream"
1159 * @callback: Service callback
Andy Greenb5b23192013-02-11 17:13:32 +08001160 * @per_session_data_size: Libwebsockets will auto-malloc this much
1161 * memory for the use of the extension, a pointer
Andy Greend6e09112011-03-05 16:12:15 +00001162 * to it comes in the @user callback parameter
Peter Pentchevc74964e2013-02-07 16:17:13 +02001163 * @per_context_private_data: Optional storage for this extension that
Andy Greenb5b23192013-02-11 17:13:32 +08001164 * is per-context, so it can track stuff across
1165 * all sessions, etc, if it wants
Andy Greend6e09112011-03-05 16:12:15 +00001166 */
1167
Andy Green4b85c1d2015-12-04 11:08:32 +08001168struct lws_extension {
Andy Greend6e09112011-03-05 16:12:15 +00001169 const char *name;
David Brooks6c6a3d32012-04-20 12:13:50 +08001170 extension_callback_function *callback;
Andy Greend6e09112011-03-05 16:12:15 +00001171 size_t per_session_data_size;
Andy Greenb5b23192013-02-11 17:13:32 +08001172 void *per_context_private_data;
Andy Greend6e09112011-03-05 16:12:15 +00001173};
Andy Green3182ece2013-01-20 17:08:31 +08001174#endif
Andy Greend6e09112011-03-05 16:12:15 +00001175
Andy Green1b265272013-02-09 14:01:09 +08001176/**
1177 * struct lws_context_creation_info: parameters to create context with
1178 *
Andy Green67f94592014-07-31 09:44:00 +08001179 * @port: Port to listen on... you can use CONTEXT_PORT_NO_LISTEN to
1180 * suppress listening on any port, that's what you want if you are
1181 * not running a websocket server at all but just using it as a
1182 * client
Andy Green7a506ba2013-02-12 10:13:02 +08001183 * @iface: NULL to bind the listen socket to all interfaces, or the
Andy Green1b265272013-02-09 14:01:09 +08001184 * interface name, eg, "eth2"
1185 * @protocols: Array of structures listing supported protocols and a protocol-
1186 * specific callback for each one. The list is ended with an
1187 * entry that has a NULL callback pointer.
1188 * It's not const because we write the owning_server member
Andy Green4b85c1d2015-12-04 11:08:32 +08001189 * @extensions: NULL or array of lws_extension structs listing the
Andy Green1b265272013-02-09 14:01:09 +08001190 * extensions this context supports. If you configured with
1191 * --without-extensions, you should give NULL here.
Andrew Canaday3d2ebf32014-06-29 00:30:45 -04001192 * @token_limits: NULL or struct lws_token_limits pointer which is initialized
1193 * with a token length limit for each possible WSI_TOKEN_***
Andy Green1b265272013-02-09 14:01:09 +08001194 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
1195 * to listen using SSL, set to the filepath to fetch the
1196 * server cert from, otherwise NULL for unencrypted
Octav Zlatiorcf518962014-12-15 16:29:15 +01001197 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode;
1198 * if this is set to NULL but sll_cert_filepath is set, the
1199 * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called to allow
1200 * setting of the private key directly via openSSL library calls
Andy Green1b265272013-02-09 14:01:09 +08001201 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green2672fb22013-02-22 09:54:35 +08001202 * @ssl_cipher_list: List of valid ciphers to use (eg,
1203 * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
1204 * or you can leave it as NULL to get "DEFAULT"
Andy Green6e405562015-11-08 10:15:01 +08001205 * @http_proxy_address: If non-NULL, attempts to proxy via the given address.
1206 * If proxy auth is required, use format
1207 * "username:password@server:port"
Andy Greenec86b4e2015-03-11 08:35:15 +08001208 * @http_proxy_port: If http_proxy_address was non-NULL, uses this port at the address
Andy Green1b265272013-02-09 14:01:09 +08001209 * @gid: group id to change to after setting listen socket, or -1.
1210 * @uid: user id to change to after setting listen socket, or -1.
1211 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
1212 * @user: optional user pointer that can be recovered via the context
Andy Green62304762015-12-04 08:43:54 +08001213 * pointer using lws_context_user
Andy Greena690cd02013-02-09 12:25:31 +08001214 * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to
1215 * all libwebsocket sockets, client or server
1216 * @ka_probes: if ka_time was nonzero, after the timeout expires how many
1217 * times to try to get a response from the peer before giving up
1218 * and killing the connection
1219 * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes
1220 * attempt
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +08001221 * @provided_client_ssl_ctx: If non-null, swap out libwebsockets ssl
1222 * implementation for the one provided by provided_ssl_ctx.
1223 * Libwebsockets no longer is responsible for freeing the context
1224 * if this option is selected.
Andy Green1b265272013-02-09 14:01:09 +08001225 */
1226
1227struct lws_context_creation_info {
1228 int port;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01001229 const char *iface;
Andy Green4b85c1d2015-12-04 11:08:32 +08001230 struct lws_protocols *protocols;
1231 struct lws_extension *extensions;
Olehfaeac3c2014-07-29 23:18:41 +08001232 struct lws_token_limits *token_limits;
1233 const char *ssl_private_key_password;
Andy Green1b265272013-02-09 14:01:09 +08001234 const char *ssl_cert_filepath;
1235 const char *ssl_private_key_filepath;
1236 const char *ssl_ca_filepath;
Andy Green2672fb22013-02-22 09:54:35 +08001237 const char *ssl_cipher_list;
Patrick Gansterer148b9452014-02-28 02:31:23 +01001238 const char *http_proxy_address;
1239 unsigned int http_proxy_port;
Andy Green1b265272013-02-09 14:01:09 +08001240 int gid;
1241 int uid;
1242 unsigned int options;
1243 void *user;
Andy Greena690cd02013-02-09 12:25:31 +08001244 int ka_time;
1245 int ka_probes;
1246 int ka_interval;
joseph.urciuoli4d9c8fc2014-10-16 08:53:19 +08001247#ifdef LWS_OPENSSL_SUPPORT
1248 SSL_CTX *provided_client_ssl_ctx;
1249#else /* maintain structure layout either way */
1250 void *provided_client_ssl_ctx;
1251#endif
Andy Green1b265272013-02-09 14:01:09 +08001252};
1253
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001254LWS_VISIBLE LWS_EXTERN
Andy Greenb5b23192013-02-11 17:13:32 +08001255void lws_set_log_level(int level,
1256 void (*log_emit_function)(int level, const char *line));
Andy Greend6e09112011-03-05 16:12:15 +00001257
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001258LWS_VISIBLE LWS_EXTERN void
Andy Greenc11db202013-01-19 11:12:16 +08001259lwsl_emit_syslog(int level, const char *line);
1260
Andy Green4b85c1d2015-12-04 11:08:32 +08001261LWS_VISIBLE LWS_EXTERN struct lws_context *
Andy Green29a44cf2015-12-04 07:55:17 +08001262lws_create_context(struct lws_context_creation_info *info);
shysb4e800e2013-10-24 22:12:03 +08001263
1264LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001265lws_set_proxy(struct lws_context *context, const char *proxy);
Andy Greenff95d7a2010-10-28 22:36:01 +01001266
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001267LWS_VISIBLE LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001268lws_context_destroy(struct lws_context *context);
Andy Green6964bb52011-01-23 16:50:33 +00001269
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001270LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001271lws_service(struct lws_context *context, int timeout_ms);
Andy Greene92cd172011-01-19 13:11:55 +00001272
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001273LWS_VISIBLE LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001274lws_cancel_service(struct lws_context *context);
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001275
Andy Green917f43a2014-10-12 14:31:47 +08001276LWS_VISIBLE LWS_EXTERN const unsigned char *
1277lws_token_to_string(enum lws_token_indexes token);
1278
1279LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001280lws_add_http_header_by_name(struct lws_context *context,
1281 struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001282 const unsigned char *name,
1283 const unsigned char *value,
1284 int length,
1285 unsigned char **p,
1286 unsigned char *end);
1287LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001288lws_finalize_http_header(struct lws_context *context,
1289 struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001290 unsigned char **p,
1291 unsigned char *end);
1292LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001293lws_add_http_header_by_token(struct lws_context *context,
1294 struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001295 enum lws_token_indexes token,
1296 const unsigned char *value,
1297 int length,
1298 unsigned char **p,
1299 unsigned char *end);
Andy Green29a44cf2015-12-04 07:55:17 +08001300LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001301lws_add_http_header_content_length(struct lws_context *context,
1302 struct lws *wsi,
Andy Green200f3852014-10-18 12:23:05 +08001303 unsigned long content_length,
1304 unsigned char **p,
1305 unsigned char *end);
Andy Green917f43a2014-10-12 14:31:47 +08001306LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001307lws_add_http_header_status(struct lws_context *context,
1308 struct lws *wsi,
Andy Green917f43a2014-10-12 14:31:47 +08001309 unsigned int code,
1310 unsigned char **p,
1311 unsigned char *end);
1312
Andy Green4b85c1d2015-12-04 11:08:32 +08001313LWS_EXTERN int lws_http_transaction_completed(struct lws *wsi);
Andy Green91b05892014-10-17 08:38:44 +08001314
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001315#ifdef LWS_USE_LIBEV
Andrew Canadaya8f47c92015-04-26 22:50:59 -04001316typedef void (lws_ev_signal_cb)(EV_P_ struct ev_signal *w, int revents);
1317
1318LWS_VISIBLE LWS_EXTERN int
Andy Green29a44cf2015-12-04 07:55:17 +08001319lws_sigint_cfg(
Andy Green4b85c1d2015-12-04 11:08:32 +08001320 struct lws_context *context,
Andrew Canadaya8f47c92015-04-26 22:50:59 -04001321 int use_ev_sigint,
1322 lws_ev_signal_cb* cb);
1323
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001324LWS_VISIBLE LWS_EXTERN int
Andy Green29a44cf2015-12-04 07:55:17 +08001325lws_initloop(
Andy Green4b85c1d2015-12-04 11:08:32 +08001326 struct lws_context *context, struct ev_loop *loop);
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001327
1328LWS_VISIBLE void
Andy Green29a44cf2015-12-04 07:55:17 +08001329lws_sigint_cb(
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001330 struct ev_loop *loop, struct ev_signal *watcher, int revents);
1331#endif /* LWS_USE_LIBEV */
1332
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001333LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001334lws_service_fd(struct lws_context *context,
1335 struct lws_pollfd *pollfd);
Andy Green9f990342011-02-12 11:57:45 +00001336
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001337LWS_VISIBLE LWS_EXTERN void *
Andy Green4b85c1d2015-12-04 11:08:32 +08001338lws_context_user(struct lws_context *context);
Alon Levy0291eb32012-10-19 11:21:56 +02001339
Andy Green9beedc12013-09-18 08:47:15 +08001340enum pending_timeout {
1341 NO_PENDING_TIMEOUT = 0,
1342 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
Andy Green5dc62ea2013-09-20 20:26:12 +08001343 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE,
Andy Green9beedc12013-09-18 08:47:15 +08001344 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
1345 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
1346 PENDING_TIMEOUT_AWAITING_PING,
1347 PENDING_TIMEOUT_CLOSE_ACK,
1348 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
1349 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
1350 PENDING_TIMEOUT_SSL_ACCEPT,
kapejodce64fb02013-11-19 13:38:16 +01001351 PENDING_TIMEOUT_HTTP_CONTENT,
Andy Green02138122014-04-06 06:26:35 +01001352 PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
Andy Green9f02c5d2015-04-01 05:34:18 +08001353 PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE,
Andy Green9beedc12013-09-18 08:47:15 +08001354};
1355
Patrick Gansterer2ff69552014-02-26 18:51:05 +01001356LWS_VISIBLE LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001357lws_set_timeout(struct lws *wsi,
Andy Green9beedc12013-09-18 08:47:15 +08001358 enum pending_timeout reason, int secs);
1359
Andy Green4ea60062010-10-30 12:15:07 +01001360/*
1361 * IMPORTANT NOTICE!
Andy Greene77ddd82010-11-13 10:03:47 +00001362 *
Andy Green5fd8a5e2010-10-31 11:57:17 +00001363 * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
1364 * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
Andy Green4ea60062010-10-30 12:15:07 +01001365 * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
Andy Greene77ddd82010-11-13 10:03:47 +00001366 *
Andy Green4ea60062010-10-30 12:15:07 +01001367 * This allows us to add protocol info before and after the data, and send as
1368 * one packet on the network without payload copying, for maximum efficiency.
Andy Greene77ddd82010-11-13 10:03:47 +00001369 *
Andy Green62304762015-12-04 08:43:54 +08001370 * So for example you need this kind of code to use lws_write with a
Andy Greene77ddd82010-11-13 10:03:47 +00001371 * 128-byte payload
1372 *
Andy Greenab990e42010-10-31 12:42:52 +00001373 * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
Andy Greene77ddd82010-11-13 10:03:47 +00001374 *
Andy Greenab990e42010-10-31 12:42:52 +00001375 * // fill your part of the buffer... for example here it's all zeros
1376 * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
Andy Greene77ddd82010-11-13 10:03:47 +00001377 *
Andy Green62304762015-12-04 08:43:54 +08001378 * lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128,
Alex Rhatushnyak9f2246e2013-03-09 12:01:47 +08001379 * LWS_WRITE_TEXT);
Andy Greene77ddd82010-11-13 10:03:47 +00001380 *
Andy Green5fd8a5e2010-10-31 11:57:17 +00001381 * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
1382 * use the whole buffer without taking care of the above.
Andy Green4ea60062010-10-30 12:15:07 +01001383 */
1384
Andy Green687b0182011-02-26 11:04:01 +00001385/*
1386 * this is the frame nonce plus two header plus 8 length
Andy Greena41314f2011-05-23 10:00:03 +01001387 * there's an additional two for mux extension per mux nesting level
Andy Green687b0182011-02-26 11:04:01 +00001388 * 2 byte prepend on close will already fit because control frames cannot use
1389 * the big length style
1390 */
Andy Green4739e5c2011-01-22 12:51:57 +00001391
Bruce Perens173e9c42015-10-13 09:17:39 +08001392// Pad LWS_SEND_BUFFER_PRE_PADDING to the CPU word size, so that word references
1393// to the address immediately after the padding won't cause an unaligned access
1394// error. Sometimes the recommended padding is even larger than the size of a void *.
1395// For example, for the X86-64 architecture, in Intel's document
1396// https://software.intel.com/en-us/articles/data-alignment-when-migrating-to-64-bit-intel-architecture
1397// they recommend that structures larger than 16 bytes be aligned to 16-byte
1398// boundaries.
1399//
Andy Green2cd30742015-11-02 13:10:33 +08001400
1401#if !defined(LWS_SIZEOFPTR)
1402#define LWS_SIZEOFPTR (sizeof (void *))
1403#endif
1404#if !defined(u_int64_t)
1405#define u_int64_t unsigned long long
1406#endif
1407
Bruce Perens173e9c42015-10-13 09:17:39 +08001408#if __x86_64__
1409#define _LWS_PAD_SIZE 16 // Intel recommended for best performance.
1410#else
Alexander Lukichevfe6030a2015-10-20 13:10:33 +03001411#define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target architecture */
Bruce Perens173e9c42015-10-13 09:17:39 +08001412#endif
Stepan Vondrakfc6c2742015-10-15 21:05:54 +08001413#define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
Bruce Perens173e9c42015-10-13 09:17:39 +08001414#define LWS_SEND_BUFFER_PRE_PADDING _LWS_PAD(4 + 10 + (2 * MAX_MUX_RECURSION))
David Galeanoe2cf9922013-01-09 18:06:55 +08001415#define LWS_SEND_BUFFER_POST_PADDING 4
Andy Green4ea60062010-10-30 12:15:07 +01001416
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001417LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001418lws_write(struct lws *wsi, unsigned char *buf, size_t len,
Andy Green62304762015-12-04 08:43:54 +08001419 enum lws_write_protocol protocol);
Andy Greena2b0ab02010-11-11 12:28:29 +00001420
Gabriel Gritsch890f8e92014-02-15 16:20:25 +08001421/* helper for case where buffer may be const */
Andy Green29a44cf2015-12-04 07:55:17 +08001422#define lws_write_http(wsi, buf, len) \
1423 lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
Gabriel Gritsch890f8e92014-02-15 16:20:25 +08001424
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001425LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001426lws_serve_http_file(struct lws_context *context,
1427 struct lws *wsi, const char *file,
Andy Green917f43a2014-10-12 14:31:47 +08001428 const char *content_type, const char *other_headers,
1429 int other_headers_len);
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001430LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001431lws_serve_http_file_fragment(struct lws_context *context,
1432 struct lws *wsi);
Andy Greenb45993c2010-12-18 15:13:50 +00001433
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001434LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001435lws_return_http_status(struct lws_context *context,
1436 struct lws *wsi, unsigned int code,
1437 const char *html_body);
1438
1439LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
1440lws_get_protocol(struct lws *wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +00001441
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001442LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001443lws_callback_on_writable(struct lws_context *context, struct lws *wsi);
Andy Green90c7cbc2011-01-27 06:26:52 +00001444
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001445LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001446lws_callback_on_writable_all_protocol(const struct lws_protocols *protocol);
Andy Greene39e6ef2014-02-15 16:36:38 +08001447
1448LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001449lws_callback_all_protocol(const struct lws_protocols *protocol, int reason);
Andy Green90c7cbc2011-01-27 06:26:52 +00001450
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001451LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001452lws_get_socket_fd(struct lws *wsi);
1453
1454LWS_VISIBLE LWS_EXTERN int
1455lws_is_final_fragment(struct lws *wsi);
Andy Green82c3d542011-03-07 21:16:31 +00001456
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001457LWS_VISIBLE LWS_EXTERN unsigned char
Andy Green4b85c1d2015-12-04 11:08:32 +08001458lws_get_reserved_bits(struct lws *wsi);
David Galeanoe2cf9922013-01-09 18:06:55 +08001459
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001460LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001461lws_rx_flow_control(struct lws *wsi, int enable);
Andy Green90c7cbc2011-01-27 06:26:52 +00001462
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001463LWS_VISIBLE LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001464lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol);
Andy Greenb55451c2013-03-16 12:32:27 +08001465
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001466LWS_VISIBLE LWS_EXTERN size_t
Andy Green4b85c1d2015-12-04 11:08:32 +08001467lws_remaining_packet_payload(struct lws *wsi);
Andy Green38e57bb2011-01-19 12:20:27 +00001468
Andy Green97ee57f2014-10-29 09:39:08 +08001469/*
Peter Pentchevbb085da2015-12-03 15:55:11 +02001470 * if the protocol does not have any guidance, returns -1. Currently only
Andy Green97ee57f2014-10-29 09:39:08 +08001471 * http2 connections get send window information from this API. But your code
1472 * should use it so it can work properly with any protocol.
1473 *
1474 * If nonzero return is the amount of payload data the peer or intermediary has
1475 * reported it has buffer space for. That has NO relationship with the amount
1476 * of buffer space your OS can accept on this connection for a write action.
1477 *
1478 * This number represents the maximum you could send to the peer or intermediary
1479 * on this connection right now without it complaining.
1480 *
1481 * lws manages accounting for send window updates and payload writes
1482 * automatically, so this number reflects the situation at the peer or
1483 * intermediary dynamically.
1484 */
1485LWS_VISIBLE LWS_EXTERN size_t
Andy Green4b85c1d2015-12-04 11:08:32 +08001486lws_get_peer_write_allowance(struct lws *wsi);
Andy Green97ee57f2014-10-29 09:39:08 +08001487
Andy Green4b85c1d2015-12-04 11:08:32 +08001488LWS_VISIBLE LWS_EXTERN struct lws *
1489lws_client_connect(struct lws_context *clients,
Andy Green4739e5c2011-01-22 12:51:57 +00001490 const char *address,
1491 int port,
Andy Green90c7cbc2011-01-27 06:26:52 +00001492 int ssl_connection,
Andy Green4739e5c2011-01-22 12:51:57 +00001493 const char *path,
1494 const char *host,
1495 const char *origin,
Andy Greenbfb051f2011-02-09 08:49:14 +00001496 const char *protocol,
1497 int ietf_version_or_minus_one);
Andy Green4739e5c2011-01-22 12:51:57 +00001498
Andy Green4b85c1d2015-12-04 11:08:32 +08001499LWS_VISIBLE LWS_EXTERN struct lws *
1500lws_client_connect_extended(struct lws_context *clients,
David Brooks2c60d952012-04-20 12:19:01 +08001501 const char *address,
1502 int port,
1503 int ssl_connection,
1504 const char *path,
1505 const char *host,
1506 const char *origin,
1507 const char *protocol,
1508 int ietf_version_or_minus_one,
1509 void *userdata);
1510
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001511LWS_VISIBLE LWS_EXTERN const char *
Andy Green4b85c1d2015-12-04 11:08:32 +08001512lws_canonical_hostname(struct lws_context *context);
Andy Green2ac5a6f2011-01-28 10:00:18 +00001513
Andy Green4739e5c2011-01-22 12:51:57 +00001514
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001515LWS_VISIBLE LWS_EXTERN void
Andy Green4b85c1d2015-12-04 11:08:32 +08001516lws_get_peer_addresses(struct lws_context *context,
1517 struct lws *wsi, lws_sockfd_type fd,
Andy Green6d417202015-12-04 10:39:23 +08001518 char *name, int name_len,
1519 char *rip, int rip_len);
Andy Green07034092011-02-13 08:37:12 +00001520
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001521LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001522lws_get_random(struct lws_context *context, void *buf, int len);
Andy Green8acc1942011-03-07 20:47:40 +00001523
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001524LWS_VISIBLE LWS_EXTERN int
Andy Green279a3032013-01-17 10:05:39 +08001525lws_daemonize(const char *_lock_path);
1526
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001527LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001528lws_send_pipe_choked(struct lws *wsi);
Andy Green043a0ba2011-04-25 23:54:11 +08001529
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001530LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001531lws_partial_buffered(struct lws *wsi);
Andy Green14425ea2014-08-18 22:49:39 +08001532
1533LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001534lws_frame_is_binary(struct lws *wsi);
Andy Green2fd3f2f2013-01-18 09:49:20 +08001535
Andy Greenb128ccc2014-08-16 09:54:27 +08001536LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001537lws_is_ssl(struct lws *wsi);
Andy Green4c79ee72015-10-15 11:20:40 +08001538#ifdef LWS_SHA1_USE_OPENSSL_NAME
Andy Green29a44cf2015-12-04 07:55:17 +08001539#define lws_SHA1 SHA1
Andy Green4c79ee72015-10-15 11:20:40 +08001540#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001541LWS_VISIBLE LWS_EXTERN unsigned char *
Andy Green29a44cf2015-12-04 07:55:17 +08001542lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
Andy Green4c79ee72015-10-15 11:20:40 +08001543#endif
Andy Green2836c642011-03-07 20:47:41 +00001544
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001545LWS_VISIBLE LWS_EXTERN int
Andy Greenf94e0582011-05-14 15:07:56 +02001546lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
1547
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001548LWS_VISIBLE LWS_EXTERN int
Andy Greenf94e0582011-05-14 15:07:56 +02001549lws_b64_decode_string(const char *in, char *out, int out_size);
1550
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001551LWS_VISIBLE LWS_EXTERN const char *
Andy Green7b405452013-02-01 10:50:15 +08001552lws_get_library_version(void);
1553
Andy Green16ab3182013-02-10 18:02:31 +08001554/* access to headers... only valid while headers valid */
1555
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001556LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001557lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +08001558
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001559LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001560lws_hdr_copy(struct lws *wsi, char *dest, int len,
Andy Greenb5b23192013-02-11 17:13:32 +08001561 enum lws_token_indexes h);
Andy Green16ab3182013-02-10 18:02:31 +08001562
Andy Greenea0642a2013-01-29 06:52:00 +08001563/*
1564 * Note: this is not normally needed as a user api. It's provided in case it is
1565 * useful when integrating with other app poll loop service code.
1566 */
1567
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001568LWS_VISIBLE LWS_EXTERN int
Andy Green4b85c1d2015-12-04 11:08:32 +08001569lws_read(struct lws_context *context, struct lws *wsi,
1570 unsigned char *buf, size_t len);
Andy Greenea0642a2013-01-29 06:52:00 +08001571
Andy Green3182ece2013-01-20 17:08:31 +08001572#ifndef LWS_NO_EXTENSIONS
Andy Green4b85c1d2015-12-04 11:08:32 +08001573LWS_VISIBLE LWS_EXTERN struct lws_extension *lws_get_internal_extensions();
Andy Green3182ece2013-01-20 17:08:31 +08001574#endif
Andy Green4cd87a02011-03-06 13:15:32 +00001575
Alejandro Merycdc97172014-12-04 23:15:27 +01001576/*
1577 * custom allocator support
1578 */
1579LWS_VISIBLE LWS_EXTERN void
1580lws_set_allocator(void *(*realloc)(void *ptr, size_t size));
1581
Peter Hinz56885f32011-03-02 22:03:47 +00001582#ifdef __cplusplus
1583}
1584#endif
Andy Greena11fe942011-09-25 10:30:26 +01001585
Andy Greenab990e42010-10-31 12:42:52 +00001586#endif