blob: 8ce607413114c4e90a2b8021d4f21ee4e189ab36 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010024#if defined(WIN32) || defined(_WIN32)
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010026#include <mstcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000027#else
Davidc4ef7b12013-01-12 20:39:47 +080028#ifdef LWS_BUILTIN_GETIFADDRS
29#include <getifaddrs.h>
30#else
Peter Hinz56885f32011-03-02 22:03:47 +000031#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080032#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090033#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000034#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080035#include <sys/socket.h>
36#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000037#endif
Andy Green2e24da02011-03-05 16:12:04 +000038
Patrick Gansterere5720a32014-02-28 00:57:19 +010039#ifdef HAVE_SYS_TYPES_H
Andy Green3b3fa9e2013-12-25 16:34:37 +080040#include <sys/types.h>
Patrick Gansterere5720a32014-02-28 00:57:19 +010041#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +080042
Andy Green2e24da02011-03-05 16:12:04 +000043#ifdef LWS_OPENSSL_SUPPORT
44int openssl_websocket_private_data_index;
45#endif
46
Andy Greenaa6fc442012-04-12 13:26:49 +080047#ifdef __MINGW32__
48#include "../win32port/win32helpers/websock-w32.c"
49#else
50#ifdef __MINGW64__
51#include "../win32port/win32helpers/websock-w32.c"
52#endif
53#endif
54
Andy Green7b405452013-02-01 10:50:15 +080055#ifndef LWS_BUILD_HASH
56#define LWS_BUILD_HASH "unknown-build-hash"
57#endif
Andy Green3182ece2013-01-20 17:08:31 +080058
Andy Green7c19c342013-01-19 12:18:07 +080059static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080060static void lwsl_emit_stderr(int level, const char *line);
61static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
62
Andy Green7b405452013-02-01 10:50:15 +080063static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
64
Andy Greenb5b23192013-02-11 17:13:32 +080065static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080066 "ERR",
67 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080068 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080069 "INFO",
70 "DEBUG",
71 "PARSER",
72 "HEADER",
73 "EXTENSION",
74 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080075 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080076};
77
Andy Greenb5b23192013-02-11 17:13:32 +080078#ifndef LWS_NO_CLIENT
79 extern int lws_client_socket_service(
80 struct libwebsocket_context *context,
81 struct libwebsocket *wsi, struct pollfd *pollfd);
82#endif
83#ifndef LWS_NO_SERVER
84 extern int lws_server_socket_service(
85 struct libwebsocket_context *context,
86 struct libwebsocket *wsi, struct pollfd *pollfd);
87#endif
88
Andy Green3b3fa9e2013-12-25 16:34:37 +080089
90#ifdef LWS_HAS_PPOLL
91/*
92 * set to the Thread ID that's doing the service loop just before entry to ppoll
93 * indicates service thread likely idling in ppoll()
94 * volatile because other threads may check it as part of processing for pollfd
95 * event change.
96 */
97static volatile int lws_idling_ppoll_tid;
98#endif
99
Andy Green7b405452013-02-01 10:50:15 +0800100/**
101 * lws_get_library_version: get version and git hash library built from
102 *
103 * returns a const char * to a string like "1.1 178d78c"
104 * representing the library version followed by the git head hash it
105 * was built from
106 */
107
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800108LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +0800109lws_get_library_version(void)
110{
111 return library_version;
112}
113
Patrick Gansterer92792b42014-02-26 21:37:31 +0100114static unsigned long long time_in_microseconds()
115{
116#if defined(WIN32) || defined(_WIN32)
117#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
118 FILETIME filetime;
119 ULARGE_INTEGER datetime;
120
121#ifdef _WIN32_WCE
122 GetCurrentFT(&filetime);
123#else
124 GetSystemTimeAsFileTime(&filetime);
125#endif
126
127 /*
128 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
129 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
130 * prevent alignment faults on 64-bit Windows).
131 */
132 memcpy(&datetime, &filetime, sizeof(datetime));
133
134 /* Windows file times are in 100s of nanoseconds. */
135 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
136#else
137 struct timeval tv;
138 gettimeofday(&tv, NULL);
139 return (tv.tv_sec * 1000000) + tv.tv_usec;
140#endif
141}
142
Andy Green0d338332011-02-12 11:57:43 +0000143int
Andy Greenb5b23192013-02-11 17:13:32 +0800144insert_wsi_socket_into_fds(struct libwebsocket_context *context,
145 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000146{
Joakim Soderberg6596e4b2014-02-18 13:38:14 +0100147 struct libwebsocket_pollargs pa = { wsi->sock, POLLIN, 0 };
148
Andy Greendfb23042013-01-17 12:26:48 +0800149 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800150 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000151 return 1;
152 }
153
nononamef1624922014-01-11 13:12:34 +0800154 if (wsi->sock >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800155 lwsl_err("Socket fd %d is too high (%d)\n",
156 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800157 return 1;
158 }
159
160 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800161 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800162
Andy Greenb5b23192013-02-11 17:13:32 +0800163 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
164 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800165
Andy Green7a132792013-12-18 09:48:26 +0800166 context->protocols[0].callback(context, wsi,
167 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800168 wsi->user_space, (void *) &pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800169
Andy Greendfb23042013-01-17 12:26:48 +0800170 context->lws_lookup[wsi->sock] = wsi;
171 wsi->position_in_fds_table = context->fds_count;
172 context->fds[context->fds_count].fd = wsi->sock;
173 context->fds[context->fds_count].events = POLLIN;
174 context->fds[context->fds_count++].revents = 0;
175
176 /* external POLL support via protocol 0 */
177 context->protocols[0].callback(context, wsi,
178 LWS_CALLBACK_ADD_POLL_FD,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800179 wsi->user_space, (void *) &pa, 0);
Andy Green0d338332011-02-12 11:57:43 +0000180
Andy Green7a132792013-12-18 09:48:26 +0800181 context->protocols[0].callback(context, wsi,
182 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800183 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800184
Andy Green0d338332011-02-12 11:57:43 +0000185 return 0;
186}
187
Andy Greendfb23042013-01-17 12:26:48 +0800188static int
Andy Greenb5b23192013-02-11 17:13:32 +0800189remove_wsi_socket_from_fds(struct libwebsocket_context *context,
190 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000191{
Andy Greendfb23042013-01-17 12:26:48 +0800192 int m;
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800193 struct libwebsocket_pollargs pa = { wsi->sock, 0, 0};
Andy Green0d338332011-02-12 11:57:43 +0000194
Andy Green7a132792013-12-18 09:48:26 +0800195 if (!--context->fds_count) {
196 context->protocols[0].callback(context, wsi,
197 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800198 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800199 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800200 }
Andy Green0d338332011-02-12 11:57:43 +0000201
Andy Greendfb23042013-01-17 12:26:48 +0800202 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800203 lwsl_err("Socket fd %d too high (%d)\n",
204 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800205 return 1;
206 }
Andy Green0d338332011-02-12 11:57:43 +0000207
Andy Greenb5b23192013-02-11 17:13:32 +0800208 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
209 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800210
Andy Green7a132792013-12-18 09:48:26 +0800211 context->protocols[0].callback(context, wsi,
212 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800213 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800214
Andy Greendfb23042013-01-17 12:26:48 +0800215 m = wsi->position_in_fds_table; /* replace the contents for this */
216
217 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800218 context->fds[m] = context->fds[context->fds_count];
219 /*
220 * end guy's fds_lookup entry remains unchanged
221 * (still same fd pointing to same wsi)
222 */
Andy Greendfb23042013-01-17 12:26:48 +0800223 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800224 context->lws_lookup[context->fds[context->fds_count].fd]->
225 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800226 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800227 context->lws_lookup[wsi->sock] = NULL;
228 /* removed wsi has no position any more */
229 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800230
231do_ext:
232 /* remove also from external POLL support via protocol 0 */
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800233 if (wsi->sock) {
Andy Greendfb23042013-01-17 12:26:48 +0800234 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800235 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800236 (void *) &pa, 0);
237 }
Andy Green7a132792013-12-18 09:48:26 +0800238 context->protocols[0].callback(context, wsi,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800239 LWS_CALLBACK_UNLOCK_POLL,
240 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800241 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000242}
243
Andy Green32375b72011-02-19 08:32:53 +0000244
Andy Green8f037e42010-12-19 22:13:26 +0000245void
Peter Hinz56885f32011-03-02 22:03:47 +0000246libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000247 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000248{
Andy Greenb45993c2010-12-18 15:13:50 +0000249 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000250 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000251 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
252 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800253#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000254 int ret;
255 int m;
256 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100257 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800258#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000259
Andy Green4b6fbe12011-02-14 08:03:48 +0000260 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000261 return;
262
Andy Green62c54d22011-02-14 09:14:25 +0000263 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000264
Andy Green62c54d22011-02-14 09:14:25 +0000265 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000266 return;
267
Andy Green22524a62013-02-15 22:48:58 +0800268 /* we tried the polite way... */
269 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
270 goto just_kill_connection;
271
Andy Green623a98d2013-01-21 11:04:23 +0800272 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000273
Andy Green5dc62ea2013-09-20 20:26:12 +0800274 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
275 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
276
277 context->protocols[0].callback(context, wsi,
278 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
279
280 free(wsi->u.hdr.ah);
281 goto just_kill_connection;
282 }
283
284
kapejodce64fb02013-11-19 13:38:16 +0100285 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
286 if (wsi->u.http.post_buffer) {
287 free(wsi->u.http.post_buffer);
288 wsi->u.http.post_buffer = NULL;
289 }
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100290 if (wsi->u.http.fd != LWS_INVALID_FILE) {
291 lwsl_debug("closing http file\n");
292#if defined(WIN32) || defined(_WIN32)
293 CloseHandle(wsi->u.http.fd);
294#else
kapejodce64fb02013-11-19 13:38:16 +0100295 close(wsi->u.http.fd);
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100296#endif
297 wsi->u.http.fd = LWS_INVALID_FILE;
kapejodce64fb02013-11-19 13:38:16 +0100298 context->protocols[0].callback(context, wsi,
299 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
300 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800301 }
302
Andy Green3182ece2013-01-20 17:08:31 +0800303#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000304 /*
Andy Green68b45042011-05-25 21:41:57 +0100305 * are his extensions okay with him closing? Eg he might be a mux
306 * parent and just his ch1 aspect is closing?
307 */
308
Andy Green68b45042011-05-25 21:41:57 +0100309 for (n = 0; n < wsi->count_active_extensions; n++) {
310 if (!wsi->active_extensions[n]->callback)
311 continue;
312
313 m = wsi->active_extensions[n]->callback(context,
314 wsi->active_extensions[n], wsi,
315 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
316 wsi->active_extensions_user[n], NULL, 0);
317
318 /*
319 * if somebody vetoed actually closing him at this time....
320 * up to the extension to track the attempted close, let's
321 * just bail
322 */
323
324 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800325 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100326 return;
327 }
328 }
329
Andy Green68b45042011-05-25 21:41:57 +0100330 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000331 * flush any tx pending from extensions, since we may send close packet
332 * if there are problems with send, just nuke the connection
333 */
334
335 ret = 1;
336 while (ret == 1) {
337
338 /* default to nobody has more to spill */
339
340 ret = 0;
341 eff_buf.token = NULL;
342 eff_buf.token_len = 0;
343
344 /* show every extension the new incoming data */
345
346 for (n = 0; n < wsi->count_active_extensions; n++) {
347 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000348 wsi->protocol->owning_server,
349 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000350 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
351 wsi->active_extensions_user[n], &eff_buf, 0);
352 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800353 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000354 goto just_kill_connection;
355 }
356 if (m)
357 /*
358 * at least one extension told us he has more
359 * to spill, so we will go around again after
360 */
361 ret = 1;
362 }
363
364 /* assuming they left us something to send, send it */
365
366 if (eff_buf.token_len)
367 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800368 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800369 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000370 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800371 }
Andy Greenc44159f2011-03-07 07:08:18 +0000372 }
Andy Green3182ece2013-01-20 17:08:31 +0800373#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000374
375 /*
Andy Greenda527df2011-03-07 07:08:12 +0000376 * signal we are closing, libsocket_write will
377 * add any necessary version-specific stuff. If the write fails,
378 * no worries we are closing anyway. If we didn't initiate this
379 * close, then our state has been changed to
380 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
381 *
382 * Likewise if it's a second call to close this connection after we
383 * sent the close indication to the peer already, we are in state
384 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
385 */
386
387 if (old_state == WSI_STATE_ESTABLISHED &&
388 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100389
Andy Green43db0452013-01-10 19:50:35 +0800390 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100391
Andy Greenfdd305a2013-02-11 14:32:48 +0800392 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800393 memset(buf, 0, sizeof(buf));
394 n = libwebsocket_write(wsi,
395 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000396 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800397 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000398 /*
399 * we have sent a nice protocol level indication we
400 * now wish to close, we should not send anything more
401 */
402
403 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
404
Andy Greenb5b23192013-02-11 17:13:32 +0800405 /*
406 * ...and we should wait for a reply for a bit
407 * out of politeness
408 */
Andy Greenda527df2011-03-07 07:08:12 +0000409
410 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800411 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000412
Andy Green43db0452013-01-10 19:50:35 +0800413 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000414
415 return;
416 }
417
Andy Greenb5b23192013-02-11 17:13:32 +0800418 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800419
Andy Greenda527df2011-03-07 07:08:12 +0000420 /* else, the send failed and we should just hang up */
421 }
422
Andy Greenc44159f2011-03-07 07:08:18 +0000423just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100424
Andy Greenb5b23192013-02-11 17:13:32 +0800425 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100426
Andy Greenda527df2011-03-07 07:08:12 +0000427 /*
428 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800429 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000430 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000431
Andy Greendfb23042013-01-17 12:26:48 +0800432 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000433
Andy Green251f6fa2010-11-03 11:13:06 +0000434 wsi->state = WSI_STATE_DEAD_SOCKET;
435
Andy Greenb5b23192013-02-11 17:13:32 +0800436 if ((old_state == WSI_STATE_ESTABLISHED ||
437 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800438 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
439
440 if (wsi->u.ws.rx_user_buffer) {
441 free(wsi->u.ws.rx_user_buffer);
442 wsi->u.ws.rx_user_buffer = NULL;
443 }
444 if (wsi->u.ws.rxflow_buffer) {
445 free(wsi->u.ws.rxflow_buffer);
446 wsi->u.ws.rxflow_buffer = NULL;
447 }
Andy Green2764eba2013-12-09 14:16:17 +0800448 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800449 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800450 free(wsi->truncated_send_malloc);
451 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800452 }
Andy Green54495112013-02-06 21:10:16 +0900453 }
454
Andy Green4b6fbe12011-02-14 08:03:48 +0000455 /* tell the user it's all over for this guy */
456
Andy Greend4302732011-02-28 07:45:29 +0000457 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800458 ((old_state == WSI_STATE_ESTABLISHED) ||
459 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
460 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800461 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000462 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000463 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800464 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
465 lwsl_debug("calling back CLOSED_HTTP\n");
466 context->protocols[0].callback(context, wsi,
467 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800468 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800469 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000470
Andy Green3182ece2013-01-20 17:08:31 +0800471#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000472 /* deallocate any active extension contexts */
473
474 for (n = 0; n < wsi->count_active_extensions; n++) {
475 if (!wsi->active_extensions[n]->callback)
476 continue;
477
Andy Green46c2ea02011-03-22 09:04:01 +0000478 wsi->active_extensions[n]->callback(context,
479 wsi->active_extensions[n], wsi,
480 LWS_EXT_CALLBACK_DESTROY,
481 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000482
483 free(wsi->active_extensions_user[n]);
484 }
485
Andy Greena41314f2011-05-23 10:00:03 +0100486 /*
487 * inform all extensions in case they tracked this guy out of band
488 * even though not active on him specifically
489 */
490
491 ext = context->extensions;
492 while (ext && ext->callback) {
493 ext->callback(context, ext, wsi,
494 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
495 NULL, NULL, 0);
496 ext++;
497 }
Andy Green3182ece2013-01-20 17:08:31 +0800498#endif
Andy Greena41314f2011-05-23 10:00:03 +0100499
Andy Green43db0452013-01-10 19:50:35 +0800500/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000501
Andy Green3faa9c72010-11-08 17:03:03 +0000502#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000503 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000504 n = SSL_get_fd(wsi->ssl);
505 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800506 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000507 SSL_free(wsi->ssl);
508 } else {
509#endif
Andy Green0303db42013-01-17 14:46:43 +0800510 if (wsi->sock) {
511 n = shutdown(wsi->sock, SHUT_RDWR);
512 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800513 lwsl_debug("closing: shutdown returned %d\n",
514 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800515
Andy Green0303db42013-01-17 14:46:43 +0800516 n = compatible_close(wsi->sock);
517 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800518 lwsl_debug("closing: close returned %d\n",
519 errno);
Andy Green0303db42013-01-17 14:46:43 +0800520 }
Andy Green3faa9c72010-11-08 17:03:03 +0000521#ifdef LWS_OPENSSL_SUPPORT
522 }
523#endif
Andy Green76b6ea12014-02-15 19:25:50 +0800524
525 /* outermost destroy notification for wsi (user_space still intact) */
526 context->protocols[0].callback(context, wsi,
527 LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0);
528
Andy Greenb5b23192013-02-11 17:13:32 +0800529 if (wsi->protocol && wsi->protocol->per_session_data_size &&
530 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000531 free(wsi->user_space);
532
Andy Green251f6fa2010-11-03 11:13:06 +0000533 free(wsi);
534}
535
Andy Green07034092011-02-13 08:37:12 +0000536/**
537 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800538 * @context: Libwebsockets context
539 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000540 * @fd: Connection socket descriptor
541 * @name: Buffer to take client address name
542 * @name_len: Length of client address name buffer
543 * @rip: Buffer to take client address IP qotted quad
544 * @rip_len: Length of client address IP buffer
545 *
546 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800547 * the client connected with socket descriptor @fd. Names may be
548 * truncated if there is not enough room. If either cannot be
549 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000550 */
551
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800552LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800553libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
554 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000555 char *rip, int rip_len)
556{
Bob Robertsac049112013-04-25 09:16:30 +0800557 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000558 struct sockaddr_in sin;
559 struct hostent *host;
560 struct hostent *host1;
561 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000562 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000563 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800564 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800565#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800566 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800567#endif
Andy Green07034092011-02-13 08:37:12 +0000568
569 rip[0] = '\0';
570 name[0] = '\0';
571
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800572 lws_latency_pre(context, wsi);
573
Andy Greenb5b23192013-02-11 17:13:32 +0800574 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000575 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100576 lwsl_warn("getpeername: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800577 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000578 }
Andy Green6ee372f2012-04-09 15:09:01 +0800579
Andy Greenb5b23192013-02-11 17:13:32 +0800580 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000581 AF_INET);
582 if (host == NULL) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100583 lwsl_warn("gethostbyaddr: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800584 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000585 }
586
587 strncpy(name, host->h_name, name_len);
588 name[name_len - 1] = '\0';
589
590 host1 = gethostbyname(host->h_name);
591 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800592 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000593 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000594 n = 0;
595 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000596 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000597 if (p == NULL)
598 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000599 if ((host1->h_addrtype != AF_INET)
600#ifdef AF_LOCAL
601 && (host1->h_addrtype != AF_LOCAL)
602#endif
603 )
Andy Green07034092011-02-13 08:37:12 +0000604 continue;
605
Andy Green7627af52011-03-09 15:13:52 +0000606 if (host1->h_addrtype == AF_INET)
607 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000608#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000609 else {
610 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800611 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000612 ip[sizeof(ip) - 1] = '\0';
613 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000614#endif
Andy Green07034092011-02-13 08:37:12 +0000615 p = NULL;
616 strncpy(rip, ip, rip_len);
617 rip[rip_len - 1] = '\0';
618 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800619
620 ret = 0;
621bail:
622 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000623}
Andy Green9f990342011-02-12 11:57:45 +0000624
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800625LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000626 void *buf, int len)
627{
628 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800629 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000630
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100631#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000632 for (n = 0; n < len; n++)
633 p[n] = (unsigned char)rand();
634#else
635 n = read(context->fd_random, p, len);
636#endif
637
638 return n;
639}
640
Andy Greena690cd02013-02-09 12:25:31 +0800641int lws_set_socket_options(struct libwebsocket_context *context, int fd)
642{
643 int optval = 1;
644 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100645#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800646 unsigned long optl = 0;
647#endif
648#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
649 struct protoent *tcp_proto;
650#endif
651
652 if (context->ka_time) {
653 /* enable keepalive on this socket */
654 optval = 1;
655 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
656 (const void *)&optval, optlen) < 0)
657 return 1;
658
Bob Robertsac049112013-04-25 09:16:30 +0800659#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800660
661 /*
662 * didn't find a way to set these per-socket, need to
663 * tune kernel systemwide values
664 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100665#elif WIN32
666 {
667 DWORD dwBytesRet;
668 struct tcp_keepalive alive;
669 alive.onoff = TRUE;
670 alive.keepalivetime = context->ka_time;
671 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800672
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100673 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
674 NULL, 0, &dwBytesRet, NULL, NULL))
675 return 1;
676 }
Andy Greena47865f2013-02-10 09:39:47 +0800677#else
Andy Greena690cd02013-02-09 12:25:31 +0800678 /* set the keepalive conditions we want on it too */
679 optval = context->ka_time;
680 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
681 (const void *)&optval, optlen) < 0)
682 return 1;
683
Larry Hayesbb66ac62013-02-22 09:16:20 +0800684 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800685 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
686 (const void *)&optval, optlen) < 0)
687 return 1;
688
Larry Hayesbb66ac62013-02-22 09:16:20 +0800689 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800690 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
691 (const void *)&optval, optlen) < 0)
692 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800693#endif
Andy Greena690cd02013-02-09 12:25:31 +0800694 }
695
696 /* Disable Nagle */
697 optval = 1;
698#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
699 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
700#else
701 tcp_proto = getprotobyname("TCP");
702 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
703#endif
704
705 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100706#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800707 ioctlsocket(fd, FIONBIO, &optl);
708#else
709 fcntl(fd, F_SETFL, O_NONBLOCK);
710#endif
711
712 return 0;
713}
714
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800715LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000716{
717 struct pollfd fds;
718
Andy Green2764eba2013-12-09 14:16:17 +0800719 /* treat the fact we got a truncated send pending as if we're choked */
720 if (wsi->truncated_send_malloc)
721 return 1;
722
Andy Green95a7b5d2011-03-06 10:29:39 +0000723 fds.fd = wsi->sock;
724 fds.events = POLLOUT;
725 fds.revents = 0;
726
727 if (poll(&fds, 1, 0) != 1)
728 return 1;
729
730 if ((fds.revents & POLLOUT) == 0)
731 return 1;
732
733 /* okay to send another packet without blocking */
734
735 return 0;
736}
737
Andy Greena41314f2011-05-23 10:00:03 +0100738int
Andy Green3b84c002011-03-06 13:14:42 +0000739lws_handle_POLLOUT_event(struct libwebsocket_context *context,
740 struct libwebsocket *wsi, struct pollfd *pollfd)
741{
Andy Green3b84c002011-03-06 13:14:42 +0000742 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800743
Andy Green3182ece2013-01-20 17:08:31 +0800744#ifndef LWS_NO_EXTENSIONS
745 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000746 int ret;
747 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100748 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000749
Andy Green1f4267b2013-10-17 08:09:19 +0800750 /* pending truncated sends have uber priority */
751
Andy Green2764eba2013-12-09 14:16:17 +0800752 if (wsi->truncated_send_malloc) {
753 lws_issue_raw(wsi, wsi->truncated_send_malloc +
754 wsi->truncated_send_offset,
755 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800756 /* leave POLLOUT active either way */
757 return 0;
758 }
759
Andy Greena41314f2011-05-23 10:00:03 +0100760 for (n = 0; n < wsi->count_active_extensions; n++) {
761 if (!wsi->active_extensions[n]->callback)
762 continue;
763
764 m = wsi->active_extensions[n]->callback(context,
765 wsi->active_extensions[n], wsi,
766 LWS_EXT_CALLBACK_IS_WRITEABLE,
767 wsi->active_extensions_user[n], NULL, 0);
768 if (m > handled)
769 handled = m;
770 }
771
772 if (handled == 1)
773 goto notify_action;
774
775 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000776 goto user_service;
777
778 /*
779 * check in on the active extensions, see if they
780 * had pending stuff to spill... they need to get the
781 * first look-in otherwise sequence will be disordered
782 *
783 * NULL, zero-length eff_buf means just spill pending
784 */
785
786 ret = 1;
787 while (ret == 1) {
788
789 /* default to nobody has more to spill */
790
791 ret = 0;
792 eff_buf.token = NULL;
793 eff_buf.token_len = 0;
794
795 /* give every extension a chance to spill */
796
797 for (n = 0; n < wsi->count_active_extensions; n++) {
798 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000799 wsi->protocol->owning_server,
800 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000801 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
802 wsi->active_extensions_user[n], &eff_buf, 0);
803 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800804 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000805 return -1;
806 }
807 if (m)
808 /*
809 * at least one extension told us he has more
810 * to spill, so we will go around again after
811 */
812 ret = 1;
813 }
814
815 /* assuming they gave us something to send, send it */
816
817 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800818 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
819 eff_buf.token_len);
820 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000821 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800822 /*
823 * Keep amount spilled small to minimize chance of this
824 */
825 if (n != eff_buf.token_len) {
826 lwsl_err("Unable to spill ext %d vs %s\n",
827 eff_buf.token_len, n);
828 return -1;
829 }
Andy Green3b84c002011-03-06 13:14:42 +0000830 } else
831 continue;
832
833 /* no extension has more to spill */
834
835 if (!ret)
836 continue;
837
838 /*
839 * There's more to spill from an extension, but we just sent
840 * something... did that leave the pipe choked?
841 */
842
843 if (!lws_send_pipe_choked(wsi))
844 /* no we could add more */
845 continue;
846
Andy Green43db0452013-01-10 19:50:35 +0800847 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000848
849 /*
850 * Yes, he's choked. Leave the POLLOUT masked on so we will
851 * come back here when he is unchoked. Don't call the user
852 * callback to enforce ordering of spilling, he'll get called
853 * when we come back here and there's nothing more to spill.
854 */
855
856 return 0;
857 }
858
859 wsi->extension_data_pending = 0;
860
861user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800862#endif
Andy Green3b84c002011-03-06 13:14:42 +0000863 /* one shot */
864
Andy Green91f19d82013-12-21 11:18:34 +0800865 if (pollfd)
866 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800867
Andy Green3182ece2013-01-20 17:08:31 +0800868#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100869notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800870#endif
Andy Green3b84c002011-03-06 13:14:42 +0000871
Andy Green9e4c2b62011-03-07 20:47:39 +0000872 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
873 n = LWS_CALLBACK_CLIENT_WRITEABLE;
874 else
875 n = LWS_CALLBACK_SERVER_WRITEABLE;
876
Andy Greenb8b247d2013-01-22 07:20:08 +0800877 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800878 wsi, (enum libwebsocket_callback_reasons) n,
879 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000880}
881
882
883
Andy Green1c6e1422013-02-20 19:11:31 +0800884int
Andy Greena41314f2011-05-23 10:00:03 +0100885libwebsocket_service_timeout_check(struct libwebsocket_context *context,
886 struct libwebsocket *wsi, unsigned int sec)
887{
Andy Green3182ece2013-01-20 17:08:31 +0800888#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100889 int n;
890
891 /*
892 * if extensions want in on it (eg, we are a mux parent)
893 * give them a chance to service child timeouts
894 */
895
896 for (n = 0; n < wsi->count_active_extensions; n++)
897 wsi->active_extensions[n]->callback(
898 context, wsi->active_extensions[n],
899 wsi, LWS_EXT_CALLBACK_1HZ,
900 wsi->active_extensions_user[n], NULL, sec);
901
Andy Green3182ece2013-01-20 17:08:31 +0800902#endif
Andy Greena41314f2011-05-23 10:00:03 +0100903 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800904 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800905
Andy Greena41314f2011-05-23 10:00:03 +0100906 /*
907 * if we went beyond the allowed time, kill the
908 * connection
909 */
910
911 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800912 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100913 libwebsocket_close_and_free_session(context,
914 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800915 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100916 }
Andy Green1c6e1422013-02-20 19:11:31 +0800917
918 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100919}
920
Andy Green9f990342011-02-12 11:57:45 +0000921/**
922 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000923 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000924 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800925 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000926 *
Andy Green75006172013-01-22 12:32:11 +0800927 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800928 * services it according to the state of the associated
929 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800930 *
931 * The one call deals with all "service" that might happen on a socket
932 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800933 *
934 * If a pollfd says it has something, you can just pass it to
935 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
936 * If it sees it is a lws socket, the traffic will be handled and
937 * pollfd->revents will be zeroed now.
938 *
939 * If the socket is foreign to lws, it leaves revents alone. So you can
940 * see if you should service yourself by checking the pollfd revents
941 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000942 */
943
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800944LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000945libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000946 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000947{
Andy Greena1ce6be2013-01-18 11:43:21 +0800948 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000949 int n;
Andy Green0d338332011-02-12 11:57:43 +0000950 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800951 int listen_socket_fds_index = 0;
Patrick Gansterer92792b42014-02-26 21:37:31 +0100952 time_t now;
Andy Green1c6e1422013-02-20 19:11:31 +0800953 int timed_out = 0;
954 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800955 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800956
Andy Green3182ece2013-01-20 17:08:31 +0800957#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000958 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800959#endif
Andy Green98a717c2011-03-06 13:14:15 +0000960 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800961
962 if (context->listen_service_fd)
963 listen_socket_fds_index = context->lws_lookup[
964 context->listen_service_fd]->position_in_fds_table;
965
Andy Greena71eafc2011-02-14 17:59:43 +0000966 /*
967 * you can call us with pollfd = NULL to just allow the once-per-second
968 * global timeout checks; if less than a second since the last check
969 * it returns immediately then.
970 */
971
Patrick Gansterer92792b42014-02-26 21:37:31 +0100972 time(&now);
Andy Greena71eafc2011-02-14 17:59:43 +0000973
Patrick Gansterer92792b42014-02-26 21:37:31 +0100974 if (context->last_timeout_check_s != now) {
975 context->last_timeout_check_s = now;
Andy Greena71eafc2011-02-14 17:59:43 +0000976
Joakim Soderberg4c531232013-02-06 15:26:58 +0900977 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800978 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800979 if (context->started_with_parent &&
980 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800981 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900982 #endif
Andy Green24cba922013-01-19 13:56:10 +0800983
Andy Greena71eafc2011-02-14 17:59:43 +0000984 /* global timeout check once per second */
985
Andy Green1c6e1422013-02-20 19:11:31 +0800986 if (pollfd)
987 our_fd = pollfd->fd;
988
Peter Hinz56885f32011-03-02 22:03:47 +0000989 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800990 m = context->fds[n].fd;
991 wsi = context->lws_lookup[m];
992 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800993 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800994
Patrick Gansterer92792b42014-02-26 21:37:31 +0100995 if (libwebsocket_service_timeout_check(context, wsi, now))
Andy Green1c6e1422013-02-20 19:11:31 +0800996 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800997 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800998 /* it was the guy we came to service! */
999 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001000 /* mark as handled */
1001 pollfd->revents = 0;
1002 }
Andy Greena71eafc2011-02-14 17:59:43 +00001003 }
1004 }
1005
Andy Green1c6e1422013-02-20 19:11:31 +08001006 /* the socket we came to service timed out, nothing to do */
1007 if (timed_out)
1008 return 0;
1009
Andy Greena71eafc2011-02-14 17:59:43 +00001010 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +00001011 if (pollfd == NULL)
1012 return 0;
1013
1014 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001015 wsi = context->lws_lookup[pollfd->fd];
1016 if (wsi == NULL)
1017 /* not lws connection ... leave revents alone and return */
1018 return 0;
1019
1020 /*
1021 * so that caller can tell we handled, past here we need to
1022 * zero down pollfd->revents after handling
1023 */
1024
Andy Green65b0e912013-01-16 07:59:47 +08001025 /*
1026 * deal with listen service piggybacking
1027 * every listen_service_modulo services of other fds, we
1028 * sneak one in to service the listen socket if there's anything waiting
1029 *
1030 * To handle connection storms, as found in ab, if we previously saw a
1031 * pending connection here, it causes us to check again next time.
1032 */
1033
Andy Greenb5b23192013-02-11 17:13:32 +08001034 if (context->listen_service_fd && pollfd !=
1035 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +08001036 context->listen_service_count++;
1037 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +08001038 context->listen_service_count ==
1039 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001040 context->listen_service_count = 0;
1041 m = 1;
1042 if (context->listen_service_extraseen > 5)
1043 m = 2;
1044 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001045 /*
1046 * even with extpoll, we prepared this
1047 * internal fds for listen
1048 */
1049 n = poll(&context->fds[listen_socket_fds_index],
1050 1, 0);
1051 if (n > 0) { /* there's a conn waiting for us */
1052 libwebsocket_service_fd(context,
1053 &context->
1054 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001055 context->listen_service_extraseen++;
1056 } else {
1057 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001058 context->
1059 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001060 break;
1061 }
1062 }
1063 }
1064
1065 }
1066
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001067 /* handle session socket closed */
1068
1069 if ((!(pollfd->revents & POLLIN)) &&
1070 (pollfd->revents & (POLLERR | POLLHUP))) {
1071
1072 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1073 (void *)wsi, pollfd->fd);
1074
1075 goto close_and_handled;
1076 }
1077
Andy Green65b0e912013-01-16 07:59:47 +08001078 /* okay, what we came here to do... */
1079
Andy Green0d338332011-02-12 11:57:43 +00001080 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001081
Andy Greena1ce6be2013-01-18 11:43:21 +08001082#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001083 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001084 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001085 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001086 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001087 n = lws_server_socket_service(context, wsi, pollfd);
1088 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001089#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001090
Andy Green0d338332011-02-12 11:57:43 +00001091 case LWS_CONNMODE_WS_SERVING:
1092 case LWS_CONNMODE_WS_CLIENT:
1093
Andy Green0d338332011-02-12 11:57:43 +00001094 /* the guy requested a callback when it was OK to write */
1095
Andy Greenda527df2011-03-07 07:08:12 +00001096 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001097 wsi->state == WSI_STATE_ESTABLISHED &&
1098 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001099 lwsl_info("libwebsocket_service_fd: closing\n");
1100 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001101 }
Andy Green0d338332011-02-12 11:57:43 +00001102
Andy Greenca0a1292013-03-16 11:24:23 +08001103 if (wsi->u.ws.rxflow_buffer &&
1104 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1105 lwsl_info("draining rxflow\n");
1106 /* well, drain it */
1107 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1108 wsi->u.ws.rxflow_pos;
1109 eff_buf.token_len = wsi->u.ws.rxflow_len -
1110 wsi->u.ws.rxflow_pos;
1111 draining_flow = 1;
1112 goto drain;
1113 }
1114
Andy Green0d338332011-02-12 11:57:43 +00001115 /* any incoming data ready? */
1116
1117 if (!(pollfd->revents & POLLIN))
1118 break;
1119
Andy Greenb45993c2010-12-18 15:13:50 +00001120#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001121read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001122 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001123 eff_buf.token_len = SSL_read(wsi->ssl,
1124 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001125 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001126 if (!eff_buf.token_len) {
1127 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001128 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001129 ERR_error_string(n,
1130 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001131 }
1132 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001133#endif
Andy Greene84652c2013-02-08 13:01:02 +08001134 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001135 context->service_buffer,
1136 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001137
Andy Green98a717c2011-03-06 13:14:15 +00001138 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001139 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1140 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001141 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001142 goto close_and_handled;
1143 n = 0;
1144 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001145 }
Andy Green98a717c2011-03-06 13:14:15 +00001146 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001147 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001148 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001149 }
1150
Andy Green98a717c2011-03-06 13:14:15 +00001151 /*
1152 * give any active extensions a chance to munge the buffer
1153 * before parse. We pass in a pointer to an lws_tokens struct
1154 * prepared with the default buffer and content length that's in
1155 * there. Rather than rewrite the default buffer, extensions
1156 * that expect to grow the buffer can adapt .token to
1157 * point to their own per-connection buffer in the extension
1158 * user allocation. By default with no extensions or no
1159 * extension callback handling, just the normal input buffer is
1160 * used then so it is efficient.
1161 */
Andy Greenb45993c2010-12-18 15:13:50 +00001162
Andy Greene84652c2013-02-08 13:01:02 +08001163 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001164drain:
Andy Green3182ece2013-01-20 17:08:31 +08001165#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001166 more = 1;
1167 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001168
Andy Green98a717c2011-03-06 13:14:15 +00001169 more = 0;
1170
1171 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001172 m = wsi->active_extensions[n]->callback(context,
1173 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001174 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001175 wsi->active_extensions_user[n],
1176 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001177 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001178 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001179 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001180 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001181 }
1182 if (m)
1183 more = 1;
1184 }
Andy Green3182ece2013-01-20 17:08:31 +08001185#endif
Andy Green98a717c2011-03-06 13:14:15 +00001186 /* service incoming data */
1187
1188 if (eff_buf.token_len) {
1189 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001190 (unsigned char *)eff_buf.token,
1191 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001192 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001193 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001194 n = 0;
1195 goto handled;
1196 }
Andy Green98a717c2011-03-06 13:14:15 +00001197 }
Andy Green3182ece2013-01-20 17:08:31 +08001198#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001199 eff_buf.token = NULL;
1200 eff_buf.token_len = 0;
1201 }
Andy Green3182ece2013-01-20 17:08:31 +08001202#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001203 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1204 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1205 lwsl_info("flow buffer: drained\n");
1206 free(wsi->u.ws.rxflow_buffer);
1207 wsi->u.ws.rxflow_buffer = NULL;
1208 /* having drained the rxflow buffer, can rearm POLLIN */
1209 _libwebsocket_rx_flow_control(wsi);
1210 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001211
1212#ifdef LWS_OPENSSL_SUPPORT
1213 if (wsi->ssl && SSL_pending(wsi->ssl))
1214 goto read_pending;
1215#endif
Andy Green98a717c2011-03-06 13:14:15 +00001216 break;
Andy Green76f61e72013-01-16 11:53:05 +08001217
1218 default:
Andy Green03674a62013-01-16 11:47:40 +08001219#ifdef LWS_NO_CLIENT
1220 break;
1221#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001222 n = lws_client_socket_service(context, wsi, pollfd);
1223 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001224#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001225 }
1226
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001227 n = 0;
1228 goto handled;
1229
1230close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001231 libwebsocket_close_and_free_session(context, wsi,
1232 LWS_CLOSE_STATUS_NOSTATUS);
1233 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001234
1235handled:
1236 pollfd->revents = 0;
1237 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001238}
1239
Andy Green0d338332011-02-12 11:57:43 +00001240
Andy Green6964bb52011-01-23 16:50:33 +00001241/**
1242 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001243 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001244 *
1245 * This function closes any active connections and then frees the
1246 * context. After calling this, any further use of the context is
1247 * undefined.
1248 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001249LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001250libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001251{
Andy Green0d338332011-02-12 11:57:43 +00001252 int n;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001253#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001254 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001255 struct libwebsocket_extension *ext;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001256#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001257 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001258
Andy Greend636e352013-01-29 12:36:17 +08001259#ifdef LWS_LATENCY
1260 if (context->worst_latency_info[0])
1261 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1262#endif
1263
Andy Greendfb23042013-01-17 12:26:48 +08001264 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001265 struct libwebsocket *wsi =
1266 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001267 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001268 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1269 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001270 }
Andy Green6964bb52011-01-23 16:50:33 +00001271
arnaudviala7c6f26c2014-02-15 14:39:40 +08001272#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001273 /*
1274 * give all extensions a chance to clean up any per-context
1275 * allocations they might have made
1276 */
1277
1278 ext = context->extensions;
1279 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1280 if (context->listen_port)
1281 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001282 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001283 ext->callback(context, ext, NULL,
1284 (enum libwebsocket_extension_callback_reasons)m,
1285 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001286 ext++;
1287 }
arnaudviala7c6f26c2014-02-15 14:39:40 +08001288#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001289
1290 /*
1291 * inform all the protocols that they are done and will have no more
1292 * callbacks
1293 */
1294
1295 while (protocol->callback) {
1296 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1297 NULL, NULL, 0);
1298 protocol++;
1299 }
1300
Andy Greena41314f2011-05-23 10:00:03 +01001301
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001302#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001303#else
1304 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001305#endif
1306
Peter Hinz56885f32011-03-02 22:03:47 +00001307#ifdef LWS_OPENSSL_SUPPORT
1308 if (context->ssl_ctx)
1309 SSL_CTX_free(context->ssl_ctx);
1310 if (context->ssl_client_ctx)
1311 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001312
1313 ERR_remove_state(0);
1314 ERR_free_strings();
1315 EVP_cleanup();
1316 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001317#endif
1318
Andy Green46596482013-02-11 11:04:01 +08001319 if (context->fds)
1320 free(context->fds);
1321 if (context->lws_lookup)
1322 free(context->lws_lookup);
1323
Peter Hinz56885f32011-03-02 22:03:47 +00001324 free(context);
1325
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001326#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001327 WSACleanup();
1328#endif
Andy Green6964bb52011-01-23 16:50:33 +00001329}
1330
Andy Greend88146d2013-01-22 12:40:35 +08001331/**
Andy Greenb5b23192013-02-11 17:13:32 +08001332 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001333 * @context: Websocket context
1334 *
1335 * This returns the optional user allocation that can be attached to
1336 * the context the sockets live in at context_create time. It's a way
1337 * to let all sockets serviced in the same context share data without
1338 * using globals statics in the user code.
1339 */
Alon Levy0291eb32012-10-19 11:21:56 +02001340LWS_EXTERN void *
1341libwebsocket_context_user(struct libwebsocket_context *context)
1342{
Andy Greenb5b23192013-02-11 17:13:32 +08001343 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001344}
1345
Andy Green6964bb52011-01-23 16:50:33 +00001346/**
1347 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001348 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001349 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1350 * service otherwise block and service immediately, returning
1351 * after the timeout if nothing needed service.
1352 *
1353 * This function deals with any pending websocket traffic, for three
1354 * kinds of event. It handles these events on both server and client
1355 * types of connection the same.
1356 *
1357 * 1) Accept new connections to our context's server
1358 *
Andy Green6f520a52013-01-29 17:57:39 +08001359 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001360 * server or client connections.
1361 *
1362 * You need to call this service function periodically to all the above
1363 * functions to happen; if your application is single-threaded you can
1364 * just call it in your main event loop.
1365 *
1366 * Alternatively you can fork a new process that asynchronously handles
1367 * calling this service in a loop. In that case you are happy if this
1368 * call blocks your thread until it needs to take care of something and
1369 * would call it with a large nonzero timeout. Your loop then takes no
1370 * CPU while there is nothing happening.
1371 *
1372 * If you are calling it in a single-threaded app, you don't want it to
1373 * wait around blocking other things in your loop from happening, so you
1374 * would call it with a timeout_ms of 0, so it returns immediately if
1375 * nothing is pending, or as soon as it services whatever was pending.
1376 */
1377
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001378LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001379libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001380{
1381 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001382 int m;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001383#ifdef LWS_HAS_PPOLL
1384 struct timespec timeout_ts;
1385 sigset_t sigmask;
1386#endif
Andy Greene92cd172011-01-19 13:11:55 +00001387
1388 /* stay dead once we are dead */
1389
Peter Hinz56885f32011-03-02 22:03:47 +00001390 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001391 return 1;
1392
Andy Green3b3fa9e2013-12-25 16:34:37 +08001393#ifdef LWS_HAS_PPOLL
1394 lws_idling_ppoll_tid = context->protocols[0].callback(context, NULL,
1395 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1396
1397 timeout_ts.tv_sec = timeout_ms / 1000;
1398 timeout_ts.tv_nsec = timeout_ms % 1000;
Andy Greened451d52014-01-11 12:37:07 +08001399
1400 sigprocmask(SIG_BLOCK, NULL, &sigmask);
1401 sigdelset(&sigmask, SIGUSR2);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001402
Andy Green0d338332011-02-12 11:57:43 +00001403 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001404
Andy Green3b3fa9e2013-12-25 16:34:37 +08001405 n = ppoll(context->fds, context->fds_count, &timeout_ts, &sigmask);
1406 lws_idling_ppoll_tid = 0;
1407#else
Peter Hinz56885f32011-03-02 22:03:47 +00001408 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001409#endif
Andy Green5dc62ea2013-09-20 20:26:12 +08001410 if (n == 0) /* poll timeout */ {
1411 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001412 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001413 }
Andy Greene92cd172011-01-19 13:11:55 +00001414
Andy Greened451d52014-01-11 12:37:07 +08001415 if (n < 0) {
1416 if (errno != EINTR)
1417 return -1;
1418 else
1419 return 0;
1420 }
Andy Greene92cd172011-01-19 13:11:55 +00001421
Andy Greendfb23042013-01-17 12:26:48 +08001422 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001423
Andy Greenca0a1292013-03-16 11:24:23 +08001424 for (n = 0; n < context->fds_count; n++) {
1425 if (!context->fds[n].revents)
1426 continue;
1427 m = libwebsocket_service_fd(context, &context->fds[n]);
1428 if (m < 0)
1429 return -1;
1430 /* if something closed, retry this slot */
1431 if (m)
1432 n--;
1433 }
1434
Andy Greene92cd172011-01-19 13:11:55 +00001435 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001436}
1437
Andy Green3182ece2013-01-20 17:08:31 +08001438#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001439int
1440lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001441 struct libwebsocket *wsi,
1442 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001443 void *v, size_t len)
1444{
1445 int n;
1446 int handled = 0;
1447
1448 /* maybe an extension will take care of it for us */
1449
1450 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1451 if (!wsi->active_extensions[n]->callback)
1452 continue;
1453
1454 handled |= wsi->active_extensions[n]->callback(context,
1455 wsi->active_extensions[n], wsi,
1456 r, wsi->active_extensions_user[n], v, len);
1457 }
1458
1459 return handled;
1460}
1461
1462
1463void *
1464lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001465 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001466{
1467 int n = 0;
1468
Andy Green68b45042011-05-25 21:41:57 +01001469 if (wsi == NULL)
1470 return NULL;
1471
Andy Greena41314f2011-05-23 10:00:03 +01001472 while (n < wsi->count_active_extensions) {
1473 if (wsi->active_extensions[n] != ext) {
1474 n++;
1475 continue;
1476 }
1477 return wsi->active_extensions_user[n];
1478 }
1479
1480 return NULL;
1481}
Andy Green3182ece2013-01-20 17:08:31 +08001482#endif
Andy Greena41314f2011-05-23 10:00:03 +01001483
Andy Green91f19d82013-12-21 11:18:34 +08001484void
1485lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1486{
1487 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001488 int events;
1489#ifdef LWS_HAS_PPOLL
1490 int tid;
1491 int sampled_ppoll_tid;
1492#endif
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001493 struct libwebsocket_pollargs pa;
1494
1495 pa.fd = wsi->sock;
Andy Green91f19d82013-12-21 11:18:34 +08001496
1497 context->protocols[0].callback(context, wsi,
1498 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001499 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001500
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001501 pa.prev_events = events = context->fds[wsi->position_in_fds_table].events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001502
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001503 pa.events = context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001504
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001505 context->protocols[0].callback(context, wsi,
1506 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
1507 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001508
Andy Green91f19d82013-12-21 11:18:34 +08001509
Andy Green3b3fa9e2013-12-25 16:34:37 +08001510#ifdef LWS_HAS_PPOLL
1511 /*
1512 * if we changed something in this pollfd...
1513 * ... and we're running in a different thread context
1514 * than the service thread...
1515 * ... and the service thread is waiting in ppoll()...
1516 * then fire a SIGUSR2 at the service thread to force it to
1517 * restart the ppoll() with our changed events
1518 */
1519 if (events != context->fds[wsi->position_in_fds_table].events) {
1520 sampled_ppoll_tid = lws_idling_ppoll_tid;
1521 if (sampled_ppoll_tid) {
1522 tid = context->protocols[0].callback(context, NULL,
1523 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1524 if (tid != sampled_ppoll_tid)
1525 kill(sampled_ppoll_tid, SIGUSR2);
1526 }
1527 }
1528#endif
1529
Andy Green91f19d82013-12-21 11:18:34 +08001530 context->protocols[0].callback(context, wsi,
1531 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001532 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001533}
1534
1535
Andy Green90c7cbc2011-01-27 06:26:52 +00001536/**
1537 * libwebsocket_callback_on_writable() - Request a callback when this socket
1538 * becomes able to be written to without
1539 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001540 *
Peter Hinz56885f32011-03-02 22:03:47 +00001541 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001542 * @wsi: Websocket connection instance to get callback for
1543 */
1544
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001545LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001546libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001547 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001548{
Andy Green3182ece2013-01-20 17:08:31 +08001549#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001550 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001551 int handled = 0;
1552
1553 /* maybe an extension will take care of it for us */
1554
1555 for (n = 0; n < wsi->count_active_extensions; n++) {
1556 if (!wsi->active_extensions[n]->callback)
1557 continue;
1558
1559 handled |= wsi->active_extensions[n]->callback(context,
1560 wsi->active_extensions[n], wsi,
1561 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1562 wsi->active_extensions_user[n], NULL, 0);
1563 }
1564
1565 if (handled)
1566 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001567#endif
Andy Greendfb23042013-01-17 12:26:48 +08001568 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001569 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1570 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001571 return -1;
1572 }
1573
Andy Green91f19d82013-12-21 11:18:34 +08001574 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001575
Andy Green90c7cbc2011-01-27 06:26:52 +00001576 return 1;
1577}
1578
1579/**
1580 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1581 * all connections using the given protocol when it
1582 * becomes possible to write to each socket without
1583 * blocking in turn.
1584 *
1585 * @protocol: Protocol whose connections will get callbacks
1586 */
1587
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001588LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001589libwebsocket_callback_on_writable_all_protocol(
1590 const struct libwebsocket_protocols *protocol)
1591{
Peter Hinz56885f32011-03-02 22:03:47 +00001592 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001593 int n;
Andy Green0d338332011-02-12 11:57:43 +00001594 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001595
Andy Greendfb23042013-01-17 12:26:48 +08001596 for (n = 0; n < context->fds_count; n++) {
1597 wsi = context->lws_lookup[context->fds[n].fd];
1598 if (!wsi)
1599 continue;
1600 if (wsi->protocol == protocol)
1601 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001602 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001603
1604 return 0;
1605}
1606
Andy Greenbe93fef2011-02-14 20:25:43 +00001607/**
Andy Greene39e6ef2014-02-15 16:36:38 +08001608 * libwebsocket_callback_all_protocol() - Callback all connections using
1609 * the given protocol with the given reason
1610 *
1611 * @protocol: Protocol whose connections will get callbacks
1612 * @reason: Callback reason index
1613 */
1614
1615LWS_VISIBLE int
1616libwebsocket_callback_all_protocol(
1617 const struct libwebsocket_protocols *protocol, int reason)
1618{
1619 struct libwebsocket_context *context = protocol->owning_server;
1620 int n;
1621 struct libwebsocket *wsi;
1622
1623 for (n = 0; n < context->fds_count; n++) {
1624 wsi = context->lws_lookup[context->fds[n].fd];
1625 if (!wsi)
1626 continue;
1627 if (wsi->protocol == protocol)
1628 protocol->callback(context, wsi,
1629 reason, wsi->user_space, NULL, 0);
1630 }
1631
1632 return 0;
1633}
1634
1635/**
Andy Greenbe93fef2011-02-14 20:25:43 +00001636 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1637 *
1638 * You will not need this unless you are doing something special
1639 *
1640 * @wsi: Websocket connection instance
1641 * @reason: timeout reason
1642 * @secs: how many seconds
1643 */
1644
Craig McQueene7fd8b12014-02-19 09:24:17 +11001645LWS_VISIBLE void
Andy Greenbe93fef2011-02-14 20:25:43 +00001646libwebsocket_set_timeout(struct libwebsocket *wsi,
1647 enum pending_timeout reason, int secs)
1648{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001649 time_t now;
Andy Greenbe93fef2011-02-14 20:25:43 +00001650
Patrick Gansterer92792b42014-02-26 21:37:31 +01001651 time(&now);
Andy Greenbe93fef2011-02-14 20:25:43 +00001652
Patrick Gansterer92792b42014-02-26 21:37:31 +01001653 wsi->pending_timeout_limit = now + secs;
Andy Greenbe93fef2011-02-14 20:25:43 +00001654 wsi->pending_timeout = reason;
1655}
1656
Andy Greena6cbece2011-01-27 20:06:03 +00001657
1658/**
1659 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1660 *
1661 * You will not need this unless you are doing something special
1662 *
1663 * @wsi: Websocket connection instance
1664 */
1665
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001666LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001667libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1668{
1669 return wsi->sock;
1670}
1671
Andy Greend636e352013-01-29 12:36:17 +08001672#ifdef LWS_LATENCY
1673void
Andy Greenb5b23192013-02-11 17:13:32 +08001674lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1675 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001676{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001677 unsigned long long u;
Andy Greend636e352013-01-29 12:36:17 +08001678 char buf[256];
1679
Patrick Gansterer92792b42014-02-26 21:37:31 +01001680 u = time_in_microseconds();
Andy Greend636e352013-01-29 12:36:17 +08001681
1682 if (action) {
1683 if (completed) {
1684 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001685 sprintf(buf,
1686 "Completion first try lat %luus: %p: ret %d: %s\n",
1687 u - wsi->latency_start,
1688 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001689 else
Andy Greenb5b23192013-02-11 17:13:32 +08001690 sprintf(buf,
1691 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1692 u - wsi->action_start,
1693 u - wsi->latency_start,
1694 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001695 wsi->action_start = 0;
1696 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001697 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1698 u - wsi->latency_start,
1699 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001700 if (u - wsi->latency_start > context->worst_latency) {
1701 context->worst_latency = u - wsi->latency_start;
1702 strcpy(context->worst_latency_info, buf);
1703 }
1704 lwsl_latency("%s", buf);
1705 } else {
1706 wsi->latency_start = u;
1707 if (!wsi->action_start)
1708 wsi->action_start = u;
1709 }
1710}
1711#endif
1712
Andy Greena1ce6be2013-01-18 11:43:21 +08001713#ifdef LWS_NO_SERVER
1714int
Andy Green8d15cf42013-10-24 21:47:06 +08001715_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001716{
1717 return 0;
1718}
1719#else
Andy Green706961d2013-01-17 16:50:35 +08001720int
1721_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1722{
1723 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001724
Andy Greenca0a1292013-03-16 11:24:23 +08001725 /* there is no pending change */
1726 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001727 return 0;
1728
Andy Greenca0a1292013-03-16 11:24:23 +08001729 /* stuff is still buffered, not ready to really accept new input */
1730 if (wsi->u.ws.rxflow_buffer) {
1731 /* get ourselves called back to deal with stashed buffer */
1732 libwebsocket_callback_on_writable(context, wsi);
1733 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001734 }
1735
Andy Greenca0a1292013-03-16 11:24:23 +08001736 /* pending is cleared, we can change rxflow state */
1737
1738 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1739
1740 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1741 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1742
1743 /* adjust the pollfd for this wsi */
1744
1745 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001746 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001747 else
Andy Green91f19d82013-12-21 11:18:34 +08001748 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001749
Andy Green706961d2013-01-17 16:50:35 +08001750 return 1;
1751}
Andy Greena1ce6be2013-01-18 11:43:21 +08001752#endif
Andy Green706961d2013-01-17 16:50:35 +08001753
Andy Green90c7cbc2011-01-27 06:26:52 +00001754/**
1755 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1756 * receieved packets.
1757 *
1758 * If the output side of a server process becomes choked, this allows flow
1759 * control for the input side.
1760 *
1761 * @wsi: Websocket connection instance to get callback for
1762 * @enable: 0 = disable read servicing for this connection, 1 = enable
1763 */
1764
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001765LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001766libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1767{
Andy Greenca0a1292013-03-16 11:24:23 +08001768 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1769 return 0;
1770
1771 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1772 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001773
Andy Green706961d2013-01-17 16:50:35 +08001774 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001775}
1776
Andy Greenb55451c2013-03-16 12:32:27 +08001777/**
1778 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1779 *
1780 * When the user server code realizes it can accept more input, it can
1781 * call this to have the RX flow restriction removed from all connections using
1782 * the given protocol.
1783 *
1784 * @protocol: all connections using this protocol will be allowed to receive
1785 */
1786
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001787LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001788libwebsocket_rx_flow_allow_all_protocol(
1789 const struct libwebsocket_protocols *protocol)
1790{
1791 struct libwebsocket_context *context = protocol->owning_server;
1792 int n;
1793 struct libwebsocket *wsi;
1794
1795 for (n = 0; n < context->fds_count; n++) {
1796 wsi = context->lws_lookup[context->fds[n].fd];
1797 if (!wsi)
1798 continue;
1799 if (wsi->protocol == protocol)
1800 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1801 }
1802}
1803
Andy Green706961d2013-01-17 16:50:35 +08001804
Andy Green2ac5a6f2011-01-28 10:00:18 +00001805/**
1806 * libwebsocket_canonical_hostname() - returns this host's hostname
1807 *
1808 * This is typically used by client code to fill in the host parameter
1809 * when making a client connection. You can only call it after the context
1810 * has been created.
1811 *
Peter Hinz56885f32011-03-02 22:03:47 +00001812 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001813 */
1814
1815
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001816LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001817libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001818{
Peter Hinz56885f32011-03-02 22:03:47 +00001819 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001820}
1821
1822
Andy Green90c7cbc2011-01-27 06:26:52 +00001823static void sigpipe_handler(int x)
1824{
1825}
1826
Andy Green6901cb32011-02-21 08:06:47 +00001827#ifdef LWS_OPENSSL_SUPPORT
1828static int
1829OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1830{
1831
1832 SSL *ssl;
1833 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001834 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001835
1836 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1837 SSL_get_ex_data_X509_STORE_CTX_idx());
1838
1839 /*
Andy Green2e24da02011-03-05 16:12:04 +00001840 * !!! nasty openssl requires the index to come as a library-scope
1841 * static
Andy Green6901cb32011-02-21 08:06:47 +00001842 */
Andy Green2e24da02011-03-05 16:12:04 +00001843 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001844
Peter Hinz56885f32011-03-02 22:03:47 +00001845 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001846 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1847 x509_ctx, ssl, preverify_ok);
1848
1849 /* convert return code from 0 = OK to 1 = OK */
1850
1851 if (!n)
1852 n = 1;
1853 else
1854 n = 0;
1855
1856 return n;
1857}
1858#endif
1859
Andy Green706961d2013-01-17 16:50:35 +08001860int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001861 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001862 struct libwebsocket *wsi,
1863 enum libwebsocket_callback_reasons reason, void *user,
1864 void *in, size_t len)
1865{
1866 int n;
1867
1868 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001869 if (!n)
1870 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001871
Andy Greenaedc9532013-02-10 21:21:24 +08001872 return n;
Andy Green706961d2013-01-17 16:50:35 +08001873}
1874
Andy Green3b3fa9e2013-12-25 16:34:37 +08001875/*
1876 * This is just used to interrupt poll waiting
1877 * we don't have to do anything with it.
1878 */
arnaudviala7c6f26c2014-02-15 14:39:40 +08001879#ifdef LWS_OPENSSL_SUPPORT
Andy Green3b3fa9e2013-12-25 16:34:37 +08001880static void lws_sigusr2(int sig)
1881{
1882}
arnaudviala7c6f26c2014-02-15 14:39:40 +08001883#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001884
Andy Greenab990e42010-10-31 12:42:52 +00001885/**
Andy Green4739e5c2011-01-22 12:51:57 +00001886 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001887 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001888 *
Andy Green1b265272013-02-09 14:01:09 +08001889 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001890 * of all initialization in one step.
1891 *
Andy Greene92cd172011-01-19 13:11:55 +00001892 * After initialization, it returns a struct libwebsocket_context * that
1893 * represents this server. After calling, user code needs to take care
1894 * of calling libwebsocket_service() with the context pointer to get the
1895 * server's sockets serviced. This can be done in the same process context
1896 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001897 *
Andy Green8f037e42010-12-19 22:13:26 +00001898 * The protocol callback functions are called for a handful of events
1899 * including http requests coming in, websocket connections becoming
1900 * established, and data arriving; it's also called periodically to allow
1901 * async transmission.
1902 *
1903 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1904 * at that time websocket protocol has not been negotiated. Other
1905 * protocols after the first one never see any HTTP callack activity.
1906 *
1907 * The server created is a simple http server by default; part of the
1908 * websocket standard is upgrading this http connection to a websocket one.
1909 *
1910 * This allows the same server to provide files like scripts and favicon /
1911 * images or whatever over http and dynamic data over websockets all in
1912 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001913 */
Andy Green4ea60062010-10-30 12:15:07 +01001914
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001915LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001916libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001917{
Peter Hinz56885f32011-03-02 22:03:47 +00001918 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001919 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001920 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001921#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001922 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001923 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001924 struct sockaddr_in serv_addr;
1925#endif
Andy Green3182ece2013-01-20 17:08:31 +08001926#ifndef LWS_NO_EXTENSIONS
1927 int m;
Andy Green1b265272013-02-09 14:01:09 +08001928 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001929#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001930
Andy Green3faa9c72010-11-08 17:03:03 +00001931#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001932 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001933#endif
1934
Joakim Soderberg4c531232013-02-06 15:26:58 +09001935#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001936 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001937#endif
1938
Andy Greenb3a614a2013-01-19 13:08:17 +08001939 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001940 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001941 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001942 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001943#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001944 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1945 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001946#else
1947 lwsl_notice(" Configured without extension support\n");
1948#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001949 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1950 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001951 if (info->ssl_cipher_list)
1952 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001953 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1954 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001955
Peter Hinz56885f32011-03-02 22:03:47 +00001956#ifdef _WIN32
1957 {
1958 WORD wVersionRequested;
1959 WSADATA wsaData;
1960 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001961 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001962
1963 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1964 wVersionRequested = MAKEWORD(2, 2);
1965
1966 err = WSAStartup(wVersionRequested, &wsaData);
1967 if (err != 0) {
1968 /* Tell the user that we could not find a usable */
1969 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001970 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001971 return NULL;
1972 }
David Galeano7b11fec2011-10-04 19:55:18 +08001973
Andy Green6ee372f2012-04-09 15:09:01 +08001974 /* default to a poll() made out of select() */
1975 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001976
Andy Green6ee372f2012-04-09 15:09:01 +08001977 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001978 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001979 if (wsdll)
1980 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001981
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001982 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001983 if (!poll)
1984 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001985 }
1986#endif
1987
Andy Greenb5b23192013-02-11 17:13:32 +08001988 context = (struct libwebsocket_context *)
1989 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001990 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001991 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001992 return NULL;
1993 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001994 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001995#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001996 context->started_with_parent = pid_daemon;
1997 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1998#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001999
Joakim Soderberg4c531232013-02-06 15:26:58 +09002000 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08002001 context->protocols = info->protocols;
2002 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00002003 context->http_proxy_port = 0;
2004 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08002005 context->options = info->options;
Mattias Lundberg03bb8f92014-02-18 10:06:57 +01002006 context->iface = info->iface;
Andy Greendfb23042013-01-17 12:26:48 +08002007 /* to reduce this allocation, */
2008 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08002009 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
2010 sizeof(struct libwebsocket_context),
2011 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
2012 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08002013 sizeof(struct libwebsocket_context) +
2014 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
2015 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08002016
Andy Greenb5b23192013-02-11 17:13:32 +08002017 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
2018 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002019 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002020 lwsl_err("Unable to allocate fds array for %d connections\n",
2021 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002022 free(context);
2023 return NULL;
2024 }
Andy Greenb5b23192013-02-11 17:13:32 +08002025 context->lws_lookup = (struct libwebsocket **)
2026 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002027 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002028 lwsl_err(
2029 "Unable to allocate lws_lookup array for %d connections\n",
2030 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002031 free(context->fds);
2032 free(context);
2033 return NULL;
2034 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08002035 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
2036 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08002037
Peter Hinz56885f32011-03-02 22:03:47 +00002038 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002039#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08002040 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08002041#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002042 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08002043 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00002044
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002045#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002046 context->fd_random = 0;
2047#else
2048 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2049 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002050 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002051 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002052 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00002053 }
Peter Hinz56885f32011-03-02 22:03:47 +00002054#endif
Andy Green44eee682011-02-10 09:32:24 +00002055
Peter Hinz56885f32011-03-02 22:03:47 +00002056#ifdef LWS_OPENSSL_SUPPORT
2057 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08002058 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002059 context->ssl_ctx = NULL;
2060 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002061 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002062#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002063
Andy Greena1ce6be2013-01-18 11:43:21 +08002064 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002065
Andy Greena1ce6be2013-01-18 11:43:21 +08002066#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002067 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01002068 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08002069 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08002070 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01002071
Andy Greenb5b23192013-02-11 17:13:32 +08002072 lwsl_notice(" canonical_hostname = %s\n",
2073 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002074 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002075#endif
Andy Greena69f0512012-05-03 12:32:38 +08002076
Andy Green9659f372011-01-27 22:01:43 +00002077 /* split the proxy ads:port if given */
2078
2079 p = getenv("http_proxy");
2080 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00002081 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08002082 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002083 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002084 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002085
Peter Hinz56885f32011-03-02 22:03:47 +00002086 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00002087 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002088 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002089 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00002090 }
2091 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00002092 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002093
Andy Greenb3a614a2013-01-19 13:08:17 +08002094 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002095 context->http_proxy_address,
2096 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002097 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002098
Andy Greena1ce6be2013-01-18 11:43:21 +08002099#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002100 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002101
Andy Green3faa9c72010-11-08 17:03:03 +00002102#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002103 context->use_ssl = info->ssl_cert_filepath != NULL &&
2104 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002105#ifdef USE_CYASSL
2106 lwsl_notice(" Compiled with CYASSL support\n");
2107#else
2108 lwsl_notice(" Compiled with OpenSSL support\n");
2109#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002110 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002111 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002112 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002113 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002114
Andy Green90c7cbc2011-01-27 06:26:52 +00002115#else
Andy Green2b40b792013-02-10 22:22:01 +08002116 if (info->ssl_cert_filepath != NULL &&
2117 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002118 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002119 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002120 }
Andy Greenb5b23192013-02-11 17:13:32 +08002121 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002122#endif
Andy Greena17c6922013-01-20 20:21:54 +08002123
Andy Greenb5b23192013-02-11 17:13:32 +08002124 lwsl_notice(
2125 " per-conn mem: %u + %u headers + protocol rx buf\n",
2126 sizeof(struct libwebsocket),
2127 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002128 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002129#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002130
2131 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002132#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002133#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002134 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002135#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002136
2137
2138#ifdef LWS_OPENSSL_SUPPORT
2139
2140 /* basic openssl init */
2141
2142 SSL_library_init();
2143
2144 OpenSSL_add_all_algorithms();
2145 SSL_load_error_strings();
2146
Andy Green2e24da02011-03-05 16:12:04 +00002147 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002148 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2149
Andy Green90c7cbc2011-01-27 06:26:52 +00002150 /*
2151 * Firefox insists on SSLv23 not SSLv3
2152 * Konq disables SSLv2 by default now, SSLv23 works
2153 */
2154
2155 method = (SSL_METHOD *)SSLv23_server_method();
2156 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002157 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002158 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002159 error,
2160 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002161 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002162 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002163 }
Peter Hinz56885f32011-03-02 22:03:47 +00002164 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2165 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002166 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002167 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002168 error,
2169 ERR_error_string(error,
2170 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002171 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002172 }
2173
martell19c73f32014-01-22 18:17:03 +00002174#if defined(WIN32) || defined(_WIN32)
2175#else
Andy Green3b3fa9e2013-12-25 16:34:37 +08002176 signal(SIGUSR2, lws_sigusr2);
Andy Greened451d52014-01-11 12:37:07 +08002177 {
2178 sigset_t mask;
2179 sigemptyset (&mask);
2180 sigaddset (&mask, SIGUSR2);
2181
2182 sigprocmask(SIG_BLOCK, &mask, NULL);
2183 }
martell19c73f32014-01-22 18:17:03 +00002184#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002185
David Galeanocc148e42013-01-10 10:18:59 +08002186#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002187 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002188#endif
David Galeano77a677c2013-01-10 10:14:12 +08002189 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002190 if (info->ssl_cipher_list)
2191 SSL_CTX_set_cipher_list(context->ssl_ctx,
2192 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002193
Andy Greena1ce6be2013-01-18 11:43:21 +08002194#ifndef LWS_NO_CLIENT
2195
Andy Green90c7cbc2011-01-27 06:26:52 +00002196 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002197
Andy Green1b265272013-02-09 14:01:09 +08002198 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002199 method = (SSL_METHOD *)SSLv23_client_method();
2200 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002201 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002202 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002203 error,
2204 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002205 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002206 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002207 }
2208 /* create context */
2209 context->ssl_client_ctx = SSL_CTX_new(method);
2210 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002211 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002212 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002213 error,
2214 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002215 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002216 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002217 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002218
David Galeanocc148e42013-01-10 10:18:59 +08002219#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002220 SSL_CTX_set_options(context->ssl_client_ctx,
2221 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002222#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002223 SSL_CTX_set_options(context->ssl_client_ctx,
2224 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002225 if (info->ssl_cipher_list)
2226 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2227 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002228
Peter Hinz56885f32011-03-02 22:03:47 +00002229 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002230 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002231 if (!SSL_CTX_load_verify_locations(
2232 context->ssl_client_ctx, NULL,
2233 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002234 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002235 "Unable to load SSL Client certs from %s "
2236 "(set by --with-client-cert-dir= "
2237 "in configure) -- client ssl isn't "
2238 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002239 } else
2240 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002241 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002242 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002243 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002244 "Unable to load SSL Client certs "
2245 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002246 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002247
2248 /*
2249 * callback allowing user code to load extra verification certs
2250 * helping the client to verify server identity
2251 */
2252
prasannateamf1b353a452013-11-12 17:21:01 -08002253 /* support for client-side certificate authentication */
2254 if (info->ssl_cert_filepath) {
2255 n = SSL_CTX_use_certificate_chain_file(
2256 context->ssl_client_ctx,
2257 info->ssl_cert_filepath);
2258 if (n != 1) {
2259 lwsl_err("problem getting cert '%s' %lu: %s\n",
2260 info->ssl_cert_filepath,
2261 ERR_get_error(),
2262 ERR_error_string(ERR_get_error(),
2263 (char *)context->service_buffer));
2264 goto bail;
2265 }
2266 }
2267 if (info->ssl_private_key_filepath) {
2268 /* set the private key from KeyFile */
2269 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2270 info->ssl_private_key_filepath,
2271 SSL_FILETYPE_PEM) != 1) {
2272 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2273 info->ssl_private_key_filepath,
2274 ERR_get_error(),
2275 ERR_error_string(ERR_get_error(),
2276 (char *)context->service_buffer));
2277 goto bail;
2278 }
2279
2280 /* verify private key */
2281 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2282 lwsl_err("Private SSL key doesn't match cert\n");
2283 goto bail;
2284 }
2285 }
2286
Peter Hinz56885f32011-03-02 22:03:47 +00002287 context->protocols[0].callback(context, NULL,
2288 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2289 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002290 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002291#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002292
Andy Greenc6bf2c22011-02-20 11:10:47 +00002293 /* as a server, are we requiring clients to identify themselves? */
2294
Andy Greenb5b23192013-02-11 17:13:32 +08002295 if (info->options &
2296 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002297
2298 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002299
Peter Hinz56885f32011-03-02 22:03:47 +00002300 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002301 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2302 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002303
2304 /*
2305 * give user code a chance to load certs into the server
2306 * allowing it to verify incoming client certs
2307 */
2308
Peter Hinz56885f32011-03-02 22:03:47 +00002309 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002310 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002311 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002312 }
2313
James Devine5b34c972013-12-14 11:41:29 +08002314 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2315 /* Normally SSL listener rejects non-ssl, optionally allow */
2316 context->allow_non_ssl_on_ssl_port = 1;
2317 }
2318
Peter Hinz56885f32011-03-02 22:03:47 +00002319 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002320
2321 /* openssl init for server sockets */
2322
Andy Green3faa9c72010-11-08 17:03:03 +00002323 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002324 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002325 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002326 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002327 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002328 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002329 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002330 error,
2331 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002332 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002333 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002334 }
2335 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002336 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002337 info->ssl_private_key_filepath,
2338 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002339 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002340 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002341 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002342 error,
2343 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002344 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002345 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002346 }
2347 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002348 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002349 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002350 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002351 }
2352
2353 /* SSL is happy and has a cert it's content with */
2354 }
2355#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002356
Andy Greena1ce6be2013-01-18 11:43:21 +08002357#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002358 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002359
Andy Green1b265272013-02-09 14:01:09 +08002360 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002361 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002362
Andy Green4739e5c2011-01-22 12:51:57 +00002363 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2364 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002365 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002366 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002367 }
Andy Green775c0dd2010-10-29 14:15:22 +01002368
Andy Greenb5b23192013-02-11 17:13:32 +08002369 /*
2370 * allow us to restart even if old sockets in TIME_WAIT
Andy Greenb5b23192013-02-11 17:13:32 +08002371 */
Andy Green6ee372f2012-04-09 15:09:01 +08002372 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2373 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002374
2375 /* Disable Nagle */
2376 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002377 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2378 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002379
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002380 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002381 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002382 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002383 #else
Andy Greene2160712013-01-28 12:19:10 +08002384 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002385 #endif
Andy Greene2160712013-01-28 12:19:10 +08002386
Andy Green4739e5c2011-01-22 12:51:57 +00002387 bzero((char *) &serv_addr, sizeof(serv_addr));
2388 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002389 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002390 serv_addr.sin_addr.s_addr = INADDR_ANY;
2391 else
Andy Greend1eac602013-11-09 08:07:38 +08002392 if (interface_to_sa(info->iface, &serv_addr,
2393 sizeof(serv_addr)) < 0) {
2394 lwsl_err("Unable to find interface %s\n",
2395 info->iface);
2396 compatible_close(sockfd);
2397 goto bail;
2398 }
Andy Green1b265272013-02-09 14:01:09 +08002399 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002400
2401 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2402 sizeof(serv_addr));
2403 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002404 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002405 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002406 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002407 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002408 }
Andy Green0d338332011-02-12 11:57:43 +00002409
Andy Greenb5b23192013-02-11 17:13:32 +08002410 wsi = (struct libwebsocket *)malloc(
2411 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002412 if (wsi == NULL) {
2413 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002414 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002415 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002416 }
Andy Greenb5b23192013-02-11 17:13:32 +08002417 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002418 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002419#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002420 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002421#endif
Andy Green0d338332011-02-12 11:57:43 +00002422 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002423
2424 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002425
Andy Green65b0e912013-01-16 07:59:47 +08002426 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2427 context->listen_service_count = 0;
2428 context->listen_service_fd = sockfd;
2429
Andy Greena824d182013-01-15 20:52:29 +08002430 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002431 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002432 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002433#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002434
Andy Green6ee372f2012-04-09 15:09:01 +08002435 /*
2436 * drop any root privs for this process
2437 * to listen on port < 1023 we would have needed root, but now we are
2438 * listening, we don't want the power for anything else
2439 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002440#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002441#else
Andy Green1b265272013-02-09 14:01:09 +08002442 if (info->gid != -1)
2443 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002444 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002445 if (info->uid != -1)
2446 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002447 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002448#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002449
Andy Green6f520a52013-01-29 17:57:39 +08002450 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002451
Peter Hinz56885f32011-03-02 22:03:47 +00002452 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002453 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002454 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002455
Andy Green43db0452013-01-10 19:50:35 +08002456 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002457 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002458
Andy Green1b265272013-02-09 14:01:09 +08002459 info->protocols[context->count_protocols].owning_server =
2460 context;
2461 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002462 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002463
2464 /*
2465 * inform all the protocols that they are doing their one-time
2466 * initialization if they want to
2467 */
2468 info->protocols[context->count_protocols].callback(context,
2469 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002470 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002471
Andy Green3182ece2013-01-20 17:08:31 +08002472#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002473 /*
2474 * give all extensions a chance to create any per-context
2475 * allocations they need
2476 */
2477
2478 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002479 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002480 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002481
Andy Green1b265272013-02-09 14:01:09 +08002482 if (info->extensions) {
2483 ext = info->extensions;
2484 while (ext->callback) {
2485 lwsl_ext(" Extension: %s\n", ext->name);
2486 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002487 (enum libwebsocket_extension_callback_reasons)m,
2488 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002489 ext++;
2490 }
Andy Greena41314f2011-05-23 10:00:03 +01002491 }
Andy Green3182ece2013-01-20 17:08:31 +08002492#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002493
Peter Hinz56885f32011-03-02 22:03:47 +00002494 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002495
2496bail:
2497 libwebsocket_context_destroy(context);
2498 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002499}
Andy Greenb45993c2010-12-18 15:13:50 +00002500
Andy Greenb45993c2010-12-18 15:13:50 +00002501/**
shysb4e800e2013-10-24 22:12:03 +08002502 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2503 * @context: pointer to struct libwebsocket_context you want set proxy to
2504 * @proxy: pointer to c string containing proxy in format address:port
2505 *
2506 * Returns 0 if proxy string was parsed and proxy was setup.
2507 * Returns -1 if @proxy is NULL or has incorrect format.
2508 *
2509 * This is only required if your OS does not provide the http_proxy
2510 * enviroment variable (eg, OSX)
2511 *
2512 * IMPORTANT! You should call this function right after creation of the
2513 * libwebsocket_context and before call to connect. If you call this
2514 * function after connect behavior is undefined.
2515 * This function will override proxy settings made on libwebsocket_context
2516 * creation with genenv() call.
2517 */
2518
2519LWS_VISIBLE int
2520libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2521{
2522 char *p;
2523
2524 if (!proxy)
2525 return -1;
2526
2527 strncpy(context->http_proxy_address, proxy,
2528 sizeof(context->http_proxy_address) - 1);
2529 context->http_proxy_address[
2530 sizeof(context->http_proxy_address) - 1] = '\0';
2531
2532 p = strchr(context->http_proxy_address, ':');
2533 if (!p) {
2534 lwsl_err("http_proxy needs to be ads:port\n");
2535
2536 return -1;
2537 }
2538 *p = '\0';
2539 context->http_proxy_port = atoi(p + 1);
2540
2541 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2542 context->http_proxy_port);
2543
2544 return 0;
2545}
2546
2547/**
Andy Greenb45993c2010-12-18 15:13:50 +00002548 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002549 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002550 * @wsi: pointer to struct websocket you want to know the protocol of
2551 *
Andy Green8f037e42010-12-19 22:13:26 +00002552 *
Andy Green6f520a52013-01-29 17:57:39 +08002553 * Some apis can act on all live connections of a given protocol,
2554 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002555 */
Andy Greenab990e42010-10-31 12:42:52 +00002556
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002557LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002558libwebsockets_get_protocol(struct libwebsocket *wsi)
2559{
2560 return wsi->protocol;
2561}
2562
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002563LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002564libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2565{
Andy Green623a98d2013-01-21 11:04:23 +08002566 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002567}
Alex Bligh49146db2011-11-07 17:19:25 +08002568
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002569LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002570libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2571{
Andy Green623a98d2013-01-21 11:04:23 +08002572 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002573}
2574
Andy Green2af4d5b2013-02-18 16:30:10 +08002575int
Alex Bligh49146db2011-11-07 17:19:25 +08002576libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2577{
Andy Green67d556c2013-02-15 22:31:55 +08002578 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002579 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002580
Alex Bligh49146db2011-11-07 17:19:25 +08002581 /* allocate the per-connection user memory (if any) */
2582
2583 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2584 wsi->user_space = malloc(
2585 wsi->protocol->per_session_data_size);
2586 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002587 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002588 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002589 }
Andy Green6ee372f2012-04-09 15:09:01 +08002590 memset(wsi->user_space, 0,
2591 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002592 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002593 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002594}
Andy Green43db0452013-01-10 19:50:35 +08002595
Andy Green0b319092013-01-19 11:17:56 +08002596static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002597{
Andy Green0b319092013-01-19 11:17:56 +08002598 char buf[300];
Patrick Gansterer92792b42014-02-26 21:37:31 +01002599 unsigned long long now;
Andy Green0b319092013-01-19 11:17:56 +08002600 int n;
2601
Andy Green0b319092013-01-19 11:17:56 +08002602 buf[0] = '\0';
2603 for (n = 0; n < LLL_COUNT; n++)
2604 if (level == (1 << n)) {
Patrick Gansterer92792b42014-02-26 21:37:31 +01002605 now = time_in_microseconds() / 100;
2606 sprintf(buf, "[%lu:%04d] %s: ", (unsigned long) now / 10000,
2607 (int)(now % 10000), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002608 break;
2609 }
Andy Greenb5b23192013-02-11 17:13:32 +08002610
Andy Green0b319092013-01-19 11:17:56 +08002611 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002612}
2613
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002614#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002615LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002616{
2617 lwsl_emit_stderr(level, line);
2618}
2619#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002620LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002621{
2622 int syslog_level = LOG_DEBUG;
2623
2624 switch (level) {
2625 case LLL_ERR:
2626 syslog_level = LOG_ERR;
2627 break;
2628 case LLL_WARN:
2629 syslog_level = LOG_WARNING;
2630 break;
2631 case LLL_NOTICE:
2632 syslog_level = LOG_NOTICE;
2633 break;
2634 case LLL_INFO:
2635 syslog_level = LOG_INFO;
2636 break;
2637 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002638 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002639}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002640#endif
Andy Greenc11db202013-01-19 11:12:16 +08002641
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002642LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002643{
Andy Greende8f27a2013-01-12 09:17:42 +08002644 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002645 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002646
2647 if (!(log_level & filter))
2648 return;
2649
Andy Green43db0452013-01-10 19:50:35 +08002650 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002651 vsnprintf(buf, sizeof(buf), format, ap);
2652 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002653 va_end(ap);
2654
Andy Green0b319092013-01-19 11:17:56 +08002655 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002656}
2657
2658/**
2659 * lws_set_log_level() - Set the logging bitfield
2660 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002661 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2662 * function to perform log string emission instead of
2663 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002664 *
Andy Greenc6511a02013-02-19 10:01:48 +08002665 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002666 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002667 */
2668
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002669LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002670 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002671{
2672 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002673 if (log_emit_function)
2674 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002675}
Andy Green92f9b162014-02-21 18:43:42 +08002676
2677int
2678interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
2679{
2680 int rc = -1;
2681#if defined(WIN32) || defined(_WIN32)
2682 /* TODO */
2683#else
2684 struct ifaddrs *ifr;
2685 struct ifaddrs *ifc;
2686 struct sockaddr_in *sin;
2687
2688 getifaddrs(&ifr);
2689 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
2690 if (ifc->ifa_addr == NULL)
2691 continue;
2692 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
2693 if (strcmp(ifc->ifa_name, ifname))
2694 continue;
2695 sin = (struct sockaddr_in *)ifc->ifa_addr;
2696 if (sin->sin_family != AF_INET)
2697 continue;
2698 memcpy(addr, sin, addrlen);
2699 rc = 0;
2700 }
2701
2702 freeifaddrs(ifr);
2703#endif
2704 return rc;
2705}
2706