blob: 661876edac225dcaaa48bd6342191ec4ebc834d2 [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
Peter Hinz56885f32011-03-02 22:03:47 +000024#ifdef 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);
116 assert(wsi->sock);
117
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 Green7cf6cb02013-05-19 14:04:10 +0800209 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED && wsi->u.http.fd) {
Andy Green25eddab2013-03-09 12:54:14 +0800210 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
Andy Greenb8b247d2013-01-22 07:20:08 +0800211 close(wsi->u.http.fd);
212 wsi->u.http.fd = 0;
Andy Green0c9563b2013-06-10 22:54:40 +0800213 context->protocols[0].callback(context, wsi,
214 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
Andy Greenb8b247d2013-01-22 07:20:08 +0800215 }
216
Andy Green3182ece2013-01-20 17:08:31 +0800217#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000218 /*
Andy Green68b45042011-05-25 21:41:57 +0100219 * are his extensions okay with him closing? Eg he might be a mux
220 * parent and just his ch1 aspect is closing?
221 */
222
Andy Green68b45042011-05-25 21:41:57 +0100223 for (n = 0; n < wsi->count_active_extensions; n++) {
224 if (!wsi->active_extensions[n]->callback)
225 continue;
226
227 m = wsi->active_extensions[n]->callback(context,
228 wsi->active_extensions[n], wsi,
229 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
230 wsi->active_extensions_user[n], NULL, 0);
231
232 /*
233 * if somebody vetoed actually closing him at this time....
234 * up to the extension to track the attempted close, let's
235 * just bail
236 */
237
238 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800239 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100240 return;
241 }
242 }
243
Andy Green68b45042011-05-25 21:41:57 +0100244 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000245 * flush any tx pending from extensions, since we may send close packet
246 * if there are problems with send, just nuke the connection
247 */
248
249 ret = 1;
250 while (ret == 1) {
251
252 /* default to nobody has more to spill */
253
254 ret = 0;
255 eff_buf.token = NULL;
256 eff_buf.token_len = 0;
257
258 /* show every extension the new incoming data */
259
260 for (n = 0; n < wsi->count_active_extensions; n++) {
261 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000262 wsi->protocol->owning_server,
263 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000264 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
265 wsi->active_extensions_user[n], &eff_buf, 0);
266 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800267 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000268 goto just_kill_connection;
269 }
270 if (m)
271 /*
272 * at least one extension told us he has more
273 * to spill, so we will go around again after
274 */
275 ret = 1;
276 }
277
278 /* assuming they left us something to send, send it */
279
280 if (eff_buf.token_len)
281 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800282 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800283 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000284 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800285 }
Andy Greenc44159f2011-03-07 07:08:18 +0000286 }
Andy Green3182ece2013-01-20 17:08:31 +0800287#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000288
289 /*
Andy Greenda527df2011-03-07 07:08:12 +0000290 * signal we are closing, libsocket_write will
291 * add any necessary version-specific stuff. If the write fails,
292 * no worries we are closing anyway. If we didn't initiate this
293 * close, then our state has been changed to
294 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
295 *
296 * Likewise if it's a second call to close this connection after we
297 * sent the close indication to the peer already, we are in state
298 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
299 */
300
301 if (old_state == WSI_STATE_ESTABLISHED &&
302 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100303
Andy Green43db0452013-01-10 19:50:35 +0800304 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100305
Andy Greenfdd305a2013-02-11 14:32:48 +0800306 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800307 memset(buf, 0, sizeof(buf));
308 n = libwebsocket_write(wsi,
309 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000310 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800311 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000312 /*
313 * we have sent a nice protocol level indication we
314 * now wish to close, we should not send anything more
315 */
316
317 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
318
Andy Greenb5b23192013-02-11 17:13:32 +0800319 /*
320 * ...and we should wait for a reply for a bit
321 * out of politeness
322 */
Andy Greenda527df2011-03-07 07:08:12 +0000323
324 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800325 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000326
Andy Green43db0452013-01-10 19:50:35 +0800327 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000328
329 return;
330 }
331
Andy Greenb5b23192013-02-11 17:13:32 +0800332 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800333
Andy Greenda527df2011-03-07 07:08:12 +0000334 /* else, the send failed and we should just hang up */
335 }
336
Andy Greenc44159f2011-03-07 07:08:18 +0000337just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100338
Andy Greenb5b23192013-02-11 17:13:32 +0800339 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100340
Andy Greenda527df2011-03-07 07:08:12 +0000341 /*
342 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800343 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000344 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000345
Andy Greendfb23042013-01-17 12:26:48 +0800346 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000347
Andy Green251f6fa2010-11-03 11:13:06 +0000348 wsi->state = WSI_STATE_DEAD_SOCKET;
349
Andy Greenb5b23192013-02-11 17:13:32 +0800350 if ((old_state == WSI_STATE_ESTABLISHED ||
351 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800352 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
353
354 if (wsi->u.ws.rx_user_buffer) {
355 free(wsi->u.ws.rx_user_buffer);
356 wsi->u.ws.rx_user_buffer = NULL;
357 }
358 if (wsi->u.ws.rxflow_buffer) {
359 free(wsi->u.ws.rxflow_buffer);
360 wsi->u.ws.rxflow_buffer = NULL;
361 }
Andy Green54495112013-02-06 21:10:16 +0900362 }
363
Andy Green4b6fbe12011-02-14 08:03:48 +0000364 /* tell the user it's all over for this guy */
365
Andy Greend4302732011-02-28 07:45:29 +0000366 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800367 ((old_state == WSI_STATE_ESTABLISHED) ||
368 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
369 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800370 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000371 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000372 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800373 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
374 lwsl_debug("calling back CLOSED_HTTP\n");
375 context->protocols[0].callback(context, wsi,
376 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800377 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800378 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000379
Andy Green3182ece2013-01-20 17:08:31 +0800380#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000381 /* deallocate any active extension contexts */
382
383 for (n = 0; n < wsi->count_active_extensions; n++) {
384 if (!wsi->active_extensions[n]->callback)
385 continue;
386
Andy Green46c2ea02011-03-22 09:04:01 +0000387 wsi->active_extensions[n]->callback(context,
388 wsi->active_extensions[n], wsi,
389 LWS_EXT_CALLBACK_DESTROY,
390 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000391
392 free(wsi->active_extensions_user[n]);
393 }
394
Andy Greena41314f2011-05-23 10:00:03 +0100395 /*
396 * inform all extensions in case they tracked this guy out of band
397 * even though not active on him specifically
398 */
399
400 ext = context->extensions;
401 while (ext && ext->callback) {
402 ext->callback(context, ext, wsi,
403 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
404 NULL, NULL, 0);
405 ext++;
406 }
Andy Green3182ece2013-01-20 17:08:31 +0800407#endif
Andy Greena41314f2011-05-23 10:00:03 +0100408
Andy Green43db0452013-01-10 19:50:35 +0800409/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000410
Andy Green3faa9c72010-11-08 17:03:03 +0000411#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000412 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000413 n = SSL_get_fd(wsi->ssl);
414 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800415 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000416 SSL_free(wsi->ssl);
417 } else {
418#endif
Andy Green0303db42013-01-17 14:46:43 +0800419 if (wsi->sock) {
420 n = shutdown(wsi->sock, SHUT_RDWR);
421 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800422 lwsl_debug("closing: shutdown returned %d\n",
423 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800424
Andy Green0303db42013-01-17 14:46:43 +0800425 n = compatible_close(wsi->sock);
426 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800427 lwsl_debug("closing: close returned %d\n",
428 errno);
Andy Green0303db42013-01-17 14:46:43 +0800429 }
Andy Green3faa9c72010-11-08 17:03:03 +0000430#ifdef LWS_OPENSSL_SUPPORT
431 }
432#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800433 if (wsi->protocol && wsi->protocol->per_session_data_size &&
434 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000435 free(wsi->user_space);
436
Andy Green251f6fa2010-11-03 11:13:06 +0000437 free(wsi);
438}
439
Andy Green07034092011-02-13 08:37:12 +0000440/**
441 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800442 * @context: Libwebsockets context
443 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000444 * @fd: Connection socket descriptor
445 * @name: Buffer to take client address name
446 * @name_len: Length of client address name buffer
447 * @rip: Buffer to take client address IP qotted quad
448 * @rip_len: Length of client address IP buffer
449 *
450 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800451 * the client connected with socket descriptor @fd. Names may be
452 * truncated if there is not enough room. If either cannot be
453 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000454 */
455
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800456LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800457libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
458 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000459 char *rip, int rip_len)
460{
Bob Robertsac049112013-04-25 09:16:30 +0800461 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000462 struct sockaddr_in sin;
463 struct hostent *host;
464 struct hostent *host1;
465 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000466 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000467 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800468 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800469#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800470 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800471#endif
Andy Green07034092011-02-13 08:37:12 +0000472
473 rip[0] = '\0';
474 name[0] = '\0';
475
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800476 lws_latency_pre(context, wsi);
477
Andy Greenb5b23192013-02-11 17:13:32 +0800478 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000479 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
480 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800481 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000482 }
Andy Green6ee372f2012-04-09 15:09:01 +0800483
Andy Greenb5b23192013-02-11 17:13:32 +0800484 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000485 AF_INET);
486 if (host == NULL) {
487 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800488 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000489 }
490
491 strncpy(name, host->h_name, name_len);
492 name[name_len - 1] = '\0';
493
494 host1 = gethostbyname(host->h_name);
495 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800496 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000497 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000498 n = 0;
499 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000500 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000501 if (p == NULL)
502 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000503 if ((host1->h_addrtype != AF_INET)
504#ifdef AF_LOCAL
505 && (host1->h_addrtype != AF_LOCAL)
506#endif
507 )
Andy Green07034092011-02-13 08:37:12 +0000508 continue;
509
Andy Green7627af52011-03-09 15:13:52 +0000510 if (host1->h_addrtype == AF_INET)
511 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000512#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000513 else {
514 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800515 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000516 ip[sizeof(ip) - 1] = '\0';
517 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000518#endif
Andy Green07034092011-02-13 08:37:12 +0000519 p = NULL;
520 strncpy(rip, ip, rip_len);
521 rip[rip_len - 1] = '\0';
522 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800523
524 ret = 0;
525bail:
526 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000527}
Andy Green9f990342011-02-12 11:57:45 +0000528
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800529LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000530 void *buf, int len)
531{
532 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800533 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000534
535#ifdef WIN32
536 for (n = 0; n < len; n++)
537 p[n] = (unsigned char)rand();
538#else
539 n = read(context->fd_random, p, len);
540#endif
541
542 return n;
543}
544
Andy Greena690cd02013-02-09 12:25:31 +0800545int lws_set_socket_options(struct libwebsocket_context *context, int fd)
546{
547 int optval = 1;
548 socklen_t optlen = sizeof(optval);
549#ifdef WIN32
550 unsigned long optl = 0;
551#endif
552#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
553 struct protoent *tcp_proto;
554#endif
555
556 if (context->ka_time) {
557 /* enable keepalive on this socket */
558 optval = 1;
559 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
560 (const void *)&optval, optlen) < 0)
561 return 1;
562
Bob Robertsac049112013-04-25 09:16:30 +0800563#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800564
565 /*
566 * didn't find a way to set these per-socket, need to
567 * tune kernel systemwide values
568 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100569#elif WIN32
570 {
571 DWORD dwBytesRet;
572 struct tcp_keepalive alive;
573 alive.onoff = TRUE;
574 alive.keepalivetime = context->ka_time;
575 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800576
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100577 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
578 NULL, 0, &dwBytesRet, NULL, NULL))
579 return 1;
580 }
Andy Greena47865f2013-02-10 09:39:47 +0800581#else
Andy Greena690cd02013-02-09 12:25:31 +0800582 /* set the keepalive conditions we want on it too */
583 optval = context->ka_time;
584 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
585 (const void *)&optval, optlen) < 0)
586 return 1;
587
Larry Hayesbb66ac62013-02-22 09:16:20 +0800588 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800589 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
590 (const void *)&optval, optlen) < 0)
591 return 1;
592
Larry Hayesbb66ac62013-02-22 09:16:20 +0800593 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800594 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
595 (const void *)&optval, optlen) < 0)
596 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800597#endif
Andy Greena690cd02013-02-09 12:25:31 +0800598 }
599
600 /* Disable Nagle */
601 optval = 1;
602#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
603 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
604#else
605 tcp_proto = getprotobyname("TCP");
606 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
607#endif
608
609 /* We are nonblocking... */
610#ifdef WIN32
611 ioctlsocket(fd, FIONBIO, &optl);
612#else
613 fcntl(fd, F_SETFL, O_NONBLOCK);
614#endif
615
616 return 0;
617}
618
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800619LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000620{
621 struct pollfd fds;
622
623 fds.fd = wsi->sock;
624 fds.events = POLLOUT;
625 fds.revents = 0;
626
627 if (poll(&fds, 1, 0) != 1)
628 return 1;
629
630 if ((fds.revents & POLLOUT) == 0)
631 return 1;
632
633 /* okay to send another packet without blocking */
634
635 return 0;
636}
637
Andy Greena41314f2011-05-23 10:00:03 +0100638int
Andy Green3b84c002011-03-06 13:14:42 +0000639lws_handle_POLLOUT_event(struct libwebsocket_context *context,
640 struct libwebsocket *wsi, struct pollfd *pollfd)
641{
Andy Green3b84c002011-03-06 13:14:42 +0000642 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800643
Andy Green3182ece2013-01-20 17:08:31 +0800644#ifndef LWS_NO_EXTENSIONS
645 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000646 int ret;
647 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100648 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000649
Andy Greena41314f2011-05-23 10:00:03 +0100650 for (n = 0; n < wsi->count_active_extensions; n++) {
651 if (!wsi->active_extensions[n]->callback)
652 continue;
653
654 m = wsi->active_extensions[n]->callback(context,
655 wsi->active_extensions[n], wsi,
656 LWS_EXT_CALLBACK_IS_WRITEABLE,
657 wsi->active_extensions_user[n], NULL, 0);
658 if (m > handled)
659 handled = m;
660 }
661
662 if (handled == 1)
663 goto notify_action;
664
665 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000666 goto user_service;
667
668 /*
669 * check in on the active extensions, see if they
670 * had pending stuff to spill... they need to get the
671 * first look-in otherwise sequence will be disordered
672 *
673 * NULL, zero-length eff_buf means just spill pending
674 */
675
676 ret = 1;
677 while (ret == 1) {
678
679 /* default to nobody has more to spill */
680
681 ret = 0;
682 eff_buf.token = NULL;
683 eff_buf.token_len = 0;
684
685 /* give every extension a chance to spill */
686
687 for (n = 0; n < wsi->count_active_extensions; n++) {
688 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000689 wsi->protocol->owning_server,
690 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000691 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
692 wsi->active_extensions_user[n], &eff_buf, 0);
693 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800694 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000695 return -1;
696 }
697 if (m)
698 /*
699 * at least one extension told us he has more
700 * to spill, so we will go around again after
701 */
702 ret = 1;
703 }
704
705 /* assuming they gave us something to send, send it */
706
707 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800708 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
709 eff_buf.token_len);
710 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000711 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800712 /*
713 * Keep amount spilled small to minimize chance of this
714 */
715 if (n != eff_buf.token_len) {
716 lwsl_err("Unable to spill ext %d vs %s\n",
717 eff_buf.token_len, n);
718 return -1;
719 }
Andy Green3b84c002011-03-06 13:14:42 +0000720 } else
721 continue;
722
723 /* no extension has more to spill */
724
725 if (!ret)
726 continue;
727
728 /*
729 * There's more to spill from an extension, but we just sent
730 * something... did that leave the pipe choked?
731 */
732
733 if (!lws_send_pipe_choked(wsi))
734 /* no we could add more */
735 continue;
736
Andy Green43db0452013-01-10 19:50:35 +0800737 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000738
739 /*
740 * Yes, he's choked. Leave the POLLOUT masked on so we will
741 * come back here when he is unchoked. Don't call the user
742 * callback to enforce ordering of spilling, he'll get called
743 * when we come back here and there's nothing more to spill.
744 */
745
746 return 0;
747 }
748
749 wsi->extension_data_pending = 0;
750
751user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800752#endif
Andy Green3b84c002011-03-06 13:14:42 +0000753 /* one shot */
754
Andy Greena41314f2011-05-23 10:00:03 +0100755 if (pollfd) {
756 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000757
Andy Greena41314f2011-05-23 10:00:03 +0100758 /* external POLL support via protocol 0 */
759 context->protocols[0].callback(context, wsi,
760 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800761 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100762 }
Andy Green3182ece2013-01-20 17:08:31 +0800763#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100764notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800765#endif
Andy Green3b84c002011-03-06 13:14:42 +0000766
Andy Green9e4c2b62011-03-07 20:47:39 +0000767 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
768 n = LWS_CALLBACK_CLIENT_WRITEABLE;
769 else
770 n = LWS_CALLBACK_SERVER_WRITEABLE;
771
Andy Greenb8b247d2013-01-22 07:20:08 +0800772 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800773 wsi, (enum libwebsocket_callback_reasons) n,
774 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000775}
776
777
778
Andy Green1c6e1422013-02-20 19:11:31 +0800779int
Andy Greena41314f2011-05-23 10:00:03 +0100780libwebsocket_service_timeout_check(struct libwebsocket_context *context,
781 struct libwebsocket *wsi, unsigned int sec)
782{
Andy Green3182ece2013-01-20 17:08:31 +0800783#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100784 int n;
785
786 /*
787 * if extensions want in on it (eg, we are a mux parent)
788 * give them a chance to service child timeouts
789 */
790
791 for (n = 0; n < wsi->count_active_extensions; n++)
792 wsi->active_extensions[n]->callback(
793 context, wsi->active_extensions[n],
794 wsi, LWS_EXT_CALLBACK_1HZ,
795 wsi->active_extensions_user[n], NULL, sec);
796
Andy Green3182ece2013-01-20 17:08:31 +0800797#endif
Andy Greena41314f2011-05-23 10:00:03 +0100798 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800799 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800800
Andy Greena41314f2011-05-23 10:00:03 +0100801 /*
802 * if we went beyond the allowed time, kill the
803 * connection
804 */
805
806 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800807 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100808 libwebsocket_close_and_free_session(context,
809 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800810 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100811 }
Andy Green1c6e1422013-02-20 19:11:31 +0800812
813 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100814}
815
Andy Green9f990342011-02-12 11:57:45 +0000816/**
817 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000818 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000819 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800820 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000821 *
Andy Green75006172013-01-22 12:32:11 +0800822 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800823 * services it according to the state of the associated
824 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800825 *
826 * The one call deals with all "service" that might happen on a socket
827 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800828 *
829 * If a pollfd says it has something, you can just pass it to
830 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
831 * If it sees it is a lws socket, the traffic will be handled and
832 * pollfd->revents will be zeroed now.
833 *
834 * If the socket is foreign to lws, it leaves revents alone. So you can
835 * see if you should service yourself by checking the pollfd revents
836 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000837 */
838
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800839LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000840libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000841 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000842{
Andy Greena1ce6be2013-01-18 11:43:21 +0800843 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000844 int n;
Andy Green0d338332011-02-12 11:57:43 +0000845 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800846 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000847 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800848 int timed_out = 0;
849 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800850 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800851
Andy Green3182ece2013-01-20 17:08:31 +0800852#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000853 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800854#endif
Andy Green98a717c2011-03-06 13:14:15 +0000855 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800856
857 if (context->listen_service_fd)
858 listen_socket_fds_index = context->lws_lookup[
859 context->listen_service_fd]->position_in_fds_table;
860
Andy Greena71eafc2011-02-14 17:59:43 +0000861 /*
862 * you can call us with pollfd = NULL to just allow the once-per-second
863 * global timeout checks; if less than a second since the last check
864 * it returns immediately then.
865 */
866
867 gettimeofday(&tv, NULL);
868
Peter Hinz56885f32011-03-02 22:03:47 +0000869 if (context->last_timeout_check_s != tv.tv_sec) {
870 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000871
Joakim Soderberg4c531232013-02-06 15:26:58 +0900872 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800873 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800874 if (context->started_with_parent &&
875 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800876 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900877 #endif
Andy Green24cba922013-01-19 13:56:10 +0800878
Andy Greena71eafc2011-02-14 17:59:43 +0000879 /* global timeout check once per second */
880
Andy Green1c6e1422013-02-20 19:11:31 +0800881 if (pollfd)
882 our_fd = pollfd->fd;
883
Peter Hinz56885f32011-03-02 22:03:47 +0000884 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800885 m = context->fds[n].fd;
886 wsi = context->lws_lookup[m];
887 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800888 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800889
890 if (libwebsocket_service_timeout_check(context, wsi,
891 tv.tv_sec))
892 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800893 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800894 /* it was the guy we came to service! */
895 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800896 /* mark as handled */
897 pollfd->revents = 0;
898 }
Andy Greena71eafc2011-02-14 17:59:43 +0000899 }
900 }
901
Andy Green1c6e1422013-02-20 19:11:31 +0800902 /* the socket we came to service timed out, nothing to do */
903 if (timed_out)
904 return 0;
905
Andy Greena71eafc2011-02-14 17:59:43 +0000906 /* just here for timeout management? */
907
908 if (pollfd == NULL)
909 return 0;
910
911 /* no, here to service a socket descriptor */
912
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800913 wsi = context->lws_lookup[pollfd->fd];
914 if (wsi == NULL)
915 /* not lws connection ... leave revents alone and return */
916 return 0;
917
918 /*
919 * so that caller can tell we handled, past here we need to
920 * zero down pollfd->revents after handling
921 */
922
Andy Green65b0e912013-01-16 07:59:47 +0800923 /*
924 * deal with listen service piggybacking
925 * every listen_service_modulo services of other fds, we
926 * sneak one in to service the listen socket if there's anything waiting
927 *
928 * To handle connection storms, as found in ab, if we previously saw a
929 * pending connection here, it causes us to check again next time.
930 */
931
Andy Greenb5b23192013-02-11 17:13:32 +0800932 if (context->listen_service_fd && pollfd !=
933 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800934 context->listen_service_count++;
935 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800936 context->listen_service_count ==
937 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800938 context->listen_service_count = 0;
939 m = 1;
940 if (context->listen_service_extraseen > 5)
941 m = 2;
942 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800943 /*
944 * even with extpoll, we prepared this
945 * internal fds for listen
946 */
947 n = poll(&context->fds[listen_socket_fds_index],
948 1, 0);
949 if (n > 0) { /* there's a conn waiting for us */
950 libwebsocket_service_fd(context,
951 &context->
952 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800953 context->listen_service_extraseen++;
954 } else {
955 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800956 context->
957 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800958 break;
959 }
960 }
961 }
962
963 }
964
965 /* okay, what we came here to do... */
966
Andy Green0d338332011-02-12 11:57:43 +0000967 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800968
Andy Greena1ce6be2013-01-18 11:43:21 +0800969#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800970 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +0800971 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +0000972 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800973 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800974 n = lws_server_socket_service(context, wsi, pollfd);
975 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +0800976#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000977
Andy Green0d338332011-02-12 11:57:43 +0000978 case LWS_CONNMODE_WS_SERVING:
979 case LWS_CONNMODE_WS_CLIENT:
980
981 /* handle session socket closed */
982
Simon Wulf502b9942013-05-06 06:11:53 +0800983 if ((!(pollfd->revents & POLLIN)) &&
Andy Greenc9ac31e2013-02-16 10:17:52 +0800984 (pollfd->revents & (POLLERR | POLLHUP))) {
Andy Green0d338332011-02-12 11:57:43 +0000985
Andy Green43db0452013-01-10 19:50:35 +0800986 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000987 (void *)wsi, pollfd->fd);
988
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800989 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +0000990 }
991
Andy Green0d338332011-02-12 11:57:43 +0000992 /* the guy requested a callback when it was OK to write */
993
Andy Greenda527df2011-03-07 07:08:12 +0000994 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +0800995 wsi->state == WSI_STATE_ESTABLISHED &&
996 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800997 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800998 goto close_and_handled;
Andy Green3b84c002011-03-06 13:14:42 +0000999 }
Andy Green0d338332011-02-12 11:57:43 +00001000
Andy Green0d338332011-02-12 11:57:43 +00001001
Andy Greenca0a1292013-03-16 11:24:23 +08001002 if (wsi->u.ws.rxflow_buffer &&
1003 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1004 lwsl_info("draining rxflow\n");
1005 /* well, drain it */
1006 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1007 wsi->u.ws.rxflow_pos;
1008 eff_buf.token_len = wsi->u.ws.rxflow_len -
1009 wsi->u.ws.rxflow_pos;
1010 draining_flow = 1;
1011 goto drain;
1012 }
1013
Andy Green0d338332011-02-12 11:57:43 +00001014 /* any incoming data ready? */
1015
1016 if (!(pollfd->revents & POLLIN))
1017 break;
1018
Andy Greenb45993c2010-12-18 15:13:50 +00001019#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001020read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001021 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001022 eff_buf.token_len = SSL_read(wsi->ssl,
1023 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001024 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001025 if (!eff_buf.token_len) {
1026 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001027 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001028 ERR_error_string(n,
1029 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001030 }
1031 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001032#endif
Andy Greene84652c2013-02-08 13:01:02 +08001033 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001034 context->service_buffer,
1035 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001036
Andy Green98a717c2011-03-06 13:14:15 +00001037 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001038 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1039 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001040 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001041 goto close_and_handled;
1042 n = 0;
1043 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001044 }
Andy Green98a717c2011-03-06 13:14:15 +00001045 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001046 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001047 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001048 }
1049
Andy Green98a717c2011-03-06 13:14:15 +00001050 /*
1051 * give any active extensions a chance to munge the buffer
1052 * before parse. We pass in a pointer to an lws_tokens struct
1053 * prepared with the default buffer and content length that's in
1054 * there. Rather than rewrite the default buffer, extensions
1055 * that expect to grow the buffer can adapt .token to
1056 * point to their own per-connection buffer in the extension
1057 * user allocation. By default with no extensions or no
1058 * extension callback handling, just the normal input buffer is
1059 * used then so it is efficient.
1060 */
Andy Greenb45993c2010-12-18 15:13:50 +00001061
Andy Greene84652c2013-02-08 13:01:02 +08001062 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001063drain:
Andy Green3182ece2013-01-20 17:08:31 +08001064#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001065 more = 1;
1066 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001067
Andy Green98a717c2011-03-06 13:14:15 +00001068 more = 0;
1069
1070 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001071 m = wsi->active_extensions[n]->callback(context,
1072 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001073 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001074 wsi->active_extensions_user[n],
1075 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001076 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001077 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001078 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001079 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001080 }
1081 if (m)
1082 more = 1;
1083 }
Andy Green3182ece2013-01-20 17:08:31 +08001084#endif
Andy Green98a717c2011-03-06 13:14:15 +00001085 /* service incoming data */
1086
1087 if (eff_buf.token_len) {
1088 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001089 (unsigned char *)eff_buf.token,
1090 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001091 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001092 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001093 n = 0;
1094 goto handled;
1095 }
Andy Green98a717c2011-03-06 13:14:15 +00001096 }
Andy Green3182ece2013-01-20 17:08:31 +08001097#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001098 eff_buf.token = NULL;
1099 eff_buf.token_len = 0;
1100 }
Andy Green3182ece2013-01-20 17:08:31 +08001101#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001102 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1103 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1104 lwsl_info("flow buffer: drained\n");
1105 free(wsi->u.ws.rxflow_buffer);
1106 wsi->u.ws.rxflow_buffer = NULL;
1107 /* having drained the rxflow buffer, can rearm POLLIN */
1108 _libwebsocket_rx_flow_control(wsi);
1109 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001110
1111#ifdef LWS_OPENSSL_SUPPORT
1112 if (wsi->ssl && SSL_pending(wsi->ssl))
1113 goto read_pending;
1114#endif
Andy Green98a717c2011-03-06 13:14:15 +00001115 break;
Andy Green76f61e72013-01-16 11:53:05 +08001116
1117 default:
Andy Green03674a62013-01-16 11:47:40 +08001118#ifdef LWS_NO_CLIENT
1119 break;
1120#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001121 n = lws_client_socket_service(context, wsi, pollfd);
1122 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001123#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001124 }
1125
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001126 n = 0;
1127 goto handled;
1128
1129close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001130 libwebsocket_close_and_free_session(context, wsi,
1131 LWS_CLOSE_STATUS_NOSTATUS);
1132 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001133
1134handled:
1135 pollfd->revents = 0;
1136 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001137}
1138
Andy Green0d338332011-02-12 11:57:43 +00001139
Andy Green6964bb52011-01-23 16:50:33 +00001140/**
1141 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001142 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001143 *
1144 * This function closes any active connections and then frees the
1145 * context. After calling this, any further use of the context is
1146 * undefined.
1147 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001148LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001149libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001150{
Andy Green3182ece2013-01-20 17:08:31 +08001151#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001152 int n;
1153 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001154 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001155 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001156
Andy Greend636e352013-01-29 12:36:17 +08001157#ifdef LWS_LATENCY
1158 if (context->worst_latency_info[0])
1159 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1160#endif
1161
Andy Greendfb23042013-01-17 12:26:48 +08001162 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001163 struct libwebsocket *wsi =
1164 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001165 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001166 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1167 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001168 }
Andy Green6964bb52011-01-23 16:50:33 +00001169
Andy Greena41314f2011-05-23 10:00:03 +01001170 /*
1171 * give all extensions a chance to clean up any per-context
1172 * allocations they might have made
1173 */
1174
1175 ext = context->extensions;
1176 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1177 if (context->listen_port)
1178 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001179 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001180 ext->callback(context, ext, NULL,
1181 (enum libwebsocket_extension_callback_reasons)m,
1182 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001183 ext++;
1184 }
Andy Greena7109e62013-02-11 12:05:54 +08001185
1186 /*
1187 * inform all the protocols that they are done and will have no more
1188 * callbacks
1189 */
1190
1191 while (protocol->callback) {
1192 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1193 NULL, NULL, 0);
1194 protocol++;
1195 }
1196
Andy Green3182ece2013-01-20 17:08:31 +08001197#endif
Andy Greena41314f2011-05-23 10:00:03 +01001198
Peter Hinz56885f32011-03-02 22:03:47 +00001199#ifdef WIN32
1200#else
1201 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001202#endif
1203
Peter Hinz56885f32011-03-02 22:03:47 +00001204#ifdef LWS_OPENSSL_SUPPORT
1205 if (context->ssl_ctx)
1206 SSL_CTX_free(context->ssl_ctx);
1207 if (context->ssl_client_ctx)
1208 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001209
1210 ERR_remove_state(0);
1211 ERR_free_strings();
1212 EVP_cleanup();
1213 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001214#endif
1215
Andy Green46596482013-02-11 11:04:01 +08001216 if (context->fds)
1217 free(context->fds);
1218 if (context->lws_lookup)
1219 free(context->lws_lookup);
1220
Peter Hinz56885f32011-03-02 22:03:47 +00001221 free(context);
1222
1223#ifdef WIN32
1224 WSACleanup();
1225#endif
Andy Green6964bb52011-01-23 16:50:33 +00001226}
1227
Andy Greend88146d2013-01-22 12:40:35 +08001228/**
Andy Greenb5b23192013-02-11 17:13:32 +08001229 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001230 * @context: Websocket context
1231 *
1232 * This returns the optional user allocation that can be attached to
1233 * the context the sockets live in at context_create time. It's a way
1234 * to let all sockets serviced in the same context share data without
1235 * using globals statics in the user code.
1236 */
Alon Levy0291eb32012-10-19 11:21:56 +02001237LWS_EXTERN void *
1238libwebsocket_context_user(struct libwebsocket_context *context)
1239{
Andy Greenb5b23192013-02-11 17:13:32 +08001240 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001241}
1242
Andy Green6964bb52011-01-23 16:50:33 +00001243/**
1244 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001245 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001246 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1247 * service otherwise block and service immediately, returning
1248 * after the timeout if nothing needed service.
1249 *
1250 * This function deals with any pending websocket traffic, for three
1251 * kinds of event. It handles these events on both server and client
1252 * types of connection the same.
1253 *
1254 * 1) Accept new connections to our context's server
1255 *
Andy Green6f520a52013-01-29 17:57:39 +08001256 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001257 * server or client connections.
1258 *
1259 * You need to call this service function periodically to all the above
1260 * functions to happen; if your application is single-threaded you can
1261 * just call it in your main event loop.
1262 *
1263 * Alternatively you can fork a new process that asynchronously handles
1264 * calling this service in a loop. In that case you are happy if this
1265 * call blocks your thread until it needs to take care of something and
1266 * would call it with a large nonzero timeout. Your loop then takes no
1267 * CPU while there is nothing happening.
1268 *
1269 * If you are calling it in a single-threaded app, you don't want it to
1270 * wait around blocking other things in your loop from happening, so you
1271 * would call it with a timeout_ms of 0, so it returns immediately if
1272 * nothing is pending, or as soon as it services whatever was pending.
1273 */
1274
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001275LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001276libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001277{
1278 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001279 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001280
1281 /* stay dead once we are dead */
1282
Peter Hinz56885f32011-03-02 22:03:47 +00001283 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001284 return 1;
1285
Andy Green0d338332011-02-12 11:57:43 +00001286 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001287
Peter Hinz56885f32011-03-02 22:03:47 +00001288 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001289 if (n == 0) /* poll timeout */
1290 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001291
Andy Greendfb23042013-01-17 12:26:48 +08001292 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001293 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001294
Andy Greendfb23042013-01-17 12:26:48 +08001295 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001296
Andy Greenca0a1292013-03-16 11:24:23 +08001297 for (n = 0; n < context->fds_count; n++) {
1298 if (!context->fds[n].revents)
1299 continue;
1300 m = libwebsocket_service_fd(context, &context->fds[n]);
1301 if (m < 0)
1302 return -1;
1303 /* if something closed, retry this slot */
1304 if (m)
1305 n--;
1306 }
1307
Andy Greene92cd172011-01-19 13:11:55 +00001308 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001309}
1310
Andy Green3182ece2013-01-20 17:08:31 +08001311#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001312int
1313lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001314 struct libwebsocket *wsi,
1315 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001316 void *v, size_t len)
1317{
1318 int n;
1319 int handled = 0;
1320
1321 /* maybe an extension will take care of it for us */
1322
1323 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1324 if (!wsi->active_extensions[n]->callback)
1325 continue;
1326
1327 handled |= wsi->active_extensions[n]->callback(context,
1328 wsi->active_extensions[n], wsi,
1329 r, wsi->active_extensions_user[n], v, len);
1330 }
1331
1332 return handled;
1333}
1334
1335
1336void *
1337lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001338 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001339{
1340 int n = 0;
1341
Andy Green68b45042011-05-25 21:41:57 +01001342 if (wsi == NULL)
1343 return NULL;
1344
Andy Greena41314f2011-05-23 10:00:03 +01001345 while (n < wsi->count_active_extensions) {
1346 if (wsi->active_extensions[n] != ext) {
1347 n++;
1348 continue;
1349 }
1350 return wsi->active_extensions_user[n];
1351 }
1352
1353 return NULL;
1354}
Andy Green3182ece2013-01-20 17:08:31 +08001355#endif
Andy Greena41314f2011-05-23 10:00:03 +01001356
Andy Green90c7cbc2011-01-27 06:26:52 +00001357/**
1358 * libwebsocket_callback_on_writable() - Request a callback when this socket
1359 * becomes able to be written to without
1360 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001361 *
Peter Hinz56885f32011-03-02 22:03:47 +00001362 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001363 * @wsi: Websocket connection instance to get callback for
1364 */
1365
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001366LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001367libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001368 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001369{
Andy Green3182ece2013-01-20 17:08:31 +08001370#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001371 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001372 int handled = 0;
1373
1374 /* maybe an extension will take care of it for us */
1375
1376 for (n = 0; n < wsi->count_active_extensions; n++) {
1377 if (!wsi->active_extensions[n]->callback)
1378 continue;
1379
1380 handled |= wsi->active_extensions[n]->callback(context,
1381 wsi->active_extensions[n], wsi,
1382 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1383 wsi->active_extensions_user[n], NULL, 0);
1384 }
1385
1386 if (handled)
1387 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001388#endif
Andy Greendfb23042013-01-17 12:26:48 +08001389 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001390 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1391 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001392 return -1;
1393 }
1394
1395 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001396
Andy Green3221f922011-02-12 13:14:11 +00001397 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001398 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001399 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001400 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001401
Andy Green90c7cbc2011-01-27 06:26:52 +00001402 return 1;
1403}
1404
1405/**
1406 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1407 * all connections using the given protocol when it
1408 * becomes possible to write to each socket without
1409 * blocking in turn.
1410 *
1411 * @protocol: Protocol whose connections will get callbacks
1412 */
1413
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001414LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001415libwebsocket_callback_on_writable_all_protocol(
1416 const struct libwebsocket_protocols *protocol)
1417{
Peter Hinz56885f32011-03-02 22:03:47 +00001418 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001419 int n;
Andy Green0d338332011-02-12 11:57:43 +00001420 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001421
Andy Greendfb23042013-01-17 12:26:48 +08001422 for (n = 0; n < context->fds_count; n++) {
1423 wsi = context->lws_lookup[context->fds[n].fd];
1424 if (!wsi)
1425 continue;
1426 if (wsi->protocol == protocol)
1427 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001428 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001429
1430 return 0;
1431}
1432
Andy Greenbe93fef2011-02-14 20:25:43 +00001433/**
1434 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1435 *
1436 * You will not need this unless you are doing something special
1437 *
1438 * @wsi: Websocket connection instance
1439 * @reason: timeout reason
1440 * @secs: how many seconds
1441 */
1442
1443void
1444libwebsocket_set_timeout(struct libwebsocket *wsi,
1445 enum pending_timeout reason, int secs)
1446{
1447 struct timeval tv;
1448
1449 gettimeofday(&tv, NULL);
1450
1451 wsi->pending_timeout_limit = tv.tv_sec + secs;
1452 wsi->pending_timeout = reason;
1453}
1454
Andy Greena6cbece2011-01-27 20:06:03 +00001455
1456/**
1457 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1458 *
1459 * You will not need this unless you are doing something special
1460 *
1461 * @wsi: Websocket connection instance
1462 */
1463
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001464LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001465libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1466{
1467 return wsi->sock;
1468}
1469
Andy Greend636e352013-01-29 12:36:17 +08001470#ifdef LWS_LATENCY
1471void
Andy Greenb5b23192013-02-11 17:13:32 +08001472lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1473 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001474{
1475 struct timeval tv;
1476 unsigned long u;
1477 char buf[256];
1478
1479 gettimeofday(&tv, NULL);
1480
1481 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1482
1483 if (action) {
1484 if (completed) {
1485 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001486 sprintf(buf,
1487 "Completion first try lat %luus: %p: ret %d: %s\n",
1488 u - wsi->latency_start,
1489 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001490 else
Andy Greenb5b23192013-02-11 17:13:32 +08001491 sprintf(buf,
1492 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1493 u - wsi->action_start,
1494 u - wsi->latency_start,
1495 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001496 wsi->action_start = 0;
1497 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001498 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1499 u - wsi->latency_start,
1500 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001501 if (u - wsi->latency_start > context->worst_latency) {
1502 context->worst_latency = u - wsi->latency_start;
1503 strcpy(context->worst_latency_info, buf);
1504 }
1505 lwsl_latency("%s", buf);
1506 } else {
1507 wsi->latency_start = u;
1508 if (!wsi->action_start)
1509 wsi->action_start = u;
1510 }
1511}
1512#endif
1513
Andy Greena1ce6be2013-01-18 11:43:21 +08001514#ifdef LWS_NO_SERVER
1515int
Andy Greenca0a1292013-03-16 11:24:23 +08001516_libwebsocket_rx_flow_control(struct libswebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001517{
1518 return 0;
1519}
1520#else
Andy Green706961d2013-01-17 16:50:35 +08001521int
1522_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1523{
1524 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001525
Andy Greenca0a1292013-03-16 11:24:23 +08001526 /* there is no pending change */
1527 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001528 return 0;
1529
Andy Greenca0a1292013-03-16 11:24:23 +08001530 /* stuff is still buffered, not ready to really accept new input */
1531 if (wsi->u.ws.rxflow_buffer) {
1532 /* get ourselves called back to deal with stashed buffer */
1533 libwebsocket_callback_on_writable(context, wsi);
1534 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001535 }
1536
Andy Greenca0a1292013-03-16 11:24:23 +08001537 /* pending is cleared, we can change rxflow state */
1538
1539 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1540
1541 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1542 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1543
1544 /* adjust the pollfd for this wsi */
1545
1546 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001547 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1548 else
1549 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1550
Andy Greenca0a1292013-03-16 11:24:23 +08001551 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001552 /* external POLL support via protocol 0 */
1553 context->protocols[0].callback(context, wsi,
1554 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001555 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001556 else
1557 /* external POLL support via protocol 0 */
1558 context->protocols[0].callback(context, wsi,
1559 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001560 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001561
1562 return 1;
1563}
Andy Greena1ce6be2013-01-18 11:43:21 +08001564#endif
Andy Green706961d2013-01-17 16:50:35 +08001565
Andy Green90c7cbc2011-01-27 06:26:52 +00001566/**
1567 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1568 * receieved packets.
1569 *
1570 * If the output side of a server process becomes choked, this allows flow
1571 * control for the input side.
1572 *
1573 * @wsi: Websocket connection instance to get callback for
1574 * @enable: 0 = disable read servicing for this connection, 1 = enable
1575 */
1576
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001577LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001578libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1579{
Andy Greenca0a1292013-03-16 11:24:23 +08001580 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1581 return 0;
1582
1583 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1584 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001585
Andy Green706961d2013-01-17 16:50:35 +08001586 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001587}
1588
Andy Greenb55451c2013-03-16 12:32:27 +08001589/**
1590 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1591 *
1592 * When the user server code realizes it can accept more input, it can
1593 * call this to have the RX flow restriction removed from all connections using
1594 * the given protocol.
1595 *
1596 * @protocol: all connections using this protocol will be allowed to receive
1597 */
1598
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001599LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001600libwebsocket_rx_flow_allow_all_protocol(
1601 const struct libwebsocket_protocols *protocol)
1602{
1603 struct libwebsocket_context *context = protocol->owning_server;
1604 int n;
1605 struct libwebsocket *wsi;
1606
1607 for (n = 0; n < context->fds_count; n++) {
1608 wsi = context->lws_lookup[context->fds[n].fd];
1609 if (!wsi)
1610 continue;
1611 if (wsi->protocol == protocol)
1612 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1613 }
1614}
1615
Andy Green706961d2013-01-17 16:50:35 +08001616
Andy Green2ac5a6f2011-01-28 10:00:18 +00001617/**
1618 * libwebsocket_canonical_hostname() - returns this host's hostname
1619 *
1620 * This is typically used by client code to fill in the host parameter
1621 * when making a client connection. You can only call it after the context
1622 * has been created.
1623 *
Peter Hinz56885f32011-03-02 22:03:47 +00001624 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001625 */
1626
1627
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001628LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001629libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001630{
Peter Hinz56885f32011-03-02 22:03:47 +00001631 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001632}
1633
1634
Andy Green90c7cbc2011-01-27 06:26:52 +00001635static void sigpipe_handler(int x)
1636{
1637}
1638
Andy Green6901cb32011-02-21 08:06:47 +00001639#ifdef LWS_OPENSSL_SUPPORT
1640static int
1641OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1642{
1643
1644 SSL *ssl;
1645 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001646 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001647
1648 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1649 SSL_get_ex_data_X509_STORE_CTX_idx());
1650
1651 /*
Andy Green2e24da02011-03-05 16:12:04 +00001652 * !!! nasty openssl requires the index to come as a library-scope
1653 * static
Andy Green6901cb32011-02-21 08:06:47 +00001654 */
Andy Green2e24da02011-03-05 16:12:04 +00001655 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001656
Peter Hinz56885f32011-03-02 22:03:47 +00001657 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001658 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1659 x509_ctx, ssl, preverify_ok);
1660
1661 /* convert return code from 0 = OK to 1 = OK */
1662
1663 if (!n)
1664 n = 1;
1665 else
1666 n = 0;
1667
1668 return n;
1669}
1670#endif
1671
Andy Green706961d2013-01-17 16:50:35 +08001672int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001673 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001674 struct libwebsocket *wsi,
1675 enum libwebsocket_callback_reasons reason, void *user,
1676 void *in, size_t len)
1677{
1678 int n;
1679
1680 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001681 if (!n)
1682 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001683
Andy Greenaedc9532013-02-10 21:21:24 +08001684 return n;
Andy Green706961d2013-01-17 16:50:35 +08001685}
1686
Andy Greenb45993c2010-12-18 15:13:50 +00001687
Andy Greenab990e42010-10-31 12:42:52 +00001688/**
Andy Green4739e5c2011-01-22 12:51:57 +00001689 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001690 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001691 *
Andy Green1b265272013-02-09 14:01:09 +08001692 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001693 * of all initialization in one step.
1694 *
Andy Greene92cd172011-01-19 13:11:55 +00001695 * After initialization, it returns a struct libwebsocket_context * that
1696 * represents this server. After calling, user code needs to take care
1697 * of calling libwebsocket_service() with the context pointer to get the
1698 * server's sockets serviced. This can be done in the same process context
1699 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001700 *
Andy Green8f037e42010-12-19 22:13:26 +00001701 * The protocol callback functions are called for a handful of events
1702 * including http requests coming in, websocket connections becoming
1703 * established, and data arriving; it's also called periodically to allow
1704 * async transmission.
1705 *
1706 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1707 * at that time websocket protocol has not been negotiated. Other
1708 * protocols after the first one never see any HTTP callack activity.
1709 *
1710 * The server created is a simple http server by default; part of the
1711 * websocket standard is upgrading this http connection to a websocket one.
1712 *
1713 * This allows the same server to provide files like scripts and favicon /
1714 * images or whatever over http and dynamic data over websockets all in
1715 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001716 */
Andy Green4ea60062010-10-30 12:15:07 +01001717
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001718LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001719libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001720{
Peter Hinz56885f32011-03-02 22:03:47 +00001721 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001722 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001723 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001724#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001725 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001726 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001727 struct sockaddr_in serv_addr;
1728#endif
Andy Green3182ece2013-01-20 17:08:31 +08001729#ifndef LWS_NO_EXTENSIONS
1730 int m;
Andy Green1b265272013-02-09 14:01:09 +08001731 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001732#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001733
Andy Green3faa9c72010-11-08 17:03:03 +00001734#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001735 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001736#endif
1737
Joakim Soderberg4c531232013-02-06 15:26:58 +09001738#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001739 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001740#endif
1741
Andy Greenb3a614a2013-01-19 13:08:17 +08001742 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001743 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001744 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001745 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001746#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001747 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1748 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001749#else
1750 lwsl_notice(" Configured without extension support\n");
1751#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001752 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1753 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001754 if (info->ssl_cipher_list)
1755 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001756 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1757 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001758
Peter Hinz56885f32011-03-02 22:03:47 +00001759#ifdef _WIN32
1760 {
1761 WORD wVersionRequested;
1762 WSADATA wsaData;
1763 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001764 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001765
1766 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1767 wVersionRequested = MAKEWORD(2, 2);
1768
1769 err = WSAStartup(wVersionRequested, &wsaData);
1770 if (err != 0) {
1771 /* Tell the user that we could not find a usable */
1772 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001773 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001774 return NULL;
1775 }
David Galeano7b11fec2011-10-04 19:55:18 +08001776
Andy Green6ee372f2012-04-09 15:09:01 +08001777 /* default to a poll() made out of select() */
1778 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001779
Andy Green6ee372f2012-04-09 15:09:01 +08001780 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001781 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001782 if (wsdll)
1783 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001784
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001785 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001786 if (!poll)
1787 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001788 }
1789#endif
1790
Andy Greenb5b23192013-02-11 17:13:32 +08001791 context = (struct libwebsocket_context *)
1792 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001793 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001794 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001795 return NULL;
1796 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001797 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001798#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001799 context->started_with_parent = pid_daemon;
1800 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1801#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001802
Joakim Soderberg4c531232013-02-06 15:26:58 +09001803 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001804 context->protocols = info->protocols;
1805 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001806 context->http_proxy_port = 0;
1807 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001808 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001809 /* to reduce this allocation, */
1810 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001811 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1812 sizeof(struct libwebsocket_context),
1813 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1814 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001815 sizeof(struct libwebsocket_context) +
1816 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1817 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001818
Andy Greenb5b23192013-02-11 17:13:32 +08001819 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1820 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001821 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001822 lwsl_err("Unable to allocate fds array for %d connections\n",
1823 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001824 free(context);
1825 return NULL;
1826 }
Andy Greenb5b23192013-02-11 17:13:32 +08001827 context->lws_lookup = (struct libwebsocket **)
1828 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001829 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001830 lwsl_err(
1831 "Unable to allocate lws_lookup array for %d connections\n",
1832 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001833 free(context->fds);
1834 free(context);
1835 return NULL;
1836 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001837 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1838 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001839
Peter Hinz56885f32011-03-02 22:03:47 +00001840 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001841#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001842 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001843#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001844 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001845 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001846
Peter Hinz56885f32011-03-02 22:03:47 +00001847#ifdef WIN32
1848 context->fd_random = 0;
1849#else
1850 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1851 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001852 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001853 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001854 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001855 }
Peter Hinz56885f32011-03-02 22:03:47 +00001856#endif
Andy Green44eee682011-02-10 09:32:24 +00001857
Peter Hinz56885f32011-03-02 22:03:47 +00001858#ifdef LWS_OPENSSL_SUPPORT
1859 context->use_ssl = 0;
1860 context->ssl_ctx = NULL;
1861 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001862 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001863#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001864
Andy Greena1ce6be2013-01-18 11:43:21 +08001865 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001866
Andy Greena1ce6be2013-01-18 11:43:21 +08001867#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001868 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001869 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001870 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001871 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001872
Andy Greenb5b23192013-02-11 17:13:32 +08001873 lwsl_notice(" canonical_hostname = %s\n",
1874 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001875 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001876#endif
Andy Greena69f0512012-05-03 12:32:38 +08001877
Andy Green9659f372011-01-27 22:01:43 +00001878 /* split the proxy ads:port if given */
1879
1880 p = getenv("http_proxy");
1881 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001882 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001883 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001884 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001885 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001886
Peter Hinz56885f32011-03-02 22:03:47 +00001887 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001888 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001889 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001890 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001891 }
1892 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001893 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001894
Andy Greenb3a614a2013-01-19 13:08:17 +08001895 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001896 context->http_proxy_address,
1897 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001898 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001899
Andy Greena1ce6be2013-01-18 11:43:21 +08001900#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001901 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001902
Andy Green3faa9c72010-11-08 17:03:03 +00001903#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001904 context->use_ssl = info->ssl_cert_filepath != NULL &&
1905 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001906#ifdef USE_CYASSL
1907 lwsl_notice(" Compiled with CYASSL support\n");
1908#else
1909 lwsl_notice(" Compiled with OpenSSL support\n");
1910#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001911 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001912 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001913 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001914 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001915
Andy Green90c7cbc2011-01-27 06:26:52 +00001916#else
Andy Green2b40b792013-02-10 22:22:01 +08001917 if (info->ssl_cert_filepath != NULL &&
1918 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001919 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001920 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001921 }
Andy Greenb5b23192013-02-11 17:13:32 +08001922 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001923#endif
Andy Greena17c6922013-01-20 20:21:54 +08001924
Andy Greenb5b23192013-02-11 17:13:32 +08001925 lwsl_notice(
1926 " per-conn mem: %u + %u headers + protocol rx buf\n",
1927 sizeof(struct libwebsocket),
1928 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001929 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001930#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001931
1932 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001933#ifdef WIN32
1934#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001935 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001936#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001937
1938
1939#ifdef LWS_OPENSSL_SUPPORT
1940
1941 /* basic openssl init */
1942
1943 SSL_library_init();
1944
1945 OpenSSL_add_all_algorithms();
1946 SSL_load_error_strings();
1947
Andy Green2e24da02011-03-05 16:12:04 +00001948 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001949 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1950
Andy Green90c7cbc2011-01-27 06:26:52 +00001951 /*
1952 * Firefox insists on SSLv23 not SSLv3
1953 * Konq disables SSLv2 by default now, SSLv23 works
1954 */
1955
1956 method = (SSL_METHOD *)SSLv23_server_method();
1957 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001958 lwsl_err("problem creating ssl method %lu: %s\n",
1959 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001960 ERR_error_string(ERR_get_error(),
1961 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001962 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001963 }
Peter Hinz56885f32011-03-02 22:03:47 +00001964 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1965 if (!context->ssl_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001966 lwsl_err("problem creating ssl context %lu: %s\n",
1967 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001968 ERR_error_string(ERR_get_error(),
1969 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001970 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001971 }
1972
David Galeanocc148e42013-01-10 10:18:59 +08001973#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001974 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001975#endif
David Galeano77a677c2013-01-10 10:14:12 +08001976 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001977 if (info->ssl_cipher_list)
1978 SSL_CTX_set_cipher_list(context->ssl_ctx,
1979 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001980
Andy Greena1ce6be2013-01-18 11:43:21 +08001981#ifndef LWS_NO_CLIENT
1982
Andy Green90c7cbc2011-01-27 06:26:52 +00001983 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001984
Andy Green1b265272013-02-09 14:01:09 +08001985 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001986 method = (SSL_METHOD *)SSLv23_client_method();
1987 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001988 lwsl_err("problem creating ssl method %lu: %s\n",
1989 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001990 ERR_error_string(ERR_get_error(),
1991 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001992 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001993 }
1994 /* create context */
1995 context->ssl_client_ctx = SSL_CTX_new(method);
1996 if (!context->ssl_client_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001997 lwsl_err("problem creating ssl context %lu: %s\n",
1998 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001999 ERR_error_string(ERR_get_error(),
2000 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002001 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002002 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002003
David Galeanocc148e42013-01-10 10:18:59 +08002004#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002005 SSL_CTX_set_options(context->ssl_client_ctx,
2006 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002007#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002008 SSL_CTX_set_options(context->ssl_client_ctx,
2009 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002010 if (info->ssl_cipher_list)
2011 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2012 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002013
Peter Hinz56885f32011-03-02 22:03:47 +00002014 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002015 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002016 if (!SSL_CTX_load_verify_locations(
2017 context->ssl_client_ctx, NULL,
2018 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002019 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002020 "Unable to load SSL Client certs from %s "
2021 "(set by --with-client-cert-dir= "
2022 "in configure) -- client ssl isn't "
2023 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002024 } else
2025 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002026 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002027 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002028 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002029 "Unable to load SSL Client certs "
2030 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002031 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002032
2033 /*
2034 * callback allowing user code to load extra verification certs
2035 * helping the client to verify server identity
2036 */
2037
2038 context->protocols[0].callback(context, NULL,
2039 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2040 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002041 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002042#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002043
Andy Greenc6bf2c22011-02-20 11:10:47 +00002044 /* as a server, are we requiring clients to identify themselves? */
2045
Andy Greenb5b23192013-02-11 17:13:32 +08002046 if (info->options &
2047 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002048
2049 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002050
Peter Hinz56885f32011-03-02 22:03:47 +00002051 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002052 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2053 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002054
2055 /*
2056 * give user code a chance to load certs into the server
2057 * allowing it to verify incoming client certs
2058 */
2059
Peter Hinz56885f32011-03-02 22:03:47 +00002060 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002061 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002062 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002063 }
2064
Peter Hinz56885f32011-03-02 22:03:47 +00002065 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002066
2067 /* openssl init for server sockets */
2068
Andy Green3faa9c72010-11-08 17:03:03 +00002069 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002070 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002071 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002072 if (n != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002073 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002074 info->ssl_cert_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002075 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002076 ERR_error_string(ERR_get_error(),
2077 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002078 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002079 }
2080 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002081 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002082 info->ssl_private_key_filepath,
2083 SSL_FILETYPE_PEM) != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002084 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002085 info->ssl_private_key_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002086 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002087 ERR_error_string(ERR_get_error(),
2088 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002089 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002090 }
2091 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002092 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002093 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002094 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002095 }
2096
2097 /* SSL is happy and has a cert it's content with */
2098 }
2099#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002100
Andy Greendf736162011-01-18 15:39:02 +00002101 /* selftest */
2102
2103 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002104 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002105
Andy Greena1ce6be2013-01-18 11:43:21 +08002106#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002107 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002108
Andy Green1b265272013-02-09 14:01:09 +08002109 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002110 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002111
Andy Green4739e5c2011-01-22 12:51:57 +00002112 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2113 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002114 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002115 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002116 }
Andy Green775c0dd2010-10-29 14:15:22 +01002117
Joakim Soderberg4c531232013-02-06 15:26:58 +09002118#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002119 /*
2120 * allow us to restart even if old sockets in TIME_WAIT
2121 * (REUSEADDR on Unix means, "don't hang on to this
2122 * address after the listener is closed." On Windows, though,
2123 * it means "don't keep other processes from binding to
2124 * this address while we're using it)
2125 */
Andy Green6ee372f2012-04-09 15:09:01 +08002126 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2127 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002128#endif
Andy Green6c939552011-03-08 08:56:57 +00002129
2130 /* Disable Nagle */
2131 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002132 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2133 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002134
Joakim Soderberg4c531232013-02-06 15:26:58 +09002135 #ifdef WIN32
2136 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002137 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002138 #else
Andy Greene2160712013-01-28 12:19:10 +08002139 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002140 #endif
Andy Greene2160712013-01-28 12:19:10 +08002141
Andy Green4739e5c2011-01-22 12:51:57 +00002142 bzero((char *) &serv_addr, sizeof(serv_addr));
2143 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002144 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002145 serv_addr.sin_addr.s_addr = INADDR_ANY;
2146 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002147 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002148 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002149 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002150
2151 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2152 sizeof(serv_addr));
2153 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002154 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002155 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002156 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002157 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002158 }
Andy Green0d338332011-02-12 11:57:43 +00002159
Andy Greenb5b23192013-02-11 17:13:32 +08002160 wsi = (struct libwebsocket *)malloc(
2161 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002162 if (wsi == NULL) {
2163 lwsl_err("Out of mem\n");
2164 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002165 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002166 }
Andy Greenb5b23192013-02-11 17:13:32 +08002167 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002168 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002169#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002170 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002171#endif
Andy Green0d338332011-02-12 11:57:43 +00002172 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002173
2174 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002175
Andy Green65b0e912013-01-16 07:59:47 +08002176 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2177 context->listen_service_count = 0;
2178 context->listen_service_fd = sockfd;
2179
Andy Greena824d182013-01-15 20:52:29 +08002180 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002181 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002182 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002183#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002184
Andy Green6ee372f2012-04-09 15:09:01 +08002185 /*
2186 * drop any root privs for this process
2187 * to listen on port < 1023 we would have needed root, but now we are
2188 * listening, we don't want the power for anything else
2189 */
Peter Hinz56885f32011-03-02 22:03:47 +00002190#ifdef WIN32
2191#else
Andy Green1b265272013-02-09 14:01:09 +08002192 if (info->gid != -1)
2193 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002194 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002195 if (info->uid != -1)
2196 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002197 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002198#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002199
Andy Green6f520a52013-01-29 17:57:39 +08002200 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002201
Peter Hinz56885f32011-03-02 22:03:47 +00002202 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002203 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002204 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002205
Andy Green43db0452013-01-10 19:50:35 +08002206 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002207 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002208
Andy Green1b265272013-02-09 14:01:09 +08002209 info->protocols[context->count_protocols].owning_server =
2210 context;
2211 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002212 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002213
2214 /*
2215 * inform all the protocols that they are doing their one-time
2216 * initialization if they want to
2217 */
2218 info->protocols[context->count_protocols].callback(context,
2219 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002220 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002221
Andy Green3182ece2013-01-20 17:08:31 +08002222#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002223 /*
2224 * give all extensions a chance to create any per-context
2225 * allocations they need
2226 */
2227
2228 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002229 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002230 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002231
Andy Green1b265272013-02-09 14:01:09 +08002232 if (info->extensions) {
2233 ext = info->extensions;
2234 while (ext->callback) {
2235 lwsl_ext(" Extension: %s\n", ext->name);
2236 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002237 (enum libwebsocket_extension_callback_reasons)m,
2238 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002239 ext++;
2240 }
Andy Greena41314f2011-05-23 10:00:03 +01002241 }
Andy Green3182ece2013-01-20 17:08:31 +08002242#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002243 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002244
2245bail:
2246 libwebsocket_context_destroy(context);
2247 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002248}
Andy Greenb45993c2010-12-18 15:13:50 +00002249
Andy Greenb45993c2010-12-18 15:13:50 +00002250/**
2251 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002252 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002253 * @wsi: pointer to struct websocket you want to know the protocol of
2254 *
Andy Green8f037e42010-12-19 22:13:26 +00002255 *
Andy Green6f520a52013-01-29 17:57:39 +08002256 * Some apis can act on all live connections of a given protocol,
2257 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002258 */
Andy Greenab990e42010-10-31 12:42:52 +00002259
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002260LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002261libwebsockets_get_protocol(struct libwebsocket *wsi)
2262{
2263 return wsi->protocol;
2264}
2265
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002266LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002267libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2268{
Andy Green623a98d2013-01-21 11:04:23 +08002269 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002270}
Alex Bligh49146db2011-11-07 17:19:25 +08002271
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002272LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002273libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2274{
Andy Green623a98d2013-01-21 11:04:23 +08002275 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002276}
2277
Andy Green2af4d5b2013-02-18 16:30:10 +08002278int
Alex Bligh49146db2011-11-07 17:19:25 +08002279libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2280{
Andy Green67d556c2013-02-15 22:31:55 +08002281 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002282 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002283
Alex Bligh49146db2011-11-07 17:19:25 +08002284 /* allocate the per-connection user memory (if any) */
2285
2286 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2287 wsi->user_space = malloc(
2288 wsi->protocol->per_session_data_size);
2289 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002290 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002291 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002292 }
Andy Green6ee372f2012-04-09 15:09:01 +08002293 memset(wsi->user_space, 0,
2294 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002295 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002296 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002297}
Andy Green43db0452013-01-10 19:50:35 +08002298
Andy Green0b319092013-01-19 11:17:56 +08002299static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002300{
Andy Green0b319092013-01-19 11:17:56 +08002301 char buf[300];
2302 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002303 int n;
2304
2305 gettimeofday(&tv, NULL);
2306
2307 buf[0] = '\0';
2308 for (n = 0; n < LLL_COUNT; n++)
2309 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002310 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002311 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002312 break;
2313 }
Andy Greenb5b23192013-02-11 17:13:32 +08002314
Andy Green0b319092013-01-19 11:17:56 +08002315 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002316}
2317
Joakim Soderberg4c531232013-02-06 15:26:58 +09002318#ifdef WIN32
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002319LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002320{
2321 lwsl_emit_stderr(level, line);
2322}
2323#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002324LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002325{
2326 int syslog_level = LOG_DEBUG;
2327
2328 switch (level) {
2329 case LLL_ERR:
2330 syslog_level = LOG_ERR;
2331 break;
2332 case LLL_WARN:
2333 syslog_level = LOG_WARNING;
2334 break;
2335 case LLL_NOTICE:
2336 syslog_level = LOG_NOTICE;
2337 break;
2338 case LLL_INFO:
2339 syslog_level = LOG_INFO;
2340 break;
2341 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002342 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002343}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002344#endif
Andy Greenc11db202013-01-19 11:12:16 +08002345
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002346LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002347{
Andy Greende8f27a2013-01-12 09:17:42 +08002348 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002349 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002350
2351 if (!(log_level & filter))
2352 return;
2353
Andy Green43db0452013-01-10 19:50:35 +08002354 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002355 vsnprintf(buf, sizeof(buf), format, ap);
2356 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002357 va_end(ap);
2358
Andy Green0b319092013-01-19 11:17:56 +08002359 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002360}
2361
2362/**
2363 * lws_set_log_level() - Set the logging bitfield
2364 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002365 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2366 * function to perform log string emission instead of
2367 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002368 *
Andy Greenc6511a02013-02-19 10:01:48 +08002369 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002370 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002371 */
2372
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002373LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002374 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002375{
2376 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002377 if (log_emit_function)
2378 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002379}