blob: 7f9d20eb006e4edaf5b17f421b17bd97426d3276 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010024#if defined(WIN32) || defined(_WIN32)
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010027#include <mstcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000028#else
Davidc4ef7b12013-01-12 20:39:47 +080029#ifdef LWS_BUILTIN_GETIFADDRS
30#include <getifaddrs.h>
31#else
Peter Hinz56885f32011-03-02 22:03:47 +000032#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080033#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090034#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000035#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080036#include <sys/socket.h>
37#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000038#endif
Andy Green2e24da02011-03-05 16:12:04 +000039
Andy Green3b3fa9e2013-12-25 16:34:37 +080040#include <sys/types.h>
41
Andy Green2e24da02011-03-05 16:12:04 +000042#ifdef LWS_OPENSSL_SUPPORT
43int openssl_websocket_private_data_index;
44#endif
45
Andy Greenaa6fc442012-04-12 13:26:49 +080046#ifdef __MINGW32__
47#include "../win32port/win32helpers/websock-w32.c"
48#else
49#ifdef __MINGW64__
50#include "../win32port/win32helpers/websock-w32.c"
51#endif
52#endif
53
Andy Green7b405452013-02-01 10:50:15 +080054#ifndef LWS_BUILD_HASH
55#define LWS_BUILD_HASH "unknown-build-hash"
56#endif
Andy Green3182ece2013-01-20 17:08:31 +080057
Andy Green7c19c342013-01-19 12:18:07 +080058static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080059static void lwsl_emit_stderr(int level, const char *line);
60static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
61
Andy Green7b405452013-02-01 10:50:15 +080062static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
63
Andy Greenb5b23192013-02-11 17:13:32 +080064static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080065 "ERR",
66 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080067 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080068 "INFO",
69 "DEBUG",
70 "PARSER",
71 "HEADER",
72 "EXTENSION",
73 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080074 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080075};
76
Andy Greenb5b23192013-02-11 17:13:32 +080077#ifndef LWS_NO_CLIENT
78 extern int lws_client_socket_service(
79 struct libwebsocket_context *context,
80 struct libwebsocket *wsi, struct pollfd *pollfd);
81#endif
82#ifndef LWS_NO_SERVER
83 extern int lws_server_socket_service(
84 struct libwebsocket_context *context,
85 struct libwebsocket *wsi, struct pollfd *pollfd);
86#endif
87
Andy Green3b3fa9e2013-12-25 16:34:37 +080088
89#ifdef LWS_HAS_PPOLL
90/*
91 * set to the Thread ID that's doing the service loop just before entry to ppoll
92 * indicates service thread likely idling in ppoll()
93 * volatile because other threads may check it as part of processing for pollfd
94 * event change.
95 */
96static volatile int lws_idling_ppoll_tid;
97#endif
98
Andy Green7b405452013-02-01 10:50:15 +080099/**
100 * lws_get_library_version: get version and git hash library built from
101 *
102 * returns a const char * to a string like "1.1 178d78c"
103 * representing the library version followed by the git head hash it
104 * was built from
105 */
106
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800107LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +0800108lws_get_library_version(void)
109{
110 return library_version;
111}
112
Patrick Gansterer92792b42014-02-26 21:37:31 +0100113static unsigned long long time_in_microseconds()
114{
115#if defined(WIN32) || defined(_WIN32)
116#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
117 FILETIME filetime;
118 ULARGE_INTEGER datetime;
119
120#ifdef _WIN32_WCE
121 GetCurrentFT(&filetime);
122#else
123 GetSystemTimeAsFileTime(&filetime);
124#endif
125
126 /*
127 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
128 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
129 * prevent alignment faults on 64-bit Windows).
130 */
131 memcpy(&datetime, &filetime, sizeof(datetime));
132
133 /* Windows file times are in 100s of nanoseconds. */
134 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
135#else
136 struct timeval tv;
137 gettimeofday(&tv, NULL);
138 return (tv.tv_sec * 1000000) + tv.tv_usec;
139#endif
140}
141
Andy Green0d338332011-02-12 11:57:43 +0000142int
Andy Greenb5b23192013-02-11 17:13:32 +0800143insert_wsi_socket_into_fds(struct libwebsocket_context *context,
144 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000145{
Joakim Soderberg6596e4b2014-02-18 13:38:14 +0100146 struct libwebsocket_pollargs pa = { wsi->sock, POLLIN, 0 };
147
Andy Greendfb23042013-01-17 12:26:48 +0800148 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800149 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000150 return 1;
151 }
152
nononamef1624922014-01-11 13:12:34 +0800153 if (wsi->sock >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800154 lwsl_err("Socket fd %d is too high (%d)\n",
155 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800156 return 1;
157 }
158
159 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800160 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800161
Andy Greenb5b23192013-02-11 17:13:32 +0800162 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
163 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800164
Andy Green7a132792013-12-18 09:48:26 +0800165 context->protocols[0].callback(context, wsi,
166 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800167 wsi->user_space, (void *) &pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800168
Andy Greendfb23042013-01-17 12:26:48 +0800169 context->lws_lookup[wsi->sock] = wsi;
170 wsi->position_in_fds_table = context->fds_count;
171 context->fds[context->fds_count].fd = wsi->sock;
172 context->fds[context->fds_count].events = POLLIN;
173 context->fds[context->fds_count++].revents = 0;
174
175 /* external POLL support via protocol 0 */
176 context->protocols[0].callback(context, wsi,
177 LWS_CALLBACK_ADD_POLL_FD,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800178 wsi->user_space, (void *) &pa, 0);
Andy Green0d338332011-02-12 11:57:43 +0000179
Andy Green7a132792013-12-18 09:48:26 +0800180 context->protocols[0].callback(context, wsi,
181 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800182 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800183
Andy Green0d338332011-02-12 11:57:43 +0000184 return 0;
185}
186
Andy Greendfb23042013-01-17 12:26:48 +0800187static int
Andy Greenb5b23192013-02-11 17:13:32 +0800188remove_wsi_socket_from_fds(struct libwebsocket_context *context,
189 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000190{
Andy Greendfb23042013-01-17 12:26:48 +0800191 int m;
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800192 struct libwebsocket_pollargs pa = { wsi->sock, 0, 0};
Andy Green0d338332011-02-12 11:57:43 +0000193
Andy Green7a132792013-12-18 09:48:26 +0800194 if (!--context->fds_count) {
195 context->protocols[0].callback(context, wsi,
196 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800197 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800198 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800199 }
Andy Green0d338332011-02-12 11:57:43 +0000200
Andy Greendfb23042013-01-17 12:26:48 +0800201 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800202 lwsl_err("Socket fd %d too high (%d)\n",
203 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800204 return 1;
205 }
Andy Green0d338332011-02-12 11:57:43 +0000206
Andy Greenb5b23192013-02-11 17:13:32 +0800207 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
208 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800209
Andy Green7a132792013-12-18 09:48:26 +0800210 context->protocols[0].callback(context, wsi,
211 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800212 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800213
Andy Greendfb23042013-01-17 12:26:48 +0800214 m = wsi->position_in_fds_table; /* replace the contents for this */
215
216 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800217 context->fds[m] = context->fds[context->fds_count];
218 /*
219 * end guy's fds_lookup entry remains unchanged
220 * (still same fd pointing to same wsi)
221 */
Andy Greendfb23042013-01-17 12:26:48 +0800222 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800223 context->lws_lookup[context->fds[context->fds_count].fd]->
224 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800225 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800226 context->lws_lookup[wsi->sock] = NULL;
227 /* removed wsi has no position any more */
228 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800229
230do_ext:
231 /* remove also from external POLL support via protocol 0 */
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800232 if (wsi->sock) {
Andy Greendfb23042013-01-17 12:26:48 +0800233 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800234 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800235 (void *) &pa, 0);
236 }
Andy Green7a132792013-12-18 09:48:26 +0800237 context->protocols[0].callback(context, wsi,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800238 LWS_CALLBACK_UNLOCK_POLL,
239 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800240 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000241}
242
Andy Green32375b72011-02-19 08:32:53 +0000243
Andy Green8f037e42010-12-19 22:13:26 +0000244void
Peter Hinz56885f32011-03-02 22:03:47 +0000245libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000246 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000247{
Andy Greenb45993c2010-12-18 15:13:50 +0000248 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000249 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000250 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
251 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800252#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000253 int ret;
254 int m;
255 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100256 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800257#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000258
Andy Green4b6fbe12011-02-14 08:03:48 +0000259 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000260 return;
261
Andy Green62c54d22011-02-14 09:14:25 +0000262 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000263
Andy Green62c54d22011-02-14 09:14:25 +0000264 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000265 return;
266
Andy Green22524a62013-02-15 22:48:58 +0800267 /* we tried the polite way... */
268 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
269 goto just_kill_connection;
270
Andy Green623a98d2013-01-21 11:04:23 +0800271 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000272
Andy Green5dc62ea2013-09-20 20:26:12 +0800273 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
274 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
275
276 context->protocols[0].callback(context, wsi,
277 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
278
279 free(wsi->u.hdr.ah);
280 goto just_kill_connection;
281 }
282
283
kapejodce64fb02013-11-19 13:38:16 +0100284 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
285 if (wsi->u.http.post_buffer) {
286 free(wsi->u.http.post_buffer);
287 wsi->u.http.post_buffer = NULL;
288 }
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100289 if (wsi->u.http.fd != LWS_INVALID_FILE) {
290 lwsl_debug("closing http file\n");
291#if defined(WIN32) || defined(_WIN32)
292 CloseHandle(wsi->u.http.fd);
293#else
kapejodce64fb02013-11-19 13:38:16 +0100294 close(wsi->u.http.fd);
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100295#endif
296 wsi->u.http.fd = LWS_INVALID_FILE;
kapejodce64fb02013-11-19 13:38:16 +0100297 context->protocols[0].callback(context, wsi,
298 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
299 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800300 }
301
Andy Green3182ece2013-01-20 17:08:31 +0800302#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000303 /*
Andy Green68b45042011-05-25 21:41:57 +0100304 * are his extensions okay with him closing? Eg he might be a mux
305 * parent and just his ch1 aspect is closing?
306 */
307
Andy Green68b45042011-05-25 21:41:57 +0100308 for (n = 0; n < wsi->count_active_extensions; n++) {
309 if (!wsi->active_extensions[n]->callback)
310 continue;
311
312 m = wsi->active_extensions[n]->callback(context,
313 wsi->active_extensions[n], wsi,
314 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
315 wsi->active_extensions_user[n], NULL, 0);
316
317 /*
318 * if somebody vetoed actually closing him at this time....
319 * up to the extension to track the attempted close, let's
320 * just bail
321 */
322
323 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800324 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100325 return;
326 }
327 }
328
Andy Green68b45042011-05-25 21:41:57 +0100329 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000330 * flush any tx pending from extensions, since we may send close packet
331 * if there are problems with send, just nuke the connection
332 */
333
334 ret = 1;
335 while (ret == 1) {
336
337 /* default to nobody has more to spill */
338
339 ret = 0;
340 eff_buf.token = NULL;
341 eff_buf.token_len = 0;
342
343 /* show every extension the new incoming data */
344
345 for (n = 0; n < wsi->count_active_extensions; n++) {
346 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000347 wsi->protocol->owning_server,
348 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000349 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
350 wsi->active_extensions_user[n], &eff_buf, 0);
351 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800352 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000353 goto just_kill_connection;
354 }
355 if (m)
356 /*
357 * at least one extension told us he has more
358 * to spill, so we will go around again after
359 */
360 ret = 1;
361 }
362
363 /* assuming they left us something to send, send it */
364
365 if (eff_buf.token_len)
366 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800367 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800368 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000369 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800370 }
Andy Greenc44159f2011-03-07 07:08:18 +0000371 }
Andy Green3182ece2013-01-20 17:08:31 +0800372#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000373
374 /*
Andy Greenda527df2011-03-07 07:08:12 +0000375 * signal we are closing, libsocket_write will
376 * add any necessary version-specific stuff. If the write fails,
377 * no worries we are closing anyway. If we didn't initiate this
378 * close, then our state has been changed to
379 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
380 *
381 * Likewise if it's a second call to close this connection after we
382 * sent the close indication to the peer already, we are in state
383 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
384 */
385
386 if (old_state == WSI_STATE_ESTABLISHED &&
387 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100388
Andy Green43db0452013-01-10 19:50:35 +0800389 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100390
Andy Greenfdd305a2013-02-11 14:32:48 +0800391 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800392 memset(buf, 0, sizeof(buf));
393 n = libwebsocket_write(wsi,
394 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000395 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800396 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000397 /*
398 * we have sent a nice protocol level indication we
399 * now wish to close, we should not send anything more
400 */
401
402 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
403
Andy Greenb5b23192013-02-11 17:13:32 +0800404 /*
405 * ...and we should wait for a reply for a bit
406 * out of politeness
407 */
Andy Greenda527df2011-03-07 07:08:12 +0000408
409 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800410 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000411
Andy Green43db0452013-01-10 19:50:35 +0800412 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000413
414 return;
415 }
416
Andy Greenb5b23192013-02-11 17:13:32 +0800417 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800418
Andy Greenda527df2011-03-07 07:08:12 +0000419 /* else, the send failed and we should just hang up */
420 }
421
Andy Greenc44159f2011-03-07 07:08:18 +0000422just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100423
Andy Greenb5b23192013-02-11 17:13:32 +0800424 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100425
Andy Greenda527df2011-03-07 07:08:12 +0000426 /*
427 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800428 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000429 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000430
Andy Greendfb23042013-01-17 12:26:48 +0800431 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000432
Andy Green251f6fa2010-11-03 11:13:06 +0000433 wsi->state = WSI_STATE_DEAD_SOCKET;
434
Andy Greenb5b23192013-02-11 17:13:32 +0800435 if ((old_state == WSI_STATE_ESTABLISHED ||
436 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800437 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
438
439 if (wsi->u.ws.rx_user_buffer) {
440 free(wsi->u.ws.rx_user_buffer);
441 wsi->u.ws.rx_user_buffer = NULL;
442 }
443 if (wsi->u.ws.rxflow_buffer) {
444 free(wsi->u.ws.rxflow_buffer);
445 wsi->u.ws.rxflow_buffer = NULL;
446 }
Andy Green2764eba2013-12-09 14:16:17 +0800447 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800448 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800449 free(wsi->truncated_send_malloc);
450 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800451 }
Andy Green54495112013-02-06 21:10:16 +0900452 }
453
Andy Green4b6fbe12011-02-14 08:03:48 +0000454 /* tell the user it's all over for this guy */
455
Andy Greend4302732011-02-28 07:45:29 +0000456 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800457 ((old_state == WSI_STATE_ESTABLISHED) ||
458 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
459 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800460 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000461 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000462 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800463 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
464 lwsl_debug("calling back CLOSED_HTTP\n");
465 context->protocols[0].callback(context, wsi,
466 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800467 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800468 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000469
Andy Green3182ece2013-01-20 17:08:31 +0800470#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000471 /* deallocate any active extension contexts */
472
473 for (n = 0; n < wsi->count_active_extensions; n++) {
474 if (!wsi->active_extensions[n]->callback)
475 continue;
476
Andy Green46c2ea02011-03-22 09:04:01 +0000477 wsi->active_extensions[n]->callback(context,
478 wsi->active_extensions[n], wsi,
479 LWS_EXT_CALLBACK_DESTROY,
480 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000481
482 free(wsi->active_extensions_user[n]);
483 }
484
Andy Greena41314f2011-05-23 10:00:03 +0100485 /*
486 * inform all extensions in case they tracked this guy out of band
487 * even though not active on him specifically
488 */
489
490 ext = context->extensions;
491 while (ext && ext->callback) {
492 ext->callback(context, ext, wsi,
493 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
494 NULL, NULL, 0);
495 ext++;
496 }
Andy Green3182ece2013-01-20 17:08:31 +0800497#endif
Andy Greena41314f2011-05-23 10:00:03 +0100498
Andy Green43db0452013-01-10 19:50:35 +0800499/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000500
Andy Green3faa9c72010-11-08 17:03:03 +0000501#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000502 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000503 n = SSL_get_fd(wsi->ssl);
504 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800505 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000506 SSL_free(wsi->ssl);
507 } else {
508#endif
Andy Green0303db42013-01-17 14:46:43 +0800509 if (wsi->sock) {
510 n = shutdown(wsi->sock, SHUT_RDWR);
511 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800512 lwsl_debug("closing: shutdown returned %d\n",
513 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800514
Andy Green0303db42013-01-17 14:46:43 +0800515 n = compatible_close(wsi->sock);
516 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800517 lwsl_debug("closing: close returned %d\n",
518 errno);
Andy Green0303db42013-01-17 14:46:43 +0800519 }
Andy Green3faa9c72010-11-08 17:03:03 +0000520#ifdef LWS_OPENSSL_SUPPORT
521 }
522#endif
Andy Green76b6ea12014-02-15 19:25:50 +0800523
524 /* outermost destroy notification for wsi (user_space still intact) */
525 context->protocols[0].callback(context, wsi,
526 LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0);
527
Andy Greenb5b23192013-02-11 17:13:32 +0800528 if (wsi->protocol && wsi->protocol->per_session_data_size &&
529 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000530 free(wsi->user_space);
531
Andy Green251f6fa2010-11-03 11:13:06 +0000532 free(wsi);
533}
534
Andy Green07034092011-02-13 08:37:12 +0000535/**
536 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800537 * @context: Libwebsockets context
538 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000539 * @fd: Connection socket descriptor
540 * @name: Buffer to take client address name
541 * @name_len: Length of client address name buffer
542 * @rip: Buffer to take client address IP qotted quad
543 * @rip_len: Length of client address IP buffer
544 *
545 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800546 * the client connected with socket descriptor @fd. Names may be
547 * truncated if there is not enough room. If either cannot be
548 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000549 */
550
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800551LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800552libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
553 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000554 char *rip, int rip_len)
555{
Bob Robertsac049112013-04-25 09:16:30 +0800556 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000557 struct sockaddr_in sin;
558 struct hostent *host;
559 struct hostent *host1;
560 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000561 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000562 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800563 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800564#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800565 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800566#endif
Andy Green07034092011-02-13 08:37:12 +0000567
568 rip[0] = '\0';
569 name[0] = '\0';
570
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800571 lws_latency_pre(context, wsi);
572
Andy Greenb5b23192013-02-11 17:13:32 +0800573 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000574 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100575 lwsl_warn("getpeername: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800576 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000577 }
Andy Green6ee372f2012-04-09 15:09:01 +0800578
Andy Greenb5b23192013-02-11 17:13:32 +0800579 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000580 AF_INET);
581 if (host == NULL) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100582 lwsl_warn("gethostbyaddr: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800583 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000584 }
585
586 strncpy(name, host->h_name, name_len);
587 name[name_len - 1] = '\0';
588
589 host1 = gethostbyname(host->h_name);
590 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800591 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000592 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000593 n = 0;
594 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000595 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000596 if (p == NULL)
597 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000598 if ((host1->h_addrtype != AF_INET)
599#ifdef AF_LOCAL
600 && (host1->h_addrtype != AF_LOCAL)
601#endif
602 )
Andy Green07034092011-02-13 08:37:12 +0000603 continue;
604
Andy Green7627af52011-03-09 15:13:52 +0000605 if (host1->h_addrtype == AF_INET)
606 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000607#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000608 else {
609 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800610 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000611 ip[sizeof(ip) - 1] = '\0';
612 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000613#endif
Andy Green07034092011-02-13 08:37:12 +0000614 p = NULL;
615 strncpy(rip, ip, rip_len);
616 rip[rip_len - 1] = '\0';
617 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800618
619 ret = 0;
620bail:
621 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000622}
Andy Green9f990342011-02-12 11:57:45 +0000623
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800624LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000625 void *buf, int len)
626{
627 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800628 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000629
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100630#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000631 for (n = 0; n < len; n++)
632 p[n] = (unsigned char)rand();
633#else
634 n = read(context->fd_random, p, len);
635#endif
636
637 return n;
638}
639
Andy Greena690cd02013-02-09 12:25:31 +0800640int lws_set_socket_options(struct libwebsocket_context *context, int fd)
641{
642 int optval = 1;
643 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100644#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800645 unsigned long optl = 0;
646#endif
647#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
648 struct protoent *tcp_proto;
649#endif
650
651 if (context->ka_time) {
652 /* enable keepalive on this socket */
653 optval = 1;
654 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
655 (const void *)&optval, optlen) < 0)
656 return 1;
657
Bob Robertsac049112013-04-25 09:16:30 +0800658#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800659
660 /*
661 * didn't find a way to set these per-socket, need to
662 * tune kernel systemwide values
663 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100664#elif WIN32
665 {
666 DWORD dwBytesRet;
667 struct tcp_keepalive alive;
668 alive.onoff = TRUE;
669 alive.keepalivetime = context->ka_time;
670 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800671
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100672 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
673 NULL, 0, &dwBytesRet, NULL, NULL))
674 return 1;
675 }
Andy Greena47865f2013-02-10 09:39:47 +0800676#else
Andy Greena690cd02013-02-09 12:25:31 +0800677 /* set the keepalive conditions we want on it too */
678 optval = context->ka_time;
679 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
680 (const void *)&optval, optlen) < 0)
681 return 1;
682
Larry Hayesbb66ac62013-02-22 09:16:20 +0800683 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800684 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
685 (const void *)&optval, optlen) < 0)
686 return 1;
687
Larry Hayesbb66ac62013-02-22 09:16:20 +0800688 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800689 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
690 (const void *)&optval, optlen) < 0)
691 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800692#endif
Andy Greena690cd02013-02-09 12:25:31 +0800693 }
694
695 /* Disable Nagle */
696 optval = 1;
697#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
698 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
699#else
700 tcp_proto = getprotobyname("TCP");
701 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
702#endif
703
704 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100705#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800706 ioctlsocket(fd, FIONBIO, &optl);
707#else
708 fcntl(fd, F_SETFL, O_NONBLOCK);
709#endif
710
711 return 0;
712}
713
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800714LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000715{
716 struct pollfd fds;
717
Andy Green2764eba2013-12-09 14:16:17 +0800718 /* treat the fact we got a truncated send pending as if we're choked */
719 if (wsi->truncated_send_malloc)
720 return 1;
721
Andy Green95a7b5d2011-03-06 10:29:39 +0000722 fds.fd = wsi->sock;
723 fds.events = POLLOUT;
724 fds.revents = 0;
725
726 if (poll(&fds, 1, 0) != 1)
727 return 1;
728
729 if ((fds.revents & POLLOUT) == 0)
730 return 1;
731
732 /* okay to send another packet without blocking */
733
734 return 0;
735}
736
Andy Greena41314f2011-05-23 10:00:03 +0100737int
Andy Green3b84c002011-03-06 13:14:42 +0000738lws_handle_POLLOUT_event(struct libwebsocket_context *context,
739 struct libwebsocket *wsi, struct pollfd *pollfd)
740{
Andy Green3b84c002011-03-06 13:14:42 +0000741 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800742
Andy Green3182ece2013-01-20 17:08:31 +0800743#ifndef LWS_NO_EXTENSIONS
744 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000745 int ret;
746 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100747 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000748
Andy Green1f4267b2013-10-17 08:09:19 +0800749 /* pending truncated sends have uber priority */
750
Andy Green2764eba2013-12-09 14:16:17 +0800751 if (wsi->truncated_send_malloc) {
752 lws_issue_raw(wsi, wsi->truncated_send_malloc +
753 wsi->truncated_send_offset,
754 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800755 /* leave POLLOUT active either way */
756 return 0;
757 }
758
Andy Greena41314f2011-05-23 10:00:03 +0100759 for (n = 0; n < wsi->count_active_extensions; n++) {
760 if (!wsi->active_extensions[n]->callback)
761 continue;
762
763 m = wsi->active_extensions[n]->callback(context,
764 wsi->active_extensions[n], wsi,
765 LWS_EXT_CALLBACK_IS_WRITEABLE,
766 wsi->active_extensions_user[n], NULL, 0);
767 if (m > handled)
768 handled = m;
769 }
770
771 if (handled == 1)
772 goto notify_action;
773
774 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000775 goto user_service;
776
777 /*
778 * check in on the active extensions, see if they
779 * had pending stuff to spill... they need to get the
780 * first look-in otherwise sequence will be disordered
781 *
782 * NULL, zero-length eff_buf means just spill pending
783 */
784
785 ret = 1;
786 while (ret == 1) {
787
788 /* default to nobody has more to spill */
789
790 ret = 0;
791 eff_buf.token = NULL;
792 eff_buf.token_len = 0;
793
794 /* give every extension a chance to spill */
795
796 for (n = 0; n < wsi->count_active_extensions; n++) {
797 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000798 wsi->protocol->owning_server,
799 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000800 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
801 wsi->active_extensions_user[n], &eff_buf, 0);
802 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800803 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000804 return -1;
805 }
806 if (m)
807 /*
808 * at least one extension told us he has more
809 * to spill, so we will go around again after
810 */
811 ret = 1;
812 }
813
814 /* assuming they gave us something to send, send it */
815
816 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800817 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
818 eff_buf.token_len);
819 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000820 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800821 /*
822 * Keep amount spilled small to minimize chance of this
823 */
824 if (n != eff_buf.token_len) {
825 lwsl_err("Unable to spill ext %d vs %s\n",
826 eff_buf.token_len, n);
827 return -1;
828 }
Andy Green3b84c002011-03-06 13:14:42 +0000829 } else
830 continue;
831
832 /* no extension has more to spill */
833
834 if (!ret)
835 continue;
836
837 /*
838 * There's more to spill from an extension, but we just sent
839 * something... did that leave the pipe choked?
840 */
841
842 if (!lws_send_pipe_choked(wsi))
843 /* no we could add more */
844 continue;
845
Andy Green43db0452013-01-10 19:50:35 +0800846 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000847
848 /*
849 * Yes, he's choked. Leave the POLLOUT masked on so we will
850 * come back here when he is unchoked. Don't call the user
851 * callback to enforce ordering of spilling, he'll get called
852 * when we come back here and there's nothing more to spill.
853 */
854
855 return 0;
856 }
857
858 wsi->extension_data_pending = 0;
859
860user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800861#endif
Andy Green3b84c002011-03-06 13:14:42 +0000862 /* one shot */
863
Andy Green91f19d82013-12-21 11:18:34 +0800864 if (pollfd)
865 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800866
Andy Green3182ece2013-01-20 17:08:31 +0800867#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100868notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800869#endif
Andy Green3b84c002011-03-06 13:14:42 +0000870
Andy Green9e4c2b62011-03-07 20:47:39 +0000871 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
872 n = LWS_CALLBACK_CLIENT_WRITEABLE;
873 else
874 n = LWS_CALLBACK_SERVER_WRITEABLE;
875
Andy Greenb8b247d2013-01-22 07:20:08 +0800876 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800877 wsi, (enum libwebsocket_callback_reasons) n,
878 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000879}
880
881
882
Andy Green1c6e1422013-02-20 19:11:31 +0800883int
Andy Greena41314f2011-05-23 10:00:03 +0100884libwebsocket_service_timeout_check(struct libwebsocket_context *context,
885 struct libwebsocket *wsi, unsigned int sec)
886{
Andy Green3182ece2013-01-20 17:08:31 +0800887#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100888 int n;
889
890 /*
891 * if extensions want in on it (eg, we are a mux parent)
892 * give them a chance to service child timeouts
893 */
894
895 for (n = 0; n < wsi->count_active_extensions; n++)
896 wsi->active_extensions[n]->callback(
897 context, wsi->active_extensions[n],
898 wsi, LWS_EXT_CALLBACK_1HZ,
899 wsi->active_extensions_user[n], NULL, sec);
900
Andy Green3182ece2013-01-20 17:08:31 +0800901#endif
Andy Greena41314f2011-05-23 10:00:03 +0100902 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800903 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800904
Andy Greena41314f2011-05-23 10:00:03 +0100905 /*
906 * if we went beyond the allowed time, kill the
907 * connection
908 */
909
910 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800911 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100912 libwebsocket_close_and_free_session(context,
913 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800914 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100915 }
Andy Green1c6e1422013-02-20 19:11:31 +0800916
917 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100918}
919
Andy Green9f990342011-02-12 11:57:45 +0000920/**
921 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000922 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000923 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800924 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000925 *
Andy Green75006172013-01-22 12:32:11 +0800926 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800927 * services it according to the state of the associated
928 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800929 *
930 * The one call deals with all "service" that might happen on a socket
931 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800932 *
933 * If a pollfd says it has something, you can just pass it to
934 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
935 * If it sees it is a lws socket, the traffic will be handled and
936 * pollfd->revents will be zeroed now.
937 *
938 * If the socket is foreign to lws, it leaves revents alone. So you can
939 * see if you should service yourself by checking the pollfd revents
940 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000941 */
942
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800943LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000944libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000945 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000946{
Andy Greena1ce6be2013-01-18 11:43:21 +0800947 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000948 int n;
Andy Green0d338332011-02-12 11:57:43 +0000949 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800950 int listen_socket_fds_index = 0;
Patrick Gansterer92792b42014-02-26 21:37:31 +0100951 time_t now;
Andy Green1c6e1422013-02-20 19:11:31 +0800952 int timed_out = 0;
953 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800954 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800955
Andy Green3182ece2013-01-20 17:08:31 +0800956#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000957 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800958#endif
Andy Green98a717c2011-03-06 13:14:15 +0000959 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800960
961 if (context->listen_service_fd)
962 listen_socket_fds_index = context->lws_lookup[
963 context->listen_service_fd]->position_in_fds_table;
964
Andy Greena71eafc2011-02-14 17:59:43 +0000965 /*
966 * you can call us with pollfd = NULL to just allow the once-per-second
967 * global timeout checks; if less than a second since the last check
968 * it returns immediately then.
969 */
970
Patrick Gansterer92792b42014-02-26 21:37:31 +0100971 time(&now);
Andy Greena71eafc2011-02-14 17:59:43 +0000972
Patrick Gansterer92792b42014-02-26 21:37:31 +0100973 if (context->last_timeout_check_s != now) {
974 context->last_timeout_check_s = now;
Andy Greena71eafc2011-02-14 17:59:43 +0000975
Joakim Soderberg4c531232013-02-06 15:26:58 +0900976 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800977 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800978 if (context->started_with_parent &&
979 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800980 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900981 #endif
Andy Green24cba922013-01-19 13:56:10 +0800982
Andy Greena71eafc2011-02-14 17:59:43 +0000983 /* global timeout check once per second */
984
Andy Green1c6e1422013-02-20 19:11:31 +0800985 if (pollfd)
986 our_fd = pollfd->fd;
987
Peter Hinz56885f32011-03-02 22:03:47 +0000988 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800989 m = context->fds[n].fd;
990 wsi = context->lws_lookup[m];
991 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800992 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800993
Patrick Gansterer92792b42014-02-26 21:37:31 +0100994 if (libwebsocket_service_timeout_check(context, wsi, now))
Andy Green1c6e1422013-02-20 19:11:31 +0800995 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800996 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800997 /* it was the guy we came to service! */
998 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800999 /* mark as handled */
1000 pollfd->revents = 0;
1001 }
Andy Greena71eafc2011-02-14 17:59:43 +00001002 }
1003 }
1004
Andy Green1c6e1422013-02-20 19:11:31 +08001005 /* the socket we came to service timed out, nothing to do */
1006 if (timed_out)
1007 return 0;
1008
Andy Greena71eafc2011-02-14 17:59:43 +00001009 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +00001010 if (pollfd == NULL)
1011 return 0;
1012
1013 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001014 wsi = context->lws_lookup[pollfd->fd];
1015 if (wsi == NULL)
1016 /* not lws connection ... leave revents alone and return */
1017 return 0;
1018
1019 /*
1020 * so that caller can tell we handled, past here we need to
1021 * zero down pollfd->revents after handling
1022 */
1023
Andy Green65b0e912013-01-16 07:59:47 +08001024 /*
1025 * deal with listen service piggybacking
1026 * every listen_service_modulo services of other fds, we
1027 * sneak one in to service the listen socket if there's anything waiting
1028 *
1029 * To handle connection storms, as found in ab, if we previously saw a
1030 * pending connection here, it causes us to check again next time.
1031 */
1032
Andy Greenb5b23192013-02-11 17:13:32 +08001033 if (context->listen_service_fd && pollfd !=
1034 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +08001035 context->listen_service_count++;
1036 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +08001037 context->listen_service_count ==
1038 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001039 context->listen_service_count = 0;
1040 m = 1;
1041 if (context->listen_service_extraseen > 5)
1042 m = 2;
1043 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001044 /*
1045 * even with extpoll, we prepared this
1046 * internal fds for listen
1047 */
1048 n = poll(&context->fds[listen_socket_fds_index],
1049 1, 0);
1050 if (n > 0) { /* there's a conn waiting for us */
1051 libwebsocket_service_fd(context,
1052 &context->
1053 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001054 context->listen_service_extraseen++;
1055 } else {
1056 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001057 context->
1058 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001059 break;
1060 }
1061 }
1062 }
1063
1064 }
1065
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001066 /* handle session socket closed */
1067
1068 if ((!(pollfd->revents & POLLIN)) &&
1069 (pollfd->revents & (POLLERR | POLLHUP))) {
1070
1071 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1072 (void *)wsi, pollfd->fd);
1073
1074 goto close_and_handled;
1075 }
1076
Andy Green65b0e912013-01-16 07:59:47 +08001077 /* okay, what we came here to do... */
1078
Andy Green0d338332011-02-12 11:57:43 +00001079 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001080
Andy Greena1ce6be2013-01-18 11:43:21 +08001081#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001082 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001083 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001084 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001085 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001086 n = lws_server_socket_service(context, wsi, pollfd);
1087 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001088#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001089
Andy Green0d338332011-02-12 11:57:43 +00001090 case LWS_CONNMODE_WS_SERVING:
1091 case LWS_CONNMODE_WS_CLIENT:
1092
Andy Green0d338332011-02-12 11:57:43 +00001093 /* the guy requested a callback when it was OK to write */
1094
Andy Greenda527df2011-03-07 07:08:12 +00001095 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001096 wsi->state == WSI_STATE_ESTABLISHED &&
1097 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001098 lwsl_info("libwebsocket_service_fd: closing\n");
1099 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001100 }
Andy Green0d338332011-02-12 11:57:43 +00001101
Andy Greenca0a1292013-03-16 11:24:23 +08001102 if (wsi->u.ws.rxflow_buffer &&
1103 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1104 lwsl_info("draining rxflow\n");
1105 /* well, drain it */
1106 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1107 wsi->u.ws.rxflow_pos;
1108 eff_buf.token_len = wsi->u.ws.rxflow_len -
1109 wsi->u.ws.rxflow_pos;
1110 draining_flow = 1;
1111 goto drain;
1112 }
1113
Andy Green0d338332011-02-12 11:57:43 +00001114 /* any incoming data ready? */
1115
1116 if (!(pollfd->revents & POLLIN))
1117 break;
1118
Andy Greenb45993c2010-12-18 15:13:50 +00001119#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001120read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001121 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001122 eff_buf.token_len = SSL_read(wsi->ssl,
1123 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001124 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001125 if (!eff_buf.token_len) {
1126 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001127 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001128 ERR_error_string(n,
1129 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001130 }
1131 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001132#endif
Andy Greene84652c2013-02-08 13:01:02 +08001133 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001134 context->service_buffer,
1135 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001136
Andy Green98a717c2011-03-06 13:14:15 +00001137 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001138 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1139 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001140 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001141 goto close_and_handled;
1142 n = 0;
1143 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001144 }
Andy Green98a717c2011-03-06 13:14:15 +00001145 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001146 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001147 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001148 }
1149
Andy Green98a717c2011-03-06 13:14:15 +00001150 /*
1151 * give any active extensions a chance to munge the buffer
1152 * before parse. We pass in a pointer to an lws_tokens struct
1153 * prepared with the default buffer and content length that's in
1154 * there. Rather than rewrite the default buffer, extensions
1155 * that expect to grow the buffer can adapt .token to
1156 * point to their own per-connection buffer in the extension
1157 * user allocation. By default with no extensions or no
1158 * extension callback handling, just the normal input buffer is
1159 * used then so it is efficient.
1160 */
Andy Greenb45993c2010-12-18 15:13:50 +00001161
Andy Greene84652c2013-02-08 13:01:02 +08001162 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001163drain:
Andy Green3182ece2013-01-20 17:08:31 +08001164#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001165 more = 1;
1166 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001167
Andy Green98a717c2011-03-06 13:14:15 +00001168 more = 0;
1169
1170 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001171 m = wsi->active_extensions[n]->callback(context,
1172 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001173 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001174 wsi->active_extensions_user[n],
1175 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001176 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001177 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001178 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001179 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001180 }
1181 if (m)
1182 more = 1;
1183 }
Andy Green3182ece2013-01-20 17:08:31 +08001184#endif
Andy Green98a717c2011-03-06 13:14:15 +00001185 /* service incoming data */
1186
1187 if (eff_buf.token_len) {
1188 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001189 (unsigned char *)eff_buf.token,
1190 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001191 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001192 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001193 n = 0;
1194 goto handled;
1195 }
Andy Green98a717c2011-03-06 13:14:15 +00001196 }
Andy Green3182ece2013-01-20 17:08:31 +08001197#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001198 eff_buf.token = NULL;
1199 eff_buf.token_len = 0;
1200 }
Andy Green3182ece2013-01-20 17:08:31 +08001201#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001202 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1203 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1204 lwsl_info("flow buffer: drained\n");
1205 free(wsi->u.ws.rxflow_buffer);
1206 wsi->u.ws.rxflow_buffer = NULL;
1207 /* having drained the rxflow buffer, can rearm POLLIN */
1208 _libwebsocket_rx_flow_control(wsi);
1209 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001210
1211#ifdef LWS_OPENSSL_SUPPORT
1212 if (wsi->ssl && SSL_pending(wsi->ssl))
1213 goto read_pending;
1214#endif
Andy Green98a717c2011-03-06 13:14:15 +00001215 break;
Andy Green76f61e72013-01-16 11:53:05 +08001216
1217 default:
Andy Green03674a62013-01-16 11:47:40 +08001218#ifdef LWS_NO_CLIENT
1219 break;
1220#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001221 n = lws_client_socket_service(context, wsi, pollfd);
1222 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001223#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001224 }
1225
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001226 n = 0;
1227 goto handled;
1228
1229close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001230 libwebsocket_close_and_free_session(context, wsi,
1231 LWS_CLOSE_STATUS_NOSTATUS);
1232 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001233
1234handled:
1235 pollfd->revents = 0;
1236 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001237}
1238
Andy Green0d338332011-02-12 11:57:43 +00001239
Andy Green6964bb52011-01-23 16:50:33 +00001240/**
1241 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001242 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001243 *
1244 * This function closes any active connections and then frees the
1245 * context. After calling this, any further use of the context is
1246 * undefined.
1247 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001248LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001249libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001250{
Andy Green0d338332011-02-12 11:57:43 +00001251 int n;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001252#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001253 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001254 struct libwebsocket_extension *ext;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001255#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001256 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001257
Andy Greend636e352013-01-29 12:36:17 +08001258#ifdef LWS_LATENCY
1259 if (context->worst_latency_info[0])
1260 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1261#endif
1262
Andy Greendfb23042013-01-17 12:26:48 +08001263 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001264 struct libwebsocket *wsi =
1265 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001266 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001267 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1268 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001269 }
Andy Green6964bb52011-01-23 16:50:33 +00001270
arnaudviala7c6f26c2014-02-15 14:39:40 +08001271#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001272 /*
1273 * give all extensions a chance to clean up any per-context
1274 * allocations they might have made
1275 */
1276
1277 ext = context->extensions;
1278 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1279 if (context->listen_port)
1280 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001281 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001282 ext->callback(context, ext, NULL,
1283 (enum libwebsocket_extension_callback_reasons)m,
1284 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001285 ext++;
1286 }
arnaudviala7c6f26c2014-02-15 14:39:40 +08001287#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001288
1289 /*
1290 * inform all the protocols that they are done and will have no more
1291 * callbacks
1292 */
1293
1294 while (protocol->callback) {
1295 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1296 NULL, NULL, 0);
1297 protocol++;
1298 }
1299
Andy Greena41314f2011-05-23 10:00:03 +01001300
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001301#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001302#else
1303 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001304#endif
1305
Peter Hinz56885f32011-03-02 22:03:47 +00001306#ifdef LWS_OPENSSL_SUPPORT
1307 if (context->ssl_ctx)
1308 SSL_CTX_free(context->ssl_ctx);
1309 if (context->ssl_client_ctx)
1310 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001311
1312 ERR_remove_state(0);
1313 ERR_free_strings();
1314 EVP_cleanup();
1315 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001316#endif
1317
Andy Green46596482013-02-11 11:04:01 +08001318 if (context->fds)
1319 free(context->fds);
1320 if (context->lws_lookup)
1321 free(context->lws_lookup);
1322
Peter Hinz56885f32011-03-02 22:03:47 +00001323 free(context);
1324
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001325#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001326 WSACleanup();
1327#endif
Andy Green6964bb52011-01-23 16:50:33 +00001328}
1329
Andy Greend88146d2013-01-22 12:40:35 +08001330/**
Andy Greenb5b23192013-02-11 17:13:32 +08001331 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001332 * @context: Websocket context
1333 *
1334 * This returns the optional user allocation that can be attached to
1335 * the context the sockets live in at context_create time. It's a way
1336 * to let all sockets serviced in the same context share data without
1337 * using globals statics in the user code.
1338 */
Alon Levy0291eb32012-10-19 11:21:56 +02001339LWS_EXTERN void *
1340libwebsocket_context_user(struct libwebsocket_context *context)
1341{
Andy Greenb5b23192013-02-11 17:13:32 +08001342 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001343}
1344
Andy Green6964bb52011-01-23 16:50:33 +00001345/**
1346 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001347 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001348 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1349 * service otherwise block and service immediately, returning
1350 * after the timeout if nothing needed service.
1351 *
1352 * This function deals with any pending websocket traffic, for three
1353 * kinds of event. It handles these events on both server and client
1354 * types of connection the same.
1355 *
1356 * 1) Accept new connections to our context's server
1357 *
Andy Green6f520a52013-01-29 17:57:39 +08001358 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001359 * server or client connections.
1360 *
1361 * You need to call this service function periodically to all the above
1362 * functions to happen; if your application is single-threaded you can
1363 * just call it in your main event loop.
1364 *
1365 * Alternatively you can fork a new process that asynchronously handles
1366 * calling this service in a loop. In that case you are happy if this
1367 * call blocks your thread until it needs to take care of something and
1368 * would call it with a large nonzero timeout. Your loop then takes no
1369 * CPU while there is nothing happening.
1370 *
1371 * If you are calling it in a single-threaded app, you don't want it to
1372 * wait around blocking other things in your loop from happening, so you
1373 * would call it with a timeout_ms of 0, so it returns immediately if
1374 * nothing is pending, or as soon as it services whatever was pending.
1375 */
1376
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001377LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001378libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001379{
1380 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001381 int m;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001382#ifdef LWS_HAS_PPOLL
1383 struct timespec timeout_ts;
1384 sigset_t sigmask;
1385#endif
Andy Greene92cd172011-01-19 13:11:55 +00001386
1387 /* stay dead once we are dead */
1388
Peter Hinz56885f32011-03-02 22:03:47 +00001389 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001390 return 1;
1391
Andy Green3b3fa9e2013-12-25 16:34:37 +08001392#ifdef LWS_HAS_PPOLL
1393 lws_idling_ppoll_tid = context->protocols[0].callback(context, NULL,
1394 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1395
1396 timeout_ts.tv_sec = timeout_ms / 1000;
1397 timeout_ts.tv_nsec = timeout_ms % 1000;
Andy Greened451d52014-01-11 12:37:07 +08001398
1399 sigprocmask(SIG_BLOCK, NULL, &sigmask);
1400 sigdelset(&sigmask, SIGUSR2);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001401
Andy Green0d338332011-02-12 11:57:43 +00001402 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001403
Andy Green3b3fa9e2013-12-25 16:34:37 +08001404 n = ppoll(context->fds, context->fds_count, &timeout_ts, &sigmask);
1405 lws_idling_ppoll_tid = 0;
1406#else
Peter Hinz56885f32011-03-02 22:03:47 +00001407 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001408#endif
Andy Green5dc62ea2013-09-20 20:26:12 +08001409 if (n == 0) /* poll timeout */ {
1410 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001411 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001412 }
Andy Greene92cd172011-01-19 13:11:55 +00001413
Andy Greened451d52014-01-11 12:37:07 +08001414 if (n < 0) {
1415 if (errno != EINTR)
1416 return -1;
1417 else
1418 return 0;
1419 }
Andy Greene92cd172011-01-19 13:11:55 +00001420
Andy Greendfb23042013-01-17 12:26:48 +08001421 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001422
Andy Greenca0a1292013-03-16 11:24:23 +08001423 for (n = 0; n < context->fds_count; n++) {
1424 if (!context->fds[n].revents)
1425 continue;
1426 m = libwebsocket_service_fd(context, &context->fds[n]);
1427 if (m < 0)
1428 return -1;
1429 /* if something closed, retry this slot */
1430 if (m)
1431 n--;
1432 }
1433
Andy Greene92cd172011-01-19 13:11:55 +00001434 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001435}
1436
Andy Green3182ece2013-01-20 17:08:31 +08001437#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001438int
1439lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001440 struct libwebsocket *wsi,
1441 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001442 void *v, size_t len)
1443{
1444 int n;
1445 int handled = 0;
1446
1447 /* maybe an extension will take care of it for us */
1448
1449 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1450 if (!wsi->active_extensions[n]->callback)
1451 continue;
1452
1453 handled |= wsi->active_extensions[n]->callback(context,
1454 wsi->active_extensions[n], wsi,
1455 r, wsi->active_extensions_user[n], v, len);
1456 }
1457
1458 return handled;
1459}
1460
1461
1462void *
1463lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001464 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001465{
1466 int n = 0;
1467
Andy Green68b45042011-05-25 21:41:57 +01001468 if (wsi == NULL)
1469 return NULL;
1470
Andy Greena41314f2011-05-23 10:00:03 +01001471 while (n < wsi->count_active_extensions) {
1472 if (wsi->active_extensions[n] != ext) {
1473 n++;
1474 continue;
1475 }
1476 return wsi->active_extensions_user[n];
1477 }
1478
1479 return NULL;
1480}
Andy Green3182ece2013-01-20 17:08:31 +08001481#endif
Andy Greena41314f2011-05-23 10:00:03 +01001482
Andy Green91f19d82013-12-21 11:18:34 +08001483void
1484lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1485{
1486 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001487 int events;
1488#ifdef LWS_HAS_PPOLL
1489 int tid;
1490 int sampled_ppoll_tid;
1491#endif
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001492 struct libwebsocket_pollargs pa;
1493
1494 pa.fd = wsi->sock;
Andy Green91f19d82013-12-21 11:18:34 +08001495
1496 context->protocols[0].callback(context, wsi,
1497 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001498 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001499
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001500 pa.prev_events = events = context->fds[wsi->position_in_fds_table].events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001501
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001502 pa.events = context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001503
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001504 context->protocols[0].callback(context, wsi,
1505 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
1506 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001507
Andy Green91f19d82013-12-21 11:18:34 +08001508
Andy Green3b3fa9e2013-12-25 16:34:37 +08001509#ifdef LWS_HAS_PPOLL
1510 /*
1511 * if we changed something in this pollfd...
1512 * ... and we're running in a different thread context
1513 * than the service thread...
1514 * ... and the service thread is waiting in ppoll()...
1515 * then fire a SIGUSR2 at the service thread to force it to
1516 * restart the ppoll() with our changed events
1517 */
1518 if (events != context->fds[wsi->position_in_fds_table].events) {
1519 sampled_ppoll_tid = lws_idling_ppoll_tid;
1520 if (sampled_ppoll_tid) {
1521 tid = context->protocols[0].callback(context, NULL,
1522 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1523 if (tid != sampled_ppoll_tid)
1524 kill(sampled_ppoll_tid, SIGUSR2);
1525 }
1526 }
1527#endif
1528
Andy Green91f19d82013-12-21 11:18:34 +08001529 context->protocols[0].callback(context, wsi,
1530 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001531 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001532}
1533
1534
Andy Green90c7cbc2011-01-27 06:26:52 +00001535/**
1536 * libwebsocket_callback_on_writable() - Request a callback when this socket
1537 * becomes able to be written to without
1538 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001539 *
Peter Hinz56885f32011-03-02 22:03:47 +00001540 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001541 * @wsi: Websocket connection instance to get callback for
1542 */
1543
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001544LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001545libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001546 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001547{
Andy Green3182ece2013-01-20 17:08:31 +08001548#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001549 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001550 int handled = 0;
1551
1552 /* maybe an extension will take care of it for us */
1553
1554 for (n = 0; n < wsi->count_active_extensions; n++) {
1555 if (!wsi->active_extensions[n]->callback)
1556 continue;
1557
1558 handled |= wsi->active_extensions[n]->callback(context,
1559 wsi->active_extensions[n], wsi,
1560 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1561 wsi->active_extensions_user[n], NULL, 0);
1562 }
1563
1564 if (handled)
1565 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001566#endif
Andy Greendfb23042013-01-17 12:26:48 +08001567 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001568 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1569 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001570 return -1;
1571 }
1572
Andy Green91f19d82013-12-21 11:18:34 +08001573 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001574
Andy Green90c7cbc2011-01-27 06:26:52 +00001575 return 1;
1576}
1577
1578/**
1579 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1580 * all connections using the given protocol when it
1581 * becomes possible to write to each socket without
1582 * blocking in turn.
1583 *
1584 * @protocol: Protocol whose connections will get callbacks
1585 */
1586
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001587LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001588libwebsocket_callback_on_writable_all_protocol(
1589 const struct libwebsocket_protocols *protocol)
1590{
Peter Hinz56885f32011-03-02 22:03:47 +00001591 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001592 int n;
Andy Green0d338332011-02-12 11:57:43 +00001593 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001594
Andy Greendfb23042013-01-17 12:26:48 +08001595 for (n = 0; n < context->fds_count; n++) {
1596 wsi = context->lws_lookup[context->fds[n].fd];
1597 if (!wsi)
1598 continue;
1599 if (wsi->protocol == protocol)
1600 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001601 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001602
1603 return 0;
1604}
1605
Andy Greenbe93fef2011-02-14 20:25:43 +00001606/**
Andy Greene39e6ef2014-02-15 16:36:38 +08001607 * libwebsocket_callback_all_protocol() - Callback all connections using
1608 * the given protocol with the given reason
1609 *
1610 * @protocol: Protocol whose connections will get callbacks
1611 * @reason: Callback reason index
1612 */
1613
1614LWS_VISIBLE int
1615libwebsocket_callback_all_protocol(
1616 const struct libwebsocket_protocols *protocol, int reason)
1617{
1618 struct libwebsocket_context *context = protocol->owning_server;
1619 int n;
1620 struct libwebsocket *wsi;
1621
1622 for (n = 0; n < context->fds_count; n++) {
1623 wsi = context->lws_lookup[context->fds[n].fd];
1624 if (!wsi)
1625 continue;
1626 if (wsi->protocol == protocol)
1627 protocol->callback(context, wsi,
1628 reason, wsi->user_space, NULL, 0);
1629 }
1630
1631 return 0;
1632}
1633
1634/**
Andy Greenbe93fef2011-02-14 20:25:43 +00001635 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1636 *
1637 * You will not need this unless you are doing something special
1638 *
1639 * @wsi: Websocket connection instance
1640 * @reason: timeout reason
1641 * @secs: how many seconds
1642 */
1643
Craig McQueene7fd8b12014-02-19 09:24:17 +11001644LWS_VISIBLE void
Andy Greenbe93fef2011-02-14 20:25:43 +00001645libwebsocket_set_timeout(struct libwebsocket *wsi,
1646 enum pending_timeout reason, int secs)
1647{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001648 time_t now;
Andy Greenbe93fef2011-02-14 20:25:43 +00001649
Patrick Gansterer92792b42014-02-26 21:37:31 +01001650 time(&now);
Andy Greenbe93fef2011-02-14 20:25:43 +00001651
Patrick Gansterer92792b42014-02-26 21:37:31 +01001652 wsi->pending_timeout_limit = now + secs;
Andy Greenbe93fef2011-02-14 20:25:43 +00001653 wsi->pending_timeout = reason;
1654}
1655
Andy Greena6cbece2011-01-27 20:06:03 +00001656
1657/**
1658 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1659 *
1660 * You will not need this unless you are doing something special
1661 *
1662 * @wsi: Websocket connection instance
1663 */
1664
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001665LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001666libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1667{
1668 return wsi->sock;
1669}
1670
Andy Greend636e352013-01-29 12:36:17 +08001671#ifdef LWS_LATENCY
1672void
Andy Greenb5b23192013-02-11 17:13:32 +08001673lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1674 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001675{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001676 unsigned long long u;
Andy Greend636e352013-01-29 12:36:17 +08001677 char buf[256];
1678
Patrick Gansterer92792b42014-02-26 21:37:31 +01001679 u = time_in_microseconds();
Andy Greend636e352013-01-29 12:36:17 +08001680
1681 if (action) {
1682 if (completed) {
1683 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001684 sprintf(buf,
1685 "Completion first try lat %luus: %p: ret %d: %s\n",
1686 u - wsi->latency_start,
1687 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001688 else
Andy Greenb5b23192013-02-11 17:13:32 +08001689 sprintf(buf,
1690 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1691 u - wsi->action_start,
1692 u - wsi->latency_start,
1693 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001694 wsi->action_start = 0;
1695 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001696 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1697 u - wsi->latency_start,
1698 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001699 if (u - wsi->latency_start > context->worst_latency) {
1700 context->worst_latency = u - wsi->latency_start;
1701 strcpy(context->worst_latency_info, buf);
1702 }
1703 lwsl_latency("%s", buf);
1704 } else {
1705 wsi->latency_start = u;
1706 if (!wsi->action_start)
1707 wsi->action_start = u;
1708 }
1709}
1710#endif
1711
Andy Greena1ce6be2013-01-18 11:43:21 +08001712#ifdef LWS_NO_SERVER
1713int
Andy Green8d15cf42013-10-24 21:47:06 +08001714_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001715{
1716 return 0;
1717}
1718#else
Andy Green706961d2013-01-17 16:50:35 +08001719int
1720_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1721{
1722 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001723
Andy Greenca0a1292013-03-16 11:24:23 +08001724 /* there is no pending change */
1725 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001726 return 0;
1727
Andy Greenca0a1292013-03-16 11:24:23 +08001728 /* stuff is still buffered, not ready to really accept new input */
1729 if (wsi->u.ws.rxflow_buffer) {
1730 /* get ourselves called back to deal with stashed buffer */
1731 libwebsocket_callback_on_writable(context, wsi);
1732 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001733 }
1734
Andy Greenca0a1292013-03-16 11:24:23 +08001735 /* pending is cleared, we can change rxflow state */
1736
1737 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1738
1739 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1740 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1741
1742 /* adjust the pollfd for this wsi */
1743
1744 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001745 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001746 else
Andy Green91f19d82013-12-21 11:18:34 +08001747 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001748
Andy Green706961d2013-01-17 16:50:35 +08001749 return 1;
1750}
Andy Greena1ce6be2013-01-18 11:43:21 +08001751#endif
Andy Green706961d2013-01-17 16:50:35 +08001752
Andy Green90c7cbc2011-01-27 06:26:52 +00001753/**
1754 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1755 * receieved packets.
1756 *
1757 * If the output side of a server process becomes choked, this allows flow
1758 * control for the input side.
1759 *
1760 * @wsi: Websocket connection instance to get callback for
1761 * @enable: 0 = disable read servicing for this connection, 1 = enable
1762 */
1763
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001764LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001765libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1766{
Andy Greenca0a1292013-03-16 11:24:23 +08001767 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1768 return 0;
1769
1770 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1771 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001772
Andy Green706961d2013-01-17 16:50:35 +08001773 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001774}
1775
Andy Greenb55451c2013-03-16 12:32:27 +08001776/**
1777 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1778 *
1779 * When the user server code realizes it can accept more input, it can
1780 * call this to have the RX flow restriction removed from all connections using
1781 * the given protocol.
1782 *
1783 * @protocol: all connections using this protocol will be allowed to receive
1784 */
1785
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001786LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001787libwebsocket_rx_flow_allow_all_protocol(
1788 const struct libwebsocket_protocols *protocol)
1789{
1790 struct libwebsocket_context *context = protocol->owning_server;
1791 int n;
1792 struct libwebsocket *wsi;
1793
1794 for (n = 0; n < context->fds_count; n++) {
1795 wsi = context->lws_lookup[context->fds[n].fd];
1796 if (!wsi)
1797 continue;
1798 if (wsi->protocol == protocol)
1799 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1800 }
1801}
1802
Andy Green706961d2013-01-17 16:50:35 +08001803
Andy Green2ac5a6f2011-01-28 10:00:18 +00001804/**
1805 * libwebsocket_canonical_hostname() - returns this host's hostname
1806 *
1807 * This is typically used by client code to fill in the host parameter
1808 * when making a client connection. You can only call it after the context
1809 * has been created.
1810 *
Peter Hinz56885f32011-03-02 22:03:47 +00001811 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001812 */
1813
1814
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001815LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001816libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001817{
Peter Hinz56885f32011-03-02 22:03:47 +00001818 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001819}
1820
1821
Andy Green90c7cbc2011-01-27 06:26:52 +00001822static void sigpipe_handler(int x)
1823{
1824}
1825
Andy Green6901cb32011-02-21 08:06:47 +00001826#ifdef LWS_OPENSSL_SUPPORT
1827static int
1828OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1829{
1830
1831 SSL *ssl;
1832 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001833 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001834
1835 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1836 SSL_get_ex_data_X509_STORE_CTX_idx());
1837
1838 /*
Andy Green2e24da02011-03-05 16:12:04 +00001839 * !!! nasty openssl requires the index to come as a library-scope
1840 * static
Andy Green6901cb32011-02-21 08:06:47 +00001841 */
Andy Green2e24da02011-03-05 16:12:04 +00001842 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001843
Peter Hinz56885f32011-03-02 22:03:47 +00001844 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001845 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1846 x509_ctx, ssl, preverify_ok);
1847
1848 /* convert return code from 0 = OK to 1 = OK */
1849
1850 if (!n)
1851 n = 1;
1852 else
1853 n = 0;
1854
1855 return n;
1856}
1857#endif
1858
Andy Green706961d2013-01-17 16:50:35 +08001859int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001860 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001861 struct libwebsocket *wsi,
1862 enum libwebsocket_callback_reasons reason, void *user,
1863 void *in, size_t len)
1864{
1865 int n;
1866
1867 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001868 if (!n)
1869 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001870
Andy Greenaedc9532013-02-10 21:21:24 +08001871 return n;
Andy Green706961d2013-01-17 16:50:35 +08001872}
1873
Andy Green3b3fa9e2013-12-25 16:34:37 +08001874/*
1875 * This is just used to interrupt poll waiting
1876 * we don't have to do anything with it.
1877 */
arnaudviala7c6f26c2014-02-15 14:39:40 +08001878#ifdef LWS_OPENSSL_SUPPORT
Andy Green3b3fa9e2013-12-25 16:34:37 +08001879static void lws_sigusr2(int sig)
1880{
1881}
arnaudviala7c6f26c2014-02-15 14:39:40 +08001882#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001883
Andy Greenab990e42010-10-31 12:42:52 +00001884/**
Andy Green4739e5c2011-01-22 12:51:57 +00001885 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001886 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001887 *
Andy Green1b265272013-02-09 14:01:09 +08001888 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001889 * of all initialization in one step.
1890 *
Andy Greene92cd172011-01-19 13:11:55 +00001891 * After initialization, it returns a struct libwebsocket_context * that
1892 * represents this server. After calling, user code needs to take care
1893 * of calling libwebsocket_service() with the context pointer to get the
1894 * server's sockets serviced. This can be done in the same process context
1895 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001896 *
Andy Green8f037e42010-12-19 22:13:26 +00001897 * The protocol callback functions are called for a handful of events
1898 * including http requests coming in, websocket connections becoming
1899 * established, and data arriving; it's also called periodically to allow
1900 * async transmission.
1901 *
1902 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1903 * at that time websocket protocol has not been negotiated. Other
1904 * protocols after the first one never see any HTTP callack activity.
1905 *
1906 * The server created is a simple http server by default; part of the
1907 * websocket standard is upgrading this http connection to a websocket one.
1908 *
1909 * This allows the same server to provide files like scripts and favicon /
1910 * images or whatever over http and dynamic data over websockets all in
1911 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001912 */
Andy Green4ea60062010-10-30 12:15:07 +01001913
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001914LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001915libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001916{
Peter Hinz56885f32011-03-02 22:03:47 +00001917 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001918 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001919 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001920#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001921 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001922 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001923 struct sockaddr_in serv_addr;
1924#endif
Andy Green3182ece2013-01-20 17:08:31 +08001925#ifndef LWS_NO_EXTENSIONS
1926 int m;
Andy Green1b265272013-02-09 14:01:09 +08001927 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001928#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001929
Andy Green3faa9c72010-11-08 17:03:03 +00001930#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001931 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001932#endif
1933
Joakim Soderberg4c531232013-02-06 15:26:58 +09001934#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001935 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001936#endif
1937
Andy Greenb3a614a2013-01-19 13:08:17 +08001938 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001939 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001940 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001941 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001942#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001943 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1944 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001945#else
1946 lwsl_notice(" Configured without extension support\n");
1947#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001948 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1949 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001950 if (info->ssl_cipher_list)
1951 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001952 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1953 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001954
Peter Hinz56885f32011-03-02 22:03:47 +00001955#ifdef _WIN32
1956 {
1957 WORD wVersionRequested;
1958 WSADATA wsaData;
1959 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001960 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001961
1962 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1963 wVersionRequested = MAKEWORD(2, 2);
1964
1965 err = WSAStartup(wVersionRequested, &wsaData);
1966 if (err != 0) {
1967 /* Tell the user that we could not find a usable */
1968 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001969 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001970 return NULL;
1971 }
David Galeano7b11fec2011-10-04 19:55:18 +08001972
Andy Green6ee372f2012-04-09 15:09:01 +08001973 /* default to a poll() made out of select() */
1974 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001975
Andy Green6ee372f2012-04-09 15:09:01 +08001976 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001977 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001978 if (wsdll)
1979 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001980
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001981 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001982 if (!poll)
1983 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001984 }
1985#endif
1986
Andy Greenb5b23192013-02-11 17:13:32 +08001987 context = (struct libwebsocket_context *)
1988 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001989 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001990 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001991 return NULL;
1992 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001993 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001994#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001995 context->started_with_parent = pid_daemon;
1996 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1997#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001998
Joakim Soderberg4c531232013-02-06 15:26:58 +09001999 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08002000 context->protocols = info->protocols;
2001 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00002002 context->http_proxy_port = 0;
2003 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08002004 context->options = info->options;
Mattias Lundberg03bb8f92014-02-18 10:06:57 +01002005 context->iface = info->iface;
Andy Greendfb23042013-01-17 12:26:48 +08002006 /* to reduce this allocation, */
2007 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08002008 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
2009 sizeof(struct libwebsocket_context),
2010 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
2011 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08002012 sizeof(struct libwebsocket_context) +
2013 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
2014 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08002015
Andy Greenb5b23192013-02-11 17:13:32 +08002016 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
2017 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002018 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002019 lwsl_err("Unable to allocate fds array for %d connections\n",
2020 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002021 free(context);
2022 return NULL;
2023 }
Andy Greenb5b23192013-02-11 17:13:32 +08002024 context->lws_lookup = (struct libwebsocket **)
2025 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002026 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002027 lwsl_err(
2028 "Unable to allocate lws_lookup array for %d connections\n",
2029 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002030 free(context->fds);
2031 free(context);
2032 return NULL;
2033 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08002034 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
2035 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08002036
Peter Hinz56885f32011-03-02 22:03:47 +00002037 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002038#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08002039 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08002040#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002041 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08002042 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00002043
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002044#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002045 context->fd_random = 0;
2046#else
2047 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2048 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002049 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002050 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002051 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00002052 }
Peter Hinz56885f32011-03-02 22:03:47 +00002053#endif
Andy Green44eee682011-02-10 09:32:24 +00002054
Peter Hinz56885f32011-03-02 22:03:47 +00002055#ifdef LWS_OPENSSL_SUPPORT
2056 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08002057 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002058 context->ssl_ctx = NULL;
2059 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002060 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002061#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002062
Andy Greena1ce6be2013-01-18 11:43:21 +08002063 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002064
Andy Greena1ce6be2013-01-18 11:43:21 +08002065#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002066 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01002067 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08002068 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08002069 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01002070
Andy Greenb5b23192013-02-11 17:13:32 +08002071 lwsl_notice(" canonical_hostname = %s\n",
2072 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002073 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002074#endif
Andy Greena69f0512012-05-03 12:32:38 +08002075
Andy Green9659f372011-01-27 22:01:43 +00002076 /* split the proxy ads:port if given */
2077
2078 p = getenv("http_proxy");
2079 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00002080 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08002081 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002082 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002083 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002084
Peter Hinz56885f32011-03-02 22:03:47 +00002085 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00002086 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002087 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002088 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00002089 }
2090 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00002091 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002092
Andy Greenb3a614a2013-01-19 13:08:17 +08002093 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002094 context->http_proxy_address,
2095 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002096 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002097
Andy Greena1ce6be2013-01-18 11:43:21 +08002098#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002099 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002100
Andy Green3faa9c72010-11-08 17:03:03 +00002101#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002102 context->use_ssl = info->ssl_cert_filepath != NULL &&
2103 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002104#ifdef USE_CYASSL
2105 lwsl_notice(" Compiled with CYASSL support\n");
2106#else
2107 lwsl_notice(" Compiled with OpenSSL support\n");
2108#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002109 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002110 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002111 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002112 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002113
Andy Green90c7cbc2011-01-27 06:26:52 +00002114#else
Andy Green2b40b792013-02-10 22:22:01 +08002115 if (info->ssl_cert_filepath != NULL &&
2116 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002117 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002118 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002119 }
Andy Greenb5b23192013-02-11 17:13:32 +08002120 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002121#endif
Andy Greena17c6922013-01-20 20:21:54 +08002122
Andy Greenb5b23192013-02-11 17:13:32 +08002123 lwsl_notice(
2124 " per-conn mem: %u + %u headers + protocol rx buf\n",
2125 sizeof(struct libwebsocket),
2126 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002127 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002128#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002129
2130 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002131#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002132#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002133 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002134#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002135
2136
2137#ifdef LWS_OPENSSL_SUPPORT
2138
2139 /* basic openssl init */
2140
2141 SSL_library_init();
2142
2143 OpenSSL_add_all_algorithms();
2144 SSL_load_error_strings();
2145
Andy Green2e24da02011-03-05 16:12:04 +00002146 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002147 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2148
Andy Green90c7cbc2011-01-27 06:26:52 +00002149 /*
2150 * Firefox insists on SSLv23 not SSLv3
2151 * Konq disables SSLv2 by default now, SSLv23 works
2152 */
2153
2154 method = (SSL_METHOD *)SSLv23_server_method();
2155 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002156 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002157 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002158 error,
2159 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002160 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002161 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002162 }
Peter Hinz56885f32011-03-02 22:03:47 +00002163 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2164 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002165 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002166 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002167 error,
2168 ERR_error_string(error,
2169 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002170 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002171 }
2172
martell19c73f32014-01-22 18:17:03 +00002173#if defined(WIN32) || defined(_WIN32)
2174#else
Andy Green3b3fa9e2013-12-25 16:34:37 +08002175 signal(SIGUSR2, lws_sigusr2);
Andy Greened451d52014-01-11 12:37:07 +08002176 {
2177 sigset_t mask;
2178 sigemptyset (&mask);
2179 sigaddset (&mask, SIGUSR2);
2180
2181 sigprocmask(SIG_BLOCK, &mask, NULL);
2182 }
martell19c73f32014-01-22 18:17:03 +00002183#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002184
David Galeanocc148e42013-01-10 10:18:59 +08002185#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002186 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002187#endif
David Galeano77a677c2013-01-10 10:14:12 +08002188 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002189 if (info->ssl_cipher_list)
2190 SSL_CTX_set_cipher_list(context->ssl_ctx,
2191 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002192
Andy Greena1ce6be2013-01-18 11:43:21 +08002193#ifndef LWS_NO_CLIENT
2194
Andy Green90c7cbc2011-01-27 06:26:52 +00002195 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002196
Andy Green1b265272013-02-09 14:01:09 +08002197 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002198 method = (SSL_METHOD *)SSLv23_client_method();
2199 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002200 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002201 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002202 error,
2203 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002204 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002205 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002206 }
2207 /* create context */
2208 context->ssl_client_ctx = SSL_CTX_new(method);
2209 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002210 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002211 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002212 error,
2213 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002214 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002215 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002216 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002217
David Galeanocc148e42013-01-10 10:18:59 +08002218#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002219 SSL_CTX_set_options(context->ssl_client_ctx,
2220 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002221#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002222 SSL_CTX_set_options(context->ssl_client_ctx,
2223 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002224 if (info->ssl_cipher_list)
2225 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2226 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002227
Peter Hinz56885f32011-03-02 22:03:47 +00002228 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002229 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002230 if (!SSL_CTX_load_verify_locations(
2231 context->ssl_client_ctx, NULL,
2232 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002233 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002234 "Unable to load SSL Client certs from %s "
2235 "(set by --with-client-cert-dir= "
2236 "in configure) -- client ssl isn't "
2237 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002238 } else
2239 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002240 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002241 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002242 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002243 "Unable to load SSL Client certs "
2244 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002245 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002246
2247 /*
2248 * callback allowing user code to load extra verification certs
2249 * helping the client to verify server identity
2250 */
2251
prasannateamf1b353a452013-11-12 17:21:01 -08002252 /* support for client-side certificate authentication */
2253 if (info->ssl_cert_filepath) {
2254 n = SSL_CTX_use_certificate_chain_file(
2255 context->ssl_client_ctx,
2256 info->ssl_cert_filepath);
2257 if (n != 1) {
2258 lwsl_err("problem getting cert '%s' %lu: %s\n",
2259 info->ssl_cert_filepath,
2260 ERR_get_error(),
2261 ERR_error_string(ERR_get_error(),
2262 (char *)context->service_buffer));
2263 goto bail;
2264 }
2265 }
2266 if (info->ssl_private_key_filepath) {
2267 /* set the private key from KeyFile */
2268 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2269 info->ssl_private_key_filepath,
2270 SSL_FILETYPE_PEM) != 1) {
2271 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2272 info->ssl_private_key_filepath,
2273 ERR_get_error(),
2274 ERR_error_string(ERR_get_error(),
2275 (char *)context->service_buffer));
2276 goto bail;
2277 }
2278
2279 /* verify private key */
2280 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2281 lwsl_err("Private SSL key doesn't match cert\n");
2282 goto bail;
2283 }
2284 }
2285
Peter Hinz56885f32011-03-02 22:03:47 +00002286 context->protocols[0].callback(context, NULL,
2287 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2288 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002289 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002290#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002291
Andy Greenc6bf2c22011-02-20 11:10:47 +00002292 /* as a server, are we requiring clients to identify themselves? */
2293
Andy Greenb5b23192013-02-11 17:13:32 +08002294 if (info->options &
2295 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002296
2297 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002298
Peter Hinz56885f32011-03-02 22:03:47 +00002299 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002300 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2301 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002302
2303 /*
2304 * give user code a chance to load certs into the server
2305 * allowing it to verify incoming client certs
2306 */
2307
Peter Hinz56885f32011-03-02 22:03:47 +00002308 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002309 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002310 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002311 }
2312
James Devine5b34c972013-12-14 11:41:29 +08002313 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2314 /* Normally SSL listener rejects non-ssl, optionally allow */
2315 context->allow_non_ssl_on_ssl_port = 1;
2316 }
2317
Peter Hinz56885f32011-03-02 22:03:47 +00002318 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002319
2320 /* openssl init for server sockets */
2321
Andy Green3faa9c72010-11-08 17:03:03 +00002322 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002323 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002324 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002325 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002326 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002327 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002328 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002329 error,
2330 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002331 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002332 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002333 }
2334 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002335 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002336 info->ssl_private_key_filepath,
2337 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002338 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002339 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002340 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002341 error,
2342 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002343 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002344 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002345 }
2346 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002347 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002348 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002349 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002350 }
2351
2352 /* SSL is happy and has a cert it's content with */
2353 }
2354#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002355
Andy Greena1ce6be2013-01-18 11:43:21 +08002356#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002357 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002358
Andy Green1b265272013-02-09 14:01:09 +08002359 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002360 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002361
Andy Green4739e5c2011-01-22 12:51:57 +00002362 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2363 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002364 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002365 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002366 }
Andy Green775c0dd2010-10-29 14:15:22 +01002367
Andy Greenb5b23192013-02-11 17:13:32 +08002368 /*
2369 * allow us to restart even if old sockets in TIME_WAIT
Andy Greenb5b23192013-02-11 17:13:32 +08002370 */
Andy Green6ee372f2012-04-09 15:09:01 +08002371 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2372 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002373
2374 /* Disable Nagle */
2375 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002376 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2377 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002378
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002379 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002380 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002381 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002382 #else
Andy Greene2160712013-01-28 12:19:10 +08002383 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002384 #endif
Andy Greene2160712013-01-28 12:19:10 +08002385
Andy Green4739e5c2011-01-22 12:51:57 +00002386 bzero((char *) &serv_addr, sizeof(serv_addr));
2387 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002388 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002389 serv_addr.sin_addr.s_addr = INADDR_ANY;
2390 else
Andy Greend1eac602013-11-09 08:07:38 +08002391 if (interface_to_sa(info->iface, &serv_addr,
2392 sizeof(serv_addr)) < 0) {
2393 lwsl_err("Unable to find interface %s\n",
2394 info->iface);
2395 compatible_close(sockfd);
2396 goto bail;
2397 }
Andy Green1b265272013-02-09 14:01:09 +08002398 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002399
2400 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2401 sizeof(serv_addr));
2402 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002403 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002404 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002405 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002406 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002407 }
Andy Green0d338332011-02-12 11:57:43 +00002408
Andy Greenb5b23192013-02-11 17:13:32 +08002409 wsi = (struct libwebsocket *)malloc(
2410 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002411 if (wsi == NULL) {
2412 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002413 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002414 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002415 }
Andy Greenb5b23192013-02-11 17:13:32 +08002416 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002417 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002418#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002419 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002420#endif
Andy Green0d338332011-02-12 11:57:43 +00002421 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002422
2423 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002424
Andy Green65b0e912013-01-16 07:59:47 +08002425 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2426 context->listen_service_count = 0;
2427 context->listen_service_fd = sockfd;
2428
Andy Greena824d182013-01-15 20:52:29 +08002429 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002430 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002431 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002432#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002433
Andy Green6ee372f2012-04-09 15:09:01 +08002434 /*
2435 * drop any root privs for this process
2436 * to listen on port < 1023 we would have needed root, but now we are
2437 * listening, we don't want the power for anything else
2438 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002439#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002440#else
Andy Green1b265272013-02-09 14:01:09 +08002441 if (info->gid != -1)
2442 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002443 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002444 if (info->uid != -1)
2445 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002446 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002447#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002448
Andy Green6f520a52013-01-29 17:57:39 +08002449 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002450
Peter Hinz56885f32011-03-02 22:03:47 +00002451 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002452 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002453 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002454
Andy Green43db0452013-01-10 19:50:35 +08002455 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002456 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002457
Andy Green1b265272013-02-09 14:01:09 +08002458 info->protocols[context->count_protocols].owning_server =
2459 context;
2460 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002461 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002462
2463 /*
2464 * inform all the protocols that they are doing their one-time
2465 * initialization if they want to
2466 */
2467 info->protocols[context->count_protocols].callback(context,
2468 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002469 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002470
Andy Green3182ece2013-01-20 17:08:31 +08002471#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002472 /*
2473 * give all extensions a chance to create any per-context
2474 * allocations they need
2475 */
2476
2477 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002478 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002479 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002480
Andy Green1b265272013-02-09 14:01:09 +08002481 if (info->extensions) {
2482 ext = info->extensions;
2483 while (ext->callback) {
2484 lwsl_ext(" Extension: %s\n", ext->name);
2485 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002486 (enum libwebsocket_extension_callback_reasons)m,
2487 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002488 ext++;
2489 }
Andy Greena41314f2011-05-23 10:00:03 +01002490 }
Andy Green3182ece2013-01-20 17:08:31 +08002491#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002492
Peter Hinz56885f32011-03-02 22:03:47 +00002493 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002494
2495bail:
2496 libwebsocket_context_destroy(context);
2497 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002498}
Andy Greenb45993c2010-12-18 15:13:50 +00002499
Andy Greenb45993c2010-12-18 15:13:50 +00002500/**
shysb4e800e2013-10-24 22:12:03 +08002501 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2502 * @context: pointer to struct libwebsocket_context you want set proxy to
2503 * @proxy: pointer to c string containing proxy in format address:port
2504 *
2505 * Returns 0 if proxy string was parsed and proxy was setup.
2506 * Returns -1 if @proxy is NULL or has incorrect format.
2507 *
2508 * This is only required if your OS does not provide the http_proxy
2509 * enviroment variable (eg, OSX)
2510 *
2511 * IMPORTANT! You should call this function right after creation of the
2512 * libwebsocket_context and before call to connect. If you call this
2513 * function after connect behavior is undefined.
2514 * This function will override proxy settings made on libwebsocket_context
2515 * creation with genenv() call.
2516 */
2517
2518LWS_VISIBLE int
2519libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2520{
2521 char *p;
2522
2523 if (!proxy)
2524 return -1;
2525
2526 strncpy(context->http_proxy_address, proxy,
2527 sizeof(context->http_proxy_address) - 1);
2528 context->http_proxy_address[
2529 sizeof(context->http_proxy_address) - 1] = '\0';
2530
2531 p = strchr(context->http_proxy_address, ':');
2532 if (!p) {
2533 lwsl_err("http_proxy needs to be ads:port\n");
2534
2535 return -1;
2536 }
2537 *p = '\0';
2538 context->http_proxy_port = atoi(p + 1);
2539
2540 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2541 context->http_proxy_port);
2542
2543 return 0;
2544}
2545
2546/**
Andy Greenb45993c2010-12-18 15:13:50 +00002547 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002548 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002549 * @wsi: pointer to struct websocket you want to know the protocol of
2550 *
Andy Green8f037e42010-12-19 22:13:26 +00002551 *
Andy Green6f520a52013-01-29 17:57:39 +08002552 * Some apis can act on all live connections of a given protocol,
2553 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002554 */
Andy Greenab990e42010-10-31 12:42:52 +00002555
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002556LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002557libwebsockets_get_protocol(struct libwebsocket *wsi)
2558{
2559 return wsi->protocol;
2560}
2561
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002562LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002563libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2564{
Andy Green623a98d2013-01-21 11:04:23 +08002565 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002566}
Alex Bligh49146db2011-11-07 17:19:25 +08002567
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002568LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002569libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2570{
Andy Green623a98d2013-01-21 11:04:23 +08002571 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002572}
2573
Andy Green2af4d5b2013-02-18 16:30:10 +08002574int
Alex Bligh49146db2011-11-07 17:19:25 +08002575libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2576{
Andy Green67d556c2013-02-15 22:31:55 +08002577 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002578 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002579
Alex Bligh49146db2011-11-07 17:19:25 +08002580 /* allocate the per-connection user memory (if any) */
2581
2582 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2583 wsi->user_space = malloc(
2584 wsi->protocol->per_session_data_size);
2585 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002586 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002587 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002588 }
Andy Green6ee372f2012-04-09 15:09:01 +08002589 memset(wsi->user_space, 0,
2590 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002591 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002592 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002593}
Andy Green43db0452013-01-10 19:50:35 +08002594
Andy Green0b319092013-01-19 11:17:56 +08002595static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002596{
Andy Green0b319092013-01-19 11:17:56 +08002597 char buf[300];
Patrick Gansterer92792b42014-02-26 21:37:31 +01002598 unsigned long long now;
Andy Green0b319092013-01-19 11:17:56 +08002599 int n;
2600
Andy Green0b319092013-01-19 11:17:56 +08002601 buf[0] = '\0';
2602 for (n = 0; n < LLL_COUNT; n++)
2603 if (level == (1 << n)) {
Patrick Gansterer92792b42014-02-26 21:37:31 +01002604 now = time_in_microseconds() / 100;
2605 sprintf(buf, "[%lu:%04d] %s: ", (unsigned long) now / 10000,
2606 (int)(now % 10000), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002607 break;
2608 }
Andy Greenb5b23192013-02-11 17:13:32 +08002609
Andy Green0b319092013-01-19 11:17:56 +08002610 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002611}
2612
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002613#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002614LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002615{
2616 lwsl_emit_stderr(level, line);
2617}
2618#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002619LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002620{
2621 int syslog_level = LOG_DEBUG;
2622
2623 switch (level) {
2624 case LLL_ERR:
2625 syslog_level = LOG_ERR;
2626 break;
2627 case LLL_WARN:
2628 syslog_level = LOG_WARNING;
2629 break;
2630 case LLL_NOTICE:
2631 syslog_level = LOG_NOTICE;
2632 break;
2633 case LLL_INFO:
2634 syslog_level = LOG_INFO;
2635 break;
2636 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002637 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002638}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002639#endif
Andy Greenc11db202013-01-19 11:12:16 +08002640
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002641LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002642{
Andy Greende8f27a2013-01-12 09:17:42 +08002643 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002644 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002645
2646 if (!(log_level & filter))
2647 return;
2648
Andy Green43db0452013-01-10 19:50:35 +08002649 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002650 vsnprintf(buf, sizeof(buf), format, ap);
2651 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002652 va_end(ap);
2653
Andy Green0b319092013-01-19 11:17:56 +08002654 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002655}
2656
2657/**
2658 * lws_set_log_level() - Set the logging bitfield
2659 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002660 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2661 * function to perform log string emission instead of
2662 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002663 *
Andy Greenc6511a02013-02-19 10:01:48 +08002664 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002665 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002666 */
2667
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002668LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002669 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002670{
2671 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002672 if (log_emit_function)
2673 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002674}
Andy Green92f9b162014-02-21 18:43:42 +08002675
2676int
2677interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
2678{
2679 int rc = -1;
2680#if defined(WIN32) || defined(_WIN32)
2681 /* TODO */
2682#else
2683 struct ifaddrs *ifr;
2684 struct ifaddrs *ifc;
2685 struct sockaddr_in *sin;
2686
2687 getifaddrs(&ifr);
2688 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
2689 if (ifc->ifa_addr == NULL)
2690 continue;
2691 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
2692 if (strcmp(ifc->ifa_name, ifname))
2693 continue;
2694 sin = (struct sockaddr_in *)ifc->ifa_addr;
2695 if (sin->sin_family != AF_INET)
2696 continue;
2697 memcpy(addr, sin, addrlen);
2698 rc = 0;
2699 }
2700
2701 freeifaddrs(ifr);
2702#endif
2703 return rc;
2704}
2705