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
37 LWS_VISIBLE const char *
38 lws_get_library_version(void)
40 return library_version;
43 static const char * const mount_protocols[] = {
50 LWS_VISIBLE LWS_EXTERN int
51 lws_write_http_mount(struct lws_http_mount *next, struct lws_http_mount **res,
52 void *store, const char *mountpoint, const char *origin,
55 struct lws_http_mount *m;
57 unsigned long l = (unsigned long)store;
64 m = (struct lws_http_mount *)store;
68 m->mountpoint = mountpoint;
69 m->mountpoint_len = (unsigned char)strlen(mountpoint);
73 for (n = 0; n < ARRAY_SIZE(mount_protocols); n++)
74 if (!strncmp(origin, mount_protocols[n],
75 strlen(mount_protocols[n]))) {
76 m->origin_protocol = n;
77 m->origin = origin + strlen(mount_protocols[n]);
81 if (n == ARRAY_SIZE(mount_protocols)) {
82 lwsl_err("unsupported protocol://\n");
83 return 0; /* ie, fail */
86 return ((char *)store + sizeof(*m)) - (char *)orig;
89 LWS_VISIBLE struct lws_vhost *
90 lws_create_vhost(struct lws_context *context,
91 struct lws_context_creation_info *info,
92 struct lws_http_mount *mounts)
94 struct lws_vhost *vh = lws_zalloc(sizeof(*vh)),
95 **vh1 = &context->vhost_list;
103 vh->context = context;
104 if (!info->vhost_name)
105 vh->name = "default";
107 vh->name = info->vhost_name;
109 vh->iface = info->iface;
110 vh->protocols = info->protocols;
111 for (vh->count_protocols = 0;
112 info->protocols[vh->count_protocols].callback;
113 vh->count_protocols++)
116 vh->mount_list = mounts;
118 lwsl_notice("Creating Vhost '%s' port %d, %d protocols\n",
119 vh->name, info->port, vh->count_protocols);
122 lwsl_notice(" mounting %s%s to %s\n",
123 mount_protocols[mounts->origin_protocol],
124 mounts->origin, mounts->mountpoint);
125 mounts = mounts->mount_next;
128 #ifndef LWS_NO_EXTENSIONS
129 vh->extensions = info->extensions;
132 vh->listen_port = info->port;
133 vh->http_proxy_port = 0;
134 vh->http_proxy_address[0] = '\0';
136 /* either use proxy from info, or try get it from env var */
138 if (info->http_proxy_address) {
139 /* override for backwards compatibility */
140 if (info->http_proxy_port)
141 vh->http_proxy_port = info->http_proxy_port;
142 lws_set_proxy(vh, info->http_proxy_address);
144 #ifdef LWS_HAVE_GETENV
145 p = getenv("http_proxy");
147 lws_set_proxy(vh, p);
151 memset(&wsi, 0, sizeof(wsi));
152 wsi.context = context;
155 /* initialize supported protocols */
157 for (n = 0; n < vh->count_protocols; n++)
159 * inform all the protocols that they are doing their one-time
160 * initialization if they want to.
162 * NOTE the wsi is all zeros except for the context & vh ptrs
163 * so lws_get_context(wsi) can work in the callback.
165 info->protocols[n].callback(&wsi,
166 LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
168 vh->ka_time = info->ka_time;
169 vh->ka_interval = info->ka_interval;
170 vh->ka_probes = info->ka_probes;
172 if (lws_context_init_server_ssl(info, vh))
175 if (lws_context_init_client_ssl(info, vh))
178 if (lws_context_init_server(info, vh))
186 vh1 = &(*vh1)->vhost_next;
198 * lws_create_context() - Create the websocket handler
199 * @info: pointer to struct with parameters
201 * This function creates the listening socket (if serving) and takes care
202 * of all initialization in one step.
204 * After initialization, it returns a struct lws_context * that
205 * represents this server. After calling, user code needs to take care
206 * of calling lws_service() with the context pointer to get the
207 * server's sockets serviced. This must be done in the same process
208 * context as the initialization call.
210 * The protocol callback functions are called for a handful of events
211 * including http requests coming in, websocket connections becoming
212 * established, and data arriving; it's also called periodically to allow
213 * async transmission.
215 * HTTP requests are sent always to the FIRST protocol in @protocol, since
216 * at that time websocket protocol has not been negotiated. Other
217 * protocols after the first one never see any HTTP callack activity.
219 * The server created is a simple http server by default; part of the
220 * websocket standard is upgrading this http connection to a websocket one.
222 * This allows the same server to provide files like scripts and favicon /
223 * images or whatever over http and dynamic data over websockets all in
224 * one place; they're all handled in the user callback.
226 LWS_VISIBLE struct lws_context *
227 lws_create_context(struct lws_context_creation_info *info)
229 struct lws_context *context = NULL;
231 #ifndef LWS_NO_DAEMONIZE
232 int pid_daemon = get_daemonize_pid();
236 lwsl_notice("Initial logging level %d\n", log_level);
237 lwsl_notice("Libwebsockets version: %s\n", library_version);
240 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
241 lwsl_notice("IPV6 compiled in and enabled\n");
243 lwsl_notice("IPV6 compiled in but disabled\n");
245 lwsl_notice("IPV6 not compiled in\n");
247 lws_feature_status_libev(info);
248 lws_feature_status_libuv(info);
250 lwsl_info(" LWS_DEF_HEADER_LEN : %u\n", LWS_DEF_HEADER_LEN);
251 lwsl_info(" LWS_MAX_PROTOCOLS : %u\n", LWS_MAX_PROTOCOLS);
252 lwsl_info(" LWS_MAX_SMP : %u\n", LWS_MAX_SMP);
253 lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
254 lwsl_info(" sizeof (*info) : %u\n", sizeof(*info));
256 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
258 if (lws_plat_context_early_init())
261 context = lws_zalloc(sizeof(struct lws_context));
263 lwsl_err("No memory for websocket context\n");
266 #ifndef LWS_NO_DAEMONIZE
268 context->started_with_parent = pid_daemon;
269 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
272 context->max_fds = getdtablesize();
274 if (info->count_threads)
275 context->count_threads = info->count_threads;
277 context->count_threads = 1;
279 if (context->count_threads > LWS_MAX_SMP)
280 context->count_threads = LWS_MAX_SMP;
282 context->token_limits = info->token_limits;
284 context->options = info->options;
286 if (info->timeout_secs)
287 context->timeout_secs = info->timeout_secs;
289 context->timeout_secs = AWAITING_TIMEOUT;
291 lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
293 if (info->max_http_header_data)
294 context->max_http_header_data = info->max_http_header_data;
296 context->max_http_header_data = LWS_DEF_HEADER_LEN;
297 if (info->max_http_header_pool)
298 context->max_http_header_pool = info->max_http_header_pool;
300 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
303 * Allocate the per-thread storage for scratchpad buffers,
304 * and header data pool
306 for (n = 0; n < context->count_threads; n++) {
307 context->pt[n].serv_buf = lws_zalloc(LWS_MAX_SOCKET_IO_BUF);
308 if (!context->pt[n].serv_buf) {
313 context->pt[n].context = context;
314 context->pt[n].tid = n;
315 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
316 context->max_http_header_pool);
317 if (!context->pt[n].http_header_data)
320 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
321 context->max_http_header_pool);
322 for (m = 0; m < context->max_http_header_pool; m++)
323 context->pt[n].ah_pool[m].data =
324 (char *)context->pt[n].http_header_data +
325 (m * context->max_http_header_data);
326 if (!context->pt[n].ah_pool)
329 lws_pt_mutex_init(&context->pt[n]);
332 if (info->fd_limit_per_thread)
333 context->fd_limit_per_thread = info->fd_limit_per_thread;
335 context->fd_limit_per_thread = context->max_fds /
336 context->count_threads;
338 lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
339 context->fd_limit_per_thread);
341 memset(&wsi, 0, sizeof(wsi));
342 wsi.context = context;
344 if (!info->ka_interval && info->ka_time > 0) {
345 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
350 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
351 * enable libev mediated SIGINT handling with a default handler of
352 * lws_sigint_cb. The handler can be overridden or disabled
353 * by invoking lws_sigint_cfg after creating the context, but
354 * before invoking lws_initloop:
356 context->use_ev_sigint = 1;
357 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
358 #endif /* LWS_USE_LIBEV */
360 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
361 * enable libev mediated SIGINT handling with a default handler of
362 * lws_sigint_cb. The handler can be overridden or disabled
363 * by invoking lws_sigint_cfg after creating the context, but
364 * before invoking lws_initloop:
366 context->use_ev_sigint = 1;
367 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
370 lwsl_info(" mem: context: %5u bytes (%d ctx + (%d thr x %d))\n",
371 sizeof(struct lws_context) +
372 (context->count_threads * LWS_MAX_SOCKET_IO_BUF),
373 sizeof(struct lws_context),
374 context->count_threads,
375 LWS_MAX_SOCKET_IO_BUF);
377 lwsl_info(" mem: http hdr rsvd: %5u bytes (%u thr x (%u + %u) x %u))\n",
378 (context->max_http_header_data +
379 sizeof(struct allocated_headers)) *
380 context->max_http_header_pool * context->count_threads,
381 context->count_threads,
382 context->max_http_header_data,
383 sizeof(struct allocated_headers),
384 context->max_http_header_pool);
385 n = sizeof(struct lws_pollfd) * context->count_threads *
386 context->fd_limit_per_thread;
387 context->pt[0].fds = lws_zalloc(n);
388 if (context->pt[0].fds == NULL) {
389 lwsl_err("OOM allocating %d fds\n", context->max_fds);
392 lwsl_info(" mem: pollfd map: %5u\n", n);
395 /* each thread serves his own chunk of fds */
396 for (n = 1; n < (int)info->count_threads; n++)
397 context->pt[n].fds = context->pt[n - 1].fds +
398 context->fd_limit_per_thread;
401 if (lws_plat_init(context, info))
404 lws_context_init_ssl_library(info);
407 * if he's not saying he'll make his own vhosts later then act
408 * compatibly and make a default vhost using the data in the info
410 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
411 if (!lws_create_vhost(context, info, NULL)) {
412 lwsl_err("Failed to create default vhost\n");
416 lws_context_init_extensions(info, context);
418 context->user_space = info->user;
420 lwsl_notice(" mem: per-conn: %5u bytes + protocol rx buf\n",
423 strcpy(context->canonical_hostname, "unknown");
424 lws_server_get_canonical_hostname(context, info);
426 context->uid = info->uid;
427 context->gid = info->gid;
430 * drop any root privs for this process
431 * to listen on port < 1023 we would have needed root, but now we are
432 * listening, we don't want the power for anything else
434 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
435 lws_plat_drop_app_privileges(info);
438 * give all extensions a chance to create any per-context
439 * allocations they need
441 if (info->port != CONTEXT_PORT_NO_LISTEN) {
442 if (lws_ext_cb_all_exts(context, NULL,
443 LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
446 if (lws_ext_cb_all_exts(context, NULL,
447 LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
453 lws_context_destroy(context);
458 * lws_context_destroy() - Destroy the websocket context
459 * @context: Websocket context
461 * This function closes any active connections and then frees the
462 * context. After calling this, any further use of the context is
466 lws_context_destroy(struct lws_context *context)
468 const struct lws_protocols *protocol = NULL;
469 struct lws_context_per_thread *pt;
470 struct lws_vhost *vh;
474 lwsl_notice("%s\n", __func__);
479 m = context->count_threads;
480 context->being_destroyed = 1;
482 memset(&wsi, 0, sizeof(wsi));
483 wsi.context = context;
486 if (context->worst_latency_info[0])
487 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
491 pt = &context->pt[m];
493 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
494 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
498 lws_close_free_wsi(wsi,
499 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
500 /* no protocol close */);
505 * give all extensions a chance to clean up any per-context
506 * allocations they might have made
509 n = lws_ext_cb_all_exts(context, NULL,
510 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
512 n = lws_ext_cb_all_exts(context, NULL,
513 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
516 * inform all the protocols that they are done and will have no more
519 vh = context->vhost_list;
521 protocol = vh->protocols;
523 while (protocol->callback) {
524 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
528 lws_ssl_SSL_CTX_destroy(vh);
532 for (n = 0; n < context->count_threads; n++) {
533 pt = &context->pt[n];
535 lws_libev_destroyloop(context, n);
536 lws_libuv_destroyloop(context, n);
538 lws_free_set_NULL(context->pt[n].serv_buf);
540 lws_free(pt->ah_pool);
541 if (pt->http_header_data)
542 lws_free(pt->http_header_data);
544 lws_plat_context_early_destroy(context);
545 lws_ssl_context_destroy(context);
547 if (context->pt[0].fds)
548 lws_free_set_NULL(context->pt[0].fds);
550 lws_plat_context_late_destroy(context);