blob: e2d7731dc4a4764b202f43d594eef9583a726d3b [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 Green9f990342011-02-12 11:57:45 +0000822 */
823
824int
Peter Hinz56885f32011-03-02 22:03:47 +0000825libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000826 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000827{
Andy Greena1ce6be2013-01-18 11:43:21 +0800828 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000829 int n;
Andy Green0d338332011-02-12 11:57:43 +0000830 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800831 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000832 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800833 int timed_out = 0;
834 int our_fd = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800835
Andy Green3182ece2013-01-20 17:08:31 +0800836#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000837 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800838#endif
Andy Green98a717c2011-03-06 13:14:15 +0000839 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800840
841 if (context->listen_service_fd)
842 listen_socket_fds_index = context->lws_lookup[
843 context->listen_service_fd]->position_in_fds_table;
844
Andy Greena71eafc2011-02-14 17:59:43 +0000845 /*
846 * you can call us with pollfd = NULL to just allow the once-per-second
847 * global timeout checks; if less than a second since the last check
848 * it returns immediately then.
849 */
850
851 gettimeofday(&tv, NULL);
852
Peter Hinz56885f32011-03-02 22:03:47 +0000853 if (context->last_timeout_check_s != tv.tv_sec) {
854 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000855
Joakim Soderberg4c531232013-02-06 15:26:58 +0900856 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800857 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800858 if (context->started_with_parent &&
859 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800860 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900861 #endif
Andy Green24cba922013-01-19 13:56:10 +0800862
Andy Greena71eafc2011-02-14 17:59:43 +0000863 /* global timeout check once per second */
864
Andy Green1c6e1422013-02-20 19:11:31 +0800865 if (pollfd)
866 our_fd = pollfd->fd;
867
Peter Hinz56885f32011-03-02 22:03:47 +0000868 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800869 m = context->fds[n].fd;
870 wsi = context->lws_lookup[m];
871 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800872 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800873
874 if (libwebsocket_service_timeout_check(context, wsi,
875 tv.tv_sec))
876 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800877 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800878 /* it was the guy we came to service! */
879 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800880 /* mark as handled */
881 pollfd->revents = 0;
882 }
Andy Greena71eafc2011-02-14 17:59:43 +0000883 }
884 }
885
Andy Green1c6e1422013-02-20 19:11:31 +0800886 /* the socket we came to service timed out, nothing to do */
887 if (timed_out)
888 return 0;
889
Andy Greena71eafc2011-02-14 17:59:43 +0000890 /* just here for timeout management? */
891
892 if (pollfd == NULL)
893 return 0;
894
895 /* no, here to service a socket descriptor */
896
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800897 wsi = context->lws_lookup[pollfd->fd];
898 if (wsi == NULL)
899 /* not lws connection ... leave revents alone and return */
900 return 0;
901
902 /*
903 * so that caller can tell we handled, past here we need to
904 * zero down pollfd->revents after handling
905 */
906
Andy Green65b0e912013-01-16 07:59:47 +0800907 /*
908 * deal with listen service piggybacking
909 * every listen_service_modulo services of other fds, we
910 * sneak one in to service the listen socket if there's anything waiting
911 *
912 * To handle connection storms, as found in ab, if we previously saw a
913 * pending connection here, it causes us to check again next time.
914 */
915
Andy Greenb5b23192013-02-11 17:13:32 +0800916 if (context->listen_service_fd && pollfd !=
917 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800918 context->listen_service_count++;
919 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800920 context->listen_service_count ==
921 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800922 context->listen_service_count = 0;
923 m = 1;
924 if (context->listen_service_extraseen > 5)
925 m = 2;
926 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800927 /*
928 * even with extpoll, we prepared this
929 * internal fds for listen
930 */
931 n = poll(&context->fds[listen_socket_fds_index],
932 1, 0);
933 if (n > 0) { /* there's a conn waiting for us */
934 libwebsocket_service_fd(context,
935 &context->
936 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800937 context->listen_service_extraseen++;
938 } else {
939 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800940 context->
941 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800942 break;
943 }
944 }
945 }
946
947 }
948
949 /* okay, what we came here to do... */
950
Andy Green0d338332011-02-12 11:57:43 +0000951 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800952
Andy Greena1ce6be2013-01-18 11:43:21 +0800953#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800954 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000955 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800956 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800957 n = lws_server_socket_service(context, wsi, pollfd);
958 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +0800959#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000960
Andy Green0d338332011-02-12 11:57:43 +0000961 case LWS_CONNMODE_WS_SERVING:
962 case LWS_CONNMODE_WS_CLIENT:
963
964 /* handle session socket closed */
965
Andy Greenc9ac31e2013-02-16 10:17:52 +0800966 if ((!pollfd->revents & POLLIN) &&
967 (pollfd->revents & (POLLERR | POLLHUP))) {
Andy Green0d338332011-02-12 11:57:43 +0000968
Andy Green43db0452013-01-10 19:50:35 +0800969 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000970 (void *)wsi, pollfd->fd);
971
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800972 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +0000973 }
974
Andy Green0d338332011-02-12 11:57:43 +0000975 /* the guy requested a callback when it was OK to write */
976
Andy Greenda527df2011-03-07 07:08:12 +0000977 if ((pollfd->revents & POLLOUT) &&
978 wsi->state == WSI_STATE_ESTABLISHED)
979 if (lws_handle_POLLOUT_event(context, wsi,
980 pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800981 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800982 goto close_and_handled;
Andy Green3b84c002011-03-06 13:14:42 +0000983 }
Andy Green0d338332011-02-12 11:57:43 +0000984
Andy Green0d338332011-02-12 11:57:43 +0000985
986 /* any incoming data ready? */
987
988 if (!(pollfd->revents & POLLIN))
989 break;
990
Andy Greenb45993c2010-12-18 15:13:50 +0000991#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800992read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800993 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800994 eff_buf.token_len = SSL_read(wsi->ssl,
995 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800996 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800997 if (!eff_buf.token_len) {
998 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800999 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001000 ERR_error_string(n,
1001 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001002 }
1003 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001004#endif
Andy Greene84652c2013-02-08 13:01:02 +08001005 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001006 context->service_buffer,
1007 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001008
Andy Green98a717c2011-03-06 13:14:15 +00001009 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001010 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +00001011 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +02001012 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001013 goto close_and_handled;
1014 n = 0;
1015 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001016 }
Andy Green98a717c2011-03-06 13:14:15 +00001017 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +08001018 lwsl_info("closing connection due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001019 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001020 }
1021
Andy Green98a717c2011-03-06 13:14:15 +00001022 /*
1023 * give any active extensions a chance to munge the buffer
1024 * before parse. We pass in a pointer to an lws_tokens struct
1025 * prepared with the default buffer and content length that's in
1026 * there. Rather than rewrite the default buffer, extensions
1027 * that expect to grow the buffer can adapt .token to
1028 * point to their own per-connection buffer in the extension
1029 * user allocation. By default with no extensions or no
1030 * extension callback handling, just the normal input buffer is
1031 * used then so it is efficient.
1032 */
Andy Greenb45993c2010-12-18 15:13:50 +00001033
Andy Greene84652c2013-02-08 13:01:02 +08001034 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001035#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001036 more = 1;
1037 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001038
Andy Green98a717c2011-03-06 13:14:15 +00001039 more = 0;
1040
1041 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001042 m = wsi->active_extensions[n]->callback(context,
1043 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001044 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001045 wsi->active_extensions_user[n],
1046 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001047 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001048 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001049 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001050 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001051 }
1052 if (m)
1053 more = 1;
1054 }
Andy Green3182ece2013-01-20 17:08:31 +08001055#endif
Andy Green98a717c2011-03-06 13:14:15 +00001056 /* service incoming data */
1057
1058 if (eff_buf.token_len) {
1059 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001060 (unsigned char *)eff_buf.token,
1061 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001062 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001063 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001064 n = 0;
1065 goto handled;
1066 }
Andy Green98a717c2011-03-06 13:14:15 +00001067 }
Andy Green3182ece2013-01-20 17:08:31 +08001068#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001069 eff_buf.token = NULL;
1070 eff_buf.token_len = 0;
1071 }
Andy Green3182ece2013-01-20 17:08:31 +08001072#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001073
1074#ifdef LWS_OPENSSL_SUPPORT
1075 if (wsi->ssl && SSL_pending(wsi->ssl))
1076 goto read_pending;
1077#endif
Andy Green98a717c2011-03-06 13:14:15 +00001078 break;
Andy Green76f61e72013-01-16 11:53:05 +08001079
1080 default:
Andy Green03674a62013-01-16 11:47:40 +08001081#ifdef LWS_NO_CLIENT
1082 break;
1083#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001084 n = lws_client_socket_service(context, wsi, pollfd);
1085 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001086#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001087 }
1088
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001089 n = 0;
1090 goto handled;
1091
1092close_and_handled:
1093 libwebsocket_close_and_free_session(
1094 context, wsi,
1095 LWS_CLOSE_STATUS_NOSTATUS);
1096 n = 0;
1097
1098handled:
1099 pollfd->revents = 0;
1100 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001101}
1102
Andy Green0d338332011-02-12 11:57:43 +00001103
Andy Green6964bb52011-01-23 16:50:33 +00001104/**
1105 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001106 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001107 *
1108 * This function closes any active connections and then frees the
1109 * context. After calling this, any further use of the context is
1110 * undefined.
1111 */
1112void
Peter Hinz56885f32011-03-02 22:03:47 +00001113libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001114{
Andy Green3182ece2013-01-20 17:08:31 +08001115#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001116 int n;
1117 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001118 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001119 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001120
Andy Greend636e352013-01-29 12:36:17 +08001121#ifdef LWS_LATENCY
1122 if (context->worst_latency_info[0])
1123 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1124#endif
1125
Andy Greendfb23042013-01-17 12:26:48 +08001126 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001127 struct libwebsocket *wsi =
1128 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001129 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001130 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1131 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001132 }
Andy Green6964bb52011-01-23 16:50:33 +00001133
Andy Greena41314f2011-05-23 10:00:03 +01001134 /*
1135 * give all extensions a chance to clean up any per-context
1136 * allocations they might have made
1137 */
1138
1139 ext = context->extensions;
1140 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1141 if (context->listen_port)
1142 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001143 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001144 ext->callback(context, ext, NULL,
1145 (enum libwebsocket_extension_callback_reasons)m,
1146 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001147 ext++;
1148 }
Andy Greena7109e62013-02-11 12:05:54 +08001149
1150 /*
1151 * inform all the protocols that they are done and will have no more
1152 * callbacks
1153 */
1154
1155 while (protocol->callback) {
1156 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1157 NULL, NULL, 0);
1158 protocol++;
1159 }
1160
Andy Green3182ece2013-01-20 17:08:31 +08001161#endif
Andy Greena41314f2011-05-23 10:00:03 +01001162
Peter Hinz56885f32011-03-02 22:03:47 +00001163#ifdef WIN32
1164#else
1165 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001166#endif
1167
Peter Hinz56885f32011-03-02 22:03:47 +00001168#ifdef LWS_OPENSSL_SUPPORT
1169 if (context->ssl_ctx)
1170 SSL_CTX_free(context->ssl_ctx);
1171 if (context->ssl_client_ctx)
1172 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001173
1174 ERR_remove_state(0);
1175 ERR_free_strings();
1176 EVP_cleanup();
1177 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001178#endif
1179
Andy Green46596482013-02-11 11:04:01 +08001180 if (context->fds)
1181 free(context->fds);
1182 if (context->lws_lookup)
1183 free(context->lws_lookup);
1184
Peter Hinz56885f32011-03-02 22:03:47 +00001185 free(context);
1186
1187#ifdef WIN32
1188 WSACleanup();
1189#endif
Andy Green6964bb52011-01-23 16:50:33 +00001190}
1191
Andy Greend88146d2013-01-22 12:40:35 +08001192/**
Andy Greenb5b23192013-02-11 17:13:32 +08001193 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001194 * @context: Websocket context
1195 *
1196 * This returns the optional user allocation that can be attached to
1197 * the context the sockets live in at context_create time. It's a way
1198 * to let all sockets serviced in the same context share data without
1199 * using globals statics in the user code.
1200 */
Alon Levy0291eb32012-10-19 11:21:56 +02001201LWS_EXTERN void *
1202libwebsocket_context_user(struct libwebsocket_context *context)
1203{
Andy Greenb5b23192013-02-11 17:13:32 +08001204 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001205}
1206
Andy Green6964bb52011-01-23 16:50:33 +00001207/**
1208 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001209 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001210 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1211 * service otherwise block and service immediately, returning
1212 * after the timeout if nothing needed service.
1213 *
1214 * This function deals with any pending websocket traffic, for three
1215 * kinds of event. It handles these events on both server and client
1216 * types of connection the same.
1217 *
1218 * 1) Accept new connections to our context's server
1219 *
Andy Green6f520a52013-01-29 17:57:39 +08001220 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001221 * server or client connections.
1222 *
1223 * You need to call this service function periodically to all the above
1224 * functions to happen; if your application is single-threaded you can
1225 * just call it in your main event loop.
1226 *
1227 * Alternatively you can fork a new process that asynchronously handles
1228 * calling this service in a loop. In that case you are happy if this
1229 * call blocks your thread until it needs to take care of something and
1230 * would call it with a large nonzero timeout. Your loop then takes no
1231 * CPU while there is nothing happening.
1232 *
1233 * If you are calling it in a single-threaded app, you don't want it to
1234 * wait around blocking other things in your loop from happening, so you
1235 * would call it with a timeout_ms of 0, so it returns immediately if
1236 * nothing is pending, or as soon as it services whatever was pending.
1237 */
1238
Andy Greene92cd172011-01-19 13:11:55 +00001239int
Peter Hinz56885f32011-03-02 22:03:47 +00001240libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001241{
1242 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001243
1244 /* stay dead once we are dead */
1245
Peter Hinz56885f32011-03-02 22:03:47 +00001246 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001247 return 1;
1248
Andy Green0d338332011-02-12 11:57:43 +00001249 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001250
Peter Hinz56885f32011-03-02 22:03:47 +00001251 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001252 if (n == 0) /* poll timeout */
1253 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001254
Andy Greendfb23042013-01-17 12:26:48 +08001255 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001256 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001257
Andy Greendfb23042013-01-17 12:26:48 +08001258 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001259
Peter Hinz56885f32011-03-02 22:03:47 +00001260 for (n = 0; n < context->fds_count; n++)
1261 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001262 if (libwebsocket_service_fd(context,
1263 &context->fds[n]) < 0)
1264 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001265 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001266}
1267
Andy Green3182ece2013-01-20 17:08:31 +08001268#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001269int
1270lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001271 struct libwebsocket *wsi,
1272 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001273 void *v, size_t len)
1274{
1275 int n;
1276 int handled = 0;
1277
1278 /* maybe an extension will take care of it for us */
1279
1280 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1281 if (!wsi->active_extensions[n]->callback)
1282 continue;
1283
1284 handled |= wsi->active_extensions[n]->callback(context,
1285 wsi->active_extensions[n], wsi,
1286 r, wsi->active_extensions_user[n], v, len);
1287 }
1288
1289 return handled;
1290}
1291
1292
1293void *
1294lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001295 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001296{
1297 int n = 0;
1298
Andy Green68b45042011-05-25 21:41:57 +01001299 if (wsi == NULL)
1300 return NULL;
1301
Andy Greena41314f2011-05-23 10:00:03 +01001302 while (n < wsi->count_active_extensions) {
1303 if (wsi->active_extensions[n] != ext) {
1304 n++;
1305 continue;
1306 }
1307 return wsi->active_extensions_user[n];
1308 }
1309
1310 return NULL;
1311}
Andy Green3182ece2013-01-20 17:08:31 +08001312#endif
Andy Greena41314f2011-05-23 10:00:03 +01001313
Andy Green90c7cbc2011-01-27 06:26:52 +00001314/**
1315 * libwebsocket_callback_on_writable() - Request a callback when this socket
1316 * becomes able to be written to without
1317 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001318 *
Peter Hinz56885f32011-03-02 22:03:47 +00001319 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001320 * @wsi: Websocket connection instance to get callback for
1321 */
1322
1323int
Peter Hinz56885f32011-03-02 22:03:47 +00001324libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001325 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001326{
Andy Green3182ece2013-01-20 17:08:31 +08001327#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001328 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001329 int handled = 0;
1330
1331 /* maybe an extension will take care of it for us */
1332
1333 for (n = 0; n < wsi->count_active_extensions; n++) {
1334 if (!wsi->active_extensions[n]->callback)
1335 continue;
1336
1337 handled |= wsi->active_extensions[n]->callback(context,
1338 wsi->active_extensions[n], wsi,
1339 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1340 wsi->active_extensions_user[n], NULL, 0);
1341 }
1342
1343 if (handled)
1344 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001345#endif
Andy Greendfb23042013-01-17 12:26:48 +08001346 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001347 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1348 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001349 return -1;
1350 }
1351
1352 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001353
Andy Green3221f922011-02-12 13:14:11 +00001354 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001355 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001356 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001357 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001358
Andy Green90c7cbc2011-01-27 06:26:52 +00001359 return 1;
1360}
1361
1362/**
1363 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1364 * all connections using the given protocol when it
1365 * becomes possible to write to each socket without
1366 * blocking in turn.
1367 *
1368 * @protocol: Protocol whose connections will get callbacks
1369 */
1370
1371int
1372libwebsocket_callback_on_writable_all_protocol(
1373 const struct libwebsocket_protocols *protocol)
1374{
Peter Hinz56885f32011-03-02 22:03:47 +00001375 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001376 int n;
Andy Green0d338332011-02-12 11:57:43 +00001377 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001378
Andy Greendfb23042013-01-17 12:26:48 +08001379 for (n = 0; n < context->fds_count; n++) {
1380 wsi = context->lws_lookup[context->fds[n].fd];
1381 if (!wsi)
1382 continue;
1383 if (wsi->protocol == protocol)
1384 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001385 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001386
1387 return 0;
1388}
1389
Andy Greenbe93fef2011-02-14 20:25:43 +00001390/**
1391 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1392 *
1393 * You will not need this unless you are doing something special
1394 *
1395 * @wsi: Websocket connection instance
1396 * @reason: timeout reason
1397 * @secs: how many seconds
1398 */
1399
1400void
1401libwebsocket_set_timeout(struct libwebsocket *wsi,
1402 enum pending_timeout reason, int secs)
1403{
1404 struct timeval tv;
1405
1406 gettimeofday(&tv, NULL);
1407
1408 wsi->pending_timeout_limit = tv.tv_sec + secs;
1409 wsi->pending_timeout = reason;
1410}
1411
Andy Greena6cbece2011-01-27 20:06:03 +00001412
1413/**
1414 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1415 *
1416 * You will not need this unless you are doing something special
1417 *
1418 * @wsi: Websocket connection instance
1419 */
1420
1421int
1422libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1423{
1424 return wsi->sock;
1425}
1426
Andy Greend636e352013-01-29 12:36:17 +08001427#ifdef LWS_LATENCY
1428void
Andy Greenb5b23192013-02-11 17:13:32 +08001429lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1430 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001431{
1432 struct timeval tv;
1433 unsigned long u;
1434 char buf[256];
1435
1436 gettimeofday(&tv, NULL);
1437
1438 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1439
1440 if (action) {
1441 if (completed) {
1442 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001443 sprintf(buf,
1444 "Completion first try lat %luus: %p: ret %d: %s\n",
1445 u - wsi->latency_start,
1446 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001447 else
Andy Greenb5b23192013-02-11 17:13:32 +08001448 sprintf(buf,
1449 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1450 u - wsi->action_start,
1451 u - wsi->latency_start,
1452 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001453 wsi->action_start = 0;
1454 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001455 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1456 u - wsi->latency_start,
1457 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001458 if (u - wsi->latency_start > context->worst_latency) {
1459 context->worst_latency = u - wsi->latency_start;
1460 strcpy(context->worst_latency_info, buf);
1461 }
1462 lwsl_latency("%s", buf);
1463 } else {
1464 wsi->latency_start = u;
1465 if (!wsi->action_start)
1466 wsi->action_start = u;
1467 }
1468}
1469#endif
1470
Andy Greena1ce6be2013-01-18 11:43:21 +08001471#ifdef LWS_NO_SERVER
1472int
1473_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1474{
1475 return 0;
1476}
1477#else
Andy Green706961d2013-01-17 16:50:35 +08001478int
1479_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1480{
1481 struct libwebsocket_context *context = wsi->protocol->owning_server;
1482 int n;
1483
Andy Green623a98d2013-01-21 11:04:23 +08001484 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001485 return 0;
1486
Andy Green623a98d2013-01-21 11:04:23 +08001487 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001488
Andy Greenb5b23192013-02-11 17:13:32 +08001489 lwsl_info("rxflow: wsi %p change_to %d\n",
1490 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001491
1492 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001493 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001494 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1495 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001496 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001497 return -1;
1498 }
1499 if (n)
1500 /* oh he stuck again, do nothing */
1501 return 0;
1502 }
1503
Andy Green623a98d2013-01-21 11:04:23 +08001504 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001505 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1506 else
1507 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1508
Andy Green623a98d2013-01-21 11:04:23 +08001509 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001510 /* external POLL support via protocol 0 */
1511 context->protocols[0].callback(context, wsi,
1512 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001513 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001514 else
1515 /* external POLL support via protocol 0 */
1516 context->protocols[0].callback(context, wsi,
1517 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001518 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001519
1520 return 1;
1521}
Andy Greena1ce6be2013-01-18 11:43:21 +08001522#endif
Andy Green706961d2013-01-17 16:50:35 +08001523
Andy Green90c7cbc2011-01-27 06:26:52 +00001524/**
1525 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1526 * receieved packets.
1527 *
1528 * If the output side of a server process becomes choked, this allows flow
1529 * control for the input side.
1530 *
1531 * @wsi: Websocket connection instance to get callback for
1532 * @enable: 0 = disable read servicing for this connection, 1 = enable
1533 */
1534
1535int
1536libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1537{
Andy Green623a98d2013-01-21 11:04:23 +08001538 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001539
Andy Green706961d2013-01-17 16:50:35 +08001540 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001541}
1542
Andy Green706961d2013-01-17 16:50:35 +08001543
Andy Green2ac5a6f2011-01-28 10:00:18 +00001544/**
1545 * libwebsocket_canonical_hostname() - returns this host's hostname
1546 *
1547 * This is typically used by client code to fill in the host parameter
1548 * when making a client connection. You can only call it after the context
1549 * has been created.
1550 *
Peter Hinz56885f32011-03-02 22:03:47 +00001551 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001552 */
1553
1554
1555extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001556libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001557{
Peter Hinz56885f32011-03-02 22:03:47 +00001558 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001559}
1560
1561
Andy Green90c7cbc2011-01-27 06:26:52 +00001562static void sigpipe_handler(int x)
1563{
1564}
1565
Andy Green6901cb32011-02-21 08:06:47 +00001566#ifdef LWS_OPENSSL_SUPPORT
1567static int
1568OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1569{
1570
1571 SSL *ssl;
1572 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001573 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001574
1575 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1576 SSL_get_ex_data_X509_STORE_CTX_idx());
1577
1578 /*
Andy Green2e24da02011-03-05 16:12:04 +00001579 * !!! nasty openssl requires the index to come as a library-scope
1580 * static
Andy Green6901cb32011-02-21 08:06:47 +00001581 */
Andy Green2e24da02011-03-05 16:12:04 +00001582 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001583
Peter Hinz56885f32011-03-02 22:03:47 +00001584 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001585 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1586 x509_ctx, ssl, preverify_ok);
1587
1588 /* convert return code from 0 = OK to 1 = OK */
1589
1590 if (!n)
1591 n = 1;
1592 else
1593 n = 0;
1594
1595 return n;
1596}
1597#endif
1598
Andy Green706961d2013-01-17 16:50:35 +08001599int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001600 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001601 struct libwebsocket *wsi,
1602 enum libwebsocket_callback_reasons reason, void *user,
1603 void *in, size_t len)
1604{
1605 int n;
1606
1607 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001608 if (!n)
1609 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001610
Andy Greenaedc9532013-02-10 21:21:24 +08001611 return n;
Andy Green706961d2013-01-17 16:50:35 +08001612}
1613
Andy Greenb45993c2010-12-18 15:13:50 +00001614
Andy Greenab990e42010-10-31 12:42:52 +00001615/**
Andy Green4739e5c2011-01-22 12:51:57 +00001616 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001617 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001618 *
Andy Green1b265272013-02-09 14:01:09 +08001619 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001620 * of all initialization in one step.
1621 *
Andy Greene92cd172011-01-19 13:11:55 +00001622 * After initialization, it returns a struct libwebsocket_context * that
1623 * represents this server. After calling, user code needs to take care
1624 * of calling libwebsocket_service() with the context pointer to get the
1625 * server's sockets serviced. This can be done in the same process context
1626 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001627 *
Andy Green8f037e42010-12-19 22:13:26 +00001628 * The protocol callback functions are called for a handful of events
1629 * including http requests coming in, websocket connections becoming
1630 * established, and data arriving; it's also called periodically to allow
1631 * async transmission.
1632 *
1633 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1634 * at that time websocket protocol has not been negotiated. Other
1635 * protocols after the first one never see any HTTP callack activity.
1636 *
1637 * The server created is a simple http server by default; part of the
1638 * websocket standard is upgrading this http connection to a websocket one.
1639 *
1640 * This allows the same server to provide files like scripts and favicon /
1641 * images or whatever over http and dynamic data over websockets all in
1642 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001643 */
Andy Green4ea60062010-10-30 12:15:07 +01001644
Andy Greene92cd172011-01-19 13:11:55 +00001645struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001646libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001647{
Peter Hinz56885f32011-03-02 22:03:47 +00001648 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001649 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001650 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001651#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001652 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001653 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001654 struct sockaddr_in serv_addr;
1655#endif
Andy Green3182ece2013-01-20 17:08:31 +08001656#ifndef LWS_NO_EXTENSIONS
1657 int m;
Andy Green1b265272013-02-09 14:01:09 +08001658 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001659#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001660
Andy Green3faa9c72010-11-08 17:03:03 +00001661#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001662 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001663#endif
1664
Joakim Soderberg4c531232013-02-06 15:26:58 +09001665#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001666 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001667#endif
1668
Andy Greenb3a614a2013-01-19 13:08:17 +08001669 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001670 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001671 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001672 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001673#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001674 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1675 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001676#else
1677 lwsl_notice(" Configured without extension support\n");
1678#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001679 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1680 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001681 if (info->ssl_cipher_list)
1682 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001683 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1684 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001685
Peter Hinz56885f32011-03-02 22:03:47 +00001686#ifdef _WIN32
1687 {
1688 WORD wVersionRequested;
1689 WSADATA wsaData;
1690 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001691 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001692
1693 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1694 wVersionRequested = MAKEWORD(2, 2);
1695
1696 err = WSAStartup(wVersionRequested, &wsaData);
1697 if (err != 0) {
1698 /* Tell the user that we could not find a usable */
1699 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001700 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001701 return NULL;
1702 }
David Galeano7b11fec2011-10-04 19:55:18 +08001703
Andy Green6ee372f2012-04-09 15:09:01 +08001704 /* default to a poll() made out of select() */
1705 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001706
Andy Green6ee372f2012-04-09 15:09:01 +08001707 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001708 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001709 if (wsdll)
1710 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001711
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001712 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001713 if (!poll)
1714 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001715 }
1716#endif
1717
Andy Greenb5b23192013-02-11 17:13:32 +08001718 context = (struct libwebsocket_context *)
1719 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001720 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001721 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001722 return NULL;
1723 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001724 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001725#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001726 context->started_with_parent = pid_daemon;
1727 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1728#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001729
Joakim Soderberg4c531232013-02-06 15:26:58 +09001730 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001731 context->protocols = info->protocols;
1732 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001733 context->http_proxy_port = 0;
1734 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001735 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001736 /* to reduce this allocation, */
1737 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001738 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1739 sizeof(struct libwebsocket_context),
1740 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1741 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001742 sizeof(struct libwebsocket_context) +
1743 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1744 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001745
Andy Greenb5b23192013-02-11 17:13:32 +08001746 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1747 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001748 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001749 lwsl_err("Unable to allocate fds array for %d connections\n",
1750 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001751 free(context);
1752 return NULL;
1753 }
Andy Greenb5b23192013-02-11 17:13:32 +08001754 context->lws_lookup = (struct libwebsocket **)
1755 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001756 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001757 lwsl_err(
1758 "Unable to allocate lws_lookup array for %d connections\n",
1759 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001760 free(context->fds);
1761 free(context);
1762 return NULL;
1763 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001764 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1765 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001766
Peter Hinz56885f32011-03-02 22:03:47 +00001767 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001768#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001769 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001770#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001771 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001772 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001773
Peter Hinz56885f32011-03-02 22:03:47 +00001774#ifdef WIN32
1775 context->fd_random = 0;
1776#else
1777 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1778 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001779 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001780 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001781 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001782 }
Peter Hinz56885f32011-03-02 22:03:47 +00001783#endif
Andy Green44eee682011-02-10 09:32:24 +00001784
Peter Hinz56885f32011-03-02 22:03:47 +00001785#ifdef LWS_OPENSSL_SUPPORT
1786 context->use_ssl = 0;
1787 context->ssl_ctx = NULL;
1788 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001789 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001790#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001791
Andy Greena1ce6be2013-01-18 11:43:21 +08001792 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001793
Andy Greena1ce6be2013-01-18 11:43:21 +08001794#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001795 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001796 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001797 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001798 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001799
Andy Greenb5b23192013-02-11 17:13:32 +08001800 lwsl_notice(" canonical_hostname = %s\n",
1801 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001802 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001803#endif
Andy Greena69f0512012-05-03 12:32:38 +08001804
Andy Green9659f372011-01-27 22:01:43 +00001805 /* split the proxy ads:port if given */
1806
1807 p = getenv("http_proxy");
1808 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001809 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001810 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001811 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001812 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001813
Peter Hinz56885f32011-03-02 22:03:47 +00001814 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001815 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001816 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001817 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001818 }
1819 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001820 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001821
Andy Greenb3a614a2013-01-19 13:08:17 +08001822 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001823 context->http_proxy_address,
1824 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001825 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001826
Andy Greena1ce6be2013-01-18 11:43:21 +08001827#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001828 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001829
Andy Green3faa9c72010-11-08 17:03:03 +00001830#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001831 context->use_ssl = info->ssl_cert_filepath != NULL &&
1832 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001833#ifdef USE_CYASSL
1834 lwsl_notice(" Compiled with CYASSL support\n");
1835#else
1836 lwsl_notice(" Compiled with OpenSSL support\n");
1837#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001838 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001839 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001840 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001841 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001842
Andy Green90c7cbc2011-01-27 06:26:52 +00001843#else
Andy Green2b40b792013-02-10 22:22:01 +08001844 if (info->ssl_cert_filepath != NULL &&
1845 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001846 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001847 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001848 }
Andy Greenb5b23192013-02-11 17:13:32 +08001849 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001850#endif
Andy Greena17c6922013-01-20 20:21:54 +08001851
Andy Greenb5b23192013-02-11 17:13:32 +08001852 lwsl_notice(
1853 " per-conn mem: %u + %u headers + protocol rx buf\n",
1854 sizeof(struct libwebsocket),
1855 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001856 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001857#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001858
1859 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001860#ifdef WIN32
1861#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001862 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001863#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001864
1865
1866#ifdef LWS_OPENSSL_SUPPORT
1867
1868 /* basic openssl init */
1869
1870 SSL_library_init();
1871
1872 OpenSSL_add_all_algorithms();
1873 SSL_load_error_strings();
1874
Andy Green2e24da02011-03-05 16:12:04 +00001875 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001876 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1877
Andy Green90c7cbc2011-01-27 06:26:52 +00001878 /*
1879 * Firefox insists on SSLv23 not SSLv3
1880 * Konq disables SSLv2 by default now, SSLv23 works
1881 */
1882
1883 method = (SSL_METHOD *)SSLv23_server_method();
1884 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001885 lwsl_err("problem creating ssl method %lu: %s\n",
1886 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001887 ERR_error_string(ERR_get_error(),
1888 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001889 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001890 }
Peter Hinz56885f32011-03-02 22:03:47 +00001891 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1892 if (!context->ssl_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001893 lwsl_err("problem creating ssl context %lu: %s\n",
1894 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001895 ERR_error_string(ERR_get_error(),
1896 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001897 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001898 }
1899
David Galeanocc148e42013-01-10 10:18:59 +08001900#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001901 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001902#endif
David Galeano77a677c2013-01-10 10:14:12 +08001903 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001904 if (info->ssl_cipher_list)
1905 SSL_CTX_set_cipher_list(context->ssl_ctx,
1906 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001907
Andy Greena1ce6be2013-01-18 11:43:21 +08001908#ifndef LWS_NO_CLIENT
1909
Andy Green90c7cbc2011-01-27 06:26:52 +00001910 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001911
Andy Green1b265272013-02-09 14:01:09 +08001912 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001913 method = (SSL_METHOD *)SSLv23_client_method();
1914 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001915 lwsl_err("problem creating ssl method %lu: %s\n",
1916 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001917 ERR_error_string(ERR_get_error(),
1918 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001919 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001920 }
1921 /* create context */
1922 context->ssl_client_ctx = SSL_CTX_new(method);
1923 if (!context->ssl_client_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001924 lwsl_err("problem creating ssl context %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;
Peter Hinz56885f32011-03-02 22:03:47 +00001929 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001930
David Galeanocc148e42013-01-10 10:18:59 +08001931#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001932 SSL_CTX_set_options(context->ssl_client_ctx,
1933 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001934#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001935 SSL_CTX_set_options(context->ssl_client_ctx,
1936 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001937 if (info->ssl_cipher_list)
1938 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1939 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001940
Peter Hinz56885f32011-03-02 22:03:47 +00001941 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001942 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001943 if (!SSL_CTX_load_verify_locations(
1944 context->ssl_client_ctx, NULL,
1945 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001946 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001947 "Unable to load SSL Client certs from %s "
1948 "(set by --with-client-cert-dir= "
1949 "in configure) -- client ssl isn't "
1950 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001951 } else
1952 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001953 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001954 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001955 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001956 "Unable to load SSL Client certs "
1957 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001958 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001959
1960 /*
1961 * callback allowing user code to load extra verification certs
1962 * helping the client to verify server identity
1963 */
1964
1965 context->protocols[0].callback(context, NULL,
1966 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1967 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001968 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001969#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001970
Andy Greenc6bf2c22011-02-20 11:10:47 +00001971 /* as a server, are we requiring clients to identify themselves? */
1972
Andy Greenb5b23192013-02-11 17:13:32 +08001973 if (info->options &
1974 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001975
1976 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001977
Peter Hinz56885f32011-03-02 22:03:47 +00001978 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001979 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1980 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001981
1982 /*
1983 * give user code a chance to load certs into the server
1984 * allowing it to verify incoming client certs
1985 */
1986
Peter Hinz56885f32011-03-02 22:03:47 +00001987 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001988 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001989 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001990 }
1991
Peter Hinz56885f32011-03-02 22:03:47 +00001992 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001993
1994 /* openssl init for server sockets */
1995
Andy Green3faa9c72010-11-08 17:03:03 +00001996 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001997 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001998 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001999 if (n != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002000 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002001 info->ssl_cert_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002002 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002003 ERR_error_string(ERR_get_error(),
2004 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002005 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002006 }
2007 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002008 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002009 info->ssl_private_key_filepath,
2010 SSL_FILETYPE_PEM) != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002011 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002012 info->ssl_private_key_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002013 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002014 ERR_error_string(ERR_get_error(),
2015 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002016 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002017 }
2018 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002019 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002020 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002021 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002022 }
2023
2024 /* SSL is happy and has a cert it's content with */
2025 }
2026#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002027
Andy Greendf736162011-01-18 15:39:02 +00002028 /* selftest */
2029
2030 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002031 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002032
Andy Greena1ce6be2013-01-18 11:43:21 +08002033#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002034 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002035
Andy Green1b265272013-02-09 14:01:09 +08002036 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002037 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002038
Andy Green4739e5c2011-01-22 12:51:57 +00002039 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2040 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002041 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002042 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002043 }
Andy Green775c0dd2010-10-29 14:15:22 +01002044
Joakim Soderberg4c531232013-02-06 15:26:58 +09002045#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002046 /*
2047 * allow us to restart even if old sockets in TIME_WAIT
2048 * (REUSEADDR on Unix means, "don't hang on to this
2049 * address after the listener is closed." On Windows, though,
2050 * it means "don't keep other processes from binding to
2051 * this address while we're using it)
2052 */
Andy Green6ee372f2012-04-09 15:09:01 +08002053 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2054 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002055#endif
Andy Green6c939552011-03-08 08:56:57 +00002056
2057 /* Disable Nagle */
2058 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002059 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2060 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002061
Joakim Soderberg4c531232013-02-06 15:26:58 +09002062 #ifdef WIN32
2063 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002064 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002065 #else
Andy Greene2160712013-01-28 12:19:10 +08002066 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002067 #endif
Andy Greene2160712013-01-28 12:19:10 +08002068
Andy Green4739e5c2011-01-22 12:51:57 +00002069 bzero((char *) &serv_addr, sizeof(serv_addr));
2070 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002071 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002072 serv_addr.sin_addr.s_addr = INADDR_ANY;
2073 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002074 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002075 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002076 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002077
2078 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2079 sizeof(serv_addr));
2080 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002081 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002082 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002083 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002084 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002085 }
Andy Green0d338332011-02-12 11:57:43 +00002086
Andy Greenb5b23192013-02-11 17:13:32 +08002087 wsi = (struct libwebsocket *)malloc(
2088 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002089 if (wsi == NULL) {
2090 lwsl_err("Out of mem\n");
2091 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002092 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002093 }
Andy Greenb5b23192013-02-11 17:13:32 +08002094 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002095 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002096#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002097 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002098#endif
Andy Green0d338332011-02-12 11:57:43 +00002099 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002100
2101 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002102
Andy Green65b0e912013-01-16 07:59:47 +08002103 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2104 context->listen_service_count = 0;
2105 context->listen_service_fd = sockfd;
2106
Andy Greena824d182013-01-15 20:52:29 +08002107 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002108 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002109 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002110#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002111
Andy Green6ee372f2012-04-09 15:09:01 +08002112 /*
2113 * drop any root privs for this process
2114 * to listen on port < 1023 we would have needed root, but now we are
2115 * listening, we don't want the power for anything else
2116 */
Peter Hinz56885f32011-03-02 22:03:47 +00002117#ifdef WIN32
2118#else
Andy Green1b265272013-02-09 14:01:09 +08002119 if (info->gid != -1)
2120 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002121 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002122 if (info->uid != -1)
2123 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002124 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002125#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002126
Andy Green6f520a52013-01-29 17:57:39 +08002127 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002128
Peter Hinz56885f32011-03-02 22:03:47 +00002129 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002130 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002131 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002132
Andy Green43db0452013-01-10 19:50:35 +08002133 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002134 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002135
Andy Green1b265272013-02-09 14:01:09 +08002136 info->protocols[context->count_protocols].owning_server =
2137 context;
2138 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002139 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002140
2141 /*
2142 * inform all the protocols that they are doing their one-time
2143 * initialization if they want to
2144 */
2145 info->protocols[context->count_protocols].callback(context,
2146 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002147 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002148
Andy Green3182ece2013-01-20 17:08:31 +08002149#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002150 /*
2151 * give all extensions a chance to create any per-context
2152 * allocations they need
2153 */
2154
2155 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002156 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002157 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002158
Andy Green1b265272013-02-09 14:01:09 +08002159 if (info->extensions) {
2160 ext = info->extensions;
2161 while (ext->callback) {
2162 lwsl_ext(" Extension: %s\n", ext->name);
2163 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002164 (enum libwebsocket_extension_callback_reasons)m,
2165 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002166 ext++;
2167 }
Andy Greena41314f2011-05-23 10:00:03 +01002168 }
Andy Green3182ece2013-01-20 17:08:31 +08002169#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002170 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002171
2172bail:
2173 libwebsocket_context_destroy(context);
2174 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002175}
Andy Greenb45993c2010-12-18 15:13:50 +00002176
Andy Greenb45993c2010-12-18 15:13:50 +00002177/**
2178 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002179 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002180 * @wsi: pointer to struct websocket you want to know the protocol of
2181 *
Andy Green8f037e42010-12-19 22:13:26 +00002182 *
Andy Green6f520a52013-01-29 17:57:39 +08002183 * Some apis can act on all live connections of a given protocol,
2184 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002185 */
Andy Greenab990e42010-10-31 12:42:52 +00002186
Andy Greenb45993c2010-12-18 15:13:50 +00002187const struct libwebsocket_protocols *
2188libwebsockets_get_protocol(struct libwebsocket *wsi)
2189{
2190 return wsi->protocol;
2191}
2192
Andy Green82c3d542011-03-07 21:16:31 +00002193int
2194libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2195{
Andy Green623a98d2013-01-21 11:04:23 +08002196 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002197}
Alex Bligh49146db2011-11-07 17:19:25 +08002198
David Galeanoe2cf9922013-01-09 18:06:55 +08002199unsigned char
2200libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2201{
Andy Green623a98d2013-01-21 11:04:23 +08002202 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002203}
2204
Andy Green2af4d5b2013-02-18 16:30:10 +08002205int
Alex Bligh49146db2011-11-07 17:19:25 +08002206libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2207{
Andy Green67d556c2013-02-15 22:31:55 +08002208 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002209 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002210
Alex Bligh49146db2011-11-07 17:19:25 +08002211 /* allocate the per-connection user memory (if any) */
2212
2213 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2214 wsi->user_space = malloc(
2215 wsi->protocol->per_session_data_size);
2216 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002217 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002218 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002219 }
Andy Green6ee372f2012-04-09 15:09:01 +08002220 memset(wsi->user_space, 0,
2221 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002222 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002223 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002224}
Andy Green43db0452013-01-10 19:50:35 +08002225
Andy Green0b319092013-01-19 11:17:56 +08002226static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002227{
Andy Green0b319092013-01-19 11:17:56 +08002228 char buf[300];
2229 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002230 int n;
2231
2232 gettimeofday(&tv, NULL);
2233
2234 buf[0] = '\0';
2235 for (n = 0; n < LLL_COUNT; n++)
2236 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002237 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002238 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002239 break;
2240 }
Andy Greenb5b23192013-02-11 17:13:32 +08002241
Andy Green0b319092013-01-19 11:17:56 +08002242 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002243}
2244
Joakim Soderberg4c531232013-02-06 15:26:58 +09002245#ifdef WIN32
2246void lwsl_emit_syslog(int level, const char *line)
2247{
2248 lwsl_emit_stderr(level, line);
2249}
2250#else
Andy Greenc11db202013-01-19 11:12:16 +08002251void lwsl_emit_syslog(int level, const char *line)
2252{
2253 int syslog_level = LOG_DEBUG;
2254
2255 switch (level) {
2256 case LLL_ERR:
2257 syslog_level = LOG_ERR;
2258 break;
2259 case LLL_WARN:
2260 syslog_level = LOG_WARNING;
2261 break;
2262 case LLL_NOTICE:
2263 syslog_level = LOG_NOTICE;
2264 break;
2265 case LLL_INFO:
2266 syslog_level = LOG_INFO;
2267 break;
2268 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002269 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002270}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002271#endif
Andy Greenc11db202013-01-19 11:12:16 +08002272
Andy Green43db0452013-01-10 19:50:35 +08002273void _lws_log(int filter, const char *format, ...)
2274{
Andy Greende8f27a2013-01-12 09:17:42 +08002275 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002276 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002277
2278 if (!(log_level & filter))
2279 return;
2280
Andy Green43db0452013-01-10 19:50:35 +08002281 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002282 vsnprintf(buf, sizeof(buf), format, ap);
2283 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002284 va_end(ap);
2285
Andy Green0b319092013-01-19 11:17:56 +08002286 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002287}
2288
2289/**
2290 * lws_set_log_level() - Set the logging bitfield
2291 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002292 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2293 * function to perform log string emission instead of
2294 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002295 *
Andy Greenc6511a02013-02-19 10:01:48 +08002296 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002297 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002298 */
2299
Andy Greenb5b23192013-02-11 17:13:32 +08002300void lws_set_log_level(int level, void (*log_emit_function)(int level,
2301 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002302{
2303 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002304 if (log_emit_function)
2305 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002306}