blob: 31d6f6793d99cd156dd82cf4530f215fc7bcf6ee [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>
Peter Hinz56885f32011-03-02 22:03:47 +000027#else
Davidc4ef7b12013-01-12 20:39:47 +080028#ifdef LWS_BUILTIN_GETIFADDRS
29#include <getifaddrs.h>
30#else
Peter Hinz56885f32011-03-02 22:03:47 +000031#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080032#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090033#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000034#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080035#include <sys/socket.h>
36#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000037#endif
Andy Green2e24da02011-03-05 16:12:04 +000038
39#ifdef LWS_OPENSSL_SUPPORT
40int openssl_websocket_private_data_index;
41#endif
42
Andy Greenaa6fc442012-04-12 13:26:49 +080043#ifdef __MINGW32__
44#include "../win32port/win32helpers/websock-w32.c"
45#else
46#ifdef __MINGW64__
47#include "../win32port/win32helpers/websock-w32.c"
48#endif
49#endif
50
Andy Green7b405452013-02-01 10:50:15 +080051#ifndef LWS_BUILD_HASH
52#define LWS_BUILD_HASH "unknown-build-hash"
53#endif
Andy Green3182ece2013-01-20 17:08:31 +080054
Andy Green7c19c342013-01-19 12:18:07 +080055static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080056static void lwsl_emit_stderr(int level, const char *line);
57static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
58
Andy Green7b405452013-02-01 10:50:15 +080059static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
60
Andy Green43db0452013-01-10 19:50:35 +080061static const char *log_level_names[] = {
62 "ERR",
63 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080064 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080065 "INFO",
66 "DEBUG",
67 "PARSER",
68 "HEADER",
69 "EXTENSION",
70 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080071 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080072};
73
Andy Green7b405452013-02-01 10:50:15 +080074/**
75 * lws_get_library_version: get version and git hash library built from
76 *
77 * returns a const char * to a string like "1.1 178d78c"
78 * representing the library version followed by the git head hash it
79 * was built from
80 */
81
82const char *
83lws_get_library_version(void)
84{
85 return library_version;
86}
87
Andy Green0d338332011-02-12 11:57:43 +000088int
Andy Greendfb23042013-01-17 12:26:48 +080089insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +000090{
Andy Greendfb23042013-01-17 12:26:48 +080091 if (context->fds_count >= context->max_fds) {
92 lwsl_err("Reached limit of fds tracking (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +000093 return 1;
94 }
95
Andy Greendfb23042013-01-17 12:26:48 +080096 if (wsi->sock > context->max_fds) {
97 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
98 return 1;
99 }
100
101 assert(wsi);
102 assert(wsi->sock);
103
104 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, context->fds_count);
105
106 context->lws_lookup[wsi->sock] = wsi;
107 wsi->position_in_fds_table = context->fds_count;
108 context->fds[context->fds_count].fd = wsi->sock;
109 context->fds[context->fds_count].events = POLLIN;
110 context->fds[context->fds_count++].revents = 0;
111
112 /* external POLL support via protocol 0 */
113 context->protocols[0].callback(context, wsi,
114 LWS_CALLBACK_ADD_POLL_FD,
115 (void *)(long)wsi->sock, NULL, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000116
117 return 0;
118}
119
Andy Greendfb23042013-01-17 12:26:48 +0800120static int
121remove_wsi_socket_from_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000122{
Andy Greendfb23042013-01-17 12:26:48 +0800123 int m;
Andy Green0d338332011-02-12 11:57:43 +0000124
Andy Greendfb23042013-01-17 12:26:48 +0800125 if (!--context->fds_count)
126 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000127
Andy Greendfb23042013-01-17 12:26:48 +0800128 if (wsi->sock > context->max_fds) {
129 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
130 return 1;
131 }
Andy Green0d338332011-02-12 11:57:43 +0000132
Andy Greendfb23042013-01-17 12:26:48 +0800133 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, wsi->position_in_fds_table);
134
135 m = wsi->position_in_fds_table; /* replace the contents for this */
136
137 /* have the last guy take up the vacant slot */
138 context->fds[m] = context->fds[context->fds_count]; /* vacant fds slot filled with end one */
139 /* end guy's fds_lookup entry remains unchanged (still same fd pointing to same wsi) */
140 /* end guy's "position in fds table" changed */
141 context->lws_lookup[context->fds[context->fds_count].fd]->position_in_fds_table = m;
142 /* deletion guy's lws_lookup entry needs nuking */
143 context->lws_lookup[wsi->sock] = NULL; /* no WSI for the socket of the wsi being removed*/
144 wsi->position_in_fds_table = -1; /* removed wsi has no position any more */
145
146do_ext:
147 /* remove also from external POLL support via protocol 0 */
148 if (wsi->sock)
149 context->protocols[0].callback(context, wsi,
150 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
151
152 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000153}
154
Andy Green32375b72011-02-19 08:32:53 +0000155
Andy Green8f037e42010-12-19 22:13:26 +0000156void
Peter Hinz56885f32011-03-02 22:03:47 +0000157libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000158 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000159{
Andy Greenb45993c2010-12-18 15:13:50 +0000160 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000161 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000162 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
163 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800164#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000165 int ret;
166 int m;
167 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100168 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800169#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000170
Andy Green4b6fbe12011-02-14 08:03:48 +0000171 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000172 return;
173
Andy Green62c54d22011-02-14 09:14:25 +0000174 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000175
Andy Green62c54d22011-02-14 09:14:25 +0000176 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000177 return;
178
Andy Green623a98d2013-01-21 11:04:23 +0800179 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000180
Andy Greenb8b247d2013-01-22 07:20:08 +0800181 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
182 close(wsi->u.http.fd);
183 wsi->u.http.fd = 0;
184 }
185
Andy Green3182ece2013-01-20 17:08:31 +0800186#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000187 /*
Andy Green68b45042011-05-25 21:41:57 +0100188 * are his extensions okay with him closing? Eg he might be a mux
189 * parent and just his ch1 aspect is closing?
190 */
191
Andy Green68b45042011-05-25 21:41:57 +0100192 for (n = 0; n < wsi->count_active_extensions; n++) {
193 if (!wsi->active_extensions[n]->callback)
194 continue;
195
196 m = wsi->active_extensions[n]->callback(context,
197 wsi->active_extensions[n], wsi,
198 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
199 wsi->active_extensions_user[n], NULL, 0);
200
201 /*
202 * if somebody vetoed actually closing him at this time....
203 * up to the extension to track the attempted close, let's
204 * just bail
205 */
206
207 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800208 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100209 return;
210 }
211 }
212
Andy Green68b45042011-05-25 21:41:57 +0100213 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000214 * flush any tx pending from extensions, since we may send close packet
215 * if there are problems with send, just nuke the connection
216 */
217
218 ret = 1;
219 while (ret == 1) {
220
221 /* default to nobody has more to spill */
222
223 ret = 0;
224 eff_buf.token = NULL;
225 eff_buf.token_len = 0;
226
227 /* show every extension the new incoming data */
228
229 for (n = 0; n < wsi->count_active_extensions; n++) {
230 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000231 wsi->protocol->owning_server,
232 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000233 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
234 wsi->active_extensions_user[n], &eff_buf, 0);
235 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800236 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000237 goto just_kill_connection;
238 }
239 if (m)
240 /*
241 * at least one extension told us he has more
242 * to spill, so we will go around again after
243 */
244 ret = 1;
245 }
246
247 /* assuming they left us something to send, send it */
248
249 if (eff_buf.token_len)
250 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Green0303db42013-01-17 14:46:43 +0800251 eff_buf.token_len)) {
252 lwsl_debug("close: sending final extension spill had problems\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000253 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800254 }
Andy Greenc44159f2011-03-07 07:08:18 +0000255 }
Andy Green3182ece2013-01-20 17:08:31 +0800256#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000257
258 /*
Andy Greenda527df2011-03-07 07:08:12 +0000259 * signal we are closing, libsocket_write will
260 * add any necessary version-specific stuff. If the write fails,
261 * no worries we are closing anyway. If we didn't initiate this
262 * close, then our state has been changed to
263 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
264 *
265 * Likewise if it's a second call to close this connection after we
266 * sent the close indication to the peer already, we are in state
267 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
268 */
269
270 if (old_state == WSI_STATE_ESTABLISHED &&
271 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100272
Andy Green43db0452013-01-10 19:50:35 +0800273 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100274
Andy Greenda527df2011-03-07 07:08:12 +0000275 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
276 0, LWS_WRITE_CLOSE);
277 if (!n) {
278 /*
279 * we have sent a nice protocol level indication we
280 * now wish to close, we should not send anything more
281 */
282
283 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
284
Andy Green0303db42013-01-17 14:46:43 +0800285 /* and we should wait for a reply for a bit out of politeness */
Andy Greenda527df2011-03-07 07:08:12 +0000286
287 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800288 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000289
Andy Green43db0452013-01-10 19:50:35 +0800290 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000291
292 return;
293 }
294
Andy Green0303db42013-01-17 14:46:43 +0800295 lwsl_info("close: sending the close packet failed, hanging up\n");
296
Andy Greenda527df2011-03-07 07:08:12 +0000297 /* else, the send failed and we should just hang up */
298 }
299
Andy Green3182ece2013-01-20 17:08:31 +0800300#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000301just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800302#endif
Andy Green66a16f32011-05-24 22:07:45 +0100303
Andy Green43db0452013-01-10 19:50:35 +0800304 lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100305
Andy Greenda527df2011-03-07 07:08:12 +0000306 /*
307 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800308 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000309 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000310
Andy Greendfb23042013-01-17 12:26:48 +0800311 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000312
Andy Green251f6fa2010-11-03 11:13:06 +0000313 wsi->state = WSI_STATE_DEAD_SOCKET;
314
Andy Green54495112013-02-06 21:10:16 +0900315 if (old_state == WSI_STATE_ESTABLISHED && wsi->u.ws.rx_user_buffer) {
316 free(wsi->u.ws.rx_user_buffer);
317 wsi->u.ws.rx_user_buffer = NULL;
318 }
319
Andy Green4b6fbe12011-02-14 08:03:48 +0000320 /* tell the user it's all over for this guy */
321
Andy Greend4302732011-02-28 07:45:29 +0000322 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800323 ((old_state == WSI_STATE_ESTABLISHED) ||
324 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
325 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800326 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000327 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000328 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800329 } else
Andy Green43db0452013-01-10 19:50:35 +0800330 lwsl_debug("not calling back closed, old_state=%d\n", old_state);
Andy Green251f6fa2010-11-03 11:13:06 +0000331
Andy Green3182ece2013-01-20 17:08:31 +0800332#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000333 /* deallocate any active extension contexts */
334
335 for (n = 0; n < wsi->count_active_extensions; n++) {
336 if (!wsi->active_extensions[n]->callback)
337 continue;
338
Andy Green46c2ea02011-03-22 09:04:01 +0000339 wsi->active_extensions[n]->callback(context,
340 wsi->active_extensions[n], wsi,
341 LWS_EXT_CALLBACK_DESTROY,
342 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000343
344 free(wsi->active_extensions_user[n]);
345 }
346
Andy Greena41314f2011-05-23 10:00:03 +0100347 /*
348 * inform all extensions in case they tracked this guy out of band
349 * even though not active on him specifically
350 */
351
352 ext = context->extensions;
353 while (ext && ext->callback) {
354 ext->callback(context, ext, wsi,
355 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
356 NULL, NULL, 0);
357 ext++;
358 }
Andy Green3182ece2013-01-20 17:08:31 +0800359#endif
Andy Greena41314f2011-05-23 10:00:03 +0100360
Andy Greena1ce6be2013-01-18 11:43:21 +0800361#ifndef LWS_NO_CLIENT
Andy Greena41314f2011-05-23 10:00:03 +0100362 if (wsi->c_address)
363 free(wsi->c_address);
Andy Greena1ce6be2013-01-18 11:43:21 +0800364#endif
Andy Green623a98d2013-01-21 11:04:23 +0800365 if (wsi->u.ws.rxflow_buffer)
366 free(wsi->u.ws.rxflow_buffer);
Andy Green706961d2013-01-17 16:50:35 +0800367
Andy Green43db0452013-01-10 19:50:35 +0800368/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000369
Andy Green3faa9c72010-11-08 17:03:03 +0000370#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000371 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000372 n = SSL_get_fd(wsi->ssl);
373 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800374 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000375 SSL_free(wsi->ssl);
376 } else {
377#endif
Andy Green0303db42013-01-17 14:46:43 +0800378 if (wsi->sock) {
379 n = shutdown(wsi->sock, SHUT_RDWR);
380 if (n)
381 lwsl_debug("closing: shutdown returned %d\n", errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800382
Andy Green0303db42013-01-17 14:46:43 +0800383 n = compatible_close(wsi->sock);
384 if (n)
385 lwsl_debug("closing: close returned %d\n", errno);
386 }
Andy Green3faa9c72010-11-08 17:03:03 +0000387#ifdef LWS_OPENSSL_SUPPORT
388 }
389#endif
David Brooks2c60d952012-04-20 12:19:01 +0800390 if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000391 free(wsi->user_space);
392
Andy Green251f6fa2010-11-03 11:13:06 +0000393 free(wsi);
394}
395
Andy Green07034092011-02-13 08:37:12 +0000396/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000397 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800398 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000399 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000400 * @fd: Connection socket descriptor
401 */
402
403void
Peter Hinz56885f32011-03-02 22:03:47 +0000404libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000405{
Andy Greendfb23042013-01-17 12:26:48 +0800406 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000407
Andy Greendfb23042013-01-17 12:26:48 +0800408 if (wsi) {
Andy Green467c7ef2013-01-30 12:28:34 +0800409 lwsl_info("closing connection at libwebsockets_hangup_on_client:\n");
Andy Greendfb23042013-01-17 12:26:48 +0800410 libwebsocket_close_and_free_session(context,
411 wsi, LWS_CLOSE_STATUS_NOSTATUS);
412 } else
413 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000414}
415
416
417/**
Andy Green07034092011-02-13 08:37:12 +0000418 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800419 * @context: Libwebsockets context
420 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000421 * @fd: Connection socket descriptor
422 * @name: Buffer to take client address name
423 * @name_len: Length of client address name buffer
424 * @rip: Buffer to take client address IP qotted quad
425 * @rip_len: Length of client address IP buffer
426 *
427 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800428 * the client connected with socket descriptor @fd. Names may be
429 * truncated if there is not enough room. If either cannot be
430 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000431 */
432
433void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800434libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
435 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000436 char *rip, int rip_len)
437{
438 unsigned int len;
439 struct sockaddr_in sin;
440 struct hostent *host;
441 struct hostent *host1;
442 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000443 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000444 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800445 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800446#ifdef AF_LOCAL
447 struct sockaddr_un *un;
448#endif
Andy Green07034092011-02-13 08:37:12 +0000449
450 rip[0] = '\0';
451 name[0] = '\0';
452
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800453 lws_latency_pre(context, wsi);
454
Andy Green07034092011-02-13 08:37:12 +0000455 len = sizeof sin;
456 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
457 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800458 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000459 }
Andy Green6ee372f2012-04-09 15:09:01 +0800460
Andy Green07034092011-02-13 08:37:12 +0000461 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
462 AF_INET);
463 if (host == NULL) {
464 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800465 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000466 }
467
468 strncpy(name, host->h_name, name_len);
469 name[name_len - 1] = '\0';
470
471 host1 = gethostbyname(host->h_name);
472 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800473 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000474 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000475 n = 0;
476 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000477 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000478 if (p == NULL)
479 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000480 if ((host1->h_addrtype != AF_INET)
481#ifdef AF_LOCAL
482 && (host1->h_addrtype != AF_LOCAL)
483#endif
484 )
Andy Green07034092011-02-13 08:37:12 +0000485 continue;
486
Andy Green7627af52011-03-09 15:13:52 +0000487 if (host1->h_addrtype == AF_INET)
488 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000489#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000490 else {
491 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800492 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000493 ip[sizeof(ip) - 1] = '\0';
494 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000495#endif
Andy Green07034092011-02-13 08:37:12 +0000496 p = NULL;
497 strncpy(rip, ip, rip_len);
498 rip[rip_len - 1] = '\0';
499 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800500
501 ret = 0;
502bail:
503 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000504}
Andy Green9f990342011-02-12 11:57:45 +0000505
Peter Hinz56885f32011-03-02 22:03:47 +0000506int libwebsockets_get_random(struct libwebsocket_context *context,
507 void *buf, int len)
508{
509 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800510 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000511
512#ifdef WIN32
513 for (n = 0; n < len; n++)
514 p[n] = (unsigned char)rand();
515#else
516 n = read(context->fd_random, p, len);
517#endif
518
519 return n;
520}
521
Andy Green95a7b5d2011-03-06 10:29:39 +0000522int lws_send_pipe_choked(struct libwebsocket *wsi)
523{
524 struct pollfd fds;
525
526 fds.fd = wsi->sock;
527 fds.events = POLLOUT;
528 fds.revents = 0;
529
530 if (poll(&fds, 1, 0) != 1)
531 return 1;
532
533 if ((fds.revents & POLLOUT) == 0)
534 return 1;
535
536 /* okay to send another packet without blocking */
537
538 return 0;
539}
540
Andy Greena41314f2011-05-23 10:00:03 +0100541int
Andy Green3b84c002011-03-06 13:14:42 +0000542lws_handle_POLLOUT_event(struct libwebsocket_context *context,
543 struct libwebsocket *wsi, struct pollfd *pollfd)
544{
Andy Green3b84c002011-03-06 13:14:42 +0000545 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800546
Andy Green3182ece2013-01-20 17:08:31 +0800547#ifndef LWS_NO_EXTENSIONS
548 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000549 int ret;
550 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100551 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000552
Andy Greena41314f2011-05-23 10:00:03 +0100553 for (n = 0; n < wsi->count_active_extensions; n++) {
554 if (!wsi->active_extensions[n]->callback)
555 continue;
556
557 m = wsi->active_extensions[n]->callback(context,
558 wsi->active_extensions[n], wsi,
559 LWS_EXT_CALLBACK_IS_WRITEABLE,
560 wsi->active_extensions_user[n], NULL, 0);
561 if (m > handled)
562 handled = m;
563 }
564
565 if (handled == 1)
566 goto notify_action;
567
568 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000569 goto user_service;
570
571 /*
572 * check in on the active extensions, see if they
573 * had pending stuff to spill... they need to get the
574 * first look-in otherwise sequence will be disordered
575 *
576 * NULL, zero-length eff_buf means just spill pending
577 */
578
579 ret = 1;
580 while (ret == 1) {
581
582 /* default to nobody has more to spill */
583
584 ret = 0;
585 eff_buf.token = NULL;
586 eff_buf.token_len = 0;
587
588 /* give every extension a chance to spill */
589
590 for (n = 0; n < wsi->count_active_extensions; n++) {
591 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000592 wsi->protocol->owning_server,
593 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000594 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
595 wsi->active_extensions_user[n], &eff_buf, 0);
596 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800597 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000598 return -1;
599 }
600 if (m)
601 /*
602 * at least one extension told us he has more
603 * to spill, so we will go around again after
604 */
605 ret = 1;
606 }
607
608 /* assuming they gave us something to send, send it */
609
610 if (eff_buf.token_len) {
611 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
612 eff_buf.token_len))
613 return -1;
614 } else
615 continue;
616
617 /* no extension has more to spill */
618
619 if (!ret)
620 continue;
621
622 /*
623 * There's more to spill from an extension, but we just sent
624 * something... did that leave the pipe choked?
625 */
626
627 if (!lws_send_pipe_choked(wsi))
628 /* no we could add more */
629 continue;
630
Andy Green43db0452013-01-10 19:50:35 +0800631 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000632
633 /*
634 * Yes, he's choked. Leave the POLLOUT masked on so we will
635 * come back here when he is unchoked. Don't call the user
636 * callback to enforce ordering of spilling, he'll get called
637 * when we come back here and there's nothing more to spill.
638 */
639
640 return 0;
641 }
642
643 wsi->extension_data_pending = 0;
644
645user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800646#endif
Andy Green3b84c002011-03-06 13:14:42 +0000647 /* one shot */
648
Andy Greena41314f2011-05-23 10:00:03 +0100649 if (pollfd) {
650 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000651
Andy Greena41314f2011-05-23 10:00:03 +0100652 /* external POLL support via protocol 0 */
653 context->protocols[0].callback(context, wsi,
654 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
655 (void *)(long)wsi->sock, NULL, POLLOUT);
656 }
Andy Green3182ece2013-01-20 17:08:31 +0800657#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100658notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800659#endif
Andy Green3b84c002011-03-06 13:14:42 +0000660
Andy Green9e4c2b62011-03-07 20:47:39 +0000661 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
662 n = LWS_CALLBACK_CLIENT_WRITEABLE;
663 else
664 n = LWS_CALLBACK_SERVER_WRITEABLE;
665
Andy Greenb8b247d2013-01-22 07:20:08 +0800666 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Green706961d2013-01-17 16:50:35 +0800667 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000668}
669
670
671
Andy Greena41314f2011-05-23 10:00:03 +0100672void
673libwebsocket_service_timeout_check(struct libwebsocket_context *context,
674 struct libwebsocket *wsi, unsigned int sec)
675{
Andy Green3182ece2013-01-20 17:08:31 +0800676#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100677 int n;
678
679 /*
680 * if extensions want in on it (eg, we are a mux parent)
681 * give them a chance to service child timeouts
682 */
683
684 for (n = 0; n < wsi->count_active_extensions; n++)
685 wsi->active_extensions[n]->callback(
686 context, wsi->active_extensions[n],
687 wsi, LWS_EXT_CALLBACK_1HZ,
688 wsi->active_extensions_user[n], NULL, sec);
689
Andy Green3182ece2013-01-20 17:08:31 +0800690#endif
Andy Greena41314f2011-05-23 10:00:03 +0100691 if (!wsi->pending_timeout)
692 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800693
Andy Greena41314f2011-05-23 10:00:03 +0100694 /*
695 * if we went beyond the allowed time, kill the
696 * connection
697 */
698
699 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800700 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100701 libwebsocket_close_and_free_session(context,
702 wsi, LWS_CLOSE_STATUS_NOSTATUS);
703 }
704}
705
Andy Green9f990342011-02-12 11:57:45 +0000706/**
707 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000708 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000709 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800710 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000711 *
Andy Green75006172013-01-22 12:32:11 +0800712 * This function takes a pollfd that has POLLIN or POLLOUT activity and
713 * services it according to the state of the associated struct libwebsocket.
714 *
715 * The one call deals with all "service" that might happen on a socket
716 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000717 */
718
719int
Peter Hinz56885f32011-03-02 22:03:47 +0000720libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000721 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000722{
Andy Greena1ce6be2013-01-18 11:43:21 +0800723 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000724 int n;
Andy Green0d338332011-02-12 11:57:43 +0000725 int m;
Andy Greena71eafc2011-02-14 17:59:43 +0000726 struct timeval tv;
Jack Mitchelldaed4fb2013-01-30 18:53:36 +0800727#ifdef LWS_OPENSSL_SUPPORT
Andy Green467c7ef2013-01-30 12:28:34 +0800728 char ssl_err_buf[512];
Jack Mitchelldaed4fb2013-01-30 18:53:36 +0800729#endif
Andy Green6f520a52013-01-29 17:57:39 +0800730
Andy Green3182ece2013-01-20 17:08:31 +0800731#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000732 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800733#endif
Andy Green98a717c2011-03-06 13:14:15 +0000734 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800735#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800736 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800737#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800738#ifndef LWS_NO_SERVER
739 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
740#endif
Andy Greena71eafc2011-02-14 17:59:43 +0000741 /*
742 * you can call us with pollfd = NULL to just allow the once-per-second
743 * global timeout checks; if less than a second since the last check
744 * it returns immediately then.
745 */
746
747 gettimeofday(&tv, NULL);
748
Peter Hinz56885f32011-03-02 22:03:47 +0000749 if (context->last_timeout_check_s != tv.tv_sec) {
750 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000751
Joakim Soderberg4c531232013-02-06 15:26:58 +0900752 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800753 /* if our parent went down, don't linger around */
754 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
755 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900756 #endif
Andy Green24cba922013-01-19 13:56:10 +0800757
Andy Greena71eafc2011-02-14 17:59:43 +0000758 /* global timeout check once per second */
759
Peter Hinz56885f32011-03-02 22:03:47 +0000760 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800761 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
762 if (!new_wsi)
763 continue;
764 libwebsocket_service_timeout_check(context,
765 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000766 }
767 }
768
769 /* just here for timeout management? */
770
771 if (pollfd == NULL)
772 return 0;
773
774 /* no, here to service a socket descriptor */
775
Andy Green65b0e912013-01-16 07:59:47 +0800776 /*
777 * deal with listen service piggybacking
778 * every listen_service_modulo services of other fds, we
779 * sneak one in to service the listen socket if there's anything waiting
780 *
781 * To handle connection storms, as found in ab, if we previously saw a
782 * pending connection here, it causes us to check again next time.
783 */
784
785 if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) {
786 context->listen_service_count++;
787 if (context->listen_service_extraseen ||
788 context->listen_service_count == context->listen_service_modulo) {
789 context->listen_service_count = 0;
790 m = 1;
791 if (context->listen_service_extraseen > 5)
792 m = 2;
793 while (m--) {
794 /* even with extpoll, we prepared this internal fds for listen */
795 n = poll(&context->fds[0], 1, 0);
796 if (n > 0) { /* there's a connection waiting for us */
797 libwebsocket_service_fd(context, &context->fds[0]);
798 context->listen_service_extraseen++;
799 } else {
800 if (context->listen_service_extraseen)
801 context->listen_service_extraseen--;
802 break;
803 }
804 }
805 }
806
807 }
808
809 /* okay, what we came here to do... */
810
Andy Greendfb23042013-01-17 12:26:48 +0800811 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800812 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800813 if (pollfd->fd > 11)
814 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800815 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800816 }
Andy Green8f037e42010-12-19 22:13:26 +0000817
Andy Green0d338332011-02-12 11:57:43 +0000818 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800819
Andy Greena1ce6be2013-01-18 11:43:21 +0800820#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800821 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000822 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800823 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800824 return lws_server_socket_service(context, wsi, pollfd);
825#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000826
Andy Green0d338332011-02-12 11:57:43 +0000827 case LWS_CONNMODE_WS_SERVING:
828 case LWS_CONNMODE_WS_CLIENT:
829
830 /* handle session socket closed */
831
832 if (pollfd->revents & (POLLERR | POLLHUP)) {
833
Andy Green43db0452013-01-10 19:50:35 +0800834 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000835 (void *)wsi, pollfd->fd);
836
Peter Hinz56885f32011-03-02 22:03:47 +0000837 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000838 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800839 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000840 }
841
Andy Green0d338332011-02-12 11:57:43 +0000842 /* the guy requested a callback when it was OK to write */
843
Andy Greenda527df2011-03-07 07:08:12 +0000844 if ((pollfd->revents & POLLOUT) &&
845 wsi->state == WSI_STATE_ESTABLISHED)
846 if (lws_handle_POLLOUT_event(context, wsi,
847 pollfd) < 0) {
848 libwebsocket_close_and_free_session(
849 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800850 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000851 }
Andy Green0d338332011-02-12 11:57:43 +0000852
Andy Green0d338332011-02-12 11:57:43 +0000853
854 /* any incoming data ready? */
855
856 if (!(pollfd->revents & POLLIN))
857 break;
858
Andy Greenb45993c2010-12-18 15:13:50 +0000859#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800860read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800861 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800862 eff_buf.token_len = SSL_read(wsi->ssl,
863 context->service_buffer,
864 sizeof context->service_buffer);
Andy Green467c7ef2013-01-30 12:28:34 +0800865 if (!eff_buf.token_len) {
866 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800867 lwsl_err("SSL_read returned 0 with reason %s\n",
868 ERR_error_string(n, ssl_err_buf));
Andy Green467c7ef2013-01-30 12:28:34 +0800869 }
870 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000871#endif
Andy Greene84652c2013-02-08 13:01:02 +0800872 eff_buf.token_len = recv(pollfd->fd,
873 context->service_buffer,
874 sizeof context->service_buffer, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000875
Andy Green98a717c2011-03-06 13:14:15 +0000876 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800877 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000878 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200879 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800880 libwebsocket_close_and_free_session(context,
881 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800882 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000883 }
Andy Green98a717c2011-03-06 13:14:15 +0000884 if (!eff_buf.token_len) {
Andy Green467c7ef2013-01-30 12:28:34 +0800885 lwsl_info("closing connection due to zero length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000886 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800887 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800888 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000889 }
890
Andy Green98a717c2011-03-06 13:14:15 +0000891 /*
892 * give any active extensions a chance to munge the buffer
893 * before parse. We pass in a pointer to an lws_tokens struct
894 * prepared with the default buffer and content length that's in
895 * there. Rather than rewrite the default buffer, extensions
896 * that expect to grow the buffer can adapt .token to
897 * point to their own per-connection buffer in the extension
898 * user allocation. By default with no extensions or no
899 * extension callback handling, just the normal input buffer is
900 * used then so it is efficient.
901 */
Andy Greenb45993c2010-12-18 15:13:50 +0000902
Andy Greene84652c2013-02-08 13:01:02 +0800903 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +0800904#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000905 more = 1;
906 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000907
Andy Green98a717c2011-03-06 13:14:15 +0000908 more = 0;
909
910 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000911 m = wsi->active_extensions[n]->callback(context,
912 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000913 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000914 wsi->active_extensions_user[n],
915 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000916 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800917 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800918 "Extension reports fatal error\n");
919 libwebsocket_close_and_free_session(
920 context, wsi,
921 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800922 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000923 }
924 if (m)
925 more = 1;
926 }
Andy Green3182ece2013-01-20 17:08:31 +0800927#endif
Andy Green98a717c2011-03-06 13:14:15 +0000928 /* service incoming data */
929
930 if (eff_buf.token_len) {
931 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800932 (unsigned char *)eff_buf.token,
933 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +0000934 if (n < 0)
935 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +0800936 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000937 }
Andy Green3182ece2013-01-20 17:08:31 +0800938#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000939 eff_buf.token = NULL;
940 eff_buf.token_len = 0;
941 }
Andy Green3182ece2013-01-20 17:08:31 +0800942#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +0800943
944#ifdef LWS_OPENSSL_SUPPORT
945 if (wsi->ssl && SSL_pending(wsi->ssl))
946 goto read_pending;
947#endif
Andy Green98a717c2011-03-06 13:14:15 +0000948 break;
Andy Green76f61e72013-01-16 11:53:05 +0800949
950 default:
Andy Green03674a62013-01-16 11:47:40 +0800951#ifdef LWS_NO_CLIENT
952 break;
953#else
Andy Green76f61e72013-01-16 11:53:05 +0800954 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800955#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000956 }
957
958 return 0;
959}
960
Andy Green0d338332011-02-12 11:57:43 +0000961
Andy Green6964bb52011-01-23 16:50:33 +0000962/**
963 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +0000964 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000965 *
966 * This function closes any active connections and then frees the
967 * context. After calling this, any further use of the context is
968 * undefined.
969 */
970void
Peter Hinz56885f32011-03-02 22:03:47 +0000971libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +0000972{
Andy Green3182ece2013-01-20 17:08:31 +0800973#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +0000974 int n;
975 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100976 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +0000977
Andy Greend636e352013-01-29 12:36:17 +0800978#ifdef LWS_LATENCY
979 if (context->worst_latency_info[0])
980 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
981#endif
982
Andy Greendfb23042013-01-17 12:26:48 +0800983 for (n = 0; n < context->fds_count; n++) {
984 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
985 libwebsocket_close_and_free_session(context,
986 wsi, LWS_CLOSE_STATUS_GOINGAWAY);
987 }
Andy Green6964bb52011-01-23 16:50:33 +0000988
Andy Greena41314f2011-05-23 10:00:03 +0100989 /*
990 * give all extensions a chance to clean up any per-context
991 * allocations they might have made
992 */
993
994 ext = context->extensions;
995 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
996 if (context->listen_port)
997 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +0800998 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800999 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001000 ext++;
1001 }
Andy Green3182ece2013-01-20 17:08:31 +08001002#endif
Andy Greena41314f2011-05-23 10:00:03 +01001003
Peter Hinz56885f32011-03-02 22:03:47 +00001004#ifdef WIN32
1005#else
1006 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001007#endif
1008
Peter Hinz56885f32011-03-02 22:03:47 +00001009#ifdef LWS_OPENSSL_SUPPORT
1010 if (context->ssl_ctx)
1011 SSL_CTX_free(context->ssl_ctx);
1012 if (context->ssl_client_ctx)
1013 SSL_CTX_free(context->ssl_client_ctx);
1014#endif
1015
1016 free(context);
1017
1018#ifdef WIN32
1019 WSACleanup();
1020#endif
Andy Green6964bb52011-01-23 16:50:33 +00001021}
1022
Andy Greend88146d2013-01-22 12:40:35 +08001023/**
1024 * libwebsocket_context_user() - get the user data associated with the whole context
1025 * @context: Websocket context
1026 *
1027 * This returns the optional user allocation that can be attached to
1028 * the context the sockets live in at context_create time. It's a way
1029 * to let all sockets serviced in the same context share data without
1030 * using globals statics in the user code.
1031 */
1032
1033
Alon Levy0291eb32012-10-19 11:21:56 +02001034LWS_EXTERN void *
1035libwebsocket_context_user(struct libwebsocket_context *context)
1036{
1037 return context->user_space;
1038}
1039
Andy Green6964bb52011-01-23 16:50:33 +00001040/**
1041 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001042 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001043 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1044 * service otherwise block and service immediately, returning
1045 * after the timeout if nothing needed service.
1046 *
1047 * This function deals with any pending websocket traffic, for three
1048 * kinds of event. It handles these events on both server and client
1049 * types of connection the same.
1050 *
1051 * 1) Accept new connections to our context's server
1052 *
Andy Green6f520a52013-01-29 17:57:39 +08001053 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001054 * server or client connections.
1055 *
1056 * You need to call this service function periodically to all the above
1057 * functions to happen; if your application is single-threaded you can
1058 * just call it in your main event loop.
1059 *
1060 * Alternatively you can fork a new process that asynchronously handles
1061 * calling this service in a loop. In that case you are happy if this
1062 * call blocks your thread until it needs to take care of something and
1063 * would call it with a large nonzero timeout. Your loop then takes no
1064 * CPU while there is nothing happening.
1065 *
1066 * If you are calling it in a single-threaded app, you don't want it to
1067 * wait around blocking other things in your loop from happening, so you
1068 * would call it with a timeout_ms of 0, so it returns immediately if
1069 * nothing is pending, or as soon as it services whatever was pending.
1070 */
1071
Andy Greenb45993c2010-12-18 15:13:50 +00001072
Andy Greene92cd172011-01-19 13:11:55 +00001073int
Peter Hinz56885f32011-03-02 22:03:47 +00001074libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001075{
1076 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001077
1078 /* stay dead once we are dead */
1079
Peter Hinz56885f32011-03-02 22:03:47 +00001080 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001081 return 1;
1082
Andy Green0d338332011-02-12 11:57:43 +00001083 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001084
Peter Hinz56885f32011-03-02 22:03:47 +00001085 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001086 if (n == 0) /* poll timeout */
1087 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001088
Andy Greendfb23042013-01-17 12:26:48 +08001089 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001090 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001091
Andy Greendfb23042013-01-17 12:26:48 +08001092 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001093
Peter Hinz56885f32011-03-02 22:03:47 +00001094 for (n = 0; n < context->fds_count; n++)
1095 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001096 if (libwebsocket_service_fd(context,
1097 &context->fds[n]) < 0)
1098 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001099 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001100}
1101
Andy Green3182ece2013-01-20 17:08:31 +08001102#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001103int
1104lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001105 struct libwebsocket *wsi,
1106 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001107 void *v, size_t len)
1108{
1109 int n;
1110 int handled = 0;
1111
1112 /* maybe an extension will take care of it for us */
1113
1114 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1115 if (!wsi->active_extensions[n]->callback)
1116 continue;
1117
1118 handled |= wsi->active_extensions[n]->callback(context,
1119 wsi->active_extensions[n], wsi,
1120 r, wsi->active_extensions_user[n], v, len);
1121 }
1122
1123 return handled;
1124}
1125
1126
1127void *
1128lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001129 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001130{
1131 int n = 0;
1132
Andy Green68b45042011-05-25 21:41:57 +01001133 if (wsi == NULL)
1134 return NULL;
1135
Andy Greena41314f2011-05-23 10:00:03 +01001136 while (n < wsi->count_active_extensions) {
1137 if (wsi->active_extensions[n] != ext) {
1138 n++;
1139 continue;
1140 }
1141 return wsi->active_extensions_user[n];
1142 }
1143
1144 return NULL;
1145}
Andy Green3182ece2013-01-20 17:08:31 +08001146#endif
Andy Greena41314f2011-05-23 10:00:03 +01001147
Andy Green90c7cbc2011-01-27 06:26:52 +00001148/**
1149 * libwebsocket_callback_on_writable() - Request a callback when this socket
1150 * becomes able to be written to without
1151 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001152 *
Peter Hinz56885f32011-03-02 22:03:47 +00001153 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001154 * @wsi: Websocket connection instance to get callback for
1155 */
1156
1157int
Peter Hinz56885f32011-03-02 22:03:47 +00001158libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001159 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001160{
Andy Green3182ece2013-01-20 17:08:31 +08001161#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001162 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001163 int handled = 0;
1164
1165 /* maybe an extension will take care of it for us */
1166
1167 for (n = 0; n < wsi->count_active_extensions; n++) {
1168 if (!wsi->active_extensions[n]->callback)
1169 continue;
1170
1171 handled |= wsi->active_extensions[n]->callback(context,
1172 wsi->active_extensions[n], wsi,
1173 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1174 wsi->active_extensions_user[n], NULL, 0);
1175 }
1176
1177 if (handled)
1178 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001179#endif
Andy Greendfb23042013-01-17 12:26:48 +08001180 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001181 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001182 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001183 return -1;
1184 }
1185
1186 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001187
Andy Green3221f922011-02-12 13:14:11 +00001188 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001189 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001190 LWS_CALLBACK_SET_MODE_POLL_FD,
1191 (void *)(long)wsi->sock, NULL, POLLOUT);
1192
Andy Green90c7cbc2011-01-27 06:26:52 +00001193 return 1;
1194}
1195
1196/**
1197 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1198 * all connections using the given protocol when it
1199 * becomes possible to write to each socket without
1200 * blocking in turn.
1201 *
1202 * @protocol: Protocol whose connections will get callbacks
1203 */
1204
1205int
1206libwebsocket_callback_on_writable_all_protocol(
1207 const struct libwebsocket_protocols *protocol)
1208{
Peter Hinz56885f32011-03-02 22:03:47 +00001209 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001210 int n;
Andy Green0d338332011-02-12 11:57:43 +00001211 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001212
Andy Greendfb23042013-01-17 12:26:48 +08001213 for (n = 0; n < context->fds_count; n++) {
1214 wsi = context->lws_lookup[context->fds[n].fd];
1215 if (!wsi)
1216 continue;
1217 if (wsi->protocol == protocol)
1218 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001219 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001220
1221 return 0;
1222}
1223
Andy Greenbe93fef2011-02-14 20:25:43 +00001224/**
1225 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1226 *
1227 * You will not need this unless you are doing something special
1228 *
1229 * @wsi: Websocket connection instance
1230 * @reason: timeout reason
1231 * @secs: how many seconds
1232 */
1233
1234void
1235libwebsocket_set_timeout(struct libwebsocket *wsi,
1236 enum pending_timeout reason, int secs)
1237{
1238 struct timeval tv;
1239
1240 gettimeofday(&tv, NULL);
1241
1242 wsi->pending_timeout_limit = tv.tv_sec + secs;
1243 wsi->pending_timeout = reason;
1244}
1245
Andy Greena6cbece2011-01-27 20:06:03 +00001246
1247/**
1248 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1249 *
1250 * You will not need this unless you are doing something special
1251 *
1252 * @wsi: Websocket connection instance
1253 */
1254
1255int
1256libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1257{
1258 return wsi->sock;
1259}
1260
Andy Greend636e352013-01-29 12:36:17 +08001261#ifdef LWS_LATENCY
1262void
1263lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completed)
1264{
1265 struct timeval tv;
1266 unsigned long u;
1267 char buf[256];
1268
1269 gettimeofday(&tv, NULL);
1270
1271 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1272
1273 if (action) {
1274 if (completed) {
1275 if (wsi->action_start == wsi->latency_start)
1276 sprintf(buf, "Completion first try lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1277 else
1278 sprintf(buf, "Completion %luus: lat %luus: %p: ret %d: %s\n", u - wsi->action_start, u - wsi->latency_start, (void *)wsi, ret, action);
1279 wsi->action_start = 0;
1280 } else
1281 sprintf(buf, "lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1282 if (u - wsi->latency_start > context->worst_latency) {
1283 context->worst_latency = u - wsi->latency_start;
1284 strcpy(context->worst_latency_info, buf);
1285 }
1286 lwsl_latency("%s", buf);
1287 } else {
1288 wsi->latency_start = u;
1289 if (!wsi->action_start)
1290 wsi->action_start = u;
1291 }
1292}
1293#endif
1294
Andy Greena1ce6be2013-01-18 11:43:21 +08001295#ifdef LWS_NO_SERVER
1296int
1297_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1298{
1299 return 0;
1300}
1301#else
Andy Green706961d2013-01-17 16:50:35 +08001302int
1303_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1304{
1305 struct libwebsocket_context *context = wsi->protocol->owning_server;
1306 int n;
1307
Andy Green623a98d2013-01-21 11:04:23 +08001308 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001309 return 0;
1310
Andy Green623a98d2013-01-21 11:04:23 +08001311 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001312
Andy Green623a98d2013-01-21 11:04:23 +08001313 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001314
1315 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001316 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001317 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1318 if (n < 0) {
Andy Green467c7ef2013-01-30 12:28:34 +08001319 lwsl_info("closing connection at libwebsocket_rx_flow_control:\n");
Andy Green706961d2013-01-17 16:50:35 +08001320 libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
1321 return -1;
1322 }
1323 if (n)
1324 /* oh he stuck again, do nothing */
1325 return 0;
1326 }
1327
Andy Green623a98d2013-01-21 11:04:23 +08001328 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001329 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1330 else
1331 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1332
Andy Green623a98d2013-01-21 11:04:23 +08001333 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001334 /* external POLL support via protocol 0 */
1335 context->protocols[0].callback(context, wsi,
1336 LWS_CALLBACK_SET_MODE_POLL_FD,
1337 (void *)(long)wsi->sock, NULL, POLLIN);
1338 else
1339 /* external POLL support via protocol 0 */
1340 context->protocols[0].callback(context, wsi,
1341 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1342 (void *)(long)wsi->sock, NULL, POLLIN);
1343
1344 return 1;
1345}
Andy Greena1ce6be2013-01-18 11:43:21 +08001346#endif
Andy Green706961d2013-01-17 16:50:35 +08001347
Andy Green90c7cbc2011-01-27 06:26:52 +00001348/**
1349 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1350 * receieved packets.
1351 *
1352 * If the output side of a server process becomes choked, this allows flow
1353 * control for the input side.
1354 *
1355 * @wsi: Websocket connection instance to get callback for
1356 * @enable: 0 = disable read servicing for this connection, 1 = enable
1357 */
1358
1359int
1360libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1361{
Andy Green623a98d2013-01-21 11:04:23 +08001362 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001363
Andy Green706961d2013-01-17 16:50:35 +08001364 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001365}
1366
Andy Green706961d2013-01-17 16:50:35 +08001367
Andy Green2ac5a6f2011-01-28 10:00:18 +00001368/**
1369 * libwebsocket_canonical_hostname() - returns this host's hostname
1370 *
1371 * This is typically used by client code to fill in the host parameter
1372 * when making a client connection. You can only call it after the context
1373 * has been created.
1374 *
Peter Hinz56885f32011-03-02 22:03:47 +00001375 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001376 */
1377
1378
1379extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001380libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001381{
Peter Hinz56885f32011-03-02 22:03:47 +00001382 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001383}
1384
1385
Andy Green90c7cbc2011-01-27 06:26:52 +00001386static void sigpipe_handler(int x)
1387{
1388}
1389
Andy Green6901cb32011-02-21 08:06:47 +00001390#ifdef LWS_OPENSSL_SUPPORT
1391static int
1392OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1393{
1394
1395 SSL *ssl;
1396 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001397 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001398
1399 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1400 SSL_get_ex_data_X509_STORE_CTX_idx());
1401
1402 /*
Andy Green2e24da02011-03-05 16:12:04 +00001403 * !!! nasty openssl requires the index to come as a library-scope
1404 * static
Andy Green6901cb32011-02-21 08:06:47 +00001405 */
Andy Green2e24da02011-03-05 16:12:04 +00001406 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001407
Peter Hinz56885f32011-03-02 22:03:47 +00001408 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001409 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1410 x509_ctx, ssl, preverify_ok);
1411
1412 /* convert return code from 0 = OK to 1 = OK */
1413
1414 if (!n)
1415 n = 1;
1416 else
1417 n = 0;
1418
1419 return n;
1420}
1421#endif
1422
Andy Green706961d2013-01-17 16:50:35 +08001423int user_callback_handle_rxflow(callback_function callback_function,
1424 struct libwebsocket_context * context,
1425 struct libwebsocket *wsi,
1426 enum libwebsocket_callback_reasons reason, void *user,
1427 void *in, size_t len)
1428{
1429 int n;
1430
1431 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenb8b247d2013-01-22 07:20:08 +08001432 if (n) {
1433 libwebsocket_close_and_free_session(context, wsi,
1434 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green706961d2013-01-17 16:50:35 +08001435 return n;
Andy Greenb8b247d2013-01-22 07:20:08 +08001436 }
Andy Green706961d2013-01-17 16:50:35 +08001437
1438 _libwebsocket_rx_flow_control(wsi);
1439
1440 return 0;
1441}
1442
Andy Greenb45993c2010-12-18 15:13:50 +00001443
Andy Greenab990e42010-10-31 12:42:52 +00001444/**
Andy Green4739e5c2011-01-22 12:51:57 +00001445 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001446 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001447 *
Andy Green1b265272013-02-09 14:01:09 +08001448 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001449 * of all initialization in one step.
1450 *
Andy Greene92cd172011-01-19 13:11:55 +00001451 * After initialization, it returns a struct libwebsocket_context * that
1452 * represents this server. After calling, user code needs to take care
1453 * of calling libwebsocket_service() with the context pointer to get the
1454 * server's sockets serviced. This can be done in the same process context
1455 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001456 *
Andy Green8f037e42010-12-19 22:13:26 +00001457 * The protocol callback functions are called for a handful of events
1458 * including http requests coming in, websocket connections becoming
1459 * established, and data arriving; it's also called periodically to allow
1460 * async transmission.
1461 *
1462 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1463 * at that time websocket protocol has not been negotiated. Other
1464 * protocols after the first one never see any HTTP callack activity.
1465 *
1466 * The server created is a simple http server by default; part of the
1467 * websocket standard is upgrading this http connection to a websocket one.
1468 *
1469 * This allows the same server to provide files like scripts and favicon /
1470 * images or whatever over http and dynamic data over websockets all in
1471 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001472 */
Andy Green4ea60062010-10-30 12:15:07 +01001473
Andy Greene92cd172011-01-19 13:11:55 +00001474struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001475libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001476{
1477 int n;
Peter Hinz56885f32011-03-02 22:03:47 +00001478 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001479 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001480#ifndef LWS_NO_SERVER
1481 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001482 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001483 struct sockaddr_in serv_addr;
1484#endif
Andy Green3182ece2013-01-20 17:08:31 +08001485#ifndef LWS_NO_EXTENSIONS
1486 int m;
Andy Green1b265272013-02-09 14:01:09 +08001487 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001488#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001489
Andy Green3faa9c72010-11-08 17:03:03 +00001490#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001491 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001492 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00001493#endif
1494
Joakim Soderberg4c531232013-02-06 15:26:58 +09001495#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001496 extern int get_daemonize_pid();
1497 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001498#endif
1499
Andy Greenb3a614a2013-01-19 13:08:17 +08001500 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001501 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001502 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1503 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
1504 lwsl_info(" LWS_INITIAL_HDR_ALLOC: %u\n", LWS_INITIAL_HDR_ALLOC);
1505 lwsl_info(" LWS_ADDITIONAL_HDR_ALLOC: %u\n", LWS_ADDITIONAL_HDR_ALLOC);
Andy Greenc0d6b632013-01-12 23:42:17 +08001506 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001507#ifndef LWS_NO_EXTENSIONS
Andy Greenc0d6b632013-01-12 23:42:17 +08001508 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001509#else
1510 lwsl_notice(" Configured without extension support\n");
1511#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001512 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1513 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1514 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1515 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1516 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001517
Peter Hinz56885f32011-03-02 22:03:47 +00001518#ifdef _WIN32
1519 {
1520 WORD wVersionRequested;
1521 WSADATA wsaData;
1522 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001523 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001524
1525 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1526 wVersionRequested = MAKEWORD(2, 2);
1527
1528 err = WSAStartup(wVersionRequested, &wsaData);
1529 if (err != 0) {
1530 /* Tell the user that we could not find a usable */
1531 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001532 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001533 return NULL;
1534 }
David Galeano7b11fec2011-10-04 19:55:18 +08001535
Andy Green6ee372f2012-04-09 15:09:01 +08001536 /* default to a poll() made out of select() */
1537 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001538
Andy Green6ee372f2012-04-09 15:09:01 +08001539 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001540 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001541 if (wsdll)
1542 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001543
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001544 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001545 if (!poll)
1546 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001547 }
1548#endif
1549
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001550 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001551 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001552 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001553 return NULL;
1554 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001555 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001556#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001557 context->started_with_parent = pid_daemon;
1558 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1559#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +09001560
1561 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001562 context->protocols = info->protocols;
1563 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001564 context->http_proxy_port = 0;
1565 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001566 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001567 /* to reduce this allocation, */
1568 context->max_fds = getdtablesize();
Andy Greenb3a614a2013-01-19 13:08:17 +08001569 lwsl_notice(" max fd tracked: %u\n", context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001570 lwsl_notice(" static allocation: %u bytes\n",
Andy Greenf2703422013-02-08 12:53:27 +08001571 sizeof(struct libwebsocket_context) +
Andy Greena17c6922013-01-20 20:21:54 +08001572 (sizeof(struct pollfd) * context->max_fds) +
1573 (sizeof(struct libwebsocket *) * context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001574
1575 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1576 if (context->fds == NULL) {
1577 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1578 free(context);
1579 return NULL;
1580 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08001581 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001582 if (context->lws_lookup == NULL) {
1583 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1584 free(context->fds);
1585 free(context);
1586 return NULL;
1587 }
Andy Greena17c6922013-01-20 20:21:54 +08001588
Peter Hinz56885f32011-03-02 22:03:47 +00001589 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001590#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001591 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001592#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001593 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001594 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001595
Peter Hinz56885f32011-03-02 22:03:47 +00001596#ifdef WIN32
1597 context->fd_random = 0;
1598#else
1599 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1600 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001601 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001602 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001603 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001604 }
Peter Hinz56885f32011-03-02 22:03:47 +00001605#endif
Andy Green44eee682011-02-10 09:32:24 +00001606
Peter Hinz56885f32011-03-02 22:03:47 +00001607#ifdef LWS_OPENSSL_SUPPORT
1608 context->use_ssl = 0;
1609 context->ssl_ctx = NULL;
1610 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001611 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001612#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001613
Andy Greena1ce6be2013-01-18 11:43:21 +08001614 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001615
Andy Greena1ce6be2013-01-18 11:43:21 +08001616#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001617 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001618 struct sockaddr sa;
1619 char hostname[1024] = "";
Andy Green788c4a82012-10-22 12:29:57 +01001620
1621 /* find canonical hostname */
1622
1623 hostname[(sizeof hostname) - 1] = '\0';
1624 memset(&sa, 0, sizeof(sa));
1625 sa.sa_family = AF_INET;
1626 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
1627 gethostname(hostname, (sizeof hostname) - 1);
1628
1629 n = 0;
1630
David Galeanoed3c8402013-01-10 10:45:24 +08001631 if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
Andy Green788c4a82012-10-22 12:29:57 +01001632 strcpy(sa.sa_data, hostname);
Andy Greend09d7d42013-01-31 10:05:43 +08001633 lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Green788c4a82012-10-22 12:29:57 +01001634 n = getnameinfo(&sa, sizeof(sa), hostname,
Andy Greend09d7d42013-01-31 10:05:43 +08001635 (sizeof hostname) - 1, NULL, 0, NI_NAMEREQD);
Andy Green788c4a82012-10-22 12:29:57 +01001636 }
1637
1638 if (!n) {
1639 strncpy(context->canonical_hostname, hostname,
1640 sizeof context->canonical_hostname - 1);
1641 context->canonical_hostname[
1642 sizeof context->canonical_hostname - 1] = '\0';
1643 } else
1644 strncpy(context->canonical_hostname, hostname,
1645 sizeof context->canonical_hostname - 1);
1646
Andy Greenb3a614a2013-01-19 13:08:17 +08001647 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001648 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001649#endif
Andy Greena69f0512012-05-03 12:32:38 +08001650
Andy Green9659f372011-01-27 22:01:43 +00001651 /* split the proxy ads:port if given */
1652
1653 p = getenv("http_proxy");
1654 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001655 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001656 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001657 context->http_proxy_address[
1658 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001659
Peter Hinz56885f32011-03-02 22:03:47 +00001660 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001661 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001662 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001663 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001664 }
1665 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001666 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001667
Andy Greenb3a614a2013-01-19 13:08:17 +08001668 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001669 context->http_proxy_address,
1670 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001671 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001672
Andy Greena1ce6be2013-01-18 11:43:21 +08001673#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001674 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001675
Andy Green3faa9c72010-11-08 17:03:03 +00001676#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001677 context->use_ssl = info->ssl_cert_filepath != NULL &&
1678 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001679#ifdef USE_CYASSL
1680 lwsl_notice(" Compiled with CYASSL support\n");
1681#else
1682 lwsl_notice(" Compiled with OpenSSL support\n");
1683#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001684 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001685 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001686 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001687 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001688
Andy Green90c7cbc2011-01-27 06:26:52 +00001689#else
1690 if (ssl_cert_filepath != NULL &&
1691 ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001692 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001693 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001694 }
Andy Greenb3a614a2013-01-19 13:08:17 +08001695 lwsl_notice(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001696 "serving unencrypted\n");
1697#endif
Andy Greena17c6922013-01-20 20:21:54 +08001698
Andy Green54495112013-02-06 21:10:16 +09001699 lwsl_notice(" per-connection allocation: %u + headers during handshake + frame buffer set by protocol\n", sizeof(struct libwebsocket));
Andy Green90c7cbc2011-01-27 06:26:52 +00001700 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001701#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001702
1703 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001704#ifdef WIN32
1705#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001706 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001707#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001708
1709
1710#ifdef LWS_OPENSSL_SUPPORT
1711
1712 /* basic openssl init */
1713
1714 SSL_library_init();
1715
1716 OpenSSL_add_all_algorithms();
1717 SSL_load_error_strings();
1718
Andy Green2e24da02011-03-05 16:12:04 +00001719 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001720 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1721
Andy Green90c7cbc2011-01-27 06:26:52 +00001722 /*
1723 * Firefox insists on SSLv23 not SSLv3
1724 * Konq disables SSLv2 by default now, SSLv23 works
1725 */
1726
1727 method = (SSL_METHOD *)SSLv23_server_method();
1728 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001729 lwsl_err("problem creating ssl method: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001730 ERR_error_string(ERR_get_error(), ssl_err_buf));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001731 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001732 }
Peter Hinz56885f32011-03-02 22:03:47 +00001733 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1734 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001735 lwsl_err("problem creating ssl context: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001736 ERR_error_string(ERR_get_error(), ssl_err_buf));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001737 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001738 }
1739
David Galeanocc148e42013-01-10 10:18:59 +08001740#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001741 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001742#endif
David Galeano77a677c2013-01-10 10:14:12 +08001743 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001744 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001745
Andy Greena1ce6be2013-01-18 11:43:21 +08001746#ifndef LWS_NO_CLIENT
1747
Andy Green90c7cbc2011-01-27 06:26:52 +00001748 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001749
Andy Green1b265272013-02-09 14:01:09 +08001750 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001751 method = (SSL_METHOD *)SSLv23_client_method();
1752 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001753 lwsl_err("problem creating ssl method: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001754 ERR_error_string(ERR_get_error(), ssl_err_buf));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001755 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001756 }
1757 /* create context */
1758 context->ssl_client_ctx = SSL_CTX_new(method);
1759 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001760 lwsl_err("problem creating ssl context: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001761 ERR_error_string(ERR_get_error(), ssl_err_buf));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001762 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001763 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001764
David Galeanocc148e42013-01-10 10:18:59 +08001765#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001766 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001767#endif
David Galeano77a677c2013-01-10 10:14:12 +08001768 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001769 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001770
Peter Hinz56885f32011-03-02 22:03:47 +00001771 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001772 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001773 if (!SSL_CTX_load_verify_locations(
1774 context->ssl_client_ctx, NULL,
1775 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001776 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001777 "Unable to load SSL Client certs from %s "
1778 "(set by --with-client-cert-dir= in configure) -- "
1779 " client ssl isn't going to work",
1780 LWS_OPENSSL_CLIENT_CERTS);
1781 } else
1782 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001783 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001784 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001785 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001786 "Unable to load SSL Client certs "
1787 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001788 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001789
1790 /*
1791 * callback allowing user code to load extra verification certs
1792 * helping the client to verify server identity
1793 */
1794
1795 context->protocols[0].callback(context, NULL,
1796 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1797 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001798 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001799#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001800
Andy Greenc6bf2c22011-02-20 11:10:47 +00001801 /* as a server, are we requiring clients to identify themselves? */
1802
Andy Green1b265272013-02-09 14:01:09 +08001803 if (info->options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001804
1805 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001806
Peter Hinz56885f32011-03-02 22:03:47 +00001807 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001808 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1809 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001810
1811 /*
1812 * give user code a chance to load certs into the server
1813 * allowing it to verify incoming client certs
1814 */
1815
Peter Hinz56885f32011-03-02 22:03:47 +00001816 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001817 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001818 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001819 }
1820
Peter Hinz56885f32011-03-02 22:03:47 +00001821 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001822
1823 /* openssl init for server sockets */
1824
Andy Green3faa9c72010-11-08 17:03:03 +00001825 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001826 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001827 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001828 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001829 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001830 info->ssl_cert_filepath,
Andy Green3faa9c72010-11-08 17:03:03 +00001831 ERR_error_string(ERR_get_error(), ssl_err_buf));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001832 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001833 }
1834 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001835 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001836 info->ssl_private_key_filepath,
1837 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001838 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001839 info->ssl_private_key_filepath,
Andy Green018d8eb2010-11-08 21:04:23 +00001840 ERR_error_string(ERR_get_error(), ssl_err_buf));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001841 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001842 }
1843 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001844 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001845 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001846 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001847 }
1848
1849 /* SSL is happy and has a cert it's content with */
1850 }
1851#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001852
Andy Greendf736162011-01-18 15:39:02 +00001853 /* selftest */
1854
1855 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001856 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00001857
Andy Greena1ce6be2013-01-18 11:43:21 +08001858#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001859 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001860
Andy Green1b265272013-02-09 14:01:09 +08001861 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001862 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1863 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001864
Andy Green4739e5c2011-01-22 12:51:57 +00001865 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1866 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001867 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001868 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001869 }
Andy Green775c0dd2010-10-29 14:15:22 +01001870
Joakim Soderberg4c531232013-02-06 15:26:58 +09001871#ifndef WIN32
1872 /* allow us to restart even if old sockets in TIME_WAIT
1873 * (REUSEADDR on Unix means, "don't hang on to this address after the
1874 * listener is closed." On Windows, though, it means "don't keep other
1875 * processes from binding to this address while we're using it) */
Andy Green6ee372f2012-04-09 15:09:01 +08001876 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1877 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09001878#endif
Andy Green6c939552011-03-08 08:56:57 +00001879
1880 /* Disable Nagle */
1881 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001882 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1883 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001884
Joakim Soderberg4c531232013-02-06 15:26:58 +09001885 #ifdef WIN32
1886 opt = 0;
1887 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt );
1888 #else
Andy Greene2160712013-01-28 12:19:10 +08001889 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09001890 #endif
Andy Greene2160712013-01-28 12:19:10 +08001891
Andy Green4739e5c2011-01-22 12:51:57 +00001892 bzero((char *) &serv_addr, sizeof(serv_addr));
1893 serv_addr.sin_family = AF_INET;
Andy Green1b265272013-02-09 14:01:09 +08001894 if (info->interface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001895 serv_addr.sin_addr.s_addr = INADDR_ANY;
1896 else
Andy Green1b265272013-02-09 14:01:09 +08001897 interface_to_sa(info->interface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001898 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08001899 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00001900
1901 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1902 sizeof(serv_addr));
1903 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001904 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08001905 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001906 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001907 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001908 }
Andy Green0d338332011-02-12 11:57:43 +00001909
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001910 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001911 if (wsi == NULL) {
1912 lwsl_err("Out of mem\n");
1913 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001914 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08001915 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001916 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001917 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08001918#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001919 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001920#endif
Andy Green0d338332011-02-12 11:57:43 +00001921 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08001922
1923 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001924
Andy Green65b0e912013-01-16 07:59:47 +08001925 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
1926 context->listen_service_count = 0;
1927 context->listen_service_fd = sockfd;
1928
Andy Greena824d182013-01-15 20:52:29 +08001929 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08001930 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00001931 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001932#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001933
Andy Green6ee372f2012-04-09 15:09:01 +08001934 /*
1935 * drop any root privs for this process
1936 * to listen on port < 1023 we would have needed root, but now we are
1937 * listening, we don't want the power for anything else
1938 */
Peter Hinz56885f32011-03-02 22:03:47 +00001939#ifdef WIN32
1940#else
Andy Green1b265272013-02-09 14:01:09 +08001941 if (info->gid != -1)
1942 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08001943 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08001944 if (info->uid != -1)
1945 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08001946 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00001947#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001948
Andy Green6f520a52013-01-29 17:57:39 +08001949 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00001950
Peter Hinz56885f32011-03-02 22:03:47 +00001951 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08001952 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00001953 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01001954
Andy Green43db0452013-01-10 19:50:35 +08001955 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001956 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01001957
Andy Green1b265272013-02-09 14:01:09 +08001958 info->protocols[context->count_protocols].owning_server =
1959 context;
1960 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00001961 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00001962 }
Andy Greenf5bc1302013-01-21 09:09:52 +08001963
Andy Green3182ece2013-01-20 17:08:31 +08001964#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001965 /*
1966 * give all extensions a chance to create any per-context
1967 * allocations they need
1968 */
1969
1970 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08001971 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01001972 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08001973
Andy Green1b265272013-02-09 14:01:09 +08001974 if (info->extensions) {
1975 ext = info->extensions;
1976 while (ext->callback) {
1977 lwsl_ext(" Extension: %s\n", ext->name);
1978 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001979 (enum libwebsocket_extension_callback_reasons)m,
1980 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08001981 ext++;
1982 }
Andy Greena41314f2011-05-23 10:00:03 +01001983 }
Andy Green3182ece2013-01-20 17:08:31 +08001984#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001985 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001986
1987bail:
1988 libwebsocket_context_destroy(context);
1989 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00001990}
Andy Greenb45993c2010-12-18 15:13:50 +00001991
Andy Greenb45993c2010-12-18 15:13:50 +00001992/**
1993 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00001994 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00001995 * @wsi: pointer to struct websocket you want to know the protocol of
1996 *
Andy Green8f037e42010-12-19 22:13:26 +00001997 *
Andy Green6f520a52013-01-29 17:57:39 +08001998 * Some apis can act on all live connections of a given protocol,
1999 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002000 */
Andy Greenab990e42010-10-31 12:42:52 +00002001
Andy Greenb45993c2010-12-18 15:13:50 +00002002const struct libwebsocket_protocols *
2003libwebsockets_get_protocol(struct libwebsocket *wsi)
2004{
2005 return wsi->protocol;
2006}
2007
Andy Green82c3d542011-03-07 21:16:31 +00002008int
2009libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2010{
Andy Green623a98d2013-01-21 11:04:23 +08002011 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002012}
Alex Bligh49146db2011-11-07 17:19:25 +08002013
David Galeanoe2cf9922013-01-09 18:06:55 +08002014unsigned char
2015libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2016{
Andy Green623a98d2013-01-21 11:04:23 +08002017 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002018}
2019
Alex Bligh49146db2011-11-07 17:19:25 +08002020void *
2021libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2022{
2023 /* allocate the per-connection user memory (if any) */
2024
2025 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2026 wsi->user_space = malloc(
2027 wsi->protocol->per_session_data_size);
2028 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002029 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002030 return NULL;
2031 }
Andy Green6ee372f2012-04-09 15:09:01 +08002032 memset(wsi->user_space, 0,
2033 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002034 }
2035 return wsi->user_space;
2036}
Andy Green43db0452013-01-10 19:50:35 +08002037
Andy Green0b319092013-01-19 11:17:56 +08002038static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002039{
Andy Green0b319092013-01-19 11:17:56 +08002040 char buf[300];
2041 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002042 int n;
2043
2044 gettimeofday(&tv, NULL);
2045
2046 buf[0] = '\0';
2047 for (n = 0; n < LLL_COUNT; n++)
2048 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002049 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Green0b319092013-01-19 11:17:56 +08002050 (int)(tv.tv_usec / 100), log_level_names[n]);
2051 break;
2052 }
2053
2054 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002055}
2056
Joakim Soderberg4c531232013-02-06 15:26:58 +09002057#ifdef WIN32
2058void lwsl_emit_syslog(int level, const char *line)
2059{
2060 lwsl_emit_stderr(level, line);
2061}
2062#else
Andy Greenc11db202013-01-19 11:12:16 +08002063void lwsl_emit_syslog(int level, const char *line)
2064{
2065 int syslog_level = LOG_DEBUG;
2066
2067 switch (level) {
2068 case LLL_ERR:
2069 syslog_level = LOG_ERR;
2070 break;
2071 case LLL_WARN:
2072 syslog_level = LOG_WARNING;
2073 break;
2074 case LLL_NOTICE:
2075 syslog_level = LOG_NOTICE;
2076 break;
2077 case LLL_INFO:
2078 syslog_level = LOG_INFO;
2079 break;
2080 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002081 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002082}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002083#endif
Andy Greenc11db202013-01-19 11:12:16 +08002084
Andy Green43db0452013-01-10 19:50:35 +08002085void _lws_log(int filter, const char *format, ...)
2086{
Andy Greende8f27a2013-01-12 09:17:42 +08002087 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002088 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002089
2090 if (!(log_level & filter))
2091 return;
2092
Andy Green43db0452013-01-10 19:50:35 +08002093 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08002094 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08002095 buf[(sizeof buf) - 1] = '\0';
2096 va_end(ap);
2097
Andy Green0b319092013-01-19 11:17:56 +08002098 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002099}
2100
2101/**
2102 * lws_set_log_level() - Set the logging bitfield
2103 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002104 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2105 * function to perform log string emission instead of
2106 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002107 *
Andy Greende8f27a2013-01-12 09:17:42 +08002108 * log level defaults to "err" and "warn" contexts enabled only and
2109 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002110 */
2111
Andy Green058ba812013-01-19 11:32:18 +08002112void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002113{
2114 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002115 if (log_emit_function)
2116 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002117}