blob: 458a5479e213ffcd0a2e91c4fa80b30bc3bd29c9 [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
Patrick Gansterer92792b42014-02-26 21:37:31 +0100113static unsigned long long time_in_microseconds()
114{
115#if defined(WIN32) || defined(_WIN32)
116#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
117 FILETIME filetime;
118 ULARGE_INTEGER datetime;
119
120#ifdef _WIN32_WCE
121 GetCurrentFT(&filetime);
122#else
123 GetSystemTimeAsFileTime(&filetime);
124#endif
125
126 /*
127 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
128 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
129 * prevent alignment faults on 64-bit Windows).
130 */
131 memcpy(&datetime, &filetime, sizeof(datetime));
132
133 /* Windows file times are in 100s of nanoseconds. */
134 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
135#else
136 struct timeval tv;
137 gettimeofday(&tv, NULL);
138 return (tv.tv_sec * 1000000) + tv.tv_usec;
139#endif
140}
141
Andy Green0d338332011-02-12 11:57:43 +0000142int
Andy Greenb5b23192013-02-11 17:13:32 +0800143insert_wsi_socket_into_fds(struct libwebsocket_context *context,
144 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000145{
Joakim Soderberg6596e4b2014-02-18 13:38:14 +0100146 struct libwebsocket_pollargs pa = { wsi->sock, POLLIN, 0 };
147
Andy Greendfb23042013-01-17 12:26:48 +0800148 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800149 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000150 return 1;
151 }
152
nononamef1624922014-01-11 13:12:34 +0800153 if (wsi->sock >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800154 lwsl_err("Socket fd %d is too high (%d)\n",
155 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800156 return 1;
157 }
158
159 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800160 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800161
Andy Greenb5b23192013-02-11 17:13:32 +0800162 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
163 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800164
Andy Green7a132792013-12-18 09:48:26 +0800165 context->protocols[0].callback(context, wsi,
166 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800167 wsi->user_space, (void *) &pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800168
Andy Greendfb23042013-01-17 12:26:48 +0800169 context->lws_lookup[wsi->sock] = wsi;
170 wsi->position_in_fds_table = context->fds_count;
171 context->fds[context->fds_count].fd = wsi->sock;
172 context->fds[context->fds_count].events = POLLIN;
173 context->fds[context->fds_count++].revents = 0;
174
175 /* external POLL support via protocol 0 */
176 context->protocols[0].callback(context, wsi,
177 LWS_CALLBACK_ADD_POLL_FD,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800178 wsi->user_space, (void *) &pa, 0);
Andy Green0d338332011-02-12 11:57:43 +0000179
Andy Green7a132792013-12-18 09:48:26 +0800180 context->protocols[0].callback(context, wsi,
181 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800182 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800183
Andy Green0d338332011-02-12 11:57:43 +0000184 return 0;
185}
186
Andy Greendfb23042013-01-17 12:26:48 +0800187static int
Andy Greenb5b23192013-02-11 17:13:32 +0800188remove_wsi_socket_from_fds(struct libwebsocket_context *context,
189 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000190{
Andy Greendfb23042013-01-17 12:26:48 +0800191 int m;
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800192 struct libwebsocket_pollargs pa = { wsi->sock, 0, 0};
Andy Green0d338332011-02-12 11:57:43 +0000193
Andy Green7a132792013-12-18 09:48:26 +0800194 if (!--context->fds_count) {
195 context->protocols[0].callback(context, wsi,
196 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800197 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800198 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800199 }
Andy Green0d338332011-02-12 11:57:43 +0000200
Andy Greendfb23042013-01-17 12:26:48 +0800201 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800202 lwsl_err("Socket fd %d too high (%d)\n",
203 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800204 return 1;
205 }
Andy Green0d338332011-02-12 11:57:43 +0000206
Andy Greenb5b23192013-02-11 17:13:32 +0800207 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
208 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800209
Andy Green7a132792013-12-18 09:48:26 +0800210 context->protocols[0].callback(context, wsi,
211 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800212 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800213
Andy Greendfb23042013-01-17 12:26:48 +0800214 m = wsi->position_in_fds_table; /* replace the contents for this */
215
216 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800217 context->fds[m] = context->fds[context->fds_count];
218 /*
219 * end guy's fds_lookup entry remains unchanged
220 * (still same fd pointing to same wsi)
221 */
Andy Greendfb23042013-01-17 12:26:48 +0800222 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800223 context->lws_lookup[context->fds[context->fds_count].fd]->
224 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800225 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800226 context->lws_lookup[wsi->sock] = NULL;
227 /* removed wsi has no position any more */
228 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800229
230do_ext:
231 /* remove also from external POLL support via protocol 0 */
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800232 if (wsi->sock) {
Andy Greendfb23042013-01-17 12:26:48 +0800233 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800234 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800235 (void *) &pa, 0);
236 }
Andy Green7a132792013-12-18 09:48:26 +0800237 context->protocols[0].callback(context, wsi,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800238 LWS_CALLBACK_UNLOCK_POLL,
239 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800240 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000241}
242
Andy Green32375b72011-02-19 08:32:53 +0000243
Andy Green8f037e42010-12-19 22:13:26 +0000244void
Peter Hinz56885f32011-03-02 22:03:47 +0000245libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000246 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000247{
Andy Greenb45993c2010-12-18 15:13:50 +0000248 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000249 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000250 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
251 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800252#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000253 int ret;
254 int m;
255 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100256 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800257#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000258
Andy Green4b6fbe12011-02-14 08:03:48 +0000259 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000260 return;
261
Andy Green62c54d22011-02-14 09:14:25 +0000262 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000263
Andy Green62c54d22011-02-14 09:14:25 +0000264 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000265 return;
266
Andy Green22524a62013-02-15 22:48:58 +0800267 /* we tried the polite way... */
268 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
269 goto just_kill_connection;
270
Andy Green623a98d2013-01-21 11:04:23 +0800271 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000272
Andy Green5dc62ea2013-09-20 20:26:12 +0800273 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
274 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
275
276 context->protocols[0].callback(context, wsi,
277 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
278
279 free(wsi->u.hdr.ah);
280 goto just_kill_connection;
281 }
282
283
kapejodce64fb02013-11-19 13:38:16 +0100284 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
285 if (wsi->u.http.post_buffer) {
286 free(wsi->u.http.post_buffer);
287 wsi->u.http.post_buffer = NULL;
288 }
289 if (wsi->u.http.fd >= 0) {
290 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
291 close(wsi->u.http.fd);
292 wsi->u.http.fd = -1;
293 context->protocols[0].callback(context, wsi,
294 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
295 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800296 }
297
Andy Green3182ece2013-01-20 17:08:31 +0800298#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000299 /*
Andy Green68b45042011-05-25 21:41:57 +0100300 * are his extensions okay with him closing? Eg he might be a mux
301 * parent and just his ch1 aspect is closing?
302 */
303
Andy Green68b45042011-05-25 21:41:57 +0100304 for (n = 0; n < wsi->count_active_extensions; n++) {
305 if (!wsi->active_extensions[n]->callback)
306 continue;
307
308 m = wsi->active_extensions[n]->callback(context,
309 wsi->active_extensions[n], wsi,
310 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
311 wsi->active_extensions_user[n], NULL, 0);
312
313 /*
314 * if somebody vetoed actually closing him at this time....
315 * up to the extension to track the attempted close, let's
316 * just bail
317 */
318
319 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800320 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100321 return;
322 }
323 }
324
Andy Green68b45042011-05-25 21:41:57 +0100325 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000326 * flush any tx pending from extensions, since we may send close packet
327 * if there are problems with send, just nuke the connection
328 */
329
330 ret = 1;
331 while (ret == 1) {
332
333 /* default to nobody has more to spill */
334
335 ret = 0;
336 eff_buf.token = NULL;
337 eff_buf.token_len = 0;
338
339 /* show every extension the new incoming data */
340
341 for (n = 0; n < wsi->count_active_extensions; n++) {
342 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000343 wsi->protocol->owning_server,
344 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000345 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
346 wsi->active_extensions_user[n], &eff_buf, 0);
347 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800348 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000349 goto just_kill_connection;
350 }
351 if (m)
352 /*
353 * at least one extension told us he has more
354 * to spill, so we will go around again after
355 */
356 ret = 1;
357 }
358
359 /* assuming they left us something to send, send it */
360
361 if (eff_buf.token_len)
362 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800363 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800364 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000365 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800366 }
Andy Greenc44159f2011-03-07 07:08:18 +0000367 }
Andy Green3182ece2013-01-20 17:08:31 +0800368#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000369
370 /*
Andy Greenda527df2011-03-07 07:08:12 +0000371 * signal we are closing, libsocket_write will
372 * add any necessary version-specific stuff. If the write fails,
373 * no worries we are closing anyway. If we didn't initiate this
374 * close, then our state has been changed to
375 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
376 *
377 * Likewise if it's a second call to close this connection after we
378 * sent the close indication to the peer already, we are in state
379 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
380 */
381
382 if (old_state == WSI_STATE_ESTABLISHED &&
383 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100384
Andy Green43db0452013-01-10 19:50:35 +0800385 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100386
Andy Greenfdd305a2013-02-11 14:32:48 +0800387 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800388 memset(buf, 0, sizeof(buf));
389 n = libwebsocket_write(wsi,
390 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000391 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800392 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000393 /*
394 * we have sent a nice protocol level indication we
395 * now wish to close, we should not send anything more
396 */
397
398 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
399
Andy Greenb5b23192013-02-11 17:13:32 +0800400 /*
401 * ...and we should wait for a reply for a bit
402 * out of politeness
403 */
Andy Greenda527df2011-03-07 07:08:12 +0000404
405 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800406 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000407
Andy Green43db0452013-01-10 19:50:35 +0800408 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000409
410 return;
411 }
412
Andy Greenb5b23192013-02-11 17:13:32 +0800413 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800414
Andy Greenda527df2011-03-07 07:08:12 +0000415 /* else, the send failed and we should just hang up */
416 }
417
Andy Greenc44159f2011-03-07 07:08:18 +0000418just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100419
Andy Greenb5b23192013-02-11 17:13:32 +0800420 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100421
Andy Greenda527df2011-03-07 07:08:12 +0000422 /*
423 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800424 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000425 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000426
Andy Greendfb23042013-01-17 12:26:48 +0800427 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000428
Andy Green251f6fa2010-11-03 11:13:06 +0000429 wsi->state = WSI_STATE_DEAD_SOCKET;
430
Andy Greenb5b23192013-02-11 17:13:32 +0800431 if ((old_state == WSI_STATE_ESTABLISHED ||
432 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800433 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
434
435 if (wsi->u.ws.rx_user_buffer) {
436 free(wsi->u.ws.rx_user_buffer);
437 wsi->u.ws.rx_user_buffer = NULL;
438 }
439 if (wsi->u.ws.rxflow_buffer) {
440 free(wsi->u.ws.rxflow_buffer);
441 wsi->u.ws.rxflow_buffer = NULL;
442 }
Andy Green2764eba2013-12-09 14:16:17 +0800443 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800444 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800445 free(wsi->truncated_send_malloc);
446 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800447 }
Andy Green54495112013-02-06 21:10:16 +0900448 }
449
Andy Green4b6fbe12011-02-14 08:03:48 +0000450 /* tell the user it's all over for this guy */
451
Andy Greend4302732011-02-28 07:45:29 +0000452 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800453 ((old_state == WSI_STATE_ESTABLISHED) ||
454 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
455 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800456 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000457 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000458 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800459 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
460 lwsl_debug("calling back CLOSED_HTTP\n");
461 context->protocols[0].callback(context, wsi,
462 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800463 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800464 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000465
Andy Green3182ece2013-01-20 17:08:31 +0800466#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000467 /* deallocate any active extension contexts */
468
469 for (n = 0; n < wsi->count_active_extensions; n++) {
470 if (!wsi->active_extensions[n]->callback)
471 continue;
472
Andy Green46c2ea02011-03-22 09:04:01 +0000473 wsi->active_extensions[n]->callback(context,
474 wsi->active_extensions[n], wsi,
475 LWS_EXT_CALLBACK_DESTROY,
476 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000477
478 free(wsi->active_extensions_user[n]);
479 }
480
Andy Greena41314f2011-05-23 10:00:03 +0100481 /*
482 * inform all extensions in case they tracked this guy out of band
483 * even though not active on him specifically
484 */
485
486 ext = context->extensions;
487 while (ext && ext->callback) {
488 ext->callback(context, ext, wsi,
489 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
490 NULL, NULL, 0);
491 ext++;
492 }
Andy Green3182ece2013-01-20 17:08:31 +0800493#endif
Andy Greena41314f2011-05-23 10:00:03 +0100494
Andy Green43db0452013-01-10 19:50:35 +0800495/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000496
Andy Green3faa9c72010-11-08 17:03:03 +0000497#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000498 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000499 n = SSL_get_fd(wsi->ssl);
500 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800501 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000502 SSL_free(wsi->ssl);
503 } else {
504#endif
Andy Green0303db42013-01-17 14:46:43 +0800505 if (wsi->sock) {
506 n = shutdown(wsi->sock, SHUT_RDWR);
507 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800508 lwsl_debug("closing: shutdown returned %d\n",
509 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800510
Andy Green0303db42013-01-17 14:46:43 +0800511 n = compatible_close(wsi->sock);
512 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800513 lwsl_debug("closing: close returned %d\n",
514 errno);
Andy Green0303db42013-01-17 14:46:43 +0800515 }
Andy Green3faa9c72010-11-08 17:03:03 +0000516#ifdef LWS_OPENSSL_SUPPORT
517 }
518#endif
Andy Green76b6ea12014-02-15 19:25:50 +0800519
520 /* outermost destroy notification for wsi (user_space still intact) */
521 context->protocols[0].callback(context, wsi,
522 LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0);
523
Andy Greenb5b23192013-02-11 17:13:32 +0800524 if (wsi->protocol && wsi->protocol->per_session_data_size &&
525 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000526 free(wsi->user_space);
527
Andy Green251f6fa2010-11-03 11:13:06 +0000528 free(wsi);
529}
530
Andy Green07034092011-02-13 08:37:12 +0000531/**
532 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800533 * @context: Libwebsockets context
534 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000535 * @fd: Connection socket descriptor
536 * @name: Buffer to take client address name
537 * @name_len: Length of client address name buffer
538 * @rip: Buffer to take client address IP qotted quad
539 * @rip_len: Length of client address IP buffer
540 *
541 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800542 * the client connected with socket descriptor @fd. Names may be
543 * truncated if there is not enough room. If either cannot be
544 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000545 */
546
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800547LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800548libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
549 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000550 char *rip, int rip_len)
551{
Bob Robertsac049112013-04-25 09:16:30 +0800552 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000553 struct sockaddr_in sin;
554 struct hostent *host;
555 struct hostent *host1;
556 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000557 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000558 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800559 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800560#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800561 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800562#endif
Andy Green07034092011-02-13 08:37:12 +0000563
564 rip[0] = '\0';
565 name[0] = '\0';
566
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800567 lws_latency_pre(context, wsi);
568
Andy Greenb5b23192013-02-11 17:13:32 +0800569 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000570 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100571 lwsl_warn("getpeername: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800572 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000573 }
Andy Green6ee372f2012-04-09 15:09:01 +0800574
Andy Greenb5b23192013-02-11 17:13:32 +0800575 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000576 AF_INET);
577 if (host == NULL) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100578 lwsl_warn("gethostbyaddr: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800579 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000580 }
581
582 strncpy(name, host->h_name, name_len);
583 name[name_len - 1] = '\0';
584
585 host1 = gethostbyname(host->h_name);
586 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800587 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000588 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000589 n = 0;
590 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000591 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000592 if (p == NULL)
593 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000594 if ((host1->h_addrtype != AF_INET)
595#ifdef AF_LOCAL
596 && (host1->h_addrtype != AF_LOCAL)
597#endif
598 )
Andy Green07034092011-02-13 08:37:12 +0000599 continue;
600
Andy Green7627af52011-03-09 15:13:52 +0000601 if (host1->h_addrtype == AF_INET)
602 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000603#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000604 else {
605 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800606 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000607 ip[sizeof(ip) - 1] = '\0';
608 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000609#endif
Andy Green07034092011-02-13 08:37:12 +0000610 p = NULL;
611 strncpy(rip, ip, rip_len);
612 rip[rip_len - 1] = '\0';
613 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800614
615 ret = 0;
616bail:
617 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000618}
Andy Green9f990342011-02-12 11:57:45 +0000619
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800620LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000621 void *buf, int len)
622{
623 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800624 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000625
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100626#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000627 for (n = 0; n < len; n++)
628 p[n] = (unsigned char)rand();
629#else
630 n = read(context->fd_random, p, len);
631#endif
632
633 return n;
634}
635
Andy Greena690cd02013-02-09 12:25:31 +0800636int lws_set_socket_options(struct libwebsocket_context *context, int fd)
637{
638 int optval = 1;
639 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100640#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800641 unsigned long optl = 0;
642#endif
643#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
644 struct protoent *tcp_proto;
645#endif
646
647 if (context->ka_time) {
648 /* enable keepalive on this socket */
649 optval = 1;
650 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
651 (const void *)&optval, optlen) < 0)
652 return 1;
653
Bob Robertsac049112013-04-25 09:16:30 +0800654#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800655
656 /*
657 * didn't find a way to set these per-socket, need to
658 * tune kernel systemwide values
659 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100660#elif WIN32
661 {
662 DWORD dwBytesRet;
663 struct tcp_keepalive alive;
664 alive.onoff = TRUE;
665 alive.keepalivetime = context->ka_time;
666 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800667
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100668 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
669 NULL, 0, &dwBytesRet, NULL, NULL))
670 return 1;
671 }
Andy Greena47865f2013-02-10 09:39:47 +0800672#else
Andy Greena690cd02013-02-09 12:25:31 +0800673 /* set the keepalive conditions we want on it too */
674 optval = context->ka_time;
675 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
676 (const void *)&optval, optlen) < 0)
677 return 1;
678
Larry Hayesbb66ac62013-02-22 09:16:20 +0800679 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800680 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
681 (const void *)&optval, optlen) < 0)
682 return 1;
683
Larry Hayesbb66ac62013-02-22 09:16:20 +0800684 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800685 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
686 (const void *)&optval, optlen) < 0)
687 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800688#endif
Andy Greena690cd02013-02-09 12:25:31 +0800689 }
690
691 /* Disable Nagle */
692 optval = 1;
693#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
694 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
695#else
696 tcp_proto = getprotobyname("TCP");
697 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
698#endif
699
700 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100701#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800702 ioctlsocket(fd, FIONBIO, &optl);
703#else
704 fcntl(fd, F_SETFL, O_NONBLOCK);
705#endif
706
707 return 0;
708}
709
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800710LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000711{
712 struct pollfd fds;
713
Andy Green2764eba2013-12-09 14:16:17 +0800714 /* treat the fact we got a truncated send pending as if we're choked */
715 if (wsi->truncated_send_malloc)
716 return 1;
717
Andy Green95a7b5d2011-03-06 10:29:39 +0000718 fds.fd = wsi->sock;
719 fds.events = POLLOUT;
720 fds.revents = 0;
721
722 if (poll(&fds, 1, 0) != 1)
723 return 1;
724
725 if ((fds.revents & POLLOUT) == 0)
726 return 1;
727
728 /* okay to send another packet without blocking */
729
730 return 0;
731}
732
Andy Greena41314f2011-05-23 10:00:03 +0100733int
Andy Green3b84c002011-03-06 13:14:42 +0000734lws_handle_POLLOUT_event(struct libwebsocket_context *context,
735 struct libwebsocket *wsi, struct pollfd *pollfd)
736{
Andy Green3b84c002011-03-06 13:14:42 +0000737 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800738
Andy Green3182ece2013-01-20 17:08:31 +0800739#ifndef LWS_NO_EXTENSIONS
740 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000741 int ret;
742 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100743 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000744
Andy Green1f4267b2013-10-17 08:09:19 +0800745 /* pending truncated sends have uber priority */
746
Andy Green2764eba2013-12-09 14:16:17 +0800747 if (wsi->truncated_send_malloc) {
748 lws_issue_raw(wsi, wsi->truncated_send_malloc +
749 wsi->truncated_send_offset,
750 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800751 /* leave POLLOUT active either way */
752 return 0;
753 }
754
Andy Greena41314f2011-05-23 10:00:03 +0100755 for (n = 0; n < wsi->count_active_extensions; n++) {
756 if (!wsi->active_extensions[n]->callback)
757 continue;
758
759 m = wsi->active_extensions[n]->callback(context,
760 wsi->active_extensions[n], wsi,
761 LWS_EXT_CALLBACK_IS_WRITEABLE,
762 wsi->active_extensions_user[n], NULL, 0);
763 if (m > handled)
764 handled = m;
765 }
766
767 if (handled == 1)
768 goto notify_action;
769
770 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000771 goto user_service;
772
773 /*
774 * check in on the active extensions, see if they
775 * had pending stuff to spill... they need to get the
776 * first look-in otherwise sequence will be disordered
777 *
778 * NULL, zero-length eff_buf means just spill pending
779 */
780
781 ret = 1;
782 while (ret == 1) {
783
784 /* default to nobody has more to spill */
785
786 ret = 0;
787 eff_buf.token = NULL;
788 eff_buf.token_len = 0;
789
790 /* give every extension a chance to spill */
791
792 for (n = 0; n < wsi->count_active_extensions; n++) {
793 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000794 wsi->protocol->owning_server,
795 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000796 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
797 wsi->active_extensions_user[n], &eff_buf, 0);
798 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800799 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000800 return -1;
801 }
802 if (m)
803 /*
804 * at least one extension told us he has more
805 * to spill, so we will go around again after
806 */
807 ret = 1;
808 }
809
810 /* assuming they gave us something to send, send it */
811
812 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800813 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
814 eff_buf.token_len);
815 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000816 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800817 /*
818 * Keep amount spilled small to minimize chance of this
819 */
820 if (n != eff_buf.token_len) {
821 lwsl_err("Unable to spill ext %d vs %s\n",
822 eff_buf.token_len, n);
823 return -1;
824 }
Andy Green3b84c002011-03-06 13:14:42 +0000825 } else
826 continue;
827
828 /* no extension has more to spill */
829
830 if (!ret)
831 continue;
832
833 /*
834 * There's more to spill from an extension, but we just sent
835 * something... did that leave the pipe choked?
836 */
837
838 if (!lws_send_pipe_choked(wsi))
839 /* no we could add more */
840 continue;
841
Andy Green43db0452013-01-10 19:50:35 +0800842 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000843
844 /*
845 * Yes, he's choked. Leave the POLLOUT masked on so we will
846 * come back here when he is unchoked. Don't call the user
847 * callback to enforce ordering of spilling, he'll get called
848 * when we come back here and there's nothing more to spill.
849 */
850
851 return 0;
852 }
853
854 wsi->extension_data_pending = 0;
855
856user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800857#endif
Andy Green3b84c002011-03-06 13:14:42 +0000858 /* one shot */
859
Andy Green91f19d82013-12-21 11:18:34 +0800860 if (pollfd)
861 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800862
Andy Green3182ece2013-01-20 17:08:31 +0800863#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100864notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800865#endif
Andy Green3b84c002011-03-06 13:14:42 +0000866
Andy Green9e4c2b62011-03-07 20:47:39 +0000867 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
868 n = LWS_CALLBACK_CLIENT_WRITEABLE;
869 else
870 n = LWS_CALLBACK_SERVER_WRITEABLE;
871
Andy Greenb8b247d2013-01-22 07:20:08 +0800872 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800873 wsi, (enum libwebsocket_callback_reasons) n,
874 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000875}
876
877
878
Andy Green1c6e1422013-02-20 19:11:31 +0800879int
Andy Greena41314f2011-05-23 10:00:03 +0100880libwebsocket_service_timeout_check(struct libwebsocket_context *context,
881 struct libwebsocket *wsi, unsigned int sec)
882{
Andy Green3182ece2013-01-20 17:08:31 +0800883#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100884 int n;
885
886 /*
887 * if extensions want in on it (eg, we are a mux parent)
888 * give them a chance to service child timeouts
889 */
890
891 for (n = 0; n < wsi->count_active_extensions; n++)
892 wsi->active_extensions[n]->callback(
893 context, wsi->active_extensions[n],
894 wsi, LWS_EXT_CALLBACK_1HZ,
895 wsi->active_extensions_user[n], NULL, sec);
896
Andy Green3182ece2013-01-20 17:08:31 +0800897#endif
Andy Greena41314f2011-05-23 10:00:03 +0100898 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800899 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800900
Andy Greena41314f2011-05-23 10:00:03 +0100901 /*
902 * if we went beyond the allowed time, kill the
903 * connection
904 */
905
906 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800907 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100908 libwebsocket_close_and_free_session(context,
909 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800910 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100911 }
Andy Green1c6e1422013-02-20 19:11:31 +0800912
913 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100914}
915
Andy Green9f990342011-02-12 11:57:45 +0000916/**
917 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000918 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000919 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800920 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000921 *
Andy Green75006172013-01-22 12:32:11 +0800922 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800923 * services it according to the state of the associated
924 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800925 *
926 * The one call deals with all "service" that might happen on a socket
927 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800928 *
929 * If a pollfd says it has something, you can just pass it to
930 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
931 * If it sees it is a lws socket, the traffic will be handled and
932 * pollfd->revents will be zeroed now.
933 *
934 * If the socket is foreign to lws, it leaves revents alone. So you can
935 * see if you should service yourself by checking the pollfd revents
936 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000937 */
938
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800939LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000940libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000941 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000942{
Andy Greena1ce6be2013-01-18 11:43:21 +0800943 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000944 int n;
Andy Green0d338332011-02-12 11:57:43 +0000945 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800946 int listen_socket_fds_index = 0;
Patrick Gansterer92792b42014-02-26 21:37:31 +0100947 time_t now;
Andy Green1c6e1422013-02-20 19:11:31 +0800948 int timed_out = 0;
949 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800950 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800951
Andy Green3182ece2013-01-20 17:08:31 +0800952#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000953 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800954#endif
Andy Green98a717c2011-03-06 13:14:15 +0000955 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800956
957 if (context->listen_service_fd)
958 listen_socket_fds_index = context->lws_lookup[
959 context->listen_service_fd]->position_in_fds_table;
960
Andy Greena71eafc2011-02-14 17:59:43 +0000961 /*
962 * you can call us with pollfd = NULL to just allow the once-per-second
963 * global timeout checks; if less than a second since the last check
964 * it returns immediately then.
965 */
966
Patrick Gansterer92792b42014-02-26 21:37:31 +0100967 time(&now);
Andy Greena71eafc2011-02-14 17:59:43 +0000968
Patrick Gansterer92792b42014-02-26 21:37:31 +0100969 if (context->last_timeout_check_s != now) {
970 context->last_timeout_check_s = now;
Andy Greena71eafc2011-02-14 17:59:43 +0000971
Joakim Soderberg4c531232013-02-06 15:26:58 +0900972 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800973 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800974 if (context->started_with_parent &&
975 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800976 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900977 #endif
Andy Green24cba922013-01-19 13:56:10 +0800978
Andy Greena71eafc2011-02-14 17:59:43 +0000979 /* global timeout check once per second */
980
Andy Green1c6e1422013-02-20 19:11:31 +0800981 if (pollfd)
982 our_fd = pollfd->fd;
983
Peter Hinz56885f32011-03-02 22:03:47 +0000984 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800985 m = context->fds[n].fd;
986 wsi = context->lws_lookup[m];
987 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800988 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800989
Patrick Gansterer92792b42014-02-26 21:37:31 +0100990 if (libwebsocket_service_timeout_check(context, wsi, now))
Andy Green1c6e1422013-02-20 19:11:31 +0800991 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800992 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800993 /* it was the guy we came to service! */
994 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800995 /* mark as handled */
996 pollfd->revents = 0;
997 }
Andy Greena71eafc2011-02-14 17:59:43 +0000998 }
999 }
1000
Andy Green1c6e1422013-02-20 19:11:31 +08001001 /* the socket we came to service timed out, nothing to do */
1002 if (timed_out)
1003 return 0;
1004
Andy Greena71eafc2011-02-14 17:59:43 +00001005 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +00001006 if (pollfd == NULL)
1007 return 0;
1008
1009 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001010 wsi = context->lws_lookup[pollfd->fd];
1011 if (wsi == NULL)
1012 /* not lws connection ... leave revents alone and return */
1013 return 0;
1014
1015 /*
1016 * so that caller can tell we handled, past here we need to
1017 * zero down pollfd->revents after handling
1018 */
1019
Andy Green65b0e912013-01-16 07:59:47 +08001020 /*
1021 * deal with listen service piggybacking
1022 * every listen_service_modulo services of other fds, we
1023 * sneak one in to service the listen socket if there's anything waiting
1024 *
1025 * To handle connection storms, as found in ab, if we previously saw a
1026 * pending connection here, it causes us to check again next time.
1027 */
1028
Andy Greenb5b23192013-02-11 17:13:32 +08001029 if (context->listen_service_fd && pollfd !=
1030 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +08001031 context->listen_service_count++;
1032 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +08001033 context->listen_service_count ==
1034 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001035 context->listen_service_count = 0;
1036 m = 1;
1037 if (context->listen_service_extraseen > 5)
1038 m = 2;
1039 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001040 /*
1041 * even with extpoll, we prepared this
1042 * internal fds for listen
1043 */
1044 n = poll(&context->fds[listen_socket_fds_index],
1045 1, 0);
1046 if (n > 0) { /* there's a conn waiting for us */
1047 libwebsocket_service_fd(context,
1048 &context->
1049 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001050 context->listen_service_extraseen++;
1051 } else {
1052 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001053 context->
1054 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001055 break;
1056 }
1057 }
1058 }
1059
1060 }
1061
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001062 /* handle session socket closed */
1063
1064 if ((!(pollfd->revents & POLLIN)) &&
1065 (pollfd->revents & (POLLERR | POLLHUP))) {
1066
1067 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1068 (void *)wsi, pollfd->fd);
1069
1070 goto close_and_handled;
1071 }
1072
Andy Green65b0e912013-01-16 07:59:47 +08001073 /* okay, what we came here to do... */
1074
Andy Green0d338332011-02-12 11:57:43 +00001075 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001076
Andy Greena1ce6be2013-01-18 11:43:21 +08001077#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001078 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001079 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001080 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001081 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001082 n = lws_server_socket_service(context, wsi, pollfd);
1083 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001084#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001085
Andy Green0d338332011-02-12 11:57:43 +00001086 case LWS_CONNMODE_WS_SERVING:
1087 case LWS_CONNMODE_WS_CLIENT:
1088
Andy Green0d338332011-02-12 11:57:43 +00001089 /* the guy requested a callback when it was OK to write */
1090
Andy Greenda527df2011-03-07 07:08:12 +00001091 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001092 wsi->state == WSI_STATE_ESTABLISHED &&
1093 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001094 lwsl_info("libwebsocket_service_fd: closing\n");
1095 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001096 }
Andy Green0d338332011-02-12 11:57:43 +00001097
Andy Greenca0a1292013-03-16 11:24:23 +08001098 if (wsi->u.ws.rxflow_buffer &&
1099 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1100 lwsl_info("draining rxflow\n");
1101 /* well, drain it */
1102 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1103 wsi->u.ws.rxflow_pos;
1104 eff_buf.token_len = wsi->u.ws.rxflow_len -
1105 wsi->u.ws.rxflow_pos;
1106 draining_flow = 1;
1107 goto drain;
1108 }
1109
Andy Green0d338332011-02-12 11:57:43 +00001110 /* any incoming data ready? */
1111
1112 if (!(pollfd->revents & POLLIN))
1113 break;
1114
Andy Greenb45993c2010-12-18 15:13:50 +00001115#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001116read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001117 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001118 eff_buf.token_len = SSL_read(wsi->ssl,
1119 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001120 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001121 if (!eff_buf.token_len) {
1122 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001123 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001124 ERR_error_string(n,
1125 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001126 }
1127 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001128#endif
Andy Greene84652c2013-02-08 13:01:02 +08001129 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001130 context->service_buffer,
1131 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001132
Andy Green98a717c2011-03-06 13:14:15 +00001133 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001134 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1135 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001136 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001137 goto close_and_handled;
1138 n = 0;
1139 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001140 }
Andy Green98a717c2011-03-06 13:14:15 +00001141 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001142 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001143 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001144 }
1145
Andy Green98a717c2011-03-06 13:14:15 +00001146 /*
1147 * give any active extensions a chance to munge the buffer
1148 * before parse. We pass in a pointer to an lws_tokens struct
1149 * prepared with the default buffer and content length that's in
1150 * there. Rather than rewrite the default buffer, extensions
1151 * that expect to grow the buffer can adapt .token to
1152 * point to their own per-connection buffer in the extension
1153 * user allocation. By default with no extensions or no
1154 * extension callback handling, just the normal input buffer is
1155 * used then so it is efficient.
1156 */
Andy Greenb45993c2010-12-18 15:13:50 +00001157
Andy Greene84652c2013-02-08 13:01:02 +08001158 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001159drain:
Andy Green3182ece2013-01-20 17:08:31 +08001160#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001161 more = 1;
1162 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001163
Andy Green98a717c2011-03-06 13:14:15 +00001164 more = 0;
1165
1166 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001167 m = wsi->active_extensions[n]->callback(context,
1168 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001169 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001170 wsi->active_extensions_user[n],
1171 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001172 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001173 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001174 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001175 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001176 }
1177 if (m)
1178 more = 1;
1179 }
Andy Green3182ece2013-01-20 17:08:31 +08001180#endif
Andy Green98a717c2011-03-06 13:14:15 +00001181 /* service incoming data */
1182
1183 if (eff_buf.token_len) {
1184 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001185 (unsigned char *)eff_buf.token,
1186 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001187 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001188 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001189 n = 0;
1190 goto handled;
1191 }
Andy Green98a717c2011-03-06 13:14:15 +00001192 }
Andy Green3182ece2013-01-20 17:08:31 +08001193#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001194 eff_buf.token = NULL;
1195 eff_buf.token_len = 0;
1196 }
Andy Green3182ece2013-01-20 17:08:31 +08001197#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001198 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1199 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1200 lwsl_info("flow buffer: drained\n");
1201 free(wsi->u.ws.rxflow_buffer);
1202 wsi->u.ws.rxflow_buffer = NULL;
1203 /* having drained the rxflow buffer, can rearm POLLIN */
1204 _libwebsocket_rx_flow_control(wsi);
1205 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001206
1207#ifdef LWS_OPENSSL_SUPPORT
1208 if (wsi->ssl && SSL_pending(wsi->ssl))
1209 goto read_pending;
1210#endif
Andy Green98a717c2011-03-06 13:14:15 +00001211 break;
Andy Green76f61e72013-01-16 11:53:05 +08001212
1213 default:
Andy Green03674a62013-01-16 11:47:40 +08001214#ifdef LWS_NO_CLIENT
1215 break;
1216#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001217 n = lws_client_socket_service(context, wsi, pollfd);
1218 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001219#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001220 }
1221
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001222 n = 0;
1223 goto handled;
1224
1225close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001226 libwebsocket_close_and_free_session(context, wsi,
1227 LWS_CLOSE_STATUS_NOSTATUS);
1228 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001229
1230handled:
1231 pollfd->revents = 0;
1232 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001233}
1234
Andy Green0d338332011-02-12 11:57:43 +00001235
Andy Green6964bb52011-01-23 16:50:33 +00001236/**
1237 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001238 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001239 *
1240 * This function closes any active connections and then frees the
1241 * context. After calling this, any further use of the context is
1242 * undefined.
1243 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001244LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001245libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001246{
Andy Green0d338332011-02-12 11:57:43 +00001247 int n;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001248#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001249 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001250 struct libwebsocket_extension *ext;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001251#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001252 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001253
Andy Greend636e352013-01-29 12:36:17 +08001254#ifdef LWS_LATENCY
1255 if (context->worst_latency_info[0])
1256 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1257#endif
1258
Andy Greendfb23042013-01-17 12:26:48 +08001259 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001260 struct libwebsocket *wsi =
1261 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001262 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001263 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1264 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001265 }
Andy Green6964bb52011-01-23 16:50:33 +00001266
arnaudviala7c6f26c2014-02-15 14:39:40 +08001267#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001268 /*
1269 * give all extensions a chance to clean up any per-context
1270 * allocations they might have made
1271 */
1272
1273 ext = context->extensions;
1274 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1275 if (context->listen_port)
1276 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001277 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001278 ext->callback(context, ext, NULL,
1279 (enum libwebsocket_extension_callback_reasons)m,
1280 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001281 ext++;
1282 }
arnaudviala7c6f26c2014-02-15 14:39:40 +08001283#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001284
1285 /*
1286 * inform all the protocols that they are done and will have no more
1287 * callbacks
1288 */
1289
1290 while (protocol->callback) {
1291 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1292 NULL, NULL, 0);
1293 protocol++;
1294 }
1295
Andy Greena41314f2011-05-23 10:00:03 +01001296
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001297#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001298#else
1299 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001300#endif
1301
Peter Hinz56885f32011-03-02 22:03:47 +00001302#ifdef LWS_OPENSSL_SUPPORT
1303 if (context->ssl_ctx)
1304 SSL_CTX_free(context->ssl_ctx);
1305 if (context->ssl_client_ctx)
1306 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001307
1308 ERR_remove_state(0);
1309 ERR_free_strings();
1310 EVP_cleanup();
1311 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001312#endif
1313
Andy Green46596482013-02-11 11:04:01 +08001314 if (context->fds)
1315 free(context->fds);
1316 if (context->lws_lookup)
1317 free(context->lws_lookup);
1318
Peter Hinz56885f32011-03-02 22:03:47 +00001319 free(context);
1320
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001321#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001322 WSACleanup();
1323#endif
Andy Green6964bb52011-01-23 16:50:33 +00001324}
1325
Andy Greend88146d2013-01-22 12:40:35 +08001326/**
Andy Greenb5b23192013-02-11 17:13:32 +08001327 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001328 * @context: Websocket context
1329 *
1330 * This returns the optional user allocation that can be attached to
1331 * the context the sockets live in at context_create time. It's a way
1332 * to let all sockets serviced in the same context share data without
1333 * using globals statics in the user code.
1334 */
Alon Levy0291eb32012-10-19 11:21:56 +02001335LWS_EXTERN void *
1336libwebsocket_context_user(struct libwebsocket_context *context)
1337{
Andy Greenb5b23192013-02-11 17:13:32 +08001338 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001339}
1340
Andy Green6964bb52011-01-23 16:50:33 +00001341/**
1342 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001343 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001344 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1345 * service otherwise block and service immediately, returning
1346 * after the timeout if nothing needed service.
1347 *
1348 * This function deals with any pending websocket traffic, for three
1349 * kinds of event. It handles these events on both server and client
1350 * types of connection the same.
1351 *
1352 * 1) Accept new connections to our context's server
1353 *
Andy Green6f520a52013-01-29 17:57:39 +08001354 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001355 * server or client connections.
1356 *
1357 * You need to call this service function periodically to all the above
1358 * functions to happen; if your application is single-threaded you can
1359 * just call it in your main event loop.
1360 *
1361 * Alternatively you can fork a new process that asynchronously handles
1362 * calling this service in a loop. In that case you are happy if this
1363 * call blocks your thread until it needs to take care of something and
1364 * would call it with a large nonzero timeout. Your loop then takes no
1365 * CPU while there is nothing happening.
1366 *
1367 * If you are calling it in a single-threaded app, you don't want it to
1368 * wait around blocking other things in your loop from happening, so you
1369 * would call it with a timeout_ms of 0, so it returns immediately if
1370 * nothing is pending, or as soon as it services whatever was pending.
1371 */
1372
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001373LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001374libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001375{
1376 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001377 int m;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001378#ifdef LWS_HAS_PPOLL
1379 struct timespec timeout_ts;
1380 sigset_t sigmask;
1381#endif
Andy Greene92cd172011-01-19 13:11:55 +00001382
1383 /* stay dead once we are dead */
1384
Peter Hinz56885f32011-03-02 22:03:47 +00001385 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001386 return 1;
1387
Andy Green3b3fa9e2013-12-25 16:34:37 +08001388#ifdef LWS_HAS_PPOLL
1389 lws_idling_ppoll_tid = context->protocols[0].callback(context, NULL,
1390 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1391
1392 timeout_ts.tv_sec = timeout_ms / 1000;
1393 timeout_ts.tv_nsec = timeout_ms % 1000;
Andy Greened451d52014-01-11 12:37:07 +08001394
1395 sigprocmask(SIG_BLOCK, NULL, &sigmask);
1396 sigdelset(&sigmask, SIGUSR2);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001397
Andy Green0d338332011-02-12 11:57:43 +00001398 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001399
Andy Green3b3fa9e2013-12-25 16:34:37 +08001400 n = ppoll(context->fds, context->fds_count, &timeout_ts, &sigmask);
1401 lws_idling_ppoll_tid = 0;
1402#else
Peter Hinz56885f32011-03-02 22:03:47 +00001403 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001404#endif
Andy Green5dc62ea2013-09-20 20:26:12 +08001405 if (n == 0) /* poll timeout */ {
1406 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001407 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001408 }
Andy Greene92cd172011-01-19 13:11:55 +00001409
Andy Greened451d52014-01-11 12:37:07 +08001410 if (n < 0) {
1411 if (errno != EINTR)
1412 return -1;
1413 else
1414 return 0;
1415 }
Andy Greene92cd172011-01-19 13:11:55 +00001416
Andy Greendfb23042013-01-17 12:26:48 +08001417 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001418
Andy Greenca0a1292013-03-16 11:24:23 +08001419 for (n = 0; n < context->fds_count; n++) {
1420 if (!context->fds[n].revents)
1421 continue;
1422 m = libwebsocket_service_fd(context, &context->fds[n]);
1423 if (m < 0)
1424 return -1;
1425 /* if something closed, retry this slot */
1426 if (m)
1427 n--;
1428 }
1429
Andy Greene92cd172011-01-19 13:11:55 +00001430 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001431}
1432
Andy Green3182ece2013-01-20 17:08:31 +08001433#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001434int
1435lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001436 struct libwebsocket *wsi,
1437 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001438 void *v, size_t len)
1439{
1440 int n;
1441 int handled = 0;
1442
1443 /* maybe an extension will take care of it for us */
1444
1445 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1446 if (!wsi->active_extensions[n]->callback)
1447 continue;
1448
1449 handled |= wsi->active_extensions[n]->callback(context,
1450 wsi->active_extensions[n], wsi,
1451 r, wsi->active_extensions_user[n], v, len);
1452 }
1453
1454 return handled;
1455}
1456
1457
1458void *
1459lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001460 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001461{
1462 int n = 0;
1463
Andy Green68b45042011-05-25 21:41:57 +01001464 if (wsi == NULL)
1465 return NULL;
1466
Andy Greena41314f2011-05-23 10:00:03 +01001467 while (n < wsi->count_active_extensions) {
1468 if (wsi->active_extensions[n] != ext) {
1469 n++;
1470 continue;
1471 }
1472 return wsi->active_extensions_user[n];
1473 }
1474
1475 return NULL;
1476}
Andy Green3182ece2013-01-20 17:08:31 +08001477#endif
Andy Greena41314f2011-05-23 10:00:03 +01001478
Andy Green91f19d82013-12-21 11:18:34 +08001479void
1480lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1481{
1482 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001483 int events;
1484#ifdef LWS_HAS_PPOLL
1485 int tid;
1486 int sampled_ppoll_tid;
1487#endif
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001488 struct libwebsocket_pollargs pa;
1489
1490 pa.fd = wsi->sock;
Andy Green91f19d82013-12-21 11:18:34 +08001491
1492 context->protocols[0].callback(context, wsi,
1493 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001494 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001495
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001496 pa.prev_events = events = context->fds[wsi->position_in_fds_table].events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001497
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001498 pa.events = context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001499
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001500 context->protocols[0].callback(context, wsi,
1501 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
1502 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001503
Andy Green91f19d82013-12-21 11:18:34 +08001504
Andy Green3b3fa9e2013-12-25 16:34:37 +08001505#ifdef LWS_HAS_PPOLL
1506 /*
1507 * if we changed something in this pollfd...
1508 * ... and we're running in a different thread context
1509 * than the service thread...
1510 * ... and the service thread is waiting in ppoll()...
1511 * then fire a SIGUSR2 at the service thread to force it to
1512 * restart the ppoll() with our changed events
1513 */
1514 if (events != context->fds[wsi->position_in_fds_table].events) {
1515 sampled_ppoll_tid = lws_idling_ppoll_tid;
1516 if (sampled_ppoll_tid) {
1517 tid = context->protocols[0].callback(context, NULL,
1518 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1519 if (tid != sampled_ppoll_tid)
1520 kill(sampled_ppoll_tid, SIGUSR2);
1521 }
1522 }
1523#endif
1524
Andy Green91f19d82013-12-21 11:18:34 +08001525 context->protocols[0].callback(context, wsi,
1526 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001527 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001528}
1529
1530
Andy Green90c7cbc2011-01-27 06:26:52 +00001531/**
1532 * libwebsocket_callback_on_writable() - Request a callback when this socket
1533 * becomes able to be written to without
1534 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001535 *
Peter Hinz56885f32011-03-02 22:03:47 +00001536 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001537 * @wsi: Websocket connection instance to get callback for
1538 */
1539
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001540LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001541libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001542 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001543{
Andy Green3182ece2013-01-20 17:08:31 +08001544#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001545 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001546 int handled = 0;
1547
1548 /* maybe an extension will take care of it for us */
1549
1550 for (n = 0; n < wsi->count_active_extensions; n++) {
1551 if (!wsi->active_extensions[n]->callback)
1552 continue;
1553
1554 handled |= wsi->active_extensions[n]->callback(context,
1555 wsi->active_extensions[n], wsi,
1556 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1557 wsi->active_extensions_user[n], NULL, 0);
1558 }
1559
1560 if (handled)
1561 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001562#endif
Andy Greendfb23042013-01-17 12:26:48 +08001563 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001564 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1565 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001566 return -1;
1567 }
1568
Andy Green91f19d82013-12-21 11:18:34 +08001569 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001570
Andy Green90c7cbc2011-01-27 06:26:52 +00001571 return 1;
1572}
1573
1574/**
1575 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1576 * all connections using the given protocol when it
1577 * becomes possible to write to each socket without
1578 * blocking in turn.
1579 *
1580 * @protocol: Protocol whose connections will get callbacks
1581 */
1582
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001583LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001584libwebsocket_callback_on_writable_all_protocol(
1585 const struct libwebsocket_protocols *protocol)
1586{
Peter Hinz56885f32011-03-02 22:03:47 +00001587 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001588 int n;
Andy Green0d338332011-02-12 11:57:43 +00001589 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001590
Andy Greendfb23042013-01-17 12:26:48 +08001591 for (n = 0; n < context->fds_count; n++) {
1592 wsi = context->lws_lookup[context->fds[n].fd];
1593 if (!wsi)
1594 continue;
1595 if (wsi->protocol == protocol)
1596 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001597 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001598
1599 return 0;
1600}
1601
Andy Greenbe93fef2011-02-14 20:25:43 +00001602/**
Andy Greene39e6ef2014-02-15 16:36:38 +08001603 * libwebsocket_callback_all_protocol() - Callback all connections using
1604 * the given protocol with the given reason
1605 *
1606 * @protocol: Protocol whose connections will get callbacks
1607 * @reason: Callback reason index
1608 */
1609
1610LWS_VISIBLE int
1611libwebsocket_callback_all_protocol(
1612 const struct libwebsocket_protocols *protocol, int reason)
1613{
1614 struct libwebsocket_context *context = protocol->owning_server;
1615 int n;
1616 struct libwebsocket *wsi;
1617
1618 for (n = 0; n < context->fds_count; n++) {
1619 wsi = context->lws_lookup[context->fds[n].fd];
1620 if (!wsi)
1621 continue;
1622 if (wsi->protocol == protocol)
1623 protocol->callback(context, wsi,
1624 reason, wsi->user_space, NULL, 0);
1625 }
1626
1627 return 0;
1628}
1629
1630/**
Andy Greenbe93fef2011-02-14 20:25:43 +00001631 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1632 *
1633 * You will not need this unless you are doing something special
1634 *
1635 * @wsi: Websocket connection instance
1636 * @reason: timeout reason
1637 * @secs: how many seconds
1638 */
1639
Craig McQueene7fd8b12014-02-19 09:24:17 +11001640LWS_VISIBLE void
Andy Greenbe93fef2011-02-14 20:25:43 +00001641libwebsocket_set_timeout(struct libwebsocket *wsi,
1642 enum pending_timeout reason, int secs)
1643{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001644 time_t now;
Andy Greenbe93fef2011-02-14 20:25:43 +00001645
Patrick Gansterer92792b42014-02-26 21:37:31 +01001646 time(&now);
Andy Greenbe93fef2011-02-14 20:25:43 +00001647
Patrick Gansterer92792b42014-02-26 21:37:31 +01001648 wsi->pending_timeout_limit = now + secs;
Andy Greenbe93fef2011-02-14 20:25:43 +00001649 wsi->pending_timeout = reason;
1650}
1651
Andy Greena6cbece2011-01-27 20:06:03 +00001652
1653/**
1654 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1655 *
1656 * You will not need this unless you are doing something special
1657 *
1658 * @wsi: Websocket connection instance
1659 */
1660
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001661LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001662libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1663{
1664 return wsi->sock;
1665}
1666
Andy Greend636e352013-01-29 12:36:17 +08001667#ifdef LWS_LATENCY
1668void
Andy Greenb5b23192013-02-11 17:13:32 +08001669lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1670 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001671{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001672 unsigned long long u;
Andy Greend636e352013-01-29 12:36:17 +08001673 char buf[256];
1674
Patrick Gansterer92792b42014-02-26 21:37:31 +01001675 u = time_in_microseconds();
Andy Greend636e352013-01-29 12:36:17 +08001676
1677 if (action) {
1678 if (completed) {
1679 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001680 sprintf(buf,
1681 "Completion first try lat %luus: %p: ret %d: %s\n",
1682 u - wsi->latency_start,
1683 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001684 else
Andy Greenb5b23192013-02-11 17:13:32 +08001685 sprintf(buf,
1686 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1687 u - wsi->action_start,
1688 u - wsi->latency_start,
1689 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001690 wsi->action_start = 0;
1691 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001692 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1693 u - wsi->latency_start,
1694 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001695 if (u - wsi->latency_start > context->worst_latency) {
1696 context->worst_latency = u - wsi->latency_start;
1697 strcpy(context->worst_latency_info, buf);
1698 }
1699 lwsl_latency("%s", buf);
1700 } else {
1701 wsi->latency_start = u;
1702 if (!wsi->action_start)
1703 wsi->action_start = u;
1704 }
1705}
1706#endif
1707
Andy Greena1ce6be2013-01-18 11:43:21 +08001708#ifdef LWS_NO_SERVER
1709int
Andy Green8d15cf42013-10-24 21:47:06 +08001710_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001711{
1712 return 0;
1713}
1714#else
Andy Green706961d2013-01-17 16:50:35 +08001715int
1716_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1717{
1718 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001719
Andy Greenca0a1292013-03-16 11:24:23 +08001720 /* there is no pending change */
1721 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001722 return 0;
1723
Andy Greenca0a1292013-03-16 11:24:23 +08001724 /* stuff is still buffered, not ready to really accept new input */
1725 if (wsi->u.ws.rxflow_buffer) {
1726 /* get ourselves called back to deal with stashed buffer */
1727 libwebsocket_callback_on_writable(context, wsi);
1728 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001729 }
1730
Andy Greenca0a1292013-03-16 11:24:23 +08001731 /* pending is cleared, we can change rxflow state */
1732
1733 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1734
1735 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1736 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1737
1738 /* adjust the pollfd for this wsi */
1739
1740 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001741 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001742 else
Andy Green91f19d82013-12-21 11:18:34 +08001743 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001744
Andy Green706961d2013-01-17 16:50:35 +08001745 return 1;
1746}
Andy Greena1ce6be2013-01-18 11:43:21 +08001747#endif
Andy Green706961d2013-01-17 16:50:35 +08001748
Andy Green90c7cbc2011-01-27 06:26:52 +00001749/**
1750 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1751 * receieved packets.
1752 *
1753 * If the output side of a server process becomes choked, this allows flow
1754 * control for the input side.
1755 *
1756 * @wsi: Websocket connection instance to get callback for
1757 * @enable: 0 = disable read servicing for this connection, 1 = enable
1758 */
1759
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001760LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001761libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1762{
Andy Greenca0a1292013-03-16 11:24:23 +08001763 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1764 return 0;
1765
1766 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1767 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001768
Andy Green706961d2013-01-17 16:50:35 +08001769 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001770}
1771
Andy Greenb55451c2013-03-16 12:32:27 +08001772/**
1773 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1774 *
1775 * When the user server code realizes it can accept more input, it can
1776 * call this to have the RX flow restriction removed from all connections using
1777 * the given protocol.
1778 *
1779 * @protocol: all connections using this protocol will be allowed to receive
1780 */
1781
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001782LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001783libwebsocket_rx_flow_allow_all_protocol(
1784 const struct libwebsocket_protocols *protocol)
1785{
1786 struct libwebsocket_context *context = protocol->owning_server;
1787 int n;
1788 struct libwebsocket *wsi;
1789
1790 for (n = 0; n < context->fds_count; n++) {
1791 wsi = context->lws_lookup[context->fds[n].fd];
1792 if (!wsi)
1793 continue;
1794 if (wsi->protocol == protocol)
1795 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1796 }
1797}
1798
Andy Green706961d2013-01-17 16:50:35 +08001799
Andy Green2ac5a6f2011-01-28 10:00:18 +00001800/**
1801 * libwebsocket_canonical_hostname() - returns this host's hostname
1802 *
1803 * This is typically used by client code to fill in the host parameter
1804 * when making a client connection. You can only call it after the context
1805 * has been created.
1806 *
Peter Hinz56885f32011-03-02 22:03:47 +00001807 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001808 */
1809
1810
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001811LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001812libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001813{
Peter Hinz56885f32011-03-02 22:03:47 +00001814 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001815}
1816
1817
Andy Green90c7cbc2011-01-27 06:26:52 +00001818static void sigpipe_handler(int x)
1819{
1820}
1821
Andy Green6901cb32011-02-21 08:06:47 +00001822#ifdef LWS_OPENSSL_SUPPORT
1823static int
1824OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1825{
1826
1827 SSL *ssl;
1828 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001829 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001830
1831 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1832 SSL_get_ex_data_X509_STORE_CTX_idx());
1833
1834 /*
Andy Green2e24da02011-03-05 16:12:04 +00001835 * !!! nasty openssl requires the index to come as a library-scope
1836 * static
Andy Green6901cb32011-02-21 08:06:47 +00001837 */
Andy Green2e24da02011-03-05 16:12:04 +00001838 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001839
Peter Hinz56885f32011-03-02 22:03:47 +00001840 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001841 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1842 x509_ctx, ssl, preverify_ok);
1843
1844 /* convert return code from 0 = OK to 1 = OK */
1845
1846 if (!n)
1847 n = 1;
1848 else
1849 n = 0;
1850
1851 return n;
1852}
1853#endif
1854
Andy Green706961d2013-01-17 16:50:35 +08001855int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001856 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001857 struct libwebsocket *wsi,
1858 enum libwebsocket_callback_reasons reason, void *user,
1859 void *in, size_t len)
1860{
1861 int n;
1862
1863 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001864 if (!n)
1865 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001866
Andy Greenaedc9532013-02-10 21:21:24 +08001867 return n;
Andy Green706961d2013-01-17 16:50:35 +08001868}
1869
Andy Green3b3fa9e2013-12-25 16:34:37 +08001870/*
1871 * This is just used to interrupt poll waiting
1872 * we don't have to do anything with it.
1873 */
arnaudviala7c6f26c2014-02-15 14:39:40 +08001874#ifdef LWS_OPENSSL_SUPPORT
Andy Green3b3fa9e2013-12-25 16:34:37 +08001875static void lws_sigusr2(int sig)
1876{
1877}
arnaudviala7c6f26c2014-02-15 14:39:40 +08001878#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001879
Andy Greenab990e42010-10-31 12:42:52 +00001880/**
Andy Green4739e5c2011-01-22 12:51:57 +00001881 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001882 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001883 *
Andy Green1b265272013-02-09 14:01:09 +08001884 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001885 * of all initialization in one step.
1886 *
Andy Greene92cd172011-01-19 13:11:55 +00001887 * After initialization, it returns a struct libwebsocket_context * that
1888 * represents this server. After calling, user code needs to take care
1889 * of calling libwebsocket_service() with the context pointer to get the
1890 * server's sockets serviced. This can be done in the same process context
1891 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001892 *
Andy Green8f037e42010-12-19 22:13:26 +00001893 * The protocol callback functions are called for a handful of events
1894 * including http requests coming in, websocket connections becoming
1895 * established, and data arriving; it's also called periodically to allow
1896 * async transmission.
1897 *
1898 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1899 * at that time websocket protocol has not been negotiated. Other
1900 * protocols after the first one never see any HTTP callack activity.
1901 *
1902 * The server created is a simple http server by default; part of the
1903 * websocket standard is upgrading this http connection to a websocket one.
1904 *
1905 * This allows the same server to provide files like scripts and favicon /
1906 * images or whatever over http and dynamic data over websockets all in
1907 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001908 */
Andy Green4ea60062010-10-30 12:15:07 +01001909
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001910LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001911libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001912{
Peter Hinz56885f32011-03-02 22:03:47 +00001913 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001914 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001915 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001916#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001917 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001918 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001919 struct sockaddr_in serv_addr;
1920#endif
Andy Green3182ece2013-01-20 17:08:31 +08001921#ifndef LWS_NO_EXTENSIONS
1922 int m;
Andy Green1b265272013-02-09 14:01:09 +08001923 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001924#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001925
Andy Green3faa9c72010-11-08 17:03:03 +00001926#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001927 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001928#endif
1929
Joakim Soderberg4c531232013-02-06 15:26:58 +09001930#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001931 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001932#endif
1933
Andy Greenb3a614a2013-01-19 13:08:17 +08001934 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001935 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001936 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001937 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001938#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001939 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1940 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001941#else
1942 lwsl_notice(" Configured without extension support\n");
1943#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001944 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1945 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001946 if (info->ssl_cipher_list)
1947 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001948 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1949 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001950
Peter Hinz56885f32011-03-02 22:03:47 +00001951#ifdef _WIN32
1952 {
1953 WORD wVersionRequested;
1954 WSADATA wsaData;
1955 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001956 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001957
1958 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1959 wVersionRequested = MAKEWORD(2, 2);
1960
1961 err = WSAStartup(wVersionRequested, &wsaData);
1962 if (err != 0) {
1963 /* Tell the user that we could not find a usable */
1964 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001965 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001966 return NULL;
1967 }
David Galeano7b11fec2011-10-04 19:55:18 +08001968
Andy Green6ee372f2012-04-09 15:09:01 +08001969 /* default to a poll() made out of select() */
1970 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001971
Andy Green6ee372f2012-04-09 15:09:01 +08001972 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001973 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001974 if (wsdll)
1975 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001976
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001977 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001978 if (!poll)
1979 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001980 }
1981#endif
1982
Andy Greenb5b23192013-02-11 17:13:32 +08001983 context = (struct libwebsocket_context *)
1984 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001985 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001986 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001987 return NULL;
1988 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001989 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001990#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001991 context->started_with_parent = pid_daemon;
1992 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1993#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001994
Joakim Soderberg4c531232013-02-06 15:26:58 +09001995 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001996 context->protocols = info->protocols;
1997 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001998 context->http_proxy_port = 0;
1999 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08002000 context->options = info->options;
Mattias Lundberg03bb8f92014-02-18 10:06:57 +01002001 context->iface = info->iface;
Andy Greendfb23042013-01-17 12:26:48 +08002002 /* to reduce this allocation, */
2003 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08002004 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
2005 sizeof(struct libwebsocket_context),
2006 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
2007 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08002008 sizeof(struct libwebsocket_context) +
2009 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
2010 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08002011
Andy Greenb5b23192013-02-11 17:13:32 +08002012 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
2013 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002014 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002015 lwsl_err("Unable to allocate fds array for %d connections\n",
2016 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002017 free(context);
2018 return NULL;
2019 }
Andy Greenb5b23192013-02-11 17:13:32 +08002020 context->lws_lookup = (struct libwebsocket **)
2021 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002022 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002023 lwsl_err(
2024 "Unable to allocate lws_lookup array for %d connections\n",
2025 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002026 free(context->fds);
2027 free(context);
2028 return NULL;
2029 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08002030 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
2031 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08002032
Peter Hinz56885f32011-03-02 22:03:47 +00002033 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002034#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08002035 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08002036#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002037 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08002038 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00002039
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002040#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002041 context->fd_random = 0;
2042#else
2043 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2044 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002045 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002046 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002047 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00002048 }
Peter Hinz56885f32011-03-02 22:03:47 +00002049#endif
Andy Green44eee682011-02-10 09:32:24 +00002050
Peter Hinz56885f32011-03-02 22:03:47 +00002051#ifdef LWS_OPENSSL_SUPPORT
2052 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08002053 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002054 context->ssl_ctx = NULL;
2055 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002056 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002057#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002058
Andy Greena1ce6be2013-01-18 11:43:21 +08002059 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002060
Andy Greena1ce6be2013-01-18 11:43:21 +08002061#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002062 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01002063 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08002064 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08002065 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01002066
Andy Greenb5b23192013-02-11 17:13:32 +08002067 lwsl_notice(" canonical_hostname = %s\n",
2068 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002069 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002070#endif
Andy Greena69f0512012-05-03 12:32:38 +08002071
Andy Green9659f372011-01-27 22:01:43 +00002072 /* split the proxy ads:port if given */
2073
2074 p = getenv("http_proxy");
2075 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00002076 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08002077 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002078 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002079 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002080
Peter Hinz56885f32011-03-02 22:03:47 +00002081 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00002082 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002083 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002084 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00002085 }
2086 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00002087 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002088
Andy Greenb3a614a2013-01-19 13:08:17 +08002089 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002090 context->http_proxy_address,
2091 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002092 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002093
Andy Greena1ce6be2013-01-18 11:43:21 +08002094#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002095 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002096
Andy Green3faa9c72010-11-08 17:03:03 +00002097#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002098 context->use_ssl = info->ssl_cert_filepath != NULL &&
2099 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002100#ifdef USE_CYASSL
2101 lwsl_notice(" Compiled with CYASSL support\n");
2102#else
2103 lwsl_notice(" Compiled with OpenSSL support\n");
2104#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002105 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002106 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002107 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002108 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002109
Andy Green90c7cbc2011-01-27 06:26:52 +00002110#else
Andy Green2b40b792013-02-10 22:22:01 +08002111 if (info->ssl_cert_filepath != NULL &&
2112 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002113 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002114 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002115 }
Andy Greenb5b23192013-02-11 17:13:32 +08002116 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002117#endif
Andy Greena17c6922013-01-20 20:21:54 +08002118
Andy Greenb5b23192013-02-11 17:13:32 +08002119 lwsl_notice(
2120 " per-conn mem: %u + %u headers + protocol rx buf\n",
2121 sizeof(struct libwebsocket),
2122 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002123 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002124#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002125
2126 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002127#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002128#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002129 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002130#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002131
2132
2133#ifdef LWS_OPENSSL_SUPPORT
2134
2135 /* basic openssl init */
2136
2137 SSL_library_init();
2138
2139 OpenSSL_add_all_algorithms();
2140 SSL_load_error_strings();
2141
Andy Green2e24da02011-03-05 16:12:04 +00002142 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002143 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2144
Andy Green90c7cbc2011-01-27 06:26:52 +00002145 /*
2146 * Firefox insists on SSLv23 not SSLv3
2147 * Konq disables SSLv2 by default now, SSLv23 works
2148 */
2149
2150 method = (SSL_METHOD *)SSLv23_server_method();
2151 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002152 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002153 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002154 error,
2155 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002156 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002157 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002158 }
Peter Hinz56885f32011-03-02 22:03:47 +00002159 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2160 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002161 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002162 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002163 error,
2164 ERR_error_string(error,
2165 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002166 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002167 }
2168
martell19c73f32014-01-22 18:17:03 +00002169#if defined(WIN32) || defined(_WIN32)
2170#else
Andy Green3b3fa9e2013-12-25 16:34:37 +08002171 signal(SIGUSR2, lws_sigusr2);
Andy Greened451d52014-01-11 12:37:07 +08002172 {
2173 sigset_t mask;
2174 sigemptyset (&mask);
2175 sigaddset (&mask, SIGUSR2);
2176
2177 sigprocmask(SIG_BLOCK, &mask, NULL);
2178 }
martell19c73f32014-01-22 18:17:03 +00002179#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002180
David Galeanocc148e42013-01-10 10:18:59 +08002181#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002182 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002183#endif
David Galeano77a677c2013-01-10 10:14:12 +08002184 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002185 if (info->ssl_cipher_list)
2186 SSL_CTX_set_cipher_list(context->ssl_ctx,
2187 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002188
Andy Greena1ce6be2013-01-18 11:43:21 +08002189#ifndef LWS_NO_CLIENT
2190
Andy Green90c7cbc2011-01-27 06:26:52 +00002191 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002192
Andy Green1b265272013-02-09 14:01:09 +08002193 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002194 method = (SSL_METHOD *)SSLv23_client_method();
2195 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002196 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002197 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002198 error,
2199 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002200 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002201 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002202 }
2203 /* create context */
2204 context->ssl_client_ctx = SSL_CTX_new(method);
2205 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002206 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002207 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002208 error,
2209 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002210 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002211 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002212 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002213
David Galeanocc148e42013-01-10 10:18:59 +08002214#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002215 SSL_CTX_set_options(context->ssl_client_ctx,
2216 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002217#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002218 SSL_CTX_set_options(context->ssl_client_ctx,
2219 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002220 if (info->ssl_cipher_list)
2221 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2222 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002223
Peter Hinz56885f32011-03-02 22:03:47 +00002224 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002225 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002226 if (!SSL_CTX_load_verify_locations(
2227 context->ssl_client_ctx, NULL,
2228 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002229 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002230 "Unable to load SSL Client certs from %s "
2231 "(set by --with-client-cert-dir= "
2232 "in configure) -- client ssl isn't "
2233 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002234 } else
2235 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002236 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002237 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002238 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002239 "Unable to load SSL Client certs "
2240 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002241 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002242
2243 /*
2244 * callback allowing user code to load extra verification certs
2245 * helping the client to verify server identity
2246 */
2247
prasannateamf1b353a452013-11-12 17:21:01 -08002248 /* support for client-side certificate authentication */
2249 if (info->ssl_cert_filepath) {
2250 n = SSL_CTX_use_certificate_chain_file(
2251 context->ssl_client_ctx,
2252 info->ssl_cert_filepath);
2253 if (n != 1) {
2254 lwsl_err("problem getting cert '%s' %lu: %s\n",
2255 info->ssl_cert_filepath,
2256 ERR_get_error(),
2257 ERR_error_string(ERR_get_error(),
2258 (char *)context->service_buffer));
2259 goto bail;
2260 }
2261 }
2262 if (info->ssl_private_key_filepath) {
2263 /* set the private key from KeyFile */
2264 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2265 info->ssl_private_key_filepath,
2266 SSL_FILETYPE_PEM) != 1) {
2267 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2268 info->ssl_private_key_filepath,
2269 ERR_get_error(),
2270 ERR_error_string(ERR_get_error(),
2271 (char *)context->service_buffer));
2272 goto bail;
2273 }
2274
2275 /* verify private key */
2276 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2277 lwsl_err("Private SSL key doesn't match cert\n");
2278 goto bail;
2279 }
2280 }
2281
Peter Hinz56885f32011-03-02 22:03:47 +00002282 context->protocols[0].callback(context, NULL,
2283 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2284 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002285 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002286#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002287
Andy Greenc6bf2c22011-02-20 11:10:47 +00002288 /* as a server, are we requiring clients to identify themselves? */
2289
Andy Greenb5b23192013-02-11 17:13:32 +08002290 if (info->options &
2291 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002292
2293 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002294
Peter Hinz56885f32011-03-02 22:03:47 +00002295 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002296 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2297 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002298
2299 /*
2300 * give user code a chance to load certs into the server
2301 * allowing it to verify incoming client certs
2302 */
2303
Peter Hinz56885f32011-03-02 22:03:47 +00002304 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002305 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002306 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002307 }
2308
James Devine5b34c972013-12-14 11:41:29 +08002309 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2310 /* Normally SSL listener rejects non-ssl, optionally allow */
2311 context->allow_non_ssl_on_ssl_port = 1;
2312 }
2313
Peter Hinz56885f32011-03-02 22:03:47 +00002314 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002315
2316 /* openssl init for server sockets */
2317
Andy Green3faa9c72010-11-08 17:03:03 +00002318 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002319 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002320 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002321 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002322 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002323 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002324 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002325 error,
2326 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002327 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002328 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002329 }
2330 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002331 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002332 info->ssl_private_key_filepath,
2333 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002334 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002335 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002336 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002337 error,
2338 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002339 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002340 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002341 }
2342 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002343 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002344 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002345 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002346 }
2347
2348 /* SSL is happy and has a cert it's content with */
2349 }
2350#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002351
Andy Greena1ce6be2013-01-18 11:43:21 +08002352#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002353 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002354
Andy Green1b265272013-02-09 14:01:09 +08002355 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002356 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002357
Andy Green4739e5c2011-01-22 12:51:57 +00002358 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2359 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002360 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002361 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002362 }
Andy Green775c0dd2010-10-29 14:15:22 +01002363
Andy Greenb5b23192013-02-11 17:13:32 +08002364 /*
2365 * allow us to restart even if old sockets in TIME_WAIT
Andy Greenb5b23192013-02-11 17:13:32 +08002366 */
Andy Green6ee372f2012-04-09 15:09:01 +08002367 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2368 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002369
2370 /* Disable Nagle */
2371 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002372 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2373 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002374
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002375 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002376 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002377 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002378 #else
Andy Greene2160712013-01-28 12:19:10 +08002379 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002380 #endif
Andy Greene2160712013-01-28 12:19:10 +08002381
Andy Green4739e5c2011-01-22 12:51:57 +00002382 bzero((char *) &serv_addr, sizeof(serv_addr));
2383 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002384 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002385 serv_addr.sin_addr.s_addr = INADDR_ANY;
2386 else
Andy Greend1eac602013-11-09 08:07:38 +08002387 if (interface_to_sa(info->iface, &serv_addr,
2388 sizeof(serv_addr)) < 0) {
2389 lwsl_err("Unable to find interface %s\n",
2390 info->iface);
2391 compatible_close(sockfd);
2392 goto bail;
2393 }
Andy Green1b265272013-02-09 14:01:09 +08002394 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002395
2396 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2397 sizeof(serv_addr));
2398 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002399 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002400 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002401 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002402 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002403 }
Andy Green0d338332011-02-12 11:57:43 +00002404
Andy Greenb5b23192013-02-11 17:13:32 +08002405 wsi = (struct libwebsocket *)malloc(
2406 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002407 if (wsi == NULL) {
2408 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002409 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002410 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002411 }
Andy Greenb5b23192013-02-11 17:13:32 +08002412 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002413 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002414#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002415 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002416#endif
Andy Green0d338332011-02-12 11:57:43 +00002417 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002418
2419 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002420
Andy Green65b0e912013-01-16 07:59:47 +08002421 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2422 context->listen_service_count = 0;
2423 context->listen_service_fd = sockfd;
2424
Andy Greena824d182013-01-15 20:52:29 +08002425 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002426 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002427 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002428#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002429
Andy Green6ee372f2012-04-09 15:09:01 +08002430 /*
2431 * drop any root privs for this process
2432 * to listen on port < 1023 we would have needed root, but now we are
2433 * listening, we don't want the power for anything else
2434 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002435#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002436#else
Andy Green1b265272013-02-09 14:01:09 +08002437 if (info->gid != -1)
2438 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002439 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002440 if (info->uid != -1)
2441 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002442 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002443#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002444
Andy Green6f520a52013-01-29 17:57:39 +08002445 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002446
Peter Hinz56885f32011-03-02 22:03:47 +00002447 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002448 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002449 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002450
Andy Green43db0452013-01-10 19:50:35 +08002451 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002452 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002453
Andy Green1b265272013-02-09 14:01:09 +08002454 info->protocols[context->count_protocols].owning_server =
2455 context;
2456 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002457 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002458
2459 /*
2460 * inform all the protocols that they are doing their one-time
2461 * initialization if they want to
2462 */
2463 info->protocols[context->count_protocols].callback(context,
2464 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002465 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002466
Andy Green3182ece2013-01-20 17:08:31 +08002467#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002468 /*
2469 * give all extensions a chance to create any per-context
2470 * allocations they need
2471 */
2472
2473 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002474 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002475 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002476
Andy Green1b265272013-02-09 14:01:09 +08002477 if (info->extensions) {
2478 ext = info->extensions;
2479 while (ext->callback) {
2480 lwsl_ext(" Extension: %s\n", ext->name);
2481 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002482 (enum libwebsocket_extension_callback_reasons)m,
2483 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002484 ext++;
2485 }
Andy Greena41314f2011-05-23 10:00:03 +01002486 }
Andy Green3182ece2013-01-20 17:08:31 +08002487#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002488
Peter Hinz56885f32011-03-02 22:03:47 +00002489 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002490
2491bail:
2492 libwebsocket_context_destroy(context);
2493 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002494}
Andy Greenb45993c2010-12-18 15:13:50 +00002495
Andy Greenb45993c2010-12-18 15:13:50 +00002496/**
shysb4e800e2013-10-24 22:12:03 +08002497 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2498 * @context: pointer to struct libwebsocket_context you want set proxy to
2499 * @proxy: pointer to c string containing proxy in format address:port
2500 *
2501 * Returns 0 if proxy string was parsed and proxy was setup.
2502 * Returns -1 if @proxy is NULL or has incorrect format.
2503 *
2504 * This is only required if your OS does not provide the http_proxy
2505 * enviroment variable (eg, OSX)
2506 *
2507 * IMPORTANT! You should call this function right after creation of the
2508 * libwebsocket_context and before call to connect. If you call this
2509 * function after connect behavior is undefined.
2510 * This function will override proxy settings made on libwebsocket_context
2511 * creation with genenv() call.
2512 */
2513
2514LWS_VISIBLE int
2515libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2516{
2517 char *p;
2518
2519 if (!proxy)
2520 return -1;
2521
2522 strncpy(context->http_proxy_address, proxy,
2523 sizeof(context->http_proxy_address) - 1);
2524 context->http_proxy_address[
2525 sizeof(context->http_proxy_address) - 1] = '\0';
2526
2527 p = strchr(context->http_proxy_address, ':');
2528 if (!p) {
2529 lwsl_err("http_proxy needs to be ads:port\n");
2530
2531 return -1;
2532 }
2533 *p = '\0';
2534 context->http_proxy_port = atoi(p + 1);
2535
2536 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2537 context->http_proxy_port);
2538
2539 return 0;
2540}
2541
2542/**
Andy Greenb45993c2010-12-18 15:13:50 +00002543 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002544 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002545 * @wsi: pointer to struct websocket you want to know the protocol of
2546 *
Andy Green8f037e42010-12-19 22:13:26 +00002547 *
Andy Green6f520a52013-01-29 17:57:39 +08002548 * Some apis can act on all live connections of a given protocol,
2549 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002550 */
Andy Greenab990e42010-10-31 12:42:52 +00002551
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002552LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002553libwebsockets_get_protocol(struct libwebsocket *wsi)
2554{
2555 return wsi->protocol;
2556}
2557
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002558LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002559libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2560{
Andy Green623a98d2013-01-21 11:04:23 +08002561 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002562}
Alex Bligh49146db2011-11-07 17:19:25 +08002563
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002564LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002565libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2566{
Andy Green623a98d2013-01-21 11:04:23 +08002567 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002568}
2569
Andy Green2af4d5b2013-02-18 16:30:10 +08002570int
Alex Bligh49146db2011-11-07 17:19:25 +08002571libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2572{
Andy Green67d556c2013-02-15 22:31:55 +08002573 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002574 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002575
Alex Bligh49146db2011-11-07 17:19:25 +08002576 /* allocate the per-connection user memory (if any) */
2577
2578 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2579 wsi->user_space = malloc(
2580 wsi->protocol->per_session_data_size);
2581 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002582 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002583 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002584 }
Andy Green6ee372f2012-04-09 15:09:01 +08002585 memset(wsi->user_space, 0,
2586 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002587 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002588 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002589}
Andy Green43db0452013-01-10 19:50:35 +08002590
Andy Green0b319092013-01-19 11:17:56 +08002591static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002592{
Andy Green0b319092013-01-19 11:17:56 +08002593 char buf[300];
Patrick Gansterer92792b42014-02-26 21:37:31 +01002594 unsigned long long now;
Andy Green0b319092013-01-19 11:17:56 +08002595 int n;
2596
Andy Green0b319092013-01-19 11:17:56 +08002597 buf[0] = '\0';
2598 for (n = 0; n < LLL_COUNT; n++)
2599 if (level == (1 << n)) {
Patrick Gansterer92792b42014-02-26 21:37:31 +01002600 now = time_in_microseconds() / 100;
2601 sprintf(buf, "[%lu:%04d] %s: ", (unsigned long) now / 10000,
2602 (int)(now % 10000), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002603 break;
2604 }
Andy Greenb5b23192013-02-11 17:13:32 +08002605
Andy Green0b319092013-01-19 11:17:56 +08002606 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002607}
2608
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002609#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002610LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002611{
2612 lwsl_emit_stderr(level, line);
2613}
2614#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002615LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002616{
2617 int syslog_level = LOG_DEBUG;
2618
2619 switch (level) {
2620 case LLL_ERR:
2621 syslog_level = LOG_ERR;
2622 break;
2623 case LLL_WARN:
2624 syslog_level = LOG_WARNING;
2625 break;
2626 case LLL_NOTICE:
2627 syslog_level = LOG_NOTICE;
2628 break;
2629 case LLL_INFO:
2630 syslog_level = LOG_INFO;
2631 break;
2632 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002633 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002634}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002635#endif
Andy Greenc11db202013-01-19 11:12:16 +08002636
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002637LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002638{
Andy Greende8f27a2013-01-12 09:17:42 +08002639 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002640 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002641
2642 if (!(log_level & filter))
2643 return;
2644
Andy Green43db0452013-01-10 19:50:35 +08002645 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002646 vsnprintf(buf, sizeof(buf), format, ap);
2647 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002648 va_end(ap);
2649
Andy Green0b319092013-01-19 11:17:56 +08002650 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002651}
2652
2653/**
2654 * lws_set_log_level() - Set the logging bitfield
2655 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002656 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2657 * function to perform log string emission instead of
2658 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002659 *
Andy Greenc6511a02013-02-19 10:01:48 +08002660 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002661 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002662 */
2663
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002664LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002665 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002666{
2667 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002668 if (log_emit_function)
2669 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002670}
Andy Green92f9b162014-02-21 18:43:42 +08002671
2672int
2673interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
2674{
2675 int rc = -1;
2676#if defined(WIN32) || defined(_WIN32)
2677 /* TODO */
2678#else
2679 struct ifaddrs *ifr;
2680 struct ifaddrs *ifc;
2681 struct sockaddr_in *sin;
2682
2683 getifaddrs(&ifr);
2684 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
2685 if (ifc->ifa_addr == NULL)
2686 continue;
2687 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
2688 if (strcmp(ifc->ifa_name, ifname))
2689 continue;
2690 sin = (struct sockaddr_in *)ifc->ifa_addr;
2691 if (sin->sin_family != AF_INET)
2692 continue;
2693 memcpy(addr, sin, addrlen);
2694 rc = 0;
2695 }
2696
2697 freeifaddrs(ifr);
2698#endif
2699 return rc;
2700}
2701