blob: 453c1e7511a166e7302150743fac578f8b5c1807 [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
Andy Green3b3fa9e2013-12-25 16:34:37 +080040#include <sys/types.h>
41
Andy Green2e24da02011-03-05 16:12:04 +000042#ifdef LWS_OPENSSL_SUPPORT
43int openssl_websocket_private_data_index;
44#endif
45
Andy Greenaa6fc442012-04-12 13:26:49 +080046#ifdef __MINGW32__
47#include "../win32port/win32helpers/websock-w32.c"
48#else
49#ifdef __MINGW64__
50#include "../win32port/win32helpers/websock-w32.c"
51#endif
52#endif
53
Andy Green7b405452013-02-01 10:50:15 +080054#ifndef LWS_BUILD_HASH
55#define LWS_BUILD_HASH "unknown-build-hash"
56#endif
Andy Green3182ece2013-01-20 17:08:31 +080057
Andy Green7c19c342013-01-19 12:18:07 +080058static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080059static void lwsl_emit_stderr(int level, const char *line);
60static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
61
Andy Green7b405452013-02-01 10:50:15 +080062static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
63
Andy Greenb5b23192013-02-11 17:13:32 +080064static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080065 "ERR",
66 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080067 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080068 "INFO",
69 "DEBUG",
70 "PARSER",
71 "HEADER",
72 "EXTENSION",
73 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080074 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080075};
76
Andy Greenb5b23192013-02-11 17:13:32 +080077#ifndef LWS_NO_CLIENT
78 extern int lws_client_socket_service(
79 struct libwebsocket_context *context,
80 struct libwebsocket *wsi, struct pollfd *pollfd);
81#endif
82#ifndef LWS_NO_SERVER
83 extern int lws_server_socket_service(
84 struct libwebsocket_context *context,
85 struct libwebsocket *wsi, struct pollfd *pollfd);
86#endif
87
Andy Green3b3fa9e2013-12-25 16:34:37 +080088
89#ifdef LWS_HAS_PPOLL
90/*
91 * set to the Thread ID that's doing the service loop just before entry to ppoll
92 * indicates service thread likely idling in ppoll()
93 * volatile because other threads may check it as part of processing for pollfd
94 * event change.
95 */
96static volatile int lws_idling_ppoll_tid;
97#endif
98
Andy Green7b405452013-02-01 10:50:15 +080099/**
100 * lws_get_library_version: get version and git hash library built from
101 *
102 * returns a const char * to a string like "1.1 178d78c"
103 * representing the library version followed by the git head hash it
104 * was built from
105 */
106
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800107LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +0800108lws_get_library_version(void)
109{
110 return library_version;
111}
112
Andy Green0d338332011-02-12 11:57:43 +0000113int
Andy Greenb5b23192013-02-11 17:13:32 +0800114insert_wsi_socket_into_fds(struct libwebsocket_context *context,
115 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000116{
Andy Greendfb23042013-01-17 12:26:48 +0800117 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000119 return 1;
120 }
121
nononamef1624922014-01-11 13:12:34 +0800122 if (wsi->sock >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800123 lwsl_err("Socket fd %d is too high (%d)\n",
124 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800125 return 1;
126 }
127
128 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800129 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800130
Andy Greenb5b23192013-02-11 17:13:32 +0800131 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
132 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800133
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800134 struct libwebsocket_pollargs pa = {wsi->sock, POLLIN, 0 };
Andy Green7a132792013-12-18 09:48:26 +0800135 context->protocols[0].callback(context, wsi,
136 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800137 wsi->user_space, (void *) &pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800138
Andy Greendfb23042013-01-17 12:26:48 +0800139 context->lws_lookup[wsi->sock] = wsi;
140 wsi->position_in_fds_table = context->fds_count;
141 context->fds[context->fds_count].fd = wsi->sock;
142 context->fds[context->fds_count].events = POLLIN;
143 context->fds[context->fds_count++].revents = 0;
144
145 /* external POLL support via protocol 0 */
146 context->protocols[0].callback(context, wsi,
147 LWS_CALLBACK_ADD_POLL_FD,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800148 wsi->user_space, (void *) &pa, 0);
Andy Green0d338332011-02-12 11:57:43 +0000149
Andy Green7a132792013-12-18 09:48:26 +0800150 context->protocols[0].callback(context, wsi,
151 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800152 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800153
Andy Green0d338332011-02-12 11:57:43 +0000154 return 0;
155}
156
Andy Greendfb23042013-01-17 12:26:48 +0800157static int
Andy Greenb5b23192013-02-11 17:13:32 +0800158remove_wsi_socket_from_fds(struct libwebsocket_context *context,
159 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000160{
Andy Greendfb23042013-01-17 12:26:48 +0800161 int m;
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800162 struct libwebsocket_pollargs pa = { wsi->sock, 0, 0};
Andy Green0d338332011-02-12 11:57:43 +0000163
Andy Green7a132792013-12-18 09:48:26 +0800164 if (!--context->fds_count) {
165 context->protocols[0].callback(context, wsi,
166 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800167 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800168 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800169 }
Andy Green0d338332011-02-12 11:57:43 +0000170
Andy Greendfb23042013-01-17 12:26:48 +0800171 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800172 lwsl_err("Socket fd %d too high (%d)\n",
173 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800174 return 1;
175 }
Andy Green0d338332011-02-12 11:57:43 +0000176
Andy Greenb5b23192013-02-11 17:13:32 +0800177 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
178 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800179
Andy Green7a132792013-12-18 09:48:26 +0800180 context->protocols[0].callback(context, wsi,
181 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800182 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800183
Andy Greendfb23042013-01-17 12:26:48 +0800184 m = wsi->position_in_fds_table; /* replace the contents for this */
185
186 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800187 context->fds[m] = context->fds[context->fds_count];
188 /*
189 * end guy's fds_lookup entry remains unchanged
190 * (still same fd pointing to same wsi)
191 */
Andy Greendfb23042013-01-17 12:26:48 +0800192 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800193 context->lws_lookup[context->fds[context->fds_count].fd]->
194 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800195 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800196 context->lws_lookup[wsi->sock] = NULL;
197 /* removed wsi has no position any more */
198 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800199
200do_ext:
201 /* remove also from external POLL support via protocol 0 */
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800202 if (wsi->sock) {
Andy Greendfb23042013-01-17 12:26:48 +0800203 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800204 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800205 (void *) &pa, 0);
206 }
Andy Green7a132792013-12-18 09:48:26 +0800207 context->protocols[0].callback(context, wsi,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800208 LWS_CALLBACK_UNLOCK_POLL,
209 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800210 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000211}
212
Andy Green32375b72011-02-19 08:32:53 +0000213
Andy Green8f037e42010-12-19 22:13:26 +0000214void
Peter Hinz56885f32011-03-02 22:03:47 +0000215libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000216 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000217{
Andy Greenb45993c2010-12-18 15:13:50 +0000218 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000219 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000220 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
221 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800222#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000223 int ret;
224 int m;
225 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100226 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800227#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000228
Andy Green4b6fbe12011-02-14 08:03:48 +0000229 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000230 return;
231
Andy Green62c54d22011-02-14 09:14:25 +0000232 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000233
Andy Green62c54d22011-02-14 09:14:25 +0000234 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000235 return;
236
Andy Green22524a62013-02-15 22:48:58 +0800237 /* we tried the polite way... */
238 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
239 goto just_kill_connection;
240
Andy Green623a98d2013-01-21 11:04:23 +0800241 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000242
Andy Green5dc62ea2013-09-20 20:26:12 +0800243 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
244 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
245
246 context->protocols[0].callback(context, wsi,
247 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
248
249 free(wsi->u.hdr.ah);
250 goto just_kill_connection;
251 }
252
253
kapejodce64fb02013-11-19 13:38:16 +0100254 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
255 if (wsi->u.http.post_buffer) {
256 free(wsi->u.http.post_buffer);
257 wsi->u.http.post_buffer = NULL;
258 }
259 if (wsi->u.http.fd >= 0) {
260 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
261 close(wsi->u.http.fd);
262 wsi->u.http.fd = -1;
263 context->protocols[0].callback(context, wsi,
264 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
265 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800266 }
267
Andy Green3182ece2013-01-20 17:08:31 +0800268#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000269 /*
Andy Green68b45042011-05-25 21:41:57 +0100270 * are his extensions okay with him closing? Eg he might be a mux
271 * parent and just his ch1 aspect is closing?
272 */
273
Andy Green68b45042011-05-25 21:41:57 +0100274 for (n = 0; n < wsi->count_active_extensions; n++) {
275 if (!wsi->active_extensions[n]->callback)
276 continue;
277
278 m = wsi->active_extensions[n]->callback(context,
279 wsi->active_extensions[n], wsi,
280 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
281 wsi->active_extensions_user[n], NULL, 0);
282
283 /*
284 * if somebody vetoed actually closing him at this time....
285 * up to the extension to track the attempted close, let's
286 * just bail
287 */
288
289 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800290 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100291 return;
292 }
293 }
294
Andy Green68b45042011-05-25 21:41:57 +0100295 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000296 * flush any tx pending from extensions, since we may send close packet
297 * if there are problems with send, just nuke the connection
298 */
299
300 ret = 1;
301 while (ret == 1) {
302
303 /* default to nobody has more to spill */
304
305 ret = 0;
306 eff_buf.token = NULL;
307 eff_buf.token_len = 0;
308
309 /* show every extension the new incoming data */
310
311 for (n = 0; n < wsi->count_active_extensions; n++) {
312 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000313 wsi->protocol->owning_server,
314 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000315 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
316 wsi->active_extensions_user[n], &eff_buf, 0);
317 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800318 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000319 goto just_kill_connection;
320 }
321 if (m)
322 /*
323 * at least one extension told us he has more
324 * to spill, so we will go around again after
325 */
326 ret = 1;
327 }
328
329 /* assuming they left us something to send, send it */
330
331 if (eff_buf.token_len)
332 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800333 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800334 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000335 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800336 }
Andy Greenc44159f2011-03-07 07:08:18 +0000337 }
Andy Green3182ece2013-01-20 17:08:31 +0800338#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000339
340 /*
Andy Greenda527df2011-03-07 07:08:12 +0000341 * signal we are closing, libsocket_write will
342 * add any necessary version-specific stuff. If the write fails,
343 * no worries we are closing anyway. If we didn't initiate this
344 * close, then our state has been changed to
345 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
346 *
347 * Likewise if it's a second call to close this connection after we
348 * sent the close indication to the peer already, we are in state
349 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
350 */
351
352 if (old_state == WSI_STATE_ESTABLISHED &&
353 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100354
Andy Green43db0452013-01-10 19:50:35 +0800355 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100356
Andy Greenfdd305a2013-02-11 14:32:48 +0800357 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800358 memset(buf, 0, sizeof(buf));
359 n = libwebsocket_write(wsi,
360 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000361 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800362 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000363 /*
364 * we have sent a nice protocol level indication we
365 * now wish to close, we should not send anything more
366 */
367
368 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
369
Andy Greenb5b23192013-02-11 17:13:32 +0800370 /*
371 * ...and we should wait for a reply for a bit
372 * out of politeness
373 */
Andy Greenda527df2011-03-07 07:08:12 +0000374
375 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800376 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000377
Andy Green43db0452013-01-10 19:50:35 +0800378 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000379
380 return;
381 }
382
Andy Greenb5b23192013-02-11 17:13:32 +0800383 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800384
Andy Greenda527df2011-03-07 07:08:12 +0000385 /* else, the send failed and we should just hang up */
386 }
387
Andy Greenc44159f2011-03-07 07:08:18 +0000388just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100389
Andy Greenb5b23192013-02-11 17:13:32 +0800390 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100391
Andy Greenda527df2011-03-07 07:08:12 +0000392 /*
393 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800394 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000395 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000396
Andy Greendfb23042013-01-17 12:26:48 +0800397 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000398
Andy Green251f6fa2010-11-03 11:13:06 +0000399 wsi->state = WSI_STATE_DEAD_SOCKET;
400
Andy Greenb5b23192013-02-11 17:13:32 +0800401 if ((old_state == WSI_STATE_ESTABLISHED ||
402 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800403 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
404
405 if (wsi->u.ws.rx_user_buffer) {
406 free(wsi->u.ws.rx_user_buffer);
407 wsi->u.ws.rx_user_buffer = NULL;
408 }
409 if (wsi->u.ws.rxflow_buffer) {
410 free(wsi->u.ws.rxflow_buffer);
411 wsi->u.ws.rxflow_buffer = NULL;
412 }
Andy Green2764eba2013-12-09 14:16:17 +0800413 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800414 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800415 free(wsi->truncated_send_malloc);
416 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800417 }
Andy Green54495112013-02-06 21:10:16 +0900418 }
419
Andy Green4b6fbe12011-02-14 08:03:48 +0000420 /* tell the user it's all over for this guy */
421
Andy Greend4302732011-02-28 07:45:29 +0000422 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800423 ((old_state == WSI_STATE_ESTABLISHED) ||
424 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
425 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800426 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000427 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000428 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800429 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
430 lwsl_debug("calling back CLOSED_HTTP\n");
431 context->protocols[0].callback(context, wsi,
432 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800433 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800434 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000435
Andy Green3182ece2013-01-20 17:08:31 +0800436#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000437 /* deallocate any active extension contexts */
438
439 for (n = 0; n < wsi->count_active_extensions; n++) {
440 if (!wsi->active_extensions[n]->callback)
441 continue;
442
Andy Green46c2ea02011-03-22 09:04:01 +0000443 wsi->active_extensions[n]->callback(context,
444 wsi->active_extensions[n], wsi,
445 LWS_EXT_CALLBACK_DESTROY,
446 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000447
448 free(wsi->active_extensions_user[n]);
449 }
450
Andy Greena41314f2011-05-23 10:00:03 +0100451 /*
452 * inform all extensions in case they tracked this guy out of band
453 * even though not active on him specifically
454 */
455
456 ext = context->extensions;
457 while (ext && ext->callback) {
458 ext->callback(context, ext, wsi,
459 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
460 NULL, NULL, 0);
461 ext++;
462 }
Andy Green3182ece2013-01-20 17:08:31 +0800463#endif
Andy Greena41314f2011-05-23 10:00:03 +0100464
Andy Green43db0452013-01-10 19:50:35 +0800465/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000466
Andy Green3faa9c72010-11-08 17:03:03 +0000467#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000468 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000469 n = SSL_get_fd(wsi->ssl);
470 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800471 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000472 SSL_free(wsi->ssl);
473 } else {
474#endif
Andy Green0303db42013-01-17 14:46:43 +0800475 if (wsi->sock) {
476 n = shutdown(wsi->sock, SHUT_RDWR);
477 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800478 lwsl_debug("closing: shutdown returned %d\n",
479 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800480
Andy Green0303db42013-01-17 14:46:43 +0800481 n = compatible_close(wsi->sock);
482 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800483 lwsl_debug("closing: close returned %d\n",
484 errno);
Andy Green0303db42013-01-17 14:46:43 +0800485 }
Andy Green3faa9c72010-11-08 17:03:03 +0000486#ifdef LWS_OPENSSL_SUPPORT
487 }
488#endif
Andy Green76b6ea12014-02-15 19:25:50 +0800489
490 /* outermost destroy notification for wsi (user_space still intact) */
491 context->protocols[0].callback(context, wsi,
492 LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0);
493
Andy Greenb5b23192013-02-11 17:13:32 +0800494 if (wsi->protocol && wsi->protocol->per_session_data_size &&
495 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000496 free(wsi->user_space);
497
Andy Green251f6fa2010-11-03 11:13:06 +0000498 free(wsi);
499}
500
Andy Green07034092011-02-13 08:37:12 +0000501/**
502 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800503 * @context: Libwebsockets context
504 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000505 * @fd: Connection socket descriptor
506 * @name: Buffer to take client address name
507 * @name_len: Length of client address name buffer
508 * @rip: Buffer to take client address IP qotted quad
509 * @rip_len: Length of client address IP buffer
510 *
511 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800512 * the client connected with socket descriptor @fd. Names may be
513 * truncated if there is not enough room. If either cannot be
514 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000515 */
516
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800517LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800518libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
519 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000520 char *rip, int rip_len)
521{
Bob Robertsac049112013-04-25 09:16:30 +0800522 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000523 struct sockaddr_in sin;
524 struct hostent *host;
525 struct hostent *host1;
526 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000527 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000528 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800529 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800530#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800531 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800532#endif
Andy Green07034092011-02-13 08:37:12 +0000533
534 rip[0] = '\0';
535 name[0] = '\0';
536
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800537 lws_latency_pre(context, wsi);
538
Andy Greenb5b23192013-02-11 17:13:32 +0800539 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000540 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
541 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800542 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000543 }
Andy Green6ee372f2012-04-09 15:09:01 +0800544
Andy Greenb5b23192013-02-11 17:13:32 +0800545 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000546 AF_INET);
547 if (host == NULL) {
548 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800549 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000550 }
551
552 strncpy(name, host->h_name, name_len);
553 name[name_len - 1] = '\0';
554
555 host1 = gethostbyname(host->h_name);
556 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800557 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000558 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000559 n = 0;
560 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000561 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000562 if (p == NULL)
563 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000564 if ((host1->h_addrtype != AF_INET)
565#ifdef AF_LOCAL
566 && (host1->h_addrtype != AF_LOCAL)
567#endif
568 )
Andy Green07034092011-02-13 08:37:12 +0000569 continue;
570
Andy Green7627af52011-03-09 15:13:52 +0000571 if (host1->h_addrtype == AF_INET)
572 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000573#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000574 else {
575 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800576 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000577 ip[sizeof(ip) - 1] = '\0';
578 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000579#endif
Andy Green07034092011-02-13 08:37:12 +0000580 p = NULL;
581 strncpy(rip, ip, rip_len);
582 rip[rip_len - 1] = '\0';
583 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800584
585 ret = 0;
586bail:
587 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000588}
Andy Green9f990342011-02-12 11:57:45 +0000589
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800590LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000591 void *buf, int len)
592{
593 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800594 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000595
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100596#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000597 for (n = 0; n < len; n++)
598 p[n] = (unsigned char)rand();
599#else
600 n = read(context->fd_random, p, len);
601#endif
602
603 return n;
604}
605
Andy Greena690cd02013-02-09 12:25:31 +0800606int lws_set_socket_options(struct libwebsocket_context *context, int fd)
607{
608 int optval = 1;
609 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100610#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800611 unsigned long optl = 0;
612#endif
613#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
614 struct protoent *tcp_proto;
615#endif
616
617 if (context->ka_time) {
618 /* enable keepalive on this socket */
619 optval = 1;
620 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
621 (const void *)&optval, optlen) < 0)
622 return 1;
623
Bob Robertsac049112013-04-25 09:16:30 +0800624#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800625
626 /*
627 * didn't find a way to set these per-socket, need to
628 * tune kernel systemwide values
629 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100630#elif WIN32
631 {
632 DWORD dwBytesRet;
633 struct tcp_keepalive alive;
634 alive.onoff = TRUE;
635 alive.keepalivetime = context->ka_time;
636 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800637
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100638 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
639 NULL, 0, &dwBytesRet, NULL, NULL))
640 return 1;
641 }
Andy Greena47865f2013-02-10 09:39:47 +0800642#else
Andy Greena690cd02013-02-09 12:25:31 +0800643 /* set the keepalive conditions we want on it too */
644 optval = context->ka_time;
645 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
646 (const void *)&optval, optlen) < 0)
647 return 1;
648
Larry Hayesbb66ac62013-02-22 09:16:20 +0800649 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800650 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
651 (const void *)&optval, optlen) < 0)
652 return 1;
653
Larry Hayesbb66ac62013-02-22 09:16:20 +0800654 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800655 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
656 (const void *)&optval, optlen) < 0)
657 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800658#endif
Andy Greena690cd02013-02-09 12:25:31 +0800659 }
660
661 /* Disable Nagle */
662 optval = 1;
663#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
664 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
665#else
666 tcp_proto = getprotobyname("TCP");
667 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
668#endif
669
670 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100671#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800672 ioctlsocket(fd, FIONBIO, &optl);
673#else
674 fcntl(fd, F_SETFL, O_NONBLOCK);
675#endif
676
677 return 0;
678}
679
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800680LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000681{
682 struct pollfd fds;
683
Andy Green2764eba2013-12-09 14:16:17 +0800684 /* treat the fact we got a truncated send pending as if we're choked */
685 if (wsi->truncated_send_malloc)
686 return 1;
687
Andy Green95a7b5d2011-03-06 10:29:39 +0000688 fds.fd = wsi->sock;
689 fds.events = POLLOUT;
690 fds.revents = 0;
691
692 if (poll(&fds, 1, 0) != 1)
693 return 1;
694
695 if ((fds.revents & POLLOUT) == 0)
696 return 1;
697
698 /* okay to send another packet without blocking */
699
700 return 0;
701}
702
Andy Greena41314f2011-05-23 10:00:03 +0100703int
Andy Green3b84c002011-03-06 13:14:42 +0000704lws_handle_POLLOUT_event(struct libwebsocket_context *context,
705 struct libwebsocket *wsi, struct pollfd *pollfd)
706{
Andy Green3b84c002011-03-06 13:14:42 +0000707 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800708
Andy Green3182ece2013-01-20 17:08:31 +0800709#ifndef LWS_NO_EXTENSIONS
710 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000711 int ret;
712 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100713 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000714
Andy Green1f4267b2013-10-17 08:09:19 +0800715 /* pending truncated sends have uber priority */
716
Andy Green2764eba2013-12-09 14:16:17 +0800717 if (wsi->truncated_send_malloc) {
718 lws_issue_raw(wsi, wsi->truncated_send_malloc +
719 wsi->truncated_send_offset,
720 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800721 /* leave POLLOUT active either way */
722 return 0;
723 }
724
Andy Greena41314f2011-05-23 10:00:03 +0100725 for (n = 0; n < wsi->count_active_extensions; n++) {
726 if (!wsi->active_extensions[n]->callback)
727 continue;
728
729 m = wsi->active_extensions[n]->callback(context,
730 wsi->active_extensions[n], wsi,
731 LWS_EXT_CALLBACK_IS_WRITEABLE,
732 wsi->active_extensions_user[n], NULL, 0);
733 if (m > handled)
734 handled = m;
735 }
736
737 if (handled == 1)
738 goto notify_action;
739
740 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000741 goto user_service;
742
743 /*
744 * check in on the active extensions, see if they
745 * had pending stuff to spill... they need to get the
746 * first look-in otherwise sequence will be disordered
747 *
748 * NULL, zero-length eff_buf means just spill pending
749 */
750
751 ret = 1;
752 while (ret == 1) {
753
754 /* default to nobody has more to spill */
755
756 ret = 0;
757 eff_buf.token = NULL;
758 eff_buf.token_len = 0;
759
760 /* give every extension a chance to spill */
761
762 for (n = 0; n < wsi->count_active_extensions; n++) {
763 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000764 wsi->protocol->owning_server,
765 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000766 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
767 wsi->active_extensions_user[n], &eff_buf, 0);
768 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800769 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000770 return -1;
771 }
772 if (m)
773 /*
774 * at least one extension told us he has more
775 * to spill, so we will go around again after
776 */
777 ret = 1;
778 }
779
780 /* assuming they gave us something to send, send it */
781
782 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800783 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
784 eff_buf.token_len);
785 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000786 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800787 /*
788 * Keep amount spilled small to minimize chance of this
789 */
790 if (n != eff_buf.token_len) {
791 lwsl_err("Unable to spill ext %d vs %s\n",
792 eff_buf.token_len, n);
793 return -1;
794 }
Andy Green3b84c002011-03-06 13:14:42 +0000795 } else
796 continue;
797
798 /* no extension has more to spill */
799
800 if (!ret)
801 continue;
802
803 /*
804 * There's more to spill from an extension, but we just sent
805 * something... did that leave the pipe choked?
806 */
807
808 if (!lws_send_pipe_choked(wsi))
809 /* no we could add more */
810 continue;
811
Andy Green43db0452013-01-10 19:50:35 +0800812 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000813
814 /*
815 * Yes, he's choked. Leave the POLLOUT masked on so we will
816 * come back here when he is unchoked. Don't call the user
817 * callback to enforce ordering of spilling, he'll get called
818 * when we come back here and there's nothing more to spill.
819 */
820
821 return 0;
822 }
823
824 wsi->extension_data_pending = 0;
825
826user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800827#endif
Andy Green3b84c002011-03-06 13:14:42 +0000828 /* one shot */
829
Andy Green91f19d82013-12-21 11:18:34 +0800830 if (pollfd)
831 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800832
Andy Green3182ece2013-01-20 17:08:31 +0800833#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100834notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800835#endif
Andy Green3b84c002011-03-06 13:14:42 +0000836
Andy Green9e4c2b62011-03-07 20:47:39 +0000837 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
838 n = LWS_CALLBACK_CLIENT_WRITEABLE;
839 else
840 n = LWS_CALLBACK_SERVER_WRITEABLE;
841
Andy Greenb8b247d2013-01-22 07:20:08 +0800842 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800843 wsi, (enum libwebsocket_callback_reasons) n,
844 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000845}
846
847
848
Andy Green1c6e1422013-02-20 19:11:31 +0800849int
Andy Greena41314f2011-05-23 10:00:03 +0100850libwebsocket_service_timeout_check(struct libwebsocket_context *context,
851 struct libwebsocket *wsi, unsigned int sec)
852{
Andy Green3182ece2013-01-20 17:08:31 +0800853#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100854 int n;
855
856 /*
857 * if extensions want in on it (eg, we are a mux parent)
858 * give them a chance to service child timeouts
859 */
860
861 for (n = 0; n < wsi->count_active_extensions; n++)
862 wsi->active_extensions[n]->callback(
863 context, wsi->active_extensions[n],
864 wsi, LWS_EXT_CALLBACK_1HZ,
865 wsi->active_extensions_user[n], NULL, sec);
866
Andy Green3182ece2013-01-20 17:08:31 +0800867#endif
Andy Greena41314f2011-05-23 10:00:03 +0100868 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800869 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800870
Andy Greena41314f2011-05-23 10:00:03 +0100871 /*
872 * if we went beyond the allowed time, kill the
873 * connection
874 */
875
876 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800877 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100878 libwebsocket_close_and_free_session(context,
879 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800880 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100881 }
Andy Green1c6e1422013-02-20 19:11:31 +0800882
883 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100884}
885
Andy Green9f990342011-02-12 11:57:45 +0000886/**
887 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000888 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000889 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800890 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000891 *
Andy Green75006172013-01-22 12:32:11 +0800892 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800893 * services it according to the state of the associated
894 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800895 *
896 * The one call deals with all "service" that might happen on a socket
897 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800898 *
899 * If a pollfd says it has something, you can just pass it to
900 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
901 * If it sees it is a lws socket, the traffic will be handled and
902 * pollfd->revents will be zeroed now.
903 *
904 * If the socket is foreign to lws, it leaves revents alone. So you can
905 * see if you should service yourself by checking the pollfd revents
906 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000907 */
908
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800909LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000910libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000911 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000912{
Andy Greena1ce6be2013-01-18 11:43:21 +0800913 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000914 int n;
Andy Green0d338332011-02-12 11:57:43 +0000915 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800916 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000917 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800918 int timed_out = 0;
919 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800920 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800921
Andy Green3182ece2013-01-20 17:08:31 +0800922#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000923 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800924#endif
Andy Green98a717c2011-03-06 13:14:15 +0000925 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800926
927 if (context->listen_service_fd)
928 listen_socket_fds_index = context->lws_lookup[
929 context->listen_service_fd]->position_in_fds_table;
930
Andy Greena71eafc2011-02-14 17:59:43 +0000931 /*
932 * you can call us with pollfd = NULL to just allow the once-per-second
933 * global timeout checks; if less than a second since the last check
934 * it returns immediately then.
935 */
936
937 gettimeofday(&tv, NULL);
938
Peter Hinz56885f32011-03-02 22:03:47 +0000939 if (context->last_timeout_check_s != tv.tv_sec) {
940 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000941
Joakim Soderberg4c531232013-02-06 15:26:58 +0900942 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800943 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800944 if (context->started_with_parent &&
945 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800946 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900947 #endif
Andy Green24cba922013-01-19 13:56:10 +0800948
Andy Greena71eafc2011-02-14 17:59:43 +0000949 /* global timeout check once per second */
950
Andy Green1c6e1422013-02-20 19:11:31 +0800951 if (pollfd)
952 our_fd = pollfd->fd;
953
Peter Hinz56885f32011-03-02 22:03:47 +0000954 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800955 m = context->fds[n].fd;
956 wsi = context->lws_lookup[m];
957 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800958 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800959
960 if (libwebsocket_service_timeout_check(context, wsi,
961 tv.tv_sec))
962 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800963 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800964 /* it was the guy we came to service! */
965 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800966 /* mark as handled */
967 pollfd->revents = 0;
968 }
Andy Greena71eafc2011-02-14 17:59:43 +0000969 }
970 }
971
Andy Green1c6e1422013-02-20 19:11:31 +0800972 /* the socket we came to service timed out, nothing to do */
973 if (timed_out)
974 return 0;
975
Andy Greena71eafc2011-02-14 17:59:43 +0000976 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000977 if (pollfd == NULL)
978 return 0;
979
980 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800981 wsi = context->lws_lookup[pollfd->fd];
982 if (wsi == NULL)
983 /* not lws connection ... leave revents alone and return */
984 return 0;
985
986 /*
987 * so that caller can tell we handled, past here we need to
988 * zero down pollfd->revents after handling
989 */
990
Andy Green65b0e912013-01-16 07:59:47 +0800991 /*
992 * deal with listen service piggybacking
993 * every listen_service_modulo services of other fds, we
994 * sneak one in to service the listen socket if there's anything waiting
995 *
996 * To handle connection storms, as found in ab, if we previously saw a
997 * pending connection here, it causes us to check again next time.
998 */
999
Andy Greenb5b23192013-02-11 17:13:32 +08001000 if (context->listen_service_fd && pollfd !=
1001 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +08001002 context->listen_service_count++;
1003 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +08001004 context->listen_service_count ==
1005 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001006 context->listen_service_count = 0;
1007 m = 1;
1008 if (context->listen_service_extraseen > 5)
1009 m = 2;
1010 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001011 /*
1012 * even with extpoll, we prepared this
1013 * internal fds for listen
1014 */
1015 n = poll(&context->fds[listen_socket_fds_index],
1016 1, 0);
1017 if (n > 0) { /* there's a conn waiting for us */
1018 libwebsocket_service_fd(context,
1019 &context->
1020 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001021 context->listen_service_extraseen++;
1022 } else {
1023 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001024 context->
1025 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001026 break;
1027 }
1028 }
1029 }
1030
1031 }
1032
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001033 /* handle session socket closed */
1034
1035 if ((!(pollfd->revents & POLLIN)) &&
1036 (pollfd->revents & (POLLERR | POLLHUP))) {
1037
1038 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1039 (void *)wsi, pollfd->fd);
1040
1041 goto close_and_handled;
1042 }
1043
Andy Green65b0e912013-01-16 07:59:47 +08001044 /* okay, what we came here to do... */
1045
Andy Green0d338332011-02-12 11:57:43 +00001046 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001047
Andy Greena1ce6be2013-01-18 11:43:21 +08001048#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001049 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001050 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001051 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001052 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001053 n = lws_server_socket_service(context, wsi, pollfd);
1054 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001055#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001056
Andy Green0d338332011-02-12 11:57:43 +00001057 case LWS_CONNMODE_WS_SERVING:
1058 case LWS_CONNMODE_WS_CLIENT:
1059
Andy Green0d338332011-02-12 11:57:43 +00001060 /* the guy requested a callback when it was OK to write */
1061
Andy Greenda527df2011-03-07 07:08:12 +00001062 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001063 wsi->state == WSI_STATE_ESTABLISHED &&
1064 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001065 lwsl_info("libwebsocket_service_fd: closing\n");
1066 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001067 }
Andy Green0d338332011-02-12 11:57:43 +00001068
Andy Greenca0a1292013-03-16 11:24:23 +08001069 if (wsi->u.ws.rxflow_buffer &&
1070 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1071 lwsl_info("draining rxflow\n");
1072 /* well, drain it */
1073 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1074 wsi->u.ws.rxflow_pos;
1075 eff_buf.token_len = wsi->u.ws.rxflow_len -
1076 wsi->u.ws.rxflow_pos;
1077 draining_flow = 1;
1078 goto drain;
1079 }
1080
Andy Green0d338332011-02-12 11:57:43 +00001081 /* any incoming data ready? */
1082
1083 if (!(pollfd->revents & POLLIN))
1084 break;
1085
Andy Greenb45993c2010-12-18 15:13:50 +00001086#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001087read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001088 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001089 eff_buf.token_len = SSL_read(wsi->ssl,
1090 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001091 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001092 if (!eff_buf.token_len) {
1093 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001094 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001095 ERR_error_string(n,
1096 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001097 }
1098 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001099#endif
Andy Greene84652c2013-02-08 13:01:02 +08001100 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001101 context->service_buffer,
1102 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001103
Andy Green98a717c2011-03-06 13:14:15 +00001104 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001105 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1106 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001107 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001108 goto close_and_handled;
1109 n = 0;
1110 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001111 }
Andy Green98a717c2011-03-06 13:14:15 +00001112 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001113 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001114 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001115 }
1116
Andy Green98a717c2011-03-06 13:14:15 +00001117 /*
1118 * give any active extensions a chance to munge the buffer
1119 * before parse. We pass in a pointer to an lws_tokens struct
1120 * prepared with the default buffer and content length that's in
1121 * there. Rather than rewrite the default buffer, extensions
1122 * that expect to grow the buffer can adapt .token to
1123 * point to their own per-connection buffer in the extension
1124 * user allocation. By default with no extensions or no
1125 * extension callback handling, just the normal input buffer is
1126 * used then so it is efficient.
1127 */
Andy Greenb45993c2010-12-18 15:13:50 +00001128
Andy Greene84652c2013-02-08 13:01:02 +08001129 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001130drain:
Andy Green3182ece2013-01-20 17:08:31 +08001131#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001132 more = 1;
1133 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001134
Andy Green98a717c2011-03-06 13:14:15 +00001135 more = 0;
1136
1137 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001138 m = wsi->active_extensions[n]->callback(context,
1139 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001140 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001141 wsi->active_extensions_user[n],
1142 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001143 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001144 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001145 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001146 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001147 }
1148 if (m)
1149 more = 1;
1150 }
Andy Green3182ece2013-01-20 17:08:31 +08001151#endif
Andy Green98a717c2011-03-06 13:14:15 +00001152 /* service incoming data */
1153
1154 if (eff_buf.token_len) {
1155 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001156 (unsigned char *)eff_buf.token,
1157 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001158 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001159 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001160 n = 0;
1161 goto handled;
1162 }
Andy Green98a717c2011-03-06 13:14:15 +00001163 }
Andy Green3182ece2013-01-20 17:08:31 +08001164#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001165 eff_buf.token = NULL;
1166 eff_buf.token_len = 0;
1167 }
Andy Green3182ece2013-01-20 17:08:31 +08001168#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001169 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1170 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1171 lwsl_info("flow buffer: drained\n");
1172 free(wsi->u.ws.rxflow_buffer);
1173 wsi->u.ws.rxflow_buffer = NULL;
1174 /* having drained the rxflow buffer, can rearm POLLIN */
1175 _libwebsocket_rx_flow_control(wsi);
1176 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001177
1178#ifdef LWS_OPENSSL_SUPPORT
1179 if (wsi->ssl && SSL_pending(wsi->ssl))
1180 goto read_pending;
1181#endif
Andy Green98a717c2011-03-06 13:14:15 +00001182 break;
Andy Green76f61e72013-01-16 11:53:05 +08001183
1184 default:
Andy Green03674a62013-01-16 11:47:40 +08001185#ifdef LWS_NO_CLIENT
1186 break;
1187#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001188 n = lws_client_socket_service(context, wsi, pollfd);
1189 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001190#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001191 }
1192
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001193 n = 0;
1194 goto handled;
1195
1196close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001197 libwebsocket_close_and_free_session(context, wsi,
1198 LWS_CLOSE_STATUS_NOSTATUS);
1199 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001200
1201handled:
1202 pollfd->revents = 0;
1203 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001204}
1205
Andy Green0d338332011-02-12 11:57:43 +00001206
Andy Green6964bb52011-01-23 16:50:33 +00001207/**
1208 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001209 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001210 *
1211 * This function closes any active connections and then frees the
1212 * context. After calling this, any further use of the context is
1213 * undefined.
1214 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001215LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001216libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001217{
Andy Green0d338332011-02-12 11:57:43 +00001218 int n;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001219#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001220 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001221 struct libwebsocket_extension *ext;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001222#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001223 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001224
Andy Greend636e352013-01-29 12:36:17 +08001225#ifdef LWS_LATENCY
1226 if (context->worst_latency_info[0])
1227 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1228#endif
1229
Andy Greendfb23042013-01-17 12:26:48 +08001230 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001231 struct libwebsocket *wsi =
1232 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001233 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001234 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1235 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001236 }
Andy Green6964bb52011-01-23 16:50:33 +00001237
arnaudviala7c6f26c2014-02-15 14:39:40 +08001238#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001239 /*
1240 * give all extensions a chance to clean up any per-context
1241 * allocations they might have made
1242 */
1243
1244 ext = context->extensions;
1245 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1246 if (context->listen_port)
1247 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001248 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001249 ext->callback(context, ext, NULL,
1250 (enum libwebsocket_extension_callback_reasons)m,
1251 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001252 ext++;
1253 }
arnaudviala7c6f26c2014-02-15 14:39:40 +08001254#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001255
1256 /*
1257 * inform all the protocols that they are done and will have no more
1258 * callbacks
1259 */
1260
1261 while (protocol->callback) {
1262 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1263 NULL, NULL, 0);
1264 protocol++;
1265 }
1266
Andy Greena41314f2011-05-23 10:00:03 +01001267
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001268#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001269#else
1270 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001271#endif
1272
Peter Hinz56885f32011-03-02 22:03:47 +00001273#ifdef LWS_OPENSSL_SUPPORT
1274 if (context->ssl_ctx)
1275 SSL_CTX_free(context->ssl_ctx);
1276 if (context->ssl_client_ctx)
1277 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001278
1279 ERR_remove_state(0);
1280 ERR_free_strings();
1281 EVP_cleanup();
1282 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001283#endif
1284
Andy Green46596482013-02-11 11:04:01 +08001285 if (context->fds)
1286 free(context->fds);
1287 if (context->lws_lookup)
1288 free(context->lws_lookup);
1289
Peter Hinz56885f32011-03-02 22:03:47 +00001290 free(context);
1291
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001292#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001293 WSACleanup();
1294#endif
Andy Green6964bb52011-01-23 16:50:33 +00001295}
1296
Andy Greend88146d2013-01-22 12:40:35 +08001297/**
Andy Greenb5b23192013-02-11 17:13:32 +08001298 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001299 * @context: Websocket context
1300 *
1301 * This returns the optional user allocation that can be attached to
1302 * the context the sockets live in at context_create time. It's a way
1303 * to let all sockets serviced in the same context share data without
1304 * using globals statics in the user code.
1305 */
Alon Levy0291eb32012-10-19 11:21:56 +02001306LWS_EXTERN void *
1307libwebsocket_context_user(struct libwebsocket_context *context)
1308{
Andy Greenb5b23192013-02-11 17:13:32 +08001309 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001310}
1311
Andy Green6964bb52011-01-23 16:50:33 +00001312/**
1313 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001314 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001315 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1316 * service otherwise block and service immediately, returning
1317 * after the timeout if nothing needed service.
1318 *
1319 * This function deals with any pending websocket traffic, for three
1320 * kinds of event. It handles these events on both server and client
1321 * types of connection the same.
1322 *
1323 * 1) Accept new connections to our context's server
1324 *
Andy Green6f520a52013-01-29 17:57:39 +08001325 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001326 * server or client connections.
1327 *
1328 * You need to call this service function periodically to all the above
1329 * functions to happen; if your application is single-threaded you can
1330 * just call it in your main event loop.
1331 *
1332 * Alternatively you can fork a new process that asynchronously handles
1333 * calling this service in a loop. In that case you are happy if this
1334 * call blocks your thread until it needs to take care of something and
1335 * would call it with a large nonzero timeout. Your loop then takes no
1336 * CPU while there is nothing happening.
1337 *
1338 * If you are calling it in a single-threaded app, you don't want it to
1339 * wait around blocking other things in your loop from happening, so you
1340 * would call it with a timeout_ms of 0, so it returns immediately if
1341 * nothing is pending, or as soon as it services whatever was pending.
1342 */
1343
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001344LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001345libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001346{
1347 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001348 int m;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001349#ifdef LWS_HAS_PPOLL
1350 struct timespec timeout_ts;
1351 sigset_t sigmask;
1352#endif
Andy Greene92cd172011-01-19 13:11:55 +00001353
1354 /* stay dead once we are dead */
1355
Peter Hinz56885f32011-03-02 22:03:47 +00001356 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001357 return 1;
1358
Andy Green3b3fa9e2013-12-25 16:34:37 +08001359#ifdef LWS_HAS_PPOLL
1360 lws_idling_ppoll_tid = context->protocols[0].callback(context, NULL,
1361 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1362
1363 timeout_ts.tv_sec = timeout_ms / 1000;
1364 timeout_ts.tv_nsec = timeout_ms % 1000;
Andy Greened451d52014-01-11 12:37:07 +08001365
1366 sigprocmask(SIG_BLOCK, NULL, &sigmask);
1367 sigdelset(&sigmask, SIGUSR2);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001368
Andy Green0d338332011-02-12 11:57:43 +00001369 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001370
Andy Green3b3fa9e2013-12-25 16:34:37 +08001371 n = ppoll(context->fds, context->fds_count, &timeout_ts, &sigmask);
1372 lws_idling_ppoll_tid = 0;
1373#else
Peter Hinz56885f32011-03-02 22:03:47 +00001374 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001375#endif
Andy Green5dc62ea2013-09-20 20:26:12 +08001376 if (n == 0) /* poll timeout */ {
1377 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001378 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001379 }
Andy Greene92cd172011-01-19 13:11:55 +00001380
Andy Greened451d52014-01-11 12:37:07 +08001381 if (n < 0) {
1382 if (errno != EINTR)
1383 return -1;
1384 else
1385 return 0;
1386 }
Andy Greene92cd172011-01-19 13:11:55 +00001387
Andy Greendfb23042013-01-17 12:26:48 +08001388 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001389
Andy Greenca0a1292013-03-16 11:24:23 +08001390 for (n = 0; n < context->fds_count; n++) {
1391 if (!context->fds[n].revents)
1392 continue;
1393 m = libwebsocket_service_fd(context, &context->fds[n]);
1394 if (m < 0)
1395 return -1;
1396 /* if something closed, retry this slot */
1397 if (m)
1398 n--;
1399 }
1400
Andy Greene92cd172011-01-19 13:11:55 +00001401 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001402}
1403
Andy Green3182ece2013-01-20 17:08:31 +08001404#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001405int
1406lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001407 struct libwebsocket *wsi,
1408 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001409 void *v, size_t len)
1410{
1411 int n;
1412 int handled = 0;
1413
1414 /* maybe an extension will take care of it for us */
1415
1416 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1417 if (!wsi->active_extensions[n]->callback)
1418 continue;
1419
1420 handled |= wsi->active_extensions[n]->callback(context,
1421 wsi->active_extensions[n], wsi,
1422 r, wsi->active_extensions_user[n], v, len);
1423 }
1424
1425 return handled;
1426}
1427
1428
1429void *
1430lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001431 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001432{
1433 int n = 0;
1434
Andy Green68b45042011-05-25 21:41:57 +01001435 if (wsi == NULL)
1436 return NULL;
1437
Andy Greena41314f2011-05-23 10:00:03 +01001438 while (n < wsi->count_active_extensions) {
1439 if (wsi->active_extensions[n] != ext) {
1440 n++;
1441 continue;
1442 }
1443 return wsi->active_extensions_user[n];
1444 }
1445
1446 return NULL;
1447}
Andy Green3182ece2013-01-20 17:08:31 +08001448#endif
Andy Greena41314f2011-05-23 10:00:03 +01001449
Andy Green91f19d82013-12-21 11:18:34 +08001450void
1451lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1452{
1453 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001454 int events;
1455#ifdef LWS_HAS_PPOLL
1456 int tid;
1457 int sampled_ppoll_tid;
1458#endif
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001459 struct libwebsocket_pollargs pa;
1460
1461 pa.fd = wsi->sock;
Andy Green91f19d82013-12-21 11:18:34 +08001462
1463 context->protocols[0].callback(context, wsi,
1464 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001465 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001466
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001467 pa.prev_events = events = context->fds[wsi->position_in_fds_table].events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001468
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001469 pa.events = context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001470
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001471 context->protocols[0].callback(context, wsi,
1472 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
1473 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001474
Andy Green91f19d82013-12-21 11:18:34 +08001475
Andy Green3b3fa9e2013-12-25 16:34:37 +08001476#ifdef LWS_HAS_PPOLL
1477 /*
1478 * if we changed something in this pollfd...
1479 * ... and we're running in a different thread context
1480 * than the service thread...
1481 * ... and the service thread is waiting in ppoll()...
1482 * then fire a SIGUSR2 at the service thread to force it to
1483 * restart the ppoll() with our changed events
1484 */
1485 if (events != context->fds[wsi->position_in_fds_table].events) {
1486 sampled_ppoll_tid = lws_idling_ppoll_tid;
1487 if (sampled_ppoll_tid) {
1488 tid = context->protocols[0].callback(context, NULL,
1489 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1490 if (tid != sampled_ppoll_tid)
1491 kill(sampled_ppoll_tid, SIGUSR2);
1492 }
1493 }
1494#endif
1495
Andy Green91f19d82013-12-21 11:18:34 +08001496 context->protocols[0].callback(context, wsi,
1497 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001498 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001499}
1500
1501
Andy Green90c7cbc2011-01-27 06:26:52 +00001502/**
1503 * libwebsocket_callback_on_writable() - Request a callback when this socket
1504 * becomes able to be written to without
1505 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001506 *
Peter Hinz56885f32011-03-02 22:03:47 +00001507 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001508 * @wsi: Websocket connection instance to get callback for
1509 */
1510
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001511LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001512libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001513 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001514{
Andy Green3182ece2013-01-20 17:08:31 +08001515#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001516 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001517 int handled = 0;
1518
1519 /* maybe an extension will take care of it for us */
1520
1521 for (n = 0; n < wsi->count_active_extensions; n++) {
1522 if (!wsi->active_extensions[n]->callback)
1523 continue;
1524
1525 handled |= wsi->active_extensions[n]->callback(context,
1526 wsi->active_extensions[n], wsi,
1527 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1528 wsi->active_extensions_user[n], NULL, 0);
1529 }
1530
1531 if (handled)
1532 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001533#endif
Andy Greendfb23042013-01-17 12:26:48 +08001534 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001535 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1536 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001537 return -1;
1538 }
1539
Andy Green91f19d82013-12-21 11:18:34 +08001540 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001541
Andy Green90c7cbc2011-01-27 06:26:52 +00001542 return 1;
1543}
1544
1545/**
1546 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1547 * all connections using the given protocol when it
1548 * becomes possible to write to each socket without
1549 * blocking in turn.
1550 *
1551 * @protocol: Protocol whose connections will get callbacks
1552 */
1553
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001554LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001555libwebsocket_callback_on_writable_all_protocol(
1556 const struct libwebsocket_protocols *protocol)
1557{
Peter Hinz56885f32011-03-02 22:03:47 +00001558 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001559 int n;
Andy Green0d338332011-02-12 11:57:43 +00001560 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001561
Andy Greendfb23042013-01-17 12:26:48 +08001562 for (n = 0; n < context->fds_count; n++) {
1563 wsi = context->lws_lookup[context->fds[n].fd];
1564 if (!wsi)
1565 continue;
1566 if (wsi->protocol == protocol)
1567 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001568 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001569
1570 return 0;
1571}
1572
Andy Greenbe93fef2011-02-14 20:25:43 +00001573/**
Andy Greene39e6ef2014-02-15 16:36:38 +08001574 * libwebsocket_callback_all_protocol() - Callback all connections using
1575 * the given protocol with the given reason
1576 *
1577 * @protocol: Protocol whose connections will get callbacks
1578 * @reason: Callback reason index
1579 */
1580
1581LWS_VISIBLE int
1582libwebsocket_callback_all_protocol(
1583 const struct libwebsocket_protocols *protocol, int reason)
1584{
1585 struct libwebsocket_context *context = protocol->owning_server;
1586 int n;
1587 struct libwebsocket *wsi;
1588
1589 for (n = 0; n < context->fds_count; n++) {
1590 wsi = context->lws_lookup[context->fds[n].fd];
1591 if (!wsi)
1592 continue;
1593 if (wsi->protocol == protocol)
1594 protocol->callback(context, wsi,
1595 reason, wsi->user_space, NULL, 0);
1596 }
1597
1598 return 0;
1599}
1600
1601/**
Andy Greenbe93fef2011-02-14 20:25:43 +00001602 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1603 *
1604 * You will not need this unless you are doing something special
1605 *
1606 * @wsi: Websocket connection instance
1607 * @reason: timeout reason
1608 * @secs: how many seconds
1609 */
1610
Craig McQueene7fd8b12014-02-19 09:24:17 +11001611LWS_VISIBLE void
Andy Greenbe93fef2011-02-14 20:25:43 +00001612libwebsocket_set_timeout(struct libwebsocket *wsi,
1613 enum pending_timeout reason, int secs)
1614{
1615 struct timeval tv;
1616
1617 gettimeofday(&tv, NULL);
1618
1619 wsi->pending_timeout_limit = tv.tv_sec + secs;
1620 wsi->pending_timeout = reason;
1621}
1622
Andy Greena6cbece2011-01-27 20:06:03 +00001623
1624/**
1625 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1626 *
1627 * You will not need this unless you are doing something special
1628 *
1629 * @wsi: Websocket connection instance
1630 */
1631
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001632LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001633libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1634{
1635 return wsi->sock;
1636}
1637
Andy Greend636e352013-01-29 12:36:17 +08001638#ifdef LWS_LATENCY
1639void
Andy Greenb5b23192013-02-11 17:13:32 +08001640lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1641 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001642{
1643 struct timeval tv;
1644 unsigned long u;
1645 char buf[256];
1646
1647 gettimeofday(&tv, NULL);
1648
1649 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1650
1651 if (action) {
1652 if (completed) {
1653 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001654 sprintf(buf,
1655 "Completion first try lat %luus: %p: ret %d: %s\n",
1656 u - wsi->latency_start,
1657 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001658 else
Andy Greenb5b23192013-02-11 17:13:32 +08001659 sprintf(buf,
1660 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1661 u - wsi->action_start,
1662 u - wsi->latency_start,
1663 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001664 wsi->action_start = 0;
1665 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001666 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1667 u - wsi->latency_start,
1668 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001669 if (u - wsi->latency_start > context->worst_latency) {
1670 context->worst_latency = u - wsi->latency_start;
1671 strcpy(context->worst_latency_info, buf);
1672 }
1673 lwsl_latency("%s", buf);
1674 } else {
1675 wsi->latency_start = u;
1676 if (!wsi->action_start)
1677 wsi->action_start = u;
1678 }
1679}
1680#endif
1681
Andy Greena1ce6be2013-01-18 11:43:21 +08001682#ifdef LWS_NO_SERVER
1683int
Andy Green8d15cf42013-10-24 21:47:06 +08001684_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001685{
1686 return 0;
1687}
1688#else
Andy Green706961d2013-01-17 16:50:35 +08001689int
1690_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1691{
1692 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001693
Andy Greenca0a1292013-03-16 11:24:23 +08001694 /* there is no pending change */
1695 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001696 return 0;
1697
Andy Greenca0a1292013-03-16 11:24:23 +08001698 /* stuff is still buffered, not ready to really accept new input */
1699 if (wsi->u.ws.rxflow_buffer) {
1700 /* get ourselves called back to deal with stashed buffer */
1701 libwebsocket_callback_on_writable(context, wsi);
1702 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001703 }
1704
Andy Greenca0a1292013-03-16 11:24:23 +08001705 /* pending is cleared, we can change rxflow state */
1706
1707 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1708
1709 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1710 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1711
1712 /* adjust the pollfd for this wsi */
1713
1714 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001715 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001716 else
Andy Green91f19d82013-12-21 11:18:34 +08001717 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001718
Andy Green706961d2013-01-17 16:50:35 +08001719 return 1;
1720}
Andy Greena1ce6be2013-01-18 11:43:21 +08001721#endif
Andy Green706961d2013-01-17 16:50:35 +08001722
Andy Green90c7cbc2011-01-27 06:26:52 +00001723/**
1724 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1725 * receieved packets.
1726 *
1727 * If the output side of a server process becomes choked, this allows flow
1728 * control for the input side.
1729 *
1730 * @wsi: Websocket connection instance to get callback for
1731 * @enable: 0 = disable read servicing for this connection, 1 = enable
1732 */
1733
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001734LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001735libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1736{
Andy Greenca0a1292013-03-16 11:24:23 +08001737 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1738 return 0;
1739
1740 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1741 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001742
Andy Green706961d2013-01-17 16:50:35 +08001743 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001744}
1745
Andy Greenb55451c2013-03-16 12:32:27 +08001746/**
1747 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1748 *
1749 * When the user server code realizes it can accept more input, it can
1750 * call this to have the RX flow restriction removed from all connections using
1751 * the given protocol.
1752 *
1753 * @protocol: all connections using this protocol will be allowed to receive
1754 */
1755
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001756LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001757libwebsocket_rx_flow_allow_all_protocol(
1758 const struct libwebsocket_protocols *protocol)
1759{
1760 struct libwebsocket_context *context = protocol->owning_server;
1761 int n;
1762 struct libwebsocket *wsi;
1763
1764 for (n = 0; n < context->fds_count; n++) {
1765 wsi = context->lws_lookup[context->fds[n].fd];
1766 if (!wsi)
1767 continue;
1768 if (wsi->protocol == protocol)
1769 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1770 }
1771}
1772
Andy Green706961d2013-01-17 16:50:35 +08001773
Andy Green2ac5a6f2011-01-28 10:00:18 +00001774/**
1775 * libwebsocket_canonical_hostname() - returns this host's hostname
1776 *
1777 * This is typically used by client code to fill in the host parameter
1778 * when making a client connection. You can only call it after the context
1779 * has been created.
1780 *
Peter Hinz56885f32011-03-02 22:03:47 +00001781 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001782 */
1783
1784
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001785LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001786libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001787{
Peter Hinz56885f32011-03-02 22:03:47 +00001788 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001789}
1790
1791
Andy Green90c7cbc2011-01-27 06:26:52 +00001792static void sigpipe_handler(int x)
1793{
1794}
1795
Andy Green6901cb32011-02-21 08:06:47 +00001796#ifdef LWS_OPENSSL_SUPPORT
1797static int
1798OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1799{
1800
1801 SSL *ssl;
1802 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001803 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001804
1805 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1806 SSL_get_ex_data_X509_STORE_CTX_idx());
1807
1808 /*
Andy Green2e24da02011-03-05 16:12:04 +00001809 * !!! nasty openssl requires the index to come as a library-scope
1810 * static
Andy Green6901cb32011-02-21 08:06:47 +00001811 */
Andy Green2e24da02011-03-05 16:12:04 +00001812 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001813
Peter Hinz56885f32011-03-02 22:03:47 +00001814 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001815 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1816 x509_ctx, ssl, preverify_ok);
1817
1818 /* convert return code from 0 = OK to 1 = OK */
1819
1820 if (!n)
1821 n = 1;
1822 else
1823 n = 0;
1824
1825 return n;
1826}
1827#endif
1828
Andy Green706961d2013-01-17 16:50:35 +08001829int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001830 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001831 struct libwebsocket *wsi,
1832 enum libwebsocket_callback_reasons reason, void *user,
1833 void *in, size_t len)
1834{
1835 int n;
1836
1837 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001838 if (!n)
1839 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001840
Andy Greenaedc9532013-02-10 21:21:24 +08001841 return n;
Andy Green706961d2013-01-17 16:50:35 +08001842}
1843
Andy Green3b3fa9e2013-12-25 16:34:37 +08001844/*
1845 * This is just used to interrupt poll waiting
1846 * we don't have to do anything with it.
1847 */
arnaudviala7c6f26c2014-02-15 14:39:40 +08001848#ifdef LWS_OPENSSL_SUPPORT
Andy Green3b3fa9e2013-12-25 16:34:37 +08001849static void lws_sigusr2(int sig)
1850{
1851}
arnaudviala7c6f26c2014-02-15 14:39:40 +08001852#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001853
Andy Greenab990e42010-10-31 12:42:52 +00001854/**
Andy Green4739e5c2011-01-22 12:51:57 +00001855 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001856 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001857 *
Andy Green1b265272013-02-09 14:01:09 +08001858 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001859 * of all initialization in one step.
1860 *
Andy Greene92cd172011-01-19 13:11:55 +00001861 * After initialization, it returns a struct libwebsocket_context * that
1862 * represents this server. After calling, user code needs to take care
1863 * of calling libwebsocket_service() with the context pointer to get the
1864 * server's sockets serviced. This can be done in the same process context
1865 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001866 *
Andy Green8f037e42010-12-19 22:13:26 +00001867 * The protocol callback functions are called for a handful of events
1868 * including http requests coming in, websocket connections becoming
1869 * established, and data arriving; it's also called periodically to allow
1870 * async transmission.
1871 *
1872 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1873 * at that time websocket protocol has not been negotiated. Other
1874 * protocols after the first one never see any HTTP callack activity.
1875 *
1876 * The server created is a simple http server by default; part of the
1877 * websocket standard is upgrading this http connection to a websocket one.
1878 *
1879 * This allows the same server to provide files like scripts and favicon /
1880 * images or whatever over http and dynamic data over websockets all in
1881 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001882 */
Andy Green4ea60062010-10-30 12:15:07 +01001883
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001884LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001885libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001886{
Peter Hinz56885f32011-03-02 22:03:47 +00001887 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001888 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001889 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001890#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001891 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001892 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001893 struct sockaddr_in serv_addr;
1894#endif
Andy Green3182ece2013-01-20 17:08:31 +08001895#ifndef LWS_NO_EXTENSIONS
1896 int m;
Andy Green1b265272013-02-09 14:01:09 +08001897 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001898#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001899
Andy Green3faa9c72010-11-08 17:03:03 +00001900#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001901 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001902#endif
1903
Joakim Soderberg4c531232013-02-06 15:26:58 +09001904#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001905 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001906#endif
1907
Andy Greenb3a614a2013-01-19 13:08:17 +08001908 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001909 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001910 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001911 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001912#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001913 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1914 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001915#else
1916 lwsl_notice(" Configured without extension support\n");
1917#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001918 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1919 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001920 if (info->ssl_cipher_list)
1921 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001922 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1923 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001924
Peter Hinz56885f32011-03-02 22:03:47 +00001925#ifdef _WIN32
1926 {
1927 WORD wVersionRequested;
1928 WSADATA wsaData;
1929 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001930 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001931
1932 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1933 wVersionRequested = MAKEWORD(2, 2);
1934
1935 err = WSAStartup(wVersionRequested, &wsaData);
1936 if (err != 0) {
1937 /* Tell the user that we could not find a usable */
1938 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001939 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001940 return NULL;
1941 }
David Galeano7b11fec2011-10-04 19:55:18 +08001942
Andy Green6ee372f2012-04-09 15:09:01 +08001943 /* default to a poll() made out of select() */
1944 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001945
Andy Green6ee372f2012-04-09 15:09:01 +08001946 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001947 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001948 if (wsdll)
1949 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001950
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001951 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001952 if (!poll)
1953 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001954 }
1955#endif
1956
Andy Greenb5b23192013-02-11 17:13:32 +08001957 context = (struct libwebsocket_context *)
1958 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001959 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001960 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001961 return NULL;
1962 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001963 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001964#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001965 context->started_with_parent = pid_daemon;
1966 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1967#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001968
Joakim Soderberg4c531232013-02-06 15:26:58 +09001969 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001970 context->protocols = info->protocols;
1971 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001972 context->http_proxy_port = 0;
1973 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001974 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001975 /* to reduce this allocation, */
1976 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001977 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1978 sizeof(struct libwebsocket_context),
1979 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1980 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001981 sizeof(struct libwebsocket_context) +
1982 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1983 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001984
Andy Greenb5b23192013-02-11 17:13:32 +08001985 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1986 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001987 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001988 lwsl_err("Unable to allocate fds array for %d connections\n",
1989 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001990 free(context);
1991 return NULL;
1992 }
Andy Greenb5b23192013-02-11 17:13:32 +08001993 context->lws_lookup = (struct libwebsocket **)
1994 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001995 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001996 lwsl_err(
1997 "Unable to allocate lws_lookup array for %d connections\n",
1998 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001999 free(context->fds);
2000 free(context);
2001 return NULL;
2002 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08002003 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
2004 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08002005
Peter Hinz56885f32011-03-02 22:03:47 +00002006 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002007#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08002008 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08002009#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002010 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08002011 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00002012
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002013#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002014 context->fd_random = 0;
2015#else
2016 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2017 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002018 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002019 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002020 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00002021 }
Peter Hinz56885f32011-03-02 22:03:47 +00002022#endif
Andy Green44eee682011-02-10 09:32:24 +00002023
Peter Hinz56885f32011-03-02 22:03:47 +00002024#ifdef LWS_OPENSSL_SUPPORT
2025 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08002026 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002027 context->ssl_ctx = NULL;
2028 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002029 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002030#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002031
Andy Greena1ce6be2013-01-18 11:43:21 +08002032 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002033
Andy Greena1ce6be2013-01-18 11:43:21 +08002034#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002035 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01002036 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08002037 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08002038 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01002039
Andy Greenb5b23192013-02-11 17:13:32 +08002040 lwsl_notice(" canonical_hostname = %s\n",
2041 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002042 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002043#endif
Andy Greena69f0512012-05-03 12:32:38 +08002044
Andy Green9659f372011-01-27 22:01:43 +00002045 /* split the proxy ads:port if given */
2046
2047 p = getenv("http_proxy");
2048 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00002049 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08002050 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002051 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002052 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002053
Peter Hinz56885f32011-03-02 22:03:47 +00002054 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00002055 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002056 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002057 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00002058 }
2059 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00002060 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002061
Andy Greenb3a614a2013-01-19 13:08:17 +08002062 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002063 context->http_proxy_address,
2064 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002065 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002066
Andy Greena1ce6be2013-01-18 11:43:21 +08002067#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002068 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002069
Andy Green3faa9c72010-11-08 17:03:03 +00002070#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002071 context->use_ssl = info->ssl_cert_filepath != NULL &&
2072 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002073#ifdef USE_CYASSL
2074 lwsl_notice(" Compiled with CYASSL support\n");
2075#else
2076 lwsl_notice(" Compiled with OpenSSL support\n");
2077#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002078 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002079 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002080 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002081 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002082
Andy Green90c7cbc2011-01-27 06:26:52 +00002083#else
Andy Green2b40b792013-02-10 22:22:01 +08002084 if (info->ssl_cert_filepath != NULL &&
2085 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002086 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002087 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002088 }
Andy Greenb5b23192013-02-11 17:13:32 +08002089 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002090#endif
Andy Greena17c6922013-01-20 20:21:54 +08002091
Andy Greenb5b23192013-02-11 17:13:32 +08002092 lwsl_notice(
2093 " per-conn mem: %u + %u headers + protocol rx buf\n",
2094 sizeof(struct libwebsocket),
2095 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002096 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002097#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002098
2099 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002100#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002101#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002102 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002103#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002104
2105
2106#ifdef LWS_OPENSSL_SUPPORT
2107
2108 /* basic openssl init */
2109
2110 SSL_library_init();
2111
2112 OpenSSL_add_all_algorithms();
2113 SSL_load_error_strings();
2114
Andy Green2e24da02011-03-05 16:12:04 +00002115 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002116 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2117
Andy Green90c7cbc2011-01-27 06:26:52 +00002118 /*
2119 * Firefox insists on SSLv23 not SSLv3
2120 * Konq disables SSLv2 by default now, SSLv23 works
2121 */
2122
2123 method = (SSL_METHOD *)SSLv23_server_method();
2124 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002125 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002126 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002127 error,
2128 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002129 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002130 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002131 }
Peter Hinz56885f32011-03-02 22:03:47 +00002132 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2133 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002134 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002135 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002136 error,
2137 ERR_error_string(error,
2138 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002139 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002140 }
2141
martell19c73f32014-01-22 18:17:03 +00002142#if defined(WIN32) || defined(_WIN32)
2143#else
Andy Green3b3fa9e2013-12-25 16:34:37 +08002144 signal(SIGUSR2, lws_sigusr2);
Andy Greened451d52014-01-11 12:37:07 +08002145 {
2146 sigset_t mask;
2147 sigemptyset (&mask);
2148 sigaddset (&mask, SIGUSR2);
2149
2150 sigprocmask(SIG_BLOCK, &mask, NULL);
2151 }
martell19c73f32014-01-22 18:17:03 +00002152#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002153
David Galeanocc148e42013-01-10 10:18:59 +08002154#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002155 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002156#endif
David Galeano77a677c2013-01-10 10:14:12 +08002157 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002158 if (info->ssl_cipher_list)
2159 SSL_CTX_set_cipher_list(context->ssl_ctx,
2160 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002161
Andy Greena1ce6be2013-01-18 11:43:21 +08002162#ifndef LWS_NO_CLIENT
2163
Andy Green90c7cbc2011-01-27 06:26:52 +00002164 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002165
Andy Green1b265272013-02-09 14:01:09 +08002166 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002167 method = (SSL_METHOD *)SSLv23_client_method();
2168 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002169 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002170 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002171 error,
2172 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002173 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002174 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002175 }
2176 /* create context */
2177 context->ssl_client_ctx = SSL_CTX_new(method);
2178 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002179 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002180 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002181 error,
2182 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002183 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002184 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002185 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002186
David Galeanocc148e42013-01-10 10:18:59 +08002187#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002188 SSL_CTX_set_options(context->ssl_client_ctx,
2189 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002190#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002191 SSL_CTX_set_options(context->ssl_client_ctx,
2192 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002193 if (info->ssl_cipher_list)
2194 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2195 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002196
Peter Hinz56885f32011-03-02 22:03:47 +00002197 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002198 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002199 if (!SSL_CTX_load_verify_locations(
2200 context->ssl_client_ctx, NULL,
2201 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002202 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002203 "Unable to load SSL Client certs from %s "
2204 "(set by --with-client-cert-dir= "
2205 "in configure) -- client ssl isn't "
2206 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002207 } else
2208 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002209 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002210 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002211 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002212 "Unable to load SSL Client certs "
2213 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002214 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002215
2216 /*
2217 * callback allowing user code to load extra verification certs
2218 * helping the client to verify server identity
2219 */
2220
prasannateamf1b353a452013-11-12 17:21:01 -08002221 /* support for client-side certificate authentication */
2222 if (info->ssl_cert_filepath) {
2223 n = SSL_CTX_use_certificate_chain_file(
2224 context->ssl_client_ctx,
2225 info->ssl_cert_filepath);
2226 if (n != 1) {
2227 lwsl_err("problem getting cert '%s' %lu: %s\n",
2228 info->ssl_cert_filepath,
2229 ERR_get_error(),
2230 ERR_error_string(ERR_get_error(),
2231 (char *)context->service_buffer));
2232 goto bail;
2233 }
2234 }
2235 if (info->ssl_private_key_filepath) {
2236 /* set the private key from KeyFile */
2237 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2238 info->ssl_private_key_filepath,
2239 SSL_FILETYPE_PEM) != 1) {
2240 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2241 info->ssl_private_key_filepath,
2242 ERR_get_error(),
2243 ERR_error_string(ERR_get_error(),
2244 (char *)context->service_buffer));
2245 goto bail;
2246 }
2247
2248 /* verify private key */
2249 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2250 lwsl_err("Private SSL key doesn't match cert\n");
2251 goto bail;
2252 }
2253 }
2254
Peter Hinz56885f32011-03-02 22:03:47 +00002255 context->protocols[0].callback(context, NULL,
2256 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2257 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002258 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002259#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002260
Andy Greenc6bf2c22011-02-20 11:10:47 +00002261 /* as a server, are we requiring clients to identify themselves? */
2262
Andy Greenb5b23192013-02-11 17:13:32 +08002263 if (info->options &
2264 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002265
2266 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002267
Peter Hinz56885f32011-03-02 22:03:47 +00002268 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002269 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2270 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002271
2272 /*
2273 * give user code a chance to load certs into the server
2274 * allowing it to verify incoming client certs
2275 */
2276
Peter Hinz56885f32011-03-02 22:03:47 +00002277 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002278 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002279 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002280 }
2281
James Devine5b34c972013-12-14 11:41:29 +08002282 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2283 /* Normally SSL listener rejects non-ssl, optionally allow */
2284 context->allow_non_ssl_on_ssl_port = 1;
2285 }
2286
Peter Hinz56885f32011-03-02 22:03:47 +00002287 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002288
2289 /* openssl init for server sockets */
2290
Andy Green3faa9c72010-11-08 17:03:03 +00002291 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002292 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002293 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002294 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002295 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002296 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002297 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002298 error,
2299 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002300 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002301 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002302 }
2303 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002304 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002305 info->ssl_private_key_filepath,
2306 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002307 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002308 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002309 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002310 error,
2311 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002312 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002313 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002314 }
2315 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002316 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002317 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002318 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002319 }
2320
2321 /* SSL is happy and has a cert it's content with */
2322 }
2323#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002324
Andy Greena1ce6be2013-01-18 11:43:21 +08002325#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002326 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002327
Andy Green1b265272013-02-09 14:01:09 +08002328 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002329 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002330
Andy Green4739e5c2011-01-22 12:51:57 +00002331 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2332 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002333 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002334 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002335 }
Andy Green775c0dd2010-10-29 14:15:22 +01002336
Joakim Soderberg4c531232013-02-06 15:26:58 +09002337#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002338 /*
2339 * allow us to restart even if old sockets in TIME_WAIT
2340 * (REUSEADDR on Unix means, "don't hang on to this
2341 * address after the listener is closed." On Windows, though,
2342 * it means "don't keep other processes from binding to
2343 * this address while we're using it)
2344 */
Andy Green6ee372f2012-04-09 15:09:01 +08002345 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2346 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002347#endif
Andy Green6c939552011-03-08 08:56:57 +00002348
2349 /* Disable Nagle */
2350 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002351 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2352 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002353
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002354 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002355 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002356 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002357 #else
Andy Greene2160712013-01-28 12:19:10 +08002358 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002359 #endif
Andy Greene2160712013-01-28 12:19:10 +08002360
Andy Green4739e5c2011-01-22 12:51:57 +00002361 bzero((char *) &serv_addr, sizeof(serv_addr));
2362 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002363 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002364 serv_addr.sin_addr.s_addr = INADDR_ANY;
2365 else
Andy Greend1eac602013-11-09 08:07:38 +08002366 if (interface_to_sa(info->iface, &serv_addr,
2367 sizeof(serv_addr)) < 0) {
2368 lwsl_err("Unable to find interface %s\n",
2369 info->iface);
2370 compatible_close(sockfd);
2371 goto bail;
2372 }
Andy Green1b265272013-02-09 14:01:09 +08002373 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002374
2375 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2376 sizeof(serv_addr));
2377 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002378 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002379 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002380 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002381 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002382 }
Andy Green0d338332011-02-12 11:57:43 +00002383
Andy Greenb5b23192013-02-11 17:13:32 +08002384 wsi = (struct libwebsocket *)malloc(
2385 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002386 if (wsi == NULL) {
2387 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002388 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002389 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002390 }
Andy Greenb5b23192013-02-11 17:13:32 +08002391 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002392 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002393#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002394 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002395#endif
Andy Green0d338332011-02-12 11:57:43 +00002396 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002397
2398 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002399
Andy Green65b0e912013-01-16 07:59:47 +08002400 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2401 context->listen_service_count = 0;
2402 context->listen_service_fd = sockfd;
2403
Andy Greena824d182013-01-15 20:52:29 +08002404 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002405 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002406 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002407#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002408
Andy Green6ee372f2012-04-09 15:09:01 +08002409 /*
2410 * drop any root privs for this process
2411 * to listen on port < 1023 we would have needed root, but now we are
2412 * listening, we don't want the power for anything else
2413 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002414#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002415#else
Andy Green1b265272013-02-09 14:01:09 +08002416 if (info->gid != -1)
2417 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002418 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002419 if (info->uid != -1)
2420 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002421 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002422#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002423
Andy Green6f520a52013-01-29 17:57:39 +08002424 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002425
Peter Hinz56885f32011-03-02 22:03:47 +00002426 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002427 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002428 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002429
Andy Green43db0452013-01-10 19:50:35 +08002430 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002431 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002432
Andy Green1b265272013-02-09 14:01:09 +08002433 info->protocols[context->count_protocols].owning_server =
2434 context;
2435 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002436 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002437
2438 /*
2439 * inform all the protocols that they are doing their one-time
2440 * initialization if they want to
2441 */
2442 info->protocols[context->count_protocols].callback(context,
2443 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002444 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002445
Andy Green3182ece2013-01-20 17:08:31 +08002446#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002447 /*
2448 * give all extensions a chance to create any per-context
2449 * allocations they need
2450 */
2451
2452 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002453 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002454 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002455
Andy Green1b265272013-02-09 14:01:09 +08002456 if (info->extensions) {
2457 ext = info->extensions;
2458 while (ext->callback) {
2459 lwsl_ext(" Extension: %s\n", ext->name);
2460 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002461 (enum libwebsocket_extension_callback_reasons)m,
2462 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002463 ext++;
2464 }
Andy Greena41314f2011-05-23 10:00:03 +01002465 }
Andy Green3182ece2013-01-20 17:08:31 +08002466#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002467
Peter Hinz56885f32011-03-02 22:03:47 +00002468 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002469
2470bail:
2471 libwebsocket_context_destroy(context);
2472 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002473}
Andy Greenb45993c2010-12-18 15:13:50 +00002474
Andy Greenb45993c2010-12-18 15:13:50 +00002475/**
shysb4e800e2013-10-24 22:12:03 +08002476 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2477 * @context: pointer to struct libwebsocket_context you want set proxy to
2478 * @proxy: pointer to c string containing proxy in format address:port
2479 *
2480 * Returns 0 if proxy string was parsed and proxy was setup.
2481 * Returns -1 if @proxy is NULL or has incorrect format.
2482 *
2483 * This is only required if your OS does not provide the http_proxy
2484 * enviroment variable (eg, OSX)
2485 *
2486 * IMPORTANT! You should call this function right after creation of the
2487 * libwebsocket_context and before call to connect. If you call this
2488 * function after connect behavior is undefined.
2489 * This function will override proxy settings made on libwebsocket_context
2490 * creation with genenv() call.
2491 */
2492
2493LWS_VISIBLE int
2494libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2495{
2496 char *p;
2497
2498 if (!proxy)
2499 return -1;
2500
2501 strncpy(context->http_proxy_address, proxy,
2502 sizeof(context->http_proxy_address) - 1);
2503 context->http_proxy_address[
2504 sizeof(context->http_proxy_address) - 1] = '\0';
2505
2506 p = strchr(context->http_proxy_address, ':');
2507 if (!p) {
2508 lwsl_err("http_proxy needs to be ads:port\n");
2509
2510 return -1;
2511 }
2512 *p = '\0';
2513 context->http_proxy_port = atoi(p + 1);
2514
2515 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2516 context->http_proxy_port);
2517
2518 return 0;
2519}
2520
2521/**
Andy Greenb45993c2010-12-18 15:13:50 +00002522 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002523 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002524 * @wsi: pointer to struct websocket you want to know the protocol of
2525 *
Andy Green8f037e42010-12-19 22:13:26 +00002526 *
Andy Green6f520a52013-01-29 17:57:39 +08002527 * Some apis can act on all live connections of a given protocol,
2528 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002529 */
Andy Greenab990e42010-10-31 12:42:52 +00002530
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002531LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002532libwebsockets_get_protocol(struct libwebsocket *wsi)
2533{
2534 return wsi->protocol;
2535}
2536
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002537LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002538libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2539{
Andy Green623a98d2013-01-21 11:04:23 +08002540 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002541}
Alex Bligh49146db2011-11-07 17:19:25 +08002542
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002543LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002544libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2545{
Andy Green623a98d2013-01-21 11:04:23 +08002546 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002547}
2548
Andy Green2af4d5b2013-02-18 16:30:10 +08002549int
Alex Bligh49146db2011-11-07 17:19:25 +08002550libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2551{
Andy Green67d556c2013-02-15 22:31:55 +08002552 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002553 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002554
Alex Bligh49146db2011-11-07 17:19:25 +08002555 /* allocate the per-connection user memory (if any) */
2556
2557 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2558 wsi->user_space = malloc(
2559 wsi->protocol->per_session_data_size);
2560 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002561 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002562 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002563 }
Andy Green6ee372f2012-04-09 15:09:01 +08002564 memset(wsi->user_space, 0,
2565 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002566 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002567 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002568}
Andy Green43db0452013-01-10 19:50:35 +08002569
Andy Green0b319092013-01-19 11:17:56 +08002570static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002571{
Andy Green0b319092013-01-19 11:17:56 +08002572 char buf[300];
2573 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002574 int n;
2575
2576 gettimeofday(&tv, NULL);
2577
2578 buf[0] = '\0';
2579 for (n = 0; n < LLL_COUNT; n++)
2580 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002581 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002582 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002583 break;
2584 }
Andy Greenb5b23192013-02-11 17:13:32 +08002585
Andy Green0b319092013-01-19 11:17:56 +08002586 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002587}
2588
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002589#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002590LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002591{
2592 lwsl_emit_stderr(level, line);
2593}
2594#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002595LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002596{
2597 int syslog_level = LOG_DEBUG;
2598
2599 switch (level) {
2600 case LLL_ERR:
2601 syslog_level = LOG_ERR;
2602 break;
2603 case LLL_WARN:
2604 syslog_level = LOG_WARNING;
2605 break;
2606 case LLL_NOTICE:
2607 syslog_level = LOG_NOTICE;
2608 break;
2609 case LLL_INFO:
2610 syslog_level = LOG_INFO;
2611 break;
2612 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002613 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002614}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002615#endif
Andy Greenc11db202013-01-19 11:12:16 +08002616
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002617LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002618{
Andy Greende8f27a2013-01-12 09:17:42 +08002619 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002620 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002621
2622 if (!(log_level & filter))
2623 return;
2624
Andy Green43db0452013-01-10 19:50:35 +08002625 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002626 vsnprintf(buf, sizeof(buf), format, ap);
2627 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002628 va_end(ap);
2629
Andy Green0b319092013-01-19 11:17:56 +08002630 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002631}
2632
2633/**
2634 * lws_set_log_level() - Set the logging bitfield
2635 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002636 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2637 * function to perform log string emission instead of
2638 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002639 *
Andy Greenc6511a02013-02-19 10:01:48 +08002640 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002641 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002642 */
2643
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002644LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002645 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002646{
2647 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002648 if (log_emit_function)
2649 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002650}