blob: c96db244702c29c597728ef8aa16789b2d933930 [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
94const char *
95lws_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
450void
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{
455 unsigned int len;
456 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 Hinz56885f32011-03-02 22:03:47 +0000523int libwebsockets_get_random(struct libwebsocket_context *context,
524 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
Andy Greena47865f2013-02-10 09:39:47 +0800557#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
558
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
Andy Green95a7b5d2011-03-06 10:29:39 +0000613int lws_send_pipe_choked(struct libwebsocket *wsi)
614{
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
833int
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 Green43db0452013-01-10 19:50:35 +08001031 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +00001032 eff_buf.token_len);
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 Greenb5b23192013-02-11 17:13:32 +08001039 lwsl_info("closing connection 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 */
1141void
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
Andy Greene92cd172011-01-19 13:11:55 +00001268int
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
1359int
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
1407int
1408libwebsocket_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
1457int
1458libwebsocket_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
1570int
1571libwebsocket_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 Green706961d2013-01-17 16:50:35 +08001582
Andy Green2ac5a6f2011-01-28 10:00:18 +00001583/**
1584 * libwebsocket_canonical_hostname() - returns this host's hostname
1585 *
1586 * This is typically used by client code to fill in the host parameter
1587 * when making a client connection. You can only call it after the context
1588 * has been created.
1589 *
Peter Hinz56885f32011-03-02 22:03:47 +00001590 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001591 */
1592
1593
1594extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001595libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001596{
Peter Hinz56885f32011-03-02 22:03:47 +00001597 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001598}
1599
1600
Andy Green90c7cbc2011-01-27 06:26:52 +00001601static void sigpipe_handler(int x)
1602{
1603}
1604
Andy Green6901cb32011-02-21 08:06:47 +00001605#ifdef LWS_OPENSSL_SUPPORT
1606static int
1607OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1608{
1609
1610 SSL *ssl;
1611 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001612 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001613
1614 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1615 SSL_get_ex_data_X509_STORE_CTX_idx());
1616
1617 /*
Andy Green2e24da02011-03-05 16:12:04 +00001618 * !!! nasty openssl requires the index to come as a library-scope
1619 * static
Andy Green6901cb32011-02-21 08:06:47 +00001620 */
Andy Green2e24da02011-03-05 16:12:04 +00001621 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001622
Peter Hinz56885f32011-03-02 22:03:47 +00001623 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001624 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1625 x509_ctx, ssl, preverify_ok);
1626
1627 /* convert return code from 0 = OK to 1 = OK */
1628
1629 if (!n)
1630 n = 1;
1631 else
1632 n = 0;
1633
1634 return n;
1635}
1636#endif
1637
Andy Green706961d2013-01-17 16:50:35 +08001638int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001639 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001640 struct libwebsocket *wsi,
1641 enum libwebsocket_callback_reasons reason, void *user,
1642 void *in, size_t len)
1643{
1644 int n;
1645
1646 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001647 if (!n)
1648 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001649
Andy Greenaedc9532013-02-10 21:21:24 +08001650 return n;
Andy Green706961d2013-01-17 16:50:35 +08001651}
1652
Andy Greenb45993c2010-12-18 15:13:50 +00001653
Andy Greenab990e42010-10-31 12:42:52 +00001654/**
Andy Green4739e5c2011-01-22 12:51:57 +00001655 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001656 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001657 *
Andy Green1b265272013-02-09 14:01:09 +08001658 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001659 * of all initialization in one step.
1660 *
Andy Greene92cd172011-01-19 13:11:55 +00001661 * After initialization, it returns a struct libwebsocket_context * that
1662 * represents this server. After calling, user code needs to take care
1663 * of calling libwebsocket_service() with the context pointer to get the
1664 * server's sockets serviced. This can be done in the same process context
1665 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001666 *
Andy Green8f037e42010-12-19 22:13:26 +00001667 * The protocol callback functions are called for a handful of events
1668 * including http requests coming in, websocket connections becoming
1669 * established, and data arriving; it's also called periodically to allow
1670 * async transmission.
1671 *
1672 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1673 * at that time websocket protocol has not been negotiated. Other
1674 * protocols after the first one never see any HTTP callack activity.
1675 *
1676 * The server created is a simple http server by default; part of the
1677 * websocket standard is upgrading this http connection to a websocket one.
1678 *
1679 * This allows the same server to provide files like scripts and favicon /
1680 * images or whatever over http and dynamic data over websockets all in
1681 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001682 */
Andy Green4ea60062010-10-30 12:15:07 +01001683
Andy Greene92cd172011-01-19 13:11:55 +00001684struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001685libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001686{
Peter Hinz56885f32011-03-02 22:03:47 +00001687 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001688 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001689 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001690#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001691 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001692 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001693 struct sockaddr_in serv_addr;
1694#endif
Andy Green3182ece2013-01-20 17:08:31 +08001695#ifndef LWS_NO_EXTENSIONS
1696 int m;
Andy Green1b265272013-02-09 14:01:09 +08001697 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001698#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001699
Andy Green3faa9c72010-11-08 17:03:03 +00001700#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001701 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001702#endif
1703
Joakim Soderberg4c531232013-02-06 15:26:58 +09001704#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001705 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001706#endif
1707
Andy Greenb3a614a2013-01-19 13:08:17 +08001708 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001709 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001710 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001711 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001712#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001713 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1714 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001715#else
1716 lwsl_notice(" Configured without extension support\n");
1717#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001718 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1719 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001720 if (info->ssl_cipher_list)
1721 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001722 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1723 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001724
Peter Hinz56885f32011-03-02 22:03:47 +00001725#ifdef _WIN32
1726 {
1727 WORD wVersionRequested;
1728 WSADATA wsaData;
1729 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001730 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001731
1732 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1733 wVersionRequested = MAKEWORD(2, 2);
1734
1735 err = WSAStartup(wVersionRequested, &wsaData);
1736 if (err != 0) {
1737 /* Tell the user that we could not find a usable */
1738 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001739 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001740 return NULL;
1741 }
David Galeano7b11fec2011-10-04 19:55:18 +08001742
Andy Green6ee372f2012-04-09 15:09:01 +08001743 /* default to a poll() made out of select() */
1744 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001745
Andy Green6ee372f2012-04-09 15:09:01 +08001746 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001747 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001748 if (wsdll)
1749 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001750
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001751 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001752 if (!poll)
1753 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001754 }
1755#endif
1756
Andy Greenb5b23192013-02-11 17:13:32 +08001757 context = (struct libwebsocket_context *)
1758 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001759 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001760 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001761 return NULL;
1762 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001763 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001764#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001765 context->started_with_parent = pid_daemon;
1766 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1767#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001768
Joakim Soderberg4c531232013-02-06 15:26:58 +09001769 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001770 context->protocols = info->protocols;
1771 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001772 context->http_proxy_port = 0;
1773 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001774 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001775 /* to reduce this allocation, */
1776 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001777 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1778 sizeof(struct libwebsocket_context),
1779 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1780 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001781 sizeof(struct libwebsocket_context) +
1782 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1783 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001784
Andy Greenb5b23192013-02-11 17:13:32 +08001785 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1786 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001787 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001788 lwsl_err("Unable to allocate fds array for %d connections\n",
1789 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001790 free(context);
1791 return NULL;
1792 }
Andy Greenb5b23192013-02-11 17:13:32 +08001793 context->lws_lookup = (struct libwebsocket **)
1794 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001795 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001796 lwsl_err(
1797 "Unable to allocate lws_lookup array for %d connections\n",
1798 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001799 free(context->fds);
1800 free(context);
1801 return NULL;
1802 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001803 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1804 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001805
Peter Hinz56885f32011-03-02 22:03:47 +00001806 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001807#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001808 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001809#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001810 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001811 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001812
Peter Hinz56885f32011-03-02 22:03:47 +00001813#ifdef WIN32
1814 context->fd_random = 0;
1815#else
1816 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1817 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001818 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001819 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001820 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001821 }
Peter Hinz56885f32011-03-02 22:03:47 +00001822#endif
Andy Green44eee682011-02-10 09:32:24 +00001823
Peter Hinz56885f32011-03-02 22:03:47 +00001824#ifdef LWS_OPENSSL_SUPPORT
1825 context->use_ssl = 0;
1826 context->ssl_ctx = NULL;
1827 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001828 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001829#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001830
Andy Greena1ce6be2013-01-18 11:43:21 +08001831 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001832
Andy Greena1ce6be2013-01-18 11:43:21 +08001833#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001834 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001835 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001836 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001837 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001838
Andy Greenb5b23192013-02-11 17:13:32 +08001839 lwsl_notice(" canonical_hostname = %s\n",
1840 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001841 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001842#endif
Andy Greena69f0512012-05-03 12:32:38 +08001843
Andy Green9659f372011-01-27 22:01:43 +00001844 /* split the proxy ads:port if given */
1845
1846 p = getenv("http_proxy");
1847 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001848 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001849 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001850 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001851 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001852
Peter Hinz56885f32011-03-02 22:03:47 +00001853 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001854 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001855 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001856 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001857 }
1858 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001859 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001860
Andy Greenb3a614a2013-01-19 13:08:17 +08001861 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001862 context->http_proxy_address,
1863 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001864 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001865
Andy Greena1ce6be2013-01-18 11:43:21 +08001866#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001867 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001868
Andy Green3faa9c72010-11-08 17:03:03 +00001869#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001870 context->use_ssl = info->ssl_cert_filepath != NULL &&
1871 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001872#ifdef USE_CYASSL
1873 lwsl_notice(" Compiled with CYASSL support\n");
1874#else
1875 lwsl_notice(" Compiled with OpenSSL support\n");
1876#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001877 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001878 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001879 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001880 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001881
Andy Green90c7cbc2011-01-27 06:26:52 +00001882#else
Andy Green2b40b792013-02-10 22:22:01 +08001883 if (info->ssl_cert_filepath != NULL &&
1884 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001885 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001886 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001887 }
Andy Greenb5b23192013-02-11 17:13:32 +08001888 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001889#endif
Andy Greena17c6922013-01-20 20:21:54 +08001890
Andy Greenb5b23192013-02-11 17:13:32 +08001891 lwsl_notice(
1892 " per-conn mem: %u + %u headers + protocol rx buf\n",
1893 sizeof(struct libwebsocket),
1894 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001895 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001896#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001897
1898 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001899#ifdef WIN32
1900#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001901 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001902#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001903
1904
1905#ifdef LWS_OPENSSL_SUPPORT
1906
1907 /* basic openssl init */
1908
1909 SSL_library_init();
1910
1911 OpenSSL_add_all_algorithms();
1912 SSL_load_error_strings();
1913
Andy Green2e24da02011-03-05 16:12:04 +00001914 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001915 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1916
Andy Green90c7cbc2011-01-27 06:26:52 +00001917 /*
1918 * Firefox insists on SSLv23 not SSLv3
1919 * Konq disables SSLv2 by default now, SSLv23 works
1920 */
1921
1922 method = (SSL_METHOD *)SSLv23_server_method();
1923 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001924 lwsl_err("problem creating ssl method %lu: %s\n",
1925 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001926 ERR_error_string(ERR_get_error(),
1927 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001928 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001929 }
Peter Hinz56885f32011-03-02 22:03:47 +00001930 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1931 if (!context->ssl_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001932 lwsl_err("problem creating ssl context %lu: %s\n",
1933 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001934 ERR_error_string(ERR_get_error(),
1935 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001936 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001937 }
1938
David Galeanocc148e42013-01-10 10:18:59 +08001939#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001940 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001941#endif
David Galeano77a677c2013-01-10 10:14:12 +08001942 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001943 if (info->ssl_cipher_list)
1944 SSL_CTX_set_cipher_list(context->ssl_ctx,
1945 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001946
Andy Greena1ce6be2013-01-18 11:43:21 +08001947#ifndef LWS_NO_CLIENT
1948
Andy Green90c7cbc2011-01-27 06:26:52 +00001949 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001950
Andy Green1b265272013-02-09 14:01:09 +08001951 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001952 method = (SSL_METHOD *)SSLv23_client_method();
1953 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001954 lwsl_err("problem creating ssl method %lu: %s\n",
1955 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001956 ERR_error_string(ERR_get_error(),
1957 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001958 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001959 }
1960 /* create context */
1961 context->ssl_client_ctx = SSL_CTX_new(method);
1962 if (!context->ssl_client_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001963 lwsl_err("problem creating ssl context %lu: %s\n",
1964 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001965 ERR_error_string(ERR_get_error(),
1966 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001967 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001968 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001969
David Galeanocc148e42013-01-10 10:18:59 +08001970#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001971 SSL_CTX_set_options(context->ssl_client_ctx,
1972 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001973#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001974 SSL_CTX_set_options(context->ssl_client_ctx,
1975 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001976 if (info->ssl_cipher_list)
1977 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1978 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001979
Peter Hinz56885f32011-03-02 22:03:47 +00001980 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001981 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001982 if (!SSL_CTX_load_verify_locations(
1983 context->ssl_client_ctx, NULL,
1984 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001985 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001986 "Unable to load SSL Client certs from %s "
1987 "(set by --with-client-cert-dir= "
1988 "in configure) -- client ssl isn't "
1989 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001990 } else
1991 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001992 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001993 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001994 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001995 "Unable to load SSL Client certs "
1996 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001997 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001998
1999 /*
2000 * callback allowing user code to load extra verification certs
2001 * helping the client to verify server identity
2002 */
2003
2004 context->protocols[0].callback(context, NULL,
2005 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2006 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002007 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002008#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002009
Andy Greenc6bf2c22011-02-20 11:10:47 +00002010 /* as a server, are we requiring clients to identify themselves? */
2011
Andy Greenb5b23192013-02-11 17:13:32 +08002012 if (info->options &
2013 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002014
2015 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002016
Peter Hinz56885f32011-03-02 22:03:47 +00002017 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002018 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2019 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002020
2021 /*
2022 * give user code a chance to load certs into the server
2023 * allowing it to verify incoming client certs
2024 */
2025
Peter Hinz56885f32011-03-02 22:03:47 +00002026 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002027 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002028 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002029 }
2030
Peter Hinz56885f32011-03-02 22:03:47 +00002031 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002032
2033 /* openssl init for server sockets */
2034
Andy Green3faa9c72010-11-08 17:03:03 +00002035 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002036 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002037 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002038 if (n != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002039 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002040 info->ssl_cert_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002041 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002042 ERR_error_string(ERR_get_error(),
2043 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002044 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002045 }
2046 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002047 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002048 info->ssl_private_key_filepath,
2049 SSL_FILETYPE_PEM) != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002050 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002051 info->ssl_private_key_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002052 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002053 ERR_error_string(ERR_get_error(),
2054 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002055 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002056 }
2057 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002058 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002059 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002060 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002061 }
2062
2063 /* SSL is happy and has a cert it's content with */
2064 }
2065#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002066
Andy Greendf736162011-01-18 15:39:02 +00002067 /* selftest */
2068
2069 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002070 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002071
Andy Greena1ce6be2013-01-18 11:43:21 +08002072#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002073 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002074
Andy Green1b265272013-02-09 14:01:09 +08002075 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002076 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002077
Andy Green4739e5c2011-01-22 12:51:57 +00002078 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2079 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002080 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002081 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002082 }
Andy Green775c0dd2010-10-29 14:15:22 +01002083
Joakim Soderberg4c531232013-02-06 15:26:58 +09002084#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002085 /*
2086 * allow us to restart even if old sockets in TIME_WAIT
2087 * (REUSEADDR on Unix means, "don't hang on to this
2088 * address after the listener is closed." On Windows, though,
2089 * it means "don't keep other processes from binding to
2090 * this address while we're using it)
2091 */
Andy Green6ee372f2012-04-09 15:09:01 +08002092 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2093 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002094#endif
Andy Green6c939552011-03-08 08:56:57 +00002095
2096 /* Disable Nagle */
2097 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002098 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2099 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002100
Joakim Soderberg4c531232013-02-06 15:26:58 +09002101 #ifdef WIN32
2102 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002103 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002104 #else
Andy Greene2160712013-01-28 12:19:10 +08002105 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002106 #endif
Andy Greene2160712013-01-28 12:19:10 +08002107
Andy Green4739e5c2011-01-22 12:51:57 +00002108 bzero((char *) &serv_addr, sizeof(serv_addr));
2109 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002110 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002111 serv_addr.sin_addr.s_addr = INADDR_ANY;
2112 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002113 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002114 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002115 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002116
2117 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2118 sizeof(serv_addr));
2119 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002120 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002121 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002122 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002123 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002124 }
Andy Green0d338332011-02-12 11:57:43 +00002125
Andy Greenb5b23192013-02-11 17:13:32 +08002126 wsi = (struct libwebsocket *)malloc(
2127 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002128 if (wsi == NULL) {
2129 lwsl_err("Out of mem\n");
2130 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002131 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002132 }
Andy Greenb5b23192013-02-11 17:13:32 +08002133 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002134 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002135#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002136 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002137#endif
Andy Green0d338332011-02-12 11:57:43 +00002138 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002139
2140 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002141
Andy Green65b0e912013-01-16 07:59:47 +08002142 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2143 context->listen_service_count = 0;
2144 context->listen_service_fd = sockfd;
2145
Andy Greena824d182013-01-15 20:52:29 +08002146 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002147 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002148 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002149#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002150
Andy Green6ee372f2012-04-09 15:09:01 +08002151 /*
2152 * drop any root privs for this process
2153 * to listen on port < 1023 we would have needed root, but now we are
2154 * listening, we don't want the power for anything else
2155 */
Peter Hinz56885f32011-03-02 22:03:47 +00002156#ifdef WIN32
2157#else
Andy Green1b265272013-02-09 14:01:09 +08002158 if (info->gid != -1)
2159 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002160 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002161 if (info->uid != -1)
2162 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002163 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002164#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002165
Andy Green6f520a52013-01-29 17:57:39 +08002166 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002167
Peter Hinz56885f32011-03-02 22:03:47 +00002168 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002169 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002170 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002171
Andy Green43db0452013-01-10 19:50:35 +08002172 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002173 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002174
Andy Green1b265272013-02-09 14:01:09 +08002175 info->protocols[context->count_protocols].owning_server =
2176 context;
2177 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002178 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002179
2180 /*
2181 * inform all the protocols that they are doing their one-time
2182 * initialization if they want to
2183 */
2184 info->protocols[context->count_protocols].callback(context,
2185 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002186 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002187
Andy Green3182ece2013-01-20 17:08:31 +08002188#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002189 /*
2190 * give all extensions a chance to create any per-context
2191 * allocations they need
2192 */
2193
2194 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002195 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002196 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002197
Andy Green1b265272013-02-09 14:01:09 +08002198 if (info->extensions) {
2199 ext = info->extensions;
2200 while (ext->callback) {
2201 lwsl_ext(" Extension: %s\n", ext->name);
2202 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002203 (enum libwebsocket_extension_callback_reasons)m,
2204 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002205 ext++;
2206 }
Andy Greena41314f2011-05-23 10:00:03 +01002207 }
Andy Green3182ece2013-01-20 17:08:31 +08002208#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002209 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002210
2211bail:
2212 libwebsocket_context_destroy(context);
2213 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002214}
Andy Greenb45993c2010-12-18 15:13:50 +00002215
Andy Greenb45993c2010-12-18 15:13:50 +00002216/**
2217 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002218 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002219 * @wsi: pointer to struct websocket you want to know the protocol of
2220 *
Andy Green8f037e42010-12-19 22:13:26 +00002221 *
Andy Green6f520a52013-01-29 17:57:39 +08002222 * Some apis can act on all live connections of a given protocol,
2223 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002224 */
Andy Greenab990e42010-10-31 12:42:52 +00002225
Andy Greenb45993c2010-12-18 15:13:50 +00002226const struct libwebsocket_protocols *
2227libwebsockets_get_protocol(struct libwebsocket *wsi)
2228{
2229 return wsi->protocol;
2230}
2231
Andy Green82c3d542011-03-07 21:16:31 +00002232int
2233libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2234{
Andy Green623a98d2013-01-21 11:04:23 +08002235 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002236}
Alex Bligh49146db2011-11-07 17:19:25 +08002237
David Galeanoe2cf9922013-01-09 18:06:55 +08002238unsigned char
2239libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2240{
Andy Green623a98d2013-01-21 11:04:23 +08002241 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002242}
2243
Andy Green2af4d5b2013-02-18 16:30:10 +08002244int
Alex Bligh49146db2011-11-07 17:19:25 +08002245libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2246{
Andy Green67d556c2013-02-15 22:31:55 +08002247 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002248 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002249
Alex Bligh49146db2011-11-07 17:19:25 +08002250 /* allocate the per-connection user memory (if any) */
2251
2252 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2253 wsi->user_space = malloc(
2254 wsi->protocol->per_session_data_size);
2255 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002256 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002257 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002258 }
Andy Green6ee372f2012-04-09 15:09:01 +08002259 memset(wsi->user_space, 0,
2260 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002261 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002262 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002263}
Andy Green43db0452013-01-10 19:50:35 +08002264
Andy Green0b319092013-01-19 11:17:56 +08002265static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002266{
Andy Green0b319092013-01-19 11:17:56 +08002267 char buf[300];
2268 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002269 int n;
2270
2271 gettimeofday(&tv, NULL);
2272
2273 buf[0] = '\0';
2274 for (n = 0; n < LLL_COUNT; n++)
2275 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002276 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002277 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002278 break;
2279 }
Andy Greenb5b23192013-02-11 17:13:32 +08002280
Andy Green0b319092013-01-19 11:17:56 +08002281 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002282}
2283
Joakim Soderberg4c531232013-02-06 15:26:58 +09002284#ifdef WIN32
2285void lwsl_emit_syslog(int level, const char *line)
2286{
2287 lwsl_emit_stderr(level, line);
2288}
2289#else
Andy Greenc11db202013-01-19 11:12:16 +08002290void lwsl_emit_syslog(int level, const char *line)
2291{
2292 int syslog_level = LOG_DEBUG;
2293
2294 switch (level) {
2295 case LLL_ERR:
2296 syslog_level = LOG_ERR;
2297 break;
2298 case LLL_WARN:
2299 syslog_level = LOG_WARNING;
2300 break;
2301 case LLL_NOTICE:
2302 syslog_level = LOG_NOTICE;
2303 break;
2304 case LLL_INFO:
2305 syslog_level = LOG_INFO;
2306 break;
2307 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002308 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002309}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002310#endif
Andy Greenc11db202013-01-19 11:12:16 +08002311
Andy Green43db0452013-01-10 19:50:35 +08002312void _lws_log(int filter, const char *format, ...)
2313{
Andy Greende8f27a2013-01-12 09:17:42 +08002314 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002315 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002316
2317 if (!(log_level & filter))
2318 return;
2319
Andy Green43db0452013-01-10 19:50:35 +08002320 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002321 vsnprintf(buf, sizeof(buf), format, ap);
2322 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002323 va_end(ap);
2324
Andy Green0b319092013-01-19 11:17:56 +08002325 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002326}
2327
2328/**
2329 * lws_set_log_level() - Set the logging bitfield
2330 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002331 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2332 * function to perform log string emission instead of
2333 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002334 *
Andy Greenc6511a02013-02-19 10:01:48 +08002335 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002336 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002337 */
2338
Andy Greenb5b23192013-02-11 17:13:32 +08002339void lws_set_log_level(int level, void (*log_emit_function)(int level,
2340 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002341{
2342 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002343 if (log_emit_function)
2344 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002345}