blob: 475162b71ff28ea980537919397be7f6c85a6b72 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010024#if defined(WIN32) || defined(_WIN32)
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010027#include <mstcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000028#else
Davidc4ef7b12013-01-12 20:39:47 +080029#ifdef LWS_BUILTIN_GETIFADDRS
30#include <getifaddrs.h>
31#else
Peter Hinz56885f32011-03-02 22:03:47 +000032#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080033#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090034#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000035#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080036#include <sys/socket.h>
37#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000038#endif
Andy Green2e24da02011-03-05 16:12:04 +000039
40#ifdef LWS_OPENSSL_SUPPORT
41int openssl_websocket_private_data_index;
42#endif
43
Andy Greenaa6fc442012-04-12 13:26:49 +080044#ifdef __MINGW32__
45#include "../win32port/win32helpers/websock-w32.c"
46#else
47#ifdef __MINGW64__
48#include "../win32port/win32helpers/websock-w32.c"
49#endif
50#endif
51
Andy Green7b405452013-02-01 10:50:15 +080052#ifndef LWS_BUILD_HASH
53#define LWS_BUILD_HASH "unknown-build-hash"
54#endif
Andy Green3182ece2013-01-20 17:08:31 +080055
Andy Green7c19c342013-01-19 12:18:07 +080056static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080057static void lwsl_emit_stderr(int level, const char *line);
58static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
59
Andy Green7b405452013-02-01 10:50:15 +080060static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
61
Andy Greenb5b23192013-02-11 17:13:32 +080062static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080063 "ERR",
64 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080065 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080066 "INFO",
67 "DEBUG",
68 "PARSER",
69 "HEADER",
70 "EXTENSION",
71 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080072 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080073};
74
Andy Greenb5b23192013-02-11 17:13:32 +080075#ifndef LWS_NO_CLIENT
76 extern int lws_client_socket_service(
77 struct libwebsocket_context *context,
78 struct libwebsocket *wsi, struct pollfd *pollfd);
79#endif
80#ifndef LWS_NO_SERVER
81 extern int lws_server_socket_service(
82 struct libwebsocket_context *context,
83 struct libwebsocket *wsi, struct pollfd *pollfd);
84#endif
85
Andy Green7b405452013-02-01 10:50:15 +080086/**
87 * lws_get_library_version: get version and git hash library built from
88 *
89 * returns a const char * to a string like "1.1 178d78c"
90 * representing the library version followed by the git head hash it
91 * was built from
92 */
93
Peter Pentchev9a4fef72013-03-30 09:52:21 +080094LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +080095lws_get_library_version(void)
96{
97 return library_version;
98}
99
Andy Green0d338332011-02-12 11:57:43 +0000100int
Andy Greenb5b23192013-02-11 17:13:32 +0800101insert_wsi_socket_into_fds(struct libwebsocket_context *context,
102 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000103{
Andy Greendfb23042013-01-17 12:26:48 +0800104 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800105 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000106 return 1;
107 }
108
Andy Greendfb23042013-01-17 12:26:48 +0800109 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800110 lwsl_err("Socket fd %d is too high (%d)\n",
111 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800112 return 1;
113 }
114
115 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800116 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800117
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
119 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800120
121 context->lws_lookup[wsi->sock] = wsi;
122 wsi->position_in_fds_table = context->fds_count;
123 context->fds[context->fds_count].fd = wsi->sock;
124 context->fds[context->fds_count].events = POLLIN;
125 context->fds[context->fds_count++].revents = 0;
126
127 /* external POLL support via protocol 0 */
128 context->protocols[0].callback(context, wsi,
129 LWS_CALLBACK_ADD_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800130 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000131
132 return 0;
133}
134
Andy Greendfb23042013-01-17 12:26:48 +0800135static int
Andy Greenb5b23192013-02-11 17:13:32 +0800136remove_wsi_socket_from_fds(struct libwebsocket_context *context,
137 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000138{
Andy Greendfb23042013-01-17 12:26:48 +0800139 int m;
Andy Green0d338332011-02-12 11:57:43 +0000140
Andy Greendfb23042013-01-17 12:26:48 +0800141 if (!--context->fds_count)
142 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000143
Andy Greendfb23042013-01-17 12:26:48 +0800144 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800145 lwsl_err("Socket fd %d too high (%d)\n",
146 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800147 return 1;
148 }
Andy Green0d338332011-02-12 11:57:43 +0000149
Andy Greenb5b23192013-02-11 17:13:32 +0800150 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
151 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800152
153 m = wsi->position_in_fds_table; /* replace the contents for this */
154
155 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800156 context->fds[m] = context->fds[context->fds_count];
157 /*
158 * end guy's fds_lookup entry remains unchanged
159 * (still same fd pointing to same wsi)
160 */
Andy Greendfb23042013-01-17 12:26:48 +0800161 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800162 context->lws_lookup[context->fds[context->fds_count].fd]->
163 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800164 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800165 context->lws_lookup[wsi->sock] = NULL;
166 /* removed wsi has no position any more */
167 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800168
169do_ext:
170 /* remove also from external POLL support via protocol 0 */
171 if (wsi->sock)
172 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800173 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
174 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800175
176 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000177}
178
Andy Green32375b72011-02-19 08:32:53 +0000179
Andy Green8f037e42010-12-19 22:13:26 +0000180void
Peter Hinz56885f32011-03-02 22:03:47 +0000181libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000182 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000183{
Andy Greenb45993c2010-12-18 15:13:50 +0000184 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000185 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000186 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
187 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800188#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000189 int ret;
190 int m;
191 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100192 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800193#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000194
Andy Green4b6fbe12011-02-14 08:03:48 +0000195 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000196 return;
197
Andy Green62c54d22011-02-14 09:14:25 +0000198 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000199
Andy Green62c54d22011-02-14 09:14:25 +0000200 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000201 return;
202
Andy Green22524a62013-02-15 22:48:58 +0800203 /* we tried the polite way... */
204 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
205 goto just_kill_connection;
206
Andy Green623a98d2013-01-21 11:04:23 +0800207 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000208
Andy Green5dc62ea2013-09-20 20:26:12 +0800209 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
210 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
211
212 context->protocols[0].callback(context, wsi,
213 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
214
215 free(wsi->u.hdr.ah);
216 goto just_kill_connection;
217 }
218
219
Andy Green7cf6cb02013-05-19 14:04:10 +0800220 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED && wsi->u.http.fd) {
Andy Green25eddab2013-03-09 12:54:14 +0800221 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
Andy Greenb8b247d2013-01-22 07:20:08 +0800222 close(wsi->u.http.fd);
223 wsi->u.http.fd = 0;
Andy Green0c9563b2013-06-10 22:54:40 +0800224 context->protocols[0].callback(context, wsi,
225 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
Andy Greenb8b247d2013-01-22 07:20:08 +0800226 }
227
Andy Green3182ece2013-01-20 17:08:31 +0800228#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000229 /*
Andy Green68b45042011-05-25 21:41:57 +0100230 * are his extensions okay with him closing? Eg he might be a mux
231 * parent and just his ch1 aspect is closing?
232 */
233
Andy Green68b45042011-05-25 21:41:57 +0100234 for (n = 0; n < wsi->count_active_extensions; n++) {
235 if (!wsi->active_extensions[n]->callback)
236 continue;
237
238 m = wsi->active_extensions[n]->callback(context,
239 wsi->active_extensions[n], wsi,
240 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
241 wsi->active_extensions_user[n], NULL, 0);
242
243 /*
244 * if somebody vetoed actually closing him at this time....
245 * up to the extension to track the attempted close, let's
246 * just bail
247 */
248
249 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800250 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100251 return;
252 }
253 }
254
Andy Green68b45042011-05-25 21:41:57 +0100255 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000256 * flush any tx pending from extensions, since we may send close packet
257 * if there are problems with send, just nuke the connection
258 */
259
260 ret = 1;
261 while (ret == 1) {
262
263 /* default to nobody has more to spill */
264
265 ret = 0;
266 eff_buf.token = NULL;
267 eff_buf.token_len = 0;
268
269 /* show every extension the new incoming data */
270
271 for (n = 0; n < wsi->count_active_extensions; n++) {
272 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000273 wsi->protocol->owning_server,
274 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000275 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
276 wsi->active_extensions_user[n], &eff_buf, 0);
277 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800278 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000279 goto just_kill_connection;
280 }
281 if (m)
282 /*
283 * at least one extension told us he has more
284 * to spill, so we will go around again after
285 */
286 ret = 1;
287 }
288
289 /* assuming they left us something to send, send it */
290
291 if (eff_buf.token_len)
292 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800293 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800294 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000295 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800296 }
Andy Greenc44159f2011-03-07 07:08:18 +0000297 }
Andy Green3182ece2013-01-20 17:08:31 +0800298#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000299
300 /*
Andy Greenda527df2011-03-07 07:08:12 +0000301 * signal we are closing, libsocket_write will
302 * add any necessary version-specific stuff. If the write fails,
303 * no worries we are closing anyway. If we didn't initiate this
304 * close, then our state has been changed to
305 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
306 *
307 * Likewise if it's a second call to close this connection after we
308 * sent the close indication to the peer already, we are in state
309 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
310 */
311
312 if (old_state == WSI_STATE_ESTABLISHED &&
313 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100314
Andy Green43db0452013-01-10 19:50:35 +0800315 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100316
Andy Greenfdd305a2013-02-11 14:32:48 +0800317 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800318 memset(buf, 0, sizeof(buf));
319 n = libwebsocket_write(wsi,
320 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000321 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800322 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000323 /*
324 * we have sent a nice protocol level indication we
325 * now wish to close, we should not send anything more
326 */
327
328 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
329
Andy Greenb5b23192013-02-11 17:13:32 +0800330 /*
331 * ...and we should wait for a reply for a bit
332 * out of politeness
333 */
Andy Greenda527df2011-03-07 07:08:12 +0000334
335 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800336 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000337
Andy Green43db0452013-01-10 19:50:35 +0800338 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000339
340 return;
341 }
342
Andy Greenb5b23192013-02-11 17:13:32 +0800343 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800344
Andy Greenda527df2011-03-07 07:08:12 +0000345 /* else, the send failed and we should just hang up */
346 }
347
Andy Greenc44159f2011-03-07 07:08:18 +0000348just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100349
Andy Greenb5b23192013-02-11 17:13:32 +0800350 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100351
Andy Greenda527df2011-03-07 07:08:12 +0000352 /*
353 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800354 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000355 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000356
Andy Greendfb23042013-01-17 12:26:48 +0800357 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000358
Andy Green251f6fa2010-11-03 11:13:06 +0000359 wsi->state = WSI_STATE_DEAD_SOCKET;
360
Andy Greenb5b23192013-02-11 17:13:32 +0800361 if ((old_state == WSI_STATE_ESTABLISHED ||
362 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800363 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
364
365 if (wsi->u.ws.rx_user_buffer) {
366 free(wsi->u.ws.rx_user_buffer);
367 wsi->u.ws.rx_user_buffer = NULL;
368 }
369 if (wsi->u.ws.rxflow_buffer) {
370 free(wsi->u.ws.rxflow_buffer);
371 wsi->u.ws.rxflow_buffer = NULL;
372 }
Andy Green1f4267b2013-10-17 08:09:19 +0800373 if (wsi->u.ws.truncated_send_malloc) {
374 /* not going to be completed... nuke it */
375 free(wsi->u.ws.truncated_send_malloc);
376 wsi->u.ws.truncated_send_malloc = NULL;
377 }
Andy Green54495112013-02-06 21:10:16 +0900378 }
379
Andy Green4b6fbe12011-02-14 08:03:48 +0000380 /* tell the user it's all over for this guy */
381
Andy Greend4302732011-02-28 07:45:29 +0000382 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800383 ((old_state == WSI_STATE_ESTABLISHED) ||
384 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
385 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800386 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000387 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000388 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800389 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
390 lwsl_debug("calling back CLOSED_HTTP\n");
391 context->protocols[0].callback(context, wsi,
392 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800393 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800394 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000395
Andy Green3182ece2013-01-20 17:08:31 +0800396#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000397 /* deallocate any active extension contexts */
398
399 for (n = 0; n < wsi->count_active_extensions; n++) {
400 if (!wsi->active_extensions[n]->callback)
401 continue;
402
Andy Green46c2ea02011-03-22 09:04:01 +0000403 wsi->active_extensions[n]->callback(context,
404 wsi->active_extensions[n], wsi,
405 LWS_EXT_CALLBACK_DESTROY,
406 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000407
408 free(wsi->active_extensions_user[n]);
409 }
410
Andy Greena41314f2011-05-23 10:00:03 +0100411 /*
412 * inform all extensions in case they tracked this guy out of band
413 * even though not active on him specifically
414 */
415
416 ext = context->extensions;
417 while (ext && ext->callback) {
418 ext->callback(context, ext, wsi,
419 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
420 NULL, NULL, 0);
421 ext++;
422 }
Andy Green3182ece2013-01-20 17:08:31 +0800423#endif
Andy Greena41314f2011-05-23 10:00:03 +0100424
Andy Green43db0452013-01-10 19:50:35 +0800425/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000426
Andy Green3faa9c72010-11-08 17:03:03 +0000427#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000428 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000429 n = SSL_get_fd(wsi->ssl);
430 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800431 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000432 SSL_free(wsi->ssl);
433 } else {
434#endif
Andy Green0303db42013-01-17 14:46:43 +0800435 if (wsi->sock) {
436 n = shutdown(wsi->sock, SHUT_RDWR);
437 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800438 lwsl_debug("closing: shutdown returned %d\n",
439 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800440
Andy Green0303db42013-01-17 14:46:43 +0800441 n = compatible_close(wsi->sock);
442 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800443 lwsl_debug("closing: close returned %d\n",
444 errno);
Andy Green0303db42013-01-17 14:46:43 +0800445 }
Andy Green3faa9c72010-11-08 17:03:03 +0000446#ifdef LWS_OPENSSL_SUPPORT
447 }
448#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800449 if (wsi->protocol && wsi->protocol->per_session_data_size &&
450 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000451 free(wsi->user_space);
452
Andy Green251f6fa2010-11-03 11:13:06 +0000453 free(wsi);
454}
455
Andy Green07034092011-02-13 08:37:12 +0000456/**
457 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800458 * @context: Libwebsockets context
459 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000460 * @fd: Connection socket descriptor
461 * @name: Buffer to take client address name
462 * @name_len: Length of client address name buffer
463 * @rip: Buffer to take client address IP qotted quad
464 * @rip_len: Length of client address IP buffer
465 *
466 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800467 * the client connected with socket descriptor @fd. Names may be
468 * truncated if there is not enough room. If either cannot be
469 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000470 */
471
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800472LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800473libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
474 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000475 char *rip, int rip_len)
476{
Bob Robertsac049112013-04-25 09:16:30 +0800477 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000478 struct sockaddr_in sin;
479 struct hostent *host;
480 struct hostent *host1;
481 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000482 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000483 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800484 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800485#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800486 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800487#endif
Andy Green07034092011-02-13 08:37:12 +0000488
489 rip[0] = '\0';
490 name[0] = '\0';
491
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800492 lws_latency_pre(context, wsi);
493
Andy Greenb5b23192013-02-11 17:13:32 +0800494 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000495 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
496 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800497 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000498 }
Andy Green6ee372f2012-04-09 15:09:01 +0800499
Andy Greenb5b23192013-02-11 17:13:32 +0800500 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000501 AF_INET);
502 if (host == NULL) {
503 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800504 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000505 }
506
507 strncpy(name, host->h_name, name_len);
508 name[name_len - 1] = '\0';
509
510 host1 = gethostbyname(host->h_name);
511 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800512 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000513 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000514 n = 0;
515 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000516 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000517 if (p == NULL)
518 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000519 if ((host1->h_addrtype != AF_INET)
520#ifdef AF_LOCAL
521 && (host1->h_addrtype != AF_LOCAL)
522#endif
523 )
Andy Green07034092011-02-13 08:37:12 +0000524 continue;
525
Andy Green7627af52011-03-09 15:13:52 +0000526 if (host1->h_addrtype == AF_INET)
527 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000528#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000529 else {
530 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800531 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000532 ip[sizeof(ip) - 1] = '\0';
533 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000534#endif
Andy Green07034092011-02-13 08:37:12 +0000535 p = NULL;
536 strncpy(rip, ip, rip_len);
537 rip[rip_len - 1] = '\0';
538 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800539
540 ret = 0;
541bail:
542 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000543}
Andy Green9f990342011-02-12 11:57:45 +0000544
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800545LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000546 void *buf, int len)
547{
548 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800549 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000550
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100551#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000552 for (n = 0; n < len; n++)
553 p[n] = (unsigned char)rand();
554#else
555 n = read(context->fd_random, p, len);
556#endif
557
558 return n;
559}
560
Andy Greena690cd02013-02-09 12:25:31 +0800561int lws_set_socket_options(struct libwebsocket_context *context, int fd)
562{
563 int optval = 1;
564 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100565#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800566 unsigned long optl = 0;
567#endif
568#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
569 struct protoent *tcp_proto;
570#endif
571
572 if (context->ka_time) {
573 /* enable keepalive on this socket */
574 optval = 1;
575 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
576 (const void *)&optval, optlen) < 0)
577 return 1;
578
Bob Robertsac049112013-04-25 09:16:30 +0800579#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800580
581 /*
582 * didn't find a way to set these per-socket, need to
583 * tune kernel systemwide values
584 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100585#elif WIN32
586 {
587 DWORD dwBytesRet;
588 struct tcp_keepalive alive;
589 alive.onoff = TRUE;
590 alive.keepalivetime = context->ka_time;
591 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800592
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100593 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
594 NULL, 0, &dwBytesRet, NULL, NULL))
595 return 1;
596 }
Andy Greena47865f2013-02-10 09:39:47 +0800597#else
Andy Greena690cd02013-02-09 12:25:31 +0800598 /* set the keepalive conditions we want on it too */
599 optval = context->ka_time;
600 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
601 (const void *)&optval, optlen) < 0)
602 return 1;
603
Larry Hayesbb66ac62013-02-22 09:16:20 +0800604 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800605 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
606 (const void *)&optval, optlen) < 0)
607 return 1;
608
Larry Hayesbb66ac62013-02-22 09:16:20 +0800609 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800610 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
611 (const void *)&optval, optlen) < 0)
612 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800613#endif
Andy Greena690cd02013-02-09 12:25:31 +0800614 }
615
616 /* Disable Nagle */
617 optval = 1;
618#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
619 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
620#else
621 tcp_proto = getprotobyname("TCP");
622 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
623#endif
624
625 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100626#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800627 ioctlsocket(fd, FIONBIO, &optl);
628#else
629 fcntl(fd, F_SETFL, O_NONBLOCK);
630#endif
631
632 return 0;
633}
634
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800635LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000636{
637 struct pollfd fds;
638
639 fds.fd = wsi->sock;
640 fds.events = POLLOUT;
641 fds.revents = 0;
642
643 if (poll(&fds, 1, 0) != 1)
644 return 1;
645
646 if ((fds.revents & POLLOUT) == 0)
647 return 1;
648
649 /* okay to send another packet without blocking */
650
651 return 0;
652}
653
Andy Greena41314f2011-05-23 10:00:03 +0100654int
Andy Green3b84c002011-03-06 13:14:42 +0000655lws_handle_POLLOUT_event(struct libwebsocket_context *context,
656 struct libwebsocket *wsi, struct pollfd *pollfd)
657{
Andy Green3b84c002011-03-06 13:14:42 +0000658 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800659
Andy Green3182ece2013-01-20 17:08:31 +0800660#ifndef LWS_NO_EXTENSIONS
661 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000662 int ret;
663 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100664 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000665
Andy Green1f4267b2013-10-17 08:09:19 +0800666 /* pending truncated sends have uber priority */
667
668 if (wsi->u.ws.truncated_send_malloc) {
669 lws_issue_raw(wsi, wsi->u.ws.truncated_send_malloc +
670 wsi->u.ws.truncated_send_offset,
671 wsi->u.ws.truncated_send_len);
672 /* leave POLLOUT active either way */
673 return 0;
674 }
675
Andy Greena41314f2011-05-23 10:00:03 +0100676 for (n = 0; n < wsi->count_active_extensions; n++) {
677 if (!wsi->active_extensions[n]->callback)
678 continue;
679
680 m = wsi->active_extensions[n]->callback(context,
681 wsi->active_extensions[n], wsi,
682 LWS_EXT_CALLBACK_IS_WRITEABLE,
683 wsi->active_extensions_user[n], NULL, 0);
684 if (m > handled)
685 handled = m;
686 }
687
688 if (handled == 1)
689 goto notify_action;
690
691 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000692 goto user_service;
693
694 /*
695 * check in on the active extensions, see if they
696 * had pending stuff to spill... they need to get the
697 * first look-in otherwise sequence will be disordered
698 *
699 * NULL, zero-length eff_buf means just spill pending
700 */
701
702 ret = 1;
703 while (ret == 1) {
704
705 /* default to nobody has more to spill */
706
707 ret = 0;
708 eff_buf.token = NULL;
709 eff_buf.token_len = 0;
710
711 /* give every extension a chance to spill */
712
713 for (n = 0; n < wsi->count_active_extensions; n++) {
714 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000715 wsi->protocol->owning_server,
716 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000717 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
718 wsi->active_extensions_user[n], &eff_buf, 0);
719 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800720 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000721 return -1;
722 }
723 if (m)
724 /*
725 * at least one extension told us he has more
726 * to spill, so we will go around again after
727 */
728 ret = 1;
729 }
730
731 /* assuming they gave us something to send, send it */
732
733 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800734 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
735 eff_buf.token_len);
736 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000737 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800738 /*
739 * Keep amount spilled small to minimize chance of this
740 */
741 if (n != eff_buf.token_len) {
742 lwsl_err("Unable to spill ext %d vs %s\n",
743 eff_buf.token_len, n);
744 return -1;
745 }
Andy Green3b84c002011-03-06 13:14:42 +0000746 } else
747 continue;
748
749 /* no extension has more to spill */
750
751 if (!ret)
752 continue;
753
754 /*
755 * There's more to spill from an extension, but we just sent
756 * something... did that leave the pipe choked?
757 */
758
759 if (!lws_send_pipe_choked(wsi))
760 /* no we could add more */
761 continue;
762
Andy Green43db0452013-01-10 19:50:35 +0800763 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000764
765 /*
766 * Yes, he's choked. Leave the POLLOUT masked on so we will
767 * come back here when he is unchoked. Don't call the user
768 * callback to enforce ordering of spilling, he'll get called
769 * when we come back here and there's nothing more to spill.
770 */
771
772 return 0;
773 }
774
775 wsi->extension_data_pending = 0;
776
777user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800778#endif
Andy Green3b84c002011-03-06 13:14:42 +0000779 /* one shot */
780
Andy Greena41314f2011-05-23 10:00:03 +0100781 if (pollfd) {
782 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000783
Andy Greena41314f2011-05-23 10:00:03 +0100784 /* external POLL support via protocol 0 */
785 context->protocols[0].callback(context, wsi,
786 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800787 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100788 }
Andy Green3182ece2013-01-20 17:08:31 +0800789#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100790notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800791#endif
Andy Green3b84c002011-03-06 13:14:42 +0000792
Andy Green9e4c2b62011-03-07 20:47:39 +0000793 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
794 n = LWS_CALLBACK_CLIENT_WRITEABLE;
795 else
796 n = LWS_CALLBACK_SERVER_WRITEABLE;
797
Andy Greenb8b247d2013-01-22 07:20:08 +0800798 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800799 wsi, (enum libwebsocket_callback_reasons) n,
800 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000801}
802
803
804
Andy Green1c6e1422013-02-20 19:11:31 +0800805int
Andy Greena41314f2011-05-23 10:00:03 +0100806libwebsocket_service_timeout_check(struct libwebsocket_context *context,
807 struct libwebsocket *wsi, unsigned int sec)
808{
Andy Green3182ece2013-01-20 17:08:31 +0800809#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100810 int n;
811
812 /*
813 * if extensions want in on it (eg, we are a mux parent)
814 * give them a chance to service child timeouts
815 */
816
817 for (n = 0; n < wsi->count_active_extensions; n++)
818 wsi->active_extensions[n]->callback(
819 context, wsi->active_extensions[n],
820 wsi, LWS_EXT_CALLBACK_1HZ,
821 wsi->active_extensions_user[n], NULL, sec);
822
Andy Green3182ece2013-01-20 17:08:31 +0800823#endif
Andy Greena41314f2011-05-23 10:00:03 +0100824 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800825 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800826
Andy Greena41314f2011-05-23 10:00:03 +0100827 /*
828 * if we went beyond the allowed time, kill the
829 * connection
830 */
831
832 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800833 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100834 libwebsocket_close_and_free_session(context,
835 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800836 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100837 }
Andy Green1c6e1422013-02-20 19:11:31 +0800838
839 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100840}
841
Andy Green9f990342011-02-12 11:57:45 +0000842/**
843 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000844 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000845 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800846 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000847 *
Andy Green75006172013-01-22 12:32:11 +0800848 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800849 * services it according to the state of the associated
850 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800851 *
852 * The one call deals with all "service" that might happen on a socket
853 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800854 *
855 * If a pollfd says it has something, you can just pass it to
856 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
857 * If it sees it is a lws socket, the traffic will be handled and
858 * pollfd->revents will be zeroed now.
859 *
860 * If the socket is foreign to lws, it leaves revents alone. So you can
861 * see if you should service yourself by checking the pollfd revents
862 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000863 */
864
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800865LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000866libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000867 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000868{
Andy Greena1ce6be2013-01-18 11:43:21 +0800869 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000870 int n;
Andy Green0d338332011-02-12 11:57:43 +0000871 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800872 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000873 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800874 int timed_out = 0;
875 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800876 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800877
Andy Green3182ece2013-01-20 17:08:31 +0800878#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000879 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800880#endif
Andy Green98a717c2011-03-06 13:14:15 +0000881 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800882
883 if (context->listen_service_fd)
884 listen_socket_fds_index = context->lws_lookup[
885 context->listen_service_fd]->position_in_fds_table;
886
Andy Greena71eafc2011-02-14 17:59:43 +0000887 /*
888 * you can call us with pollfd = NULL to just allow the once-per-second
889 * global timeout checks; if less than a second since the last check
890 * it returns immediately then.
891 */
892
893 gettimeofday(&tv, NULL);
894
Peter Hinz56885f32011-03-02 22:03:47 +0000895 if (context->last_timeout_check_s != tv.tv_sec) {
896 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000897
Joakim Soderberg4c531232013-02-06 15:26:58 +0900898 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800899 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800900 if (context->started_with_parent &&
901 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800902 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900903 #endif
Andy Green24cba922013-01-19 13:56:10 +0800904
Andy Greena71eafc2011-02-14 17:59:43 +0000905 /* global timeout check once per second */
906
Andy Green1c6e1422013-02-20 19:11:31 +0800907 if (pollfd)
908 our_fd = pollfd->fd;
909
Peter Hinz56885f32011-03-02 22:03:47 +0000910 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800911 m = context->fds[n].fd;
912 wsi = context->lws_lookup[m];
913 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800914 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800915
916 if (libwebsocket_service_timeout_check(context, wsi,
917 tv.tv_sec))
918 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800919 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800920 /* it was the guy we came to service! */
921 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800922 /* mark as handled */
923 pollfd->revents = 0;
924 }
Andy Greena71eafc2011-02-14 17:59:43 +0000925 }
926 }
927
Andy Green1c6e1422013-02-20 19:11:31 +0800928 /* the socket we came to service timed out, nothing to do */
929 if (timed_out)
930 return 0;
931
Andy Greena71eafc2011-02-14 17:59:43 +0000932 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000933 if (pollfd == NULL)
934 return 0;
935
936 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800937 wsi = context->lws_lookup[pollfd->fd];
938 if (wsi == NULL)
939 /* not lws connection ... leave revents alone and return */
940 return 0;
941
942 /*
943 * so that caller can tell we handled, past here we need to
944 * zero down pollfd->revents after handling
945 */
946
Andy Green65b0e912013-01-16 07:59:47 +0800947 /*
948 * deal with listen service piggybacking
949 * every listen_service_modulo services of other fds, we
950 * sneak one in to service the listen socket if there's anything waiting
951 *
952 * To handle connection storms, as found in ab, if we previously saw a
953 * pending connection here, it causes us to check again next time.
954 */
955
Andy Greenb5b23192013-02-11 17:13:32 +0800956 if (context->listen_service_fd && pollfd !=
957 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800958 context->listen_service_count++;
959 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800960 context->listen_service_count ==
961 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800962 context->listen_service_count = 0;
963 m = 1;
964 if (context->listen_service_extraseen > 5)
965 m = 2;
966 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800967 /*
968 * even with extpoll, we prepared this
969 * internal fds for listen
970 */
971 n = poll(&context->fds[listen_socket_fds_index],
972 1, 0);
973 if (n > 0) { /* there's a conn waiting for us */
974 libwebsocket_service_fd(context,
975 &context->
976 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800977 context->listen_service_extraseen++;
978 } else {
979 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800980 context->
981 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800982 break;
983 }
984 }
985 }
986
987 }
988
Graham Newtonb1aa1d32013-11-04 09:47:06 +0800989 /* handle session socket closed */
990
991 if ((!(pollfd->revents & POLLIN)) &&
992 (pollfd->revents & (POLLERR | POLLHUP))) {
993
994 lwsl_debug("Session Socket %p (fd=%d) dead\n",
995 (void *)wsi, pollfd->fd);
996
997 goto close_and_handled;
998 }
999
Andy Green65b0e912013-01-16 07:59:47 +08001000 /* okay, what we came here to do... */
1001
Andy Green0d338332011-02-12 11:57:43 +00001002 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001003
Andy Greena1ce6be2013-01-18 11:43:21 +08001004#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001005 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001006 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001007 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001008 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001009 n = lws_server_socket_service(context, wsi, pollfd);
1010 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001011#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001012
Andy Green0d338332011-02-12 11:57:43 +00001013 case LWS_CONNMODE_WS_SERVING:
1014 case LWS_CONNMODE_WS_CLIENT:
1015
Andy Green0d338332011-02-12 11:57:43 +00001016 /* the guy requested a callback when it was OK to write */
1017
Andy Greenda527df2011-03-07 07:08:12 +00001018 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001019 wsi->state == WSI_STATE_ESTABLISHED &&
1020 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +08001021 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001022 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001023 }
Andy Green0d338332011-02-12 11:57:43 +00001024
Andy Greenca0a1292013-03-16 11:24:23 +08001025 if (wsi->u.ws.rxflow_buffer &&
1026 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1027 lwsl_info("draining rxflow\n");
1028 /* well, drain it */
1029 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1030 wsi->u.ws.rxflow_pos;
1031 eff_buf.token_len = wsi->u.ws.rxflow_len -
1032 wsi->u.ws.rxflow_pos;
1033 draining_flow = 1;
1034 goto drain;
1035 }
1036
Andy Green0d338332011-02-12 11:57:43 +00001037 /* any incoming data ready? */
1038
1039 if (!(pollfd->revents & POLLIN))
1040 break;
1041
Andy Greenb45993c2010-12-18 15:13:50 +00001042#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001043read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001044 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001045 eff_buf.token_len = SSL_read(wsi->ssl,
1046 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001047 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001048 if (!eff_buf.token_len) {
1049 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001050 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001051 ERR_error_string(n,
1052 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001053 }
1054 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001055#endif
Andy Greene84652c2013-02-08 13:01:02 +08001056 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001057 context->service_buffer,
1058 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001059
Andy Green98a717c2011-03-06 13:14:15 +00001060 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001061 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1062 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001063 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001064 goto close_and_handled;
1065 n = 0;
1066 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001067 }
Andy Green98a717c2011-03-06 13:14:15 +00001068 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001069 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001070 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001071 }
1072
Andy Green98a717c2011-03-06 13:14:15 +00001073 /*
1074 * give any active extensions a chance to munge the buffer
1075 * before parse. We pass in a pointer to an lws_tokens struct
1076 * prepared with the default buffer and content length that's in
1077 * there. Rather than rewrite the default buffer, extensions
1078 * that expect to grow the buffer can adapt .token to
1079 * point to their own per-connection buffer in the extension
1080 * user allocation. By default with no extensions or no
1081 * extension callback handling, just the normal input buffer is
1082 * used then so it is efficient.
1083 */
Andy Greenb45993c2010-12-18 15:13:50 +00001084
Andy Greene84652c2013-02-08 13:01:02 +08001085 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001086drain:
Andy Green3182ece2013-01-20 17:08:31 +08001087#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001088 more = 1;
1089 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001090
Andy Green98a717c2011-03-06 13:14:15 +00001091 more = 0;
1092
1093 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001094 m = wsi->active_extensions[n]->callback(context,
1095 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001096 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001097 wsi->active_extensions_user[n],
1098 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001099 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001100 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001101 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001102 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001103 }
1104 if (m)
1105 more = 1;
1106 }
Andy Green3182ece2013-01-20 17:08:31 +08001107#endif
Andy Green98a717c2011-03-06 13:14:15 +00001108 /* service incoming data */
1109
1110 if (eff_buf.token_len) {
1111 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001112 (unsigned char *)eff_buf.token,
1113 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001114 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001115 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001116 n = 0;
1117 goto handled;
1118 }
Andy Green98a717c2011-03-06 13:14:15 +00001119 }
Andy Green3182ece2013-01-20 17:08:31 +08001120#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001121 eff_buf.token = NULL;
1122 eff_buf.token_len = 0;
1123 }
Andy Green3182ece2013-01-20 17:08:31 +08001124#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001125 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1126 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1127 lwsl_info("flow buffer: drained\n");
1128 free(wsi->u.ws.rxflow_buffer);
1129 wsi->u.ws.rxflow_buffer = NULL;
1130 /* having drained the rxflow buffer, can rearm POLLIN */
1131 _libwebsocket_rx_flow_control(wsi);
1132 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001133
1134#ifdef LWS_OPENSSL_SUPPORT
1135 if (wsi->ssl && SSL_pending(wsi->ssl))
1136 goto read_pending;
1137#endif
Andy Green98a717c2011-03-06 13:14:15 +00001138 break;
Andy Green76f61e72013-01-16 11:53:05 +08001139
1140 default:
Andy Green03674a62013-01-16 11:47:40 +08001141#ifdef LWS_NO_CLIENT
1142 break;
1143#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001144 n = lws_client_socket_service(context, wsi, pollfd);
1145 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001146#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001147 }
1148
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001149 n = 0;
1150 goto handled;
1151
1152close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001153 libwebsocket_close_and_free_session(context, wsi,
1154 LWS_CLOSE_STATUS_NOSTATUS);
1155 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001156
1157handled:
1158 pollfd->revents = 0;
1159 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001160}
1161
Andy Green0d338332011-02-12 11:57:43 +00001162
Andy Green6964bb52011-01-23 16:50:33 +00001163/**
1164 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001165 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001166 *
1167 * This function closes any active connections and then frees the
1168 * context. After calling this, any further use of the context is
1169 * undefined.
1170 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001171LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001172libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001173{
Andy Green3182ece2013-01-20 17:08:31 +08001174#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001175 int n;
1176 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001177 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001178 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001179
Andy Greend636e352013-01-29 12:36:17 +08001180#ifdef LWS_LATENCY
1181 if (context->worst_latency_info[0])
1182 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1183#endif
1184
Andy Greendfb23042013-01-17 12:26:48 +08001185 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001186 struct libwebsocket *wsi =
1187 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001188 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001189 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1190 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001191 }
Andy Green6964bb52011-01-23 16:50:33 +00001192
Andy Greena41314f2011-05-23 10:00:03 +01001193 /*
1194 * give all extensions a chance to clean up any per-context
1195 * allocations they might have made
1196 */
1197
1198 ext = context->extensions;
1199 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1200 if (context->listen_port)
1201 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001202 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001203 ext->callback(context, ext, NULL,
1204 (enum libwebsocket_extension_callback_reasons)m,
1205 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001206 ext++;
1207 }
Andy Greena7109e62013-02-11 12:05:54 +08001208
1209 /*
1210 * inform all the protocols that they are done and will have no more
1211 * callbacks
1212 */
1213
1214 while (protocol->callback) {
1215 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1216 NULL, NULL, 0);
1217 protocol++;
1218 }
1219
Andy Green3182ece2013-01-20 17:08:31 +08001220#endif
Andy Greena41314f2011-05-23 10:00:03 +01001221
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001222#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001223#else
1224 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001225#endif
1226
Peter Hinz56885f32011-03-02 22:03:47 +00001227#ifdef LWS_OPENSSL_SUPPORT
1228 if (context->ssl_ctx)
1229 SSL_CTX_free(context->ssl_ctx);
1230 if (context->ssl_client_ctx)
1231 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001232
1233 ERR_remove_state(0);
1234 ERR_free_strings();
1235 EVP_cleanup();
1236 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001237#endif
1238
Andy Green46596482013-02-11 11:04:01 +08001239 if (context->fds)
1240 free(context->fds);
1241 if (context->lws_lookup)
1242 free(context->lws_lookup);
1243
Peter Hinz56885f32011-03-02 22:03:47 +00001244 free(context);
1245
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001246#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001247 WSACleanup();
1248#endif
Andy Green6964bb52011-01-23 16:50:33 +00001249}
1250
Andy Greend88146d2013-01-22 12:40:35 +08001251/**
Andy Greenb5b23192013-02-11 17:13:32 +08001252 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001253 * @context: Websocket context
1254 *
1255 * This returns the optional user allocation that can be attached to
1256 * the context the sockets live in at context_create time. It's a way
1257 * to let all sockets serviced in the same context share data without
1258 * using globals statics in the user code.
1259 */
Alon Levy0291eb32012-10-19 11:21:56 +02001260LWS_EXTERN void *
1261libwebsocket_context_user(struct libwebsocket_context *context)
1262{
Andy Greenb5b23192013-02-11 17:13:32 +08001263 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001264}
1265
Andy Green6964bb52011-01-23 16:50:33 +00001266/**
1267 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001268 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001269 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1270 * service otherwise block and service immediately, returning
1271 * after the timeout if nothing needed service.
1272 *
1273 * This function deals with any pending websocket traffic, for three
1274 * kinds of event. It handles these events on both server and client
1275 * types of connection the same.
1276 *
1277 * 1) Accept new connections to our context's server
1278 *
Andy Green6f520a52013-01-29 17:57:39 +08001279 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001280 * server or client connections.
1281 *
1282 * You need to call this service function periodically to all the above
1283 * functions to happen; if your application is single-threaded you can
1284 * just call it in your main event loop.
1285 *
1286 * Alternatively you can fork a new process that asynchronously handles
1287 * calling this service in a loop. In that case you are happy if this
1288 * call blocks your thread until it needs to take care of something and
1289 * would call it with a large nonzero timeout. Your loop then takes no
1290 * CPU while there is nothing happening.
1291 *
1292 * If you are calling it in a single-threaded app, you don't want it to
1293 * wait around blocking other things in your loop from happening, so you
1294 * would call it with a timeout_ms of 0, so it returns immediately if
1295 * nothing is pending, or as soon as it services whatever was pending.
1296 */
1297
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001298LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001299libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001300{
1301 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001302 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001303
1304 /* stay dead once we are dead */
1305
Peter Hinz56885f32011-03-02 22:03:47 +00001306 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001307 return 1;
1308
Andy Green0d338332011-02-12 11:57:43 +00001309 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001310
Peter Hinz56885f32011-03-02 22:03:47 +00001311 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green5dc62ea2013-09-20 20:26:12 +08001312 if (n == 0) /* poll timeout */ {
1313 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001314 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001315 }
Andy Greene92cd172011-01-19 13:11:55 +00001316
Andy Greendfb23042013-01-17 12:26:48 +08001317 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001318 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001319
Andy Greendfb23042013-01-17 12:26:48 +08001320 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001321
Andy Greenca0a1292013-03-16 11:24:23 +08001322 for (n = 0; n < context->fds_count; n++) {
1323 if (!context->fds[n].revents)
1324 continue;
1325 m = libwebsocket_service_fd(context, &context->fds[n]);
1326 if (m < 0)
1327 return -1;
1328 /* if something closed, retry this slot */
1329 if (m)
1330 n--;
1331 }
1332
Andy Greene92cd172011-01-19 13:11:55 +00001333 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001334}
1335
Andy Green3182ece2013-01-20 17:08:31 +08001336#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001337int
1338lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001339 struct libwebsocket *wsi,
1340 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001341 void *v, size_t len)
1342{
1343 int n;
1344 int handled = 0;
1345
1346 /* maybe an extension will take care of it for us */
1347
1348 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1349 if (!wsi->active_extensions[n]->callback)
1350 continue;
1351
1352 handled |= wsi->active_extensions[n]->callback(context,
1353 wsi->active_extensions[n], wsi,
1354 r, wsi->active_extensions_user[n], v, len);
1355 }
1356
1357 return handled;
1358}
1359
1360
1361void *
1362lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001363 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001364{
1365 int n = 0;
1366
Andy Green68b45042011-05-25 21:41:57 +01001367 if (wsi == NULL)
1368 return NULL;
1369
Andy Greena41314f2011-05-23 10:00:03 +01001370 while (n < wsi->count_active_extensions) {
1371 if (wsi->active_extensions[n] != ext) {
1372 n++;
1373 continue;
1374 }
1375 return wsi->active_extensions_user[n];
1376 }
1377
1378 return NULL;
1379}
Andy Green3182ece2013-01-20 17:08:31 +08001380#endif
Andy Greena41314f2011-05-23 10:00:03 +01001381
Andy Green90c7cbc2011-01-27 06:26:52 +00001382/**
1383 * libwebsocket_callback_on_writable() - Request a callback when this socket
1384 * becomes able to be written to without
1385 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001386 *
Peter Hinz56885f32011-03-02 22:03:47 +00001387 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001388 * @wsi: Websocket connection instance to get callback for
1389 */
1390
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001391LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001392libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001393 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001394{
Andy Green3182ece2013-01-20 17:08:31 +08001395#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001396 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001397 int handled = 0;
1398
1399 /* maybe an extension will take care of it for us */
1400
1401 for (n = 0; n < wsi->count_active_extensions; n++) {
1402 if (!wsi->active_extensions[n]->callback)
1403 continue;
1404
1405 handled |= wsi->active_extensions[n]->callback(context,
1406 wsi->active_extensions[n], wsi,
1407 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1408 wsi->active_extensions_user[n], NULL, 0);
1409 }
1410
1411 if (handled)
1412 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001413#endif
Andy Greendfb23042013-01-17 12:26:48 +08001414 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001415 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1416 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001417 return -1;
1418 }
1419
1420 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001421
Andy Green3221f922011-02-12 13:14:11 +00001422 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001423 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001424 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001425 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001426
Andy Green90c7cbc2011-01-27 06:26:52 +00001427 return 1;
1428}
1429
1430/**
1431 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1432 * all connections using the given protocol when it
1433 * becomes possible to write to each socket without
1434 * blocking in turn.
1435 *
1436 * @protocol: Protocol whose connections will get callbacks
1437 */
1438
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001439LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001440libwebsocket_callback_on_writable_all_protocol(
1441 const struct libwebsocket_protocols *protocol)
1442{
Peter Hinz56885f32011-03-02 22:03:47 +00001443 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001444 int n;
Andy Green0d338332011-02-12 11:57:43 +00001445 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001446
Andy Greendfb23042013-01-17 12:26:48 +08001447 for (n = 0; n < context->fds_count; n++) {
1448 wsi = context->lws_lookup[context->fds[n].fd];
1449 if (!wsi)
1450 continue;
1451 if (wsi->protocol == protocol)
1452 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001453 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001454
1455 return 0;
1456}
1457
Andy Greenbe93fef2011-02-14 20:25:43 +00001458/**
1459 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1460 *
1461 * You will not need this unless you are doing something special
1462 *
1463 * @wsi: Websocket connection instance
1464 * @reason: timeout reason
1465 * @secs: how many seconds
1466 */
1467
1468void
1469libwebsocket_set_timeout(struct libwebsocket *wsi,
1470 enum pending_timeout reason, int secs)
1471{
1472 struct timeval tv;
1473
1474 gettimeofday(&tv, NULL);
1475
1476 wsi->pending_timeout_limit = tv.tv_sec + secs;
1477 wsi->pending_timeout = reason;
1478}
1479
Andy Greena6cbece2011-01-27 20:06:03 +00001480
1481/**
1482 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1483 *
1484 * You will not need this unless you are doing something special
1485 *
1486 * @wsi: Websocket connection instance
1487 */
1488
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001489LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001490libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1491{
1492 return wsi->sock;
1493}
1494
Andy Greend636e352013-01-29 12:36:17 +08001495#ifdef LWS_LATENCY
1496void
Andy Greenb5b23192013-02-11 17:13:32 +08001497lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1498 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001499{
1500 struct timeval tv;
1501 unsigned long u;
1502 char buf[256];
1503
1504 gettimeofday(&tv, NULL);
1505
1506 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1507
1508 if (action) {
1509 if (completed) {
1510 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001511 sprintf(buf,
1512 "Completion first try lat %luus: %p: ret %d: %s\n",
1513 u - wsi->latency_start,
1514 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001515 else
Andy Greenb5b23192013-02-11 17:13:32 +08001516 sprintf(buf,
1517 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1518 u - wsi->action_start,
1519 u - wsi->latency_start,
1520 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001521 wsi->action_start = 0;
1522 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001523 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1524 u - wsi->latency_start,
1525 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001526 if (u - wsi->latency_start > context->worst_latency) {
1527 context->worst_latency = u - wsi->latency_start;
1528 strcpy(context->worst_latency_info, buf);
1529 }
1530 lwsl_latency("%s", buf);
1531 } else {
1532 wsi->latency_start = u;
1533 if (!wsi->action_start)
1534 wsi->action_start = u;
1535 }
1536}
1537#endif
1538
Andy Greena1ce6be2013-01-18 11:43:21 +08001539#ifdef LWS_NO_SERVER
1540int
Andy Green8d15cf42013-10-24 21:47:06 +08001541_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001542{
1543 return 0;
1544}
1545#else
Andy Green706961d2013-01-17 16:50:35 +08001546int
1547_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1548{
1549 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001550
Andy Greenca0a1292013-03-16 11:24:23 +08001551 /* there is no pending change */
1552 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001553 return 0;
1554
Andy Greenca0a1292013-03-16 11:24:23 +08001555 /* stuff is still buffered, not ready to really accept new input */
1556 if (wsi->u.ws.rxflow_buffer) {
1557 /* get ourselves called back to deal with stashed buffer */
1558 libwebsocket_callback_on_writable(context, wsi);
1559 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001560 }
1561
Andy Greenca0a1292013-03-16 11:24:23 +08001562 /* pending is cleared, we can change rxflow state */
1563
1564 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1565
1566 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1567 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1568
1569 /* adjust the pollfd for this wsi */
1570
1571 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001572 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1573 else
1574 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1575
Andy Greenca0a1292013-03-16 11:24:23 +08001576 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001577 /* external POLL support via protocol 0 */
1578 context->protocols[0].callback(context, wsi,
1579 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001580 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001581 else
1582 /* external POLL support via protocol 0 */
1583 context->protocols[0].callback(context, wsi,
1584 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001585 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001586
1587 return 1;
1588}
Andy Greena1ce6be2013-01-18 11:43:21 +08001589#endif
Andy Green706961d2013-01-17 16:50:35 +08001590
Andy Green90c7cbc2011-01-27 06:26:52 +00001591/**
1592 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1593 * receieved packets.
1594 *
1595 * If the output side of a server process becomes choked, this allows flow
1596 * control for the input side.
1597 *
1598 * @wsi: Websocket connection instance to get callback for
1599 * @enable: 0 = disable read servicing for this connection, 1 = enable
1600 */
1601
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001602LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001603libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1604{
Andy Greenca0a1292013-03-16 11:24:23 +08001605 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1606 return 0;
1607
1608 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1609 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001610
Andy Green706961d2013-01-17 16:50:35 +08001611 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001612}
1613
Andy Greenb55451c2013-03-16 12:32:27 +08001614/**
1615 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1616 *
1617 * When the user server code realizes it can accept more input, it can
1618 * call this to have the RX flow restriction removed from all connections using
1619 * the given protocol.
1620 *
1621 * @protocol: all connections using this protocol will be allowed to receive
1622 */
1623
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001624LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001625libwebsocket_rx_flow_allow_all_protocol(
1626 const struct libwebsocket_protocols *protocol)
1627{
1628 struct libwebsocket_context *context = protocol->owning_server;
1629 int n;
1630 struct libwebsocket *wsi;
1631
1632 for (n = 0; n < context->fds_count; n++) {
1633 wsi = context->lws_lookup[context->fds[n].fd];
1634 if (!wsi)
1635 continue;
1636 if (wsi->protocol == protocol)
1637 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1638 }
1639}
1640
Andy Green706961d2013-01-17 16:50:35 +08001641
Andy Green2ac5a6f2011-01-28 10:00:18 +00001642/**
1643 * libwebsocket_canonical_hostname() - returns this host's hostname
1644 *
1645 * This is typically used by client code to fill in the host parameter
1646 * when making a client connection. You can only call it after the context
1647 * has been created.
1648 *
Peter Hinz56885f32011-03-02 22:03:47 +00001649 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001650 */
1651
1652
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001653LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001654libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001655{
Peter Hinz56885f32011-03-02 22:03:47 +00001656 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001657}
1658
1659
Andy Green90c7cbc2011-01-27 06:26:52 +00001660static void sigpipe_handler(int x)
1661{
1662}
1663
Andy Green6901cb32011-02-21 08:06:47 +00001664#ifdef LWS_OPENSSL_SUPPORT
1665static int
1666OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1667{
1668
1669 SSL *ssl;
1670 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001671 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001672
1673 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1674 SSL_get_ex_data_X509_STORE_CTX_idx());
1675
1676 /*
Andy Green2e24da02011-03-05 16:12:04 +00001677 * !!! nasty openssl requires the index to come as a library-scope
1678 * static
Andy Green6901cb32011-02-21 08:06:47 +00001679 */
Andy Green2e24da02011-03-05 16:12:04 +00001680 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001681
Peter Hinz56885f32011-03-02 22:03:47 +00001682 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001683 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1684 x509_ctx, ssl, preverify_ok);
1685
1686 /* convert return code from 0 = OK to 1 = OK */
1687
1688 if (!n)
1689 n = 1;
1690 else
1691 n = 0;
1692
1693 return n;
1694}
1695#endif
1696
Andy Green706961d2013-01-17 16:50:35 +08001697int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001698 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001699 struct libwebsocket *wsi,
1700 enum libwebsocket_callback_reasons reason, void *user,
1701 void *in, size_t len)
1702{
1703 int n;
1704
1705 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001706 if (!n)
1707 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001708
Andy Greenaedc9532013-02-10 21:21:24 +08001709 return n;
Andy Green706961d2013-01-17 16:50:35 +08001710}
1711
Andy Greenb45993c2010-12-18 15:13:50 +00001712
Andy Greenab990e42010-10-31 12:42:52 +00001713/**
Andy Green4739e5c2011-01-22 12:51:57 +00001714 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001715 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001716 *
Andy Green1b265272013-02-09 14:01:09 +08001717 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001718 * of all initialization in one step.
1719 *
Andy Greene92cd172011-01-19 13:11:55 +00001720 * After initialization, it returns a struct libwebsocket_context * that
1721 * represents this server. After calling, user code needs to take care
1722 * of calling libwebsocket_service() with the context pointer to get the
1723 * server's sockets serviced. This can be done in the same process context
1724 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001725 *
Andy Green8f037e42010-12-19 22:13:26 +00001726 * The protocol callback functions are called for a handful of events
1727 * including http requests coming in, websocket connections becoming
1728 * established, and data arriving; it's also called periodically to allow
1729 * async transmission.
1730 *
1731 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1732 * at that time websocket protocol has not been negotiated. Other
1733 * protocols after the first one never see any HTTP callack activity.
1734 *
1735 * The server created is a simple http server by default; part of the
1736 * websocket standard is upgrading this http connection to a websocket one.
1737 *
1738 * This allows the same server to provide files like scripts and favicon /
1739 * images or whatever over http and dynamic data over websockets all in
1740 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001741 */
Andy Green4ea60062010-10-30 12:15:07 +01001742
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001743LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001744libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001745{
Peter Hinz56885f32011-03-02 22:03:47 +00001746 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001747 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001748 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001749#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001750 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001751 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001752 struct sockaddr_in serv_addr;
1753#endif
Andy Green3182ece2013-01-20 17:08:31 +08001754#ifndef LWS_NO_EXTENSIONS
1755 int m;
Andy Green1b265272013-02-09 14:01:09 +08001756 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001757#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001758
Andy Green3faa9c72010-11-08 17:03:03 +00001759#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001760 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001761#endif
1762
Joakim Soderberg4c531232013-02-06 15:26:58 +09001763#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001764 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001765#endif
1766
Andy Greenb3a614a2013-01-19 13:08:17 +08001767 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001768 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001769 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001770 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001771#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001772 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1773 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001774#else
1775 lwsl_notice(" Configured without extension support\n");
1776#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001777 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1778 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001779 if (info->ssl_cipher_list)
1780 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001781 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1782 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001783
Peter Hinz56885f32011-03-02 22:03:47 +00001784#ifdef _WIN32
1785 {
1786 WORD wVersionRequested;
1787 WSADATA wsaData;
1788 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001789 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001790
1791 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1792 wVersionRequested = MAKEWORD(2, 2);
1793
1794 err = WSAStartup(wVersionRequested, &wsaData);
1795 if (err != 0) {
1796 /* Tell the user that we could not find a usable */
1797 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001798 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001799 return NULL;
1800 }
David Galeano7b11fec2011-10-04 19:55:18 +08001801
Andy Green6ee372f2012-04-09 15:09:01 +08001802 /* default to a poll() made out of select() */
1803 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001804
Andy Green6ee372f2012-04-09 15:09:01 +08001805 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001806 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001807 if (wsdll)
1808 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001809
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001810 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001811 if (!poll)
1812 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001813 }
1814#endif
1815
Andy Greenb5b23192013-02-11 17:13:32 +08001816 context = (struct libwebsocket_context *)
1817 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001818 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001819 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001820 return NULL;
1821 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001822 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001823#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001824 context->started_with_parent = pid_daemon;
1825 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1826#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001827
Joakim Soderberg4c531232013-02-06 15:26:58 +09001828 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001829 context->protocols = info->protocols;
1830 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001831 context->http_proxy_port = 0;
1832 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001833 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001834 /* to reduce this allocation, */
1835 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001836 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1837 sizeof(struct libwebsocket_context),
1838 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1839 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001840 sizeof(struct libwebsocket_context) +
1841 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1842 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001843
Andy Greenb5b23192013-02-11 17:13:32 +08001844 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1845 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001846 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001847 lwsl_err("Unable to allocate fds array for %d connections\n",
1848 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001849 free(context);
1850 return NULL;
1851 }
Andy Greenb5b23192013-02-11 17:13:32 +08001852 context->lws_lookup = (struct libwebsocket **)
1853 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001854 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001855 lwsl_err(
1856 "Unable to allocate lws_lookup array for %d connections\n",
1857 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001858 free(context->fds);
1859 free(context);
1860 return NULL;
1861 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001862 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1863 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001864
Peter Hinz56885f32011-03-02 22:03:47 +00001865 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001866#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001867 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001868#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001869 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001870 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001871
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001872#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001873 context->fd_random = 0;
1874#else
1875 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1876 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001877 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001878 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001879 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001880 }
Peter Hinz56885f32011-03-02 22:03:47 +00001881#endif
Andy Green44eee682011-02-10 09:32:24 +00001882
Peter Hinz56885f32011-03-02 22:03:47 +00001883#ifdef LWS_OPENSSL_SUPPORT
1884 context->use_ssl = 0;
1885 context->ssl_ctx = NULL;
1886 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001887 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001888#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001889
Andy Greena1ce6be2013-01-18 11:43:21 +08001890 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001891
Andy Greena1ce6be2013-01-18 11:43:21 +08001892#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001893 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001894 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001895 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001896 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001897
Andy Greenb5b23192013-02-11 17:13:32 +08001898 lwsl_notice(" canonical_hostname = %s\n",
1899 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001900 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001901#endif
Andy Greena69f0512012-05-03 12:32:38 +08001902
Andy Green9659f372011-01-27 22:01:43 +00001903 /* split the proxy ads:port if given */
1904
1905 p = getenv("http_proxy");
1906 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001907 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001908 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001909 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001910 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001911
Peter Hinz56885f32011-03-02 22:03:47 +00001912 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001913 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001914 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001915 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001916 }
1917 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001918 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001919
Andy Greenb3a614a2013-01-19 13:08:17 +08001920 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001921 context->http_proxy_address,
1922 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001923 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001924
Andy Greena1ce6be2013-01-18 11:43:21 +08001925#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001926 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001927
Andy Green3faa9c72010-11-08 17:03:03 +00001928#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001929 context->use_ssl = info->ssl_cert_filepath != NULL &&
1930 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001931#ifdef USE_CYASSL
1932 lwsl_notice(" Compiled with CYASSL support\n");
1933#else
1934 lwsl_notice(" Compiled with OpenSSL support\n");
1935#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001936 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001937 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001938 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001939 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001940
Andy Green90c7cbc2011-01-27 06:26:52 +00001941#else
Andy Green2b40b792013-02-10 22:22:01 +08001942 if (info->ssl_cert_filepath != NULL &&
1943 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001944 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001945 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001946 }
Andy Greenb5b23192013-02-11 17:13:32 +08001947 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001948#endif
Andy Greena17c6922013-01-20 20:21:54 +08001949
Andy Greenb5b23192013-02-11 17:13:32 +08001950 lwsl_notice(
1951 " per-conn mem: %u + %u headers + protocol rx buf\n",
1952 sizeof(struct libwebsocket),
1953 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001954 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001955#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001956
1957 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001958#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001959#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001960 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001961#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001962
1963
1964#ifdef LWS_OPENSSL_SUPPORT
1965
1966 /* basic openssl init */
1967
1968 SSL_library_init();
1969
1970 OpenSSL_add_all_algorithms();
1971 SSL_load_error_strings();
1972
Andy Green2e24da02011-03-05 16:12:04 +00001973 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001974 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1975
Andy Green90c7cbc2011-01-27 06:26:52 +00001976 /*
1977 * Firefox insists on SSLv23 not SSLv3
1978 * Konq disables SSLv2 by default now, SSLv23 works
1979 */
1980
1981 method = (SSL_METHOD *)SSLv23_server_method();
1982 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001983 lwsl_err("problem creating ssl method %lu: %s\n",
1984 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001985 ERR_error_string(ERR_get_error(),
1986 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001987 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001988 }
Peter Hinz56885f32011-03-02 22:03:47 +00001989 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1990 if (!context->ssl_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001991 lwsl_err("problem creating ssl context %lu: %s\n",
1992 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001993 ERR_error_string(ERR_get_error(),
1994 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001995 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001996 }
1997
David Galeanocc148e42013-01-10 10:18:59 +08001998#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001999 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002000#endif
David Galeano77a677c2013-01-10 10:14:12 +08002001 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002002 if (info->ssl_cipher_list)
2003 SSL_CTX_set_cipher_list(context->ssl_ctx,
2004 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002005
Andy Greena1ce6be2013-01-18 11:43:21 +08002006#ifndef LWS_NO_CLIENT
2007
Andy Green90c7cbc2011-01-27 06:26:52 +00002008 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002009
Andy Green1b265272013-02-09 14:01:09 +08002010 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002011 method = (SSL_METHOD *)SSLv23_client_method();
2012 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002013 lwsl_err("problem creating ssl method %lu: %s\n",
2014 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002015 ERR_error_string(ERR_get_error(),
2016 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002017 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002018 }
2019 /* create context */
2020 context->ssl_client_ctx = SSL_CTX_new(method);
2021 if (!context->ssl_client_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002022 lwsl_err("problem creating ssl context %lu: %s\n",
2023 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002024 ERR_error_string(ERR_get_error(),
2025 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002026 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002027 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002028
David Galeanocc148e42013-01-10 10:18:59 +08002029#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002030 SSL_CTX_set_options(context->ssl_client_ctx,
2031 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002032#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002033 SSL_CTX_set_options(context->ssl_client_ctx,
2034 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002035 if (info->ssl_cipher_list)
2036 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2037 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002038
Peter Hinz56885f32011-03-02 22:03:47 +00002039 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002040 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002041 if (!SSL_CTX_load_verify_locations(
2042 context->ssl_client_ctx, NULL,
2043 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002044 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002045 "Unable to load SSL Client certs from %s "
2046 "(set by --with-client-cert-dir= "
2047 "in configure) -- client ssl isn't "
2048 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002049 } else
2050 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002051 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002052 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002053 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002054 "Unable to load SSL Client certs "
2055 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002056 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002057
2058 /*
2059 * callback allowing user code to load extra verification certs
2060 * helping the client to verify server identity
2061 */
2062
2063 context->protocols[0].callback(context, NULL,
2064 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2065 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002066 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002067#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002068
Andy Greenc6bf2c22011-02-20 11:10:47 +00002069 /* as a server, are we requiring clients to identify themselves? */
2070
Andy Greenb5b23192013-02-11 17:13:32 +08002071 if (info->options &
2072 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002073
2074 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002075
Peter Hinz56885f32011-03-02 22:03:47 +00002076 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002077 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2078 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002079
2080 /*
2081 * give user code a chance to load certs into the server
2082 * allowing it to verify incoming client certs
2083 */
2084
Peter Hinz56885f32011-03-02 22:03:47 +00002085 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002086 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002087 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002088 }
2089
Peter Hinz56885f32011-03-02 22:03:47 +00002090 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002091
2092 /* openssl init for server sockets */
2093
Andy Green3faa9c72010-11-08 17:03:03 +00002094 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002095 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002096 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002097 if (n != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002098 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002099 info->ssl_cert_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002100 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002101 ERR_error_string(ERR_get_error(),
2102 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002103 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002104 }
2105 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002106 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002107 info->ssl_private_key_filepath,
2108 SSL_FILETYPE_PEM) != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002109 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002110 info->ssl_private_key_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002111 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002112 ERR_error_string(ERR_get_error(),
2113 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002114 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002115 }
2116 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002117 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002118 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002119 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002120 }
2121
2122 /* SSL is happy and has a cert it's content with */
2123 }
2124#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002125
Andy Greena1ce6be2013-01-18 11:43:21 +08002126#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002127 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002128
Andy Green1b265272013-02-09 14:01:09 +08002129 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002130 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002131
Andy Green4739e5c2011-01-22 12:51:57 +00002132 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2133 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002134 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002135 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002136 }
Andy Green775c0dd2010-10-29 14:15:22 +01002137
Joakim Soderberg4c531232013-02-06 15:26:58 +09002138#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002139 /*
2140 * allow us to restart even if old sockets in TIME_WAIT
2141 * (REUSEADDR on Unix means, "don't hang on to this
2142 * address after the listener is closed." On Windows, though,
2143 * it means "don't keep other processes from binding to
2144 * this address while we're using it)
2145 */
Andy Green6ee372f2012-04-09 15:09:01 +08002146 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2147 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002148#endif
Andy Green6c939552011-03-08 08:56:57 +00002149
2150 /* Disable Nagle */
2151 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002152 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2153 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002154
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002155 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002156 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002157 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002158 #else
Andy Greene2160712013-01-28 12:19:10 +08002159 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002160 #endif
Andy Greene2160712013-01-28 12:19:10 +08002161
Andy Green4739e5c2011-01-22 12:51:57 +00002162 bzero((char *) &serv_addr, sizeof(serv_addr));
2163 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002164 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002165 serv_addr.sin_addr.s_addr = INADDR_ANY;
2166 else
Andy Greend1eac602013-11-09 08:07:38 +08002167 if (interface_to_sa(info->iface, &serv_addr,
2168 sizeof(serv_addr)) < 0) {
2169 lwsl_err("Unable to find interface %s\n",
2170 info->iface);
2171 compatible_close(sockfd);
2172 goto bail;
2173 }
Andy Green1b265272013-02-09 14:01:09 +08002174 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002175
2176 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2177 sizeof(serv_addr));
2178 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002179 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002180 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002181 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002182 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002183 }
Andy Green0d338332011-02-12 11:57:43 +00002184
Andy Greenb5b23192013-02-11 17:13:32 +08002185 wsi = (struct libwebsocket *)malloc(
2186 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002187 if (wsi == NULL) {
2188 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002189 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002190 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002191 }
Andy Greenb5b23192013-02-11 17:13:32 +08002192 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002193 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002194#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002195 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002196#endif
Andy Green0d338332011-02-12 11:57:43 +00002197 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002198
2199 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002200
Andy Green65b0e912013-01-16 07:59:47 +08002201 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2202 context->listen_service_count = 0;
2203 context->listen_service_fd = sockfd;
2204
Andy Greena824d182013-01-15 20:52:29 +08002205 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002206 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002207 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002208#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002209
Andy Green6ee372f2012-04-09 15:09:01 +08002210 /*
2211 * drop any root privs for this process
2212 * to listen on port < 1023 we would have needed root, but now we are
2213 * listening, we don't want the power for anything else
2214 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002215#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002216#else
Andy Green1b265272013-02-09 14:01:09 +08002217 if (info->gid != -1)
2218 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002219 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002220 if (info->uid != -1)
2221 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002222 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002223#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002224
Andy Green6f520a52013-01-29 17:57:39 +08002225 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002226
Peter Hinz56885f32011-03-02 22:03:47 +00002227 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002228 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002229 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002230
Andy Green43db0452013-01-10 19:50:35 +08002231 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002232 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002233
Andy Green1b265272013-02-09 14:01:09 +08002234 info->protocols[context->count_protocols].owning_server =
2235 context;
2236 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002237 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002238
2239 /*
2240 * inform all the protocols that they are doing their one-time
2241 * initialization if they want to
2242 */
2243 info->protocols[context->count_protocols].callback(context,
2244 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002245 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002246
Andy Green3182ece2013-01-20 17:08:31 +08002247#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002248 /*
2249 * give all extensions a chance to create any per-context
2250 * allocations they need
2251 */
2252
2253 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002254 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002255 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002256
Andy Green1b265272013-02-09 14:01:09 +08002257 if (info->extensions) {
2258 ext = info->extensions;
2259 while (ext->callback) {
2260 lwsl_ext(" Extension: %s\n", ext->name);
2261 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002262 (enum libwebsocket_extension_callback_reasons)m,
2263 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002264 ext++;
2265 }
Andy Greena41314f2011-05-23 10:00:03 +01002266 }
Andy Green3182ece2013-01-20 17:08:31 +08002267#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002268 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002269
2270bail:
2271 libwebsocket_context_destroy(context);
2272 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002273}
Andy Greenb45993c2010-12-18 15:13:50 +00002274
Andy Greenb45993c2010-12-18 15:13:50 +00002275/**
shysb4e800e2013-10-24 22:12:03 +08002276 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2277 * @context: pointer to struct libwebsocket_context you want set proxy to
2278 * @proxy: pointer to c string containing proxy in format address:port
2279 *
2280 * Returns 0 if proxy string was parsed and proxy was setup.
2281 * Returns -1 if @proxy is NULL or has incorrect format.
2282 *
2283 * This is only required if your OS does not provide the http_proxy
2284 * enviroment variable (eg, OSX)
2285 *
2286 * IMPORTANT! You should call this function right after creation of the
2287 * libwebsocket_context and before call to connect. If you call this
2288 * function after connect behavior is undefined.
2289 * This function will override proxy settings made on libwebsocket_context
2290 * creation with genenv() call.
2291 */
2292
2293LWS_VISIBLE int
2294libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2295{
2296 char *p;
2297
2298 if (!proxy)
2299 return -1;
2300
2301 strncpy(context->http_proxy_address, proxy,
2302 sizeof(context->http_proxy_address) - 1);
2303 context->http_proxy_address[
2304 sizeof(context->http_proxy_address) - 1] = '\0';
2305
2306 p = strchr(context->http_proxy_address, ':');
2307 if (!p) {
2308 lwsl_err("http_proxy needs to be ads:port\n");
2309
2310 return -1;
2311 }
2312 *p = '\0';
2313 context->http_proxy_port = atoi(p + 1);
2314
2315 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2316 context->http_proxy_port);
2317
2318 return 0;
2319}
2320
2321/**
Andy Greenb45993c2010-12-18 15:13:50 +00002322 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002323 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002324 * @wsi: pointer to struct websocket you want to know the protocol of
2325 *
Andy Green8f037e42010-12-19 22:13:26 +00002326 *
Andy Green6f520a52013-01-29 17:57:39 +08002327 * Some apis can act on all live connections of a given protocol,
2328 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002329 */
Andy Greenab990e42010-10-31 12:42:52 +00002330
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002331LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002332libwebsockets_get_protocol(struct libwebsocket *wsi)
2333{
2334 return wsi->protocol;
2335}
2336
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002337LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002338libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2339{
Andy Green623a98d2013-01-21 11:04:23 +08002340 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002341}
Alex Bligh49146db2011-11-07 17:19:25 +08002342
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002343LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002344libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2345{
Andy Green623a98d2013-01-21 11:04:23 +08002346 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002347}
2348
Andy Green2af4d5b2013-02-18 16:30:10 +08002349int
Alex Bligh49146db2011-11-07 17:19:25 +08002350libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2351{
Andy Green67d556c2013-02-15 22:31:55 +08002352 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002353 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002354
Alex Bligh49146db2011-11-07 17:19:25 +08002355 /* allocate the per-connection user memory (if any) */
2356
2357 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2358 wsi->user_space = malloc(
2359 wsi->protocol->per_session_data_size);
2360 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002361 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002362 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002363 }
Andy Green6ee372f2012-04-09 15:09:01 +08002364 memset(wsi->user_space, 0,
2365 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002366 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002367 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002368}
Andy Green43db0452013-01-10 19:50:35 +08002369
Andy Green0b319092013-01-19 11:17:56 +08002370static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002371{
Andy Green0b319092013-01-19 11:17:56 +08002372 char buf[300];
2373 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002374 int n;
2375
2376 gettimeofday(&tv, NULL);
2377
2378 buf[0] = '\0';
2379 for (n = 0; n < LLL_COUNT; n++)
2380 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002381 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002382 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002383 break;
2384 }
Andy Greenb5b23192013-02-11 17:13:32 +08002385
Andy Green0b319092013-01-19 11:17:56 +08002386 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002387}
2388
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002389#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002390LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002391{
2392 lwsl_emit_stderr(level, line);
2393}
2394#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002395LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002396{
2397 int syslog_level = LOG_DEBUG;
2398
2399 switch (level) {
2400 case LLL_ERR:
2401 syslog_level = LOG_ERR;
2402 break;
2403 case LLL_WARN:
2404 syslog_level = LOG_WARNING;
2405 break;
2406 case LLL_NOTICE:
2407 syslog_level = LOG_NOTICE;
2408 break;
2409 case LLL_INFO:
2410 syslog_level = LOG_INFO;
2411 break;
2412 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002413 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002414}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002415#endif
Andy Greenc11db202013-01-19 11:12:16 +08002416
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002417LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002418{
Andy Greende8f27a2013-01-12 09:17:42 +08002419 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002420 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002421
2422 if (!(log_level & filter))
2423 return;
2424
Andy Green43db0452013-01-10 19:50:35 +08002425 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002426 vsnprintf(buf, sizeof(buf), format, ap);
2427 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002428 va_end(ap);
2429
Andy Green0b319092013-01-19 11:17:56 +08002430 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002431}
2432
2433/**
2434 * lws_set_log_level() - Set the logging bitfield
2435 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002436 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2437 * function to perform log string emission instead of
2438 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002439 *
Andy Greenc6511a02013-02-19 10:01:48 +08002440 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002441 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002442 */
2443
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002444LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002445 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002446{
2447 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002448 if (log_emit_function)
2449 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002450}