blob: 520aa6b88700fb634952c501e3c7a1c65164d4c6 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010024#if defined(WIN32) || defined(_WIN32)
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010026#include <mstcpip.h>
Patrick Gansterer5b71aac2014-02-28 01:31:39 +010027#ifdef _WIN32_WCE
28#define vsnprintf _vsnprintf
29#endif
Peter Hinz56885f32011-03-02 22:03:47 +000030#else
Davidc4ef7b12013-01-12 20:39:47 +080031#ifdef LWS_BUILTIN_GETIFADDRS
32#include <getifaddrs.h>
33#else
Peter Hinz56885f32011-03-02 22:03:47 +000034#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080035#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090036#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000037#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080038#include <sys/socket.h>
39#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000040#endif
Andy Green2e24da02011-03-05 16:12:04 +000041
Patrick Gansterere5720a32014-02-28 00:57:19 +010042#ifdef HAVE_SYS_TYPES_H
Andy Green3b3fa9e2013-12-25 16:34:37 +080043#include <sys/types.h>
Patrick Gansterere5720a32014-02-28 00:57:19 +010044#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +080045
Andy Green2e24da02011-03-05 16:12:04 +000046#ifdef LWS_OPENSSL_SUPPORT
47int openssl_websocket_private_data_index;
48#endif
49
Andy Greenaa6fc442012-04-12 13:26:49 +080050#ifdef __MINGW32__
51#include "../win32port/win32helpers/websock-w32.c"
52#else
53#ifdef __MINGW64__
54#include "../win32port/win32helpers/websock-w32.c"
55#endif
56#endif
57
Andy Green7b405452013-02-01 10:50:15 +080058#ifndef LWS_BUILD_HASH
59#define LWS_BUILD_HASH "unknown-build-hash"
60#endif
Andy Green3182ece2013-01-20 17:08:31 +080061
Andy Green7c19c342013-01-19 12:18:07 +080062static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080063static void lwsl_emit_stderr(int level, const char *line);
64static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
65
Andrew Canaday9769f4f2014-03-23 13:25:07 +080066#ifdef LWS_USE_LIBEV
67#define _LWS_EV_TAG " libev"
68#else
69#define _LWS_EV_TAG
70#endif /* LWS_USE_LIBEV */
71static const char *library_version =
72 LWS_LIBRARY_VERSION " " LWS_BUILD_HASH _LWS_EV_TAG;
Andy Green7b405452013-02-01 10:50:15 +080073
Andy Greenb5b23192013-02-11 17:13:32 +080074static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080075 "ERR",
76 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080077 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080078 "INFO",
79 "DEBUG",
80 "PARSER",
81 "HEADER",
82 "EXTENSION",
83 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080084 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080085};
86
Andy Greenb5b23192013-02-11 17:13:32 +080087#ifndef LWS_NO_CLIENT
88 extern int lws_client_socket_service(
89 struct libwebsocket_context *context,
90 struct libwebsocket *wsi, struct pollfd *pollfd);
91#endif
92#ifndef LWS_NO_SERVER
93 extern int lws_server_socket_service(
94 struct libwebsocket_context *context,
95 struct libwebsocket *wsi, struct pollfd *pollfd);
96#endif
97
Andy Green3b3fa9e2013-12-25 16:34:37 +080098
Andy Green7b405452013-02-01 10:50:15 +080099/**
100 * lws_get_library_version: get version and git hash library built from
101 *
102 * returns a const char * to a string like "1.1 178d78c"
103 * representing the library version followed by the git head hash it
104 * was built from
105 */
106
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800107LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +0800108lws_get_library_version(void)
109{
110 return library_version;
111}
112
Patrick Gansterer92792b42014-02-26 21:37:31 +0100113static unsigned long long time_in_microseconds()
114{
115#if defined(WIN32) || defined(_WIN32)
116#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
117 FILETIME filetime;
118 ULARGE_INTEGER datetime;
119
120#ifdef _WIN32_WCE
121 GetCurrentFT(&filetime);
122#else
123 GetSystemTimeAsFileTime(&filetime);
124#endif
125
126 /*
127 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
128 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
129 * prevent alignment faults on 64-bit Windows).
130 */
131 memcpy(&datetime, &filetime, sizeof(datetime));
132
133 /* Windows file times are in 100s of nanoseconds. */
134 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
135#else
136 struct timeval tv;
137 gettimeofday(&tv, NULL);
138 return (tv.tv_sec * 1000000) + tv.tv_usec;
139#endif
140}
141
Patrick Gansterer5b71aac2014-02-28 01:31:39 +0100142#ifdef _WIN32_WCE
143static inline time_t time(time_t *t)
144{
145 time_t ret = time_in_microseconds() / 1000000;
146 *t = ret;
147 return ret;
148}
149#endif
150
Andy Green0d338332011-02-12 11:57:43 +0000151int
Andy Greenb5b23192013-02-11 17:13:32 +0800152insert_wsi_socket_into_fds(struct libwebsocket_context *context,
153 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000154{
Joakim Soderberg6596e4b2014-02-18 13:38:14 +0100155 struct libwebsocket_pollargs pa = { wsi->sock, POLLIN, 0 };
156
Andy Greendfb23042013-01-17 12:26:48 +0800157 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800158 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000159 return 1;
160 }
161
nononamef1624922014-01-11 13:12:34 +0800162 if (wsi->sock >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800163 lwsl_err("Socket fd %d is too high (%d)\n",
164 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800165 return 1;
166 }
167
168 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800169 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800170
Andy Greenb5b23192013-02-11 17:13:32 +0800171 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
172 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800173
Andy Green7a132792013-12-18 09:48:26 +0800174 context->protocols[0].callback(context, wsi,
175 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800176 wsi->user_space, (void *) &pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800177
Andy Greendfb23042013-01-17 12:26:48 +0800178 context->lws_lookup[wsi->sock] = wsi;
179 wsi->position_in_fds_table = context->fds_count;
180 context->fds[context->fds_count].fd = wsi->sock;
181 context->fds[context->fds_count].events = POLLIN;
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800182#ifdef LWS_USE_LIBEV
183 if (context && context->io_loop && LWS_LIBEV_ENABLED(context))
184 ev_io_start(context->io_loop, (struct ev_io *)&wsi->w_read);
185
186#endif /* LWS_USE_LIBEV */
Andy Greendfb23042013-01-17 12:26:48 +0800187 context->fds[context->fds_count++].revents = 0;
188
189 /* external POLL support via protocol 0 */
190 context->protocols[0].callback(context, wsi,
191 LWS_CALLBACK_ADD_POLL_FD,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800192 wsi->user_space, (void *) &pa, 0);
Andy Green0d338332011-02-12 11:57:43 +0000193
Andy Green7a132792013-12-18 09:48:26 +0800194 context->protocols[0].callback(context, wsi,
195 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800196 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800197
Andy Green0d338332011-02-12 11:57:43 +0000198 return 0;
199}
200
Andy Greendfb23042013-01-17 12:26:48 +0800201static int
Andy Greenb5b23192013-02-11 17:13:32 +0800202remove_wsi_socket_from_fds(struct libwebsocket_context *context,
203 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000204{
Andy Greendfb23042013-01-17 12:26:48 +0800205 int m;
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800206 struct libwebsocket_pollargs pa = { wsi->sock, 0, 0};
Andy Green0d338332011-02-12 11:57:43 +0000207
Andy Green7a132792013-12-18 09:48:26 +0800208 if (!--context->fds_count) {
209 context->protocols[0].callback(context, wsi,
210 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800211 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800212 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800213 }
Andy Green0d338332011-02-12 11:57:43 +0000214
Andy Greendfb23042013-01-17 12:26:48 +0800215 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800216 lwsl_err("Socket fd %d too high (%d)\n",
217 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800218 return 1;
219 }
Andy Green0d338332011-02-12 11:57:43 +0000220
Andy Greenb5b23192013-02-11 17:13:32 +0800221 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
222 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800223
Andy Green7a132792013-12-18 09:48:26 +0800224 context->protocols[0].callback(context, wsi,
225 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800226 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800227
Andy Greendfb23042013-01-17 12:26:48 +0800228 m = wsi->position_in_fds_table; /* replace the contents for this */
229
230 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800231 context->fds[m] = context->fds[context->fds_count];
232 /*
233 * end guy's fds_lookup entry remains unchanged
234 * (still same fd pointing to same wsi)
235 */
Andy Greendfb23042013-01-17 12:26:48 +0800236 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800237 context->lws_lookup[context->fds[context->fds_count].fd]->
238 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800239 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800240 context->lws_lookup[wsi->sock] = NULL;
241 /* removed wsi has no position any more */
242 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800243
244do_ext:
245 /* remove also from external POLL support via protocol 0 */
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800246 if (wsi->sock) {
Andy Greendfb23042013-01-17 12:26:48 +0800247 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800248 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800249 (void *) &pa, 0);
250 }
Andy Green7a132792013-12-18 09:48:26 +0800251 context->protocols[0].callback(context, wsi,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800252 LWS_CALLBACK_UNLOCK_POLL,
253 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800254 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000255}
256
Andy Green32375b72011-02-19 08:32:53 +0000257
Andy Green8f037e42010-12-19 22:13:26 +0000258void
Peter Hinz56885f32011-03-02 22:03:47 +0000259libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000260 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000261{
Andy Greenb45993c2010-12-18 15:13:50 +0000262 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000263 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000264 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
265 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800266#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000267 int ret;
268 int m;
269 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100270 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800271#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000272
Andy Green4b6fbe12011-02-14 08:03:48 +0000273 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000274 return;
275
Andy Green62c54d22011-02-14 09:14:25 +0000276 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000277
Andy Green62c54d22011-02-14 09:14:25 +0000278 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000279 return;
280
Andy Green22524a62013-02-15 22:48:58 +0800281 /* we tried the polite way... */
282 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
283 goto just_kill_connection;
284
Andy Green623a98d2013-01-21 11:04:23 +0800285 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000286
Andy Green5dc62ea2013-09-20 20:26:12 +0800287 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
288 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
289
290 context->protocols[0].callback(context, wsi,
291 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
292
293 free(wsi->u.hdr.ah);
294 goto just_kill_connection;
295 }
296
297
kapejodce64fb02013-11-19 13:38:16 +0100298 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
299 if (wsi->u.http.post_buffer) {
300 free(wsi->u.http.post_buffer);
301 wsi->u.http.post_buffer = NULL;
302 }
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100303 if (wsi->u.http.fd != LWS_INVALID_FILE) {
304 lwsl_debug("closing http file\n");
305#if defined(WIN32) || defined(_WIN32)
306 CloseHandle(wsi->u.http.fd);
307#else
kapejodce64fb02013-11-19 13:38:16 +0100308 close(wsi->u.http.fd);
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100309#endif
310 wsi->u.http.fd = LWS_INVALID_FILE;
kapejodce64fb02013-11-19 13:38:16 +0100311 context->protocols[0].callback(context, wsi,
312 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
313 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800314 }
315
Andy Green3182ece2013-01-20 17:08:31 +0800316#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000317 /*
Andy Green68b45042011-05-25 21:41:57 +0100318 * are his extensions okay with him closing? Eg he might be a mux
319 * parent and just his ch1 aspect is closing?
320 */
321
Andy Green68b45042011-05-25 21:41:57 +0100322 for (n = 0; n < wsi->count_active_extensions; n++) {
323 if (!wsi->active_extensions[n]->callback)
324 continue;
325
326 m = wsi->active_extensions[n]->callback(context,
327 wsi->active_extensions[n], wsi,
328 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
329 wsi->active_extensions_user[n], NULL, 0);
330
331 /*
332 * if somebody vetoed actually closing him at this time....
333 * up to the extension to track the attempted close, let's
334 * just bail
335 */
336
337 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800338 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100339 return;
340 }
341 }
342
Andy Green68b45042011-05-25 21:41:57 +0100343 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000344 * flush any tx pending from extensions, since we may send close packet
345 * if there are problems with send, just nuke the connection
346 */
347
348 ret = 1;
349 while (ret == 1) {
350
351 /* default to nobody has more to spill */
352
353 ret = 0;
354 eff_buf.token = NULL;
355 eff_buf.token_len = 0;
356
357 /* show every extension the new incoming data */
358
359 for (n = 0; n < wsi->count_active_extensions; n++) {
360 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000361 wsi->protocol->owning_server,
362 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000363 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
364 wsi->active_extensions_user[n], &eff_buf, 0);
365 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800366 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000367 goto just_kill_connection;
368 }
369 if (m)
370 /*
371 * at least one extension told us he has more
372 * to spill, so we will go around again after
373 */
374 ret = 1;
375 }
376
377 /* assuming they left us something to send, send it */
378
379 if (eff_buf.token_len)
380 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800381 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800382 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000383 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800384 }
Andy Greenc44159f2011-03-07 07:08:18 +0000385 }
Andy Green3182ece2013-01-20 17:08:31 +0800386#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000387
388 /*
Andy Greenda527df2011-03-07 07:08:12 +0000389 * signal we are closing, libsocket_write will
390 * add any necessary version-specific stuff. If the write fails,
391 * no worries we are closing anyway. If we didn't initiate this
392 * close, then our state has been changed to
393 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
394 *
395 * Likewise if it's a second call to close this connection after we
396 * sent the close indication to the peer already, we are in state
397 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
398 */
399
400 if (old_state == WSI_STATE_ESTABLISHED &&
401 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100402
Andy Green43db0452013-01-10 19:50:35 +0800403 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100404
Andy Greenfdd305a2013-02-11 14:32:48 +0800405 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800406 memset(buf, 0, sizeof(buf));
407 n = libwebsocket_write(wsi,
408 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000409 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800410 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000411 /*
412 * we have sent a nice protocol level indication we
413 * now wish to close, we should not send anything more
414 */
415
416 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
417
Andy Greenb5b23192013-02-11 17:13:32 +0800418 /*
419 * ...and we should wait for a reply for a bit
420 * out of politeness
421 */
Andy Greenda527df2011-03-07 07:08:12 +0000422
423 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800424 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000425
Andy Green43db0452013-01-10 19:50:35 +0800426 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000427
428 return;
429 }
430
Andy Greenb5b23192013-02-11 17:13:32 +0800431 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800432
Andy Greenda527df2011-03-07 07:08:12 +0000433 /* else, the send failed and we should just hang up */
434 }
435
Andy Greenc44159f2011-03-07 07:08:18 +0000436just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100437
Andy Greenb5b23192013-02-11 17:13:32 +0800438 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100439
Andy Greenda527df2011-03-07 07:08:12 +0000440 /*
441 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800442 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000443 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000444
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800445#ifdef LWS_USE_LIBEV
446 if (LWS_LIBEV_ENABLED(context)) {
447 ev_io_stop(context->io_loop,(struct ev_io *)&wsi->w_read);
448 ev_io_stop(context->io_loop,(struct ev_io *)&wsi->w_write);
449 }
450#endif /* LWS_USE_LIBEV */
451
Andy Greendfb23042013-01-17 12:26:48 +0800452 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000453
Andy Green251f6fa2010-11-03 11:13:06 +0000454 wsi->state = WSI_STATE_DEAD_SOCKET;
455
Andy Greenb5b23192013-02-11 17:13:32 +0800456 if ((old_state == WSI_STATE_ESTABLISHED ||
457 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800458 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
459
460 if (wsi->u.ws.rx_user_buffer) {
461 free(wsi->u.ws.rx_user_buffer);
462 wsi->u.ws.rx_user_buffer = NULL;
463 }
464 if (wsi->u.ws.rxflow_buffer) {
465 free(wsi->u.ws.rxflow_buffer);
466 wsi->u.ws.rxflow_buffer = NULL;
467 }
Andy Green2764eba2013-12-09 14:16:17 +0800468 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800469 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800470 free(wsi->truncated_send_malloc);
471 wsi->truncated_send_malloc = NULL;
Andy Greene254d952014-03-23 11:41:15 +0800472 wsi->truncated_send_len = 0;
Andy Green1f4267b2013-10-17 08:09:19 +0800473 }
Andy Green54495112013-02-06 21:10:16 +0900474 }
475
Andy Green4b6fbe12011-02-14 08:03:48 +0000476 /* tell the user it's all over for this guy */
477
Andy Greend4302732011-02-28 07:45:29 +0000478 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800479 ((old_state == WSI_STATE_ESTABLISHED) ||
480 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
481 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800482 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000483 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000484 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800485 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
486 lwsl_debug("calling back CLOSED_HTTP\n");
487 context->protocols[0].callback(context, wsi,
488 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800489 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800490 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000491
Andy Green3182ece2013-01-20 17:08:31 +0800492#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000493 /* deallocate any active extension contexts */
494
495 for (n = 0; n < wsi->count_active_extensions; n++) {
496 if (!wsi->active_extensions[n]->callback)
497 continue;
498
Andy Green46c2ea02011-03-22 09:04:01 +0000499 wsi->active_extensions[n]->callback(context,
500 wsi->active_extensions[n], wsi,
501 LWS_EXT_CALLBACK_DESTROY,
502 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000503
504 free(wsi->active_extensions_user[n]);
505 }
506
Andy Greena41314f2011-05-23 10:00:03 +0100507 /*
508 * inform all extensions in case they tracked this guy out of band
509 * even though not active on him specifically
510 */
511
512 ext = context->extensions;
513 while (ext && ext->callback) {
514 ext->callback(context, ext, wsi,
515 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
516 NULL, NULL, 0);
517 ext++;
518 }
Andy Green3182ece2013-01-20 17:08:31 +0800519#endif
Andy Greena41314f2011-05-23 10:00:03 +0100520
Andy Green43db0452013-01-10 19:50:35 +0800521/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000522
Andy Green3faa9c72010-11-08 17:03:03 +0000523#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000524 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000525 n = SSL_get_fd(wsi->ssl);
526 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800527 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000528 SSL_free(wsi->ssl);
529 } else {
530#endif
Andy Green0303db42013-01-17 14:46:43 +0800531 if (wsi->sock) {
532 n = shutdown(wsi->sock, SHUT_RDWR);
533 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800534 lwsl_debug("closing: shutdown returned %d\n",
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100535 LWS_ERRNO);
Andy Green3fc2c652013-01-14 15:35:02 +0800536
Andy Green0303db42013-01-17 14:46:43 +0800537 n = compatible_close(wsi->sock);
538 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800539 lwsl_debug("closing: close returned %d\n",
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100540 LWS_ERRNO);
Andy Green0303db42013-01-17 14:46:43 +0800541 }
Andy Green3faa9c72010-11-08 17:03:03 +0000542#ifdef LWS_OPENSSL_SUPPORT
543 }
544#endif
Andy Green76b6ea12014-02-15 19:25:50 +0800545
546 /* outermost destroy notification for wsi (user_space still intact) */
547 context->protocols[0].callback(context, wsi,
548 LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0);
549
Andy Greenb5b23192013-02-11 17:13:32 +0800550 if (wsi->protocol && wsi->protocol->per_session_data_size &&
551 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000552 free(wsi->user_space);
553
Andy Green251f6fa2010-11-03 11:13:06 +0000554 free(wsi);
555}
556
Andy Green07034092011-02-13 08:37:12 +0000557/**
558 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800559 * @context: Libwebsockets context
560 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000561 * @fd: Connection socket descriptor
562 * @name: Buffer to take client address name
563 * @name_len: Length of client address name buffer
564 * @rip: Buffer to take client address IP qotted quad
565 * @rip_len: Length of client address IP buffer
566 *
567 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800568 * the client connected with socket descriptor @fd. Names may be
569 * truncated if there is not enough room. If either cannot be
570 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000571 */
572
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800573LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800574libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
575 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000576 char *rip, int rip_len)
577{
Bob Robertsac049112013-04-25 09:16:30 +0800578 socklen_t len;
Andy Green055f2972014-03-24 16:09:25 +0800579#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +0800580 struct sockaddr_in6 sin6;
581#endif
582 struct sockaddr_in sin4;
Andy Green07034092011-02-13 08:37:12 +0000583 struct hostent *host;
584 struct hostent *host1;
585 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000586 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000587 int n;
David Galeanocb193682013-01-09 15:29:00 +0800588#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800589 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800590#endif
James Devine3f13ea22014-03-24 16:09:25 +0800591 int ret = -1;
Andy Green07034092011-02-13 08:37:12 +0000592
593 rip[0] = '\0';
594 name[0] = '\0';
595
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800596 lws_latency_pre(context, wsi);
597
Andy Green055f2972014-03-24 16:09:25 +0800598#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +0800599 if (LWS_IPV6_ENABLED(context)) {
Andy Green6ee372f2012-04-09 15:09:01 +0800600
James Devine3f13ea22014-03-24 16:09:25 +0800601 len = sizeof(sin6);
602 if (getpeername(fd, (struct sockaddr *) &sin6, &len) < 0) {
603 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
604 goto bail;
Andy Green7627af52011-03-09 15:13:52 +0000605 }
James Devine3f13ea22014-03-24 16:09:25 +0800606
607 if (inet_ntop(AF_INET6, &sin6.sin6_addr, rip, rip_len) == NULL) {
608 perror("inet_ntop");
609 goto bail;
610 }
611
612 // Strip off the IPv4 to IPv6 header if one exists
613 if (strncmp(rip, "::ffff:", 7) == 0) {
614 memmove(rip, rip + 7, strlen(rip) - 6);
615 }
616
617 getnameinfo((struct sockaddr *)&sin6,
618 sizeof(struct sockaddr_in6), name,
619 name_len, NULL, 0, 0);
620
621 } else
Peter Hinzbb45a902011-03-10 18:14:01 +0000622#endif
James Devine3f13ea22014-03-24 16:09:25 +0800623 {
624 len = sizeof(sin4);
625 if (getpeername(fd, (struct sockaddr *) &sin4, &len) < 0) {
626 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
627 goto bail;
628 }
629 host = gethostbyaddr((char *) &sin4.sin_addr,
630 sizeof(sin4.sin_addr), AF_INET);
631 if (host == NULL) {
632 lwsl_warn("gethostbyaddr: %s\n", strerror(LWS_ERRNO));
633 goto bail;
634 }
635
636 strncpy(name, host->h_name, name_len);
637 name[name_len - 1] = '\0';
638
639 host1 = gethostbyname(host->h_name);
640 if (host1 == NULL)
641 goto bail;
642 p = (unsigned char *)host1;
643 n = 0;
644 while (p != NULL) {
645 p = (unsigned char *)host1->h_addr_list[n++];
646 if (p == NULL)
647 continue;
648 if ((host1->h_addrtype != AF_INET)
649 #ifdef AF_LOCAL
650 && (host1->h_addrtype != AF_LOCAL)
651 #endif
652 )
653 continue;
654
655 if (host1->h_addrtype == AF_INET)
656 sprintf(ip, "%u.%u.%u.%u",
657 p[0], p[1], p[2], p[3]);
658 #ifdef AF_LOCAL
659 else {
660 un = (struct sockaddr_un *)p;
661 strncpy(ip, un->sun_path, sizeof(ip) - 1);
662 ip[sizeof(ip) - 1] = '\0';
663 }
664 #endif
665 p = NULL;
666 strncpy(rip, ip, rip_len);
667 rip[rip_len - 1] = '\0';
668 }
Andy Green07034092011-02-13 08:37:12 +0000669 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800670
671 ret = 0;
672bail:
673 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000674}
Andy Green9f990342011-02-12 11:57:45 +0000675
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800676LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000677 void *buf, int len)
678{
679 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800680 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000681
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100682#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000683 for (n = 0; n < len; n++)
684 p[n] = (unsigned char)rand();
685#else
686 n = read(context->fd_random, p, len);
687#endif
688
689 return n;
690}
691
Andy Greena690cd02013-02-09 12:25:31 +0800692int lws_set_socket_options(struct libwebsocket_context *context, int fd)
693{
694 int optval = 1;
695 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100696#if defined(WIN32) || defined(_WIN32)
Patrick Gansterera6163002014-03-27 11:21:41 +0100697 u_long optl = 1;
Andy Greena690cd02013-02-09 12:25:31 +0800698#endif
699#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
700 struct protoent *tcp_proto;
701#endif
702
703 if (context->ka_time) {
704 /* enable keepalive on this socket */
705 optval = 1;
706 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
707 (const void *)&optval, optlen) < 0)
708 return 1;
709
Bob Robertsac049112013-04-25 09:16:30 +0800710#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800711
712 /*
713 * didn't find a way to set these per-socket, need to
714 * tune kernel systemwide values
715 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100716#elif WIN32
717 {
718 DWORD dwBytesRet;
719 struct tcp_keepalive alive;
720 alive.onoff = TRUE;
721 alive.keepalivetime = context->ka_time;
722 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800723
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100724 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
725 NULL, 0, &dwBytesRet, NULL, NULL))
726 return 1;
727 }
Andy Greena47865f2013-02-10 09:39:47 +0800728#else
Andy Greena690cd02013-02-09 12:25:31 +0800729 /* set the keepalive conditions we want on it too */
730 optval = context->ka_time;
731 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
732 (const void *)&optval, optlen) < 0)
733 return 1;
734
Larry Hayesbb66ac62013-02-22 09:16:20 +0800735 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800736 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
737 (const void *)&optval, optlen) < 0)
738 return 1;
739
Larry Hayesbb66ac62013-02-22 09:16:20 +0800740 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800741 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
742 (const void *)&optval, optlen) < 0)
743 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800744#endif
Andy Greena690cd02013-02-09 12:25:31 +0800745 }
746
747 /* Disable Nagle */
748 optval = 1;
749#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
750 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
751#else
752 tcp_proto = getprotobyname("TCP");
753 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
754#endif
755
756 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100757#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800758 ioctlsocket(fd, FIONBIO, &optl);
759#else
760 fcntl(fd, F_SETFL, O_NONBLOCK);
761#endif
762
763 return 0;
764}
765
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800766LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000767{
768 struct pollfd fds;
769
Andy Green2764eba2013-12-09 14:16:17 +0800770 /* treat the fact we got a truncated send pending as if we're choked */
Andy Greene254d952014-03-23 11:41:15 +0800771 if (wsi->truncated_send_len)
Andy Green2764eba2013-12-09 14:16:17 +0800772 return 1;
773
Andy Green95a7b5d2011-03-06 10:29:39 +0000774 fds.fd = wsi->sock;
775 fds.events = POLLOUT;
776 fds.revents = 0;
777
778 if (poll(&fds, 1, 0) != 1)
779 return 1;
780
781 if ((fds.revents & POLLOUT) == 0)
782 return 1;
783
784 /* okay to send another packet without blocking */
785
786 return 0;
787}
788
Andy Greena41314f2011-05-23 10:00:03 +0100789int
Andy Green3b84c002011-03-06 13:14:42 +0000790lws_handle_POLLOUT_event(struct libwebsocket_context *context,
791 struct libwebsocket *wsi, struct pollfd *pollfd)
792{
Andy Green3b84c002011-03-06 13:14:42 +0000793 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800794
Andy Green3182ece2013-01-20 17:08:31 +0800795#ifndef LWS_NO_EXTENSIONS
796 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000797 int ret;
798 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100799 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000800
Andy Green1f4267b2013-10-17 08:09:19 +0800801 /* pending truncated sends have uber priority */
802
Andy Greene254d952014-03-23 11:41:15 +0800803 if (wsi->truncated_send_len) {
Andy Green2764eba2013-12-09 14:16:17 +0800804 lws_issue_raw(wsi, wsi->truncated_send_malloc +
805 wsi->truncated_send_offset,
806 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800807 /* leave POLLOUT active either way */
808 return 0;
809 }
810
Andy Greena41314f2011-05-23 10:00:03 +0100811 for (n = 0; n < wsi->count_active_extensions; n++) {
812 if (!wsi->active_extensions[n]->callback)
813 continue;
814
815 m = wsi->active_extensions[n]->callback(context,
816 wsi->active_extensions[n], wsi,
817 LWS_EXT_CALLBACK_IS_WRITEABLE,
818 wsi->active_extensions_user[n], NULL, 0);
819 if (m > handled)
820 handled = m;
821 }
822
823 if (handled == 1)
824 goto notify_action;
825
826 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000827 goto user_service;
828
829 /*
830 * check in on the active extensions, see if they
831 * had pending stuff to spill... they need to get the
832 * first look-in otherwise sequence will be disordered
833 *
834 * NULL, zero-length eff_buf means just spill pending
835 */
836
837 ret = 1;
838 while (ret == 1) {
839
840 /* default to nobody has more to spill */
841
842 ret = 0;
843 eff_buf.token = NULL;
844 eff_buf.token_len = 0;
845
846 /* give every extension a chance to spill */
847
848 for (n = 0; n < wsi->count_active_extensions; n++) {
849 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000850 wsi->protocol->owning_server,
851 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000852 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
853 wsi->active_extensions_user[n], &eff_buf, 0);
854 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800855 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000856 return -1;
857 }
858 if (m)
859 /*
860 * at least one extension told us he has more
861 * to spill, so we will go around again after
862 */
863 ret = 1;
864 }
865
866 /* assuming they gave us something to send, send it */
867
868 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800869 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
870 eff_buf.token_len);
871 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000872 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800873 /*
874 * Keep amount spilled small to minimize chance of this
875 */
876 if (n != eff_buf.token_len) {
877 lwsl_err("Unable to spill ext %d vs %s\n",
878 eff_buf.token_len, n);
879 return -1;
880 }
Andy Green3b84c002011-03-06 13:14:42 +0000881 } else
882 continue;
883
884 /* no extension has more to spill */
885
886 if (!ret)
887 continue;
888
889 /*
890 * There's more to spill from an extension, but we just sent
891 * something... did that leave the pipe choked?
892 */
893
894 if (!lws_send_pipe_choked(wsi))
895 /* no we could add more */
896 continue;
897
Andy Green43db0452013-01-10 19:50:35 +0800898 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000899
900 /*
901 * Yes, he's choked. Leave the POLLOUT masked on so we will
902 * come back here when he is unchoked. Don't call the user
903 * callback to enforce ordering of spilling, he'll get called
904 * when we come back here and there's nothing more to spill.
905 */
906
907 return 0;
908 }
909
910 wsi->extension_data_pending = 0;
911
912user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800913#endif
Andy Green3b84c002011-03-06 13:14:42 +0000914 /* one shot */
915
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800916 if (pollfd) {
Andy Green91f19d82013-12-21 11:18:34 +0800917 lws_change_pollfd(wsi, POLLOUT, 0);
Andrew Canaday9769f4f2014-03-23 13:25:07 +0800918#ifdef LWS_USE_LIBEV
919 if (LWS_LIBEV_ENABLED(context))
920 ev_io_stop(context->io_loop,
921 (struct ev_io *)&wsi->w_write);
922#endif /* LWS_USE_LIBEV */
923 }
Andy Green3182ece2013-01-20 17:08:31 +0800924#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100925notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800926#endif
Andy Green3b84c002011-03-06 13:14:42 +0000927
Andy Green9e4c2b62011-03-07 20:47:39 +0000928 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
929 n = LWS_CALLBACK_CLIENT_WRITEABLE;
930 else
931 n = LWS_CALLBACK_SERVER_WRITEABLE;
932
Andy Greenb8b247d2013-01-22 07:20:08 +0800933 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800934 wsi, (enum libwebsocket_callback_reasons) n,
935 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000936}
937
938
939
Andy Green1c6e1422013-02-20 19:11:31 +0800940int
Andy Greena41314f2011-05-23 10:00:03 +0100941libwebsocket_service_timeout_check(struct libwebsocket_context *context,
942 struct libwebsocket *wsi, unsigned int sec)
943{
Andy Green3182ece2013-01-20 17:08:31 +0800944#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100945 int n;
946
947 /*
948 * if extensions want in on it (eg, we are a mux parent)
949 * give them a chance to service child timeouts
950 */
951
952 for (n = 0; n < wsi->count_active_extensions; n++)
953 wsi->active_extensions[n]->callback(
954 context, wsi->active_extensions[n],
955 wsi, LWS_EXT_CALLBACK_1HZ,
956 wsi->active_extensions_user[n], NULL, sec);
957
Andy Green3182ece2013-01-20 17:08:31 +0800958#endif
Andy Greena41314f2011-05-23 10:00:03 +0100959 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800960 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800961
Andy Greena41314f2011-05-23 10:00:03 +0100962 /*
963 * if we went beyond the allowed time, kill the
964 * connection
965 */
966
967 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800968 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100969 libwebsocket_close_and_free_session(context,
970 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800971 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100972 }
Andy Green1c6e1422013-02-20 19:11:31 +0800973
974 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100975}
976
Andy Green9f990342011-02-12 11:57:45 +0000977/**
978 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000979 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000980 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800981 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000982 *
Andy Green75006172013-01-22 12:32:11 +0800983 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800984 * services it according to the state of the associated
985 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800986 *
987 * The one call deals with all "service" that might happen on a socket
988 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800989 *
990 * If a pollfd says it has something, you can just pass it to
991 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
992 * If it sees it is a lws socket, the traffic will be handled and
993 * pollfd->revents will be zeroed now.
994 *
995 * If the socket is foreign to lws, it leaves revents alone. So you can
996 * see if you should service yourself by checking the pollfd revents
997 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000998 */
999
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001000LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001001libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +00001002 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +00001003{
Andy Greena1ce6be2013-01-18 11:43:21 +08001004 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +00001005 int n;
Andy Green0d338332011-02-12 11:57:43 +00001006 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +08001007 int listen_socket_fds_index = 0;
Patrick Gansterer92792b42014-02-26 21:37:31 +01001008 time_t now;
Andy Green1c6e1422013-02-20 19:11:31 +08001009 int timed_out = 0;
1010 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +08001011 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +08001012
Andy Green3182ece2013-01-20 17:08:31 +08001013#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +00001014 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +08001015#endif
Andy Green98a717c2011-03-06 13:14:15 +00001016 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +08001017
1018 if (context->listen_service_fd)
1019 listen_socket_fds_index = context->lws_lookup[
1020 context->listen_service_fd]->position_in_fds_table;
1021
Andy Greena71eafc2011-02-14 17:59:43 +00001022 /*
1023 * you can call us with pollfd = NULL to just allow the once-per-second
1024 * global timeout checks; if less than a second since the last check
1025 * it returns immediately then.
1026 */
1027
Patrick Gansterer92792b42014-02-26 21:37:31 +01001028 time(&now);
Andy Greena71eafc2011-02-14 17:59:43 +00001029
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001030 /* TODO: if using libev, we should probably use timeout watchers... */
Patrick Gansterer92792b42014-02-26 21:37:31 +01001031 if (context->last_timeout_check_s != now) {
1032 context->last_timeout_check_s = now;
Andy Greena71eafc2011-02-14 17:59:43 +00001033
Joakim Soderberg4c531232013-02-06 15:26:58 +09001034 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +08001035 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +08001036 if (context->started_with_parent &&
1037 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +08001038 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +09001039 #endif
Andy Green24cba922013-01-19 13:56:10 +08001040
Andy Greena71eafc2011-02-14 17:59:43 +00001041 /* global timeout check once per second */
1042
Andy Green1c6e1422013-02-20 19:11:31 +08001043 if (pollfd)
1044 our_fd = pollfd->fd;
1045
Peter Hinz56885f32011-03-02 22:03:47 +00001046 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +08001047 m = context->fds[n].fd;
1048 wsi = context->lws_lookup[m];
1049 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +08001050 continue;
Andy Green1c6e1422013-02-20 19:11:31 +08001051
Patrick Gansterer92792b42014-02-26 21:37:31 +01001052 if (libwebsocket_service_timeout_check(context, wsi, now))
Andy Green1c6e1422013-02-20 19:11:31 +08001053 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001054 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +08001055 /* it was the guy we came to service! */
1056 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001057 /* mark as handled */
1058 pollfd->revents = 0;
1059 }
Andy Greena71eafc2011-02-14 17:59:43 +00001060 }
1061 }
1062
Andy Green1c6e1422013-02-20 19:11:31 +08001063 /* the socket we came to service timed out, nothing to do */
1064 if (timed_out)
1065 return 0;
1066
Andy Greena71eafc2011-02-14 17:59:43 +00001067 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +00001068 if (pollfd == NULL)
1069 return 0;
1070
1071 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001072 wsi = context->lws_lookup[pollfd->fd];
1073 if (wsi == NULL)
1074 /* not lws connection ... leave revents alone and return */
1075 return 0;
1076
1077 /*
1078 * so that caller can tell we handled, past here we need to
1079 * zero down pollfd->revents after handling
1080 */
1081
Andy Green65b0e912013-01-16 07:59:47 +08001082 /*
1083 * deal with listen service piggybacking
1084 * every listen_service_modulo services of other fds, we
1085 * sneak one in to service the listen socket if there's anything waiting
1086 *
1087 * To handle connection storms, as found in ab, if we previously saw a
1088 * pending connection here, it causes us to check again next time.
1089 */
1090
Andy Greenb5b23192013-02-11 17:13:32 +08001091 if (context->listen_service_fd && pollfd !=
1092 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +08001093 context->listen_service_count++;
1094 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +08001095 context->listen_service_count ==
1096 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001097 context->listen_service_count = 0;
1098 m = 1;
1099 if (context->listen_service_extraseen > 5)
1100 m = 2;
1101 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001102 /*
1103 * even with extpoll, we prepared this
1104 * internal fds for listen
1105 */
1106 n = poll(&context->fds[listen_socket_fds_index],
1107 1, 0);
1108 if (n > 0) { /* there's a conn waiting for us */
1109 libwebsocket_service_fd(context,
1110 &context->
1111 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001112 context->listen_service_extraseen++;
1113 } else {
1114 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001115 context->
1116 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001117 break;
1118 }
1119 }
1120 }
1121
1122 }
1123
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001124 /* handle session socket closed */
1125
1126 if ((!(pollfd->revents & POLLIN)) &&
1127 (pollfd->revents & (POLLERR | POLLHUP))) {
1128
1129 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1130 (void *)wsi, pollfd->fd);
1131
1132 goto close_and_handled;
1133 }
1134
Andy Green65b0e912013-01-16 07:59:47 +08001135 /* okay, what we came here to do... */
1136
Andy Green0d338332011-02-12 11:57:43 +00001137 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001138
Andy Greena1ce6be2013-01-18 11:43:21 +08001139#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001140 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001141 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001142 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001143 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001144 n = lws_server_socket_service(context, wsi, pollfd);
1145 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001146#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001147
Andy Green0d338332011-02-12 11:57:43 +00001148 case LWS_CONNMODE_WS_SERVING:
1149 case LWS_CONNMODE_WS_CLIENT:
1150
Andy Green0d338332011-02-12 11:57:43 +00001151 /* the guy requested a callback when it was OK to write */
1152
Andy Greenda527df2011-03-07 07:08:12 +00001153 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001154 wsi->state == WSI_STATE_ESTABLISHED &&
1155 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001156 lwsl_info("libwebsocket_service_fd: closing\n");
1157 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001158 }
Andy Green0d338332011-02-12 11:57:43 +00001159
Andy Greenca0a1292013-03-16 11:24:23 +08001160 if (wsi->u.ws.rxflow_buffer &&
1161 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1162 lwsl_info("draining rxflow\n");
1163 /* well, drain it */
1164 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1165 wsi->u.ws.rxflow_pos;
1166 eff_buf.token_len = wsi->u.ws.rxflow_len -
1167 wsi->u.ws.rxflow_pos;
1168 draining_flow = 1;
1169 goto drain;
1170 }
1171
Andy Green0d338332011-02-12 11:57:43 +00001172 /* any incoming data ready? */
1173
1174 if (!(pollfd->revents & POLLIN))
1175 break;
1176
Andy Greenb45993c2010-12-18 15:13:50 +00001177#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001178read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001179 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001180 eff_buf.token_len = SSL_read(wsi->ssl,
1181 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001182 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001183 if (!eff_buf.token_len) {
1184 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001185 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001186 ERR_error_string(n,
1187 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001188 }
1189 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001190#endif
Andy Greene84652c2013-02-08 13:01:02 +08001191 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001192 context->service_buffer,
1193 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001194
Andy Green98a717c2011-03-06 13:14:15 +00001195 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001196 lwsl_debug("service_fd read ret = %d, errno = %d\n",
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01001197 eff_buf.token_len, LWS_ERRNO);
1198 if (LWS_ERRNO != LWS_EINTR && LWS_ERRNO != LWS_EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001199 goto close_and_handled;
1200 n = 0;
1201 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001202 }
Andy Green98a717c2011-03-06 13:14:15 +00001203 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001204 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001205 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001206 }
1207
Andy Green98a717c2011-03-06 13:14:15 +00001208 /*
1209 * give any active extensions a chance to munge the buffer
1210 * before parse. We pass in a pointer to an lws_tokens struct
1211 * prepared with the default buffer and content length that's in
1212 * there. Rather than rewrite the default buffer, extensions
1213 * that expect to grow the buffer can adapt .token to
1214 * point to their own per-connection buffer in the extension
1215 * user allocation. By default with no extensions or no
1216 * extension callback handling, just the normal input buffer is
1217 * used then so it is efficient.
1218 */
Andy Greenb45993c2010-12-18 15:13:50 +00001219
Andy Greene84652c2013-02-08 13:01:02 +08001220 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001221drain:
Andy Green3182ece2013-01-20 17:08:31 +08001222#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001223 more = 1;
1224 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001225
Andy Green98a717c2011-03-06 13:14:15 +00001226 more = 0;
1227
1228 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001229 m = wsi->active_extensions[n]->callback(context,
1230 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001231 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001232 wsi->active_extensions_user[n],
1233 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001234 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001235 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001236 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001237 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001238 }
1239 if (m)
1240 more = 1;
1241 }
Andy Green3182ece2013-01-20 17:08:31 +08001242#endif
Andy Green98a717c2011-03-06 13:14:15 +00001243 /* service incoming data */
1244
1245 if (eff_buf.token_len) {
1246 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001247 (unsigned char *)eff_buf.token,
1248 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001249 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001250 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001251 n = 0;
1252 goto handled;
1253 }
Andy Green98a717c2011-03-06 13:14:15 +00001254 }
Andy Green3182ece2013-01-20 17:08:31 +08001255#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001256 eff_buf.token = NULL;
1257 eff_buf.token_len = 0;
1258 }
Andy Green3182ece2013-01-20 17:08:31 +08001259#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001260 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1261 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1262 lwsl_info("flow buffer: drained\n");
1263 free(wsi->u.ws.rxflow_buffer);
1264 wsi->u.ws.rxflow_buffer = NULL;
1265 /* having drained the rxflow buffer, can rearm POLLIN */
1266 _libwebsocket_rx_flow_control(wsi);
1267 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001268
1269#ifdef LWS_OPENSSL_SUPPORT
1270 if (wsi->ssl && SSL_pending(wsi->ssl))
1271 goto read_pending;
1272#endif
Andy Green98a717c2011-03-06 13:14:15 +00001273 break;
Andy Green76f61e72013-01-16 11:53:05 +08001274
1275 default:
Andy Green03674a62013-01-16 11:47:40 +08001276#ifdef LWS_NO_CLIENT
1277 break;
1278#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001279 n = lws_client_socket_service(context, wsi, pollfd);
1280 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001281#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001282 }
1283
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001284 n = 0;
1285 goto handled;
1286
1287close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001288 libwebsocket_close_and_free_session(context, wsi,
1289 LWS_CLOSE_STATUS_NOSTATUS);
1290 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001291
1292handled:
1293 pollfd->revents = 0;
1294 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001295}
1296
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001297#ifdef LWS_USE_LIBEV
1298LWS_VISIBLE void
1299libwebsocket_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
1300{
1301 struct pollfd eventfd;
1302 struct lws_io_watcher *lws_io = (struct lws_io_watcher*)watcher;
1303 struct libwebsocket_context *context = lws_io->context;
1304
1305 if (revents & EV_ERROR)
1306 return;
1307
1308 eventfd.fd = watcher->fd;
1309 eventfd.revents = EV_NONE;
1310 if (revents & EV_READ)
1311 eventfd.revents |= POLLIN;
1312
1313 if (revents & EV_WRITE)
1314 eventfd.revents |= POLLOUT;
1315
1316 libwebsocket_service_fd(context,&eventfd);
1317}
1318
1319LWS_VISIBLE void
1320libwebsocket_sigint_cb(
1321 struct ev_loop *loop, struct ev_signal* watcher, int revents)
1322{
1323 ev_break(loop, EVBREAK_ALL);
1324}
1325#endif /* LWS_USE_LIBEV */
1326
Andy Green0d338332011-02-12 11:57:43 +00001327
Andy Green6964bb52011-01-23 16:50:33 +00001328/**
1329 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001330 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001331 *
1332 * This function closes any active connections and then frees the
1333 * context. After calling this, any further use of the context is
1334 * undefined.
1335 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001336LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001337libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001338{
Andy Green0d338332011-02-12 11:57:43 +00001339 int n;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001340#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001341 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001342 struct libwebsocket_extension *ext;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001343#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001344 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001345
Andy Greend636e352013-01-29 12:36:17 +08001346#ifdef LWS_LATENCY
1347 if (context->worst_latency_info[0])
1348 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1349#endif
1350
Andy Greendfb23042013-01-17 12:26:48 +08001351 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001352 struct libwebsocket *wsi =
1353 context->lws_lookup[context->fds[n].fd];
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001354 if (!wsi)
1355 continue;
Andy Greendfb23042013-01-17 12:26:48 +08001356 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001357 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1358 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001359 }
Andy Green6964bb52011-01-23 16:50:33 +00001360
arnaudviala7c6f26c2014-02-15 14:39:40 +08001361#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001362 /*
1363 * give all extensions a chance to clean up any per-context
1364 * allocations they might have made
1365 */
1366
1367 ext = context->extensions;
1368 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1369 if (context->listen_port)
1370 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001371 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001372 ext->callback(context, ext, NULL,
1373 (enum libwebsocket_extension_callback_reasons)m,
1374 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001375 ext++;
1376 }
arnaudviala7c6f26c2014-02-15 14:39:40 +08001377#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001378
1379 /*
1380 * inform all the protocols that they are done and will have no more
1381 * callbacks
1382 */
1383
1384 while (protocol->callback) {
1385 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1386 NULL, NULL, 0);
1387 protocol++;
1388 }
1389
Andy Greena41314f2011-05-23 10:00:03 +01001390
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001391#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001392#else
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001393 close(context->dummy_pipe_fds[0]);
1394 close(context->dummy_pipe_fds[1]);
Peter Hinz56885f32011-03-02 22:03:47 +00001395 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001396#endif
1397
Peter Hinz56885f32011-03-02 22:03:47 +00001398#ifdef LWS_OPENSSL_SUPPORT
1399 if (context->ssl_ctx)
1400 SSL_CTX_free(context->ssl_ctx);
1401 if (context->ssl_client_ctx)
1402 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001403
1404 ERR_remove_state(0);
1405 ERR_free_strings();
1406 EVP_cleanup();
1407 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001408#endif
1409
Andy Green46596482013-02-11 11:04:01 +08001410 if (context->fds)
1411 free(context->fds);
1412 if (context->lws_lookup)
1413 free(context->lws_lookup);
1414
Peter Hinz56885f32011-03-02 22:03:47 +00001415 free(context);
1416
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001417#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001418 WSACleanup();
1419#endif
Andy Green6964bb52011-01-23 16:50:33 +00001420}
1421
Andy Greend88146d2013-01-22 12:40:35 +08001422/**
Andy Greenb5b23192013-02-11 17:13:32 +08001423 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001424 * @context: Websocket context
1425 *
1426 * This returns the optional user allocation that can be attached to
1427 * the context the sockets live in at context_create time. It's a way
1428 * to let all sockets serviced in the same context share data without
1429 * using globals statics in the user code.
1430 */
Alon Levy0291eb32012-10-19 11:21:56 +02001431LWS_EXTERN void *
1432libwebsocket_context_user(struct libwebsocket_context *context)
1433{
Andy Greenb5b23192013-02-11 17:13:32 +08001434 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001435}
1436
Andy Green6964bb52011-01-23 16:50:33 +00001437/**
1438 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001439 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001440 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1441 * service otherwise block and service immediately, returning
1442 * after the timeout if nothing needed service.
1443 *
1444 * This function deals with any pending websocket traffic, for three
1445 * kinds of event. It handles these events on both server and client
1446 * types of connection the same.
1447 *
1448 * 1) Accept new connections to our context's server
1449 *
Andy Green6f520a52013-01-29 17:57:39 +08001450 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001451 * server or client connections.
1452 *
1453 * You need to call this service function periodically to all the above
1454 * functions to happen; if your application is single-threaded you can
1455 * just call it in your main event loop.
1456 *
1457 * Alternatively you can fork a new process that asynchronously handles
1458 * calling this service in a loop. In that case you are happy if this
1459 * call blocks your thread until it needs to take care of something and
1460 * would call it with a large nonzero timeout. Your loop then takes no
1461 * CPU while there is nothing happening.
1462 *
1463 * If you are calling it in a single-threaded app, you don't want it to
1464 * wait around blocking other things in your loop from happening, so you
1465 * would call it with a timeout_ms of 0, so it returns immediately if
1466 * nothing is pending, or as soon as it services whatever was pending.
1467 */
1468
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001469LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001470libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001471{
1472 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001473 int m;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001474 char buf;
Andy Greene92cd172011-01-19 13:11:55 +00001475
1476 /* stay dead once we are dead */
1477
Peter Hinz56885f32011-03-02 22:03:47 +00001478 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001479 return 1;
1480
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001481#ifdef LWS_USE_LIBEV
1482 if (context->io_loop && LWS_LIBEV_ENABLED(context))
1483 ev_run(context->io_loop, 0);
1484#endif /* LWS_USE_LIBEV */
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001485 context->service_tid = context->protocols[0].callback(context, NULL,
Andy Green3b3fa9e2013-12-25 16:34:37 +08001486 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
Peter Hinz56885f32011-03-02 22:03:47 +00001487 n = poll(context->fds, context->fds_count, timeout_ms);
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001488 context->service_tid = 0;
1489
Andy Green5dc62ea2013-09-20 20:26:12 +08001490 if (n == 0) /* poll timeout */ {
1491 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001492 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001493 }
Andy Greene92cd172011-01-19 13:11:55 +00001494
Andy Greened451d52014-01-11 12:37:07 +08001495 if (n < 0) {
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01001496 if (LWS_ERRNO != LWS_EINTR)
Andy Greened451d52014-01-11 12:37:07 +08001497 return -1;
1498 else
1499 return 0;
1500 }
Andy Greene92cd172011-01-19 13:11:55 +00001501
Andy Greendfb23042013-01-17 12:26:48 +08001502 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001503
Andy Greenca0a1292013-03-16 11:24:23 +08001504 for (n = 0; n < context->fds_count; n++) {
1505 if (!context->fds[n].revents)
1506 continue;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001507#ifndef _WIN32
1508 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
1509 if (read(context->fds[n].fd, &buf, 1) != 1)
1510 lwsl_err("Cannot read from dummy pipe.");
1511 continue;
1512 }
1513#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001514 m = libwebsocket_service_fd(context, &context->fds[n]);
1515 if (m < 0)
1516 return -1;
1517 /* if something closed, retry this slot */
1518 if (m)
1519 n--;
1520 }
1521
Andy Greene92cd172011-01-19 13:11:55 +00001522 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001523}
1524
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001525/**
1526 * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
1527 * @context: Websocket context
1528 *
1529 * This function let a call to libwebsocket_service() waiting for a timeout
1530 * immediately return.
1531 *
1532 * At the moment this functionality cannot be used on Windows.
1533 */
1534LWS_VISIBLE void
1535libwebsocket_cancel_service(struct libwebsocket_context *context)
1536{
1537#ifndef _WIN32
1538 char buf = 0;
1539 if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
1540 lwsl_err("Cannot write to dummy pipe.");
1541#endif
1542}
1543
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001544#ifdef LWS_USE_LIBEV
1545LWS_VISIBLE int
1546libwebsocket_initloop(
1547 struct libwebsocket_context *context,
1548 struct ev_loop *loop)
1549{
1550 int status = 0;
1551 int backend;
1552 const char * backend_name;
1553 struct ev_io *w_accept = (ev_io *)&context->w_accept;
1554 struct ev_signal *w_sigint = (ev_signal *)&context->w_sigint;
1555
1556 if (!loop)
1557 loop = ev_default_loop(0);
1558
1559 context->io_loop = loop;
1560
1561 /*
1562 * Initialize the accept w_accept with the listening socket
1563 * and register a callback for read operations:
1564 */
1565 ev_io_init(w_accept, libwebsocket_accept_cb,
1566 context->listen_service_fd, EV_READ);
1567 ev_io_start(context->io_loop,w_accept);
1568 ev_signal_init(w_sigint, libwebsocket_sigint_cb, SIGINT);
1569 ev_signal_start(context->io_loop,w_sigint);
1570 backend = ev_backend(loop);
1571
1572 switch (backend) {
1573 case EVBACKEND_SELECT:
1574 backend_name = "select";
1575 break;
1576 case EVBACKEND_POLL:
1577 backend_name = "poll";
1578 break;
1579 case EVBACKEND_EPOLL:
1580 backend_name = "epoll";
1581 break;
1582 case EVBACKEND_KQUEUE:
1583 backend_name = "kqueue";
1584 break;
1585 case EVBACKEND_DEVPOLL:
1586 backend_name = "/dev/poll";
1587 break;
1588 case EVBACKEND_PORT:
1589 backend_name = "Solaris 10 \"port\"";
1590 break;
1591 default:
1592 backend_name = "Unknown libev backend";
1593 break;
1594 };
1595
1596 lwsl_notice(" libev backend: %s\n", backend_name);
1597
1598 return status;
1599}
1600#endif /* LWS_USE_LIBEV */
1601
Andy Green3182ece2013-01-20 17:08:31 +08001602#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001603int
1604lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001605 struct libwebsocket *wsi,
1606 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001607 void *v, size_t len)
1608{
1609 int n;
1610 int handled = 0;
1611
1612 /* maybe an extension will take care of it for us */
1613
1614 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1615 if (!wsi->active_extensions[n]->callback)
1616 continue;
1617
1618 handled |= wsi->active_extensions[n]->callback(context,
1619 wsi->active_extensions[n], wsi,
1620 r, wsi->active_extensions_user[n], v, len);
1621 }
1622
1623 return handled;
1624}
1625
1626
1627void *
1628lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001629 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001630{
1631 int n = 0;
1632
Andy Green68b45042011-05-25 21:41:57 +01001633 if (wsi == NULL)
1634 return NULL;
1635
Andy Greena41314f2011-05-23 10:00:03 +01001636 while (n < wsi->count_active_extensions) {
1637 if (wsi->active_extensions[n] != ext) {
1638 n++;
1639 continue;
1640 }
1641 return wsi->active_extensions_user[n];
1642 }
1643
1644 return NULL;
1645}
Andy Green3182ece2013-01-20 17:08:31 +08001646#endif
Andy Greena41314f2011-05-23 10:00:03 +01001647
Andy Green91f19d82013-12-21 11:18:34 +08001648void
1649lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1650{
1651 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001652 int events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001653 int tid;
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001654 int sampled_tid;
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001655 struct libwebsocket_pollargs pa;
1656
1657 pa.fd = wsi->sock;
Andy Green91f19d82013-12-21 11:18:34 +08001658
1659 context->protocols[0].callback(context, wsi,
1660 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001661 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001662
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001663 pa.prev_events = events = context->fds[wsi->position_in_fds_table].events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001664
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001665 pa.events = context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001666
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001667 context->protocols[0].callback(context, wsi,
1668 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
1669 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001670
Andy Green3b3fa9e2013-12-25 16:34:37 +08001671 /*
1672 * if we changed something in this pollfd...
1673 * ... and we're running in a different thread context
1674 * than the service thread...
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001675 * ... and the service thread is waiting ...
1676 * then cancel it to force a restart with our changed events
Andy Green3b3fa9e2013-12-25 16:34:37 +08001677 */
1678 if (events != context->fds[wsi->position_in_fds_table].events) {
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001679 sampled_tid = context->service_tid;
1680 if (sampled_tid) {
Andy Green3b3fa9e2013-12-25 16:34:37 +08001681 tid = context->protocols[0].callback(context, NULL,
1682 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01001683 if (tid != sampled_tid)
1684 libwebsocket_cancel_service(context);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001685 }
1686 }
Andy Green3b3fa9e2013-12-25 16:34:37 +08001687
Andy Green91f19d82013-12-21 11:18:34 +08001688 context->protocols[0].callback(context, wsi,
1689 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001690 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001691}
1692
1693
Andy Green90c7cbc2011-01-27 06:26:52 +00001694/**
1695 * libwebsocket_callback_on_writable() - Request a callback when this socket
1696 * becomes able to be written to without
1697 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001698 *
Peter Hinz56885f32011-03-02 22:03:47 +00001699 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001700 * @wsi: Websocket connection instance to get callback for
1701 */
1702
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001703LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001704libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001705 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001706{
Andy Green3182ece2013-01-20 17:08:31 +08001707#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001708 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001709 int handled = 0;
1710
1711 /* maybe an extension will take care of it for us */
1712
1713 for (n = 0; n < wsi->count_active_extensions; n++) {
1714 if (!wsi->active_extensions[n]->callback)
1715 continue;
1716
1717 handled |= wsi->active_extensions[n]->callback(context,
1718 wsi->active_extensions[n], wsi,
1719 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1720 wsi->active_extensions_user[n], NULL, 0);
1721 }
1722
1723 if (handled)
1724 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001725#endif
Andy Greendfb23042013-01-17 12:26:48 +08001726 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001727 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1728 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001729 return -1;
1730 }
1731
Andy Green91f19d82013-12-21 11:18:34 +08001732 lws_change_pollfd(wsi, 0, POLLOUT);
Andrew Canaday9769f4f2014-03-23 13:25:07 +08001733#ifdef LWS_USE_LIBEV
1734 if (LWS_LIBEV_ENABLED(context))
1735 ev_io_start(context->io_loop, (struct ev_io *)&wsi->w_write);
1736#endif /* LWS_USE_LIBEV */
Andy Green7a132792013-12-18 09:48:26 +08001737
Andy Green90c7cbc2011-01-27 06:26:52 +00001738 return 1;
1739}
1740
1741/**
1742 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1743 * all connections using the given protocol when it
1744 * becomes possible to write to each socket without
1745 * blocking in turn.
1746 *
1747 * @protocol: Protocol whose connections will get callbacks
1748 */
1749
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001750LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001751libwebsocket_callback_on_writable_all_protocol(
1752 const struct libwebsocket_protocols *protocol)
1753{
Peter Hinz56885f32011-03-02 22:03:47 +00001754 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001755 int n;
Andy Green0d338332011-02-12 11:57:43 +00001756 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001757
Andy Greendfb23042013-01-17 12:26:48 +08001758 for (n = 0; n < context->fds_count; n++) {
1759 wsi = context->lws_lookup[context->fds[n].fd];
1760 if (!wsi)
1761 continue;
1762 if (wsi->protocol == protocol)
1763 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001764 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001765
1766 return 0;
1767}
1768
Andy Greenbe93fef2011-02-14 20:25:43 +00001769/**
Andy Greene39e6ef2014-02-15 16:36:38 +08001770 * libwebsocket_callback_all_protocol() - Callback all connections using
1771 * the given protocol with the given reason
1772 *
1773 * @protocol: Protocol whose connections will get callbacks
1774 * @reason: Callback reason index
1775 */
1776
1777LWS_VISIBLE int
1778libwebsocket_callback_all_protocol(
1779 const struct libwebsocket_protocols *protocol, int reason)
1780{
1781 struct libwebsocket_context *context = protocol->owning_server;
1782 int n;
1783 struct libwebsocket *wsi;
1784
1785 for (n = 0; n < context->fds_count; n++) {
1786 wsi = context->lws_lookup[context->fds[n].fd];
1787 if (!wsi)
1788 continue;
1789 if (wsi->protocol == protocol)
1790 protocol->callback(context, wsi,
1791 reason, wsi->user_space, NULL, 0);
1792 }
1793
1794 return 0;
1795}
1796
1797/**
Andy Greenbe93fef2011-02-14 20:25:43 +00001798 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1799 *
1800 * You will not need this unless you are doing something special
1801 *
1802 * @wsi: Websocket connection instance
1803 * @reason: timeout reason
1804 * @secs: how many seconds
1805 */
1806
Craig McQueene7fd8b12014-02-19 09:24:17 +11001807LWS_VISIBLE void
Andy Greenbe93fef2011-02-14 20:25:43 +00001808libwebsocket_set_timeout(struct libwebsocket *wsi,
1809 enum pending_timeout reason, int secs)
1810{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001811 time_t now;
Andy Greenbe93fef2011-02-14 20:25:43 +00001812
Patrick Gansterer92792b42014-02-26 21:37:31 +01001813 time(&now);
Andy Greenbe93fef2011-02-14 20:25:43 +00001814
Patrick Gansterer92792b42014-02-26 21:37:31 +01001815 wsi->pending_timeout_limit = now + secs;
Andy Greenbe93fef2011-02-14 20:25:43 +00001816 wsi->pending_timeout = reason;
1817}
1818
Andy Greena6cbece2011-01-27 20:06:03 +00001819
1820/**
1821 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1822 *
1823 * You will not need this unless you are doing something special
1824 *
1825 * @wsi: Websocket connection instance
1826 */
1827
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001828LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001829libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1830{
1831 return wsi->sock;
1832}
1833
Andy Greend636e352013-01-29 12:36:17 +08001834#ifdef LWS_LATENCY
1835void
Andy Greenb5b23192013-02-11 17:13:32 +08001836lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1837 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001838{
Patrick Gansterer92792b42014-02-26 21:37:31 +01001839 unsigned long long u;
Andy Greend636e352013-01-29 12:36:17 +08001840 char buf[256];
1841
Patrick Gansterer92792b42014-02-26 21:37:31 +01001842 u = time_in_microseconds();
Andy Greend636e352013-01-29 12:36:17 +08001843
1844 if (action) {
1845 if (completed) {
1846 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001847 sprintf(buf,
1848 "Completion first try lat %luus: %p: ret %d: %s\n",
1849 u - wsi->latency_start,
1850 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001851 else
Andy Greenb5b23192013-02-11 17:13:32 +08001852 sprintf(buf,
1853 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1854 u - wsi->action_start,
1855 u - wsi->latency_start,
1856 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001857 wsi->action_start = 0;
1858 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001859 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1860 u - wsi->latency_start,
1861 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001862 if (u - wsi->latency_start > context->worst_latency) {
1863 context->worst_latency = u - wsi->latency_start;
1864 strcpy(context->worst_latency_info, buf);
1865 }
1866 lwsl_latency("%s", buf);
1867 } else {
1868 wsi->latency_start = u;
1869 if (!wsi->action_start)
1870 wsi->action_start = u;
1871 }
1872}
1873#endif
1874
Andy Greena1ce6be2013-01-18 11:43:21 +08001875#ifdef LWS_NO_SERVER
1876int
Andy Green8d15cf42013-10-24 21:47:06 +08001877_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001878{
1879 return 0;
1880}
1881#else
Andy Green706961d2013-01-17 16:50:35 +08001882int
1883_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1884{
1885 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001886
Andy Greenca0a1292013-03-16 11:24:23 +08001887 /* there is no pending change */
1888 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001889 return 0;
1890
Andy Greenca0a1292013-03-16 11:24:23 +08001891 /* stuff is still buffered, not ready to really accept new input */
1892 if (wsi->u.ws.rxflow_buffer) {
1893 /* get ourselves called back to deal with stashed buffer */
1894 libwebsocket_callback_on_writable(context, wsi);
1895 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001896 }
1897
Andy Greenca0a1292013-03-16 11:24:23 +08001898 /* pending is cleared, we can change rxflow state */
1899
1900 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1901
1902 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1903 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1904
1905 /* adjust the pollfd for this wsi */
1906
1907 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001908 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001909 else
Andy Green91f19d82013-12-21 11:18:34 +08001910 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001911
Andy Green706961d2013-01-17 16:50:35 +08001912 return 1;
1913}
Andy Greena1ce6be2013-01-18 11:43:21 +08001914#endif
Andy Green706961d2013-01-17 16:50:35 +08001915
Andy Green90c7cbc2011-01-27 06:26:52 +00001916/**
1917 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1918 * receieved packets.
1919 *
1920 * If the output side of a server process becomes choked, this allows flow
1921 * control for the input side.
1922 *
1923 * @wsi: Websocket connection instance to get callback for
1924 * @enable: 0 = disable read servicing for this connection, 1 = enable
1925 */
1926
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001927LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001928libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1929{
Andy Greenca0a1292013-03-16 11:24:23 +08001930 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1931 return 0;
1932
1933 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1934 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001935
Andy Green706961d2013-01-17 16:50:35 +08001936 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001937}
1938
Andy Greenb55451c2013-03-16 12:32:27 +08001939/**
1940 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1941 *
1942 * When the user server code realizes it can accept more input, it can
1943 * call this to have the RX flow restriction removed from all connections using
1944 * the given protocol.
1945 *
1946 * @protocol: all connections using this protocol will be allowed to receive
1947 */
1948
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001949LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001950libwebsocket_rx_flow_allow_all_protocol(
1951 const struct libwebsocket_protocols *protocol)
1952{
1953 struct libwebsocket_context *context = protocol->owning_server;
1954 int n;
1955 struct libwebsocket *wsi;
1956
1957 for (n = 0; n < context->fds_count; n++) {
1958 wsi = context->lws_lookup[context->fds[n].fd];
1959 if (!wsi)
1960 continue;
1961 if (wsi->protocol == protocol)
1962 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1963 }
1964}
1965
Andy Green706961d2013-01-17 16:50:35 +08001966
Andy Green2ac5a6f2011-01-28 10:00:18 +00001967/**
1968 * libwebsocket_canonical_hostname() - returns this host's hostname
1969 *
1970 * This is typically used by client code to fill in the host parameter
1971 * when making a client connection. You can only call it after the context
1972 * has been created.
1973 *
Peter Hinz56885f32011-03-02 22:03:47 +00001974 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001975 */
1976
1977
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001978LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001979libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001980{
Peter Hinz56885f32011-03-02 22:03:47 +00001981 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001982}
1983
1984
Andy Green90c7cbc2011-01-27 06:26:52 +00001985static void sigpipe_handler(int x)
1986{
1987}
1988
Andy Green6901cb32011-02-21 08:06:47 +00001989#ifdef LWS_OPENSSL_SUPPORT
1990static int
1991OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1992{
1993
1994 SSL *ssl;
1995 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001996 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001997
1998 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1999 SSL_get_ex_data_X509_STORE_CTX_idx());
2000
2001 /*
Andy Green2e24da02011-03-05 16:12:04 +00002002 * !!! nasty openssl requires the index to come as a library-scope
2003 * static
Andy Green6901cb32011-02-21 08:06:47 +00002004 */
Andy Green2e24da02011-03-05 16:12:04 +00002005 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08002006
Peter Hinz56885f32011-03-02 22:03:47 +00002007 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00002008 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
2009 x509_ctx, ssl, preverify_ok);
2010
2011 /* convert return code from 0 = OK to 1 = OK */
2012
2013 if (!n)
2014 n = 1;
2015 else
2016 n = 0;
2017
2018 return n;
2019}
2020#endif
2021
Andy Green706961d2013-01-17 16:50:35 +08002022int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08002023 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08002024 struct libwebsocket *wsi,
2025 enum libwebsocket_callback_reasons reason, void *user,
2026 void *in, size_t len)
2027{
2028 int n;
2029
2030 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08002031 if (!n)
2032 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08002033
Andy Greenaedc9532013-02-10 21:21:24 +08002034 return n;
Andy Green706961d2013-01-17 16:50:35 +08002035}
2036
Andy Green3b3fa9e2013-12-25 16:34:37 +08002037/*
2038 * This is just used to interrupt poll waiting
2039 * we don't have to do anything with it.
2040 */
arnaudviala7c6f26c2014-02-15 14:39:40 +08002041#ifdef LWS_OPENSSL_SUPPORT
Andy Green3b3fa9e2013-12-25 16:34:37 +08002042static void lws_sigusr2(int sig)
2043{
2044}
arnaudviala7c6f26c2014-02-15 14:39:40 +08002045#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002046
Andy Greenab990e42010-10-31 12:42:52 +00002047/**
Andy Green4739e5c2011-01-22 12:51:57 +00002048 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08002049 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00002050 *
Andy Green1b265272013-02-09 14:01:09 +08002051 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00002052 * of all initialization in one step.
2053 *
Andy Greene92cd172011-01-19 13:11:55 +00002054 * After initialization, it returns a struct libwebsocket_context * that
2055 * represents this server. After calling, user code needs to take care
2056 * of calling libwebsocket_service() with the context pointer to get the
2057 * server's sockets serviced. This can be done in the same process context
2058 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00002059 *
Andy Green8f037e42010-12-19 22:13:26 +00002060 * The protocol callback functions are called for a handful of events
2061 * including http requests coming in, websocket connections becoming
2062 * established, and data arriving; it's also called periodically to allow
2063 * async transmission.
2064 *
2065 * HTTP requests are sent always to the FIRST protocol in @protocol, since
2066 * at that time websocket protocol has not been negotiated. Other
2067 * protocols after the first one never see any HTTP callack activity.
2068 *
2069 * The server created is a simple http server by default; part of the
2070 * websocket standard is upgrading this http connection to a websocket one.
2071 *
2072 * This allows the same server to provide files like scripts and favicon /
2073 * images or whatever over http and dynamic data over websockets all in
2074 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00002075 */
Andy Green4ea60062010-10-30 12:15:07 +01002076
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002077LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08002078libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01002079{
Peter Hinz56885f32011-03-02 22:03:47 +00002080 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00002081 char *p;
Andy Green14f47292013-02-11 19:36:15 +08002082 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08002083#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08002084 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00002085 struct libwebsocket *wsi;
Andy Green055f2972014-03-24 16:09:25 +08002086#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08002087 struct sockaddr_in6 serv_addr6;
2088#endif
2089 struct sockaddr_in serv_addr4;
2090 struct sockaddr *v;
Andy Greencbb31222013-01-31 09:57:05 +08002091#endif
Andy Green3182ece2013-01-20 17:08:31 +08002092#ifndef LWS_NO_EXTENSIONS
2093 int m;
Andy Green1b265272013-02-09 14:01:09 +08002094 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08002095#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01002096
Andy Green3faa9c72010-11-08 17:03:03 +00002097#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00002098 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00002099#endif
2100
Joakim Soderberg4c531232013-02-06 15:26:58 +09002101#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09002102 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09002103#endif
2104
Andy Greenb3a614a2013-01-19 13:08:17 +08002105 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08002106 lwsl_notice("Library version: %s\n", library_version);
Andy Green055f2972014-03-24 16:09:25 +08002107#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08002108 if (!(info->options & LWS_SERVER_OPTION_DISABLE_IPV6))
2109 lwsl_notice("IPV6 compiled in and enabled\n");
2110 else
2111 lwsl_notice("IPV6 compiled in but disabled\n");
2112#else
2113 lwsl_notice("IPV6 not compiled in\n");
2114#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08002115 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08002116 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08002117#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08002118 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
2119 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08002120#else
2121 lwsl_notice(" Configured without extension support\n");
2122#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08002123 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
2124 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08002125 if (info->ssl_cipher_list)
2126 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08002127 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
2128 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08002129
Peter Hinz56885f32011-03-02 22:03:47 +00002130#ifdef _WIN32
2131 {
2132 WORD wVersionRequested;
2133 WSADATA wsaData;
2134 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08002135 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00002136
2137 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
2138 wVersionRequested = MAKEWORD(2, 2);
2139
2140 err = WSAStartup(wVersionRequested, &wsaData);
2141 if (err != 0) {
2142 /* Tell the user that we could not find a usable */
2143 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08002144 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00002145 return NULL;
2146 }
David Galeano7b11fec2011-10-04 19:55:18 +08002147
Andy Green6ee372f2012-04-09 15:09:01 +08002148 /* default to a poll() made out of select() */
2149 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08002150
Patrick Gansterer5b71aac2014-02-28 01:31:39 +01002151#ifndef _WIN32_WCE
Andy Green6ee372f2012-04-09 15:09:01 +08002152 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08002153 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08002154 if (wsdll)
2155 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09002156
Joakim Söderberg68e8d732013-02-06 15:27:39 +09002157 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09002158 if (!poll)
2159 poll = emulated_poll;
Patrick Gansterer5b71aac2014-02-28 01:31:39 +01002160#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002161 }
2162#endif
2163
Andy Greenb5b23192013-02-11 17:13:32 +08002164 context = (struct libwebsocket_context *)
2165 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00002166 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08002167 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002168 return NULL;
2169 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002170 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08002171#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08002172 context->started_with_parent = pid_daemon;
2173 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
2174#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002175
Joakim Soderberg4c531232013-02-06 15:26:58 +09002176 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08002177 context->protocols = info->protocols;
2178 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00002179 context->http_proxy_port = 0;
2180 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08002181 context->options = info->options;
Mattias Lundberg03bb8f92014-02-18 10:06:57 +01002182 context->iface = info->iface;
Andy Greendfb23042013-01-17 12:26:48 +08002183 /* to reduce this allocation, */
2184 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08002185 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
2186 sizeof(struct libwebsocket_context),
2187 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
2188 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08002189 sizeof(struct libwebsocket_context) +
2190 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
2191 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08002192
Andy Greenb5b23192013-02-11 17:13:32 +08002193 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
2194 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002195 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002196 lwsl_err("Unable to allocate fds array for %d connections\n",
2197 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002198 free(context);
2199 return NULL;
2200 }
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002201
2202#ifdef LWS_USE_LIBEV
2203 if (LWS_LIBEV_ENABLED(context)) {
2204 context->w_accept.context = context;
2205 context->w_sigint.context = context;
2206 }
2207#endif /* LWS_USE_LIBEV */
2208
Andy Greenb5b23192013-02-11 17:13:32 +08002209 context->lws_lookup = (struct libwebsocket **)
2210 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002211 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08002212 lwsl_err(
2213 "Unable to allocate lws_lookup array for %d connections\n",
2214 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002215 free(context->fds);
2216 free(context);
2217 return NULL;
2218 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08002219 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
2220 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08002221
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002222 if (!LWS_LIBEV_ENABLED(context)) {
2223 #ifdef _WIN32
2224 context->fds_count = 0;
2225 #else
2226 if (pipe(context->dummy_pipe_fds)) {
2227 lwsl_err("Unable to create pipe\n");
2228 free(context->lws_lookup);
2229 free(context->fds);
2230 free(context);
2231 return NULL;
2232 }
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01002233
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002234 /* use the read end of pipe as first item */
2235 context->fds[0].fd = context->dummy_pipe_fds[0];
2236 context->fds[0].events = POLLIN;
2237 context->fds[0].revents = 0;
2238 context->fds_count = 1;
2239 #endif
2240 }
Patrick Gansterer1ee57f62014-03-06 11:57:50 +01002241
Andy Green3182ece2013-01-20 17:08:31 +08002242#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08002243 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08002244#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002245 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08002246 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00002247
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002248#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002249 context->fd_random = 0;
2250#else
2251 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2252 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002253 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002254 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002255 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00002256 }
Peter Hinz56885f32011-03-02 22:03:47 +00002257#endif
Andy Green44eee682011-02-10 09:32:24 +00002258
Peter Hinz56885f32011-03-02 22:03:47 +00002259#ifdef LWS_OPENSSL_SUPPORT
2260 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08002261 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002262 context->ssl_ctx = NULL;
2263 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002264 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002265#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002266
Andy Greena1ce6be2013-01-18 11:43:21 +08002267 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002268
Andy Greena1ce6be2013-01-18 11:43:21 +08002269#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002270 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01002271 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08002272 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08002273 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01002274
Andy Greenb5b23192013-02-11 17:13:32 +08002275 lwsl_notice(" canonical_hostname = %s\n",
2276 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002277 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002278#endif
Andy Greena69f0512012-05-03 12:32:38 +08002279
Andy Green9659f372011-01-27 22:01:43 +00002280 /* split the proxy ads:port if given */
2281
Patrick Gansterer148b9452014-02-28 02:31:23 +01002282 if (info->http_proxy_address) {
2283 strncpy(context->http_proxy_address, info->http_proxy_address,
Andy Greenb5b23192013-02-11 17:13:32 +08002284 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002285 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002286 sizeof(context->http_proxy_address) - 1] = '\0';
Patrick Gansterer148b9452014-02-28 02:31:23 +01002287 context->http_proxy_port = info->http_proxy_port;
2288 } else {
Patrick Gansterer61a6ae42014-02-28 02:32:03 +01002289#ifdef HAVE_GETENV
Patrick Gansterer148b9452014-02-28 02:31:23 +01002290 p = getenv("http_proxy");
2291 if (p) {
2292 strncpy(context->http_proxy_address, p,
2293 sizeof(context->http_proxy_address) - 1);
2294 context->http_proxy_address[
2295 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002296
Patrick Gansterer148b9452014-02-28 02:31:23 +01002297 p = strchr(context->http_proxy_address, ':');
2298 if (p == NULL) {
2299 lwsl_err("http_proxy needs to be ads:port\n");
2300 goto bail;
2301 }
2302 *p = '\0';
2303 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002304 }
Patrick Gansterer61a6ae42014-02-28 02:32:03 +01002305#endif
Patrick Gansterer148b9452014-02-28 02:31:23 +01002306 }
Andy Green9659f372011-01-27 22:01:43 +00002307
Patrick Gansterer148b9452014-02-28 02:31:23 +01002308 if (context->http_proxy_address[0]) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002309 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002310 context->http_proxy_address,
2311 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002312 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002313
Andy Greena1ce6be2013-01-18 11:43:21 +08002314#ifndef LWS_NO_SERVER
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002315 if (info->port != CONTEXT_PORT_NO_LISTEN) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002316
Andy Green3faa9c72010-11-08 17:03:03 +00002317#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002318 context->use_ssl = info->ssl_cert_filepath != NULL &&
2319 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002320#ifdef USE_CYASSL
2321 lwsl_notice(" Compiled with CYASSL support\n");
2322#else
2323 lwsl_notice(" Compiled with OpenSSL support\n");
2324#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002325 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002326 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002327 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002328 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002329
Andy Green90c7cbc2011-01-27 06:26:52 +00002330#else
Andy Green2b40b792013-02-10 22:22:01 +08002331 if (info->ssl_cert_filepath != NULL &&
2332 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002333 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002334 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002335 }
Andy Greenb5b23192013-02-11 17:13:32 +08002336 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002337#endif
Andy Greena17c6922013-01-20 20:21:54 +08002338
Andy Greenb5b23192013-02-11 17:13:32 +08002339 lwsl_notice(
2340 " per-conn mem: %u + %u headers + protocol rx buf\n",
2341 sizeof(struct libwebsocket),
2342 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002343 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002344#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002345
2346 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002347#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002348#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002349 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002350#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002351
2352
2353#ifdef LWS_OPENSSL_SUPPORT
2354
2355 /* basic openssl init */
2356
2357 SSL_library_init();
2358
2359 OpenSSL_add_all_algorithms();
2360 SSL_load_error_strings();
2361
Andy Green2e24da02011-03-05 16:12:04 +00002362 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002363 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2364
Andy Green90c7cbc2011-01-27 06:26:52 +00002365 /*
2366 * Firefox insists on SSLv23 not SSLv3
2367 * Konq disables SSLv2 by default now, SSLv23 works
2368 */
2369
2370 method = (SSL_METHOD *)SSLv23_server_method();
2371 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002372 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002373 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002374 error,
2375 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002376 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002377 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002378 }
Peter Hinz56885f32011-03-02 22:03:47 +00002379 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2380 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002381 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002382 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002383 error,
2384 ERR_error_string(error,
2385 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002386 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002387 }
2388
martell19c73f32014-01-22 18:17:03 +00002389#if defined(WIN32) || defined(_WIN32)
2390#else
Andy Green3b3fa9e2013-12-25 16:34:37 +08002391 signal(SIGUSR2, lws_sigusr2);
Andy Greened451d52014-01-11 12:37:07 +08002392 {
2393 sigset_t mask;
2394 sigemptyset (&mask);
2395 sigaddset (&mask, SIGUSR2);
2396
2397 sigprocmask(SIG_BLOCK, &mask, NULL);
2398 }
martell19c73f32014-01-22 18:17:03 +00002399#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002400
David Galeanocc148e42013-01-10 10:18:59 +08002401#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002402 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002403#endif
David Galeano77a677c2013-01-10 10:14:12 +08002404 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002405 if (info->ssl_cipher_list)
2406 SSL_CTX_set_cipher_list(context->ssl_ctx,
2407 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002408
Andy Greena1ce6be2013-01-18 11:43:21 +08002409#ifndef LWS_NO_CLIENT
2410
Andy Green90c7cbc2011-01-27 06:26:52 +00002411 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002412
Andy Green1b265272013-02-09 14:01:09 +08002413 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002414 method = (SSL_METHOD *)SSLv23_client_method();
2415 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002416 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002417 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002418 error,
2419 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002420 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002421 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002422 }
2423 /* create context */
2424 context->ssl_client_ctx = SSL_CTX_new(method);
2425 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002426 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002427 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002428 error,
2429 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002430 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002431 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002432 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002433
David Galeanocc148e42013-01-10 10:18:59 +08002434#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002435 SSL_CTX_set_options(context->ssl_client_ctx,
2436 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002437#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002438 SSL_CTX_set_options(context->ssl_client_ctx,
2439 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002440 if (info->ssl_cipher_list)
2441 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2442 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002443
Andy Greend2ec7ad2014-03-15 10:39:29 +08002444#ifdef LWS_SSL_CLIENT_USE_OS_CA_CERTS
2445 /* loads OS default CA certs */
2446 SSL_CTX_set_default_verify_paths(context->ssl_client_ctx);
2447#endif
2448
Peter Hinz56885f32011-03-02 22:03:47 +00002449 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002450 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002451 if (!SSL_CTX_load_verify_locations(
2452 context->ssl_client_ctx, NULL,
2453 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002454 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002455 "Unable to load SSL Client certs from %s "
2456 "(set by --with-client-cert-dir= "
2457 "in configure) -- client ssl isn't "
2458 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002459 } else
2460 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002461 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002462 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002463 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002464 "Unable to load SSL Client certs "
2465 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002466 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002467
2468 /*
2469 * callback allowing user code to load extra verification certs
2470 * helping the client to verify server identity
2471 */
2472
prasannateamf1b353a452013-11-12 17:21:01 -08002473 /* support for client-side certificate authentication */
2474 if (info->ssl_cert_filepath) {
2475 n = SSL_CTX_use_certificate_chain_file(
2476 context->ssl_client_ctx,
2477 info->ssl_cert_filepath);
2478 if (n != 1) {
2479 lwsl_err("problem getting cert '%s' %lu: %s\n",
2480 info->ssl_cert_filepath,
2481 ERR_get_error(),
2482 ERR_error_string(ERR_get_error(),
2483 (char *)context->service_buffer));
2484 goto bail;
2485 }
2486 }
2487 if (info->ssl_private_key_filepath) {
2488 /* set the private key from KeyFile */
2489 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2490 info->ssl_private_key_filepath,
2491 SSL_FILETYPE_PEM) != 1) {
2492 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2493 info->ssl_private_key_filepath,
2494 ERR_get_error(),
2495 ERR_error_string(ERR_get_error(),
2496 (char *)context->service_buffer));
2497 goto bail;
2498 }
2499
2500 /* verify private key */
2501 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2502 lwsl_err("Private SSL key doesn't match cert\n");
2503 goto bail;
2504 }
2505 }
2506
Peter Hinz56885f32011-03-02 22:03:47 +00002507 context->protocols[0].callback(context, NULL,
2508 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2509 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002510 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002511#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002512
Andy Greenc6bf2c22011-02-20 11:10:47 +00002513 /* as a server, are we requiring clients to identify themselves? */
2514
Andy Greenb5b23192013-02-11 17:13:32 +08002515 if (info->options &
2516 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002517
2518 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002519
Peter Hinz56885f32011-03-02 22:03:47 +00002520 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002521 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2522 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002523
2524 /*
2525 * give user code a chance to load certs into the server
2526 * allowing it to verify incoming client certs
2527 */
2528
Peter Hinz56885f32011-03-02 22:03:47 +00002529 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002530 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002531 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002532 }
2533
James Devine3f13ea22014-03-24 16:09:25 +08002534 if (info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
James Devine5b34c972013-12-14 11:41:29 +08002535 /* Normally SSL listener rejects non-ssl, optionally allow */
2536 context->allow_non_ssl_on_ssl_port = 1;
2537 }
2538
Peter Hinz56885f32011-03-02 22:03:47 +00002539 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002540
2541 /* openssl init for server sockets */
2542
Andy Green3faa9c72010-11-08 17:03:03 +00002543 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002544 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002545 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002546 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002547 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002548 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002549 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002550 error,
2551 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002552 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002553 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002554 }
2555 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002556 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002557 info->ssl_private_key_filepath,
2558 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002559 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002560 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002561 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002562 error,
2563 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002564 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002565 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002566 }
2567 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002568 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002569 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002570 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002571 }
2572
2573 /* SSL is happy and has a cert it's content with */
2574 }
2575#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002576
Andy Greena1ce6be2013-01-18 11:43:21 +08002577#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002578 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002579
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002580 if (info->port != CONTEXT_PORT_NO_LISTEN) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002581 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002582
Andy Green055f2972014-03-24 16:09:25 +08002583#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08002584 if (LWS_IPV6_ENABLED(context))
2585 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
2586 else
2587#endif
2588 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2589
Andy Green4739e5c2011-01-22 12:51:57 +00002590 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002591 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002592 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002593 }
Andy Green775c0dd2010-10-29 14:15:22 +01002594
Andy Greenb5b23192013-02-11 17:13:32 +08002595 /*
2596 * allow us to restart even if old sockets in TIME_WAIT
Andy Greenb5b23192013-02-11 17:13:32 +08002597 */
Andy Green6ee372f2012-04-09 15:09:01 +08002598 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2599 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002600
2601 /* Disable Nagle */
2602 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002603 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2604 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002605
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002606 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002607 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002608 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002609 #else
Andy Greene2160712013-01-28 12:19:10 +08002610 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002611 #endif
Andy Greene2160712013-01-28 12:19:10 +08002612
Andy Green055f2972014-03-24 16:09:25 +08002613#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08002614 if (LWS_IPV6_ENABLED(context)) {
2615 v = (struct sockaddr *)&serv_addr6;
2616 n = sizeof(struct sockaddr_in6);
2617 bzero((char *) &serv_addr6, sizeof(serv_addr6));
2618 serv_addr6.sin6_addr = in6addr_any;
2619 serv_addr6.sin6_family = AF_INET6;
2620 serv_addr6.sin6_port = htons(info->port);
2621 } else
2622#endif
2623 {
2624 v = (struct sockaddr *)&serv_addr4;
2625 n = sizeof(serv_addr4);
2626 bzero((char *) &serv_addr4, sizeof(serv_addr4));
2627 serv_addr4.sin_addr.s_addr = INADDR_ANY;
2628 serv_addr4.sin_family = AF_INET;
2629 serv_addr4.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002630
James Devine3f13ea22014-03-24 16:09:25 +08002631 if (info->iface) {
2632 if (interface_to_sa(context, info->iface,
2633 (struct sockaddr_in *)v, n) < 0) {
2634 lwsl_err("Unable to find interface %s\n",
2635 info->iface);
2636 compatible_close(sockfd);
2637 goto bail;
2638 }
2639 }
2640 } /* ipv4 */
2641
2642 n = bind(sockfd, v, n);
Andy Green4739e5c2011-01-22 12:51:57 +00002643 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002644 lwsl_err("ERROR on binding to port %d (%d %d)\n",
James Devine3f13ea22014-03-24 16:09:25 +08002645 info->port, n, LWS_ERRNO);
Andy Green8b2d6f02013-10-22 06:49:30 +08002646 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002647 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002648 }
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002649
James Devine3f13ea22014-03-24 16:09:25 +08002650 struct sockaddr_in sin;
2651 socklen_t len = sizeof(sin);
2652 if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
2653 perror("getsockname");
2654 else
2655 info->port = ntohs(sin.sin_port);
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002656
James Devine3f13ea22014-03-24 16:09:25 +08002657 context->listen_port = info->port;
Andy Green0d338332011-02-12 11:57:43 +00002658
Andy Greenb5b23192013-02-11 17:13:32 +08002659 wsi = (struct libwebsocket *)malloc(
2660 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002661 if (wsi == NULL) {
2662 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002663 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002664 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002665 }
Andy Greenb5b23192013-02-11 17:13:32 +08002666 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002667 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002668#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002669 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002670#endif
Andy Green0d338332011-02-12 11:57:43 +00002671 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002672
2673 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002674
Andy Green65b0e912013-01-16 07:59:47 +08002675 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2676 context->listen_service_count = 0;
2677 context->listen_service_fd = sockfd;
2678
Andy Greena824d182013-01-15 20:52:29 +08002679 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002680 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002681 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002682#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002683
Andy Green6ee372f2012-04-09 15:09:01 +08002684 /*
2685 * drop any root privs for this process
2686 * to listen on port < 1023 we would have needed root, but now we are
2687 * listening, we don't want the power for anything else
2688 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002689#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002690#else
Andy Green1b265272013-02-09 14:01:09 +08002691 if (info->gid != -1)
2692 if (setgid(info->gid))
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01002693 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
Andy Green1b265272013-02-09 14:01:09 +08002694 if (info->uid != -1)
2695 if (setuid(info->uid))
Patrick Gansterer2dbd8372014-02-28 12:37:52 +01002696 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
Peter Hinz56885f32011-03-02 22:03:47 +00002697#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002698
Andy Green6f520a52013-01-29 17:57:39 +08002699 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002700
Peter Hinz56885f32011-03-02 22:03:47 +00002701 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002702 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002703 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002704
Andy Green43db0452013-01-10 19:50:35 +08002705 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002706 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002707
Andy Green1b265272013-02-09 14:01:09 +08002708 info->protocols[context->count_protocols].owning_server =
2709 context;
2710 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002711 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002712
2713 /*
2714 * inform all the protocols that they are doing their one-time
2715 * initialization if they want to
2716 */
2717 info->protocols[context->count_protocols].callback(context,
2718 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002719 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002720
Andy Green3182ece2013-01-20 17:08:31 +08002721#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002722 /*
2723 * give all extensions a chance to create any per-context
2724 * allocations they need
2725 */
2726
2727 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andrew Canaday9769f4f2014-03-23 13:25:07 +08002728 if (info->port != CONTEXT_PORT_NO_LISTEN)
Andy Greena41314f2011-05-23 10:00:03 +01002729 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002730
Andy Green1b265272013-02-09 14:01:09 +08002731 if (info->extensions) {
2732 ext = info->extensions;
2733 while (ext->callback) {
2734 lwsl_ext(" Extension: %s\n", ext->name);
2735 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002736 (enum libwebsocket_extension_callback_reasons)m,
2737 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002738 ext++;
2739 }
Andy Greena41314f2011-05-23 10:00:03 +01002740 }
Andy Green3182ece2013-01-20 17:08:31 +08002741#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002742
Peter Hinz56885f32011-03-02 22:03:47 +00002743 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002744
2745bail:
2746 libwebsocket_context_destroy(context);
2747 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002748}
Andy Greenb45993c2010-12-18 15:13:50 +00002749
Andy Greenb45993c2010-12-18 15:13:50 +00002750/**
shysb4e800e2013-10-24 22:12:03 +08002751 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2752 * @context: pointer to struct libwebsocket_context you want set proxy to
2753 * @proxy: pointer to c string containing proxy in format address:port
2754 *
2755 * Returns 0 if proxy string was parsed and proxy was setup.
2756 * Returns -1 if @proxy is NULL or has incorrect format.
2757 *
2758 * This is only required if your OS does not provide the http_proxy
2759 * enviroment variable (eg, OSX)
2760 *
2761 * IMPORTANT! You should call this function right after creation of the
2762 * libwebsocket_context and before call to connect. If you call this
2763 * function after connect behavior is undefined.
2764 * This function will override proxy settings made on libwebsocket_context
2765 * creation with genenv() call.
2766 */
2767
2768LWS_VISIBLE int
2769libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2770{
2771 char *p;
2772
2773 if (!proxy)
2774 return -1;
2775
2776 strncpy(context->http_proxy_address, proxy,
2777 sizeof(context->http_proxy_address) - 1);
2778 context->http_proxy_address[
2779 sizeof(context->http_proxy_address) - 1] = '\0';
2780
2781 p = strchr(context->http_proxy_address, ':');
2782 if (!p) {
2783 lwsl_err("http_proxy needs to be ads:port\n");
2784
2785 return -1;
2786 }
2787 *p = '\0';
2788 context->http_proxy_port = atoi(p + 1);
2789
2790 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2791 context->http_proxy_port);
2792
2793 return 0;
2794}
2795
2796/**
Andy Greenb45993c2010-12-18 15:13:50 +00002797 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002798 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002799 * @wsi: pointer to struct websocket you want to know the protocol of
2800 *
Andy Green8f037e42010-12-19 22:13:26 +00002801 *
Andy Green6f520a52013-01-29 17:57:39 +08002802 * Some apis can act on all live connections of a given protocol,
2803 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002804 */
Andy Greenab990e42010-10-31 12:42:52 +00002805
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002806LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002807libwebsockets_get_protocol(struct libwebsocket *wsi)
2808{
2809 return wsi->protocol;
2810}
2811
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002812LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002813libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2814{
Andy Green623a98d2013-01-21 11:04:23 +08002815 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002816}
Alex Bligh49146db2011-11-07 17:19:25 +08002817
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002818LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002819libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2820{
Andy Green623a98d2013-01-21 11:04:23 +08002821 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002822}
2823
Andy Green2af4d5b2013-02-18 16:30:10 +08002824int
Alex Bligh49146db2011-11-07 17:19:25 +08002825libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2826{
Andy Green67d556c2013-02-15 22:31:55 +08002827 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002828 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002829
Alex Bligh49146db2011-11-07 17:19:25 +08002830 /* allocate the per-connection user memory (if any) */
2831
2832 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2833 wsi->user_space = malloc(
2834 wsi->protocol->per_session_data_size);
2835 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002836 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002837 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002838 }
Andy Green6ee372f2012-04-09 15:09:01 +08002839 memset(wsi->user_space, 0,
2840 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002841 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002842 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002843}
Andy Green43db0452013-01-10 19:50:35 +08002844
Andy Green0b319092013-01-19 11:17:56 +08002845static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002846{
Andy Green0b319092013-01-19 11:17:56 +08002847 char buf[300];
Patrick Gansterer92792b42014-02-26 21:37:31 +01002848 unsigned long long now;
Andy Green0b319092013-01-19 11:17:56 +08002849 int n;
2850
Andy Green0b319092013-01-19 11:17:56 +08002851 buf[0] = '\0';
2852 for (n = 0; n < LLL_COUNT; n++)
2853 if (level == (1 << n)) {
Patrick Gansterer92792b42014-02-26 21:37:31 +01002854 now = time_in_microseconds() / 100;
2855 sprintf(buf, "[%lu:%04d] %s: ", (unsigned long) now / 10000,
2856 (int)(now % 10000), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002857 break;
2858 }
Andy Greenb5b23192013-02-11 17:13:32 +08002859
Andy Green0b319092013-01-19 11:17:56 +08002860 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002861}
2862
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002863#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002864LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002865{
2866 lwsl_emit_stderr(level, line);
2867}
2868#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002869LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002870{
2871 int syslog_level = LOG_DEBUG;
2872
2873 switch (level) {
2874 case LLL_ERR:
2875 syslog_level = LOG_ERR;
2876 break;
2877 case LLL_WARN:
2878 syslog_level = LOG_WARNING;
2879 break;
2880 case LLL_NOTICE:
2881 syslog_level = LOG_NOTICE;
2882 break;
2883 case LLL_INFO:
2884 syslog_level = LOG_INFO;
2885 break;
2886 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002887 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002888}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002889#endif
Andy Greenc11db202013-01-19 11:12:16 +08002890
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002891LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002892{
Andy Greende8f27a2013-01-12 09:17:42 +08002893 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002894 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002895
2896 if (!(log_level & filter))
2897 return;
2898
Andy Green43db0452013-01-10 19:50:35 +08002899 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002900 vsnprintf(buf, sizeof(buf), format, ap);
2901 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002902 va_end(ap);
2903
Andy Green0b319092013-01-19 11:17:56 +08002904 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002905}
2906
2907/**
2908 * lws_set_log_level() - Set the logging bitfield
2909 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002910 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2911 * function to perform log string emission instead of
2912 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002913 *
Andy Greenc6511a02013-02-19 10:01:48 +08002914 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002915 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002916 */
2917
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002918LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002919 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002920{
2921 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002922 if (log_emit_function)
2923 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002924}
Andy Green92f9b162014-02-21 18:43:42 +08002925
James Devine3f13ea22014-03-24 16:09:25 +08002926/* cast a struct sockaddr_in6 * into addr for ipv6 */
2927
Andy Green92f9b162014-02-21 18:43:42 +08002928int
James Devine3f13ea22014-03-24 16:09:25 +08002929interface_to_sa(struct libwebsocket_context *context,
2930 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
Andy Green92f9b162014-02-21 18:43:42 +08002931{
2932 int rc = -1;
2933#if defined(WIN32) || defined(_WIN32)
2934 /* TODO */
2935#else
2936 struct ifaddrs *ifr;
2937 struct ifaddrs *ifc;
Andy Green055f2972014-03-24 16:09:25 +08002938#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08002939 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2940#endif
Andy Green92f9b162014-02-21 18:43:42 +08002941
2942 getifaddrs(&ifr);
2943 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
James Devine3f13ea22014-03-24 16:09:25 +08002944 if (!ifc->ifa_addr)
Andy Green92f9b162014-02-21 18:43:42 +08002945 continue;
James Devine3f13ea22014-03-24 16:09:25 +08002946
Andy Green92f9b162014-02-21 18:43:42 +08002947 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
James Devine3f13ea22014-03-24 16:09:25 +08002948
Andy Green92f9b162014-02-21 18:43:42 +08002949 if (strcmp(ifc->ifa_name, ifname))
2950 continue;
James Devine3f13ea22014-03-24 16:09:25 +08002951
2952 switch (ifc->ifa_addr->sa_family) {
2953 case AF_INET:
Andy Green055f2972014-03-24 16:09:25 +08002954#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08002955 if (LWS_IPV6_ENABLED(context)) {
2956 /* map IPv4 to IPv6 */
2957 bzero((char *)&addr6->sin6_addr,
2958 sizeof(struct in6_addr));
2959 addr6->sin6_addr.s6_addr16[5] = 0xffff;
2960 bcopy(&((struct sockaddr_in *)ifc->ifa_addr)->
2961 sin_addr,
2962 &addr6->sin6_addr.s6_addr16[6],
2963 sizeof(struct in_addr));
2964 } else
2965#endif
2966 memcpy(addr,
2967 (struct sockaddr_in *)ifc->ifa_addr,
2968 sizeof(struct sockaddr_in));
2969 break;
Andy Green055f2972014-03-24 16:09:25 +08002970#ifdef LWS_USE_IPV6
James Devine3f13ea22014-03-24 16:09:25 +08002971 case AF_INET6:
2972 if (rc >= 0)
2973 break;
2974 memcpy(&addr6->sin6_addr,
2975 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
2976 sizeof(struct in6_addr));
2977 break;
2978#endif
2979 default:
Andy Green92f9b162014-02-21 18:43:42 +08002980 continue;
James Devine3f13ea22014-03-24 16:09:25 +08002981 }
Andy Green92f9b162014-02-21 18:43:42 +08002982 rc = 0;
2983 }
2984
2985 freeifaddrs(ifr);
2986#endif
2987 return rc;
2988}
2989