whitespace trailing mass cleanout
[platform/upstream/libwebsockets.git] / lib / context.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 #ifndef LWS_BUILD_HASH
25 #define LWS_BUILD_HASH "unknown-build-hash"
26 #endif
27
28 static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
29
30 /**
31  * lws_get_library_version: get version and git hash library built from
32  *
33  *      returns a const char * to a string like "1.1 178d78c"
34  *      representing the library version followed by the git head hash it
35  *      was built from
36  */
37
38 LWS_VISIBLE const char *
39 lws_get_library_version(void)
40 {
41         return library_version;
42 }
43
44 /**
45  * lws_create_context() - Create the websocket handler
46  * @info:       pointer to struct with parameters
47  *
48  *      This function creates the listening socket (if serving) and takes care
49  *      of all initialization in one step.
50  *
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.
56  *
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
60  *      async transmission.
61  *
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.
65  *
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.
68  *
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.
72  */
73
74 LWS_VISIBLE struct lws_context *
75 lws_create_context(struct lws_context_creation_info *info)
76 {
77         struct lws_context *context = NULL;
78 #ifndef LWS_NO_DAEMONIZE
79         int pid_daemon = get_daemonize_pid();
80 #endif
81         char *p;
82
83         lwsl_notice("Initial logging level %d\n", log_level);
84
85         lwsl_notice("Libwebsockets version: %s\n", library_version);
86 #if LWS_POSIX
87 #ifdef LWS_USE_IPV6
88         if (!(info->options & LWS_SERVER_OPTION_DISABLE_IPV6))
89                 lwsl_notice("IPV6 compiled in and enabled\n");
90         else
91                 lwsl_notice("IPV6 compiled in but disabled\n");
92 #else
93         lwsl_notice("IPV6 not compiled in\n");
94 #endif
95         lws_feature_status_libev(info);
96 #endif
97         lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
98         lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
99
100         lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
101         lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
102 #if LWS_POSIX
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);
105 #endif
106         if (lws_plat_context_early_init())
107                 return NULL;
108
109         context = lws_zalloc(sizeof(struct lws_context));
110         if (!context) {
111                 lwsl_err("No memory for websocket context\n");
112                 return NULL;
113         }
114 #ifndef LWS_NO_DAEMONIZE
115         if (pid_daemon) {
116                 context->started_with_parent = pid_daemon;
117                 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
118         }
119 #endif
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;
131
132         if (!info->ka_interval && info->ka_time > 0) {
133                 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
134                 return NULL;
135         }
136
137 #ifdef LWS_USE_LIBEV
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:
143          */
144         context->use_ev_sigint = 1;
145         context->lws_ev_sigint_cb = &lws_sigint_cb;
146 #endif /* LWS_USE_LIBEV */
147
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 *)) *
152                     context->max_fds));
153
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);
157                 goto bail;
158         }
159
160         if (lws_plat_init(context, info))
161                 goto bail;
162
163         lws_context_init_extensions(info, context);
164
165         context->user_space = info->user;
166
167         strcpy(context->canonical_hostname, "unknown");
168
169         lws_server_get_canonical_hostname(context, info);
170
171         /* either use proxy from info, or try get it from env var */
172
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);
178         } else {
179 #ifdef LWS_HAVE_GETENV
180                 p = getenv("http_proxy");
181                 if (p)
182                         lws_set_proxy(context, p);
183 #endif
184         }
185
186         lwsl_notice(" per-conn mem: %u + %u headers + protocol rx buf\n",
187                     sizeof(struct lws), sizeof(struct allocated_headers));
188
189         if (lws_context_init_server_ssl(info, context))
190                 goto bail;
191
192         if (lws_context_init_client_ssl(info, context))
193                 goto bail;
194
195         if (lws_context_init_server(info, context))
196                 goto bail;
197
198         /*
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
202          */
203         lws_plat_drop_app_privileges(info);
204
205         /* initialize supported protocols */
206
207         for (context->count_protocols = 0;
208              info->protocols[context->count_protocols].callback;
209              context->count_protocols++)
210                 /*
211                  * inform all the protocols that they are doing their one-time
212                  * initialization if they want to
213                  */
214                 info->protocols[context->count_protocols].callback(context,
215                                NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
216
217         /*
218          * give all extensions a chance to create any per-context
219          * allocations they need
220          */
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)
224                         goto bail;
225         } else
226                 if (lws_ext_callback_for_each_extension_type(context, NULL,
227                         LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
228                         goto bail;
229
230         return context;
231
232 bail:
233         lws_context_destroy(context);
234         return NULL;
235 }
236
237 /**
238  * lws_context_destroy() - Destroy the websocket context
239  * @context:    Websocket context
240  *
241  *      This function closes any active connections and then frees the
242  *      context.  After calling this, any further use of the context is
243  *      undefined.
244  */
245 LWS_VISIBLE void
246 lws_context_destroy(struct lws_context *context)
247 {
248         const struct lws_protocols *protocol = NULL;
249         int n;
250
251         lwsl_notice("%s\n", __func__);
252
253         if (!context)
254                 return;
255
256 #ifdef LWS_LATENCY
257         if (context->worst_latency_info[0])
258                 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
259 #endif
260
261         for (n = 0; n < context->fds_count; n++) {
262                 struct lws *wsi = wsi_from_fd(context, context->fds[n].fd);
263                 if (!wsi)
264                         continue;
265                 lws_close_and_free_session(context, wsi,
266                                 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
267                                 /* no protocol close */);
268                 n--;
269         }
270
271         /*
272          * give all extensions a chance to clean up any per-context
273          * allocations they might have made
274          */
275
276         n = lws_ext_callback_for_each_extension_type(context, NULL,
277                         LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT, NULL, 0);
278
279         n = lws_ext_callback_for_each_extension_type(context, NULL,
280                         LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
281
282         /*
283          * inform all the protocols that they are done and will have no more
284          * callbacks
285          */
286         protocol = context->protocols;
287         if (protocol) {
288                 while (protocol->callback) {
289                         protocol->callback(context, NULL,
290                                            LWS_CALLBACK_PROTOCOL_DESTROY,
291                                            NULL, NULL, 0);
292                         protocol++;
293                 }
294         }
295
296         lws_plat_context_early_destroy(context);
297         lws_ssl_context_destroy(context);
298
299         if (context->fds)
300                 lws_free(context->fds);
301
302         lws_plat_context_late_destroy(context);
303
304         lws_free(context);
305 }