blob: bcde2acb4341c2ecff410524e7b68820a705f35a [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Peter Hinz56885f32011-03-02 22:03:47 +000024#ifdef WIN32
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010027#include <mstcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000028#else
Davidc4ef7b12013-01-12 20:39:47 +080029#ifdef LWS_BUILTIN_GETIFADDRS
30#include <getifaddrs.h>
31#else
Peter Hinz56885f32011-03-02 22:03:47 +000032#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080033#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090034#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000035#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080036#include <sys/socket.h>
37#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000038#endif
Andy Green2e24da02011-03-05 16:12:04 +000039
40#ifdef LWS_OPENSSL_SUPPORT
41int openssl_websocket_private_data_index;
42#endif
43
Andy Greenaa6fc442012-04-12 13:26:49 +080044#ifdef __MINGW32__
45#include "../win32port/win32helpers/websock-w32.c"
46#else
47#ifdef __MINGW64__
48#include "../win32port/win32helpers/websock-w32.c"
49#endif
50#endif
51
Andy Green7b405452013-02-01 10:50:15 +080052#ifndef LWS_BUILD_HASH
53#define LWS_BUILD_HASH "unknown-build-hash"
54#endif
Andy Green3182ece2013-01-20 17:08:31 +080055
Andy Green7c19c342013-01-19 12:18:07 +080056static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080057static void lwsl_emit_stderr(int level, const char *line);
58static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
59
Andy Green7b405452013-02-01 10:50:15 +080060static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
61
Andy Greenb5b23192013-02-11 17:13:32 +080062static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080063 "ERR",
64 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080065 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080066 "INFO",
67 "DEBUG",
68 "PARSER",
69 "HEADER",
70 "EXTENSION",
71 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080072 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080073};
74
Andy Greenb5b23192013-02-11 17:13:32 +080075#ifndef LWS_NO_CLIENT
76 extern int lws_client_socket_service(
77 struct libwebsocket_context *context,
78 struct libwebsocket *wsi, struct pollfd *pollfd);
79#endif
80#ifndef LWS_NO_SERVER
81 extern int lws_server_socket_service(
82 struct libwebsocket_context *context,
83 struct libwebsocket *wsi, struct pollfd *pollfd);
84#endif
85
Andy Green7b405452013-02-01 10:50:15 +080086/**
87 * lws_get_library_version: get version and git hash library built from
88 *
89 * returns a const char * to a string like "1.1 178d78c"
90 * representing the library version followed by the git head hash it
91 * was built from
92 */
93
94const char *
95lws_get_library_version(void)
96{
97 return library_version;
98}
99
Andy Green0d338332011-02-12 11:57:43 +0000100int
Andy Greenb5b23192013-02-11 17:13:32 +0800101insert_wsi_socket_into_fds(struct libwebsocket_context *context,
102 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000103{
Andy Greendfb23042013-01-17 12:26:48 +0800104 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800105 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000106 return 1;
107 }
108
Andy Greendfb23042013-01-17 12:26:48 +0800109 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800110 lwsl_err("Socket fd %d is too high (%d)\n",
111 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800112 return 1;
113 }
114
115 assert(wsi);
116 assert(wsi->sock);
117
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
119 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800120
121 context->lws_lookup[wsi->sock] = wsi;
122 wsi->position_in_fds_table = context->fds_count;
123 context->fds[context->fds_count].fd = wsi->sock;
124 context->fds[context->fds_count].events = POLLIN;
125 context->fds[context->fds_count++].revents = 0;
126
127 /* external POLL support via protocol 0 */
128 context->protocols[0].callback(context, wsi,
129 LWS_CALLBACK_ADD_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800130 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000131
132 return 0;
133}
134
Andy Greendfb23042013-01-17 12:26:48 +0800135static int
Andy Greenb5b23192013-02-11 17:13:32 +0800136remove_wsi_socket_from_fds(struct libwebsocket_context *context,
137 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000138{
Andy Greendfb23042013-01-17 12:26:48 +0800139 int m;
Andy Green0d338332011-02-12 11:57:43 +0000140
Andy Greendfb23042013-01-17 12:26:48 +0800141 if (!--context->fds_count)
142 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000143
Andy Greendfb23042013-01-17 12:26:48 +0800144 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800145 lwsl_err("Socket fd %d too high (%d)\n",
146 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800147 return 1;
148 }
Andy Green0d338332011-02-12 11:57:43 +0000149
Andy Greenb5b23192013-02-11 17:13:32 +0800150 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
151 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800152
153 m = wsi->position_in_fds_table; /* replace the contents for this */
154
155 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800156 context->fds[m] = context->fds[context->fds_count];
157 /*
158 * end guy's fds_lookup entry remains unchanged
159 * (still same fd pointing to same wsi)
160 */
Andy Greendfb23042013-01-17 12:26:48 +0800161 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800162 context->lws_lookup[context->fds[context->fds_count].fd]->
163 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800164 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800165 context->lws_lookup[wsi->sock] = NULL;
166 /* removed wsi has no position any more */
167 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800168
169do_ext:
170 /* remove also from external POLL support via protocol 0 */
171 if (wsi->sock)
172 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800173 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
174 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800175
176 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000177}
178
Andy Green32375b72011-02-19 08:32:53 +0000179
Andy Green8f037e42010-12-19 22:13:26 +0000180void
Peter Hinz56885f32011-03-02 22:03:47 +0000181libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000182 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000183{
Andy Greenb45993c2010-12-18 15:13:50 +0000184 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000185 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000186 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
187 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800188#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000189 int ret;
190 int m;
191 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100192 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800193#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000194
Andy Green4b6fbe12011-02-14 08:03:48 +0000195 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000196 return;
197
Andy Green62c54d22011-02-14 09:14:25 +0000198 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000199
Andy Green62c54d22011-02-14 09:14:25 +0000200 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000201 return;
202
Andy Green22524a62013-02-15 22:48:58 +0800203 /* we tried the polite way... */
204 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
205 goto just_kill_connection;
206
Andy Green623a98d2013-01-21 11:04:23 +0800207 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000208
Andy Greenb8b247d2013-01-22 07:20:08 +0800209 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
Andy Green25eddab2013-03-09 12:54:14 +0800210 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
Andy Greenb8b247d2013-01-22 07:20:08 +0800211 close(wsi->u.http.fd);
212 wsi->u.http.fd = 0;
213 }
214
Andy Green3182ece2013-01-20 17:08:31 +0800215#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000216 /*
Andy Green68b45042011-05-25 21:41:57 +0100217 * are his extensions okay with him closing? Eg he might be a mux
218 * parent and just his ch1 aspect is closing?
219 */
220
Andy Green68b45042011-05-25 21:41:57 +0100221 for (n = 0; n < wsi->count_active_extensions; n++) {
222 if (!wsi->active_extensions[n]->callback)
223 continue;
224
225 m = wsi->active_extensions[n]->callback(context,
226 wsi->active_extensions[n], wsi,
227 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
228 wsi->active_extensions_user[n], NULL, 0);
229
230 /*
231 * if somebody vetoed actually closing him at this time....
232 * up to the extension to track the attempted close, let's
233 * just bail
234 */
235
236 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800237 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100238 return;
239 }
240 }
241
Andy Green68b45042011-05-25 21:41:57 +0100242 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000243 * flush any tx pending from extensions, since we may send close packet
244 * if there are problems with send, just nuke the connection
245 */
246
247 ret = 1;
248 while (ret == 1) {
249
250 /* default to nobody has more to spill */
251
252 ret = 0;
253 eff_buf.token = NULL;
254 eff_buf.token_len = 0;
255
256 /* show every extension the new incoming data */
257
258 for (n = 0; n < wsi->count_active_extensions; n++) {
259 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000260 wsi->protocol->owning_server,
261 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000262 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
263 wsi->active_extensions_user[n], &eff_buf, 0);
264 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800265 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000266 goto just_kill_connection;
267 }
268 if (m)
269 /*
270 * at least one extension told us he has more
271 * to spill, so we will go around again after
272 */
273 ret = 1;
274 }
275
276 /* assuming they left us something to send, send it */
277
278 if (eff_buf.token_len)
279 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800280 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800281 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000282 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800283 }
Andy Greenc44159f2011-03-07 07:08:18 +0000284 }
Andy Green3182ece2013-01-20 17:08:31 +0800285#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000286
287 /*
Andy Greenda527df2011-03-07 07:08:12 +0000288 * signal we are closing, libsocket_write will
289 * add any necessary version-specific stuff. If the write fails,
290 * no worries we are closing anyway. If we didn't initiate this
291 * close, then our state has been changed to
292 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
293 *
294 * Likewise if it's a second call to close this connection after we
295 * sent the close indication to the peer already, we are in state
296 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
297 */
298
299 if (old_state == WSI_STATE_ESTABLISHED &&
300 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100301
Andy Green43db0452013-01-10 19:50:35 +0800302 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100303
Andy Greenfdd305a2013-02-11 14:32:48 +0800304 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800305 memset(buf, 0, sizeof(buf));
306 n = libwebsocket_write(wsi,
307 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000308 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800309 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000310 /*
311 * we have sent a nice protocol level indication we
312 * now wish to close, we should not send anything more
313 */
314
315 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
316
Andy Greenb5b23192013-02-11 17:13:32 +0800317 /*
318 * ...and we should wait for a reply for a bit
319 * out of politeness
320 */
Andy Greenda527df2011-03-07 07:08:12 +0000321
322 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800323 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000324
Andy Green43db0452013-01-10 19:50:35 +0800325 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000326
327 return;
328 }
329
Andy Greenb5b23192013-02-11 17:13:32 +0800330 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800331
Andy Greenda527df2011-03-07 07:08:12 +0000332 /* else, the send failed and we should just hang up */
333 }
334
Andy Greenc44159f2011-03-07 07:08:18 +0000335just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100336
Andy Greenb5b23192013-02-11 17:13:32 +0800337 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100338
Andy Greenda527df2011-03-07 07:08:12 +0000339 /*
340 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800341 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000342 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000343
Andy Greendfb23042013-01-17 12:26:48 +0800344 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000345
Andy Green251f6fa2010-11-03 11:13:06 +0000346 wsi->state = WSI_STATE_DEAD_SOCKET;
347
Andy Greenb5b23192013-02-11 17:13:32 +0800348 if ((old_state == WSI_STATE_ESTABLISHED ||
349 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800350 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
351
352 if (wsi->u.ws.rx_user_buffer) {
353 free(wsi->u.ws.rx_user_buffer);
354 wsi->u.ws.rx_user_buffer = NULL;
355 }
356 if (wsi->u.ws.rxflow_buffer) {
357 free(wsi->u.ws.rxflow_buffer);
358 wsi->u.ws.rxflow_buffer = NULL;
359 }
Andy Green54495112013-02-06 21:10:16 +0900360 }
361
Andy Green4b6fbe12011-02-14 08:03:48 +0000362 /* tell the user it's all over for this guy */
363
Andy Greend4302732011-02-28 07:45:29 +0000364 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800365 ((old_state == WSI_STATE_ESTABLISHED) ||
366 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
367 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800368 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000369 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000370 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800371 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800372 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000373
Andy Green3182ece2013-01-20 17:08:31 +0800374#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000375 /* deallocate any active extension contexts */
376
377 for (n = 0; n < wsi->count_active_extensions; n++) {
378 if (!wsi->active_extensions[n]->callback)
379 continue;
380
Andy Green46c2ea02011-03-22 09:04:01 +0000381 wsi->active_extensions[n]->callback(context,
382 wsi->active_extensions[n], wsi,
383 LWS_EXT_CALLBACK_DESTROY,
384 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000385
386 free(wsi->active_extensions_user[n]);
387 }
388
Andy Greena41314f2011-05-23 10:00:03 +0100389 /*
390 * inform all extensions in case they tracked this guy out of band
391 * even though not active on him specifically
392 */
393
394 ext = context->extensions;
395 while (ext && ext->callback) {
396 ext->callback(context, ext, wsi,
397 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
398 NULL, NULL, 0);
399 ext++;
400 }
Andy Green3182ece2013-01-20 17:08:31 +0800401#endif
Andy Greena41314f2011-05-23 10:00:03 +0100402
Andy Green43db0452013-01-10 19:50:35 +0800403/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000404
Andy Green3faa9c72010-11-08 17:03:03 +0000405#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000406 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000407 n = SSL_get_fd(wsi->ssl);
408 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800409 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000410 SSL_free(wsi->ssl);
411 } else {
412#endif
Andy Green0303db42013-01-17 14:46:43 +0800413 if (wsi->sock) {
414 n = shutdown(wsi->sock, SHUT_RDWR);
415 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800416 lwsl_debug("closing: shutdown returned %d\n",
417 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800418
Andy Green0303db42013-01-17 14:46:43 +0800419 n = compatible_close(wsi->sock);
420 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800421 lwsl_debug("closing: close returned %d\n",
422 errno);
Andy Green0303db42013-01-17 14:46:43 +0800423 }
Andy Green3faa9c72010-11-08 17:03:03 +0000424#ifdef LWS_OPENSSL_SUPPORT
425 }
426#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800427 if (wsi->protocol && wsi->protocol->per_session_data_size &&
428 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000429 free(wsi->user_space);
430
Andy Green251f6fa2010-11-03 11:13:06 +0000431 free(wsi);
432}
433
Andy Green07034092011-02-13 08:37:12 +0000434/**
435 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800436 * @context: Libwebsockets context
437 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000438 * @fd: Connection socket descriptor
439 * @name: Buffer to take client address name
440 * @name_len: Length of client address name buffer
441 * @rip: Buffer to take client address IP qotted quad
442 * @rip_len: Length of client address IP buffer
443 *
444 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800445 * the client connected with socket descriptor @fd. Names may be
446 * truncated if there is not enough room. If either cannot be
447 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000448 */
449
450void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800451libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
452 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000453 char *rip, int rip_len)
454{
455 unsigned int len;
456 struct sockaddr_in sin;
457 struct hostent *host;
458 struct hostent *host1;
459 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000460 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000461 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800462 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800463#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800464 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800465#endif
Andy Green07034092011-02-13 08:37:12 +0000466
467 rip[0] = '\0';
468 name[0] = '\0';
469
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800470 lws_latency_pre(context, wsi);
471
Andy Greenb5b23192013-02-11 17:13:32 +0800472 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000473 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
474 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800475 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000476 }
Andy Green6ee372f2012-04-09 15:09:01 +0800477
Andy Greenb5b23192013-02-11 17:13:32 +0800478 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000479 AF_INET);
480 if (host == NULL) {
481 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800482 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000483 }
484
485 strncpy(name, host->h_name, name_len);
486 name[name_len - 1] = '\0';
487
488 host1 = gethostbyname(host->h_name);
489 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800490 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000491 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000492 n = 0;
493 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000494 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000495 if (p == NULL)
496 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000497 if ((host1->h_addrtype != AF_INET)
498#ifdef AF_LOCAL
499 && (host1->h_addrtype != AF_LOCAL)
500#endif
501 )
Andy Green07034092011-02-13 08:37:12 +0000502 continue;
503
Andy Green7627af52011-03-09 15:13:52 +0000504 if (host1->h_addrtype == AF_INET)
505 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000506#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000507 else {
508 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800509 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000510 ip[sizeof(ip) - 1] = '\0';
511 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000512#endif
Andy Green07034092011-02-13 08:37:12 +0000513 p = NULL;
514 strncpy(rip, ip, rip_len);
515 rip[rip_len - 1] = '\0';
516 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800517
518 ret = 0;
519bail:
520 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000521}
Andy Green9f990342011-02-12 11:57:45 +0000522
Peter Hinz56885f32011-03-02 22:03:47 +0000523int libwebsockets_get_random(struct libwebsocket_context *context,
524 void *buf, int len)
525{
526 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800527 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000528
529#ifdef WIN32
530 for (n = 0; n < len; n++)
531 p[n] = (unsigned char)rand();
532#else
533 n = read(context->fd_random, p, len);
534#endif
535
536 return n;
537}
538
Andy Greena690cd02013-02-09 12:25:31 +0800539int lws_set_socket_options(struct libwebsocket_context *context, int fd)
540{
541 int optval = 1;
542 socklen_t optlen = sizeof(optval);
543#ifdef WIN32
544 unsigned long optl = 0;
545#endif
546#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
547 struct protoent *tcp_proto;
548#endif
549
550 if (context->ka_time) {
551 /* enable keepalive on this socket */
552 optval = 1;
553 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
554 (const void *)&optval, optlen) < 0)
555 return 1;
556
Andy Greena47865f2013-02-10 09:39:47 +0800557#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
558
559 /*
560 * didn't find a way to set these per-socket, need to
561 * tune kernel systemwide values
562 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100563#elif WIN32
564 {
565 DWORD dwBytesRet;
566 struct tcp_keepalive alive;
567 alive.onoff = TRUE;
568 alive.keepalivetime = context->ka_time;
569 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800570
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100571 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
572 NULL, 0, &dwBytesRet, NULL, NULL))
573 return 1;
574 }
Andy Greena47865f2013-02-10 09:39:47 +0800575#else
Andy Greena690cd02013-02-09 12:25:31 +0800576 /* set the keepalive conditions we want on it too */
577 optval = context->ka_time;
578 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
579 (const void *)&optval, optlen) < 0)
580 return 1;
581
Larry Hayesbb66ac62013-02-22 09:16:20 +0800582 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800583 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
584 (const void *)&optval, optlen) < 0)
585 return 1;
586
Larry Hayesbb66ac62013-02-22 09:16:20 +0800587 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800588 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
589 (const void *)&optval, optlen) < 0)
590 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800591#endif
Andy Greena690cd02013-02-09 12:25:31 +0800592 }
593
594 /* Disable Nagle */
595 optval = 1;
596#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
597 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
598#else
599 tcp_proto = getprotobyname("TCP");
600 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
601#endif
602
603 /* We are nonblocking... */
604#ifdef WIN32
605 ioctlsocket(fd, FIONBIO, &optl);
606#else
607 fcntl(fd, F_SETFL, O_NONBLOCK);
608#endif
609
610 return 0;
611}
612
Andy Green95a7b5d2011-03-06 10:29:39 +0000613int lws_send_pipe_choked(struct libwebsocket *wsi)
614{
615 struct pollfd fds;
616
617 fds.fd = wsi->sock;
618 fds.events = POLLOUT;
619 fds.revents = 0;
620
621 if (poll(&fds, 1, 0) != 1)
622 return 1;
623
624 if ((fds.revents & POLLOUT) == 0)
625 return 1;
626
627 /* okay to send another packet without blocking */
628
629 return 0;
630}
631
Andy Greena41314f2011-05-23 10:00:03 +0100632int
Andy Green3b84c002011-03-06 13:14:42 +0000633lws_handle_POLLOUT_event(struct libwebsocket_context *context,
634 struct libwebsocket *wsi, struct pollfd *pollfd)
635{
Andy Green3b84c002011-03-06 13:14:42 +0000636 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800637
Andy Green3182ece2013-01-20 17:08:31 +0800638#ifndef LWS_NO_EXTENSIONS
639 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000640 int ret;
641 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100642 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000643
Andy Greena41314f2011-05-23 10:00:03 +0100644 for (n = 0; n < wsi->count_active_extensions; n++) {
645 if (!wsi->active_extensions[n]->callback)
646 continue;
647
648 m = wsi->active_extensions[n]->callback(context,
649 wsi->active_extensions[n], wsi,
650 LWS_EXT_CALLBACK_IS_WRITEABLE,
651 wsi->active_extensions_user[n], NULL, 0);
652 if (m > handled)
653 handled = m;
654 }
655
656 if (handled == 1)
657 goto notify_action;
658
659 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000660 goto user_service;
661
662 /*
663 * check in on the active extensions, see if they
664 * had pending stuff to spill... they need to get the
665 * first look-in otherwise sequence will be disordered
666 *
667 * NULL, zero-length eff_buf means just spill pending
668 */
669
670 ret = 1;
671 while (ret == 1) {
672
673 /* default to nobody has more to spill */
674
675 ret = 0;
676 eff_buf.token = NULL;
677 eff_buf.token_len = 0;
678
679 /* give every extension a chance to spill */
680
681 for (n = 0; n < wsi->count_active_extensions; n++) {
682 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000683 wsi->protocol->owning_server,
684 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000685 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
686 wsi->active_extensions_user[n], &eff_buf, 0);
687 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800688 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000689 return -1;
690 }
691 if (m)
692 /*
693 * at least one extension told us he has more
694 * to spill, so we will go around again after
695 */
696 ret = 1;
697 }
698
699 /* assuming they gave us something to send, send it */
700
701 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800702 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
703 eff_buf.token_len);
704 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000705 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800706 /*
707 * Keep amount spilled small to minimize chance of this
708 */
709 if (n != eff_buf.token_len) {
710 lwsl_err("Unable to spill ext %d vs %s\n",
711 eff_buf.token_len, n);
712 return -1;
713 }
Andy Green3b84c002011-03-06 13:14:42 +0000714 } else
715 continue;
716
717 /* no extension has more to spill */
718
719 if (!ret)
720 continue;
721
722 /*
723 * There's more to spill from an extension, but we just sent
724 * something... did that leave the pipe choked?
725 */
726
727 if (!lws_send_pipe_choked(wsi))
728 /* no we could add more */
729 continue;
730
Andy Green43db0452013-01-10 19:50:35 +0800731 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000732
733 /*
734 * Yes, he's choked. Leave the POLLOUT masked on so we will
735 * come back here when he is unchoked. Don't call the user
736 * callback to enforce ordering of spilling, he'll get called
737 * when we come back here and there's nothing more to spill.
738 */
739
740 return 0;
741 }
742
743 wsi->extension_data_pending = 0;
744
745user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800746#endif
Andy Green3b84c002011-03-06 13:14:42 +0000747 /* one shot */
748
Andy Greena41314f2011-05-23 10:00:03 +0100749 if (pollfd) {
750 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000751
Andy Greena41314f2011-05-23 10:00:03 +0100752 /* external POLL support via protocol 0 */
753 context->protocols[0].callback(context, wsi,
754 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800755 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100756 }
Andy Green3182ece2013-01-20 17:08:31 +0800757#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100758notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800759#endif
Andy Green3b84c002011-03-06 13:14:42 +0000760
Andy Green9e4c2b62011-03-07 20:47:39 +0000761 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
762 n = LWS_CALLBACK_CLIENT_WRITEABLE;
763 else
764 n = LWS_CALLBACK_SERVER_WRITEABLE;
765
Andy Greenb8b247d2013-01-22 07:20:08 +0800766 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800767 wsi, (enum libwebsocket_callback_reasons) n,
768 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000769}
770
771
772
Andy Green1c6e1422013-02-20 19:11:31 +0800773int
Andy Greena41314f2011-05-23 10:00:03 +0100774libwebsocket_service_timeout_check(struct libwebsocket_context *context,
775 struct libwebsocket *wsi, unsigned int sec)
776{
Andy Green3182ece2013-01-20 17:08:31 +0800777#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100778 int n;
779
780 /*
781 * if extensions want in on it (eg, we are a mux parent)
782 * give them a chance to service child timeouts
783 */
784
785 for (n = 0; n < wsi->count_active_extensions; n++)
786 wsi->active_extensions[n]->callback(
787 context, wsi->active_extensions[n],
788 wsi, LWS_EXT_CALLBACK_1HZ,
789 wsi->active_extensions_user[n], NULL, sec);
790
Andy Green3182ece2013-01-20 17:08:31 +0800791#endif
Andy Greena41314f2011-05-23 10:00:03 +0100792 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800793 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800794
Andy Greena41314f2011-05-23 10:00:03 +0100795 /*
796 * if we went beyond the allowed time, kill the
797 * connection
798 */
799
800 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800801 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100802 libwebsocket_close_and_free_session(context,
803 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800804 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100805 }
Andy Green1c6e1422013-02-20 19:11:31 +0800806
807 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100808}
809
Andy Green9f990342011-02-12 11:57:45 +0000810/**
811 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000812 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000813 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800814 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000815 *
Andy Green75006172013-01-22 12:32:11 +0800816 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800817 * services it according to the state of the associated
818 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800819 *
820 * The one call deals with all "service" that might happen on a socket
821 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800822 *
823 * If a pollfd says it has something, you can just pass it to
824 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
825 * If it sees it is a lws socket, the traffic will be handled and
826 * pollfd->revents will be zeroed now.
827 *
828 * If the socket is foreign to lws, it leaves revents alone. So you can
829 * see if you should service yourself by checking the pollfd revents
830 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000831 */
832
833int
Peter Hinz56885f32011-03-02 22:03:47 +0000834libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000835 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000836{
Andy Greena1ce6be2013-01-18 11:43:21 +0800837 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000838 int n;
Andy Green0d338332011-02-12 11:57:43 +0000839 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800840 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000841 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800842 int timed_out = 0;
843 int our_fd = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800844
Andy Green3182ece2013-01-20 17:08:31 +0800845#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000846 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800847#endif
Andy Green98a717c2011-03-06 13:14:15 +0000848 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800849
850 if (context->listen_service_fd)
851 listen_socket_fds_index = context->lws_lookup[
852 context->listen_service_fd]->position_in_fds_table;
853
Andy Greena71eafc2011-02-14 17:59:43 +0000854 /*
855 * you can call us with pollfd = NULL to just allow the once-per-second
856 * global timeout checks; if less than a second since the last check
857 * it returns immediately then.
858 */
859
860 gettimeofday(&tv, NULL);
861
Peter Hinz56885f32011-03-02 22:03:47 +0000862 if (context->last_timeout_check_s != tv.tv_sec) {
863 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000864
Joakim Soderberg4c531232013-02-06 15:26:58 +0900865 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800866 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800867 if (context->started_with_parent &&
868 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800869 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900870 #endif
Andy Green24cba922013-01-19 13:56:10 +0800871
Andy Greena71eafc2011-02-14 17:59:43 +0000872 /* global timeout check once per second */
873
Andy Green1c6e1422013-02-20 19:11:31 +0800874 if (pollfd)
875 our_fd = pollfd->fd;
876
Peter Hinz56885f32011-03-02 22:03:47 +0000877 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800878 m = context->fds[n].fd;
879 wsi = context->lws_lookup[m];
880 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800881 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800882
883 if (libwebsocket_service_timeout_check(context, wsi,
884 tv.tv_sec))
885 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800886 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800887 /* it was the guy we came to service! */
888 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800889 /* mark as handled */
890 pollfd->revents = 0;
891 }
Andy Greena71eafc2011-02-14 17:59:43 +0000892 }
893 }
894
Andy Green1c6e1422013-02-20 19:11:31 +0800895 /* the socket we came to service timed out, nothing to do */
896 if (timed_out)
897 return 0;
898
Andy Greena71eafc2011-02-14 17:59:43 +0000899 /* just here for timeout management? */
900
901 if (pollfd == NULL)
902 return 0;
903
904 /* no, here to service a socket descriptor */
905
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800906 wsi = context->lws_lookup[pollfd->fd];
907 if (wsi == NULL)
908 /* not lws connection ... leave revents alone and return */
909 return 0;
910
911 /*
912 * so that caller can tell we handled, past here we need to
913 * zero down pollfd->revents after handling
914 */
915
Andy Green65b0e912013-01-16 07:59:47 +0800916 /*
917 * deal with listen service piggybacking
918 * every listen_service_modulo services of other fds, we
919 * sneak one in to service the listen socket if there's anything waiting
920 *
921 * To handle connection storms, as found in ab, if we previously saw a
922 * pending connection here, it causes us to check again next time.
923 */
924
Andy Greenb5b23192013-02-11 17:13:32 +0800925 if (context->listen_service_fd && pollfd !=
926 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800927 context->listen_service_count++;
928 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800929 context->listen_service_count ==
930 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800931 context->listen_service_count = 0;
932 m = 1;
933 if (context->listen_service_extraseen > 5)
934 m = 2;
935 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800936 /*
937 * even with extpoll, we prepared this
938 * internal fds for listen
939 */
940 n = poll(&context->fds[listen_socket_fds_index],
941 1, 0);
942 if (n > 0) { /* there's a conn waiting for us */
943 libwebsocket_service_fd(context,
944 &context->
945 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800946 context->listen_service_extraseen++;
947 } else {
948 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800949 context->
950 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800951 break;
952 }
953 }
954 }
955
956 }
957
958 /* okay, what we came here to do... */
959
Andy Green0d338332011-02-12 11:57:43 +0000960 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800961
Andy Greena1ce6be2013-01-18 11:43:21 +0800962#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800963 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000964 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800965 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800966 n = lws_server_socket_service(context, wsi, pollfd);
967 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +0800968#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000969
Andy Green0d338332011-02-12 11:57:43 +0000970 case LWS_CONNMODE_WS_SERVING:
971 case LWS_CONNMODE_WS_CLIENT:
972
973 /* handle session socket closed */
974
Andy Greenc9ac31e2013-02-16 10:17:52 +0800975 if ((!pollfd->revents & POLLIN) &&
976 (pollfd->revents & (POLLERR | POLLHUP))) {
Andy Green0d338332011-02-12 11:57:43 +0000977
Andy Green43db0452013-01-10 19:50:35 +0800978 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000979 (void *)wsi, pollfd->fd);
980
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800981 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +0000982 }
983
Andy Green0d338332011-02-12 11:57:43 +0000984 /* the guy requested a callback when it was OK to write */
985
Andy Greenda527df2011-03-07 07:08:12 +0000986 if ((pollfd->revents & POLLOUT) &&
987 wsi->state == WSI_STATE_ESTABLISHED)
988 if (lws_handle_POLLOUT_event(context, wsi,
989 pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800990 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800991 goto close_and_handled;
Andy Green3b84c002011-03-06 13:14:42 +0000992 }
Andy Green0d338332011-02-12 11:57:43 +0000993
Andy Green0d338332011-02-12 11:57:43 +0000994
995 /* any incoming data ready? */
996
997 if (!(pollfd->revents & POLLIN))
998 break;
999
Andy Greenb45993c2010-12-18 15:13:50 +00001000#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001001read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001002 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001003 eff_buf.token_len = SSL_read(wsi->ssl,
1004 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001005 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001006 if (!eff_buf.token_len) {
1007 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001008 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001009 ERR_error_string(n,
1010 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001011 }
1012 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001013#endif
Andy Greene84652c2013-02-08 13:01:02 +08001014 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001015 context->service_buffer,
1016 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001017
Andy Green98a717c2011-03-06 13:14:15 +00001018 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001019 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +00001020 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +02001021 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001022 goto close_and_handled;
1023 n = 0;
1024 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001025 }
Andy Green98a717c2011-03-06 13:14:15 +00001026 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +08001027 lwsl_info("closing connection due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001028 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001029 }
1030
Andy Green98a717c2011-03-06 13:14:15 +00001031 /*
1032 * give any active extensions a chance to munge the buffer
1033 * before parse. We pass in a pointer to an lws_tokens struct
1034 * prepared with the default buffer and content length that's in
1035 * there. Rather than rewrite the default buffer, extensions
1036 * that expect to grow the buffer can adapt .token to
1037 * point to their own per-connection buffer in the extension
1038 * user allocation. By default with no extensions or no
1039 * extension callback handling, just the normal input buffer is
1040 * used then so it is efficient.
1041 */
Andy Greenb45993c2010-12-18 15:13:50 +00001042
Andy Greene84652c2013-02-08 13:01:02 +08001043 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001044#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001045 more = 1;
1046 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001047
Andy Green98a717c2011-03-06 13:14:15 +00001048 more = 0;
1049
1050 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001051 m = wsi->active_extensions[n]->callback(context,
1052 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001053 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001054 wsi->active_extensions_user[n],
1055 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001056 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001057 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001058 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001059 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001060 }
1061 if (m)
1062 more = 1;
1063 }
Andy Green3182ece2013-01-20 17:08:31 +08001064#endif
Andy Green98a717c2011-03-06 13:14:15 +00001065 /* service incoming data */
1066
1067 if (eff_buf.token_len) {
1068 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001069 (unsigned char *)eff_buf.token,
1070 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001071 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001072 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001073 n = 0;
1074 goto handled;
1075 }
Andy Green98a717c2011-03-06 13:14:15 +00001076 }
Andy Green3182ece2013-01-20 17:08:31 +08001077#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001078 eff_buf.token = NULL;
1079 eff_buf.token_len = 0;
1080 }
Andy Green3182ece2013-01-20 17:08:31 +08001081#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001082
1083#ifdef LWS_OPENSSL_SUPPORT
1084 if (wsi->ssl && SSL_pending(wsi->ssl))
1085 goto read_pending;
1086#endif
Andy Green98a717c2011-03-06 13:14:15 +00001087 break;
Andy Green76f61e72013-01-16 11:53:05 +08001088
1089 default:
Andy Green03674a62013-01-16 11:47:40 +08001090#ifdef LWS_NO_CLIENT
1091 break;
1092#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001093 n = lws_client_socket_service(context, wsi, pollfd);
1094 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001095#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001096 }
1097
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001098 n = 0;
1099 goto handled;
1100
1101close_and_handled:
1102 libwebsocket_close_and_free_session(
1103 context, wsi,
1104 LWS_CLOSE_STATUS_NOSTATUS);
1105 n = 0;
1106
1107handled:
1108 pollfd->revents = 0;
1109 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001110}
1111
Andy Green0d338332011-02-12 11:57:43 +00001112
Andy Green6964bb52011-01-23 16:50:33 +00001113/**
1114 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001115 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001116 *
1117 * This function closes any active connections and then frees the
1118 * context. After calling this, any further use of the context is
1119 * undefined.
1120 */
1121void
Peter Hinz56885f32011-03-02 22:03:47 +00001122libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001123{
Andy Green3182ece2013-01-20 17:08:31 +08001124#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001125 int n;
1126 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001127 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001128 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001129
Andy Greend636e352013-01-29 12:36:17 +08001130#ifdef LWS_LATENCY
1131 if (context->worst_latency_info[0])
1132 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1133#endif
1134
Andy Greendfb23042013-01-17 12:26:48 +08001135 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001136 struct libwebsocket *wsi =
1137 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001138 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001139 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1140 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001141 }
Andy Green6964bb52011-01-23 16:50:33 +00001142
Andy Greena41314f2011-05-23 10:00:03 +01001143 /*
1144 * give all extensions a chance to clean up any per-context
1145 * allocations they might have made
1146 */
1147
1148 ext = context->extensions;
1149 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1150 if (context->listen_port)
1151 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001152 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001153 ext->callback(context, ext, NULL,
1154 (enum libwebsocket_extension_callback_reasons)m,
1155 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001156 ext++;
1157 }
Andy Greena7109e62013-02-11 12:05:54 +08001158
1159 /*
1160 * inform all the protocols that they are done and will have no more
1161 * callbacks
1162 */
1163
1164 while (protocol->callback) {
1165 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1166 NULL, NULL, 0);
1167 protocol++;
1168 }
1169
Andy Green3182ece2013-01-20 17:08:31 +08001170#endif
Andy Greena41314f2011-05-23 10:00:03 +01001171
Peter Hinz56885f32011-03-02 22:03:47 +00001172#ifdef WIN32
1173#else
1174 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001175#endif
1176
Peter Hinz56885f32011-03-02 22:03:47 +00001177#ifdef LWS_OPENSSL_SUPPORT
1178 if (context->ssl_ctx)
1179 SSL_CTX_free(context->ssl_ctx);
1180 if (context->ssl_client_ctx)
1181 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001182
1183 ERR_remove_state(0);
1184 ERR_free_strings();
1185 EVP_cleanup();
1186 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001187#endif
1188
Andy Green46596482013-02-11 11:04:01 +08001189 if (context->fds)
1190 free(context->fds);
1191 if (context->lws_lookup)
1192 free(context->lws_lookup);
1193
Peter Hinz56885f32011-03-02 22:03:47 +00001194 free(context);
1195
1196#ifdef WIN32
1197 WSACleanup();
1198#endif
Andy Green6964bb52011-01-23 16:50:33 +00001199}
1200
Andy Greend88146d2013-01-22 12:40:35 +08001201/**
Andy Greenb5b23192013-02-11 17:13:32 +08001202 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001203 * @context: Websocket context
1204 *
1205 * This returns the optional user allocation that can be attached to
1206 * the context the sockets live in at context_create time. It's a way
1207 * to let all sockets serviced in the same context share data without
1208 * using globals statics in the user code.
1209 */
Alon Levy0291eb32012-10-19 11:21:56 +02001210LWS_EXTERN void *
1211libwebsocket_context_user(struct libwebsocket_context *context)
1212{
Andy Greenb5b23192013-02-11 17:13:32 +08001213 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001214}
1215
Andy Green6964bb52011-01-23 16:50:33 +00001216/**
1217 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001218 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001219 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1220 * service otherwise block and service immediately, returning
1221 * after the timeout if nothing needed service.
1222 *
1223 * This function deals with any pending websocket traffic, for three
1224 * kinds of event. It handles these events on both server and client
1225 * types of connection the same.
1226 *
1227 * 1) Accept new connections to our context's server
1228 *
Andy Green6f520a52013-01-29 17:57:39 +08001229 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001230 * server or client connections.
1231 *
1232 * You need to call this service function periodically to all the above
1233 * functions to happen; if your application is single-threaded you can
1234 * just call it in your main event loop.
1235 *
1236 * Alternatively you can fork a new process that asynchronously handles
1237 * calling this service in a loop. In that case you are happy if this
1238 * call blocks your thread until it needs to take care of something and
1239 * would call it with a large nonzero timeout. Your loop then takes no
1240 * CPU while there is nothing happening.
1241 *
1242 * If you are calling it in a single-threaded app, you don't want it to
1243 * wait around blocking other things in your loop from happening, so you
1244 * would call it with a timeout_ms of 0, so it returns immediately if
1245 * nothing is pending, or as soon as it services whatever was pending.
1246 */
1247
Andy Greene92cd172011-01-19 13:11:55 +00001248int
Peter Hinz56885f32011-03-02 22:03:47 +00001249libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001250{
1251 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001252
1253 /* stay dead once we are dead */
1254
Peter Hinz56885f32011-03-02 22:03:47 +00001255 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001256 return 1;
1257
Andy Green0d338332011-02-12 11:57:43 +00001258 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001259
Peter Hinz56885f32011-03-02 22:03:47 +00001260 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001261 if (n == 0) /* poll timeout */
1262 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001263
Andy Greendfb23042013-01-17 12:26:48 +08001264 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001265 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001266
Andy Greendfb23042013-01-17 12:26:48 +08001267 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001268
Peter Hinz56885f32011-03-02 22:03:47 +00001269 for (n = 0; n < context->fds_count; n++)
1270 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001271 if (libwebsocket_service_fd(context,
1272 &context->fds[n]) < 0)
1273 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001274 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001275}
1276
Andy Green3182ece2013-01-20 17:08:31 +08001277#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001278int
1279lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001280 struct libwebsocket *wsi,
1281 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001282 void *v, size_t len)
1283{
1284 int n;
1285 int handled = 0;
1286
1287 /* maybe an extension will take care of it for us */
1288
1289 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1290 if (!wsi->active_extensions[n]->callback)
1291 continue;
1292
1293 handled |= wsi->active_extensions[n]->callback(context,
1294 wsi->active_extensions[n], wsi,
1295 r, wsi->active_extensions_user[n], v, len);
1296 }
1297
1298 return handled;
1299}
1300
1301
1302void *
1303lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001304 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001305{
1306 int n = 0;
1307
Andy Green68b45042011-05-25 21:41:57 +01001308 if (wsi == NULL)
1309 return NULL;
1310
Andy Greena41314f2011-05-23 10:00:03 +01001311 while (n < wsi->count_active_extensions) {
1312 if (wsi->active_extensions[n] != ext) {
1313 n++;
1314 continue;
1315 }
1316 return wsi->active_extensions_user[n];
1317 }
1318
1319 return NULL;
1320}
Andy Green3182ece2013-01-20 17:08:31 +08001321#endif
Andy Greena41314f2011-05-23 10:00:03 +01001322
Andy Green90c7cbc2011-01-27 06:26:52 +00001323/**
1324 * libwebsocket_callback_on_writable() - Request a callback when this socket
1325 * becomes able to be written to without
1326 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001327 *
Peter Hinz56885f32011-03-02 22:03:47 +00001328 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001329 * @wsi: Websocket connection instance to get callback for
1330 */
1331
1332int
Peter Hinz56885f32011-03-02 22:03:47 +00001333libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001334 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001335{
Andy Green3182ece2013-01-20 17:08:31 +08001336#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001337 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001338 int handled = 0;
1339
1340 /* maybe an extension will take care of it for us */
1341
1342 for (n = 0; n < wsi->count_active_extensions; n++) {
1343 if (!wsi->active_extensions[n]->callback)
1344 continue;
1345
1346 handled |= wsi->active_extensions[n]->callback(context,
1347 wsi->active_extensions[n], wsi,
1348 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1349 wsi->active_extensions_user[n], NULL, 0);
1350 }
1351
1352 if (handled)
1353 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001354#endif
Andy Greendfb23042013-01-17 12:26:48 +08001355 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001356 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1357 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001358 return -1;
1359 }
1360
1361 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001362
Andy Green3221f922011-02-12 13:14:11 +00001363 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001364 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001365 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001366 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001367
Andy Green90c7cbc2011-01-27 06:26:52 +00001368 return 1;
1369}
1370
1371/**
1372 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1373 * all connections using the given protocol when it
1374 * becomes possible to write to each socket without
1375 * blocking in turn.
1376 *
1377 * @protocol: Protocol whose connections will get callbacks
1378 */
1379
1380int
1381libwebsocket_callback_on_writable_all_protocol(
1382 const struct libwebsocket_protocols *protocol)
1383{
Peter Hinz56885f32011-03-02 22:03:47 +00001384 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001385 int n;
Andy Green0d338332011-02-12 11:57:43 +00001386 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001387
Andy Greendfb23042013-01-17 12:26:48 +08001388 for (n = 0; n < context->fds_count; n++) {
1389 wsi = context->lws_lookup[context->fds[n].fd];
1390 if (!wsi)
1391 continue;
1392 if (wsi->protocol == protocol)
1393 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001394 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001395
1396 return 0;
1397}
1398
Andy Greenbe93fef2011-02-14 20:25:43 +00001399/**
1400 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1401 *
1402 * You will not need this unless you are doing something special
1403 *
1404 * @wsi: Websocket connection instance
1405 * @reason: timeout reason
1406 * @secs: how many seconds
1407 */
1408
1409void
1410libwebsocket_set_timeout(struct libwebsocket *wsi,
1411 enum pending_timeout reason, int secs)
1412{
1413 struct timeval tv;
1414
1415 gettimeofday(&tv, NULL);
1416
1417 wsi->pending_timeout_limit = tv.tv_sec + secs;
1418 wsi->pending_timeout = reason;
1419}
1420
Andy Greena6cbece2011-01-27 20:06:03 +00001421
1422/**
1423 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1424 *
1425 * You will not need this unless you are doing something special
1426 *
1427 * @wsi: Websocket connection instance
1428 */
1429
1430int
1431libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1432{
1433 return wsi->sock;
1434}
1435
Andy Greend636e352013-01-29 12:36:17 +08001436#ifdef LWS_LATENCY
1437void
Andy Greenb5b23192013-02-11 17:13:32 +08001438lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1439 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001440{
1441 struct timeval tv;
1442 unsigned long u;
1443 char buf[256];
1444
1445 gettimeofday(&tv, NULL);
1446
1447 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1448
1449 if (action) {
1450 if (completed) {
1451 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001452 sprintf(buf,
1453 "Completion first try lat %luus: %p: ret %d: %s\n",
1454 u - wsi->latency_start,
1455 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001456 else
Andy Greenb5b23192013-02-11 17:13:32 +08001457 sprintf(buf,
1458 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1459 u - wsi->action_start,
1460 u - wsi->latency_start,
1461 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001462 wsi->action_start = 0;
1463 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001464 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1465 u - wsi->latency_start,
1466 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001467 if (u - wsi->latency_start > context->worst_latency) {
1468 context->worst_latency = u - wsi->latency_start;
1469 strcpy(context->worst_latency_info, buf);
1470 }
1471 lwsl_latency("%s", buf);
1472 } else {
1473 wsi->latency_start = u;
1474 if (!wsi->action_start)
1475 wsi->action_start = u;
1476 }
1477}
1478#endif
1479
Andy Greena1ce6be2013-01-18 11:43:21 +08001480#ifdef LWS_NO_SERVER
1481int
1482_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1483{
1484 return 0;
1485}
1486#else
Andy Green706961d2013-01-17 16:50:35 +08001487int
1488_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1489{
1490 struct libwebsocket_context *context = wsi->protocol->owning_server;
1491 int n;
1492
Andy Green623a98d2013-01-21 11:04:23 +08001493 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001494 return 0;
1495
Andy Green623a98d2013-01-21 11:04:23 +08001496 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001497
Andy Greenb5b23192013-02-11 17:13:32 +08001498 lwsl_info("rxflow: wsi %p change_to %d\n",
1499 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001500
1501 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001502 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001503 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1504 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001505 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001506 return -1;
1507 }
1508 if (n)
1509 /* oh he stuck again, do nothing */
1510 return 0;
1511 }
1512
Andy Green623a98d2013-01-21 11:04:23 +08001513 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001514 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1515 else
1516 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1517
Andy Green623a98d2013-01-21 11:04:23 +08001518 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001519 /* external POLL support via protocol 0 */
1520 context->protocols[0].callback(context, wsi,
1521 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001522 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001523 else
1524 /* external POLL support via protocol 0 */
1525 context->protocols[0].callback(context, wsi,
1526 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001527 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001528
1529 return 1;
1530}
Andy Greena1ce6be2013-01-18 11:43:21 +08001531#endif
Andy Green706961d2013-01-17 16:50:35 +08001532
Andy Green90c7cbc2011-01-27 06:26:52 +00001533/**
1534 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1535 * receieved packets.
1536 *
1537 * If the output side of a server process becomes choked, this allows flow
1538 * control for the input side.
1539 *
1540 * @wsi: Websocket connection instance to get callback for
1541 * @enable: 0 = disable read servicing for this connection, 1 = enable
1542 */
1543
1544int
1545libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1546{
Andy Green623a98d2013-01-21 11:04:23 +08001547 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001548
Andy Green706961d2013-01-17 16:50:35 +08001549 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001550}
1551
Andy Green706961d2013-01-17 16:50:35 +08001552
Andy Green2ac5a6f2011-01-28 10:00:18 +00001553/**
1554 * libwebsocket_canonical_hostname() - returns this host's hostname
1555 *
1556 * This is typically used by client code to fill in the host parameter
1557 * when making a client connection. You can only call it after the context
1558 * has been created.
1559 *
Peter Hinz56885f32011-03-02 22:03:47 +00001560 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001561 */
1562
1563
1564extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001565libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001566{
Peter Hinz56885f32011-03-02 22:03:47 +00001567 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001568}
1569
1570
Andy Green90c7cbc2011-01-27 06:26:52 +00001571static void sigpipe_handler(int x)
1572{
1573}
1574
Andy Green6901cb32011-02-21 08:06:47 +00001575#ifdef LWS_OPENSSL_SUPPORT
1576static int
1577OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1578{
1579
1580 SSL *ssl;
1581 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001582 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001583
1584 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1585 SSL_get_ex_data_X509_STORE_CTX_idx());
1586
1587 /*
Andy Green2e24da02011-03-05 16:12:04 +00001588 * !!! nasty openssl requires the index to come as a library-scope
1589 * static
Andy Green6901cb32011-02-21 08:06:47 +00001590 */
Andy Green2e24da02011-03-05 16:12:04 +00001591 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001592
Peter Hinz56885f32011-03-02 22:03:47 +00001593 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001594 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1595 x509_ctx, ssl, preverify_ok);
1596
1597 /* convert return code from 0 = OK to 1 = OK */
1598
1599 if (!n)
1600 n = 1;
1601 else
1602 n = 0;
1603
1604 return n;
1605}
1606#endif
1607
Andy Green706961d2013-01-17 16:50:35 +08001608int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001609 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001610 struct libwebsocket *wsi,
1611 enum libwebsocket_callback_reasons reason, void *user,
1612 void *in, size_t len)
1613{
1614 int n;
1615
1616 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001617 if (!n)
1618 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001619
Andy Greenaedc9532013-02-10 21:21:24 +08001620 return n;
Andy Green706961d2013-01-17 16:50:35 +08001621}
1622
Andy Greenb45993c2010-12-18 15:13:50 +00001623
Andy Greenab990e42010-10-31 12:42:52 +00001624/**
Andy Green4739e5c2011-01-22 12:51:57 +00001625 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001626 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001627 *
Andy Green1b265272013-02-09 14:01:09 +08001628 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001629 * of all initialization in one step.
1630 *
Andy Greene92cd172011-01-19 13:11:55 +00001631 * After initialization, it returns a struct libwebsocket_context * that
1632 * represents this server. After calling, user code needs to take care
1633 * of calling libwebsocket_service() with the context pointer to get the
1634 * server's sockets serviced. This can be done in the same process context
1635 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001636 *
Andy Green8f037e42010-12-19 22:13:26 +00001637 * The protocol callback functions are called for a handful of events
1638 * including http requests coming in, websocket connections becoming
1639 * established, and data arriving; it's also called periodically to allow
1640 * async transmission.
1641 *
1642 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1643 * at that time websocket protocol has not been negotiated. Other
1644 * protocols after the first one never see any HTTP callack activity.
1645 *
1646 * The server created is a simple http server by default; part of the
1647 * websocket standard is upgrading this http connection to a websocket one.
1648 *
1649 * This allows the same server to provide files like scripts and favicon /
1650 * images or whatever over http and dynamic data over websockets all in
1651 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001652 */
Andy Green4ea60062010-10-30 12:15:07 +01001653
Andy Greene92cd172011-01-19 13:11:55 +00001654struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001655libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001656{
Peter Hinz56885f32011-03-02 22:03:47 +00001657 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001658 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001659 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001660#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001661 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001662 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001663 struct sockaddr_in serv_addr;
1664#endif
Andy Green3182ece2013-01-20 17:08:31 +08001665#ifndef LWS_NO_EXTENSIONS
1666 int m;
Andy Green1b265272013-02-09 14:01:09 +08001667 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001668#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001669
Andy Green3faa9c72010-11-08 17:03:03 +00001670#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001671 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001672#endif
1673
Joakim Soderberg4c531232013-02-06 15:26:58 +09001674#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001675 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001676#endif
1677
Andy Greenb3a614a2013-01-19 13:08:17 +08001678 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001679 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001680 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001681 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001682#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001683 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1684 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001685#else
1686 lwsl_notice(" Configured without extension support\n");
1687#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001688 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1689 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001690 if (info->ssl_cipher_list)
1691 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001692 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1693 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001694
Peter Hinz56885f32011-03-02 22:03:47 +00001695#ifdef _WIN32
1696 {
1697 WORD wVersionRequested;
1698 WSADATA wsaData;
1699 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001700 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001701
1702 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1703 wVersionRequested = MAKEWORD(2, 2);
1704
1705 err = WSAStartup(wVersionRequested, &wsaData);
1706 if (err != 0) {
1707 /* Tell the user that we could not find a usable */
1708 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001709 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001710 return NULL;
1711 }
David Galeano7b11fec2011-10-04 19:55:18 +08001712
Andy Green6ee372f2012-04-09 15:09:01 +08001713 /* default to a poll() made out of select() */
1714 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001715
Andy Green6ee372f2012-04-09 15:09:01 +08001716 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001717 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001718 if (wsdll)
1719 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001720
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001721 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001722 if (!poll)
1723 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001724 }
1725#endif
1726
Andy Greenb5b23192013-02-11 17:13:32 +08001727 context = (struct libwebsocket_context *)
1728 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001729 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001730 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001731 return NULL;
1732 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001733 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001734#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001735 context->started_with_parent = pid_daemon;
1736 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1737#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001738
Joakim Soderberg4c531232013-02-06 15:26:58 +09001739 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001740 context->protocols = info->protocols;
1741 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001742 context->http_proxy_port = 0;
1743 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001744 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001745 /* to reduce this allocation, */
1746 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001747 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1748 sizeof(struct libwebsocket_context),
1749 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1750 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001751 sizeof(struct libwebsocket_context) +
1752 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1753 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001754
Andy Greenb5b23192013-02-11 17:13:32 +08001755 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1756 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001757 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001758 lwsl_err("Unable to allocate fds array for %d connections\n",
1759 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001760 free(context);
1761 return NULL;
1762 }
Andy Greenb5b23192013-02-11 17:13:32 +08001763 context->lws_lookup = (struct libwebsocket **)
1764 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001765 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001766 lwsl_err(
1767 "Unable to allocate lws_lookup array for %d connections\n",
1768 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001769 free(context->fds);
1770 free(context);
1771 return NULL;
1772 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001773 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1774 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001775
Peter Hinz56885f32011-03-02 22:03:47 +00001776 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001777#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001778 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001779#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001780 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001781 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001782
Peter Hinz56885f32011-03-02 22:03:47 +00001783#ifdef WIN32
1784 context->fd_random = 0;
1785#else
1786 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1787 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001788 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001789 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001790 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001791 }
Peter Hinz56885f32011-03-02 22:03:47 +00001792#endif
Andy Green44eee682011-02-10 09:32:24 +00001793
Peter Hinz56885f32011-03-02 22:03:47 +00001794#ifdef LWS_OPENSSL_SUPPORT
1795 context->use_ssl = 0;
1796 context->ssl_ctx = NULL;
1797 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001798 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001799#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001800
Andy Greena1ce6be2013-01-18 11:43:21 +08001801 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001802
Andy Greena1ce6be2013-01-18 11:43:21 +08001803#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001804 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001805 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001806 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001807 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001808
Andy Greenb5b23192013-02-11 17:13:32 +08001809 lwsl_notice(" canonical_hostname = %s\n",
1810 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001811 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001812#endif
Andy Greena69f0512012-05-03 12:32:38 +08001813
Andy Green9659f372011-01-27 22:01:43 +00001814 /* split the proxy ads:port if given */
1815
1816 p = getenv("http_proxy");
1817 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001818 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001819 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001820 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001821 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001822
Peter Hinz56885f32011-03-02 22:03:47 +00001823 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001824 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001825 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001826 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001827 }
1828 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001829 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001830
Andy Greenb3a614a2013-01-19 13:08:17 +08001831 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001832 context->http_proxy_address,
1833 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001834 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001835
Andy Greena1ce6be2013-01-18 11:43:21 +08001836#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001837 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001838
Andy Green3faa9c72010-11-08 17:03:03 +00001839#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001840 context->use_ssl = info->ssl_cert_filepath != NULL &&
1841 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001842#ifdef USE_CYASSL
1843 lwsl_notice(" Compiled with CYASSL support\n");
1844#else
1845 lwsl_notice(" Compiled with OpenSSL support\n");
1846#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001847 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001848 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001849 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001850 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001851
Andy Green90c7cbc2011-01-27 06:26:52 +00001852#else
Andy Green2b40b792013-02-10 22:22:01 +08001853 if (info->ssl_cert_filepath != NULL &&
1854 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001855 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001856 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001857 }
Andy Greenb5b23192013-02-11 17:13:32 +08001858 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001859#endif
Andy Greena17c6922013-01-20 20:21:54 +08001860
Andy Greenb5b23192013-02-11 17:13:32 +08001861 lwsl_notice(
1862 " per-conn mem: %u + %u headers + protocol rx buf\n",
1863 sizeof(struct libwebsocket),
1864 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001865 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001866#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001867
1868 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001869#ifdef WIN32
1870#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001871 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001872#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001873
1874
1875#ifdef LWS_OPENSSL_SUPPORT
1876
1877 /* basic openssl init */
1878
1879 SSL_library_init();
1880
1881 OpenSSL_add_all_algorithms();
1882 SSL_load_error_strings();
1883
Andy Green2e24da02011-03-05 16:12:04 +00001884 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001885 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1886
Andy Green90c7cbc2011-01-27 06:26:52 +00001887 /*
1888 * Firefox insists on SSLv23 not SSLv3
1889 * Konq disables SSLv2 by default now, SSLv23 works
1890 */
1891
1892 method = (SSL_METHOD *)SSLv23_server_method();
1893 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001894 lwsl_err("problem creating ssl method %lu: %s\n",
1895 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001896 ERR_error_string(ERR_get_error(),
1897 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001898 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001899 }
Peter Hinz56885f32011-03-02 22:03:47 +00001900 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1901 if (!context->ssl_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001902 lwsl_err("problem creating ssl context %lu: %s\n",
1903 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001904 ERR_error_string(ERR_get_error(),
1905 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001906 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001907 }
1908
David Galeanocc148e42013-01-10 10:18:59 +08001909#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001910 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001911#endif
David Galeano77a677c2013-01-10 10:14:12 +08001912 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001913 if (info->ssl_cipher_list)
1914 SSL_CTX_set_cipher_list(context->ssl_ctx,
1915 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001916
Andy Greena1ce6be2013-01-18 11:43:21 +08001917#ifndef LWS_NO_CLIENT
1918
Andy Green90c7cbc2011-01-27 06:26:52 +00001919 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001920
Andy Green1b265272013-02-09 14:01:09 +08001921 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001922 method = (SSL_METHOD *)SSLv23_client_method();
1923 if (!method) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001924 lwsl_err("problem creating ssl method %lu: %s\n",
1925 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001926 ERR_error_string(ERR_get_error(),
1927 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001928 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001929 }
1930 /* create context */
1931 context->ssl_client_ctx = SSL_CTX_new(method);
1932 if (!context->ssl_client_ctx) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08001933 lwsl_err("problem creating ssl context %lu: %s\n",
1934 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08001935 ERR_error_string(ERR_get_error(),
1936 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001937 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001938 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001939
David Galeanocc148e42013-01-10 10:18:59 +08001940#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001941 SSL_CTX_set_options(context->ssl_client_ctx,
1942 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001943#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001944 SSL_CTX_set_options(context->ssl_client_ctx,
1945 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08001946 if (info->ssl_cipher_list)
1947 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1948 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08001949
Peter Hinz56885f32011-03-02 22:03:47 +00001950 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001951 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001952 if (!SSL_CTX_load_verify_locations(
1953 context->ssl_client_ctx, NULL,
1954 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001955 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001956 "Unable to load SSL Client certs from %s "
1957 "(set by --with-client-cert-dir= "
1958 "in configure) -- client ssl isn't "
1959 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001960 } else
1961 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001962 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001963 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001964 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001965 "Unable to load SSL Client certs "
1966 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001967 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001968
1969 /*
1970 * callback allowing user code to load extra verification certs
1971 * helping the client to verify server identity
1972 */
1973
1974 context->protocols[0].callback(context, NULL,
1975 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1976 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001977 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001978#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001979
Andy Greenc6bf2c22011-02-20 11:10:47 +00001980 /* as a server, are we requiring clients to identify themselves? */
1981
Andy Greenb5b23192013-02-11 17:13:32 +08001982 if (info->options &
1983 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001984
1985 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001986
Peter Hinz56885f32011-03-02 22:03:47 +00001987 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001988 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1989 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001990
1991 /*
1992 * give user code a chance to load certs into the server
1993 * allowing it to verify incoming client certs
1994 */
1995
Peter Hinz56885f32011-03-02 22:03:47 +00001996 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001997 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001998 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001999 }
2000
Peter Hinz56885f32011-03-02 22:03:47 +00002001 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002002
2003 /* openssl init for server sockets */
2004
Andy Green3faa9c72010-11-08 17:03:03 +00002005 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002006 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002007 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002008 if (n != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002009 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002010 info->ssl_cert_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002011 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002012 ERR_error_string(ERR_get_error(),
2013 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002014 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002015 }
2016 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002017 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002018 info->ssl_private_key_filepath,
2019 SSL_FILETYPE_PEM) != 1) {
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002020 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002021 info->ssl_private_key_filepath,
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002022 ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +08002023 ERR_error_string(ERR_get_error(),
2024 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002025 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002026 }
2027 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002028 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002029 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002030 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002031 }
2032
2033 /* SSL is happy and has a cert it's content with */
2034 }
2035#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002036
Andy Greendf736162011-01-18 15:39:02 +00002037 /* selftest */
2038
2039 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002040 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002041
Andy Greena1ce6be2013-01-18 11:43:21 +08002042#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002043 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002044
Andy Green1b265272013-02-09 14:01:09 +08002045 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002046 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002047
Andy Green4739e5c2011-01-22 12:51:57 +00002048 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2049 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002050 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002051 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002052 }
Andy Green775c0dd2010-10-29 14:15:22 +01002053
Joakim Soderberg4c531232013-02-06 15:26:58 +09002054#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002055 /*
2056 * allow us to restart even if old sockets in TIME_WAIT
2057 * (REUSEADDR on Unix means, "don't hang on to this
2058 * address after the listener is closed." On Windows, though,
2059 * it means "don't keep other processes from binding to
2060 * this address while we're using it)
2061 */
Andy Green6ee372f2012-04-09 15:09:01 +08002062 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2063 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002064#endif
Andy Green6c939552011-03-08 08:56:57 +00002065
2066 /* Disable Nagle */
2067 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002068 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2069 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002070
Joakim Soderberg4c531232013-02-06 15:26:58 +09002071 #ifdef WIN32
2072 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002073 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002074 #else
Andy Greene2160712013-01-28 12:19:10 +08002075 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002076 #endif
Andy Greene2160712013-01-28 12:19:10 +08002077
Andy Green4739e5c2011-01-22 12:51:57 +00002078 bzero((char *) &serv_addr, sizeof(serv_addr));
2079 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002080 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002081 serv_addr.sin_addr.s_addr = INADDR_ANY;
2082 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002083 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002084 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002085 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002086
2087 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2088 sizeof(serv_addr));
2089 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002090 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002091 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002092 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002093 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002094 }
Andy Green0d338332011-02-12 11:57:43 +00002095
Andy Greenb5b23192013-02-11 17:13:32 +08002096 wsi = (struct libwebsocket *)malloc(
2097 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002098 if (wsi == NULL) {
2099 lwsl_err("Out of mem\n");
2100 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002101 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002102 }
Andy Greenb5b23192013-02-11 17:13:32 +08002103 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002104 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002105#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002106 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002107#endif
Andy Green0d338332011-02-12 11:57:43 +00002108 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002109
2110 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002111
Andy Green65b0e912013-01-16 07:59:47 +08002112 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2113 context->listen_service_count = 0;
2114 context->listen_service_fd = sockfd;
2115
Andy Greena824d182013-01-15 20:52:29 +08002116 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002117 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002118 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002119#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002120
Andy Green6ee372f2012-04-09 15:09:01 +08002121 /*
2122 * drop any root privs for this process
2123 * to listen on port < 1023 we would have needed root, but now we are
2124 * listening, we don't want the power for anything else
2125 */
Peter Hinz56885f32011-03-02 22:03:47 +00002126#ifdef WIN32
2127#else
Andy Green1b265272013-02-09 14:01:09 +08002128 if (info->gid != -1)
2129 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002130 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002131 if (info->uid != -1)
2132 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002133 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002134#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002135
Andy Green6f520a52013-01-29 17:57:39 +08002136 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002137
Peter Hinz56885f32011-03-02 22:03:47 +00002138 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002139 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002140 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002141
Andy Green43db0452013-01-10 19:50:35 +08002142 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002143 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002144
Andy Green1b265272013-02-09 14:01:09 +08002145 info->protocols[context->count_protocols].owning_server =
2146 context;
2147 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002148 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002149
2150 /*
2151 * inform all the protocols that they are doing their one-time
2152 * initialization if they want to
2153 */
2154 info->protocols[context->count_protocols].callback(context,
2155 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002156 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002157
Andy Green3182ece2013-01-20 17:08:31 +08002158#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002159 /*
2160 * give all extensions a chance to create any per-context
2161 * allocations they need
2162 */
2163
2164 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002165 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002166 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002167
Andy Green1b265272013-02-09 14:01:09 +08002168 if (info->extensions) {
2169 ext = info->extensions;
2170 while (ext->callback) {
2171 lwsl_ext(" Extension: %s\n", ext->name);
2172 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002173 (enum libwebsocket_extension_callback_reasons)m,
2174 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002175 ext++;
2176 }
Andy Greena41314f2011-05-23 10:00:03 +01002177 }
Andy Green3182ece2013-01-20 17:08:31 +08002178#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002179 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002180
2181bail:
2182 libwebsocket_context_destroy(context);
2183 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002184}
Andy Greenb45993c2010-12-18 15:13:50 +00002185
Andy Greenb45993c2010-12-18 15:13:50 +00002186/**
2187 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002188 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002189 * @wsi: pointer to struct websocket you want to know the protocol of
2190 *
Andy Green8f037e42010-12-19 22:13:26 +00002191 *
Andy Green6f520a52013-01-29 17:57:39 +08002192 * Some apis can act on all live connections of a given protocol,
2193 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002194 */
Andy Greenab990e42010-10-31 12:42:52 +00002195
Andy Greenb45993c2010-12-18 15:13:50 +00002196const struct libwebsocket_protocols *
2197libwebsockets_get_protocol(struct libwebsocket *wsi)
2198{
2199 return wsi->protocol;
2200}
2201
Andy Green82c3d542011-03-07 21:16:31 +00002202int
2203libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2204{
Andy Green623a98d2013-01-21 11:04:23 +08002205 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002206}
Alex Bligh49146db2011-11-07 17:19:25 +08002207
David Galeanoe2cf9922013-01-09 18:06:55 +08002208unsigned char
2209libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2210{
Andy Green623a98d2013-01-21 11:04:23 +08002211 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002212}
2213
Andy Green2af4d5b2013-02-18 16:30:10 +08002214int
Alex Bligh49146db2011-11-07 17:19:25 +08002215libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2216{
Andy Green67d556c2013-02-15 22:31:55 +08002217 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002218 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002219
Alex Bligh49146db2011-11-07 17:19:25 +08002220 /* allocate the per-connection user memory (if any) */
2221
2222 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2223 wsi->user_space = malloc(
2224 wsi->protocol->per_session_data_size);
2225 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002226 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002227 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002228 }
Andy Green6ee372f2012-04-09 15:09:01 +08002229 memset(wsi->user_space, 0,
2230 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002231 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002232 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002233}
Andy Green43db0452013-01-10 19:50:35 +08002234
Andy Green0b319092013-01-19 11:17:56 +08002235static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002236{
Andy Green0b319092013-01-19 11:17:56 +08002237 char buf[300];
2238 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002239 int n;
2240
2241 gettimeofday(&tv, NULL);
2242
2243 buf[0] = '\0';
2244 for (n = 0; n < LLL_COUNT; n++)
2245 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002246 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002247 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002248 break;
2249 }
Andy Greenb5b23192013-02-11 17:13:32 +08002250
Andy Green0b319092013-01-19 11:17:56 +08002251 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002252}
2253
Joakim Soderberg4c531232013-02-06 15:26:58 +09002254#ifdef WIN32
2255void lwsl_emit_syslog(int level, const char *line)
2256{
2257 lwsl_emit_stderr(level, line);
2258}
2259#else
Andy Greenc11db202013-01-19 11:12:16 +08002260void lwsl_emit_syslog(int level, const char *line)
2261{
2262 int syslog_level = LOG_DEBUG;
2263
2264 switch (level) {
2265 case LLL_ERR:
2266 syslog_level = LOG_ERR;
2267 break;
2268 case LLL_WARN:
2269 syslog_level = LOG_WARNING;
2270 break;
2271 case LLL_NOTICE:
2272 syslog_level = LOG_NOTICE;
2273 break;
2274 case LLL_INFO:
2275 syslog_level = LOG_INFO;
2276 break;
2277 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002278 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002279}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002280#endif
Andy Greenc11db202013-01-19 11:12:16 +08002281
Andy Green43db0452013-01-10 19:50:35 +08002282void _lws_log(int filter, const char *format, ...)
2283{
Andy Greende8f27a2013-01-12 09:17:42 +08002284 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002285 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002286
2287 if (!(log_level & filter))
2288 return;
2289
Andy Green43db0452013-01-10 19:50:35 +08002290 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002291 vsnprintf(buf, sizeof(buf), format, ap);
2292 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002293 va_end(ap);
2294
Andy Green0b319092013-01-19 11:17:56 +08002295 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002296}
2297
2298/**
2299 * lws_set_log_level() - Set the logging bitfield
2300 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002301 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2302 * function to perform log string emission instead of
2303 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002304 *
Andy Greenc6511a02013-02-19 10:01:48 +08002305 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002306 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002307 */
2308
Andy Greenb5b23192013-02-11 17:13:32 +08002309void lws_set_log_level(int level, void (*log_emit_function)(int level,
2310 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002311{
2312 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002313 if (log_emit_function)
2314 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002315}