blob: 6dc4d1d18f890ea155926706dd452023fe6c5e1f [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
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +010024#if defined(WIN32) || defined(_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
Andy Green3b3fa9e2013-12-25 16:34:37 +080040#include <sys/types.h>
41
Andy Green2e24da02011-03-05 16:12:04 +000042#ifdef LWS_OPENSSL_SUPPORT
43int openssl_websocket_private_data_index;
44#endif
45
Andy Greenaa6fc442012-04-12 13:26:49 +080046#ifdef __MINGW32__
47#include "../win32port/win32helpers/websock-w32.c"
48#else
49#ifdef __MINGW64__
50#include "../win32port/win32helpers/websock-w32.c"
51#endif
52#endif
53
Andy Green7b405452013-02-01 10:50:15 +080054#ifndef LWS_BUILD_HASH
55#define LWS_BUILD_HASH "unknown-build-hash"
56#endif
Andy Green3182ece2013-01-20 17:08:31 +080057
Andy Green7c19c342013-01-19 12:18:07 +080058static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080059static void lwsl_emit_stderr(int level, const char *line);
60static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
61
Andy Green7b405452013-02-01 10:50:15 +080062static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
63
Andy Greenb5b23192013-02-11 17:13:32 +080064static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080065 "ERR",
66 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080067 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080068 "INFO",
69 "DEBUG",
70 "PARSER",
71 "HEADER",
72 "EXTENSION",
73 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080074 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080075};
76
Andy Greenb5b23192013-02-11 17:13:32 +080077#ifndef LWS_NO_CLIENT
78 extern int lws_client_socket_service(
79 struct libwebsocket_context *context,
80 struct libwebsocket *wsi, struct pollfd *pollfd);
81#endif
82#ifndef LWS_NO_SERVER
83 extern int lws_server_socket_service(
84 struct libwebsocket_context *context,
85 struct libwebsocket *wsi, struct pollfd *pollfd);
86#endif
87
Andy Green3b3fa9e2013-12-25 16:34:37 +080088
89#ifdef LWS_HAS_PPOLL
90/*
91 * set to the Thread ID that's doing the service loop just before entry to ppoll
92 * indicates service thread likely idling in ppoll()
93 * volatile because other threads may check it as part of processing for pollfd
94 * event change.
95 */
96static volatile int lws_idling_ppoll_tid;
97#endif
98
Andy Green7b405452013-02-01 10:50:15 +080099/**
100 * lws_get_library_version: get version and git hash library built from
101 *
102 * returns a const char * to a string like "1.1 178d78c"
103 * representing the library version followed by the git head hash it
104 * was built from
105 */
106
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800107LWS_VISIBLE const char *
Andy Green7b405452013-02-01 10:50:15 +0800108lws_get_library_version(void)
109{
110 return library_version;
111}
112
Andy Green0d338332011-02-12 11:57:43 +0000113int
Andy Greenb5b23192013-02-11 17:13:32 +0800114insert_wsi_socket_into_fds(struct libwebsocket_context *context,
115 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000116{
Andy Greendfb23042013-01-17 12:26:48 +0800117 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000119 return 1;
120 }
121
Andy Greendfb23042013-01-17 12:26:48 +0800122 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800123 lwsl_err("Socket fd %d is too high (%d)\n",
124 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800125 return 1;
126 }
127
128 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800129 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800130
Andy Greenb5b23192013-02-11 17:13:32 +0800131 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
132 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800133
Andy Green7a132792013-12-18 09:48:26 +0800134 context->protocols[0].callback(context, wsi,
135 LWS_CALLBACK_LOCK_POLL,
136 wsi->user_space, (void *)(long)wsi->sock, 0);
137
Andy Greendfb23042013-01-17 12:26:48 +0800138 context->lws_lookup[wsi->sock] = wsi;
139 wsi->position_in_fds_table = context->fds_count;
140 context->fds[context->fds_count].fd = wsi->sock;
141 context->fds[context->fds_count].events = POLLIN;
142 context->fds[context->fds_count++].revents = 0;
143
144 /* external POLL support via protocol 0 */
145 context->protocols[0].callback(context, wsi,
146 LWS_CALLBACK_ADD_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800147 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000148
Andy Green7a132792013-12-18 09:48:26 +0800149 context->protocols[0].callback(context, wsi,
150 LWS_CALLBACK_UNLOCK_POLL,
151 wsi->user_space, (void *)(long)wsi->sock, 0);
152
Andy Green0d338332011-02-12 11:57:43 +0000153 return 0;
154}
155
Andy Greendfb23042013-01-17 12:26:48 +0800156static int
Andy Greenb5b23192013-02-11 17:13:32 +0800157remove_wsi_socket_from_fds(struct libwebsocket_context *context,
158 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000159{
Andy Greendfb23042013-01-17 12:26:48 +0800160 int m;
Andy Green0d338332011-02-12 11:57:43 +0000161
Andy Green7a132792013-12-18 09:48:26 +0800162 if (!--context->fds_count) {
163 context->protocols[0].callback(context, wsi,
164 LWS_CALLBACK_LOCK_POLL,
165 wsi->user_space, (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800166 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800167 }
Andy Green0d338332011-02-12 11:57:43 +0000168
Andy Greendfb23042013-01-17 12:26:48 +0800169 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800170 lwsl_err("Socket fd %d too high (%d)\n",
171 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800172 return 1;
173 }
Andy Green0d338332011-02-12 11:57:43 +0000174
Andy Greenb5b23192013-02-11 17:13:32 +0800175 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
176 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800177
Andy Green7a132792013-12-18 09:48:26 +0800178 context->protocols[0].callback(context, wsi,
179 LWS_CALLBACK_LOCK_POLL,
180 wsi->user_space, (void *)(long)wsi->sock, 0);
181
Andy Greendfb23042013-01-17 12:26:48 +0800182 m = wsi->position_in_fds_table; /* replace the contents for this */
183
184 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800185 context->fds[m] = context->fds[context->fds_count];
186 /*
187 * end guy's fds_lookup entry remains unchanged
188 * (still same fd pointing to same wsi)
189 */
Andy Greendfb23042013-01-17 12:26:48 +0800190 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800191 context->lws_lookup[context->fds[context->fds_count].fd]->
192 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800193 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800194 context->lws_lookup[wsi->sock] = NULL;
195 /* removed wsi has no position any more */
196 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800197
198do_ext:
199 /* remove also from external POLL support via protocol 0 */
200 if (wsi->sock)
201 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800202 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
203 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800204
Andy Green7a132792013-12-18 09:48:26 +0800205 context->protocols[0].callback(context, wsi,
206 LWS_CALLBACK_UNLOCK_POLL,
207 wsi->user_space, (void *)(long)wsi->sock, 0);
208
Andy Greendfb23042013-01-17 12:26:48 +0800209 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000210}
211
Andy Green32375b72011-02-19 08:32:53 +0000212
Andy Green8f037e42010-12-19 22:13:26 +0000213void
Peter Hinz56885f32011-03-02 22:03:47 +0000214libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000215 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000216{
Andy Greenb45993c2010-12-18 15:13:50 +0000217 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000218 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000219 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
220 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800221#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000222 int ret;
223 int m;
224 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100225 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800226#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000227
Andy Green4b6fbe12011-02-14 08:03:48 +0000228 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000229 return;
230
Andy Green62c54d22011-02-14 09:14:25 +0000231 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000232
Andy Green62c54d22011-02-14 09:14:25 +0000233 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000234 return;
235
Andy Green22524a62013-02-15 22:48:58 +0800236 /* we tried the polite way... */
237 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
238 goto just_kill_connection;
239
Andy Green623a98d2013-01-21 11:04:23 +0800240 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000241
Andy Green5dc62ea2013-09-20 20:26:12 +0800242 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
243 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
244
245 context->protocols[0].callback(context, wsi,
246 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
247
248 free(wsi->u.hdr.ah);
249 goto just_kill_connection;
250 }
251
252
kapejodce64fb02013-11-19 13:38:16 +0100253 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
254 if (wsi->u.http.post_buffer) {
255 free(wsi->u.http.post_buffer);
256 wsi->u.http.post_buffer = NULL;
257 }
258 if (wsi->u.http.fd >= 0) {
259 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
260 close(wsi->u.http.fd);
261 wsi->u.http.fd = -1;
262 context->protocols[0].callback(context, wsi,
263 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
264 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800265 }
266
Andy Green3182ece2013-01-20 17:08:31 +0800267#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000268 /*
Andy Green68b45042011-05-25 21:41:57 +0100269 * are his extensions okay with him closing? Eg he might be a mux
270 * parent and just his ch1 aspect is closing?
271 */
272
Andy Green68b45042011-05-25 21:41:57 +0100273 for (n = 0; n < wsi->count_active_extensions; n++) {
274 if (!wsi->active_extensions[n]->callback)
275 continue;
276
277 m = wsi->active_extensions[n]->callback(context,
278 wsi->active_extensions[n], wsi,
279 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
280 wsi->active_extensions_user[n], NULL, 0);
281
282 /*
283 * if somebody vetoed actually closing him at this time....
284 * up to the extension to track the attempted close, let's
285 * just bail
286 */
287
288 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800289 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100290 return;
291 }
292 }
293
Andy Green68b45042011-05-25 21:41:57 +0100294 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000295 * flush any tx pending from extensions, since we may send close packet
296 * if there are problems with send, just nuke the connection
297 */
298
299 ret = 1;
300 while (ret == 1) {
301
302 /* default to nobody has more to spill */
303
304 ret = 0;
305 eff_buf.token = NULL;
306 eff_buf.token_len = 0;
307
308 /* show every extension the new incoming data */
309
310 for (n = 0; n < wsi->count_active_extensions; n++) {
311 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000312 wsi->protocol->owning_server,
313 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000314 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
315 wsi->active_extensions_user[n], &eff_buf, 0);
316 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800317 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000318 goto just_kill_connection;
319 }
320 if (m)
321 /*
322 * at least one extension told us he has more
323 * to spill, so we will go around again after
324 */
325 ret = 1;
326 }
327
328 /* assuming they left us something to send, send it */
329
330 if (eff_buf.token_len)
331 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800332 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800333 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000334 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800335 }
Andy Greenc44159f2011-03-07 07:08:18 +0000336 }
Andy Green3182ece2013-01-20 17:08:31 +0800337#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000338
339 /*
Andy Greenda527df2011-03-07 07:08:12 +0000340 * signal we are closing, libsocket_write will
341 * add any necessary version-specific stuff. If the write fails,
342 * no worries we are closing anyway. If we didn't initiate this
343 * close, then our state has been changed to
344 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
345 *
346 * Likewise if it's a second call to close this connection after we
347 * sent the close indication to the peer already, we are in state
348 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
349 */
350
351 if (old_state == WSI_STATE_ESTABLISHED &&
352 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100353
Andy Green43db0452013-01-10 19:50:35 +0800354 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100355
Andy Greenfdd305a2013-02-11 14:32:48 +0800356 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800357 memset(buf, 0, sizeof(buf));
358 n = libwebsocket_write(wsi,
359 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000360 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800361 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000362 /*
363 * we have sent a nice protocol level indication we
364 * now wish to close, we should not send anything more
365 */
366
367 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
368
Andy Greenb5b23192013-02-11 17:13:32 +0800369 /*
370 * ...and we should wait for a reply for a bit
371 * out of politeness
372 */
Andy Greenda527df2011-03-07 07:08:12 +0000373
374 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800375 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000376
Andy Green43db0452013-01-10 19:50:35 +0800377 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000378
379 return;
380 }
381
Andy Greenb5b23192013-02-11 17:13:32 +0800382 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800383
Andy Greenda527df2011-03-07 07:08:12 +0000384 /* else, the send failed and we should just hang up */
385 }
386
Andy Greenc44159f2011-03-07 07:08:18 +0000387just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100388
Andy Greenb5b23192013-02-11 17:13:32 +0800389 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100390
Andy Greenda527df2011-03-07 07:08:12 +0000391 /*
392 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800393 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000394 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000395
Andy Greendfb23042013-01-17 12:26:48 +0800396 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000397
Andy Green251f6fa2010-11-03 11:13:06 +0000398 wsi->state = WSI_STATE_DEAD_SOCKET;
399
Andy Greenb5b23192013-02-11 17:13:32 +0800400 if ((old_state == WSI_STATE_ESTABLISHED ||
401 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800402 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
403
404 if (wsi->u.ws.rx_user_buffer) {
405 free(wsi->u.ws.rx_user_buffer);
406 wsi->u.ws.rx_user_buffer = NULL;
407 }
408 if (wsi->u.ws.rxflow_buffer) {
409 free(wsi->u.ws.rxflow_buffer);
410 wsi->u.ws.rxflow_buffer = NULL;
411 }
Andy Green2764eba2013-12-09 14:16:17 +0800412 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800413 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800414 free(wsi->truncated_send_malloc);
415 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800416 }
Andy Green54495112013-02-06 21:10:16 +0900417 }
418
Andy Green4b6fbe12011-02-14 08:03:48 +0000419 /* tell the user it's all over for this guy */
420
Andy Greend4302732011-02-28 07:45:29 +0000421 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800422 ((old_state == WSI_STATE_ESTABLISHED) ||
423 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
424 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800425 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000426 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000427 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800428 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
429 lwsl_debug("calling back CLOSED_HTTP\n");
430 context->protocols[0].callback(context, wsi,
431 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800432 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800433 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000434
Andy Green3182ece2013-01-20 17:08:31 +0800435#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000436 /* deallocate any active extension contexts */
437
438 for (n = 0; n < wsi->count_active_extensions; n++) {
439 if (!wsi->active_extensions[n]->callback)
440 continue;
441
Andy Green46c2ea02011-03-22 09:04:01 +0000442 wsi->active_extensions[n]->callback(context,
443 wsi->active_extensions[n], wsi,
444 LWS_EXT_CALLBACK_DESTROY,
445 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000446
447 free(wsi->active_extensions_user[n]);
448 }
449
Andy Greena41314f2011-05-23 10:00:03 +0100450 /*
451 * inform all extensions in case they tracked this guy out of band
452 * even though not active on him specifically
453 */
454
455 ext = context->extensions;
456 while (ext && ext->callback) {
457 ext->callback(context, ext, wsi,
458 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
459 NULL, NULL, 0);
460 ext++;
461 }
Andy Green3182ece2013-01-20 17:08:31 +0800462#endif
Andy Greena41314f2011-05-23 10:00:03 +0100463
Andy Green43db0452013-01-10 19:50:35 +0800464/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000465
Andy Green3faa9c72010-11-08 17:03:03 +0000466#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000467 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000468 n = SSL_get_fd(wsi->ssl);
469 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800470 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000471 SSL_free(wsi->ssl);
472 } else {
473#endif
Andy Green0303db42013-01-17 14:46:43 +0800474 if (wsi->sock) {
475 n = shutdown(wsi->sock, SHUT_RDWR);
476 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800477 lwsl_debug("closing: shutdown returned %d\n",
478 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800479
Andy Green0303db42013-01-17 14:46:43 +0800480 n = compatible_close(wsi->sock);
481 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800482 lwsl_debug("closing: close returned %d\n",
483 errno);
Andy Green0303db42013-01-17 14:46:43 +0800484 }
Andy Green3faa9c72010-11-08 17:03:03 +0000485#ifdef LWS_OPENSSL_SUPPORT
486 }
487#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800488 if (wsi->protocol && wsi->protocol->per_session_data_size &&
489 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000490 free(wsi->user_space);
491
Andy Green251f6fa2010-11-03 11:13:06 +0000492 free(wsi);
493}
494
Andy Green07034092011-02-13 08:37:12 +0000495/**
496 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800497 * @context: Libwebsockets context
498 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000499 * @fd: Connection socket descriptor
500 * @name: Buffer to take client address name
501 * @name_len: Length of client address name buffer
502 * @rip: Buffer to take client address IP qotted quad
503 * @rip_len: Length of client address IP buffer
504 *
505 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800506 * the client connected with socket descriptor @fd. Names may be
507 * truncated if there is not enough room. If either cannot be
508 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000509 */
510
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800511LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800512libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
513 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000514 char *rip, int rip_len)
515{
Bob Robertsac049112013-04-25 09:16:30 +0800516 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000517 struct sockaddr_in sin;
518 struct hostent *host;
519 struct hostent *host1;
520 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000521 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000522 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800523 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800524#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800525 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800526#endif
Andy Green07034092011-02-13 08:37:12 +0000527
528 rip[0] = '\0';
529 name[0] = '\0';
530
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800531 lws_latency_pre(context, wsi);
532
Andy Greenb5b23192013-02-11 17:13:32 +0800533 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000534 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
535 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800536 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000537 }
Andy Green6ee372f2012-04-09 15:09:01 +0800538
Andy Greenb5b23192013-02-11 17:13:32 +0800539 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000540 AF_INET);
541 if (host == NULL) {
542 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800543 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000544 }
545
546 strncpy(name, host->h_name, name_len);
547 name[name_len - 1] = '\0';
548
549 host1 = gethostbyname(host->h_name);
550 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800551 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000552 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000553 n = 0;
554 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000555 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000556 if (p == NULL)
557 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000558 if ((host1->h_addrtype != AF_INET)
559#ifdef AF_LOCAL
560 && (host1->h_addrtype != AF_LOCAL)
561#endif
562 )
Andy Green07034092011-02-13 08:37:12 +0000563 continue;
564
Andy Green7627af52011-03-09 15:13:52 +0000565 if (host1->h_addrtype == AF_INET)
566 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000567#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000568 else {
569 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800570 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000571 ip[sizeof(ip) - 1] = '\0';
572 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000573#endif
Andy Green07034092011-02-13 08:37:12 +0000574 p = NULL;
575 strncpy(rip, ip, rip_len);
576 rip[rip_len - 1] = '\0';
577 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800578
579 ret = 0;
580bail:
581 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000582}
Andy Green9f990342011-02-12 11:57:45 +0000583
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800584LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000585 void *buf, int len)
586{
587 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800588 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000589
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100590#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000591 for (n = 0; n < len; n++)
592 p[n] = (unsigned char)rand();
593#else
594 n = read(context->fd_random, p, len);
595#endif
596
597 return n;
598}
599
Andy Greena690cd02013-02-09 12:25:31 +0800600int lws_set_socket_options(struct libwebsocket_context *context, int fd)
601{
602 int optval = 1;
603 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100604#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800605 unsigned long optl = 0;
606#endif
607#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
608 struct protoent *tcp_proto;
609#endif
610
611 if (context->ka_time) {
612 /* enable keepalive on this socket */
613 optval = 1;
614 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
615 (const void *)&optval, optlen) < 0)
616 return 1;
617
Bob Robertsac049112013-04-25 09:16:30 +0800618#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800619
620 /*
621 * didn't find a way to set these per-socket, need to
622 * tune kernel systemwide values
623 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100624#elif WIN32
625 {
626 DWORD dwBytesRet;
627 struct tcp_keepalive alive;
628 alive.onoff = TRUE;
629 alive.keepalivetime = context->ka_time;
630 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800631
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100632 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
633 NULL, 0, &dwBytesRet, NULL, NULL))
634 return 1;
635 }
Andy Greena47865f2013-02-10 09:39:47 +0800636#else
Andy Greena690cd02013-02-09 12:25:31 +0800637 /* set the keepalive conditions we want on it too */
638 optval = context->ka_time;
639 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
640 (const void *)&optval, optlen) < 0)
641 return 1;
642
Larry Hayesbb66ac62013-02-22 09:16:20 +0800643 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800644 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
645 (const void *)&optval, optlen) < 0)
646 return 1;
647
Larry Hayesbb66ac62013-02-22 09:16:20 +0800648 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800649 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
650 (const void *)&optval, optlen) < 0)
651 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800652#endif
Andy Greena690cd02013-02-09 12:25:31 +0800653 }
654
655 /* Disable Nagle */
656 optval = 1;
657#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
658 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
659#else
660 tcp_proto = getprotobyname("TCP");
661 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
662#endif
663
664 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100665#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800666 ioctlsocket(fd, FIONBIO, &optl);
667#else
668 fcntl(fd, F_SETFL, O_NONBLOCK);
669#endif
670
671 return 0;
672}
673
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800674LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000675{
676 struct pollfd fds;
677
Andy Green2764eba2013-12-09 14:16:17 +0800678 /* treat the fact we got a truncated send pending as if we're choked */
679 if (wsi->truncated_send_malloc)
680 return 1;
681
Andy Green95a7b5d2011-03-06 10:29:39 +0000682 fds.fd = wsi->sock;
683 fds.events = POLLOUT;
684 fds.revents = 0;
685
686 if (poll(&fds, 1, 0) != 1)
687 return 1;
688
689 if ((fds.revents & POLLOUT) == 0)
690 return 1;
691
692 /* okay to send another packet without blocking */
693
694 return 0;
695}
696
Andy Greena41314f2011-05-23 10:00:03 +0100697int
Andy Green3b84c002011-03-06 13:14:42 +0000698lws_handle_POLLOUT_event(struct libwebsocket_context *context,
699 struct libwebsocket *wsi, struct pollfd *pollfd)
700{
Andy Green3b84c002011-03-06 13:14:42 +0000701 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800702
Andy Green3182ece2013-01-20 17:08:31 +0800703#ifndef LWS_NO_EXTENSIONS
704 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000705 int ret;
706 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100707 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000708
Andy Green1f4267b2013-10-17 08:09:19 +0800709 /* pending truncated sends have uber priority */
710
Andy Green2764eba2013-12-09 14:16:17 +0800711 if (wsi->truncated_send_malloc) {
712 lws_issue_raw(wsi, wsi->truncated_send_malloc +
713 wsi->truncated_send_offset,
714 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800715 /* leave POLLOUT active either way */
716 return 0;
717 }
718
Andy Greena41314f2011-05-23 10:00:03 +0100719 for (n = 0; n < wsi->count_active_extensions; n++) {
720 if (!wsi->active_extensions[n]->callback)
721 continue;
722
723 m = wsi->active_extensions[n]->callback(context,
724 wsi->active_extensions[n], wsi,
725 LWS_EXT_CALLBACK_IS_WRITEABLE,
726 wsi->active_extensions_user[n], NULL, 0);
727 if (m > handled)
728 handled = m;
729 }
730
731 if (handled == 1)
732 goto notify_action;
733
734 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000735 goto user_service;
736
737 /*
738 * check in on the active extensions, see if they
739 * had pending stuff to spill... they need to get the
740 * first look-in otherwise sequence will be disordered
741 *
742 * NULL, zero-length eff_buf means just spill pending
743 */
744
745 ret = 1;
746 while (ret == 1) {
747
748 /* default to nobody has more to spill */
749
750 ret = 0;
751 eff_buf.token = NULL;
752 eff_buf.token_len = 0;
753
754 /* give every extension a chance to spill */
755
756 for (n = 0; n < wsi->count_active_extensions; n++) {
757 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000758 wsi->protocol->owning_server,
759 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000760 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
761 wsi->active_extensions_user[n], &eff_buf, 0);
762 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800763 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000764 return -1;
765 }
766 if (m)
767 /*
768 * at least one extension told us he has more
769 * to spill, so we will go around again after
770 */
771 ret = 1;
772 }
773
774 /* assuming they gave us something to send, send it */
775
776 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800777 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
778 eff_buf.token_len);
779 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000780 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800781 /*
782 * Keep amount spilled small to minimize chance of this
783 */
784 if (n != eff_buf.token_len) {
785 lwsl_err("Unable to spill ext %d vs %s\n",
786 eff_buf.token_len, n);
787 return -1;
788 }
Andy Green3b84c002011-03-06 13:14:42 +0000789 } else
790 continue;
791
792 /* no extension has more to spill */
793
794 if (!ret)
795 continue;
796
797 /*
798 * There's more to spill from an extension, but we just sent
799 * something... did that leave the pipe choked?
800 */
801
802 if (!lws_send_pipe_choked(wsi))
803 /* no we could add more */
804 continue;
805
Andy Green43db0452013-01-10 19:50:35 +0800806 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000807
808 /*
809 * Yes, he's choked. Leave the POLLOUT masked on so we will
810 * come back here when he is unchoked. Don't call the user
811 * callback to enforce ordering of spilling, he'll get called
812 * when we come back here and there's nothing more to spill.
813 */
814
815 return 0;
816 }
817
818 wsi->extension_data_pending = 0;
819
820user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800821#endif
Andy Green3b84c002011-03-06 13:14:42 +0000822 /* one shot */
823
Andy Green91f19d82013-12-21 11:18:34 +0800824 if (pollfd)
825 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800826
Andy Green3182ece2013-01-20 17:08:31 +0800827#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100828notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800829#endif
Andy Green3b84c002011-03-06 13:14:42 +0000830
Andy Green9e4c2b62011-03-07 20:47:39 +0000831 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
832 n = LWS_CALLBACK_CLIENT_WRITEABLE;
833 else
834 n = LWS_CALLBACK_SERVER_WRITEABLE;
835
Andy Greenb8b247d2013-01-22 07:20:08 +0800836 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800837 wsi, (enum libwebsocket_callback_reasons) n,
838 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000839}
840
841
842
Andy Green1c6e1422013-02-20 19:11:31 +0800843int
Andy Greena41314f2011-05-23 10:00:03 +0100844libwebsocket_service_timeout_check(struct libwebsocket_context *context,
845 struct libwebsocket *wsi, unsigned int sec)
846{
Andy Green3182ece2013-01-20 17:08:31 +0800847#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100848 int n;
849
850 /*
851 * if extensions want in on it (eg, we are a mux parent)
852 * give them a chance to service child timeouts
853 */
854
855 for (n = 0; n < wsi->count_active_extensions; n++)
856 wsi->active_extensions[n]->callback(
857 context, wsi->active_extensions[n],
858 wsi, LWS_EXT_CALLBACK_1HZ,
859 wsi->active_extensions_user[n], NULL, sec);
860
Andy Green3182ece2013-01-20 17:08:31 +0800861#endif
Andy Greena41314f2011-05-23 10:00:03 +0100862 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800863 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800864
Andy Greena41314f2011-05-23 10:00:03 +0100865 /*
866 * if we went beyond the allowed time, kill the
867 * connection
868 */
869
870 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800871 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100872 libwebsocket_close_and_free_session(context,
873 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800874 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100875 }
Andy Green1c6e1422013-02-20 19:11:31 +0800876
877 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100878}
879
Andy Green9f990342011-02-12 11:57:45 +0000880/**
881 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000882 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000883 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800884 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000885 *
Andy Green75006172013-01-22 12:32:11 +0800886 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800887 * services it according to the state of the associated
888 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800889 *
890 * The one call deals with all "service" that might happen on a socket
891 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800892 *
893 * If a pollfd says it has something, you can just pass it to
894 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
895 * If it sees it is a lws socket, the traffic will be handled and
896 * pollfd->revents will be zeroed now.
897 *
898 * If the socket is foreign to lws, it leaves revents alone. So you can
899 * see if you should service yourself by checking the pollfd revents
900 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000901 */
902
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800903LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000904libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000905 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000906{
Andy Greena1ce6be2013-01-18 11:43:21 +0800907 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000908 int n;
Andy Green0d338332011-02-12 11:57:43 +0000909 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800910 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000911 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800912 int timed_out = 0;
913 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800914 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800915
Andy Green3182ece2013-01-20 17:08:31 +0800916#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000917 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800918#endif
Andy Green98a717c2011-03-06 13:14:15 +0000919 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800920
921 if (context->listen_service_fd)
922 listen_socket_fds_index = context->lws_lookup[
923 context->listen_service_fd]->position_in_fds_table;
924
Andy Greena71eafc2011-02-14 17:59:43 +0000925 /*
926 * you can call us with pollfd = NULL to just allow the once-per-second
927 * global timeout checks; if less than a second since the last check
928 * it returns immediately then.
929 */
930
931 gettimeofday(&tv, NULL);
932
Peter Hinz56885f32011-03-02 22:03:47 +0000933 if (context->last_timeout_check_s != tv.tv_sec) {
934 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000935
Joakim Soderberg4c531232013-02-06 15:26:58 +0900936 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800937 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800938 if (context->started_with_parent &&
939 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800940 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900941 #endif
Andy Green24cba922013-01-19 13:56:10 +0800942
Andy Greena71eafc2011-02-14 17:59:43 +0000943 /* global timeout check once per second */
944
Andy Green1c6e1422013-02-20 19:11:31 +0800945 if (pollfd)
946 our_fd = pollfd->fd;
947
Peter Hinz56885f32011-03-02 22:03:47 +0000948 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800949 m = context->fds[n].fd;
950 wsi = context->lws_lookup[m];
951 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800952 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800953
954 if (libwebsocket_service_timeout_check(context, wsi,
955 tv.tv_sec))
956 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800957 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800958 /* it was the guy we came to service! */
959 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800960 /* mark as handled */
961 pollfd->revents = 0;
962 }
Andy Greena71eafc2011-02-14 17:59:43 +0000963 }
964 }
965
Andy Green1c6e1422013-02-20 19:11:31 +0800966 /* the socket we came to service timed out, nothing to do */
967 if (timed_out)
968 return 0;
969
Andy Greena71eafc2011-02-14 17:59:43 +0000970 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000971 if (pollfd == NULL)
972 return 0;
973
974 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800975 wsi = context->lws_lookup[pollfd->fd];
976 if (wsi == NULL)
977 /* not lws connection ... leave revents alone and return */
978 return 0;
979
980 /*
981 * so that caller can tell we handled, past here we need to
982 * zero down pollfd->revents after handling
983 */
984
Andy Green65b0e912013-01-16 07:59:47 +0800985 /*
986 * deal with listen service piggybacking
987 * every listen_service_modulo services of other fds, we
988 * sneak one in to service the listen socket if there's anything waiting
989 *
990 * To handle connection storms, as found in ab, if we previously saw a
991 * pending connection here, it causes us to check again next time.
992 */
993
Andy Greenb5b23192013-02-11 17:13:32 +0800994 if (context->listen_service_fd && pollfd !=
995 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800996 context->listen_service_count++;
997 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800998 context->listen_service_count ==
999 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001000 context->listen_service_count = 0;
1001 m = 1;
1002 if (context->listen_service_extraseen > 5)
1003 m = 2;
1004 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001005 /*
1006 * even with extpoll, we prepared this
1007 * internal fds for listen
1008 */
1009 n = poll(&context->fds[listen_socket_fds_index],
1010 1, 0);
1011 if (n > 0) { /* there's a conn waiting for us */
1012 libwebsocket_service_fd(context,
1013 &context->
1014 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001015 context->listen_service_extraseen++;
1016 } else {
1017 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001018 context->
1019 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001020 break;
1021 }
1022 }
1023 }
1024
1025 }
1026
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001027 /* handle session socket closed */
1028
1029 if ((!(pollfd->revents & POLLIN)) &&
1030 (pollfd->revents & (POLLERR | POLLHUP))) {
1031
1032 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1033 (void *)wsi, pollfd->fd);
1034
1035 goto close_and_handled;
1036 }
1037
Andy Green65b0e912013-01-16 07:59:47 +08001038 /* okay, what we came here to do... */
1039
Andy Green0d338332011-02-12 11:57:43 +00001040 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001041
Andy Greena1ce6be2013-01-18 11:43:21 +08001042#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001043 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001044 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001045 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001046 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001047 n = lws_server_socket_service(context, wsi, pollfd);
1048 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001049#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001050
Andy Green0d338332011-02-12 11:57:43 +00001051 case LWS_CONNMODE_WS_SERVING:
1052 case LWS_CONNMODE_WS_CLIENT:
1053
Andy Green0d338332011-02-12 11:57:43 +00001054 /* the guy requested a callback when it was OK to write */
1055
Andy Greenda527df2011-03-07 07:08:12 +00001056 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001057 wsi->state == WSI_STATE_ESTABLISHED &&
1058 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001059 lwsl_info("libwebsocket_service_fd: closing\n");
1060 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001061 }
Andy Green0d338332011-02-12 11:57:43 +00001062
Andy Greenca0a1292013-03-16 11:24:23 +08001063 if (wsi->u.ws.rxflow_buffer &&
1064 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1065 lwsl_info("draining rxflow\n");
1066 /* well, drain it */
1067 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1068 wsi->u.ws.rxflow_pos;
1069 eff_buf.token_len = wsi->u.ws.rxflow_len -
1070 wsi->u.ws.rxflow_pos;
1071 draining_flow = 1;
1072 goto drain;
1073 }
1074
Andy Green0d338332011-02-12 11:57:43 +00001075 /* any incoming data ready? */
1076
1077 if (!(pollfd->revents & POLLIN))
1078 break;
1079
Andy Greenb45993c2010-12-18 15:13:50 +00001080#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001081read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001082 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001083 eff_buf.token_len = SSL_read(wsi->ssl,
1084 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001085 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001086 if (!eff_buf.token_len) {
1087 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001088 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001089 ERR_error_string(n,
1090 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001091 }
1092 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001093#endif
Andy Greene84652c2013-02-08 13:01:02 +08001094 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001095 context->service_buffer,
1096 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001097
Andy Green98a717c2011-03-06 13:14:15 +00001098 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001099 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1100 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001101 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001102 goto close_and_handled;
1103 n = 0;
1104 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001105 }
Andy Green98a717c2011-03-06 13:14:15 +00001106 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001107 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001108 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001109 }
1110
Andy Green98a717c2011-03-06 13:14:15 +00001111 /*
1112 * give any active extensions a chance to munge the buffer
1113 * before parse. We pass in a pointer to an lws_tokens struct
1114 * prepared with the default buffer and content length that's in
1115 * there. Rather than rewrite the default buffer, extensions
1116 * that expect to grow the buffer can adapt .token to
1117 * point to their own per-connection buffer in the extension
1118 * user allocation. By default with no extensions or no
1119 * extension callback handling, just the normal input buffer is
1120 * used then so it is efficient.
1121 */
Andy Greenb45993c2010-12-18 15:13:50 +00001122
Andy Greene84652c2013-02-08 13:01:02 +08001123 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001124drain:
Andy Green3182ece2013-01-20 17:08:31 +08001125#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001126 more = 1;
1127 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001128
Andy Green98a717c2011-03-06 13:14:15 +00001129 more = 0;
1130
1131 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001132 m = wsi->active_extensions[n]->callback(context,
1133 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001134 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001135 wsi->active_extensions_user[n],
1136 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001137 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001138 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001139 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001140 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001141 }
1142 if (m)
1143 more = 1;
1144 }
Andy Green3182ece2013-01-20 17:08:31 +08001145#endif
Andy Green98a717c2011-03-06 13:14:15 +00001146 /* service incoming data */
1147
1148 if (eff_buf.token_len) {
1149 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001150 (unsigned char *)eff_buf.token,
1151 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001152 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001153 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001154 n = 0;
1155 goto handled;
1156 }
Andy Green98a717c2011-03-06 13:14:15 +00001157 }
Andy Green3182ece2013-01-20 17:08:31 +08001158#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001159 eff_buf.token = NULL;
1160 eff_buf.token_len = 0;
1161 }
Andy Green3182ece2013-01-20 17:08:31 +08001162#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001163 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1164 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1165 lwsl_info("flow buffer: drained\n");
1166 free(wsi->u.ws.rxflow_buffer);
1167 wsi->u.ws.rxflow_buffer = NULL;
1168 /* having drained the rxflow buffer, can rearm POLLIN */
1169 _libwebsocket_rx_flow_control(wsi);
1170 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001171
1172#ifdef LWS_OPENSSL_SUPPORT
1173 if (wsi->ssl && SSL_pending(wsi->ssl))
1174 goto read_pending;
1175#endif
Andy Green98a717c2011-03-06 13:14:15 +00001176 break;
Andy Green76f61e72013-01-16 11:53:05 +08001177
1178 default:
Andy Green03674a62013-01-16 11:47:40 +08001179#ifdef LWS_NO_CLIENT
1180 break;
1181#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001182 n = lws_client_socket_service(context, wsi, pollfd);
1183 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001184#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001185 }
1186
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001187 n = 0;
1188 goto handled;
1189
1190close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001191 libwebsocket_close_and_free_session(context, wsi,
1192 LWS_CLOSE_STATUS_NOSTATUS);
1193 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001194
1195handled:
1196 pollfd->revents = 0;
1197 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001198}
1199
Andy Green0d338332011-02-12 11:57:43 +00001200
Andy Green6964bb52011-01-23 16:50:33 +00001201/**
1202 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001203 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001204 *
1205 * This function closes any active connections and then frees the
1206 * context. After calling this, any further use of the context is
1207 * undefined.
1208 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001209LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001210libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001211{
Andy Green3182ece2013-01-20 17:08:31 +08001212#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001213 int n;
1214 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001215 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001216 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001217
Andy Greend636e352013-01-29 12:36:17 +08001218#ifdef LWS_LATENCY
1219 if (context->worst_latency_info[0])
1220 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1221#endif
1222
Andy Greendfb23042013-01-17 12:26:48 +08001223 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001224 struct libwebsocket *wsi =
1225 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001226 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001227 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1228 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001229 }
Andy Green6964bb52011-01-23 16:50:33 +00001230
Andy Greena41314f2011-05-23 10:00:03 +01001231 /*
1232 * give all extensions a chance to clean up any per-context
1233 * allocations they might have made
1234 */
1235
1236 ext = context->extensions;
1237 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1238 if (context->listen_port)
1239 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001240 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001241 ext->callback(context, ext, NULL,
1242 (enum libwebsocket_extension_callback_reasons)m,
1243 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001244 ext++;
1245 }
Andy Greena7109e62013-02-11 12:05:54 +08001246
1247 /*
1248 * inform all the protocols that they are done and will have no more
1249 * callbacks
1250 */
1251
1252 while (protocol->callback) {
1253 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1254 NULL, NULL, 0);
1255 protocol++;
1256 }
1257
Andy Green3182ece2013-01-20 17:08:31 +08001258#endif
Andy Greena41314f2011-05-23 10:00:03 +01001259
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001260#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001261#else
1262 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001263#endif
1264
Peter Hinz56885f32011-03-02 22:03:47 +00001265#ifdef LWS_OPENSSL_SUPPORT
1266 if (context->ssl_ctx)
1267 SSL_CTX_free(context->ssl_ctx);
1268 if (context->ssl_client_ctx)
1269 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001270
1271 ERR_remove_state(0);
1272 ERR_free_strings();
1273 EVP_cleanup();
1274 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001275#endif
1276
Andy Green46596482013-02-11 11:04:01 +08001277 if (context->fds)
1278 free(context->fds);
1279 if (context->lws_lookup)
1280 free(context->lws_lookup);
1281
Peter Hinz56885f32011-03-02 22:03:47 +00001282 free(context);
1283
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001284#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001285 WSACleanup();
1286#endif
Andy Green6964bb52011-01-23 16:50:33 +00001287}
1288
Andy Greend88146d2013-01-22 12:40:35 +08001289/**
Andy Greenb5b23192013-02-11 17:13:32 +08001290 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001291 * @context: Websocket context
1292 *
1293 * This returns the optional user allocation that can be attached to
1294 * the context the sockets live in at context_create time. It's a way
1295 * to let all sockets serviced in the same context share data without
1296 * using globals statics in the user code.
1297 */
Alon Levy0291eb32012-10-19 11:21:56 +02001298LWS_EXTERN void *
1299libwebsocket_context_user(struct libwebsocket_context *context)
1300{
Andy Greenb5b23192013-02-11 17:13:32 +08001301 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001302}
1303
Andy Green6964bb52011-01-23 16:50:33 +00001304/**
1305 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001306 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001307 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1308 * service otherwise block and service immediately, returning
1309 * after the timeout if nothing needed service.
1310 *
1311 * This function deals with any pending websocket traffic, for three
1312 * kinds of event. It handles these events on both server and client
1313 * types of connection the same.
1314 *
1315 * 1) Accept new connections to our context's server
1316 *
Andy Green6f520a52013-01-29 17:57:39 +08001317 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001318 * server or client connections.
1319 *
1320 * You need to call this service function periodically to all the above
1321 * functions to happen; if your application is single-threaded you can
1322 * just call it in your main event loop.
1323 *
1324 * Alternatively you can fork a new process that asynchronously handles
1325 * calling this service in a loop. In that case you are happy if this
1326 * call blocks your thread until it needs to take care of something and
1327 * would call it with a large nonzero timeout. Your loop then takes no
1328 * CPU while there is nothing happening.
1329 *
1330 * If you are calling it in a single-threaded app, you don't want it to
1331 * wait around blocking other things in your loop from happening, so you
1332 * would call it with a timeout_ms of 0, so it returns immediately if
1333 * nothing is pending, or as soon as it services whatever was pending.
1334 */
1335
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001336LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001337libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001338{
1339 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001340 int m;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001341#ifdef LWS_HAS_PPOLL
1342 struct timespec timeout_ts;
1343 sigset_t sigmask;
1344#endif
Andy Greene92cd172011-01-19 13:11:55 +00001345
1346 /* stay dead once we are dead */
1347
Peter Hinz56885f32011-03-02 22:03:47 +00001348 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001349 return 1;
1350
Andy Green3b3fa9e2013-12-25 16:34:37 +08001351#ifdef LWS_HAS_PPOLL
1352 lws_idling_ppoll_tid = context->protocols[0].callback(context, NULL,
1353 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1354
1355 timeout_ts.tv_sec = timeout_ms / 1000;
1356 timeout_ts.tv_nsec = timeout_ms % 1000;
1357 sigemptyset(&sigmask);
1358 sigaddset(&sigmask, SIGUSR2);
1359
Andy Green0d338332011-02-12 11:57:43 +00001360 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001361
Andy Green3b3fa9e2013-12-25 16:34:37 +08001362 n = ppoll(context->fds, context->fds_count, &timeout_ts, &sigmask);
1363 lws_idling_ppoll_tid = 0;
1364#else
Peter Hinz56885f32011-03-02 22:03:47 +00001365 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001366#endif
Andy Green5dc62ea2013-09-20 20:26:12 +08001367 if (n == 0) /* poll timeout */ {
1368 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001369 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001370 }
Andy Greene92cd172011-01-19 13:11:55 +00001371
Andy Greendfb23042013-01-17 12:26:48 +08001372 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001373 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001374
Andy Greendfb23042013-01-17 12:26:48 +08001375 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001376
Andy Greenca0a1292013-03-16 11:24:23 +08001377 for (n = 0; n < context->fds_count; n++) {
1378 if (!context->fds[n].revents)
1379 continue;
1380 m = libwebsocket_service_fd(context, &context->fds[n]);
1381 if (m < 0)
1382 return -1;
1383 /* if something closed, retry this slot */
1384 if (m)
1385 n--;
1386 }
1387
Andy Greene92cd172011-01-19 13:11:55 +00001388 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001389}
1390
Andy Green3182ece2013-01-20 17:08:31 +08001391#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001392int
1393lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001394 struct libwebsocket *wsi,
1395 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001396 void *v, size_t len)
1397{
1398 int n;
1399 int handled = 0;
1400
1401 /* maybe an extension will take care of it for us */
1402
1403 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1404 if (!wsi->active_extensions[n]->callback)
1405 continue;
1406
1407 handled |= wsi->active_extensions[n]->callback(context,
1408 wsi->active_extensions[n], wsi,
1409 r, wsi->active_extensions_user[n], v, len);
1410 }
1411
1412 return handled;
1413}
1414
1415
1416void *
1417lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001418 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001419{
1420 int n = 0;
1421
Andy Green68b45042011-05-25 21:41:57 +01001422 if (wsi == NULL)
1423 return NULL;
1424
Andy Greena41314f2011-05-23 10:00:03 +01001425 while (n < wsi->count_active_extensions) {
1426 if (wsi->active_extensions[n] != ext) {
1427 n++;
1428 continue;
1429 }
1430 return wsi->active_extensions_user[n];
1431 }
1432
1433 return NULL;
1434}
Andy Green3182ece2013-01-20 17:08:31 +08001435#endif
Andy Greena41314f2011-05-23 10:00:03 +01001436
Andy Green91f19d82013-12-21 11:18:34 +08001437void
1438lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1439{
1440 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001441 int events;
1442#ifdef LWS_HAS_PPOLL
1443 int tid;
1444 int sampled_ppoll_tid;
1445#endif
Andy Green91f19d82013-12-21 11:18:34 +08001446
1447 context->protocols[0].callback(context, wsi,
1448 LWS_CALLBACK_LOCK_POLL,
1449 wsi->user_space, (void *)(long)wsi->sock, 0);
1450
Andy Green3b3fa9e2013-12-25 16:34:37 +08001451 events = context->fds[wsi->position_in_fds_table].events;
1452
1453 context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001454
1455 /* external POLL support via protocol 0 */
1456 if (_and)
1457 context->protocols[0].callback(context, wsi,
1458 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1459 wsi->user_space, (void *)(long)wsi->sock, _and);
1460
1461 if (_or)
1462 context->protocols[0].callback(context, wsi,
1463 LWS_CALLBACK_SET_MODE_POLL_FD,
1464 wsi->user_space, (void *)(long)wsi->sock, _or);
1465
Andy Green3b3fa9e2013-12-25 16:34:37 +08001466#ifdef LWS_HAS_PPOLL
1467 /*
1468 * if we changed something in this pollfd...
1469 * ... and we're running in a different thread context
1470 * than the service thread...
1471 * ... and the service thread is waiting in ppoll()...
1472 * then fire a SIGUSR2 at the service thread to force it to
1473 * restart the ppoll() with our changed events
1474 */
1475 if (events != context->fds[wsi->position_in_fds_table].events) {
1476 sampled_ppoll_tid = lws_idling_ppoll_tid;
1477 if (sampled_ppoll_tid) {
1478 tid = context->protocols[0].callback(context, NULL,
1479 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1480 if (tid != sampled_ppoll_tid)
1481 kill(sampled_ppoll_tid, SIGUSR2);
1482 }
1483 }
1484#endif
1485
Andy Green91f19d82013-12-21 11:18:34 +08001486 context->protocols[0].callback(context, wsi,
1487 LWS_CALLBACK_UNLOCK_POLL,
1488 wsi->user_space, (void *)(long)wsi->sock, 0);
1489}
1490
1491
Andy Green90c7cbc2011-01-27 06:26:52 +00001492/**
1493 * libwebsocket_callback_on_writable() - Request a callback when this socket
1494 * becomes able to be written to without
1495 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001496 *
Peter Hinz56885f32011-03-02 22:03:47 +00001497 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001498 * @wsi: Websocket connection instance to get callback for
1499 */
1500
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001501LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001502libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001503 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001504{
Andy Green3182ece2013-01-20 17:08:31 +08001505#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001506 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001507 int handled = 0;
1508
1509 /* maybe an extension will take care of it for us */
1510
1511 for (n = 0; n < wsi->count_active_extensions; n++) {
1512 if (!wsi->active_extensions[n]->callback)
1513 continue;
1514
1515 handled |= wsi->active_extensions[n]->callback(context,
1516 wsi->active_extensions[n], wsi,
1517 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1518 wsi->active_extensions_user[n], NULL, 0);
1519 }
1520
1521 if (handled)
1522 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001523#endif
Andy Greendfb23042013-01-17 12:26:48 +08001524 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001525 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1526 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001527 return -1;
1528 }
1529
Andy Green91f19d82013-12-21 11:18:34 +08001530 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001531
Andy Green90c7cbc2011-01-27 06:26:52 +00001532 return 1;
1533}
1534
1535/**
1536 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1537 * all connections using the given protocol when it
1538 * becomes possible to write to each socket without
1539 * blocking in turn.
1540 *
1541 * @protocol: Protocol whose connections will get callbacks
1542 */
1543
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001544LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001545libwebsocket_callback_on_writable_all_protocol(
1546 const struct libwebsocket_protocols *protocol)
1547{
Peter Hinz56885f32011-03-02 22:03:47 +00001548 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001549 int n;
Andy Green0d338332011-02-12 11:57:43 +00001550 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001551
Andy Greendfb23042013-01-17 12:26:48 +08001552 for (n = 0; n < context->fds_count; n++) {
1553 wsi = context->lws_lookup[context->fds[n].fd];
1554 if (!wsi)
1555 continue;
1556 if (wsi->protocol == protocol)
1557 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001558 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001559
1560 return 0;
1561}
1562
Andy Greenbe93fef2011-02-14 20:25:43 +00001563/**
1564 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1565 *
1566 * You will not need this unless you are doing something special
1567 *
1568 * @wsi: Websocket connection instance
1569 * @reason: timeout reason
1570 * @secs: how many seconds
1571 */
1572
1573void
1574libwebsocket_set_timeout(struct libwebsocket *wsi,
1575 enum pending_timeout reason, int secs)
1576{
1577 struct timeval tv;
1578
1579 gettimeofday(&tv, NULL);
1580
1581 wsi->pending_timeout_limit = tv.tv_sec + secs;
1582 wsi->pending_timeout = reason;
1583}
1584
Andy Greena6cbece2011-01-27 20:06:03 +00001585
1586/**
1587 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1588 *
1589 * You will not need this unless you are doing something special
1590 *
1591 * @wsi: Websocket connection instance
1592 */
1593
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001594LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001595libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1596{
1597 return wsi->sock;
1598}
1599
Andy Greend636e352013-01-29 12:36:17 +08001600#ifdef LWS_LATENCY
1601void
Andy Greenb5b23192013-02-11 17:13:32 +08001602lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1603 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001604{
1605 struct timeval tv;
1606 unsigned long u;
1607 char buf[256];
1608
1609 gettimeofday(&tv, NULL);
1610
1611 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1612
1613 if (action) {
1614 if (completed) {
1615 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001616 sprintf(buf,
1617 "Completion first try lat %luus: %p: ret %d: %s\n",
1618 u - wsi->latency_start,
1619 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001620 else
Andy Greenb5b23192013-02-11 17:13:32 +08001621 sprintf(buf,
1622 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1623 u - wsi->action_start,
1624 u - wsi->latency_start,
1625 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001626 wsi->action_start = 0;
1627 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001628 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1629 u - wsi->latency_start,
1630 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001631 if (u - wsi->latency_start > context->worst_latency) {
1632 context->worst_latency = u - wsi->latency_start;
1633 strcpy(context->worst_latency_info, buf);
1634 }
1635 lwsl_latency("%s", buf);
1636 } else {
1637 wsi->latency_start = u;
1638 if (!wsi->action_start)
1639 wsi->action_start = u;
1640 }
1641}
1642#endif
1643
Andy Greena1ce6be2013-01-18 11:43:21 +08001644#ifdef LWS_NO_SERVER
1645int
Andy Green8d15cf42013-10-24 21:47:06 +08001646_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001647{
1648 return 0;
1649}
1650#else
Andy Green706961d2013-01-17 16:50:35 +08001651int
1652_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1653{
1654 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001655
Andy Greenca0a1292013-03-16 11:24:23 +08001656 /* there is no pending change */
1657 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001658 return 0;
1659
Andy Greenca0a1292013-03-16 11:24:23 +08001660 /* stuff is still buffered, not ready to really accept new input */
1661 if (wsi->u.ws.rxflow_buffer) {
1662 /* get ourselves called back to deal with stashed buffer */
1663 libwebsocket_callback_on_writable(context, wsi);
1664 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001665 }
1666
Andy Greenca0a1292013-03-16 11:24:23 +08001667 /* pending is cleared, we can change rxflow state */
1668
1669 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1670
1671 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1672 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1673
1674 /* adjust the pollfd for this wsi */
1675
1676 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001677 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001678 else
Andy Green91f19d82013-12-21 11:18:34 +08001679 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001680
Andy Green706961d2013-01-17 16:50:35 +08001681 return 1;
1682}
Andy Greena1ce6be2013-01-18 11:43:21 +08001683#endif
Andy Green706961d2013-01-17 16:50:35 +08001684
Andy Green90c7cbc2011-01-27 06:26:52 +00001685/**
1686 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1687 * receieved packets.
1688 *
1689 * If the output side of a server process becomes choked, this allows flow
1690 * control for the input side.
1691 *
1692 * @wsi: Websocket connection instance to get callback for
1693 * @enable: 0 = disable read servicing for this connection, 1 = enable
1694 */
1695
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001696LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001697libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1698{
Andy Greenca0a1292013-03-16 11:24:23 +08001699 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1700 return 0;
1701
1702 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1703 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001704
Andy Green706961d2013-01-17 16:50:35 +08001705 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001706}
1707
Andy Greenb55451c2013-03-16 12:32:27 +08001708/**
1709 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1710 *
1711 * When the user server code realizes it can accept more input, it can
1712 * call this to have the RX flow restriction removed from all connections using
1713 * the given protocol.
1714 *
1715 * @protocol: all connections using this protocol will be allowed to receive
1716 */
1717
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001718LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001719libwebsocket_rx_flow_allow_all_protocol(
1720 const struct libwebsocket_protocols *protocol)
1721{
1722 struct libwebsocket_context *context = protocol->owning_server;
1723 int n;
1724 struct libwebsocket *wsi;
1725
1726 for (n = 0; n < context->fds_count; n++) {
1727 wsi = context->lws_lookup[context->fds[n].fd];
1728 if (!wsi)
1729 continue;
1730 if (wsi->protocol == protocol)
1731 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1732 }
1733}
1734
Andy Green706961d2013-01-17 16:50:35 +08001735
Andy Green2ac5a6f2011-01-28 10:00:18 +00001736/**
1737 * libwebsocket_canonical_hostname() - returns this host's hostname
1738 *
1739 * This is typically used by client code to fill in the host parameter
1740 * when making a client connection. You can only call it after the context
1741 * has been created.
1742 *
Peter Hinz56885f32011-03-02 22:03:47 +00001743 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001744 */
1745
1746
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001747LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001748libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001749{
Peter Hinz56885f32011-03-02 22:03:47 +00001750 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001751}
1752
1753
Andy Green90c7cbc2011-01-27 06:26:52 +00001754static void sigpipe_handler(int x)
1755{
1756}
1757
Andy Green6901cb32011-02-21 08:06:47 +00001758#ifdef LWS_OPENSSL_SUPPORT
1759static int
1760OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1761{
1762
1763 SSL *ssl;
1764 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001765 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001766
1767 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1768 SSL_get_ex_data_X509_STORE_CTX_idx());
1769
1770 /*
Andy Green2e24da02011-03-05 16:12:04 +00001771 * !!! nasty openssl requires the index to come as a library-scope
1772 * static
Andy Green6901cb32011-02-21 08:06:47 +00001773 */
Andy Green2e24da02011-03-05 16:12:04 +00001774 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001775
Peter Hinz56885f32011-03-02 22:03:47 +00001776 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001777 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1778 x509_ctx, ssl, preverify_ok);
1779
1780 /* convert return code from 0 = OK to 1 = OK */
1781
1782 if (!n)
1783 n = 1;
1784 else
1785 n = 0;
1786
1787 return n;
1788}
1789#endif
1790
Andy Green706961d2013-01-17 16:50:35 +08001791int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001792 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001793 struct libwebsocket *wsi,
1794 enum libwebsocket_callback_reasons reason, void *user,
1795 void *in, size_t len)
1796{
1797 int n;
1798
1799 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001800 if (!n)
1801 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001802
Andy Greenaedc9532013-02-10 21:21:24 +08001803 return n;
Andy Green706961d2013-01-17 16:50:35 +08001804}
1805
Andy Green3b3fa9e2013-12-25 16:34:37 +08001806/*
1807 * This is just used to interrupt poll waiting
1808 * we don't have to do anything with it.
1809 */
1810static void lws_sigusr2(int sig)
1811{
1812}
Andy Greenb45993c2010-12-18 15:13:50 +00001813
Andy Greenab990e42010-10-31 12:42:52 +00001814/**
Andy Green4739e5c2011-01-22 12:51:57 +00001815 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001816 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001817 *
Andy Green1b265272013-02-09 14:01:09 +08001818 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001819 * of all initialization in one step.
1820 *
Andy Greene92cd172011-01-19 13:11:55 +00001821 * After initialization, it returns a struct libwebsocket_context * that
1822 * represents this server. After calling, user code needs to take care
1823 * of calling libwebsocket_service() with the context pointer to get the
1824 * server's sockets serviced. This can be done in the same process context
1825 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001826 *
Andy Green8f037e42010-12-19 22:13:26 +00001827 * The protocol callback functions are called for a handful of events
1828 * including http requests coming in, websocket connections becoming
1829 * established, and data arriving; it's also called periodically to allow
1830 * async transmission.
1831 *
1832 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1833 * at that time websocket protocol has not been negotiated. Other
1834 * protocols after the first one never see any HTTP callack activity.
1835 *
1836 * The server created is a simple http server by default; part of the
1837 * websocket standard is upgrading this http connection to a websocket one.
1838 *
1839 * This allows the same server to provide files like scripts and favicon /
1840 * images or whatever over http and dynamic data over websockets all in
1841 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001842 */
Andy Green4ea60062010-10-30 12:15:07 +01001843
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001844LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001845libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001846{
Peter Hinz56885f32011-03-02 22:03:47 +00001847 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001848 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001849 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001850#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001851 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001852 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001853 struct sockaddr_in serv_addr;
1854#endif
Andy Green3182ece2013-01-20 17:08:31 +08001855#ifndef LWS_NO_EXTENSIONS
1856 int m;
Andy Green1b265272013-02-09 14:01:09 +08001857 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001858#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001859
Andy Green3faa9c72010-11-08 17:03:03 +00001860#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001861 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001862#endif
1863
Joakim Soderberg4c531232013-02-06 15:26:58 +09001864#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001865 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001866#endif
1867
Andy Greenb3a614a2013-01-19 13:08:17 +08001868 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001869 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001870 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001871 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001872#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001873 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1874 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001875#else
1876 lwsl_notice(" Configured without extension support\n");
1877#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001878 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1879 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001880 if (info->ssl_cipher_list)
1881 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001882 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1883 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001884
Peter Hinz56885f32011-03-02 22:03:47 +00001885#ifdef _WIN32
1886 {
1887 WORD wVersionRequested;
1888 WSADATA wsaData;
1889 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001890 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001891
1892 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1893 wVersionRequested = MAKEWORD(2, 2);
1894
1895 err = WSAStartup(wVersionRequested, &wsaData);
1896 if (err != 0) {
1897 /* Tell the user that we could not find a usable */
1898 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001899 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001900 return NULL;
1901 }
David Galeano7b11fec2011-10-04 19:55:18 +08001902
Andy Green6ee372f2012-04-09 15:09:01 +08001903 /* default to a poll() made out of select() */
1904 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001905
Andy Green6ee372f2012-04-09 15:09:01 +08001906 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001907 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001908 if (wsdll)
1909 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001910
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001911 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001912 if (!poll)
1913 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001914 }
1915#endif
1916
Andy Greenb5b23192013-02-11 17:13:32 +08001917 context = (struct libwebsocket_context *)
1918 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001919 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001920 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001921 return NULL;
1922 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001923 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001924#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001925 context->started_with_parent = pid_daemon;
1926 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1927#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001928
Joakim Soderberg4c531232013-02-06 15:26:58 +09001929 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001930 context->protocols = info->protocols;
1931 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001932 context->http_proxy_port = 0;
1933 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001934 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001935 /* to reduce this allocation, */
1936 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001937 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1938 sizeof(struct libwebsocket_context),
1939 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1940 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001941 sizeof(struct libwebsocket_context) +
1942 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1943 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001944
Andy Greenb5b23192013-02-11 17:13:32 +08001945 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1946 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001947 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001948 lwsl_err("Unable to allocate fds array for %d connections\n",
1949 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001950 free(context);
1951 return NULL;
1952 }
Andy Greenb5b23192013-02-11 17:13:32 +08001953 context->lws_lookup = (struct libwebsocket **)
1954 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001955 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001956 lwsl_err(
1957 "Unable to allocate lws_lookup array for %d connections\n",
1958 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001959 free(context->fds);
1960 free(context);
1961 return NULL;
1962 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001963 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
1964 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001965
Peter Hinz56885f32011-03-02 22:03:47 +00001966 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001967#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001968 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001969#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001970 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001971 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001972
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001973#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001974 context->fd_random = 0;
1975#else
1976 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1977 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001978 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001979 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001980 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001981 }
Peter Hinz56885f32011-03-02 22:03:47 +00001982#endif
Andy Green44eee682011-02-10 09:32:24 +00001983
Peter Hinz56885f32011-03-02 22:03:47 +00001984#ifdef LWS_OPENSSL_SUPPORT
1985 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08001986 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001987 context->ssl_ctx = NULL;
1988 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001989 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001990#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001991
Andy Greena1ce6be2013-01-18 11:43:21 +08001992 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001993
Andy Greena1ce6be2013-01-18 11:43:21 +08001994#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001995 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001996 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001997 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001998 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001999
Andy Greenb5b23192013-02-11 17:13:32 +08002000 lwsl_notice(" canonical_hostname = %s\n",
2001 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002002 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002003#endif
Andy Greena69f0512012-05-03 12:32:38 +08002004
Andy Green9659f372011-01-27 22:01:43 +00002005 /* split the proxy ads:port if given */
2006
2007 p = getenv("http_proxy");
2008 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00002009 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08002010 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002011 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002012 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002013
Peter Hinz56885f32011-03-02 22:03:47 +00002014 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00002015 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002016 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002017 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00002018 }
2019 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00002020 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002021
Andy Greenb3a614a2013-01-19 13:08:17 +08002022 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002023 context->http_proxy_address,
2024 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002025 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002026
Andy Greena1ce6be2013-01-18 11:43:21 +08002027#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002028 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002029
Andy Green3faa9c72010-11-08 17:03:03 +00002030#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002031 context->use_ssl = info->ssl_cert_filepath != NULL &&
2032 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002033#ifdef USE_CYASSL
2034 lwsl_notice(" Compiled with CYASSL support\n");
2035#else
2036 lwsl_notice(" Compiled with OpenSSL support\n");
2037#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002038 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002039 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002040 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002041 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002042
Andy Green90c7cbc2011-01-27 06:26:52 +00002043#else
Andy Green2b40b792013-02-10 22:22:01 +08002044 if (info->ssl_cert_filepath != NULL &&
2045 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002046 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002047 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002048 }
Andy Greenb5b23192013-02-11 17:13:32 +08002049 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002050#endif
Andy Greena17c6922013-01-20 20:21:54 +08002051
Andy Greenb5b23192013-02-11 17:13:32 +08002052 lwsl_notice(
2053 " per-conn mem: %u + %u headers + protocol rx buf\n",
2054 sizeof(struct libwebsocket),
2055 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002056 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002057#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002058
2059 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002060#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002061#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002062 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002063#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002064
2065
2066#ifdef LWS_OPENSSL_SUPPORT
2067
2068 /* basic openssl init */
2069
2070 SSL_library_init();
2071
2072 OpenSSL_add_all_algorithms();
2073 SSL_load_error_strings();
2074
Andy Green2e24da02011-03-05 16:12:04 +00002075 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002076 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2077
Andy Green90c7cbc2011-01-27 06:26:52 +00002078 /*
2079 * Firefox insists on SSLv23 not SSLv3
2080 * Konq disables SSLv2 by default now, SSLv23 works
2081 */
2082
2083 method = (SSL_METHOD *)SSLv23_server_method();
2084 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002085 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002086 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002087 error,
2088 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002089 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002090 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002091 }
Peter Hinz56885f32011-03-02 22:03:47 +00002092 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2093 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002094 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002095 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002096 error,
2097 ERR_error_string(error,
2098 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002099 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002100 }
2101
Andy Green3b3fa9e2013-12-25 16:34:37 +08002102 signal(SIGUSR2, lws_sigusr2);
2103
David Galeanocc148e42013-01-10 10:18:59 +08002104#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002105 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002106#endif
David Galeano77a677c2013-01-10 10:14:12 +08002107 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002108 if (info->ssl_cipher_list)
2109 SSL_CTX_set_cipher_list(context->ssl_ctx,
2110 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002111
Andy Greena1ce6be2013-01-18 11:43:21 +08002112#ifndef LWS_NO_CLIENT
2113
Andy Green90c7cbc2011-01-27 06:26:52 +00002114 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002115
Andy Green1b265272013-02-09 14:01:09 +08002116 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002117 method = (SSL_METHOD *)SSLv23_client_method();
2118 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002119 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002120 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002121 error,
2122 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002123 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002124 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002125 }
2126 /* create context */
2127 context->ssl_client_ctx = SSL_CTX_new(method);
2128 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002129 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002130 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002131 error,
2132 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002133 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002134 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002135 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002136
David Galeanocc148e42013-01-10 10:18:59 +08002137#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002138 SSL_CTX_set_options(context->ssl_client_ctx,
2139 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002140#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002141 SSL_CTX_set_options(context->ssl_client_ctx,
2142 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002143 if (info->ssl_cipher_list)
2144 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2145 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002146
Peter Hinz56885f32011-03-02 22:03:47 +00002147 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002148 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002149 if (!SSL_CTX_load_verify_locations(
2150 context->ssl_client_ctx, NULL,
2151 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002152 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002153 "Unable to load SSL Client certs from %s "
2154 "(set by --with-client-cert-dir= "
2155 "in configure) -- client ssl isn't "
2156 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002157 } else
2158 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002159 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002160 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002161 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002162 "Unable to load SSL Client certs "
2163 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002164 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002165
2166 /*
2167 * callback allowing user code to load extra verification certs
2168 * helping the client to verify server identity
2169 */
2170
prasannateamf1b353a452013-11-12 17:21:01 -08002171 /* support for client-side certificate authentication */
2172 if (info->ssl_cert_filepath) {
2173 n = SSL_CTX_use_certificate_chain_file(
2174 context->ssl_client_ctx,
2175 info->ssl_cert_filepath);
2176 if (n != 1) {
2177 lwsl_err("problem getting cert '%s' %lu: %s\n",
2178 info->ssl_cert_filepath,
2179 ERR_get_error(),
2180 ERR_error_string(ERR_get_error(),
2181 (char *)context->service_buffer));
2182 goto bail;
2183 }
2184 }
2185 if (info->ssl_private_key_filepath) {
2186 /* set the private key from KeyFile */
2187 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2188 info->ssl_private_key_filepath,
2189 SSL_FILETYPE_PEM) != 1) {
2190 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2191 info->ssl_private_key_filepath,
2192 ERR_get_error(),
2193 ERR_error_string(ERR_get_error(),
2194 (char *)context->service_buffer));
2195 goto bail;
2196 }
2197
2198 /* verify private key */
2199 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2200 lwsl_err("Private SSL key doesn't match cert\n");
2201 goto bail;
2202 }
2203 }
2204
Peter Hinz56885f32011-03-02 22:03:47 +00002205 context->protocols[0].callback(context, NULL,
2206 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2207 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002208 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002209#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002210
Andy Greenc6bf2c22011-02-20 11:10:47 +00002211 /* as a server, are we requiring clients to identify themselves? */
2212
Andy Greenb5b23192013-02-11 17:13:32 +08002213 if (info->options &
2214 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002215
2216 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002217
Peter Hinz56885f32011-03-02 22:03:47 +00002218 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002219 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2220 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002221
2222 /*
2223 * give user code a chance to load certs into the server
2224 * allowing it to verify incoming client certs
2225 */
2226
Peter Hinz56885f32011-03-02 22:03:47 +00002227 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002228 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002229 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002230 }
2231
James Devine5b34c972013-12-14 11:41:29 +08002232 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2233 /* Normally SSL listener rejects non-ssl, optionally allow */
2234 context->allow_non_ssl_on_ssl_port = 1;
2235 }
2236
Peter Hinz56885f32011-03-02 22:03:47 +00002237 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002238
2239 /* openssl init for server sockets */
2240
Andy Green3faa9c72010-11-08 17:03:03 +00002241 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002242 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002243 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002244 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002245 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002246 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002247 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002248 error,
2249 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002250 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002251 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002252 }
2253 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002254 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002255 info->ssl_private_key_filepath,
2256 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002257 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002258 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002259 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002260 error,
2261 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002262 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002263 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002264 }
2265 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002266 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002267 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002268 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002269 }
2270
2271 /* SSL is happy and has a cert it's content with */
2272 }
2273#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002274
Andy Greena1ce6be2013-01-18 11:43:21 +08002275#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002276 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002277
Andy Green1b265272013-02-09 14:01:09 +08002278 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002279 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002280
Andy Green4739e5c2011-01-22 12:51:57 +00002281 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2282 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002283 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002284 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002285 }
Andy Green775c0dd2010-10-29 14:15:22 +01002286
Joakim Soderberg4c531232013-02-06 15:26:58 +09002287#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002288 /*
2289 * allow us to restart even if old sockets in TIME_WAIT
2290 * (REUSEADDR on Unix means, "don't hang on to this
2291 * address after the listener is closed." On Windows, though,
2292 * it means "don't keep other processes from binding to
2293 * this address while we're using it)
2294 */
Andy Green6ee372f2012-04-09 15:09:01 +08002295 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2296 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002297#endif
Andy Green6c939552011-03-08 08:56:57 +00002298
2299 /* Disable Nagle */
2300 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002301 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2302 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002303
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002304 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002305 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002306 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002307 #else
Andy Greene2160712013-01-28 12:19:10 +08002308 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002309 #endif
Andy Greene2160712013-01-28 12:19:10 +08002310
Andy Green4739e5c2011-01-22 12:51:57 +00002311 bzero((char *) &serv_addr, sizeof(serv_addr));
2312 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002313 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002314 serv_addr.sin_addr.s_addr = INADDR_ANY;
2315 else
Andy Greend1eac602013-11-09 08:07:38 +08002316 if (interface_to_sa(info->iface, &serv_addr,
2317 sizeof(serv_addr)) < 0) {
2318 lwsl_err("Unable to find interface %s\n",
2319 info->iface);
2320 compatible_close(sockfd);
2321 goto bail;
2322 }
Andy Green1b265272013-02-09 14:01:09 +08002323 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002324
2325 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2326 sizeof(serv_addr));
2327 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002328 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002329 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002330 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002331 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002332 }
Andy Green0d338332011-02-12 11:57:43 +00002333
Andy Greenb5b23192013-02-11 17:13:32 +08002334 wsi = (struct libwebsocket *)malloc(
2335 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002336 if (wsi == NULL) {
2337 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002338 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002339 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002340 }
Andy Greenb5b23192013-02-11 17:13:32 +08002341 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002342 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002343#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002344 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002345#endif
Andy Green0d338332011-02-12 11:57:43 +00002346 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002347
2348 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002349
Andy Green65b0e912013-01-16 07:59:47 +08002350 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2351 context->listen_service_count = 0;
2352 context->listen_service_fd = sockfd;
2353
Andy Greena824d182013-01-15 20:52:29 +08002354 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002355 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002356 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002357#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002358
Andy Green6ee372f2012-04-09 15:09:01 +08002359 /*
2360 * drop any root privs for this process
2361 * to listen on port < 1023 we would have needed root, but now we are
2362 * listening, we don't want the power for anything else
2363 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002364#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002365#else
Andy Green1b265272013-02-09 14:01:09 +08002366 if (info->gid != -1)
2367 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002368 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002369 if (info->uid != -1)
2370 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002371 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002372#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002373
Andy Green6f520a52013-01-29 17:57:39 +08002374 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002375
Peter Hinz56885f32011-03-02 22:03:47 +00002376 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002377 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002378 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002379
Andy Green43db0452013-01-10 19:50:35 +08002380 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002381 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002382
Andy Green1b265272013-02-09 14:01:09 +08002383 info->protocols[context->count_protocols].owning_server =
2384 context;
2385 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002386 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002387
2388 /*
2389 * inform all the protocols that they are doing their one-time
2390 * initialization if they want to
2391 */
2392 info->protocols[context->count_protocols].callback(context,
2393 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002394 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002395
Andy Green3182ece2013-01-20 17:08:31 +08002396#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002397 /*
2398 * give all extensions a chance to create any per-context
2399 * allocations they need
2400 */
2401
2402 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002403 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002404 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002405
Andy Green1b265272013-02-09 14:01:09 +08002406 if (info->extensions) {
2407 ext = info->extensions;
2408 while (ext->callback) {
2409 lwsl_ext(" Extension: %s\n", ext->name);
2410 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002411 (enum libwebsocket_extension_callback_reasons)m,
2412 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002413 ext++;
2414 }
Andy Greena41314f2011-05-23 10:00:03 +01002415 }
Andy Green3182ece2013-01-20 17:08:31 +08002416#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002417
Peter Hinz56885f32011-03-02 22:03:47 +00002418 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002419
2420bail:
2421 libwebsocket_context_destroy(context);
2422 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002423}
Andy Greenb45993c2010-12-18 15:13:50 +00002424
Andy Greenb45993c2010-12-18 15:13:50 +00002425/**
shysb4e800e2013-10-24 22:12:03 +08002426 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2427 * @context: pointer to struct libwebsocket_context you want set proxy to
2428 * @proxy: pointer to c string containing proxy in format address:port
2429 *
2430 * Returns 0 if proxy string was parsed and proxy was setup.
2431 * Returns -1 if @proxy is NULL or has incorrect format.
2432 *
2433 * This is only required if your OS does not provide the http_proxy
2434 * enviroment variable (eg, OSX)
2435 *
2436 * IMPORTANT! You should call this function right after creation of the
2437 * libwebsocket_context and before call to connect. If you call this
2438 * function after connect behavior is undefined.
2439 * This function will override proxy settings made on libwebsocket_context
2440 * creation with genenv() call.
2441 */
2442
2443LWS_VISIBLE int
2444libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2445{
2446 char *p;
2447
2448 if (!proxy)
2449 return -1;
2450
2451 strncpy(context->http_proxy_address, proxy,
2452 sizeof(context->http_proxy_address) - 1);
2453 context->http_proxy_address[
2454 sizeof(context->http_proxy_address) - 1] = '\0';
2455
2456 p = strchr(context->http_proxy_address, ':');
2457 if (!p) {
2458 lwsl_err("http_proxy needs to be ads:port\n");
2459
2460 return -1;
2461 }
2462 *p = '\0';
2463 context->http_proxy_port = atoi(p + 1);
2464
2465 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2466 context->http_proxy_port);
2467
2468 return 0;
2469}
2470
2471/**
Andy Greenb45993c2010-12-18 15:13:50 +00002472 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002473 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002474 * @wsi: pointer to struct websocket you want to know the protocol of
2475 *
Andy Green8f037e42010-12-19 22:13:26 +00002476 *
Andy Green6f520a52013-01-29 17:57:39 +08002477 * Some apis can act on all live connections of a given protocol,
2478 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002479 */
Andy Greenab990e42010-10-31 12:42:52 +00002480
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002481LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002482libwebsockets_get_protocol(struct libwebsocket *wsi)
2483{
2484 return wsi->protocol;
2485}
2486
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002487LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002488libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2489{
Andy Green623a98d2013-01-21 11:04:23 +08002490 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002491}
Alex Bligh49146db2011-11-07 17:19:25 +08002492
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002493LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002494libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2495{
Andy Green623a98d2013-01-21 11:04:23 +08002496 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002497}
2498
Andy Green2af4d5b2013-02-18 16:30:10 +08002499int
Alex Bligh49146db2011-11-07 17:19:25 +08002500libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2501{
Andy Green67d556c2013-02-15 22:31:55 +08002502 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002503 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002504
Alex Bligh49146db2011-11-07 17:19:25 +08002505 /* allocate the per-connection user memory (if any) */
2506
2507 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2508 wsi->user_space = malloc(
2509 wsi->protocol->per_session_data_size);
2510 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002511 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002512 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002513 }
Andy Green6ee372f2012-04-09 15:09:01 +08002514 memset(wsi->user_space, 0,
2515 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002516 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002517 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002518}
Andy Green43db0452013-01-10 19:50:35 +08002519
Andy Green0b319092013-01-19 11:17:56 +08002520static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002521{
Andy Green0b319092013-01-19 11:17:56 +08002522 char buf[300];
2523 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002524 int n;
2525
2526 gettimeofday(&tv, NULL);
2527
2528 buf[0] = '\0';
2529 for (n = 0; n < LLL_COUNT; n++)
2530 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002531 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002532 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002533 break;
2534 }
Andy Greenb5b23192013-02-11 17:13:32 +08002535
Andy Green0b319092013-01-19 11:17:56 +08002536 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002537}
2538
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002539#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002540LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002541{
2542 lwsl_emit_stderr(level, line);
2543}
2544#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002545LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002546{
2547 int syslog_level = LOG_DEBUG;
2548
2549 switch (level) {
2550 case LLL_ERR:
2551 syslog_level = LOG_ERR;
2552 break;
2553 case LLL_WARN:
2554 syslog_level = LOG_WARNING;
2555 break;
2556 case LLL_NOTICE:
2557 syslog_level = LOG_NOTICE;
2558 break;
2559 case LLL_INFO:
2560 syslog_level = LOG_INFO;
2561 break;
2562 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002563 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002564}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002565#endif
Andy Greenc11db202013-01-19 11:12:16 +08002566
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002567LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002568{
Andy Greende8f27a2013-01-12 09:17:42 +08002569 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002570 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002571
2572 if (!(log_level & filter))
2573 return;
2574
Andy Green43db0452013-01-10 19:50:35 +08002575 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002576 vsnprintf(buf, sizeof(buf), format, ap);
2577 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002578 va_end(ap);
2579
Andy Green0b319092013-01-19 11:17:56 +08002580 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002581}
2582
2583/**
2584 * lws_set_log_level() - Set the logging bitfield
2585 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002586 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2587 * function to perform log string emission instead of
2588 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002589 *
Andy Greenc6511a02013-02-19 10:01:48 +08002590 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002591 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002592 */
2593
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002594LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002595 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002596{
2597 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002598 if (log_emit_function)
2599 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002600}