blob: 3cc5635f73161b7bdb828e51072e2325fd670c33 [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>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010026#include <mstcpip.h>
Patrick Gansterer5b71aac2014-02-28 01:31:39 +010027#ifdef _WIN32_WCE
28#define vsnprintf _vsnprintf
29#endif
Peter Hinz56885f32011-03-02 22:03:47 +000030#else
Davidc4ef7b12013-01-12 20:39:47 +080031#ifdef LWS_BUILTIN_GETIFADDRS
32#include <getifaddrs.h>
33#else
Peter Hinz56885f32011-03-02 22:03:47 +000034#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080035#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090036#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000037#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080038#include <sys/socket.h>
39#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000040#endif
Andy Green2e24da02011-03-05 16:12:04 +000041
Patrick Gansterere5720a32014-02-28 00:57:19 +010042#ifdef HAVE_SYS_TYPES_H
Andy Green3b3fa9e2013-12-25 16:34:37 +080043#include <sys/types.h>
Patrick Gansterere5720a32014-02-28 00:57:19 +010044#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +080045
Andy Green2e24da02011-03-05 16:12:04 +000046#ifdef LWS_OPENSSL_SUPPORT
47int openssl_websocket_private_data_index;
48#endif
49
Andy Greenaa6fc442012-04-12 13:26:49 +080050#ifdef __MINGW32__
51#include "../win32port/win32helpers/websock-w32.c"
52#else
53#ifdef __MINGW64__
54#include "../win32port/win32helpers/websock-w32.c"
55#endif
56#endif
57
Andy Green7b405452013-02-01 10:50:15 +080058#ifndef LWS_BUILD_HASH
59#define LWS_BUILD_HASH "unknown-build-hash"
60#endif
Andy Green3182ece2013-01-20 17:08:31 +080061
Andy Green7c19c342013-01-19 12:18:07 +080062static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080063static void lwsl_emit_stderr(int level, const char *line);
64static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
65
Andy Green7b405452013-02-01 10:50:15 +080066static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
67
Andy Greenb5b23192013-02-11 17:13:32 +080068static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080069 "ERR",
70 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080071 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080072 "INFO",
73 "DEBUG",
74 "PARSER",
75 "HEADER",
76 "EXTENSION",
77 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080078 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080079};
80
Andy Greenb5b23192013-02-11 17:13:32 +080081#ifndef LWS_NO_CLIENT
82 extern int lws_client_socket_service(
83 struct libwebsocket_context *context,
84 struct libwebsocket *wsi, struct pollfd *pollfd);
85#endif
86#ifndef LWS_NO_SERVER
87 extern int lws_server_socket_service(
88 struct libwebsocket_context *context,
89 struct libwebsocket *wsi, struct pollfd *pollfd);
90#endif
91
Andy Green3b3fa9e2013-12-25 16:34:37 +080092
Andy Green7b405452013-02-01 10:50:15 +080093/**
94 * lws_get_library_version: get version and git hash library built from
95 *
96 * returns a const char * to a string like "1.1 178d78c"
97 * representing the library version followed by the git head hash it
98 * was built from
99 */
100
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800101LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +0800102lws_get_library_version(void)
103{
104 return library_version;
105}
106
Patrick Gansterer92792b42014-02-26 21:37:31 +0100107static unsigned long long time_in_microseconds()
108{
109#if defined(WIN32) || defined(_WIN32)
110#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
111 FILETIME filetime;
112 ULARGE_INTEGER datetime;
113
114#ifdef _WIN32_WCE
115 GetCurrentFT(&filetime);
116#else
117 GetSystemTimeAsFileTime(&filetime);
118#endif
119
120 /*
121 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
122 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
123 * prevent alignment faults on 64-bit Windows).
124 */
125 memcpy(&datetime, &filetime, sizeof(datetime));
126
127 /* Windows file times are in 100s of nanoseconds. */
128 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
129#else
130 struct timeval tv;
131 gettimeofday(&tv, NULL);
132 return (tv.tv_sec * 1000000) + tv.tv_usec;
133#endif
134}
135
Patrick Gansterer5b71aac2014-02-28 01:31:39 +0100136#ifdef _WIN32_WCE
137static inline time_t time(time_t *t)
138{
139 time_t ret = time_in_microseconds() / 1000000;
140 *t = ret;
141 return ret;
142}
143#endif
144
Andy Green0d338332011-02-12 11:57:43 +0000145int
Andy Greenb5b23192013-02-11 17:13:32 +0800146insert_wsi_socket_into_fds(struct libwebsocket_context *context,
147 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000148{
Joakim Soderberg6596e4b2014-02-18 13:38:14 +0100149 struct libwebsocket_pollargs pa = { wsi->sock, POLLIN, 0 };
150
Andy Greendfb23042013-01-17 12:26:48 +0800151 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800152 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000153 return 1;
154 }
155
nononamef1624922014-01-11 13:12:34 +0800156 if (wsi->sock >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800157 lwsl_err("Socket fd %d is too high (%d)\n",
158 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800159 return 1;
160 }
161
162 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800163 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800164
Andy Greenb5b23192013-02-11 17:13:32 +0800165 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
166 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800167
Andy Green7a132792013-12-18 09:48:26 +0800168 context->protocols[0].callback(context, wsi,
169 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800170 wsi->user_space, (void *) &pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800171
Andy Greendfb23042013-01-17 12:26:48 +0800172 context->lws_lookup[wsi->sock] = wsi;
173 wsi->position_in_fds_table = context->fds_count;
174 context->fds[context->fds_count].fd = wsi->sock;
175 context->fds[context->fds_count].events = POLLIN;
176 context->fds[context->fds_count++].revents = 0;
177
178 /* external POLL support via protocol 0 */
179 context->protocols[0].callback(context, wsi,
180 LWS_CALLBACK_ADD_POLL_FD,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800181 wsi->user_space, (void *) &pa, 0);
Andy Green0d338332011-02-12 11:57:43 +0000182
Andy Green7a132792013-12-18 09:48:26 +0800183 context->protocols[0].callback(context, wsi,
184 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800185 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800186
Andy Green0d338332011-02-12 11:57:43 +0000187 return 0;
188}
189
Andy Greendfb23042013-01-17 12:26:48 +0800190static int
Andy Greenb5b23192013-02-11 17:13:32 +0800191remove_wsi_socket_from_fds(struct libwebsocket_context *context,
192 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000193{
Andy Greendfb23042013-01-17 12:26:48 +0800194 int m;
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800195 struct libwebsocket_pollargs pa = { wsi->sock, 0, 0};
Andy Green0d338332011-02-12 11:57:43 +0000196
Andy Green7a132792013-12-18 09:48:26 +0800197 if (!--context->fds_count) {
198 context->protocols[0].callback(context, wsi,
199 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800200 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800201 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800202 }
Andy Green0d338332011-02-12 11:57:43 +0000203
Andy Greendfb23042013-01-17 12:26:48 +0800204 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800205 lwsl_err("Socket fd %d too high (%d)\n",
206 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800207 return 1;
208 }
Andy Green0d338332011-02-12 11:57:43 +0000209
Andy Greenb5b23192013-02-11 17:13:32 +0800210 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
211 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800212
Andy Green7a132792013-12-18 09:48:26 +0800213 context->protocols[0].callback(context, wsi,
214 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800215 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800216
Andy Greendfb23042013-01-17 12:26:48 +0800217 m = wsi->position_in_fds_table; /* replace the contents for this */
218
219 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800220 context->fds[m] = context->fds[context->fds_count];
221 /*
222 * end guy's fds_lookup entry remains unchanged
223 * (still same fd pointing to same wsi)
224 */
Andy Greendfb23042013-01-17 12:26:48 +0800225 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800226 context->lws_lookup[context->fds[context->fds_count].fd]->
227 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800228 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800229 context->lws_lookup[wsi->sock] = NULL;
230 /* removed wsi has no position any more */
231 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800232
233do_ext:
234 /* remove also from external POLL support via protocol 0 */
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800235 if (wsi->sock) {
Andy Greendfb23042013-01-17 12:26:48 +0800236 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800237 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800238 (void *) &pa, 0);
239 }
Andy Green7a132792013-12-18 09:48:26 +0800240 context->protocols[0].callback(context, wsi,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800241 LWS_CALLBACK_UNLOCK_POLL,
242 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800243 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000244}
245
Andy Green32375b72011-02-19 08:32:53 +0000246
Andy Green8f037e42010-12-19 22:13:26 +0000247void
Peter Hinz56885f32011-03-02 22:03:47 +0000248libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000249 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000250{
Andy Greenb45993c2010-12-18 15:13:50 +0000251 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000252 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000253 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
254 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800255#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000256 int ret;
257 int m;
258 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100259 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800260#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000261
Andy Green4b6fbe12011-02-14 08:03:48 +0000262 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000263 return;
264
Andy Green62c54d22011-02-14 09:14:25 +0000265 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000266
Andy Green62c54d22011-02-14 09:14:25 +0000267 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000268 return;
269
Andy Green22524a62013-02-15 22:48:58 +0800270 /* we tried the polite way... */
271 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
272 goto just_kill_connection;
273
Andy Green623a98d2013-01-21 11:04:23 +0800274 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000275
Andy Green5dc62ea2013-09-20 20:26:12 +0800276 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
277 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
278
279 context->protocols[0].callback(context, wsi,
280 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
281
282 free(wsi->u.hdr.ah);
283 goto just_kill_connection;
284 }
285
286
kapejodce64fb02013-11-19 13:38:16 +0100287 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
288 if (wsi->u.http.post_buffer) {
289 free(wsi->u.http.post_buffer);
290 wsi->u.http.post_buffer = NULL;
291 }
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100292 if (wsi->u.http.fd != LWS_INVALID_FILE) {
293 lwsl_debug("closing http file\n");
294#if defined(WIN32) || defined(_WIN32)
295 CloseHandle(wsi->u.http.fd);
296#else
kapejodce64fb02013-11-19 13:38:16 +0100297 close(wsi->u.http.fd);
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100298#endif
299 wsi->u.http.fd = LWS_INVALID_FILE;
kapejodce64fb02013-11-19 13:38:16 +0100300 context->protocols[0].callback(context, wsi,
301 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
302 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800303 }
304
Andy Green3182ece2013-01-20 17:08:31 +0800305#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000306 /*
Andy Green68b45042011-05-25 21:41:57 +0100307 * are his extensions okay with him closing? Eg he might be a mux
308 * parent and just his ch1 aspect is closing?
309 */
310
Andy Green68b45042011-05-25 21:41:57 +0100311 for (n = 0; n < wsi->count_active_extensions; n++) {
312 if (!wsi->active_extensions[n]->callback)
313 continue;
314
315 m = wsi->active_extensions[n]->callback(context,
316 wsi->active_extensions[n], wsi,
317 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
318 wsi->active_extensions_user[n], NULL, 0);
319
320 /*
321 * if somebody vetoed actually closing him at this time....
322 * up to the extension to track the attempted close, let's
323 * just bail
324 */
325
326 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800327 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100328 return;
329 }
330 }
331
Andy Green68b45042011-05-25 21:41:57 +0100332 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000333 * flush any tx pending from extensions, since we may send close packet
334 * if there are problems with send, just nuke the connection
335 */
336
337 ret = 1;
338 while (ret == 1) {
339
340 /* default to nobody has more to spill */
341
342 ret = 0;
343 eff_buf.token = NULL;
344 eff_buf.token_len = 0;
345
346 /* show every extension the new incoming data */
347
348 for (n = 0; n < wsi->count_active_extensions; n++) {
349 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000350 wsi->protocol->owning_server,
351 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000352 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
353 wsi->active_extensions_user[n], &eff_buf, 0);
354 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800355 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000356 goto just_kill_connection;
357 }
358 if (m)
359 /*
360 * at least one extension told us he has more
361 * to spill, so we will go around again after
362 */
363 ret = 1;
364 }
365
366 /* assuming they left us something to send, send it */
367
368 if (eff_buf.token_len)
369 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800370 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800371 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000372 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800373 }
Andy Greenc44159f2011-03-07 07:08:18 +0000374 }
Andy Green3182ece2013-01-20 17:08:31 +0800375#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000376
377 /*
Andy Greenda527df2011-03-07 07:08:12 +0000378 * signal we are closing, libsocket_write will
379 * add any necessary version-specific stuff. If the write fails,
380 * no worries we are closing anyway. If we didn't initiate this
381 * close, then our state has been changed to
382 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
383 *
384 * Likewise if it's a second call to close this connection after we
385 * sent the close indication to the peer already, we are in state
386 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
387 */
388
389 if (old_state == WSI_STATE_ESTABLISHED &&
390 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100391
Andy Green43db0452013-01-10 19:50:35 +0800392 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100393
Andy Greenfdd305a2013-02-11 14:32:48 +0800394 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800395 memset(buf, 0, sizeof(buf));
396 n = libwebsocket_write(wsi,
397 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000398 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800399 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000400 /*
401 * we have sent a nice protocol level indication we
402 * now wish to close, we should not send anything more
403 */
404
405 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
406
Andy Greenb5b23192013-02-11 17:13:32 +0800407 /*
408 * ...and we should wait for a reply for a bit
409 * out of politeness
410 */
Andy Greenda527df2011-03-07 07:08:12 +0000411
412 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800413 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000414
Andy Green43db0452013-01-10 19:50:35 +0800415 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000416
417 return;
418 }
419
Andy Greenb5b23192013-02-11 17:13:32 +0800420 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800421
Andy Greenda527df2011-03-07 07:08:12 +0000422 /* else, the send failed and we should just hang up */
423 }
424
Andy Greenc44159f2011-03-07 07:08:18 +0000425just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100426
Andy Greenb5b23192013-02-11 17:13:32 +0800427 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100428
Andy Greenda527df2011-03-07 07:08:12 +0000429 /*
430 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800431 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000432 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000433
Andy Greendfb23042013-01-17 12:26:48 +0800434 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000435
Andy Green251f6fa2010-11-03 11:13:06 +0000436 wsi->state = WSI_STATE_DEAD_SOCKET;
437
Andy Greenb5b23192013-02-11 17:13:32 +0800438 if ((old_state == WSI_STATE_ESTABLISHED ||
439 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800440 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
441
442 if (wsi->u.ws.rx_user_buffer) {
443 free(wsi->u.ws.rx_user_buffer);
444 wsi->u.ws.rx_user_buffer = NULL;
445 }
446 if (wsi->u.ws.rxflow_buffer) {
447 free(wsi->u.ws.rxflow_buffer);
448 wsi->u.ws.rxflow_buffer = NULL;
449 }
Andy Green2764eba2013-12-09 14:16:17 +0800450 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800451 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800452 free(wsi->truncated_send_malloc);
453 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800454 }
Andy Green54495112013-02-06 21:10:16 +0900455 }
456
Andy Green4b6fbe12011-02-14 08:03:48 +0000457 /* tell the user it's all over for this guy */
458
Andy Greend4302732011-02-28 07:45:29 +0000459 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800460 ((old_state == WSI_STATE_ESTABLISHED) ||
461 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
462 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800463 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000464 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000465 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800466 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
467 lwsl_debug("calling back CLOSED_HTTP\n");
468 context->protocols[0].callback(context, wsi,
469 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800470 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800471 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000472
Andy Green3182ece2013-01-20 17:08:31 +0800473#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000474 /* deallocate any active extension contexts */
475
476 for (n = 0; n < wsi->count_active_extensions; n++) {
477 if (!wsi->active_extensions[n]->callback)
478 continue;
479
Andy Green46c2ea02011-03-22 09:04:01 +0000480 wsi->active_extensions[n]->callback(context,
481 wsi->active_extensions[n], wsi,
482 LWS_EXT_CALLBACK_DESTROY,
483 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000484
485 free(wsi->active_extensions_user[n]);
486 }
487
Andy Greena41314f2011-05-23 10:00:03 +0100488 /*
489 * inform all extensions in case they tracked this guy out of band
490 * even though not active on him specifically
491 */
492
493 ext = context->extensions;
494 while (ext && ext->callback) {
495 ext->callback(context, ext, wsi,
496 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
497 NULL, NULL, 0);
498 ext++;
499 }
Andy Green3182ece2013-01-20 17:08:31 +0800500#endif
Andy Greena41314f2011-05-23 10:00:03 +0100501
Andy Green43db0452013-01-10 19:50:35 +0800502/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000503
Andy Green3faa9c72010-11-08 17:03:03 +0000504#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000505 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000506 n = SSL_get_fd(wsi->ssl);
507 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800508 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000509 SSL_free(wsi->ssl);
510 } else {
511#endif
Andy Green0303db42013-01-17 14:46:43 +0800512 if (wsi->sock) {
513 n = shutdown(wsi->sock, SHUT_RDWR);
514 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800515 lwsl_debug("closing: shutdown returned %d\n",
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100516 LWS_ERRNO);
Andy Green3fc2c652013-01-14 15:35:02 +0800517
Andy Green0303db42013-01-17 14:46:43 +0800518 n = compatible_close(wsi->sock);
519 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800520 lwsl_debug("closing: close returned %d\n",
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100521 LWS_ERRNO);
Andy Green0303db42013-01-17 14:46:43 +0800522 }
Andy Green3faa9c72010-11-08 17:03:03 +0000523#ifdef LWS_OPENSSL_SUPPORT
524 }
525#endif
Andy Green76b6ea12014-02-15 19:25:50 +0800526
527 /* outermost destroy notification for wsi (user_space still intact) */
528 context->protocols[0].callback(context, wsi,
529 LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0);
530
Andy Greenb5b23192013-02-11 17:13:32 +0800531 if (wsi->protocol && wsi->protocol->per_session_data_size &&
532 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000533 free(wsi->user_space);
534
Andy Green251f6fa2010-11-03 11:13:06 +0000535 free(wsi);
536}
537
Andy Green07034092011-02-13 08:37:12 +0000538/**
539 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800540 * @context: Libwebsockets context
541 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000542 * @fd: Connection socket descriptor
543 * @name: Buffer to take client address name
544 * @name_len: Length of client address name buffer
545 * @rip: Buffer to take client address IP qotted quad
546 * @rip_len: Length of client address IP buffer
547 *
548 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800549 * the client connected with socket descriptor @fd. Names may be
550 * truncated if there is not enough room. If either cannot be
551 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000552 */
553
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800554LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800555libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
556 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000557 char *rip, int rip_len)
558{
Bob Robertsac049112013-04-25 09:16:30 +0800559 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000560 struct sockaddr_in sin;
561 struct hostent *host;
562 struct hostent *host1;
563 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000564 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000565 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800566 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800567#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800568 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800569#endif
Andy Green07034092011-02-13 08:37:12 +0000570
571 rip[0] = '\0';
572 name[0] = '\0';
573
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800574 lws_latency_pre(context, wsi);
575
Andy Greenb5b23192013-02-11 17:13:32 +0800576 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000577 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100578 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800579 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000580 }
Andy Green6ee372f2012-04-09 15:09:01 +0800581
Andy Greenb5b23192013-02-11 17:13:32 +0800582 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000583 AF_INET);
584 if (host == NULL) {
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100585 lwsl_warn("gethostbyaddr: %s\n", strerror(LWS_ERRNO));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800586 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000587 }
588
589 strncpy(name, host->h_name, name_len);
590 name[name_len - 1] = '\0';
591
592 host1 = gethostbyname(host->h_name);
593 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800594 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000595 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000596 n = 0;
597 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000598 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000599 if (p == NULL)
600 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000601 if ((host1->h_addrtype != AF_INET)
602#ifdef AF_LOCAL
603 && (host1->h_addrtype != AF_LOCAL)
604#endif
605 )
Andy Green07034092011-02-13 08:37:12 +0000606 continue;
607
Andy Green7627af52011-03-09 15:13:52 +0000608 if (host1->h_addrtype == AF_INET)
609 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000610#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000611 else {
612 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800613 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000614 ip[sizeof(ip) - 1] = '\0';
615 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000616#endif
Andy Green07034092011-02-13 08:37:12 +0000617 p = NULL;
618 strncpy(rip, ip, rip_len);
619 rip[rip_len - 1] = '\0';
620 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800621
622 ret = 0;
623bail:
624 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000625}
Andy Green9f990342011-02-12 11:57:45 +0000626
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800627LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000628 void *buf, int len)
629{
630 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800631 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000632
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100633#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000634 for (n = 0; n < len; n++)
635 p[n] = (unsigned char)rand();
636#else
637 n = read(context->fd_random, p, len);
638#endif
639
640 return n;
641}
642
Andy Greena690cd02013-02-09 12:25:31 +0800643int lws_set_socket_options(struct libwebsocket_context *context, int fd)
644{
645 int optval = 1;
646 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100647#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800648 unsigned long optl = 0;
649#endif
650#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
651 struct protoent *tcp_proto;
652#endif
653
654 if (context->ka_time) {
655 /* enable keepalive on this socket */
656 optval = 1;
657 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
658 (const void *)&optval, optlen) < 0)
659 return 1;
660
Bob Robertsac049112013-04-25 09:16:30 +0800661#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800662
663 /*
664 * didn't find a way to set these per-socket, need to
665 * tune kernel systemwide values
666 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100667#elif WIN32
668 {
669 DWORD dwBytesRet;
670 struct tcp_keepalive alive;
671 alive.onoff = TRUE;
672 alive.keepalivetime = context->ka_time;
673 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800674
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100675 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
676 NULL, 0, &dwBytesRet, NULL, NULL))
677 return 1;
678 }
Andy Greena47865f2013-02-10 09:39:47 +0800679#else
Andy Greena690cd02013-02-09 12:25:31 +0800680 /* set the keepalive conditions we want on it too */
681 optval = context->ka_time;
682 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
683 (const void *)&optval, optlen) < 0)
684 return 1;
685
Larry Hayesbb66ac62013-02-22 09:16:20 +0800686 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800687 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
688 (const void *)&optval, optlen) < 0)
689 return 1;
690
Larry Hayesbb66ac62013-02-22 09:16:20 +0800691 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800692 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
693 (const void *)&optval, optlen) < 0)
694 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800695#endif
Andy Greena690cd02013-02-09 12:25:31 +0800696 }
697
698 /* Disable Nagle */
699 optval = 1;
700#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
701 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
702#else
703 tcp_proto = getprotobyname("TCP");
704 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
705#endif
706
707 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100708#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800709 ioctlsocket(fd, FIONBIO, &optl);
710#else
711 fcntl(fd, F_SETFL, O_NONBLOCK);
712#endif
713
714 return 0;
715}
716
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800717LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000718{
719 struct pollfd fds;
720
Andy Green2764eba2013-12-09 14:16:17 +0800721 /* treat the fact we got a truncated send pending as if we're choked */
722 if (wsi->truncated_send_malloc)
723 return 1;
724
Andy Green95a7b5d2011-03-06 10:29:39 +0000725 fds.fd = wsi->sock;
726 fds.events = POLLOUT;
727 fds.revents = 0;
728
729 if (poll(&fds, 1, 0) != 1)
730 return 1;
731
732 if ((fds.revents & POLLOUT) == 0)
733 return 1;
734
735 /* okay to send another packet without blocking */
736
737 return 0;
738}
739
Andy Greena41314f2011-05-23 10:00:03 +0100740int
Andy Green3b84c002011-03-06 13:14:42 +0000741lws_handle_POLLOUT_event(struct libwebsocket_context *context,
742 struct libwebsocket *wsi, struct pollfd *pollfd)
743{
Andy Green3b84c002011-03-06 13:14:42 +0000744 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800745
Andy Green3182ece2013-01-20 17:08:31 +0800746#ifndef LWS_NO_EXTENSIONS
747 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000748 int ret;
749 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100750 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000751
Andy Green1f4267b2013-10-17 08:09:19 +0800752 /* pending truncated sends have uber priority */
753
Andy Green2764eba2013-12-09 14:16:17 +0800754 if (wsi->truncated_send_malloc) {
755 lws_issue_raw(wsi, wsi->truncated_send_malloc +
756 wsi->truncated_send_offset,
757 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800758 /* leave POLLOUT active either way */
759 return 0;
760 }
761
Andy Greena41314f2011-05-23 10:00:03 +0100762 for (n = 0; n < wsi->count_active_extensions; n++) {
763 if (!wsi->active_extensions[n]->callback)
764 continue;
765
766 m = wsi->active_extensions[n]->callback(context,
767 wsi->active_extensions[n], wsi,
768 LWS_EXT_CALLBACK_IS_WRITEABLE,
769 wsi->active_extensions_user[n], NULL, 0);
770 if (m > handled)
771 handled = m;
772 }
773
774 if (handled == 1)
775 goto notify_action;
776
777 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000778 goto user_service;
779
780 /*
781 * check in on the active extensions, see if they
782 * had pending stuff to spill... they need to get the
783 * first look-in otherwise sequence will be disordered
784 *
785 * NULL, zero-length eff_buf means just spill pending
786 */
787
788 ret = 1;
789 while (ret == 1) {
790
791 /* default to nobody has more to spill */
792
793 ret = 0;
794 eff_buf.token = NULL;
795 eff_buf.token_len = 0;
796
797 /* give every extension a chance to spill */
798
799 for (n = 0; n < wsi->count_active_extensions; n++) {
800 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000801 wsi->protocol->owning_server,
802 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000803 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
804 wsi->active_extensions_user[n], &eff_buf, 0);
805 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800806 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000807 return -1;
808 }
809 if (m)
810 /*
811 * at least one extension told us he has more
812 * to spill, so we will go around again after
813 */
814 ret = 1;
815 }
816
817 /* assuming they gave us something to send, send it */
818
819 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800820 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
821 eff_buf.token_len);
822 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000823 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800824 /*
825 * Keep amount spilled small to minimize chance of this
826 */
827 if (n != eff_buf.token_len) {
828 lwsl_err("Unable to spill ext %d vs %s\n",
829 eff_buf.token_len, n);
830 return -1;
831 }
Andy Green3b84c002011-03-06 13:14:42 +0000832 } else
833 continue;
834
835 /* no extension has more to spill */
836
837 if (!ret)
838 continue;
839
840 /*
841 * There's more to spill from an extension, but we just sent
842 * something... did that leave the pipe choked?
843 */
844
845 if (!lws_send_pipe_choked(wsi))
846 /* no we could add more */
847 continue;
848
Andy Green43db0452013-01-10 19:50:35 +0800849 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000850
851 /*
852 * Yes, he's choked. Leave the POLLOUT masked on so we will
853 * come back here when he is unchoked. Don't call the user
854 * callback to enforce ordering of spilling, he'll get called
855 * when we come back here and there's nothing more to spill.
856 */
857
858 return 0;
859 }
860
861 wsi->extension_data_pending = 0;
862
863user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800864#endif
Andy Green3b84c002011-03-06 13:14:42 +0000865 /* one shot */
866
Andy Green91f19d82013-12-21 11:18:34 +0800867 if (pollfd)
868 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800869
Andy Green3182ece2013-01-20 17:08:31 +0800870#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100871notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800872#endif
Andy Green3b84c002011-03-06 13:14:42 +0000873
Andy Green9e4c2b62011-03-07 20:47:39 +0000874 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
875 n = LWS_CALLBACK_CLIENT_WRITEABLE;
876 else
877 n = LWS_CALLBACK_SERVER_WRITEABLE;
878
Andy Greenb8b247d2013-01-22 07:20:08 +0800879 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800880 wsi, (enum libwebsocket_callback_reasons) n,
881 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000882}
883
884
885
Andy Green1c6e1422013-02-20 19:11:31 +0800886int
Andy Greena41314f2011-05-23 10:00:03 +0100887libwebsocket_service_timeout_check(struct libwebsocket_context *context,
888 struct libwebsocket *wsi, unsigned int sec)
889{
Andy Green3182ece2013-01-20 17:08:31 +0800890#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100891 int n;
892
893 /*
894 * if extensions want in on it (eg, we are a mux parent)
895 * give them a chance to service child timeouts
896 */
897
898 for (n = 0; n < wsi->count_active_extensions; n++)
899 wsi->active_extensions[n]->callback(
900 context, wsi->active_extensions[n],
901 wsi, LWS_EXT_CALLBACK_1HZ,
902 wsi->active_extensions_user[n], NULL, sec);
903
Andy Green3182ece2013-01-20 17:08:31 +0800904#endif
Andy Greena41314f2011-05-23 10:00:03 +0100905 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800906 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800907
Andy Greena41314f2011-05-23 10:00:03 +0100908 /*
909 * if we went beyond the allowed time, kill the
910 * connection
911 */
912
913 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800914 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100915 libwebsocket_close_and_free_session(context,
916 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800917 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100918 }
Andy Green1c6e1422013-02-20 19:11:31 +0800919
920 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100921}
922
Andy Green9f990342011-02-12 11:57:45 +0000923/**
924 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000925 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000926 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800927 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000928 *
Andy Green75006172013-01-22 12:32:11 +0800929 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800930 * services it according to the state of the associated
931 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800932 *
933 * The one call deals with all "service" that might happen on a socket
934 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800935 *
936 * If a pollfd says it has something, you can just pass it to
937 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
938 * If it sees it is a lws socket, the traffic will be handled and
939 * pollfd->revents will be zeroed now.
940 *
941 * If the socket is foreign to lws, it leaves revents alone. So you can
942 * see if you should service yourself by checking the pollfd revents
943 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000944 */
945
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800946LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000947libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000948 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000949{
Andy Greena1ce6be2013-01-18 11:43:21 +0800950 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000951 int n;
Andy Green0d338332011-02-12 11:57:43 +0000952 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800953 int listen_socket_fds_index = 0;
Patrick Gansterer92792b42014-02-26 21:37:31 +0100954 time_t now;
Andy Green1c6e1422013-02-20 19:11:31 +0800955 int timed_out = 0;
956 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800957 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800958
Andy Green3182ece2013-01-20 17:08:31 +0800959#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000960 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800961#endif
Andy Green98a717c2011-03-06 13:14:15 +0000962 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800963
964 if (context->listen_service_fd)
965 listen_socket_fds_index = context->lws_lookup[
966 context->listen_service_fd]->position_in_fds_table;
967
Andy Greena71eafc2011-02-14 17:59:43 +0000968 /*
969 * you can call us with pollfd = NULL to just allow the once-per-second
970 * global timeout checks; if less than a second since the last check
971 * it returns immediately then.
972 */
973
Patrick Gansterer92792b42014-02-26 21:37:31 +0100974 time(&now);
Andy Greena71eafc2011-02-14 17:59:43 +0000975
Patrick Gansterer92792b42014-02-26 21:37:31 +0100976 if (context->last_timeout_check_s != now) {
977 context->last_timeout_check_s = now;
Andy Greena71eafc2011-02-14 17:59:43 +0000978
Joakim Soderberg4c531232013-02-06 15:26:58 +0900979 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800980 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800981 if (context->started_with_parent &&
982 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800983 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900984 #endif
Andy Green24cba922013-01-19 13:56:10 +0800985
Andy Greena71eafc2011-02-14 17:59:43 +0000986 /* global timeout check once per second */
987
Andy Green1c6e1422013-02-20 19:11:31 +0800988 if (pollfd)
989 our_fd = pollfd->fd;
990
Peter Hinz56885f32011-03-02 22:03:47 +0000991 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800992 m = context->fds[n].fd;
993 wsi = context->lws_lookup[m];
994 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800995 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800996
Patrick Gansterer92792b42014-02-26 21:37:31 +0100997 if (libwebsocket_service_timeout_check(context, wsi, now))
Andy Green1c6e1422013-02-20 19:11:31 +0800998 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800999 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +08001000 /* it was the guy we came to service! */
1001 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001002 /* mark as handled */
1003 pollfd->revents = 0;
1004 }
Andy Greena71eafc2011-02-14 17:59:43 +00001005 }
1006 }
1007
Andy Green1c6e1422013-02-20 19:11:31 +08001008 /* the socket we came to service timed out, nothing to do */
1009 if (timed_out)
1010 return 0;
1011
Andy Greena71eafc2011-02-14 17:59:43 +00001012 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +00001013 if (pollfd == NULL)
1014 return 0;
1015
1016 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001017 wsi = context->lws_lookup[pollfd->fd];
1018 if (wsi == NULL)
1019 /* not lws connection ... leave revents alone and return */
1020 return 0;
1021
1022 /*
1023 * so that caller can tell we handled, past here we need to
1024 * zero down pollfd->revents after handling
1025 */
1026
Andy Green65b0e912013-01-16 07:59:47 +08001027 /*
1028 * deal with listen service piggybacking
1029 * every listen_service_modulo services of other fds, we
1030 * sneak one in to service the listen socket if there's anything waiting
1031 *
1032 * To handle connection storms, as found in ab, if we previously saw a
1033 * pending connection here, it causes us to check again next time.
1034 */
1035
Andy Greenb5b23192013-02-11 17:13:32 +08001036 if (context->listen_service_fd && pollfd !=
1037 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +08001038 context->listen_service_count++;
1039 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +08001040 context->listen_service_count ==
1041 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001042 context->listen_service_count = 0;
1043 m = 1;
1044 if (context->listen_service_extraseen > 5)
1045 m = 2;
1046 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001047 /*
1048 * even with extpoll, we prepared this
1049 * internal fds for listen
1050 */
1051 n = poll(&context->fds[listen_socket_fds_index],
1052 1, 0);
1053 if (n > 0) { /* there's a conn waiting for us */
1054 libwebsocket_service_fd(context,
1055 &context->
1056 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001057 context->listen_service_extraseen++;
1058 } else {
1059 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001060 context->
1061 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001062 break;
1063 }
1064 }
1065 }
1066
1067 }
1068
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001069 /* handle session socket closed */
1070
1071 if ((!(pollfd->revents & POLLIN)) &&
1072 (pollfd->revents & (POLLERR | POLLHUP))) {
1073
1074 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1075 (void *)wsi, pollfd->fd);
1076
1077 goto close_and_handled;
1078 }
1079
Andy Green65b0e912013-01-16 07:59:47 +08001080 /* okay, what we came here to do... */
1081
Andy Green0d338332011-02-12 11:57:43 +00001082 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001083
Andy Greena1ce6be2013-01-18 11:43:21 +08001084#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001085 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001086 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001087 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001088 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001089 n = lws_server_socket_service(context, wsi, pollfd);
1090 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001091#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001092
Andy Green0d338332011-02-12 11:57:43 +00001093 case LWS_CONNMODE_WS_SERVING:
1094 case LWS_CONNMODE_WS_CLIENT:
1095
Andy Green0d338332011-02-12 11:57:43 +00001096 /* the guy requested a callback when it was OK to write */
1097
Andy Greenda527df2011-03-07 07:08:12 +00001098 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001099 wsi->state == WSI_STATE_ESTABLISHED &&
1100 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001101 lwsl_info("libwebsocket_service_fd: closing\n");
1102 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001103 }
Andy Green0d338332011-02-12 11:57:43 +00001104
Andy Greenca0a1292013-03-16 11:24:23 +08001105 if (wsi->u.ws.rxflow_buffer &&
1106 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1107 lwsl_info("draining rxflow\n");
1108 /* well, drain it */
1109 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1110 wsi->u.ws.rxflow_pos;
1111 eff_buf.token_len = wsi->u.ws.rxflow_len -
1112 wsi->u.ws.rxflow_pos;
1113 draining_flow = 1;
1114 goto drain;
1115 }
1116
Andy Green0d338332011-02-12 11:57:43 +00001117 /* any incoming data ready? */
1118
1119 if (!(pollfd->revents & POLLIN))
1120 break;
1121
Andy Greenb45993c2010-12-18 15:13:50 +00001122#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001123read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001124 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001125 eff_buf.token_len = SSL_read(wsi->ssl,
1126 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001127 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001128 if (!eff_buf.token_len) {
1129 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001130 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001131 ERR_error_string(n,
1132 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001133 }
1134 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001135#endif
Andy Greene84652c2013-02-08 13:01:02 +08001136 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001137 context->service_buffer,
1138 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001139
Andy Green98a717c2011-03-06 13:14:15 +00001140 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001141 lwsl_debug("service_fd read ret = %d, errno = %d\n",
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01001142 eff_buf.token_len, LWS_ERRNO);
1143 if (LWS_ERRNO != LWS_EINTR && LWS_ERRNO != LWS_EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001144 goto close_and_handled;
1145 n = 0;
1146 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001147 }
Andy Green98a717c2011-03-06 13:14:15 +00001148 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001149 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001150 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001151 }
1152
Andy Green98a717c2011-03-06 13:14:15 +00001153 /*
1154 * give any active extensions a chance to munge the buffer
1155 * before parse. We pass in a pointer to an lws_tokens struct
1156 * prepared with the default buffer and content length that's in
1157 * there. Rather than rewrite the default buffer, extensions
1158 * that expect to grow the buffer can adapt .token to
1159 * point to their own per-connection buffer in the extension
1160 * user allocation. By default with no extensions or no
1161 * extension callback handling, just the normal input buffer is
1162 * used then so it is efficient.
1163 */
Andy Greenb45993c2010-12-18 15:13:50 +00001164
Andy Greene84652c2013-02-08 13:01:02 +08001165 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001166drain:
Andy Green3182ece2013-01-20 17:08:31 +08001167#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001168 more = 1;
1169 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001170
Andy Green98a717c2011-03-06 13:14:15 +00001171 more = 0;
1172
1173 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001174 m = wsi->active_extensions[n]->callback(context,
1175 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001176 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001177 wsi->active_extensions_user[n],
1178 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001179 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001180 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001181 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001182 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001183 }
1184 if (m)
1185 more = 1;
1186 }
Andy Green3182ece2013-01-20 17:08:31 +08001187#endif
Andy Green98a717c2011-03-06 13:14:15 +00001188 /* service incoming data */
1189
1190 if (eff_buf.token_len) {
1191 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001192 (unsigned char *)eff_buf.token,
1193 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001194 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001195 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001196 n = 0;
1197 goto handled;
1198 }
Andy Green98a717c2011-03-06 13:14:15 +00001199 }
Andy Green3182ece2013-01-20 17:08:31 +08001200#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001201 eff_buf.token = NULL;
1202 eff_buf.token_len = 0;
1203 }
Andy Green3182ece2013-01-20 17:08:31 +08001204#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001205 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1206 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1207 lwsl_info("flow buffer: drained\n");
1208 free(wsi->u.ws.rxflow_buffer);
1209 wsi->u.ws.rxflow_buffer = NULL;
1210 /* having drained the rxflow buffer, can rearm POLLIN */
1211 _libwebsocket_rx_flow_control(wsi);
1212 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001213
1214#ifdef LWS_OPENSSL_SUPPORT
1215 if (wsi->ssl && SSL_pending(wsi->ssl))
1216 goto read_pending;
1217#endif
Andy Green98a717c2011-03-06 13:14:15 +00001218 break;
Andy Green76f61e72013-01-16 11:53:05 +08001219
1220 default:
Andy Green03674a62013-01-16 11:47:40 +08001221#ifdef LWS_NO_CLIENT
1222 break;
1223#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001224 n = lws_client_socket_service(context, wsi, pollfd);
1225 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001226#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001227 }
1228
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001229 n = 0;
1230 goto handled;
1231
1232close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001233 libwebsocket_close_and_free_session(context, wsi,
1234 LWS_CLOSE_STATUS_NOSTATUS);
1235 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001236
1237handled:
1238 pollfd->revents = 0;
1239 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001240}
1241
Andy Green0d338332011-02-12 11:57:43 +00001242
Andy Green6964bb52011-01-23 16:50:33 +00001243/**
1244 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001245 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001246 *
1247 * This function closes any active connections and then frees the
1248 * context. After calling this, any further use of the context is
1249 * undefined.
1250 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001251LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001252libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001253{
Andy Green0d338332011-02-12 11:57:43 +00001254 int n;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001255#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001256 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001257 struct libwebsocket_extension *ext;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001258#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001259 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001260
Andy Greend636e352013-01-29 12:36:17 +08001261#ifdef LWS_LATENCY
1262 if (context->worst_latency_info[0])
1263 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1264#endif
1265
Andy Greendfb23042013-01-17 12:26:48 +08001266 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001267 struct libwebsocket *wsi =
1268 context->lws_lookup[context->fds[n].fd];
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001269 if (!wsi)
1270 continue;
Andy Greendfb23042013-01-17 12:26:48 +08001271 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001272 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1273 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001274 }
Andy Green6964bb52011-01-23 16:50:33 +00001275
arnaudviala7c6f26c2014-02-15 14:39:40 +08001276#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001277 /*
1278 * give all extensions a chance to clean up any per-context
1279 * allocations they might have made
1280 */
1281
1282 ext = context->extensions;
1283 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1284 if (context->listen_port)
1285 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001286 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001287 ext->callback(context, ext, NULL,
1288 (enum libwebsocket_extension_callback_reasons)m,
1289 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001290 ext++;
1291 }
arnaudviala7c6f26c2014-02-15 14:39:40 +08001292#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001293
1294 /*
1295 * inform all the protocols that they are done and will have no more
1296 * callbacks
1297 */
1298
1299 while (protocol->callback) {
1300 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1301 NULL, NULL, 0);
1302 protocol++;
1303 }
1304
Andy Greena41314f2011-05-23 10:00:03 +01001305
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001306#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001307#else
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001308 close(context->dummy_pipe_fds[0]);
1309 close(context->dummy_pipe_fds[1]);
Peter Hinz56885f32011-03-02 22:03:47 +00001310 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001311#endif
1312
Peter Hinz56885f32011-03-02 22:03:47 +00001313#ifdef LWS_OPENSSL_SUPPORT
1314 if (context->ssl_ctx)
1315 SSL_CTX_free(context->ssl_ctx);
1316 if (context->ssl_client_ctx)
1317 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001318
1319 ERR_remove_state(0);
1320 ERR_free_strings();
1321 EVP_cleanup();
1322 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001323#endif
1324
Andy Green46596482013-02-11 11:04:01 +08001325 if (context->fds)
1326 free(context->fds);
1327 if (context->lws_lookup)
1328 free(context->lws_lookup);
1329
Peter Hinz56885f32011-03-02 22:03:47 +00001330 free(context);
1331
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001332#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001333 WSACleanup();
1334#endif
Andy Green6964bb52011-01-23 16:50:33 +00001335}
1336
Andy Greend88146d2013-01-22 12:40:35 +08001337/**
Andy Greenb5b23192013-02-11 17:13:32 +08001338 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001339 * @context: Websocket context
1340 *
1341 * This returns the optional user allocation that can be attached to
1342 * the context the sockets live in at context_create time. It's a way
1343 * to let all sockets serviced in the same context share data without
1344 * using globals statics in the user code.
1345 */
Alon Levy0291eb32012-10-19 11:21:56 +02001346LWS_EXTERN void *
1347libwebsocket_context_user(struct libwebsocket_context *context)
1348{
Andy Greenb5b23192013-02-11 17:13:32 +08001349 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001350}
1351
Andy Green6964bb52011-01-23 16:50:33 +00001352/**
1353 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001354 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001355 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1356 * service otherwise block and service immediately, returning
1357 * after the timeout if nothing needed service.
1358 *
1359 * This function deals with any pending websocket traffic, for three
1360 * kinds of event. It handles these events on both server and client
1361 * types of connection the same.
1362 *
1363 * 1) Accept new connections to our context's server
1364 *
Andy Green6f520a52013-01-29 17:57:39 +08001365 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001366 * server or client connections.
1367 *
1368 * You need to call this service function periodically to all the above
1369 * functions to happen; if your application is single-threaded you can
1370 * just call it in your main event loop.
1371 *
1372 * Alternatively you can fork a new process that asynchronously handles
1373 * calling this service in a loop. In that case you are happy if this
1374 * call blocks your thread until it needs to take care of something and
1375 * would call it with a large nonzero timeout. Your loop then takes no
1376 * CPU while there is nothing happening.
1377 *
1378 * If you are calling it in a single-threaded app, you don't want it to
1379 * wait around blocking other things in your loop from happening, so you
1380 * would call it with a timeout_ms of 0, so it returns immediately if
1381 * nothing is pending, or as soon as it services whatever was pending.
1382 */
1383
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001384LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001385libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001386{
1387 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001388 int m;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001389 char buf;
Andy Greene92cd172011-01-19 13:11:55 +00001390
1391 /* stay dead once we are dead */
1392
Peter Hinz56885f32011-03-02 22:03:47 +00001393 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001394 return 1;
1395
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001396 context->service_tid = context->protocols[0].callback(context, NULL,
Andy Green3b3fa9e2013-12-25 16:34:37 +08001397 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
Peter Hinz56885f32011-03-02 22:03:47 +00001398 n = poll(context->fds, context->fds_count, timeout_ms);
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001399 context->service_tid = 0;
1400
Andy Green5dc62ea2013-09-20 20:26:12 +08001401 if (n == 0) /* poll timeout */ {
1402 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001403 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001404 }
Andy Greene92cd172011-01-19 13:11:55 +00001405
Andy Greened451d52014-01-11 12:37:07 +08001406 if (n < 0) {
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01001407 if (LWS_ERRNO != LWS_EINTR)
Andy Greened451d52014-01-11 12:37:07 +08001408 return -1;
1409 else
1410 return 0;
1411 }
Andy Greene92cd172011-01-19 13:11:55 +00001412
Andy Greendfb23042013-01-17 12:26:48 +08001413 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001414
Andy Greenca0a1292013-03-16 11:24:23 +08001415 for (n = 0; n < context->fds_count; n++) {
1416 if (!context->fds[n].revents)
1417 continue;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001418#ifndef _WIN32
1419 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
1420 if (read(context->fds[n].fd, &buf, 1) != 1)
1421 lwsl_err("Cannot read from dummy pipe.");
1422 continue;
1423 }
1424#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001425 m = libwebsocket_service_fd(context, &context->fds[n]);
1426 if (m < 0)
1427 return -1;
1428 /* if something closed, retry this slot */
1429 if (m)
1430 n--;
1431 }
1432
Andy Greene92cd172011-01-19 13:11:55 +00001433 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001434}
1435
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001436/**
1437 * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
1438 * @context: Websocket context
1439 *
1440 * This function let a call to libwebsocket_service() waiting for a timeout
1441 * immediately return.
1442 *
1443 * At the moment this functionality cannot be used on Windows.
1444 */
1445LWS_VISIBLE void
1446libwebsocket_cancel_service(struct libwebsocket_context *context)
1447{
1448#ifndef _WIN32
1449 char buf = 0;
1450 if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
1451 lwsl_err("Cannot write to dummy pipe.");
1452#endif
1453}
1454
Andy Green3182ece2013-01-20 17:08:31 +08001455#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001456int
1457lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001458 struct libwebsocket *wsi,
1459 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001460 void *v, size_t len)
1461{
1462 int n;
1463 int handled = 0;
1464
1465 /* maybe an extension will take care of it for us */
1466
1467 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1468 if (!wsi->active_extensions[n]->callback)
1469 continue;
1470
1471 handled |= wsi->active_extensions[n]->callback(context,
1472 wsi->active_extensions[n], wsi,
1473 r, wsi->active_extensions_user[n], v, len);
1474 }
1475
1476 return handled;
1477}
1478
1479
1480void *
1481lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001482 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001483{
1484 int n = 0;
1485
Andy Green68b45042011-05-25 21:41:57 +01001486 if (wsi == NULL)
1487 return NULL;
1488
Andy Greena41314f2011-05-23 10:00:03 +01001489 while (n < wsi->count_active_extensions) {
1490 if (wsi->active_extensions[n] != ext) {
1491 n++;
1492 continue;
1493 }
1494 return wsi->active_extensions_user[n];
1495 }
1496
1497 return NULL;
1498}
Andy Green3182ece2013-01-20 17:08:31 +08001499#endif
Andy Greena41314f2011-05-23 10:00:03 +01001500
Andy Green91f19d82013-12-21 11:18:34 +08001501void
1502lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1503{
1504 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001505 int events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001506 int tid;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001507 int sampled_tid;
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001508 struct libwebsocket_pollargs pa;
1509
1510 pa.fd = wsi->sock;
Andy Green91f19d82013-12-21 11:18:34 +08001511
1512 context->protocols[0].callback(context, wsi,
1513 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001514 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001515
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001516 pa.prev_events = events = context->fds[wsi->position_in_fds_table].events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001517
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001518 pa.events = context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001519
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001520 context->protocols[0].callback(context, wsi,
1521 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
1522 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001523
Andy Green3b3fa9e2013-12-25 16:34:37 +08001524 /*
1525 * if we changed something in this pollfd...
1526 * ... and we're running in a different thread context
1527 * than the service thread...
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001528 * ... and the service thread is waiting ...
1529 * then cancel it to force a restart with our changed events
Andy Green3b3fa9e2013-12-25 16:34:37 +08001530 */
1531 if (events != context->fds[wsi->position_in_fds_table].events) {
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001532 sampled_tid = context->service_tid;
1533 if (sampled_tid) {
Andy Green3b3fa9e2013-12-25 16:34:37 +08001534 tid = context->protocols[0].callback(context, NULL,
1535 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001536 if (tid != sampled_tid)
1537 libwebsocket_cancel_service(context);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001538 }
1539 }
Andy Green3b3fa9e2013-12-25 16:34:37 +08001540
Andy Green91f19d82013-12-21 11:18:34 +08001541 context->protocols[0].callback(context, wsi,
1542 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001543 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001544}
1545
1546
Andy Green90c7cbc2011-01-27 06:26:52 +00001547/**
1548 * libwebsocket_callback_on_writable() - Request a callback when this socket
1549 * becomes able to be written to without
1550 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001551 *
Peter Hinz56885f32011-03-02 22:03:47 +00001552 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001553 * @wsi: Websocket connection instance to get callback for
1554 */
1555
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001556LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001557libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001558 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001559{
Andy Green3182ece2013-01-20 17:08:31 +08001560#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001561 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001562 int handled = 0;
1563
1564 /* maybe an extension will take care of it for us */
1565
1566 for (n = 0; n < wsi->count_active_extensions; n++) {
1567 if (!wsi->active_extensions[n]->callback)
1568 continue;
1569
1570 handled |= wsi->active_extensions[n]->callback(context,
1571 wsi->active_extensions[n], wsi,
1572 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1573 wsi->active_extensions_user[n], NULL, 0);
1574 }
1575
1576 if (handled)
1577 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001578#endif
Andy Greendfb23042013-01-17 12:26:48 +08001579 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001580 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1581 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001582 return -1;
1583 }
1584
Andy Green91f19d82013-12-21 11:18:34 +08001585 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001586
Andy Green90c7cbc2011-01-27 06:26:52 +00001587 return 1;
1588}
1589
1590/**
1591 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1592 * all connections using the given protocol when it
1593 * becomes possible to write to each socket without
1594 * blocking in turn.
1595 *
1596 * @protocol: Protocol whose connections will get callbacks
1597 */
1598
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001599LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001600libwebsocket_callback_on_writable_all_protocol(
1601 const struct libwebsocket_protocols *protocol)
1602{
Peter Hinz56885f32011-03-02 22:03:47 +00001603 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001604 int n;
Andy Green0d338332011-02-12 11:57:43 +00001605 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001606
Andy Greendfb23042013-01-17 12:26:48 +08001607 for (n = 0; n < context->fds_count; n++) {
1608 wsi = context->lws_lookup[context->fds[n].fd];
1609 if (!wsi)
1610 continue;
1611 if (wsi->protocol == protocol)
1612 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001613 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001614
1615 return 0;
1616}
1617
Andy Greenbe93fef2011-02-14 20:25:43 +00001618/**
Andy Greene39e6ef2014-02-15 16:36:38 +08001619 * libwebsocket_callback_all_protocol() - Callback all connections using
1620 * the given protocol with the given reason
1621 *
1622 * @protocol: Protocol whose connections will get callbacks
1623 * @reason: Callback reason index
1624 */
1625
1626LWS_VISIBLE int
1627libwebsocket_callback_all_protocol(
1628 const struct libwebsocket_protocols *protocol, int reason)
1629{
1630 struct libwebsocket_context *context = protocol->owning_server;
1631 int n;
1632 struct libwebsocket *wsi;
1633
1634 for (n = 0; n < context->fds_count; n++) {
1635 wsi = context->lws_lookup[context->fds[n].fd];
1636 if (!wsi)
1637 continue;
1638 if (wsi->protocol == protocol)
1639 protocol->callback(context, wsi,
1640 reason, wsi->user_space, NULL, 0);
1641 }
1642
1643 return 0;
1644}
1645
1646/**
Andy Greenbe93fef2011-02-14 20:25:43 +00001647 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1648 *
1649 * You will not need this unless you are doing something special
1650 *
1651 * @wsi: Websocket connection instance
1652 * @reason: timeout reason
1653 * @secs: how many seconds
1654 */
1655
Craig McQueene7fd8b12014-02-19 09:24:17 +11001656LWS_VISIBLE void
Andy Greenbe93fef2011-02-14 20:25:43 +00001657libwebsocket_set_timeout(struct libwebsocket *wsi,
1658 enum pending_timeout reason, int secs)
1659{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001660 time_t now;
Andy Greenbe93fef2011-02-14 20:25:43 +00001661
Patrick Gansterer92792b42014-02-26 21:37:31 +01001662 time(&now);
Andy Greenbe93fef2011-02-14 20:25:43 +00001663
Patrick Gansterer92792b42014-02-26 21:37:31 +01001664 wsi->pending_timeout_limit = now + secs;
Andy Greenbe93fef2011-02-14 20:25:43 +00001665 wsi->pending_timeout = reason;
1666}
1667
Andy Greena6cbece2011-01-27 20:06:03 +00001668
1669/**
1670 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1671 *
1672 * You will not need this unless you are doing something special
1673 *
1674 * @wsi: Websocket connection instance
1675 */
1676
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001677LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001678libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1679{
1680 return wsi->sock;
1681}
1682
Andy Greend636e352013-01-29 12:36:17 +08001683#ifdef LWS_LATENCY
1684void
Andy Greenb5b23192013-02-11 17:13:32 +08001685lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1686 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001687{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001688 unsigned long long u;
Andy Greend636e352013-01-29 12:36:17 +08001689 char buf[256];
1690
Patrick Gansterer92792b42014-02-26 21:37:31 +01001691 u = time_in_microseconds();
Andy Greend636e352013-01-29 12:36:17 +08001692
1693 if (action) {
1694 if (completed) {
1695 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001696 sprintf(buf,
1697 "Completion first try lat %luus: %p: ret %d: %s\n",
1698 u - wsi->latency_start,
1699 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001700 else
Andy Greenb5b23192013-02-11 17:13:32 +08001701 sprintf(buf,
1702 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1703 u - wsi->action_start,
1704 u - wsi->latency_start,
1705 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001706 wsi->action_start = 0;
1707 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001708 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1709 u - wsi->latency_start,
1710 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001711 if (u - wsi->latency_start > context->worst_latency) {
1712 context->worst_latency = u - wsi->latency_start;
1713 strcpy(context->worst_latency_info, buf);
1714 }
1715 lwsl_latency("%s", buf);
1716 } else {
1717 wsi->latency_start = u;
1718 if (!wsi->action_start)
1719 wsi->action_start = u;
1720 }
1721}
1722#endif
1723
Andy Greena1ce6be2013-01-18 11:43:21 +08001724#ifdef LWS_NO_SERVER
1725int
Andy Green8d15cf42013-10-24 21:47:06 +08001726_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001727{
1728 return 0;
1729}
1730#else
Andy Green706961d2013-01-17 16:50:35 +08001731int
1732_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1733{
1734 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001735
Andy Greenca0a1292013-03-16 11:24:23 +08001736 /* there is no pending change */
1737 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001738 return 0;
1739
Andy Greenca0a1292013-03-16 11:24:23 +08001740 /* stuff is still buffered, not ready to really accept new input */
1741 if (wsi->u.ws.rxflow_buffer) {
1742 /* get ourselves called back to deal with stashed buffer */
1743 libwebsocket_callback_on_writable(context, wsi);
1744 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001745 }
1746
Andy Greenca0a1292013-03-16 11:24:23 +08001747 /* pending is cleared, we can change rxflow state */
1748
1749 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1750
1751 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1752 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1753
1754 /* adjust the pollfd for this wsi */
1755
1756 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001757 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001758 else
Andy Green91f19d82013-12-21 11:18:34 +08001759 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001760
Andy Green706961d2013-01-17 16:50:35 +08001761 return 1;
1762}
Andy Greena1ce6be2013-01-18 11:43:21 +08001763#endif
Andy Green706961d2013-01-17 16:50:35 +08001764
Andy Green90c7cbc2011-01-27 06:26:52 +00001765/**
1766 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1767 * receieved packets.
1768 *
1769 * If the output side of a server process becomes choked, this allows flow
1770 * control for the input side.
1771 *
1772 * @wsi: Websocket connection instance to get callback for
1773 * @enable: 0 = disable read servicing for this connection, 1 = enable
1774 */
1775
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001776LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001777libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1778{
Andy Greenca0a1292013-03-16 11:24:23 +08001779 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1780 return 0;
1781
1782 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1783 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001784
Andy Green706961d2013-01-17 16:50:35 +08001785 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001786}
1787
Andy Greenb55451c2013-03-16 12:32:27 +08001788/**
1789 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1790 *
1791 * When the user server code realizes it can accept more input, it can
1792 * call this to have the RX flow restriction removed from all connections using
1793 * the given protocol.
1794 *
1795 * @protocol: all connections using this protocol will be allowed to receive
1796 */
1797
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001798LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001799libwebsocket_rx_flow_allow_all_protocol(
1800 const struct libwebsocket_protocols *protocol)
1801{
1802 struct libwebsocket_context *context = protocol->owning_server;
1803 int n;
1804 struct libwebsocket *wsi;
1805
1806 for (n = 0; n < context->fds_count; n++) {
1807 wsi = context->lws_lookup[context->fds[n].fd];
1808 if (!wsi)
1809 continue;
1810 if (wsi->protocol == protocol)
1811 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1812 }
1813}
1814
Andy Green706961d2013-01-17 16:50:35 +08001815
Andy Green2ac5a6f2011-01-28 10:00:18 +00001816/**
1817 * libwebsocket_canonical_hostname() - returns this host's hostname
1818 *
1819 * This is typically used by client code to fill in the host parameter
1820 * when making a client connection. You can only call it after the context
1821 * has been created.
1822 *
Peter Hinz56885f32011-03-02 22:03:47 +00001823 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001824 */
1825
1826
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001827LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001828libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001829{
Peter Hinz56885f32011-03-02 22:03:47 +00001830 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001831}
1832
1833
Andy Green90c7cbc2011-01-27 06:26:52 +00001834static void sigpipe_handler(int x)
1835{
1836}
1837
Andy Green6901cb32011-02-21 08:06:47 +00001838#ifdef LWS_OPENSSL_SUPPORT
1839static int
1840OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1841{
1842
1843 SSL *ssl;
1844 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001845 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001846
1847 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1848 SSL_get_ex_data_X509_STORE_CTX_idx());
1849
1850 /*
Andy Green2e24da02011-03-05 16:12:04 +00001851 * !!! nasty openssl requires the index to come as a library-scope
1852 * static
Andy Green6901cb32011-02-21 08:06:47 +00001853 */
Andy Green2e24da02011-03-05 16:12:04 +00001854 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001855
Peter Hinz56885f32011-03-02 22:03:47 +00001856 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001857 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1858 x509_ctx, ssl, preverify_ok);
1859
1860 /* convert return code from 0 = OK to 1 = OK */
1861
1862 if (!n)
1863 n = 1;
1864 else
1865 n = 0;
1866
1867 return n;
1868}
1869#endif
1870
Andy Green706961d2013-01-17 16:50:35 +08001871int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001872 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001873 struct libwebsocket *wsi,
1874 enum libwebsocket_callback_reasons reason, void *user,
1875 void *in, size_t len)
1876{
1877 int n;
1878
1879 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001880 if (!n)
1881 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001882
Andy Greenaedc9532013-02-10 21:21:24 +08001883 return n;
Andy Green706961d2013-01-17 16:50:35 +08001884}
1885
Andy Green3b3fa9e2013-12-25 16:34:37 +08001886/*
1887 * This is just used to interrupt poll waiting
1888 * we don't have to do anything with it.
1889 */
arnaudviala7c6f26c2014-02-15 14:39:40 +08001890#ifdef LWS_OPENSSL_SUPPORT
Andy Green3b3fa9e2013-12-25 16:34:37 +08001891static void lws_sigusr2(int sig)
1892{
1893}
arnaudviala7c6f26c2014-02-15 14:39:40 +08001894#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001895
Andy Greenab990e42010-10-31 12:42:52 +00001896/**
Andy Green4739e5c2011-01-22 12:51:57 +00001897 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001898 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001899 *
Andy Green1b265272013-02-09 14:01:09 +08001900 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001901 * of all initialization in one step.
1902 *
Andy Greene92cd172011-01-19 13:11:55 +00001903 * After initialization, it returns a struct libwebsocket_context * that
1904 * represents this server. After calling, user code needs to take care
1905 * of calling libwebsocket_service() with the context pointer to get the
1906 * server's sockets serviced. This can be done in the same process context
1907 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001908 *
Andy Green8f037e42010-12-19 22:13:26 +00001909 * The protocol callback functions are called for a handful of events
1910 * including http requests coming in, websocket connections becoming
1911 * established, and data arriving; it's also called periodically to allow
1912 * async transmission.
1913 *
1914 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1915 * at that time websocket protocol has not been negotiated. Other
1916 * protocols after the first one never see any HTTP callack activity.
1917 *
1918 * The server created is a simple http server by default; part of the
1919 * websocket standard is upgrading this http connection to a websocket one.
1920 *
1921 * This allows the same server to provide files like scripts and favicon /
1922 * images or whatever over http and dynamic data over websockets all in
1923 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001924 */
Andy Green4ea60062010-10-30 12:15:07 +01001925
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001926LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001927libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001928{
Peter Hinz56885f32011-03-02 22:03:47 +00001929 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001930 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001931 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001932#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001933 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001934 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001935 struct sockaddr_in serv_addr;
1936#endif
Andy Green3182ece2013-01-20 17:08:31 +08001937#ifndef LWS_NO_EXTENSIONS
1938 int m;
Andy Green1b265272013-02-09 14:01:09 +08001939 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001940#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001941
Andy Green3faa9c72010-11-08 17:03:03 +00001942#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001943 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001944#endif
1945
Joakim Soderberg4c531232013-02-06 15:26:58 +09001946#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001947 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001948#endif
1949
Andy Greenb3a614a2013-01-19 13:08:17 +08001950 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001951 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001952 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001953 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001954#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001955 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1956 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001957#else
1958 lwsl_notice(" Configured without extension support\n");
1959#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001960 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1961 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001962 if (info->ssl_cipher_list)
1963 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001964 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1965 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001966
Peter Hinz56885f32011-03-02 22:03:47 +00001967#ifdef _WIN32
1968 {
1969 WORD wVersionRequested;
1970 WSADATA wsaData;
1971 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001972 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001973
1974 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1975 wVersionRequested = MAKEWORD(2, 2);
1976
1977 err = WSAStartup(wVersionRequested, &wsaData);
1978 if (err != 0) {
1979 /* Tell the user that we could not find a usable */
1980 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001981 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001982 return NULL;
1983 }
David Galeano7b11fec2011-10-04 19:55:18 +08001984
Andy Green6ee372f2012-04-09 15:09:01 +08001985 /* default to a poll() made out of select() */
1986 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001987
Patrick Gansterer5b71aac2014-02-28 01:31:39 +01001988#ifndef _WIN32_WCE
Andy Green6ee372f2012-04-09 15:09:01 +08001989 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001990 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001991 if (wsdll)
1992 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001993
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001994 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001995 if (!poll)
1996 poll = emulated_poll;
Patrick Gansterer5b71aac2014-02-28 01:31:39 +01001997#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001998 }
1999#endif
2000
Andy Greenb5b23192013-02-11 17:13:32 +08002001 context = (struct libwebsocket_context *)
2002 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00002003 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08002004 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002005 return NULL;
2006 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002007 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08002008#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08002009 context->started_with_parent = pid_daemon;
2010 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
2011#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002012
Joakim Soderberg4c531232013-02-06 15:26:58 +09002013 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08002014 context->protocols = info->protocols;
2015 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00002016 context->http_proxy_port = 0;
2017 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08002018 context->options = info->options;
Mattias Lundberg03bb8f92014-02-18 10:06:57 +01002019 context->iface = info->iface;
Andy Greendfb23042013-01-17 12:26:48 +08002020 /* to reduce this allocation, */
2021 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08002022 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
2023 sizeof(struct libwebsocket_context),
2024 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
2025 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08002026 sizeof(struct libwebsocket_context) +
2027 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
2028 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08002029
Andy Greenb5b23192013-02-11 17:13:32 +08002030 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
2031 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002032 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002033 lwsl_err("Unable to allocate fds array for %d connections\n",
2034 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002035 free(context);
2036 return NULL;
2037 }
Andy Greenb5b23192013-02-11 17:13:32 +08002038 context->lws_lookup = (struct libwebsocket **)
2039 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002040 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002041 lwsl_err(
2042 "Unable to allocate lws_lookup array for %d connections\n",
2043 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002044 free(context->fds);
2045 free(context);
2046 return NULL;
2047 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08002048 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
2049 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08002050
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01002051#ifdef _WIN32
Peter Hinz56885f32011-03-02 22:03:47 +00002052 context->fds_count = 0;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01002053#else
2054 if (pipe(context->dummy_pipe_fds)) {
2055 lwsl_err("Unable to create pipe\n");
2056 free(context->lws_lookup);
2057 free(context->fds);
2058 free(context);
2059 return NULL;
2060 }
2061
2062 /* use the read end of pipe as first item */
2063 context->fds[0].fd = context->dummy_pipe_fds[0];
2064 context->fds[0].events = POLLIN;
2065 context->fds[0].revents = 0;
2066 context->fds_count = 1;
2067#endif
2068
Andy Green3182ece2013-01-20 17:08:31 +08002069#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08002070 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08002071#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002072 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08002073 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00002074
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002075#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002076 context->fd_random = 0;
2077#else
2078 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2079 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002080 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002081 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002082 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00002083 }
Peter Hinz56885f32011-03-02 22:03:47 +00002084#endif
Andy Green44eee682011-02-10 09:32:24 +00002085
Peter Hinz56885f32011-03-02 22:03:47 +00002086#ifdef LWS_OPENSSL_SUPPORT
2087 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08002088 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002089 context->ssl_ctx = NULL;
2090 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002091 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002092#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002093
Andy Greena1ce6be2013-01-18 11:43:21 +08002094 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002095
Andy Greena1ce6be2013-01-18 11:43:21 +08002096#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002097 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01002098 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08002099 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08002100 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01002101
Andy Greenb5b23192013-02-11 17:13:32 +08002102 lwsl_notice(" canonical_hostname = %s\n",
2103 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002104 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002105#endif
Andy Greena69f0512012-05-03 12:32:38 +08002106
Andy Green9659f372011-01-27 22:01:43 +00002107 /* split the proxy ads:port if given */
2108
Patrick Gansterer148b9452014-02-28 02:31:23 +01002109 if (info->http_proxy_address) {
2110 strncpy(context->http_proxy_address, info->http_proxy_address,
Andy Greenb5b23192013-02-11 17:13:32 +08002111 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002112 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002113 sizeof(context->http_proxy_address) - 1] = '\0';
Patrick Gansterer148b9452014-02-28 02:31:23 +01002114 context->http_proxy_port = info->http_proxy_port;
2115 } else {
Patrick Gansterer61a6ae42014-02-28 02:32:03 +01002116#ifdef HAVE_GETENV
Patrick Gansterer148b9452014-02-28 02:31:23 +01002117 p = getenv("http_proxy");
2118 if (p) {
2119 strncpy(context->http_proxy_address, p,
2120 sizeof(context->http_proxy_address) - 1);
2121 context->http_proxy_address[
2122 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002123
Patrick Gansterer148b9452014-02-28 02:31:23 +01002124 p = strchr(context->http_proxy_address, ':');
2125 if (p == NULL) {
2126 lwsl_err("http_proxy needs to be ads:port\n");
2127 goto bail;
2128 }
2129 *p = '\0';
2130 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002131 }
Patrick Gansterer61a6ae42014-02-28 02:32:03 +01002132#endif
Patrick Gansterer148b9452014-02-28 02:31:23 +01002133 }
Andy Green9659f372011-01-27 22:01:43 +00002134
Patrick Gansterer148b9452014-02-28 02:31:23 +01002135 if (context->http_proxy_address[0]) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002136 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002137 context->http_proxy_address,
2138 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002139 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002140
Andy Greena1ce6be2013-01-18 11:43:21 +08002141#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002142 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002143
Andy Green3faa9c72010-11-08 17:03:03 +00002144#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002145 context->use_ssl = info->ssl_cert_filepath != NULL &&
2146 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002147#ifdef USE_CYASSL
2148 lwsl_notice(" Compiled with CYASSL support\n");
2149#else
2150 lwsl_notice(" Compiled with OpenSSL support\n");
2151#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002152 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002153 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002154 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002155 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002156
Andy Green90c7cbc2011-01-27 06:26:52 +00002157#else
Andy Green2b40b792013-02-10 22:22:01 +08002158 if (info->ssl_cert_filepath != NULL &&
2159 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002160 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002161 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002162 }
Andy Greenb5b23192013-02-11 17:13:32 +08002163 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002164#endif
Andy Greena17c6922013-01-20 20:21:54 +08002165
Andy Greenb5b23192013-02-11 17:13:32 +08002166 lwsl_notice(
2167 " per-conn mem: %u + %u headers + protocol rx buf\n",
2168 sizeof(struct libwebsocket),
2169 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002170 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002171#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002172
2173 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002174#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002175#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002176 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002177#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002178
2179
2180#ifdef LWS_OPENSSL_SUPPORT
2181
2182 /* basic openssl init */
2183
2184 SSL_library_init();
2185
2186 OpenSSL_add_all_algorithms();
2187 SSL_load_error_strings();
2188
Andy Green2e24da02011-03-05 16:12:04 +00002189 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002190 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2191
Andy Green90c7cbc2011-01-27 06:26:52 +00002192 /*
2193 * Firefox insists on SSLv23 not SSLv3
2194 * Konq disables SSLv2 by default now, SSLv23 works
2195 */
2196
2197 method = (SSL_METHOD *)SSLv23_server_method();
2198 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002199 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002200 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002201 error,
2202 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002203 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002204 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002205 }
Peter Hinz56885f32011-03-02 22:03:47 +00002206 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2207 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002208 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002209 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002210 error,
2211 ERR_error_string(error,
2212 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002213 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002214 }
2215
martell19c73f32014-01-22 18:17:03 +00002216#if defined(WIN32) || defined(_WIN32)
2217#else
Andy Green3b3fa9e2013-12-25 16:34:37 +08002218 signal(SIGUSR2, lws_sigusr2);
Andy Greened451d52014-01-11 12:37:07 +08002219 {
2220 sigset_t mask;
2221 sigemptyset (&mask);
2222 sigaddset (&mask, SIGUSR2);
2223
2224 sigprocmask(SIG_BLOCK, &mask, NULL);
2225 }
martell19c73f32014-01-22 18:17:03 +00002226#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002227
David Galeanocc148e42013-01-10 10:18:59 +08002228#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002229 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002230#endif
David Galeano77a677c2013-01-10 10:14:12 +08002231 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002232 if (info->ssl_cipher_list)
2233 SSL_CTX_set_cipher_list(context->ssl_ctx,
2234 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002235
Andy Greena1ce6be2013-01-18 11:43:21 +08002236#ifndef LWS_NO_CLIENT
2237
Andy Green90c7cbc2011-01-27 06:26:52 +00002238 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002239
Andy Green1b265272013-02-09 14:01:09 +08002240 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002241 method = (SSL_METHOD *)SSLv23_client_method();
2242 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002243 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002244 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002245 error,
2246 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002247 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002248 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002249 }
2250 /* create context */
2251 context->ssl_client_ctx = SSL_CTX_new(method);
2252 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002253 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002254 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002255 error,
2256 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002257 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002258 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002259 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002260
David Galeanocc148e42013-01-10 10:18:59 +08002261#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002262 SSL_CTX_set_options(context->ssl_client_ctx,
2263 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002264#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002265 SSL_CTX_set_options(context->ssl_client_ctx,
2266 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002267 if (info->ssl_cipher_list)
2268 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2269 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002270
Peter Hinz56885f32011-03-02 22:03:47 +00002271 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002272 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002273 if (!SSL_CTX_load_verify_locations(
2274 context->ssl_client_ctx, NULL,
2275 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002276 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002277 "Unable to load SSL Client certs from %s "
2278 "(set by --with-client-cert-dir= "
2279 "in configure) -- client ssl isn't "
2280 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002281 } else
2282 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002283 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002284 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002285 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002286 "Unable to load SSL Client certs "
2287 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002288 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002289
2290 /*
2291 * callback allowing user code to load extra verification certs
2292 * helping the client to verify server identity
2293 */
2294
prasannateamf1b353a452013-11-12 17:21:01 -08002295 /* support for client-side certificate authentication */
2296 if (info->ssl_cert_filepath) {
2297 n = SSL_CTX_use_certificate_chain_file(
2298 context->ssl_client_ctx,
2299 info->ssl_cert_filepath);
2300 if (n != 1) {
2301 lwsl_err("problem getting cert '%s' %lu: %s\n",
2302 info->ssl_cert_filepath,
2303 ERR_get_error(),
2304 ERR_error_string(ERR_get_error(),
2305 (char *)context->service_buffer));
2306 goto bail;
2307 }
2308 }
2309 if (info->ssl_private_key_filepath) {
2310 /* set the private key from KeyFile */
2311 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2312 info->ssl_private_key_filepath,
2313 SSL_FILETYPE_PEM) != 1) {
2314 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2315 info->ssl_private_key_filepath,
2316 ERR_get_error(),
2317 ERR_error_string(ERR_get_error(),
2318 (char *)context->service_buffer));
2319 goto bail;
2320 }
2321
2322 /* verify private key */
2323 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2324 lwsl_err("Private SSL key doesn't match cert\n");
2325 goto bail;
2326 }
2327 }
2328
Peter Hinz56885f32011-03-02 22:03:47 +00002329 context->protocols[0].callback(context, NULL,
2330 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2331 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002332 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002333#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002334
Andy Greenc6bf2c22011-02-20 11:10:47 +00002335 /* as a server, are we requiring clients to identify themselves? */
2336
Andy Greenb5b23192013-02-11 17:13:32 +08002337 if (info->options &
2338 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002339
2340 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002341
Peter Hinz56885f32011-03-02 22:03:47 +00002342 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002343 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2344 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002345
2346 /*
2347 * give user code a chance to load certs into the server
2348 * allowing it to verify incoming client certs
2349 */
2350
Peter Hinz56885f32011-03-02 22:03:47 +00002351 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002352 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002353 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002354 }
2355
James Devine5b34c972013-12-14 11:41:29 +08002356 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2357 /* Normally SSL listener rejects non-ssl, optionally allow */
2358 context->allow_non_ssl_on_ssl_port = 1;
2359 }
2360
Peter Hinz56885f32011-03-02 22:03:47 +00002361 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002362
2363 /* openssl init for server sockets */
2364
Andy Green3faa9c72010-11-08 17:03:03 +00002365 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002366 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002367 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002368 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002369 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002370 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002371 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002372 error,
2373 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002374 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002375 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002376 }
2377 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002378 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002379 info->ssl_private_key_filepath,
2380 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002381 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002382 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002383 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002384 error,
2385 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002386 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002387 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002388 }
2389 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002390 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002391 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002392 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002393 }
2394
2395 /* SSL is happy and has a cert it's content with */
2396 }
2397#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002398
Andy Greena1ce6be2013-01-18 11:43:21 +08002399#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002400 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002401
Andy Green1b265272013-02-09 14:01:09 +08002402 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002403 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002404
Andy Green4739e5c2011-01-22 12:51:57 +00002405 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2406 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002407 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002408 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002409 }
Andy Green775c0dd2010-10-29 14:15:22 +01002410
Andy Greenb5b23192013-02-11 17:13:32 +08002411 /*
2412 * allow us to restart even if old sockets in TIME_WAIT
Andy Greenb5b23192013-02-11 17:13:32 +08002413 */
Andy Green6ee372f2012-04-09 15:09:01 +08002414 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2415 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002416
2417 /* Disable Nagle */
2418 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002419 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2420 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002421
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002422 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002423 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002424 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002425 #else
Andy Greene2160712013-01-28 12:19:10 +08002426 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002427 #endif
Andy Greene2160712013-01-28 12:19:10 +08002428
Andy Green4739e5c2011-01-22 12:51:57 +00002429 bzero((char *) &serv_addr, sizeof(serv_addr));
2430 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002431 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002432 serv_addr.sin_addr.s_addr = INADDR_ANY;
2433 else
Andy Greend1eac602013-11-09 08:07:38 +08002434 if (interface_to_sa(info->iface, &serv_addr,
2435 sizeof(serv_addr)) < 0) {
2436 lwsl_err("Unable to find interface %s\n",
2437 info->iface);
2438 compatible_close(sockfd);
2439 goto bail;
2440 }
Andy Green1b265272013-02-09 14:01:09 +08002441 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002442
2443 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2444 sizeof(serv_addr));
2445 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002446 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01002447 info->port, n, LWS_ERRNO);
Andy Green8b2d6f02013-10-22 06:49:30 +08002448 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002449 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002450 }
Andy Green0d338332011-02-12 11:57:43 +00002451
Andy Greenb5b23192013-02-11 17:13:32 +08002452 wsi = (struct libwebsocket *)malloc(
2453 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002454 if (wsi == NULL) {
2455 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002456 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002457 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002458 }
Andy Greenb5b23192013-02-11 17:13:32 +08002459 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002460 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002461#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002462 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002463#endif
Andy Green0d338332011-02-12 11:57:43 +00002464 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002465
2466 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002467
Andy Green65b0e912013-01-16 07:59:47 +08002468 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2469 context->listen_service_count = 0;
2470 context->listen_service_fd = sockfd;
2471
Andy Greena824d182013-01-15 20:52:29 +08002472 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002473 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002474 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002475#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002476
Andy Green6ee372f2012-04-09 15:09:01 +08002477 /*
2478 * drop any root privs for this process
2479 * to listen on port < 1023 we would have needed root, but now we are
2480 * listening, we don't want the power for anything else
2481 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002482#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002483#else
Andy Green1b265272013-02-09 14:01:09 +08002484 if (info->gid != -1)
2485 if (setgid(info->gid))
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01002486 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
Andy Green1b265272013-02-09 14:01:09 +08002487 if (info->uid != -1)
2488 if (setuid(info->uid))
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01002489 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
Peter Hinz56885f32011-03-02 22:03:47 +00002490#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002491
Andy Green6f520a52013-01-29 17:57:39 +08002492 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002493
Peter Hinz56885f32011-03-02 22:03:47 +00002494 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002495 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002496 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002497
Andy Green43db0452013-01-10 19:50:35 +08002498 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002499 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002500
Andy Green1b265272013-02-09 14:01:09 +08002501 info->protocols[context->count_protocols].owning_server =
2502 context;
2503 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002504 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002505
2506 /*
2507 * inform all the protocols that they are doing their one-time
2508 * initialization if they want to
2509 */
2510 info->protocols[context->count_protocols].callback(context,
2511 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002512 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002513
Andy Green3182ece2013-01-20 17:08:31 +08002514#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002515 /*
2516 * give all extensions a chance to create any per-context
2517 * allocations they need
2518 */
2519
2520 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002521 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002522 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002523
Andy Green1b265272013-02-09 14:01:09 +08002524 if (info->extensions) {
2525 ext = info->extensions;
2526 while (ext->callback) {
2527 lwsl_ext(" Extension: %s\n", ext->name);
2528 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002529 (enum libwebsocket_extension_callback_reasons)m,
2530 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002531 ext++;
2532 }
Andy Greena41314f2011-05-23 10:00:03 +01002533 }
Andy Green3182ece2013-01-20 17:08:31 +08002534#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002535
Peter Hinz56885f32011-03-02 22:03:47 +00002536 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002537
2538bail:
2539 libwebsocket_context_destroy(context);
2540 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002541}
Andy Greenb45993c2010-12-18 15:13:50 +00002542
Andy Greenb45993c2010-12-18 15:13:50 +00002543/**
shysb4e800e2013-10-24 22:12:03 +08002544 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2545 * @context: pointer to struct libwebsocket_context you want set proxy to
2546 * @proxy: pointer to c string containing proxy in format address:port
2547 *
2548 * Returns 0 if proxy string was parsed and proxy was setup.
2549 * Returns -1 if @proxy is NULL or has incorrect format.
2550 *
2551 * This is only required if your OS does not provide the http_proxy
2552 * enviroment variable (eg, OSX)
2553 *
2554 * IMPORTANT! You should call this function right after creation of the
2555 * libwebsocket_context and before call to connect. If you call this
2556 * function after connect behavior is undefined.
2557 * This function will override proxy settings made on libwebsocket_context
2558 * creation with genenv() call.
2559 */
2560
2561LWS_VISIBLE int
2562libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2563{
2564 char *p;
2565
2566 if (!proxy)
2567 return -1;
2568
2569 strncpy(context->http_proxy_address, proxy,
2570 sizeof(context->http_proxy_address) - 1);
2571 context->http_proxy_address[
2572 sizeof(context->http_proxy_address) - 1] = '\0';
2573
2574 p = strchr(context->http_proxy_address, ':');
2575 if (!p) {
2576 lwsl_err("http_proxy needs to be ads:port\n");
2577
2578 return -1;
2579 }
2580 *p = '\0';
2581 context->http_proxy_port = atoi(p + 1);
2582
2583 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2584 context->http_proxy_port);
2585
2586 return 0;
2587}
2588
2589/**
Andy Greenb45993c2010-12-18 15:13:50 +00002590 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002591 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002592 * @wsi: pointer to struct websocket you want to know the protocol of
2593 *
Andy Green8f037e42010-12-19 22:13:26 +00002594 *
Andy Green6f520a52013-01-29 17:57:39 +08002595 * Some apis can act on all live connections of a given protocol,
2596 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002597 */
Andy Greenab990e42010-10-31 12:42:52 +00002598
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002599LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002600libwebsockets_get_protocol(struct libwebsocket *wsi)
2601{
2602 return wsi->protocol;
2603}
2604
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002605LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002606libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2607{
Andy Green623a98d2013-01-21 11:04:23 +08002608 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002609}
Alex Bligh49146db2011-11-07 17:19:25 +08002610
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002611LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002612libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2613{
Andy Green623a98d2013-01-21 11:04:23 +08002614 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002615}
2616
Andy Green2af4d5b2013-02-18 16:30:10 +08002617int
Alex Bligh49146db2011-11-07 17:19:25 +08002618libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2619{
Andy Green67d556c2013-02-15 22:31:55 +08002620 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002621 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002622
Alex Bligh49146db2011-11-07 17:19:25 +08002623 /* allocate the per-connection user memory (if any) */
2624
2625 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2626 wsi->user_space = malloc(
2627 wsi->protocol->per_session_data_size);
2628 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002629 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002630 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002631 }
Andy Green6ee372f2012-04-09 15:09:01 +08002632 memset(wsi->user_space, 0,
2633 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002634 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002635 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002636}
Andy Green43db0452013-01-10 19:50:35 +08002637
Andy Green0b319092013-01-19 11:17:56 +08002638static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002639{
Andy Green0b319092013-01-19 11:17:56 +08002640 char buf[300];
Patrick Gansterer92792b42014-02-26 21:37:31 +01002641 unsigned long long now;
Andy Green0b319092013-01-19 11:17:56 +08002642 int n;
2643
Andy Green0b319092013-01-19 11:17:56 +08002644 buf[0] = '\0';
2645 for (n = 0; n < LLL_COUNT; n++)
2646 if (level == (1 << n)) {
Patrick Gansterer92792b42014-02-26 21:37:31 +01002647 now = time_in_microseconds() / 100;
2648 sprintf(buf, "[%lu:%04d] %s: ", (unsigned long) now / 10000,
2649 (int)(now % 10000), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002650 break;
2651 }
Andy Greenb5b23192013-02-11 17:13:32 +08002652
Andy Green0b319092013-01-19 11:17:56 +08002653 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002654}
2655
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002656#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002657LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002658{
2659 lwsl_emit_stderr(level, line);
2660}
2661#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002662LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002663{
2664 int syslog_level = LOG_DEBUG;
2665
2666 switch (level) {
2667 case LLL_ERR:
2668 syslog_level = LOG_ERR;
2669 break;
2670 case LLL_WARN:
2671 syslog_level = LOG_WARNING;
2672 break;
2673 case LLL_NOTICE:
2674 syslog_level = LOG_NOTICE;
2675 break;
2676 case LLL_INFO:
2677 syslog_level = LOG_INFO;
2678 break;
2679 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002680 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002681}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002682#endif
Andy Greenc11db202013-01-19 11:12:16 +08002683
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002684LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002685{
Andy Greende8f27a2013-01-12 09:17:42 +08002686 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002687 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002688
2689 if (!(log_level & filter))
2690 return;
2691
Andy Green43db0452013-01-10 19:50:35 +08002692 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002693 vsnprintf(buf, sizeof(buf), format, ap);
2694 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002695 va_end(ap);
2696
Andy Green0b319092013-01-19 11:17:56 +08002697 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002698}
2699
2700/**
2701 * lws_set_log_level() - Set the logging bitfield
2702 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002703 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2704 * function to perform log string emission instead of
2705 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002706 *
Andy Greenc6511a02013-02-19 10:01:48 +08002707 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002708 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002709 */
2710
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002711LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002712 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002713{
2714 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002715 if (log_emit_function)
2716 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002717}
Andy Green92f9b162014-02-21 18:43:42 +08002718
2719int
2720interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
2721{
2722 int rc = -1;
2723#if defined(WIN32) || defined(_WIN32)
2724 /* TODO */
2725#else
2726 struct ifaddrs *ifr;
2727 struct ifaddrs *ifc;
2728 struct sockaddr_in *sin;
2729
2730 getifaddrs(&ifr);
2731 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
2732 if (ifc->ifa_addr == NULL)
2733 continue;
2734 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
2735 if (strcmp(ifc->ifa_name, ifname))
2736 continue;
2737 sin = (struct sockaddr_in *)ifc->ifa_addr;
2738 if (sin->sin_family != AF_INET)
2739 continue;
2740 memcpy(addr, sin, addrlen);
2741 rc = 0;
2742 }
2743
2744 freeifaddrs(ifr);
2745#endif
2746 return rc;
2747}
2748