blob: 3cdc6fa00b5d9f15fb5f53417c548942373286b5 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Peter Hinz56885f32011-03-02 22:03:47 +000024#ifdef WIN32
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010027#include <mstcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000028#else
Davidc4ef7b12013-01-12 20:39:47 +080029#ifdef LWS_BUILTIN_GETIFADDRS
30#include <getifaddrs.h>
31#else
Peter Hinz56885f32011-03-02 22:03:47 +000032#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080033#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090034#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000035#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080036#include <sys/socket.h>
37#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000038#endif
Andy Green2e24da02011-03-05 16:12:04 +000039
40#ifdef LWS_OPENSSL_SUPPORT
41int openssl_websocket_private_data_index;
42#endif
43
Andy Greenaa6fc442012-04-12 13:26:49 +080044#ifdef __MINGW32__
45#include "../win32port/win32helpers/websock-w32.c"
46#else
47#ifdef __MINGW64__
48#include "../win32port/win32helpers/websock-w32.c"
49#endif
50#endif
51
Andy Green7b405452013-02-01 10:50:15 +080052#ifndef LWS_BUILD_HASH
53#define LWS_BUILD_HASH "unknown-build-hash"
54#endif
Andy Green3182ece2013-01-20 17:08:31 +080055
Andy Green7c19c342013-01-19 12:18:07 +080056static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080057static void lwsl_emit_stderr(int level, const char *line);
58static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
59
Andy Green7b405452013-02-01 10:50:15 +080060static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
61
Andy Greenb5b23192013-02-11 17:13:32 +080062static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080063 "ERR",
64 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080065 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080066 "INFO",
67 "DEBUG",
68 "PARSER",
69 "HEADER",
70 "EXTENSION",
71 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080072 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080073};
74
Andy Greenb5b23192013-02-11 17:13:32 +080075#ifndef LWS_NO_CLIENT
76 extern int lws_client_socket_service(
77 struct libwebsocket_context *context,
78 struct libwebsocket *wsi, struct pollfd *pollfd);
79#endif
80#ifndef LWS_NO_SERVER
81 extern int lws_server_socket_service(
82 struct libwebsocket_context *context,
83 struct libwebsocket *wsi, struct pollfd *pollfd);
84#endif
85
Andy Green7b405452013-02-01 10:50:15 +080086/**
87 * lws_get_library_version: get version and git hash library built from
88 *
89 * returns a const char * to a string like "1.1 178d78c"
90 * representing the library version followed by the git head hash it
91 * was built from
92 */
93
94const char *
95lws_get_library_version(void)
96{
97 return library_version;
98}
99
Andy Green0d338332011-02-12 11:57:43 +0000100int
Andy Greenb5b23192013-02-11 17:13:32 +0800101insert_wsi_socket_into_fds(struct libwebsocket_context *context,
102 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000103{
Andy Greendfb23042013-01-17 12:26:48 +0800104 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800105 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000106 return 1;
107 }
108
Andy Greendfb23042013-01-17 12:26:48 +0800109 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800110 lwsl_err("Socket fd %d is too high (%d)\n",
111 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800112 return 1;
113 }
114
115 assert(wsi);
116 assert(wsi->sock);
117
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
119 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800120
121 context->lws_lookup[wsi->sock] = wsi;
122 wsi->position_in_fds_table = context->fds_count;
123 context->fds[context->fds_count].fd = wsi->sock;
124 context->fds[context->fds_count].events = POLLIN;
125 context->fds[context->fds_count++].revents = 0;
126
127 /* external POLL support via protocol 0 */
128 context->protocols[0].callback(context, wsi,
129 LWS_CALLBACK_ADD_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800130 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000131
132 return 0;
133}
134
Andy Greendfb23042013-01-17 12:26:48 +0800135static int
Andy Greenb5b23192013-02-11 17:13:32 +0800136remove_wsi_socket_from_fds(struct libwebsocket_context *context,
137 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000138{
Andy Greendfb23042013-01-17 12:26:48 +0800139 int m;
Andy Green0d338332011-02-12 11:57:43 +0000140
Andy Greendfb23042013-01-17 12:26:48 +0800141 if (!--context->fds_count)
142 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000143
Andy Greendfb23042013-01-17 12:26:48 +0800144 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800145 lwsl_err("Socket fd %d too high (%d)\n",
146 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800147 return 1;
148 }
Andy Green0d338332011-02-12 11:57:43 +0000149
Andy Greenb5b23192013-02-11 17:13:32 +0800150 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
151 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800152
153 m = wsi->position_in_fds_table; /* replace the contents for this */
154
155 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800156 context->fds[m] = context->fds[context->fds_count];
157 /*
158 * end guy's fds_lookup entry remains unchanged
159 * (still same fd pointing to same wsi)
160 */
Andy Greendfb23042013-01-17 12:26:48 +0800161 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800162 context->lws_lookup[context->fds[context->fds_count].fd]->
163 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800164 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800165 context->lws_lookup[wsi->sock] = NULL;
166 /* removed wsi has no position any more */
167 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800168
169do_ext:
170 /* remove also from external POLL support via protocol 0 */
171 if (wsi->sock)
172 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800173 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
174 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800175
176 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000177}
178
Andy Green32375b72011-02-19 08:32:53 +0000179
Andy Green8f037e42010-12-19 22:13:26 +0000180void
Peter Hinz56885f32011-03-02 22:03:47 +0000181libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000182 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000183{
Andy Greenb45993c2010-12-18 15:13:50 +0000184 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000185 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000186 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
187 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800188#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000189 int ret;
190 int m;
191 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100192 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800193#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000194
Andy Green4b6fbe12011-02-14 08:03:48 +0000195 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000196 return;
197
Andy Green62c54d22011-02-14 09:14:25 +0000198 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000199
Andy Green62c54d22011-02-14 09:14:25 +0000200 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000201 return;
202
Andy Green22524a62013-02-15 22:48:58 +0800203 /* we tried the polite way... */
204 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
205 goto just_kill_connection;
206
Andy Green623a98d2013-01-21 11:04:23 +0800207 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000208
Andy Greenb8b247d2013-01-22 07:20:08 +0800209 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
210 close(wsi->u.http.fd);
211 wsi->u.http.fd = 0;
212 }
213
Andy Green3182ece2013-01-20 17:08:31 +0800214#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000215 /*
Andy Green68b45042011-05-25 21:41:57 +0100216 * are his extensions okay with him closing? Eg he might be a mux
217 * parent and just his ch1 aspect is closing?
218 */
219
Andy Green68b45042011-05-25 21:41:57 +0100220 for (n = 0; n < wsi->count_active_extensions; n++) {
221 if (!wsi->active_extensions[n]->callback)
222 continue;
223
224 m = wsi->active_extensions[n]->callback(context,
225 wsi->active_extensions[n], wsi,
226 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
227 wsi->active_extensions_user[n], NULL, 0);
228
229 /*
230 * if somebody vetoed actually closing him at this time....
231 * up to the extension to track the attempted close, let's
232 * just bail
233 */
234
235 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800236 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100237 return;
238 }
239 }
240
Andy Green68b45042011-05-25 21:41:57 +0100241 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000242 * flush any tx pending from extensions, since we may send close packet
243 * if there are problems with send, just nuke the connection
244 */
245
246 ret = 1;
247 while (ret == 1) {
248
249 /* default to nobody has more to spill */
250
251 ret = 0;
252 eff_buf.token = NULL;
253 eff_buf.token_len = 0;
254
255 /* show every extension the new incoming data */
256
257 for (n = 0; n < wsi->count_active_extensions; n++) {
258 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000259 wsi->protocol->owning_server,
260 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000261 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
262 wsi->active_extensions_user[n], &eff_buf, 0);
263 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800264 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000265 goto just_kill_connection;
266 }
267 if (m)
268 /*
269 * at least one extension told us he has more
270 * to spill, so we will go around again after
271 */
272 ret = 1;
273 }
274
275 /* assuming they left us something to send, send it */
276
277 if (eff_buf.token_len)
278 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenb5b23192013-02-11 17:13:32 +0800279 eff_buf.token_len)) {
280 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000281 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800282 }
Andy Greenc44159f2011-03-07 07:08:18 +0000283 }
Andy Green3182ece2013-01-20 17:08:31 +0800284#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000285
286 /*
Andy Greenda527df2011-03-07 07:08:12 +0000287 * signal we are closing, libsocket_write will
288 * add any necessary version-specific stuff. If the write fails,
289 * no worries we are closing anyway. If we didn't initiate this
290 * close, then our state has been changed to
291 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
292 *
293 * Likewise if it's a second call to close this connection after we
294 * sent the close indication to the peer already, we are in state
295 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
296 */
297
298 if (old_state == WSI_STATE_ESTABLISHED &&
299 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100300
Andy Green43db0452013-01-10 19:50:35 +0800301 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100302
Andy Greenfdd305a2013-02-11 14:32:48 +0800303 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800304 memset(buf, 0, sizeof(buf));
305 n = libwebsocket_write(wsi,
306 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000307 0, LWS_WRITE_CLOSE);
308 if (!n) {
309 /*
310 * we have sent a nice protocol level indication we
311 * now wish to close, we should not send anything more
312 */
313
314 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
315
Andy Greenb5b23192013-02-11 17:13:32 +0800316 /*
317 * ...and we should wait for a reply for a bit
318 * out of politeness
319 */
Andy Greenda527df2011-03-07 07:08:12 +0000320
321 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800322 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000323
Andy Green43db0452013-01-10 19:50:35 +0800324 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000325
326 return;
327 }
328
Andy Greenb5b23192013-02-11 17:13:32 +0800329 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800330
Andy Greenda527df2011-03-07 07:08:12 +0000331 /* else, the send failed and we should just hang up */
332 }
333
Andy Green3182ece2013-01-20 17:08:31 +0800334#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000335just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800336#endif
Andy Green66a16f32011-05-24 22:07:45 +0100337
Andy Greenb5b23192013-02-11 17:13:32 +0800338 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100339
Andy Greenda527df2011-03-07 07:08:12 +0000340 /*
341 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800342 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000343 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000344
Andy Greendfb23042013-01-17 12:26:48 +0800345 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000346
Andy Green251f6fa2010-11-03 11:13:06 +0000347 wsi->state = WSI_STATE_DEAD_SOCKET;
348
Andy Greenb5b23192013-02-11 17:13:32 +0800349 if ((old_state == WSI_STATE_ESTABLISHED ||
350 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800351 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
352
353 if (wsi->u.ws.rx_user_buffer) {
354 free(wsi->u.ws.rx_user_buffer);
355 wsi->u.ws.rx_user_buffer = NULL;
356 }
357 if (wsi->u.ws.rxflow_buffer) {
358 free(wsi->u.ws.rxflow_buffer);
359 wsi->u.ws.rxflow_buffer = NULL;
360 }
Andy Green54495112013-02-06 21:10:16 +0900361 }
362
Andy Green4b6fbe12011-02-14 08:03:48 +0000363 /* tell the user it's all over for this guy */
364
Andy Greend4302732011-02-28 07:45:29 +0000365 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800366 ((old_state == WSI_STATE_ESTABLISHED) ||
367 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
368 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800369 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000370 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000371 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800372 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800373 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000374
Andy Green3182ece2013-01-20 17:08:31 +0800375#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000376 /* deallocate any active extension contexts */
377
378 for (n = 0; n < wsi->count_active_extensions; n++) {
379 if (!wsi->active_extensions[n]->callback)
380 continue;
381
Andy Green46c2ea02011-03-22 09:04:01 +0000382 wsi->active_extensions[n]->callback(context,
383 wsi->active_extensions[n], wsi,
384 LWS_EXT_CALLBACK_DESTROY,
385 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000386
387 free(wsi->active_extensions_user[n]);
388 }
389
Andy Greena41314f2011-05-23 10:00:03 +0100390 /*
391 * inform all extensions in case they tracked this guy out of band
392 * even though not active on him specifically
393 */
394
395 ext = context->extensions;
396 while (ext && ext->callback) {
397 ext->callback(context, ext, wsi,
398 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
399 NULL, NULL, 0);
400 ext++;
401 }
Andy Green3182ece2013-01-20 17:08:31 +0800402#endif
Andy Greena41314f2011-05-23 10:00:03 +0100403
Andy Green43db0452013-01-10 19:50:35 +0800404/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000405
Andy Green3faa9c72010-11-08 17:03:03 +0000406#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000407 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000408 n = SSL_get_fd(wsi->ssl);
409 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800410 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000411 SSL_free(wsi->ssl);
412 } else {
413#endif
Andy Green0303db42013-01-17 14:46:43 +0800414 if (wsi->sock) {
415 n = shutdown(wsi->sock, SHUT_RDWR);
416 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800417 lwsl_debug("closing: shutdown returned %d\n",
418 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800419
Andy Green0303db42013-01-17 14:46:43 +0800420 n = compatible_close(wsi->sock);
421 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800422 lwsl_debug("closing: close returned %d\n",
423 errno);
Andy Green0303db42013-01-17 14:46:43 +0800424 }
Andy Green3faa9c72010-11-08 17:03:03 +0000425#ifdef LWS_OPENSSL_SUPPORT
426 }
427#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800428 if (wsi->protocol && wsi->protocol->per_session_data_size &&
429 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000430 free(wsi->user_space);
431
Andy Green251f6fa2010-11-03 11:13:06 +0000432 free(wsi);
433}
434
Andy Green07034092011-02-13 08:37:12 +0000435/**
436 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800437 * @context: Libwebsockets context
438 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000439 * @fd: Connection socket descriptor
440 * @name: Buffer to take client address name
441 * @name_len: Length of client address name buffer
442 * @rip: Buffer to take client address IP qotted quad
443 * @rip_len: Length of client address IP buffer
444 *
445 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800446 * the client connected with socket descriptor @fd. Names may be
447 * truncated if there is not enough room. If either cannot be
448 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000449 */
450
451void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800452libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
453 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000454 char *rip, int rip_len)
455{
456 unsigned int len;
457 struct sockaddr_in sin;
458 struct hostent *host;
459 struct hostent *host1;
460 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000461 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000462 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800463 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800464#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800465 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800466#endif
Andy Green07034092011-02-13 08:37:12 +0000467
468 rip[0] = '\0';
469 name[0] = '\0';
470
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800471 lws_latency_pre(context, wsi);
472
Andy Greenb5b23192013-02-11 17:13:32 +0800473 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000474 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
475 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800476 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000477 }
Andy Green6ee372f2012-04-09 15:09:01 +0800478
Andy Greenb5b23192013-02-11 17:13:32 +0800479 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000480 AF_INET);
481 if (host == NULL) {
482 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800483 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000484 }
485
486 strncpy(name, host->h_name, name_len);
487 name[name_len - 1] = '\0';
488
489 host1 = gethostbyname(host->h_name);
490 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800491 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000492 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000493 n = 0;
494 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000495 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000496 if (p == NULL)
497 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000498 if ((host1->h_addrtype != AF_INET)
499#ifdef AF_LOCAL
500 && (host1->h_addrtype != AF_LOCAL)
501#endif
502 )
Andy Green07034092011-02-13 08:37:12 +0000503 continue;
504
Andy Green7627af52011-03-09 15:13:52 +0000505 if (host1->h_addrtype == AF_INET)
506 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000507#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000508 else {
509 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800510 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000511 ip[sizeof(ip) - 1] = '\0';
512 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000513#endif
Andy Green07034092011-02-13 08:37:12 +0000514 p = NULL;
515 strncpy(rip, ip, rip_len);
516 rip[rip_len - 1] = '\0';
517 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800518
519 ret = 0;
520bail:
521 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000522}
Andy Green9f990342011-02-12 11:57:45 +0000523
Peter Hinz56885f32011-03-02 22:03:47 +0000524int libwebsockets_get_random(struct libwebsocket_context *context,
525 void *buf, int len)
526{
527 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800528 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000529
530#ifdef WIN32
531 for (n = 0; n < len; n++)
532 p[n] = (unsigned char)rand();
533#else
534 n = read(context->fd_random, p, len);
535#endif
536
537 return n;
538}
539
Andy Greena690cd02013-02-09 12:25:31 +0800540int lws_set_socket_options(struct libwebsocket_context *context, int fd)
541{
542 int optval = 1;
543 socklen_t optlen = sizeof(optval);
544#ifdef WIN32
545 unsigned long optl = 0;
546#endif
547#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
548 struct protoent *tcp_proto;
549#endif
550
551 if (context->ka_time) {
552 /* enable keepalive on this socket */
553 optval = 1;
554 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
555 (const void *)&optval, optlen) < 0)
556 return 1;
557
Andy Greena47865f2013-02-10 09:39:47 +0800558#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
559
560 /*
561 * didn't find a way to set these per-socket, need to
562 * tune kernel systemwide values
563 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100564#elif WIN32
565 {
566 DWORD dwBytesRet;
567 struct tcp_keepalive alive;
568 alive.onoff = TRUE;
569 alive.keepalivetime = context->ka_time;
570 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800571
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100572 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
573 NULL, 0, &dwBytesRet, NULL, NULL))
574 return 1;
575 }
Andy Greena47865f2013-02-10 09:39:47 +0800576#else
Andy Greena690cd02013-02-09 12:25:31 +0800577 /* set the keepalive conditions we want on it too */
578 optval = context->ka_time;
579 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
580 (const void *)&optval, optlen) < 0)
581 return 1;
582
583 optval = context->ka_probes;
584 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
585 (const void *)&optval, optlen) < 0)
586 return 1;
587
588 optval = context->ka_interval;
589 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
590 (const void *)&optval, optlen) < 0)
591 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800592#endif
Andy Greena690cd02013-02-09 12:25:31 +0800593 }
594
595 /* Disable Nagle */
596 optval = 1;
597#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
598 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
599#else
600 tcp_proto = getprotobyname("TCP");
601 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
602#endif
603
604 /* We are nonblocking... */
605#ifdef WIN32
606 ioctlsocket(fd, FIONBIO, &optl);
607#else
608 fcntl(fd, F_SETFL, O_NONBLOCK);
609#endif
610
611 return 0;
612}
613
Andy Green95a7b5d2011-03-06 10:29:39 +0000614int lws_send_pipe_choked(struct libwebsocket *wsi)
615{
616 struct pollfd fds;
617
618 fds.fd = wsi->sock;
619 fds.events = POLLOUT;
620 fds.revents = 0;
621
622 if (poll(&fds, 1, 0) != 1)
623 return 1;
624
625 if ((fds.revents & POLLOUT) == 0)
626 return 1;
627
628 /* okay to send another packet without blocking */
629
630 return 0;
631}
632
Andy Greena41314f2011-05-23 10:00:03 +0100633int
Andy Green3b84c002011-03-06 13:14:42 +0000634lws_handle_POLLOUT_event(struct libwebsocket_context *context,
635 struct libwebsocket *wsi, struct pollfd *pollfd)
636{
Andy Green3b84c002011-03-06 13:14:42 +0000637 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800638
Andy Green3182ece2013-01-20 17:08:31 +0800639#ifndef LWS_NO_EXTENSIONS
640 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000641 int ret;
642 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100643 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000644
Andy Greena41314f2011-05-23 10:00:03 +0100645 for (n = 0; n < wsi->count_active_extensions; n++) {
646 if (!wsi->active_extensions[n]->callback)
647 continue;
648
649 m = wsi->active_extensions[n]->callback(context,
650 wsi->active_extensions[n], wsi,
651 LWS_EXT_CALLBACK_IS_WRITEABLE,
652 wsi->active_extensions_user[n], NULL, 0);
653 if (m > handled)
654 handled = m;
655 }
656
657 if (handled == 1)
658 goto notify_action;
659
660 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000661 goto user_service;
662
663 /*
664 * check in on the active extensions, see if they
665 * had pending stuff to spill... they need to get the
666 * first look-in otherwise sequence will be disordered
667 *
668 * NULL, zero-length eff_buf means just spill pending
669 */
670
671 ret = 1;
672 while (ret == 1) {
673
674 /* default to nobody has more to spill */
675
676 ret = 0;
677 eff_buf.token = NULL;
678 eff_buf.token_len = 0;
679
680 /* give every extension a chance to spill */
681
682 for (n = 0; n < wsi->count_active_extensions; n++) {
683 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000684 wsi->protocol->owning_server,
685 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000686 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
687 wsi->active_extensions_user[n], &eff_buf, 0);
688 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800689 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000690 return -1;
691 }
692 if (m)
693 /*
694 * at least one extension told us he has more
695 * to spill, so we will go around again after
696 */
697 ret = 1;
698 }
699
700 /* assuming they gave us something to send, send it */
701
702 if (eff_buf.token_len) {
703 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
704 eff_buf.token_len))
705 return -1;
706 } else
707 continue;
708
709 /* no extension has more to spill */
710
711 if (!ret)
712 continue;
713
714 /*
715 * There's more to spill from an extension, but we just sent
716 * something... did that leave the pipe choked?
717 */
718
719 if (!lws_send_pipe_choked(wsi))
720 /* no we could add more */
721 continue;
722
Andy Green43db0452013-01-10 19:50:35 +0800723 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000724
725 /*
726 * Yes, he's choked. Leave the POLLOUT masked on so we will
727 * come back here when he is unchoked. Don't call the user
728 * callback to enforce ordering of spilling, he'll get called
729 * when we come back here and there's nothing more to spill.
730 */
731
732 return 0;
733 }
734
735 wsi->extension_data_pending = 0;
736
737user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800738#endif
Andy Green3b84c002011-03-06 13:14:42 +0000739 /* one shot */
740
Andy Greena41314f2011-05-23 10:00:03 +0100741 if (pollfd) {
742 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000743
Andy Greena41314f2011-05-23 10:00:03 +0100744 /* external POLL support via protocol 0 */
745 context->protocols[0].callback(context, wsi,
746 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800747 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100748 }
Andy Green3182ece2013-01-20 17:08:31 +0800749#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100750notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800751#endif
Andy Green3b84c002011-03-06 13:14:42 +0000752
Andy Green9e4c2b62011-03-07 20:47:39 +0000753 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
754 n = LWS_CALLBACK_CLIENT_WRITEABLE;
755 else
756 n = LWS_CALLBACK_SERVER_WRITEABLE;
757
Andy Greenb8b247d2013-01-22 07:20:08 +0800758 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800759 wsi, (enum libwebsocket_callback_reasons) n,
760 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000761}
762
763
764
Andy Greena41314f2011-05-23 10:00:03 +0100765void
766libwebsocket_service_timeout_check(struct libwebsocket_context *context,
767 struct libwebsocket *wsi, unsigned int sec)
768{
Andy Green3182ece2013-01-20 17:08:31 +0800769#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100770 int n;
771
772 /*
773 * if extensions want in on it (eg, we are a mux parent)
774 * give them a chance to service child timeouts
775 */
776
777 for (n = 0; n < wsi->count_active_extensions; n++)
778 wsi->active_extensions[n]->callback(
779 context, wsi->active_extensions[n],
780 wsi, LWS_EXT_CALLBACK_1HZ,
781 wsi->active_extensions_user[n], NULL, sec);
782
Andy Green3182ece2013-01-20 17:08:31 +0800783#endif
Andy Greena41314f2011-05-23 10:00:03 +0100784 if (!wsi->pending_timeout)
785 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800786
Andy Greena41314f2011-05-23 10:00:03 +0100787 /*
788 * if we went beyond the allowed time, kill the
789 * connection
790 */
791
792 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800793 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100794 libwebsocket_close_and_free_session(context,
795 wsi, LWS_CLOSE_STATUS_NOSTATUS);
796 }
797}
798
Andy Green9f990342011-02-12 11:57:45 +0000799/**
800 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000801 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000802 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800803 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000804 *
Andy Green75006172013-01-22 12:32:11 +0800805 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800806 * services it according to the state of the associated
807 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800808 *
809 * The one call deals with all "service" that might happen on a socket
810 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000811 */
812
813int
Peter Hinz56885f32011-03-02 22:03:47 +0000814libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000815 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000816{
Andy Greena1ce6be2013-01-18 11:43:21 +0800817 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000818 int n;
Andy Green0d338332011-02-12 11:57:43 +0000819 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800820 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000821 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800822
Andy Green3182ece2013-01-20 17:08:31 +0800823#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000824 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800825#endif
Andy Green98a717c2011-03-06 13:14:15 +0000826 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800827
828 if (context->listen_service_fd)
829 listen_socket_fds_index = context->lws_lookup[
830 context->listen_service_fd]->position_in_fds_table;
831
Andy Greena71eafc2011-02-14 17:59:43 +0000832 /*
833 * you can call us with pollfd = NULL to just allow the once-per-second
834 * global timeout checks; if less than a second since the last check
835 * it returns immediately then.
836 */
837
838 gettimeofday(&tv, NULL);
839
Peter Hinz56885f32011-03-02 22:03:47 +0000840 if (context->last_timeout_check_s != tv.tv_sec) {
841 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000842
Joakim Soderberg4c531232013-02-06 15:26:58 +0900843 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800844 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800845 if (context->started_with_parent &&
846 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800847 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900848 #endif
Andy Green24cba922013-01-19 13:56:10 +0800849
Andy Greena71eafc2011-02-14 17:59:43 +0000850 /* global timeout check once per second */
851
Peter Hinz56885f32011-03-02 22:03:47 +0000852 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +0800853 struct libwebsocket *new_wsi =
854 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +0800855 if (!new_wsi)
856 continue;
857 libwebsocket_service_timeout_check(context,
858 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000859 }
860 }
861
862 /* just here for timeout management? */
863
864 if (pollfd == NULL)
865 return 0;
866
867 /* no, here to service a socket descriptor */
868
Andy Green65b0e912013-01-16 07:59:47 +0800869 /*
870 * deal with listen service piggybacking
871 * every listen_service_modulo services of other fds, we
872 * sneak one in to service the listen socket if there's anything waiting
873 *
874 * To handle connection storms, as found in ab, if we previously saw a
875 * pending connection here, it causes us to check again next time.
876 */
877
Andy Greenb5b23192013-02-11 17:13:32 +0800878 if (context->listen_service_fd && pollfd !=
879 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800880 context->listen_service_count++;
881 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800882 context->listen_service_count ==
883 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800884 context->listen_service_count = 0;
885 m = 1;
886 if (context->listen_service_extraseen > 5)
887 m = 2;
888 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800889 /*
890 * even with extpoll, we prepared this
891 * internal fds for listen
892 */
893 n = poll(&context->fds[listen_socket_fds_index],
894 1, 0);
895 if (n > 0) { /* there's a conn waiting for us */
896 libwebsocket_service_fd(context,
897 &context->
898 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800899 context->listen_service_extraseen++;
900 } else {
901 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800902 context->
903 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800904 break;
905 }
906 }
907 }
908
909 }
910
911 /* okay, what we came here to do... */
912
Andy Greendfb23042013-01-17 12:26:48 +0800913 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800914 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800915 if (pollfd->fd > 11)
Andy Greenb5b23192013-02-11 17:13:32 +0800916 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n",
917 pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800918 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800919 }
Andy Green8f037e42010-12-19 22:13:26 +0000920
Andy Green0d338332011-02-12 11:57:43 +0000921 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800922
Andy Greena1ce6be2013-01-18 11:43:21 +0800923#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800924 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000925 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800926 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800927 return lws_server_socket_service(context, wsi, pollfd);
928#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000929
Andy Green0d338332011-02-12 11:57:43 +0000930 case LWS_CONNMODE_WS_SERVING:
931 case LWS_CONNMODE_WS_CLIENT:
932
933 /* handle session socket closed */
934
935 if (pollfd->revents & (POLLERR | POLLHUP)) {
936
Andy Green43db0452013-01-10 19:50:35 +0800937 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000938 (void *)wsi, pollfd->fd);
939
Peter Hinz56885f32011-03-02 22:03:47 +0000940 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000941 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800942 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000943 }
944
Andy Green0d338332011-02-12 11:57:43 +0000945 /* the guy requested a callback when it was OK to write */
946
Andy Greenda527df2011-03-07 07:08:12 +0000947 if ((pollfd->revents & POLLOUT) &&
948 wsi->state == WSI_STATE_ESTABLISHED)
949 if (lws_handle_POLLOUT_event(context, wsi,
950 pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800951 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenda527df2011-03-07 07:08:12 +0000952 libwebsocket_close_and_free_session(
Andy Greenb0593712013-02-15 22:32:53 +0800953 context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800954 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000955 }
Andy Green0d338332011-02-12 11:57:43 +0000956
Andy Green0d338332011-02-12 11:57:43 +0000957
958 /* any incoming data ready? */
959
960 if (!(pollfd->revents & POLLIN))
961 break;
962
Andy Greenb45993c2010-12-18 15:13:50 +0000963#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800964read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800965 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800966 eff_buf.token_len = SSL_read(wsi->ssl,
967 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800968 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800969 if (!eff_buf.token_len) {
970 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800971 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +0800972 ERR_error_string(n,
973 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800974 }
975 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000976#endif
Andy Greene84652c2013-02-08 13:01:02 +0800977 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +0800978 context->service_buffer,
979 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000980
Andy Green98a717c2011-03-06 13:14:15 +0000981 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800982 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000983 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200984 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800985 libwebsocket_close_and_free_session(context,
986 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800987 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000988 }
Andy Green98a717c2011-03-06 13:14:15 +0000989 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800990 lwsl_info("closing connection due to 0 length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000991 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800992 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800993 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000994 }
995
Andy Green98a717c2011-03-06 13:14:15 +0000996 /*
997 * give any active extensions a chance to munge the buffer
998 * before parse. We pass in a pointer to an lws_tokens struct
999 * prepared with the default buffer and content length that's in
1000 * there. Rather than rewrite the default buffer, extensions
1001 * that expect to grow the buffer can adapt .token to
1002 * point to their own per-connection buffer in the extension
1003 * user allocation. By default with no extensions or no
1004 * extension callback handling, just the normal input buffer is
1005 * used then so it is efficient.
1006 */
Andy Greenb45993c2010-12-18 15:13:50 +00001007
Andy Greene84652c2013-02-08 13:01:02 +08001008 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001009#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001010 more = 1;
1011 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001012
Andy Green98a717c2011-03-06 13:14:15 +00001013 more = 0;
1014
1015 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001016 m = wsi->active_extensions[n]->callback(context,
1017 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001018 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001019 wsi->active_extensions_user[n],
1020 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001021 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001022 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001023 "Extension reports fatal error\n");
1024 libwebsocket_close_and_free_session(
1025 context, wsi,
1026 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001027 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001028 }
1029 if (m)
1030 more = 1;
1031 }
Andy Green3182ece2013-01-20 17:08:31 +08001032#endif
Andy Green98a717c2011-03-06 13:14:15 +00001033 /* service incoming data */
1034
1035 if (eff_buf.token_len) {
1036 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001037 (unsigned char *)eff_buf.token,
1038 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001039 if (n < 0)
1040 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001041 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001042 }
Andy Green3182ece2013-01-20 17:08:31 +08001043#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001044 eff_buf.token = NULL;
1045 eff_buf.token_len = 0;
1046 }
Andy Green3182ece2013-01-20 17:08:31 +08001047#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001048
1049#ifdef LWS_OPENSSL_SUPPORT
1050 if (wsi->ssl && SSL_pending(wsi->ssl))
1051 goto read_pending;
1052#endif
Andy Green98a717c2011-03-06 13:14:15 +00001053 break;
Andy Green76f61e72013-01-16 11:53:05 +08001054
1055 default:
Andy Green03674a62013-01-16 11:47:40 +08001056#ifdef LWS_NO_CLIENT
1057 break;
1058#else
Andy Green76f61e72013-01-16 11:53:05 +08001059 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001060#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001061 }
1062
1063 return 0;
1064}
1065
Andy Green0d338332011-02-12 11:57:43 +00001066
Andy Green6964bb52011-01-23 16:50:33 +00001067/**
1068 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001069 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001070 *
1071 * This function closes any active connections and then frees the
1072 * context. After calling this, any further use of the context is
1073 * undefined.
1074 */
1075void
Peter Hinz56885f32011-03-02 22:03:47 +00001076libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001077{
Andy Green3182ece2013-01-20 17:08:31 +08001078#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001079 int n;
1080 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001081 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001082 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001083
Andy Greend636e352013-01-29 12:36:17 +08001084#ifdef LWS_LATENCY
1085 if (context->worst_latency_info[0])
1086 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1087#endif
1088
Andy Greendfb23042013-01-17 12:26:48 +08001089 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001090 struct libwebsocket *wsi =
1091 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001092 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001093 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1094 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001095 }
Andy Green6964bb52011-01-23 16:50:33 +00001096
Andy Greena41314f2011-05-23 10:00:03 +01001097 /*
1098 * give all extensions a chance to clean up any per-context
1099 * allocations they might have made
1100 */
1101
1102 ext = context->extensions;
1103 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1104 if (context->listen_port)
1105 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001106 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001107 ext->callback(context, ext, NULL,
1108 (enum libwebsocket_extension_callback_reasons)m,
1109 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001110 ext++;
1111 }
Andy Greena7109e62013-02-11 12:05:54 +08001112
1113 /*
1114 * inform all the protocols that they are done and will have no more
1115 * callbacks
1116 */
1117
1118 while (protocol->callback) {
1119 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1120 NULL, NULL, 0);
1121 protocol++;
1122 }
1123
Andy Green3182ece2013-01-20 17:08:31 +08001124#endif
Andy Greena41314f2011-05-23 10:00:03 +01001125
Peter Hinz56885f32011-03-02 22:03:47 +00001126#ifdef WIN32
1127#else
1128 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001129#endif
1130
Peter Hinz56885f32011-03-02 22:03:47 +00001131#ifdef LWS_OPENSSL_SUPPORT
1132 if (context->ssl_ctx)
1133 SSL_CTX_free(context->ssl_ctx);
1134 if (context->ssl_client_ctx)
1135 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001136
1137 ERR_remove_state(0);
1138 ERR_free_strings();
1139 EVP_cleanup();
1140 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001141#endif
1142
Andy Green46596482013-02-11 11:04:01 +08001143 if (context->fds)
1144 free(context->fds);
1145 if (context->lws_lookup)
1146 free(context->lws_lookup);
1147
Peter Hinz56885f32011-03-02 22:03:47 +00001148 free(context);
1149
1150#ifdef WIN32
1151 WSACleanup();
1152#endif
Andy Green6964bb52011-01-23 16:50:33 +00001153}
1154
Andy Greend88146d2013-01-22 12:40:35 +08001155/**
Andy Greenb5b23192013-02-11 17:13:32 +08001156 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001157 * @context: Websocket context
1158 *
1159 * This returns the optional user allocation that can be attached to
1160 * the context the sockets live in at context_create time. It's a way
1161 * to let all sockets serviced in the same context share data without
1162 * using globals statics in the user code.
1163 */
Alon Levy0291eb32012-10-19 11:21:56 +02001164LWS_EXTERN void *
1165libwebsocket_context_user(struct libwebsocket_context *context)
1166{
Andy Greenb5b23192013-02-11 17:13:32 +08001167 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001168}
1169
Andy Green6964bb52011-01-23 16:50:33 +00001170/**
1171 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001172 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001173 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1174 * service otherwise block and service immediately, returning
1175 * after the timeout if nothing needed service.
1176 *
1177 * This function deals with any pending websocket traffic, for three
1178 * kinds of event. It handles these events on both server and client
1179 * types of connection the same.
1180 *
1181 * 1) Accept new connections to our context's server
1182 *
Andy Green6f520a52013-01-29 17:57:39 +08001183 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001184 * server or client connections.
1185 *
1186 * You need to call this service function periodically to all the above
1187 * functions to happen; if your application is single-threaded you can
1188 * just call it in your main event loop.
1189 *
1190 * Alternatively you can fork a new process that asynchronously handles
1191 * calling this service in a loop. In that case you are happy if this
1192 * call blocks your thread until it needs to take care of something and
1193 * would call it with a large nonzero timeout. Your loop then takes no
1194 * CPU while there is nothing happening.
1195 *
1196 * If you are calling it in a single-threaded app, you don't want it to
1197 * wait around blocking other things in your loop from happening, so you
1198 * would call it with a timeout_ms of 0, so it returns immediately if
1199 * nothing is pending, or as soon as it services whatever was pending.
1200 */
1201
Andy Greene92cd172011-01-19 13:11:55 +00001202int
Peter Hinz56885f32011-03-02 22:03:47 +00001203libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001204{
1205 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001206
1207 /* stay dead once we are dead */
1208
Peter Hinz56885f32011-03-02 22:03:47 +00001209 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001210 return 1;
1211
Andy Green0d338332011-02-12 11:57:43 +00001212 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001213
Peter Hinz56885f32011-03-02 22:03:47 +00001214 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001215 if (n == 0) /* poll timeout */
1216 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001217
Andy Greendfb23042013-01-17 12:26:48 +08001218 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001219 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001220
Andy Greendfb23042013-01-17 12:26:48 +08001221 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001222
Peter Hinz56885f32011-03-02 22:03:47 +00001223 for (n = 0; n < context->fds_count; n++)
1224 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001225 if (libwebsocket_service_fd(context,
1226 &context->fds[n]) < 0)
1227 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001228 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001229}
1230
Andy Green3182ece2013-01-20 17:08:31 +08001231#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001232int
1233lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001234 struct libwebsocket *wsi,
1235 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001236 void *v, size_t len)
1237{
1238 int n;
1239 int handled = 0;
1240
1241 /* maybe an extension will take care of it for us */
1242
1243 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1244 if (!wsi->active_extensions[n]->callback)
1245 continue;
1246
1247 handled |= wsi->active_extensions[n]->callback(context,
1248 wsi->active_extensions[n], wsi,
1249 r, wsi->active_extensions_user[n], v, len);
1250 }
1251
1252 return handled;
1253}
1254
1255
1256void *
1257lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001258 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001259{
1260 int n = 0;
1261
Andy Green68b45042011-05-25 21:41:57 +01001262 if (wsi == NULL)
1263 return NULL;
1264
Andy Greena41314f2011-05-23 10:00:03 +01001265 while (n < wsi->count_active_extensions) {
1266 if (wsi->active_extensions[n] != ext) {
1267 n++;
1268 continue;
1269 }
1270 return wsi->active_extensions_user[n];
1271 }
1272
1273 return NULL;
1274}
Andy Green3182ece2013-01-20 17:08:31 +08001275#endif
Andy Greena41314f2011-05-23 10:00:03 +01001276
Andy Green90c7cbc2011-01-27 06:26:52 +00001277/**
1278 * libwebsocket_callback_on_writable() - Request a callback when this socket
1279 * becomes able to be written to without
1280 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001281 *
Peter Hinz56885f32011-03-02 22:03:47 +00001282 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001283 * @wsi: Websocket connection instance to get callback for
1284 */
1285
1286int
Peter Hinz56885f32011-03-02 22:03:47 +00001287libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001288 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001289{
Andy Green3182ece2013-01-20 17:08:31 +08001290#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001291 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001292 int handled = 0;
1293
1294 /* maybe an extension will take care of it for us */
1295
1296 for (n = 0; n < wsi->count_active_extensions; n++) {
1297 if (!wsi->active_extensions[n]->callback)
1298 continue;
1299
1300 handled |= wsi->active_extensions[n]->callback(context,
1301 wsi->active_extensions[n], wsi,
1302 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1303 wsi->active_extensions_user[n], NULL, 0);
1304 }
1305
1306 if (handled)
1307 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001308#endif
Andy Greendfb23042013-01-17 12:26:48 +08001309 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001310 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1311 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001312 return -1;
1313 }
1314
1315 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001316
Andy Green3221f922011-02-12 13:14:11 +00001317 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001318 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001319 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001320 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001321
Andy Green90c7cbc2011-01-27 06:26:52 +00001322 return 1;
1323}
1324
1325/**
1326 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1327 * all connections using the given protocol when it
1328 * becomes possible to write to each socket without
1329 * blocking in turn.
1330 *
1331 * @protocol: Protocol whose connections will get callbacks
1332 */
1333
1334int
1335libwebsocket_callback_on_writable_all_protocol(
1336 const struct libwebsocket_protocols *protocol)
1337{
Peter Hinz56885f32011-03-02 22:03:47 +00001338 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001339 int n;
Andy Green0d338332011-02-12 11:57:43 +00001340 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001341
Andy Greendfb23042013-01-17 12:26:48 +08001342 for (n = 0; n < context->fds_count; n++) {
1343 wsi = context->lws_lookup[context->fds[n].fd];
1344 if (!wsi)
1345 continue;
1346 if (wsi->protocol == protocol)
1347 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001348 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001349
1350 return 0;
1351}
1352
Andy Greenbe93fef2011-02-14 20:25:43 +00001353/**
1354 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1355 *
1356 * You will not need this unless you are doing something special
1357 *
1358 * @wsi: Websocket connection instance
1359 * @reason: timeout reason
1360 * @secs: how many seconds
1361 */
1362
1363void
1364libwebsocket_set_timeout(struct libwebsocket *wsi,
1365 enum pending_timeout reason, int secs)
1366{
1367 struct timeval tv;
1368
1369 gettimeofday(&tv, NULL);
1370
1371 wsi->pending_timeout_limit = tv.tv_sec + secs;
1372 wsi->pending_timeout = reason;
1373}
1374
Andy Greena6cbece2011-01-27 20:06:03 +00001375
1376/**
1377 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1378 *
1379 * You will not need this unless you are doing something special
1380 *
1381 * @wsi: Websocket connection instance
1382 */
1383
1384int
1385libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1386{
1387 return wsi->sock;
1388}
1389
Andy Greend636e352013-01-29 12:36:17 +08001390#ifdef LWS_LATENCY
1391void
Andy Greenb5b23192013-02-11 17:13:32 +08001392lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1393 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001394{
1395 struct timeval tv;
1396 unsigned long u;
1397 char buf[256];
1398
1399 gettimeofday(&tv, NULL);
1400
1401 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1402
1403 if (action) {
1404 if (completed) {
1405 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001406 sprintf(buf,
1407 "Completion first try lat %luus: %p: ret %d: %s\n",
1408 u - wsi->latency_start,
1409 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001410 else
Andy Greenb5b23192013-02-11 17:13:32 +08001411 sprintf(buf,
1412 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1413 u - wsi->action_start,
1414 u - wsi->latency_start,
1415 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001416 wsi->action_start = 0;
1417 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001418 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1419 u - wsi->latency_start,
1420 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001421 if (u - wsi->latency_start > context->worst_latency) {
1422 context->worst_latency = u - wsi->latency_start;
1423 strcpy(context->worst_latency_info, buf);
1424 }
1425 lwsl_latency("%s", buf);
1426 } else {
1427 wsi->latency_start = u;
1428 if (!wsi->action_start)
1429 wsi->action_start = u;
1430 }
1431}
1432#endif
1433
Andy Greena1ce6be2013-01-18 11:43:21 +08001434#ifdef LWS_NO_SERVER
1435int
1436_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1437{
1438 return 0;
1439}
1440#else
Andy Green706961d2013-01-17 16:50:35 +08001441int
1442_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1443{
1444 struct libwebsocket_context *context = wsi->protocol->owning_server;
1445 int n;
1446
Andy Green623a98d2013-01-21 11:04:23 +08001447 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001448 return 0;
1449
Andy Green623a98d2013-01-21 11:04:23 +08001450 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001451
Andy Greenb5b23192013-02-11 17:13:32 +08001452 lwsl_info("rxflow: wsi %p change_to %d\n",
1453 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001454
1455 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001456 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001457 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1458 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001459 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001460 return -1;
1461 }
1462 if (n)
1463 /* oh he stuck again, do nothing */
1464 return 0;
1465 }
1466
Andy Green623a98d2013-01-21 11:04:23 +08001467 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001468 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1469 else
1470 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1471
Andy Green623a98d2013-01-21 11:04:23 +08001472 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001473 /* external POLL support via protocol 0 */
1474 context->protocols[0].callback(context, wsi,
1475 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001476 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001477 else
1478 /* external POLL support via protocol 0 */
1479 context->protocols[0].callback(context, wsi,
1480 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001481 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001482
1483 return 1;
1484}
Andy Greena1ce6be2013-01-18 11:43:21 +08001485#endif
Andy Green706961d2013-01-17 16:50:35 +08001486
Andy Green90c7cbc2011-01-27 06:26:52 +00001487/**
1488 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1489 * receieved packets.
1490 *
1491 * If the output side of a server process becomes choked, this allows flow
1492 * control for the input side.
1493 *
1494 * @wsi: Websocket connection instance to get callback for
1495 * @enable: 0 = disable read servicing for this connection, 1 = enable
1496 */
1497
1498int
1499libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1500{
Andy Green623a98d2013-01-21 11:04:23 +08001501 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001502
Andy Green706961d2013-01-17 16:50:35 +08001503 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001504}
1505
Andy Green706961d2013-01-17 16:50:35 +08001506
Andy Green2ac5a6f2011-01-28 10:00:18 +00001507/**
1508 * libwebsocket_canonical_hostname() - returns this host's hostname
1509 *
1510 * This is typically used by client code to fill in the host parameter
1511 * when making a client connection. You can only call it after the context
1512 * has been created.
1513 *
Peter Hinz56885f32011-03-02 22:03:47 +00001514 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001515 */
1516
1517
1518extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001519libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001520{
Peter Hinz56885f32011-03-02 22:03:47 +00001521 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001522}
1523
1524
Andy Green90c7cbc2011-01-27 06:26:52 +00001525static void sigpipe_handler(int x)
1526{
1527}
1528
Andy Green6901cb32011-02-21 08:06:47 +00001529#ifdef LWS_OPENSSL_SUPPORT
1530static int
1531OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1532{
1533
1534 SSL *ssl;
1535 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001536 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001537
1538 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1539 SSL_get_ex_data_X509_STORE_CTX_idx());
1540
1541 /*
Andy Green2e24da02011-03-05 16:12:04 +00001542 * !!! nasty openssl requires the index to come as a library-scope
1543 * static
Andy Green6901cb32011-02-21 08:06:47 +00001544 */
Andy Green2e24da02011-03-05 16:12:04 +00001545 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001546
Peter Hinz56885f32011-03-02 22:03:47 +00001547 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001548 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1549 x509_ctx, ssl, preverify_ok);
1550
1551 /* convert return code from 0 = OK to 1 = OK */
1552
1553 if (!n)
1554 n = 1;
1555 else
1556 n = 0;
1557
1558 return n;
1559}
1560#endif
1561
Andy Green706961d2013-01-17 16:50:35 +08001562int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001563 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001564 struct libwebsocket *wsi,
1565 enum libwebsocket_callback_reasons reason, void *user,
1566 void *in, size_t len)
1567{
1568 int n;
1569
1570 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001571 if (!n)
1572 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001573
Andy Greenaedc9532013-02-10 21:21:24 +08001574 return n;
Andy Green706961d2013-01-17 16:50:35 +08001575}
1576
Andy Greenb45993c2010-12-18 15:13:50 +00001577
Andy Greenab990e42010-10-31 12:42:52 +00001578/**
Andy Green4739e5c2011-01-22 12:51:57 +00001579 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001580 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001581 *
Andy Green1b265272013-02-09 14:01:09 +08001582 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001583 * of all initialization in one step.
1584 *
Andy Greene92cd172011-01-19 13:11:55 +00001585 * After initialization, it returns a struct libwebsocket_context * that
1586 * represents this server. After calling, user code needs to take care
1587 * of calling libwebsocket_service() with the context pointer to get the
1588 * server's sockets serviced. This can be done in the same process context
1589 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001590 *
Andy Green8f037e42010-12-19 22:13:26 +00001591 * The protocol callback functions are called for a handful of events
1592 * including http requests coming in, websocket connections becoming
1593 * established, and data arriving; it's also called periodically to allow
1594 * async transmission.
1595 *
1596 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1597 * at that time websocket protocol has not been negotiated. Other
1598 * protocols after the first one never see any HTTP callack activity.
1599 *
1600 * The server created is a simple http server by default; part of the
1601 * websocket standard is upgrading this http connection to a websocket one.
1602 *
1603 * This allows the same server to provide files like scripts and favicon /
1604 * images or whatever over http and dynamic data over websockets all in
1605 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001606 */
Andy Green4ea60062010-10-30 12:15:07 +01001607
Andy Greene92cd172011-01-19 13:11:55 +00001608struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001609libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001610{
Peter Hinz56885f32011-03-02 22:03:47 +00001611 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001612 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001613#ifndef LWS_NO_SERVER
Andy Green14f47292013-02-11 19:36:15 +08001614 int n;
Andy Greencbb31222013-01-31 09:57:05 +08001615 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001616 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001617 struct sockaddr_in serv_addr;
1618#endif
Andy Green3182ece2013-01-20 17:08:31 +08001619#ifndef LWS_NO_EXTENSIONS
1620 int m;
Andy Green1b265272013-02-09 14:01:09 +08001621 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001622#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001623
Andy Green3faa9c72010-11-08 17:03:03 +00001624#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001625 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001626#endif
1627
Joakim Soderberg4c531232013-02-06 15:26:58 +09001628#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001629 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001630#endif
1631
Andy Greenb3a614a2013-01-19 13:08:17 +08001632 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001633 lwsl_notice("Library version: %s\n", library_version);
Andy Greenb5b23192013-02-11 17:13:32 +08001634 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n",
1635 LWS_MAX_HEADER_NAME_LENGTH);
Andy Greenc0d6b632013-01-12 23:42:17 +08001636 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001637 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001638#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001639 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1640 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001641#else
1642 lwsl_notice(" Configured without extension support\n");
1643#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001644 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1645 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1646 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1647 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1648 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001649
Peter Hinz56885f32011-03-02 22:03:47 +00001650#ifdef _WIN32
1651 {
1652 WORD wVersionRequested;
1653 WSADATA wsaData;
1654 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001655 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001656
1657 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1658 wVersionRequested = MAKEWORD(2, 2);
1659
1660 err = WSAStartup(wVersionRequested, &wsaData);
1661 if (err != 0) {
1662 /* Tell the user that we could not find a usable */
1663 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001664 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001665 return NULL;
1666 }
David Galeano7b11fec2011-10-04 19:55:18 +08001667
Andy Green6ee372f2012-04-09 15:09:01 +08001668 /* default to a poll() made out of select() */
1669 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001670
Andy Green6ee372f2012-04-09 15:09:01 +08001671 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001672 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001673 if (wsdll)
1674 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001675
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001676 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001677 if (!poll)
1678 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001679 }
1680#endif
1681
Andy Greenb5b23192013-02-11 17:13:32 +08001682 context = (struct libwebsocket_context *)
1683 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001684 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001685 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001686 return NULL;
1687 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001688 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001689#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001690 context->started_with_parent = pid_daemon;
1691 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1692#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001693
Joakim Soderberg4c531232013-02-06 15:26:58 +09001694 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001695 context->protocols = info->protocols;
1696 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001697 context->http_proxy_port = 0;
1698 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001699 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001700 /* to reduce this allocation, */
1701 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001702 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1703 sizeof(struct libwebsocket_context),
1704 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1705 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001706 sizeof(struct libwebsocket_context) +
1707 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1708 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001709
Andy Greenb5b23192013-02-11 17:13:32 +08001710 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1711 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001712 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001713 lwsl_err("Unable to allocate fds array for %d connections\n",
1714 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001715 free(context);
1716 return NULL;
1717 }
Andy Greenb5b23192013-02-11 17:13:32 +08001718 context->lws_lookup = (struct libwebsocket **)
1719 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001720 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001721 lwsl_err(
1722 "Unable to allocate lws_lookup array for %d connections\n",
1723 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001724 free(context->fds);
1725 free(context);
1726 return NULL;
1727 }
Andy Greena17c6922013-01-20 20:21:54 +08001728
Peter Hinz56885f32011-03-02 22:03:47 +00001729 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001730#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001731 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001732#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001733 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001734 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001735
Peter Hinz56885f32011-03-02 22:03:47 +00001736#ifdef WIN32
1737 context->fd_random = 0;
1738#else
1739 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1740 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001741 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001742 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001743 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001744 }
Peter Hinz56885f32011-03-02 22:03:47 +00001745#endif
Andy Green44eee682011-02-10 09:32:24 +00001746
Peter Hinz56885f32011-03-02 22:03:47 +00001747#ifdef LWS_OPENSSL_SUPPORT
1748 context->use_ssl = 0;
1749 context->ssl_ctx = NULL;
1750 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001751 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001752#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001753
Andy Greena1ce6be2013-01-18 11:43:21 +08001754 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001755
Andy Greena1ce6be2013-01-18 11:43:21 +08001756#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001757 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001758 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001759 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001760 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001761
Andy Greenb5b23192013-02-11 17:13:32 +08001762 lwsl_notice(" canonical_hostname = %s\n",
1763 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001764 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001765#endif
Andy Greena69f0512012-05-03 12:32:38 +08001766
Andy Green9659f372011-01-27 22:01:43 +00001767 /* split the proxy ads:port if given */
1768
1769 p = getenv("http_proxy");
1770 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001771 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001772 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001773 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001774 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001775
Peter Hinz56885f32011-03-02 22:03:47 +00001776 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001777 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001778 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001779 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001780 }
1781 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001782 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001783
Andy Greenb3a614a2013-01-19 13:08:17 +08001784 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001785 context->http_proxy_address,
1786 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001787 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001788
Andy Greena1ce6be2013-01-18 11:43:21 +08001789#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001790 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001791
Andy Green3faa9c72010-11-08 17:03:03 +00001792#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001793 context->use_ssl = info->ssl_cert_filepath != NULL &&
1794 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001795#ifdef USE_CYASSL
1796 lwsl_notice(" Compiled with CYASSL support\n");
1797#else
1798 lwsl_notice(" Compiled with OpenSSL support\n");
1799#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001800 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001801 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001802 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001803 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001804
Andy Green90c7cbc2011-01-27 06:26:52 +00001805#else
Andy Green2b40b792013-02-10 22:22:01 +08001806 if (info->ssl_cert_filepath != NULL &&
1807 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001808 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001809 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001810 }
Andy Greenb5b23192013-02-11 17:13:32 +08001811 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001812#endif
Andy Greena17c6922013-01-20 20:21:54 +08001813
Andy Greenb5b23192013-02-11 17:13:32 +08001814 lwsl_notice(
1815 " per-conn mem: %u + %u headers + protocol rx buf\n",
1816 sizeof(struct libwebsocket),
1817 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001818 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001819#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001820
1821 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001822#ifdef WIN32
1823#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001824 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001825#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001826
1827
1828#ifdef LWS_OPENSSL_SUPPORT
1829
1830 /* basic openssl init */
1831
1832 SSL_library_init();
1833
1834 OpenSSL_add_all_algorithms();
1835 SSL_load_error_strings();
1836
Andy Green2e24da02011-03-05 16:12:04 +00001837 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001838 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1839
Andy Green90c7cbc2011-01-27 06:26:52 +00001840 /*
1841 * Firefox insists on SSLv23 not SSLv3
1842 * Konq disables SSLv2 by default now, SSLv23 works
1843 */
1844
1845 method = (SSL_METHOD *)SSLv23_server_method();
1846 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001847 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001848 ERR_error_string(ERR_get_error(),
1849 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001850 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001851 }
Peter Hinz56885f32011-03-02 22:03:47 +00001852 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1853 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001854 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001855 ERR_error_string(ERR_get_error(),
1856 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001857 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001858 }
1859
David Galeanocc148e42013-01-10 10:18:59 +08001860#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001861 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001862#endif
David Galeano77a677c2013-01-10 10:14:12 +08001863 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001864 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001865
Andy Greena1ce6be2013-01-18 11:43:21 +08001866#ifndef LWS_NO_CLIENT
1867
Andy Green90c7cbc2011-01-27 06:26:52 +00001868 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001869
Andy Green1b265272013-02-09 14:01:09 +08001870 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001871 method = (SSL_METHOD *)SSLv23_client_method();
1872 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001873 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001874 ERR_error_string(ERR_get_error(),
1875 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001876 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001877 }
1878 /* create context */
1879 context->ssl_client_ctx = SSL_CTX_new(method);
1880 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001881 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001882 ERR_error_string(ERR_get_error(),
1883 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001884 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001885 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001886
David Galeanocc148e42013-01-10 10:18:59 +08001887#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001888 SSL_CTX_set_options(context->ssl_client_ctx,
1889 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001890#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001891 SSL_CTX_set_options(context->ssl_client_ctx,
1892 SSL_OP_CIPHER_SERVER_PREFERENCE);
1893 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1894 CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001895
Peter Hinz56885f32011-03-02 22:03:47 +00001896 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001897 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001898 if (!SSL_CTX_load_verify_locations(
1899 context->ssl_client_ctx, NULL,
1900 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001901 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001902 "Unable to load SSL Client certs from %s "
1903 "(set by --with-client-cert-dir= "
1904 "in configure) -- client ssl isn't "
1905 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001906 } else
1907 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001908 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001909 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001910 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001911 "Unable to load SSL Client certs "
1912 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001913 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001914
1915 /*
1916 * callback allowing user code to load extra verification certs
1917 * helping the client to verify server identity
1918 */
1919
1920 context->protocols[0].callback(context, NULL,
1921 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1922 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001923 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001924#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001925
Andy Greenc6bf2c22011-02-20 11:10:47 +00001926 /* as a server, are we requiring clients to identify themselves? */
1927
Andy Greenb5b23192013-02-11 17:13:32 +08001928 if (info->options &
1929 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001930
1931 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001932
Peter Hinz56885f32011-03-02 22:03:47 +00001933 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001934 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1935 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001936
1937 /*
1938 * give user code a chance to load certs into the server
1939 * allowing it to verify incoming client certs
1940 */
1941
Peter Hinz56885f32011-03-02 22:03:47 +00001942 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001943 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001944 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001945 }
1946
Peter Hinz56885f32011-03-02 22:03:47 +00001947 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001948
1949 /* openssl init for server sockets */
1950
Andy Green3faa9c72010-11-08 17:03:03 +00001951 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001952 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001953 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001954 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001955 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001956 info->ssl_cert_filepath,
Andy Greenb5b23192013-02-11 17:13:32 +08001957 ERR_error_string(ERR_get_error(),
1958 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001959 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001960 }
1961 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001962 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001963 info->ssl_private_key_filepath,
1964 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001965 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001966 info->ssl_private_key_filepath,
1967 ERR_error_string(ERR_get_error(),
1968 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001969 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001970 }
1971 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001972 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001973 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001974 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001975 }
1976
1977 /* SSL is happy and has a cert it's content with */
1978 }
1979#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001980
Andy Greendf736162011-01-18 15:39:02 +00001981 /* selftest */
1982
1983 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001984 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00001985
Andy Greena1ce6be2013-01-18 11:43:21 +08001986#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001987 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001988
Andy Green1b265272013-02-09 14:01:09 +08001989 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001990 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001991
Andy Green4739e5c2011-01-22 12:51:57 +00001992 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1993 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001994 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001995 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001996 }
Andy Green775c0dd2010-10-29 14:15:22 +01001997
Joakim Soderberg4c531232013-02-06 15:26:58 +09001998#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08001999 /*
2000 * allow us to restart even if old sockets in TIME_WAIT
2001 * (REUSEADDR on Unix means, "don't hang on to this
2002 * address after the listener is closed." On Windows, though,
2003 * it means "don't keep other processes from binding to
2004 * this address while we're using it)
2005 */
Andy Green6ee372f2012-04-09 15:09:01 +08002006 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2007 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002008#endif
Andy Green6c939552011-03-08 08:56:57 +00002009
2010 /* Disable Nagle */
2011 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002012 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2013 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002014
Joakim Soderberg4c531232013-02-06 15:26:58 +09002015 #ifdef WIN32
2016 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002017 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002018 #else
Andy Greene2160712013-01-28 12:19:10 +08002019 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002020 #endif
Andy Greene2160712013-01-28 12:19:10 +08002021
Andy Green4739e5c2011-01-22 12:51:57 +00002022 bzero((char *) &serv_addr, sizeof(serv_addr));
2023 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002024 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002025 serv_addr.sin_addr.s_addr = INADDR_ANY;
2026 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002027 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002028 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002029 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002030
2031 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2032 sizeof(serv_addr));
2033 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002034 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002035 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002036 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002037 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002038 }
Andy Green0d338332011-02-12 11:57:43 +00002039
Andy Greenb5b23192013-02-11 17:13:32 +08002040 wsi = (struct libwebsocket *)malloc(
2041 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002042 if (wsi == NULL) {
2043 lwsl_err("Out of mem\n");
2044 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002045 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002046 }
Andy Greenb5b23192013-02-11 17:13:32 +08002047 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002048 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002049#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002050 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002051#endif
Andy Green0d338332011-02-12 11:57:43 +00002052 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002053
2054 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002055
Andy Green65b0e912013-01-16 07:59:47 +08002056 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2057 context->listen_service_count = 0;
2058 context->listen_service_fd = sockfd;
2059
Andy Greena824d182013-01-15 20:52:29 +08002060 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002061 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002062 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002063#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002064
Andy Green6ee372f2012-04-09 15:09:01 +08002065 /*
2066 * drop any root privs for this process
2067 * to listen on port < 1023 we would have needed root, but now we are
2068 * listening, we don't want the power for anything else
2069 */
Peter Hinz56885f32011-03-02 22:03:47 +00002070#ifdef WIN32
2071#else
Andy Green1b265272013-02-09 14:01:09 +08002072 if (info->gid != -1)
2073 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002074 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002075 if (info->uid != -1)
2076 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002077 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002078#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002079
Andy Green6f520a52013-01-29 17:57:39 +08002080 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002081
Peter Hinz56885f32011-03-02 22:03:47 +00002082 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002083 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002084 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002085
Andy Green43db0452013-01-10 19:50:35 +08002086 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002087 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002088
Andy Green1b265272013-02-09 14:01:09 +08002089 info->protocols[context->count_protocols].owning_server =
2090 context;
2091 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002092 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002093
2094 /*
2095 * inform all the protocols that they are doing their one-time
2096 * initialization if they want to
2097 */
2098 info->protocols[context->count_protocols].callback(context,
2099 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002100 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002101
Andy Green3182ece2013-01-20 17:08:31 +08002102#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002103 /*
2104 * give all extensions a chance to create any per-context
2105 * allocations they need
2106 */
2107
2108 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002109 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002110 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002111
Andy Green1b265272013-02-09 14:01:09 +08002112 if (info->extensions) {
2113 ext = info->extensions;
2114 while (ext->callback) {
2115 lwsl_ext(" Extension: %s\n", ext->name);
2116 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002117 (enum libwebsocket_extension_callback_reasons)m,
2118 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002119 ext++;
2120 }
Andy Greena41314f2011-05-23 10:00:03 +01002121 }
Andy Green3182ece2013-01-20 17:08:31 +08002122#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002123 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002124
2125bail:
2126 libwebsocket_context_destroy(context);
2127 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002128}
Andy Greenb45993c2010-12-18 15:13:50 +00002129
Andy Greenb45993c2010-12-18 15:13:50 +00002130/**
2131 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002132 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002133 * @wsi: pointer to struct websocket you want to know the protocol of
2134 *
Andy Green8f037e42010-12-19 22:13:26 +00002135 *
Andy Green6f520a52013-01-29 17:57:39 +08002136 * Some apis can act on all live connections of a given protocol,
2137 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002138 */
Andy Greenab990e42010-10-31 12:42:52 +00002139
Andy Greenb45993c2010-12-18 15:13:50 +00002140const struct libwebsocket_protocols *
2141libwebsockets_get_protocol(struct libwebsocket *wsi)
2142{
2143 return wsi->protocol;
2144}
2145
Andy Green82c3d542011-03-07 21:16:31 +00002146int
2147libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2148{
Andy Green623a98d2013-01-21 11:04:23 +08002149 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002150}
Alex Bligh49146db2011-11-07 17:19:25 +08002151
David Galeanoe2cf9922013-01-09 18:06:55 +08002152unsigned char
2153libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2154{
Andy Green623a98d2013-01-21 11:04:23 +08002155 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002156}
2157
Andy Green67d556c2013-02-15 22:31:55 +08002158/**
2159 * libwebsocket_ensure_user_space(): return the user context for the connection if possible
2160 * @wsi: websocket connection instance
2161 */
2162
Alex Bligh49146db2011-11-07 17:19:25 +08002163void *
2164libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2165{
Andy Green67d556c2013-02-15 22:31:55 +08002166 if (!wsi->protocol)
2167 return NULL;
2168
Alex Bligh49146db2011-11-07 17:19:25 +08002169 /* allocate the per-connection user memory (if any) */
2170
2171 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2172 wsi->user_space = malloc(
2173 wsi->protocol->per_session_data_size);
2174 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002175 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002176 return NULL;
2177 }
Andy Green6ee372f2012-04-09 15:09:01 +08002178 memset(wsi->user_space, 0,
2179 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002180 }
2181 return wsi->user_space;
2182}
Andy Green43db0452013-01-10 19:50:35 +08002183
Andy Green0b319092013-01-19 11:17:56 +08002184static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002185{
Andy Green0b319092013-01-19 11:17:56 +08002186 char buf[300];
2187 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002188 int n;
2189
2190 gettimeofday(&tv, NULL);
2191
2192 buf[0] = '\0';
2193 for (n = 0; n < LLL_COUNT; n++)
2194 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002195 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002196 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002197 break;
2198 }
Andy Greenb5b23192013-02-11 17:13:32 +08002199
Andy Green0b319092013-01-19 11:17:56 +08002200 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002201}
2202
Joakim Soderberg4c531232013-02-06 15:26:58 +09002203#ifdef WIN32
2204void lwsl_emit_syslog(int level, const char *line)
2205{
2206 lwsl_emit_stderr(level, line);
2207}
2208#else
Andy Greenc11db202013-01-19 11:12:16 +08002209void lwsl_emit_syslog(int level, const char *line)
2210{
2211 int syslog_level = LOG_DEBUG;
2212
2213 switch (level) {
2214 case LLL_ERR:
2215 syslog_level = LOG_ERR;
2216 break;
2217 case LLL_WARN:
2218 syslog_level = LOG_WARNING;
2219 break;
2220 case LLL_NOTICE:
2221 syslog_level = LOG_NOTICE;
2222 break;
2223 case LLL_INFO:
2224 syslog_level = LOG_INFO;
2225 break;
2226 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002227 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002228}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002229#endif
Andy Greenc11db202013-01-19 11:12:16 +08002230
Andy Green43db0452013-01-10 19:50:35 +08002231void _lws_log(int filter, const char *format, ...)
2232{
Andy Greende8f27a2013-01-12 09:17:42 +08002233 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002234 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002235
2236 if (!(log_level & filter))
2237 return;
2238
Andy Green43db0452013-01-10 19:50:35 +08002239 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002240 vsnprintf(buf, sizeof(buf), format, ap);
2241 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002242 va_end(ap);
2243
Andy Green0b319092013-01-19 11:17:56 +08002244 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002245}
2246
2247/**
2248 * lws_set_log_level() - Set the logging bitfield
2249 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002250 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2251 * function to perform log string emission instead of
2252 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002253 *
Andy Greende8f27a2013-01-12 09:17:42 +08002254 * log level defaults to "err" and "warn" contexts enabled only and
2255 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002256 */
2257
Andy Greenb5b23192013-02-11 17:13:32 +08002258void lws_set_log_level(int level, void (*log_emit_function)(int level,
2259 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002260{
2261 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002262 if (log_emit_function)
2263 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002264}