blob: 31e14242964a1c8ca10d0b2e2109c1eac923a055 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 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
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010024#if defined(WIN32) || defined(_WIN32)
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010027#include <mstcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000028#else
Davidc4ef7b12013-01-12 20:39:47 +080029#ifdef LWS_BUILTIN_GETIFADDRS
30#include <getifaddrs.h>
31#else
Peter Hinz56885f32011-03-02 22:03:47 +000032#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080033#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090034#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000035#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080036#include <sys/socket.h>
37#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000038#endif
Andy Green2e24da02011-03-05 16:12:04 +000039
40#ifdef LWS_OPENSSL_SUPPORT
41int openssl_websocket_private_data_index;
42#endif
43
Andy Greenaa6fc442012-04-12 13:26:49 +080044#ifdef __MINGW32__
45#include "../win32port/win32helpers/websock-w32.c"
46#else
47#ifdef __MINGW64__
48#include "../win32port/win32helpers/websock-w32.c"
49#endif
50#endif
51
Andy Green7b405452013-02-01 10:50:15 +080052#ifndef LWS_BUILD_HASH
53#define LWS_BUILD_HASH "unknown-build-hash"
54#endif
Andy Green3182ece2013-01-20 17:08:31 +080055
Andy Green7c19c342013-01-19 12:18:07 +080056static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080057static void lwsl_emit_stderr(int level, const char *line);
58static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
59
Andy Green7b405452013-02-01 10:50:15 +080060static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
61
Andy Greenb5b23192013-02-11 17:13:32 +080062static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080063 "ERR",
64 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080065 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080066 "INFO",
67 "DEBUG",
68 "PARSER",
69 "HEADER",
70 "EXTENSION",
71 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080072 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080073};
74
Andy Greenb5b23192013-02-11 17:13:32 +080075#ifndef LWS_NO_CLIENT
76 extern int lws_client_socket_service(
77 struct libwebsocket_context *context,
78 struct libwebsocket *wsi, struct pollfd *pollfd);
79#endif
80#ifndef LWS_NO_SERVER
81 extern int lws_server_socket_service(
82 struct libwebsocket_context *context,
83 struct libwebsocket *wsi, struct pollfd *pollfd);
84#endif
85
Andy Green7b405452013-02-01 10:50:15 +080086/**
87 * lws_get_library_version: get version and git hash library built from
88 *
89 * returns a const char * to a string like "1.1 178d78c"
90 * representing the library version followed by the git head hash it
91 * was built from
92 */
93
Peter Pentchev9a4fef72013-03-30 09:52:21 +080094LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +080095lws_get_library_version(void)
96{
97 return library_version;
98}
99
Andy Green0d338332011-02-12 11:57:43 +0000100int
Andy Greenb5b23192013-02-11 17:13:32 +0800101insert_wsi_socket_into_fds(struct libwebsocket_context *context,
102 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000103{
Andy Greendfb23042013-01-17 12:26:48 +0800104 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800105 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000106 return 1;
107 }
108
Andy Greendfb23042013-01-17 12:26:48 +0800109 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800110 lwsl_err("Socket fd %d is too high (%d)\n",
111 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800112 return 1;
113 }
114
115 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800116 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800117
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
119 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800120
121 context->lws_lookup[wsi->sock] = wsi;
122 wsi->position_in_fds_table = context->fds_count;
123 context->fds[context->fds_count].fd = wsi->sock;
124 context->fds[context->fds_count].events = POLLIN;
125 context->fds[context->fds_count++].revents = 0;
126
127 /* external POLL support via protocol 0 */
128 context->protocols[0].callback(context, wsi,
129 LWS_CALLBACK_ADD_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800130 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000131
132 return 0;
133}
134
Andy Greendfb23042013-01-17 12:26:48 +0800135static int
Andy Greenb5b23192013-02-11 17:13:32 +0800136remove_wsi_socket_from_fds(struct libwebsocket_context *context,
137 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000138{
Andy Greendfb23042013-01-17 12:26:48 +0800139 int m;
Andy Green0d338332011-02-12 11:57:43 +0000140
Andy Greendfb23042013-01-17 12:26:48 +0800141 if (!--context->fds_count)
142 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000143
Andy Greendfb23042013-01-17 12:26:48 +0800144 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800145 lwsl_err("Socket fd %d too high (%d)\n",
146 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800147 return 1;
148 }
Andy Green0d338332011-02-12 11:57:43 +0000149
Andy Greenb5b23192013-02-11 17:13:32 +0800150 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
151 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800152
153 m = wsi->position_in_fds_table; /* replace the contents for this */
154
155 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800156 context->fds[m] = context->fds[context->fds_count];
157 /*
158 * end guy's fds_lookup entry remains unchanged
159 * (still same fd pointing to same wsi)
160 */
Andy Greendfb23042013-01-17 12:26:48 +0800161 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800162 context->lws_lookup[context->fds[context->fds_count].fd]->
163 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800164 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800165 context->lws_lookup[wsi->sock] = NULL;
166 /* removed wsi has no position any more */
167 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800168
169do_ext:
170 /* remove also from external POLL support via protocol 0 */
171 if (wsi->sock)
172 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800173 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
174 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800175
176 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000177}
178
Andy Green32375b72011-02-19 08:32:53 +0000179
Andy Green8f037e42010-12-19 22:13:26 +0000180void
Peter Hinz56885f32011-03-02 22:03:47 +0000181libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000182 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000183{
Andy Greenb45993c2010-12-18 15:13:50 +0000184 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000185 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000186 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
187 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800188#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000189 int ret;
190 int m;
191 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100192 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800193#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000194
Andy Green4b6fbe12011-02-14 08:03:48 +0000195 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000196 return;
197
Andy Green62c54d22011-02-14 09:14:25 +0000198 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000199
Andy Green62c54d22011-02-14 09:14:25 +0000200 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000201 return;
202
Andy Green22524a62013-02-15 22:48:58 +0800203 /* we tried the polite way... */
204 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
205 goto just_kill_connection;
206
Andy Green623a98d2013-01-21 11:04:23 +0800207 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000208
Andy Green5dc62ea2013-09-20 20:26:12 +0800209 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
210 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
211
212 context->protocols[0].callback(context, wsi,
213 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
214
215 free(wsi->u.hdr.ah);
216 goto just_kill_connection;
217 }
218
219
kapejodce64fb02013-11-19 13:38:16 +0100220 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
221 if (wsi->u.http.post_buffer) {
222 free(wsi->u.http.post_buffer);
223 wsi->u.http.post_buffer = NULL;
224 }
225 if (wsi->u.http.fd >= 0) {
226 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
227 close(wsi->u.http.fd);
228 wsi->u.http.fd = -1;
229 context->protocols[0].callback(context, wsi,
230 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
231 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800232 }
233
Andy Green3182ece2013-01-20 17:08:31 +0800234#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000235 /*
Andy Green68b45042011-05-25 21:41:57 +0100236 * are his extensions okay with him closing? Eg he might be a mux
237 * parent and just his ch1 aspect is closing?
238 */
239
Andy Green68b45042011-05-25 21:41:57 +0100240 for (n = 0; n < wsi->count_active_extensions; n++) {
241 if (!wsi->active_extensions[n]->callback)
242 continue;
243
244 m = wsi->active_extensions[n]->callback(context,
245 wsi->active_extensions[n], wsi,
246 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
247 wsi->active_extensions_user[n], NULL, 0);
248
249 /*
250 * if somebody vetoed actually closing him at this time....
251 * up to the extension to track the attempted close, let's
252 * just bail
253 */
254
255 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800256 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100257 return;
258 }
259 }
260
Andy Green68b45042011-05-25 21:41:57 +0100261 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000262 * flush any tx pending from extensions, since we may send close packet
263 * if there are problems with send, just nuke the connection
264 */
265
266 ret = 1;
267 while (ret == 1) {
268
269 /* default to nobody has more to spill */
270
271 ret = 0;
272 eff_buf.token = NULL;
273 eff_buf.token_len = 0;
274
275 /* show every extension the new incoming data */
276
277 for (n = 0; n < wsi->count_active_extensions; n++) {
278 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000279 wsi->protocol->owning_server,
280 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000281 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
282 wsi->active_extensions_user[n], &eff_buf, 0);
283 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800284 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000285 goto just_kill_connection;
286 }
287 if (m)
288 /*
289 * at least one extension told us he has more
290 * to spill, so we will go around again after
291 */
292 ret = 1;
293 }
294
295 /* assuming they left us something to send, send it */
296
297 if (eff_buf.token_len)
298 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800299 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800300 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000301 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800302 }
Andy Greenc44159f2011-03-07 07:08:18 +0000303 }
Andy Green3182ece2013-01-20 17:08:31 +0800304#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000305
306 /*
Andy Greenda527df2011-03-07 07:08:12 +0000307 * signal we are closing, libsocket_write will
308 * add any necessary version-specific stuff. If the write fails,
309 * no worries we are closing anyway. If we didn't initiate this
310 * close, then our state has been changed to
311 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
312 *
313 * Likewise if it's a second call to close this connection after we
314 * sent the close indication to the peer already, we are in state
315 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
316 */
317
318 if (old_state == WSI_STATE_ESTABLISHED &&
319 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100320
Andy Green43db0452013-01-10 19:50:35 +0800321 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100322
Andy Greenfdd305a2013-02-11 14:32:48 +0800323 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800324 memset(buf, 0, sizeof(buf));
325 n = libwebsocket_write(wsi,
326 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000327 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800328 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000329 /*
330 * we have sent a nice protocol level indication we
331 * now wish to close, we should not send anything more
332 */
333
334 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
335
Andy Greenb5b23192013-02-11 17:13:32 +0800336 /*
337 * ...and we should wait for a reply for a bit
338 * out of politeness
339 */
Andy Greenda527df2011-03-07 07:08:12 +0000340
341 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800342 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000343
Andy Green43db0452013-01-10 19:50:35 +0800344 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000345
346 return;
347 }
348
Andy Greenb5b23192013-02-11 17:13:32 +0800349 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800350
Andy Greenda527df2011-03-07 07:08:12 +0000351 /* else, the send failed and we should just hang up */
352 }
353
Andy Greenc44159f2011-03-07 07:08:18 +0000354just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100355
Andy Greenb5b23192013-02-11 17:13:32 +0800356 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100357
Andy Greenda527df2011-03-07 07:08:12 +0000358 /*
359 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800360 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000361 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000362
Andy Greendfb23042013-01-17 12:26:48 +0800363 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000364
Andy Green251f6fa2010-11-03 11:13:06 +0000365 wsi->state = WSI_STATE_DEAD_SOCKET;
366
Andy Greenb5b23192013-02-11 17:13:32 +0800367 if ((old_state == WSI_STATE_ESTABLISHED ||
368 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800369 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
370
371 if (wsi->u.ws.rx_user_buffer) {
372 free(wsi->u.ws.rx_user_buffer);
373 wsi->u.ws.rx_user_buffer = NULL;
374 }
375 if (wsi->u.ws.rxflow_buffer) {
376 free(wsi->u.ws.rxflow_buffer);
377 wsi->u.ws.rxflow_buffer = NULL;
378 }
Andy Green1f4267b2013-10-17 08:09:19 +0800379 if (wsi->u.ws.truncated_send_malloc) {
380 /* not going to be completed... nuke it */
381 free(wsi->u.ws.truncated_send_malloc);
382 wsi->u.ws.truncated_send_malloc = NULL;
383 }
Andy Green54495112013-02-06 21:10:16 +0900384 }
385
Andy Green4b6fbe12011-02-14 08:03:48 +0000386 /* tell the user it's all over for this guy */
387
Andy Greend4302732011-02-28 07:45:29 +0000388 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800389 ((old_state == WSI_STATE_ESTABLISHED) ||
390 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
391 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800392 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000393 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000394 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800395 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
396 lwsl_debug("calling back CLOSED_HTTP\n");
397 context->protocols[0].callback(context, wsi,
398 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800399 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800400 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000401
Andy Green3182ece2013-01-20 17:08:31 +0800402#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000403 /* deallocate any active extension contexts */
404
405 for (n = 0; n < wsi->count_active_extensions; n++) {
406 if (!wsi->active_extensions[n]->callback)
407 continue;
408
Andy Green46c2ea02011-03-22 09:04:01 +0000409 wsi->active_extensions[n]->callback(context,
410 wsi->active_extensions[n], wsi,
411 LWS_EXT_CALLBACK_DESTROY,
412 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000413
414 free(wsi->active_extensions_user[n]);
415 }
416
Andy Greena41314f2011-05-23 10:00:03 +0100417 /*
418 * inform all extensions in case they tracked this guy out of band
419 * even though not active on him specifically
420 */
421
422 ext = context->extensions;
423 while (ext && ext->callback) {
424 ext->callback(context, ext, wsi,
425 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
426 NULL, NULL, 0);
427 ext++;
428 }
Andy Green3182ece2013-01-20 17:08:31 +0800429#endif
Andy Greena41314f2011-05-23 10:00:03 +0100430
Andy Green43db0452013-01-10 19:50:35 +0800431/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000432
Andy Green3faa9c72010-11-08 17:03:03 +0000433#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000434 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000435 n = SSL_get_fd(wsi->ssl);
436 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800437 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000438 SSL_free(wsi->ssl);
439 } else {
440#endif
Andy Green0303db42013-01-17 14:46:43 +0800441 if (wsi->sock) {
442 n = shutdown(wsi->sock, SHUT_RDWR);
443 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800444 lwsl_debug("closing: shutdown returned %d\n",
445 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800446
Andy Green0303db42013-01-17 14:46:43 +0800447 n = compatible_close(wsi->sock);
448 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800449 lwsl_debug("closing: close returned %d\n",
450 errno);
Andy Green0303db42013-01-17 14:46:43 +0800451 }
Andy Green3faa9c72010-11-08 17:03:03 +0000452#ifdef LWS_OPENSSL_SUPPORT
453 }
454#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800455 if (wsi->protocol && wsi->protocol->per_session_data_size &&
456 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000457 free(wsi->user_space);
458
Andy Green251f6fa2010-11-03 11:13:06 +0000459 free(wsi);
460}
461
Andy Green07034092011-02-13 08:37:12 +0000462/**
463 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800464 * @context: Libwebsockets context
465 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000466 * @fd: Connection socket descriptor
467 * @name: Buffer to take client address name
468 * @name_len: Length of client address name buffer
469 * @rip: Buffer to take client address IP qotted quad
470 * @rip_len: Length of client address IP buffer
471 *
472 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800473 * the client connected with socket descriptor @fd. Names may be
474 * truncated if there is not enough room. If either cannot be
475 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000476 */
477
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800478LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800479libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
480 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000481 char *rip, int rip_len)
482{
Bob Robertsac049112013-04-25 09:16:30 +0800483 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000484 struct sockaddr_in sin;
485 struct hostent *host;
486 struct hostent *host1;
487 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000488 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000489 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800490 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800491#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800492 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800493#endif
Andy Green07034092011-02-13 08:37:12 +0000494
495 rip[0] = '\0';
496 name[0] = '\0';
497
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800498 lws_latency_pre(context, wsi);
499
Andy Greenb5b23192013-02-11 17:13:32 +0800500 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000501 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
502 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800503 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000504 }
Andy Green6ee372f2012-04-09 15:09:01 +0800505
Andy Greenb5b23192013-02-11 17:13:32 +0800506 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000507 AF_INET);
508 if (host == NULL) {
509 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800510 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000511 }
512
513 strncpy(name, host->h_name, name_len);
514 name[name_len - 1] = '\0';
515
516 host1 = gethostbyname(host->h_name);
517 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800518 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000519 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000520 n = 0;
521 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000522 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000523 if (p == NULL)
524 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000525 if ((host1->h_addrtype != AF_INET)
526#ifdef AF_LOCAL
527 && (host1->h_addrtype != AF_LOCAL)
528#endif
529 )
Andy Green07034092011-02-13 08:37:12 +0000530 continue;
531
Andy Green7627af52011-03-09 15:13:52 +0000532 if (host1->h_addrtype == AF_INET)
533 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000534#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000535 else {
536 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800537 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000538 ip[sizeof(ip) - 1] = '\0';
539 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000540#endif
Andy Green07034092011-02-13 08:37:12 +0000541 p = NULL;
542 strncpy(rip, ip, rip_len);
543 rip[rip_len - 1] = '\0';
544 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800545
546 ret = 0;
547bail:
548 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000549}
Andy Green9f990342011-02-12 11:57:45 +0000550
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800551LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000552 void *buf, int len)
553{
554 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800555 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000556
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100557#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000558 for (n = 0; n < len; n++)
559 p[n] = (unsigned char)rand();
560#else
561 n = read(context->fd_random, p, len);
562#endif
563
564 return n;
565}
566
Andy Greena690cd02013-02-09 12:25:31 +0800567int lws_set_socket_options(struct libwebsocket_context *context, int fd)
568{
569 int optval = 1;
570 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100571#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800572 unsigned long optl = 0;
573#endif
574#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
575 struct protoent *tcp_proto;
576#endif
577
578 if (context->ka_time) {
579 /* enable keepalive on this socket */
580 optval = 1;
581 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
582 (const void *)&optval, optlen) < 0)
583 return 1;
584
Bob Robertsac049112013-04-25 09:16:30 +0800585#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800586
587 /*
588 * didn't find a way to set these per-socket, need to
589 * tune kernel systemwide values
590 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100591#elif WIN32
592 {
593 DWORD dwBytesRet;
594 struct tcp_keepalive alive;
595 alive.onoff = TRUE;
596 alive.keepalivetime = context->ka_time;
597 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800598
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100599 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
600 NULL, 0, &dwBytesRet, NULL, NULL))
601 return 1;
602 }
Andy Greena47865f2013-02-10 09:39:47 +0800603#else
Andy Greena690cd02013-02-09 12:25:31 +0800604 /* set the keepalive conditions we want on it too */
605 optval = context->ka_time;
606 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
607 (const void *)&optval, optlen) < 0)
608 return 1;
609
Larry Hayesbb66ac62013-02-22 09:16:20 +0800610 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800611 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
612 (const void *)&optval, optlen) < 0)
613 return 1;
614
Larry Hayesbb66ac62013-02-22 09:16:20 +0800615 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800616 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
617 (const void *)&optval, optlen) < 0)
618 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800619#endif
Andy Greena690cd02013-02-09 12:25:31 +0800620 }
621
622 /* Disable Nagle */
623 optval = 1;
624#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
625 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
626#else
627 tcp_proto = getprotobyname("TCP");
628 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
629#endif
630
631 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100632#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800633 ioctlsocket(fd, FIONBIO, &optl);
634#else
635 fcntl(fd, F_SETFL, O_NONBLOCK);
636#endif
637
638 return 0;
639}
640
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800641LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000642{
643 struct pollfd fds;
644
645 fds.fd = wsi->sock;
646 fds.events = POLLOUT;
647 fds.revents = 0;
648
649 if (poll(&fds, 1, 0) != 1)
650 return 1;
651
652 if ((fds.revents & POLLOUT) == 0)
653 return 1;
654
655 /* okay to send another packet without blocking */
656
657 return 0;
658}
659
Andy Greena41314f2011-05-23 10:00:03 +0100660int
Andy Green3b84c002011-03-06 13:14:42 +0000661lws_handle_POLLOUT_event(struct libwebsocket_context *context,
662 struct libwebsocket *wsi, struct pollfd *pollfd)
663{
Andy Green3b84c002011-03-06 13:14:42 +0000664 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800665
Andy Green3182ece2013-01-20 17:08:31 +0800666#ifndef LWS_NO_EXTENSIONS
667 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000668 int ret;
669 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100670 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000671
Andy Green1f4267b2013-10-17 08:09:19 +0800672 /* pending truncated sends have uber priority */
673
674 if (wsi->u.ws.truncated_send_malloc) {
675 lws_issue_raw(wsi, wsi->u.ws.truncated_send_malloc +
676 wsi->u.ws.truncated_send_offset,
677 wsi->u.ws.truncated_send_len);
678 /* leave POLLOUT active either way */
679 return 0;
680 }
681
Andy Greena41314f2011-05-23 10:00:03 +0100682 for (n = 0; n < wsi->count_active_extensions; n++) {
683 if (!wsi->active_extensions[n]->callback)
684 continue;
685
686 m = wsi->active_extensions[n]->callback(context,
687 wsi->active_extensions[n], wsi,
688 LWS_EXT_CALLBACK_IS_WRITEABLE,
689 wsi->active_extensions_user[n], NULL, 0);
690 if (m > handled)
691 handled = m;
692 }
693
694 if (handled == 1)
695 goto notify_action;
696
697 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000698 goto user_service;
699
700 /*
701 * check in on the active extensions, see if they
702 * had pending stuff to spill... they need to get the
703 * first look-in otherwise sequence will be disordered
704 *
705 * NULL, zero-length eff_buf means just spill pending
706 */
707
708 ret = 1;
709 while (ret == 1) {
710
711 /* default to nobody has more to spill */
712
713 ret = 0;
714 eff_buf.token = NULL;
715 eff_buf.token_len = 0;
716
717 /* give every extension a chance to spill */
718
719 for (n = 0; n < wsi->count_active_extensions; n++) {
720 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000721 wsi->protocol->owning_server,
722 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000723 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
724 wsi->active_extensions_user[n], &eff_buf, 0);
725 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800726 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000727 return -1;
728 }
729 if (m)
730 /*
731 * at least one extension told us he has more
732 * to spill, so we will go around again after
733 */
734 ret = 1;
735 }
736
737 /* assuming they gave us something to send, send it */
738
739 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800740 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
741 eff_buf.token_len);
742 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000743 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800744 /*
745 * Keep amount spilled small to minimize chance of this
746 */
747 if (n != eff_buf.token_len) {
748 lwsl_err("Unable to spill ext %d vs %s\n",
749 eff_buf.token_len, n);
750 return -1;
751 }
Andy Green3b84c002011-03-06 13:14:42 +0000752 } else
753 continue;
754
755 /* no extension has more to spill */
756
757 if (!ret)
758 continue;
759
760 /*
761 * There's more to spill from an extension, but we just sent
762 * something... did that leave the pipe choked?
763 */
764
765 if (!lws_send_pipe_choked(wsi))
766 /* no we could add more */
767 continue;
768
Andy Green43db0452013-01-10 19:50:35 +0800769 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000770
771 /*
772 * Yes, he's choked. Leave the POLLOUT masked on so we will
773 * come back here when he is unchoked. Don't call the user
774 * callback to enforce ordering of spilling, he'll get called
775 * when we come back here and there's nothing more to spill.
776 */
777
778 return 0;
779 }
780
781 wsi->extension_data_pending = 0;
782
783user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800784#endif
Andy Green3b84c002011-03-06 13:14:42 +0000785 /* one shot */
786
Andy Greena41314f2011-05-23 10:00:03 +0100787 if (pollfd) {
788 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000789
Andy Greena41314f2011-05-23 10:00:03 +0100790 /* external POLL support via protocol 0 */
791 context->protocols[0].callback(context, wsi,
792 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800793 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100794 }
Andy Green3182ece2013-01-20 17:08:31 +0800795#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100796notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800797#endif
Andy Green3b84c002011-03-06 13:14:42 +0000798
Andy Green9e4c2b62011-03-07 20:47:39 +0000799 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
800 n = LWS_CALLBACK_CLIENT_WRITEABLE;
801 else
802 n = LWS_CALLBACK_SERVER_WRITEABLE;
803
Andy Greenb8b247d2013-01-22 07:20:08 +0800804 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800805 wsi, (enum libwebsocket_callback_reasons) n,
806 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000807}
808
809
810
Andy Green1c6e1422013-02-20 19:11:31 +0800811int
Andy Greena41314f2011-05-23 10:00:03 +0100812libwebsocket_service_timeout_check(struct libwebsocket_context *context,
813 struct libwebsocket *wsi, unsigned int sec)
814{
Andy Green3182ece2013-01-20 17:08:31 +0800815#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100816 int n;
817
818 /*
819 * if extensions want in on it (eg, we are a mux parent)
820 * give them a chance to service child timeouts
821 */
822
823 for (n = 0; n < wsi->count_active_extensions; n++)
824 wsi->active_extensions[n]->callback(
825 context, wsi->active_extensions[n],
826 wsi, LWS_EXT_CALLBACK_1HZ,
827 wsi->active_extensions_user[n], NULL, sec);
828
Andy Green3182ece2013-01-20 17:08:31 +0800829#endif
Andy Greena41314f2011-05-23 10:00:03 +0100830 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800831 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800832
Andy Greena41314f2011-05-23 10:00:03 +0100833 /*
834 * if we went beyond the allowed time, kill the
835 * connection
836 */
837
838 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800839 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100840 libwebsocket_close_and_free_session(context,
841 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800842 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100843 }
Andy Green1c6e1422013-02-20 19:11:31 +0800844
845 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100846}
847
Andy Green9f990342011-02-12 11:57:45 +0000848/**
849 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000850 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000851 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800852 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000853 *
Andy Green75006172013-01-22 12:32:11 +0800854 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800855 * services it according to the state of the associated
856 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800857 *
858 * The one call deals with all "service" that might happen on a socket
859 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800860 *
861 * If a pollfd says it has something, you can just pass it to
862 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
863 * If it sees it is a lws socket, the traffic will be handled and
864 * pollfd->revents will be zeroed now.
865 *
866 * If the socket is foreign to lws, it leaves revents alone. So you can
867 * see if you should service yourself by checking the pollfd revents
868 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000869 */
870
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800871LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000872libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000873 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000874{
Andy Greena1ce6be2013-01-18 11:43:21 +0800875 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000876 int n;
Andy Green0d338332011-02-12 11:57:43 +0000877 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800878 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000879 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800880 int timed_out = 0;
881 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800882 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800883
Andy Green3182ece2013-01-20 17:08:31 +0800884#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000885 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800886#endif
Andy Green98a717c2011-03-06 13:14:15 +0000887 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800888
889 if (context->listen_service_fd)
890 listen_socket_fds_index = context->lws_lookup[
891 context->listen_service_fd]->position_in_fds_table;
892
Andy Greena71eafc2011-02-14 17:59:43 +0000893 /*
894 * you can call us with pollfd = NULL to just allow the once-per-second
895 * global timeout checks; if less than a second since the last check
896 * it returns immediately then.
897 */
898
899 gettimeofday(&tv, NULL);
900
Peter Hinz56885f32011-03-02 22:03:47 +0000901 if (context->last_timeout_check_s != tv.tv_sec) {
902 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000903
Joakim Soderberg4c531232013-02-06 15:26:58 +0900904 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800905 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800906 if (context->started_with_parent &&
907 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800908 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900909 #endif
Andy Green24cba922013-01-19 13:56:10 +0800910
Andy Greena71eafc2011-02-14 17:59:43 +0000911 /* global timeout check once per second */
912
Andy Green1c6e1422013-02-20 19:11:31 +0800913 if (pollfd)
914 our_fd = pollfd->fd;
915
Peter Hinz56885f32011-03-02 22:03:47 +0000916 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800917 m = context->fds[n].fd;
918 wsi = context->lws_lookup[m];
919 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800920 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800921
922 if (libwebsocket_service_timeout_check(context, wsi,
923 tv.tv_sec))
924 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800925 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800926 /* it was the guy we came to service! */
927 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800928 /* mark as handled */
929 pollfd->revents = 0;
930 }
Andy Greena71eafc2011-02-14 17:59:43 +0000931 }
932 }
933
Andy Green1c6e1422013-02-20 19:11:31 +0800934 /* the socket we came to service timed out, nothing to do */
935 if (timed_out)
936 return 0;
937
Andy Greena71eafc2011-02-14 17:59:43 +0000938 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000939 if (pollfd == NULL)
940 return 0;
941
942 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800943 wsi = context->lws_lookup[pollfd->fd];
944 if (wsi == NULL)
945 /* not lws connection ... leave revents alone and return */
946 return 0;
947
948 /*
949 * so that caller can tell we handled, past here we need to
950 * zero down pollfd->revents after handling
951 */
952
Andy Green65b0e912013-01-16 07:59:47 +0800953 /*
954 * deal with listen service piggybacking
955 * every listen_service_modulo services of other fds, we
956 * sneak one in to service the listen socket if there's anything waiting
957 *
958 * To handle connection storms, as found in ab, if we previously saw a
959 * pending connection here, it causes us to check again next time.
960 */
961
Andy Greenb5b23192013-02-11 17:13:32 +0800962 if (context->listen_service_fd && pollfd !=
963 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800964 context->listen_service_count++;
965 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800966 context->listen_service_count ==
967 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800968 context->listen_service_count = 0;
969 m = 1;
970 if (context->listen_service_extraseen > 5)
971 m = 2;
972 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800973 /*
974 * even with extpoll, we prepared this
975 * internal fds for listen
976 */
977 n = poll(&context->fds[listen_socket_fds_index],
978 1, 0);
979 if (n > 0) { /* there's a conn waiting for us */
980 libwebsocket_service_fd(context,
981 &context->
982 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800983 context->listen_service_extraseen++;
984 } else {
985 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800986 context->
987 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800988 break;
989 }
990 }
991 }
992
993 }
994
Graham Newtonb1aa1d32013-11-04 09:47:06 +0800995 /* handle session socket closed */
996
997 if ((!(pollfd->revents & POLLIN)) &&
998 (pollfd->revents & (POLLERR | POLLHUP))) {
999
1000 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1001 (void *)wsi, pollfd->fd);
1002
1003 goto close_and_handled;
1004 }
1005
Andy Green65b0e912013-01-16 07:59:47 +08001006 /* okay, what we came here to do... */
1007
Andy Green0d338332011-02-12 11:57:43 +00001008 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001009
Andy Greena1ce6be2013-01-18 11:43:21 +08001010#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001011 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001012 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001013 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001014 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001015 n = lws_server_socket_service(context, wsi, pollfd);
1016 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001017#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001018
Andy Green0d338332011-02-12 11:57:43 +00001019 case LWS_CONNMODE_WS_SERVING:
1020 case LWS_CONNMODE_WS_CLIENT:
1021
Andy Green0d338332011-02-12 11:57:43 +00001022 /* the guy requested a callback when it was OK to write */
1023
Andy Greenda527df2011-03-07 07:08:12 +00001024 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001025 wsi->state == WSI_STATE_ESTABLISHED &&
1026 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +08001027 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001028 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001029 }
Andy Green0d338332011-02-12 11:57:43 +00001030
Andy Greenca0a1292013-03-16 11:24:23 +08001031 if (wsi->u.ws.rxflow_buffer &&
1032 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1033 lwsl_info("draining rxflow\n");
1034 /* well, drain it */
1035 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1036 wsi->u.ws.rxflow_pos;
1037 eff_buf.token_len = wsi->u.ws.rxflow_len -
1038 wsi->u.ws.rxflow_pos;
1039 draining_flow = 1;
1040 goto drain;
1041 }
1042
Andy Green0d338332011-02-12 11:57:43 +00001043 /* any incoming data ready? */
1044
1045 if (!(pollfd->revents & POLLIN))
1046 break;
1047
Andy Greenb45993c2010-12-18 15:13:50 +00001048#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001049read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001050 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001051 eff_buf.token_len = SSL_read(wsi->ssl,
1052 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001053 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001054 if (!eff_buf.token_len) {
1055 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001056 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001057 ERR_error_string(n,
1058 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001059 }
1060 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001061#endif
Andy Greene84652c2013-02-08 13:01:02 +08001062 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001063 context->service_buffer,
1064 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001065
Andy Green98a717c2011-03-06 13:14:15 +00001066 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001067 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1068 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001069 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001070 goto close_and_handled;
1071 n = 0;
1072 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001073 }
Andy Green98a717c2011-03-06 13:14:15 +00001074 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001075 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001076 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001077 }
1078
Andy Green98a717c2011-03-06 13:14:15 +00001079 /*
1080 * give any active extensions a chance to munge the buffer
1081 * before parse. We pass in a pointer to an lws_tokens struct
1082 * prepared with the default buffer and content length that's in
1083 * there. Rather than rewrite the default buffer, extensions
1084 * that expect to grow the buffer can adapt .token to
1085 * point to their own per-connection buffer in the extension
1086 * user allocation. By default with no extensions or no
1087 * extension callback handling, just the normal input buffer is
1088 * used then so it is efficient.
1089 */
Andy Greenb45993c2010-12-18 15:13:50 +00001090
Andy Greene84652c2013-02-08 13:01:02 +08001091 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001092drain:
Andy Green3182ece2013-01-20 17:08:31 +08001093#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001094 more = 1;
1095 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001096
Andy Green98a717c2011-03-06 13:14:15 +00001097 more = 0;
1098
1099 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001100 m = wsi->active_extensions[n]->callback(context,
1101 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001102 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001103 wsi->active_extensions_user[n],
1104 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001105 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001106 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001107 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001108 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001109 }
1110 if (m)
1111 more = 1;
1112 }
Andy Green3182ece2013-01-20 17:08:31 +08001113#endif
Andy Green98a717c2011-03-06 13:14:15 +00001114 /* service incoming data */
1115
1116 if (eff_buf.token_len) {
1117 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001118 (unsigned char *)eff_buf.token,
1119 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001120 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001121 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001122 n = 0;
1123 goto handled;
1124 }
Andy Green98a717c2011-03-06 13:14:15 +00001125 }
Andy Green3182ece2013-01-20 17:08:31 +08001126#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001127 eff_buf.token = NULL;
1128 eff_buf.token_len = 0;
1129 }
Andy Green3182ece2013-01-20 17:08:31 +08001130#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001131 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1132 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1133 lwsl_info("flow buffer: drained\n");
1134 free(wsi->u.ws.rxflow_buffer);
1135 wsi->u.ws.rxflow_buffer = NULL;
1136 /* having drained the rxflow buffer, can rearm POLLIN */
1137 _libwebsocket_rx_flow_control(wsi);
1138 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001139
1140#ifdef LWS_OPENSSL_SUPPORT
1141 if (wsi->ssl && SSL_pending(wsi->ssl))
1142 goto read_pending;
1143#endif
Andy Green98a717c2011-03-06 13:14:15 +00001144 break;
Andy Green76f61e72013-01-16 11:53:05 +08001145
1146 default:
Andy Green03674a62013-01-16 11:47:40 +08001147#ifdef LWS_NO_CLIENT
1148 break;
1149#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001150 n = lws_client_socket_service(context, wsi, pollfd);
1151 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001152#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001153 }
1154
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001155 n = 0;
1156 goto handled;
1157
1158close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001159 libwebsocket_close_and_free_session(context, wsi,
1160 LWS_CLOSE_STATUS_NOSTATUS);
1161 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001162
1163handled:
1164 pollfd->revents = 0;
1165 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001166}
1167
Andy Green0d338332011-02-12 11:57:43 +00001168
Andy Green6964bb52011-01-23 16:50:33 +00001169/**
1170 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001171 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001172 *
1173 * This function closes any active connections and then frees the
1174 * context. After calling this, any further use of the context is
1175 * undefined.
1176 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001177LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001178libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001179{
Andy Green3182ece2013-01-20 17:08:31 +08001180#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001181 int n;
1182 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001183 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001184 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001185
Andy Greend636e352013-01-29 12:36:17 +08001186#ifdef LWS_LATENCY
1187 if (context->worst_latency_info[0])
1188 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1189#endif
1190
Andy Greendfb23042013-01-17 12:26:48 +08001191 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001192 struct libwebsocket *wsi =
1193 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001194 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001195 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1196 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001197 }
Andy Green6964bb52011-01-23 16:50:33 +00001198
Andy Greena41314f2011-05-23 10:00:03 +01001199 /*
1200 * give all extensions a chance to clean up any per-context
1201 * allocations they might have made
1202 */
1203
1204 ext = context->extensions;
1205 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1206 if (context->listen_port)
1207 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001208 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001209 ext->callback(context, ext, NULL,
1210 (enum libwebsocket_extension_callback_reasons)m,
1211 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001212 ext++;
1213 }
Andy Greena7109e62013-02-11 12:05:54 +08001214
1215 /*
1216 * inform all the protocols that they are done and will have no more
1217 * callbacks
1218 */
1219
1220 while (protocol->callback) {
1221 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1222 NULL, NULL, 0);
1223 protocol++;
1224 }
1225
Andy Green3182ece2013-01-20 17:08:31 +08001226#endif
Andy Greena41314f2011-05-23 10:00:03 +01001227
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001228#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001229#else
1230 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001231#endif
1232
Peter Hinz56885f32011-03-02 22:03:47 +00001233#ifdef LWS_OPENSSL_SUPPORT
1234 if (context->ssl_ctx)
1235 SSL_CTX_free(context->ssl_ctx);
1236 if (context->ssl_client_ctx)
1237 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001238
1239 ERR_remove_state(0);
1240 ERR_free_strings();
1241 EVP_cleanup();
1242 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001243#endif
1244
Andy Green46596482013-02-11 11:04:01 +08001245 if (context->fds)
1246 free(context->fds);
1247 if (context->lws_lookup)
1248 free(context->lws_lookup);
1249
Peter Hinz56885f32011-03-02 22:03:47 +00001250 free(context);
1251
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001252#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001253 WSACleanup();
1254#endif
Andy Green6964bb52011-01-23 16:50:33 +00001255}
1256
Andy Greend88146d2013-01-22 12:40:35 +08001257/**
Andy Greenb5b23192013-02-11 17:13:32 +08001258 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001259 * @context: Websocket context
1260 *
1261 * This returns the optional user allocation that can be attached to
1262 * the context the sockets live in at context_create time. It's a way
1263 * to let all sockets serviced in the same context share data without
1264 * using globals statics in the user code.
1265 */
Alon Levy0291eb32012-10-19 11:21:56 +02001266LWS_EXTERN void *
1267libwebsocket_context_user(struct libwebsocket_context *context)
1268{
Andy Greenb5b23192013-02-11 17:13:32 +08001269 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001270}
1271
Andy Green6964bb52011-01-23 16:50:33 +00001272/**
1273 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001274 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001275 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1276 * service otherwise block and service immediately, returning
1277 * after the timeout if nothing needed service.
1278 *
1279 * This function deals with any pending websocket traffic, for three
1280 * kinds of event. It handles these events on both server and client
1281 * types of connection the same.
1282 *
1283 * 1) Accept new connections to our context's server
1284 *
Andy Green6f520a52013-01-29 17:57:39 +08001285 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001286 * server or client connections.
1287 *
1288 * You need to call this service function periodically to all the above
1289 * functions to happen; if your application is single-threaded you can
1290 * just call it in your main event loop.
1291 *
1292 * Alternatively you can fork a new process that asynchronously handles
1293 * calling this service in a loop. In that case you are happy if this
1294 * call blocks your thread until it needs to take care of something and
1295 * would call it with a large nonzero timeout. Your loop then takes no
1296 * CPU while there is nothing happening.
1297 *
1298 * If you are calling it in a single-threaded app, you don't want it to
1299 * wait around blocking other things in your loop from happening, so you
1300 * would call it with a timeout_ms of 0, so it returns immediately if
1301 * nothing is pending, or as soon as it services whatever was pending.
1302 */
1303
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001304LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001305libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001306{
1307 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001308 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001309
1310 /* stay dead once we are dead */
1311
Peter Hinz56885f32011-03-02 22:03:47 +00001312 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001313 return 1;
1314
Andy Green0d338332011-02-12 11:57:43 +00001315 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001316
Peter Hinz56885f32011-03-02 22:03:47 +00001317 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green5dc62ea2013-09-20 20:26:12 +08001318 if (n == 0) /* poll timeout */ {
1319 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001320 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001321 }
Andy Greene92cd172011-01-19 13:11:55 +00001322
Andy Greendfb23042013-01-17 12:26:48 +08001323 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001324 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001325
Andy Greendfb23042013-01-17 12:26:48 +08001326 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001327
Andy Greenca0a1292013-03-16 11:24:23 +08001328 for (n = 0; n < context->fds_count; n++) {
1329 if (!context->fds[n].revents)
1330 continue;
1331 m = libwebsocket_service_fd(context, &context->fds[n]);
1332 if (m < 0)
1333 return -1;
1334 /* if something closed, retry this slot */
1335 if (m)
1336 n--;
1337 }
1338
Andy Greene92cd172011-01-19 13:11:55 +00001339 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001340}
1341
Andy Green3182ece2013-01-20 17:08:31 +08001342#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001343int
1344lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001345 struct libwebsocket *wsi,
1346 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001347 void *v, size_t len)
1348{
1349 int n;
1350 int handled = 0;
1351
1352 /* maybe an extension will take care of it for us */
1353
1354 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1355 if (!wsi->active_extensions[n]->callback)
1356 continue;
1357
1358 handled |= wsi->active_extensions[n]->callback(context,
1359 wsi->active_extensions[n], wsi,
1360 r, wsi->active_extensions_user[n], v, len);
1361 }
1362
1363 return handled;
1364}
1365
1366
1367void *
1368lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001369 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001370{
1371 int n = 0;
1372
Andy Green68b45042011-05-25 21:41:57 +01001373 if (wsi == NULL)
1374 return NULL;
1375
Andy Greena41314f2011-05-23 10:00:03 +01001376 while (n < wsi->count_active_extensions) {
1377 if (wsi->active_extensions[n] != ext) {
1378 n++;
1379 continue;
1380 }
1381 return wsi->active_extensions_user[n];
1382 }
1383
1384 return NULL;
1385}
Andy Green3182ece2013-01-20 17:08:31 +08001386#endif
Andy Greena41314f2011-05-23 10:00:03 +01001387
Andy Green90c7cbc2011-01-27 06:26:52 +00001388/**
1389 * libwebsocket_callback_on_writable() - Request a callback when this socket
1390 * becomes able to be written to without
1391 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001392 *
Peter Hinz56885f32011-03-02 22:03:47 +00001393 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001394 * @wsi: Websocket connection instance to get callback for
1395 */
1396
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001397LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001398libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001399 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001400{
Andy Green3182ece2013-01-20 17:08:31 +08001401#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001402 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001403 int handled = 0;
1404
1405 /* maybe an extension will take care of it for us */
1406
1407 for (n = 0; n < wsi->count_active_extensions; n++) {
1408 if (!wsi->active_extensions[n]->callback)
1409 continue;
1410
1411 handled |= wsi->active_extensions[n]->callback(context,
1412 wsi->active_extensions[n], wsi,
1413 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1414 wsi->active_extensions_user[n], NULL, 0);
1415 }
1416
1417 if (handled)
1418 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001419#endif
Andy Greendfb23042013-01-17 12:26:48 +08001420 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001421 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1422 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001423 return -1;
1424 }
1425
1426 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001427
Andy Green3221f922011-02-12 13:14:11 +00001428 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001429 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001430 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001431 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001432
Andy Green90c7cbc2011-01-27 06:26:52 +00001433 return 1;
1434}
1435
1436/**
1437 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1438 * all connections using the given protocol when it
1439 * becomes possible to write to each socket without
1440 * blocking in turn.
1441 *
1442 * @protocol: Protocol whose connections will get callbacks
1443 */
1444
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001445LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001446libwebsocket_callback_on_writable_all_protocol(
1447 const struct libwebsocket_protocols *protocol)
1448{
Peter Hinz56885f32011-03-02 22:03:47 +00001449 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001450 int n;
Andy Green0d338332011-02-12 11:57:43 +00001451 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001452
Andy Greendfb23042013-01-17 12:26:48 +08001453 for (n = 0; n < context->fds_count; n++) {
1454 wsi = context->lws_lookup[context->fds[n].fd];
1455 if (!wsi)
1456 continue;
1457 if (wsi->protocol == protocol)
1458 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001459 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001460
1461 return 0;
1462}
1463
Andy Greenbe93fef2011-02-14 20:25:43 +00001464/**
1465 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1466 *
1467 * You will not need this unless you are doing something special
1468 *
1469 * @wsi: Websocket connection instance
1470 * @reason: timeout reason
1471 * @secs: how many seconds
1472 */
1473
1474void
1475libwebsocket_set_timeout(struct libwebsocket *wsi,
1476 enum pending_timeout reason, int secs)
1477{
1478 struct timeval tv;
1479
1480 gettimeofday(&tv, NULL);
1481
1482 wsi->pending_timeout_limit = tv.tv_sec + secs;
1483 wsi->pending_timeout = reason;
1484}
1485
Andy Greena6cbece2011-01-27 20:06:03 +00001486
1487/**
1488 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1489 *
1490 * You will not need this unless you are doing something special
1491 *
1492 * @wsi: Websocket connection instance
1493 */
1494
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001495LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001496libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1497{
1498 return wsi->sock;
1499}
1500
Andy Greend636e352013-01-29 12:36:17 +08001501#ifdef LWS_LATENCY
1502void
Andy Greenb5b23192013-02-11 17:13:32 +08001503lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1504 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001505{
1506 struct timeval tv;
1507 unsigned long u;
1508 char buf[256];
1509
1510 gettimeofday(&tv, NULL);
1511
1512 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1513
1514 if (action) {
1515 if (completed) {
1516 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001517 sprintf(buf,
1518 "Completion first try lat %luus: %p: ret %d: %s\n",
1519 u - wsi->latency_start,
1520 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001521 else
Andy Greenb5b23192013-02-11 17:13:32 +08001522 sprintf(buf,
1523 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1524 u - wsi->action_start,
1525 u - wsi->latency_start,
1526 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001527 wsi->action_start = 0;
1528 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001529 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1530 u - wsi->latency_start,
1531 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001532 if (u - wsi->latency_start > context->worst_latency) {
1533 context->worst_latency = u - wsi->latency_start;
1534 strcpy(context->worst_latency_info, buf);
1535 }
1536 lwsl_latency("%s", buf);
1537 } else {
1538 wsi->latency_start = u;
1539 if (!wsi->action_start)
1540 wsi->action_start = u;
1541 }
1542}
1543#endif
1544
Andy Greena1ce6be2013-01-18 11:43:21 +08001545#ifdef LWS_NO_SERVER
1546int
Andy Green8d15cf42013-10-24 21:47:06 +08001547_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001548{
1549 return 0;
1550}
1551#else
Andy Green706961d2013-01-17 16:50:35 +08001552int
1553_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1554{
1555 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001556
Andy Greenca0a1292013-03-16 11:24:23 +08001557 /* there is no pending change */
1558 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001559 return 0;
1560
Andy Greenca0a1292013-03-16 11:24:23 +08001561 /* stuff is still buffered, not ready to really accept new input */
1562 if (wsi->u.ws.rxflow_buffer) {
1563 /* get ourselves called back to deal with stashed buffer */
1564 libwebsocket_callback_on_writable(context, wsi);
1565 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001566 }
1567
Andy Greenca0a1292013-03-16 11:24:23 +08001568 /* pending is cleared, we can change rxflow state */
1569
1570 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1571
1572 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1573 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1574
1575 /* adjust the pollfd for this wsi */
1576
1577 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001578 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1579 else
1580 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1581
Andy Greenca0a1292013-03-16 11:24:23 +08001582 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001583 /* external POLL support via protocol 0 */
1584 context->protocols[0].callback(context, wsi,
1585 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001586 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001587 else
1588 /* external POLL support via protocol 0 */
1589 context->protocols[0].callback(context, wsi,
1590 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001591 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001592
1593 return 1;
1594}
Andy Greena1ce6be2013-01-18 11:43:21 +08001595#endif
Andy Green706961d2013-01-17 16:50:35 +08001596
Andy Green90c7cbc2011-01-27 06:26:52 +00001597/**
1598 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1599 * receieved packets.
1600 *
1601 * If the output side of a server process becomes choked, this allows flow
1602 * control for the input side.
1603 *
1604 * @wsi: Websocket connection instance to get callback for
1605 * @enable: 0 = disable read servicing for this connection, 1 = enable
1606 */
1607
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001608LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001609libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1610{
Andy Greenca0a1292013-03-16 11:24:23 +08001611 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1612 return 0;
1613
1614 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1615 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001616
Andy Green706961d2013-01-17 16:50:35 +08001617 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001618}
1619
Andy Greenb55451c2013-03-16 12:32:27 +08001620/**
1621 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1622 *
1623 * When the user server code realizes it can accept more input, it can
1624 * call this to have the RX flow restriction removed from all connections using
1625 * the given protocol.
1626 *
1627 * @protocol: all connections using this protocol will be allowed to receive
1628 */
1629
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001630LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001631libwebsocket_rx_flow_allow_all_protocol(
1632 const struct libwebsocket_protocols *protocol)
1633{
1634 struct libwebsocket_context *context = protocol->owning_server;
1635 int n;
1636 struct libwebsocket *wsi;
1637
1638 for (n = 0; n < context->fds_count; n++) {
1639 wsi = context->lws_lookup[context->fds[n].fd];
1640 if (!wsi)
1641 continue;
1642 if (wsi->protocol == protocol)
1643 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1644 }
1645}
1646
Andy Green706961d2013-01-17 16:50:35 +08001647
Andy Green2ac5a6f2011-01-28 10:00:18 +00001648/**
1649 * libwebsocket_canonical_hostname() - returns this host's hostname
1650 *
1651 * This is typically used by client code to fill in the host parameter
1652 * when making a client connection. You can only call it after the context
1653 * has been created.
1654 *
Peter Hinz56885f32011-03-02 22:03:47 +00001655 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001656 */
1657
1658
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001659LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001660libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001661{
Peter Hinz56885f32011-03-02 22:03:47 +00001662 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001663}
1664
1665
Andy Green90c7cbc2011-01-27 06:26:52 +00001666static void sigpipe_handler(int x)
1667{
1668}
1669
Andy Green6901cb32011-02-21 08:06:47 +00001670#ifdef LWS_OPENSSL_SUPPORT
1671static int
1672OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1673{
1674
1675 SSL *ssl;
1676 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001677 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001678
1679 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1680 SSL_get_ex_data_X509_STORE_CTX_idx());
1681
1682 /*
Andy Green2e24da02011-03-05 16:12:04 +00001683 * !!! nasty openssl requires the index to come as a library-scope
1684 * static
Andy Green6901cb32011-02-21 08:06:47 +00001685 */
Andy Green2e24da02011-03-05 16:12:04 +00001686 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001687
Peter Hinz56885f32011-03-02 22:03:47 +00001688 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001689 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1690 x509_ctx, ssl, preverify_ok);
1691
1692 /* convert return code from 0 = OK to 1 = OK */
1693
1694 if (!n)
1695 n = 1;
1696 else
1697 n = 0;
1698
1699 return n;
1700}
1701#endif
1702
Andy Green706961d2013-01-17 16:50:35 +08001703int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001704 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001705 struct libwebsocket *wsi,
1706 enum libwebsocket_callback_reasons reason, void *user,
1707 void *in, size_t len)
1708{
1709 int n;
1710
1711 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001712 if (!n)
1713 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001714
Andy Greenaedc9532013-02-10 21:21:24 +08001715 return n;
Andy Green706961d2013-01-17 16:50:35 +08001716}
1717
Andy Greenb45993c2010-12-18 15:13:50 +00001718
Andy Greenab990e42010-10-31 12:42:52 +00001719/**
Andy Green4739e5c2011-01-22 12:51:57 +00001720 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001721 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001722 *
Andy Green1b265272013-02-09 14:01:09 +08001723 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001724 * of all initialization in one step.
1725 *
Andy Greene92cd172011-01-19 13:11:55 +00001726 * After initialization, it returns a struct libwebsocket_context * that
1727 * represents this server. After calling, user code needs to take care
1728 * of calling libwebsocket_service() with the context pointer to get the
1729 * server's sockets serviced. This can be done in the same process context
1730 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001731 *
Andy Green8f037e42010-12-19 22:13:26 +00001732 * The protocol callback functions are called for a handful of events
1733 * including http requests coming in, websocket connections becoming
1734 * established, and data arriving; it's also called periodically to allow
1735 * async transmission.
1736 *
1737 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1738 * at that time websocket protocol has not been negotiated. Other
1739 * protocols after the first one never see any HTTP callack activity.
1740 *
1741 * The server created is a simple http server by default; part of the
1742 * websocket standard is upgrading this http connection to a websocket one.
1743 *
1744 * This allows the same server to provide files like scripts and favicon /
1745 * images or whatever over http and dynamic data over websockets all in
1746 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001747 */
Andy Green4ea60062010-10-30 12:15:07 +01001748
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001749LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001750libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001751{
Peter Hinz56885f32011-03-02 22:03:47 +00001752 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001753 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001754 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001755#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001756 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001757 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001758 struct sockaddr_in serv_addr;
1759#endif
Andy Green3182ece2013-01-20 17:08:31 +08001760#ifndef LWS_NO_EXTENSIONS
1761 int m;
Andy Green1b265272013-02-09 14:01:09 +08001762 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001763#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001764
Andy Green3faa9c72010-11-08 17:03:03 +00001765#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001766 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001767#endif
1768
Joakim Soderberg4c531232013-02-06 15:26:58 +09001769#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001770 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001771#endif
1772
Andy Greenb3a614a2013-01-19 13:08:17 +08001773 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001774 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001775 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001776 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001777#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001778 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1779 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001780#else
1781 lwsl_notice(" Configured without extension support\n");
1782#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001783 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1784 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001785 if (info->ssl_cipher_list)
1786 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001787 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1788 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001789
Peter Hinz56885f32011-03-02 22:03:47 +00001790#ifdef _WIN32
1791 {
1792 WORD wVersionRequested;
1793 WSADATA wsaData;
1794 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001795 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001796
1797 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1798 wVersionRequested = MAKEWORD(2, 2);
1799
1800 err = WSAStartup(wVersionRequested, &wsaData);
1801 if (err != 0) {
1802 /* Tell the user that we could not find a usable */
1803 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001804 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001805 return NULL;
1806 }
David Galeano7b11fec2011-10-04 19:55:18 +08001807
Andy Green6ee372f2012-04-09 15:09:01 +08001808 /* default to a poll() made out of select() */
1809 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001810
Andy Green6ee372f2012-04-09 15:09:01 +08001811 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001812 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001813 if (wsdll)
1814 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001815
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001816 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001817 if (!poll)
1818 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001819 }
1820#endif
1821
Andy Greenb5b23192013-02-11 17:13:32 +08001822 context = (struct libwebsocket_context *)
1823 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001824 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001825 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001826 return NULL;
1827 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001828 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001829#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001830 context->started_with_parent = pid_daemon;
1831 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1832#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001833
Joakim Soderberg4c531232013-02-06 15:26:58 +09001834 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001835 context->protocols = info->protocols;
1836 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001837 context->http_proxy_port = 0;
1838 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001839 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001840 /* to reduce this allocation, */
1841 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001842 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1843 sizeof(struct libwebsocket_context),
1844 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1845 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001846 sizeof(struct libwebsocket_context) +
1847 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1848 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001849
Andy Greenb5b23192013-02-11 17:13:32 +08001850 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1851 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001852 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001853 lwsl_err("Unable to allocate fds array for %d connections\n",
1854 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001855 free(context);
1856 return NULL;
1857 }
Andy Greenb5b23192013-02-11 17:13:32 +08001858 context->lws_lookup = (struct libwebsocket **)
1859 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001860 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001861 lwsl_err(
1862 "Unable to allocate lws_lookup array for %d connections\n",
1863 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001864 free(context->fds);
1865 free(context);
1866 return NULL;
1867 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001868 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1869 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001870
Peter Hinz56885f32011-03-02 22:03:47 +00001871 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001872#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001873 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001874#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001875 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001876 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001877
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001878#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001879 context->fd_random = 0;
1880#else
1881 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1882 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001883 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001884 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001885 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001886 }
Peter Hinz56885f32011-03-02 22:03:47 +00001887#endif
Andy Green44eee682011-02-10 09:32:24 +00001888
Peter Hinz56885f32011-03-02 22:03:47 +00001889#ifdef LWS_OPENSSL_SUPPORT
1890 context->use_ssl = 0;
1891 context->ssl_ctx = NULL;
1892 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001893 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001894#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001895
Andy Greena1ce6be2013-01-18 11:43:21 +08001896 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001897
Andy Greena1ce6be2013-01-18 11:43:21 +08001898#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001899 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001900 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001901 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001902 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001903
Andy Greenb5b23192013-02-11 17:13:32 +08001904 lwsl_notice(" canonical_hostname = %s\n",
1905 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001906 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001907#endif
Andy Greena69f0512012-05-03 12:32:38 +08001908
Andy Green9659f372011-01-27 22:01:43 +00001909 /* split the proxy ads:port if given */
1910
1911 p = getenv("http_proxy");
1912 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001913 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001914 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001915 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001916 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001917
Peter Hinz56885f32011-03-02 22:03:47 +00001918 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001919 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001920 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001921 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001922 }
1923 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001924 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001925
Andy Greenb3a614a2013-01-19 13:08:17 +08001926 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001927 context->http_proxy_address,
1928 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001929 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001930
Andy Greena1ce6be2013-01-18 11:43:21 +08001931#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001932 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001933
Andy Green3faa9c72010-11-08 17:03:03 +00001934#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001935 context->use_ssl = info->ssl_cert_filepath != NULL &&
1936 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001937#ifdef USE_CYASSL
1938 lwsl_notice(" Compiled with CYASSL support\n");
1939#else
1940 lwsl_notice(" Compiled with OpenSSL support\n");
1941#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001942 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001943 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001944 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001945 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001946
Andy Green90c7cbc2011-01-27 06:26:52 +00001947#else
Andy Green2b40b792013-02-10 22:22:01 +08001948 if (info->ssl_cert_filepath != NULL &&
1949 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001950 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001951 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001952 }
Andy Greenb5b23192013-02-11 17:13:32 +08001953 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001954#endif
Andy Greena17c6922013-01-20 20:21:54 +08001955
Andy Greenb5b23192013-02-11 17:13:32 +08001956 lwsl_notice(
1957 " per-conn mem: %u + %u headers + protocol rx buf\n",
1958 sizeof(struct libwebsocket),
1959 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001960 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001961#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001962
1963 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001964#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001965#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001966 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001967#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001968
1969
1970#ifdef LWS_OPENSSL_SUPPORT
1971
1972 /* basic openssl init */
1973
1974 SSL_library_init();
1975
1976 OpenSSL_add_all_algorithms();
1977 SSL_load_error_strings();
1978
Andy Green2e24da02011-03-05 16:12:04 +00001979 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001980 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1981
Andy Green90c7cbc2011-01-27 06:26:52 +00001982 /*
1983 * Firefox insists on SSLv23 not SSLv3
1984 * Konq disables SSLv2 by default now, SSLv23 works
1985 */
1986
1987 method = (SSL_METHOD *)SSLv23_server_method();
1988 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02001989 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001990 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02001991 error,
1992 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08001993 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001994 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001995 }
Peter Hinz56885f32011-03-02 22:03:47 +00001996 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1997 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02001998 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001999 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002000 error,
2001 ERR_error_string(error,
2002 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002003 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002004 }
2005
David Galeanocc148e42013-01-10 10:18:59 +08002006#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002007 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002008#endif
David Galeano77a677c2013-01-10 10:14:12 +08002009 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002010 if (info->ssl_cipher_list)
2011 SSL_CTX_set_cipher_list(context->ssl_ctx,
2012 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002013
Andy Greena1ce6be2013-01-18 11:43:21 +08002014#ifndef LWS_NO_CLIENT
2015
Andy Green90c7cbc2011-01-27 06:26:52 +00002016 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002017
Andy Green1b265272013-02-09 14:01:09 +08002018 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002019 method = (SSL_METHOD *)SSLv23_client_method();
2020 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002021 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002022 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002023 error,
2024 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002025 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002026 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002027 }
2028 /* create context */
2029 context->ssl_client_ctx = SSL_CTX_new(method);
2030 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002031 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002032 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002033 error,
2034 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002035 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002036 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002037 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002038
David Galeanocc148e42013-01-10 10:18:59 +08002039#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002040 SSL_CTX_set_options(context->ssl_client_ctx,
2041 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002042#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002043 SSL_CTX_set_options(context->ssl_client_ctx,
2044 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002045 if (info->ssl_cipher_list)
2046 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2047 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002048
Peter Hinz56885f32011-03-02 22:03:47 +00002049 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002050 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002051 if (!SSL_CTX_load_verify_locations(
2052 context->ssl_client_ctx, NULL,
2053 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002054 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002055 "Unable to load SSL Client certs from %s "
2056 "(set by --with-client-cert-dir= "
2057 "in configure) -- client ssl isn't "
2058 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002059 } else
2060 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002061 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002062 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002063 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002064 "Unable to load SSL Client certs "
2065 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002066 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002067
2068 /*
2069 * callback allowing user code to load extra verification certs
2070 * helping the client to verify server identity
2071 */
2072
prasannateamf1b353a452013-11-12 17:21:01 -08002073 /* support for client-side certificate authentication */
2074 if (info->ssl_cert_filepath) {
2075 n = SSL_CTX_use_certificate_chain_file(
2076 context->ssl_client_ctx,
2077 info->ssl_cert_filepath);
2078 if (n != 1) {
2079 lwsl_err("problem getting cert '%s' %lu: %s\n",
2080 info->ssl_cert_filepath,
2081 ERR_get_error(),
2082 ERR_error_string(ERR_get_error(),
2083 (char *)context->service_buffer));
2084 goto bail;
2085 }
2086 }
2087 if (info->ssl_private_key_filepath) {
2088 /* set the private key from KeyFile */
2089 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2090 info->ssl_private_key_filepath,
2091 SSL_FILETYPE_PEM) != 1) {
2092 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2093 info->ssl_private_key_filepath,
2094 ERR_get_error(),
2095 ERR_error_string(ERR_get_error(),
2096 (char *)context->service_buffer));
2097 goto bail;
2098 }
2099
2100 /* verify private key */
2101 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2102 lwsl_err("Private SSL key doesn't match cert\n");
2103 goto bail;
2104 }
2105 }
2106
Peter Hinz56885f32011-03-02 22:03:47 +00002107 context->protocols[0].callback(context, NULL,
2108 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2109 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002110 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002111#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002112
Andy Greenc6bf2c22011-02-20 11:10:47 +00002113 /* as a server, are we requiring clients to identify themselves? */
2114
Andy Greenb5b23192013-02-11 17:13:32 +08002115 if (info->options &
2116 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002117
2118 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002119
Peter Hinz56885f32011-03-02 22:03:47 +00002120 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002121 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2122 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002123
2124 /*
2125 * give user code a chance to load certs into the server
2126 * allowing it to verify incoming client certs
2127 */
2128
Peter Hinz56885f32011-03-02 22:03:47 +00002129 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002130 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002131 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002132 }
2133
Peter Hinz56885f32011-03-02 22:03:47 +00002134 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002135
2136 /* openssl init for server sockets */
2137
Andy Green3faa9c72010-11-08 17:03:03 +00002138 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002139 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002140 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002141 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002142 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002143 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002144 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002145 error,
2146 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002147 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002148 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002149 }
2150 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002151 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002152 info->ssl_private_key_filepath,
2153 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002154 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002155 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002156 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002157 error,
2158 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002159 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002160 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002161 }
2162 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002163 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002164 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002165 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002166 }
2167
2168 /* SSL is happy and has a cert it's content with */
2169 }
2170#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002171
Andy Greena1ce6be2013-01-18 11:43:21 +08002172#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002173 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002174
Andy Green1b265272013-02-09 14:01:09 +08002175 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002176 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002177
Andy Green4739e5c2011-01-22 12:51:57 +00002178 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2179 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002180 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002181 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002182 }
Andy Green775c0dd2010-10-29 14:15:22 +01002183
Joakim Soderberg4c531232013-02-06 15:26:58 +09002184#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002185 /*
2186 * allow us to restart even if old sockets in TIME_WAIT
2187 * (REUSEADDR on Unix means, "don't hang on to this
2188 * address after the listener is closed." On Windows, though,
2189 * it means "don't keep other processes from binding to
2190 * this address while we're using it)
2191 */
Andy Green6ee372f2012-04-09 15:09:01 +08002192 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2193 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002194#endif
Andy Green6c939552011-03-08 08:56:57 +00002195
2196 /* Disable Nagle */
2197 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002198 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2199 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002200
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002201 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002202 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002203 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002204 #else
Andy Greene2160712013-01-28 12:19:10 +08002205 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002206 #endif
Andy Greene2160712013-01-28 12:19:10 +08002207
Andy Green4739e5c2011-01-22 12:51:57 +00002208 bzero((char *) &serv_addr, sizeof(serv_addr));
2209 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002210 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002211 serv_addr.sin_addr.s_addr = INADDR_ANY;
2212 else
Andy Greend1eac602013-11-09 08:07:38 +08002213 if (interface_to_sa(info->iface, &serv_addr,
2214 sizeof(serv_addr)) < 0) {
2215 lwsl_err("Unable to find interface %s\n",
2216 info->iface);
2217 compatible_close(sockfd);
2218 goto bail;
2219 }
Andy Green1b265272013-02-09 14:01:09 +08002220 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002221
2222 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2223 sizeof(serv_addr));
2224 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002225 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002226 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002227 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002228 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002229 }
Andy Green0d338332011-02-12 11:57:43 +00002230
Andy Greenb5b23192013-02-11 17:13:32 +08002231 wsi = (struct libwebsocket *)malloc(
2232 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002233 if (wsi == NULL) {
2234 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002235 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002236 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002237 }
Andy Greenb5b23192013-02-11 17:13:32 +08002238 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002239 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002240#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002241 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002242#endif
Andy Green0d338332011-02-12 11:57:43 +00002243 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002244
2245 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002246
Andy Green65b0e912013-01-16 07:59:47 +08002247 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2248 context->listen_service_count = 0;
2249 context->listen_service_fd = sockfd;
2250
Andy Greena824d182013-01-15 20:52:29 +08002251 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002252 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002253 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002254#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002255
Andy Green6ee372f2012-04-09 15:09:01 +08002256 /*
2257 * drop any root privs for this process
2258 * to listen on port < 1023 we would have needed root, but now we are
2259 * listening, we don't want the power for anything else
2260 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002261#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002262#else
Andy Green1b265272013-02-09 14:01:09 +08002263 if (info->gid != -1)
2264 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002265 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002266 if (info->uid != -1)
2267 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002268 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002269#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002270
Andy Green6f520a52013-01-29 17:57:39 +08002271 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002272
Peter Hinz56885f32011-03-02 22:03:47 +00002273 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002274 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002275 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002276
Andy Green43db0452013-01-10 19:50:35 +08002277 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002278 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002279
Andy Green1b265272013-02-09 14:01:09 +08002280 info->protocols[context->count_protocols].owning_server =
2281 context;
2282 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002283 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002284
2285 /*
2286 * inform all the protocols that they are doing their one-time
2287 * initialization if they want to
2288 */
2289 info->protocols[context->count_protocols].callback(context,
2290 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002291 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002292
Andy Green3182ece2013-01-20 17:08:31 +08002293#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002294 /*
2295 * give all extensions a chance to create any per-context
2296 * allocations they need
2297 */
2298
2299 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002300 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002301 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002302
Andy Green1b265272013-02-09 14:01:09 +08002303 if (info->extensions) {
2304 ext = info->extensions;
2305 while (ext->callback) {
2306 lwsl_ext(" Extension: %s\n", ext->name);
2307 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002308 (enum libwebsocket_extension_callback_reasons)m,
2309 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002310 ext++;
2311 }
Andy Greena41314f2011-05-23 10:00:03 +01002312 }
Andy Green3182ece2013-01-20 17:08:31 +08002313#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002314 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002315
2316bail:
2317 libwebsocket_context_destroy(context);
2318 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002319}
Andy Greenb45993c2010-12-18 15:13:50 +00002320
Andy Greenb45993c2010-12-18 15:13:50 +00002321/**
shysb4e800e2013-10-24 22:12:03 +08002322 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2323 * @context: pointer to struct libwebsocket_context you want set proxy to
2324 * @proxy: pointer to c string containing proxy in format address:port
2325 *
2326 * Returns 0 if proxy string was parsed and proxy was setup.
2327 * Returns -1 if @proxy is NULL or has incorrect format.
2328 *
2329 * This is only required if your OS does not provide the http_proxy
2330 * enviroment variable (eg, OSX)
2331 *
2332 * IMPORTANT! You should call this function right after creation of the
2333 * libwebsocket_context and before call to connect. If you call this
2334 * function after connect behavior is undefined.
2335 * This function will override proxy settings made on libwebsocket_context
2336 * creation with genenv() call.
2337 */
2338
2339LWS_VISIBLE int
2340libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2341{
2342 char *p;
2343
2344 if (!proxy)
2345 return -1;
2346
2347 strncpy(context->http_proxy_address, proxy,
2348 sizeof(context->http_proxy_address) - 1);
2349 context->http_proxy_address[
2350 sizeof(context->http_proxy_address) - 1] = '\0';
2351
2352 p = strchr(context->http_proxy_address, ':');
2353 if (!p) {
2354 lwsl_err("http_proxy needs to be ads:port\n");
2355
2356 return -1;
2357 }
2358 *p = '\0';
2359 context->http_proxy_port = atoi(p + 1);
2360
2361 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2362 context->http_proxy_port);
2363
2364 return 0;
2365}
2366
2367/**
Andy Greenb45993c2010-12-18 15:13:50 +00002368 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002369 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002370 * @wsi: pointer to struct websocket you want to know the protocol of
2371 *
Andy Green8f037e42010-12-19 22:13:26 +00002372 *
Andy Green6f520a52013-01-29 17:57:39 +08002373 * Some apis can act on all live connections of a given protocol,
2374 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002375 */
Andy Greenab990e42010-10-31 12:42:52 +00002376
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002377LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002378libwebsockets_get_protocol(struct libwebsocket *wsi)
2379{
2380 return wsi->protocol;
2381}
2382
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002383LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002384libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2385{
Andy Green623a98d2013-01-21 11:04:23 +08002386 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002387}
Alex Bligh49146db2011-11-07 17:19:25 +08002388
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002389LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002390libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2391{
Andy Green623a98d2013-01-21 11:04:23 +08002392 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002393}
2394
Andy Green2af4d5b2013-02-18 16:30:10 +08002395int
Alex Bligh49146db2011-11-07 17:19:25 +08002396libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2397{
Andy Green67d556c2013-02-15 22:31:55 +08002398 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002399 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002400
Alex Bligh49146db2011-11-07 17:19:25 +08002401 /* allocate the per-connection user memory (if any) */
2402
2403 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2404 wsi->user_space = malloc(
2405 wsi->protocol->per_session_data_size);
2406 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002407 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002408 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002409 }
Andy Green6ee372f2012-04-09 15:09:01 +08002410 memset(wsi->user_space, 0,
2411 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002412 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002413 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002414}
Andy Green43db0452013-01-10 19:50:35 +08002415
Andy Green0b319092013-01-19 11:17:56 +08002416static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002417{
Andy Green0b319092013-01-19 11:17:56 +08002418 char buf[300];
2419 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002420 int n;
2421
2422 gettimeofday(&tv, NULL);
2423
2424 buf[0] = '\0';
2425 for (n = 0; n < LLL_COUNT; n++)
2426 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002427 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002428 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002429 break;
2430 }
Andy Greenb5b23192013-02-11 17:13:32 +08002431
Andy Green0b319092013-01-19 11:17:56 +08002432 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002433}
2434
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002435#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002436LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002437{
2438 lwsl_emit_stderr(level, line);
2439}
2440#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002441LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002442{
2443 int syslog_level = LOG_DEBUG;
2444
2445 switch (level) {
2446 case LLL_ERR:
2447 syslog_level = LOG_ERR;
2448 break;
2449 case LLL_WARN:
2450 syslog_level = LOG_WARNING;
2451 break;
2452 case LLL_NOTICE:
2453 syslog_level = LOG_NOTICE;
2454 break;
2455 case LLL_INFO:
2456 syslog_level = LOG_INFO;
2457 break;
2458 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002459 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002460}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002461#endif
Andy Greenc11db202013-01-19 11:12:16 +08002462
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002463LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002464{
Andy Greende8f27a2013-01-12 09:17:42 +08002465 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002466 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002467
2468 if (!(log_level & filter))
2469 return;
2470
Andy Green43db0452013-01-10 19:50:35 +08002471 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002472 vsnprintf(buf, sizeof(buf), format, ap);
2473 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002474 va_end(ap);
2475
Andy Green0b319092013-01-19 11:17:56 +08002476 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002477}
2478
2479/**
2480 * lws_set_log_level() - Set the logging bitfield
2481 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002482 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2483 * function to perform log string emission instead of
2484 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002485 *
Andy Greenc6511a02013-02-19 10:01:48 +08002486 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002487 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002488 */
2489
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002490LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002491 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002492{
2493 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002494 if (log_emit_function)
2495 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002496}