2014-04-03 07:29:50 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2015-11-02 13:10:33 +08:00
|
|
|
* Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
|
2014-04-03 07:29:50 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
|
|
|
#ifndef LWS_BUILD_HASH
|
|
|
|
#define LWS_BUILD_HASH "unknown-build-hash"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lws_get_library_version: get version and git hash library built from
|
|
|
|
*
|
|
|
|
* returns a const char * to a string like "1.1 178d78c"
|
|
|
|
* representing the library version followed by the git head hash it
|
|
|
|
* was built from
|
|
|
|
*/
|
|
|
|
|
|
|
|
LWS_VISIBLE const char *
|
|
|
|
lws_get_library_version(void)
|
|
|
|
{
|
|
|
|
return library_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-04 08:43:54 +08:00
|
|
|
* lws_create_context() - Create the websocket handler
|
2014-04-03 07:29:50 +08:00
|
|
|
* @info: pointer to struct with parameters
|
|
|
|
*
|
|
|
|
* This function creates the listening socket (if serving) and takes care
|
|
|
|
* of all initialization in one step.
|
|
|
|
*
|
2015-12-04 11:08:32 +08:00
|
|
|
* After initialization, it returns a struct lws_context * that
|
2014-04-03 07:29:50 +08:00
|
|
|
* represents this server. After calling, user code needs to take care
|
2015-12-04 08:43:54 +08:00
|
|
|
* of calling lws_service() with the context pointer to get the
|
2014-04-10 10:13:43 +08:00
|
|
|
* server's sockets serviced. This must be done in the same process
|
|
|
|
* context as the initialization call.
|
2014-04-03 07:29:50 +08:00
|
|
|
*
|
|
|
|
* The protocol callback functions are called for a handful of events
|
|
|
|
* including http requests coming in, websocket connections becoming
|
|
|
|
* established, and data arriving; it's also called periodically to allow
|
|
|
|
* async transmission.
|
|
|
|
*
|
|
|
|
* HTTP requests are sent always to the FIRST protocol in @protocol, since
|
|
|
|
* at that time websocket protocol has not been negotiated. Other
|
|
|
|
* protocols after the first one never see any HTTP callack activity.
|
|
|
|
*
|
|
|
|
* The server created is a simple http server by default; part of the
|
|
|
|
* websocket standard is upgrading this http connection to a websocket one.
|
|
|
|
*
|
|
|
|
* This allows the same server to provide files like scripts and favicon /
|
|
|
|
* images or whatever over http and dynamic data over websockets all in
|
|
|
|
* one place; they're all handled in the user callback.
|
|
|
|
*/
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
LWS_VISIBLE struct lws_context *
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_create_context(struct lws_context_creation_info *info)
|
2014-04-03 07:29:50 +08:00
|
|
|
{
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_context *context = NULL;
|
2015-12-17 07:54:44 +08:00
|
|
|
struct lws wsi;
|
2015-12-06 11:00:36 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2014-04-03 07:29:50 +08:00
|
|
|
int pid_daemon = get_daemonize_pid();
|
2015-11-02 20:34:12 +08:00
|
|
|
#endif
|
2015-12-06 08:40:00 +08:00
|
|
|
char *p;
|
2014-04-03 07:29:50 +08:00
|
|
|
|
|
|
|
lwsl_notice("Initial logging level %d\n", log_level);
|
2015-11-02 20:34:12 +08:00
|
|
|
|
|
|
|
lwsl_notice("Libwebsockets version: %s\n", library_version);
|
|
|
|
#if LWS_POSIX
|
2014-04-03 07:29:50 +08:00
|
|
|
#ifdef LWS_USE_IPV6
|
|
|
|
if (!(info->options & LWS_SERVER_OPTION_DISABLE_IPV6))
|
|
|
|
lwsl_notice("IPV6 compiled in and enabled\n");
|
|
|
|
else
|
|
|
|
lwsl_notice("IPV6 compiled in but disabled\n");
|
|
|
|
#else
|
|
|
|
lwsl_notice("IPV6 not compiled in\n");
|
|
|
|
#endif
|
2014-04-11 13:14:37 +08:00
|
|
|
lws_feature_status_libev(info);
|
2015-12-04 16:54:12 +08:00
|
|
|
#endif
|
2014-04-03 07:29:50 +08:00
|
|
|
lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
|
|
|
|
lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
|
2014-04-12 10:07:02 +08:00
|
|
|
|
2014-04-03 07:29:50 +08:00
|
|
|
lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
|
|
|
|
lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
|
2015-11-08 12:10:26 +08:00
|
|
|
#if LWS_POSIX
|
2014-04-03 07:29:50 +08:00
|
|
|
lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
|
|
|
|
lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
|
2015-11-08 12:10:26 +08:00
|
|
|
#endif
|
2014-04-03 07:29:50 +08:00
|
|
|
if (lws_plat_context_early_init())
|
|
|
|
return NULL;
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
context = lws_zalloc(sizeof(struct lws_context));
|
2014-04-03 07:29:50 +08:00
|
|
|
if (!context) {
|
|
|
|
lwsl_err("No memory for websocket context\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-12-06 11:00:36 +08:00
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2014-04-12 10:07:02 +08:00
|
|
|
if (pid_daemon) {
|
|
|
|
context->started_with_parent = pid_daemon;
|
|
|
|
lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
|
|
|
|
}
|
2015-11-02 20:34:12 +08:00
|
|
|
#endif
|
2015-12-17 17:03:59 +08:00
|
|
|
context->lserv_seen = 0;
|
2014-04-03 07:29:50 +08:00
|
|
|
context->protocols = info->protocols;
|
2014-06-29 00:25:19 -04:00
|
|
|
context->token_limits = info->token_limits;
|
2014-04-03 07:29:50 +08:00
|
|
|
context->listen_port = info->port;
|
|
|
|
context->http_proxy_port = 0;
|
|
|
|
context->http_proxy_address[0] = '\0';
|
|
|
|
context->options = info->options;
|
|
|
|
context->iface = info->iface;
|
2014-07-29 15:36:06 +03:00
|
|
|
context->ka_time = info->ka_time;
|
|
|
|
context->ka_interval = info->ka_interval;
|
|
|
|
context->ka_probes = info->ka_probes;
|
|
|
|
|
2015-12-17 07:54:44 +08:00
|
|
|
memset(&wsi, 0, sizeof(wsi));
|
|
|
|
wsi.context = context;
|
|
|
|
|
2015-10-16 10:54:04 +08:00
|
|
|
if (!info->ka_interval && info->ka_time > 0) {
|
|
|
|
lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-04-26 23:32:16 -04:00
|
|
|
#ifdef LWS_USE_LIBEV
|
|
|
|
/* (Issue #264) In order to *avoid breaking backwards compatibility*, we
|
|
|
|
* enable libev mediated SIGINT handling with a default handler of
|
2015-12-04 08:43:54 +08:00
|
|
|
* lws_sigint_cb. The handler can be overridden or disabled
|
|
|
|
* by invoking lws_sigint_cfg after creating the context, but
|
|
|
|
* before invoking lws_initloop:
|
2015-04-26 23:32:16 -04:00
|
|
|
*/
|
|
|
|
context->use_ev_sigint = 1;
|
2015-12-04 08:43:54 +08:00
|
|
|
context->lws_ev_sigint_cb = &lws_sigint_cb;
|
2015-04-26 23:32:16 -04:00
|
|
|
#endif /* LWS_USE_LIBEV */
|
|
|
|
|
2014-04-03 07:29:50 +08:00
|
|
|
/* to reduce this allocation, */
|
|
|
|
context->max_fds = getdtablesize();
|
2015-12-04 16:54:12 +08:00
|
|
|
lwsl_notice(" ctx mem: %u bytes\n", sizeof(struct lws_context) +
|
|
|
|
((sizeof(struct lws_pollfd) + sizeof(struct lws *)) *
|
|
|
|
context->max_fds));
|
|
|
|
|
|
|
|
context->fds = lws_zalloc(sizeof(struct lws_pollfd) * context->max_fds);
|
2014-04-03 07:29:50 +08:00
|
|
|
if (context->fds == NULL) {
|
2015-12-04 16:54:12 +08:00
|
|
|
lwsl_err("OOM allocating %d fds\n", context->max_fds);
|
2015-06-25 17:51:07 +02:00
|
|
|
goto bail;
|
2014-04-03 07:29:50 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 07:14:16 +08:00
|
|
|
if (lws_plat_init(context, info))
|
2015-06-25 17:51:07 +02:00
|
|
|
goto bail;
|
2015-11-08 12:10:26 +08:00
|
|
|
|
2014-04-12 10:07:02 +08:00
|
|
|
lws_context_init_extensions(info, context);
|
|
|
|
|
2014-04-03 07:29:50 +08:00
|
|
|
context->user_space = info->user;
|
|
|
|
|
|
|
|
strcpy(context->canonical_hostname, "unknown");
|
|
|
|
|
2014-04-12 10:07:02 +08:00
|
|
|
lws_server_get_canonical_hostname(context, info);
|
2014-04-03 07:29:50 +08:00
|
|
|
|
2015-11-08 10:15:01 +08:00
|
|
|
/* either use proxy from info, or try get it from env var */
|
2014-04-03 07:29:50 +08:00
|
|
|
|
|
|
|
if (info->http_proxy_address) {
|
2015-11-08 10:15:01 +08:00
|
|
|
/* override for backwards compatibility */
|
|
|
|
if (info->http_proxy_port)
|
|
|
|
context->http_proxy_port = info->http_proxy_port;
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_set_proxy(context, info->http_proxy_address);
|
2014-04-03 07:29:50 +08:00
|
|
|
} else {
|
2015-06-24 16:46:02 +02:00
|
|
|
#ifdef LWS_HAVE_GETENV
|
2014-04-03 07:29:50 +08:00
|
|
|
p = getenv("http_proxy");
|
2015-11-08 10:15:01 +08:00
|
|
|
if (p)
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_set_proxy(context, p);
|
2014-04-03 07:29:50 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-12-04 16:54:12 +08:00
|
|
|
lwsl_notice(" per-conn mem: %u + %u headers + protocol rx buf\n",
|
|
|
|
sizeof(struct lws), sizeof(struct allocated_headers));
|
2014-07-29 15:36:06 +03:00
|
|
|
|
2014-10-17 08:47:51 +08:00
|
|
|
if (lws_context_init_server_ssl(info, context))
|
2014-10-16 08:53:19 +08:00
|
|
|
goto bail;
|
2014-10-17 08:47:51 +08:00
|
|
|
|
|
|
|
if (lws_context_init_client_ssl(info, context))
|
2014-04-03 08:24:29 +08:00
|
|
|
goto bail;
|
2014-04-03 07:29:50 +08:00
|
|
|
|
2014-04-03 08:24:29 +08:00
|
|
|
if (lws_context_init_server(info, context))
|
|
|
|
goto bail;
|
2014-04-03 07:29:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* drop any root privs for this process
|
|
|
|
* to listen on port < 1023 we would have needed root, but now we are
|
|
|
|
* listening, we don't want the power for anything else
|
|
|
|
*/
|
|
|
|
lws_plat_drop_app_privileges(info);
|
|
|
|
|
|
|
|
/* initialize supported protocols */
|
|
|
|
|
|
|
|
for (context->count_protocols = 0;
|
2015-12-04 16:54:12 +08:00
|
|
|
info->protocols[context->count_protocols].callback;
|
2015-12-11 09:36:14 +08:00
|
|
|
context->count_protocols++)
|
2014-04-03 07:29:50 +08:00
|
|
|
/*
|
|
|
|
* inform all the protocols that they are doing their one-time
|
2015-12-17 07:54:44 +08:00
|
|
|
* initialization if they want to.
|
|
|
|
*
|
|
|
|
* NOTE the wsi is all zeros except for the context pointer
|
2015-12-17 18:25:25 +08:00
|
|
|
* so lws_get_context(wsi) can work in the callback.
|
2014-04-03 07:29:50 +08:00
|
|
|
*/
|
2015-12-17 07:54:44 +08:00
|
|
|
info->protocols[context->count_protocols].callback(&wsi,
|
|
|
|
LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
|
2014-04-03 07:29:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* give all extensions a chance to create any per-context
|
|
|
|
* allocations they need
|
|
|
|
*/
|
|
|
|
if (info->port != CONTEXT_PORT_NO_LISTEN) {
|
2015-12-17 17:03:59 +08:00
|
|
|
if (lws_ext_cb_all_exts(context, NULL,
|
2015-12-04 16:54:12 +08:00
|
|
|
LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
|
2014-04-03 07:29:50 +08:00
|
|
|
goto bail;
|
|
|
|
} else
|
2015-12-17 17:03:59 +08:00
|
|
|
if (lws_ext_cb_all_exts(context, NULL,
|
2015-12-04 16:54:12 +08:00
|
|
|
LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
|
2014-04-03 07:29:50 +08:00
|
|
|
goto bail;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2014-04-03 07:29:50 +08:00
|
|
|
return context;
|
|
|
|
|
|
|
|
bail:
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_context_destroy(context);
|
2014-04-03 07:29:50 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-04 08:43:54 +08:00
|
|
|
* lws_context_destroy() - Destroy the websocket context
|
2014-04-03 07:29:50 +08:00
|
|
|
* @context: Websocket context
|
|
|
|
*
|
|
|
|
* This function closes any active connections and then frees the
|
|
|
|
* context. After calling this, any further use of the context is
|
|
|
|
* undefined.
|
|
|
|
*/
|
|
|
|
LWS_VISIBLE void
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_context_destroy(struct lws_context *context)
|
2014-04-03 07:29:50 +08:00
|
|
|
{
|
2015-12-11 10:45:35 +08:00
|
|
|
const struct lws_protocols *protocol = NULL;
|
2015-12-18 01:08:14 +08:00
|
|
|
struct lws wsi;
|
2015-12-04 16:54:12 +08:00
|
|
|
int n;
|
2014-04-03 07:29:50 +08:00
|
|
|
|
2014-04-12 10:07:02 +08:00
|
|
|
lwsl_notice("%s\n", __func__);
|
|
|
|
|
2015-06-25 17:51:07 +02:00
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2016-01-20 09:23:24 +08:00
|
|
|
context->being_destroyed = 1;
|
|
|
|
|
2015-12-18 01:08:14 +08:00
|
|
|
memset(&wsi, 0, sizeof(wsi));
|
|
|
|
wsi.context = context;
|
|
|
|
|
2014-04-03 07:29:50 +08:00
|
|
|
#ifdef LWS_LATENCY
|
|
|
|
if (context->worst_latency_info[0])
|
|
|
|
lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (n = 0; n < context->fds_count; n++) {
|
2015-12-04 16:54:12 +08:00
|
|
|
struct lws *wsi = wsi_from_fd(context, context->fds[n].fd);
|
2014-04-03 07:29:50 +08:00
|
|
|
if (!wsi)
|
|
|
|
continue;
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
|
2015-12-04 16:54:12 +08:00
|
|
|
/* no protocol close */);
|
2014-04-03 07:29:50 +08:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* give all extensions a chance to clean up any per-context
|
|
|
|
* allocations they might have made
|
|
|
|
*/
|
2015-12-04 16:54:12 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
n = lws_ext_cb_all_exts(context, NULL,
|
2015-12-04 16:54:12 +08:00
|
|
|
LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT, NULL, 0);
|
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
n = lws_ext_cb_all_exts(context, NULL,
|
2015-12-04 16:54:12 +08:00
|
|
|
LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
|
2014-04-03 07:29:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* inform all the protocols that they are done and will have no more
|
|
|
|
* callbacks
|
|
|
|
*/
|
2015-06-25 17:51:07 +02:00
|
|
|
protocol = context->protocols;
|
|
|
|
if (protocol) {
|
|
|
|
while (protocol->callback) {
|
2015-12-18 01:08:14 +08:00
|
|
|
protocol->callback(&wsi,
|
2015-12-04 16:54:12 +08:00
|
|
|
LWS_CALLBACK_PROTOCOL_DESTROY,
|
|
|
|
NULL, NULL, 0);
|
2015-06-25 17:51:07 +02:00
|
|
|
protocol++;
|
|
|
|
}
|
2014-04-03 07:29:50 +08:00
|
|
|
}
|
2016-01-29 08:40:11 +08:00
|
|
|
#ifdef LWS_USE_LIBEV
|
|
|
|
ev_io_stop(context->io_loop, &context->w_accept.watcher);
|
|
|
|
if(context->use_ev_sigint)
|
|
|
|
ev_signal_stop(context->io_loop, &context->w_sigint.watcher);
|
|
|
|
#endif /* LWS_USE_LIBEV */
|
2014-04-03 07:29:50 +08:00
|
|
|
|
|
|
|
lws_plat_context_early_destroy(context);
|
2014-04-12 10:07:02 +08:00
|
|
|
lws_ssl_context_destroy(context);
|
2014-04-03 07:29:50 +08:00
|
|
|
|
2015-06-25 17:51:07 +02:00
|
|
|
if (context->fds)
|
|
|
|
lws_free(context->fds);
|
|
|
|
|
2014-04-03 07:29:50 +08:00
|
|
|
lws_plat_context_late_destroy(context);
|
2014-04-12 10:07:02 +08:00
|
|
|
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_free(context);
|
2014-04-03 07:29:50 +08:00
|
|
|
}
|