blob: 579763d7cbeca2036434723ec9fb30507c69a925 [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 Greenb8b247d2013-01-22 07:20:08 +0800209 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && 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 Green0d338332011-02-12 11:57:43 +0000965 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800966 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800967 n = lws_server_socket_service(context, wsi, pollfd);
968 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +0800969#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000970
Andy Green0d338332011-02-12 11:57:43 +0000971 case LWS_CONNMODE_WS_SERVING:
972 case LWS_CONNMODE_WS_CLIENT:
973
974 /* handle session socket closed */
975
Andy Greenc9ac31e2013-02-16 10:17:52 +0800976 if ((!pollfd->revents & POLLIN) &&
977 (pollfd->revents & (POLLERR | POLLHUP))) {
Andy Green0d338332011-02-12 11:57:43 +0000978
Andy Green43db0452013-01-10 19:50:35 +0800979 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000980 (void *)wsi, pollfd->fd);
981
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800982 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +0000983 }
984
Andy Green0d338332011-02-12 11:57:43 +0000985 /* the guy requested a callback when it was OK to write */
986
Andy Greenda527df2011-03-07 07:08:12 +0000987 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +0800988 wsi->state == WSI_STATE_ESTABLISHED &&
989 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800990 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800991 goto close_and_handled;
Andy Green3b84c002011-03-06 13:14:42 +0000992 }
Andy Green0d338332011-02-12 11:57:43 +0000993
Andy Green0d338332011-02-12 11:57:43 +0000994
Andy Greenca0a1292013-03-16 11:24:23 +0800995 if (wsi->u.ws.rxflow_buffer &&
996 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
997 lwsl_info("draining rxflow\n");
998 /* well, drain it */
999 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1000 wsi->u.ws.rxflow_pos;
1001 eff_buf.token_len = wsi->u.ws.rxflow_len -
1002 wsi->u.ws.rxflow_pos;
1003 draining_flow = 1;
1004 goto drain;
1005 }
1006
Andy Green0d338332011-02-12 11:57:43 +00001007 /* any incoming data ready? */
1008
1009 if (!(pollfd->revents & POLLIN))
1010 break;
1011
Andy Greenb45993c2010-12-18 15:13:50 +00001012#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001013read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001014 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001015 eff_buf.token_len = SSL_read(wsi->ssl,
1016 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001017 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001018 if (!eff_buf.token_len) {
1019 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001020 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001021 ERR_error_string(n,
1022 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001023 }
1024 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001025#endif
Andy Greene84652c2013-02-08 13:01:02 +08001026 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001027 context->service_buffer,
1028 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001029
Andy Green98a717c2011-03-06 13:14:15 +00001030 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001031 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1032 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001033 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001034 goto close_and_handled;
1035 n = 0;
1036 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001037 }
Andy Green98a717c2011-03-06 13:14:15 +00001038 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001039 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001040 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001041 }
1042
Andy Green98a717c2011-03-06 13:14:15 +00001043 /*
1044 * give any active extensions a chance to munge the buffer
1045 * before parse. We pass in a pointer to an lws_tokens struct
1046 * prepared with the default buffer and content length that's in
1047 * there. Rather than rewrite the default buffer, extensions
1048 * that expect to grow the buffer can adapt .token to
1049 * point to their own per-connection buffer in the extension
1050 * user allocation. By default with no extensions or no
1051 * extension callback handling, just the normal input buffer is
1052 * used then so it is efficient.
1053 */
Andy Greenb45993c2010-12-18 15:13:50 +00001054
Andy Greene84652c2013-02-08 13:01:02 +08001055 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001056drain:
Andy Green3182ece2013-01-20 17:08:31 +08001057#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001058 more = 1;
1059 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001060
Andy Green98a717c2011-03-06 13:14:15 +00001061 more = 0;
1062
1063 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001064 m = wsi->active_extensions[n]->callback(context,
1065 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001066 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001067 wsi->active_extensions_user[n],
1068 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001069 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001070 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001071 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001072 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001073 }
1074 if (m)
1075 more = 1;
1076 }
Andy Green3182ece2013-01-20 17:08:31 +08001077#endif
Andy Green98a717c2011-03-06 13:14:15 +00001078 /* service incoming data */
1079
1080 if (eff_buf.token_len) {
1081 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001082 (unsigned char *)eff_buf.token,
1083 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001084 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001085 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001086 n = 0;
1087 goto handled;
1088 }
Andy Green98a717c2011-03-06 13:14:15 +00001089 }
Andy Green3182ece2013-01-20 17:08:31 +08001090#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001091 eff_buf.token = NULL;
1092 eff_buf.token_len = 0;
1093 }
Andy Green3182ece2013-01-20 17:08:31 +08001094#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001095 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1096 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1097 lwsl_info("flow buffer: drained\n");
1098 free(wsi->u.ws.rxflow_buffer);
1099 wsi->u.ws.rxflow_buffer = NULL;
1100 /* having drained the rxflow buffer, can rearm POLLIN */
1101 _libwebsocket_rx_flow_control(wsi);
1102 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001103
1104#ifdef LWS_OPENSSL_SUPPORT
1105 if (wsi->ssl && SSL_pending(wsi->ssl))
1106 goto read_pending;
1107#endif
Andy Green98a717c2011-03-06 13:14:15 +00001108 break;
Andy Green76f61e72013-01-16 11:53:05 +08001109
1110 default:
Andy Green03674a62013-01-16 11:47:40 +08001111#ifdef LWS_NO_CLIENT
1112 break;
1113#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001114 n = lws_client_socket_service(context, wsi, pollfd);
1115 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001116#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001117 }
1118
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001119 n = 0;
1120 goto handled;
1121
1122close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001123 libwebsocket_close_and_free_session(context, wsi,
1124 LWS_CLOSE_STATUS_NOSTATUS);
1125 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001126
1127handled:
1128 pollfd->revents = 0;
1129 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001130}
1131
Andy Green0d338332011-02-12 11:57:43 +00001132
Andy Green6964bb52011-01-23 16:50:33 +00001133/**
1134 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001135 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001136 *
1137 * This function closes any active connections and then frees the
1138 * context. After calling this, any further use of the context is
1139 * undefined.
1140 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001141LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001142libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001143{
Andy Green3182ece2013-01-20 17:08:31 +08001144#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001145 int n;
1146 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001147 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001148 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001149
Andy Greend636e352013-01-29 12:36:17 +08001150#ifdef LWS_LATENCY
1151 if (context->worst_latency_info[0])
1152 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1153#endif
1154
Andy Greendfb23042013-01-17 12:26:48 +08001155 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001156 struct libwebsocket *wsi =
1157 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001158 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001159 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1160 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001161 }
Andy Green6964bb52011-01-23 16:50:33 +00001162
Andy Greena41314f2011-05-23 10:00:03 +01001163 /*
1164 * give all extensions a chance to clean up any per-context
1165 * allocations they might have made
1166 */
1167
1168 ext = context->extensions;
1169 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1170 if (context->listen_port)
1171 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001172 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001173 ext->callback(context, ext, NULL,
1174 (enum libwebsocket_extension_callback_reasons)m,
1175 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001176 ext++;
1177 }
Andy Greena7109e62013-02-11 12:05:54 +08001178
1179 /*
1180 * inform all the protocols that they are done and will have no more
1181 * callbacks
1182 */
1183
1184 while (protocol->callback) {
1185 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1186 NULL, NULL, 0);
1187 protocol++;
1188 }
1189
Andy Green3182ece2013-01-20 17:08:31 +08001190#endif
Andy Greena41314f2011-05-23 10:00:03 +01001191
Peter Hinz56885f32011-03-02 22:03:47 +00001192#ifdef WIN32
1193#else
1194 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001195#endif
1196
Peter Hinz56885f32011-03-02 22:03:47 +00001197#ifdef LWS_OPENSSL_SUPPORT
1198 if (context->ssl_ctx)
1199 SSL_CTX_free(context->ssl_ctx);
1200 if (context->ssl_client_ctx)
1201 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001202
1203 ERR_remove_state(0);
1204 ERR_free_strings();
1205 EVP_cleanup();
1206 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001207#endif
1208
Andy Green46596482013-02-11 11:04:01 +08001209 if (context->fds)
1210 free(context->fds);
1211 if (context->lws_lookup)
1212 free(context->lws_lookup);
1213
Peter Hinz56885f32011-03-02 22:03:47 +00001214 free(context);
1215
1216#ifdef WIN32
1217 WSACleanup();
1218#endif
Andy Green6964bb52011-01-23 16:50:33 +00001219}
1220
Andy Greend88146d2013-01-22 12:40:35 +08001221/**
Andy Greenb5b23192013-02-11 17:13:32 +08001222 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001223 * @context: Websocket context
1224 *
1225 * This returns the optional user allocation that can be attached to
1226 * the context the sockets live in at context_create time. It's a way
1227 * to let all sockets serviced in the same context share data without
1228 * using globals statics in the user code.
1229 */
Alon Levy0291eb32012-10-19 11:21:56 +02001230LWS_EXTERN void *
1231libwebsocket_context_user(struct libwebsocket_context *context)
1232{
Andy Greenb5b23192013-02-11 17:13:32 +08001233 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001234}
1235
Andy Green6964bb52011-01-23 16:50:33 +00001236/**
1237 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001238 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001239 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1240 * service otherwise block and service immediately, returning
1241 * after the timeout if nothing needed service.
1242 *
1243 * This function deals with any pending websocket traffic, for three
1244 * kinds of event. It handles these events on both server and client
1245 * types of connection the same.
1246 *
1247 * 1) Accept new connections to our context's server
1248 *
Andy Green6f520a52013-01-29 17:57:39 +08001249 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001250 * server or client connections.
1251 *
1252 * You need to call this service function periodically to all the above
1253 * functions to happen; if your application is single-threaded you can
1254 * just call it in your main event loop.
1255 *
1256 * Alternatively you can fork a new process that asynchronously handles
1257 * calling this service in a loop. In that case you are happy if this
1258 * call blocks your thread until it needs to take care of something and
1259 * would call it with a large nonzero timeout. Your loop then takes no
1260 * CPU while there is nothing happening.
1261 *
1262 * If you are calling it in a single-threaded app, you don't want it to
1263 * wait around blocking other things in your loop from happening, so you
1264 * would call it with a timeout_ms of 0, so it returns immediately if
1265 * nothing is pending, or as soon as it services whatever was pending.
1266 */
1267
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001268LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001269libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001270{
1271 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001272 int m;
Andy Greene92cd172011-01-19 13:11:55 +00001273
1274 /* stay dead once we are dead */
1275
Peter Hinz56885f32011-03-02 22:03:47 +00001276 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001277 return 1;
1278
Andy Green0d338332011-02-12 11:57:43 +00001279 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001280
Peter Hinz56885f32011-03-02 22:03:47 +00001281 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001282 if (n == 0) /* poll timeout */
1283 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001284
Andy Greendfb23042013-01-17 12:26:48 +08001285 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001286 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001287
Andy Greendfb23042013-01-17 12:26:48 +08001288 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001289
Andy Greenca0a1292013-03-16 11:24:23 +08001290 for (n = 0; n < context->fds_count; n++) {
1291 if (!context->fds[n].revents)
1292 continue;
1293 m = libwebsocket_service_fd(context, &context->fds[n]);
1294 if (m < 0)
1295 return -1;
1296 /* if something closed, retry this slot */
1297 if (m)
1298 n--;
1299 }
1300
Andy Greene92cd172011-01-19 13:11:55 +00001301 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001302}
1303
Andy Green3182ece2013-01-20 17:08:31 +08001304#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001305int
1306lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001307 struct libwebsocket *wsi,
1308 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001309 void *v, size_t len)
1310{
1311 int n;
1312 int handled = 0;
1313
1314 /* maybe an extension will take care of it for us */
1315
1316 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1317 if (!wsi->active_extensions[n]->callback)
1318 continue;
1319
1320 handled |= wsi->active_extensions[n]->callback(context,
1321 wsi->active_extensions[n], wsi,
1322 r, wsi->active_extensions_user[n], v, len);
1323 }
1324
1325 return handled;
1326}
1327
1328
1329void *
1330lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001331 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001332{
1333 int n = 0;
1334
Andy Green68b45042011-05-25 21:41:57 +01001335 if (wsi == NULL)
1336 return NULL;
1337
Andy Greena41314f2011-05-23 10:00:03 +01001338 while (n < wsi->count_active_extensions) {
1339 if (wsi->active_extensions[n] != ext) {
1340 n++;
1341 continue;
1342 }
1343 return wsi->active_extensions_user[n];
1344 }
1345
1346 return NULL;
1347}
Andy Green3182ece2013-01-20 17:08:31 +08001348#endif
Andy Greena41314f2011-05-23 10:00:03 +01001349
Andy Green90c7cbc2011-01-27 06:26:52 +00001350/**
1351 * libwebsocket_callback_on_writable() - Request a callback when this socket
1352 * becomes able to be written to without
1353 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001354 *
Peter Hinz56885f32011-03-02 22:03:47 +00001355 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001356 * @wsi: Websocket connection instance to get callback for
1357 */
1358
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001359LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001360libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001361 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001362{
Andy Green3182ece2013-01-20 17:08:31 +08001363#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001364 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001365 int handled = 0;
1366
1367 /* maybe an extension will take care of it for us */
1368
1369 for (n = 0; n < wsi->count_active_extensions; n++) {
1370 if (!wsi->active_extensions[n]->callback)
1371 continue;
1372
1373 handled |= wsi->active_extensions[n]->callback(context,
1374 wsi->active_extensions[n], wsi,
1375 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1376 wsi->active_extensions_user[n], NULL, 0);
1377 }
1378
1379 if (handled)
1380 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001381#endif
Andy Greendfb23042013-01-17 12:26:48 +08001382 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001383 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1384 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001385 return -1;
1386 }
1387
1388 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001389
Andy Green3221f922011-02-12 13:14:11 +00001390 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001391 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001392 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001393 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001394
Andy Green90c7cbc2011-01-27 06:26:52 +00001395 return 1;
1396}
1397
1398/**
1399 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1400 * all connections using the given protocol when it
1401 * becomes possible to write to each socket without
1402 * blocking in turn.
1403 *
1404 * @protocol: Protocol whose connections will get callbacks
1405 */
1406
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001407LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001408libwebsocket_callback_on_writable_all_protocol(
1409 const struct libwebsocket_protocols *protocol)
1410{
Peter Hinz56885f32011-03-02 22:03:47 +00001411 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001412 int n;
Andy Green0d338332011-02-12 11:57:43 +00001413 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001414
Andy Greendfb23042013-01-17 12:26:48 +08001415 for (n = 0; n < context->fds_count; n++) {
1416 wsi = context->lws_lookup[context->fds[n].fd];
1417 if (!wsi)
1418 continue;
1419 if (wsi->protocol == protocol)
1420 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001421 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001422
1423 return 0;
1424}
1425
Andy Greenbe93fef2011-02-14 20:25:43 +00001426/**
1427 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1428 *
1429 * You will not need this unless you are doing something special
1430 *
1431 * @wsi: Websocket connection instance
1432 * @reason: timeout reason
1433 * @secs: how many seconds
1434 */
1435
1436void
1437libwebsocket_set_timeout(struct libwebsocket *wsi,
1438 enum pending_timeout reason, int secs)
1439{
1440 struct timeval tv;
1441
1442 gettimeofday(&tv, NULL);
1443
1444 wsi->pending_timeout_limit = tv.tv_sec + secs;
1445 wsi->pending_timeout = reason;
1446}
1447
Andy Greena6cbece2011-01-27 20:06:03 +00001448
1449/**
1450 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1451 *
1452 * You will not need this unless you are doing something special
1453 *
1454 * @wsi: Websocket connection instance
1455 */
1456
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001457LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001458libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1459{
1460 return wsi->sock;
1461}
1462
Andy Greend636e352013-01-29 12:36:17 +08001463#ifdef LWS_LATENCY
1464void
Andy Greenb5b23192013-02-11 17:13:32 +08001465lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1466 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001467{
1468 struct timeval tv;
1469 unsigned long u;
1470 char buf[256];
1471
1472 gettimeofday(&tv, NULL);
1473
1474 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1475
1476 if (action) {
1477 if (completed) {
1478 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001479 sprintf(buf,
1480 "Completion first try lat %luus: %p: ret %d: %s\n",
1481 u - wsi->latency_start,
1482 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001483 else
Andy Greenb5b23192013-02-11 17:13:32 +08001484 sprintf(buf,
1485 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1486 u - wsi->action_start,
1487 u - wsi->latency_start,
1488 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001489 wsi->action_start = 0;
1490 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001491 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1492 u - wsi->latency_start,
1493 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001494 if (u - wsi->latency_start > context->worst_latency) {
1495 context->worst_latency = u - wsi->latency_start;
1496 strcpy(context->worst_latency_info, buf);
1497 }
1498 lwsl_latency("%s", buf);
1499 } else {
1500 wsi->latency_start = u;
1501 if (!wsi->action_start)
1502 wsi->action_start = u;
1503 }
1504}
1505#endif
1506
Andy Greena1ce6be2013-01-18 11:43:21 +08001507#ifdef LWS_NO_SERVER
1508int
Andy Greenca0a1292013-03-16 11:24:23 +08001509_libwebsocket_rx_flow_control(struct libswebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001510{
1511 return 0;
1512}
1513#else
Andy Green706961d2013-01-17 16:50:35 +08001514int
1515_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1516{
1517 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001518
Andy Greenca0a1292013-03-16 11:24:23 +08001519 /* there is no pending change */
1520 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001521 return 0;
1522
Andy Greenca0a1292013-03-16 11:24:23 +08001523 /* stuff is still buffered, not ready to really accept new input */
1524 if (wsi->u.ws.rxflow_buffer) {
1525 /* get ourselves called back to deal with stashed buffer */
1526 libwebsocket_callback_on_writable(context, wsi);
1527 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001528 }
1529
Andy Greenca0a1292013-03-16 11:24:23 +08001530 /* pending is cleared, we can change rxflow state */
1531
1532 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1533
1534 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1535 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1536
1537 /* adjust the pollfd for this wsi */
1538
1539 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001540 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1541 else
1542 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1543
Andy Greenca0a1292013-03-16 11:24:23 +08001544 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green706961d2013-01-17 16:50:35 +08001545 /* external POLL support via protocol 0 */
1546 context->protocols[0].callback(context, wsi,
1547 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001548 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001549 else
1550 /* external POLL support via protocol 0 */
1551 context->protocols[0].callback(context, wsi,
1552 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001553 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001554
1555 return 1;
1556}
Andy Greena1ce6be2013-01-18 11:43:21 +08001557#endif
Andy Green706961d2013-01-17 16:50:35 +08001558
Andy Green90c7cbc2011-01-27 06:26:52 +00001559/**
1560 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1561 * receieved packets.
1562 *
1563 * If the output side of a server process becomes choked, this allows flow
1564 * control for the input side.
1565 *
1566 * @wsi: Websocket connection instance to get callback for
1567 * @enable: 0 = disable read servicing for this connection, 1 = enable
1568 */
1569
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001570LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001571libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1572{
Andy Greenca0a1292013-03-16 11:24:23 +08001573 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1574 return 0;
1575
1576 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1577 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001578
Andy Green706961d2013-01-17 16:50:35 +08001579 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001580}
1581
Andy Greenb55451c2013-03-16 12:32:27 +08001582/**
1583 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1584 *
1585 * When the user server code realizes it can accept more input, it can
1586 * call this to have the RX flow restriction removed from all connections using
1587 * the given protocol.
1588 *
1589 * @protocol: all connections using this protocol will be allowed to receive
1590 */
1591
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001592LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001593libwebsocket_rx_flow_allow_all_protocol(
1594 const struct libwebsocket_protocols *protocol)
1595{
1596 struct libwebsocket_context *context = protocol->owning_server;
1597 int n;
1598 struct libwebsocket *wsi;
1599
1600 for (n = 0; n < context->fds_count; n++) {
1601 wsi = context->lws_lookup[context->fds[n].fd];
1602 if (!wsi)
1603 continue;
1604 if (wsi->protocol == protocol)
1605 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1606 }
1607}
1608
Andy Green706961d2013-01-17 16:50:35 +08001609
Andy Green2ac5a6f2011-01-28 10:00:18 +00001610/**
1611 * libwebsocket_canonical_hostname() - returns this host's hostname
1612 *
1613 * This is typically used by client code to fill in the host parameter
1614 * when making a client connection. You can only call it after the context
1615 * has been created.
1616 *
Peter Hinz56885f32011-03-02 22:03:47 +00001617 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001618 */
1619
1620
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001621LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001622libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001623{
Peter Hinz56885f32011-03-02 22:03:47 +00001624 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001625}
1626
1627
Andy Green90c7cbc2011-01-27 06:26:52 +00001628static void sigpipe_handler(int x)
1629{
1630}
1631
Andy Green6901cb32011-02-21 08:06:47 +00001632#ifdef LWS_OPENSSL_SUPPORT
1633static int
1634OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1635{
1636
1637 SSL *ssl;
1638 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001639 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001640
1641 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1642 SSL_get_ex_data_X509_STORE_CTX_idx());
1643
1644 /*
Andy Green2e24da02011-03-05 16:12:04 +00001645 * !!! nasty openssl requires the index to come as a library-scope
1646 * static
Andy Green6901cb32011-02-21 08:06:47 +00001647 */
Andy Green2e24da02011-03-05 16:12:04 +00001648 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001649
Peter Hinz56885f32011-03-02 22:03:47 +00001650 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001651 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1652 x509_ctx, ssl, preverify_ok);
1653
1654 /* convert return code from 0 = OK to 1 = OK */
1655
1656 if (!n)
1657 n = 1;
1658 else
1659 n = 0;
1660
1661 return n;
1662}
1663#endif
1664
Andy Green706961d2013-01-17 16:50:35 +08001665int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001666 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001667 struct libwebsocket *wsi,
1668 enum libwebsocket_callback_reasons reason, void *user,
1669 void *in, size_t len)
1670{
1671 int n;
1672
1673 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001674 if (!n)
1675 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001676
Andy Greenaedc9532013-02-10 21:21:24 +08001677 return n;
Andy Green706961d2013-01-17 16:50:35 +08001678}
1679
Andy Greenb45993c2010-12-18 15:13:50 +00001680
Andy Greenab990e42010-10-31 12:42:52 +00001681/**
Andy Green4739e5c2011-01-22 12:51:57 +00001682 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001683 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001684 *
Andy Green1b265272013-02-09 14:01:09 +08001685 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001686 * of all initialization in one step.
1687 *
Andy Greene92cd172011-01-19 13:11:55 +00001688 * After initialization, it returns a struct libwebsocket_context * that
1689 * represents this server. After calling, user code needs to take care
1690 * of calling libwebsocket_service() with the context pointer to get the
1691 * server's sockets serviced. This can be done in the same process context
1692 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001693 *
Andy Green8f037e42010-12-19 22:13:26 +00001694 * The protocol callback functions are called for a handful of events
1695 * including http requests coming in, websocket connections becoming
1696 * established, and data arriving; it's also called periodically to allow
1697 * async transmission.
1698 *
1699 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1700 * at that time websocket protocol has not been negotiated. Other
1701 * protocols after the first one never see any HTTP callack activity.
1702 *
1703 * The server created is a simple http server by default; part of the
1704 * websocket standard is upgrading this http connection to a websocket one.
1705 *
1706 * This allows the same server to provide files like scripts and favicon /
1707 * images or whatever over http and dynamic data over websockets all in
1708 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001709 */
Andy Green4ea60062010-10-30 12:15:07 +01001710
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001711LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001712libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001713{
Peter Hinz56885f32011-03-02 22:03:47 +00001714 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001715 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001716 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001717#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001718 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001719 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001720 struct sockaddr_in serv_addr;
1721#endif
Andy Green3182ece2013-01-20 17:08:31 +08001722#ifndef LWS_NO_EXTENSIONS
1723 int m;
Andy Green1b265272013-02-09 14:01:09 +08001724 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001725#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001726
Andy Green3faa9c72010-11-08 17:03:03 +00001727#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001728 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001729#endif
1730
Joakim Soderberg4c531232013-02-06 15:26:58 +09001731#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001732 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001733#endif
1734
Andy Greenb3a614a2013-01-19 13:08:17 +08001735 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001736 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001737 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001738 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001739#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001740 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1741 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001742#else
1743 lwsl_notice(" Configured without extension support\n");
1744#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001745 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1746 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001747 if (info->ssl_cipher_list)
1748 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001749 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1750 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001751
Peter Hinz56885f32011-03-02 22:03:47 +00001752#ifdef _WIN32
1753 {
1754 WORD wVersionRequested;
1755 WSADATA wsaData;
1756 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001757 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001758
1759 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1760 wVersionRequested = MAKEWORD(2, 2);
1761
1762 err = WSAStartup(wVersionRequested, &wsaData);
1763 if (err != 0) {
1764 /* Tell the user that we could not find a usable */
1765 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001766 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001767 return NULL;
1768 }
David Galeano7b11fec2011-10-04 19:55:18 +08001769
Andy Green6ee372f2012-04-09 15:09:01 +08001770 /* default to a poll() made out of select() */
1771 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001772
Andy Green6ee372f2012-04-09 15:09:01 +08001773 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001774 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001775 if (wsdll)
1776 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001777
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001778 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001779 if (!poll)
1780 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001781 }
1782#endif
1783
Andy Greenb5b23192013-02-11 17:13:32 +08001784 context = (struct libwebsocket_context *)
1785 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001786 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001787 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001788 return NULL;
1789 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001790 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001791#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001792 context->started_with_parent = pid_daemon;
1793 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1794#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001795
Joakim Soderberg4c531232013-02-06 15:26:58 +09001796 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001797 context->protocols = info->protocols;
1798 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001799 context->http_proxy_port = 0;
1800 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001801 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001802 /* to reduce this allocation, */
1803 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001804 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1805 sizeof(struct libwebsocket_context),
1806 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1807 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001808 sizeof(struct libwebsocket_context) +
1809 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1810 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001811
Andy Greenb5b23192013-02-11 17:13:32 +08001812 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1813 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001814 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001815 lwsl_err("Unable to allocate fds array for %d connections\n",
1816 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001817 free(context);
1818 return NULL;
1819 }
Andy Greenb5b23192013-02-11 17:13:32 +08001820 context->lws_lookup = (struct libwebsocket **)
1821 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001822 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001823 lwsl_err(
1824 "Unable to allocate lws_lookup array for %d connections\n",
1825 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001826 free(context->fds);
1827 free(context);
1828 return NULL;
1829 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001830 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1831 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001832
Peter Hinz56885f32011-03-02 22:03:47 +00001833 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001834#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001835 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001836#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001837 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001838 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001839
Peter Hinz56885f32011-03-02 22:03:47 +00001840#ifdef WIN32
1841 context->fd_random = 0;
1842#else
1843 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1844 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001845 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001846 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001847 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001848 }
Peter Hinz56885f32011-03-02 22:03:47 +00001849#endif
Andy Green44eee682011-02-10 09:32:24 +00001850
Peter Hinz56885f32011-03-02 22:03:47 +00001851#ifdef LWS_OPENSSL_SUPPORT
1852 context->use_ssl = 0;
1853 context->ssl_ctx = NULL;
1854 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001855 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001856#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001857
Andy Greena1ce6be2013-01-18 11:43:21 +08001858 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001859
Andy Greena1ce6be2013-01-18 11:43:21 +08001860#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001861 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001862 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001863 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001864 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001865
Andy Greenb5b23192013-02-11 17:13:32 +08001866 lwsl_notice(" canonical_hostname = %s\n",
1867 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001868 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001869#endif
Andy Greena69f0512012-05-03 12:32:38 +08001870
Andy Green9659f372011-01-27 22:01:43 +00001871 /* split the proxy ads:port if given */
1872
1873 p = getenv("http_proxy");
1874 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001875 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001876 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001877 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001878 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001879
Peter Hinz56885f32011-03-02 22:03:47 +00001880 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001881 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001882 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001883 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001884 }
1885 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001886 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001887
Andy Greenb3a614a2013-01-19 13:08:17 +08001888 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001889 context->http_proxy_address,
1890 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001891 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001892
Andy Greena1ce6be2013-01-18 11:43:21 +08001893#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001894 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001895
Andy Green3faa9c72010-11-08 17:03:03 +00001896#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001897 context->use_ssl = info->ssl_cert_filepath != NULL &&
1898 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001899#ifdef USE_CYASSL
1900 lwsl_notice(" Compiled with CYASSL support\n");
1901#else
1902 lwsl_notice(" Compiled with OpenSSL support\n");
1903#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001904 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001905 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001906 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001907 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001908
Andy Green90c7cbc2011-01-27 06:26:52 +00001909#else
Andy Green2b40b792013-02-10 22:22:01 +08001910 if (info->ssl_cert_filepath != NULL &&
1911 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001912 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001913 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001914 }
Andy Greenb5b23192013-02-11 17:13:32 +08001915 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001916#endif
Andy Greena17c6922013-01-20 20:21:54 +08001917
Andy Greenb5b23192013-02-11 17:13:32 +08001918 lwsl_notice(
1919 " per-conn mem: %u + %u headers + protocol rx buf\n",
1920 sizeof(struct libwebsocket),
1921 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001922 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001923#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001924
1925 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001926#ifdef WIN32
1927#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001928 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001929#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001930
1931
1932#ifdef LWS_OPENSSL_SUPPORT
1933
1934 /* basic openssl init */
1935
1936 SSL_library_init();
1937
1938 OpenSSL_add_all_algorithms();
1939 SSL_load_error_strings();
1940
Andy Green2e24da02011-03-05 16:12:04 +00001941 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001942 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1943
Andy Green90c7cbc2011-01-27 06:26:52 +00001944 /*
1945 * Firefox insists on SSLv23 not SSLv3
1946 * Konq disables SSLv2 by default now, SSLv23 works
1947 */
1948
1949 method = (SSL_METHOD *)SSLv23_server_method();
1950 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001951 lwsl_err("problem creating ssl method %lu: %s\n",
1952 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001953 ERR_error_string(ERR_get_error(),
1954 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001955 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001956 }
Peter Hinz56885f32011-03-02 22:03:47 +00001957 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1958 if (!context->ssl_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001959 lwsl_err("problem creating ssl context %lu: %s\n",
1960 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001961 ERR_error_string(ERR_get_error(),
1962 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001963 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001964 }
1965
David Galeanocc148e42013-01-10 10:18:59 +08001966#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001967 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001968#endif
David Galeano77a677c2013-01-10 10:14:12 +08001969 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001970 if (info->ssl_cipher_list)
1971 SSL_CTX_set_cipher_list(context->ssl_ctx,
1972 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001973
Andy Greena1ce6be2013-01-18 11:43:21 +08001974#ifndef LWS_NO_CLIENT
1975
Andy Green90c7cbc2011-01-27 06:26:52 +00001976 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001977
Andy Green1b265272013-02-09 14:01:09 +08001978 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001979 method = (SSL_METHOD *)SSLv23_client_method();
1980 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001981 lwsl_err("problem creating ssl method %lu: %s\n",
1982 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001983 ERR_error_string(ERR_get_error(),
1984 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001985 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001986 }
1987 /* create context */
1988 context->ssl_client_ctx = SSL_CTX_new(method);
1989 if (!context->ssl_client_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001990 lwsl_err("problem creating ssl context %lu: %s\n",
1991 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001992 ERR_error_string(ERR_get_error(),
1993 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001994 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001995 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001996
David Galeanocc148e42013-01-10 10:18:59 +08001997#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001998 SSL_CTX_set_options(context->ssl_client_ctx,
1999 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002000#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002001 SSL_CTX_set_options(context->ssl_client_ctx,
2002 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002003 if (info->ssl_cipher_list)
2004 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2005 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002006
Peter Hinz56885f32011-03-02 22:03:47 +00002007 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002008 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002009 if (!SSL_CTX_load_verify_locations(
2010 context->ssl_client_ctx, NULL,
2011 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002012 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002013 "Unable to load SSL Client certs from %s "
2014 "(set by --with-client-cert-dir= "
2015 "in configure) -- client ssl isn't "
2016 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002017 } else
2018 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002019 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002020 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002021 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002022 "Unable to load SSL Client certs "
2023 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002024 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002025
2026 /*
2027 * callback allowing user code to load extra verification certs
2028 * helping the client to verify server identity
2029 */
2030
2031 context->protocols[0].callback(context, NULL,
2032 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2033 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002034 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002035#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002036
Andy Greenc6bf2c22011-02-20 11:10:47 +00002037 /* as a server, are we requiring clients to identify themselves? */
2038
Andy Greenb5b23192013-02-11 17:13:32 +08002039 if (info->options &
2040 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002041
2042 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002043
Peter Hinz56885f32011-03-02 22:03:47 +00002044 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002045 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2046 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002047
2048 /*
2049 * give user code a chance to load certs into the server
2050 * allowing it to verify incoming client certs
2051 */
2052
Peter Hinz56885f32011-03-02 22:03:47 +00002053 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002054 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002055 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002056 }
2057
Peter Hinz56885f32011-03-02 22:03:47 +00002058 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002059
2060 /* openssl init for server sockets */
2061
Andy Green3faa9c72010-11-08 17:03:03 +00002062 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002063 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002064 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002065 if (n != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002066 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002067 info->ssl_cert_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002068 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002069 ERR_error_string(ERR_get_error(),
2070 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002071 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002072 }
2073 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002074 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002075 info->ssl_private_key_filepath,
2076 SSL_FILETYPE_PEM) != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002077 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002078 info->ssl_private_key_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002079 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002080 ERR_error_string(ERR_get_error(),
2081 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002082 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002083 }
2084 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002085 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002086 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002087 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002088 }
2089
2090 /* SSL is happy and has a cert it's content with */
2091 }
2092#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002093
Andy Greendf736162011-01-18 15:39:02 +00002094 /* selftest */
2095
2096 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002097 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002098
Andy Greena1ce6be2013-01-18 11:43:21 +08002099#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002100 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002101
Andy Green1b265272013-02-09 14:01:09 +08002102 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002103 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002104
Andy Green4739e5c2011-01-22 12:51:57 +00002105 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2106 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002107 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002108 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002109 }
Andy Green775c0dd2010-10-29 14:15:22 +01002110
Joakim Soderberg4c531232013-02-06 15:26:58 +09002111#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002112 /*
2113 * allow us to restart even if old sockets in TIME_WAIT
2114 * (REUSEADDR on Unix means, "don't hang on to this
2115 * address after the listener is closed." On Windows, though,
2116 * it means "don't keep other processes from binding to
2117 * this address while we're using it)
2118 */
Andy Green6ee372f2012-04-09 15:09:01 +08002119 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2120 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002121#endif
Andy Green6c939552011-03-08 08:56:57 +00002122
2123 /* Disable Nagle */
2124 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002125 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2126 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002127
Joakim Soderberg4c531232013-02-06 15:26:58 +09002128 #ifdef WIN32
2129 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002130 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002131 #else
Andy Greene2160712013-01-28 12:19:10 +08002132 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002133 #endif
Andy Greene2160712013-01-28 12:19:10 +08002134
Andy Green4739e5c2011-01-22 12:51:57 +00002135 bzero((char *) &serv_addr, sizeof(serv_addr));
2136 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002137 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002138 serv_addr.sin_addr.s_addr = INADDR_ANY;
2139 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002140 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002141 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002142 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002143
2144 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2145 sizeof(serv_addr));
2146 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002147 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002148 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002149 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002150 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002151 }
Andy Green0d338332011-02-12 11:57:43 +00002152
Andy Greenb5b23192013-02-11 17:13:32 +08002153 wsi = (struct libwebsocket *)malloc(
2154 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002155 if (wsi == NULL) {
2156 lwsl_err("Out of mem\n");
2157 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002158 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002159 }
Andy Greenb5b23192013-02-11 17:13:32 +08002160 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002161 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002162#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002163 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002164#endif
Andy Green0d338332011-02-12 11:57:43 +00002165 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002166
2167 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002168
Andy Green65b0e912013-01-16 07:59:47 +08002169 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2170 context->listen_service_count = 0;
2171 context->listen_service_fd = sockfd;
2172
Andy Greena824d182013-01-15 20:52:29 +08002173 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002174 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002175 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002176#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002177
Andy Green6ee372f2012-04-09 15:09:01 +08002178 /*
2179 * drop any root privs for this process
2180 * to listen on port < 1023 we would have needed root, but now we are
2181 * listening, we don't want the power for anything else
2182 */
Peter Hinz56885f32011-03-02 22:03:47 +00002183#ifdef WIN32
2184#else
Andy Green1b265272013-02-09 14:01:09 +08002185 if (info->gid != -1)
2186 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002187 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002188 if (info->uid != -1)
2189 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002190 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002191#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002192
Andy Green6f520a52013-01-29 17:57:39 +08002193 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002194
Peter Hinz56885f32011-03-02 22:03:47 +00002195 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002196 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002197 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002198
Andy Green43db0452013-01-10 19:50:35 +08002199 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002200 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002201
Andy Green1b265272013-02-09 14:01:09 +08002202 info->protocols[context->count_protocols].owning_server =
2203 context;
2204 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002205 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002206
2207 /*
2208 * inform all the protocols that they are doing their one-time
2209 * initialization if they want to
2210 */
2211 info->protocols[context->count_protocols].callback(context,
2212 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002213 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002214
Andy Green3182ece2013-01-20 17:08:31 +08002215#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002216 /*
2217 * give all extensions a chance to create any per-context
2218 * allocations they need
2219 */
2220
2221 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002222 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002223 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002224
Andy Green1b265272013-02-09 14:01:09 +08002225 if (info->extensions) {
2226 ext = info->extensions;
2227 while (ext->callback) {
2228 lwsl_ext(" Extension: %s\n", ext->name);
2229 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002230 (enum libwebsocket_extension_callback_reasons)m,
2231 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002232 ext++;
2233 }
Andy Greena41314f2011-05-23 10:00:03 +01002234 }
Andy Green3182ece2013-01-20 17:08:31 +08002235#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002236 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002237
2238bail:
2239 libwebsocket_context_destroy(context);
2240 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002241}
Andy Greenb45993c2010-12-18 15:13:50 +00002242
Andy Greenb45993c2010-12-18 15:13:50 +00002243/**
2244 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002245 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002246 * @wsi: pointer to struct websocket you want to know the protocol of
2247 *
Andy Green8f037e42010-12-19 22:13:26 +00002248 *
Andy Green6f520a52013-01-29 17:57:39 +08002249 * Some apis can act on all live connections of a given protocol,
2250 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002251 */
Andy Greenab990e42010-10-31 12:42:52 +00002252
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002253LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002254libwebsockets_get_protocol(struct libwebsocket *wsi)
2255{
2256 return wsi->protocol;
2257}
2258
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002259LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002260libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2261{
Andy Green623a98d2013-01-21 11:04:23 +08002262 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002263}
Alex Bligh49146db2011-11-07 17:19:25 +08002264
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002265LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002266libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2267{
Andy Green623a98d2013-01-21 11:04:23 +08002268 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002269}
2270
Andy Green2af4d5b2013-02-18 16:30:10 +08002271int
Alex Bligh49146db2011-11-07 17:19:25 +08002272libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2273{
Andy Green67d556c2013-02-15 22:31:55 +08002274 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002275 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002276
Alex Bligh49146db2011-11-07 17:19:25 +08002277 /* allocate the per-connection user memory (if any) */
2278
2279 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2280 wsi->user_space = malloc(
2281 wsi->protocol->per_session_data_size);
2282 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002283 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002284 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002285 }
Andy Green6ee372f2012-04-09 15:09:01 +08002286 memset(wsi->user_space, 0,
2287 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002288 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002289 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002290}
Andy Green43db0452013-01-10 19:50:35 +08002291
Andy Green0b319092013-01-19 11:17:56 +08002292static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002293{
Andy Green0b319092013-01-19 11:17:56 +08002294 char buf[300];
2295 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002296 int n;
2297
2298 gettimeofday(&tv, NULL);
2299
2300 buf[0] = '\0';
2301 for (n = 0; n < LLL_COUNT; n++)
2302 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002303 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002304 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002305 break;
2306 }
Andy Greenb5b23192013-02-11 17:13:32 +08002307
Andy Green0b319092013-01-19 11:17:56 +08002308 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002309}
2310
Joakim Soderberg4c531232013-02-06 15:26:58 +09002311#ifdef WIN32
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002312LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002313{
2314 lwsl_emit_stderr(level, line);
2315}
2316#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002317LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002318{
2319 int syslog_level = LOG_DEBUG;
2320
2321 switch (level) {
2322 case LLL_ERR:
2323 syslog_level = LOG_ERR;
2324 break;
2325 case LLL_WARN:
2326 syslog_level = LOG_WARNING;
2327 break;
2328 case LLL_NOTICE:
2329 syslog_level = LOG_NOTICE;
2330 break;
2331 case LLL_INFO:
2332 syslog_level = LOG_INFO;
2333 break;
2334 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002335 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002336}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002337#endif
Andy Greenc11db202013-01-19 11:12:16 +08002338
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002339LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002340{
Andy Greende8f27a2013-01-12 09:17:42 +08002341 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002342 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002343
2344 if (!(log_level & filter))
2345 return;
2346
Andy Green43db0452013-01-10 19:50:35 +08002347 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002348 vsnprintf(buf, sizeof(buf), format, ap);
2349 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002350 va_end(ap);
2351
Andy Green0b319092013-01-19 11:17:56 +08002352 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002353}
2354
2355/**
2356 * lws_set_log_level() - Set the logging bitfield
2357 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002358 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2359 * function to perform log string emission instead of
2360 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002361 *
Andy Greenc6511a02013-02-19 10:01:48 +08002362 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002363 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002364 */
2365
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002366LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002367 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002368{
2369 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002370 if (log_emit_function)
2371 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002372}