blob: c553f10fe452a960e29751a438dea40e59cf9863 [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,
130 (void *)(long)wsi->sock, NULL, 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,
173 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
174
175 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000176}
177
Andy Green32375b72011-02-19 08:32:53 +0000178
Andy Green8f037e42010-12-19 22:13:26 +0000179void
Peter Hinz56885f32011-03-02 22:03:47 +0000180libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000181 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000182{
Andy Greenb45993c2010-12-18 15:13:50 +0000183 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000184 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000185 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
186 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800187#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000188 int ret;
189 int m;
190 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100191 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800192#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000193
Andy Green4b6fbe12011-02-14 08:03:48 +0000194 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000195 return;
196
Andy Green62c54d22011-02-14 09:14:25 +0000197 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000198
Andy Green62c54d22011-02-14 09:14:25 +0000199 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000200 return;
201
Andy Green623a98d2013-01-21 11:04:23 +0800202 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000203
Andy Greenb8b247d2013-01-22 07:20:08 +0800204 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
205 close(wsi->u.http.fd);
206 wsi->u.http.fd = 0;
207 }
208
Andy Green3182ece2013-01-20 17:08:31 +0800209#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000210 /*
Andy Green68b45042011-05-25 21:41:57 +0100211 * are his extensions okay with him closing? Eg he might be a mux
212 * parent and just his ch1 aspect is closing?
213 */
214
Andy Green68b45042011-05-25 21:41:57 +0100215 for (n = 0; n < wsi->count_active_extensions; n++) {
216 if (!wsi->active_extensions[n]->callback)
217 continue;
218
219 m = wsi->active_extensions[n]->callback(context,
220 wsi->active_extensions[n], wsi,
221 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
222 wsi->active_extensions_user[n], NULL, 0);
223
224 /*
225 * if somebody vetoed actually closing him at this time....
226 * up to the extension to track the attempted close, let's
227 * just bail
228 */
229
230 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800231 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100232 return;
233 }
234 }
235
Andy Green68b45042011-05-25 21:41:57 +0100236 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000237 * flush any tx pending from extensions, since we may send close packet
238 * if there are problems with send, just nuke the connection
239 */
240
241 ret = 1;
242 while (ret == 1) {
243
244 /* default to nobody has more to spill */
245
246 ret = 0;
247 eff_buf.token = NULL;
248 eff_buf.token_len = 0;
249
250 /* show every extension the new incoming data */
251
252 for (n = 0; n < wsi->count_active_extensions; n++) {
253 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000254 wsi->protocol->owning_server,
255 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000256 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
257 wsi->active_extensions_user[n], &eff_buf, 0);
258 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800259 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000260 goto just_kill_connection;
261 }
262 if (m)
263 /*
264 * at least one extension told us he has more
265 * to spill, so we will go around again after
266 */
267 ret = 1;
268 }
269
270 /* assuming they left us something to send, send it */
271
272 if (eff_buf.token_len)
273 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenb5b23192013-02-11 17:13:32 +0800274 eff_buf.token_len)) {
275 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000276 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800277 }
Andy Greenc44159f2011-03-07 07:08:18 +0000278 }
Andy Green3182ece2013-01-20 17:08:31 +0800279#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000280
281 /*
Andy Greenda527df2011-03-07 07:08:12 +0000282 * signal we are closing, libsocket_write will
283 * add any necessary version-specific stuff. If the write fails,
284 * no worries we are closing anyway. If we didn't initiate this
285 * close, then our state has been changed to
286 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
287 *
288 * Likewise if it's a second call to close this connection after we
289 * sent the close indication to the peer already, we are in state
290 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
291 */
292
293 if (old_state == WSI_STATE_ESTABLISHED &&
294 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100295
Andy Green43db0452013-01-10 19:50:35 +0800296 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100297
Andy Greenfdd305a2013-02-11 14:32:48 +0800298 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800299 memset(buf, 0, sizeof(buf));
300 n = libwebsocket_write(wsi,
301 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000302 0, LWS_WRITE_CLOSE);
303 if (!n) {
304 /*
305 * we have sent a nice protocol level indication we
306 * now wish to close, we should not send anything more
307 */
308
309 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
310
Andy Greenb5b23192013-02-11 17:13:32 +0800311 /*
312 * ...and we should wait for a reply for a bit
313 * out of politeness
314 */
Andy Greenda527df2011-03-07 07:08:12 +0000315
316 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800317 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000318
Andy Green43db0452013-01-10 19:50:35 +0800319 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000320
321 return;
322 }
323
Andy Greenb5b23192013-02-11 17:13:32 +0800324 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800325
Andy Greenda527df2011-03-07 07:08:12 +0000326 /* else, the send failed and we should just hang up */
327 }
328
Andy Green3182ece2013-01-20 17:08:31 +0800329#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000330just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800331#endif
Andy Green66a16f32011-05-24 22:07:45 +0100332
Andy Greenb5b23192013-02-11 17:13:32 +0800333 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100334
Andy Greenda527df2011-03-07 07:08:12 +0000335 /*
336 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800337 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000338 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000339
Andy Greendfb23042013-01-17 12:26:48 +0800340 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000341
Andy Green251f6fa2010-11-03 11:13:06 +0000342 wsi->state = WSI_STATE_DEAD_SOCKET;
343
Andy Greenb5b23192013-02-11 17:13:32 +0800344 if ((old_state == WSI_STATE_ESTABLISHED ||
345 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800346 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
347
348 if (wsi->u.ws.rx_user_buffer) {
349 free(wsi->u.ws.rx_user_buffer);
350 wsi->u.ws.rx_user_buffer = NULL;
351 }
352 if (wsi->u.ws.rxflow_buffer) {
353 free(wsi->u.ws.rxflow_buffer);
354 wsi->u.ws.rxflow_buffer = NULL;
355 }
Andy Green54495112013-02-06 21:10:16 +0900356 }
357
Andy Green4b6fbe12011-02-14 08:03:48 +0000358 /* tell the user it's all over for this guy */
359
Andy Greend4302732011-02-28 07:45:29 +0000360 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800361 ((old_state == WSI_STATE_ESTABLISHED) ||
362 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
363 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800364 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000365 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000366 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800367 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800368 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000369
Andy Green3182ece2013-01-20 17:08:31 +0800370#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000371 /* deallocate any active extension contexts */
372
373 for (n = 0; n < wsi->count_active_extensions; n++) {
374 if (!wsi->active_extensions[n]->callback)
375 continue;
376
Andy Green46c2ea02011-03-22 09:04:01 +0000377 wsi->active_extensions[n]->callback(context,
378 wsi->active_extensions[n], wsi,
379 LWS_EXT_CALLBACK_DESTROY,
380 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000381
382 free(wsi->active_extensions_user[n]);
383 }
384
Andy Greena41314f2011-05-23 10:00:03 +0100385 /*
386 * inform all extensions in case they tracked this guy out of band
387 * even though not active on him specifically
388 */
389
390 ext = context->extensions;
391 while (ext && ext->callback) {
392 ext->callback(context, ext, wsi,
393 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
394 NULL, NULL, 0);
395 ext++;
396 }
Andy Green3182ece2013-01-20 17:08:31 +0800397#endif
Andy Greena41314f2011-05-23 10:00:03 +0100398
Andy Green43db0452013-01-10 19:50:35 +0800399/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000400
Andy Green3faa9c72010-11-08 17:03:03 +0000401#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000402 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000403 n = SSL_get_fd(wsi->ssl);
404 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800405 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000406 SSL_free(wsi->ssl);
407 } else {
408#endif
Andy Green0303db42013-01-17 14:46:43 +0800409 if (wsi->sock) {
410 n = shutdown(wsi->sock, SHUT_RDWR);
411 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800412 lwsl_debug("closing: shutdown returned %d\n",
413 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800414
Andy Green0303db42013-01-17 14:46:43 +0800415 n = compatible_close(wsi->sock);
416 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800417 lwsl_debug("closing: close returned %d\n",
418 errno);
Andy Green0303db42013-01-17 14:46:43 +0800419 }
Andy Green3faa9c72010-11-08 17:03:03 +0000420#ifdef LWS_OPENSSL_SUPPORT
421 }
422#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800423 if (wsi->protocol && wsi->protocol->per_session_data_size &&
424 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000425 free(wsi->user_space);
426
Andy Green251f6fa2010-11-03 11:13:06 +0000427 free(wsi);
428}
429
Andy Green07034092011-02-13 08:37:12 +0000430/**
431 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800432 * @context: Libwebsockets context
433 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000434 * @fd: Connection socket descriptor
435 * @name: Buffer to take client address name
436 * @name_len: Length of client address name buffer
437 * @rip: Buffer to take client address IP qotted quad
438 * @rip_len: Length of client address IP buffer
439 *
440 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800441 * the client connected with socket descriptor @fd. Names may be
442 * truncated if there is not enough room. If either cannot be
443 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000444 */
445
446void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800447libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
448 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000449 char *rip, int rip_len)
450{
451 unsigned int len;
452 struct sockaddr_in sin;
453 struct hostent *host;
454 struct hostent *host1;
455 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000456 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000457 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800458 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800459#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800460 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800461#endif
Andy Green07034092011-02-13 08:37:12 +0000462
463 rip[0] = '\0';
464 name[0] = '\0';
465
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800466 lws_latency_pre(context, wsi);
467
Andy Greenb5b23192013-02-11 17:13:32 +0800468 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000469 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
470 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800471 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000472 }
Andy Green6ee372f2012-04-09 15:09:01 +0800473
Andy Greenb5b23192013-02-11 17:13:32 +0800474 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000475 AF_INET);
476 if (host == NULL) {
477 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800478 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000479 }
480
481 strncpy(name, host->h_name, name_len);
482 name[name_len - 1] = '\0';
483
484 host1 = gethostbyname(host->h_name);
485 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800486 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000487 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000488 n = 0;
489 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000490 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000491 if (p == NULL)
492 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000493 if ((host1->h_addrtype != AF_INET)
494#ifdef AF_LOCAL
495 && (host1->h_addrtype != AF_LOCAL)
496#endif
497 )
Andy Green07034092011-02-13 08:37:12 +0000498 continue;
499
Andy Green7627af52011-03-09 15:13:52 +0000500 if (host1->h_addrtype == AF_INET)
501 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000502#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000503 else {
504 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800505 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000506 ip[sizeof(ip) - 1] = '\0';
507 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000508#endif
Andy Green07034092011-02-13 08:37:12 +0000509 p = NULL;
510 strncpy(rip, ip, rip_len);
511 rip[rip_len - 1] = '\0';
512 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800513
514 ret = 0;
515bail:
516 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000517}
Andy Green9f990342011-02-12 11:57:45 +0000518
Peter Hinz56885f32011-03-02 22:03:47 +0000519int libwebsockets_get_random(struct libwebsocket_context *context,
520 void *buf, int len)
521{
522 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800523 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000524
525#ifdef WIN32
526 for (n = 0; n < len; n++)
527 p[n] = (unsigned char)rand();
528#else
529 n = read(context->fd_random, p, len);
530#endif
531
532 return n;
533}
534
Andy Greena690cd02013-02-09 12:25:31 +0800535int lws_set_socket_options(struct libwebsocket_context *context, int fd)
536{
537 int optval = 1;
538 socklen_t optlen = sizeof(optval);
539#ifdef WIN32
540 unsigned long optl = 0;
541#endif
542#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
543 struct protoent *tcp_proto;
544#endif
545
546 if (context->ka_time) {
547 /* enable keepalive on this socket */
548 optval = 1;
549 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
550 (const void *)&optval, optlen) < 0)
551 return 1;
552
Andy Greena47865f2013-02-10 09:39:47 +0800553#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
554
555 /*
556 * didn't find a way to set these per-socket, need to
557 * tune kernel systemwide values
558 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100559#elif WIN32
560 {
561 DWORD dwBytesRet;
562 struct tcp_keepalive alive;
563 alive.onoff = TRUE;
564 alive.keepalivetime = context->ka_time;
565 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800566
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100567 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
568 NULL, 0, &dwBytesRet, NULL, NULL))
569 return 1;
570 }
Andy Greena47865f2013-02-10 09:39:47 +0800571#else
Andy Greena690cd02013-02-09 12:25:31 +0800572 /* set the keepalive conditions we want on it too */
573 optval = context->ka_time;
574 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
575 (const void *)&optval, optlen) < 0)
576 return 1;
577
578 optval = context->ka_probes;
579 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
580 (const void *)&optval, optlen) < 0)
581 return 1;
582
583 optval = context->ka_interval;
584 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
585 (const void *)&optval, optlen) < 0)
586 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800587#endif
Andy Greena690cd02013-02-09 12:25:31 +0800588 }
589
590 /* Disable Nagle */
591 optval = 1;
592#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
593 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
594#else
595 tcp_proto = getprotobyname("TCP");
596 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
597#endif
598
599 /* We are nonblocking... */
600#ifdef WIN32
601 ioctlsocket(fd, FIONBIO, &optl);
602#else
603 fcntl(fd, F_SETFL, O_NONBLOCK);
604#endif
605
606 return 0;
607}
608
Andy Green95a7b5d2011-03-06 10:29:39 +0000609int lws_send_pipe_choked(struct libwebsocket *wsi)
610{
611 struct pollfd fds;
612
613 fds.fd = wsi->sock;
614 fds.events = POLLOUT;
615 fds.revents = 0;
616
617 if (poll(&fds, 1, 0) != 1)
618 return 1;
619
620 if ((fds.revents & POLLOUT) == 0)
621 return 1;
622
623 /* okay to send another packet without blocking */
624
625 return 0;
626}
627
Andy Greena41314f2011-05-23 10:00:03 +0100628int
Andy Green3b84c002011-03-06 13:14:42 +0000629lws_handle_POLLOUT_event(struct libwebsocket_context *context,
630 struct libwebsocket *wsi, struct pollfd *pollfd)
631{
Andy Green3b84c002011-03-06 13:14:42 +0000632 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800633
Andy Green3182ece2013-01-20 17:08:31 +0800634#ifndef LWS_NO_EXTENSIONS
635 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000636 int ret;
637 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100638 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000639
Andy Greena41314f2011-05-23 10:00:03 +0100640 for (n = 0; n < wsi->count_active_extensions; n++) {
641 if (!wsi->active_extensions[n]->callback)
642 continue;
643
644 m = wsi->active_extensions[n]->callback(context,
645 wsi->active_extensions[n], wsi,
646 LWS_EXT_CALLBACK_IS_WRITEABLE,
647 wsi->active_extensions_user[n], NULL, 0);
648 if (m > handled)
649 handled = m;
650 }
651
652 if (handled == 1)
653 goto notify_action;
654
655 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000656 goto user_service;
657
658 /*
659 * check in on the active extensions, see if they
660 * had pending stuff to spill... they need to get the
661 * first look-in otherwise sequence will be disordered
662 *
663 * NULL, zero-length eff_buf means just spill pending
664 */
665
666 ret = 1;
667 while (ret == 1) {
668
669 /* default to nobody has more to spill */
670
671 ret = 0;
672 eff_buf.token = NULL;
673 eff_buf.token_len = 0;
674
675 /* give every extension a chance to spill */
676
677 for (n = 0; n < wsi->count_active_extensions; n++) {
678 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000679 wsi->protocol->owning_server,
680 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000681 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
682 wsi->active_extensions_user[n], &eff_buf, 0);
683 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800684 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000685 return -1;
686 }
687 if (m)
688 /*
689 * at least one extension told us he has more
690 * to spill, so we will go around again after
691 */
692 ret = 1;
693 }
694
695 /* assuming they gave us something to send, send it */
696
697 if (eff_buf.token_len) {
698 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
699 eff_buf.token_len))
700 return -1;
701 } else
702 continue;
703
704 /* no extension has more to spill */
705
706 if (!ret)
707 continue;
708
709 /*
710 * There's more to spill from an extension, but we just sent
711 * something... did that leave the pipe choked?
712 */
713
714 if (!lws_send_pipe_choked(wsi))
715 /* no we could add more */
716 continue;
717
Andy Green43db0452013-01-10 19:50:35 +0800718 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000719
720 /*
721 * Yes, he's choked. Leave the POLLOUT masked on so we will
722 * come back here when he is unchoked. Don't call the user
723 * callback to enforce ordering of spilling, he'll get called
724 * when we come back here and there's nothing more to spill.
725 */
726
727 return 0;
728 }
729
730 wsi->extension_data_pending = 0;
731
732user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800733#endif
Andy Green3b84c002011-03-06 13:14:42 +0000734 /* one shot */
735
Andy Greena41314f2011-05-23 10:00:03 +0100736 if (pollfd) {
737 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000738
Andy Greena41314f2011-05-23 10:00:03 +0100739 /* external POLL support via protocol 0 */
740 context->protocols[0].callback(context, wsi,
741 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
742 (void *)(long)wsi->sock, NULL, POLLOUT);
743 }
Andy Green3182ece2013-01-20 17:08:31 +0800744#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100745notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800746#endif
Andy Green3b84c002011-03-06 13:14:42 +0000747
Andy Green9e4c2b62011-03-07 20:47:39 +0000748 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
749 n = LWS_CALLBACK_CLIENT_WRITEABLE;
750 else
751 n = LWS_CALLBACK_SERVER_WRITEABLE;
752
Andy Greenb8b247d2013-01-22 07:20:08 +0800753 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800754 wsi, (enum libwebsocket_callback_reasons) n,
755 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000756}
757
758
759
Andy Greena41314f2011-05-23 10:00:03 +0100760void
761libwebsocket_service_timeout_check(struct libwebsocket_context *context,
762 struct libwebsocket *wsi, unsigned int sec)
763{
Andy Green3182ece2013-01-20 17:08:31 +0800764#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100765 int n;
766
767 /*
768 * if extensions want in on it (eg, we are a mux parent)
769 * give them a chance to service child timeouts
770 */
771
772 for (n = 0; n < wsi->count_active_extensions; n++)
773 wsi->active_extensions[n]->callback(
774 context, wsi->active_extensions[n],
775 wsi, LWS_EXT_CALLBACK_1HZ,
776 wsi->active_extensions_user[n], NULL, sec);
777
Andy Green3182ece2013-01-20 17:08:31 +0800778#endif
Andy Greena41314f2011-05-23 10:00:03 +0100779 if (!wsi->pending_timeout)
780 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800781
Andy Greena41314f2011-05-23 10:00:03 +0100782 /*
783 * if we went beyond the allowed time, kill the
784 * connection
785 */
786
787 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800788 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100789 libwebsocket_close_and_free_session(context,
790 wsi, LWS_CLOSE_STATUS_NOSTATUS);
791 }
792}
793
Andy Green9f990342011-02-12 11:57:45 +0000794/**
795 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000796 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000797 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800798 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000799 *
Andy Green75006172013-01-22 12:32:11 +0800800 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800801 * services it according to the state of the associated
802 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800803 *
804 * The one call deals with all "service" that might happen on a socket
805 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000806 */
807
808int
Peter Hinz56885f32011-03-02 22:03:47 +0000809libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000810 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000811{
Andy Greena1ce6be2013-01-18 11:43:21 +0800812 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000813 int n;
Andy Green0d338332011-02-12 11:57:43 +0000814 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800815 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000816 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800817
Andy Green3182ece2013-01-20 17:08:31 +0800818#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000819 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800820#endif
Andy Green98a717c2011-03-06 13:14:15 +0000821 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800822
823 if (context->listen_service_fd)
824 listen_socket_fds_index = context->lws_lookup[
825 context->listen_service_fd]->position_in_fds_table;
826
Andy Greena71eafc2011-02-14 17:59:43 +0000827 /*
828 * you can call us with pollfd = NULL to just allow the once-per-second
829 * global timeout checks; if less than a second since the last check
830 * it returns immediately then.
831 */
832
833 gettimeofday(&tv, NULL);
834
Peter Hinz56885f32011-03-02 22:03:47 +0000835 if (context->last_timeout_check_s != tv.tv_sec) {
836 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000837
Joakim Soderberg4c531232013-02-06 15:26:58 +0900838 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800839 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800840 if (context->started_with_parent &&
841 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800842 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900843 #endif
Andy Green24cba922013-01-19 13:56:10 +0800844
Andy Greena71eafc2011-02-14 17:59:43 +0000845 /* global timeout check once per second */
846
Peter Hinz56885f32011-03-02 22:03:47 +0000847 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +0800848 struct libwebsocket *new_wsi =
849 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +0800850 if (!new_wsi)
851 continue;
852 libwebsocket_service_timeout_check(context,
853 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000854 }
855 }
856
857 /* just here for timeout management? */
858
859 if (pollfd == NULL)
860 return 0;
861
862 /* no, here to service a socket descriptor */
863
Andy Green65b0e912013-01-16 07:59:47 +0800864 /*
865 * deal with listen service piggybacking
866 * every listen_service_modulo services of other fds, we
867 * sneak one in to service the listen socket if there's anything waiting
868 *
869 * To handle connection storms, as found in ab, if we previously saw a
870 * pending connection here, it causes us to check again next time.
871 */
872
Andy Greenb5b23192013-02-11 17:13:32 +0800873 if (context->listen_service_fd && pollfd !=
874 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800875 context->listen_service_count++;
876 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800877 context->listen_service_count ==
878 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800879 context->listen_service_count = 0;
880 m = 1;
881 if (context->listen_service_extraseen > 5)
882 m = 2;
883 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800884 /*
885 * even with extpoll, we prepared this
886 * internal fds for listen
887 */
888 n = poll(&context->fds[listen_socket_fds_index],
889 1, 0);
890 if (n > 0) { /* there's a conn waiting for us */
891 libwebsocket_service_fd(context,
892 &context->
893 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800894 context->listen_service_extraseen++;
895 } else {
896 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800897 context->
898 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800899 break;
900 }
901 }
902 }
903
904 }
905
906 /* okay, what we came here to do... */
907
Andy Greendfb23042013-01-17 12:26:48 +0800908 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800909 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800910 if (pollfd->fd > 11)
Andy Greenb5b23192013-02-11 17:13:32 +0800911 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n",
912 pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800913 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800914 }
Andy Green8f037e42010-12-19 22:13:26 +0000915
Andy Green0d338332011-02-12 11:57:43 +0000916 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800917
Andy Greena1ce6be2013-01-18 11:43:21 +0800918#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800919 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000920 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800921 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800922 return lws_server_socket_service(context, wsi, pollfd);
923#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000924
Andy Green0d338332011-02-12 11:57:43 +0000925 case LWS_CONNMODE_WS_SERVING:
926 case LWS_CONNMODE_WS_CLIENT:
927
928 /* handle session socket closed */
929
930 if (pollfd->revents & (POLLERR | POLLHUP)) {
931
Andy Green43db0452013-01-10 19:50:35 +0800932 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000933 (void *)wsi, pollfd->fd);
934
Peter Hinz56885f32011-03-02 22:03:47 +0000935 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000936 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800937 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000938 }
939
Andy Green0d338332011-02-12 11:57:43 +0000940 /* the guy requested a callback when it was OK to write */
941
Andy Greenda527df2011-03-07 07:08:12 +0000942 if ((pollfd->revents & POLLOUT) &&
943 wsi->state == WSI_STATE_ESTABLISHED)
944 if (lws_handle_POLLOUT_event(context, wsi,
945 pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800946 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenda527df2011-03-07 07:08:12 +0000947 libwebsocket_close_and_free_session(
948 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800949 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000950 }
Andy Green0d338332011-02-12 11:57:43 +0000951
Andy Green0d338332011-02-12 11:57:43 +0000952
953 /* any incoming data ready? */
954
955 if (!(pollfd->revents & POLLIN))
956 break;
957
Andy Greenb45993c2010-12-18 15:13:50 +0000958#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800959read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800960 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800961 eff_buf.token_len = SSL_read(wsi->ssl,
962 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800963 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800964 if (!eff_buf.token_len) {
965 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800966 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +0800967 ERR_error_string(n,
968 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800969 }
970 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000971#endif
Andy Greene84652c2013-02-08 13:01:02 +0800972 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +0800973 context->service_buffer,
974 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000975
Andy Green98a717c2011-03-06 13:14:15 +0000976 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800977 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000978 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200979 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800980 libwebsocket_close_and_free_session(context,
981 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800982 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000983 }
Andy Green98a717c2011-03-06 13:14:15 +0000984 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800985 lwsl_info("closing connection due to 0 length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000986 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800987 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800988 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000989 }
990
Andy Green98a717c2011-03-06 13:14:15 +0000991 /*
992 * give any active extensions a chance to munge the buffer
993 * before parse. We pass in a pointer to an lws_tokens struct
994 * prepared with the default buffer and content length that's in
995 * there. Rather than rewrite the default buffer, extensions
996 * that expect to grow the buffer can adapt .token to
997 * point to their own per-connection buffer in the extension
998 * user allocation. By default with no extensions or no
999 * extension callback handling, just the normal input buffer is
1000 * used then so it is efficient.
1001 */
Andy Greenb45993c2010-12-18 15:13:50 +00001002
Andy Greene84652c2013-02-08 13:01:02 +08001003 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001004#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001005 more = 1;
1006 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001007
Andy Green98a717c2011-03-06 13:14:15 +00001008 more = 0;
1009
1010 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001011 m = wsi->active_extensions[n]->callback(context,
1012 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001013 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001014 wsi->active_extensions_user[n],
1015 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001016 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001017 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001018 "Extension reports fatal error\n");
1019 libwebsocket_close_and_free_session(
1020 context, wsi,
1021 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001022 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001023 }
1024 if (m)
1025 more = 1;
1026 }
Andy Green3182ece2013-01-20 17:08:31 +08001027#endif
Andy Green98a717c2011-03-06 13:14:15 +00001028 /* service incoming data */
1029
1030 if (eff_buf.token_len) {
1031 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001032 (unsigned char *)eff_buf.token,
1033 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001034 if (n < 0)
1035 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001036 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001037 }
Andy Green3182ece2013-01-20 17:08:31 +08001038#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001039 eff_buf.token = NULL;
1040 eff_buf.token_len = 0;
1041 }
Andy Green3182ece2013-01-20 17:08:31 +08001042#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001043
1044#ifdef LWS_OPENSSL_SUPPORT
1045 if (wsi->ssl && SSL_pending(wsi->ssl))
1046 goto read_pending;
1047#endif
Andy Green98a717c2011-03-06 13:14:15 +00001048 break;
Andy Green76f61e72013-01-16 11:53:05 +08001049
1050 default:
Andy Green03674a62013-01-16 11:47:40 +08001051#ifdef LWS_NO_CLIENT
1052 break;
1053#else
Andy Green76f61e72013-01-16 11:53:05 +08001054 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001055#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001056 }
1057
1058 return 0;
1059}
1060
Andy Green0d338332011-02-12 11:57:43 +00001061
Andy Green6964bb52011-01-23 16:50:33 +00001062/**
1063 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001064 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001065 *
1066 * This function closes any active connections and then frees the
1067 * context. After calling this, any further use of the context is
1068 * undefined.
1069 */
1070void
Peter Hinz56885f32011-03-02 22:03:47 +00001071libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001072{
Andy Green3182ece2013-01-20 17:08:31 +08001073#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001074 int n;
1075 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001076 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001077 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001078
Andy Greend636e352013-01-29 12:36:17 +08001079#ifdef LWS_LATENCY
1080 if (context->worst_latency_info[0])
1081 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1082#endif
1083
Andy Greendfb23042013-01-17 12:26:48 +08001084 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001085 struct libwebsocket *wsi =
1086 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001087 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001088 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1089 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001090 }
Andy Green6964bb52011-01-23 16:50:33 +00001091
Andy Greena41314f2011-05-23 10:00:03 +01001092 /*
1093 * give all extensions a chance to clean up any per-context
1094 * allocations they might have made
1095 */
1096
1097 ext = context->extensions;
1098 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1099 if (context->listen_port)
1100 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001101 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001102 ext->callback(context, ext, NULL,
1103 (enum libwebsocket_extension_callback_reasons)m,
1104 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001105 ext++;
1106 }
Andy Greena7109e62013-02-11 12:05:54 +08001107
1108 /*
1109 * inform all the protocols that they are done and will have no more
1110 * callbacks
1111 */
1112
1113 while (protocol->callback) {
1114 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1115 NULL, NULL, 0);
1116 protocol++;
1117 }
1118
Andy Green3182ece2013-01-20 17:08:31 +08001119#endif
Andy Greena41314f2011-05-23 10:00:03 +01001120
Peter Hinz56885f32011-03-02 22:03:47 +00001121#ifdef WIN32
1122#else
1123 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001124#endif
1125
Peter Hinz56885f32011-03-02 22:03:47 +00001126#ifdef LWS_OPENSSL_SUPPORT
1127 if (context->ssl_ctx)
1128 SSL_CTX_free(context->ssl_ctx);
1129 if (context->ssl_client_ctx)
1130 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001131
1132 ERR_remove_state(0);
1133 ERR_free_strings();
1134 EVP_cleanup();
1135 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001136#endif
1137
Andy Green46596482013-02-11 11:04:01 +08001138 if (context->fds)
1139 free(context->fds);
1140 if (context->lws_lookup)
1141 free(context->lws_lookup);
1142
Peter Hinz56885f32011-03-02 22:03:47 +00001143 free(context);
1144
1145#ifdef WIN32
1146 WSACleanup();
1147#endif
Andy Green6964bb52011-01-23 16:50:33 +00001148}
1149
Andy Greend88146d2013-01-22 12:40:35 +08001150/**
Andy Greenb5b23192013-02-11 17:13:32 +08001151 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001152 * @context: Websocket context
1153 *
1154 * This returns the optional user allocation that can be attached to
1155 * the context the sockets live in at context_create time. It's a way
1156 * to let all sockets serviced in the same context share data without
1157 * using globals statics in the user code.
1158 */
Alon Levy0291eb32012-10-19 11:21:56 +02001159LWS_EXTERN void *
1160libwebsocket_context_user(struct libwebsocket_context *context)
1161{
Andy Greenb5b23192013-02-11 17:13:32 +08001162 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001163}
1164
Andy Green6964bb52011-01-23 16:50:33 +00001165/**
1166 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001167 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001168 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1169 * service otherwise block and service immediately, returning
1170 * after the timeout if nothing needed service.
1171 *
1172 * This function deals with any pending websocket traffic, for three
1173 * kinds of event. It handles these events on both server and client
1174 * types of connection the same.
1175 *
1176 * 1) Accept new connections to our context's server
1177 *
Andy Green6f520a52013-01-29 17:57:39 +08001178 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001179 * server or client connections.
1180 *
1181 * You need to call this service function periodically to all the above
1182 * functions to happen; if your application is single-threaded you can
1183 * just call it in your main event loop.
1184 *
1185 * Alternatively you can fork a new process that asynchronously handles
1186 * calling this service in a loop. In that case you are happy if this
1187 * call blocks your thread until it needs to take care of something and
1188 * would call it with a large nonzero timeout. Your loop then takes no
1189 * CPU while there is nothing happening.
1190 *
1191 * If you are calling it in a single-threaded app, you don't want it to
1192 * wait around blocking other things in your loop from happening, so you
1193 * would call it with a timeout_ms of 0, so it returns immediately if
1194 * nothing is pending, or as soon as it services whatever was pending.
1195 */
1196
Andy Greene92cd172011-01-19 13:11:55 +00001197int
Peter Hinz56885f32011-03-02 22:03:47 +00001198libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001199{
1200 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001201
1202 /* stay dead once we are dead */
1203
Peter Hinz56885f32011-03-02 22:03:47 +00001204 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001205 return 1;
1206
Andy Green0d338332011-02-12 11:57:43 +00001207 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001208
Peter Hinz56885f32011-03-02 22:03:47 +00001209 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001210 if (n == 0) /* poll timeout */
1211 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001212
Andy Greendfb23042013-01-17 12:26:48 +08001213 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001214 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001215
Andy Greendfb23042013-01-17 12:26:48 +08001216 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001217
Peter Hinz56885f32011-03-02 22:03:47 +00001218 for (n = 0; n < context->fds_count; n++)
1219 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001220 if (libwebsocket_service_fd(context,
1221 &context->fds[n]) < 0)
1222 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001223 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001224}
1225
Andy Green3182ece2013-01-20 17:08:31 +08001226#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001227int
1228lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001229 struct libwebsocket *wsi,
1230 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001231 void *v, size_t len)
1232{
1233 int n;
1234 int handled = 0;
1235
1236 /* maybe an extension will take care of it for us */
1237
1238 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1239 if (!wsi->active_extensions[n]->callback)
1240 continue;
1241
1242 handled |= wsi->active_extensions[n]->callback(context,
1243 wsi->active_extensions[n], wsi,
1244 r, wsi->active_extensions_user[n], v, len);
1245 }
1246
1247 return handled;
1248}
1249
1250
1251void *
1252lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001253 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001254{
1255 int n = 0;
1256
Andy Green68b45042011-05-25 21:41:57 +01001257 if (wsi == NULL)
1258 return NULL;
1259
Andy Greena41314f2011-05-23 10:00:03 +01001260 while (n < wsi->count_active_extensions) {
1261 if (wsi->active_extensions[n] != ext) {
1262 n++;
1263 continue;
1264 }
1265 return wsi->active_extensions_user[n];
1266 }
1267
1268 return NULL;
1269}
Andy Green3182ece2013-01-20 17:08:31 +08001270#endif
Andy Greena41314f2011-05-23 10:00:03 +01001271
Andy Green90c7cbc2011-01-27 06:26:52 +00001272/**
1273 * libwebsocket_callback_on_writable() - Request a callback when this socket
1274 * becomes able to be written to without
1275 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001276 *
Peter Hinz56885f32011-03-02 22:03:47 +00001277 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001278 * @wsi: Websocket connection instance to get callback for
1279 */
1280
1281int
Peter Hinz56885f32011-03-02 22:03:47 +00001282libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001283 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001284{
Andy Green3182ece2013-01-20 17:08:31 +08001285#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001286 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001287 int handled = 0;
1288
1289 /* maybe an extension will take care of it for us */
1290
1291 for (n = 0; n < wsi->count_active_extensions; n++) {
1292 if (!wsi->active_extensions[n]->callback)
1293 continue;
1294
1295 handled |= wsi->active_extensions[n]->callback(context,
1296 wsi->active_extensions[n], wsi,
1297 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1298 wsi->active_extensions_user[n], NULL, 0);
1299 }
1300
1301 if (handled)
1302 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001303#endif
Andy Greendfb23042013-01-17 12:26:48 +08001304 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001305 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1306 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001307 return -1;
1308 }
1309
1310 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001311
Andy Green3221f922011-02-12 13:14:11 +00001312 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001313 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001314 LWS_CALLBACK_SET_MODE_POLL_FD,
1315 (void *)(long)wsi->sock, NULL, POLLOUT);
1316
Andy Green90c7cbc2011-01-27 06:26:52 +00001317 return 1;
1318}
1319
1320/**
1321 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1322 * all connections using the given protocol when it
1323 * becomes possible to write to each socket without
1324 * blocking in turn.
1325 *
1326 * @protocol: Protocol whose connections will get callbacks
1327 */
1328
1329int
1330libwebsocket_callback_on_writable_all_protocol(
1331 const struct libwebsocket_protocols *protocol)
1332{
Peter Hinz56885f32011-03-02 22:03:47 +00001333 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001334 int n;
Andy Green0d338332011-02-12 11:57:43 +00001335 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001336
Andy Greendfb23042013-01-17 12:26:48 +08001337 for (n = 0; n < context->fds_count; n++) {
1338 wsi = context->lws_lookup[context->fds[n].fd];
1339 if (!wsi)
1340 continue;
1341 if (wsi->protocol == protocol)
1342 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001343 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001344
1345 return 0;
1346}
1347
Andy Greenbe93fef2011-02-14 20:25:43 +00001348/**
1349 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1350 *
1351 * You will not need this unless you are doing something special
1352 *
1353 * @wsi: Websocket connection instance
1354 * @reason: timeout reason
1355 * @secs: how many seconds
1356 */
1357
1358void
1359libwebsocket_set_timeout(struct libwebsocket *wsi,
1360 enum pending_timeout reason, int secs)
1361{
1362 struct timeval tv;
1363
1364 gettimeofday(&tv, NULL);
1365
1366 wsi->pending_timeout_limit = tv.tv_sec + secs;
1367 wsi->pending_timeout = reason;
1368}
1369
Andy Greena6cbece2011-01-27 20:06:03 +00001370
1371/**
1372 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1373 *
1374 * You will not need this unless you are doing something special
1375 *
1376 * @wsi: Websocket connection instance
1377 */
1378
1379int
1380libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1381{
1382 return wsi->sock;
1383}
1384
Andy Greend636e352013-01-29 12:36:17 +08001385#ifdef LWS_LATENCY
1386void
Andy Greenb5b23192013-02-11 17:13:32 +08001387lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1388 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001389{
1390 struct timeval tv;
1391 unsigned long u;
1392 char buf[256];
1393
1394 gettimeofday(&tv, NULL);
1395
1396 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1397
1398 if (action) {
1399 if (completed) {
1400 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001401 sprintf(buf,
1402 "Completion first try lat %luus: %p: ret %d: %s\n",
1403 u - wsi->latency_start,
1404 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001405 else
Andy Greenb5b23192013-02-11 17:13:32 +08001406 sprintf(buf,
1407 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1408 u - wsi->action_start,
1409 u - wsi->latency_start,
1410 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001411 wsi->action_start = 0;
1412 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001413 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1414 u - wsi->latency_start,
1415 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001416 if (u - wsi->latency_start > context->worst_latency) {
1417 context->worst_latency = u - wsi->latency_start;
1418 strcpy(context->worst_latency_info, buf);
1419 }
1420 lwsl_latency("%s", buf);
1421 } else {
1422 wsi->latency_start = u;
1423 if (!wsi->action_start)
1424 wsi->action_start = u;
1425 }
1426}
1427#endif
1428
Andy Greena1ce6be2013-01-18 11:43:21 +08001429#ifdef LWS_NO_SERVER
1430int
1431_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1432{
1433 return 0;
1434}
1435#else
Andy Green706961d2013-01-17 16:50:35 +08001436int
1437_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1438{
1439 struct libwebsocket_context *context = wsi->protocol->owning_server;
1440 int n;
1441
Andy Green623a98d2013-01-21 11:04:23 +08001442 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001443 return 0;
1444
Andy Green623a98d2013-01-21 11:04:23 +08001445 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001446
Andy Greenb5b23192013-02-11 17:13:32 +08001447 lwsl_info("rxflow: wsi %p change_to %d\n",
1448 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001449
1450 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001451 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001452 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1453 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001454 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001455 return -1;
1456 }
1457 if (n)
1458 /* oh he stuck again, do nothing */
1459 return 0;
1460 }
1461
Andy Green623a98d2013-01-21 11:04:23 +08001462 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001463 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1464 else
1465 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
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 /* external POLL support via protocol 0 */
1469 context->protocols[0].callback(context, wsi,
1470 LWS_CALLBACK_SET_MODE_POLL_FD,
1471 (void *)(long)wsi->sock, NULL, POLLIN);
1472 else
1473 /* external POLL support via protocol 0 */
1474 context->protocols[0].callback(context, wsi,
1475 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1476 (void *)(long)wsi->sock, NULL, POLLIN);
1477
1478 return 1;
1479}
Andy Greena1ce6be2013-01-18 11:43:21 +08001480#endif
Andy Green706961d2013-01-17 16:50:35 +08001481
Andy Green90c7cbc2011-01-27 06:26:52 +00001482/**
1483 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1484 * receieved packets.
1485 *
1486 * If the output side of a server process becomes choked, this allows flow
1487 * control for the input side.
1488 *
1489 * @wsi: Websocket connection instance to get callback for
1490 * @enable: 0 = disable read servicing for this connection, 1 = enable
1491 */
1492
1493int
1494libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1495{
Andy Green623a98d2013-01-21 11:04:23 +08001496 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001497
Andy Green706961d2013-01-17 16:50:35 +08001498 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001499}
1500
Andy Green706961d2013-01-17 16:50:35 +08001501
Andy Green2ac5a6f2011-01-28 10:00:18 +00001502/**
1503 * libwebsocket_canonical_hostname() - returns this host's hostname
1504 *
1505 * This is typically used by client code to fill in the host parameter
1506 * when making a client connection. You can only call it after the context
1507 * has been created.
1508 *
Peter Hinz56885f32011-03-02 22:03:47 +00001509 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001510 */
1511
1512
1513extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001514libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001515{
Peter Hinz56885f32011-03-02 22:03:47 +00001516 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001517}
1518
1519
Andy Green90c7cbc2011-01-27 06:26:52 +00001520static void sigpipe_handler(int x)
1521{
1522}
1523
Andy Green6901cb32011-02-21 08:06:47 +00001524#ifdef LWS_OPENSSL_SUPPORT
1525static int
1526OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1527{
1528
1529 SSL *ssl;
1530 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001531 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001532
1533 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1534 SSL_get_ex_data_X509_STORE_CTX_idx());
1535
1536 /*
Andy Green2e24da02011-03-05 16:12:04 +00001537 * !!! nasty openssl requires the index to come as a library-scope
1538 * static
Andy Green6901cb32011-02-21 08:06:47 +00001539 */
Andy Green2e24da02011-03-05 16:12:04 +00001540 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001541
Peter Hinz56885f32011-03-02 22:03:47 +00001542 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001543 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1544 x509_ctx, ssl, preverify_ok);
1545
1546 /* convert return code from 0 = OK to 1 = OK */
1547
1548 if (!n)
1549 n = 1;
1550 else
1551 n = 0;
1552
1553 return n;
1554}
1555#endif
1556
Andy Green706961d2013-01-17 16:50:35 +08001557int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001558 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001559 struct libwebsocket *wsi,
1560 enum libwebsocket_callback_reasons reason, void *user,
1561 void *in, size_t len)
1562{
1563 int n;
1564
1565 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001566 if (!n)
1567 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001568
Andy Greenaedc9532013-02-10 21:21:24 +08001569 return n;
Andy Green706961d2013-01-17 16:50:35 +08001570}
1571
Andy Greenb45993c2010-12-18 15:13:50 +00001572
Andy Greenab990e42010-10-31 12:42:52 +00001573/**
Andy Green4739e5c2011-01-22 12:51:57 +00001574 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001575 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001576 *
Andy Green1b265272013-02-09 14:01:09 +08001577 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001578 * of all initialization in one step.
1579 *
Andy Greene92cd172011-01-19 13:11:55 +00001580 * After initialization, it returns a struct libwebsocket_context * that
1581 * represents this server. After calling, user code needs to take care
1582 * of calling libwebsocket_service() with the context pointer to get the
1583 * server's sockets serviced. This can be done in the same process context
1584 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001585 *
Andy Green8f037e42010-12-19 22:13:26 +00001586 * The protocol callback functions are called for a handful of events
1587 * including http requests coming in, websocket connections becoming
1588 * established, and data arriving; it's also called periodically to allow
1589 * async transmission.
1590 *
1591 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1592 * at that time websocket protocol has not been negotiated. Other
1593 * protocols after the first one never see any HTTP callack activity.
1594 *
1595 * The server created is a simple http server by default; part of the
1596 * websocket standard is upgrading this http connection to a websocket one.
1597 *
1598 * This allows the same server to provide files like scripts and favicon /
1599 * images or whatever over http and dynamic data over websockets all in
1600 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001601 */
Andy Green4ea60062010-10-30 12:15:07 +01001602
Andy Greene92cd172011-01-19 13:11:55 +00001603struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001604libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001605{
Peter Hinz56885f32011-03-02 22:03:47 +00001606 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001607 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001608#ifndef LWS_NO_SERVER
Andy Green14f47292013-02-11 19:36:15 +08001609 int n;
Andy Greencbb31222013-01-31 09:57:05 +08001610 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001611 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001612 struct sockaddr_in serv_addr;
1613#endif
Andy Green3182ece2013-01-20 17:08:31 +08001614#ifndef LWS_NO_EXTENSIONS
1615 int m;
Andy Green1b265272013-02-09 14:01:09 +08001616 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001617#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001618
Andy Green3faa9c72010-11-08 17:03:03 +00001619#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001620 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001621#endif
1622
Joakim Soderberg4c531232013-02-06 15:26:58 +09001623#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001624 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001625#endif
1626
Andy Greenb3a614a2013-01-19 13:08:17 +08001627 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001628 lwsl_notice("Library version: %s\n", library_version);
Andy Greenb5b23192013-02-11 17:13:32 +08001629 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n",
1630 LWS_MAX_HEADER_NAME_LENGTH);
Andy Greenc0d6b632013-01-12 23:42:17 +08001631 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001632 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001633#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001634 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1635 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001636#else
1637 lwsl_notice(" Configured without extension support\n");
1638#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001639 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1640 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1641 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1642 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1643 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001644
Peter Hinz56885f32011-03-02 22:03:47 +00001645#ifdef _WIN32
1646 {
1647 WORD wVersionRequested;
1648 WSADATA wsaData;
1649 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001650 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001651
1652 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1653 wVersionRequested = MAKEWORD(2, 2);
1654
1655 err = WSAStartup(wVersionRequested, &wsaData);
1656 if (err != 0) {
1657 /* Tell the user that we could not find a usable */
1658 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001659 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001660 return NULL;
1661 }
David Galeano7b11fec2011-10-04 19:55:18 +08001662
Andy Green6ee372f2012-04-09 15:09:01 +08001663 /* default to a poll() made out of select() */
1664 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001665
Andy Green6ee372f2012-04-09 15:09:01 +08001666 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001667 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001668 if (wsdll)
1669 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001670
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001671 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001672 if (!poll)
1673 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001674 }
1675#endif
1676
Andy Greenb5b23192013-02-11 17:13:32 +08001677 context = (struct libwebsocket_context *)
1678 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001679 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001680 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001681 return NULL;
1682 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001683 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001684#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001685 context->started_with_parent = pid_daemon;
1686 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1687#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001688
Joakim Soderberg4c531232013-02-06 15:26:58 +09001689 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001690 context->protocols = info->protocols;
1691 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001692 context->http_proxy_port = 0;
1693 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001694 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001695 /* to reduce this allocation, */
1696 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001697 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1698 sizeof(struct libwebsocket_context),
1699 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1700 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001701 sizeof(struct libwebsocket_context) +
1702 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1703 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001704
Andy Greenb5b23192013-02-11 17:13:32 +08001705 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1706 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001707 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001708 lwsl_err("Unable to allocate fds array for %d connections\n",
1709 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001710 free(context);
1711 return NULL;
1712 }
Andy Greenb5b23192013-02-11 17:13:32 +08001713 context->lws_lookup = (struct libwebsocket **)
1714 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001715 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001716 lwsl_err(
1717 "Unable to allocate lws_lookup array for %d connections\n",
1718 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001719 free(context->fds);
1720 free(context);
1721 return NULL;
1722 }
Andy Greena17c6922013-01-20 20:21:54 +08001723
Peter Hinz56885f32011-03-02 22:03:47 +00001724 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001725#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001726 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001727#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001728 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001729 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001730
Peter Hinz56885f32011-03-02 22:03:47 +00001731#ifdef WIN32
1732 context->fd_random = 0;
1733#else
1734 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1735 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001736 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001737 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001738 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001739 }
Peter Hinz56885f32011-03-02 22:03:47 +00001740#endif
Andy Green44eee682011-02-10 09:32:24 +00001741
Peter Hinz56885f32011-03-02 22:03:47 +00001742#ifdef LWS_OPENSSL_SUPPORT
1743 context->use_ssl = 0;
1744 context->ssl_ctx = NULL;
1745 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001746 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001747#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001748
Andy Greena1ce6be2013-01-18 11:43:21 +08001749 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001750
Andy Greena1ce6be2013-01-18 11:43:21 +08001751#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001752 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001753 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001754 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001755 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001756
Andy Greenb5b23192013-02-11 17:13:32 +08001757 lwsl_notice(" canonical_hostname = %s\n",
1758 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001759 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001760#endif
Andy Greena69f0512012-05-03 12:32:38 +08001761
Andy Green9659f372011-01-27 22:01:43 +00001762 /* split the proxy ads:port if given */
1763
1764 p = getenv("http_proxy");
1765 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001766 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001767 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001768 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001769 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001770
Peter Hinz56885f32011-03-02 22:03:47 +00001771 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001772 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001773 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001774 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001775 }
1776 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001777 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001778
Andy Greenb3a614a2013-01-19 13:08:17 +08001779 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001780 context->http_proxy_address,
1781 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001782 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001783
Andy Greena1ce6be2013-01-18 11:43:21 +08001784#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001785 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001786
Andy Green3faa9c72010-11-08 17:03:03 +00001787#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001788 context->use_ssl = info->ssl_cert_filepath != NULL &&
1789 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001790#ifdef USE_CYASSL
1791 lwsl_notice(" Compiled with CYASSL support\n");
1792#else
1793 lwsl_notice(" Compiled with OpenSSL support\n");
1794#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001795 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001796 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001797 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001798 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001799
Andy Green90c7cbc2011-01-27 06:26:52 +00001800#else
Andy Green2b40b792013-02-10 22:22:01 +08001801 if (info->ssl_cert_filepath != NULL &&
1802 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001803 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001804 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001805 }
Andy Greenb5b23192013-02-11 17:13:32 +08001806 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001807#endif
Andy Greena17c6922013-01-20 20:21:54 +08001808
Andy Greenb5b23192013-02-11 17:13:32 +08001809 lwsl_notice(
1810 " per-conn mem: %u + %u headers + protocol rx buf\n",
1811 sizeof(struct libwebsocket),
1812 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001813 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001814#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001815
1816 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001817#ifdef WIN32
1818#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001819 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001820#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001821
1822
1823#ifdef LWS_OPENSSL_SUPPORT
1824
1825 /* basic openssl init */
1826
1827 SSL_library_init();
1828
1829 OpenSSL_add_all_algorithms();
1830 SSL_load_error_strings();
1831
Andy Green2e24da02011-03-05 16:12:04 +00001832 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001833 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1834
Andy Green90c7cbc2011-01-27 06:26:52 +00001835 /*
1836 * Firefox insists on SSLv23 not SSLv3
1837 * Konq disables SSLv2 by default now, SSLv23 works
1838 */
1839
1840 method = (SSL_METHOD *)SSLv23_server_method();
1841 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001842 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001843 ERR_error_string(ERR_get_error(),
1844 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001845 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001846 }
Peter Hinz56885f32011-03-02 22:03:47 +00001847 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1848 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001849 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001850 ERR_error_string(ERR_get_error(),
1851 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001852 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001853 }
1854
David Galeanocc148e42013-01-10 10:18:59 +08001855#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001856 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001857#endif
David Galeano77a677c2013-01-10 10:14:12 +08001858 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001859 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001860
Andy Greena1ce6be2013-01-18 11:43:21 +08001861#ifndef LWS_NO_CLIENT
1862
Andy Green90c7cbc2011-01-27 06:26:52 +00001863 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001864
Andy Green1b265272013-02-09 14:01:09 +08001865 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001866 method = (SSL_METHOD *)SSLv23_client_method();
1867 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001868 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001869 ERR_error_string(ERR_get_error(),
1870 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001871 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001872 }
1873 /* create context */
1874 context->ssl_client_ctx = SSL_CTX_new(method);
1875 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001876 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001877 ERR_error_string(ERR_get_error(),
1878 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001879 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001880 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001881
David Galeanocc148e42013-01-10 10:18:59 +08001882#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001883 SSL_CTX_set_options(context->ssl_client_ctx,
1884 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001885#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001886 SSL_CTX_set_options(context->ssl_client_ctx,
1887 SSL_OP_CIPHER_SERVER_PREFERENCE);
1888 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1889 CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001890
Peter Hinz56885f32011-03-02 22:03:47 +00001891 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001892 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001893 if (!SSL_CTX_load_verify_locations(
1894 context->ssl_client_ctx, NULL,
1895 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001896 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001897 "Unable to load SSL Client certs from %s "
1898 "(set by --with-client-cert-dir= "
1899 "in configure) -- client ssl isn't "
1900 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001901 } else
1902 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001903 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001904 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001905 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001906 "Unable to load SSL Client certs "
1907 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001908 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001909
1910 /*
1911 * callback allowing user code to load extra verification certs
1912 * helping the client to verify server identity
1913 */
1914
1915 context->protocols[0].callback(context, NULL,
1916 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1917 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001918 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001919#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001920
Andy Greenc6bf2c22011-02-20 11:10:47 +00001921 /* as a server, are we requiring clients to identify themselves? */
1922
Andy Greenb5b23192013-02-11 17:13:32 +08001923 if (info->options &
1924 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001925
1926 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001927
Peter Hinz56885f32011-03-02 22:03:47 +00001928 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001929 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1930 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001931
1932 /*
1933 * give user code a chance to load certs into the server
1934 * allowing it to verify incoming client certs
1935 */
1936
Peter Hinz56885f32011-03-02 22:03:47 +00001937 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001938 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001939 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001940 }
1941
Peter Hinz56885f32011-03-02 22:03:47 +00001942 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001943
1944 /* openssl init for server sockets */
1945
Andy Green3faa9c72010-11-08 17:03:03 +00001946 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001947 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001948 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001949 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001950 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001951 info->ssl_cert_filepath,
Andy Greenb5b23192013-02-11 17:13:32 +08001952 ERR_error_string(ERR_get_error(),
1953 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001954 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001955 }
1956 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001957 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001958 info->ssl_private_key_filepath,
1959 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001960 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001961 info->ssl_private_key_filepath,
1962 ERR_error_string(ERR_get_error(),
1963 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001964 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001965 }
1966 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001967 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001968 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001969 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001970 }
1971
1972 /* SSL is happy and has a cert it's content with */
1973 }
1974#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001975
Andy Greendf736162011-01-18 15:39:02 +00001976 /* selftest */
1977
1978 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001979 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00001980
Andy Greena1ce6be2013-01-18 11:43:21 +08001981#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001982 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001983
Andy Green1b265272013-02-09 14:01:09 +08001984 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001985 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001986
Andy Green4739e5c2011-01-22 12:51:57 +00001987 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1988 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001989 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001990 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001991 }
Andy Green775c0dd2010-10-29 14:15:22 +01001992
Joakim Soderberg4c531232013-02-06 15:26:58 +09001993#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08001994 /*
1995 * allow us to restart even if old sockets in TIME_WAIT
1996 * (REUSEADDR on Unix means, "don't hang on to this
1997 * address after the listener is closed." On Windows, though,
1998 * it means "don't keep other processes from binding to
1999 * this address while we're using it)
2000 */
Andy Green6ee372f2012-04-09 15:09:01 +08002001 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2002 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002003#endif
Andy Green6c939552011-03-08 08:56:57 +00002004
2005 /* Disable Nagle */
2006 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002007 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2008 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002009
Joakim Soderberg4c531232013-02-06 15:26:58 +09002010 #ifdef WIN32
2011 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002012 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002013 #else
Andy Greene2160712013-01-28 12:19:10 +08002014 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002015 #endif
Andy Greene2160712013-01-28 12:19:10 +08002016
Andy Green4739e5c2011-01-22 12:51:57 +00002017 bzero((char *) &serv_addr, sizeof(serv_addr));
2018 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002019 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002020 serv_addr.sin_addr.s_addr = INADDR_ANY;
2021 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002022 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002023 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002024 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002025
2026 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2027 sizeof(serv_addr));
2028 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002029 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002030 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002031 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002032 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002033 }
Andy Green0d338332011-02-12 11:57:43 +00002034
Andy Greenb5b23192013-02-11 17:13:32 +08002035 wsi = (struct libwebsocket *)malloc(
2036 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002037 if (wsi == NULL) {
2038 lwsl_err("Out of mem\n");
2039 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002040 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002041 }
Andy Greenb5b23192013-02-11 17:13:32 +08002042 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002043 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002044#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002045 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002046#endif
Andy Green0d338332011-02-12 11:57:43 +00002047 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002048
2049 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002050
Andy Green65b0e912013-01-16 07:59:47 +08002051 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2052 context->listen_service_count = 0;
2053 context->listen_service_fd = sockfd;
2054
Andy Greena824d182013-01-15 20:52:29 +08002055 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002056 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002057 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002058#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002059
Andy Green6ee372f2012-04-09 15:09:01 +08002060 /*
2061 * drop any root privs for this process
2062 * to listen on port < 1023 we would have needed root, but now we are
2063 * listening, we don't want the power for anything else
2064 */
Peter Hinz56885f32011-03-02 22:03:47 +00002065#ifdef WIN32
2066#else
Andy Green1b265272013-02-09 14:01:09 +08002067 if (info->gid != -1)
2068 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002069 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002070 if (info->uid != -1)
2071 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002072 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002073#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002074
Andy Green6f520a52013-01-29 17:57:39 +08002075 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002076
Peter Hinz56885f32011-03-02 22:03:47 +00002077 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002078 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002079 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002080
Andy Green43db0452013-01-10 19:50:35 +08002081 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002082 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002083
Andy Green1b265272013-02-09 14:01:09 +08002084 info->protocols[context->count_protocols].owning_server =
2085 context;
2086 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002087 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002088
2089 /*
2090 * inform all the protocols that they are doing their one-time
2091 * initialization if they want to
2092 */
2093 info->protocols[context->count_protocols].callback(context,
2094 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002095 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002096
Andy Green3182ece2013-01-20 17:08:31 +08002097#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002098 /*
2099 * give all extensions a chance to create any per-context
2100 * allocations they need
2101 */
2102
2103 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002104 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002105 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002106
Andy Green1b265272013-02-09 14:01:09 +08002107 if (info->extensions) {
2108 ext = info->extensions;
2109 while (ext->callback) {
2110 lwsl_ext(" Extension: %s\n", ext->name);
2111 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002112 (enum libwebsocket_extension_callback_reasons)m,
2113 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002114 ext++;
2115 }
Andy Greena41314f2011-05-23 10:00:03 +01002116 }
Andy Green3182ece2013-01-20 17:08:31 +08002117#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002118 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002119
2120bail:
2121 libwebsocket_context_destroy(context);
2122 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002123}
Andy Greenb45993c2010-12-18 15:13:50 +00002124
Andy Greenb45993c2010-12-18 15:13:50 +00002125/**
2126 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002127 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002128 * @wsi: pointer to struct websocket you want to know the protocol of
2129 *
Andy Green8f037e42010-12-19 22:13:26 +00002130 *
Andy Green6f520a52013-01-29 17:57:39 +08002131 * Some apis can act on all live connections of a given protocol,
2132 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002133 */
Andy Greenab990e42010-10-31 12:42:52 +00002134
Andy Greenb45993c2010-12-18 15:13:50 +00002135const struct libwebsocket_protocols *
2136libwebsockets_get_protocol(struct libwebsocket *wsi)
2137{
2138 return wsi->protocol;
2139}
2140
Andy Green82c3d542011-03-07 21:16:31 +00002141int
2142libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2143{
Andy Green623a98d2013-01-21 11:04:23 +08002144 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002145}
Alex Bligh49146db2011-11-07 17:19:25 +08002146
David Galeanoe2cf9922013-01-09 18:06:55 +08002147unsigned char
2148libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2149{
Andy Green623a98d2013-01-21 11:04:23 +08002150 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002151}
2152
Alex Bligh49146db2011-11-07 17:19:25 +08002153void *
2154libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2155{
2156 /* allocate the per-connection user memory (if any) */
2157
2158 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2159 wsi->user_space = malloc(
2160 wsi->protocol->per_session_data_size);
2161 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002162 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002163 return NULL;
2164 }
Andy Green6ee372f2012-04-09 15:09:01 +08002165 memset(wsi->user_space, 0,
2166 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002167 }
2168 return wsi->user_space;
2169}
Andy Green43db0452013-01-10 19:50:35 +08002170
Andy Green0b319092013-01-19 11:17:56 +08002171static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002172{
Andy Green0b319092013-01-19 11:17:56 +08002173 char buf[300];
2174 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002175 int n;
2176
2177 gettimeofday(&tv, NULL);
2178
2179 buf[0] = '\0';
2180 for (n = 0; n < LLL_COUNT; n++)
2181 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002182 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002183 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002184 break;
2185 }
Andy Greenb5b23192013-02-11 17:13:32 +08002186
Andy Green0b319092013-01-19 11:17:56 +08002187 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002188}
2189
Joakim Soderberg4c531232013-02-06 15:26:58 +09002190#ifdef WIN32
2191void lwsl_emit_syslog(int level, const char *line)
2192{
2193 lwsl_emit_stderr(level, line);
2194}
2195#else
Andy Greenc11db202013-01-19 11:12:16 +08002196void lwsl_emit_syslog(int level, const char *line)
2197{
2198 int syslog_level = LOG_DEBUG;
2199
2200 switch (level) {
2201 case LLL_ERR:
2202 syslog_level = LOG_ERR;
2203 break;
2204 case LLL_WARN:
2205 syslog_level = LOG_WARNING;
2206 break;
2207 case LLL_NOTICE:
2208 syslog_level = LOG_NOTICE;
2209 break;
2210 case LLL_INFO:
2211 syslog_level = LOG_INFO;
2212 break;
2213 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002214 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002215}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002216#endif
Andy Greenc11db202013-01-19 11:12:16 +08002217
Andy Green43db0452013-01-10 19:50:35 +08002218void _lws_log(int filter, const char *format, ...)
2219{
Andy Greende8f27a2013-01-12 09:17:42 +08002220 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002221 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002222
2223 if (!(log_level & filter))
2224 return;
2225
Andy Green43db0452013-01-10 19:50:35 +08002226 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002227 vsnprintf(buf, sizeof(buf), format, ap);
2228 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002229 va_end(ap);
2230
Andy Green0b319092013-01-19 11:17:56 +08002231 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002232}
2233
2234/**
2235 * lws_set_log_level() - Set the logging bitfield
2236 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002237 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2238 * function to perform log string emission instead of
2239 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002240 *
Andy Greende8f27a2013-01-12 09:17:42 +08002241 * log level defaults to "err" and "warn" contexts enabled only and
2242 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002243 */
2244
Andy Greenb5b23192013-02-11 17:13:32 +08002245void lws_set_log_level(int level, void (*log_emit_function)(int level,
2246 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002247{
2248 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002249 if (log_emit_function)
2250 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002251}