blob: 556257310fbf93cc2dc7fb5edc74e7e470bc5936 [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 Greena41314f2011-05-23 10:00:03 +0100811 if (pollfd) {
Andy Green7a132792013-12-18 09:48:26 +0800812
813 context->protocols[0].callback(context, wsi,
814 LWS_CALLBACK_LOCK_POLL,
815 wsi->user_space, (void *)(long)wsi->sock, 0);
816
Andy Greena41314f2011-05-23 10:00:03 +0100817 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000818
Andy Greena41314f2011-05-23 10:00:03 +0100819 /* external POLL support via protocol 0 */
820 context->protocols[0].callback(context, wsi,
821 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800822 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +0800823
824 context->protocols[0].callback(context, wsi,
825 LWS_CALLBACK_UNLOCK_POLL,
826 wsi->user_space, (void *)(long)wsi->sock, 0);
Andy Greena41314f2011-05-23 10:00:03 +0100827 }
Andy Green3182ece2013-01-20 17:08:31 +0800828#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100829notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800830#endif
Andy Green3b84c002011-03-06 13:14:42 +0000831
Andy Green9e4c2b62011-03-07 20:47:39 +0000832 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
833 n = LWS_CALLBACK_CLIENT_WRITEABLE;
834 else
835 n = LWS_CALLBACK_SERVER_WRITEABLE;
836
Andy Greenb8b247d2013-01-22 07:20:08 +0800837 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800838 wsi, (enum libwebsocket_callback_reasons) n,
839 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000840}
841
842
843
Andy Green1c6e1422013-02-20 19:11:31 +0800844int
Andy Greena41314f2011-05-23 10:00:03 +0100845libwebsocket_service_timeout_check(struct libwebsocket_context *context,
846 struct libwebsocket *wsi, unsigned int sec)
847{
Andy Green3182ece2013-01-20 17:08:31 +0800848#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100849 int n;
850
851 /*
852 * if extensions want in on it (eg, we are a mux parent)
853 * give them a chance to service child timeouts
854 */
855
856 for (n = 0; n < wsi->count_active_extensions; n++)
857 wsi->active_extensions[n]->callback(
858 context, wsi->active_extensions[n],
859 wsi, LWS_EXT_CALLBACK_1HZ,
860 wsi->active_extensions_user[n], NULL, sec);
861
Andy Green3182ece2013-01-20 17:08:31 +0800862#endif
Andy Greena41314f2011-05-23 10:00:03 +0100863 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800864 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800865
Andy Greena41314f2011-05-23 10:00:03 +0100866 /*
867 * if we went beyond the allowed time, kill the
868 * connection
869 */
870
871 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800872 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100873 libwebsocket_close_and_free_session(context,
874 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800875 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100876 }
Andy Green1c6e1422013-02-20 19:11:31 +0800877
878 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100879}
880
Andy Green9f990342011-02-12 11:57:45 +0000881/**
882 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000883 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000884 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800885 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000886 *
Andy Green75006172013-01-22 12:32:11 +0800887 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800888 * services it according to the state of the associated
889 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800890 *
891 * The one call deals with all "service" that might happen on a socket
892 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800893 *
894 * If a pollfd says it has something, you can just pass it to
895 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
896 * If it sees it is a lws socket, the traffic will be handled and
897 * pollfd->revents will be zeroed now.
898 *
899 * If the socket is foreign to lws, it leaves revents alone. So you can
900 * see if you should service yourself by checking the pollfd revents
901 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000902 */
903
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800904LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000905libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000906 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000907{
Andy Greena1ce6be2013-01-18 11:43:21 +0800908 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000909 int n;
Andy Green0d338332011-02-12 11:57:43 +0000910 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800911 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000912 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800913 int timed_out = 0;
914 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800915 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800916
Andy Green3182ece2013-01-20 17:08:31 +0800917#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000918 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800919#endif
Andy Green98a717c2011-03-06 13:14:15 +0000920 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800921
922 if (context->listen_service_fd)
923 listen_socket_fds_index = context->lws_lookup[
924 context->listen_service_fd]->position_in_fds_table;
925
Andy Greena71eafc2011-02-14 17:59:43 +0000926 /*
927 * you can call us with pollfd = NULL to just allow the once-per-second
928 * global timeout checks; if less than a second since the last check
929 * it returns immediately then.
930 */
931
932 gettimeofday(&tv, NULL);
933
Peter Hinz56885f32011-03-02 22:03:47 +0000934 if (context->last_timeout_check_s != tv.tv_sec) {
935 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000936
Joakim Soderberg4c531232013-02-06 15:26:58 +0900937 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800938 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800939 if (context->started_with_parent &&
940 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800941 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900942 #endif
Andy Green24cba922013-01-19 13:56:10 +0800943
Andy Greena71eafc2011-02-14 17:59:43 +0000944 /* global timeout check once per second */
945
Andy Green1c6e1422013-02-20 19:11:31 +0800946 if (pollfd)
947 our_fd = pollfd->fd;
948
Peter Hinz56885f32011-03-02 22:03:47 +0000949 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800950 m = context->fds[n].fd;
951 wsi = context->lws_lookup[m];
952 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800953 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800954
955 if (libwebsocket_service_timeout_check(context, wsi,
956 tv.tv_sec))
957 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800958 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800959 /* it was the guy we came to service! */
960 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800961 /* mark as handled */
962 pollfd->revents = 0;
963 }
Andy Greena71eafc2011-02-14 17:59:43 +0000964 }
965 }
966
Andy Green1c6e1422013-02-20 19:11:31 +0800967 /* the socket we came to service timed out, nothing to do */
968 if (timed_out)
969 return 0;
970
Andy Greena71eafc2011-02-14 17:59:43 +0000971 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000972 if (pollfd == NULL)
973 return 0;
974
975 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800976 wsi = context->lws_lookup[pollfd->fd];
977 if (wsi == NULL)
978 /* not lws connection ... leave revents alone and return */
979 return 0;
980
981 /*
982 * so that caller can tell we handled, past here we need to
983 * zero down pollfd->revents after handling
984 */
985
Andy Green65b0e912013-01-16 07:59:47 +0800986 /*
987 * deal with listen service piggybacking
988 * every listen_service_modulo services of other fds, we
989 * sneak one in to service the listen socket if there's anything waiting
990 *
991 * To handle connection storms, as found in ab, if we previously saw a
992 * pending connection here, it causes us to check again next time.
993 */
994
Andy Greenb5b23192013-02-11 17:13:32 +0800995 if (context->listen_service_fd && pollfd !=
996 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800997 context->listen_service_count++;
998 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800999 context->listen_service_count ==
1000 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001001 context->listen_service_count = 0;
1002 m = 1;
1003 if (context->listen_service_extraseen > 5)
1004 m = 2;
1005 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001006 /*
1007 * even with extpoll, we prepared this
1008 * internal fds for listen
1009 */
1010 n = poll(&context->fds[listen_socket_fds_index],
1011 1, 0);
1012 if (n > 0) { /* there's a conn waiting for us */
1013 libwebsocket_service_fd(context,
1014 &context->
1015 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001016 context->listen_service_extraseen++;
1017 } else {
1018 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001019 context->
1020 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001021 break;
1022 }
1023 }
1024 }
1025
1026 }
1027
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001028 /* handle session socket closed */
1029
1030 if ((!(pollfd->revents & POLLIN)) &&
1031 (pollfd->revents & (POLLERR | POLLHUP))) {
1032
1033 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1034 (void *)wsi, pollfd->fd);
1035
1036 goto close_and_handled;
1037 }
1038
Andy Green65b0e912013-01-16 07:59:47 +08001039 /* okay, what we came here to do... */
1040
Andy Green0d338332011-02-12 11:57:43 +00001041 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001042
Andy Greena1ce6be2013-01-18 11:43:21 +08001043#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001044 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001045 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001046 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001047 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001048 n = lws_server_socket_service(context, wsi, pollfd);
1049 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001050#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001051
Andy Green0d338332011-02-12 11:57:43 +00001052 case LWS_CONNMODE_WS_SERVING:
1053 case LWS_CONNMODE_WS_CLIENT:
1054
Andy Green0d338332011-02-12 11:57:43 +00001055 /* the guy requested a callback when it was OK to write */
1056
Andy Greenda527df2011-03-07 07:08:12 +00001057 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001058 wsi->state == WSI_STATE_ESTABLISHED &&
1059 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +08001060 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001061 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001062 }
Andy Green0d338332011-02-12 11:57:43 +00001063
Andy Greenca0a1292013-03-16 11:24:23 +08001064 if (wsi->u.ws.rxflow_buffer &&
1065 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1066 lwsl_info("draining rxflow\n");
1067 /* well, drain it */
1068 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1069 wsi->u.ws.rxflow_pos;
1070 eff_buf.token_len = wsi->u.ws.rxflow_len -
1071 wsi->u.ws.rxflow_pos;
1072 draining_flow = 1;
1073 goto drain;
1074 }
1075
Andy Green0d338332011-02-12 11:57:43 +00001076 /* any incoming data ready? */
1077
1078 if (!(pollfd->revents & POLLIN))
1079 break;
1080
Andy Greenb45993c2010-12-18 15:13:50 +00001081#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001082read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001083 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001084 eff_buf.token_len = SSL_read(wsi->ssl,
1085 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001086 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001087 if (!eff_buf.token_len) {
1088 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001089 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001090 ERR_error_string(n,
1091 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001092 }
1093 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001094#endif
Andy Greene84652c2013-02-08 13:01:02 +08001095 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001096 context->service_buffer,
1097 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001098
Andy Green98a717c2011-03-06 13:14:15 +00001099 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001100 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1101 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001102 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001103 goto close_and_handled;
1104 n = 0;
1105 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001106 }
Andy Green98a717c2011-03-06 13:14:15 +00001107 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001108 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001109 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001110 }
1111
Andy Green98a717c2011-03-06 13:14:15 +00001112 /*
1113 * give any active extensions a chance to munge the buffer
1114 * before parse. We pass in a pointer to an lws_tokens struct
1115 * prepared with the default buffer and content length that's in
1116 * there. Rather than rewrite the default buffer, extensions
1117 * that expect to grow the buffer can adapt .token to
1118 * point to their own per-connection buffer in the extension
1119 * user allocation. By default with no extensions or no
1120 * extension callback handling, just the normal input buffer is
1121 * used then so it is efficient.
1122 */
Andy Greenb45993c2010-12-18 15:13:50 +00001123
Andy Greene84652c2013-02-08 13:01:02 +08001124 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001125drain:
Andy Green3182ece2013-01-20 17:08:31 +08001126#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001127 more = 1;
1128 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001129
Andy Green98a717c2011-03-06 13:14:15 +00001130 more = 0;
1131
1132 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001133 m = wsi->active_extensions[n]->callback(context,
1134 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001135 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001136 wsi->active_extensions_user[n],
1137 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001138 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001139 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001140 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001141 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001142 }
1143 if (m)
1144 more = 1;
1145 }
Andy Green3182ece2013-01-20 17:08:31 +08001146#endif
Andy Green98a717c2011-03-06 13:14:15 +00001147 /* service incoming data */
1148
1149 if (eff_buf.token_len) {
1150 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001151 (unsigned char *)eff_buf.token,
1152 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001153 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001154 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001155 n = 0;
1156 goto handled;
1157 }
Andy Green98a717c2011-03-06 13:14:15 +00001158 }
Andy Green3182ece2013-01-20 17:08:31 +08001159#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001160 eff_buf.token = NULL;
1161 eff_buf.token_len = 0;
1162 }
Andy Green3182ece2013-01-20 17:08:31 +08001163#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001164 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1165 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1166 lwsl_info("flow buffer: drained\n");
1167 free(wsi->u.ws.rxflow_buffer);
1168 wsi->u.ws.rxflow_buffer = NULL;
1169 /* having drained the rxflow buffer, can rearm POLLIN */
1170 _libwebsocket_rx_flow_control(wsi);
1171 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001172
1173#ifdef LWS_OPENSSL_SUPPORT
1174 if (wsi->ssl && SSL_pending(wsi->ssl))
1175 goto read_pending;
1176#endif
Andy Green98a717c2011-03-06 13:14:15 +00001177 break;
Andy Green76f61e72013-01-16 11:53:05 +08001178
1179 default:
Andy Green03674a62013-01-16 11:47:40 +08001180#ifdef LWS_NO_CLIENT
1181 break;
1182#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001183 n = lws_client_socket_service(context, wsi, pollfd);
1184 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001185#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001186 }
1187
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001188 n = 0;
1189 goto handled;
1190
1191close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001192 libwebsocket_close_and_free_session(context, wsi,
1193 LWS_CLOSE_STATUS_NOSTATUS);
1194 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001195
1196handled:
1197 pollfd->revents = 0;
1198 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001199}
1200
Andy Green0d338332011-02-12 11:57:43 +00001201
Andy Green6964bb52011-01-23 16:50:33 +00001202/**
1203 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001204 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001205 *
1206 * This function closes any active connections and then frees the
1207 * context. After calling this, any further use of the context is
1208 * undefined.
1209 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001210LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001211libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001212{
Andy Green3182ece2013-01-20 17:08:31 +08001213#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001214 int n;
1215 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001216 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001217 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001218
Andy Greend636e352013-01-29 12:36:17 +08001219#ifdef LWS_LATENCY
1220 if (context->worst_latency_info[0])
1221 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1222#endif
1223
Andy Greendfb23042013-01-17 12:26:48 +08001224 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001225 struct libwebsocket *wsi =
1226 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001227 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001228 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1229 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001230 }
Andy Green6964bb52011-01-23 16:50:33 +00001231
Andy Greena41314f2011-05-23 10:00:03 +01001232 /*
1233 * give all extensions a chance to clean up any per-context
1234 * allocations they might have made
1235 */
1236
1237 ext = context->extensions;
1238 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1239 if (context->listen_port)
1240 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001241 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001242 ext->callback(context, ext, NULL,
1243 (enum libwebsocket_extension_callback_reasons)m,
1244 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001245 ext++;
1246 }
Andy Greena7109e62013-02-11 12:05:54 +08001247
1248 /*
1249 * inform all the protocols that they are done and will have no more
1250 * callbacks
1251 */
1252
1253 while (protocol->callback) {
1254 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1255 NULL, NULL, 0);
1256 protocol++;
1257 }
1258
Andy Green3182ece2013-01-20 17:08:31 +08001259#endif
Andy Greena41314f2011-05-23 10:00:03 +01001260
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001261#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001262#else
1263 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001264#endif
1265
Peter Hinz56885f32011-03-02 22:03:47 +00001266#ifdef LWS_OPENSSL_SUPPORT
1267 if (context->ssl_ctx)
1268 SSL_CTX_free(context->ssl_ctx);
1269 if (context->ssl_client_ctx)
1270 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001271
1272 ERR_remove_state(0);
1273 ERR_free_strings();
1274 EVP_cleanup();
1275 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001276#endif
1277
Andy Green46596482013-02-11 11:04:01 +08001278 if (context->fds)
1279 free(context->fds);
1280 if (context->lws_lookup)
1281 free(context->lws_lookup);
1282
Peter Hinz56885f32011-03-02 22:03:47 +00001283 free(context);
1284
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001285#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001286 WSACleanup();
1287#endif
Andy Green6964bb52011-01-23 16:50:33 +00001288}
1289
Andy Greend88146d2013-01-22 12:40:35 +08001290/**
Andy Greenb5b23192013-02-11 17:13:32 +08001291 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001292 * @context: Websocket context
1293 *
1294 * This returns the optional user allocation that can be attached to
1295 * the context the sockets live in at context_create time. It's a way
1296 * to let all sockets serviced in the same context share data without
1297 * using globals statics in the user code.
1298 */
Alon Levy0291eb32012-10-19 11:21:56 +02001299LWS_EXTERN void *
1300libwebsocket_context_user(struct libwebsocket_context *context)
1301{
Andy Greenb5b23192013-02-11 17:13:32 +08001302 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001303}
1304
Andy Green6964bb52011-01-23 16:50:33 +00001305/**
1306 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001307 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001308 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1309 * service otherwise block and service immediately, returning
1310 * after the timeout if nothing needed service.
1311 *
1312 * This function deals with any pending websocket traffic, for three
1313 * kinds of event. It handles these events on both server and client
1314 * types of connection the same.
1315 *
1316 * 1) Accept new connections to our context's server
1317 *
Andy Green6f520a52013-01-29 17:57:39 +08001318 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001319 * server or client connections.
1320 *
1321 * You need to call this service function periodically to all the above
1322 * functions to happen; if your application is single-threaded you can
1323 * just call it in your main event loop.
1324 *
1325 * Alternatively you can fork a new process that asynchronously handles
1326 * calling this service in a loop. In that case you are happy if this
1327 * call blocks your thread until it needs to take care of something and
1328 * would call it with a large nonzero timeout. Your loop then takes no
1329 * CPU while there is nothing happening.
1330 *
1331 * If you are calling it in a single-threaded app, you don't want it to
1332 * wait around blocking other things in your loop from happening, so you
1333 * would call it with a timeout_ms of 0, so it returns immediately if
1334 * nothing is pending, or as soon as it services whatever was pending.
1335 */
1336
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001337LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001338libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001339{
1340 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001341 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001342
1343 /* stay dead once we are dead */
1344
Peter Hinz56885f32011-03-02 22:03:47 +00001345 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001346 return 1;
1347
Andy Green0d338332011-02-12 11:57:43 +00001348 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001349
Peter Hinz56885f32011-03-02 22:03:47 +00001350 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green5dc62ea2013-09-20 20:26:12 +08001351 if (n == 0) /* poll timeout */ {
1352 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001353 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001354 }
Andy Greene92cd172011-01-19 13:11:55 +00001355
Andy Greendfb23042013-01-17 12:26:48 +08001356 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001357 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001358
Andy Greendfb23042013-01-17 12:26:48 +08001359 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001360
Andy Greenca0a1292013-03-16 11:24:23 +08001361 for (n = 0; n < context->fds_count; n++) {
1362 if (!context->fds[n].revents)
1363 continue;
1364 m = libwebsocket_service_fd(context, &context->fds[n]);
1365 if (m < 0)
1366 return -1;
1367 /* if something closed, retry this slot */
1368 if (m)
1369 n--;
1370 }
1371
Andy Greene92cd172011-01-19 13:11:55 +00001372 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001373}
1374
Andy Green3182ece2013-01-20 17:08:31 +08001375#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001376int
1377lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001378 struct libwebsocket *wsi,
1379 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001380 void *v, size_t len)
1381{
1382 int n;
1383 int handled = 0;
1384
1385 /* maybe an extension will take care of it for us */
1386
1387 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1388 if (!wsi->active_extensions[n]->callback)
1389 continue;
1390
1391 handled |= wsi->active_extensions[n]->callback(context,
1392 wsi->active_extensions[n], wsi,
1393 r, wsi->active_extensions_user[n], v, len);
1394 }
1395
1396 return handled;
1397}
1398
1399
1400void *
1401lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001402 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001403{
1404 int n = 0;
1405
Andy Green68b45042011-05-25 21:41:57 +01001406 if (wsi == NULL)
1407 return NULL;
1408
Andy Greena41314f2011-05-23 10:00:03 +01001409 while (n < wsi->count_active_extensions) {
1410 if (wsi->active_extensions[n] != ext) {
1411 n++;
1412 continue;
1413 }
1414 return wsi->active_extensions_user[n];
1415 }
1416
1417 return NULL;
1418}
Andy Green3182ece2013-01-20 17:08:31 +08001419#endif
Andy Greena41314f2011-05-23 10:00:03 +01001420
Andy Green90c7cbc2011-01-27 06:26:52 +00001421/**
1422 * libwebsocket_callback_on_writable() - Request a callback when this socket
1423 * becomes able to be written to without
1424 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001425 *
Peter Hinz56885f32011-03-02 22:03:47 +00001426 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001427 * @wsi: Websocket connection instance to get callback for
1428 */
1429
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001430LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001431libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001432 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001433{
Andy Green3182ece2013-01-20 17:08:31 +08001434#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001435 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001436 int handled = 0;
1437
1438 /* maybe an extension will take care of it for us */
1439
1440 for (n = 0; n < wsi->count_active_extensions; n++) {
1441 if (!wsi->active_extensions[n]->callback)
1442 continue;
1443
1444 handled |= wsi->active_extensions[n]->callback(context,
1445 wsi->active_extensions[n], wsi,
1446 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1447 wsi->active_extensions_user[n], NULL, 0);
1448 }
1449
1450 if (handled)
1451 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001452#endif
Andy Greendfb23042013-01-17 12:26:48 +08001453 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001454 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1455 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001456 return -1;
1457 }
1458
Andy Green7a132792013-12-18 09:48:26 +08001459 context->protocols[0].callback(context, wsi,
1460 LWS_CALLBACK_LOCK_POLL,
1461 wsi->user_space, (void *)(long)wsi->sock, 0);
1462
Andy Greendfb23042013-01-17 12:26:48 +08001463 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001464
Andy Green3221f922011-02-12 13:14:11 +00001465 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001466 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001467 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001468 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001469
Andy Green7a132792013-12-18 09:48:26 +08001470 context->protocols[0].callback(context, wsi,
1471 LWS_CALLBACK_UNLOCK_POLL,
1472 wsi->user_space, (void *)(long)wsi->sock, 0);
1473
Andy Green90c7cbc2011-01-27 06:26:52 +00001474 return 1;
1475}
1476
1477/**
1478 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1479 * all connections using the given protocol when it
1480 * becomes possible to write to each socket without
1481 * blocking in turn.
1482 *
1483 * @protocol: Protocol whose connections will get callbacks
1484 */
1485
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001486LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001487libwebsocket_callback_on_writable_all_protocol(
1488 const struct libwebsocket_protocols *protocol)
1489{
Peter Hinz56885f32011-03-02 22:03:47 +00001490 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001491 int n;
Andy Green0d338332011-02-12 11:57:43 +00001492 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001493
Andy Greendfb23042013-01-17 12:26:48 +08001494 for (n = 0; n < context->fds_count; n++) {
1495 wsi = context->lws_lookup[context->fds[n].fd];
1496 if (!wsi)
1497 continue;
1498 if (wsi->protocol == protocol)
1499 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001500 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001501
1502 return 0;
1503}
1504
Andy Greenbe93fef2011-02-14 20:25:43 +00001505/**
1506 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1507 *
1508 * You will not need this unless you are doing something special
1509 *
1510 * @wsi: Websocket connection instance
1511 * @reason: timeout reason
1512 * @secs: how many seconds
1513 */
1514
1515void
1516libwebsocket_set_timeout(struct libwebsocket *wsi,
1517 enum pending_timeout reason, int secs)
1518{
1519 struct timeval tv;
1520
1521 gettimeofday(&tv, NULL);
1522
1523 wsi->pending_timeout_limit = tv.tv_sec + secs;
1524 wsi->pending_timeout = reason;
1525}
1526
Andy Greena6cbece2011-01-27 20:06:03 +00001527
1528/**
1529 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1530 *
1531 * You will not need this unless you are doing something special
1532 *
1533 * @wsi: Websocket connection instance
1534 */
1535
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001536LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001537libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1538{
1539 return wsi->sock;
1540}
1541
Andy Greend636e352013-01-29 12:36:17 +08001542#ifdef LWS_LATENCY
1543void
Andy Greenb5b23192013-02-11 17:13:32 +08001544lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1545 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001546{
1547 struct timeval tv;
1548 unsigned long u;
1549 char buf[256];
1550
1551 gettimeofday(&tv, NULL);
1552
1553 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1554
1555 if (action) {
1556 if (completed) {
1557 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001558 sprintf(buf,
1559 "Completion first try lat %luus: %p: ret %d: %s\n",
1560 u - wsi->latency_start,
1561 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001562 else
Andy Greenb5b23192013-02-11 17:13:32 +08001563 sprintf(buf,
1564 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1565 u - wsi->action_start,
1566 u - wsi->latency_start,
1567 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001568 wsi->action_start = 0;
1569 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001570 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1571 u - wsi->latency_start,
1572 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001573 if (u - wsi->latency_start > context->worst_latency) {
1574 context->worst_latency = u - wsi->latency_start;
1575 strcpy(context->worst_latency_info, buf);
1576 }
1577 lwsl_latency("%s", buf);
1578 } else {
1579 wsi->latency_start = u;
1580 if (!wsi->action_start)
1581 wsi->action_start = u;
1582 }
1583}
1584#endif
1585
Andy Greena1ce6be2013-01-18 11:43:21 +08001586#ifdef LWS_NO_SERVER
1587int
Andy Green8d15cf42013-10-24 21:47:06 +08001588_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001589{
1590 return 0;
1591}
1592#else
Andy Green706961d2013-01-17 16:50:35 +08001593int
1594_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1595{
1596 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001597
Andy Greenca0a1292013-03-16 11:24:23 +08001598 /* there is no pending change */
1599 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001600 return 0;
1601
Andy Greenca0a1292013-03-16 11:24:23 +08001602 /* stuff is still buffered, not ready to really accept new input */
1603 if (wsi->u.ws.rxflow_buffer) {
1604 /* get ourselves called back to deal with stashed buffer */
1605 libwebsocket_callback_on_writable(context, wsi);
1606 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001607 }
1608
Andy Greenca0a1292013-03-16 11:24:23 +08001609 /* pending is cleared, we can change rxflow state */
1610
1611 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1612
1613 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1614 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1615
1616 /* adjust the pollfd for this wsi */
1617
Andy Green7a132792013-12-18 09:48:26 +08001618 context->protocols[0].callback(context, wsi,
1619 LWS_CALLBACK_LOCK_POLL,
1620 wsi->user_space, (void *)(long)wsi->sock, 0);
1621
Andy Greenca0a1292013-03-16 11:24:23 +08001622 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001623 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1624 else
1625 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1626
Andy Greenca0a1292013-03-16 11:24:23 +08001627 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001628 /* external POLL support via protocol 0 */
1629 context->protocols[0].callback(context, wsi,
1630 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001631 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001632 else
1633 /* external POLL support via protocol 0 */
1634 context->protocols[0].callback(context, wsi,
1635 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001636 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001637
Andy Green7a132792013-12-18 09:48:26 +08001638 context->protocols[0].callback(context, wsi,
1639 LWS_CALLBACK_UNLOCK_POLL,
1640 wsi->user_space, (void *)(long)wsi->sock, 0);
1641
Andy Green706961d2013-01-17 16:50:35 +08001642 return 1;
1643}
Andy Greena1ce6be2013-01-18 11:43:21 +08001644#endif
Andy Green706961d2013-01-17 16:50:35 +08001645
Andy Green90c7cbc2011-01-27 06:26:52 +00001646/**
1647 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1648 * receieved packets.
1649 *
1650 * If the output side of a server process becomes choked, this allows flow
1651 * control for the input side.
1652 *
1653 * @wsi: Websocket connection instance to get callback for
1654 * @enable: 0 = disable read servicing for this connection, 1 = enable
1655 */
1656
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001657LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001658libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1659{
Andy Greenca0a1292013-03-16 11:24:23 +08001660 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1661 return 0;
1662
1663 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1664 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001665
Andy Green706961d2013-01-17 16:50:35 +08001666 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001667}
1668
Andy Greenb55451c2013-03-16 12:32:27 +08001669/**
1670 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1671 *
1672 * When the user server code realizes it can accept more input, it can
1673 * call this to have the RX flow restriction removed from all connections using
1674 * the given protocol.
1675 *
1676 * @protocol: all connections using this protocol will be allowed to receive
1677 */
1678
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001679LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001680libwebsocket_rx_flow_allow_all_protocol(
1681 const struct libwebsocket_protocols *protocol)
1682{
1683 struct libwebsocket_context *context = protocol->owning_server;
1684 int n;
1685 struct libwebsocket *wsi;
1686
1687 for (n = 0; n < context->fds_count; n++) {
1688 wsi = context->lws_lookup[context->fds[n].fd];
1689 if (!wsi)
1690 continue;
1691 if (wsi->protocol == protocol)
1692 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1693 }
1694}
1695
Andy Green706961d2013-01-17 16:50:35 +08001696
Andy Green2ac5a6f2011-01-28 10:00:18 +00001697/**
1698 * libwebsocket_canonical_hostname() - returns this host's hostname
1699 *
1700 * This is typically used by client code to fill in the host parameter
1701 * when making a client connection. You can only call it after the context
1702 * has been created.
1703 *
Peter Hinz56885f32011-03-02 22:03:47 +00001704 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001705 */
1706
1707
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001708LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001709libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001710{
Peter Hinz56885f32011-03-02 22:03:47 +00001711 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001712}
1713
1714
Andy Green90c7cbc2011-01-27 06:26:52 +00001715static void sigpipe_handler(int x)
1716{
1717}
1718
Andy Green6901cb32011-02-21 08:06:47 +00001719#ifdef LWS_OPENSSL_SUPPORT
1720static int
1721OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1722{
1723
1724 SSL *ssl;
1725 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001726 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001727
1728 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1729 SSL_get_ex_data_X509_STORE_CTX_idx());
1730
1731 /*
Andy Green2e24da02011-03-05 16:12:04 +00001732 * !!! nasty openssl requires the index to come as a library-scope
1733 * static
Andy Green6901cb32011-02-21 08:06:47 +00001734 */
Andy Green2e24da02011-03-05 16:12:04 +00001735 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001736
Peter Hinz56885f32011-03-02 22:03:47 +00001737 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001738 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1739 x509_ctx, ssl, preverify_ok);
1740
1741 /* convert return code from 0 = OK to 1 = OK */
1742
1743 if (!n)
1744 n = 1;
1745 else
1746 n = 0;
1747
1748 return n;
1749}
1750#endif
1751
Andy Green706961d2013-01-17 16:50:35 +08001752int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001753 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001754 struct libwebsocket *wsi,
1755 enum libwebsocket_callback_reasons reason, void *user,
1756 void *in, size_t len)
1757{
1758 int n;
1759
1760 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001761 if (!n)
1762 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001763
Andy Greenaedc9532013-02-10 21:21:24 +08001764 return n;
Andy Green706961d2013-01-17 16:50:35 +08001765}
1766
Andy Greenb45993c2010-12-18 15:13:50 +00001767
Andy Greenab990e42010-10-31 12:42:52 +00001768/**
Andy Green4739e5c2011-01-22 12:51:57 +00001769 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001770 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001771 *
Andy Green1b265272013-02-09 14:01:09 +08001772 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001773 * of all initialization in one step.
1774 *
Andy Greene92cd172011-01-19 13:11:55 +00001775 * After initialization, it returns a struct libwebsocket_context * that
1776 * represents this server. After calling, user code needs to take care
1777 * of calling libwebsocket_service() with the context pointer to get the
1778 * server's sockets serviced. This can be done in the same process context
1779 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001780 *
Andy Green8f037e42010-12-19 22:13:26 +00001781 * The protocol callback functions are called for a handful of events
1782 * including http requests coming in, websocket connections becoming
1783 * established, and data arriving; it's also called periodically to allow
1784 * async transmission.
1785 *
1786 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1787 * at that time websocket protocol has not been negotiated. Other
1788 * protocols after the first one never see any HTTP callack activity.
1789 *
1790 * The server created is a simple http server by default; part of the
1791 * websocket standard is upgrading this http connection to a websocket one.
1792 *
1793 * This allows the same server to provide files like scripts and favicon /
1794 * images or whatever over http and dynamic data over websockets all in
1795 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001796 */
Andy Green4ea60062010-10-30 12:15:07 +01001797
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001798LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001799libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001800{
Peter Hinz56885f32011-03-02 22:03:47 +00001801 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001802 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001803 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001804#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001805 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001806 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001807 struct sockaddr_in serv_addr;
1808#endif
Andy Green3182ece2013-01-20 17:08:31 +08001809#ifndef LWS_NO_EXTENSIONS
1810 int m;
Andy Green1b265272013-02-09 14:01:09 +08001811 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001812#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001813
Andy Green3faa9c72010-11-08 17:03:03 +00001814#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001815 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001816#endif
1817
Joakim Soderberg4c531232013-02-06 15:26:58 +09001818#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001819 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001820#endif
1821
Andy Greenb3a614a2013-01-19 13:08:17 +08001822 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001823 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001824 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001825 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001826#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001827 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1828 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001829#else
1830 lwsl_notice(" Configured without extension support\n");
1831#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001832 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1833 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001834 if (info->ssl_cipher_list)
1835 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001836 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1837 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001838
Peter Hinz56885f32011-03-02 22:03:47 +00001839#ifdef _WIN32
1840 {
1841 WORD wVersionRequested;
1842 WSADATA wsaData;
1843 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001844 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001845
1846 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1847 wVersionRequested = MAKEWORD(2, 2);
1848
1849 err = WSAStartup(wVersionRequested, &wsaData);
1850 if (err != 0) {
1851 /* Tell the user that we could not find a usable */
1852 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001853 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001854 return NULL;
1855 }
David Galeano7b11fec2011-10-04 19:55:18 +08001856
Andy Green6ee372f2012-04-09 15:09:01 +08001857 /* default to a poll() made out of select() */
1858 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001859
Andy Green6ee372f2012-04-09 15:09:01 +08001860 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001861 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001862 if (wsdll)
1863 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001864
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001865 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001866 if (!poll)
1867 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001868 }
1869#endif
1870
Andy Greenb5b23192013-02-11 17:13:32 +08001871 context = (struct libwebsocket_context *)
1872 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001873 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001874 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001875 return NULL;
1876 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001877 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001878#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001879 context->started_with_parent = pid_daemon;
1880 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1881#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001882
Joakim Soderberg4c531232013-02-06 15:26:58 +09001883 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001884 context->protocols = info->protocols;
1885 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001886 context->http_proxy_port = 0;
1887 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001888 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001889 /* to reduce this allocation, */
1890 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001891 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1892 sizeof(struct libwebsocket_context),
1893 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1894 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001895 sizeof(struct libwebsocket_context) +
1896 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1897 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001898
Andy Greenb5b23192013-02-11 17:13:32 +08001899 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1900 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001901 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001902 lwsl_err("Unable to allocate fds array for %d connections\n",
1903 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001904 free(context);
1905 return NULL;
1906 }
Andy Greenb5b23192013-02-11 17:13:32 +08001907 context->lws_lookup = (struct libwebsocket **)
1908 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001909 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001910 lwsl_err(
1911 "Unable to allocate lws_lookup array for %d connections\n",
1912 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001913 free(context->fds);
1914 free(context);
1915 return NULL;
1916 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001917 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1918 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001919
Peter Hinz56885f32011-03-02 22:03:47 +00001920 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001921#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001922 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001923#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001924 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001925 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001926
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001927#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001928 context->fd_random = 0;
1929#else
1930 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1931 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001932 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001933 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001934 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001935 }
Peter Hinz56885f32011-03-02 22:03:47 +00001936#endif
Andy Green44eee682011-02-10 09:32:24 +00001937
Peter Hinz56885f32011-03-02 22:03:47 +00001938#ifdef LWS_OPENSSL_SUPPORT
1939 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08001940 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001941 context->ssl_ctx = NULL;
1942 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001943 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001944#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001945
Andy Greena1ce6be2013-01-18 11:43:21 +08001946 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001947
Andy Greena1ce6be2013-01-18 11:43:21 +08001948#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001949 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001950 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001951 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001952 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001953
Andy Greenb5b23192013-02-11 17:13:32 +08001954 lwsl_notice(" canonical_hostname = %s\n",
1955 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001956 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001957#endif
Andy Greena69f0512012-05-03 12:32:38 +08001958
Andy Green9659f372011-01-27 22:01:43 +00001959 /* split the proxy ads:port if given */
1960
1961 p = getenv("http_proxy");
1962 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001963 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001964 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001965 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001966 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001967
Peter Hinz56885f32011-03-02 22:03:47 +00001968 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001969 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001970 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001971 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001972 }
1973 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001974 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001975
Andy Greenb3a614a2013-01-19 13:08:17 +08001976 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001977 context->http_proxy_address,
1978 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001979 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001980
Andy Greena1ce6be2013-01-18 11:43:21 +08001981#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001982 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001983
Andy Green3faa9c72010-11-08 17:03:03 +00001984#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001985 context->use_ssl = info->ssl_cert_filepath != NULL &&
1986 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001987#ifdef USE_CYASSL
1988 lwsl_notice(" Compiled with CYASSL support\n");
1989#else
1990 lwsl_notice(" Compiled with OpenSSL support\n");
1991#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001992 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001993 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001994 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001995 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001996
Andy Green90c7cbc2011-01-27 06:26:52 +00001997#else
Andy Green2b40b792013-02-10 22:22:01 +08001998 if (info->ssl_cert_filepath != NULL &&
1999 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002000 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002001 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002002 }
Andy Greenb5b23192013-02-11 17:13:32 +08002003 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002004#endif
Andy Greena17c6922013-01-20 20:21:54 +08002005
Andy Greenb5b23192013-02-11 17:13:32 +08002006 lwsl_notice(
2007 " per-conn mem: %u + %u headers + protocol rx buf\n",
2008 sizeof(struct libwebsocket),
2009 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002010 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002011#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002012
2013 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002014#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002015#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002016 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002017#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002018
2019
2020#ifdef LWS_OPENSSL_SUPPORT
2021
2022 /* basic openssl init */
2023
2024 SSL_library_init();
2025
2026 OpenSSL_add_all_algorithms();
2027 SSL_load_error_strings();
2028
Andy Green2e24da02011-03-05 16:12:04 +00002029 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002030 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2031
Andy Green90c7cbc2011-01-27 06:26:52 +00002032 /*
2033 * Firefox insists on SSLv23 not SSLv3
2034 * Konq disables SSLv2 by default now, SSLv23 works
2035 */
2036
2037 method = (SSL_METHOD *)SSLv23_server_method();
2038 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002039 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002040 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002041 error,
2042 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002043 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002044 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002045 }
Peter Hinz56885f32011-03-02 22:03:47 +00002046 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2047 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002048 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002049 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002050 error,
2051 ERR_error_string(error,
2052 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002053 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002054 }
2055
David Galeanocc148e42013-01-10 10:18:59 +08002056#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002057 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002058#endif
David Galeano77a677c2013-01-10 10:14:12 +08002059 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002060 if (info->ssl_cipher_list)
2061 SSL_CTX_set_cipher_list(context->ssl_ctx,
2062 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002063
Andy Greena1ce6be2013-01-18 11:43:21 +08002064#ifndef LWS_NO_CLIENT
2065
Andy Green90c7cbc2011-01-27 06:26:52 +00002066 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002067
Andy Green1b265272013-02-09 14:01:09 +08002068 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002069 method = (SSL_METHOD *)SSLv23_client_method();
2070 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002071 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002072 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002073 error,
2074 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002075 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002076 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002077 }
2078 /* create context */
2079 context->ssl_client_ctx = SSL_CTX_new(method);
2080 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002081 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002082 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002083 error,
2084 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002085 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002086 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002087 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002088
David Galeanocc148e42013-01-10 10:18:59 +08002089#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002090 SSL_CTX_set_options(context->ssl_client_ctx,
2091 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002092#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002093 SSL_CTX_set_options(context->ssl_client_ctx,
2094 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002095 if (info->ssl_cipher_list)
2096 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2097 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002098
Peter Hinz56885f32011-03-02 22:03:47 +00002099 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002100 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002101 if (!SSL_CTX_load_verify_locations(
2102 context->ssl_client_ctx, NULL,
2103 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002104 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002105 "Unable to load SSL Client certs from %s "
2106 "(set by --with-client-cert-dir= "
2107 "in configure) -- client ssl isn't "
2108 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002109 } else
2110 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002111 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002112 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002113 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002114 "Unable to load SSL Client certs "
2115 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002116 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002117
2118 /*
2119 * callback allowing user code to load extra verification certs
2120 * helping the client to verify server identity
2121 */
2122
prasannateamf1b353a452013-11-12 17:21:01 -08002123 /* support for client-side certificate authentication */
2124 if (info->ssl_cert_filepath) {
2125 n = SSL_CTX_use_certificate_chain_file(
2126 context->ssl_client_ctx,
2127 info->ssl_cert_filepath);
2128 if (n != 1) {
2129 lwsl_err("problem getting cert '%s' %lu: %s\n",
2130 info->ssl_cert_filepath,
2131 ERR_get_error(),
2132 ERR_error_string(ERR_get_error(),
2133 (char *)context->service_buffer));
2134 goto bail;
2135 }
2136 }
2137 if (info->ssl_private_key_filepath) {
2138 /* set the private key from KeyFile */
2139 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2140 info->ssl_private_key_filepath,
2141 SSL_FILETYPE_PEM) != 1) {
2142 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2143 info->ssl_private_key_filepath,
2144 ERR_get_error(),
2145 ERR_error_string(ERR_get_error(),
2146 (char *)context->service_buffer));
2147 goto bail;
2148 }
2149
2150 /* verify private key */
2151 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2152 lwsl_err("Private SSL key doesn't match cert\n");
2153 goto bail;
2154 }
2155 }
2156
Peter Hinz56885f32011-03-02 22:03:47 +00002157 context->protocols[0].callback(context, NULL,
2158 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2159 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002160 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002161#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002162
Andy Greenc6bf2c22011-02-20 11:10:47 +00002163 /* as a server, are we requiring clients to identify themselves? */
2164
Andy Greenb5b23192013-02-11 17:13:32 +08002165 if (info->options &
2166 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002167
2168 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002169
Peter Hinz56885f32011-03-02 22:03:47 +00002170 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002171 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2172 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002173
2174 /*
2175 * give user code a chance to load certs into the server
2176 * allowing it to verify incoming client certs
2177 */
2178
Peter Hinz56885f32011-03-02 22:03:47 +00002179 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002180 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002181 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002182 }
2183
James Devine5b34c972013-12-14 11:41:29 +08002184 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2185 /* Normally SSL listener rejects non-ssl, optionally allow */
2186 context->allow_non_ssl_on_ssl_port = 1;
2187 }
2188
Peter Hinz56885f32011-03-02 22:03:47 +00002189 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002190
2191 /* openssl init for server sockets */
2192
Andy Green3faa9c72010-11-08 17:03:03 +00002193 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002194 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002195 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002196 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002197 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002198 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002199 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002200 error,
2201 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002202 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002203 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002204 }
2205 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002206 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002207 info->ssl_private_key_filepath,
2208 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002209 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002210 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002211 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002212 error,
2213 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002214 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002215 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002216 }
2217 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002218 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002219 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002220 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002221 }
2222
2223 /* SSL is happy and has a cert it's content with */
2224 }
2225#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002226
Andy Greena1ce6be2013-01-18 11:43:21 +08002227#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002228 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002229
Andy Green1b265272013-02-09 14:01:09 +08002230 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002231 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002232
Andy Green4739e5c2011-01-22 12:51:57 +00002233 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2234 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002235 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002236 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002237 }
Andy Green775c0dd2010-10-29 14:15:22 +01002238
Joakim Soderberg4c531232013-02-06 15:26:58 +09002239#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002240 /*
2241 * allow us to restart even if old sockets in TIME_WAIT
2242 * (REUSEADDR on Unix means, "don't hang on to this
2243 * address after the listener is closed." On Windows, though,
2244 * it means "don't keep other processes from binding to
2245 * this address while we're using it)
2246 */
Andy Green6ee372f2012-04-09 15:09:01 +08002247 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2248 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002249#endif
Andy Green6c939552011-03-08 08:56:57 +00002250
2251 /* Disable Nagle */
2252 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002253 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2254 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002255
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002256 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002257 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002258 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002259 #else
Andy Greene2160712013-01-28 12:19:10 +08002260 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002261 #endif
Andy Greene2160712013-01-28 12:19:10 +08002262
Andy Green4739e5c2011-01-22 12:51:57 +00002263 bzero((char *) &serv_addr, sizeof(serv_addr));
2264 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002265 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002266 serv_addr.sin_addr.s_addr = INADDR_ANY;
2267 else
Andy Greend1eac602013-11-09 08:07:38 +08002268 if (interface_to_sa(info->iface, &serv_addr,
2269 sizeof(serv_addr)) < 0) {
2270 lwsl_err("Unable to find interface %s\n",
2271 info->iface);
2272 compatible_close(sockfd);
2273 goto bail;
2274 }
Andy Green1b265272013-02-09 14:01:09 +08002275 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002276
2277 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2278 sizeof(serv_addr));
2279 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002280 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002281 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002282 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002283 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002284 }
Andy Green0d338332011-02-12 11:57:43 +00002285
Andy Greenb5b23192013-02-11 17:13:32 +08002286 wsi = (struct libwebsocket *)malloc(
2287 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002288 if (wsi == NULL) {
2289 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002290 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002291 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002292 }
Andy Greenb5b23192013-02-11 17:13:32 +08002293 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002294 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002295#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002296 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002297#endif
Andy Green0d338332011-02-12 11:57:43 +00002298 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002299
2300 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002301
Andy Green65b0e912013-01-16 07:59:47 +08002302 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2303 context->listen_service_count = 0;
2304 context->listen_service_fd = sockfd;
2305
Andy Greena824d182013-01-15 20:52:29 +08002306 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002307 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002308 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002309#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002310
Andy Green6ee372f2012-04-09 15:09:01 +08002311 /*
2312 * drop any root privs for this process
2313 * to listen on port < 1023 we would have needed root, but now we are
2314 * listening, we don't want the power for anything else
2315 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002316#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002317#else
Andy Green1b265272013-02-09 14:01:09 +08002318 if (info->gid != -1)
2319 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002320 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002321 if (info->uid != -1)
2322 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002323 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002324#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002325
Andy Green6f520a52013-01-29 17:57:39 +08002326 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002327
Peter Hinz56885f32011-03-02 22:03:47 +00002328 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002329 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002330 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002331
Andy Green43db0452013-01-10 19:50:35 +08002332 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002333 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002334
Andy Green1b265272013-02-09 14:01:09 +08002335 info->protocols[context->count_protocols].owning_server =
2336 context;
2337 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002338 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002339
2340 /*
2341 * inform all the protocols that they are doing their one-time
2342 * initialization if they want to
2343 */
2344 info->protocols[context->count_protocols].callback(context,
2345 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002346 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002347
Andy Green3182ece2013-01-20 17:08:31 +08002348#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002349 /*
2350 * give all extensions a chance to create any per-context
2351 * allocations they need
2352 */
2353
2354 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002355 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002356 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002357
Andy Green1b265272013-02-09 14:01:09 +08002358 if (info->extensions) {
2359 ext = info->extensions;
2360 while (ext->callback) {
2361 lwsl_ext(" Extension: %s\n", ext->name);
2362 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002363 (enum libwebsocket_extension_callback_reasons)m,
2364 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002365 ext++;
2366 }
Andy Greena41314f2011-05-23 10:00:03 +01002367 }
Andy Green3182ece2013-01-20 17:08:31 +08002368#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002369 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002370
2371bail:
2372 libwebsocket_context_destroy(context);
2373 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002374}
Andy Greenb45993c2010-12-18 15:13:50 +00002375
Andy Greenb45993c2010-12-18 15:13:50 +00002376/**
shysb4e800e2013-10-24 22:12:03 +08002377 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2378 * @context: pointer to struct libwebsocket_context you want set proxy to
2379 * @proxy: pointer to c string containing proxy in format address:port
2380 *
2381 * Returns 0 if proxy string was parsed and proxy was setup.
2382 * Returns -1 if @proxy is NULL or has incorrect format.
2383 *
2384 * This is only required if your OS does not provide the http_proxy
2385 * enviroment variable (eg, OSX)
2386 *
2387 * IMPORTANT! You should call this function right after creation of the
2388 * libwebsocket_context and before call to connect. If you call this
2389 * function after connect behavior is undefined.
2390 * This function will override proxy settings made on libwebsocket_context
2391 * creation with genenv() call.
2392 */
2393
2394LWS_VISIBLE int
2395libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2396{
2397 char *p;
2398
2399 if (!proxy)
2400 return -1;
2401
2402 strncpy(context->http_proxy_address, proxy,
2403 sizeof(context->http_proxy_address) - 1);
2404 context->http_proxy_address[
2405 sizeof(context->http_proxy_address) - 1] = '\0';
2406
2407 p = strchr(context->http_proxy_address, ':');
2408 if (!p) {
2409 lwsl_err("http_proxy needs to be ads:port\n");
2410
2411 return -1;
2412 }
2413 *p = '\0';
2414 context->http_proxy_port = atoi(p + 1);
2415
2416 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2417 context->http_proxy_port);
2418
2419 return 0;
2420}
2421
2422/**
Andy Greenb45993c2010-12-18 15:13:50 +00002423 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002424 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002425 * @wsi: pointer to struct websocket you want to know the protocol of
2426 *
Andy Green8f037e42010-12-19 22:13:26 +00002427 *
Andy Green6f520a52013-01-29 17:57:39 +08002428 * Some apis can act on all live connections of a given protocol,
2429 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002430 */
Andy Greenab990e42010-10-31 12:42:52 +00002431
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002432LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002433libwebsockets_get_protocol(struct libwebsocket *wsi)
2434{
2435 return wsi->protocol;
2436}
2437
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002438LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002439libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2440{
Andy Green623a98d2013-01-21 11:04:23 +08002441 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002442}
Alex Bligh49146db2011-11-07 17:19:25 +08002443
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002444LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002445libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2446{
Andy Green623a98d2013-01-21 11:04:23 +08002447 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002448}
2449
Andy Green2af4d5b2013-02-18 16:30:10 +08002450int
Alex Bligh49146db2011-11-07 17:19:25 +08002451libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2452{
Andy Green67d556c2013-02-15 22:31:55 +08002453 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002454 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002455
Alex Bligh49146db2011-11-07 17:19:25 +08002456 /* allocate the per-connection user memory (if any) */
2457
2458 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2459 wsi->user_space = malloc(
2460 wsi->protocol->per_session_data_size);
2461 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002462 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002463 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002464 }
Andy Green6ee372f2012-04-09 15:09:01 +08002465 memset(wsi->user_space, 0,
2466 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002467 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002468 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002469}
Andy Green43db0452013-01-10 19:50:35 +08002470
Andy Green0b319092013-01-19 11:17:56 +08002471static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002472{
Andy Green0b319092013-01-19 11:17:56 +08002473 char buf[300];
2474 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002475 int n;
2476
2477 gettimeofday(&tv, NULL);
2478
2479 buf[0] = '\0';
2480 for (n = 0; n < LLL_COUNT; n++)
2481 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002482 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002483 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002484 break;
2485 }
Andy Greenb5b23192013-02-11 17:13:32 +08002486
Andy Green0b319092013-01-19 11:17:56 +08002487 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002488}
2489
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002490#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002491LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002492{
2493 lwsl_emit_stderr(level, line);
2494}
2495#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002496LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002497{
2498 int syslog_level = LOG_DEBUG;
2499
2500 switch (level) {
2501 case LLL_ERR:
2502 syslog_level = LOG_ERR;
2503 break;
2504 case LLL_WARN:
2505 syslog_level = LOG_WARNING;
2506 break;
2507 case LLL_NOTICE:
2508 syslog_level = LOG_NOTICE;
2509 break;
2510 case LLL_INFO:
2511 syslog_level = LOG_INFO;
2512 break;
2513 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002514 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002515}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002516#endif
Andy Greenc11db202013-01-19 11:12:16 +08002517
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002518LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002519{
Andy Greende8f27a2013-01-12 09:17:42 +08002520 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002521 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002522
2523 if (!(log_level & filter))
2524 return;
2525
Andy Green43db0452013-01-10 19:50:35 +08002526 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002527 vsnprintf(buf, sizeof(buf), format, ap);
2528 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002529 va_end(ap);
2530
Andy Green0b319092013-01-19 11:17:56 +08002531 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002532}
2533
2534/**
2535 * lws_set_log_level() - Set the logging bitfield
2536 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002537 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2538 * function to perform log string emission instead of
2539 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002540 *
Andy Greenc6511a02013-02-19 10:01:48 +08002541 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002542 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002543 */
2544
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002545LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002546 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002547{
2548 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002549 if (log_emit_function)
2550 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002551}