#else
#if defined(WIN32) || defined(_WIN32)
#define inline __inline
-#include <tchar.h>
-#include <mstcpip.h>
-#ifdef _WIN32_WCE
-#define vsnprintf _vsnprintf
-#endif
-
#else /* not WIN32 */
#include "config.h"
#define compatible_close(fd) closesocket(fd)
#define compatible_file_close(fd) CloseHandle(fd)
-#define compatible_file_seek_cur(fd, offset) SetFilePointer(fd, offset, FILE_CURRENT)
+#define compatible_file_seek_cur(fd, offset) SetFilePointer(fd, offset, NULL, FILE_CURRENT)
#define compatible_file_read(amount, fd, buf, len) {\
DWORD _amount; \
- if (!ReadFile(fd, buf, len, &amount, NULL)) \
+ if (!ReadFile(fd, buf, len, &_amount, NULL)) \
amount = -1; \
else \
amount = _amount; \
#define lws_set_blocking_send(wsi) wsi->sock_send_blocking = TRUE
#include <winsock2.h>
#include <windows.h>
+#include <tchar.h>
+#ifdef HAVE_IN6ADDR_H
+#include <in6addr.h>
+#endif
+#include <mstcpip.h>
+
+#ifndef __func__
+#define __func__ __FUNCTION__
+#endif
+
+#ifdef _WIN32_WCE
+#define vsnprintf _vsnprintf
+#endif
+
#define LWS_INVALID_FILE INVALID_HANDLE_VALUE
-const char *inet_ntop(int af, const void *src, char *dst, int cnt);
#else /* not windows --> */
#include <errno.h>
#include <fcntl.h>
#else
#include <ifaddrs.h>
#endif
+#if defined (__ANDROID__)
+#include <syslog.h>
+#else
#include <sys/syslog.h>
+#endif
#include <sys/un.h>
#include <sys/socket.h>
#include <netdb.h>
#ifdef LWS_OPENSSL_SUPPORT
#ifdef USE_CYASSL
#include <cyassl/openssl/ssl.h>
-#include <cyassl/error.h>
-unsigned char *
-SHA1(const unsigned char *d, size_t n, unsigned char *md);
+#include <cyassl/error-ssl.h>
#else
#include <openssl/ssl.h>
#include <openssl/evp.h>
#include <endian.h>
#endif
+#if defined(__QNX__)
+ #include <gulliver.h>
+ #if defined(__LITTLEENDIAN__)
+ #define BYTE_ORDER __LITTLEENDIAN__
+ #define LITTLE_ENDIAN __LITTLEENDIAN__
+ #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
+ #endif
+ #if defined(__BIGENDIAN__)
+ #define BYTE_ORDER __BIGENDIAN__
+ #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
+ #define BIG_ENDIAN __BIGENDIAN__
+ #endif
+#endif
+
#if !defined(BYTE_ORDER)
# define BYTE_ORDER __BYTE_ORDER
#endif
WSI_STATE_CLIENT_UNCONNECTED,
WSI_STATE_RETURNED_CLOSE_ALREADY,
WSI_STATE_AWAITING_CLOSE_ACK,
+ WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE,
+
+ WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE,
+ WSI_STATE_HTTP2_ESTABLISHED_PRE_SETTINGS,
+ WSI_STATE_HTTP2_ESTABLISHED,
+};
+
+enum http_version {
+ HTTP_VERSION_1_0,
+ HTTP_VERSION_1_1,
+};
+
+enum http_connection_type {
+ HTTP_CONNECTION_CLOSE,
+ HTTP_CONNECTION_KEEP_ALIVE
+};
+
+enum lws_pending_protocol_send {
+ LWS_PPS_NONE,
+ LWS_PPS_HTTP2_MY_SETTINGS,
+ LWS_PPS_HTTP2_ACK_SETTINGS,
};
enum lws_rx_parse_state {
LWS_CONNMODE_WS_SERVING,
LWS_CONNMODE_WS_CLIENT,
+
+ LWS_CONNMODE_HTTP2_SERVING,
/* transient, ssl delay hiding */
LWS_CONNMODE_SSL_ACK_PENDING,
LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
+ LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2,
LWS_CONNMODE_WS_CLIENT_WAITING_SSL,
LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
int allow_non_ssl_on_ssl_port;
SSL_CTX *ssl_ctx;
SSL_CTX *ssl_client_ctx;
+ unsigned int ssl_flag_buffered_reads:1;
#endif
struct libwebsocket_protocols *protocols;
int count_protocols;
#ifndef LWS_NO_EXTENSIONS
struct libwebsocket_extension *extensions;
#endif
+ struct lws_token_limits *token_limits;
void *user_space;
};
+enum {
+ LWS_EV_READ = (1 << 0),
+ LWS_EV_WRITE = (1 << 1),
+ LWS_EV_START = (1 << 2),
+ LWS_EV_STOP = (1 << 3),
+};
+
#ifdef LWS_USE_LIBEV
#define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV)
+LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
+LWS_EXTERN void
+lws_libev_accept(struct libwebsocket_context *context,
+ struct libwebsocket *new_wsi, int accept_fd);
+LWS_EXTERN void
+lws_libev_io(struct libwebsocket_context *context,
+ struct libwebsocket *wsi, int flags);
+LWS_EXTERN int
+lws_libev_init_fd_table(struct libwebsocket_context *context);
+LWS_EXTERN void
+lws_libev_run(struct libwebsocket_context *context);
#else
#define LWS_LIBEV_ENABLED(context) (0)
+#define lws_feature_status_libev(_a) \
+ lwsl_notice("libev support not compiled in\n")
+#define lws_libev_accept(_a, _b, _c) ((void) 0)
+#define lws_libev_io(_a, _b, _c) ((void) 0)
+#define lws_libev_init_fd_table(_a) (0)
+#define lws_libev_run(_a) ((void) 0)
#endif
#ifdef LWS_USE_IPV6
unsigned char next_frag_index;
};
+/* notice that these union members:
+ *
+ * hdr
+ * http
+ * http2
+ *
+ * all have a pointer to allocated_headers struct as their first member.
+ *
+ * It means for allocated_headers access, the three union paths can all be
+ * used interchangably to access the same data
+ */
+
struct allocated_headers {
unsigned short next_frag_index;
unsigned short pos;
};
struct _lws_http_mode_related {
+ /* MUST be first in struct */
struct allocated_headers *ah; /* mirroring _lws_header_related */
#if defined(WIN32) || defined(_WIN32)
HANDLE fd;
unsigned long filepos;
unsigned long filelen;
+ enum http_version request_version;
+ enum http_connection_type connection_type;
int content_length;
- int content_length_seen;
- int body_index;
- unsigned char *post_buffer;
+ int content_remain;
};
+#ifdef LWS_USE_HTTP2
+
+enum lws_http2_settings {
+ LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
+ LWS_HTTP2_SETTINGS__ENABLE_PUSH,
+ LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
+ LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
+ LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
+ LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
+
+ LWS_HTTP2_SETTINGS__COUNT /* always last */
+};
+
+enum lws_http2_wellknown_frame_types {
+ LWS_HTTP2_FRAME_TYPE_DATA,
+ LWS_HTTP2_FRAME_TYPE_HEADERS,
+ LWS_HTTP2_FRAME_TYPE_PRIORITY,
+ LWS_HTTP2_FRAME_TYPE_RST_STREAM,
+ LWS_HTTP2_FRAME_TYPE_SETTINGS,
+ LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
+ LWS_HTTP2_FRAME_TYPE_PING,
+ LWS_HTTP2_FRAME_TYPE_GOAWAY,
+ LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
+ LWS_HTTP2_FRAME_TYPE_CONTINUATION,
+
+ LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
+};
+
+#define LWS_HTTP2_STREAM_ID_MASTER 0
+#define LWS_HTTP2_FRAME_HEADER_LENGTH 9
+#define LWS_HTTP2_SETTINGS_LENGTH 6
+
+struct http2_settings {
+ unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
+};
+
+struct _lws_http2_related {
+ /*
+ * having this first lets us also re-use all HTTP union code
+ * and in turn, http_mode_related has allocated headers in right
+ * place so we can use the header apis on the wsi directly still
+ */
+ struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
+
+ struct http2_settings my_settings;
+ struct http2_settings peer_settings;
+
+ struct libwebsocket *parent_wsi;
+ struct libwebsocket *next_child_wsi;
+
+ unsigned int count;
+
+ /* frame */
+ unsigned int length;
+ unsigned int stream_id;
+ struct libwebsocket *stream_wsi;
+ unsigned char type;
+ unsigned char flags;
+ unsigned char frame_state;
+
+ unsigned int tx_credit;
+ unsigned int my_stream_id;
+ unsigned int child_count;
+ int my_priority;
+ unsigned char initialized;
+ unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
+};
+
+#define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->parent_wsi)
+
+#endif
+
struct _lws_header_related {
+ /* MUST be first in struct */
struct allocated_headers *ah;
short lextable_pos;
+ unsigned short current_token_limit;
unsigned char parser_state; /* enum lws_token_indexes */
enum uri_path_states ups;
enum uri_esc_states ues;
unsigned int frame_is_binary:1;
unsigned int all_zero_nonce:1;
short close_reason; /* enum lws_close_status */
- unsigned char *rxflow_buffer;
- int rxflow_len;
- int rxflow_pos;
- unsigned int rxflow_change_to:2;
+
unsigned int this_frame_masked:1;
unsigned int inside_frame:1; /* next write will be more of frame */
unsigned int clean_buffer:1; /* buffer not rewritten by extension */
+
+ unsigned char *ping_payload_buf; /* non-NULL if malloc'd */
+ unsigned int ping_payload_alloc; /* length malloc'd */
+ unsigned int ping_payload_len; /* nonzero if PONG pending */
};
struct libwebsocket {
unsigned int extension_data_pending:1;
#endif
unsigned char ietf_spec_revision;
+ enum lws_pending_protocol_send pps;
char mode; /* enum connection_mode */
char state; /* enum lws_connection_states */
char rx_frame_type; /* enum libwebsocket_write_protocol */
unsigned int hdr_parsing_completed:1;
+ unsigned int user_space_externally_allocated:1;
char pending_timeout; /* enum pending_timeout */
time_t pending_timeout_limit;
unsigned long action_start;
unsigned long latency_start;
#endif
+ /* rxflow handling */
+ unsigned char *rxflow_buffer;
+ int rxflow_len;
+ int rxflow_pos;
+ unsigned int rxflow_change_to:2;
/* truncated send handling */
unsigned char *truncated_send_malloc; /* non-NULL means buffering in progress */
union u {
struct _lws_http_mode_related http;
+#ifdef LWS_USE_HTTP2
+ struct _lws_http2_related http2;
+#endif
struct _lws_header_related hdr;
struct _lws_websocket_related ws;
} u;
SSL *ssl;
BIO *client_bio;
unsigned int use_ssl:2;
+ unsigned int buffered_reads_pending:1;
#endif
#ifdef _WIN32
LWS_EXTERN int
remove_wsi_socket_from_fds(struct libwebsocket_context *context,
struct libwebsocket *wsi);
+LWS_EXTERN int
+lws_rxflow_cache(struct libwebsocket *wsi, unsigned char *buf, int n, int len);
#ifndef LWS_LATENCY
static inline void lws_latency(struct libwebsocket_context *context,
int ret, int completion);
#endif
+LWS_EXTERN void lws_set_protocol_write_pending(struct libwebsocket_context *context,
+ struct libwebsocket *wsi,
+ enum lws_pending_protocol_send pend);
LWS_EXTERN int
libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
LWS_EXTERN int
-libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
+libwebsocket_parse(struct libwebsocket_context *context,
+ struct libwebsocket *wsi, unsigned char c);
LWS_EXTERN int
-libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
- unsigned char *buf, size_t len);
+lws_http_action(struct libwebsocket_context *context, struct libwebsocket *wsi);
LWS_EXTERN int
lws_b64_selftest(void);
LWS_EXTERN int
lws_handle_POLLOUT_event(struct libwebsocket_context *context,
struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd);
+/*
+ * EXTENSIONS
+ */
+
#ifndef LWS_NO_EXTENSIONS
+LWS_VISIBLE void
+lws_context_init_extensions(struct lws_context_creation_info *info,
+ struct libwebsocket_context *context);
LWS_EXTERN int
lws_any_extension_handled(struct libwebsocket_context *context,
struct libwebsocket *wsi,
#define lws_any_extension_handled(_a, _b, _c, _d, _e) (0)
#define lws_ext_callback_for_each_active(_a, _b, _c, _d) (0)
#define lws_ext_callback_for_each_extension_type(_a, _b, _c, _d, _e) (0)
+#define lws_issue_raw_ext_access lws_issue_raw
+#define lws_context_init_extensions(_a, _b)
#endif
LWS_EXTERN int
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason, void *user,
void *in, size_t len);
+#ifdef LWS_USE_HTTP2
+LWS_EXTERN int
+lws_http2_interpret_settings_payload(struct http2_settings *settings, unsigned char *buf, int len);
+LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
+LWS_EXTERN int
+lws_http2_parser(struct libwebsocket_context *context,
+ struct libwebsocket *wsi, unsigned char c);
+LWS_EXTERN int lws_http2_do_pps_send(struct libwebsocket_context *context, struct libwebsocket *wsi);
+LWS_EXTERN int lws_http2_frame_write(struct libwebsocket *wsi, int type, int flags, unsigned int sid, unsigned int len, unsigned char *buf);
+LWS_EXTERN struct libwebsocket *
+lws_http2_wsi_from_id(struct libwebsocket *wsi, unsigned int sid);
+#endif
LWS_EXTERN int
lws_plat_set_socket_options(struct libwebsocket_context *context, int fd);
struct libwebsocket_context *context);
LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
struct libwebsocket *wsi);
+LWS_EXTERN int
+libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
+ unsigned char *buf, size_t len);
+LWS_EXTERN void
+lws_server_get_canonical_hostname(struct libwebsocket_context *context,
+ struct lws_context_creation_info *info);
#else
#define lws_context_init_server(_a, _b) (0)
+#define libwebsocket_interpret_incoming_packet(_a, _b, _c) (0)
+#define lws_server_get_canonical_hostname(_a, _b)
#endif
#ifndef LWS_NO_DAEMONIZE
LWS_EXTERN int get_daemonize_pid();
+#else
+#define get_daemonize_pid() (0)
#endif
LWS_EXTERN int interface_to_sa(struct libwebsocket_context *context,
const char *ifname, struct sockaddr_in *addr, size_t addrlen);
+LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
+
#ifdef _WIN32
LWS_EXTERN HANDLE lws_plat_open_file(const char* filename, unsigned long* filelen);
#else
LWS_EXTERN int lws_plat_open_file(const char* filename, unsigned long* filelen);
#endif
-#ifndef LWS_OPENSSL_SUPPORT
+enum lws_ssl_capable_status {
+ LWS_SSL_CAPABLE_ERROR = -1,
+ LWS_SSL_CAPABLE_MORE_SERVICE = -2,
+};
-unsigned char *
-SHA1(const unsigned char *d, size_t n, unsigned char *md);
+#ifndef LWS_OPENSSL_SUPPORT
+#define LWS_SSL_ENABLED(context) (0)
+#define lws_context_init_server_ssl(_a, _b) (0)
+#define lws_ssl_destroy(_a)
+#define lws_context_init_http2_ssl(_a)
+#define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
+#define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
+#define lws_server_socket_service_ssl(_a, _b, _c, _d, _e) (0)
+#define lws_ssl_close(_a) (0)
+#define lws_ssl_context_destroy(_a)
+#else
+#define LWS_SSL_ENABLED(context) (context->use_ssl)
+LWS_EXTERN int openssl_websocket_private_data_index;
+LWS_EXTERN int
+lws_ssl_capable_read(struct libwebsocket_context *context,
+ struct libwebsocket *wsi, unsigned char *buf, int len);
+LWS_EXTERN int
+lws_ssl_capable_write(struct libwebsocket *wsi, unsigned char *buf, int len);
+LWS_EXTERN int
+lws_server_socket_service_ssl(struct libwebsocket_context *context,
+ struct libwebsocket **wsi, struct libwebsocket *new_wsi,
+ int accept_fd, struct libwebsocket_pollfd *pollfd);
+LWS_EXTERN int
+lws_ssl_close(struct libwebsocket *wsi);
+LWS_EXTERN void
+lws_ssl_context_destroy(struct libwebsocket_context *context);
+#ifndef LWS_NO_SERVER
+LWS_EXTERN int
+lws_context_init_server_ssl(struct lws_context_creation_info *info,
+ struct libwebsocket_context *context);
#else
+#define lws_context_init_server_ssl(_a, _b) (0)
+#endif
+LWS_EXTERN void
+lws_ssl_destroy(struct libwebsocket_context *context);
-LWS_EXTERN int openssl_websocket_private_data_index;
+/* HTTP2-related */
+#ifdef LWS_USE_HTTP2
+LWS_EXTERN void
+lws_context_init_http2_ssl(struct libwebsocket_context *context);
+#else
+#define lws_context_init_http2_ssl(_a)
+#endif
#endif
+LWS_EXTERN int
+lws_ssl_capable_read_no_ssl(struct libwebsocket_context *context,
+ struct libwebsocket *wsi, unsigned char *buf, int len);
+
+LWS_EXTERN int
+lws_ssl_capable_write_no_ssl(struct libwebsocket *wsi, unsigned char *buf, int len);
+
#ifndef LWS_NO_CLIENT
LWS_EXTERN int lws_client_socket_service(
struct libwebsocket_context *context,
struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd);
- LWS_EXTERN int lws_context_init_client(struct lws_context_creation_info *info,
+#ifdef LWS_OPENSSL_SUPPORT
+ LWS_EXTERN int lws_context_init_client_ssl(struct lws_context_creation_info *info,
struct libwebsocket_context *context);
+#else
+ #define lws_context_init_client_ssl(_a, _b) (0)
+#endif
LWS_EXTERN int lws_handshake_client(struct libwebsocket *wsi, unsigned char **buf, size_t len);
+ LWS_EXTERN void
+ libwebsockets_decode_ssl_error(void);
#else
-#define lws_context_init_client(_a, _b) (0)
+#define lws_context_init_client_ssl(_a, _b) (0)
#define lws_handshake_client(_a, _b, _c) (0)
#endif
#ifndef LWS_NO_SERVER
LWS_EXTERN int lws_handshake_server(struct libwebsocket_context *context,
struct libwebsocket *wsi, unsigned char **buf, size_t len);
#else
+#define lws_server_socket_service(_a, _b, _c) (0)
#define _libwebsocket_rx_flow_control(_a) (0)
#define lws_handshake_server(_a, _b, _c, _d) (0)
#endif