blob: 8b2fec52b3a8a11485b08f8049b93340c0d6a4a0 [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 Green2764eba2013-12-09 14:16:17 +0800379 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800380 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800381 free(wsi->truncated_send_malloc);
382 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800383 }
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
Andy Green2764eba2013-12-09 14:16:17 +0800645 /* treat the fact we got a truncated send pending as if we're choked */
646 if (wsi->truncated_send_malloc)
647 return 1;
648
Andy Green95a7b5d2011-03-06 10:29:39 +0000649 fds.fd = wsi->sock;
650 fds.events = POLLOUT;
651 fds.revents = 0;
652
653 if (poll(&fds, 1, 0) != 1)
654 return 1;
655
656 if ((fds.revents & POLLOUT) == 0)
657 return 1;
658
659 /* okay to send another packet without blocking */
660
661 return 0;
662}
663
Andy Greena41314f2011-05-23 10:00:03 +0100664int
Andy Green3b84c002011-03-06 13:14:42 +0000665lws_handle_POLLOUT_event(struct libwebsocket_context *context,
666 struct libwebsocket *wsi, struct pollfd *pollfd)
667{
Andy Green3b84c002011-03-06 13:14:42 +0000668 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800669
Andy Green3182ece2013-01-20 17:08:31 +0800670#ifndef LWS_NO_EXTENSIONS
671 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000672 int ret;
673 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100674 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000675
Andy Green1f4267b2013-10-17 08:09:19 +0800676 /* pending truncated sends have uber priority */
677
Andy Green2764eba2013-12-09 14:16:17 +0800678 if (wsi->truncated_send_malloc) {
679 lws_issue_raw(wsi, wsi->truncated_send_malloc +
680 wsi->truncated_send_offset,
681 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800682 /* leave POLLOUT active either way */
683 return 0;
684 }
685
Andy Greena41314f2011-05-23 10:00:03 +0100686 for (n = 0; n < wsi->count_active_extensions; n++) {
687 if (!wsi->active_extensions[n]->callback)
688 continue;
689
690 m = wsi->active_extensions[n]->callback(context,
691 wsi->active_extensions[n], wsi,
692 LWS_EXT_CALLBACK_IS_WRITEABLE,
693 wsi->active_extensions_user[n], NULL, 0);
694 if (m > handled)
695 handled = m;
696 }
697
698 if (handled == 1)
699 goto notify_action;
700
701 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000702 goto user_service;
703
704 /*
705 * check in on the active extensions, see if they
706 * had pending stuff to spill... they need to get the
707 * first look-in otherwise sequence will be disordered
708 *
709 * NULL, zero-length eff_buf means just spill pending
710 */
711
712 ret = 1;
713 while (ret == 1) {
714
715 /* default to nobody has more to spill */
716
717 ret = 0;
718 eff_buf.token = NULL;
719 eff_buf.token_len = 0;
720
721 /* give every extension a chance to spill */
722
723 for (n = 0; n < wsi->count_active_extensions; n++) {
724 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000725 wsi->protocol->owning_server,
726 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000727 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
728 wsi->active_extensions_user[n], &eff_buf, 0);
729 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800730 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000731 return -1;
732 }
733 if (m)
734 /*
735 * at least one extension told us he has more
736 * to spill, so we will go around again after
737 */
738 ret = 1;
739 }
740
741 /* assuming they gave us something to send, send it */
742
743 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800744 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
745 eff_buf.token_len);
746 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000747 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800748 /*
749 * Keep amount spilled small to minimize chance of this
750 */
751 if (n != eff_buf.token_len) {
752 lwsl_err("Unable to spill ext %d vs %s\n",
753 eff_buf.token_len, n);
754 return -1;
755 }
Andy Green3b84c002011-03-06 13:14:42 +0000756 } else
757 continue;
758
759 /* no extension has more to spill */
760
761 if (!ret)
762 continue;
763
764 /*
765 * There's more to spill from an extension, but we just sent
766 * something... did that leave the pipe choked?
767 */
768
769 if (!lws_send_pipe_choked(wsi))
770 /* no we could add more */
771 continue;
772
Andy Green43db0452013-01-10 19:50:35 +0800773 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000774
775 /*
776 * Yes, he's choked. Leave the POLLOUT masked on so we will
777 * come back here when he is unchoked. Don't call the user
778 * callback to enforce ordering of spilling, he'll get called
779 * when we come back here and there's nothing more to spill.
780 */
781
782 return 0;
783 }
784
785 wsi->extension_data_pending = 0;
786
787user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800788#endif
Andy Green3b84c002011-03-06 13:14:42 +0000789 /* one shot */
790
Andy Greena41314f2011-05-23 10:00:03 +0100791 if (pollfd) {
792 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000793
Andy Greena41314f2011-05-23 10:00:03 +0100794 /* external POLL support via protocol 0 */
795 context->protocols[0].callback(context, wsi,
796 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800797 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100798 }
Andy Green3182ece2013-01-20 17:08:31 +0800799#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100800notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800801#endif
Andy Green3b84c002011-03-06 13:14:42 +0000802
Andy Green9e4c2b62011-03-07 20:47:39 +0000803 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
804 n = LWS_CALLBACK_CLIENT_WRITEABLE;
805 else
806 n = LWS_CALLBACK_SERVER_WRITEABLE;
807
Andy Greenb8b247d2013-01-22 07:20:08 +0800808 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800809 wsi, (enum libwebsocket_callback_reasons) n,
810 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000811}
812
813
814
Andy Green1c6e1422013-02-20 19:11:31 +0800815int
Andy Greena41314f2011-05-23 10:00:03 +0100816libwebsocket_service_timeout_check(struct libwebsocket_context *context,
817 struct libwebsocket *wsi, unsigned int sec)
818{
Andy Green3182ece2013-01-20 17:08:31 +0800819#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100820 int n;
821
822 /*
823 * if extensions want in on it (eg, we are a mux parent)
824 * give them a chance to service child timeouts
825 */
826
827 for (n = 0; n < wsi->count_active_extensions; n++)
828 wsi->active_extensions[n]->callback(
829 context, wsi->active_extensions[n],
830 wsi, LWS_EXT_CALLBACK_1HZ,
831 wsi->active_extensions_user[n], NULL, sec);
832
Andy Green3182ece2013-01-20 17:08:31 +0800833#endif
Andy Greena41314f2011-05-23 10:00:03 +0100834 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800835 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800836
Andy Greena41314f2011-05-23 10:00:03 +0100837 /*
838 * if we went beyond the allowed time, kill the
839 * connection
840 */
841
842 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800843 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100844 libwebsocket_close_and_free_session(context,
845 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800846 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100847 }
Andy Green1c6e1422013-02-20 19:11:31 +0800848
849 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100850}
851
Andy Green9f990342011-02-12 11:57:45 +0000852/**
853 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000854 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000855 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800856 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000857 *
Andy Green75006172013-01-22 12:32:11 +0800858 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800859 * services it according to the state of the associated
860 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800861 *
862 * The one call deals with all "service" that might happen on a socket
863 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800864 *
865 * If a pollfd says it has something, you can just pass it to
866 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
867 * If it sees it is a lws socket, the traffic will be handled and
868 * pollfd->revents will be zeroed now.
869 *
870 * If the socket is foreign to lws, it leaves revents alone. So you can
871 * see if you should service yourself by checking the pollfd revents
872 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000873 */
874
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800875LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000876libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000877 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000878{
Andy Greena1ce6be2013-01-18 11:43:21 +0800879 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000880 int n;
Andy Green0d338332011-02-12 11:57:43 +0000881 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800882 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000883 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800884 int timed_out = 0;
885 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800886 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800887
Andy Green3182ece2013-01-20 17:08:31 +0800888#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000889 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800890#endif
Andy Green98a717c2011-03-06 13:14:15 +0000891 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800892
893 if (context->listen_service_fd)
894 listen_socket_fds_index = context->lws_lookup[
895 context->listen_service_fd]->position_in_fds_table;
896
Andy Greena71eafc2011-02-14 17:59:43 +0000897 /*
898 * you can call us with pollfd = NULL to just allow the once-per-second
899 * global timeout checks; if less than a second since the last check
900 * it returns immediately then.
901 */
902
903 gettimeofday(&tv, NULL);
904
Peter Hinz56885f32011-03-02 22:03:47 +0000905 if (context->last_timeout_check_s != tv.tv_sec) {
906 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000907
Joakim Soderberg4c531232013-02-06 15:26:58 +0900908 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800909 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800910 if (context->started_with_parent &&
911 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800912 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900913 #endif
Andy Green24cba922013-01-19 13:56:10 +0800914
Andy Greena71eafc2011-02-14 17:59:43 +0000915 /* global timeout check once per second */
916
Andy Green1c6e1422013-02-20 19:11:31 +0800917 if (pollfd)
918 our_fd = pollfd->fd;
919
Peter Hinz56885f32011-03-02 22:03:47 +0000920 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800921 m = context->fds[n].fd;
922 wsi = context->lws_lookup[m];
923 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800924 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800925
926 if (libwebsocket_service_timeout_check(context, wsi,
927 tv.tv_sec))
928 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800929 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800930 /* it was the guy we came to service! */
931 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800932 /* mark as handled */
933 pollfd->revents = 0;
934 }
Andy Greena71eafc2011-02-14 17:59:43 +0000935 }
936 }
937
Andy Green1c6e1422013-02-20 19:11:31 +0800938 /* the socket we came to service timed out, nothing to do */
939 if (timed_out)
940 return 0;
941
Andy Greena71eafc2011-02-14 17:59:43 +0000942 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000943 if (pollfd == NULL)
944 return 0;
945
946 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800947 wsi = context->lws_lookup[pollfd->fd];
948 if (wsi == NULL)
949 /* not lws connection ... leave revents alone and return */
950 return 0;
951
952 /*
953 * so that caller can tell we handled, past here we need to
954 * zero down pollfd->revents after handling
955 */
956
Andy Green65b0e912013-01-16 07:59:47 +0800957 /*
958 * deal with listen service piggybacking
959 * every listen_service_modulo services of other fds, we
960 * sneak one in to service the listen socket if there's anything waiting
961 *
962 * To handle connection storms, as found in ab, if we previously saw a
963 * pending connection here, it causes us to check again next time.
964 */
965
Andy Greenb5b23192013-02-11 17:13:32 +0800966 if (context->listen_service_fd && pollfd !=
967 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800968 context->listen_service_count++;
969 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800970 context->listen_service_count ==
971 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800972 context->listen_service_count = 0;
973 m = 1;
974 if (context->listen_service_extraseen > 5)
975 m = 2;
976 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800977 /*
978 * even with extpoll, we prepared this
979 * internal fds for listen
980 */
981 n = poll(&context->fds[listen_socket_fds_index],
982 1, 0);
983 if (n > 0) { /* there's a conn waiting for us */
984 libwebsocket_service_fd(context,
985 &context->
986 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800987 context->listen_service_extraseen++;
988 } else {
989 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800990 context->
991 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800992 break;
993 }
994 }
995 }
996
997 }
998
Graham Newtonb1aa1d32013-11-04 09:47:06 +0800999 /* handle session socket closed */
1000
1001 if ((!(pollfd->revents & POLLIN)) &&
1002 (pollfd->revents & (POLLERR | POLLHUP))) {
1003
1004 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1005 (void *)wsi, pollfd->fd);
1006
1007 goto close_and_handled;
1008 }
1009
Andy Green65b0e912013-01-16 07:59:47 +08001010 /* okay, what we came here to do... */
1011
Andy Green0d338332011-02-12 11:57:43 +00001012 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001013
Andy Greena1ce6be2013-01-18 11:43:21 +08001014#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001015 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001016 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001017 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001018 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001019 n = lws_server_socket_service(context, wsi, pollfd);
1020 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001021#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001022
Andy Green0d338332011-02-12 11:57:43 +00001023 case LWS_CONNMODE_WS_SERVING:
1024 case LWS_CONNMODE_WS_CLIENT:
1025
Andy Green0d338332011-02-12 11:57:43 +00001026 /* the guy requested a callback when it was OK to write */
1027
Andy Greenda527df2011-03-07 07:08:12 +00001028 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001029 wsi->state == WSI_STATE_ESTABLISHED &&
1030 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +08001031 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001032 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001033 }
Andy Green0d338332011-02-12 11:57:43 +00001034
Andy Greenca0a1292013-03-16 11:24:23 +08001035 if (wsi->u.ws.rxflow_buffer &&
1036 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1037 lwsl_info("draining rxflow\n");
1038 /* well, drain it */
1039 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1040 wsi->u.ws.rxflow_pos;
1041 eff_buf.token_len = wsi->u.ws.rxflow_len -
1042 wsi->u.ws.rxflow_pos;
1043 draining_flow = 1;
1044 goto drain;
1045 }
1046
Andy Green0d338332011-02-12 11:57:43 +00001047 /* any incoming data ready? */
1048
1049 if (!(pollfd->revents & POLLIN))
1050 break;
1051
Andy Greenb45993c2010-12-18 15:13:50 +00001052#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001053read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001054 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001055 eff_buf.token_len = SSL_read(wsi->ssl,
1056 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001057 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001058 if (!eff_buf.token_len) {
1059 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001060 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001061 ERR_error_string(n,
1062 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001063 }
1064 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001065#endif
Andy Greene84652c2013-02-08 13:01:02 +08001066 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001067 context->service_buffer,
1068 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001069
Andy Green98a717c2011-03-06 13:14:15 +00001070 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001071 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1072 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001073 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001074 goto close_and_handled;
1075 n = 0;
1076 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001077 }
Andy Green98a717c2011-03-06 13:14:15 +00001078 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001079 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001080 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001081 }
1082
Andy Green98a717c2011-03-06 13:14:15 +00001083 /*
1084 * give any active extensions a chance to munge the buffer
1085 * before parse. We pass in a pointer to an lws_tokens struct
1086 * prepared with the default buffer and content length that's in
1087 * there. Rather than rewrite the default buffer, extensions
1088 * that expect to grow the buffer can adapt .token to
1089 * point to their own per-connection buffer in the extension
1090 * user allocation. By default with no extensions or no
1091 * extension callback handling, just the normal input buffer is
1092 * used then so it is efficient.
1093 */
Andy Greenb45993c2010-12-18 15:13:50 +00001094
Andy Greene84652c2013-02-08 13:01:02 +08001095 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001096drain:
Andy Green3182ece2013-01-20 17:08:31 +08001097#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001098 more = 1;
1099 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001100
Andy Green98a717c2011-03-06 13:14:15 +00001101 more = 0;
1102
1103 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001104 m = wsi->active_extensions[n]->callback(context,
1105 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001106 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001107 wsi->active_extensions_user[n],
1108 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001109 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001110 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001111 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001112 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001113 }
1114 if (m)
1115 more = 1;
1116 }
Andy Green3182ece2013-01-20 17:08:31 +08001117#endif
Andy Green98a717c2011-03-06 13:14:15 +00001118 /* service incoming data */
1119
1120 if (eff_buf.token_len) {
1121 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001122 (unsigned char *)eff_buf.token,
1123 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001124 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001125 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001126 n = 0;
1127 goto handled;
1128 }
Andy Green98a717c2011-03-06 13:14:15 +00001129 }
Andy Green3182ece2013-01-20 17:08:31 +08001130#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001131 eff_buf.token = NULL;
1132 eff_buf.token_len = 0;
1133 }
Andy Green3182ece2013-01-20 17:08:31 +08001134#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001135 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1136 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1137 lwsl_info("flow buffer: drained\n");
1138 free(wsi->u.ws.rxflow_buffer);
1139 wsi->u.ws.rxflow_buffer = NULL;
1140 /* having drained the rxflow buffer, can rearm POLLIN */
1141 _libwebsocket_rx_flow_control(wsi);
1142 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001143
1144#ifdef LWS_OPENSSL_SUPPORT
1145 if (wsi->ssl && SSL_pending(wsi->ssl))
1146 goto read_pending;
1147#endif
Andy Green98a717c2011-03-06 13:14:15 +00001148 break;
Andy Green76f61e72013-01-16 11:53:05 +08001149
1150 default:
Andy Green03674a62013-01-16 11:47:40 +08001151#ifdef LWS_NO_CLIENT
1152 break;
1153#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001154 n = lws_client_socket_service(context, wsi, pollfd);
1155 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001156#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001157 }
1158
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001159 n = 0;
1160 goto handled;
1161
1162close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001163 libwebsocket_close_and_free_session(context, wsi,
1164 LWS_CLOSE_STATUS_NOSTATUS);
1165 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001166
1167handled:
1168 pollfd->revents = 0;
1169 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001170}
1171
Andy Green0d338332011-02-12 11:57:43 +00001172
Andy Green6964bb52011-01-23 16:50:33 +00001173/**
1174 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001175 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001176 *
1177 * This function closes any active connections and then frees the
1178 * context. After calling this, any further use of the context is
1179 * undefined.
1180 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001181LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001182libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001183{
Andy Green3182ece2013-01-20 17:08:31 +08001184#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001185 int n;
1186 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001187 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001188 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001189
Andy Greend636e352013-01-29 12:36:17 +08001190#ifdef LWS_LATENCY
1191 if (context->worst_latency_info[0])
1192 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1193#endif
1194
Andy Greendfb23042013-01-17 12:26:48 +08001195 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001196 struct libwebsocket *wsi =
1197 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001198 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001199 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1200 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001201 }
Andy Green6964bb52011-01-23 16:50:33 +00001202
Andy Greena41314f2011-05-23 10:00:03 +01001203 /*
1204 * give all extensions a chance to clean up any per-context
1205 * allocations they might have made
1206 */
1207
1208 ext = context->extensions;
1209 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1210 if (context->listen_port)
1211 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001212 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001213 ext->callback(context, ext, NULL,
1214 (enum libwebsocket_extension_callback_reasons)m,
1215 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001216 ext++;
1217 }
Andy Greena7109e62013-02-11 12:05:54 +08001218
1219 /*
1220 * inform all the protocols that they are done and will have no more
1221 * callbacks
1222 */
1223
1224 while (protocol->callback) {
1225 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1226 NULL, NULL, 0);
1227 protocol++;
1228 }
1229
Andy Green3182ece2013-01-20 17:08:31 +08001230#endif
Andy Greena41314f2011-05-23 10:00:03 +01001231
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001232#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001233#else
1234 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001235#endif
1236
Peter Hinz56885f32011-03-02 22:03:47 +00001237#ifdef LWS_OPENSSL_SUPPORT
1238 if (context->ssl_ctx)
1239 SSL_CTX_free(context->ssl_ctx);
1240 if (context->ssl_client_ctx)
1241 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001242
1243 ERR_remove_state(0);
1244 ERR_free_strings();
1245 EVP_cleanup();
1246 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001247#endif
1248
Andy Green46596482013-02-11 11:04:01 +08001249 if (context->fds)
1250 free(context->fds);
1251 if (context->lws_lookup)
1252 free(context->lws_lookup);
1253
Peter Hinz56885f32011-03-02 22:03:47 +00001254 free(context);
1255
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001256#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001257 WSACleanup();
1258#endif
Andy Green6964bb52011-01-23 16:50:33 +00001259}
1260
Andy Greend88146d2013-01-22 12:40:35 +08001261/**
Andy Greenb5b23192013-02-11 17:13:32 +08001262 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001263 * @context: Websocket context
1264 *
1265 * This returns the optional user allocation that can be attached to
1266 * the context the sockets live in at context_create time. It's a way
1267 * to let all sockets serviced in the same context share data without
1268 * using globals statics in the user code.
1269 */
Alon Levy0291eb32012-10-19 11:21:56 +02001270LWS_EXTERN void *
1271libwebsocket_context_user(struct libwebsocket_context *context)
1272{
Andy Greenb5b23192013-02-11 17:13:32 +08001273 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001274}
1275
Andy Green6964bb52011-01-23 16:50:33 +00001276/**
1277 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001278 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001279 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1280 * service otherwise block and service immediately, returning
1281 * after the timeout if nothing needed service.
1282 *
1283 * This function deals with any pending websocket traffic, for three
1284 * kinds of event. It handles these events on both server and client
1285 * types of connection the same.
1286 *
1287 * 1) Accept new connections to our context's server
1288 *
Andy Green6f520a52013-01-29 17:57:39 +08001289 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001290 * server or client connections.
1291 *
1292 * You need to call this service function periodically to all the above
1293 * functions to happen; if your application is single-threaded you can
1294 * just call it in your main event loop.
1295 *
1296 * Alternatively you can fork a new process that asynchronously handles
1297 * calling this service in a loop. In that case you are happy if this
1298 * call blocks your thread until it needs to take care of something and
1299 * would call it with a large nonzero timeout. Your loop then takes no
1300 * CPU while there is nothing happening.
1301 *
1302 * If you are calling it in a single-threaded app, you don't want it to
1303 * wait around blocking other things in your loop from happening, so you
1304 * would call it with a timeout_ms of 0, so it returns immediately if
1305 * nothing is pending, or as soon as it services whatever was pending.
1306 */
1307
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001308LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001309libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001310{
1311 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001312 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001313
1314 /* stay dead once we are dead */
1315
Peter Hinz56885f32011-03-02 22:03:47 +00001316 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001317 return 1;
1318
Andy Green0d338332011-02-12 11:57:43 +00001319 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001320
Peter Hinz56885f32011-03-02 22:03:47 +00001321 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green5dc62ea2013-09-20 20:26:12 +08001322 if (n == 0) /* poll timeout */ {
1323 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001324 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001325 }
Andy Greene92cd172011-01-19 13:11:55 +00001326
Andy Greendfb23042013-01-17 12:26:48 +08001327 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001328 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001329
Andy Greendfb23042013-01-17 12:26:48 +08001330 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001331
Andy Greenca0a1292013-03-16 11:24:23 +08001332 for (n = 0; n < context->fds_count; n++) {
1333 if (!context->fds[n].revents)
1334 continue;
1335 m = libwebsocket_service_fd(context, &context->fds[n]);
1336 if (m < 0)
1337 return -1;
1338 /* if something closed, retry this slot */
1339 if (m)
1340 n--;
1341 }
1342
Andy Greene92cd172011-01-19 13:11:55 +00001343 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001344}
1345
Andy Green3182ece2013-01-20 17:08:31 +08001346#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001347int
1348lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001349 struct libwebsocket *wsi,
1350 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001351 void *v, size_t len)
1352{
1353 int n;
1354 int handled = 0;
1355
1356 /* maybe an extension will take care of it for us */
1357
1358 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1359 if (!wsi->active_extensions[n]->callback)
1360 continue;
1361
1362 handled |= wsi->active_extensions[n]->callback(context,
1363 wsi->active_extensions[n], wsi,
1364 r, wsi->active_extensions_user[n], v, len);
1365 }
1366
1367 return handled;
1368}
1369
1370
1371void *
1372lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001373 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001374{
1375 int n = 0;
1376
Andy Green68b45042011-05-25 21:41:57 +01001377 if (wsi == NULL)
1378 return NULL;
1379
Andy Greena41314f2011-05-23 10:00:03 +01001380 while (n < wsi->count_active_extensions) {
1381 if (wsi->active_extensions[n] != ext) {
1382 n++;
1383 continue;
1384 }
1385 return wsi->active_extensions_user[n];
1386 }
1387
1388 return NULL;
1389}
Andy Green3182ece2013-01-20 17:08:31 +08001390#endif
Andy Greena41314f2011-05-23 10:00:03 +01001391
Andy Green90c7cbc2011-01-27 06:26:52 +00001392/**
1393 * libwebsocket_callback_on_writable() - Request a callback when this socket
1394 * becomes able to be written to without
1395 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001396 *
Peter Hinz56885f32011-03-02 22:03:47 +00001397 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001398 * @wsi: Websocket connection instance to get callback for
1399 */
1400
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001401LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001402libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001403 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001404{
Andy Green3182ece2013-01-20 17:08:31 +08001405#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001406 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001407 int handled = 0;
1408
1409 /* maybe an extension will take care of it for us */
1410
1411 for (n = 0; n < wsi->count_active_extensions; n++) {
1412 if (!wsi->active_extensions[n]->callback)
1413 continue;
1414
1415 handled |= wsi->active_extensions[n]->callback(context,
1416 wsi->active_extensions[n], wsi,
1417 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1418 wsi->active_extensions_user[n], NULL, 0);
1419 }
1420
1421 if (handled)
1422 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001423#endif
Andy Greendfb23042013-01-17 12:26:48 +08001424 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001425 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1426 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001427 return -1;
1428 }
1429
1430 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001431
Andy Green3221f922011-02-12 13:14:11 +00001432 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001433 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001434 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001435 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001436
Andy Green90c7cbc2011-01-27 06:26:52 +00001437 return 1;
1438}
1439
1440/**
1441 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1442 * all connections using the given protocol when it
1443 * becomes possible to write to each socket without
1444 * blocking in turn.
1445 *
1446 * @protocol: Protocol whose connections will get callbacks
1447 */
1448
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001449LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001450libwebsocket_callback_on_writable_all_protocol(
1451 const struct libwebsocket_protocols *protocol)
1452{
Peter Hinz56885f32011-03-02 22:03:47 +00001453 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001454 int n;
Andy Green0d338332011-02-12 11:57:43 +00001455 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001456
Andy Greendfb23042013-01-17 12:26:48 +08001457 for (n = 0; n < context->fds_count; n++) {
1458 wsi = context->lws_lookup[context->fds[n].fd];
1459 if (!wsi)
1460 continue;
1461 if (wsi->protocol == protocol)
1462 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001463 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001464
1465 return 0;
1466}
1467
Andy Greenbe93fef2011-02-14 20:25:43 +00001468/**
1469 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1470 *
1471 * You will not need this unless you are doing something special
1472 *
1473 * @wsi: Websocket connection instance
1474 * @reason: timeout reason
1475 * @secs: how many seconds
1476 */
1477
1478void
1479libwebsocket_set_timeout(struct libwebsocket *wsi,
1480 enum pending_timeout reason, int secs)
1481{
1482 struct timeval tv;
1483
1484 gettimeofday(&tv, NULL);
1485
1486 wsi->pending_timeout_limit = tv.tv_sec + secs;
1487 wsi->pending_timeout = reason;
1488}
1489
Andy Greena6cbece2011-01-27 20:06:03 +00001490
1491/**
1492 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1493 *
1494 * You will not need this unless you are doing something special
1495 *
1496 * @wsi: Websocket connection instance
1497 */
1498
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001499LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001500libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1501{
1502 return wsi->sock;
1503}
1504
Andy Greend636e352013-01-29 12:36:17 +08001505#ifdef LWS_LATENCY
1506void
Andy Greenb5b23192013-02-11 17:13:32 +08001507lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1508 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001509{
1510 struct timeval tv;
1511 unsigned long u;
1512 char buf[256];
1513
1514 gettimeofday(&tv, NULL);
1515
1516 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1517
1518 if (action) {
1519 if (completed) {
1520 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001521 sprintf(buf,
1522 "Completion first try lat %luus: %p: ret %d: %s\n",
1523 u - wsi->latency_start,
1524 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001525 else
Andy Greenb5b23192013-02-11 17:13:32 +08001526 sprintf(buf,
1527 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1528 u - wsi->action_start,
1529 u - wsi->latency_start,
1530 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001531 wsi->action_start = 0;
1532 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001533 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1534 u - wsi->latency_start,
1535 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001536 if (u - wsi->latency_start > context->worst_latency) {
1537 context->worst_latency = u - wsi->latency_start;
1538 strcpy(context->worst_latency_info, buf);
1539 }
1540 lwsl_latency("%s", buf);
1541 } else {
1542 wsi->latency_start = u;
1543 if (!wsi->action_start)
1544 wsi->action_start = u;
1545 }
1546}
1547#endif
1548
Andy Greena1ce6be2013-01-18 11:43:21 +08001549#ifdef LWS_NO_SERVER
1550int
Andy Green8d15cf42013-10-24 21:47:06 +08001551_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001552{
1553 return 0;
1554}
1555#else
Andy Green706961d2013-01-17 16:50:35 +08001556int
1557_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1558{
1559 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001560
Andy Greenca0a1292013-03-16 11:24:23 +08001561 /* there is no pending change */
1562 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001563 return 0;
1564
Andy Greenca0a1292013-03-16 11:24:23 +08001565 /* stuff is still buffered, not ready to really accept new input */
1566 if (wsi->u.ws.rxflow_buffer) {
1567 /* get ourselves called back to deal with stashed buffer */
1568 libwebsocket_callback_on_writable(context, wsi);
1569 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001570 }
1571
Andy Greenca0a1292013-03-16 11:24:23 +08001572 /* pending is cleared, we can change rxflow state */
1573
1574 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1575
1576 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1577 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1578
1579 /* adjust the pollfd for this wsi */
1580
1581 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001582 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1583 else
1584 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1585
Andy Greenca0a1292013-03-16 11:24:23 +08001586 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001587 /* external POLL support via protocol 0 */
1588 context->protocols[0].callback(context, wsi,
1589 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001590 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001591 else
1592 /* external POLL support via protocol 0 */
1593 context->protocols[0].callback(context, wsi,
1594 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001595 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001596
1597 return 1;
1598}
Andy Greena1ce6be2013-01-18 11:43:21 +08001599#endif
Andy Green706961d2013-01-17 16:50:35 +08001600
Andy Green90c7cbc2011-01-27 06:26:52 +00001601/**
1602 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1603 * receieved packets.
1604 *
1605 * If the output side of a server process becomes choked, this allows flow
1606 * control for the input side.
1607 *
1608 * @wsi: Websocket connection instance to get callback for
1609 * @enable: 0 = disable read servicing for this connection, 1 = enable
1610 */
1611
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001612LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001613libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1614{
Andy Greenca0a1292013-03-16 11:24:23 +08001615 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1616 return 0;
1617
1618 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1619 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001620
Andy Green706961d2013-01-17 16:50:35 +08001621 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001622}
1623
Andy Greenb55451c2013-03-16 12:32:27 +08001624/**
1625 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1626 *
1627 * When the user server code realizes it can accept more input, it can
1628 * call this to have the RX flow restriction removed from all connections using
1629 * the given protocol.
1630 *
1631 * @protocol: all connections using this protocol will be allowed to receive
1632 */
1633
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001634LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001635libwebsocket_rx_flow_allow_all_protocol(
1636 const struct libwebsocket_protocols *protocol)
1637{
1638 struct libwebsocket_context *context = protocol->owning_server;
1639 int n;
1640 struct libwebsocket *wsi;
1641
1642 for (n = 0; n < context->fds_count; n++) {
1643 wsi = context->lws_lookup[context->fds[n].fd];
1644 if (!wsi)
1645 continue;
1646 if (wsi->protocol == protocol)
1647 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1648 }
1649}
1650
Andy Green706961d2013-01-17 16:50:35 +08001651
Andy Green2ac5a6f2011-01-28 10:00:18 +00001652/**
1653 * libwebsocket_canonical_hostname() - returns this host's hostname
1654 *
1655 * This is typically used by client code to fill in the host parameter
1656 * when making a client connection. You can only call it after the context
1657 * has been created.
1658 *
Peter Hinz56885f32011-03-02 22:03:47 +00001659 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001660 */
1661
1662
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001663LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001664libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001665{
Peter Hinz56885f32011-03-02 22:03:47 +00001666 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001667}
1668
1669
Andy Green90c7cbc2011-01-27 06:26:52 +00001670static void sigpipe_handler(int x)
1671{
1672}
1673
Andy Green6901cb32011-02-21 08:06:47 +00001674#ifdef LWS_OPENSSL_SUPPORT
1675static int
1676OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1677{
1678
1679 SSL *ssl;
1680 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001681 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001682
1683 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1684 SSL_get_ex_data_X509_STORE_CTX_idx());
1685
1686 /*
Andy Green2e24da02011-03-05 16:12:04 +00001687 * !!! nasty openssl requires the index to come as a library-scope
1688 * static
Andy Green6901cb32011-02-21 08:06:47 +00001689 */
Andy Green2e24da02011-03-05 16:12:04 +00001690 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001691
Peter Hinz56885f32011-03-02 22:03:47 +00001692 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001693 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1694 x509_ctx, ssl, preverify_ok);
1695
1696 /* convert return code from 0 = OK to 1 = OK */
1697
1698 if (!n)
1699 n = 1;
1700 else
1701 n = 0;
1702
1703 return n;
1704}
1705#endif
1706
Andy Green706961d2013-01-17 16:50:35 +08001707int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001708 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001709 struct libwebsocket *wsi,
1710 enum libwebsocket_callback_reasons reason, void *user,
1711 void *in, size_t len)
1712{
1713 int n;
1714
1715 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001716 if (!n)
1717 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001718
Andy Greenaedc9532013-02-10 21:21:24 +08001719 return n;
Andy Green706961d2013-01-17 16:50:35 +08001720}
1721
Andy Greenb45993c2010-12-18 15:13:50 +00001722
Andy Greenab990e42010-10-31 12:42:52 +00001723/**
Andy Green4739e5c2011-01-22 12:51:57 +00001724 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001725 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001726 *
Andy Green1b265272013-02-09 14:01:09 +08001727 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001728 * of all initialization in one step.
1729 *
Andy Greene92cd172011-01-19 13:11:55 +00001730 * After initialization, it returns a struct libwebsocket_context * that
1731 * represents this server. After calling, user code needs to take care
1732 * of calling libwebsocket_service() with the context pointer to get the
1733 * server's sockets serviced. This can be done in the same process context
1734 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001735 *
Andy Green8f037e42010-12-19 22:13:26 +00001736 * The protocol callback functions are called for a handful of events
1737 * including http requests coming in, websocket connections becoming
1738 * established, and data arriving; it's also called periodically to allow
1739 * async transmission.
1740 *
1741 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1742 * at that time websocket protocol has not been negotiated. Other
1743 * protocols after the first one never see any HTTP callack activity.
1744 *
1745 * The server created is a simple http server by default; part of the
1746 * websocket standard is upgrading this http connection to a websocket one.
1747 *
1748 * This allows the same server to provide files like scripts and favicon /
1749 * images or whatever over http and dynamic data over websockets all in
1750 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001751 */
Andy Green4ea60062010-10-30 12:15:07 +01001752
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001753LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001754libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001755{
Peter Hinz56885f32011-03-02 22:03:47 +00001756 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001757 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001758 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001759#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001760 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001761 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001762 struct sockaddr_in serv_addr;
1763#endif
Andy Green3182ece2013-01-20 17:08:31 +08001764#ifndef LWS_NO_EXTENSIONS
1765 int m;
Andy Green1b265272013-02-09 14:01:09 +08001766 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001767#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001768
Andy Green3faa9c72010-11-08 17:03:03 +00001769#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001770 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001771#endif
1772
Joakim Soderberg4c531232013-02-06 15:26:58 +09001773#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001774 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001775#endif
1776
Andy Greenb3a614a2013-01-19 13:08:17 +08001777 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001778 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001779 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001780 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001781#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001782 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1783 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001784#else
1785 lwsl_notice(" Configured without extension support\n");
1786#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001787 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1788 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001789 if (info->ssl_cipher_list)
1790 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001791 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1792 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001793
Peter Hinz56885f32011-03-02 22:03:47 +00001794#ifdef _WIN32
1795 {
1796 WORD wVersionRequested;
1797 WSADATA wsaData;
1798 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001799 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001800
1801 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1802 wVersionRequested = MAKEWORD(2, 2);
1803
1804 err = WSAStartup(wVersionRequested, &wsaData);
1805 if (err != 0) {
1806 /* Tell the user that we could not find a usable */
1807 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001808 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001809 return NULL;
1810 }
David Galeano7b11fec2011-10-04 19:55:18 +08001811
Andy Green6ee372f2012-04-09 15:09:01 +08001812 /* default to a poll() made out of select() */
1813 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001814
Andy Green6ee372f2012-04-09 15:09:01 +08001815 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001816 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001817 if (wsdll)
1818 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001819
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001820 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001821 if (!poll)
1822 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001823 }
1824#endif
1825
Andy Greenb5b23192013-02-11 17:13:32 +08001826 context = (struct libwebsocket_context *)
1827 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001828 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001829 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001830 return NULL;
1831 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001832 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001833#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001834 context->started_with_parent = pid_daemon;
1835 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1836#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001837
Joakim Soderberg4c531232013-02-06 15:26:58 +09001838 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001839 context->protocols = info->protocols;
1840 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001841 context->http_proxy_port = 0;
1842 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001843 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001844 /* to reduce this allocation, */
1845 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001846 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1847 sizeof(struct libwebsocket_context),
1848 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1849 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001850 sizeof(struct libwebsocket_context) +
1851 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1852 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001853
Andy Greenb5b23192013-02-11 17:13:32 +08001854 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1855 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001856 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001857 lwsl_err("Unable to allocate fds array for %d connections\n",
1858 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001859 free(context);
1860 return NULL;
1861 }
Andy Greenb5b23192013-02-11 17:13:32 +08001862 context->lws_lookup = (struct libwebsocket **)
1863 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001864 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001865 lwsl_err(
1866 "Unable to allocate lws_lookup array for %d connections\n",
1867 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001868 free(context->fds);
1869 free(context);
1870 return NULL;
1871 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001872 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1873 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001874
Peter Hinz56885f32011-03-02 22:03:47 +00001875 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001876#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001877 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001878#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001879 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001880 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001881
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001882#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001883 context->fd_random = 0;
1884#else
1885 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1886 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001887 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001888 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001889 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001890 }
Peter Hinz56885f32011-03-02 22:03:47 +00001891#endif
Andy Green44eee682011-02-10 09:32:24 +00001892
Peter Hinz56885f32011-03-02 22:03:47 +00001893#ifdef LWS_OPENSSL_SUPPORT
1894 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08001895 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001896 context->ssl_ctx = NULL;
1897 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001898 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001899#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001900
Andy Greena1ce6be2013-01-18 11:43:21 +08001901 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001902
Andy Greena1ce6be2013-01-18 11:43:21 +08001903#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001904 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001905 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001906 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001907 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001908
Andy Greenb5b23192013-02-11 17:13:32 +08001909 lwsl_notice(" canonical_hostname = %s\n",
1910 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001911 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001912#endif
Andy Greena69f0512012-05-03 12:32:38 +08001913
Andy Green9659f372011-01-27 22:01:43 +00001914 /* split the proxy ads:port if given */
1915
1916 p = getenv("http_proxy");
1917 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001918 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001919 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001920 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001921 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001922
Peter Hinz56885f32011-03-02 22:03:47 +00001923 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001924 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001925 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001926 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001927 }
1928 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001929 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001930
Andy Greenb3a614a2013-01-19 13:08:17 +08001931 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001932 context->http_proxy_address,
1933 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001934 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001935
Andy Greena1ce6be2013-01-18 11:43:21 +08001936#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001937 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001938
Andy Green3faa9c72010-11-08 17:03:03 +00001939#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001940 context->use_ssl = info->ssl_cert_filepath != NULL &&
1941 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001942#ifdef USE_CYASSL
1943 lwsl_notice(" Compiled with CYASSL support\n");
1944#else
1945 lwsl_notice(" Compiled with OpenSSL support\n");
1946#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001947 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001948 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001949 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001950 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001951
Andy Green90c7cbc2011-01-27 06:26:52 +00001952#else
Andy Green2b40b792013-02-10 22:22:01 +08001953 if (info->ssl_cert_filepath != NULL &&
1954 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001955 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001956 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001957 }
Andy Greenb5b23192013-02-11 17:13:32 +08001958 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001959#endif
Andy Greena17c6922013-01-20 20:21:54 +08001960
Andy Greenb5b23192013-02-11 17:13:32 +08001961 lwsl_notice(
1962 " per-conn mem: %u + %u headers + protocol rx buf\n",
1963 sizeof(struct libwebsocket),
1964 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001965 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001966#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001967
1968 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001969#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001970#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001971 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001972#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001973
1974
1975#ifdef LWS_OPENSSL_SUPPORT
1976
1977 /* basic openssl init */
1978
1979 SSL_library_init();
1980
1981 OpenSSL_add_all_algorithms();
1982 SSL_load_error_strings();
1983
Andy Green2e24da02011-03-05 16:12:04 +00001984 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001985 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1986
Andy Green90c7cbc2011-01-27 06:26:52 +00001987 /*
1988 * Firefox insists on SSLv23 not SSLv3
1989 * Konq disables SSLv2 by default now, SSLv23 works
1990 */
1991
1992 method = (SSL_METHOD *)SSLv23_server_method();
1993 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02001994 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001995 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02001996 error,
1997 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08001998 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001999 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002000 }
Peter Hinz56885f32011-03-02 22:03:47 +00002001 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2002 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002003 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002004 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002005 error,
2006 ERR_error_string(error,
2007 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002008 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002009 }
2010
David Galeanocc148e42013-01-10 10:18:59 +08002011#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002012 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002013#endif
David Galeano77a677c2013-01-10 10:14:12 +08002014 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002015 if (info->ssl_cipher_list)
2016 SSL_CTX_set_cipher_list(context->ssl_ctx,
2017 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002018
Andy Greena1ce6be2013-01-18 11:43:21 +08002019#ifndef LWS_NO_CLIENT
2020
Andy Green90c7cbc2011-01-27 06:26:52 +00002021 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002022
Andy Green1b265272013-02-09 14:01:09 +08002023 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002024 method = (SSL_METHOD *)SSLv23_client_method();
2025 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002026 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002027 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002028 error,
2029 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002030 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002031 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002032 }
2033 /* create context */
2034 context->ssl_client_ctx = SSL_CTX_new(method);
2035 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002036 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002037 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002038 error,
2039 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002040 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002041 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002042 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002043
David Galeanocc148e42013-01-10 10:18:59 +08002044#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002045 SSL_CTX_set_options(context->ssl_client_ctx,
2046 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002047#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002048 SSL_CTX_set_options(context->ssl_client_ctx,
2049 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002050 if (info->ssl_cipher_list)
2051 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2052 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002053
Peter Hinz56885f32011-03-02 22:03:47 +00002054 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002055 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002056 if (!SSL_CTX_load_verify_locations(
2057 context->ssl_client_ctx, NULL,
2058 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002059 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002060 "Unable to load SSL Client certs from %s "
2061 "(set by --with-client-cert-dir= "
2062 "in configure) -- client ssl isn't "
2063 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002064 } else
2065 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002066 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002067 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002068 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002069 "Unable to load SSL Client certs "
2070 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002071 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002072
2073 /*
2074 * callback allowing user code to load extra verification certs
2075 * helping the client to verify server identity
2076 */
2077
prasannateamf1b353a452013-11-12 17:21:01 -08002078 /* support for client-side certificate authentication */
2079 if (info->ssl_cert_filepath) {
2080 n = SSL_CTX_use_certificate_chain_file(
2081 context->ssl_client_ctx,
2082 info->ssl_cert_filepath);
2083 if (n != 1) {
2084 lwsl_err("problem getting cert '%s' %lu: %s\n",
2085 info->ssl_cert_filepath,
2086 ERR_get_error(),
2087 ERR_error_string(ERR_get_error(),
2088 (char *)context->service_buffer));
2089 goto bail;
2090 }
2091 }
2092 if (info->ssl_private_key_filepath) {
2093 /* set the private key from KeyFile */
2094 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2095 info->ssl_private_key_filepath,
2096 SSL_FILETYPE_PEM) != 1) {
2097 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2098 info->ssl_private_key_filepath,
2099 ERR_get_error(),
2100 ERR_error_string(ERR_get_error(),
2101 (char *)context->service_buffer));
2102 goto bail;
2103 }
2104
2105 /* verify private key */
2106 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2107 lwsl_err("Private SSL key doesn't match cert\n");
2108 goto bail;
2109 }
2110 }
2111
Peter Hinz56885f32011-03-02 22:03:47 +00002112 context->protocols[0].callback(context, NULL,
2113 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2114 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002115 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002116#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002117
Andy Greenc6bf2c22011-02-20 11:10:47 +00002118 /* as a server, are we requiring clients to identify themselves? */
2119
Andy Greenb5b23192013-02-11 17:13:32 +08002120 if (info->options &
2121 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002122
2123 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002124
Peter Hinz56885f32011-03-02 22:03:47 +00002125 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002126 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2127 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002128
2129 /*
2130 * give user code a chance to load certs into the server
2131 * allowing it to verify incoming client certs
2132 */
2133
Peter Hinz56885f32011-03-02 22:03:47 +00002134 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002135 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002136 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002137 }
2138
James Devine5b34c972013-12-14 11:41:29 +08002139 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2140 /* Normally SSL listener rejects non-ssl, optionally allow */
2141 context->allow_non_ssl_on_ssl_port = 1;
2142 }
2143
Peter Hinz56885f32011-03-02 22:03:47 +00002144 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002145
2146 /* openssl init for server sockets */
2147
Andy Green3faa9c72010-11-08 17:03:03 +00002148 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002149 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002150 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002151 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002152 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002153 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002154 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002155 error,
2156 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002157 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002158 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002159 }
2160 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002161 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002162 info->ssl_private_key_filepath,
2163 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002164 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002165 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002166 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002167 error,
2168 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002169 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002170 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002171 }
2172 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002173 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002174 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002175 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002176 }
2177
2178 /* SSL is happy and has a cert it's content with */
2179 }
2180#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002181
Andy Greena1ce6be2013-01-18 11:43:21 +08002182#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002183 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002184
Andy Green1b265272013-02-09 14:01:09 +08002185 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002186 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002187
Andy Green4739e5c2011-01-22 12:51:57 +00002188 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2189 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002190 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002191 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002192 }
Andy Green775c0dd2010-10-29 14:15:22 +01002193
Joakim Soderberg4c531232013-02-06 15:26:58 +09002194#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002195 /*
2196 * allow us to restart even if old sockets in TIME_WAIT
2197 * (REUSEADDR on Unix means, "don't hang on to this
2198 * address after the listener is closed." On Windows, though,
2199 * it means "don't keep other processes from binding to
2200 * this address while we're using it)
2201 */
Andy Green6ee372f2012-04-09 15:09:01 +08002202 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2203 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002204#endif
Andy Green6c939552011-03-08 08:56:57 +00002205
2206 /* Disable Nagle */
2207 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002208 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2209 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002210
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002211 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002212 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002213 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002214 #else
Andy Greene2160712013-01-28 12:19:10 +08002215 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002216 #endif
Andy Greene2160712013-01-28 12:19:10 +08002217
Andy Green4739e5c2011-01-22 12:51:57 +00002218 bzero((char *) &serv_addr, sizeof(serv_addr));
2219 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002220 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002221 serv_addr.sin_addr.s_addr = INADDR_ANY;
2222 else
Andy Greend1eac602013-11-09 08:07:38 +08002223 if (interface_to_sa(info->iface, &serv_addr,
2224 sizeof(serv_addr)) < 0) {
2225 lwsl_err("Unable to find interface %s\n",
2226 info->iface);
2227 compatible_close(sockfd);
2228 goto bail;
2229 }
Andy Green1b265272013-02-09 14:01:09 +08002230 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002231
2232 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2233 sizeof(serv_addr));
2234 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002235 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002236 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002237 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002238 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002239 }
Andy Green0d338332011-02-12 11:57:43 +00002240
Andy Greenb5b23192013-02-11 17:13:32 +08002241 wsi = (struct libwebsocket *)malloc(
2242 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002243 if (wsi == NULL) {
2244 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002245 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002246 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002247 }
Andy Greenb5b23192013-02-11 17:13:32 +08002248 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002249 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002250#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002251 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002252#endif
Andy Green0d338332011-02-12 11:57:43 +00002253 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002254
2255 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002256
Andy Green65b0e912013-01-16 07:59:47 +08002257 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2258 context->listen_service_count = 0;
2259 context->listen_service_fd = sockfd;
2260
Andy Greena824d182013-01-15 20:52:29 +08002261 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002262 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002263 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002264#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002265
Andy Green6ee372f2012-04-09 15:09:01 +08002266 /*
2267 * drop any root privs for this process
2268 * to listen on port < 1023 we would have needed root, but now we are
2269 * listening, we don't want the power for anything else
2270 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002271#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002272#else
Andy Green1b265272013-02-09 14:01:09 +08002273 if (info->gid != -1)
2274 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002275 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002276 if (info->uid != -1)
2277 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002278 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002279#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002280
Andy Green6f520a52013-01-29 17:57:39 +08002281 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002282
Peter Hinz56885f32011-03-02 22:03:47 +00002283 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002284 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002285 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002286
Andy Green43db0452013-01-10 19:50:35 +08002287 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002288 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002289
Andy Green1b265272013-02-09 14:01:09 +08002290 info->protocols[context->count_protocols].owning_server =
2291 context;
2292 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002293 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002294
2295 /*
2296 * inform all the protocols that they are doing their one-time
2297 * initialization if they want to
2298 */
2299 info->protocols[context->count_protocols].callback(context,
2300 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002301 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002302
Andy Green3182ece2013-01-20 17:08:31 +08002303#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002304 /*
2305 * give all extensions a chance to create any per-context
2306 * allocations they need
2307 */
2308
2309 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002310 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002311 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002312
Andy Green1b265272013-02-09 14:01:09 +08002313 if (info->extensions) {
2314 ext = info->extensions;
2315 while (ext->callback) {
2316 lwsl_ext(" Extension: %s\n", ext->name);
2317 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002318 (enum libwebsocket_extension_callback_reasons)m,
2319 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002320 ext++;
2321 }
Andy Greena41314f2011-05-23 10:00:03 +01002322 }
Andy Green3182ece2013-01-20 17:08:31 +08002323#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002324 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002325
2326bail:
2327 libwebsocket_context_destroy(context);
2328 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002329}
Andy Greenb45993c2010-12-18 15:13:50 +00002330
Andy Greenb45993c2010-12-18 15:13:50 +00002331/**
shysb4e800e2013-10-24 22:12:03 +08002332 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2333 * @context: pointer to struct libwebsocket_context you want set proxy to
2334 * @proxy: pointer to c string containing proxy in format address:port
2335 *
2336 * Returns 0 if proxy string was parsed and proxy was setup.
2337 * Returns -1 if @proxy is NULL or has incorrect format.
2338 *
2339 * This is only required if your OS does not provide the http_proxy
2340 * enviroment variable (eg, OSX)
2341 *
2342 * IMPORTANT! You should call this function right after creation of the
2343 * libwebsocket_context and before call to connect. If you call this
2344 * function after connect behavior is undefined.
2345 * This function will override proxy settings made on libwebsocket_context
2346 * creation with genenv() call.
2347 */
2348
2349LWS_VISIBLE int
2350libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2351{
2352 char *p;
2353
2354 if (!proxy)
2355 return -1;
2356
2357 strncpy(context->http_proxy_address, proxy,
2358 sizeof(context->http_proxy_address) - 1);
2359 context->http_proxy_address[
2360 sizeof(context->http_proxy_address) - 1] = '\0';
2361
2362 p = strchr(context->http_proxy_address, ':');
2363 if (!p) {
2364 lwsl_err("http_proxy needs to be ads:port\n");
2365
2366 return -1;
2367 }
2368 *p = '\0';
2369 context->http_proxy_port = atoi(p + 1);
2370
2371 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2372 context->http_proxy_port);
2373
2374 return 0;
2375}
2376
2377/**
Andy Greenb45993c2010-12-18 15:13:50 +00002378 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002379 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002380 * @wsi: pointer to struct websocket you want to know the protocol of
2381 *
Andy Green8f037e42010-12-19 22:13:26 +00002382 *
Andy Green6f520a52013-01-29 17:57:39 +08002383 * Some apis can act on all live connections of a given protocol,
2384 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002385 */
Andy Greenab990e42010-10-31 12:42:52 +00002386
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002387LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002388libwebsockets_get_protocol(struct libwebsocket *wsi)
2389{
2390 return wsi->protocol;
2391}
2392
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002393LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002394libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2395{
Andy Green623a98d2013-01-21 11:04:23 +08002396 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002397}
Alex Bligh49146db2011-11-07 17:19:25 +08002398
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002399LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002400libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2401{
Andy Green623a98d2013-01-21 11:04:23 +08002402 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002403}
2404
Andy Green2af4d5b2013-02-18 16:30:10 +08002405int
Alex Bligh49146db2011-11-07 17:19:25 +08002406libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2407{
Andy Green67d556c2013-02-15 22:31:55 +08002408 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002409 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002410
Alex Bligh49146db2011-11-07 17:19:25 +08002411 /* allocate the per-connection user memory (if any) */
2412
2413 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2414 wsi->user_space = malloc(
2415 wsi->protocol->per_session_data_size);
2416 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002417 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002418 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002419 }
Andy Green6ee372f2012-04-09 15:09:01 +08002420 memset(wsi->user_space, 0,
2421 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002422 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002423 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002424}
Andy Green43db0452013-01-10 19:50:35 +08002425
Andy Green0b319092013-01-19 11:17:56 +08002426static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002427{
Andy Green0b319092013-01-19 11:17:56 +08002428 char buf[300];
2429 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002430 int n;
2431
2432 gettimeofday(&tv, NULL);
2433
2434 buf[0] = '\0';
2435 for (n = 0; n < LLL_COUNT; n++)
2436 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002437 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002438 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002439 break;
2440 }
Andy Greenb5b23192013-02-11 17:13:32 +08002441
Andy Green0b319092013-01-19 11:17:56 +08002442 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002443}
2444
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002445#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002446LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002447{
2448 lwsl_emit_stderr(level, line);
2449}
2450#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002451LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002452{
2453 int syslog_level = LOG_DEBUG;
2454
2455 switch (level) {
2456 case LLL_ERR:
2457 syslog_level = LOG_ERR;
2458 break;
2459 case LLL_WARN:
2460 syslog_level = LOG_WARNING;
2461 break;
2462 case LLL_NOTICE:
2463 syslog_level = LOG_NOTICE;
2464 break;
2465 case LLL_INFO:
2466 syslog_level = LOG_INFO;
2467 break;
2468 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002469 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002470}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002471#endif
Andy Greenc11db202013-01-19 11:12:16 +08002472
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002473LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002474{
Andy Greende8f27a2013-01-12 09:17:42 +08002475 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002476 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002477
2478 if (!(log_level & filter))
2479 return;
2480
Andy Green43db0452013-01-10 19:50:35 +08002481 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002482 vsnprintf(buf, sizeof(buf), format, ap);
2483 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002484 va_end(ap);
2485
Andy Green0b319092013-01-19 11:17:56 +08002486 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002487}
2488
2489/**
2490 * lws_set_log_level() - Set the logging bitfield
2491 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002492 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2493 * function to perform log string emission instead of
2494 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002495 *
Andy Greenc6511a02013-02-19 10:01:48 +08002496 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002497 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002498 */
2499
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002500LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002501 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002502{
2503 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002504 if (log_emit_function)
2505 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002506}