2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2015 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 #include "private-libwebsockets.h"
24 #ifndef LWS_BUILD_HASH
25 #define LWS_BUILD_HASH "unknown-build-hash"
28 static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
31 * lws_get_library_version: get version and git hash library built from
33 * returns a const char * to a string like "1.1 178d78c"
34 * representing the library version followed by the git head hash it
38 LWS_VISIBLE const char *
39 lws_get_library_version(void)
41 return library_version;
45 * lws_create_context() - Create the websocket handler
46 * @info: pointer to struct with parameters
48 * This function creates the listening socket (if serving) and takes care
49 * of all initialization in one step.
51 * After initialization, it returns a struct lws_context * that
52 * represents this server. After calling, user code needs to take care
53 * of calling lws_service() with the context pointer to get the
54 * server's sockets serviced. This must be done in the same process
55 * context as the initialization call.
57 * The protocol callback functions are called for a handful of events
58 * including http requests coming in, websocket connections becoming
59 * established, and data arriving; it's also called periodically to allow
62 * HTTP requests are sent always to the FIRST protocol in @protocol, since
63 * at that time websocket protocol has not been negotiated. Other
64 * protocols after the first one never see any HTTP callack activity.
66 * The server created is a simple http server by default; part of the
67 * websocket standard is upgrading this http connection to a websocket one.
69 * This allows the same server to provide files like scripts and favicon /
70 * images or whatever over http and dynamic data over websockets all in
71 * one place; they're all handled in the user callback.
74 LWS_VISIBLE struct lws_context *
75 lws_create_context(struct lws_context_creation_info *info)
77 struct lws_context *context = NULL;
78 #ifndef LWS_NO_DAEMONIZE
79 int pid_daemon = get_daemonize_pid();
83 lwsl_notice("Initial logging level %d\n", log_level);
85 lwsl_notice("Libwebsockets version: %s\n", library_version);
88 if (!(info->options & LWS_SERVER_OPTION_DISABLE_IPV6))
89 lwsl_notice("IPV6 compiled in and enabled\n");
91 lwsl_notice("IPV6 compiled in but disabled\n");
93 lwsl_notice("IPV6 not compiled in\n");
95 lws_feature_status_libev(info);
97 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
98 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
100 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
101 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
103 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
104 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
106 if (lws_plat_context_early_init())
109 context = lws_zalloc(sizeof(struct lws_context));
111 lwsl_err("No memory for websocket context\n");
114 #ifndef LWS_NO_DAEMONIZE
116 context->started_with_parent = pid_daemon;
117 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
120 context->listen_service_extraseen = 0;
121 context->protocols = info->protocols;
122 context->token_limits = info->token_limits;
123 context->listen_port = info->port;
124 context->http_proxy_port = 0;
125 context->http_proxy_address[0] = '\0';
126 context->options = info->options;
127 context->iface = info->iface;
128 context->ka_time = info->ka_time;
129 context->ka_interval = info->ka_interval;
130 context->ka_probes = info->ka_probes;
132 if (!info->ka_interval && info->ka_time > 0) {
133 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
138 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
139 * enable libev mediated SIGINT handling with a default handler of
140 * lws_sigint_cb. The handler can be overridden or disabled
141 * by invoking lws_sigint_cfg after creating the context, but
142 * before invoking lws_initloop:
144 context->use_ev_sigint = 1;
145 context->lws_ev_sigint_cb = &lws_sigint_cb;
146 #endif /* LWS_USE_LIBEV */
148 /* to reduce this allocation, */
149 context->max_fds = getdtablesize();
150 lwsl_notice(" ctx mem: %u bytes\n", sizeof(struct lws_context) +
151 ((sizeof(struct lws_pollfd) + sizeof(struct lws *)) *
154 context->fds = lws_zalloc(sizeof(struct lws_pollfd) * context->max_fds);
155 if (context->fds == NULL) {
156 lwsl_err("OOM allocating %d fds\n", context->max_fds);
160 if (lws_plat_init(context, info))
163 lws_context_init_extensions(info, context);
165 context->user_space = info->user;
167 strcpy(context->canonical_hostname, "unknown");
169 lws_server_get_canonical_hostname(context, info);
171 /* either use proxy from info, or try get it from env var */
173 if (info->http_proxy_address) {
174 /* override for backwards compatibility */
175 if (info->http_proxy_port)
176 context->http_proxy_port = info->http_proxy_port;
177 lws_set_proxy(context, info->http_proxy_address);
179 #ifdef LWS_HAVE_GETENV
180 p = getenv("http_proxy");
182 lws_set_proxy(context, p);
186 lwsl_notice(" per-conn mem: %u + %u headers + protocol rx buf\n",
187 sizeof(struct lws), sizeof(struct allocated_headers));
189 if (lws_context_init_server_ssl(info, context))
192 if (lws_context_init_client_ssl(info, context))
195 if (lws_context_init_server(info, context))
199 * drop any root privs for this process
200 * to listen on port < 1023 we would have needed root, but now we are
201 * listening, we don't want the power for anything else
203 lws_plat_drop_app_privileges(info);
205 /* initialize supported protocols */
207 for (context->count_protocols = 0;
208 info->protocols[context->count_protocols].callback;
209 context->count_protocols++)
211 * inform all the protocols that they are doing their one-time
212 * initialization if they want to
214 info->protocols[context->count_protocols].callback(context,
215 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
218 * give all extensions a chance to create any per-context
219 * allocations they need
221 if (info->port != CONTEXT_PORT_NO_LISTEN) {
222 if (lws_ext_callback_for_each_extension_type(context, NULL,
223 LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
226 if (lws_ext_callback_for_each_extension_type(context, NULL,
227 LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
233 lws_context_destroy(context);
238 * lws_context_destroy() - Destroy the websocket context
239 * @context: Websocket context
241 * This function closes any active connections and then frees the
242 * context. After calling this, any further use of the context is
246 lws_context_destroy(struct lws_context *context)
248 const struct lws_protocols *protocol = NULL;
251 lwsl_notice("%s\n", __func__);
257 if (context->worst_latency_info[0])
258 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
261 for (n = 0; n < context->fds_count; n++) {
262 struct lws *wsi = wsi_from_fd(context, context->fds[n].fd);
265 lws_close_and_free_session(context, wsi,
266 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
267 /* no protocol close */);
272 * give all extensions a chance to clean up any per-context
273 * allocations they might have made
276 n = lws_ext_callback_for_each_extension_type(context, NULL,
277 LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT, NULL, 0);
279 n = lws_ext_callback_for_each_extension_type(context, NULL,
280 LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
283 * inform all the protocols that they are done and will have no more
286 protocol = context->protocols;
288 while (protocol->callback) {
289 protocol->callback(context, NULL,
290 LWS_CALLBACK_PROTOCOL_DESTROY,
296 lws_plat_context_early_destroy(context);
297 lws_ssl_context_destroy(context);
300 lws_free(context->fds);
302 lws_plat_context_late_destroy(context);