blob: d312390d4cae554ab98d0e8322d99d580c6e6f83 [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;
213 }
214
Andy Green3182ece2013-01-20 17:08:31 +0800215#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000216 /*
Andy Green68b45042011-05-25 21:41:57 +0100217 * are his extensions okay with him closing? Eg he might be a mux
218 * parent and just his ch1 aspect is closing?
219 */
220
Andy Green68b45042011-05-25 21:41:57 +0100221 for (n = 0; n < wsi->count_active_extensions; n++) {
222 if (!wsi->active_extensions[n]->callback)
223 continue;
224
225 m = wsi->active_extensions[n]->callback(context,
226 wsi->active_extensions[n], wsi,
227 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
228 wsi->active_extensions_user[n], NULL, 0);
229
230 /*
231 * if somebody vetoed actually closing him at this time....
232 * up to the extension to track the attempted close, let's
233 * just bail
234 */
235
236 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800237 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100238 return;
239 }
240 }
241
Andy Green68b45042011-05-25 21:41:57 +0100242 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000243 * flush any tx pending from extensions, since we may send close packet
244 * if there are problems with send, just nuke the connection
245 */
246
247 ret = 1;
248 while (ret == 1) {
249
250 /* default to nobody has more to spill */
251
252 ret = 0;
253 eff_buf.token = NULL;
254 eff_buf.token_len = 0;
255
256 /* show every extension the new incoming data */
257
258 for (n = 0; n < wsi->count_active_extensions; n++) {
259 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000260 wsi->protocol->owning_server,
261 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000262 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
263 wsi->active_extensions_user[n], &eff_buf, 0);
264 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800265 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000266 goto just_kill_connection;
267 }
268 if (m)
269 /*
270 * at least one extension told us he has more
271 * to spill, so we will go around again after
272 */
273 ret = 1;
274 }
275
276 /* assuming they left us something to send, send it */
277
278 if (eff_buf.token_len)
279 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800280 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800281 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000282 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800283 }
Andy Greenc44159f2011-03-07 07:08:18 +0000284 }
Andy Green3182ece2013-01-20 17:08:31 +0800285#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000286
287 /*
Andy Greenda527df2011-03-07 07:08:12 +0000288 * signal we are closing, libsocket_write will
289 * add any necessary version-specific stuff. If the write fails,
290 * no worries we are closing anyway. If we didn't initiate this
291 * close, then our state has been changed to
292 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
293 *
294 * Likewise if it's a second call to close this connection after we
295 * sent the close indication to the peer already, we are in state
296 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
297 */
298
299 if (old_state == WSI_STATE_ESTABLISHED &&
300 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100301
Andy Green43db0452013-01-10 19:50:35 +0800302 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100303
Andy Greenfdd305a2013-02-11 14:32:48 +0800304 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800305 memset(buf, 0, sizeof(buf));
306 n = libwebsocket_write(wsi,
307 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000308 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800309 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000310 /*
311 * we have sent a nice protocol level indication we
312 * now wish to close, we should not send anything more
313 */
314
315 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
316
Andy Greenb5b23192013-02-11 17:13:32 +0800317 /*
318 * ...and we should wait for a reply for a bit
319 * out of politeness
320 */
Andy Greenda527df2011-03-07 07:08:12 +0000321
322 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800323 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000324
Andy Green43db0452013-01-10 19:50:35 +0800325 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000326
327 return;
328 }
329
Andy Greenb5b23192013-02-11 17:13:32 +0800330 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800331
Andy Greenda527df2011-03-07 07:08:12 +0000332 /* else, the send failed and we should just hang up */
333 }
334
Andy Greenc44159f2011-03-07 07:08:18 +0000335just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100336
Andy Greenb5b23192013-02-11 17:13:32 +0800337 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100338
Andy Greenda527df2011-03-07 07:08:12 +0000339 /*
340 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800341 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000342 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000343
Andy Greendfb23042013-01-17 12:26:48 +0800344 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000345
Andy Green251f6fa2010-11-03 11:13:06 +0000346 wsi->state = WSI_STATE_DEAD_SOCKET;
347
Andy Greenb5b23192013-02-11 17:13:32 +0800348 if ((old_state == WSI_STATE_ESTABLISHED ||
349 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800350 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
351
352 if (wsi->u.ws.rx_user_buffer) {
353 free(wsi->u.ws.rx_user_buffer);
354 wsi->u.ws.rx_user_buffer = NULL;
355 }
356 if (wsi->u.ws.rxflow_buffer) {
357 free(wsi->u.ws.rxflow_buffer);
358 wsi->u.ws.rxflow_buffer = NULL;
359 }
Andy Green54495112013-02-06 21:10:16 +0900360 }
361
Andy Green4b6fbe12011-02-14 08:03:48 +0000362 /* tell the user it's all over for this guy */
363
Andy Greend4302732011-02-28 07:45:29 +0000364 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800365 ((old_state == WSI_STATE_ESTABLISHED) ||
366 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
367 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800368 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000369 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000370 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800371 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800372 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000373
Andy Green3182ece2013-01-20 17:08:31 +0800374#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000375 /* deallocate any active extension contexts */
376
377 for (n = 0; n < wsi->count_active_extensions; n++) {
378 if (!wsi->active_extensions[n]->callback)
379 continue;
380
Andy Green46c2ea02011-03-22 09:04:01 +0000381 wsi->active_extensions[n]->callback(context,
382 wsi->active_extensions[n], wsi,
383 LWS_EXT_CALLBACK_DESTROY,
384 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000385
386 free(wsi->active_extensions_user[n]);
387 }
388
Andy Greena41314f2011-05-23 10:00:03 +0100389 /*
390 * inform all extensions in case they tracked this guy out of band
391 * even though not active on him specifically
392 */
393
394 ext = context->extensions;
395 while (ext && ext->callback) {
396 ext->callback(context, ext, wsi,
397 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
398 NULL, NULL, 0);
399 ext++;
400 }
Andy Green3182ece2013-01-20 17:08:31 +0800401#endif
Andy Greena41314f2011-05-23 10:00:03 +0100402
Andy Green43db0452013-01-10 19:50:35 +0800403/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000404
Andy Green3faa9c72010-11-08 17:03:03 +0000405#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000406 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000407 n = SSL_get_fd(wsi->ssl);
408 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800409 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000410 SSL_free(wsi->ssl);
411 } else {
412#endif
Andy Green0303db42013-01-17 14:46:43 +0800413 if (wsi->sock) {
414 n = shutdown(wsi->sock, SHUT_RDWR);
415 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800416 lwsl_debug("closing: shutdown returned %d\n",
417 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800418
Andy Green0303db42013-01-17 14:46:43 +0800419 n = compatible_close(wsi->sock);
420 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800421 lwsl_debug("closing: close returned %d\n",
422 errno);
Andy Green0303db42013-01-17 14:46:43 +0800423 }
Andy Green3faa9c72010-11-08 17:03:03 +0000424#ifdef LWS_OPENSSL_SUPPORT
425 }
426#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800427 if (wsi->protocol && wsi->protocol->per_session_data_size &&
428 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000429 free(wsi->user_space);
430
Andy Green251f6fa2010-11-03 11:13:06 +0000431 free(wsi);
432}
433
Andy Green07034092011-02-13 08:37:12 +0000434/**
435 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800436 * @context: Libwebsockets context
437 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000438 * @fd: Connection socket descriptor
439 * @name: Buffer to take client address name
440 * @name_len: Length of client address name buffer
441 * @rip: Buffer to take client address IP qotted quad
442 * @rip_len: Length of client address IP buffer
443 *
444 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800445 * the client connected with socket descriptor @fd. Names may be
446 * truncated if there is not enough room. If either cannot be
447 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000448 */
449
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800450LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800451libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
452 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000453 char *rip, int rip_len)
454{
Bob Robertsac049112013-04-25 09:16:30 +0800455 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000456 struct sockaddr_in sin;
457 struct hostent *host;
458 struct hostent *host1;
459 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000460 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000461 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800462 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800463#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800464 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800465#endif
Andy Green07034092011-02-13 08:37:12 +0000466
467 rip[0] = '\0';
468 name[0] = '\0';
469
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800470 lws_latency_pre(context, wsi);
471
Andy Greenb5b23192013-02-11 17:13:32 +0800472 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000473 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
474 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800475 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000476 }
Andy Green6ee372f2012-04-09 15:09:01 +0800477
Andy Greenb5b23192013-02-11 17:13:32 +0800478 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000479 AF_INET);
480 if (host == NULL) {
481 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800482 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000483 }
484
485 strncpy(name, host->h_name, name_len);
486 name[name_len - 1] = '\0';
487
488 host1 = gethostbyname(host->h_name);
489 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800490 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000491 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000492 n = 0;
493 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000494 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000495 if (p == NULL)
496 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000497 if ((host1->h_addrtype != AF_INET)
498#ifdef AF_LOCAL
499 && (host1->h_addrtype != AF_LOCAL)
500#endif
501 )
Andy Green07034092011-02-13 08:37:12 +0000502 continue;
503
Andy Green7627af52011-03-09 15:13:52 +0000504 if (host1->h_addrtype == AF_INET)
505 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000506#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000507 else {
508 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800509 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000510 ip[sizeof(ip) - 1] = '\0';
511 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000512#endif
Andy Green07034092011-02-13 08:37:12 +0000513 p = NULL;
514 strncpy(rip, ip, rip_len);
515 rip[rip_len - 1] = '\0';
516 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800517
518 ret = 0;
519bail:
520 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000521}
Andy Green9f990342011-02-12 11:57:45 +0000522
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800523LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000524 void *buf, int len)
525{
526 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800527 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000528
529#ifdef WIN32
530 for (n = 0; n < len; n++)
531 p[n] = (unsigned char)rand();
532#else
533 n = read(context->fd_random, p, len);
534#endif
535
536 return n;
537}
538
Andy Greena690cd02013-02-09 12:25:31 +0800539int lws_set_socket_options(struct libwebsocket_context *context, int fd)
540{
541 int optval = 1;
542 socklen_t optlen = sizeof(optval);
543#ifdef WIN32
544 unsigned long optl = 0;
545#endif
546#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
547 struct protoent *tcp_proto;
548#endif
549
550 if (context->ka_time) {
551 /* enable keepalive on this socket */
552 optval = 1;
553 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
554 (const void *)&optval, optlen) < 0)
555 return 1;
556
Bob Robertsac049112013-04-25 09:16:30 +0800557#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800558
559 /*
560 * didn't find a way to set these per-socket, need to
561 * tune kernel systemwide values
562 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100563#elif WIN32
564 {
565 DWORD dwBytesRet;
566 struct tcp_keepalive alive;
567 alive.onoff = TRUE;
568 alive.keepalivetime = context->ka_time;
569 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800570
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100571 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
572 NULL, 0, &dwBytesRet, NULL, NULL))
573 return 1;
574 }
Andy Greena47865f2013-02-10 09:39:47 +0800575#else
Andy Greena690cd02013-02-09 12:25:31 +0800576 /* set the keepalive conditions we want on it too */
577 optval = context->ka_time;
578 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
579 (const void *)&optval, optlen) < 0)
580 return 1;
581
Larry Hayesbb66ac62013-02-22 09:16:20 +0800582 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800583 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
584 (const void *)&optval, optlen) < 0)
585 return 1;
586
Larry Hayesbb66ac62013-02-22 09:16:20 +0800587 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800588 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
589 (const void *)&optval, optlen) < 0)
590 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800591#endif
Andy Greena690cd02013-02-09 12:25:31 +0800592 }
593
594 /* Disable Nagle */
595 optval = 1;
596#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
597 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
598#else
599 tcp_proto = getprotobyname("TCP");
600 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
601#endif
602
603 /* We are nonblocking... */
604#ifdef WIN32
605 ioctlsocket(fd, FIONBIO, &optl);
606#else
607 fcntl(fd, F_SETFL, O_NONBLOCK);
608#endif
609
610 return 0;
611}
612
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800613LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000614{
615 struct pollfd fds;
616
617 fds.fd = wsi->sock;
618 fds.events = POLLOUT;
619 fds.revents = 0;
620
621 if (poll(&fds, 1, 0) != 1)
622 return 1;
623
624 if ((fds.revents & POLLOUT) == 0)
625 return 1;
626
627 /* okay to send another packet without blocking */
628
629 return 0;
630}
631
Andy Greena41314f2011-05-23 10:00:03 +0100632int
Andy Green3b84c002011-03-06 13:14:42 +0000633lws_handle_POLLOUT_event(struct libwebsocket_context *context,
634 struct libwebsocket *wsi, struct pollfd *pollfd)
635{
Andy Green3b84c002011-03-06 13:14:42 +0000636 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800637
Andy Green3182ece2013-01-20 17:08:31 +0800638#ifndef LWS_NO_EXTENSIONS
639 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000640 int ret;
641 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100642 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000643
Andy Greena41314f2011-05-23 10:00:03 +0100644 for (n = 0; n < wsi->count_active_extensions; n++) {
645 if (!wsi->active_extensions[n]->callback)
646 continue;
647
648 m = wsi->active_extensions[n]->callback(context,
649 wsi->active_extensions[n], wsi,
650 LWS_EXT_CALLBACK_IS_WRITEABLE,
651 wsi->active_extensions_user[n], NULL, 0);
652 if (m > handled)
653 handled = m;
654 }
655
656 if (handled == 1)
657 goto notify_action;
658
659 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000660 goto user_service;
661
662 /*
663 * check in on the active extensions, see if they
664 * had pending stuff to spill... they need to get the
665 * first look-in otherwise sequence will be disordered
666 *
667 * NULL, zero-length eff_buf means just spill pending
668 */
669
670 ret = 1;
671 while (ret == 1) {
672
673 /* default to nobody has more to spill */
674
675 ret = 0;
676 eff_buf.token = NULL;
677 eff_buf.token_len = 0;
678
679 /* give every extension a chance to spill */
680
681 for (n = 0; n < wsi->count_active_extensions; n++) {
682 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000683 wsi->protocol->owning_server,
684 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000685 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
686 wsi->active_extensions_user[n], &eff_buf, 0);
687 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800688 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000689 return -1;
690 }
691 if (m)
692 /*
693 * at least one extension told us he has more
694 * to spill, so we will go around again after
695 */
696 ret = 1;
697 }
698
699 /* assuming they gave us something to send, send it */
700
701 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800702 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
703 eff_buf.token_len);
704 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000705 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800706 /*
707 * Keep amount spilled small to minimize chance of this
708 */
709 if (n != eff_buf.token_len) {
710 lwsl_err("Unable to spill ext %d vs %s\n",
711 eff_buf.token_len, n);
712 return -1;
713 }
Andy Green3b84c002011-03-06 13:14:42 +0000714 } else
715 continue;
716
717 /* no extension has more to spill */
718
719 if (!ret)
720 continue;
721
722 /*
723 * There's more to spill from an extension, but we just sent
724 * something... did that leave the pipe choked?
725 */
726
727 if (!lws_send_pipe_choked(wsi))
728 /* no we could add more */
729 continue;
730
Andy Green43db0452013-01-10 19:50:35 +0800731 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000732
733 /*
734 * Yes, he's choked. Leave the POLLOUT masked on so we will
735 * come back here when he is unchoked. Don't call the user
736 * callback to enforce ordering of spilling, he'll get called
737 * when we come back here and there's nothing more to spill.
738 */
739
740 return 0;
741 }
742
743 wsi->extension_data_pending = 0;
744
745user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800746#endif
Andy Green3b84c002011-03-06 13:14:42 +0000747 /* one shot */
748
Andy Greena41314f2011-05-23 10:00:03 +0100749 if (pollfd) {
750 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000751
Andy Greena41314f2011-05-23 10:00:03 +0100752 /* external POLL support via protocol 0 */
753 context->protocols[0].callback(context, wsi,
754 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800755 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100756 }
Andy Green3182ece2013-01-20 17:08:31 +0800757#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100758notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800759#endif
Andy Green3b84c002011-03-06 13:14:42 +0000760
Andy Green9e4c2b62011-03-07 20:47:39 +0000761 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
762 n = LWS_CALLBACK_CLIENT_WRITEABLE;
763 else
764 n = LWS_CALLBACK_SERVER_WRITEABLE;
765
Andy Greenb8b247d2013-01-22 07:20:08 +0800766 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800767 wsi, (enum libwebsocket_callback_reasons) n,
768 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000769}
770
771
772
Andy Green1c6e1422013-02-20 19:11:31 +0800773int
Andy Greena41314f2011-05-23 10:00:03 +0100774libwebsocket_service_timeout_check(struct libwebsocket_context *context,
775 struct libwebsocket *wsi, unsigned int sec)
776{
Andy Green3182ece2013-01-20 17:08:31 +0800777#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100778 int n;
779
780 /*
781 * if extensions want in on it (eg, we are a mux parent)
782 * give them a chance to service child timeouts
783 */
784
785 for (n = 0; n < wsi->count_active_extensions; n++)
786 wsi->active_extensions[n]->callback(
787 context, wsi->active_extensions[n],
788 wsi, LWS_EXT_CALLBACK_1HZ,
789 wsi->active_extensions_user[n], NULL, sec);
790
Andy Green3182ece2013-01-20 17:08:31 +0800791#endif
Andy Greena41314f2011-05-23 10:00:03 +0100792 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800793 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800794
Andy Greena41314f2011-05-23 10:00:03 +0100795 /*
796 * if we went beyond the allowed time, kill the
797 * connection
798 */
799
800 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800801 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100802 libwebsocket_close_and_free_session(context,
803 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800804 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100805 }
Andy Green1c6e1422013-02-20 19:11:31 +0800806
807 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100808}
809
Andy Green9f990342011-02-12 11:57:45 +0000810/**
811 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000812 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000813 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800814 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000815 *
Andy Green75006172013-01-22 12:32:11 +0800816 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800817 * services it according to the state of the associated
818 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800819 *
820 * The one call deals with all "service" that might happen on a socket
821 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800822 *
823 * If a pollfd says it has something, you can just pass it to
824 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
825 * If it sees it is a lws socket, the traffic will be handled and
826 * pollfd->revents will be zeroed now.
827 *
828 * If the socket is foreign to lws, it leaves revents alone. So you can
829 * see if you should service yourself by checking the pollfd revents
830 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000831 */
832
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800833LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000834libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000835 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000836{
Andy Greena1ce6be2013-01-18 11:43:21 +0800837 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000838 int n;
Andy Green0d338332011-02-12 11:57:43 +0000839 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800840 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000841 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800842 int timed_out = 0;
843 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800844 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800845
Andy Green3182ece2013-01-20 17:08:31 +0800846#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000847 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800848#endif
Andy Green98a717c2011-03-06 13:14:15 +0000849 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800850
851 if (context->listen_service_fd)
852 listen_socket_fds_index = context->lws_lookup[
853 context->listen_service_fd]->position_in_fds_table;
854
Andy Greena71eafc2011-02-14 17:59:43 +0000855 /*
856 * you can call us with pollfd = NULL to just allow the once-per-second
857 * global timeout checks; if less than a second since the last check
858 * it returns immediately then.
859 */
860
861 gettimeofday(&tv, NULL);
862
Peter Hinz56885f32011-03-02 22:03:47 +0000863 if (context->last_timeout_check_s != tv.tv_sec) {
864 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000865
Joakim Soderberg4c531232013-02-06 15:26:58 +0900866 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800867 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800868 if (context->started_with_parent &&
869 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800870 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900871 #endif
Andy Green24cba922013-01-19 13:56:10 +0800872
Andy Greena71eafc2011-02-14 17:59:43 +0000873 /* global timeout check once per second */
874
Andy Green1c6e1422013-02-20 19:11:31 +0800875 if (pollfd)
876 our_fd = pollfd->fd;
877
Peter Hinz56885f32011-03-02 22:03:47 +0000878 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800879 m = context->fds[n].fd;
880 wsi = context->lws_lookup[m];
881 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800882 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800883
884 if (libwebsocket_service_timeout_check(context, wsi,
885 tv.tv_sec))
886 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800887 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800888 /* it was the guy we came to service! */
889 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800890 /* mark as handled */
891 pollfd->revents = 0;
892 }
Andy Greena71eafc2011-02-14 17:59:43 +0000893 }
894 }
895
Andy Green1c6e1422013-02-20 19:11:31 +0800896 /* the socket we came to service timed out, nothing to do */
897 if (timed_out)
898 return 0;
899
Andy Greena71eafc2011-02-14 17:59:43 +0000900 /* just here for timeout management? */
901
902 if (pollfd == NULL)
903 return 0;
904
905 /* no, here to service a socket descriptor */
906
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800907 wsi = context->lws_lookup[pollfd->fd];
908 if (wsi == NULL)
909 /* not lws connection ... leave revents alone and return */
910 return 0;
911
912 /*
913 * so that caller can tell we handled, past here we need to
914 * zero down pollfd->revents after handling
915 */
916
Andy Green65b0e912013-01-16 07:59:47 +0800917 /*
918 * deal with listen service piggybacking
919 * every listen_service_modulo services of other fds, we
920 * sneak one in to service the listen socket if there's anything waiting
921 *
922 * To handle connection storms, as found in ab, if we previously saw a
923 * pending connection here, it causes us to check again next time.
924 */
925
Andy Greenb5b23192013-02-11 17:13:32 +0800926 if (context->listen_service_fd && pollfd !=
927 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800928 context->listen_service_count++;
929 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800930 context->listen_service_count ==
931 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800932 context->listen_service_count = 0;
933 m = 1;
934 if (context->listen_service_extraseen > 5)
935 m = 2;
936 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800937 /*
938 * even with extpoll, we prepared this
939 * internal fds for listen
940 */
941 n = poll(&context->fds[listen_socket_fds_index],
942 1, 0);
943 if (n > 0) { /* there's a conn waiting for us */
944 libwebsocket_service_fd(context,
945 &context->
946 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800947 context->listen_service_extraseen++;
948 } else {
949 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800950 context->
951 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800952 break;
953 }
954 }
955 }
956
957 }
958
959 /* okay, what we came here to do... */
960
Andy Green0d338332011-02-12 11:57:43 +0000961 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800962
Andy Greena1ce6be2013-01-18 11:43:21 +0800963#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800964 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +0800965 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +0000966 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800967 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800968 n = lws_server_socket_service(context, wsi, pollfd);
969 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +0800970#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000971
Andy Green0d338332011-02-12 11:57:43 +0000972 case LWS_CONNMODE_WS_SERVING:
973 case LWS_CONNMODE_WS_CLIENT:
974
975 /* handle session socket closed */
976
Simon Wulf502b9942013-05-06 06:11:53 +0800977 if ((!(pollfd->revents & POLLIN)) &&
Andy Greenc9ac31e2013-02-16 10:17:52 +0800978 (pollfd->revents & (POLLERR | POLLHUP))) {
Andy Green0d338332011-02-12 11:57:43 +0000979
Andy Green43db0452013-01-10 19:50:35 +0800980 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000981 (void *)wsi, pollfd->fd);
982
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800983 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +0000984 }
985
Andy Green0d338332011-02-12 11:57:43 +0000986 /* the guy requested a callback when it was OK to write */
987
Andy Greenda527df2011-03-07 07:08:12 +0000988 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +0800989 wsi->state == WSI_STATE_ESTABLISHED &&
990 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800991 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800992 goto close_and_handled;
Andy Green3b84c002011-03-06 13:14:42 +0000993 }
Andy Green0d338332011-02-12 11:57:43 +0000994
Andy Green0d338332011-02-12 11:57:43 +0000995
Andy Greenca0a1292013-03-16 11:24:23 +0800996 if (wsi->u.ws.rxflow_buffer &&
997 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
998 lwsl_info("draining rxflow\n");
999 /* well, drain it */
1000 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1001 wsi->u.ws.rxflow_pos;
1002 eff_buf.token_len = wsi->u.ws.rxflow_len -
1003 wsi->u.ws.rxflow_pos;
1004 draining_flow = 1;
1005 goto drain;
1006 }
1007
Andy Green0d338332011-02-12 11:57:43 +00001008 /* any incoming data ready? */
1009
1010 if (!(pollfd->revents & POLLIN))
1011 break;
1012
Andy Greenb45993c2010-12-18 15:13:50 +00001013#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001014read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001015 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001016 eff_buf.token_len = SSL_read(wsi->ssl,
1017 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001018 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001019 if (!eff_buf.token_len) {
1020 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001021 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001022 ERR_error_string(n,
1023 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001024 }
1025 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001026#endif
Andy Greene84652c2013-02-08 13:01:02 +08001027 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001028 context->service_buffer,
1029 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001030
Andy Green98a717c2011-03-06 13:14:15 +00001031 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001032 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1033 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001034 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001035 goto close_and_handled;
1036 n = 0;
1037 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001038 }
Andy Green98a717c2011-03-06 13:14:15 +00001039 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001040 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001041 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001042 }
1043
Andy Green98a717c2011-03-06 13:14:15 +00001044 /*
1045 * give any active extensions a chance to munge the buffer
1046 * before parse. We pass in a pointer to an lws_tokens struct
1047 * prepared with the default buffer and content length that's in
1048 * there. Rather than rewrite the default buffer, extensions
1049 * that expect to grow the buffer can adapt .token to
1050 * point to their own per-connection buffer in the extension
1051 * user allocation. By default with no extensions or no
1052 * extension callback handling, just the normal input buffer is
1053 * used then so it is efficient.
1054 */
Andy Greenb45993c2010-12-18 15:13:50 +00001055
Andy Greene84652c2013-02-08 13:01:02 +08001056 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001057drain:
Andy Green3182ece2013-01-20 17:08:31 +08001058#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001059 more = 1;
1060 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001061
Andy Green98a717c2011-03-06 13:14:15 +00001062 more = 0;
1063
1064 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001065 m = wsi->active_extensions[n]->callback(context,
1066 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001067 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001068 wsi->active_extensions_user[n],
1069 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001070 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001071 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001072 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001073 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001074 }
1075 if (m)
1076 more = 1;
1077 }
Andy Green3182ece2013-01-20 17:08:31 +08001078#endif
Andy Green98a717c2011-03-06 13:14:15 +00001079 /* service incoming data */
1080
1081 if (eff_buf.token_len) {
1082 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001083 (unsigned char *)eff_buf.token,
1084 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001085 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001086 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001087 n = 0;
1088 goto handled;
1089 }
Andy Green98a717c2011-03-06 13:14:15 +00001090 }
Andy Green3182ece2013-01-20 17:08:31 +08001091#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001092 eff_buf.token = NULL;
1093 eff_buf.token_len = 0;
1094 }
Andy Green3182ece2013-01-20 17:08:31 +08001095#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001096 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1097 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1098 lwsl_info("flow buffer: drained\n");
1099 free(wsi->u.ws.rxflow_buffer);
1100 wsi->u.ws.rxflow_buffer = NULL;
1101 /* having drained the rxflow buffer, can rearm POLLIN */
1102 _libwebsocket_rx_flow_control(wsi);
1103 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001104
1105#ifdef LWS_OPENSSL_SUPPORT
1106 if (wsi->ssl && SSL_pending(wsi->ssl))
1107 goto read_pending;
1108#endif
Andy Green98a717c2011-03-06 13:14:15 +00001109 break;
Andy Green76f61e72013-01-16 11:53:05 +08001110
1111 default:
Andy Green03674a62013-01-16 11:47:40 +08001112#ifdef LWS_NO_CLIENT
1113 break;
1114#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001115 n = lws_client_socket_service(context, wsi, pollfd);
1116 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001117#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001118 }
1119
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001120 n = 0;
1121 goto handled;
1122
1123close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001124 libwebsocket_close_and_free_session(context, wsi,
1125 LWS_CLOSE_STATUS_NOSTATUS);
1126 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001127
1128handled:
1129 pollfd->revents = 0;
1130 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001131}
1132
Andy Green0d338332011-02-12 11:57:43 +00001133
Andy Green6964bb52011-01-23 16:50:33 +00001134/**
1135 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001136 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001137 *
1138 * This function closes any active connections and then frees the
1139 * context. After calling this, any further use of the context is
1140 * undefined.
1141 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001142LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001143libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001144{
Andy Green3182ece2013-01-20 17:08:31 +08001145#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001146 int n;
1147 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001148 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001149 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001150
Andy Greend636e352013-01-29 12:36:17 +08001151#ifdef LWS_LATENCY
1152 if (context->worst_latency_info[0])
1153 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1154#endif
1155
Andy Greendfb23042013-01-17 12:26:48 +08001156 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001157 struct libwebsocket *wsi =
1158 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001159 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001160 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1161 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001162 }
Andy Green6964bb52011-01-23 16:50:33 +00001163
Andy Greena41314f2011-05-23 10:00:03 +01001164 /*
1165 * give all extensions a chance to clean up any per-context
1166 * allocations they might have made
1167 */
1168
1169 ext = context->extensions;
1170 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1171 if (context->listen_port)
1172 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001173 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001174 ext->callback(context, ext, NULL,
1175 (enum libwebsocket_extension_callback_reasons)m,
1176 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001177 ext++;
1178 }
Andy Greena7109e62013-02-11 12:05:54 +08001179
1180 /*
1181 * inform all the protocols that they are done and will have no more
1182 * callbacks
1183 */
1184
1185 while (protocol->callback) {
1186 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1187 NULL, NULL, 0);
1188 protocol++;
1189 }
1190
Andy Green3182ece2013-01-20 17:08:31 +08001191#endif
Andy Greena41314f2011-05-23 10:00:03 +01001192
Peter Hinz56885f32011-03-02 22:03:47 +00001193#ifdef WIN32
1194#else
1195 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001196#endif
1197
Peter Hinz56885f32011-03-02 22:03:47 +00001198#ifdef LWS_OPENSSL_SUPPORT
1199 if (context->ssl_ctx)
1200 SSL_CTX_free(context->ssl_ctx);
1201 if (context->ssl_client_ctx)
1202 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001203
1204 ERR_remove_state(0);
1205 ERR_free_strings();
1206 EVP_cleanup();
1207 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001208#endif
1209
Andy Green46596482013-02-11 11:04:01 +08001210 if (context->fds)
1211 free(context->fds);
1212 if (context->lws_lookup)
1213 free(context->lws_lookup);
1214
Peter Hinz56885f32011-03-02 22:03:47 +00001215 free(context);
1216
1217#ifdef WIN32
1218 WSACleanup();
1219#endif
Andy Green6964bb52011-01-23 16:50:33 +00001220}
1221
Andy Greend88146d2013-01-22 12:40:35 +08001222/**
Andy Greenb5b23192013-02-11 17:13:32 +08001223 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001224 * @context: Websocket context
1225 *
1226 * This returns the optional user allocation that can be attached to
1227 * the context the sockets live in at context_create time. It's a way
1228 * to let all sockets serviced in the same context share data without
1229 * using globals statics in the user code.
1230 */
Alon Levy0291eb32012-10-19 11:21:56 +02001231LWS_EXTERN void *
1232libwebsocket_context_user(struct libwebsocket_context *context)
1233{
Andy Greenb5b23192013-02-11 17:13:32 +08001234 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001235}
1236
Andy Green6964bb52011-01-23 16:50:33 +00001237/**
1238 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001239 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001240 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1241 * service otherwise block and service immediately, returning
1242 * after the timeout if nothing needed service.
1243 *
1244 * This function deals with any pending websocket traffic, for three
1245 * kinds of event. It handles these events on both server and client
1246 * types of connection the same.
1247 *
1248 * 1) Accept new connections to our context's server
1249 *
Andy Green6f520a52013-01-29 17:57:39 +08001250 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001251 * server or client connections.
1252 *
1253 * You need to call this service function periodically to all the above
1254 * functions to happen; if your application is single-threaded you can
1255 * just call it in your main event loop.
1256 *
1257 * Alternatively you can fork a new process that asynchronously handles
1258 * calling this service in a loop. In that case you are happy if this
1259 * call blocks your thread until it needs to take care of something and
1260 * would call it with a large nonzero timeout. Your loop then takes no
1261 * CPU while there is nothing happening.
1262 *
1263 * If you are calling it in a single-threaded app, you don't want it to
1264 * wait around blocking other things in your loop from happening, so you
1265 * would call it with a timeout_ms of 0, so it returns immediately if
1266 * nothing is pending, or as soon as it services whatever was pending.
1267 */
1268
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001269LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001270libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001271{
1272 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001273 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001274
1275 /* stay dead once we are dead */
1276
Peter Hinz56885f32011-03-02 22:03:47 +00001277 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001278 return 1;
1279
Andy Green0d338332011-02-12 11:57:43 +00001280 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001281
Peter Hinz56885f32011-03-02 22:03:47 +00001282 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001283 if (n == 0) /* poll timeout */
1284 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001285
Andy Greendfb23042013-01-17 12:26:48 +08001286 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001287 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001288
Andy Greendfb23042013-01-17 12:26:48 +08001289 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001290
Andy Greenca0a1292013-03-16 11:24:23 +08001291 for (n = 0; n < context->fds_count; n++) {
1292 if (!context->fds[n].revents)
1293 continue;
1294 m = libwebsocket_service_fd(context, &context->fds[n]);
1295 if (m < 0)
1296 return -1;
1297 /* if something closed, retry this slot */
1298 if (m)
1299 n--;
1300 }
1301
Andy Greene92cd172011-01-19 13:11:55 +00001302 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001303}
1304
Andy Green3182ece2013-01-20 17:08:31 +08001305#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001306int
1307lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001308 struct libwebsocket *wsi,
1309 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001310 void *v, size_t len)
1311{
1312 int n;
1313 int handled = 0;
1314
1315 /* maybe an extension will take care of it for us */
1316
1317 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1318 if (!wsi->active_extensions[n]->callback)
1319 continue;
1320
1321 handled |= wsi->active_extensions[n]->callback(context,
1322 wsi->active_extensions[n], wsi,
1323 r, wsi->active_extensions_user[n], v, len);
1324 }
1325
1326 return handled;
1327}
1328
1329
1330void *
1331lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001332 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001333{
1334 int n = 0;
1335
Andy Green68b45042011-05-25 21:41:57 +01001336 if (wsi == NULL)
1337 return NULL;
1338
Andy Greena41314f2011-05-23 10:00:03 +01001339 while (n < wsi->count_active_extensions) {
1340 if (wsi->active_extensions[n] != ext) {
1341 n++;
1342 continue;
1343 }
1344 return wsi->active_extensions_user[n];
1345 }
1346
1347 return NULL;
1348}
Andy Green3182ece2013-01-20 17:08:31 +08001349#endif
Andy Greena41314f2011-05-23 10:00:03 +01001350
Andy Green90c7cbc2011-01-27 06:26:52 +00001351/**
1352 * libwebsocket_callback_on_writable() - Request a callback when this socket
1353 * becomes able to be written to without
1354 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001355 *
Peter Hinz56885f32011-03-02 22:03:47 +00001356 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001357 * @wsi: Websocket connection instance to get callback for
1358 */
1359
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001360LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001361libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001362 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001363{
Andy Green3182ece2013-01-20 17:08:31 +08001364#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001365 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001366 int handled = 0;
1367
1368 /* maybe an extension will take care of it for us */
1369
1370 for (n = 0; n < wsi->count_active_extensions; n++) {
1371 if (!wsi->active_extensions[n]->callback)
1372 continue;
1373
1374 handled |= wsi->active_extensions[n]->callback(context,
1375 wsi->active_extensions[n], wsi,
1376 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1377 wsi->active_extensions_user[n], NULL, 0);
1378 }
1379
1380 if (handled)
1381 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001382#endif
Andy Greendfb23042013-01-17 12:26:48 +08001383 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001384 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1385 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001386 return -1;
1387 }
1388
1389 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001390
Andy Green3221f922011-02-12 13:14:11 +00001391 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001392 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001393 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001394 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001395
Andy Green90c7cbc2011-01-27 06:26:52 +00001396 return 1;
1397}
1398
1399/**
1400 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1401 * all connections using the given protocol when it
1402 * becomes possible to write to each socket without
1403 * blocking in turn.
1404 *
1405 * @protocol: Protocol whose connections will get callbacks
1406 */
1407
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001408LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001409libwebsocket_callback_on_writable_all_protocol(
1410 const struct libwebsocket_protocols *protocol)
1411{
Peter Hinz56885f32011-03-02 22:03:47 +00001412 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001413 int n;
Andy Green0d338332011-02-12 11:57:43 +00001414 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001415
Andy Greendfb23042013-01-17 12:26:48 +08001416 for (n = 0; n < context->fds_count; n++) {
1417 wsi = context->lws_lookup[context->fds[n].fd];
1418 if (!wsi)
1419 continue;
1420 if (wsi->protocol == protocol)
1421 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001422 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001423
1424 return 0;
1425}
1426
Andy Greenbe93fef2011-02-14 20:25:43 +00001427/**
1428 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1429 *
1430 * You will not need this unless you are doing something special
1431 *
1432 * @wsi: Websocket connection instance
1433 * @reason: timeout reason
1434 * @secs: how many seconds
1435 */
1436
1437void
1438libwebsocket_set_timeout(struct libwebsocket *wsi,
1439 enum pending_timeout reason, int secs)
1440{
1441 struct timeval tv;
1442
1443 gettimeofday(&tv, NULL);
1444
1445 wsi->pending_timeout_limit = tv.tv_sec + secs;
1446 wsi->pending_timeout = reason;
1447}
1448
Andy Greena6cbece2011-01-27 20:06:03 +00001449
1450/**
1451 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1452 *
1453 * You will not need this unless you are doing something special
1454 *
1455 * @wsi: Websocket connection instance
1456 */
1457
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001458LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001459libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1460{
1461 return wsi->sock;
1462}
1463
Andy Greend636e352013-01-29 12:36:17 +08001464#ifdef LWS_LATENCY
1465void
Andy Greenb5b23192013-02-11 17:13:32 +08001466lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1467 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001468{
1469 struct timeval tv;
1470 unsigned long u;
1471 char buf[256];
1472
1473 gettimeofday(&tv, NULL);
1474
1475 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1476
1477 if (action) {
1478 if (completed) {
1479 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001480 sprintf(buf,
1481 "Completion first try lat %luus: %p: ret %d: %s\n",
1482 u - wsi->latency_start,
1483 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001484 else
Andy Greenb5b23192013-02-11 17:13:32 +08001485 sprintf(buf,
1486 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1487 u - wsi->action_start,
1488 u - wsi->latency_start,
1489 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001490 wsi->action_start = 0;
1491 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001492 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1493 u - wsi->latency_start,
1494 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001495 if (u - wsi->latency_start > context->worst_latency) {
1496 context->worst_latency = u - wsi->latency_start;
1497 strcpy(context->worst_latency_info, buf);
1498 }
1499 lwsl_latency("%s", buf);
1500 } else {
1501 wsi->latency_start = u;
1502 if (!wsi->action_start)
1503 wsi->action_start = u;
1504 }
1505}
1506#endif
1507
Andy Greena1ce6be2013-01-18 11:43:21 +08001508#ifdef LWS_NO_SERVER
1509int
Andy Greenca0a1292013-03-16 11:24:23 +08001510_libwebsocket_rx_flow_control(struct libswebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001511{
1512 return 0;
1513}
1514#else
Andy Green706961d2013-01-17 16:50:35 +08001515int
1516_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1517{
1518 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001519
Andy Greenca0a1292013-03-16 11:24:23 +08001520 /* there is no pending change */
1521 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001522 return 0;
1523
Andy Greenca0a1292013-03-16 11:24:23 +08001524 /* stuff is still buffered, not ready to really accept new input */
1525 if (wsi->u.ws.rxflow_buffer) {
1526 /* get ourselves called back to deal with stashed buffer */
1527 libwebsocket_callback_on_writable(context, wsi);
1528 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001529 }
1530
Andy Greenca0a1292013-03-16 11:24:23 +08001531 /* pending is cleared, we can change rxflow state */
1532
1533 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1534
1535 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1536 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1537
1538 /* adjust the pollfd for this wsi */
1539
1540 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001541 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1542 else
1543 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1544
Andy Greenca0a1292013-03-16 11:24:23 +08001545 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001546 /* external POLL support via protocol 0 */
1547 context->protocols[0].callback(context, wsi,
1548 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001549 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001550 else
1551 /* external POLL support via protocol 0 */
1552 context->protocols[0].callback(context, wsi,
1553 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001554 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001555
1556 return 1;
1557}
Andy Greena1ce6be2013-01-18 11:43:21 +08001558#endif
Andy Green706961d2013-01-17 16:50:35 +08001559
Andy Green90c7cbc2011-01-27 06:26:52 +00001560/**
1561 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1562 * receieved packets.
1563 *
1564 * If the output side of a server process becomes choked, this allows flow
1565 * control for the input side.
1566 *
1567 * @wsi: Websocket connection instance to get callback for
1568 * @enable: 0 = disable read servicing for this connection, 1 = enable
1569 */
1570
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001571LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001572libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1573{
Andy Greenca0a1292013-03-16 11:24:23 +08001574 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1575 return 0;
1576
1577 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1578 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001579
Andy Green706961d2013-01-17 16:50:35 +08001580 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001581}
1582
Andy Greenb55451c2013-03-16 12:32:27 +08001583/**
1584 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1585 *
1586 * When the user server code realizes it can accept more input, it can
1587 * call this to have the RX flow restriction removed from all connections using
1588 * the given protocol.
1589 *
1590 * @protocol: all connections using this protocol will be allowed to receive
1591 */
1592
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001593LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001594libwebsocket_rx_flow_allow_all_protocol(
1595 const struct libwebsocket_protocols *protocol)
1596{
1597 struct libwebsocket_context *context = protocol->owning_server;
1598 int n;
1599 struct libwebsocket *wsi;
1600
1601 for (n = 0; n < context->fds_count; n++) {
1602 wsi = context->lws_lookup[context->fds[n].fd];
1603 if (!wsi)
1604 continue;
1605 if (wsi->protocol == protocol)
1606 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1607 }
1608}
1609
Andy Green706961d2013-01-17 16:50:35 +08001610
Andy Green2ac5a6f2011-01-28 10:00:18 +00001611/**
1612 * libwebsocket_canonical_hostname() - returns this host's hostname
1613 *
1614 * This is typically used by client code to fill in the host parameter
1615 * when making a client connection. You can only call it after the context
1616 * has been created.
1617 *
Peter Hinz56885f32011-03-02 22:03:47 +00001618 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001619 */
1620
1621
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001622LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001623libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001624{
Peter Hinz56885f32011-03-02 22:03:47 +00001625 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001626}
1627
1628
Andy Green90c7cbc2011-01-27 06:26:52 +00001629static void sigpipe_handler(int x)
1630{
1631}
1632
Andy Green6901cb32011-02-21 08:06:47 +00001633#ifdef LWS_OPENSSL_SUPPORT
1634static int
1635OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1636{
1637
1638 SSL *ssl;
1639 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001640 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001641
1642 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1643 SSL_get_ex_data_X509_STORE_CTX_idx());
1644
1645 /*
Andy Green2e24da02011-03-05 16:12:04 +00001646 * !!! nasty openssl requires the index to come as a library-scope
1647 * static
Andy Green6901cb32011-02-21 08:06:47 +00001648 */
Andy Green2e24da02011-03-05 16:12:04 +00001649 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001650
Peter Hinz56885f32011-03-02 22:03:47 +00001651 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001652 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1653 x509_ctx, ssl, preverify_ok);
1654
1655 /* convert return code from 0 = OK to 1 = OK */
1656
1657 if (!n)
1658 n = 1;
1659 else
1660 n = 0;
1661
1662 return n;
1663}
1664#endif
1665
Andy Green706961d2013-01-17 16:50:35 +08001666int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001667 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001668 struct libwebsocket *wsi,
1669 enum libwebsocket_callback_reasons reason, void *user,
1670 void *in, size_t len)
1671{
1672 int n;
1673
1674 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001675 if (!n)
1676 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001677
Andy Greenaedc9532013-02-10 21:21:24 +08001678 return n;
Andy Green706961d2013-01-17 16:50:35 +08001679}
1680
Andy Greenb45993c2010-12-18 15:13:50 +00001681
Andy Greenab990e42010-10-31 12:42:52 +00001682/**
Andy Green4739e5c2011-01-22 12:51:57 +00001683 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001684 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001685 *
Andy Green1b265272013-02-09 14:01:09 +08001686 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001687 * of all initialization in one step.
1688 *
Andy Greene92cd172011-01-19 13:11:55 +00001689 * After initialization, it returns a struct libwebsocket_context * that
1690 * represents this server. After calling, user code needs to take care
1691 * of calling libwebsocket_service() with the context pointer to get the
1692 * server's sockets serviced. This can be done in the same process context
1693 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001694 *
Andy Green8f037e42010-12-19 22:13:26 +00001695 * The protocol callback functions are called for a handful of events
1696 * including http requests coming in, websocket connections becoming
1697 * established, and data arriving; it's also called periodically to allow
1698 * async transmission.
1699 *
1700 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1701 * at that time websocket protocol has not been negotiated. Other
1702 * protocols after the first one never see any HTTP callack activity.
1703 *
1704 * The server created is a simple http server by default; part of the
1705 * websocket standard is upgrading this http connection to a websocket one.
1706 *
1707 * This allows the same server to provide files like scripts and favicon /
1708 * images or whatever over http and dynamic data over websockets all in
1709 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001710 */
Andy Green4ea60062010-10-30 12:15:07 +01001711
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001712LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001713libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001714{
Peter Hinz56885f32011-03-02 22:03:47 +00001715 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001716 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001717 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001718#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001719 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001720 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001721 struct sockaddr_in serv_addr;
1722#endif
Andy Green3182ece2013-01-20 17:08:31 +08001723#ifndef LWS_NO_EXTENSIONS
1724 int m;
Andy Green1b265272013-02-09 14:01:09 +08001725 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001726#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001727
Andy Green3faa9c72010-11-08 17:03:03 +00001728#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001729 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001730#endif
1731
Joakim Soderberg4c531232013-02-06 15:26:58 +09001732#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001733 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001734#endif
1735
Andy Greenb3a614a2013-01-19 13:08:17 +08001736 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001737 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001738 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001739 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001740#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001741 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1742 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001743#else
1744 lwsl_notice(" Configured without extension support\n");
1745#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001746 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1747 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001748 if (info->ssl_cipher_list)
1749 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001750 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1751 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001752
Peter Hinz56885f32011-03-02 22:03:47 +00001753#ifdef _WIN32
1754 {
1755 WORD wVersionRequested;
1756 WSADATA wsaData;
1757 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001758 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001759
1760 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1761 wVersionRequested = MAKEWORD(2, 2);
1762
1763 err = WSAStartup(wVersionRequested, &wsaData);
1764 if (err != 0) {
1765 /* Tell the user that we could not find a usable */
1766 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001767 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001768 return NULL;
1769 }
David Galeano7b11fec2011-10-04 19:55:18 +08001770
Andy Green6ee372f2012-04-09 15:09:01 +08001771 /* default to a poll() made out of select() */
1772 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001773
Andy Green6ee372f2012-04-09 15:09:01 +08001774 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001775 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001776 if (wsdll)
1777 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001778
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001779 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001780 if (!poll)
1781 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001782 }
1783#endif
1784
Andy Greenb5b23192013-02-11 17:13:32 +08001785 context = (struct libwebsocket_context *)
1786 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001787 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001788 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001789 return NULL;
1790 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001791 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001792#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001793 context->started_with_parent = pid_daemon;
1794 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1795#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001796
Joakim Soderberg4c531232013-02-06 15:26:58 +09001797 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001798 context->protocols = info->protocols;
1799 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001800 context->http_proxy_port = 0;
1801 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001802 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001803 /* to reduce this allocation, */
1804 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001805 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1806 sizeof(struct libwebsocket_context),
1807 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1808 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001809 sizeof(struct libwebsocket_context) +
1810 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1811 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001812
Andy Greenb5b23192013-02-11 17:13:32 +08001813 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1814 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001815 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001816 lwsl_err("Unable to allocate fds array for %d connections\n",
1817 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001818 free(context);
1819 return NULL;
1820 }
Andy Greenb5b23192013-02-11 17:13:32 +08001821 context->lws_lookup = (struct libwebsocket **)
1822 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001823 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001824 lwsl_err(
1825 "Unable to allocate lws_lookup array for %d connections\n",
1826 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001827 free(context->fds);
1828 free(context);
1829 return NULL;
1830 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001831 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1832 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001833
Peter Hinz56885f32011-03-02 22:03:47 +00001834 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001835#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001836 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001837#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001838 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001839 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001840
Peter Hinz56885f32011-03-02 22:03:47 +00001841#ifdef WIN32
1842 context->fd_random = 0;
1843#else
1844 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1845 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001846 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001847 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001848 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001849 }
Peter Hinz56885f32011-03-02 22:03:47 +00001850#endif
Andy Green44eee682011-02-10 09:32:24 +00001851
Peter Hinz56885f32011-03-02 22:03:47 +00001852#ifdef LWS_OPENSSL_SUPPORT
1853 context->use_ssl = 0;
1854 context->ssl_ctx = NULL;
1855 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001856 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001857#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001858
Andy Greena1ce6be2013-01-18 11:43:21 +08001859 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001860
Andy Greena1ce6be2013-01-18 11:43:21 +08001861#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001862 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001863 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001864 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001865 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001866
Andy Greenb5b23192013-02-11 17:13:32 +08001867 lwsl_notice(" canonical_hostname = %s\n",
1868 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001869 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001870#endif
Andy Greena69f0512012-05-03 12:32:38 +08001871
Andy Green9659f372011-01-27 22:01:43 +00001872 /* split the proxy ads:port if given */
1873
1874 p = getenv("http_proxy");
1875 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001876 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001877 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001878 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001879 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001880
Peter Hinz56885f32011-03-02 22:03:47 +00001881 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001882 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001883 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001884 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001885 }
1886 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001887 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001888
Andy Greenb3a614a2013-01-19 13:08:17 +08001889 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001890 context->http_proxy_address,
1891 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001892 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001893
Andy Greena1ce6be2013-01-18 11:43:21 +08001894#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001895 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001896
Andy Green3faa9c72010-11-08 17:03:03 +00001897#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001898 context->use_ssl = info->ssl_cert_filepath != NULL &&
1899 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001900#ifdef USE_CYASSL
1901 lwsl_notice(" Compiled with CYASSL support\n");
1902#else
1903 lwsl_notice(" Compiled with OpenSSL support\n");
1904#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001905 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001906 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001907 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001908 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001909
Andy Green90c7cbc2011-01-27 06:26:52 +00001910#else
Andy Green2b40b792013-02-10 22:22:01 +08001911 if (info->ssl_cert_filepath != NULL &&
1912 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001913 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001914 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001915 }
Andy Greenb5b23192013-02-11 17:13:32 +08001916 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001917#endif
Andy Greena17c6922013-01-20 20:21:54 +08001918
Andy Greenb5b23192013-02-11 17:13:32 +08001919 lwsl_notice(
1920 " per-conn mem: %u + %u headers + protocol rx buf\n",
1921 sizeof(struct libwebsocket),
1922 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001923 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001924#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001925
1926 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001927#ifdef WIN32
1928#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001929 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001930#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001931
1932
1933#ifdef LWS_OPENSSL_SUPPORT
1934
1935 /* basic openssl init */
1936
1937 SSL_library_init();
1938
1939 OpenSSL_add_all_algorithms();
1940 SSL_load_error_strings();
1941
Andy Green2e24da02011-03-05 16:12:04 +00001942 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001943 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1944
Andy Green90c7cbc2011-01-27 06:26:52 +00001945 /*
1946 * Firefox insists on SSLv23 not SSLv3
1947 * Konq disables SSLv2 by default now, SSLv23 works
1948 */
1949
1950 method = (SSL_METHOD *)SSLv23_server_method();
1951 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001952 lwsl_err("problem creating ssl method %lu: %s\n",
1953 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001954 ERR_error_string(ERR_get_error(),
1955 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001956 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001957 }
Peter Hinz56885f32011-03-02 22:03:47 +00001958 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1959 if (!context->ssl_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001960 lwsl_err("problem creating ssl context %lu: %s\n",
1961 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001962 ERR_error_string(ERR_get_error(),
1963 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001964 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001965 }
1966
David Galeanocc148e42013-01-10 10:18:59 +08001967#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001968 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001969#endif
David Galeano77a677c2013-01-10 10:14:12 +08001970 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001971 if (info->ssl_cipher_list)
1972 SSL_CTX_set_cipher_list(context->ssl_ctx,
1973 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001974
Andy Greena1ce6be2013-01-18 11:43:21 +08001975#ifndef LWS_NO_CLIENT
1976
Andy Green90c7cbc2011-01-27 06:26:52 +00001977 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001978
Andy Green1b265272013-02-09 14:01:09 +08001979 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001980 method = (SSL_METHOD *)SSLv23_client_method();
1981 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001982 lwsl_err("problem creating ssl method %lu: %s\n",
1983 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001984 ERR_error_string(ERR_get_error(),
1985 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001986 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001987 }
1988 /* create context */
1989 context->ssl_client_ctx = SSL_CTX_new(method);
1990 if (!context->ssl_client_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001991 lwsl_err("problem creating ssl context %lu: %s\n",
1992 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001993 ERR_error_string(ERR_get_error(),
1994 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001995 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001996 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001997
David Galeanocc148e42013-01-10 10:18:59 +08001998#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001999 SSL_CTX_set_options(context->ssl_client_ctx,
2000 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002001#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002002 SSL_CTX_set_options(context->ssl_client_ctx,
2003 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002004 if (info->ssl_cipher_list)
2005 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2006 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002007
Peter Hinz56885f32011-03-02 22:03:47 +00002008 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002009 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002010 if (!SSL_CTX_load_verify_locations(
2011 context->ssl_client_ctx, NULL,
2012 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002013 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002014 "Unable to load SSL Client certs from %s "
2015 "(set by --with-client-cert-dir= "
2016 "in configure) -- client ssl isn't "
2017 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002018 } else
2019 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002020 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002021 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002022 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002023 "Unable to load SSL Client certs "
2024 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002025 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002026
2027 /*
2028 * callback allowing user code to load extra verification certs
2029 * helping the client to verify server identity
2030 */
2031
2032 context->protocols[0].callback(context, NULL,
2033 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2034 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002035 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002036#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002037
Andy Greenc6bf2c22011-02-20 11:10:47 +00002038 /* as a server, are we requiring clients to identify themselves? */
2039
Andy Greenb5b23192013-02-11 17:13:32 +08002040 if (info->options &
2041 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002042
2043 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002044
Peter Hinz56885f32011-03-02 22:03:47 +00002045 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002046 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2047 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002048
2049 /*
2050 * give user code a chance to load certs into the server
2051 * allowing it to verify incoming client certs
2052 */
2053
Peter Hinz56885f32011-03-02 22:03:47 +00002054 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002055 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002056 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002057 }
2058
Peter Hinz56885f32011-03-02 22:03:47 +00002059 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002060
2061 /* openssl init for server sockets */
2062
Andy Green3faa9c72010-11-08 17:03:03 +00002063 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002064 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002065 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002066 if (n != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002067 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002068 info->ssl_cert_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002069 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002070 ERR_error_string(ERR_get_error(),
2071 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002072 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002073 }
2074 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002075 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002076 info->ssl_private_key_filepath,
2077 SSL_FILETYPE_PEM) != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002078 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002079 info->ssl_private_key_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002080 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002081 ERR_error_string(ERR_get_error(),
2082 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002083 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002084 }
2085 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002086 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002087 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002088 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002089 }
2090
2091 /* SSL is happy and has a cert it's content with */
2092 }
2093#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002094
Andy Greendf736162011-01-18 15:39:02 +00002095 /* selftest */
2096
2097 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002098 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002099
Andy Greena1ce6be2013-01-18 11:43:21 +08002100#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002101 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002102
Andy Green1b265272013-02-09 14:01:09 +08002103 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002104 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002105
Andy Green4739e5c2011-01-22 12:51:57 +00002106 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2107 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002108 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002109 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002110 }
Andy Green775c0dd2010-10-29 14:15:22 +01002111
Joakim Soderberg4c531232013-02-06 15:26:58 +09002112#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002113 /*
2114 * allow us to restart even if old sockets in TIME_WAIT
2115 * (REUSEADDR on Unix means, "don't hang on to this
2116 * address after the listener is closed." On Windows, though,
2117 * it means "don't keep other processes from binding to
2118 * this address while we're using it)
2119 */
Andy Green6ee372f2012-04-09 15:09:01 +08002120 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2121 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002122#endif
Andy Green6c939552011-03-08 08:56:57 +00002123
2124 /* Disable Nagle */
2125 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002126 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2127 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002128
Joakim Soderberg4c531232013-02-06 15:26:58 +09002129 #ifdef WIN32
2130 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002131 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002132 #else
Andy Greene2160712013-01-28 12:19:10 +08002133 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002134 #endif
Andy Greene2160712013-01-28 12:19:10 +08002135
Andy Green4739e5c2011-01-22 12:51:57 +00002136 bzero((char *) &serv_addr, sizeof(serv_addr));
2137 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002138 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002139 serv_addr.sin_addr.s_addr = INADDR_ANY;
2140 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002141 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002142 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002143 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002144
2145 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2146 sizeof(serv_addr));
2147 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002148 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002149 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002150 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002151 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002152 }
Andy Green0d338332011-02-12 11:57:43 +00002153
Andy Greenb5b23192013-02-11 17:13:32 +08002154 wsi = (struct libwebsocket *)malloc(
2155 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002156 if (wsi == NULL) {
2157 lwsl_err("Out of mem\n");
2158 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002159 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002160 }
Andy Greenb5b23192013-02-11 17:13:32 +08002161 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002162 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002163#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002164 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002165#endif
Andy Green0d338332011-02-12 11:57:43 +00002166 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002167
2168 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002169
Andy Green65b0e912013-01-16 07:59:47 +08002170 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2171 context->listen_service_count = 0;
2172 context->listen_service_fd = sockfd;
2173
Andy Greena824d182013-01-15 20:52:29 +08002174 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002175 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002176 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002177#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002178
Andy Green6ee372f2012-04-09 15:09:01 +08002179 /*
2180 * drop any root privs for this process
2181 * to listen on port < 1023 we would have needed root, but now we are
2182 * listening, we don't want the power for anything else
2183 */
Peter Hinz56885f32011-03-02 22:03:47 +00002184#ifdef WIN32
2185#else
Andy Green1b265272013-02-09 14:01:09 +08002186 if (info->gid != -1)
2187 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002188 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002189 if (info->uid != -1)
2190 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002191 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002192#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002193
Andy Green6f520a52013-01-29 17:57:39 +08002194 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002195
Peter Hinz56885f32011-03-02 22:03:47 +00002196 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002197 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002198 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002199
Andy Green43db0452013-01-10 19:50:35 +08002200 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002201 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002202
Andy Green1b265272013-02-09 14:01:09 +08002203 info->protocols[context->count_protocols].owning_server =
2204 context;
2205 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002206 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002207
2208 /*
2209 * inform all the protocols that they are doing their one-time
2210 * initialization if they want to
2211 */
2212 info->protocols[context->count_protocols].callback(context,
2213 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002214 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002215
Andy Green3182ece2013-01-20 17:08:31 +08002216#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002217 /*
2218 * give all extensions a chance to create any per-context
2219 * allocations they need
2220 */
2221
2222 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002223 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002224 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002225
Andy Green1b265272013-02-09 14:01:09 +08002226 if (info->extensions) {
2227 ext = info->extensions;
2228 while (ext->callback) {
2229 lwsl_ext(" Extension: %s\n", ext->name);
2230 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002231 (enum libwebsocket_extension_callback_reasons)m,
2232 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002233 ext++;
2234 }
Andy Greena41314f2011-05-23 10:00:03 +01002235 }
Andy Green3182ece2013-01-20 17:08:31 +08002236#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002237 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002238
2239bail:
2240 libwebsocket_context_destroy(context);
2241 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002242}
Andy Greenb45993c2010-12-18 15:13:50 +00002243
Andy Greenb45993c2010-12-18 15:13:50 +00002244/**
2245 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002246 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002247 * @wsi: pointer to struct websocket you want to know the protocol of
2248 *
Andy Green8f037e42010-12-19 22:13:26 +00002249 *
Andy Green6f520a52013-01-29 17:57:39 +08002250 * Some apis can act on all live connections of a given protocol,
2251 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002252 */
Andy Greenab990e42010-10-31 12:42:52 +00002253
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002254LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002255libwebsockets_get_protocol(struct libwebsocket *wsi)
2256{
2257 return wsi->protocol;
2258}
2259
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002260LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002261libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2262{
Andy Green623a98d2013-01-21 11:04:23 +08002263 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002264}
Alex Bligh49146db2011-11-07 17:19:25 +08002265
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002266LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002267libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2268{
Andy Green623a98d2013-01-21 11:04:23 +08002269 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002270}
2271
Andy Green2af4d5b2013-02-18 16:30:10 +08002272int
Alex Bligh49146db2011-11-07 17:19:25 +08002273libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2274{
Andy Green67d556c2013-02-15 22:31:55 +08002275 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002276 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002277
Alex Bligh49146db2011-11-07 17:19:25 +08002278 /* allocate the per-connection user memory (if any) */
2279
2280 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2281 wsi->user_space = malloc(
2282 wsi->protocol->per_session_data_size);
2283 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002284 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002285 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002286 }
Andy Green6ee372f2012-04-09 15:09:01 +08002287 memset(wsi->user_space, 0,
2288 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002289 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002290 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002291}
Andy Green43db0452013-01-10 19:50:35 +08002292
Andy Green0b319092013-01-19 11:17:56 +08002293static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002294{
Andy Green0b319092013-01-19 11:17:56 +08002295 char buf[300];
2296 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002297 int n;
2298
2299 gettimeofday(&tv, NULL);
2300
2301 buf[0] = '\0';
2302 for (n = 0; n < LLL_COUNT; n++)
2303 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002304 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002305 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002306 break;
2307 }
Andy Greenb5b23192013-02-11 17:13:32 +08002308
Andy Green0b319092013-01-19 11:17:56 +08002309 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002310}
2311
Joakim Soderberg4c531232013-02-06 15:26:58 +09002312#ifdef WIN32
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002313LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002314{
2315 lwsl_emit_stderr(level, line);
2316}
2317#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002318LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002319{
2320 int syslog_level = LOG_DEBUG;
2321
2322 switch (level) {
2323 case LLL_ERR:
2324 syslog_level = LOG_ERR;
2325 break;
2326 case LLL_WARN:
2327 syslog_level = LOG_WARNING;
2328 break;
2329 case LLL_NOTICE:
2330 syslog_level = LOG_NOTICE;
2331 break;
2332 case LLL_INFO:
2333 syslog_level = LOG_INFO;
2334 break;
2335 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002336 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002337}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002338#endif
Andy Greenc11db202013-01-19 11:12:16 +08002339
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002340LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002341{
Andy Greende8f27a2013-01-12 09:17:42 +08002342 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002343 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002344
2345 if (!(log_level & filter))
2346 return;
2347
Andy Green43db0452013-01-10 19:50:35 +08002348 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002349 vsnprintf(buf, sizeof(buf), format, ap);
2350 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002351 va_end(ap);
2352
Andy Green0b319092013-01-19 11:17:56 +08002353 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002354}
2355
2356/**
2357 * lws_set_log_level() - Set the logging bitfield
2358 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002359 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2360 * function to perform log string emission instead of
2361 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002362 *
Andy Greenc6511a02013-02-19 10:01:48 +08002363 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002364 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002365 */
2366
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002367LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002368 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002369{
2370 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002371 if (log_emit_function)
2372 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002373}