blob: 687bad6987e00f55933deaadf8239d13c6065c52 [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
Andy Green7a132792013-12-18 09:48:26 +0800121 context->protocols[0].callback(context, wsi,
122 LWS_CALLBACK_LOCK_POLL,
123 wsi->user_space, (void *)(long)wsi->sock, 0);
124
Andy Greendfb23042013-01-17 12:26:48 +0800125 context->lws_lookup[wsi->sock] = wsi;
126 wsi->position_in_fds_table = context->fds_count;
127 context->fds[context->fds_count].fd = wsi->sock;
128 context->fds[context->fds_count].events = POLLIN;
129 context->fds[context->fds_count++].revents = 0;
130
131 /* external POLL support via protocol 0 */
132 context->protocols[0].callback(context, wsi,
133 LWS_CALLBACK_ADD_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800134 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000135
Andy Green7a132792013-12-18 09:48:26 +0800136 context->protocols[0].callback(context, wsi,
137 LWS_CALLBACK_UNLOCK_POLL,
138 wsi->user_space, (void *)(long)wsi->sock, 0);
139
Andy Green0d338332011-02-12 11:57:43 +0000140 return 0;
141}
142
Andy Greendfb23042013-01-17 12:26:48 +0800143static int
Andy Greenb5b23192013-02-11 17:13:32 +0800144remove_wsi_socket_from_fds(struct libwebsocket_context *context,
145 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000146{
Andy Greendfb23042013-01-17 12:26:48 +0800147 int m;
Andy Green0d338332011-02-12 11:57:43 +0000148
Andy Green7a132792013-12-18 09:48:26 +0800149 if (!--context->fds_count) {
150 context->protocols[0].callback(context, wsi,
151 LWS_CALLBACK_LOCK_POLL,
152 wsi->user_space, (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800153 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800154 }
Andy Green0d338332011-02-12 11:57:43 +0000155
Andy Greendfb23042013-01-17 12:26:48 +0800156 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800157 lwsl_err("Socket fd %d too high (%d)\n",
158 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800159 return 1;
160 }
Andy Green0d338332011-02-12 11:57:43 +0000161
Andy Greenb5b23192013-02-11 17:13:32 +0800162 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
163 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800164
Andy Green7a132792013-12-18 09:48:26 +0800165 context->protocols[0].callback(context, wsi,
166 LWS_CALLBACK_LOCK_POLL,
167 wsi->user_space, (void *)(long)wsi->sock, 0);
168
Andy Greendfb23042013-01-17 12:26:48 +0800169 m = wsi->position_in_fds_table; /* replace the contents for this */
170
171 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800172 context->fds[m] = context->fds[context->fds_count];
173 /*
174 * end guy's fds_lookup entry remains unchanged
175 * (still same fd pointing to same wsi)
176 */
Andy Greendfb23042013-01-17 12:26:48 +0800177 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800178 context->lws_lookup[context->fds[context->fds_count].fd]->
179 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800180 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800181 context->lws_lookup[wsi->sock] = NULL;
182 /* removed wsi has no position any more */
183 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800184
185do_ext:
186 /* remove also from external POLL support via protocol 0 */
187 if (wsi->sock)
188 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800189 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
190 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800191
Andy Green7a132792013-12-18 09:48:26 +0800192 context->protocols[0].callback(context, wsi,
193 LWS_CALLBACK_UNLOCK_POLL,
194 wsi->user_space, (void *)(long)wsi->sock, 0);
195
Andy Greendfb23042013-01-17 12:26:48 +0800196 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000197}
198
Andy Green32375b72011-02-19 08:32:53 +0000199
Andy Green8f037e42010-12-19 22:13:26 +0000200void
Peter Hinz56885f32011-03-02 22:03:47 +0000201libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000202 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000203{
Andy Greenb45993c2010-12-18 15:13:50 +0000204 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000205 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000206 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
207 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800208#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000209 int ret;
210 int m;
211 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100212 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800213#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000214
Andy Green4b6fbe12011-02-14 08:03:48 +0000215 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000216 return;
217
Andy Green62c54d22011-02-14 09:14:25 +0000218 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000219
Andy Green62c54d22011-02-14 09:14:25 +0000220 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000221 return;
222
Andy Green22524a62013-02-15 22:48:58 +0800223 /* we tried the polite way... */
224 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
225 goto just_kill_connection;
226
Andy Green623a98d2013-01-21 11:04:23 +0800227 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000228
Andy Green5dc62ea2013-09-20 20:26:12 +0800229 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
230 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
231
232 context->protocols[0].callback(context, wsi,
233 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
234
235 free(wsi->u.hdr.ah);
236 goto just_kill_connection;
237 }
238
239
kapejodce64fb02013-11-19 13:38:16 +0100240 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
241 if (wsi->u.http.post_buffer) {
242 free(wsi->u.http.post_buffer);
243 wsi->u.http.post_buffer = NULL;
244 }
245 if (wsi->u.http.fd >= 0) {
246 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
247 close(wsi->u.http.fd);
248 wsi->u.http.fd = -1;
249 context->protocols[0].callback(context, wsi,
250 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
251 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800252 }
253
Andy Green3182ece2013-01-20 17:08:31 +0800254#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000255 /*
Andy Green68b45042011-05-25 21:41:57 +0100256 * are his extensions okay with him closing? Eg he might be a mux
257 * parent and just his ch1 aspect is closing?
258 */
259
Andy Green68b45042011-05-25 21:41:57 +0100260 for (n = 0; n < wsi->count_active_extensions; n++) {
261 if (!wsi->active_extensions[n]->callback)
262 continue;
263
264 m = wsi->active_extensions[n]->callback(context,
265 wsi->active_extensions[n], wsi,
266 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
267 wsi->active_extensions_user[n], NULL, 0);
268
269 /*
270 * if somebody vetoed actually closing him at this time....
271 * up to the extension to track the attempted close, let's
272 * just bail
273 */
274
275 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800276 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100277 return;
278 }
279 }
280
Andy Green68b45042011-05-25 21:41:57 +0100281 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000282 * flush any tx pending from extensions, since we may send close packet
283 * if there are problems with send, just nuke the connection
284 */
285
286 ret = 1;
287 while (ret == 1) {
288
289 /* default to nobody has more to spill */
290
291 ret = 0;
292 eff_buf.token = NULL;
293 eff_buf.token_len = 0;
294
295 /* show every extension the new incoming data */
296
297 for (n = 0; n < wsi->count_active_extensions; n++) {
298 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000299 wsi->protocol->owning_server,
300 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000301 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
302 wsi->active_extensions_user[n], &eff_buf, 0);
303 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800304 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000305 goto just_kill_connection;
306 }
307 if (m)
308 /*
309 * at least one extension told us he has more
310 * to spill, so we will go around again after
311 */
312 ret = 1;
313 }
314
315 /* assuming they left us something to send, send it */
316
317 if (eff_buf.token_len)
318 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800319 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800320 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000321 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800322 }
Andy Greenc44159f2011-03-07 07:08:18 +0000323 }
Andy Green3182ece2013-01-20 17:08:31 +0800324#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000325
326 /*
Andy Greenda527df2011-03-07 07:08:12 +0000327 * signal we are closing, libsocket_write will
328 * add any necessary version-specific stuff. If the write fails,
329 * no worries we are closing anyway. If we didn't initiate this
330 * close, then our state has been changed to
331 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
332 *
333 * Likewise if it's a second call to close this connection after we
334 * sent the close indication to the peer already, we are in state
335 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
336 */
337
338 if (old_state == WSI_STATE_ESTABLISHED &&
339 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100340
Andy Green43db0452013-01-10 19:50:35 +0800341 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100342
Andy Greenfdd305a2013-02-11 14:32:48 +0800343 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800344 memset(buf, 0, sizeof(buf));
345 n = libwebsocket_write(wsi,
346 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000347 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800348 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000349 /*
350 * we have sent a nice protocol level indication we
351 * now wish to close, we should not send anything more
352 */
353
354 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
355
Andy Greenb5b23192013-02-11 17:13:32 +0800356 /*
357 * ...and we should wait for a reply for a bit
358 * out of politeness
359 */
Andy Greenda527df2011-03-07 07:08:12 +0000360
361 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800362 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000363
Andy Green43db0452013-01-10 19:50:35 +0800364 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000365
366 return;
367 }
368
Andy Greenb5b23192013-02-11 17:13:32 +0800369 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800370
Andy Greenda527df2011-03-07 07:08:12 +0000371 /* else, the send failed and we should just hang up */
372 }
373
Andy Greenc44159f2011-03-07 07:08:18 +0000374just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100375
Andy Greenb5b23192013-02-11 17:13:32 +0800376 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100377
Andy Greenda527df2011-03-07 07:08:12 +0000378 /*
379 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800380 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000381 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000382
Andy Greendfb23042013-01-17 12:26:48 +0800383 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000384
Andy Green251f6fa2010-11-03 11:13:06 +0000385 wsi->state = WSI_STATE_DEAD_SOCKET;
386
Andy Greenb5b23192013-02-11 17:13:32 +0800387 if ((old_state == WSI_STATE_ESTABLISHED ||
388 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800389 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
390
391 if (wsi->u.ws.rx_user_buffer) {
392 free(wsi->u.ws.rx_user_buffer);
393 wsi->u.ws.rx_user_buffer = NULL;
394 }
395 if (wsi->u.ws.rxflow_buffer) {
396 free(wsi->u.ws.rxflow_buffer);
397 wsi->u.ws.rxflow_buffer = NULL;
398 }
Andy Green2764eba2013-12-09 14:16:17 +0800399 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800400 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800401 free(wsi->truncated_send_malloc);
402 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800403 }
Andy Green54495112013-02-06 21:10:16 +0900404 }
405
Andy Green4b6fbe12011-02-14 08:03:48 +0000406 /* tell the user it's all over for this guy */
407
Andy Greend4302732011-02-28 07:45:29 +0000408 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800409 ((old_state == WSI_STATE_ESTABLISHED) ||
410 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
411 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800412 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000413 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000414 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800415 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
416 lwsl_debug("calling back CLOSED_HTTP\n");
417 context->protocols[0].callback(context, wsi,
418 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800419 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800420 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000421
Andy Green3182ece2013-01-20 17:08:31 +0800422#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000423 /* deallocate any active extension contexts */
424
425 for (n = 0; n < wsi->count_active_extensions; n++) {
426 if (!wsi->active_extensions[n]->callback)
427 continue;
428
Andy Green46c2ea02011-03-22 09:04:01 +0000429 wsi->active_extensions[n]->callback(context,
430 wsi->active_extensions[n], wsi,
431 LWS_EXT_CALLBACK_DESTROY,
432 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000433
434 free(wsi->active_extensions_user[n]);
435 }
436
Andy Greena41314f2011-05-23 10:00:03 +0100437 /*
438 * inform all extensions in case they tracked this guy out of band
439 * even though not active on him specifically
440 */
441
442 ext = context->extensions;
443 while (ext && ext->callback) {
444 ext->callback(context, ext, wsi,
445 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
446 NULL, NULL, 0);
447 ext++;
448 }
Andy Green3182ece2013-01-20 17:08:31 +0800449#endif
Andy Greena41314f2011-05-23 10:00:03 +0100450
Andy Green43db0452013-01-10 19:50:35 +0800451/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000452
Andy Green3faa9c72010-11-08 17:03:03 +0000453#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000454 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000455 n = SSL_get_fd(wsi->ssl);
456 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800457 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000458 SSL_free(wsi->ssl);
459 } else {
460#endif
Andy Green0303db42013-01-17 14:46:43 +0800461 if (wsi->sock) {
462 n = shutdown(wsi->sock, SHUT_RDWR);
463 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800464 lwsl_debug("closing: shutdown returned %d\n",
465 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800466
Andy Green0303db42013-01-17 14:46:43 +0800467 n = compatible_close(wsi->sock);
468 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800469 lwsl_debug("closing: close returned %d\n",
470 errno);
Andy Green0303db42013-01-17 14:46:43 +0800471 }
Andy Green3faa9c72010-11-08 17:03:03 +0000472#ifdef LWS_OPENSSL_SUPPORT
473 }
474#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800475 if (wsi->protocol && wsi->protocol->per_session_data_size &&
476 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000477 free(wsi->user_space);
478
Andy Green251f6fa2010-11-03 11:13:06 +0000479 free(wsi);
480}
481
Andy Green07034092011-02-13 08:37:12 +0000482/**
483 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800484 * @context: Libwebsockets context
485 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000486 * @fd: Connection socket descriptor
487 * @name: Buffer to take client address name
488 * @name_len: Length of client address name buffer
489 * @rip: Buffer to take client address IP qotted quad
490 * @rip_len: Length of client address IP buffer
491 *
492 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800493 * the client connected with socket descriptor @fd. Names may be
494 * truncated if there is not enough room. If either cannot be
495 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000496 */
497
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800498LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800499libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
500 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000501 char *rip, int rip_len)
502{
Bob Robertsac049112013-04-25 09:16:30 +0800503 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000504 struct sockaddr_in sin;
505 struct hostent *host;
506 struct hostent *host1;
507 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000508 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000509 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800510 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800511#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800512 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800513#endif
Andy Green07034092011-02-13 08:37:12 +0000514
515 rip[0] = '\0';
516 name[0] = '\0';
517
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800518 lws_latency_pre(context, wsi);
519
Andy Greenb5b23192013-02-11 17:13:32 +0800520 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000521 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
522 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800523 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000524 }
Andy Green6ee372f2012-04-09 15:09:01 +0800525
Andy Greenb5b23192013-02-11 17:13:32 +0800526 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000527 AF_INET);
528 if (host == NULL) {
529 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800530 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000531 }
532
533 strncpy(name, host->h_name, name_len);
534 name[name_len - 1] = '\0';
535
536 host1 = gethostbyname(host->h_name);
537 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800538 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000539 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000540 n = 0;
541 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000542 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000543 if (p == NULL)
544 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000545 if ((host1->h_addrtype != AF_INET)
546#ifdef AF_LOCAL
547 && (host1->h_addrtype != AF_LOCAL)
548#endif
549 )
Andy Green07034092011-02-13 08:37:12 +0000550 continue;
551
Andy Green7627af52011-03-09 15:13:52 +0000552 if (host1->h_addrtype == AF_INET)
553 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000554#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000555 else {
556 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800557 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000558 ip[sizeof(ip) - 1] = '\0';
559 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000560#endif
Andy Green07034092011-02-13 08:37:12 +0000561 p = NULL;
562 strncpy(rip, ip, rip_len);
563 rip[rip_len - 1] = '\0';
564 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800565
566 ret = 0;
567bail:
568 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000569}
Andy Green9f990342011-02-12 11:57:45 +0000570
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800571LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000572 void *buf, int len)
573{
574 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800575 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000576
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100577#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000578 for (n = 0; n < len; n++)
579 p[n] = (unsigned char)rand();
580#else
581 n = read(context->fd_random, p, len);
582#endif
583
584 return n;
585}
586
Andy Greena690cd02013-02-09 12:25:31 +0800587int lws_set_socket_options(struct libwebsocket_context *context, int fd)
588{
589 int optval = 1;
590 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100591#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800592 unsigned long optl = 0;
593#endif
594#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
595 struct protoent *tcp_proto;
596#endif
597
598 if (context->ka_time) {
599 /* enable keepalive on this socket */
600 optval = 1;
601 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
602 (const void *)&optval, optlen) < 0)
603 return 1;
604
Bob Robertsac049112013-04-25 09:16:30 +0800605#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800606
607 /*
608 * didn't find a way to set these per-socket, need to
609 * tune kernel systemwide values
610 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100611#elif WIN32
612 {
613 DWORD dwBytesRet;
614 struct tcp_keepalive alive;
615 alive.onoff = TRUE;
616 alive.keepalivetime = context->ka_time;
617 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800618
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100619 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
620 NULL, 0, &dwBytesRet, NULL, NULL))
621 return 1;
622 }
Andy Greena47865f2013-02-10 09:39:47 +0800623#else
Andy Greena690cd02013-02-09 12:25:31 +0800624 /* set the keepalive conditions we want on it too */
625 optval = context->ka_time;
626 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
627 (const void *)&optval, optlen) < 0)
628 return 1;
629
Larry Hayesbb66ac62013-02-22 09:16:20 +0800630 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800631 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
632 (const void *)&optval, optlen) < 0)
633 return 1;
634
Larry Hayesbb66ac62013-02-22 09:16:20 +0800635 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800636 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
637 (const void *)&optval, optlen) < 0)
638 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800639#endif
Andy Greena690cd02013-02-09 12:25:31 +0800640 }
641
642 /* Disable Nagle */
643 optval = 1;
644#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
645 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
646#else
647 tcp_proto = getprotobyname("TCP");
648 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
649#endif
650
651 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100652#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800653 ioctlsocket(fd, FIONBIO, &optl);
654#else
655 fcntl(fd, F_SETFL, O_NONBLOCK);
656#endif
657
658 return 0;
659}
660
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800661LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000662{
663 struct pollfd fds;
664
Andy Green2764eba2013-12-09 14:16:17 +0800665 /* treat the fact we got a truncated send pending as if we're choked */
666 if (wsi->truncated_send_malloc)
667 return 1;
668
Andy Green95a7b5d2011-03-06 10:29:39 +0000669 fds.fd = wsi->sock;
670 fds.events = POLLOUT;
671 fds.revents = 0;
672
673 if (poll(&fds, 1, 0) != 1)
674 return 1;
675
676 if ((fds.revents & POLLOUT) == 0)
677 return 1;
678
679 /* okay to send another packet without blocking */
680
681 return 0;
682}
683
Andy Greena41314f2011-05-23 10:00:03 +0100684int
Andy Green3b84c002011-03-06 13:14:42 +0000685lws_handle_POLLOUT_event(struct libwebsocket_context *context,
686 struct libwebsocket *wsi, struct pollfd *pollfd)
687{
Andy Green3b84c002011-03-06 13:14:42 +0000688 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800689
Andy Green3182ece2013-01-20 17:08:31 +0800690#ifndef LWS_NO_EXTENSIONS
691 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000692 int ret;
693 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100694 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000695
Andy Green1f4267b2013-10-17 08:09:19 +0800696 /* pending truncated sends have uber priority */
697
Andy Green2764eba2013-12-09 14:16:17 +0800698 if (wsi->truncated_send_malloc) {
699 lws_issue_raw(wsi, wsi->truncated_send_malloc +
700 wsi->truncated_send_offset,
701 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800702 /* leave POLLOUT active either way */
703 return 0;
704 }
705
Andy Greena41314f2011-05-23 10:00:03 +0100706 for (n = 0; n < wsi->count_active_extensions; n++) {
707 if (!wsi->active_extensions[n]->callback)
708 continue;
709
710 m = wsi->active_extensions[n]->callback(context,
711 wsi->active_extensions[n], wsi,
712 LWS_EXT_CALLBACK_IS_WRITEABLE,
713 wsi->active_extensions_user[n], NULL, 0);
714 if (m > handled)
715 handled = m;
716 }
717
718 if (handled == 1)
719 goto notify_action;
720
721 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000722 goto user_service;
723
724 /*
725 * check in on the active extensions, see if they
726 * had pending stuff to spill... they need to get the
727 * first look-in otherwise sequence will be disordered
728 *
729 * NULL, zero-length eff_buf means just spill pending
730 */
731
732 ret = 1;
733 while (ret == 1) {
734
735 /* default to nobody has more to spill */
736
737 ret = 0;
738 eff_buf.token = NULL;
739 eff_buf.token_len = 0;
740
741 /* give every extension a chance to spill */
742
743 for (n = 0; n < wsi->count_active_extensions; n++) {
744 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000745 wsi->protocol->owning_server,
746 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000747 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
748 wsi->active_extensions_user[n], &eff_buf, 0);
749 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800750 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000751 return -1;
752 }
753 if (m)
754 /*
755 * at least one extension told us he has more
756 * to spill, so we will go around again after
757 */
758 ret = 1;
759 }
760
761 /* assuming they gave us something to send, send it */
762
763 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800764 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
765 eff_buf.token_len);
766 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000767 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800768 /*
769 * Keep amount spilled small to minimize chance of this
770 */
771 if (n != eff_buf.token_len) {
772 lwsl_err("Unable to spill ext %d vs %s\n",
773 eff_buf.token_len, n);
774 return -1;
775 }
Andy Green3b84c002011-03-06 13:14:42 +0000776 } else
777 continue;
778
779 /* no extension has more to spill */
780
781 if (!ret)
782 continue;
783
784 /*
785 * There's more to spill from an extension, but we just sent
786 * something... did that leave the pipe choked?
787 */
788
789 if (!lws_send_pipe_choked(wsi))
790 /* no we could add more */
791 continue;
792
Andy Green43db0452013-01-10 19:50:35 +0800793 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000794
795 /*
796 * Yes, he's choked. Leave the POLLOUT masked on so we will
797 * come back here when he is unchoked. Don't call the user
798 * callback to enforce ordering of spilling, he'll get called
799 * when we come back here and there's nothing more to spill.
800 */
801
802 return 0;
803 }
804
805 wsi->extension_data_pending = 0;
806
807user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800808#endif
Andy Green3b84c002011-03-06 13:14:42 +0000809 /* one shot */
810
Andy Green91f19d82013-12-21 11:18:34 +0800811 if (pollfd)
812 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800813
Andy Green3182ece2013-01-20 17:08:31 +0800814#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100815notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800816#endif
Andy Green3b84c002011-03-06 13:14:42 +0000817
Andy Green9e4c2b62011-03-07 20:47:39 +0000818 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
819 n = LWS_CALLBACK_CLIENT_WRITEABLE;
820 else
821 n = LWS_CALLBACK_SERVER_WRITEABLE;
822
Andy Greenb8b247d2013-01-22 07:20:08 +0800823 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800824 wsi, (enum libwebsocket_callback_reasons) n,
825 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000826}
827
828
829
Andy Green1c6e1422013-02-20 19:11:31 +0800830int
Andy Greena41314f2011-05-23 10:00:03 +0100831libwebsocket_service_timeout_check(struct libwebsocket_context *context,
832 struct libwebsocket *wsi, unsigned int sec)
833{
Andy Green3182ece2013-01-20 17:08:31 +0800834#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100835 int n;
836
837 /*
838 * if extensions want in on it (eg, we are a mux parent)
839 * give them a chance to service child timeouts
840 */
841
842 for (n = 0; n < wsi->count_active_extensions; n++)
843 wsi->active_extensions[n]->callback(
844 context, wsi->active_extensions[n],
845 wsi, LWS_EXT_CALLBACK_1HZ,
846 wsi->active_extensions_user[n], NULL, sec);
847
Andy Green3182ece2013-01-20 17:08:31 +0800848#endif
Andy Greena41314f2011-05-23 10:00:03 +0100849 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800850 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800851
Andy Greena41314f2011-05-23 10:00:03 +0100852 /*
853 * if we went beyond the allowed time, kill the
854 * connection
855 */
856
857 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800858 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100859 libwebsocket_close_and_free_session(context,
860 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800861 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100862 }
Andy Green1c6e1422013-02-20 19:11:31 +0800863
864 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100865}
866
Andy Green9f990342011-02-12 11:57:45 +0000867/**
868 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000869 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000870 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800871 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000872 *
Andy Green75006172013-01-22 12:32:11 +0800873 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800874 * services it according to the state of the associated
875 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800876 *
877 * The one call deals with all "service" that might happen on a socket
878 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800879 *
880 * If a pollfd says it has something, you can just pass it to
881 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
882 * If it sees it is a lws socket, the traffic will be handled and
883 * pollfd->revents will be zeroed now.
884 *
885 * If the socket is foreign to lws, it leaves revents alone. So you can
886 * see if you should service yourself by checking the pollfd revents
887 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000888 */
889
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800890LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000891libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000892 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000893{
Andy Greena1ce6be2013-01-18 11:43:21 +0800894 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000895 int n;
Andy Green0d338332011-02-12 11:57:43 +0000896 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800897 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000898 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800899 int timed_out = 0;
900 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800901 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800902
Andy Green3182ece2013-01-20 17:08:31 +0800903#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000904 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800905#endif
Andy Green98a717c2011-03-06 13:14:15 +0000906 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800907
908 if (context->listen_service_fd)
909 listen_socket_fds_index = context->lws_lookup[
910 context->listen_service_fd]->position_in_fds_table;
911
Andy Greena71eafc2011-02-14 17:59:43 +0000912 /*
913 * you can call us with pollfd = NULL to just allow the once-per-second
914 * global timeout checks; if less than a second since the last check
915 * it returns immediately then.
916 */
917
918 gettimeofday(&tv, NULL);
919
Peter Hinz56885f32011-03-02 22:03:47 +0000920 if (context->last_timeout_check_s != tv.tv_sec) {
921 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000922
Joakim Soderberg4c531232013-02-06 15:26:58 +0900923 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800924 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800925 if (context->started_with_parent &&
926 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800927 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900928 #endif
Andy Green24cba922013-01-19 13:56:10 +0800929
Andy Greena71eafc2011-02-14 17:59:43 +0000930 /* global timeout check once per second */
931
Andy Green1c6e1422013-02-20 19:11:31 +0800932 if (pollfd)
933 our_fd = pollfd->fd;
934
Peter Hinz56885f32011-03-02 22:03:47 +0000935 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800936 m = context->fds[n].fd;
937 wsi = context->lws_lookup[m];
938 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800939 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800940
941 if (libwebsocket_service_timeout_check(context, wsi,
942 tv.tv_sec))
943 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800944 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800945 /* it was the guy we came to service! */
946 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800947 /* mark as handled */
948 pollfd->revents = 0;
949 }
Andy Greena71eafc2011-02-14 17:59:43 +0000950 }
951 }
952
Andy Green1c6e1422013-02-20 19:11:31 +0800953 /* the socket we came to service timed out, nothing to do */
954 if (timed_out)
955 return 0;
956
Andy Greena71eafc2011-02-14 17:59:43 +0000957 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000958 if (pollfd == NULL)
959 return 0;
960
961 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800962 wsi = context->lws_lookup[pollfd->fd];
963 if (wsi == NULL)
964 /* not lws connection ... leave revents alone and return */
965 return 0;
966
967 /*
968 * so that caller can tell we handled, past here we need to
969 * zero down pollfd->revents after handling
970 */
971
Andy Green65b0e912013-01-16 07:59:47 +0800972 /*
973 * deal with listen service piggybacking
974 * every listen_service_modulo services of other fds, we
975 * sneak one in to service the listen socket if there's anything waiting
976 *
977 * To handle connection storms, as found in ab, if we previously saw a
978 * pending connection here, it causes us to check again next time.
979 */
980
Andy Greenb5b23192013-02-11 17:13:32 +0800981 if (context->listen_service_fd && pollfd !=
982 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800983 context->listen_service_count++;
984 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800985 context->listen_service_count ==
986 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800987 context->listen_service_count = 0;
988 m = 1;
989 if (context->listen_service_extraseen > 5)
990 m = 2;
991 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800992 /*
993 * even with extpoll, we prepared this
994 * internal fds for listen
995 */
996 n = poll(&context->fds[listen_socket_fds_index],
997 1, 0);
998 if (n > 0) { /* there's a conn waiting for us */
999 libwebsocket_service_fd(context,
1000 &context->
1001 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001002 context->listen_service_extraseen++;
1003 } else {
1004 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001005 context->
1006 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001007 break;
1008 }
1009 }
1010 }
1011
1012 }
1013
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001014 /* handle session socket closed */
1015
1016 if ((!(pollfd->revents & POLLIN)) &&
1017 (pollfd->revents & (POLLERR | POLLHUP))) {
1018
1019 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1020 (void *)wsi, pollfd->fd);
1021
1022 goto close_and_handled;
1023 }
1024
Andy Green65b0e912013-01-16 07:59:47 +08001025 /* okay, what we came here to do... */
1026
Andy Green0d338332011-02-12 11:57:43 +00001027 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001028
Andy Greena1ce6be2013-01-18 11:43:21 +08001029#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001030 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001031 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001032 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001033 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001034 n = lws_server_socket_service(context, wsi, pollfd);
1035 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001036#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001037
Andy Green0d338332011-02-12 11:57:43 +00001038 case LWS_CONNMODE_WS_SERVING:
1039 case LWS_CONNMODE_WS_CLIENT:
1040
Andy Green0d338332011-02-12 11:57:43 +00001041 /* the guy requested a callback when it was OK to write */
1042
Andy Greenda527df2011-03-07 07:08:12 +00001043 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001044 wsi->state == WSI_STATE_ESTABLISHED &&
1045 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001046 lwsl_info("libwebsocket_service_fd: closing\n");
1047 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001048 }
Andy Green0d338332011-02-12 11:57:43 +00001049
Andy Greenca0a1292013-03-16 11:24:23 +08001050 if (wsi->u.ws.rxflow_buffer &&
1051 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1052 lwsl_info("draining rxflow\n");
1053 /* well, drain it */
1054 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1055 wsi->u.ws.rxflow_pos;
1056 eff_buf.token_len = wsi->u.ws.rxflow_len -
1057 wsi->u.ws.rxflow_pos;
1058 draining_flow = 1;
1059 goto drain;
1060 }
1061
Andy Green0d338332011-02-12 11:57:43 +00001062 /* any incoming data ready? */
1063
1064 if (!(pollfd->revents & POLLIN))
1065 break;
1066
Andy Greenb45993c2010-12-18 15:13:50 +00001067#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001068read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001069 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001070 eff_buf.token_len = SSL_read(wsi->ssl,
1071 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001072 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001073 if (!eff_buf.token_len) {
1074 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001075 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001076 ERR_error_string(n,
1077 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001078 }
1079 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001080#endif
Andy Greene84652c2013-02-08 13:01:02 +08001081 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001082 context->service_buffer,
1083 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001084
Andy Green98a717c2011-03-06 13:14:15 +00001085 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001086 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1087 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001088 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001089 goto close_and_handled;
1090 n = 0;
1091 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001092 }
Andy Green98a717c2011-03-06 13:14:15 +00001093 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001094 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001095 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001096 }
1097
Andy Green98a717c2011-03-06 13:14:15 +00001098 /*
1099 * give any active extensions a chance to munge the buffer
1100 * before parse. We pass in a pointer to an lws_tokens struct
1101 * prepared with the default buffer and content length that's in
1102 * there. Rather than rewrite the default buffer, extensions
1103 * that expect to grow the buffer can adapt .token to
1104 * point to their own per-connection buffer in the extension
1105 * user allocation. By default with no extensions or no
1106 * extension callback handling, just the normal input buffer is
1107 * used then so it is efficient.
1108 */
Andy Greenb45993c2010-12-18 15:13:50 +00001109
Andy Greene84652c2013-02-08 13:01:02 +08001110 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001111drain:
Andy Green3182ece2013-01-20 17:08:31 +08001112#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001113 more = 1;
1114 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001115
Andy Green98a717c2011-03-06 13:14:15 +00001116 more = 0;
1117
1118 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001119 m = wsi->active_extensions[n]->callback(context,
1120 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001121 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001122 wsi->active_extensions_user[n],
1123 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001124 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001125 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001126 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001127 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001128 }
1129 if (m)
1130 more = 1;
1131 }
Andy Green3182ece2013-01-20 17:08:31 +08001132#endif
Andy Green98a717c2011-03-06 13:14:15 +00001133 /* service incoming data */
1134
1135 if (eff_buf.token_len) {
1136 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001137 (unsigned char *)eff_buf.token,
1138 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001139 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001140 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001141 n = 0;
1142 goto handled;
1143 }
Andy Green98a717c2011-03-06 13:14:15 +00001144 }
Andy Green3182ece2013-01-20 17:08:31 +08001145#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001146 eff_buf.token = NULL;
1147 eff_buf.token_len = 0;
1148 }
Andy Green3182ece2013-01-20 17:08:31 +08001149#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001150 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1151 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1152 lwsl_info("flow buffer: drained\n");
1153 free(wsi->u.ws.rxflow_buffer);
1154 wsi->u.ws.rxflow_buffer = NULL;
1155 /* having drained the rxflow buffer, can rearm POLLIN */
1156 _libwebsocket_rx_flow_control(wsi);
1157 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001158
1159#ifdef LWS_OPENSSL_SUPPORT
1160 if (wsi->ssl && SSL_pending(wsi->ssl))
1161 goto read_pending;
1162#endif
Andy Green98a717c2011-03-06 13:14:15 +00001163 break;
Andy Green76f61e72013-01-16 11:53:05 +08001164
1165 default:
Andy Green03674a62013-01-16 11:47:40 +08001166#ifdef LWS_NO_CLIENT
1167 break;
1168#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001169 n = lws_client_socket_service(context, wsi, pollfd);
1170 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001171#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001172 }
1173
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001174 n = 0;
1175 goto handled;
1176
1177close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001178 libwebsocket_close_and_free_session(context, wsi,
1179 LWS_CLOSE_STATUS_NOSTATUS);
1180 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001181
1182handled:
1183 pollfd->revents = 0;
1184 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001185}
1186
Andy Green0d338332011-02-12 11:57:43 +00001187
Andy Green6964bb52011-01-23 16:50:33 +00001188/**
1189 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001190 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001191 *
1192 * This function closes any active connections and then frees the
1193 * context. After calling this, any further use of the context is
1194 * undefined.
1195 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001196LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001197libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001198{
Andy Green3182ece2013-01-20 17:08:31 +08001199#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001200 int n;
1201 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001202 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001203 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001204
Andy Greend636e352013-01-29 12:36:17 +08001205#ifdef LWS_LATENCY
1206 if (context->worst_latency_info[0])
1207 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1208#endif
1209
Andy Greendfb23042013-01-17 12:26:48 +08001210 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001211 struct libwebsocket *wsi =
1212 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001213 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001214 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1215 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001216 }
Andy Green6964bb52011-01-23 16:50:33 +00001217
Andy Greena41314f2011-05-23 10:00:03 +01001218 /*
1219 * give all extensions a chance to clean up any per-context
1220 * allocations they might have made
1221 */
1222
1223 ext = context->extensions;
1224 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1225 if (context->listen_port)
1226 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001227 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001228 ext->callback(context, ext, NULL,
1229 (enum libwebsocket_extension_callback_reasons)m,
1230 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001231 ext++;
1232 }
Andy Greena7109e62013-02-11 12:05:54 +08001233
1234 /*
1235 * inform all the protocols that they are done and will have no more
1236 * callbacks
1237 */
1238
1239 while (protocol->callback) {
1240 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1241 NULL, NULL, 0);
1242 protocol++;
1243 }
1244
Andy Green3182ece2013-01-20 17:08:31 +08001245#endif
Andy Greena41314f2011-05-23 10:00:03 +01001246
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001247#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001248#else
1249 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001250#endif
1251
Peter Hinz56885f32011-03-02 22:03:47 +00001252#ifdef LWS_OPENSSL_SUPPORT
1253 if (context->ssl_ctx)
1254 SSL_CTX_free(context->ssl_ctx);
1255 if (context->ssl_client_ctx)
1256 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001257
1258 ERR_remove_state(0);
1259 ERR_free_strings();
1260 EVP_cleanup();
1261 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001262#endif
1263
Andy Green46596482013-02-11 11:04:01 +08001264 if (context->fds)
1265 free(context->fds);
1266 if (context->lws_lookup)
1267 free(context->lws_lookup);
1268
Peter Hinz56885f32011-03-02 22:03:47 +00001269 free(context);
1270
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001271#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001272 WSACleanup();
1273#endif
Andy Green6964bb52011-01-23 16:50:33 +00001274}
1275
Andy Greend88146d2013-01-22 12:40:35 +08001276/**
Andy Greenb5b23192013-02-11 17:13:32 +08001277 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001278 * @context: Websocket context
1279 *
1280 * This returns the optional user allocation that can be attached to
1281 * the context the sockets live in at context_create time. It's a way
1282 * to let all sockets serviced in the same context share data without
1283 * using globals statics in the user code.
1284 */
Alon Levy0291eb32012-10-19 11:21:56 +02001285LWS_EXTERN void *
1286libwebsocket_context_user(struct libwebsocket_context *context)
1287{
Andy Greenb5b23192013-02-11 17:13:32 +08001288 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001289}
1290
Andy Green6964bb52011-01-23 16:50:33 +00001291/**
1292 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001293 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001294 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1295 * service otherwise block and service immediately, returning
1296 * after the timeout if nothing needed service.
1297 *
1298 * This function deals with any pending websocket traffic, for three
1299 * kinds of event. It handles these events on both server and client
1300 * types of connection the same.
1301 *
1302 * 1) Accept new connections to our context's server
1303 *
Andy Green6f520a52013-01-29 17:57:39 +08001304 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001305 * server or client connections.
1306 *
1307 * You need to call this service function periodically to all the above
1308 * functions to happen; if your application is single-threaded you can
1309 * just call it in your main event loop.
1310 *
1311 * Alternatively you can fork a new process that asynchronously handles
1312 * calling this service in a loop. In that case you are happy if this
1313 * call blocks your thread until it needs to take care of something and
1314 * would call it with a large nonzero timeout. Your loop then takes no
1315 * CPU while there is nothing happening.
1316 *
1317 * If you are calling it in a single-threaded app, you don't want it to
1318 * wait around blocking other things in your loop from happening, so you
1319 * would call it with a timeout_ms of 0, so it returns immediately if
1320 * nothing is pending, or as soon as it services whatever was pending.
1321 */
1322
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001323LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001324libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001325{
1326 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001327 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001328
1329 /* stay dead once we are dead */
1330
Peter Hinz56885f32011-03-02 22:03:47 +00001331 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001332 return 1;
1333
Andy Green0d338332011-02-12 11:57:43 +00001334 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001335
Peter Hinz56885f32011-03-02 22:03:47 +00001336 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green5dc62ea2013-09-20 20:26:12 +08001337 if (n == 0) /* poll timeout */ {
1338 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001339 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001340 }
Andy Greene92cd172011-01-19 13:11:55 +00001341
Andy Greendfb23042013-01-17 12:26:48 +08001342 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001343 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001344
Andy Greendfb23042013-01-17 12:26:48 +08001345 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001346
Andy Greenca0a1292013-03-16 11:24:23 +08001347 for (n = 0; n < context->fds_count; n++) {
1348 if (!context->fds[n].revents)
1349 continue;
1350 m = libwebsocket_service_fd(context, &context->fds[n]);
1351 if (m < 0)
1352 return -1;
1353 /* if something closed, retry this slot */
1354 if (m)
1355 n--;
1356 }
1357
Andy Greene92cd172011-01-19 13:11:55 +00001358 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001359}
1360
Andy Green3182ece2013-01-20 17:08:31 +08001361#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001362int
1363lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001364 struct libwebsocket *wsi,
1365 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001366 void *v, size_t len)
1367{
1368 int n;
1369 int handled = 0;
1370
1371 /* maybe an extension will take care of it for us */
1372
1373 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1374 if (!wsi->active_extensions[n]->callback)
1375 continue;
1376
1377 handled |= wsi->active_extensions[n]->callback(context,
1378 wsi->active_extensions[n], wsi,
1379 r, wsi->active_extensions_user[n], v, len);
1380 }
1381
1382 return handled;
1383}
1384
1385
1386void *
1387lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001388 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001389{
1390 int n = 0;
1391
Andy Green68b45042011-05-25 21:41:57 +01001392 if (wsi == NULL)
1393 return NULL;
1394
Andy Greena41314f2011-05-23 10:00:03 +01001395 while (n < wsi->count_active_extensions) {
1396 if (wsi->active_extensions[n] != ext) {
1397 n++;
1398 continue;
1399 }
1400 return wsi->active_extensions_user[n];
1401 }
1402
1403 return NULL;
1404}
Andy Green3182ece2013-01-20 17:08:31 +08001405#endif
Andy Greena41314f2011-05-23 10:00:03 +01001406
Andy Green91f19d82013-12-21 11:18:34 +08001407void
1408lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1409{
1410 struct libwebsocket_context *context = wsi->protocol->owning_server;
1411
1412 context->protocols[0].callback(context, wsi,
1413 LWS_CALLBACK_LOCK_POLL,
1414 wsi->user_space, (void *)(long)wsi->sock, 0);
1415
1416 context->fds[wsi->position_in_fds_table].events &= ~_and;
1417 context->fds[wsi->position_in_fds_table].events |= _or;
1418
1419 /* external POLL support via protocol 0 */
1420 if (_and)
1421 context->protocols[0].callback(context, wsi,
1422 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1423 wsi->user_space, (void *)(long)wsi->sock, _and);
1424
1425 if (_or)
1426 context->protocols[0].callback(context, wsi,
1427 LWS_CALLBACK_SET_MODE_POLL_FD,
1428 wsi->user_space, (void *)(long)wsi->sock, _or);
1429
1430 context->protocols[0].callback(context, wsi,
1431 LWS_CALLBACK_UNLOCK_POLL,
1432 wsi->user_space, (void *)(long)wsi->sock, 0);
1433}
1434
1435
Andy Green90c7cbc2011-01-27 06:26:52 +00001436/**
1437 * libwebsocket_callback_on_writable() - Request a callback when this socket
1438 * becomes able to be written to without
1439 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001440 *
Peter Hinz56885f32011-03-02 22:03:47 +00001441 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001442 * @wsi: Websocket connection instance to get callback for
1443 */
1444
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001445LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001446libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001447 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001448{
Andy Green3182ece2013-01-20 17:08:31 +08001449#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001450 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001451 int handled = 0;
1452
1453 /* maybe an extension will take care of it for us */
1454
1455 for (n = 0; n < wsi->count_active_extensions; n++) {
1456 if (!wsi->active_extensions[n]->callback)
1457 continue;
1458
1459 handled |= wsi->active_extensions[n]->callback(context,
1460 wsi->active_extensions[n], wsi,
1461 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1462 wsi->active_extensions_user[n], NULL, 0);
1463 }
1464
1465 if (handled)
1466 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001467#endif
Andy Greendfb23042013-01-17 12:26:48 +08001468 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001469 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1470 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001471 return -1;
1472 }
1473
Andy Green91f19d82013-12-21 11:18:34 +08001474 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001475
Andy Green90c7cbc2011-01-27 06:26:52 +00001476 return 1;
1477}
1478
1479/**
1480 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1481 * all connections using the given protocol when it
1482 * becomes possible to write to each socket without
1483 * blocking in turn.
1484 *
1485 * @protocol: Protocol whose connections will get callbacks
1486 */
1487
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001488LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001489libwebsocket_callback_on_writable_all_protocol(
1490 const struct libwebsocket_protocols *protocol)
1491{
Peter Hinz56885f32011-03-02 22:03:47 +00001492 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001493 int n;
Andy Green0d338332011-02-12 11:57:43 +00001494 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001495
Andy Greendfb23042013-01-17 12:26:48 +08001496 for (n = 0; n < context->fds_count; n++) {
1497 wsi = context->lws_lookup[context->fds[n].fd];
1498 if (!wsi)
1499 continue;
1500 if (wsi->protocol == protocol)
1501 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001502 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001503
1504 return 0;
1505}
1506
Andy Greenbe93fef2011-02-14 20:25:43 +00001507/**
1508 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1509 *
1510 * You will not need this unless you are doing something special
1511 *
1512 * @wsi: Websocket connection instance
1513 * @reason: timeout reason
1514 * @secs: how many seconds
1515 */
1516
1517void
1518libwebsocket_set_timeout(struct libwebsocket *wsi,
1519 enum pending_timeout reason, int secs)
1520{
1521 struct timeval tv;
1522
1523 gettimeofday(&tv, NULL);
1524
1525 wsi->pending_timeout_limit = tv.tv_sec + secs;
1526 wsi->pending_timeout = reason;
1527}
1528
Andy Greena6cbece2011-01-27 20:06:03 +00001529
1530/**
1531 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1532 *
1533 * You will not need this unless you are doing something special
1534 *
1535 * @wsi: Websocket connection instance
1536 */
1537
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001538LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001539libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1540{
1541 return wsi->sock;
1542}
1543
Andy Greend636e352013-01-29 12:36:17 +08001544#ifdef LWS_LATENCY
1545void
Andy Greenb5b23192013-02-11 17:13:32 +08001546lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1547 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001548{
1549 struct timeval tv;
1550 unsigned long u;
1551 char buf[256];
1552
1553 gettimeofday(&tv, NULL);
1554
1555 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1556
1557 if (action) {
1558 if (completed) {
1559 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001560 sprintf(buf,
1561 "Completion first try lat %luus: %p: ret %d: %s\n",
1562 u - wsi->latency_start,
1563 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001564 else
Andy Greenb5b23192013-02-11 17:13:32 +08001565 sprintf(buf,
1566 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1567 u - wsi->action_start,
1568 u - wsi->latency_start,
1569 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001570 wsi->action_start = 0;
1571 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001572 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1573 u - wsi->latency_start,
1574 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001575 if (u - wsi->latency_start > context->worst_latency) {
1576 context->worst_latency = u - wsi->latency_start;
1577 strcpy(context->worst_latency_info, buf);
1578 }
1579 lwsl_latency("%s", buf);
1580 } else {
1581 wsi->latency_start = u;
1582 if (!wsi->action_start)
1583 wsi->action_start = u;
1584 }
1585}
1586#endif
1587
Andy Greena1ce6be2013-01-18 11:43:21 +08001588#ifdef LWS_NO_SERVER
1589int
Andy Green8d15cf42013-10-24 21:47:06 +08001590_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001591{
1592 return 0;
1593}
1594#else
Andy Green706961d2013-01-17 16:50:35 +08001595int
1596_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1597{
1598 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001599
Andy Greenca0a1292013-03-16 11:24:23 +08001600 /* there is no pending change */
1601 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001602 return 0;
1603
Andy Greenca0a1292013-03-16 11:24:23 +08001604 /* stuff is still buffered, not ready to really accept new input */
1605 if (wsi->u.ws.rxflow_buffer) {
1606 /* get ourselves called back to deal with stashed buffer */
1607 libwebsocket_callback_on_writable(context, wsi);
1608 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001609 }
1610
Andy Greenca0a1292013-03-16 11:24:23 +08001611 /* pending is cleared, we can change rxflow state */
1612
1613 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1614
1615 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1616 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1617
1618 /* adjust the pollfd for this wsi */
1619
1620 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001621 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001622 else
Andy Green91f19d82013-12-21 11:18:34 +08001623 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001624
Andy Green706961d2013-01-17 16:50:35 +08001625 return 1;
1626}
Andy Greena1ce6be2013-01-18 11:43:21 +08001627#endif
Andy Green706961d2013-01-17 16:50:35 +08001628
Andy Green90c7cbc2011-01-27 06:26:52 +00001629/**
1630 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1631 * receieved packets.
1632 *
1633 * If the output side of a server process becomes choked, this allows flow
1634 * control for the input side.
1635 *
1636 * @wsi: Websocket connection instance to get callback for
1637 * @enable: 0 = disable read servicing for this connection, 1 = enable
1638 */
1639
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001640LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001641libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1642{
Andy Greenca0a1292013-03-16 11:24:23 +08001643 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1644 return 0;
1645
1646 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1647 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001648
Andy Green706961d2013-01-17 16:50:35 +08001649 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001650}
1651
Andy Greenb55451c2013-03-16 12:32:27 +08001652/**
1653 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1654 *
1655 * When the user server code realizes it can accept more input, it can
1656 * call this to have the RX flow restriction removed from all connections using
1657 * the given protocol.
1658 *
1659 * @protocol: all connections using this protocol will be allowed to receive
1660 */
1661
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001662LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001663libwebsocket_rx_flow_allow_all_protocol(
1664 const struct libwebsocket_protocols *protocol)
1665{
1666 struct libwebsocket_context *context = protocol->owning_server;
1667 int n;
1668 struct libwebsocket *wsi;
1669
1670 for (n = 0; n < context->fds_count; n++) {
1671 wsi = context->lws_lookup[context->fds[n].fd];
1672 if (!wsi)
1673 continue;
1674 if (wsi->protocol == protocol)
1675 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1676 }
1677}
1678
Andy Green706961d2013-01-17 16:50:35 +08001679
Andy Green2ac5a6f2011-01-28 10:00:18 +00001680/**
1681 * libwebsocket_canonical_hostname() - returns this host's hostname
1682 *
1683 * This is typically used by client code to fill in the host parameter
1684 * when making a client connection. You can only call it after the context
1685 * has been created.
1686 *
Peter Hinz56885f32011-03-02 22:03:47 +00001687 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001688 */
1689
1690
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001691LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001692libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001693{
Peter Hinz56885f32011-03-02 22:03:47 +00001694 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001695}
1696
1697
Andy Green90c7cbc2011-01-27 06:26:52 +00001698static void sigpipe_handler(int x)
1699{
1700}
1701
Andy Green6901cb32011-02-21 08:06:47 +00001702#ifdef LWS_OPENSSL_SUPPORT
1703static int
1704OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1705{
1706
1707 SSL *ssl;
1708 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001709 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001710
1711 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1712 SSL_get_ex_data_X509_STORE_CTX_idx());
1713
1714 /*
Andy Green2e24da02011-03-05 16:12:04 +00001715 * !!! nasty openssl requires the index to come as a library-scope
1716 * static
Andy Green6901cb32011-02-21 08:06:47 +00001717 */
Andy Green2e24da02011-03-05 16:12:04 +00001718 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001719
Peter Hinz56885f32011-03-02 22:03:47 +00001720 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001721 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1722 x509_ctx, ssl, preverify_ok);
1723
1724 /* convert return code from 0 = OK to 1 = OK */
1725
1726 if (!n)
1727 n = 1;
1728 else
1729 n = 0;
1730
1731 return n;
1732}
1733#endif
1734
Andy Green706961d2013-01-17 16:50:35 +08001735int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001736 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001737 struct libwebsocket *wsi,
1738 enum libwebsocket_callback_reasons reason, void *user,
1739 void *in, size_t len)
1740{
1741 int n;
1742
1743 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001744 if (!n)
1745 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001746
Andy Greenaedc9532013-02-10 21:21:24 +08001747 return n;
Andy Green706961d2013-01-17 16:50:35 +08001748}
1749
Andy Greenb45993c2010-12-18 15:13:50 +00001750
Andy Greenab990e42010-10-31 12:42:52 +00001751/**
Andy Green4739e5c2011-01-22 12:51:57 +00001752 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001753 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001754 *
Andy Green1b265272013-02-09 14:01:09 +08001755 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001756 * of all initialization in one step.
1757 *
Andy Greene92cd172011-01-19 13:11:55 +00001758 * After initialization, it returns a struct libwebsocket_context * that
1759 * represents this server. After calling, user code needs to take care
1760 * of calling libwebsocket_service() with the context pointer to get the
1761 * server's sockets serviced. This can be done in the same process context
1762 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001763 *
Andy Green8f037e42010-12-19 22:13:26 +00001764 * The protocol callback functions are called for a handful of events
1765 * including http requests coming in, websocket connections becoming
1766 * established, and data arriving; it's also called periodically to allow
1767 * async transmission.
1768 *
1769 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1770 * at that time websocket protocol has not been negotiated. Other
1771 * protocols after the first one never see any HTTP callack activity.
1772 *
1773 * The server created is a simple http server by default; part of the
1774 * websocket standard is upgrading this http connection to a websocket one.
1775 *
1776 * This allows the same server to provide files like scripts and favicon /
1777 * images or whatever over http and dynamic data over websockets all in
1778 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001779 */
Andy Green4ea60062010-10-30 12:15:07 +01001780
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001781LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001782libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001783{
Peter Hinz56885f32011-03-02 22:03:47 +00001784 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001785 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001786 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001787#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001788 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001789 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001790 struct sockaddr_in serv_addr;
1791#endif
Andy Green3182ece2013-01-20 17:08:31 +08001792#ifndef LWS_NO_EXTENSIONS
1793 int m;
Andy Green1b265272013-02-09 14:01:09 +08001794 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001795#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001796
Andy Green3faa9c72010-11-08 17:03:03 +00001797#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001798 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001799#endif
1800
Joakim Soderberg4c531232013-02-06 15:26:58 +09001801#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001802 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001803#endif
1804
Andy Greenb3a614a2013-01-19 13:08:17 +08001805 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001806 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001807 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001808 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001809#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001810 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1811 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001812#else
1813 lwsl_notice(" Configured without extension support\n");
1814#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001815 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1816 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001817 if (info->ssl_cipher_list)
1818 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001819 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1820 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001821
Peter Hinz56885f32011-03-02 22:03:47 +00001822#ifdef _WIN32
1823 {
1824 WORD wVersionRequested;
1825 WSADATA wsaData;
1826 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001827 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001828
1829 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1830 wVersionRequested = MAKEWORD(2, 2);
1831
1832 err = WSAStartup(wVersionRequested, &wsaData);
1833 if (err != 0) {
1834 /* Tell the user that we could not find a usable */
1835 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001836 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001837 return NULL;
1838 }
David Galeano7b11fec2011-10-04 19:55:18 +08001839
Andy Green6ee372f2012-04-09 15:09:01 +08001840 /* default to a poll() made out of select() */
1841 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001842
Andy Green6ee372f2012-04-09 15:09:01 +08001843 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001844 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001845 if (wsdll)
1846 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001847
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001848 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001849 if (!poll)
1850 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001851 }
1852#endif
1853
Andy Greenb5b23192013-02-11 17:13:32 +08001854 context = (struct libwebsocket_context *)
1855 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001856 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001857 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001858 return NULL;
1859 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001860 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001861#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001862 context->started_with_parent = pid_daemon;
1863 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1864#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001865
Joakim Soderberg4c531232013-02-06 15:26:58 +09001866 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001867 context->protocols = info->protocols;
1868 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001869 context->http_proxy_port = 0;
1870 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001871 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001872 /* to reduce this allocation, */
1873 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001874 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1875 sizeof(struct libwebsocket_context),
1876 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1877 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001878 sizeof(struct libwebsocket_context) +
1879 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1880 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001881
Andy Greenb5b23192013-02-11 17:13:32 +08001882 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1883 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001884 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001885 lwsl_err("Unable to allocate fds array for %d connections\n",
1886 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001887 free(context);
1888 return NULL;
1889 }
Andy Greenb5b23192013-02-11 17:13:32 +08001890 context->lws_lookup = (struct libwebsocket **)
1891 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001892 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001893 lwsl_err(
1894 "Unable to allocate lws_lookup array for %d connections\n",
1895 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001896 free(context->fds);
1897 free(context);
1898 return NULL;
1899 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001900 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1901 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001902
Peter Hinz56885f32011-03-02 22:03:47 +00001903 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001904#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001905 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001906#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001907 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001908 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001909
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001910#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001911 context->fd_random = 0;
1912#else
1913 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1914 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001915 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001916 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001917 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001918 }
Peter Hinz56885f32011-03-02 22:03:47 +00001919#endif
Andy Green44eee682011-02-10 09:32:24 +00001920
Peter Hinz56885f32011-03-02 22:03:47 +00001921#ifdef LWS_OPENSSL_SUPPORT
1922 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08001923 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001924 context->ssl_ctx = NULL;
1925 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001926 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001927#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001928
Andy Greena1ce6be2013-01-18 11:43:21 +08001929 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001930
Andy Greena1ce6be2013-01-18 11:43:21 +08001931#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001932 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001933 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001934 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001935 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001936
Andy Greenb5b23192013-02-11 17:13:32 +08001937 lwsl_notice(" canonical_hostname = %s\n",
1938 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001939 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001940#endif
Andy Greena69f0512012-05-03 12:32:38 +08001941
Andy Green9659f372011-01-27 22:01:43 +00001942 /* split the proxy ads:port if given */
1943
1944 p = getenv("http_proxy");
1945 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001946 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001947 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001948 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001949 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001950
Peter Hinz56885f32011-03-02 22:03:47 +00001951 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001952 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001953 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001954 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001955 }
1956 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001957 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001958
Andy Greenb3a614a2013-01-19 13:08:17 +08001959 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001960 context->http_proxy_address,
1961 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001962 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001963
Andy Greena1ce6be2013-01-18 11:43:21 +08001964#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001965 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001966
Andy Green3faa9c72010-11-08 17:03:03 +00001967#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001968 context->use_ssl = info->ssl_cert_filepath != NULL &&
1969 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001970#ifdef USE_CYASSL
1971 lwsl_notice(" Compiled with CYASSL support\n");
1972#else
1973 lwsl_notice(" Compiled with OpenSSL support\n");
1974#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001975 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001976 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001977 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001978 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001979
Andy Green90c7cbc2011-01-27 06:26:52 +00001980#else
Andy Green2b40b792013-02-10 22:22:01 +08001981 if (info->ssl_cert_filepath != NULL &&
1982 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001983 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001984 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001985 }
Andy Greenb5b23192013-02-11 17:13:32 +08001986 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001987#endif
Andy Greena17c6922013-01-20 20:21:54 +08001988
Andy Greenb5b23192013-02-11 17:13:32 +08001989 lwsl_notice(
1990 " per-conn mem: %u + %u headers + protocol rx buf\n",
1991 sizeof(struct libwebsocket),
1992 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001993 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001994#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001995
1996 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001997#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001998#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001999 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002000#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002001
2002
2003#ifdef LWS_OPENSSL_SUPPORT
2004
2005 /* basic openssl init */
2006
2007 SSL_library_init();
2008
2009 OpenSSL_add_all_algorithms();
2010 SSL_load_error_strings();
2011
Andy Green2e24da02011-03-05 16:12:04 +00002012 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002013 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2014
Andy Green90c7cbc2011-01-27 06:26:52 +00002015 /*
2016 * Firefox insists on SSLv23 not SSLv3
2017 * Konq disables SSLv2 by default now, SSLv23 works
2018 */
2019
2020 method = (SSL_METHOD *)SSLv23_server_method();
2021 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002022 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002023 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002024 error,
2025 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002026 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002027 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002028 }
Peter Hinz56885f32011-03-02 22:03:47 +00002029 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2030 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002031 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002032 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002033 error,
2034 ERR_error_string(error,
2035 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002036 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002037 }
2038
David Galeanocc148e42013-01-10 10:18:59 +08002039#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002040 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002041#endif
David Galeano77a677c2013-01-10 10:14:12 +08002042 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002043 if (info->ssl_cipher_list)
2044 SSL_CTX_set_cipher_list(context->ssl_ctx,
2045 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002046
Andy Greena1ce6be2013-01-18 11:43:21 +08002047#ifndef LWS_NO_CLIENT
2048
Andy Green90c7cbc2011-01-27 06:26:52 +00002049 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002050
Andy Green1b265272013-02-09 14:01:09 +08002051 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002052 method = (SSL_METHOD *)SSLv23_client_method();
2053 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002054 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002055 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002056 error,
2057 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002058 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002059 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002060 }
2061 /* create context */
2062 context->ssl_client_ctx = SSL_CTX_new(method);
2063 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002064 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002065 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002066 error,
2067 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002068 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002069 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002070 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002071
David Galeanocc148e42013-01-10 10:18:59 +08002072#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002073 SSL_CTX_set_options(context->ssl_client_ctx,
2074 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002075#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002076 SSL_CTX_set_options(context->ssl_client_ctx,
2077 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002078 if (info->ssl_cipher_list)
2079 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2080 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002081
Peter Hinz56885f32011-03-02 22:03:47 +00002082 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002083 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002084 if (!SSL_CTX_load_verify_locations(
2085 context->ssl_client_ctx, NULL,
2086 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002087 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002088 "Unable to load SSL Client certs from %s "
2089 "(set by --with-client-cert-dir= "
2090 "in configure) -- client ssl isn't "
2091 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002092 } else
2093 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002094 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002095 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002096 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002097 "Unable to load SSL Client certs "
2098 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002099 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002100
2101 /*
2102 * callback allowing user code to load extra verification certs
2103 * helping the client to verify server identity
2104 */
2105
prasannateamf1b353a452013-11-12 17:21:01 -08002106 /* support for client-side certificate authentication */
2107 if (info->ssl_cert_filepath) {
2108 n = SSL_CTX_use_certificate_chain_file(
2109 context->ssl_client_ctx,
2110 info->ssl_cert_filepath);
2111 if (n != 1) {
2112 lwsl_err("problem getting cert '%s' %lu: %s\n",
2113 info->ssl_cert_filepath,
2114 ERR_get_error(),
2115 ERR_error_string(ERR_get_error(),
2116 (char *)context->service_buffer));
2117 goto bail;
2118 }
2119 }
2120 if (info->ssl_private_key_filepath) {
2121 /* set the private key from KeyFile */
2122 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2123 info->ssl_private_key_filepath,
2124 SSL_FILETYPE_PEM) != 1) {
2125 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2126 info->ssl_private_key_filepath,
2127 ERR_get_error(),
2128 ERR_error_string(ERR_get_error(),
2129 (char *)context->service_buffer));
2130 goto bail;
2131 }
2132
2133 /* verify private key */
2134 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2135 lwsl_err("Private SSL key doesn't match cert\n");
2136 goto bail;
2137 }
2138 }
2139
Peter Hinz56885f32011-03-02 22:03:47 +00002140 context->protocols[0].callback(context, NULL,
2141 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2142 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002143 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002144#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002145
Andy Greenc6bf2c22011-02-20 11:10:47 +00002146 /* as a server, are we requiring clients to identify themselves? */
2147
Andy Greenb5b23192013-02-11 17:13:32 +08002148 if (info->options &
2149 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002150
2151 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002152
Peter Hinz56885f32011-03-02 22:03:47 +00002153 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002154 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2155 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002156
2157 /*
2158 * give user code a chance to load certs into the server
2159 * allowing it to verify incoming client certs
2160 */
2161
Peter Hinz56885f32011-03-02 22:03:47 +00002162 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002163 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002164 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002165 }
2166
James Devine5b34c972013-12-14 11:41:29 +08002167 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2168 /* Normally SSL listener rejects non-ssl, optionally allow */
2169 context->allow_non_ssl_on_ssl_port = 1;
2170 }
2171
Peter Hinz56885f32011-03-02 22:03:47 +00002172 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002173
2174 /* openssl init for server sockets */
2175
Andy Green3faa9c72010-11-08 17:03:03 +00002176 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002177 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002178 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002179 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002180 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002181 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002182 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002183 error,
2184 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002185 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002186 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002187 }
2188 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002189 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002190 info->ssl_private_key_filepath,
2191 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002192 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002193 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002194 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002195 error,
2196 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002197 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002198 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002199 }
2200 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002201 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002202 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002203 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002204 }
2205
2206 /* SSL is happy and has a cert it's content with */
2207 }
2208#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002209
Andy Greena1ce6be2013-01-18 11:43:21 +08002210#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002211 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002212
Andy Green1b265272013-02-09 14:01:09 +08002213 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002214 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002215
Andy Green4739e5c2011-01-22 12:51:57 +00002216 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2217 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002218 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002219 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002220 }
Andy Green775c0dd2010-10-29 14:15:22 +01002221
Joakim Soderberg4c531232013-02-06 15:26:58 +09002222#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002223 /*
2224 * allow us to restart even if old sockets in TIME_WAIT
2225 * (REUSEADDR on Unix means, "don't hang on to this
2226 * address after the listener is closed." On Windows, though,
2227 * it means "don't keep other processes from binding to
2228 * this address while we're using it)
2229 */
Andy Green6ee372f2012-04-09 15:09:01 +08002230 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2231 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002232#endif
Andy Green6c939552011-03-08 08:56:57 +00002233
2234 /* Disable Nagle */
2235 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002236 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2237 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002238
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002239 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002240 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002241 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002242 #else
Andy Greene2160712013-01-28 12:19:10 +08002243 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002244 #endif
Andy Greene2160712013-01-28 12:19:10 +08002245
Andy Green4739e5c2011-01-22 12:51:57 +00002246 bzero((char *) &serv_addr, sizeof(serv_addr));
2247 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002248 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002249 serv_addr.sin_addr.s_addr = INADDR_ANY;
2250 else
Andy Greend1eac602013-11-09 08:07:38 +08002251 if (interface_to_sa(info->iface, &serv_addr,
2252 sizeof(serv_addr)) < 0) {
2253 lwsl_err("Unable to find interface %s\n",
2254 info->iface);
2255 compatible_close(sockfd);
2256 goto bail;
2257 }
Andy Green1b265272013-02-09 14:01:09 +08002258 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002259
2260 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2261 sizeof(serv_addr));
2262 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002263 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002264 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002265 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002266 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002267 }
Andy Green0d338332011-02-12 11:57:43 +00002268
Andy Greenb5b23192013-02-11 17:13:32 +08002269 wsi = (struct libwebsocket *)malloc(
2270 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002271 if (wsi == NULL) {
2272 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002273 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002274 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002275 }
Andy Greenb5b23192013-02-11 17:13:32 +08002276 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002277 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002278#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002279 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002280#endif
Andy Green0d338332011-02-12 11:57:43 +00002281 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002282
2283 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002284
Andy Green65b0e912013-01-16 07:59:47 +08002285 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2286 context->listen_service_count = 0;
2287 context->listen_service_fd = sockfd;
2288
Andy Greena824d182013-01-15 20:52:29 +08002289 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002290 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002291 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002292#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002293
Andy Green6ee372f2012-04-09 15:09:01 +08002294 /*
2295 * drop any root privs for this process
2296 * to listen on port < 1023 we would have needed root, but now we are
2297 * listening, we don't want the power for anything else
2298 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002299#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002300#else
Andy Green1b265272013-02-09 14:01:09 +08002301 if (info->gid != -1)
2302 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002303 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002304 if (info->uid != -1)
2305 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002306 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002307#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002308
Andy Green6f520a52013-01-29 17:57:39 +08002309 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002310
Peter Hinz56885f32011-03-02 22:03:47 +00002311 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002312 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002313 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002314
Andy Green43db0452013-01-10 19:50:35 +08002315 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002316 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002317
Andy Green1b265272013-02-09 14:01:09 +08002318 info->protocols[context->count_protocols].owning_server =
2319 context;
2320 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002321 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002322
2323 /*
2324 * inform all the protocols that they are doing their one-time
2325 * initialization if they want to
2326 */
2327 info->protocols[context->count_protocols].callback(context,
2328 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002329 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002330
Andy Green3182ece2013-01-20 17:08:31 +08002331#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002332 /*
2333 * give all extensions a chance to create any per-context
2334 * allocations they need
2335 */
2336
2337 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002338 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002339 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002340
Andy Green1b265272013-02-09 14:01:09 +08002341 if (info->extensions) {
2342 ext = info->extensions;
2343 while (ext->callback) {
2344 lwsl_ext(" Extension: %s\n", ext->name);
2345 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002346 (enum libwebsocket_extension_callback_reasons)m,
2347 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002348 ext++;
2349 }
Andy Greena41314f2011-05-23 10:00:03 +01002350 }
Andy Green3182ece2013-01-20 17:08:31 +08002351#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002352 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002353
2354bail:
2355 libwebsocket_context_destroy(context);
2356 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002357}
Andy Greenb45993c2010-12-18 15:13:50 +00002358
Andy Greenb45993c2010-12-18 15:13:50 +00002359/**
shysb4e800e2013-10-24 22:12:03 +08002360 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2361 * @context: pointer to struct libwebsocket_context you want set proxy to
2362 * @proxy: pointer to c string containing proxy in format address:port
2363 *
2364 * Returns 0 if proxy string was parsed and proxy was setup.
2365 * Returns -1 if @proxy is NULL or has incorrect format.
2366 *
2367 * This is only required if your OS does not provide the http_proxy
2368 * enviroment variable (eg, OSX)
2369 *
2370 * IMPORTANT! You should call this function right after creation of the
2371 * libwebsocket_context and before call to connect. If you call this
2372 * function after connect behavior is undefined.
2373 * This function will override proxy settings made on libwebsocket_context
2374 * creation with genenv() call.
2375 */
2376
2377LWS_VISIBLE int
2378libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2379{
2380 char *p;
2381
2382 if (!proxy)
2383 return -1;
2384
2385 strncpy(context->http_proxy_address, proxy,
2386 sizeof(context->http_proxy_address) - 1);
2387 context->http_proxy_address[
2388 sizeof(context->http_proxy_address) - 1] = '\0';
2389
2390 p = strchr(context->http_proxy_address, ':');
2391 if (!p) {
2392 lwsl_err("http_proxy needs to be ads:port\n");
2393
2394 return -1;
2395 }
2396 *p = '\0';
2397 context->http_proxy_port = atoi(p + 1);
2398
2399 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2400 context->http_proxy_port);
2401
2402 return 0;
2403}
2404
2405/**
Andy Greenb45993c2010-12-18 15:13:50 +00002406 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002407 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002408 * @wsi: pointer to struct websocket you want to know the protocol of
2409 *
Andy Green8f037e42010-12-19 22:13:26 +00002410 *
Andy Green6f520a52013-01-29 17:57:39 +08002411 * Some apis can act on all live connections of a given protocol,
2412 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002413 */
Andy Greenab990e42010-10-31 12:42:52 +00002414
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002415LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002416libwebsockets_get_protocol(struct libwebsocket *wsi)
2417{
2418 return wsi->protocol;
2419}
2420
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002421LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002422libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2423{
Andy Green623a98d2013-01-21 11:04:23 +08002424 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002425}
Alex Bligh49146db2011-11-07 17:19:25 +08002426
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002427LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002428libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2429{
Andy Green623a98d2013-01-21 11:04:23 +08002430 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002431}
2432
Andy Green2af4d5b2013-02-18 16:30:10 +08002433int
Alex Bligh49146db2011-11-07 17:19:25 +08002434libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2435{
Andy Green67d556c2013-02-15 22:31:55 +08002436 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002437 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002438
Alex Bligh49146db2011-11-07 17:19:25 +08002439 /* allocate the per-connection user memory (if any) */
2440
2441 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2442 wsi->user_space = malloc(
2443 wsi->protocol->per_session_data_size);
2444 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002445 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002446 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002447 }
Andy Green6ee372f2012-04-09 15:09:01 +08002448 memset(wsi->user_space, 0,
2449 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002450 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002451 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002452}
Andy Green43db0452013-01-10 19:50:35 +08002453
Andy Green0b319092013-01-19 11:17:56 +08002454static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002455{
Andy Green0b319092013-01-19 11:17:56 +08002456 char buf[300];
2457 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002458 int n;
2459
2460 gettimeofday(&tv, NULL);
2461
2462 buf[0] = '\0';
2463 for (n = 0; n < LLL_COUNT; n++)
2464 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002465 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002466 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002467 break;
2468 }
Andy Greenb5b23192013-02-11 17:13:32 +08002469
Andy Green0b319092013-01-19 11:17:56 +08002470 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002471}
2472
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002473#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002474LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002475{
2476 lwsl_emit_stderr(level, line);
2477}
2478#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002479LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002480{
2481 int syslog_level = LOG_DEBUG;
2482
2483 switch (level) {
2484 case LLL_ERR:
2485 syslog_level = LOG_ERR;
2486 break;
2487 case LLL_WARN:
2488 syslog_level = LOG_WARNING;
2489 break;
2490 case LLL_NOTICE:
2491 syslog_level = LOG_NOTICE;
2492 break;
2493 case LLL_INFO:
2494 syslog_level = LOG_INFO;
2495 break;
2496 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002497 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002498}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002499#endif
Andy Greenc11db202013-01-19 11:12:16 +08002500
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002501LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002502{
Andy Greende8f27a2013-01-12 09:17:42 +08002503 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002504 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002505
2506 if (!(log_level & filter))
2507 return;
2508
Andy Green43db0452013-01-10 19:50:35 +08002509 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002510 vsnprintf(buf, sizeof(buf), format, ap);
2511 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002512 va_end(ap);
2513
Andy Green0b319092013-01-19 11:17:56 +08002514 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002515}
2516
2517/**
2518 * lws_set_log_level() - Set the logging bitfield
2519 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002520 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2521 * function to perform log string emission instead of
2522 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002523 *
Andy Greenc6511a02013-02-19 10:01:48 +08002524 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002525 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002526 */
2527
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002528LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002529 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002530{
2531 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002532 if (log_emit_function)
2533 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002534}