2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2016 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
29 #include "mbed-drivers/mbed.h"
30 #include "sal-iface-eth/EthernetInterface.h"
31 #include "sockets/TCPListener.h"
32 #include "sal-stack-lwip/lwipv4_init.h"
36 using namespace mbed::Sockets::v0;
47 awaiting_on_writeable(0)
52 void set_wsi(struct lws *_wsi) { wsi = _wsi; }
53 int actual_onRX(Socket *s);
55 void onError(Socket *s, socket_error_t err);
56 void onDisconnect(TCPStream *s);
57 void onSent(Socket *s, uint16_t len);
58 void serialized_writeable(struct lws *wsi);
66 char awaiting_on_writeable;
69 class lws_conn_listener : lws_conn {
72 srv(SOCKET_STACK_LWIP_IPV4)
74 srv.setOnError(TCPStream::ErrorHandler_t(this,
75 &lws_conn_listener::onError));
78 void start(const uint16_t port);
82 void onError(Socket *s, socket_error_t err);
83 void onIncoming(TCPListener *s, void *impl);
84 void onDisconnect(TCPStream *s);
103 #include "lws_config.h"
105 #if defined(WIN32) || defined(_WIN32)
106 #ifndef WIN32_LEAN_AND_MEAN
107 #define WIN32_LEAN_AND_MEAN
110 #include <winsock2.h>
111 #include <ws2tcpip.h>
117 #define strcasecmp stricmp
118 #define getdtablesize() 30000
120 #define LWS_INLINE __inline
125 #define LWS_EXTERN extern __declspec(dllexport)
127 #define LWS_EXTERN extern __declspec(dllimport)
133 #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
134 #define LWS_O_RDONLY _O_RDONLY
136 #else /* NOT WIN32 */
139 #define LWS_INLINE inline
140 #define LWS_O_RDONLY O_RDONLY
142 #ifndef MBED_OPERATORS
145 #define LWS_INVALID_FILE -1
147 #define getdtablesize() (20)
148 #define LWS_INVALID_FILE NULL
151 #if defined(__GNUC__)
152 #define LWS_VISIBLE __attribute__((visibility("default")))
157 #if defined(__ANDROID__)
158 #define getdtablesize() 1024
165 #endif /* LWS_USE_LIBEV */
168 #define LWS_EXTERN extern
174 #include <sys/time.h>
178 #ifdef LWS_OPENSSL_SUPPORT
180 #ifdef USE_OLD_CYASSL
181 #include <cyassl/openssl/ssl.h>
183 #include <wolfssl/openssl/ssl.h>
184 #endif /* not USE_OLD_CYASSL */
186 #include <openssl/ssl.h>
187 #endif /* not USE_WOLFSSL */
190 #define CONTEXT_PORT_NO_LISTEN -1
192 enum lws_log_levels {
202 LLL_LATENCY = 1 << 9,
204 LLL_COUNT = 10 /* set to count of valid flags */
207 LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
208 LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
210 /* notice, warn and log are always compiled in */
211 #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
212 #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
213 #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
215 * weaker logging can be deselected at configure time using --disable-debug
216 * that gets rid of the overhead of checking while keeping _warn and _err
221 #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
222 #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
223 #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
224 #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
225 #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
226 #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
227 #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
228 LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
232 #define lwsl_info(...) {}
233 #define lwsl_debug(...) {}
234 #define lwsl_parser(...) {}
235 #define lwsl_header(...) {}
236 #define lwsl_ext(...) {}
237 #define lwsl_client(...) {}
238 #define lwsl_latency(...) {}
239 #define lwsl_hexdump(a, b)
243 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
245 /* api change list for user code to test against */
247 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
249 /* the struct lws_protocols has the id field present */
250 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
252 /* you can call lws_get_peer_write_allowance */
253 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
255 /* extra parameter introduced in 917f43ab821 */
256 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
258 /* File operations stuff exists */
259 #define LWS_FEATURE_FOPS
262 * NOTE: These public enums are part of the abi. If you want to add one,
263 * add it at where specified so existing users are unaffected.
265 enum lws_context_options {
266 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = (1 << 1),
267 LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = (1 << 2),
268 LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = (1 << 3),
269 LWS_SERVER_OPTION_LIBEV = (1 << 4),
270 LWS_SERVER_OPTION_DISABLE_IPV6 = (1 << 5),
271 LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = (1 << 6),
272 LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = (1 << 7),
273 LWS_SERVER_OPTION_VALIDATE_UTF8 = (1 << 8),
274 LWS_SERVER_OPTION_SSL_ECDH = (1 << 9),
276 /****** add new things just above ---^ ******/
280 * NOTE: These public enums are part of the abi. If you want to add one,
281 * add it at where specified so existing users are unaffected.
283 enum lws_callback_reasons {
284 LWS_CALLBACK_ESTABLISHED = 0,
285 LWS_CALLBACK_CLIENT_CONNECTION_ERROR = 1,
286 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH = 2,
287 LWS_CALLBACK_CLIENT_ESTABLISHED = 3,
288 LWS_CALLBACK_CLOSED = 4,
289 LWS_CALLBACK_CLOSED_HTTP = 5,
290 LWS_CALLBACK_RECEIVE = 6,
291 LWS_CALLBACK_RECEIVE_PONG = 7,
292 LWS_CALLBACK_CLIENT_RECEIVE = 8,
293 LWS_CALLBACK_CLIENT_RECEIVE_PONG = 9,
294 LWS_CALLBACK_CLIENT_WRITEABLE = 10,
295 LWS_CALLBACK_SERVER_WRITEABLE = 11,
296 LWS_CALLBACK_HTTP = 12,
297 LWS_CALLBACK_HTTP_BODY = 13,
298 LWS_CALLBACK_HTTP_BODY_COMPLETION = 14,
299 LWS_CALLBACK_HTTP_FILE_COMPLETION = 15,
300 LWS_CALLBACK_HTTP_WRITEABLE = 16,
301 LWS_CALLBACK_FILTER_NETWORK_CONNECTION = 17,
302 LWS_CALLBACK_FILTER_HTTP_CONNECTION = 18,
303 LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED = 19,
304 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION = 20,
305 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS = 21,
306 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS = 22,
307 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION = 23,
308 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER = 24,
309 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY = 25,
310 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED = 26,
311 LWS_CALLBACK_PROTOCOL_INIT = 27,
312 LWS_CALLBACK_PROTOCOL_DESTROY = 28,
313 LWS_CALLBACK_WSI_CREATE /* always protocol[0] */ = 29,
314 LWS_CALLBACK_WSI_DESTROY /* always protocol[0] */ = 30,
315 LWS_CALLBACK_GET_THREAD_ID = 31,
317 /* external poll() management support */
318 LWS_CALLBACK_ADD_POLL_FD = 32,
319 LWS_CALLBACK_DEL_POLL_FD = 33,
320 LWS_CALLBACK_CHANGE_MODE_POLL_FD = 34,
321 LWS_CALLBACK_LOCK_POLL = 35,
322 LWS_CALLBACK_UNLOCK_POLL = 36,
324 LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY = 37,
325 LWS_CALLBACK_WS_PEER_INITIATED_CLOSE = 38,
327 LWS_CALLBACK_WS_EXT_DEFAULTS = 39,
329 /****** add new things just above ---^ ******/
331 LWS_CALLBACK_USER = 1000, /* user code can use any including / above */
336 typedef SOCKET lws_sockfd_type;
337 typedef HANDLE lws_filefd_type;
338 #define lws_sockfd_valid(sfd) (!!sfd)
346 #if defined(MBED_OPERATORS)
347 /* it's a class lws_conn * */
348 typedef void * lws_sockfd_type;
349 typedef void * lws_filefd_type;
350 #define lws_sockfd_valid(sfd) (!!sfd)
356 #define POLLIN 0x0001
357 #define POLLPRI 0x0002
358 #define POLLOUT 0x0004
359 #define POLLERR 0x0008
360 #define POLLHUP 0x0010
361 #define POLLNVAL 0x0020
365 void * mbed3_create_tcp_stream_socket(void);
366 void mbed3_delete_tcp_stream_socket(void *sockfd);
367 void mbed3_tcp_stream_bind(void *sock, int port, struct lws *);
368 void mbed3_tcp_stream_accept(void *sock, struct lws *);
370 typedef int lws_sockfd_type;
371 typedef int lws_filefd_type;
372 #define lws_sockfd_valid(sfd) (sfd >= 0)
375 #define lws_pollfd pollfd
378 /* argument structure for all external poll related calls
381 struct lws_pollargs {
382 lws_sockfd_type fd; /* applicable socket descriptor */
383 int events; /* the new event mask */
384 int prev_events; /* the previous event mask */
388 * struct lws_plat_file_ops - Platform-specific file operations
390 * These provide platform-agnostic ways to deal with filesystem access in the
391 * library and in the user code.
393 * @open: Open file (always binary access if plat supports it)
394 * filelen is filled on exit to be the length of the file
395 * flags should be set to O_RDONLY or O_RDWR
397 * @seek_cur: Seek from current position
398 * @read: Read fron file *amount is set on exit to amount read
399 * @write: Write to file *amount is set on exit as amount written
401 struct lws_plat_file_ops {
402 lws_filefd_type (*open)(struct lws *wsi, const char *filename,
403 unsigned long *filelen, int flags);
404 int (*close)(struct lws *wsi, lws_filefd_type fd);
405 unsigned long (*seek_cur)(struct lws *wsi, lws_filefd_type fd,
406 long offset_from_cur_pos);
407 int (*read)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
408 unsigned char *buf, unsigned long len);
409 int (*write)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
410 unsigned char *buf, unsigned long len);
412 /* Add new things just above here ---^
413 * This is part of the ABI, don't needlessly break compatibilty */
417 * NOTE: These public enums are part of the abi. If you want to add one,
418 * add it at where specified so existing users are unaffected.
420 enum lws_extension_callback_reasons {
421 LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT = 0,
422 LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT = 1,
423 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT = 2,
424 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT = 3,
425 LWS_EXT_CB_CONSTRUCT = 4,
426 LWS_EXT_CB_CLIENT_CONSTRUCT = 5,
427 LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE = 6,
428 LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION = 7,
429 LWS_EXT_CB_DESTROY = 8,
430 LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING = 9,
431 LWS_EXT_CB_ANY_WSI_ESTABLISHED = 10,
432 LWS_EXT_CB_PACKET_RX_PREPARSE = 11,
433 LWS_EXT_CB_PACKET_TX_PRESEND = 12,
434 LWS_EXT_CB_PACKET_TX_DO_SEND = 13,
435 LWS_EXT_CB_HANDSHAKE_REPLY_TX = 14,
436 LWS_EXT_CB_FLUSH_PENDING_TX = 15,
437 LWS_EXT_CB_EXTENDED_PAYLOAD_RX = 16,
438 LWS_EXT_CB_CAN_PROXY_CLIENT_CONNECTION = 17,
440 LWS_EXT_CB_REQUEST_ON_WRITEABLE = 19,
441 LWS_EXT_CB_IS_WRITEABLE = 20,
442 LWS_EXT_CB_PAYLOAD_TX = 21,
443 LWS_EXT_CB_PAYLOAD_RX = 22,
444 LWS_EXT_CB_OPTION_DEFAULT = 23,
445 LWS_EXT_CB_OPTION_SET = 24,
446 LWS_EXT_CB_OPTION_CONFIRM = 25,
448 /****** add new things just above ---^ ******/
452 * NOTE: These public enums are part of the abi. If you want to add one,
453 * add it at where specified so existing users are unaffected.
455 enum lws_write_protocol {
457 LWS_WRITE_BINARY = 1,
458 LWS_WRITE_CONTINUATION = 2,
461 /* special 04+ opcodes */
463 /* LWS_WRITE_CLOSE is handled by lws_close_reason() */
467 /* Same as write_http but we know this write ends the transaction */
468 LWS_WRITE_HTTP_FINAL = 7,
472 LWS_WRITE_HTTP_HEADERS = 8,
474 /****** add new things just above ---^ ******/
478 LWS_WRITE_NO_FIN = 0x40,
480 * client packet payload goes out on wire unmunged
481 * only useful for security tests since normal servers cannot
482 * decode the content if used
484 LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
488 * you need these to look at headers that have been parsed if using the
489 * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
490 * list below is absent, .token = NULL and token_len = 0. Otherwise .token
491 * points to .token_len chars containing that header content.
500 * don't forget to update test server header dump accordingly
502 * these have to be kept in sync with lextable.h / minilex.c
504 * NOTE: These public enums are part of the abi. If you want to add one,
505 * add it at where specified so existing users are unaffected.
507 enum lws_token_indexes {
508 WSI_TOKEN_GET_URI = 0,
509 WSI_TOKEN_POST_URI = 1,
510 WSI_TOKEN_OPTIONS_URI = 2,
512 WSI_TOKEN_CONNECTION = 4,
513 WSI_TOKEN_UPGRADE = 5,
514 WSI_TOKEN_ORIGIN = 6,
516 WSI_TOKEN_CHALLENGE = 8,
517 WSI_TOKEN_EXTENSIONS = 9,
520 WSI_TOKEN_PROTOCOL = 12,
521 WSI_TOKEN_ACCEPT = 13,
522 WSI_TOKEN_NONCE = 14,
524 WSI_TOKEN_HTTP2_SETTINGS = 16,
525 WSI_TOKEN_HTTP_ACCEPT = 17,
526 WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18,
527 WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
528 WSI_TOKEN_HTTP_IF_NONE_MATCH = 20,
529 WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21,
530 WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22,
531 WSI_TOKEN_HTTP_PRAGMA = 23,
532 WSI_TOKEN_HTTP_CACHE_CONTROL = 24,
533 WSI_TOKEN_HTTP_AUTHORIZATION = 25,
534 WSI_TOKEN_HTTP_COOKIE = 26,
535 WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
536 WSI_TOKEN_HTTP_CONTENT_TYPE = 28,
537 WSI_TOKEN_HTTP_DATE = 29,
538 WSI_TOKEN_HTTP_RANGE = 30,
539 WSI_TOKEN_HTTP_REFERER = 31,
541 WSI_TOKEN_VERSION = 33,
542 WSI_TOKEN_SWORIGIN = 34,
544 WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
545 WSI_TOKEN_HTTP_COLON_METHOD = 36,
546 WSI_TOKEN_HTTP_COLON_PATH = 37,
547 WSI_TOKEN_HTTP_COLON_SCHEME = 38,
548 WSI_TOKEN_HTTP_COLON_STATUS = 39,
550 WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40,
551 WSI_TOKEN_HTTP_ACCEPT_RANGES = 41,
552 WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42,
553 WSI_TOKEN_HTTP_AGE = 43,
554 WSI_TOKEN_HTTP_ALLOW = 44,
555 WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45,
556 WSI_TOKEN_HTTP_CONTENT_ENCODING = 46,
557 WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
558 WSI_TOKEN_HTTP_CONTENT_LOCATION = 48,
559 WSI_TOKEN_HTTP_CONTENT_RANGE = 49,
560 WSI_TOKEN_HTTP_ETAG = 50,
561 WSI_TOKEN_HTTP_EXPECT = 51,
562 WSI_TOKEN_HTTP_EXPIRES = 52,
563 WSI_TOKEN_HTTP_FROM = 53,
564 WSI_TOKEN_HTTP_IF_MATCH = 54,
565 WSI_TOKEN_HTTP_IF_RANGE = 55,
566 WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56,
567 WSI_TOKEN_HTTP_LAST_MODIFIED = 57,
568 WSI_TOKEN_HTTP_LINK = 58,
569 WSI_TOKEN_HTTP_LOCATION = 59,
570 WSI_TOKEN_HTTP_MAX_FORWARDS = 60,
571 WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61,
572 WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62,
573 WSI_TOKEN_HTTP_REFRESH = 63,
574 WSI_TOKEN_HTTP_RETRY_AFTER = 64,
575 WSI_TOKEN_HTTP_SERVER = 65,
576 WSI_TOKEN_HTTP_SET_COOKIE = 66,
577 WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
578 WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68,
579 WSI_TOKEN_HTTP_USER_AGENT = 69,
580 WSI_TOKEN_HTTP_VARY = 70,
581 WSI_TOKEN_HTTP_VIA = 71,
582 WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72,
585 WSI_TOKEN_PATCH_URI = 73,
586 WSI_TOKEN_PUT_URI = 74,
587 WSI_TOKEN_DELETE_URI = 75,
589 WSI_TOKEN_HTTP_URI_ARGS = 76,
591 /* use token storage to stash these */
593 _WSI_TOKEN_CLIENT_SENT_PROTOCOLS = 77,
594 _WSI_TOKEN_CLIENT_PEER_ADDRESS = 78,
595 _WSI_TOKEN_CLIENT_URI = 79,
596 _WSI_TOKEN_CLIENT_HOST = 80,
597 _WSI_TOKEN_CLIENT_ORIGIN = 81,
599 /****** add new things just above ---^ ******/
601 /* always last real token index*/
604 /* parser state additions */
607 WSI_TOKEN_SKIPPING_SAW_CR,
608 WSI_PARSING_COMPLETE,
609 WSI_INIT_TOKEN_MUXURL,
612 struct lws_token_limits {
613 unsigned short token_limit[WSI_TOKEN_COUNT];
620 1000 indicates a normal closure, meaning that the purpose for
621 which the connection was established has been fulfilled.
625 1001 indicates that an endpoint is "going away", such as a server
626 going down or a browser having navigated away from a page.
630 1002 indicates that an endpoint is terminating the connection due
635 1003 indicates that an endpoint is terminating the connection
636 because it has received a type of data it cannot accept (e.g., an
637 endpoint that understands only text data MAY send this if it
638 receives a binary message).
642 Reserved. The specific meaning might be defined in the future.
646 1005 is a reserved value and MUST NOT be set as a status code in a
647 Close control frame by an endpoint. It is designated for use in
648 applications expecting a status code to indicate that no status
649 code was actually present.
653 1006 is a reserved value and MUST NOT be set as a status code in a
654 Close control frame by an endpoint. It is designated for use in
655 applications expecting a status code to indicate that the
656 connection was closed abnormally, e.g., without sending or
657 receiving a Close control frame.
661 1007 indicates that an endpoint is terminating the connection
662 because it has received data within a message that was not
663 consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
664 data within a text message).
668 1008 indicates that an endpoint is terminating the connection
669 because it has received a message that violates its policy. This
670 is a generic status code that can be returned when there is no
671 other more suitable status code (e.g., 1003 or 1009) or if there
672 is a need to hide specific details about the policy.
676 1009 indicates that an endpoint is terminating the connection
677 because it has received a message that is too big for it to
682 1010 indicates that an endpoint (client) is terminating the
683 connection because it has expected the server to negotiate one or
684 more extension, but the server didn't return them in the response
685 message of the WebSocket handshake. The list of extensions that
686 are needed SHOULD appear in the /reason/ part of the Close frame.
687 Note that this status code is not used by the server, because it
688 can fail the WebSocket handshake instead.
692 1011 indicates that a server is terminating the connection because
693 it encountered an unexpected condition that prevented it from
694 fulfilling the request.
698 1015 is a reserved value and MUST NOT be set as a status code in a
699 Close control frame by an endpoint. It is designated for use in
700 applications expecting a status code to indicate that the
701 connection was closed due to a failure to perform a TLS handshake
702 (e.g., the server certificate can't be verified).
706 * NOTE: These public enums are part of the abi. If you want to add one,
707 * add it at where specified so existing users are unaffected.
709 enum lws_close_status {
710 LWS_CLOSE_STATUS_NOSTATUS = 0,
711 LWS_CLOSE_STATUS_NORMAL = 1000,
712 LWS_CLOSE_STATUS_GOINGAWAY = 1001,
713 LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
714 LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
715 LWS_CLOSE_STATUS_RESERVED = 1004,
716 LWS_CLOSE_STATUS_NO_STATUS = 1005,
717 LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
718 LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
719 LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
720 LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
721 LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
722 LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
723 LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
725 /****** add new things just above ---^ ******/
727 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
731 HTTP_STATUS_OK = 200,
732 HTTP_STATUS_NO_CONTENT = 204,
734 HTTP_STATUS_BAD_REQUEST = 400,
735 HTTP_STATUS_UNAUTHORIZED,
736 HTTP_STATUS_PAYMENT_REQUIRED,
737 HTTP_STATUS_FORBIDDEN,
738 HTTP_STATUS_NOT_FOUND,
739 HTTP_STATUS_METHOD_NOT_ALLOWED,
740 HTTP_STATUS_NOT_ACCEPTABLE,
741 HTTP_STATUS_PROXY_AUTH_REQUIRED,
742 HTTP_STATUS_REQUEST_TIMEOUT,
743 HTTP_STATUS_CONFLICT,
745 HTTP_STATUS_LENGTH_REQUIRED,
746 HTTP_STATUS_PRECONDITION_FAILED,
747 HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
748 HTTP_STATUS_REQ_URI_TOO_LONG,
749 HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
750 HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
751 HTTP_STATUS_EXPECTATION_FAILED,
753 HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
754 HTTP_STATUS_NOT_IMPLEMENTED,
755 HTTP_STATUS_BAD_GATEWAY,
756 HTTP_STATUS_SERVICE_UNAVAILABLE,
757 HTTP_STATUS_GATEWAY_TIMEOUT,
758 HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
763 /* needed even with extensions disabled for create context */
764 struct lws_extension;
767 * typedef lws_callback_function() - User server actions
768 * @wsi: Opaque websocket instance pointer
769 * @reason: The reason for the call
770 * @user: Pointer to per-session user data allocated by library
771 * @in: Pointer used for some callback reasons
772 * @len: Length set for some callback reasons
774 * This callback is the way the user controls what is served. All the
775 * protocol detail is hidden and handled by the library.
777 * For each connection / session there is user data allocated that is
778 * pointed to by "user". You set the size of this user data area when
779 * the library is initialized with lws_create_server.
781 * You get an opportunity to initialize user data when called back with
782 * LWS_CALLBACK_ESTABLISHED reason.
784 * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with
785 * an incoming client. If you built the library
786 * with ssl support, @in is a pointer to the
787 * ssl struct associated with the connection or
790 * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
791 * been unable to complete a handshake with the remote server. If
792 * in is non-NULL, you can find an error string of length len where
795 * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
796 * client user code to examine the http headers
797 * and decide to reject the connection. If the
798 * content in the headers is interesting to the
799 * client (url, etc) it needs to copy it out at
800 * this point since it will be destroyed before
801 * the CLIENT_ESTABLISHED call
803 * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
804 * a handshake with the remote server
806 * LWS_CALLBACK_CLOSED: when the websocket session ends
808 * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends
810 * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
811 * remote client, it can be found at *in and is
814 * LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
815 * they appear with this callback reason. PONG
816 * packets only exist in 04+ protocol
818 * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
819 * client connection, it can be found at *in and
822 * LWS_CALLBACK_HTTP: an http request has come from a client that is not
823 * asking to upgrade the connection to a websocket
824 * one. This is a chance to serve http content,
825 * for example, to send a script to the client
826 * which will then open the websockets connection.
827 * @in points to the URI path requested and
828 * lws_serve_http_file() makes it very
829 * simple to send back a file to the client.
830 * Normally after sending the file you are done
831 * with the http connection, since the rest of the
832 * activity will come by websockets from the script
833 * that was delivered by http, so you will want to
834 * return 1; to close and free up the connection.
835 * That's important because it uses a slot in the
836 * total number of client connections allowed set
839 * LWS_CALLBACK_HTTP_BODY: the next @len bytes data from the http
840 * request body HTTP connection is now available in @in.
842 * LWS_CALLBACK_HTTP_BODY_COMPLETION: the expected amount of http request
843 * body has been delivered
845 * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol
848 * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down
849 * http link has completed.
851 * LWS_CALLBACK_CLIENT_WRITEABLE:
852 * LWS_CALLBACK_SERVER_WRITEABLE: If you call
853 * lws_callback_on_writable() on a connection, you will
854 * get one of these callbacks coming when the connection socket
855 * is able to accept another write packet without blocking.
856 * If it already was able to take another packet without blocking,
857 * you'll get this callback at the next call to the service loop
858 * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
859 * and servers get LWS_CALLBACK_SERVER_WRITEABLE.
861 * LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
862 * the server at network level; the connection is accepted but then
863 * passed to this callback to decide whether to hang up immediately
864 * or not, based on the client IP. @in contains the connection
865 * socket's descriptor. Since the client connection information is
866 * not available yet, @wsi still pointing to the main server socket.
867 * Return non-zero to terminate the connection before sending or
868 * receiving anything. Because this happens immediately after the
869 * network connection from the client, there's no websocket protocol
870 * selected yet so this callback is issued only to protocol 0.
872 * LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED: A new client just had
873 * been connected, accepted, and instantiated into the pool. This
874 * callback allows setting any relevant property to it. Because this
875 * happens immediately after the instantiation of a new client,
876 * there's no websocket protocol selected yet so this callback is
877 * issued only to protocol 0. Only @wsi is defined, pointing to the
878 * new client, and the return value is ignored.
880 * LWS_CALLBACK_FILTER_HTTP_CONNECTION: called when the request has
881 * been received and parsed from the client, but the response is
882 * not sent yet. Return non-zero to disallow the connection.
883 * @user is a pointer to the connection user space allocation,
884 * @in is the URI, eg, "/"
885 * In your handler you can use the public APIs
886 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
887 * headers using the header enums lws_token_indexes from
888 * libwebsockets.h to check for and read the supported header
889 * presence and content before deciding to allow the http
890 * connection to proceed or to kill the connection.
892 * LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
893 * been received and parsed from the client, but the response is
894 * not sent yet. Return non-zero to disallow the connection.
895 * @user is a pointer to the connection user space allocation,
896 * @in is the requested protocol name
897 * In your handler you can use the public APIs
898 * lws_hdr_total_length() / lws_hdr_copy() to access all of the
899 * headers using the header enums lws_token_indexes from
900 * libwebsockets.h to check for and read the supported header
901 * presence and content before deciding to allow the handshake
902 * to proceed or to kill the connection.
904 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for
905 * including OpenSSL support, this callback allows your user code
906 * to perform extra SSL_CTX_load_verify_locations() or similar
907 * calls to direct OpenSSL where to find certificates the client
908 * can use to confirm the remote server identity. @user is the
911 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for
912 * including OpenSSL support, this callback allows your user code
913 * to load extra certifcates into the server which allow it to
914 * verify the validity of certificates returned by clients. @user
915 * is the server's OpenSSL SSL_CTX*
917 * LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY: if configured for
918 * including OpenSSL support but no private key file has been
919 * specified (ssl_private_key_filepath is NULL), this is called to
920 * allow the user to set the private key directly via libopenssl
921 * and perform further operations if required; this might be useful
922 * in situations where the private key is not directly accessible
923 * by the OS, for example if it is stored on a smartcard
924 * @user is the server's OpenSSL SSL_CTX*
926 * LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the
927 * libwebsockets context was created with the option
928 * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
929 * callback is generated during OpenSSL verification of the cert
930 * sent from the client. It is sent to protocol[0] callback as
931 * no protocol has been negotiated on the connection yet.
932 * Notice that the libwebsockets context and wsi are both NULL
933 * during this callback. See
934 * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
935 * to understand more detail about the OpenSSL callback that
936 * generates this libwebsockets callback and the meanings of the
937 * arguments passed. In this callback, @user is the x509_ctx,
938 * @in is the ssl pointer and @len is preverify_ok
939 * Notice that this callback maintains libwebsocket return
940 * conventions, return 0 to mean the cert is OK or 1 to fail it.
941 * This also means that if you don't handle this callback then
942 * the default callback action of returning 0 allows the client
945 * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens
946 * when a client handshake is being compiled. @user is NULL,
947 * @in is a char **, it's pointing to a char * which holds the
948 * next location in the header buffer where you can add
949 * headers, and @len is the remaining space in the header buffer,
950 * which is typically some hundreds of bytes. So, to add a canned
951 * cookie, your handler code might look similar to:
953 * char **p = (char **)in;
958 * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
962 * Notice if you add anything, you just have to take care about
963 * the CRLF on the line you added. Obviously this callback is
964 * optional, if you don't handle it everything is fine.
966 * Notice the callback is coming to protocols[0] all the time,
967 * because there is no specific protocol handshook yet.
969 * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
970 * sees that it does support a requested extension, before
971 * accepting the extension by additing to the list sent back to
972 * the client it gives this callback just to check that it's okay
973 * to use that extension. It calls back to the requested protocol
974 * and with @in being the extension name, @len is 0 and @user is
975 * valid. Note though at this time the ESTABLISHED callback hasn't
976 * happened yet so if you initialize @user content there, @user
977 * content during this callback might not be useful for anything.
978 * Notice this callback comes to protocols[0].
980 * LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: When a client
981 * connection is being prepared to start a handshake to a server,
982 * each supported extension is checked with protocols[0] callback
983 * with this reason, giving the user code a chance to suppress the
984 * claim to support that extension by returning non-zero. If
985 * unhandled, by default 0 will be returned and the extension
986 * support included in the header to the server. Notice this
987 * callback comes to protocols[0].
989 * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can
990 * do initial setup / allocations etc
992 * LWS_CALLBACK_PROTOCOL_DESTROY: One-time call per protocol indicating
993 * this protocol won't get used at all after this callback, the
994 * context is getting destroyed. Take the opportunity to
995 * deallocate everything that was allocated by the protocol.
997 * LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification
999 * LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification
1001 * The next five reasons are optional and only need taking care of if you
1002 * will be integrating libwebsockets sockets into an external polling
1005 * For these calls, @in points to a struct lws_pollargs that
1006 * contains @fd, @events and @prev_events members
1008 * LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
1009 * internally, but in the case you are integrating with another
1010 * server you will need to have libwebsocket sockets share a
1011 * polling array with the other server. This and the other
1012 * POLL_FD related callbacks let you put your specialized
1013 * poll array interface code in the callback for protocol 0, the
1014 * first protocol you support, usually the HTTP protocol in the
1016 * This callback happens when a socket needs to be
1017 * added to the polling loop: @in points to a struct
1018 * lws_pollargs; the @fd member of the struct is the file
1019 * descriptor, and @events contains the active events.
1021 * If you are using the internal polling loop (the "service"
1022 * callback), you can just ignore these callbacks.
1024 * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
1025 * needs to be removed from an external polling array. @in is
1026 * again the struct lws_pollargs containing the @fd member
1027 * to be removed. If you are using the internal polling
1028 * loop, you can just ignore it.
1030 * LWS_CALLBACK_CHANGE_MODE_POLL_FD: This callback happens when
1031 * libwebsockets wants to modify the events for a connectiion.
1032 * @in is the struct lws_pollargs with the @fd to change.
1033 * The new event mask is in @events member and the old mask is in
1034 * the @prev_events member.
1035 * If you are using the internal polling loop, you can just ignore
1038 * LWS_CALLBACK_LOCK_POLL:
1039 * LWS_CALLBACK_UNLOCK_POLL: These allow the external poll changes driven
1040 * by libwebsockets to participate in an external thread locking
1041 * scheme around the changes, so the whole thing is threadsafe.
1042 * These are called around three activities in the library,
1043 * - inserting a new wsi in the wsi / fd table (len=1)
1044 * - deleting a wsi from the wsi / fd table (len=1)
1045 * - changing a wsi's POLLIN/OUT state (len=0)
1046 * Locking and unlocking external synchronization objects when
1047 * len == 1 allows external threads to be synchronized against
1048 * wsi lifecycle changes if it acquires the same lock for the
1049 * duration of wsi dereference from the other thread context.
1051 * LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
1052 * The peer has sent an unsolicited Close WS packet. @in and
1053 * @len are the optional close code (first 2 bytes, network
1054 * order) and the optional additional information which is not
1055 * defined in the standard, and may be a string or non-human-
1057 * If you return 0 lws will echo the close and then close the
1058 * connection. If you return nonzero lws will just close the
1062 lws_callback_function(struct lws *wsi, enum lws_callback_reasons reason,
1063 void *user, void *in, size_t len);
1066 * typedef lws_extension_callback_function() - Hooks to allow extensions to operate
1067 * @context: Websockets context
1068 * @ext: This extension
1069 * @wsi: Opaque websocket instance pointer
1070 * @reason: The reason for the call
1071 * @user: Pointer to ptr to per-session user data allocated by library
1072 * @in: Pointer used for some callback reasons
1073 * @len: Length set for some callback reasons
1075 * Each extension that is active on a particular connection receives
1076 * callbacks during the connection lifetime to allow the extension to
1077 * operate on websocket data and manage itself.
1079 * Libwebsockets takes care of allocating and freeing "user" memory for
1080 * each active extension on each connection. That is what is pointed to
1081 * by the @user parameter.
1083 * LWS_EXT_CB_CONSTRUCT: called when the server has decided to
1084 * select this extension from the list provided by the client,
1085 * just before the server will send back the handshake accepting
1086 * the connection with this extension active. This gives the
1087 * extension a chance to initialize its connection context found
1090 * LWS_EXT_CB_CLIENT_CONSTRUCT: same as LWS_EXT_CB_CONSTRUCT
1091 * but called when client is instantiating this extension. Some
1092 * extensions will work the same on client and server side and then
1093 * you can just merge handlers for both CONSTRUCTS.
1095 * LWS_EXT_CB_DESTROY: called when the connection the extension was
1096 * being used on is about to be closed and deallocated. It's the
1097 * last chance for the extension to deallocate anything it has
1098 * allocated in the user data (pointed to by @user) before the
1099 * user data is deleted. This same callback is used whether you
1100 * are in client or server instantiation context.
1102 * LWS_EXT_CB_PACKET_RX_PREPARSE: when this extension was active on
1103 * a connection, and a packet of data arrived at the connection,
1104 * it is passed to this callback to give the extension a chance to
1105 * change the data, eg, decompress it. @user is pointing to the
1106 * extension's private connection context data, @in is pointing
1107 * to an lws_tokens struct, it consists of a char * pointer called
1108 * token, and an int called token_len. At entry, these are
1109 * set to point to the received buffer and set to the content
1110 * length. If the extension will grow the content, it should use
1111 * a new buffer allocated in its private user context data and
1112 * set the pointed-to lws_tokens members to point to its buffer.
1114 * LWS_EXT_CB_PACKET_TX_PRESEND: this works the same way as
1115 * LWS_EXT_CB_PACKET_RX_PREPARSE above, except it gives the
1116 * extension a chance to change websocket data just before it will
1117 * be sent out. Using the same lws_token pointer scheme in @in,
1118 * the extension can change the buffer and the length to be
1119 * transmitted how it likes. Again if it wants to grow the
1120 * buffer safely, it should copy the data into its own buffer and
1121 * set the lws_tokens token pointer to it.
1123 * LWS_EXT_CB_ARGS_VALIDATE:
1126 lws_extension_callback_function(struct lws_context *context,
1127 const struct lws_extension *ext, struct lws *wsi,
1128 enum lws_extension_callback_reasons reason,
1129 void *user, void *in, size_t len);
1132 * struct lws_protocols - List of protocols and handlers server
1134 * @name: Protocol name that must match the one given in the client
1135 * Javascript new WebSocket(url, 'protocol') name.
1136 * @callback: The service callback used for this protocol. It allows the
1137 * service action for an entire protocol to be encapsulated in
1138 * the protocol-specific callback
1139 * @per_session_data_size: Each new connection using this protocol gets
1140 * this much memory allocated on connection establishment and
1141 * freed on connection takedown. A pointer to this per-connection
1142 * allocation is passed into the callback in the 'user' parameter
1143 * @rx_buffer_size: if you want atomic frames delivered to the callback, you
1144 * should set this to the size of the biggest legal frame that
1145 * you support. If the frame size is exceeded, there is no
1146 * error, but the buffer will spill to the user callback when
1147 * full, which you can detect by using
1148 * lws_remaining_packet_payload(). Notice that you
1149 * just talk about frame size here, the LWS_PRE
1150 * and post-padding are automatically also allocated on top.
1151 * @id: ignored by lws, but useful to contain user information bound
1152 * to the selected protocol. For example if this protocol was
1153 * called "myprotocol-v2", you might set id to 2, and the user
1154 * code that acts differently according to the version can do so by
1155 * switch (wsi->protocol->id), user code might use some bits as
1156 * capability flags based on selected protocol version, etc.
1157 * @user: User provided context data at the protocol level.
1158 * Accessible via lws_get_protocol(wsi)->user
1159 * This should not be confused with wsi->user, it is not the same.
1160 * The library completely ignores any value in here.
1162 * This structure represents one protocol supported by the server. An
1163 * array of these structures is passed to lws_create_server()
1164 * allows as many protocols as you like to be handled by one server.
1166 * The first protocol given has its callback used for user callbacks when
1167 * there is no agreed protocol name, that's true during HTTP part of the
1168 * connection and true if the client did not send a Protocol: header.
1171 struct lws_protocols {
1173 lws_callback_function *callback;
1174 size_t per_session_data_size;
1175 size_t rx_buffer_size;
1179 /* Add new things just above here ---^
1180 * This is part of the ABI, don't needlessly break compatibilty */
1183 enum lws_ext_options_types {
1188 /* Add new things just above here ---^
1189 * This is part of the ABI, don't needlessly break compatibilty */
1193 * struct lws_ext_options - Option arguments to the extension. These are
1194 * used in the negotiation at ws upgrade time.
1195 * The helper function lws_ext_parse_options()
1196 * uses these to generate callbacks
1198 * @name: Option name, eg, "server_no_context_takeover"
1199 * @type: What kind of args the option can take
1201 struct lws_ext_options {
1203 enum lws_ext_options_types type;
1205 /* Add new things just above here ---^
1206 * This is part of the ABI, don't needlessly break compatibilty */
1209 struct lws_ext_option_arg {
1216 * struct lws_extension - An extension we know how to cope with
1218 * @name: Formal extension name, eg, "permessage-deflate"
1219 * @callback: Service callback
1220 * @client_offer: String containing exts and options client offers
1223 struct lws_extension {
1225 lws_extension_callback_function *callback;
1226 const char *client_offer;
1228 /* Add new things just above here ---^
1229 * This is part of the ABI, don't needlessly break compatibilty */
1233 * The internal exts are part of the public abi
1234 * If we add more extensions, publish the callback here
1237 extern int lws_extension_callback_pm_deflate(
1238 struct lws_context *context, const struct lws_extension *ext,
1239 struct lws *wsi, enum lws_extension_callback_reasons reason,
1240 void *user, void *in, size_t len);
1244 * struct lws_context_creation_info - parameters to create context with
1246 * @port: Port to listen on... you can use CONTEXT_PORT_NO_LISTEN to
1247 * suppress listening on any port, that's what you want if you are
1248 * not running a websocket server at all but just using it as a
1250 * @iface: NULL to bind the listen socket to all interfaces, or the
1251 * interface name, eg, "eth2"
1252 * @protocols: Array of structures listing supported protocols and a protocol-
1253 * specific callback for each one. The list is ended with an
1254 * entry that has a NULL callback pointer.
1255 * It's not const because we write the owning_server member
1256 * @extensions: NULL or array of lws_extension structs listing the
1257 * extensions this context supports. If you configured with
1258 * --without-extensions, you should give NULL here.
1259 * @token_limits: NULL or struct lws_token_limits pointer which is initialized
1260 * with a token length limit for each possible WSI_TOKEN_***
1261 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
1262 * to listen using SSL, set to the filepath to fetch the
1263 * server cert from, otherwise NULL for unencrypted
1264 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode;
1265 * if this is set to NULL but sll_cert_filepath is set, the
1266 * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called
1267 * to allow setting of the private key directly via openSSL
1269 * @ssl_ca_filepath: CA certificate filepath or NULL
1270 * @ssl_cipher_list: List of valid ciphers to use (eg,
1271 * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
1272 * or you can leave it as NULL to get "DEFAULT"
1273 * @http_proxy_address: If non-NULL, attempts to proxy via the given address.
1274 * If proxy auth is required, use format
1275 * "username:password@server:port"
1276 * @http_proxy_port: If http_proxy_address was non-NULL, uses this port at
1278 * @gid: group id to change to after setting listen socket, or -1.
1279 * @uid: user id to change to after setting listen socket, or -1.
1280 * @options: 0, or LWS_SERVER_OPTION_... bitfields
1281 * @user: optional user pointer that can be recovered via the context
1282 * pointer using lws_context_user
1283 * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to
1284 * all libwebsocket sockets, client or server
1285 * @ka_probes: if ka_time was nonzero, after the timeout expires how many
1286 * times to try to get a response from the peer before giving up
1287 * and killing the connection
1288 * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes
1290 * @provided_client_ssl_ctx: If non-null, swap out libwebsockets ssl
1291 * implementation for the one provided by provided_ssl_ctx.
1292 * Libwebsockets no longer is responsible for freeing the context
1293 * if this option is selected.
1294 * @max_http_header_data: The max amount of header payload that can be handled
1295 * in an http request (unrecognized header payload is dropped)
1296 * @max_http_header_pool: The max number of connections with http headers that
1297 * can be processed simultaneously (the corresponding memory is
1298 * allocated for the lifetime of the context). If the pool is
1299 * busy new incoming connections must wait for accept until one
1301 * @count_threads: how many contexts to create in an array, 0 = 1
1302 * @fd_limit_per_thread: nonzero means restrict each service thread to this
1303 * many fds, 0 means the default which is divide the process fd
1304 * limit by the number of threads.
1308 struct lws_context_creation_info {
1311 const struct lws_protocols *protocols;
1312 const struct lws_extension *extensions;
1313 const struct lws_token_limits *token_limits;
1314 const char *ssl_private_key_password;
1315 const char *ssl_cert_filepath;
1316 const char *ssl_private_key_filepath;
1317 const char *ssl_ca_filepath;
1318 const char *ssl_cipher_list;
1319 const char *http_proxy_address;
1320 unsigned int http_proxy_port;
1323 unsigned int options;
1328 #ifdef LWS_OPENSSL_SUPPORT
1329 SSL_CTX *provided_client_ssl_ctx;
1330 #else /* maintain structure layout either way */
1331 void *provided_client_ssl_ctx;
1334 short max_http_header_data;
1335 short max_http_header_pool;
1337 unsigned int count_threads;
1338 unsigned int fd_limit_per_thread;
1340 /* Add new things just above here ---^
1341 * This is part of the ABI, don't needlessly break compatibility
1343 * The below is to ensure later library versions with new
1344 * members added above will see 0 (default) even if the app
1345 * was not built against the newer headers.
1352 * struct lws_client_connect_info - parameters to connect with when using
1353 * lws_client_connect_via_info()
1355 * @context: lws context to create connection in
1356 * @address: remote address to connect to
1357 * @port: remote port to connect to
1358 * @ssl_connection: nonzero for ssl
1360 * @host: content of host header
1361 * @origin: content of origin header
1362 * @protocol: list of ws protocols
1363 * @ietf_version_or_minus_one: currently leave at 0 or -1
1364 * @userdata: if non-NULL, use this as wsi user_data instead of malloc it
1365 * @client_exts: array of extensions that may be used on connection
1368 struct lws_client_connect_info {
1369 struct lws_context *context;
1370 const char *address;
1376 const char *protocol;
1377 int ietf_version_or_minus_one;
1379 const struct lws_extension *client_exts;
1381 /* Add new things just above here ---^
1382 * This is part of the ABI, don't needlessly break compatibility
1384 * The below is to ensure later library versions with new
1385 * members added above will see 0 (default) even if the app
1386 * was not built against the newer headers.
1392 LWS_VISIBLE LWS_EXTERN void
1393 lws_set_log_level(int level,
1394 void (*log_emit_function)(int level, const char *line));
1396 LWS_VISIBLE LWS_EXTERN void
1397 lwsl_emit_syslog(int level, const char *line);
1399 LWS_VISIBLE LWS_EXTERN struct lws_context *
1400 lws_create_context(struct lws_context_creation_info *info);
1402 LWS_VISIBLE LWS_EXTERN int
1403 lws_set_proxy(struct lws_context *context, const char *proxy);
1405 LWS_VISIBLE LWS_EXTERN void
1406 lws_context_destroy(struct lws_context *context);
1408 LWS_VISIBLE LWS_EXTERN int
1409 lws_service(struct lws_context *context, int timeout_ms);
1411 LWS_VISIBLE LWS_EXTERN int
1412 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1414 LWS_VISIBLE LWS_EXTERN void
1415 lws_cancel_service_pt(struct lws *wsi);
1417 LWS_VISIBLE LWS_EXTERN void
1418 lws_cancel_service(struct lws_context *context);
1420 LWS_VISIBLE LWS_EXTERN int
1421 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1423 LWS_VISIBLE LWS_EXTERN const unsigned char *
1424 lws_token_to_string(enum lws_token_indexes token);
1426 LWS_VISIBLE LWS_EXTERN int
1427 lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
1428 const unsigned char *value, int length,
1429 unsigned char **p, unsigned char *end);
1430 LWS_VISIBLE LWS_EXTERN int
1431 lws_finalize_http_header(struct lws *wsi, unsigned char **p,
1432 unsigned char *end);
1433 LWS_VISIBLE LWS_EXTERN int
1434 lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
1435 const unsigned char *value, int length,
1436 unsigned char **p, unsigned char *end);
1437 LWS_VISIBLE LWS_EXTERN int
1438 lws_add_http_header_content_length(struct lws *wsi,
1439 unsigned long content_length,
1440 unsigned char **p, unsigned char *end);
1441 LWS_VISIBLE LWS_EXTERN int
1442 lws_add_http_header_status(struct lws *wsi,
1443 unsigned int code, unsigned char **p,
1444 unsigned char *end);
1446 LWS_VISIBLE LWS_EXTERN int
1447 lws_http_transaction_completed(struct lws *wsi);
1449 #ifdef LWS_USE_LIBEV
1450 typedef void (lws_ev_signal_cb)(EV_P_ struct ev_signal *w, int revents);
1452 LWS_VISIBLE LWS_EXTERN int
1453 lws_sigint_cfg(struct lws_context *context, int use_ev_sigint,
1454 lws_ev_signal_cb *cb);
1456 LWS_VISIBLE LWS_EXTERN int
1457 lws_initloop(struct lws_context *context, struct ev_loop *loop);
1460 lws_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents);
1461 #endif /* LWS_USE_LIBEV */
1463 LWS_VISIBLE LWS_EXTERN int
1464 lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd);
1466 LWS_VISIBLE LWS_EXTERN int
1467 lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd,
1470 LWS_VISIBLE LWS_EXTERN void *
1471 lws_context_user(struct lws_context *context);
1473 LWS_VISIBLE LWS_EXTERN void *
1474 lws_wsi_user(struct lws *wsi);
1477 * NOTE: These public enums are part of the abi. If you want to add one,
1478 * add it at where specified so existing users are unaffected.
1480 enum pending_timeout {
1481 NO_PENDING_TIMEOUT = 0,
1482 PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE = 1,
1483 PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE = 2,
1484 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER = 3,
1485 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE = 4,
1486 PENDING_TIMEOUT_AWAITING_PING = 5,
1487 PENDING_TIMEOUT_CLOSE_ACK = 6,
1488 PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE = 7,
1489 PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE = 8,
1490 PENDING_TIMEOUT_SSL_ACCEPT = 9,
1491 PENDING_TIMEOUT_HTTP_CONTENT = 10,
1492 PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND = 11,
1493 PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE = 12,
1495 /****** add new things just above ---^ ******/
1498 LWS_VISIBLE LWS_EXTERN void
1499 lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
1504 * When sending with websocket protocol
1508 * LWS_WRITE_CONTINUATION,
1512 * the send buffer has to have LWS_PRE bytes valid BEFORE
1513 * the buffer pointer you pass to lws_write().
1515 * This allows us to add protocol info before and after the data, and send as
1516 * one packet on the network without payload copying, for maximum efficiency.
1518 * So for example you need this kind of code to use lws_write with a
1521 * char buf[LWS_PRE + 128];
1523 * // fill your part of the buffer... for example here it's all zeros
1524 * memset(&buf[LWS_PRE], 0, 128);
1526 * lws_write(wsi, &buf[LWS_PRE], 128, LWS_WRITE_TEXT);
1528 * When sending HTTP, with
1531 * LWS_WRITE_HTTP_HEADERS
1532 * LWS_WRITE_HTTP_FINAL
1534 * there is no protocol data prepended, and don't need to take care about the
1535 * LWS_PRE bytes valid before the buffer pointer.
1537 * LWS_PRE is at least the frame nonce + 2 header + 8 length
1538 * LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off.
1539 * The example apps no longer use it.
1541 * Pad LWS_PRE to the CPU word size, so that word references
1542 * to the address immediately after the padding won't cause an unaligned access
1543 * error. Sometimes for performance reasons the recommended padding is even
1544 * larger than sizeof(void *).
1547 #if !defined(LWS_SIZEOFPTR)
1548 #define LWS_SIZEOFPTR (sizeof (void *))
1550 #if !defined(u_int64_t)
1551 #define u_int64_t unsigned long long
1555 #define _LWS_PAD_SIZE 16 /* Intel recommended for best performance */
1557 #define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target arch */
1559 #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \
1560 ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
1561 #define LWS_PRE _LWS_PAD(4 + 10)
1562 /* used prior to 1.7 and retained for backward compatibility */
1563 #define LWS_SEND_BUFFER_PRE_PADDING LWS_PRE
1564 #define LWS_SEND_BUFFER_POST_PADDING 0
1566 LWS_VISIBLE LWS_EXTERN int
1567 lws_write(struct lws *wsi, unsigned char *buf, size_t len,
1568 enum lws_write_protocol protocol);
1571 * lws_close_reason - Set reason and aux data to send with Close packet
1572 * If you are going to return nonzero from the callback
1573 * requesting the connection to close, you can optionally
1574 * call this to set the reason the peer will be told if
1577 * @wsi: The websocket connection to set the close reason on
1578 * @status: A valid close status from websocket standard
1579 * @buf: NULL or buffer containing up to 124 bytes of auxiliary data
1580 * @len: Length of data in @buf to send
1582 LWS_VISIBLE LWS_EXTERN void
1583 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1584 unsigned char *buf, size_t len);
1586 /* helper for case where buffer may be const */
1587 #define lws_write_http(wsi, buf, len) \
1588 lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
1590 LWS_VISIBLE LWS_EXTERN int
1591 lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
1592 const char *other_headers, int other_headers_len);
1593 LWS_VISIBLE LWS_EXTERN int
1594 lws_serve_http_file_fragment(struct lws *wsi);
1596 LWS_VISIBLE LWS_EXTERN int
1597 lws_return_http_status(struct lws *wsi, unsigned int code,
1598 const char *html_body);
1600 LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
1601 lws_get_protocol(struct lws *wsi);
1603 LWS_VISIBLE LWS_EXTERN int
1604 lws_callback_on_writable(struct lws *wsi);
1606 LWS_VISIBLE LWS_EXTERN int
1607 lws_callback_on_writable_all_protocol(const struct lws_context *context,
1608 const struct lws_protocols *protocol);
1610 LWS_VISIBLE LWS_EXTERN int
1611 lws_callback_all_protocol(struct lws_context *context,
1612 const struct lws_protocols *protocol, int reason);
1614 LWS_VISIBLE LWS_EXTERN int
1615 lws_get_socket_fd(struct lws *wsi);
1617 LWS_VISIBLE LWS_EXTERN int
1618 lws_is_final_fragment(struct lws *wsi);
1620 LWS_VISIBLE LWS_EXTERN unsigned char
1621 lws_get_reserved_bits(struct lws *wsi);
1623 LWS_VISIBLE LWS_EXTERN int
1624 lws_rx_flow_control(struct lws *wsi, int enable);
1626 LWS_VISIBLE LWS_EXTERN void
1627 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
1628 const struct lws_protocols *protocol);
1630 LWS_VISIBLE LWS_EXTERN size_t
1631 lws_remaining_packet_payload(struct lws *wsi);
1634 * if the protocol does not have any guidance, returns -1. Currently only
1635 * http2 connections get send window information from this API. But your code
1636 * should use it so it can work properly with any protocol.
1638 * If nonzero return is the amount of payload data the peer or intermediary has
1639 * reported it has buffer space for. That has NO relationship with the amount
1640 * of buffer space your OS can accept on this connection for a write action.
1642 * This number represents the maximum you could send to the peer or intermediary
1643 * on this connection right now without it complaining.
1645 * lws manages accounting for send window updates and payload writes
1646 * automatically, so this number reflects the situation at the peer or
1647 * intermediary dynamically.
1649 LWS_VISIBLE LWS_EXTERN size_t
1650 lws_get_peer_write_allowance(struct lws *wsi);
1652 /* deprecated, use lws_client_connect_via_info() */
1653 LWS_VISIBLE LWS_EXTERN struct lws *
1654 lws_client_connect(struct lws_context *clients, const char *address,
1655 int port, int ssl_connection, const char *path,
1656 const char *host, const char *origin, const char *protocol,
1657 int ietf_version_or_minus_one);
1658 /* deprecated, use lws_client_connect_via_info() */
1659 LWS_VISIBLE LWS_EXTERN struct lws *
1660 lws_client_connect_extended(struct lws_context *clients, const char *address,
1661 int port, int ssl_connection, const char *path,
1662 const char *host, const char *origin,
1663 const char *protocol, int ietf_version_or_minus_one,
1666 LWS_VISIBLE LWS_EXTERN struct lws *
1667 lws_client_connect_via_info(struct lws_client_connect_info * ccinfo);
1669 LWS_VISIBLE LWS_EXTERN const char *
1670 lws_canonical_hostname(struct lws_context *context);
1673 LWS_VISIBLE LWS_EXTERN void
1674 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
1675 int name_len, char *rip, int rip_len);
1677 LWS_VISIBLE LWS_EXTERN int
1678 lws_get_random(struct lws_context *context, void *buf, int len);
1680 LWS_VISIBLE LWS_EXTERN int
1681 lws_daemonize(const char *_lock_path);
1683 LWS_VISIBLE LWS_EXTERN int
1684 lws_send_pipe_choked(struct lws *wsi);
1686 LWS_VISIBLE LWS_EXTERN int
1687 lws_partial_buffered(struct lws *wsi);
1689 LWS_VISIBLE LWS_EXTERN int
1690 lws_frame_is_binary(struct lws *wsi);
1692 LWS_VISIBLE LWS_EXTERN int
1693 lws_is_ssl(struct lws *wsi);
1694 #ifdef LWS_SHA1_USE_OPENSSL_NAME
1695 #define lws_SHA1 SHA1
1697 LWS_VISIBLE LWS_EXTERN unsigned char *
1698 lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
1701 LWS_VISIBLE LWS_EXTERN int
1702 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
1704 LWS_VISIBLE LWS_EXTERN int
1705 lws_b64_decode_string(const char *in, char *out, int out_size);
1707 LWS_VISIBLE LWS_EXTERN const char *
1708 lws_get_library_version(void);
1710 LWS_VISIBLE LWS_EXTERN int
1711 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
1715 * Access to http headers
1717 * In lws the client http headers are temporarily malloc'd only for the
1718 * duration of the http part of the handshake. It's because in most cases,
1719 * the header content is ignored for the whole rest of the connection lifetime
1720 * and would then just be taking up space needlessly.
1722 * During LWS_CALLBACK_HTTP when the URI path is delivered is the last time
1723 * the http headers are still allocated, you can use these apis then to
1724 * look at and copy out interesting header content (cookies, etc)
1726 * Notice that the header total length reported does not include a terminating
1727 * '\0', however you must allocate for it when using the _copy apis. So the
1728 * length reported for a header containing "123" is 3, but you must provide
1729 * a buffer of length 4 so that "123\0" may be copied into it, or the copy
1730 * will fail with a nonzero return code.
1733 LWS_VISIBLE LWS_EXTERN int
1734 lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
1736 LWS_VISIBLE LWS_EXTERN int
1737 lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx);
1740 * copies the whole, aggregated header, even if it was delivered in
1741 * several actual headers piece by piece
1743 LWS_VISIBLE LWS_EXTERN int
1744 lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
1747 * copies only fragment frag_idx of a header. Normally this is only useful
1748 * to parse URI arguments like ?x=1&y=2, oken index WSI_TOKEN_HTTP_URI_ARGS
1749 * fragment 0 will contain "x=1" and fragment 1 "y=2"
1751 LWS_VISIBLE LWS_EXTERN int
1752 lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
1753 enum lws_token_indexes h, int frag_idx);
1756 /* get the active file operations struct */
1757 LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops *
1758 lws_get_fops(struct lws_context *context);
1760 LWS_VISIBLE LWS_EXTERN struct lws_context *
1761 lws_get_context(const struct lws *wsi);
1763 LWS_VISIBLE LWS_EXTERN int
1764 lws_get_count_threads(struct lws_context *context);
1767 * Wsi-associated File Operations access helpers
1769 * Use these helper functions if you want to access a file from the perspective
1770 * of a specific wsi, which is usually the case. If you just want contextless
1771 * file access, use the fops callbacks directly with NULL wsi instead of these
1774 * If so, then it calls the platform handler or user overrides where present
1775 * (as defined in info->fops)
1777 * The advantage from all this is user code can be portable for file operations
1778 * without having to deal with differences between platforms.
1781 static LWS_INLINE lws_filefd_type
1782 lws_plat_file_open(struct lws *wsi, const char *filename,
1783 unsigned long *filelen, int flags)
1785 return lws_get_fops(lws_get_context(wsi))->open(wsi, filename,
1789 static LWS_INLINE int
1790 lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
1792 return lws_get_fops(lws_get_context(wsi))->close(wsi, fd);
1795 static LWS_INLINE unsigned long
1796 lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
1798 return lws_get_fops(lws_get_context(wsi))->seek_cur(wsi, fd, offset);
1801 static LWS_INLINE int
1802 lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
1803 unsigned char *buf, unsigned long len)
1805 return lws_get_fops(lws_get_context(wsi))->read(wsi, fd, amount, buf,
1809 static LWS_INLINE int
1810 lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
1811 unsigned char *buf, unsigned long len)
1813 return lws_get_fops(lws_get_context(wsi))->write(wsi, fd, amount, buf,
1818 * Note: this is not normally needed as a user api. It's provided in case it is
1819 * useful when integrating with other app poll loop service code.
1821 LWS_VISIBLE LWS_EXTERN int
1822 lws_read(struct lws *wsi, unsigned char *buf, size_t len);
1824 #ifndef LWS_NO_EXTENSIONS
1826 #define lws_get_internal_extensions() NULL
1827 LWS_VISIBLE LWS_EXTERN int
1828 lws_ext_parse_options(const struct lws_extension *ext, struct lws *wsi,
1829 void *ext_user, const struct lws_ext_options *opts, const char *o, int len);
1833 * custom allocator support
1835 LWS_VISIBLE LWS_EXTERN void
1836 lws_set_allocator(void *(*realloc)(void *ptr, size_t size));