blob: 1542deaebd11e78acc1a605d1fc1abd6b8e8ae43 [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{
Joakim Soderberg6596e4b2014-02-18 13:38:14 +0100117 struct libwebsocket_pollargs pa = { wsi->sock, POLLIN, 0 };
118
Andy Greendfb23042013-01-17 12:26:48 +0800119 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800120 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000121 return 1;
122 }
123
nononamef1624922014-01-11 13:12:34 +0800124 if (wsi->sock >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800125 lwsl_err("Socket fd %d is too high (%d)\n",
126 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800127 return 1;
128 }
129
130 assert(wsi);
Andy Green512c2462013-09-18 12:59:33 +0800131 assert(wsi->sock >= 0);
Andy Greendfb23042013-01-17 12:26:48 +0800132
Andy Greenb5b23192013-02-11 17:13:32 +0800133 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
134 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800135
Andy Green7a132792013-12-18 09:48:26 +0800136 context->protocols[0].callback(context, wsi,
137 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800138 wsi->user_space, (void *) &pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800139
Andy Greendfb23042013-01-17 12:26:48 +0800140 context->lws_lookup[wsi->sock] = wsi;
141 wsi->position_in_fds_table = context->fds_count;
142 context->fds[context->fds_count].fd = wsi->sock;
143 context->fds[context->fds_count].events = POLLIN;
144 context->fds[context->fds_count++].revents = 0;
145
146 /* external POLL support via protocol 0 */
147 context->protocols[0].callback(context, wsi,
148 LWS_CALLBACK_ADD_POLL_FD,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800149 wsi->user_space, (void *) &pa, 0);
Andy Green0d338332011-02-12 11:57:43 +0000150
Andy Green7a132792013-12-18 09:48:26 +0800151 context->protocols[0].callback(context, wsi,
152 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800153 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800154
Andy Green0d338332011-02-12 11:57:43 +0000155 return 0;
156}
157
Andy Greendfb23042013-01-17 12:26:48 +0800158static int
Andy Greenb5b23192013-02-11 17:13:32 +0800159remove_wsi_socket_from_fds(struct libwebsocket_context *context,
160 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000161{
Andy Greendfb23042013-01-17 12:26:48 +0800162 int m;
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800163 struct libwebsocket_pollargs pa = { wsi->sock, 0, 0};
Andy Green0d338332011-02-12 11:57:43 +0000164
Andy Green7a132792013-12-18 09:48:26 +0800165 if (!--context->fds_count) {
166 context->protocols[0].callback(context, wsi,
167 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800168 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800169 goto do_ext;
Andy Green7a132792013-12-18 09:48:26 +0800170 }
Andy Green0d338332011-02-12 11:57:43 +0000171
Andy Greendfb23042013-01-17 12:26:48 +0800172 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800173 lwsl_err("Socket fd %d too high (%d)\n",
174 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800175 return 1;
176 }
Andy Green0d338332011-02-12 11:57:43 +0000177
Andy Greenb5b23192013-02-11 17:13:32 +0800178 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
179 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800180
Andy Green7a132792013-12-18 09:48:26 +0800181 context->protocols[0].callback(context, wsi,
182 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800183 wsi->user_space, (void *)&pa, 0);
Andy Green7a132792013-12-18 09:48:26 +0800184
Andy Greendfb23042013-01-17 12:26:48 +0800185 m = wsi->position_in_fds_table; /* replace the contents for this */
186
187 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800188 context->fds[m] = context->fds[context->fds_count];
189 /*
190 * end guy's fds_lookup entry remains unchanged
191 * (still same fd pointing to same wsi)
192 */
Andy Greendfb23042013-01-17 12:26:48 +0800193 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800194 context->lws_lookup[context->fds[context->fds_count].fd]->
195 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800196 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800197 context->lws_lookup[wsi->sock] = NULL;
198 /* removed wsi has no position any more */
199 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800200
201do_ext:
202 /* remove also from external POLL support via protocol 0 */
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800203 if (wsi->sock) {
Andy Greendfb23042013-01-17 12:26:48 +0800204 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800205 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800206 (void *) &pa, 0);
207 }
Andy Green7a132792013-12-18 09:48:26 +0800208 context->protocols[0].callback(context, wsi,
Michael Haberlerffbc02f2014-02-15 20:19:26 +0800209 LWS_CALLBACK_UNLOCK_POLL,
210 wsi->user_space, (void *) &pa, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800211 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000212}
213
Andy Green32375b72011-02-19 08:32:53 +0000214
Andy Green8f037e42010-12-19 22:13:26 +0000215void
Peter Hinz56885f32011-03-02 22:03:47 +0000216libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000217 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000218{
Andy Greenb45993c2010-12-18 15:13:50 +0000219 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000220 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000221 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
222 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800223#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000224 int ret;
225 int m;
226 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100227 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800228#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000229
Andy Green4b6fbe12011-02-14 08:03:48 +0000230 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000231 return;
232
Andy Green62c54d22011-02-14 09:14:25 +0000233 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000234
Andy Green62c54d22011-02-14 09:14:25 +0000235 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000236 return;
237
Andy Green22524a62013-02-15 22:48:58 +0800238 /* we tried the polite way... */
239 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
240 goto just_kill_connection;
241
Andy Green623a98d2013-01-21 11:04:23 +0800242 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000243
Andy Green5dc62ea2013-09-20 20:26:12 +0800244 if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT ||
245 wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) {
246
247 context->protocols[0].callback(context, wsi,
248 LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0);
249
250 free(wsi->u.hdr.ah);
251 goto just_kill_connection;
252 }
253
254
kapejodce64fb02013-11-19 13:38:16 +0100255 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) {
256 if (wsi->u.http.post_buffer) {
257 free(wsi->u.http.post_buffer);
258 wsi->u.http.post_buffer = NULL;
259 }
260 if (wsi->u.http.fd >= 0) {
261 lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
262 close(wsi->u.http.fd);
263 wsi->u.http.fd = -1;
264 context->protocols[0].callback(context, wsi,
265 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
266 }
Andy Greenb8b247d2013-01-22 07:20:08 +0800267 }
268
Andy Green3182ece2013-01-20 17:08:31 +0800269#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000270 /*
Andy Green68b45042011-05-25 21:41:57 +0100271 * are his extensions okay with him closing? Eg he might be a mux
272 * parent and just his ch1 aspect is closing?
273 */
274
Andy Green68b45042011-05-25 21:41:57 +0100275 for (n = 0; n < wsi->count_active_extensions; n++) {
276 if (!wsi->active_extensions[n]->callback)
277 continue;
278
279 m = wsi->active_extensions[n]->callback(context,
280 wsi->active_extensions[n], wsi,
281 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
282 wsi->active_extensions_user[n], NULL, 0);
283
284 /*
285 * if somebody vetoed actually closing him at this time....
286 * up to the extension to track the attempted close, let's
287 * just bail
288 */
289
290 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800291 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100292 return;
293 }
294 }
295
Andy Green68b45042011-05-25 21:41:57 +0100296 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000297 * flush any tx pending from extensions, since we may send close packet
298 * if there are problems with send, just nuke the connection
299 */
300
301 ret = 1;
302 while (ret == 1) {
303
304 /* default to nobody has more to spill */
305
306 ret = 0;
307 eff_buf.token = NULL;
308 eff_buf.token_len = 0;
309
310 /* show every extension the new incoming data */
311
312 for (n = 0; n < wsi->count_active_extensions; n++) {
313 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000314 wsi->protocol->owning_server,
315 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000316 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
317 wsi->active_extensions_user[n], &eff_buf, 0);
318 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800319 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000320 goto just_kill_connection;
321 }
322 if (m)
323 /*
324 * at least one extension told us he has more
325 * to spill, so we will go around again after
326 */
327 ret = 1;
328 }
329
330 /* assuming they left us something to send, send it */
331
332 if (eff_buf.token_len)
333 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenfc7c5e42013-02-23 10:50:10 +0800334 eff_buf.token_len) != eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800335 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000336 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800337 }
Andy Greenc44159f2011-03-07 07:08:18 +0000338 }
Andy Green3182ece2013-01-20 17:08:31 +0800339#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000340
341 /*
Andy Greenda527df2011-03-07 07:08:12 +0000342 * signal we are closing, libsocket_write will
343 * add any necessary version-specific stuff. If the write fails,
344 * no worries we are closing anyway. If we didn't initiate this
345 * close, then our state has been changed to
346 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
347 *
348 * Likewise if it's a second call to close this connection after we
349 * sent the close indication to the peer already, we are in state
350 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
351 */
352
353 if (old_state == WSI_STATE_ESTABLISHED &&
354 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100355
Andy Green43db0452013-01-10 19:50:35 +0800356 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100357
Andy Greenfdd305a2013-02-11 14:32:48 +0800358 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800359 memset(buf, 0, sizeof(buf));
360 n = libwebsocket_write(wsi,
361 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000362 0, LWS_WRITE_CLOSE);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800363 if (n >= 0) {
Andy Greenda527df2011-03-07 07:08:12 +0000364 /*
365 * we have sent a nice protocol level indication we
366 * now wish to close, we should not send anything more
367 */
368
369 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
370
Andy Greenb5b23192013-02-11 17:13:32 +0800371 /*
372 * ...and we should wait for a reply for a bit
373 * out of politeness
374 */
Andy Greenda527df2011-03-07 07:08:12 +0000375
376 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800377 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000378
Andy Green43db0452013-01-10 19:50:35 +0800379 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000380
381 return;
382 }
383
Andy Greenb5b23192013-02-11 17:13:32 +0800384 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800385
Andy Greenda527df2011-03-07 07:08:12 +0000386 /* else, the send failed and we should just hang up */
387 }
388
Andy Greenc44159f2011-03-07 07:08:18 +0000389just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100390
Andy Greenb5b23192013-02-11 17:13:32 +0800391 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100392
Andy Greenda527df2011-03-07 07:08:12 +0000393 /*
394 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800395 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000396 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000397
Andy Greendfb23042013-01-17 12:26:48 +0800398 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000399
Andy Green251f6fa2010-11-03 11:13:06 +0000400 wsi->state = WSI_STATE_DEAD_SOCKET;
401
Andy Greenb5b23192013-02-11 17:13:32 +0800402 if ((old_state == WSI_STATE_ESTABLISHED ||
403 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800404 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
405
406 if (wsi->u.ws.rx_user_buffer) {
407 free(wsi->u.ws.rx_user_buffer);
408 wsi->u.ws.rx_user_buffer = NULL;
409 }
410 if (wsi->u.ws.rxflow_buffer) {
411 free(wsi->u.ws.rxflow_buffer);
412 wsi->u.ws.rxflow_buffer = NULL;
413 }
Andy Green2764eba2013-12-09 14:16:17 +0800414 if (wsi->truncated_send_malloc) {
Andy Green1f4267b2013-10-17 08:09:19 +0800415 /* not going to be completed... nuke it */
Andy Green2764eba2013-12-09 14:16:17 +0800416 free(wsi->truncated_send_malloc);
417 wsi->truncated_send_malloc = NULL;
Andy Green1f4267b2013-10-17 08:09:19 +0800418 }
Andy Green54495112013-02-06 21:10:16 +0900419 }
420
Andy Green4b6fbe12011-02-14 08:03:48 +0000421 /* tell the user it's all over for this guy */
422
Andy Greend4302732011-02-28 07:45:29 +0000423 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800424 ((old_state == WSI_STATE_ESTABLISHED) ||
425 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
426 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800427 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000428 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000429 wsi->user_space, NULL, 0);
Niall T. Davidsondb761be2013-06-29 10:16:18 +0800430 } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) {
431 lwsl_debug("calling back CLOSED_HTTP\n");
432 context->protocols[0].callback(context, wsi,
433 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 );
Andy Greencc012472011-11-07 19:53:23 +0800434 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800435 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000436
Andy Green3182ece2013-01-20 17:08:31 +0800437#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000438 /* deallocate any active extension contexts */
439
440 for (n = 0; n < wsi->count_active_extensions; n++) {
441 if (!wsi->active_extensions[n]->callback)
442 continue;
443
Andy Green46c2ea02011-03-22 09:04:01 +0000444 wsi->active_extensions[n]->callback(context,
445 wsi->active_extensions[n], wsi,
446 LWS_EXT_CALLBACK_DESTROY,
447 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000448
449 free(wsi->active_extensions_user[n]);
450 }
451
Andy Greena41314f2011-05-23 10:00:03 +0100452 /*
453 * inform all extensions in case they tracked this guy out of band
454 * even though not active on him specifically
455 */
456
457 ext = context->extensions;
458 while (ext && ext->callback) {
459 ext->callback(context, ext, wsi,
460 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
461 NULL, NULL, 0);
462 ext++;
463 }
Andy Green3182ece2013-01-20 17:08:31 +0800464#endif
Andy Greena41314f2011-05-23 10:00:03 +0100465
Andy Green43db0452013-01-10 19:50:35 +0800466/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000467
Andy Green3faa9c72010-11-08 17:03:03 +0000468#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000469 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000470 n = SSL_get_fd(wsi->ssl);
471 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800472 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000473 SSL_free(wsi->ssl);
474 } else {
475#endif
Andy Green0303db42013-01-17 14:46:43 +0800476 if (wsi->sock) {
477 n = shutdown(wsi->sock, SHUT_RDWR);
478 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800479 lwsl_debug("closing: shutdown returned %d\n",
480 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800481
Andy Green0303db42013-01-17 14:46:43 +0800482 n = compatible_close(wsi->sock);
483 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800484 lwsl_debug("closing: close returned %d\n",
485 errno);
Andy Green0303db42013-01-17 14:46:43 +0800486 }
Andy Green3faa9c72010-11-08 17:03:03 +0000487#ifdef LWS_OPENSSL_SUPPORT
488 }
489#endif
Andy Green76b6ea12014-02-15 19:25:50 +0800490
491 /* outermost destroy notification for wsi (user_space still intact) */
492 context->protocols[0].callback(context, wsi,
493 LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0);
494
Andy Greenb5b23192013-02-11 17:13:32 +0800495 if (wsi->protocol && wsi->protocol->per_session_data_size &&
496 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000497 free(wsi->user_space);
498
Andy Green251f6fa2010-11-03 11:13:06 +0000499 free(wsi);
500}
501
Andy Green07034092011-02-13 08:37:12 +0000502/**
503 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800504 * @context: Libwebsockets context
505 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000506 * @fd: Connection socket descriptor
507 * @name: Buffer to take client address name
508 * @name_len: Length of client address name buffer
509 * @rip: Buffer to take client address IP qotted quad
510 * @rip_len: Length of client address IP buffer
511 *
512 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800513 * the client connected with socket descriptor @fd. Names may be
514 * truncated if there is not enough room. If either cannot be
515 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000516 */
517
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800518LWS_VISIBLE void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800519libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
520 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000521 char *rip, int rip_len)
522{
Bob Robertsac049112013-04-25 09:16:30 +0800523 socklen_t len;
Andy Green07034092011-02-13 08:37:12 +0000524 struct sockaddr_in sin;
525 struct hostent *host;
526 struct hostent *host1;
527 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000528 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000529 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800530 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800531#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800532 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800533#endif
Andy Green07034092011-02-13 08:37:12 +0000534
535 rip[0] = '\0';
536 name[0] = '\0';
537
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800538 lws_latency_pre(context, wsi);
539
Andy Greenb5b23192013-02-11 17:13:32 +0800540 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000541 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100542 lwsl_warn("getpeername: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800543 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000544 }
Andy Green6ee372f2012-04-09 15:09:01 +0800545
Andy Greenb5b23192013-02-11 17:13:32 +0800546 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000547 AF_INET);
548 if (host == NULL) {
Patrick Gansterer7769a2d2014-02-26 19:52:11 +0100549 lwsl_warn("gethostbyaddr: %s\n", strerror(errno));
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800550 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000551 }
552
553 strncpy(name, host->h_name, name_len);
554 name[name_len - 1] = '\0';
555
556 host1 = gethostbyname(host->h_name);
557 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800558 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000559 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000560 n = 0;
561 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000562 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000563 if (p == NULL)
564 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000565 if ((host1->h_addrtype != AF_INET)
566#ifdef AF_LOCAL
567 && (host1->h_addrtype != AF_LOCAL)
568#endif
569 )
Andy Green07034092011-02-13 08:37:12 +0000570 continue;
571
Andy Green7627af52011-03-09 15:13:52 +0000572 if (host1->h_addrtype == AF_INET)
573 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000574#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000575 else {
576 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800577 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000578 ip[sizeof(ip) - 1] = '\0';
579 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000580#endif
Andy Green07034092011-02-13 08:37:12 +0000581 p = NULL;
582 strncpy(rip, ip, rip_len);
583 rip[rip_len - 1] = '\0';
584 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800585
586 ret = 0;
587bail:
588 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000589}
Andy Green9f990342011-02-12 11:57:45 +0000590
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800591LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
Peter Hinz56885f32011-03-02 22:03:47 +0000592 void *buf, int len)
593{
594 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800595 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000596
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100597#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +0000598 for (n = 0; n < len; n++)
599 p[n] = (unsigned char)rand();
600#else
601 n = read(context->fd_random, p, len);
602#endif
603
604 return n;
605}
606
Andy Greena690cd02013-02-09 12:25:31 +0800607int lws_set_socket_options(struct libwebsocket_context *context, int fd)
608{
609 int optval = 1;
610 socklen_t optlen = sizeof(optval);
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100611#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800612 unsigned long optl = 0;
613#endif
614#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
615 struct protoent *tcp_proto;
616#endif
617
618 if (context->ka_time) {
619 /* enable keepalive on this socket */
620 optval = 1;
621 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
622 (const void *)&optval, optlen) < 0)
623 return 1;
624
Bob Robertsac049112013-04-25 09:16:30 +0800625#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
Andy Greena47865f2013-02-10 09:39:47 +0800626
627 /*
628 * didn't find a way to set these per-socket, need to
629 * tune kernel systemwide values
630 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100631#elif WIN32
632 {
633 DWORD dwBytesRet;
634 struct tcp_keepalive alive;
635 alive.onoff = TRUE;
636 alive.keepalivetime = context->ka_time;
637 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800638
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100639 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
640 NULL, 0, &dwBytesRet, NULL, NULL))
641 return 1;
642 }
Andy Greena47865f2013-02-10 09:39:47 +0800643#else
Andy Greena690cd02013-02-09 12:25:31 +0800644 /* set the keepalive conditions we want on it too */
645 optval = context->ka_time;
646 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
647 (const void *)&optval, optlen) < 0)
648 return 1;
649
Larry Hayesbb66ac62013-02-22 09:16:20 +0800650 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800651 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
652 (const void *)&optval, optlen) < 0)
653 return 1;
654
Larry Hayesbb66ac62013-02-22 09:16:20 +0800655 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800656 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
657 (const void *)&optval, optlen) < 0)
658 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800659#endif
Andy Greena690cd02013-02-09 12:25:31 +0800660 }
661
662 /* Disable Nagle */
663 optval = 1;
664#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
665 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
666#else
667 tcp_proto = getprotobyname("TCP");
668 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
669#endif
670
671 /* We are nonblocking... */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +0100672#if defined(WIN32) || defined(_WIN32)
Andy Greena690cd02013-02-09 12:25:31 +0800673 ioctlsocket(fd, FIONBIO, &optl);
674#else
675 fcntl(fd, F_SETFL, O_NONBLOCK);
676#endif
677
678 return 0;
679}
680
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800681LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
Andy Green95a7b5d2011-03-06 10:29:39 +0000682{
683 struct pollfd fds;
684
Andy Green2764eba2013-12-09 14:16:17 +0800685 /* treat the fact we got a truncated send pending as if we're choked */
686 if (wsi->truncated_send_malloc)
687 return 1;
688
Andy Green95a7b5d2011-03-06 10:29:39 +0000689 fds.fd = wsi->sock;
690 fds.events = POLLOUT;
691 fds.revents = 0;
692
693 if (poll(&fds, 1, 0) != 1)
694 return 1;
695
696 if ((fds.revents & POLLOUT) == 0)
697 return 1;
698
699 /* okay to send another packet without blocking */
700
701 return 0;
702}
703
Andy Greena41314f2011-05-23 10:00:03 +0100704int
Andy Green3b84c002011-03-06 13:14:42 +0000705lws_handle_POLLOUT_event(struct libwebsocket_context *context,
706 struct libwebsocket *wsi, struct pollfd *pollfd)
707{
Andy Green3b84c002011-03-06 13:14:42 +0000708 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800709
Andy Green3182ece2013-01-20 17:08:31 +0800710#ifndef LWS_NO_EXTENSIONS
711 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000712 int ret;
713 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100714 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000715
Andy Green1f4267b2013-10-17 08:09:19 +0800716 /* pending truncated sends have uber priority */
717
Andy Green2764eba2013-12-09 14:16:17 +0800718 if (wsi->truncated_send_malloc) {
719 lws_issue_raw(wsi, wsi->truncated_send_malloc +
720 wsi->truncated_send_offset,
721 wsi->truncated_send_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800722 /* leave POLLOUT active either way */
723 return 0;
724 }
725
Andy Greena41314f2011-05-23 10:00:03 +0100726 for (n = 0; n < wsi->count_active_extensions; n++) {
727 if (!wsi->active_extensions[n]->callback)
728 continue;
729
730 m = wsi->active_extensions[n]->callback(context,
731 wsi->active_extensions[n], wsi,
732 LWS_EXT_CALLBACK_IS_WRITEABLE,
733 wsi->active_extensions_user[n], NULL, 0);
734 if (m > handled)
735 handled = m;
736 }
737
738 if (handled == 1)
739 goto notify_action;
740
741 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000742 goto user_service;
743
744 /*
745 * check in on the active extensions, see if they
746 * had pending stuff to spill... they need to get the
747 * first look-in otherwise sequence will be disordered
748 *
749 * NULL, zero-length eff_buf means just spill pending
750 */
751
752 ret = 1;
753 while (ret == 1) {
754
755 /* default to nobody has more to spill */
756
757 ret = 0;
758 eff_buf.token = NULL;
759 eff_buf.token_len = 0;
760
761 /* give every extension a chance to spill */
762
763 for (n = 0; n < wsi->count_active_extensions; n++) {
764 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000765 wsi->protocol->owning_server,
766 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000767 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
768 wsi->active_extensions_user[n], &eff_buf, 0);
769 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800770 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000771 return -1;
772 }
773 if (m)
774 /*
775 * at least one extension told us he has more
776 * to spill, so we will go around again after
777 */
778 ret = 1;
779 }
780
781 /* assuming they gave us something to send, send it */
782
783 if (eff_buf.token_len) {
Andy Greenfc7c5e42013-02-23 10:50:10 +0800784 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
785 eff_buf.token_len);
786 if (n < 0)
Andy Green3b84c002011-03-06 13:14:42 +0000787 return -1;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800788 /*
789 * Keep amount spilled small to minimize chance of this
790 */
791 if (n != eff_buf.token_len) {
792 lwsl_err("Unable to spill ext %d vs %s\n",
793 eff_buf.token_len, n);
794 return -1;
795 }
Andy Green3b84c002011-03-06 13:14:42 +0000796 } else
797 continue;
798
799 /* no extension has more to spill */
800
801 if (!ret)
802 continue;
803
804 /*
805 * There's more to spill from an extension, but we just sent
806 * something... did that leave the pipe choked?
807 */
808
809 if (!lws_send_pipe_choked(wsi))
810 /* no we could add more */
811 continue;
812
Andy Green43db0452013-01-10 19:50:35 +0800813 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000814
815 /*
816 * Yes, he's choked. Leave the POLLOUT masked on so we will
817 * come back here when he is unchoked. Don't call the user
818 * callback to enforce ordering of spilling, he'll get called
819 * when we come back here and there's nothing more to spill.
820 */
821
822 return 0;
823 }
824
825 wsi->extension_data_pending = 0;
826
827user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800828#endif
Andy Green3b84c002011-03-06 13:14:42 +0000829 /* one shot */
830
Andy Green91f19d82013-12-21 11:18:34 +0800831 if (pollfd)
832 lws_change_pollfd(wsi, POLLOUT, 0);
Andy Green7a132792013-12-18 09:48:26 +0800833
Andy Green3182ece2013-01-20 17:08:31 +0800834#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100835notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800836#endif
Andy Green3b84c002011-03-06 13:14:42 +0000837
Andy Green9e4c2b62011-03-07 20:47:39 +0000838 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
839 n = LWS_CALLBACK_CLIENT_WRITEABLE;
840 else
841 n = LWS_CALLBACK_SERVER_WRITEABLE;
842
Andy Greenb8b247d2013-01-22 07:20:08 +0800843 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800844 wsi, (enum libwebsocket_callback_reasons) n,
845 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000846}
847
848
849
Andy Green1c6e1422013-02-20 19:11:31 +0800850int
Andy Greena41314f2011-05-23 10:00:03 +0100851libwebsocket_service_timeout_check(struct libwebsocket_context *context,
852 struct libwebsocket *wsi, unsigned int sec)
853{
Andy Green3182ece2013-01-20 17:08:31 +0800854#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100855 int n;
856
857 /*
858 * if extensions want in on it (eg, we are a mux parent)
859 * give them a chance to service child timeouts
860 */
861
862 for (n = 0; n < wsi->count_active_extensions; n++)
863 wsi->active_extensions[n]->callback(
864 context, wsi->active_extensions[n],
865 wsi, LWS_EXT_CALLBACK_1HZ,
866 wsi->active_extensions_user[n], NULL, sec);
867
Andy Green3182ece2013-01-20 17:08:31 +0800868#endif
Andy Greena41314f2011-05-23 10:00:03 +0100869 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800870 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800871
Andy Greena41314f2011-05-23 10:00:03 +0100872 /*
873 * if we went beyond the allowed time, kill the
874 * connection
875 */
876
877 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800878 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100879 libwebsocket_close_and_free_session(context,
880 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800881 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100882 }
Andy Green1c6e1422013-02-20 19:11:31 +0800883
884 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100885}
886
Andy Green9f990342011-02-12 11:57:45 +0000887/**
888 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000889 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000890 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800891 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000892 *
Andy Green75006172013-01-22 12:32:11 +0800893 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800894 * services it according to the state of the associated
895 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800896 *
897 * The one call deals with all "service" that might happen on a socket
898 * including listen accepts, http files as well as websocket protocol.
Andy Green2577c832013-03-11 20:37:03 +0800899 *
900 * If a pollfd says it has something, you can just pass it to
901 * libwebsocket_serice_fd() whether it is a socket handled by lws or not.
902 * If it sees it is a lws socket, the traffic will be handled and
903 * pollfd->revents will be zeroed now.
904 *
905 * If the socket is foreign to lws, it leaves revents alone. So you can
906 * see if you should service yourself by checking the pollfd revents
907 * after letting lws try to service it.
Andy Green9f990342011-02-12 11:57:45 +0000908 */
909
Peter Pentchev9a4fef72013-03-30 09:52:21 +0800910LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +0000911libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000912 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000913{
Andy Greena1ce6be2013-01-18 11:43:21 +0800914 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000915 int n;
Andy Green0d338332011-02-12 11:57:43 +0000916 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800917 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000918 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800919 int timed_out = 0;
920 int our_fd = 0;
Andy Greenca0a1292013-03-16 11:24:23 +0800921 char draining_flow = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800922
Andy Green3182ece2013-01-20 17:08:31 +0800923#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000924 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800925#endif
Andy Green98a717c2011-03-06 13:14:15 +0000926 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800927
928 if (context->listen_service_fd)
929 listen_socket_fds_index = context->lws_lookup[
930 context->listen_service_fd]->position_in_fds_table;
931
Andy Greena71eafc2011-02-14 17:59:43 +0000932 /*
933 * you can call us with pollfd = NULL to just allow the once-per-second
934 * global timeout checks; if less than a second since the last check
935 * it returns immediately then.
936 */
937
938 gettimeofday(&tv, NULL);
939
Peter Hinz56885f32011-03-02 22:03:47 +0000940 if (context->last_timeout_check_s != tv.tv_sec) {
941 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000942
Joakim Soderberg4c531232013-02-06 15:26:58 +0900943 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800944 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800945 if (context->started_with_parent &&
946 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800947 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900948 #endif
Andy Green24cba922013-01-19 13:56:10 +0800949
Andy Greena71eafc2011-02-14 17:59:43 +0000950 /* global timeout check once per second */
951
Andy Green1c6e1422013-02-20 19:11:31 +0800952 if (pollfd)
953 our_fd = pollfd->fd;
954
Peter Hinz56885f32011-03-02 22:03:47 +0000955 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800956 m = context->fds[n].fd;
957 wsi = context->lws_lookup[m];
958 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800959 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800960
961 if (libwebsocket_service_timeout_check(context, wsi,
962 tv.tv_sec))
963 /* he did time out... */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800964 if (m == our_fd) {
Andy Green1c6e1422013-02-20 19:11:31 +0800965 /* it was the guy we came to service! */
966 timed_out = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800967 /* mark as handled */
968 pollfd->revents = 0;
969 }
Andy Greena71eafc2011-02-14 17:59:43 +0000970 }
971 }
972
Andy Green1c6e1422013-02-20 19:11:31 +0800973 /* the socket we came to service timed out, nothing to do */
974 if (timed_out)
975 return 0;
976
Andy Greena71eafc2011-02-14 17:59:43 +0000977 /* just here for timeout management? */
Andy Greena71eafc2011-02-14 17:59:43 +0000978 if (pollfd == NULL)
979 return 0;
980
981 /* no, here to service a socket descriptor */
Andy Greenb1a5e6c2013-03-09 12:34:30 +0800982 wsi = context->lws_lookup[pollfd->fd];
983 if (wsi == NULL)
984 /* not lws connection ... leave revents alone and return */
985 return 0;
986
987 /*
988 * so that caller can tell we handled, past here we need to
989 * zero down pollfd->revents after handling
990 */
991
Andy Green65b0e912013-01-16 07:59:47 +0800992 /*
993 * deal with listen service piggybacking
994 * every listen_service_modulo services of other fds, we
995 * sneak one in to service the listen socket if there's anything waiting
996 *
997 * To handle connection storms, as found in ab, if we previously saw a
998 * pending connection here, it causes us to check again next time.
999 */
1000
Andy Greenb5b23192013-02-11 17:13:32 +08001001 if (context->listen_service_fd && pollfd !=
1002 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +08001003 context->listen_service_count++;
1004 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +08001005 context->listen_service_count ==
1006 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +08001007 context->listen_service_count = 0;
1008 m = 1;
1009 if (context->listen_service_extraseen > 5)
1010 m = 2;
1011 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +08001012 /*
1013 * even with extpoll, we prepared this
1014 * internal fds for listen
1015 */
1016 n = poll(&context->fds[listen_socket_fds_index],
1017 1, 0);
1018 if (n > 0) { /* there's a conn waiting for us */
1019 libwebsocket_service_fd(context,
1020 &context->
1021 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +08001022 context->listen_service_extraseen++;
1023 } else {
1024 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +08001025 context->
1026 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +08001027 break;
1028 }
1029 }
1030 }
1031
1032 }
1033
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001034 /* handle session socket closed */
1035
1036 if ((!(pollfd->revents & POLLIN)) &&
1037 (pollfd->revents & (POLLERR | POLLHUP))) {
1038
1039 lwsl_debug("Session Socket %p (fd=%d) dead\n",
1040 (void *)wsi, pollfd->fd);
1041
1042 goto close_and_handled;
1043 }
1044
Andy Green65b0e912013-01-16 07:59:47 +08001045 /* okay, what we came here to do... */
1046
Andy Green0d338332011-02-12 11:57:43 +00001047 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +08001048
Andy Greena1ce6be2013-01-18 11:43:21 +08001049#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +08001050 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +08001051 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green0d338332011-02-12 11:57:43 +00001052 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +08001053 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001054 n = lws_server_socket_service(context, wsi, pollfd);
1055 goto handled;
Andy Greena1ce6be2013-01-18 11:43:21 +08001056#endif
Andy Greenbe93fef2011-02-14 20:25:43 +00001057
Andy Green0d338332011-02-12 11:57:43 +00001058 case LWS_CONNMODE_WS_SERVING:
1059 case LWS_CONNMODE_WS_CLIENT:
1060
Andy Green0d338332011-02-12 11:57:43 +00001061 /* the guy requested a callback when it was OK to write */
1062
Andy Greenda527df2011-03-07 07:08:12 +00001063 if ((pollfd->revents & POLLOUT) &&
Andy Greenca0a1292013-03-16 11:24:23 +08001064 wsi->state == WSI_STATE_ESTABLISHED &&
1065 lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
Andy Green91f19d82013-12-21 11:18:34 +08001066 lwsl_info("libwebsocket_service_fd: closing\n");
1067 goto close_and_handled;
Graham Newtonb1aa1d32013-11-04 09:47:06 +08001068 }
Andy Green0d338332011-02-12 11:57:43 +00001069
Andy Greenca0a1292013-03-16 11:24:23 +08001070 if (wsi->u.ws.rxflow_buffer &&
1071 (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) {
1072 lwsl_info("draining rxflow\n");
1073 /* well, drain it */
1074 eff_buf.token = (char *)wsi->u.ws.rxflow_buffer +
1075 wsi->u.ws.rxflow_pos;
1076 eff_buf.token_len = wsi->u.ws.rxflow_len -
1077 wsi->u.ws.rxflow_pos;
1078 draining_flow = 1;
1079 goto drain;
1080 }
1081
Andy Green0d338332011-02-12 11:57:43 +00001082 /* any incoming data ready? */
1083
1084 if (!(pollfd->revents & POLLIN))
1085 break;
1086
Andy Greenb45993c2010-12-18 15:13:50 +00001087#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +08001088read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +08001089 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +08001090 eff_buf.token_len = SSL_read(wsi->ssl,
1091 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +08001092 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001093 if (!eff_buf.token_len) {
1094 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +08001095 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001096 ERR_error_string(n,
1097 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +08001098 }
1099 } else
Andy Greenb45993c2010-12-18 15:13:50 +00001100#endif
Andy Greene84652c2013-02-08 13:01:02 +08001101 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +08001102 context->service_buffer,
1103 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +00001104
Andy Green98a717c2011-03-06 13:14:15 +00001105 if (eff_buf.token_len < 0) {
Andy Green98ba9e02013-03-23 09:44:47 +08001106 lwsl_debug("service_fd read ret = %d, errno = %d\n",
1107 eff_buf.token_len, errno);
Alon Levydc93b7f2012-10-19 11:21:57 +02001108 if (errno != EINTR && errno != EAGAIN)
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001109 goto close_and_handled;
1110 n = 0;
1111 goto handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001112 }
Andy Green98a717c2011-03-06 13:14:15 +00001113 if (!eff_buf.token_len) {
Andy Green98ba9e02013-03-23 09:44:47 +08001114 lwsl_info("service_fd: closing due to 0 length read\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001115 goto close_and_handled;
Andy Greenb45993c2010-12-18 15:13:50 +00001116 }
1117
Andy Green98a717c2011-03-06 13:14:15 +00001118 /*
1119 * give any active extensions a chance to munge the buffer
1120 * before parse. We pass in a pointer to an lws_tokens struct
1121 * prepared with the default buffer and content length that's in
1122 * there. Rather than rewrite the default buffer, extensions
1123 * that expect to grow the buffer can adapt .token to
1124 * point to their own per-connection buffer in the extension
1125 * user allocation. By default with no extensions or no
1126 * extension callback handling, just the normal input buffer is
1127 * used then so it is efficient.
1128 */
Andy Greenb45993c2010-12-18 15:13:50 +00001129
Andy Greene84652c2013-02-08 13:01:02 +08001130 eff_buf.token = (char *)context->service_buffer;
Andy Greenca0a1292013-03-16 11:24:23 +08001131drain:
Andy Green3182ece2013-01-20 17:08:31 +08001132#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001133 more = 1;
1134 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001135
Andy Green98a717c2011-03-06 13:14:15 +00001136 more = 0;
1137
1138 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001139 m = wsi->active_extensions[n]->callback(context,
1140 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001141 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001142 wsi->active_extensions_user[n],
1143 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001144 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001145 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001146 "Extension reports fatal error\n");
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001147 goto close_and_handled;
Andy Green98a717c2011-03-06 13:14:15 +00001148 }
1149 if (m)
1150 more = 1;
1151 }
Andy Green3182ece2013-01-20 17:08:31 +08001152#endif
Andy Green98a717c2011-03-06 13:14:15 +00001153 /* service incoming data */
1154
1155 if (eff_buf.token_len) {
1156 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001157 (unsigned char *)eff_buf.token,
1158 eff_buf.token_len);
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001159 if (n < 0) {
Andy Green98a717c2011-03-06 13:14:15 +00001160 /* we closed wsi */
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001161 n = 0;
1162 goto handled;
1163 }
Andy Green98a717c2011-03-06 13:14:15 +00001164 }
Andy Green3182ece2013-01-20 17:08:31 +08001165#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001166 eff_buf.token = NULL;
1167 eff_buf.token_len = 0;
1168 }
Andy Green3182ece2013-01-20 17:08:31 +08001169#endif
Andy Greenca0a1292013-03-16 11:24:23 +08001170 if (draining_flow && wsi->u.ws.rxflow_buffer &&
1171 wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) {
1172 lwsl_info("flow buffer: drained\n");
1173 free(wsi->u.ws.rxflow_buffer);
1174 wsi->u.ws.rxflow_buffer = NULL;
1175 /* having drained the rxflow buffer, can rearm POLLIN */
1176 _libwebsocket_rx_flow_control(wsi);
1177 }
David Galeano7ffbe1b2013-01-10 10:35:32 +08001178
1179#ifdef LWS_OPENSSL_SUPPORT
1180 if (wsi->ssl && SSL_pending(wsi->ssl))
1181 goto read_pending;
1182#endif
Andy Green98a717c2011-03-06 13:14:15 +00001183 break;
Andy Green76f61e72013-01-16 11:53:05 +08001184
1185 default:
Andy Green03674a62013-01-16 11:47:40 +08001186#ifdef LWS_NO_CLIENT
1187 break;
1188#else
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001189 n = lws_client_socket_service(context, wsi, pollfd);
1190 goto handled;
Andy Green03674a62013-01-16 11:47:40 +08001191#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001192 }
1193
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001194 n = 0;
1195 goto handled;
1196
1197close_and_handled:
Andy Greenca0a1292013-03-16 11:24:23 +08001198 libwebsocket_close_and_free_session(context, wsi,
1199 LWS_CLOSE_STATUS_NOSTATUS);
1200 n = 1;
Andy Greenb1a5e6c2013-03-09 12:34:30 +08001201
1202handled:
1203 pollfd->revents = 0;
1204 return n;
Andy Greenb45993c2010-12-18 15:13:50 +00001205}
1206
Andy Green0d338332011-02-12 11:57:43 +00001207
Andy Green6964bb52011-01-23 16:50:33 +00001208/**
1209 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001210 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001211 *
1212 * This function closes any active connections and then frees the
1213 * context. After calling this, any further use of the context is
1214 * undefined.
1215 */
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001216LWS_VISIBLE void
Peter Hinz56885f32011-03-02 22:03:47 +00001217libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001218{
Andy Green0d338332011-02-12 11:57:43 +00001219 int n;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001220#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001221 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001222 struct libwebsocket_extension *ext;
arnaudviala7c6f26c2014-02-15 14:39:40 +08001223#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001224 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001225
Andy Greend636e352013-01-29 12:36:17 +08001226#ifdef LWS_LATENCY
1227 if (context->worst_latency_info[0])
1228 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1229#endif
1230
Andy Greendfb23042013-01-17 12:26:48 +08001231 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001232 struct libwebsocket *wsi =
1233 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001234 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001235 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1236 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001237 }
Andy Green6964bb52011-01-23 16:50:33 +00001238
arnaudviala7c6f26c2014-02-15 14:39:40 +08001239#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001240 /*
1241 * give all extensions a chance to clean up any per-context
1242 * allocations they might have made
1243 */
1244
1245 ext = context->extensions;
1246 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1247 if (context->listen_port)
1248 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001249 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001250 ext->callback(context, ext, NULL,
1251 (enum libwebsocket_extension_callback_reasons)m,
1252 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001253 ext++;
1254 }
arnaudviala7c6f26c2014-02-15 14:39:40 +08001255#endif /* ndef LWS_NO_EXTENSIONS */
Andy Greena7109e62013-02-11 12:05:54 +08001256
1257 /*
1258 * inform all the protocols that they are done and will have no more
1259 * callbacks
1260 */
1261
1262 while (protocol->callback) {
1263 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1264 NULL, NULL, 0);
1265 protocol++;
1266 }
1267
Andy Greena41314f2011-05-23 10:00:03 +01001268
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001269#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001270#else
1271 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001272#endif
1273
Peter Hinz56885f32011-03-02 22:03:47 +00001274#ifdef LWS_OPENSSL_SUPPORT
1275 if (context->ssl_ctx)
1276 SSL_CTX_free(context->ssl_ctx);
1277 if (context->ssl_client_ctx)
1278 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001279
1280 ERR_remove_state(0);
1281 ERR_free_strings();
1282 EVP_cleanup();
1283 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001284#endif
1285
Andy Green46596482013-02-11 11:04:01 +08001286 if (context->fds)
1287 free(context->fds);
1288 if (context->lws_lookup)
1289 free(context->lws_lookup);
1290
Peter Hinz56885f32011-03-02 22:03:47 +00001291 free(context);
1292
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01001293#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00001294 WSACleanup();
1295#endif
Andy Green6964bb52011-01-23 16:50:33 +00001296}
1297
Andy Greend88146d2013-01-22 12:40:35 +08001298/**
Andy Greenb5b23192013-02-11 17:13:32 +08001299 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001300 * @context: Websocket context
1301 *
1302 * This returns the optional user allocation that can be attached to
1303 * the context the sockets live in at context_create time. It's a way
1304 * to let all sockets serviced in the same context share data without
1305 * using globals statics in the user code.
1306 */
Alon Levy0291eb32012-10-19 11:21:56 +02001307LWS_EXTERN void *
1308libwebsocket_context_user(struct libwebsocket_context *context)
1309{
Andy Greenb5b23192013-02-11 17:13:32 +08001310 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001311}
1312
Andy Green6964bb52011-01-23 16:50:33 +00001313/**
1314 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001315 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001316 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1317 * service otherwise block and service immediately, returning
1318 * after the timeout if nothing needed service.
1319 *
1320 * This function deals with any pending websocket traffic, for three
1321 * kinds of event. It handles these events on both server and client
1322 * types of connection the same.
1323 *
1324 * 1) Accept new connections to our context's server
1325 *
Andy Green6f520a52013-01-29 17:57:39 +08001326 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001327 * server or client connections.
1328 *
1329 * You need to call this service function periodically to all the above
1330 * functions to happen; if your application is single-threaded you can
1331 * just call it in your main event loop.
1332 *
1333 * Alternatively you can fork a new process that asynchronously handles
1334 * calling this service in a loop. In that case you are happy if this
1335 * call blocks your thread until it needs to take care of something and
1336 * would call it with a large nonzero timeout. Your loop then takes no
1337 * CPU while there is nothing happening.
1338 *
1339 * If you are calling it in a single-threaded app, you don't want it to
1340 * wait around blocking other things in your loop from happening, so you
1341 * would call it with a timeout_ms of 0, so it returns immediately if
1342 * nothing is pending, or as soon as it services whatever was pending.
1343 */
1344
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001345LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001346libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001347{
1348 int n;
Andy Greenca0a1292013-03-16 11:24:23 +08001349 int m;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001350#ifdef LWS_HAS_PPOLL
1351 struct timespec timeout_ts;
1352 sigset_t sigmask;
1353#endif
Andy Greene92cd172011-01-19 13:11:55 +00001354
1355 /* stay dead once we are dead */
1356
Peter Hinz56885f32011-03-02 22:03:47 +00001357 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001358 return 1;
1359
Andy Green3b3fa9e2013-12-25 16:34:37 +08001360#ifdef LWS_HAS_PPOLL
1361 lws_idling_ppoll_tid = context->protocols[0].callback(context, NULL,
1362 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1363
1364 timeout_ts.tv_sec = timeout_ms / 1000;
1365 timeout_ts.tv_nsec = timeout_ms % 1000;
Andy Greened451d52014-01-11 12:37:07 +08001366
1367 sigprocmask(SIG_BLOCK, NULL, &sigmask);
1368 sigdelset(&sigmask, SIGUSR2);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001369
Andy Green0d338332011-02-12 11:57:43 +00001370 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001371
Andy Green3b3fa9e2013-12-25 16:34:37 +08001372 n = ppoll(context->fds, context->fds_count, &timeout_ts, &sigmask);
1373 lws_idling_ppoll_tid = 0;
1374#else
Peter Hinz56885f32011-03-02 22:03:47 +00001375 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3b3fa9e2013-12-25 16:34:37 +08001376#endif
Andy Green5dc62ea2013-09-20 20:26:12 +08001377 if (n == 0) /* poll timeout */ {
1378 libwebsocket_service_fd(context, NULL);
Andy Green3221f922011-02-12 13:14:11 +00001379 return 0;
Andy Green5dc62ea2013-09-20 20:26:12 +08001380 }
Andy Greene92cd172011-01-19 13:11:55 +00001381
Andy Greened451d52014-01-11 12:37:07 +08001382 if (n < 0) {
1383 if (errno != EINTR)
1384 return -1;
1385 else
1386 return 0;
1387 }
Andy Greene92cd172011-01-19 13:11:55 +00001388
Andy Greendfb23042013-01-17 12:26:48 +08001389 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001390
Andy Greenca0a1292013-03-16 11:24:23 +08001391 for (n = 0; n < context->fds_count; n++) {
1392 if (!context->fds[n].revents)
1393 continue;
1394 m = libwebsocket_service_fd(context, &context->fds[n]);
1395 if (m < 0)
1396 return -1;
1397 /* if something closed, retry this slot */
1398 if (m)
1399 n--;
1400 }
1401
Andy Greene92cd172011-01-19 13:11:55 +00001402 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001403}
1404
Andy Green3182ece2013-01-20 17:08:31 +08001405#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001406int
1407lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001408 struct libwebsocket *wsi,
1409 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001410 void *v, size_t len)
1411{
1412 int n;
1413 int handled = 0;
1414
1415 /* maybe an extension will take care of it for us */
1416
1417 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1418 if (!wsi->active_extensions[n]->callback)
1419 continue;
1420
1421 handled |= wsi->active_extensions[n]->callback(context,
1422 wsi->active_extensions[n], wsi,
1423 r, wsi->active_extensions_user[n], v, len);
1424 }
1425
1426 return handled;
1427}
1428
1429
1430void *
1431lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001432 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001433{
1434 int n = 0;
1435
Andy Green68b45042011-05-25 21:41:57 +01001436 if (wsi == NULL)
1437 return NULL;
1438
Andy Greena41314f2011-05-23 10:00:03 +01001439 while (n < wsi->count_active_extensions) {
1440 if (wsi->active_extensions[n] != ext) {
1441 n++;
1442 continue;
1443 }
1444 return wsi->active_extensions_user[n];
1445 }
1446
1447 return NULL;
1448}
Andy Green3182ece2013-01-20 17:08:31 +08001449#endif
Andy Greena41314f2011-05-23 10:00:03 +01001450
Andy Green91f19d82013-12-21 11:18:34 +08001451void
1452lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
1453{
1454 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001455 int events;
1456#ifdef LWS_HAS_PPOLL
1457 int tid;
1458 int sampled_ppoll_tid;
1459#endif
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001460 struct libwebsocket_pollargs pa;
1461
1462 pa.fd = wsi->sock;
Andy Green91f19d82013-12-21 11:18:34 +08001463
1464 context->protocols[0].callback(context, wsi,
1465 LWS_CALLBACK_LOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001466 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001467
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001468 pa.prev_events = events = context->fds[wsi->position_in_fds_table].events;
Andy Green3b3fa9e2013-12-25 16:34:37 +08001469
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001470 pa.events = context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or;
Andy Green91f19d82013-12-21 11:18:34 +08001471
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001472 context->protocols[0].callback(context, wsi,
1473 LWS_CALLBACK_CHANGE_MODE_POLL_FD,
1474 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001475
Andy Green91f19d82013-12-21 11:18:34 +08001476
Andy Green3b3fa9e2013-12-25 16:34:37 +08001477#ifdef LWS_HAS_PPOLL
1478 /*
1479 * if we changed something in this pollfd...
1480 * ... and we're running in a different thread context
1481 * than the service thread...
1482 * ... and the service thread is waiting in ppoll()...
1483 * then fire a SIGUSR2 at the service thread to force it to
1484 * restart the ppoll() with our changed events
1485 */
1486 if (events != context->fds[wsi->position_in_fds_table].events) {
1487 sampled_ppoll_tid = lws_idling_ppoll_tid;
1488 if (sampled_ppoll_tid) {
1489 tid = context->protocols[0].callback(context, NULL,
1490 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
1491 if (tid != sampled_ppoll_tid)
1492 kill(sampled_ppoll_tid, SIGUSR2);
1493 }
1494 }
1495#endif
1496
Andy Green91f19d82013-12-21 11:18:34 +08001497 context->protocols[0].callback(context, wsi,
1498 LWS_CALLBACK_UNLOCK_POLL,
Michael Haberlerffbc02f2014-02-15 20:19:26 +08001499 wsi->user_space, (void *) &pa, 0);
Andy Green91f19d82013-12-21 11:18:34 +08001500}
1501
1502
Andy Green90c7cbc2011-01-27 06:26:52 +00001503/**
1504 * libwebsocket_callback_on_writable() - Request a callback when this socket
1505 * becomes able to be written to without
1506 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001507 *
Peter Hinz56885f32011-03-02 22:03:47 +00001508 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001509 * @wsi: Websocket connection instance to get callback for
1510 */
1511
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001512LWS_VISIBLE int
Peter Hinz56885f32011-03-02 22:03:47 +00001513libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001514 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001515{
Andy Green3182ece2013-01-20 17:08:31 +08001516#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001517 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001518 int handled = 0;
1519
1520 /* maybe an extension will take care of it for us */
1521
1522 for (n = 0; n < wsi->count_active_extensions; n++) {
1523 if (!wsi->active_extensions[n]->callback)
1524 continue;
1525
1526 handled |= wsi->active_extensions[n]->callback(context,
1527 wsi->active_extensions[n], wsi,
1528 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1529 wsi->active_extensions_user[n], NULL, 0);
1530 }
1531
1532 if (handled)
1533 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001534#endif
Andy Greendfb23042013-01-17 12:26:48 +08001535 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001536 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1537 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001538 return -1;
1539 }
1540
Andy Green91f19d82013-12-21 11:18:34 +08001541 lws_change_pollfd(wsi, 0, POLLOUT);
Andy Green7a132792013-12-18 09:48:26 +08001542
Andy Green90c7cbc2011-01-27 06:26:52 +00001543 return 1;
1544}
1545
1546/**
1547 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1548 * all connections using the given protocol when it
1549 * becomes possible to write to each socket without
1550 * blocking in turn.
1551 *
1552 * @protocol: Protocol whose connections will get callbacks
1553 */
1554
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001555LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001556libwebsocket_callback_on_writable_all_protocol(
1557 const struct libwebsocket_protocols *protocol)
1558{
Peter Hinz56885f32011-03-02 22:03:47 +00001559 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001560 int n;
Andy Green0d338332011-02-12 11:57:43 +00001561 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001562
Andy Greendfb23042013-01-17 12:26:48 +08001563 for (n = 0; n < context->fds_count; n++) {
1564 wsi = context->lws_lookup[context->fds[n].fd];
1565 if (!wsi)
1566 continue;
1567 if (wsi->protocol == protocol)
1568 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001569 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001570
1571 return 0;
1572}
1573
Andy Greenbe93fef2011-02-14 20:25:43 +00001574/**
Andy Greene39e6ef2014-02-15 16:36:38 +08001575 * libwebsocket_callback_all_protocol() - Callback all connections using
1576 * the given protocol with the given reason
1577 *
1578 * @protocol: Protocol whose connections will get callbacks
1579 * @reason: Callback reason index
1580 */
1581
1582LWS_VISIBLE int
1583libwebsocket_callback_all_protocol(
1584 const struct libwebsocket_protocols *protocol, int reason)
1585{
1586 struct libwebsocket_context *context = protocol->owning_server;
1587 int n;
1588 struct libwebsocket *wsi;
1589
1590 for (n = 0; n < context->fds_count; n++) {
1591 wsi = context->lws_lookup[context->fds[n].fd];
1592 if (!wsi)
1593 continue;
1594 if (wsi->protocol == protocol)
1595 protocol->callback(context, wsi,
1596 reason, wsi->user_space, NULL, 0);
1597 }
1598
1599 return 0;
1600}
1601
1602/**
Andy Greenbe93fef2011-02-14 20:25:43 +00001603 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1604 *
1605 * You will not need this unless you are doing something special
1606 *
1607 * @wsi: Websocket connection instance
1608 * @reason: timeout reason
1609 * @secs: how many seconds
1610 */
1611
Craig McQueene7fd8b12014-02-19 09:24:17 +11001612LWS_VISIBLE void
Andy Greenbe93fef2011-02-14 20:25:43 +00001613libwebsocket_set_timeout(struct libwebsocket *wsi,
1614 enum pending_timeout reason, int secs)
1615{
1616 struct timeval tv;
1617
1618 gettimeofday(&tv, NULL);
1619
1620 wsi->pending_timeout_limit = tv.tv_sec + secs;
1621 wsi->pending_timeout = reason;
1622}
1623
Andy Greena6cbece2011-01-27 20:06:03 +00001624
1625/**
1626 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1627 *
1628 * You will not need this unless you are doing something special
1629 *
1630 * @wsi: Websocket connection instance
1631 */
1632
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001633LWS_VISIBLE int
Andy Greena6cbece2011-01-27 20:06:03 +00001634libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1635{
1636 return wsi->sock;
1637}
1638
Andy Greend636e352013-01-29 12:36:17 +08001639#ifdef LWS_LATENCY
1640void
Andy Greenb5b23192013-02-11 17:13:32 +08001641lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1642 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001643{
1644 struct timeval tv;
1645 unsigned long u;
1646 char buf[256];
1647
1648 gettimeofday(&tv, NULL);
1649
1650 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1651
1652 if (action) {
1653 if (completed) {
1654 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001655 sprintf(buf,
1656 "Completion first try lat %luus: %p: ret %d: %s\n",
1657 u - wsi->latency_start,
1658 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001659 else
Andy Greenb5b23192013-02-11 17:13:32 +08001660 sprintf(buf,
1661 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1662 u - wsi->action_start,
1663 u - wsi->latency_start,
1664 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001665 wsi->action_start = 0;
1666 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001667 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1668 u - wsi->latency_start,
1669 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001670 if (u - wsi->latency_start > context->worst_latency) {
1671 context->worst_latency = u - wsi->latency_start;
1672 strcpy(context->worst_latency_info, buf);
1673 }
1674 lwsl_latency("%s", buf);
1675 } else {
1676 wsi->latency_start = u;
1677 if (!wsi->action_start)
1678 wsi->action_start = u;
1679 }
1680}
1681#endif
1682
Andy Greena1ce6be2013-01-18 11:43:21 +08001683#ifdef LWS_NO_SERVER
1684int
Andy Green8d15cf42013-10-24 21:47:06 +08001685_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
Andy Greena1ce6be2013-01-18 11:43:21 +08001686{
1687 return 0;
1688}
1689#else
Andy Green706961d2013-01-17 16:50:35 +08001690int
1691_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1692{
1693 struct libwebsocket_context *context = wsi->protocol->owning_server;
Andy Green706961d2013-01-17 16:50:35 +08001694
Andy Greenca0a1292013-03-16 11:24:23 +08001695 /* there is no pending change */
1696 if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Green706961d2013-01-17 16:50:35 +08001697 return 0;
1698
Andy Greenca0a1292013-03-16 11:24:23 +08001699 /* stuff is still buffered, not ready to really accept new input */
1700 if (wsi->u.ws.rxflow_buffer) {
1701 /* get ourselves called back to deal with stashed buffer */
1702 libwebsocket_callback_on_writable(context, wsi);
1703 return 0;
Andy Green706961d2013-01-17 16:50:35 +08001704 }
1705
Andy Greenca0a1292013-03-16 11:24:23 +08001706 /* pending is cleared, we can change rxflow state */
1707
1708 wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1709
1710 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1711 wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW);
1712
1713 /* adjust the pollfd for this wsi */
1714
1715 if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
Andy Green91f19d82013-12-21 11:18:34 +08001716 lws_change_pollfd(wsi, 0, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001717 else
Andy Green91f19d82013-12-21 11:18:34 +08001718 lws_change_pollfd(wsi, POLLIN, 0);
Andy Green7a132792013-12-18 09:48:26 +08001719
Andy Green706961d2013-01-17 16:50:35 +08001720 return 1;
1721}
Andy Greena1ce6be2013-01-18 11:43:21 +08001722#endif
Andy Green706961d2013-01-17 16:50:35 +08001723
Andy Green90c7cbc2011-01-27 06:26:52 +00001724/**
1725 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1726 * receieved packets.
1727 *
1728 * If the output side of a server process becomes choked, this allows flow
1729 * control for the input side.
1730 *
1731 * @wsi: Websocket connection instance to get callback for
1732 * @enable: 0 = disable read servicing for this connection, 1 = enable
1733 */
1734
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001735LWS_VISIBLE int
Andy Green90c7cbc2011-01-27 06:26:52 +00001736libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1737{
Andy Greenca0a1292013-03-16 11:24:23 +08001738 if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW))
1739 return 0;
1740
1741 lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable);
1742 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001743
Andy Green706961d2013-01-17 16:50:35 +08001744 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001745}
1746
Andy Greenb55451c2013-03-16 12:32:27 +08001747/**
1748 * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
1749 *
1750 * When the user server code realizes it can accept more input, it can
1751 * call this to have the RX flow restriction removed from all connections using
1752 * the given protocol.
1753 *
1754 * @protocol: all connections using this protocol will be allowed to receive
1755 */
1756
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001757LWS_VISIBLE void
Andy Greenb55451c2013-03-16 12:32:27 +08001758libwebsocket_rx_flow_allow_all_protocol(
1759 const struct libwebsocket_protocols *protocol)
1760{
1761 struct libwebsocket_context *context = protocol->owning_server;
1762 int n;
1763 struct libwebsocket *wsi;
1764
1765 for (n = 0; n < context->fds_count; n++) {
1766 wsi = context->lws_lookup[context->fds[n].fd];
1767 if (!wsi)
1768 continue;
1769 if (wsi->protocol == protocol)
1770 libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1771 }
1772}
1773
Andy Green706961d2013-01-17 16:50:35 +08001774
Andy Green2ac5a6f2011-01-28 10:00:18 +00001775/**
1776 * libwebsocket_canonical_hostname() - returns this host's hostname
1777 *
1778 * This is typically used by client code to fill in the host parameter
1779 * when making a client connection. You can only call it after the context
1780 * has been created.
1781 *
Peter Hinz56885f32011-03-02 22:03:47 +00001782 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001783 */
1784
1785
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001786LWS_VISIBLE extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001787libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001788{
Peter Hinz56885f32011-03-02 22:03:47 +00001789 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001790}
1791
1792
Andy Green90c7cbc2011-01-27 06:26:52 +00001793static void sigpipe_handler(int x)
1794{
1795}
1796
Andy Green6901cb32011-02-21 08:06:47 +00001797#ifdef LWS_OPENSSL_SUPPORT
1798static int
1799OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1800{
1801
1802 SSL *ssl;
1803 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001804 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001805
1806 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1807 SSL_get_ex_data_X509_STORE_CTX_idx());
1808
1809 /*
Andy Green2e24da02011-03-05 16:12:04 +00001810 * !!! nasty openssl requires the index to come as a library-scope
1811 * static
Andy Green6901cb32011-02-21 08:06:47 +00001812 */
Andy Green2e24da02011-03-05 16:12:04 +00001813 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001814
Peter Hinz56885f32011-03-02 22:03:47 +00001815 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001816 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1817 x509_ctx, ssl, preverify_ok);
1818
1819 /* convert return code from 0 = OK to 1 = OK */
1820
1821 if (!n)
1822 n = 1;
1823 else
1824 n = 0;
1825
1826 return n;
1827}
1828#endif
1829
Andy Green706961d2013-01-17 16:50:35 +08001830int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001831 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001832 struct libwebsocket *wsi,
1833 enum libwebsocket_callback_reasons reason, void *user,
1834 void *in, size_t len)
1835{
1836 int n;
1837
1838 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001839 if (!n)
1840 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001841
Andy Greenaedc9532013-02-10 21:21:24 +08001842 return n;
Andy Green706961d2013-01-17 16:50:35 +08001843}
1844
Andy Green3b3fa9e2013-12-25 16:34:37 +08001845/*
1846 * This is just used to interrupt poll waiting
1847 * we don't have to do anything with it.
1848 */
arnaudviala7c6f26c2014-02-15 14:39:40 +08001849#ifdef LWS_OPENSSL_SUPPORT
Andy Green3b3fa9e2013-12-25 16:34:37 +08001850static void lws_sigusr2(int sig)
1851{
1852}
arnaudviala7c6f26c2014-02-15 14:39:40 +08001853#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001854
Andy Greenab990e42010-10-31 12:42:52 +00001855/**
Andy Green4739e5c2011-01-22 12:51:57 +00001856 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001857 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001858 *
Andy Green1b265272013-02-09 14:01:09 +08001859 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001860 * of all initialization in one step.
1861 *
Andy Greene92cd172011-01-19 13:11:55 +00001862 * After initialization, it returns a struct libwebsocket_context * that
1863 * represents this server. After calling, user code needs to take care
1864 * of calling libwebsocket_service() with the context pointer to get the
1865 * server's sockets serviced. This can be done in the same process context
1866 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001867 *
Andy Green8f037e42010-12-19 22:13:26 +00001868 * The protocol callback functions are called for a handful of events
1869 * including http requests coming in, websocket connections becoming
1870 * established, and data arriving; it's also called periodically to allow
1871 * async transmission.
1872 *
1873 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1874 * at that time websocket protocol has not been negotiated. Other
1875 * protocols after the first one never see any HTTP callack activity.
1876 *
1877 * The server created is a simple http server by default; part of the
1878 * websocket standard is upgrading this http connection to a websocket one.
1879 *
1880 * This allows the same server to provide files like scripts and favicon /
1881 * images or whatever over http and dynamic data over websockets all in
1882 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001883 */
Andy Green4ea60062010-10-30 12:15:07 +01001884
Peter Pentchev9a4fef72013-03-30 09:52:21 +08001885LWS_VISIBLE struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001886libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001887{
Peter Hinz56885f32011-03-02 22:03:47 +00001888 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001889 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001890 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001891#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001892 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001893 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001894 struct sockaddr_in serv_addr;
1895#endif
Andy Green3182ece2013-01-20 17:08:31 +08001896#ifndef LWS_NO_EXTENSIONS
1897 int m;
Andy Green1b265272013-02-09 14:01:09 +08001898 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001899#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001900
Andy Green3faa9c72010-11-08 17:03:03 +00001901#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001902 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001903#endif
1904
Joakim Soderberg4c531232013-02-06 15:26:58 +09001905#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001906 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001907#endif
1908
Andy Greenb3a614a2013-01-19 13:08:17 +08001909 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001910 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001911 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001912 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001913#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001914 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1915 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001916#else
1917 lwsl_notice(" Configured without extension support\n");
1918#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001919 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1920 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
Andy Green2672fb22013-02-22 09:54:35 +08001921 if (info->ssl_cipher_list)
1922 lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
Andy Greenc0d6b632013-01-12 23:42:17 +08001923 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1924 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001925
Peter Hinz56885f32011-03-02 22:03:47 +00001926#ifdef _WIN32
1927 {
1928 WORD wVersionRequested;
1929 WSADATA wsaData;
1930 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001931 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001932
1933 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1934 wVersionRequested = MAKEWORD(2, 2);
1935
1936 err = WSAStartup(wVersionRequested, &wsaData);
1937 if (err != 0) {
1938 /* Tell the user that we could not find a usable */
1939 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001940 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001941 return NULL;
1942 }
David Galeano7b11fec2011-10-04 19:55:18 +08001943
Andy Green6ee372f2012-04-09 15:09:01 +08001944 /* default to a poll() made out of select() */
1945 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001946
Andy Green6ee372f2012-04-09 15:09:01 +08001947 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001948 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001949 if (wsdll)
1950 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001951
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001952 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001953 if (!poll)
1954 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001955 }
1956#endif
1957
Andy Greenb5b23192013-02-11 17:13:32 +08001958 context = (struct libwebsocket_context *)
1959 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001960 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001961 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001962 return NULL;
1963 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001964 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001965#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001966 context->started_with_parent = pid_daemon;
1967 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1968#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001969
Joakim Soderberg4c531232013-02-06 15:26:58 +09001970 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001971 context->protocols = info->protocols;
1972 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001973 context->http_proxy_port = 0;
1974 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001975 context->options = info->options;
Mattias Lundberg03bb8f92014-02-18 10:06:57 +01001976 context->iface = info->iface;
Andy Greendfb23042013-01-17 12:26:48 +08001977 /* to reduce this allocation, */
1978 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001979 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1980 sizeof(struct libwebsocket_context),
1981 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1982 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001983 sizeof(struct libwebsocket_context) +
1984 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1985 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001986
Andy Greenb5b23192013-02-11 17:13:32 +08001987 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1988 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001989 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001990 lwsl_err("Unable to allocate fds array for %d connections\n",
1991 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001992 free(context);
1993 return NULL;
1994 }
Andy Greenb5b23192013-02-11 17:13:32 +08001995 context->lws_lookup = (struct libwebsocket **)
1996 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001997 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001998 lwsl_err(
1999 "Unable to allocate lws_lookup array for %d connections\n",
2000 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08002001 free(context->fds);
2002 free(context);
2003 return NULL;
2004 }
Andy Greenb1a5e6c2013-03-09 12:34:30 +08002005 memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) *
2006 context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08002007
Peter Hinz56885f32011-03-02 22:03:47 +00002008 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002009#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08002010 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08002011#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08002012 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08002013 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00002014
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002015#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002016 context->fd_random = 0;
2017#else
2018 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
2019 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002020 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002021 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002022 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00002023 }
Peter Hinz56885f32011-03-02 22:03:47 +00002024#endif
Andy Green44eee682011-02-10 09:32:24 +00002025
Peter Hinz56885f32011-03-02 22:03:47 +00002026#ifdef LWS_OPENSSL_SUPPORT
2027 context->use_ssl = 0;
James Devine5b34c972013-12-14 11:41:29 +08002028 context->allow_non_ssl_on_ssl_port = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002029 context->ssl_ctx = NULL;
2030 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00002031 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00002032#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00002033
Andy Greena1ce6be2013-01-18 11:43:21 +08002034 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08002035
Andy Greena1ce6be2013-01-18 11:43:21 +08002036#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002037 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01002038 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08002039 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08002040 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01002041
Andy Greenb5b23192013-02-11 17:13:32 +08002042 lwsl_notice(" canonical_hostname = %s\n",
2043 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08002044 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002045#endif
Andy Greena69f0512012-05-03 12:32:38 +08002046
Andy Green9659f372011-01-27 22:01:43 +00002047 /* split the proxy ads:port if given */
2048
2049 p = getenv("http_proxy");
2050 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00002051 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08002052 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00002053 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08002054 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00002055
Peter Hinz56885f32011-03-02 22:03:47 +00002056 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00002057 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002058 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002059 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00002060 }
2061 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00002062 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00002063
Andy Greenb3a614a2013-01-19 13:08:17 +08002064 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00002065 context->http_proxy_address,
2066 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00002067 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002068
Andy Greena1ce6be2013-01-18 11:43:21 +08002069#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08002070 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002071
Andy Green3faa9c72010-11-08 17:03:03 +00002072#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08002073 context->use_ssl = info->ssl_cert_filepath != NULL &&
2074 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09002075#ifdef USE_CYASSL
2076 lwsl_notice(" Compiled with CYASSL support\n");
2077#else
2078 lwsl_notice(" Compiled with OpenSSL support\n");
2079#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002080 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09002081 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002082 else
Andy Green23c5f2e2013-02-06 15:43:00 +09002083 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00002084
Andy Green90c7cbc2011-01-27 06:26:52 +00002085#else
Andy Green2b40b792013-02-10 22:22:01 +08002086 if (info->ssl_cert_filepath != NULL &&
2087 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08002088 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002089 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002090 }
Andy Greenb5b23192013-02-11 17:13:32 +08002091 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00002092#endif
Andy Greena17c6922013-01-20 20:21:54 +08002093
Andy Greenb5b23192013-02-11 17:13:32 +08002094 lwsl_notice(
2095 " per-conn mem: %u + %u headers + protocol rx buf\n",
2096 sizeof(struct libwebsocket),
2097 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00002098 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002099#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002100
2101 /* ignore SIGPIPE */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002102#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002103#else
Andy Green90c7cbc2011-01-27 06:26:52 +00002104 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00002105#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00002106
2107
2108#ifdef LWS_OPENSSL_SUPPORT
2109
2110 /* basic openssl init */
2111
2112 SSL_library_init();
2113
2114 OpenSSL_add_all_algorithms();
2115 SSL_load_error_strings();
2116
Andy Green2e24da02011-03-05 16:12:04 +00002117 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00002118 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
2119
Andy Green90c7cbc2011-01-27 06:26:52 +00002120 /*
2121 * Firefox insists on SSLv23 not SSLv3
2122 * Konq disables SSLv2 by default now, SSLv23 works
2123 */
2124
2125 method = (SSL_METHOD *)SSLv23_server_method();
2126 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002127 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002128 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002129 error,
2130 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002131 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002132 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002133 }
Peter Hinz56885f32011-03-02 22:03:47 +00002134 context->ssl_ctx = SSL_CTX_new(method); /* create context */
2135 if (!context->ssl_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002136 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002137 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002138 error,
2139 ERR_error_string(error,
2140 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002141 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00002142 }
2143
martell19c73f32014-01-22 18:17:03 +00002144#if defined(WIN32) || defined(_WIN32)
2145#else
Andy Green3b3fa9e2013-12-25 16:34:37 +08002146 signal(SIGUSR2, lws_sigusr2);
Andy Greened451d52014-01-11 12:37:07 +08002147 {
2148 sigset_t mask;
2149 sigemptyset (&mask);
2150 sigaddset (&mask, SIGUSR2);
2151
2152 sigprocmask(SIG_BLOCK, &mask, NULL);
2153 }
martell19c73f32014-01-22 18:17:03 +00002154#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002155
David Galeanocc148e42013-01-10 10:18:59 +08002156#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08002157 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002158#endif
David Galeano77a677c2013-01-10 10:14:12 +08002159 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002160 if (info->ssl_cipher_list)
2161 SSL_CTX_set_cipher_list(context->ssl_ctx,
2162 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002163
Andy Greena1ce6be2013-01-18 11:43:21 +08002164#ifndef LWS_NO_CLIENT
2165
Andy Green90c7cbc2011-01-27 06:26:52 +00002166 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08002167
Andy Green1b265272013-02-09 14:01:09 +08002168 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00002169 method = (SSL_METHOD *)SSLv23_client_method();
2170 if (!method) {
Jontie01cb9a2013-11-22 13:14:26 +02002171 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002172 lwsl_err("problem creating ssl method %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002173 error,
2174 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002175 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002176 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002177 }
2178 /* create context */
2179 context->ssl_client_ctx = SSL_CTX_new(method);
2180 if (!context->ssl_client_ctx) {
Jontie01cb9a2013-11-22 13:14:26 +02002181 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002182 lwsl_err("problem creating ssl context %lu: %s\n",
Jontie01cb9a2013-11-22 13:14:26 +02002183 error,
2184 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002185 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002186 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00002187 }
Andy Green90c7cbc2011-01-27 06:26:52 +00002188
David Galeanocc148e42013-01-10 10:18:59 +08002189#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08002190 SSL_CTX_set_options(context->ssl_client_ctx,
2191 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08002192#endif
Andy Greenb5b23192013-02-11 17:13:32 +08002193 SSL_CTX_set_options(context->ssl_client_ctx,
2194 SSL_OP_CIPHER_SERVER_PREFERENCE);
Andy Green2672fb22013-02-22 09:54:35 +08002195 if (info->ssl_cipher_list)
2196 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
2197 info->ssl_cipher_list);
David Galeanoc72f6f92013-01-10 10:11:57 +08002198
Peter Hinz56885f32011-03-02 22:03:47 +00002199 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08002200 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08002201 if (!SSL_CTX_load_verify_locations(
2202 context->ssl_client_ctx, NULL,
2203 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08002204 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08002205 "Unable to load SSL Client certs from %s "
2206 "(set by --with-client-cert-dir= "
2207 "in configure) -- client ssl isn't "
2208 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08002209 } else
2210 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08002211 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08002212 NULL))
Andy Green43db0452013-01-10 19:50:35 +08002213 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08002214 "Unable to load SSL Client certs "
2215 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08002216 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00002217
2218 /*
2219 * callback allowing user code to load extra verification certs
2220 * helping the client to verify server identity
2221 */
2222
prasannateamf1b353a452013-11-12 17:21:01 -08002223 /* support for client-side certificate authentication */
2224 if (info->ssl_cert_filepath) {
2225 n = SSL_CTX_use_certificate_chain_file(
2226 context->ssl_client_ctx,
2227 info->ssl_cert_filepath);
2228 if (n != 1) {
2229 lwsl_err("problem getting cert '%s' %lu: %s\n",
2230 info->ssl_cert_filepath,
2231 ERR_get_error(),
2232 ERR_error_string(ERR_get_error(),
2233 (char *)context->service_buffer));
2234 goto bail;
2235 }
2236 }
2237 if (info->ssl_private_key_filepath) {
2238 /* set the private key from KeyFile */
2239 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
2240 info->ssl_private_key_filepath,
2241 SSL_FILETYPE_PEM) != 1) {
2242 lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
2243 info->ssl_private_key_filepath,
2244 ERR_get_error(),
2245 ERR_error_string(ERR_get_error(),
2246 (char *)context->service_buffer));
2247 goto bail;
2248 }
2249
2250 /* verify private key */
2251 if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) {
2252 lwsl_err("Private SSL key doesn't match cert\n");
2253 goto bail;
2254 }
2255 }
2256
Peter Hinz56885f32011-03-02 22:03:47 +00002257 context->protocols[0].callback(context, NULL,
2258 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
2259 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00002260 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002261#endif
Andy Green6ee372f2012-04-09 15:09:01 +08002262
Andy Greenc6bf2c22011-02-20 11:10:47 +00002263 /* as a server, are we requiring clients to identify themselves? */
2264
Andy Greenb5b23192013-02-11 17:13:32 +08002265 if (info->options &
2266 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00002267
2268 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08002269
Peter Hinz56885f32011-03-02 22:03:47 +00002270 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00002271 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
2272 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002273
2274 /*
2275 * give user code a chance to load certs into the server
2276 * allowing it to verify incoming client certs
2277 */
2278
Peter Hinz56885f32011-03-02 22:03:47 +00002279 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00002280 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00002281 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00002282 }
2283
James Devine5b34c972013-12-14 11:41:29 +08002284 if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
2285 /* Normally SSL listener rejects non-ssl, optionally allow */
2286 context->allow_non_ssl_on_ssl_port = 1;
2287 }
2288
Peter Hinz56885f32011-03-02 22:03:47 +00002289 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00002290
2291 /* openssl init for server sockets */
2292
Andy Green3faa9c72010-11-08 17:03:03 +00002293 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08002294 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002295 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00002296 if (n != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002297 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002298 lwsl_err("problem getting cert '%s' %lu: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002299 info->ssl_cert_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002300 error,
2301 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002302 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002303 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002304 }
2305 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002306 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002307 info->ssl_private_key_filepath,
2308 SSL_FILETYPE_PEM) != 1) {
Jontie01cb9a2013-11-22 13:14:26 +02002309 int error = ERR_get_error();
Joakim Soderbergb82b0dd2013-02-22 09:28:15 +08002310 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002311 info->ssl_private_key_filepath,
Jontie01cb9a2013-11-22 13:14:26 +02002312 error,
2313 ERR_error_string(error,
Andy Greenb5b23192013-02-11 17:13:32 +08002314 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002315 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002316 }
2317 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002318 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002319 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002320 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002321 }
2322
2323 /* SSL is happy and has a cert it's content with */
2324 }
2325#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002326
Andy Greena1ce6be2013-01-18 11:43:21 +08002327#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002328 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002329
Andy Green1b265272013-02-09 14:01:09 +08002330 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002331 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002332
Andy Green4739e5c2011-01-22 12:51:57 +00002333 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2334 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002335 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002336 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002337 }
Andy Green775c0dd2010-10-29 14:15:22 +01002338
Andy Greenb5b23192013-02-11 17:13:32 +08002339 /*
2340 * allow us to restart even if old sockets in TIME_WAIT
Andy Greenb5b23192013-02-11 17:13:32 +08002341 */
Andy Green6ee372f2012-04-09 15:09:01 +08002342 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2343 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002344
2345 /* Disable Nagle */
2346 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002347 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2348 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002349
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002350 #if defined(WIN32) || defined(_WIN32)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002351 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002352 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002353 #else
Andy Greene2160712013-01-28 12:19:10 +08002354 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002355 #endif
Andy Greene2160712013-01-28 12:19:10 +08002356
Andy Green4739e5c2011-01-22 12:51:57 +00002357 bzero((char *) &serv_addr, sizeof(serv_addr));
2358 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002359 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002360 serv_addr.sin_addr.s_addr = INADDR_ANY;
2361 else
Andy Greend1eac602013-11-09 08:07:38 +08002362 if (interface_to_sa(info->iface, &serv_addr,
2363 sizeof(serv_addr)) < 0) {
2364 lwsl_err("Unable to find interface %s\n",
2365 info->iface);
2366 compatible_close(sockfd);
2367 goto bail;
2368 }
Andy Green1b265272013-02-09 14:01:09 +08002369 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002370
2371 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2372 sizeof(serv_addr));
2373 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002374 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002375 info->port, n, errno);
Andy Green8b2d6f02013-10-22 06:49:30 +08002376 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002377 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002378 }
Andy Green0d338332011-02-12 11:57:43 +00002379
Andy Greenb5b23192013-02-11 17:13:32 +08002380 wsi = (struct libwebsocket *)malloc(
2381 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002382 if (wsi == NULL) {
2383 lwsl_err("Out of mem\n");
Andy Green8b2d6f02013-10-22 06:49:30 +08002384 compatible_close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002385 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002386 }
Andy Greenb5b23192013-02-11 17:13:32 +08002387 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002388 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002389#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002390 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002391#endif
Andy Green0d338332011-02-12 11:57:43 +00002392 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002393
2394 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002395
Andy Green65b0e912013-01-16 07:59:47 +08002396 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2397 context->listen_service_count = 0;
2398 context->listen_service_fd = sockfd;
2399
Andy Greena824d182013-01-15 20:52:29 +08002400 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002401 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002402 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002403#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002404
Andy Green6ee372f2012-04-09 15:09:01 +08002405 /*
2406 * drop any root privs for this process
2407 * to listen on port < 1023 we would have needed root, but now we are
2408 * listening, we don't want the power for anything else
2409 */
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002410#if defined(WIN32) || defined(_WIN32)
Peter Hinz56885f32011-03-02 22:03:47 +00002411#else
Andy Green1b265272013-02-09 14:01:09 +08002412 if (info->gid != -1)
2413 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002414 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002415 if (info->uid != -1)
2416 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002417 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002418#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002419
Andy Green6f520a52013-01-29 17:57:39 +08002420 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002421
Peter Hinz56885f32011-03-02 22:03:47 +00002422 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002423 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002424 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002425
Andy Green43db0452013-01-10 19:50:35 +08002426 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002427 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002428
Andy Green1b265272013-02-09 14:01:09 +08002429 info->protocols[context->count_protocols].owning_server =
2430 context;
2431 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002432 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002433
2434 /*
2435 * inform all the protocols that they are doing their one-time
2436 * initialization if they want to
2437 */
2438 info->protocols[context->count_protocols].callback(context,
2439 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002440 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002441
Andy Green3182ece2013-01-20 17:08:31 +08002442#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002443 /*
2444 * give all extensions a chance to create any per-context
2445 * allocations they need
2446 */
2447
2448 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002449 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002450 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002451
Andy Green1b265272013-02-09 14:01:09 +08002452 if (info->extensions) {
2453 ext = info->extensions;
2454 while (ext->callback) {
2455 lwsl_ext(" Extension: %s\n", ext->name);
2456 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002457 (enum libwebsocket_extension_callback_reasons)m,
2458 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002459 ext++;
2460 }
Andy Greena41314f2011-05-23 10:00:03 +01002461 }
Andy Green3182ece2013-01-20 17:08:31 +08002462#endif
Andy Green3b3fa9e2013-12-25 16:34:37 +08002463
Peter Hinz56885f32011-03-02 22:03:47 +00002464 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002465
2466bail:
2467 libwebsocket_context_destroy(context);
2468 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002469}
Andy Greenb45993c2010-12-18 15:13:50 +00002470
Andy Greenb45993c2010-12-18 15:13:50 +00002471/**
shysb4e800e2013-10-24 22:12:03 +08002472 * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context.
2473 * @context: pointer to struct libwebsocket_context you want set proxy to
2474 * @proxy: pointer to c string containing proxy in format address:port
2475 *
2476 * Returns 0 if proxy string was parsed and proxy was setup.
2477 * Returns -1 if @proxy is NULL or has incorrect format.
2478 *
2479 * This is only required if your OS does not provide the http_proxy
2480 * enviroment variable (eg, OSX)
2481 *
2482 * IMPORTANT! You should call this function right after creation of the
2483 * libwebsocket_context and before call to connect. If you call this
2484 * function after connect behavior is undefined.
2485 * This function will override proxy settings made on libwebsocket_context
2486 * creation with genenv() call.
2487 */
2488
2489LWS_VISIBLE int
2490libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy)
2491{
2492 char *p;
2493
2494 if (!proxy)
2495 return -1;
2496
2497 strncpy(context->http_proxy_address, proxy,
2498 sizeof(context->http_proxy_address) - 1);
2499 context->http_proxy_address[
2500 sizeof(context->http_proxy_address) - 1] = '\0';
2501
2502 p = strchr(context->http_proxy_address, ':');
2503 if (!p) {
2504 lwsl_err("http_proxy needs to be ads:port\n");
2505
2506 return -1;
2507 }
2508 *p = '\0';
2509 context->http_proxy_port = atoi(p + 1);
2510
2511 lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address,
2512 context->http_proxy_port);
2513
2514 return 0;
2515}
2516
2517/**
Andy Greenb45993c2010-12-18 15:13:50 +00002518 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002519 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002520 * @wsi: pointer to struct websocket you want to know the protocol of
2521 *
Andy Green8f037e42010-12-19 22:13:26 +00002522 *
Andy Green6f520a52013-01-29 17:57:39 +08002523 * Some apis can act on all live connections of a given protocol,
2524 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002525 */
Andy Greenab990e42010-10-31 12:42:52 +00002526
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002527LWS_VISIBLE const struct libwebsocket_protocols *
Andy Greenb45993c2010-12-18 15:13:50 +00002528libwebsockets_get_protocol(struct libwebsocket *wsi)
2529{
2530 return wsi->protocol;
2531}
2532
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002533LWS_VISIBLE int
Andy Green82c3d542011-03-07 21:16:31 +00002534libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2535{
Andy Green623a98d2013-01-21 11:04:23 +08002536 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002537}
Alex Bligh49146db2011-11-07 17:19:25 +08002538
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002539LWS_VISIBLE unsigned char
David Galeanoe2cf9922013-01-09 18:06:55 +08002540libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2541{
Andy Green623a98d2013-01-21 11:04:23 +08002542 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002543}
2544
Andy Green2af4d5b2013-02-18 16:30:10 +08002545int
Alex Bligh49146db2011-11-07 17:19:25 +08002546libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2547{
Andy Green67d556c2013-02-15 22:31:55 +08002548 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002549 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002550
Alex Bligh49146db2011-11-07 17:19:25 +08002551 /* allocate the per-connection user memory (if any) */
2552
2553 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2554 wsi->user_space = malloc(
2555 wsi->protocol->per_session_data_size);
2556 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002557 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002558 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002559 }
Andy Green6ee372f2012-04-09 15:09:01 +08002560 memset(wsi->user_space, 0,
2561 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002562 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002563 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002564}
Andy Green43db0452013-01-10 19:50:35 +08002565
Andy Green0b319092013-01-19 11:17:56 +08002566static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002567{
Andy Green0b319092013-01-19 11:17:56 +08002568 char buf[300];
2569 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002570 int n;
2571
2572 gettimeofday(&tv, NULL);
2573
2574 buf[0] = '\0';
2575 for (n = 0; n < LLL_COUNT; n++)
2576 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002577 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002578 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002579 break;
2580 }
Andy Greenb5b23192013-02-11 17:13:32 +08002581
Andy Green0b319092013-01-19 11:17:56 +08002582 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002583}
2584
Andreas Pakulat68bd4bd2013-10-28 15:18:04 +01002585#if defined(WIN32) || defined(_WIN32)
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002586LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Joakim Soderberg4c531232013-02-06 15:26:58 +09002587{
2588 lwsl_emit_stderr(level, line);
2589}
2590#else
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002591LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
Andy Greenc11db202013-01-19 11:12:16 +08002592{
2593 int syslog_level = LOG_DEBUG;
2594
2595 switch (level) {
2596 case LLL_ERR:
2597 syslog_level = LOG_ERR;
2598 break;
2599 case LLL_WARN:
2600 syslog_level = LOG_WARNING;
2601 break;
2602 case LLL_NOTICE:
2603 syslog_level = LOG_NOTICE;
2604 break;
2605 case LLL_INFO:
2606 syslog_level = LOG_INFO;
2607 break;
2608 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002609 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002610}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002611#endif
Andy Greenc11db202013-01-19 11:12:16 +08002612
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002613LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
Andy Green43db0452013-01-10 19:50:35 +08002614{
Andy Greende8f27a2013-01-12 09:17:42 +08002615 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002616 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002617
2618 if (!(log_level & filter))
2619 return;
2620
Andy Green43db0452013-01-10 19:50:35 +08002621 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002622 vsnprintf(buf, sizeof(buf), format, ap);
2623 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002624 va_end(ap);
2625
Andy Green0b319092013-01-19 11:17:56 +08002626 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002627}
2628
2629/**
2630 * lws_set_log_level() - Set the logging bitfield
2631 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002632 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2633 * function to perform log string emission instead of
2634 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002635 *
Andy Greenc6511a02013-02-19 10:01:48 +08002636 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002637 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002638 */
2639
Peter Pentchev9a4fef72013-03-30 09:52:21 +08002640LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level,
Andy Greenb5b23192013-02-11 17:13:32 +08002641 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002642{
2643 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002644 if (log_emit_function)
2645 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002646}
Andy Green92f9b162014-02-21 18:43:42 +08002647
2648int
2649interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen)
2650{
2651 int rc = -1;
2652#if defined(WIN32) || defined(_WIN32)
2653 /* TODO */
2654#else
2655 struct ifaddrs *ifr;
2656 struct ifaddrs *ifc;
2657 struct sockaddr_in *sin;
2658
2659 getifaddrs(&ifr);
2660 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
2661 if (ifc->ifa_addr == NULL)
2662 continue;
2663 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
2664 if (strcmp(ifc->ifa_name, ifname))
2665 continue;
2666 sin = (struct sockaddr_in *)ifc->ifa_addr;
2667 if (sin->sin_family != AF_INET)
2668 continue;
2669 memcpy(addr, sin, addrlen);
2670 rc = 0;
2671 }
2672
2673 freeifaddrs(ifr);
2674#endif
2675 return rc;
2676}
2677