1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/lib/core/private.h

616 lines
14 KiB
C
Raw Normal View History

/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
CMake support + fixed windows build. - Finalized CMake support (tested on windows only so far). - Uses a generated lws_config.h that is included in private-libwebsocket to pass defines, only used if CMAKE_BUILD is set. - Support for SSL on Windows. - Initial support for CyaSSL replacement of OpenSSL (This has been added to my older CMake-fork but haven't been tested on this version yet). - Fixed windows build (see below for details). - Fixed at least the 32-bit Debug build for the existing Visual Studio Project. (Not to keen fixing all the others when we have CMake support anyway (which can generate much better project files)...) - BUGFIXES: - handshake.c - used C99 definition of handshake_0405 function - libwebsocket.c - syslog not available on windows, put in ifdefs. - Fixed previous known crash bug on Windows where WSAPoll in Ws2_32.dll would not be present, causing the poll function pointer being set to NULL. - Uninitialized variable context->listen_service_extraseen would result in stack overflow because of infinite recursion. Fixed by initializing in libwebsocket_create_context - SO_REUSADDR means something different on Windows compared to Unix. - Setting a socket to nonblocking is done differently on Windows. (This should probably broken out into a helper function instead) - lwsl_emit_syslog -> lwsl_emit_stderr on Windows. - private-libwebsocket.h - PATH_MAX is not available on Windows, define as MAX_PATH - Always define LWS_NO_DAEMONIZE on windows. - Don't define lws_latency as inline that does nothing. inline is not support by the Microsoft compiler, replaced with an empty define instead. (It's __inline in MSVC) - server.c - Fixed nonblock call on windows - test-ping.c - Don't use C99 features (Microsoft compiler does not support it). - Move non-win32 headers into ifdefs. - Skip use of sighandler on Windows. - test-server.c - ifdef syslog parts on Windows.
2013-02-06 15:26:58 +09:00
#include "lws_config.h"
#include "lws_config_private.h"
#if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK) && \
!defined(NO_GNU_SOURCE_THIS_TIME)
#define _GNU_SOURCE
#endif
/*
#if !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200112L
#endif
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-03-30 10:19:23 +02:00
#include <time.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <inttypes.h>
#include <assert.h>
2014-04-07 11:28:08 +02:00
#ifdef LWS_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
2019-01-15 06:59:48 +08:00
#if defined(LWS_HAVE_SYS_STAT_H) && !defined(LWS_PLAT_OPTEE)
#include <sys/stat.h>
2014-09-16 14:05:13 +04:00
#endif
#if LWS_MAX_SMP > 1
#include <pthread.h>
#endif
#ifndef LWS_DEF_HEADER_LEN
#define LWS_DEF_HEADER_LEN 4096
#endif
#ifndef LWS_DEF_HEADER_POOL
#define LWS_DEF_HEADER_POOL 4
#endif
#ifndef LWS_MAX_PROTOCOLS
#define LWS_MAX_PROTOCOLS 5
#endif
#ifndef LWS_MAX_EXTENSIONS_ACTIVE
#define LWS_MAX_EXTENSIONS_ACTIVE 1
#endif
#ifndef LWS_MAX_EXT_OFFERS
#define LWS_MAX_EXT_OFFERS 8
#endif
#ifndef SPEC_LATEST_SUPPORTED
#define SPEC_LATEST_SUPPORTED 13
#endif
#ifndef AWAITING_TIMEOUT
#define AWAITING_TIMEOUT 20
#endif
#ifndef CIPHERS_LIST_STRING
#define CIPHERS_LIST_STRING "DEFAULT"
#endif
#ifndef LWS_SOMAXCONN
#define LWS_SOMAXCONN SOMAXCONN
#endif
#define MAX_WEBSOCKET_04_KEY_LEN 128
#ifndef SYSTEM_RANDOM_FILEPATH
#define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
#endif
#define LWS_H2_RX_SCRATCH_SIZE 512
2018-06-16 08:53:48 -07:00
#define lws_socket_is_valid(x) (x != LWS_SOCK_INVALID)
#ifndef LWS_HAVE_STRERROR
#define strerror(x) ""
#endif
/*
*
* ------ private platform defines ------
*
*/
#if defined(LWS_WITH_ESP32)
#include "plat/esp32/private.h"
#else
#if defined(WIN32) || defined(_WIN32)
#include "plat/windows/private.h"
#else
#if defined(LWS_PLAT_OPTEE)
#include "plat/optee/private.h"
#else
#include "plat/unix/private.h"
#endif
#endif
#endif
/*
*
* ------ public api ------
*
*/
#include "libwebsockets.h"
#include "tls/private.h"
#if defined(WIN32) || defined(_WIN32)
// Visual studio older than 2015 and WIN_CE has only _stricmp
#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#elif !defined(__MINGW32__)
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif
#define getdtablesize() 30000
#endif
#ifndef LWS_ARRAY_SIZE
#define LWS_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__clang__)
#define lws_memory_barrier() __sync_synchronize()
#elif defined(__GNUC__)
#define lws_memory_barrier() __sync_synchronize()
#else
#define lws_memory_barrier()
#endif
struct lws_ring {
void *buf;
void (*destroy_element)(void *element);
uint32_t buflen;
uint32_t element_len;
uint32_t head;
uint32_t oldest_tail;
};
struct lws_protocols;
struct lws;
#if defined(LWS_WITH_NETWORK)
#include "event-libs/private.h"
#endif
struct lws_io_watcher {
#ifdef LWS_WITH_LIBEV
struct lws_io_watcher_libev ev;
#endif
#ifdef LWS_WITH_LIBUV
struct lws_io_watcher_libuv uv;
#endif
#ifdef LWS_WITH_LIBEVENT
struct lws_io_watcher_libevent event;
#endif
struct lws_context *context;
uint8_t actual_events;
};
struct lws_signal_watcher {
#ifdef LWS_WITH_LIBEV
struct lws_signal_watcher_libev ev;
#endif
#ifdef LWS_WITH_LIBUV
struct lws_signal_watcher_libuv uv;
#endif
#ifdef LWS_WITH_LIBEVENT
struct lws_signal_watcher_libevent event;
#endif
struct lws_context *context;
};
struct lws_foreign_thread_pollfd {
struct lws_foreign_thread_pollfd *next;
int fd_index;
int _and;
int _or;
};
#if LWS_MAX_SMP > 1
struct lws_mutex_refcount {
pthread_mutex_t lock;
pthread_t lock_owner;
const char *last_lock_reason;
char lock_depth;
char metadata;
};
void
lws_mutex_refcount_init(struct lws_mutex_refcount *mr);
void
lws_mutex_refcount_destroy(struct lws_mutex_refcount *mr);
void
lws_mutex_refcount_lock(struct lws_mutex_refcount *mr, const char *reason);
void
lws_mutex_refcount_unlock(struct lws_mutex_refcount *mr);
#endif
#if defined(LWS_WITH_NETWORK)
#include "core-net/private.h"
2018-03-02 15:31:35 +08:00
#endif
#define LWS_HRTIMER_NOWAIT (0x7fffffffffffffffll)
struct lws_deferred_free
{
struct lws_deferred_free *next;
time_t deadline;
void *payload;
};
/*
* the rest is managed per-context, that includes
*
* - processwide single fd -> wsi lookup
* - contextwide headers pool
*/
struct lws_context {
time_t last_timeout_check_s;
time_t last_ws_ping_pong_check_s;
time_t time_up;
time_t time_discontiguity;
time_t time_fixup;
const struct lws_plat_file_ops *fops;
2017-03-03 12:38:10 +08:00
struct lws_plat_file_ops fops_platform;
struct lws_context **pcontext_finalize;
const struct lws_tls_ops *tls_ops;
const char *username, *groupname;
#if defined(LWS_WITH_HTTP2)
struct http2_settings set;
#endif
2017-03-03 12:38:10 +08:00
#if defined(LWS_WITH_ZIP_FOPS)
struct lws_plat_file_ops fops_zip;
#endif
#if defined(LWS_WITH_NETWORK)
struct lws_context_per_thread pt[LWS_MAX_SMP];
struct lws_conn_stats conn_stats;
struct lws_vhost *vhost_list;
struct lws_vhost *no_listener_vhost_list;
struct lws_vhost *vhost_pending_destruction_list;
struct lws_plugin *plugin_list;
#ifdef _WIN32
/* different implementation between unix and windows */
struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
#else
unix plat: add minimal wsi fd map option An lws context usually contains a processwide fd -> wsi lookup table. This allows any possible fd returned by a *nix type OS to be immediately converted to a wsi just by indexing an array of struct lws * the size of the highest possible fd, as found by ulimit -n or similar. This works modestly for Linux type systems where the default ulimit -n for a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or 64-bit systems. However in the case your lws usage is much simpler, like one outgoing client connection and no serving, this represents increasing waste. It's made much worse if the system has a much larger default ulimit -n, eg 1M, the table is occupying 4MB or 8MB, of which you will only use one. Even so, because lws can't be sure the OS won't return a socket fd at any number up to (ulimit -n - 1), it has to allocate the whole lookup table at the moment. This patch looks to see if the context creation info is setting info->fd_limit_per_thread... if it leaves it at the default 0, then everything is as it was before this patch. However if finds that (info->fd_limit_per_thread * actual_number_of_service_threads) where the default number of service threads is 1, is less than the fd limit set by ulimit -n, lws switches to a slower lookup table scheme, which only allocates the requested number of slots. Lookups happen then by iterating the table and comparing rather than indexing the array directly, which is obviously somewhat of a performance hit. However in the case where you know lws will only have a very few wsi maximum, this method can very usefully trade off speed to be able to avoid the allocation sized by ulimit -n. minimal examples for client that can make use of this are also modified by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
struct lws **lws_lookup;
#endif
#endif
#if LWS_MAX_SMP > 1
struct lws_mutex_refcount mr;
#endif
struct lws_deferred_free *deferred_free_list;
2018-09-02 14:35:37 +08:00
#if defined(LWS_WITH_THREADPOOL)
struct lws_threadpool *tp_list_head;
#endif
#if defined(LWS_WITH_PEER_LIMITS)
struct lws_peer **pl_hash_table;
struct lws_peer *peer_wait_list;
time_t next_cull;
#endif
void *external_baggage_free_on_destroy;
const struct lws_token_limits *token_limits;
void *user_space;
2016-10-13 06:32:57 +08:00
const struct lws_protocol_vhost_options *reject_service_keywords;
lws_reload_func deprecation_cb;
void (*eventlib_signal_cb)(void *event_lib_handle, int signum);
2017-06-07 06:10:02 +08:00
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
cap_value_t caps[4];
char count_caps;
#endif
#if defined(LWS_WITH_LIBEV)
struct lws_context_eventlibs_libev ev;
#endif
#if defined(LWS_WITH_LIBUV)
struct lws_context_eventlibs_libuv uv;
#endif
#if defined(LWS_WITH_LIBEVENT)
struct lws_context_eventlibs_libevent event;
#endif
struct lws_event_loop_ops *event_loop_ops;
#if defined(LWS_WITH_TLS) && defined(LWS_WITH_NETWORK)
struct lws_context_tls tls;
#endif
char canonical_hostname[128];
const char *server_string;
#ifdef LWS_LATENCY
unsigned long worst_latency;
char worst_latency_info[256];
#endif
2017-05-07 10:02:03 +08:00
#if defined(LWS_WITH_STATS)
uint64_t lws_stats[LWSSTATS_SIZE];
uint64_t last_dump;
int updated;
#endif
2017-09-11 10:23:30 +08:00
#if defined(LWS_WITH_ESP32)
unsigned long time_last_state_dump;
uint32_t last_free_heap;
#endif
2017-05-07 10:02:03 +08:00
int max_fds;
int count_event_loop_static_asset_handles;
#if !defined(LWS_NO_DAEMONIZE)
pid_t started_with_parent;
#endif
int uid, gid;
int fd_random;
int count_wsi_allocated;
int count_cgi_spawned;
unsigned int options;
unsigned int fd_limit_per_thread;
unsigned int timeout_secs;
unsigned int pt_serv_buf_size;
int max_http_header_data;
int max_http_header_pool;
2017-03-16 10:46:31 +08:00
int simultaneous_ssl_restriction;
int simultaneous_ssl;
#if defined(LWS_WITH_PEER_LIMITS)
uint32_t pl_hash_elements; /* protected by context->lock */
uint32_t count_peers; /* protected by context->lock */
unsigned short ip_limit_ah;
unsigned short ip_limit_wsi;
#endif
unsigned int deprecated:1;
unsigned int being_destroyed:1;
unsigned int being_destroyed1:1;
unsigned int being_destroyed2:1;
unsigned int requested_kill:1;
unsigned int protocol_init_done:1;
unsigned int doing_protocol_init:1;
unsigned int done_protocol_destroy_cb:1;
unsigned int finalize_destroy_after_internal_loops_stopped:1;
unix plat: add minimal wsi fd map option An lws context usually contains a processwide fd -> wsi lookup table. This allows any possible fd returned by a *nix type OS to be immediately converted to a wsi just by indexing an array of struct lws * the size of the highest possible fd, as found by ulimit -n or similar. This works modestly for Linux type systems where the default ulimit -n for a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or 64-bit systems. However in the case your lws usage is much simpler, like one outgoing client connection and no serving, this represents increasing waste. It's made much worse if the system has a much larger default ulimit -n, eg 1M, the table is occupying 4MB or 8MB, of which you will only use one. Even so, because lws can't be sure the OS won't return a socket fd at any number up to (ulimit -n - 1), it has to allocate the whole lookup table at the moment. This patch looks to see if the context creation info is setting info->fd_limit_per_thread... if it leaves it at the default 0, then everything is as it was before this patch. However if finds that (info->fd_limit_per_thread * actual_number_of_service_threads) where the default number of service threads is 1, is less than the fd limit set by ulimit -n, lws switches to a slower lookup table scheme, which only allocates the requested number of slots. Lookups happen then by iterating the table and comparing rather than indexing the array directly, which is obviously somewhat of a performance hit. However in the case where you know lws will only have a very few wsi maximum, this method can very usefully trade off speed to be able to avoid the allocation sized by ulimit -n. minimal examples for client that can make use of this are also modified by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
unsigned int max_fds_unrelated_to_ulimit:1;
short count_threads;
short plugin_protocol_count;
short plugin_extension_count;
short server_string_len;
unsigned short ws_ping_pong_interval;
unsigned short deprecation_pending_listen_close_count;
2017-03-03 12:38:10 +08:00
uint8_t max_fi;
};
int
lws_check_deferred_free(struct lws_context *context, int tsi, int force);
#define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
#define lws_get_vh_protocol(vh, x) vh->protocols[x]
int
lws_jws_base64_enc(const char *in, size_t in_len, char *out, size_t out_max);
void
lws_vhost_destroy1(struct lws_vhost *vh);
2016-07-23 14:18:25 +08:00
2018-02-24 08:14:17 +08:00
#if defined(LWS_WITH_ESP32)
LWS_EXTERN int
2018-11-23 08:47:56 +08:00
lws_find_string_in_file(const char *filename, const char *str, int stringlen);
2018-02-24 08:14:17 +08:00
#endif
signed char char_to_hex(const char c);
struct lws_buflist {
struct lws_buflist *next;
size_t len;
size_t pos;
uint8_t buf[1]; /* true length of this is set by the oversize malloc */
};
LWS_EXTERN char *
lws_strdup(const char *s);
LWS_EXTERN int log_level;
#ifndef LWS_LATENCY
2018-05-09 09:28:06 +08:00
static LWS_INLINE void
lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
int ret, int completion) {
do {
(void)context; (void)wsi; (void)action; (void)ret;
(void)completion;
} while (0);
}
2018-05-09 09:28:06 +08:00
static LWS_INLINE void
lws_latency_pre(struct lws_context *context, struct lws *wsi) {
do { (void)context; (void)wsi; } while (0);
}
#else
#define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
extern void
lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
int ret, int completion);
#endif
LWS_EXTERN int
lws_b64_selftest(void);
#ifndef LWS_NO_DAEMONIZE
LWS_EXTERN int get_daemonize_pid();
#else
#define get_daemonize_pid() (0)
#endif
LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
#if !defined(LWS_WITH_TLS)
#define LWS_SSL_ENABLED(context) (0)
#define lws_context_init_server_ssl(_a, _b) (0)
#define lws_ssl_destroy(_a)
#define lws_context_init_alpn(_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_ssl_pending lws_ssl_pending_no_ssl
#define lws_server_socket_service_ssl(_b, _c) (0)
#define lws_ssl_close(_a) (0)
#define lws_ssl_context_destroy(_a)
#define lws_ssl_SSL_CTX_destroy(_a)
#define lws_ssl_remove_wsi_from_buffered_list(_a)
#define __lws_ssl_remove_wsi_from_buffered_list(_a)
#define lws_context_init_ssl_library(_a)
#define lws_tls_check_all_cert_lifetimes(_a)
#define lws_tls_acme_sni_cert_destroy(_a)
#endif
2018-03-02 14:22:49 +08:00
#if LWS_MAX_SMP > 1
#define lws_context_lock(c, reason) lws_mutex_refcount_lock(&c->mr, reason)
#define lws_context_unlock(c) lws_mutex_refcount_unlock(&c->mr)
2018-03-02 15:31:35 +08:00
static LWS_INLINE void
lws_vhost_lock(struct lws_vhost *vhost)
{
pthread_mutex_lock(&vhost->lock);
}
static LWS_INLINE void
lws_vhost_unlock(struct lws_vhost *vhost)
{
pthread_mutex_unlock(&vhost->lock);
}
#else
#define lws_pt_mutex_init(_a) (void)(_a)
#define lws_pt_mutex_destroy(_a) (void)(_a)
2018-03-02 14:22:49 +08:00
#define lws_pt_lock(_a, b) (void)(_a)
#define lws_pt_unlock(_a) (void)(_a)
#define lws_context_lock(_a, _b) (void)(_a)
#define lws_context_unlock(_a) (void)(_a)
2018-03-02 15:31:35 +08:00
#define lws_vhost_lock(_a) (void)(_a)
#define lws_vhost_unlock(_a) (void)(_a)
2018-03-02 14:22:49 +08:00
#define lws_pt_stats_lock(_a) (void)(_a)
#define lws_pt_stats_unlock(_a) (void)(_a)
#endif
LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_ssl_pending_no_ssl(struct lws *wsi);
int
lws_tls_check_cert_lifetime(struct lws_vhost *vhost);
int lws_jws_selftest(void);
int lws_jwe_selftest(void);
int
lws_protocol_init(struct lws_context *context);
int
2018-09-02 14:35:37 +08:00
lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p,
const char *reason);
const struct lws_protocol_vhost_options *
lws_vhost_protocol_options(struct lws_vhost *vh, const char *name);
2016-06-26 06:29:20 +08:00
const struct lws_http_mount *
lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);
/*
* custom allocator
*/
LWS_EXTERN void *
2017-10-04 07:10:39 +08:00
lws_realloc(void *ptr, size_t size, const char *reason);
LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
2017-10-04 07:10:39 +08:00
lws_zalloc(size_t size, const char *reason);
#ifdef LWS_PLAT_OPTEE
2017-10-04 07:10:39 +08:00
void *lws_malloc(size_t size, const char *reason);
void lws_free(void *p);
#define lws_free_set_NULL(P) do { lws_free(P); (P) = NULL; } while(0)
#else
2017-10-04 07:10:39 +08:00
#define lws_malloc(S, R) lws_realloc(NULL, S, R)
#define lws_free(P) lws_realloc(P, 0, "lws_free")
#define lws_free_set_NULL(P) do { lws_realloc(P, 0, "free"); (P) = NULL; } while(0)
#endif
int
lws_create_event_pipes(struct lws_context *context);
2018-08-01 06:52:03 +08:00
int
lws_plat_apply_FD_CLOEXEC(int n);
2017-03-03 12:38:10 +08:00
const struct lws_plat_file_ops *
lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
const char **vpath);
/* lws_plat_ */
LWS_EXTERN int
lws_plat_context_early_init(void);
LWS_EXTERN void
lws_plat_context_early_destroy(struct lws_context *context);
LWS_EXTERN void
lws_plat_context_late_destroy(struct lws_context *context);
LWS_EXTERN int
lws_plat_init(struct lws_context *context,
const struct lws_context_creation_info *info);
LWS_EXTERN int
lws_plat_drop_app_privileges(struct lws_context *context, int actually_drop);
#if defined(LWS_WITH_UNIX_SOCK)
int
lws_plat_user_colon_group_to_ids(const char *u_colon_g, uid_t *puid, gid_t *pgid);
#endif
2018-10-09 10:29:42 +08:00
LWS_EXTERN int
lws_check_byte_utf8(unsigned char state, unsigned char c);
LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
2018-11-23 08:47:56 +08:00
LWS_EXTERN int alloc_file(struct lws_context *context, const char *filename,
uint8_t **buf, lws_filepos_t *amount);
void
lws_context_destroy2(struct lws_context *context);
#ifdef __cplusplus
};
#endif