blob: e5d148addebc3e1ffd2e84f5218b271c86b3d97c [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 Greena690cd02013-02-09 12:25:31 +0800522int lws_set_socket_options(struct libwebsocket_context *context, int fd)
523{
524 int optval = 1;
525 socklen_t optlen = sizeof(optval);
526#ifdef WIN32
527 unsigned long optl = 0;
528#endif
529#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
530 struct protoent *tcp_proto;
531#endif
532
533 if (context->ka_time) {
534 /* enable keepalive on this socket */
535 optval = 1;
536 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
537 (const void *)&optval, optlen) < 0)
538 return 1;
539
Andy Greena47865f2013-02-10 09:39:47 +0800540#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
541
542 /*
543 * didn't find a way to set these per-socket, need to
544 * tune kernel systemwide values
545 */
546
547#else
Andy Greena690cd02013-02-09 12:25:31 +0800548 /* set the keepalive conditions we want on it too */
549 optval = context->ka_time;
550 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
551 (const void *)&optval, optlen) < 0)
552 return 1;
553
554 optval = context->ka_probes;
555 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
556 (const void *)&optval, optlen) < 0)
557 return 1;
558
559 optval = context->ka_interval;
560 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
561 (const void *)&optval, optlen) < 0)
562 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800563#endif
Andy Greena690cd02013-02-09 12:25:31 +0800564 }
565
566 /* Disable Nagle */
567 optval = 1;
568#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
569 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
570#else
571 tcp_proto = getprotobyname("TCP");
572 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
573#endif
574
575 /* We are nonblocking... */
576#ifdef WIN32
577 ioctlsocket(fd, FIONBIO, &optl);
578#else
579 fcntl(fd, F_SETFL, O_NONBLOCK);
580#endif
581
582 return 0;
583}
584
Andy Green95a7b5d2011-03-06 10:29:39 +0000585int lws_send_pipe_choked(struct libwebsocket *wsi)
586{
587 struct pollfd fds;
588
589 fds.fd = wsi->sock;
590 fds.events = POLLOUT;
591 fds.revents = 0;
592
593 if (poll(&fds, 1, 0) != 1)
594 return 1;
595
596 if ((fds.revents & POLLOUT) == 0)
597 return 1;
598
599 /* okay to send another packet without blocking */
600
601 return 0;
602}
603
Andy Greena41314f2011-05-23 10:00:03 +0100604int
Andy Green3b84c002011-03-06 13:14:42 +0000605lws_handle_POLLOUT_event(struct libwebsocket_context *context,
606 struct libwebsocket *wsi, struct pollfd *pollfd)
607{
Andy Green3b84c002011-03-06 13:14:42 +0000608 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800609
Andy Green3182ece2013-01-20 17:08:31 +0800610#ifndef LWS_NO_EXTENSIONS
611 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000612 int ret;
613 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100614 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000615
Andy Greena41314f2011-05-23 10:00:03 +0100616 for (n = 0; n < wsi->count_active_extensions; n++) {
617 if (!wsi->active_extensions[n]->callback)
618 continue;
619
620 m = wsi->active_extensions[n]->callback(context,
621 wsi->active_extensions[n], wsi,
622 LWS_EXT_CALLBACK_IS_WRITEABLE,
623 wsi->active_extensions_user[n], NULL, 0);
624 if (m > handled)
625 handled = m;
626 }
627
628 if (handled == 1)
629 goto notify_action;
630
631 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000632 goto user_service;
633
634 /*
635 * check in on the active extensions, see if they
636 * had pending stuff to spill... they need to get the
637 * first look-in otherwise sequence will be disordered
638 *
639 * NULL, zero-length eff_buf means just spill pending
640 */
641
642 ret = 1;
643 while (ret == 1) {
644
645 /* default to nobody has more to spill */
646
647 ret = 0;
648 eff_buf.token = NULL;
649 eff_buf.token_len = 0;
650
651 /* give every extension a chance to spill */
652
653 for (n = 0; n < wsi->count_active_extensions; n++) {
654 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000655 wsi->protocol->owning_server,
656 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000657 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
658 wsi->active_extensions_user[n], &eff_buf, 0);
659 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800660 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000661 return -1;
662 }
663 if (m)
664 /*
665 * at least one extension told us he has more
666 * to spill, so we will go around again after
667 */
668 ret = 1;
669 }
670
671 /* assuming they gave us something to send, send it */
672
673 if (eff_buf.token_len) {
674 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
675 eff_buf.token_len))
676 return -1;
677 } else
678 continue;
679
680 /* no extension has more to spill */
681
682 if (!ret)
683 continue;
684
685 /*
686 * There's more to spill from an extension, but we just sent
687 * something... did that leave the pipe choked?
688 */
689
690 if (!lws_send_pipe_choked(wsi))
691 /* no we could add more */
692 continue;
693
Andy Green43db0452013-01-10 19:50:35 +0800694 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000695
696 /*
697 * Yes, he's choked. Leave the POLLOUT masked on so we will
698 * come back here when he is unchoked. Don't call the user
699 * callback to enforce ordering of spilling, he'll get called
700 * when we come back here and there's nothing more to spill.
701 */
702
703 return 0;
704 }
705
706 wsi->extension_data_pending = 0;
707
708user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800709#endif
Andy Green3b84c002011-03-06 13:14:42 +0000710 /* one shot */
711
Andy Greena41314f2011-05-23 10:00:03 +0100712 if (pollfd) {
713 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000714
Andy Greena41314f2011-05-23 10:00:03 +0100715 /* external POLL support via protocol 0 */
716 context->protocols[0].callback(context, wsi,
717 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
718 (void *)(long)wsi->sock, NULL, POLLOUT);
719 }
Andy Green3182ece2013-01-20 17:08:31 +0800720#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100721notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800722#endif
Andy Green3b84c002011-03-06 13:14:42 +0000723
Andy Green9e4c2b62011-03-07 20:47:39 +0000724 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
725 n = LWS_CALLBACK_CLIENT_WRITEABLE;
726 else
727 n = LWS_CALLBACK_SERVER_WRITEABLE;
728
Andy Greenb8b247d2013-01-22 07:20:08 +0800729 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Green706961d2013-01-17 16:50:35 +0800730 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000731}
732
733
734
Andy Greena41314f2011-05-23 10:00:03 +0100735void
736libwebsocket_service_timeout_check(struct libwebsocket_context *context,
737 struct libwebsocket *wsi, unsigned int sec)
738{
Andy Green3182ece2013-01-20 17:08:31 +0800739#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100740 int n;
741
742 /*
743 * if extensions want in on it (eg, we are a mux parent)
744 * give them a chance to service child timeouts
745 */
746
747 for (n = 0; n < wsi->count_active_extensions; n++)
748 wsi->active_extensions[n]->callback(
749 context, wsi->active_extensions[n],
750 wsi, LWS_EXT_CALLBACK_1HZ,
751 wsi->active_extensions_user[n], NULL, sec);
752
Andy Green3182ece2013-01-20 17:08:31 +0800753#endif
Andy Greena41314f2011-05-23 10:00:03 +0100754 if (!wsi->pending_timeout)
755 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800756
Andy Greena41314f2011-05-23 10:00:03 +0100757 /*
758 * if we went beyond the allowed time, kill the
759 * connection
760 */
761
762 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800763 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100764 libwebsocket_close_and_free_session(context,
765 wsi, LWS_CLOSE_STATUS_NOSTATUS);
766 }
767}
768
Andy Green9f990342011-02-12 11:57:45 +0000769/**
770 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000771 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000772 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800773 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000774 *
Andy Green75006172013-01-22 12:32:11 +0800775 * This function takes a pollfd that has POLLIN or POLLOUT activity and
776 * services it according to the state of the associated struct libwebsocket.
777 *
778 * The one call deals with all "service" that might happen on a socket
779 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000780 */
781
782int
Peter Hinz56885f32011-03-02 22:03:47 +0000783libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000784 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000785{
Andy Greena1ce6be2013-01-18 11:43:21 +0800786 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000787 int n;
Andy Green0d338332011-02-12 11:57:43 +0000788 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800789 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000790 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800791
Andy Green3182ece2013-01-20 17:08:31 +0800792#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000793 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800794#endif
Andy Green98a717c2011-03-06 13:14:15 +0000795 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800796#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800797 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800798#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800799#ifndef LWS_NO_SERVER
800 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
801#endif
Andy Greenf0b79e22013-02-10 10:46:45 +0800802
803 if (context->listen_service_fd)
804 listen_socket_fds_index = context->lws_lookup[
805 context->listen_service_fd]->position_in_fds_table;
806
Andy Greena71eafc2011-02-14 17:59:43 +0000807 /*
808 * you can call us with pollfd = NULL to just allow the once-per-second
809 * global timeout checks; if less than a second since the last check
810 * it returns immediately then.
811 */
812
813 gettimeofday(&tv, NULL);
814
Peter Hinz56885f32011-03-02 22:03:47 +0000815 if (context->last_timeout_check_s != tv.tv_sec) {
816 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000817
Joakim Soderberg4c531232013-02-06 15:26:58 +0900818 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800819 /* if our parent went down, don't linger around */
820 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
821 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900822 #endif
Andy Green24cba922013-01-19 13:56:10 +0800823
Andy Greena71eafc2011-02-14 17:59:43 +0000824 /* global timeout check once per second */
825
Peter Hinz56885f32011-03-02 22:03:47 +0000826 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800827 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
828 if (!new_wsi)
829 continue;
830 libwebsocket_service_timeout_check(context,
831 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000832 }
833 }
834
835 /* just here for timeout management? */
836
837 if (pollfd == NULL)
838 return 0;
839
840 /* no, here to service a socket descriptor */
841
Andy Green65b0e912013-01-16 07:59:47 +0800842 /*
843 * deal with listen service piggybacking
844 * every listen_service_modulo services of other fds, we
845 * sneak one in to service the listen socket if there's anything waiting
846 *
847 * To handle connection storms, as found in ab, if we previously saw a
848 * pending connection here, it causes us to check again next time.
849 */
850
Andy Green9e4c9172013-02-10 09:06:38 +0800851 if (context->listen_service_fd && pollfd != &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800852 context->listen_service_count++;
853 if (context->listen_service_extraseen ||
854 context->listen_service_count == context->listen_service_modulo) {
855 context->listen_service_count = 0;
856 m = 1;
857 if (context->listen_service_extraseen > 5)
858 m = 2;
859 while (m--) {
860 /* even with extpoll, we prepared this internal fds for listen */
Andy Green9e4c9172013-02-10 09:06:38 +0800861 n = poll(&context->fds[listen_socket_fds_index], 1, 0);
Andy Green65b0e912013-01-16 07:59:47 +0800862 if (n > 0) { /* there's a connection waiting for us */
Andy Green9e4c9172013-02-10 09:06:38 +0800863 libwebsocket_service_fd(context, &context->fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800864 context->listen_service_extraseen++;
865 } else {
866 if (context->listen_service_extraseen)
867 context->listen_service_extraseen--;
868 break;
869 }
870 }
871 }
872
873 }
874
875 /* okay, what we came here to do... */
876
Andy Greendfb23042013-01-17 12:26:48 +0800877 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800878 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800879 if (pollfd->fd > 11)
880 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800881 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800882 }
Andy Green8f037e42010-12-19 22:13:26 +0000883
Andy Green0d338332011-02-12 11:57:43 +0000884 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800885
Andy Greena1ce6be2013-01-18 11:43:21 +0800886#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800887 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000888 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800889 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800890 return lws_server_socket_service(context, wsi, pollfd);
891#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000892
Andy Green0d338332011-02-12 11:57:43 +0000893 case LWS_CONNMODE_WS_SERVING:
894 case LWS_CONNMODE_WS_CLIENT:
895
896 /* handle session socket closed */
897
898 if (pollfd->revents & (POLLERR | POLLHUP)) {
899
Andy Green43db0452013-01-10 19:50:35 +0800900 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000901 (void *)wsi, pollfd->fd);
902
Peter Hinz56885f32011-03-02 22:03:47 +0000903 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000904 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800905 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000906 }
907
Andy Green0d338332011-02-12 11:57:43 +0000908 /* the guy requested a callback when it was OK to write */
909
Andy Greenda527df2011-03-07 07:08:12 +0000910 if ((pollfd->revents & POLLOUT) &&
911 wsi->state == WSI_STATE_ESTABLISHED)
912 if (lws_handle_POLLOUT_event(context, wsi,
913 pollfd) < 0) {
Andy Green16ab3182013-02-10 18:02:31 +0800914 lwsl_info("libwebsocket_service_fd: closing");
Andy Greenda527df2011-03-07 07:08:12 +0000915 libwebsocket_close_and_free_session(
916 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800917 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000918 }
Andy Green0d338332011-02-12 11:57:43 +0000919
Andy Green0d338332011-02-12 11:57:43 +0000920
921 /* any incoming data ready? */
922
923 if (!(pollfd->revents & POLLIN))
924 break;
925
Andy Greenb45993c2010-12-18 15:13:50 +0000926#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800927read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800928 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800929 eff_buf.token_len = SSL_read(wsi->ssl,
930 context->service_buffer,
931 sizeof context->service_buffer);
Andy Green467c7ef2013-01-30 12:28:34 +0800932 if (!eff_buf.token_len) {
933 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800934 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +0800935 ERR_error_string(n, (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800936 }
937 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000938#endif
Andy Greene84652c2013-02-08 13:01:02 +0800939 eff_buf.token_len = recv(pollfd->fd,
940 context->service_buffer,
941 sizeof context->service_buffer, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000942
Andy Green98a717c2011-03-06 13:14:15 +0000943 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800944 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000945 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200946 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800947 libwebsocket_close_and_free_session(context,
948 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800949 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000950 }
Andy Green98a717c2011-03-06 13:14:15 +0000951 if (!eff_buf.token_len) {
Andy Green467c7ef2013-01-30 12:28:34 +0800952 lwsl_info("closing connection due to zero length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000953 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800954 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800955 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000956 }
957
Andy Green98a717c2011-03-06 13:14:15 +0000958 /*
959 * give any active extensions a chance to munge the buffer
960 * before parse. We pass in a pointer to an lws_tokens struct
961 * prepared with the default buffer and content length that's in
962 * there. Rather than rewrite the default buffer, extensions
963 * that expect to grow the buffer can adapt .token to
964 * point to their own per-connection buffer in the extension
965 * user allocation. By default with no extensions or no
966 * extension callback handling, just the normal input buffer is
967 * used then so it is efficient.
968 */
Andy Greenb45993c2010-12-18 15:13:50 +0000969
Andy Greene84652c2013-02-08 13:01:02 +0800970 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +0800971#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000972 more = 1;
973 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000974
Andy Green98a717c2011-03-06 13:14:15 +0000975 more = 0;
976
977 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000978 m = wsi->active_extensions[n]->callback(context,
979 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000980 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000981 wsi->active_extensions_user[n],
982 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000983 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800984 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800985 "Extension reports fatal error\n");
986 libwebsocket_close_and_free_session(
987 context, wsi,
988 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800989 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000990 }
991 if (m)
992 more = 1;
993 }
Andy Green3182ece2013-01-20 17:08:31 +0800994#endif
Andy Green98a717c2011-03-06 13:14:15 +0000995 /* service incoming data */
996
997 if (eff_buf.token_len) {
998 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800999 (unsigned char *)eff_buf.token,
1000 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001001 if (n < 0)
1002 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001003 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001004 }
Andy Green3182ece2013-01-20 17:08:31 +08001005#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001006 eff_buf.token = NULL;
1007 eff_buf.token_len = 0;
1008 }
Andy Green3182ece2013-01-20 17:08:31 +08001009#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001010
1011#ifdef LWS_OPENSSL_SUPPORT
1012 if (wsi->ssl && SSL_pending(wsi->ssl))
1013 goto read_pending;
1014#endif
Andy Green98a717c2011-03-06 13:14:15 +00001015 break;
Andy Green76f61e72013-01-16 11:53:05 +08001016
1017 default:
Andy Green03674a62013-01-16 11:47:40 +08001018#ifdef LWS_NO_CLIENT
1019 break;
1020#else
Andy Green76f61e72013-01-16 11:53:05 +08001021 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001022#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001023 }
1024
1025 return 0;
1026}
1027
Andy Green0d338332011-02-12 11:57:43 +00001028
Andy Green6964bb52011-01-23 16:50:33 +00001029/**
1030 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001031 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001032 *
1033 * This function closes any active connections and then frees the
1034 * context. After calling this, any further use of the context is
1035 * undefined.
1036 */
1037void
Peter Hinz56885f32011-03-02 22:03:47 +00001038libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001039{
Andy Green3182ece2013-01-20 17:08:31 +08001040#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001041 int n;
1042 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001043 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +00001044
Andy Greend636e352013-01-29 12:36:17 +08001045#ifdef LWS_LATENCY
1046 if (context->worst_latency_info[0])
1047 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1048#endif
1049
Andy Greendfb23042013-01-17 12:26:48 +08001050 for (n = 0; n < context->fds_count; n++) {
1051 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
1052 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001053 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1054 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001055 }
Andy Green6964bb52011-01-23 16:50:33 +00001056
Andy Greena41314f2011-05-23 10:00:03 +01001057 /*
1058 * give all extensions a chance to clean up any per-context
1059 * allocations they might have made
1060 */
1061
1062 ext = context->extensions;
1063 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1064 if (context->listen_port)
1065 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001066 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001067 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001068 ext++;
1069 }
Andy Green3182ece2013-01-20 17:08:31 +08001070#endif
Andy Greena41314f2011-05-23 10:00:03 +01001071
Peter Hinz56885f32011-03-02 22:03:47 +00001072#ifdef WIN32
1073#else
1074 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001075#endif
1076
Peter Hinz56885f32011-03-02 22:03:47 +00001077#ifdef LWS_OPENSSL_SUPPORT
1078 if (context->ssl_ctx)
1079 SSL_CTX_free(context->ssl_ctx);
1080 if (context->ssl_client_ctx)
1081 SSL_CTX_free(context->ssl_client_ctx);
1082#endif
1083
Andy Green46596482013-02-11 11:04:01 +08001084 if (context->fds)
1085 free(context->fds);
1086 if (context->lws_lookup)
1087 free(context->lws_lookup);
1088
Peter Hinz56885f32011-03-02 22:03:47 +00001089 free(context);
1090
1091#ifdef WIN32
1092 WSACleanup();
1093#endif
Andy Green6964bb52011-01-23 16:50:33 +00001094}
1095
Andy Greend88146d2013-01-22 12:40:35 +08001096/**
1097 * libwebsocket_context_user() - get the user data associated with the whole context
1098 * @context: Websocket context
1099 *
1100 * This returns the optional user allocation that can be attached to
1101 * the context the sockets live in at context_create time. It's a way
1102 * to let all sockets serviced in the same context share data without
1103 * using globals statics in the user code.
1104 */
1105
1106
Alon Levy0291eb32012-10-19 11:21:56 +02001107LWS_EXTERN void *
1108libwebsocket_context_user(struct libwebsocket_context *context)
1109{
1110 return context->user_space;
1111}
1112
Andy Green6964bb52011-01-23 16:50:33 +00001113/**
1114 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001115 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001116 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1117 * service otherwise block and service immediately, returning
1118 * after the timeout if nothing needed service.
1119 *
1120 * This function deals with any pending websocket traffic, for three
1121 * kinds of event. It handles these events on both server and client
1122 * types of connection the same.
1123 *
1124 * 1) Accept new connections to our context's server
1125 *
Andy Green6f520a52013-01-29 17:57:39 +08001126 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001127 * server or client connections.
1128 *
1129 * You need to call this service function periodically to all the above
1130 * functions to happen; if your application is single-threaded you can
1131 * just call it in your main event loop.
1132 *
1133 * Alternatively you can fork a new process that asynchronously handles
1134 * calling this service in a loop. In that case you are happy if this
1135 * call blocks your thread until it needs to take care of something and
1136 * would call it with a large nonzero timeout. Your loop then takes no
1137 * CPU while there is nothing happening.
1138 *
1139 * If you are calling it in a single-threaded app, you don't want it to
1140 * wait around blocking other things in your loop from happening, so you
1141 * would call it with a timeout_ms of 0, so it returns immediately if
1142 * nothing is pending, or as soon as it services whatever was pending.
1143 */
1144
Andy Greenb45993c2010-12-18 15:13:50 +00001145
Andy Greene92cd172011-01-19 13:11:55 +00001146int
Peter Hinz56885f32011-03-02 22:03:47 +00001147libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001148{
1149 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001150
1151 /* stay dead once we are dead */
1152
Peter Hinz56885f32011-03-02 22:03:47 +00001153 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001154 return 1;
1155
Andy Green0d338332011-02-12 11:57:43 +00001156 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001157
Peter Hinz56885f32011-03-02 22:03:47 +00001158 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001159 if (n == 0) /* poll timeout */
1160 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001161
Andy Greendfb23042013-01-17 12:26:48 +08001162 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001163 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001164
Andy Greendfb23042013-01-17 12:26:48 +08001165 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001166
Peter Hinz56885f32011-03-02 22:03:47 +00001167 for (n = 0; n < context->fds_count; n++)
1168 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001169 if (libwebsocket_service_fd(context,
1170 &context->fds[n]) < 0)
1171 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001172 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001173}
1174
Andy Green3182ece2013-01-20 17:08:31 +08001175#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001176int
1177lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001178 struct libwebsocket *wsi,
1179 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001180 void *v, size_t len)
1181{
1182 int n;
1183 int handled = 0;
1184
1185 /* maybe an extension will take care of it for us */
1186
1187 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1188 if (!wsi->active_extensions[n]->callback)
1189 continue;
1190
1191 handled |= wsi->active_extensions[n]->callback(context,
1192 wsi->active_extensions[n], wsi,
1193 r, wsi->active_extensions_user[n], v, len);
1194 }
1195
1196 return handled;
1197}
1198
1199
1200void *
1201lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001202 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001203{
1204 int n = 0;
1205
Andy Green68b45042011-05-25 21:41:57 +01001206 if (wsi == NULL)
1207 return NULL;
1208
Andy Greena41314f2011-05-23 10:00:03 +01001209 while (n < wsi->count_active_extensions) {
1210 if (wsi->active_extensions[n] != ext) {
1211 n++;
1212 continue;
1213 }
1214 return wsi->active_extensions_user[n];
1215 }
1216
1217 return NULL;
1218}
Andy Green3182ece2013-01-20 17:08:31 +08001219#endif
Andy Greena41314f2011-05-23 10:00:03 +01001220
Andy Green90c7cbc2011-01-27 06:26:52 +00001221/**
1222 * libwebsocket_callback_on_writable() - Request a callback when this socket
1223 * becomes able to be written to without
1224 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001225 *
Peter Hinz56885f32011-03-02 22:03:47 +00001226 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001227 * @wsi: Websocket connection instance to get callback for
1228 */
1229
1230int
Peter Hinz56885f32011-03-02 22:03:47 +00001231libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001232 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001233{
Andy Green3182ece2013-01-20 17:08:31 +08001234#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001235 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001236 int handled = 0;
1237
1238 /* maybe an extension will take care of it for us */
1239
1240 for (n = 0; n < wsi->count_active_extensions; n++) {
1241 if (!wsi->active_extensions[n]->callback)
1242 continue;
1243
1244 handled |= wsi->active_extensions[n]->callback(context,
1245 wsi->active_extensions[n], wsi,
1246 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1247 wsi->active_extensions_user[n], NULL, 0);
1248 }
1249
1250 if (handled)
1251 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001252#endif
Andy Greendfb23042013-01-17 12:26:48 +08001253 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001254 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001255 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001256 return -1;
1257 }
1258
1259 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001260
Andy Green3221f922011-02-12 13:14:11 +00001261 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001262 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001263 LWS_CALLBACK_SET_MODE_POLL_FD,
1264 (void *)(long)wsi->sock, NULL, POLLOUT);
1265
Andy Green90c7cbc2011-01-27 06:26:52 +00001266 return 1;
1267}
1268
1269/**
1270 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1271 * all connections using the given protocol when it
1272 * becomes possible to write to each socket without
1273 * blocking in turn.
1274 *
1275 * @protocol: Protocol whose connections will get callbacks
1276 */
1277
1278int
1279libwebsocket_callback_on_writable_all_protocol(
1280 const struct libwebsocket_protocols *protocol)
1281{
Peter Hinz56885f32011-03-02 22:03:47 +00001282 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001283 int n;
Andy Green0d338332011-02-12 11:57:43 +00001284 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001285
Andy Greendfb23042013-01-17 12:26:48 +08001286 for (n = 0; n < context->fds_count; n++) {
1287 wsi = context->lws_lookup[context->fds[n].fd];
1288 if (!wsi)
1289 continue;
1290 if (wsi->protocol == protocol)
1291 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001292 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001293
1294 return 0;
1295}
1296
Andy Greenbe93fef2011-02-14 20:25:43 +00001297/**
1298 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1299 *
1300 * You will not need this unless you are doing something special
1301 *
1302 * @wsi: Websocket connection instance
1303 * @reason: timeout reason
1304 * @secs: how many seconds
1305 */
1306
1307void
1308libwebsocket_set_timeout(struct libwebsocket *wsi,
1309 enum pending_timeout reason, int secs)
1310{
1311 struct timeval tv;
1312
1313 gettimeofday(&tv, NULL);
1314
1315 wsi->pending_timeout_limit = tv.tv_sec + secs;
1316 wsi->pending_timeout = reason;
1317}
1318
Andy Greena6cbece2011-01-27 20:06:03 +00001319
1320/**
1321 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1322 *
1323 * You will not need this unless you are doing something special
1324 *
1325 * @wsi: Websocket connection instance
1326 */
1327
1328int
1329libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1330{
1331 return wsi->sock;
1332}
1333
Andy Greend636e352013-01-29 12:36:17 +08001334#ifdef LWS_LATENCY
1335void
1336lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completed)
1337{
1338 struct timeval tv;
1339 unsigned long u;
1340 char buf[256];
1341
1342 gettimeofday(&tv, NULL);
1343
1344 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1345
1346 if (action) {
1347 if (completed) {
1348 if (wsi->action_start == wsi->latency_start)
1349 sprintf(buf, "Completion first try lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1350 else
1351 sprintf(buf, "Completion %luus: lat %luus: %p: ret %d: %s\n", u - wsi->action_start, u - wsi->latency_start, (void *)wsi, ret, action);
1352 wsi->action_start = 0;
1353 } else
1354 sprintf(buf, "lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1355 if (u - wsi->latency_start > context->worst_latency) {
1356 context->worst_latency = u - wsi->latency_start;
1357 strcpy(context->worst_latency_info, buf);
1358 }
1359 lwsl_latency("%s", buf);
1360 } else {
1361 wsi->latency_start = u;
1362 if (!wsi->action_start)
1363 wsi->action_start = u;
1364 }
1365}
1366#endif
1367
Andy Greena1ce6be2013-01-18 11:43:21 +08001368#ifdef LWS_NO_SERVER
1369int
1370_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1371{
1372 return 0;
1373}
1374#else
Andy Green706961d2013-01-17 16:50:35 +08001375int
1376_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1377{
1378 struct libwebsocket_context *context = wsi->protocol->owning_server;
1379 int n;
1380
Andy Green623a98d2013-01-21 11:04:23 +08001381 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001382 return 0;
1383
Andy Green623a98d2013-01-21 11:04:23 +08001384 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001385
Andy Green623a98d2013-01-21 11:04:23 +08001386 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001387
1388 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001389 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001390 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1391 if (n < 0) {
Andy Greenaedc9532013-02-10 21:21:24 +08001392 lwsl_info("returning that we want to close connection at libwebsocket_rx_flow_control:\n");
Andy Green706961d2013-01-17 16:50:35 +08001393 return -1;
1394 }
1395 if (n)
1396 /* oh he stuck again, do nothing */
1397 return 0;
1398 }
1399
Andy Green623a98d2013-01-21 11:04:23 +08001400 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001401 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1402 else
1403 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1404
Andy Green623a98d2013-01-21 11:04:23 +08001405 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001406 /* external POLL support via protocol 0 */
1407 context->protocols[0].callback(context, wsi,
1408 LWS_CALLBACK_SET_MODE_POLL_FD,
1409 (void *)(long)wsi->sock, NULL, POLLIN);
1410 else
1411 /* external POLL support via protocol 0 */
1412 context->protocols[0].callback(context, wsi,
1413 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1414 (void *)(long)wsi->sock, NULL, POLLIN);
1415
1416 return 1;
1417}
Andy Greena1ce6be2013-01-18 11:43:21 +08001418#endif
Andy Green706961d2013-01-17 16:50:35 +08001419
Andy Green90c7cbc2011-01-27 06:26:52 +00001420/**
1421 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1422 * receieved packets.
1423 *
1424 * If the output side of a server process becomes choked, this allows flow
1425 * control for the input side.
1426 *
1427 * @wsi: Websocket connection instance to get callback for
1428 * @enable: 0 = disable read servicing for this connection, 1 = enable
1429 */
1430
1431int
1432libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1433{
Andy Green623a98d2013-01-21 11:04:23 +08001434 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001435
Andy Green706961d2013-01-17 16:50:35 +08001436 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001437}
1438
Andy Green706961d2013-01-17 16:50:35 +08001439
Andy Green2ac5a6f2011-01-28 10:00:18 +00001440/**
1441 * libwebsocket_canonical_hostname() - returns this host's hostname
1442 *
1443 * This is typically used by client code to fill in the host parameter
1444 * when making a client connection. You can only call it after the context
1445 * has been created.
1446 *
Peter Hinz56885f32011-03-02 22:03:47 +00001447 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001448 */
1449
1450
1451extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001452libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001453{
Peter Hinz56885f32011-03-02 22:03:47 +00001454 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001455}
1456
1457
Andy Green90c7cbc2011-01-27 06:26:52 +00001458static void sigpipe_handler(int x)
1459{
1460}
1461
Andy Green6901cb32011-02-21 08:06:47 +00001462#ifdef LWS_OPENSSL_SUPPORT
1463static int
1464OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1465{
1466
1467 SSL *ssl;
1468 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001469 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001470
1471 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1472 SSL_get_ex_data_X509_STORE_CTX_idx());
1473
1474 /*
Andy Green2e24da02011-03-05 16:12:04 +00001475 * !!! nasty openssl requires the index to come as a library-scope
1476 * static
Andy Green6901cb32011-02-21 08:06:47 +00001477 */
Andy Green2e24da02011-03-05 16:12:04 +00001478 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001479
Peter Hinz56885f32011-03-02 22:03:47 +00001480 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001481 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1482 x509_ctx, ssl, preverify_ok);
1483
1484 /* convert return code from 0 = OK to 1 = OK */
1485
1486 if (!n)
1487 n = 1;
1488 else
1489 n = 0;
1490
1491 return n;
1492}
1493#endif
1494
Andy Green706961d2013-01-17 16:50:35 +08001495int user_callback_handle_rxflow(callback_function callback_function,
1496 struct libwebsocket_context * context,
1497 struct libwebsocket *wsi,
1498 enum libwebsocket_callback_reasons reason, void *user,
1499 void *in, size_t len)
1500{
1501 int n;
1502
1503 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001504 if (!n)
1505 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001506
Andy Greenaedc9532013-02-10 21:21:24 +08001507 return n;
Andy Green706961d2013-01-17 16:50:35 +08001508}
1509
Andy Greenb45993c2010-12-18 15:13:50 +00001510
Andy Greenab990e42010-10-31 12:42:52 +00001511/**
Andy Green4739e5c2011-01-22 12:51:57 +00001512 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001513 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001514 *
Andy Green1b265272013-02-09 14:01:09 +08001515 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001516 * of all initialization in one step.
1517 *
Andy Greene92cd172011-01-19 13:11:55 +00001518 * After initialization, it returns a struct libwebsocket_context * that
1519 * represents this server. After calling, user code needs to take care
1520 * of calling libwebsocket_service() with the context pointer to get the
1521 * server's sockets serviced. This can be done in the same process context
1522 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001523 *
Andy Green8f037e42010-12-19 22:13:26 +00001524 * The protocol callback functions are called for a handful of events
1525 * including http requests coming in, websocket connections becoming
1526 * established, and data arriving; it's also called periodically to allow
1527 * async transmission.
1528 *
1529 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1530 * at that time websocket protocol has not been negotiated. Other
1531 * protocols after the first one never see any HTTP callack activity.
1532 *
1533 * The server created is a simple http server by default; part of the
1534 * websocket standard is upgrading this http connection to a websocket one.
1535 *
1536 * This allows the same server to provide files like scripts and favicon /
1537 * images or whatever over http and dynamic data over websockets all in
1538 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001539 */
Andy Green4ea60062010-10-30 12:15:07 +01001540
Andy Greene92cd172011-01-19 13:11:55 +00001541struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001542libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001543{
1544 int n;
Peter Hinz56885f32011-03-02 22:03:47 +00001545 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001546 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001547#ifndef LWS_NO_SERVER
1548 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001549 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001550 struct sockaddr_in serv_addr;
1551#endif
Andy Green3182ece2013-01-20 17:08:31 +08001552#ifndef LWS_NO_EXTENSIONS
1553 int m;
Andy Green1b265272013-02-09 14:01:09 +08001554 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001555#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001556
Andy Green3faa9c72010-11-08 17:03:03 +00001557#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001558 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001559#endif
1560
Joakim Soderberg4c531232013-02-06 15:26:58 +09001561#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001562 extern int get_daemonize_pid();
1563 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001564#endif
1565
Andy Greenb3a614a2013-01-19 13:08:17 +08001566 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001567 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001568 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1569 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001570 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001571#ifndef LWS_NO_EXTENSIONS
Andy Greenc0d6b632013-01-12 23:42:17 +08001572 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001573#else
1574 lwsl_notice(" Configured without extension support\n");
1575#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001576 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1577 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1578 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1579 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1580 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001581
Peter Hinz56885f32011-03-02 22:03:47 +00001582#ifdef _WIN32
1583 {
1584 WORD wVersionRequested;
1585 WSADATA wsaData;
1586 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001587 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001588
1589 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1590 wVersionRequested = MAKEWORD(2, 2);
1591
1592 err = WSAStartup(wVersionRequested, &wsaData);
1593 if (err != 0) {
1594 /* Tell the user that we could not find a usable */
1595 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001596 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001597 return NULL;
1598 }
David Galeano7b11fec2011-10-04 19:55:18 +08001599
Andy Green6ee372f2012-04-09 15:09:01 +08001600 /* default to a poll() made out of select() */
1601 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001602
Andy Green6ee372f2012-04-09 15:09:01 +08001603 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001604 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001605 if (wsdll)
1606 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001607
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001608 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001609 if (!poll)
1610 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001611 }
1612#endif
1613
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001614 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001615 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001616 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001617 return NULL;
1618 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001619 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001620#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001621 context->started_with_parent = pid_daemon;
1622 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1623#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +09001624
1625 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001626 context->protocols = info->protocols;
1627 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001628 context->http_proxy_port = 0;
1629 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001630 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001631 /* to reduce this allocation, */
1632 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001633 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1634 sizeof(struct libwebsocket_context),
1635 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1636 context->max_fds,
1637 sizeof(struct libwebsocket_context) + ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) * context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001638
1639 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1640 if (context->fds == NULL) {
1641 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1642 free(context);
1643 return NULL;
1644 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08001645 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001646 if (context->lws_lookup == NULL) {
1647 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1648 free(context->fds);
1649 free(context);
1650 return NULL;
1651 }
Andy Greena17c6922013-01-20 20:21:54 +08001652
Peter Hinz56885f32011-03-02 22:03:47 +00001653 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001654#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001655 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001656#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001657 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001658 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001659
Peter Hinz56885f32011-03-02 22:03:47 +00001660#ifdef WIN32
1661 context->fd_random = 0;
1662#else
1663 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1664 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001665 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001666 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001667 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001668 }
Peter Hinz56885f32011-03-02 22:03:47 +00001669#endif
Andy Green44eee682011-02-10 09:32:24 +00001670
Peter Hinz56885f32011-03-02 22:03:47 +00001671#ifdef LWS_OPENSSL_SUPPORT
1672 context->use_ssl = 0;
1673 context->ssl_ctx = NULL;
1674 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001675 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001676#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001677
Andy Greena1ce6be2013-01-18 11:43:21 +08001678 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001679
Andy Greena1ce6be2013-01-18 11:43:21 +08001680#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001681 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001682 struct sockaddr sa;
Andy Greene310b0c2013-02-10 15:10:10 +08001683 context->service_buffer[0] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001684
1685 /* find canonical hostname */
1686
Andy Greene310b0c2013-02-10 15:10:10 +08001687 context->service_buffer[(sizeof context->service_buffer) - 1] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001688 memset(&sa, 0, sizeof(sa));
1689 sa.sa_family = AF_INET;
1690 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
Andy Greene310b0c2013-02-10 15:10:10 +08001691 gethostname((char *)context->service_buffer, (sizeof context->service_buffer) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001692
1693 n = 0;
1694
Andy Greene310b0c2013-02-10 15:10:10 +08001695 if (strlen((char *)context->service_buffer) < sizeof(sa.sa_data) - 1) {
1696 strcpy(sa.sa_data, (char *)context->service_buffer);
Andy Greend09d7d42013-01-31 10:05:43 +08001697 lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Greene310b0c2013-02-10 15:10:10 +08001698 n = getnameinfo(&sa, sizeof(sa), (char *)context->service_buffer,
1699 (sizeof context->service_buffer) - 1, NULL, 0, NI_NAMEREQD);
Andy Green788c4a82012-10-22 12:29:57 +01001700 }
1701
1702 if (!n) {
Andy Greene310b0c2013-02-10 15:10:10 +08001703 strncpy(context->canonical_hostname, (char *)context->service_buffer,
Andy Green788c4a82012-10-22 12:29:57 +01001704 sizeof context->canonical_hostname - 1);
1705 context->canonical_hostname[
1706 sizeof context->canonical_hostname - 1] = '\0';
1707 } else
Andy Greene310b0c2013-02-10 15:10:10 +08001708 strncpy(context->canonical_hostname, (char *)context->service_buffer,
Andy Green788c4a82012-10-22 12:29:57 +01001709 sizeof context->canonical_hostname - 1);
1710
Andy Greenb3a614a2013-01-19 13:08:17 +08001711 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001712 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001713#endif
Andy Greena69f0512012-05-03 12:32:38 +08001714
Andy Green9659f372011-01-27 22:01:43 +00001715 /* split the proxy ads:port if given */
1716
1717 p = getenv("http_proxy");
1718 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001719 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001720 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001721 context->http_proxy_address[
1722 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001723
Peter Hinz56885f32011-03-02 22:03:47 +00001724 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001725 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001726 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001727 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001728 }
1729 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001730 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001731
Andy Greenb3a614a2013-01-19 13:08:17 +08001732 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001733 context->http_proxy_address,
1734 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001735 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001736
Andy Greena1ce6be2013-01-18 11:43:21 +08001737#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001738 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001739
Andy Green3faa9c72010-11-08 17:03:03 +00001740#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001741 context->use_ssl = info->ssl_cert_filepath != NULL &&
1742 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001743#ifdef USE_CYASSL
1744 lwsl_notice(" Compiled with CYASSL support\n");
1745#else
1746 lwsl_notice(" Compiled with OpenSSL support\n");
1747#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001748 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001749 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001750 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001751 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001752
Andy Green90c7cbc2011-01-27 06:26:52 +00001753#else
Andy Green2b40b792013-02-10 22:22:01 +08001754 if (info->ssl_cert_filepath != NULL &&
1755 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001756 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001757 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001758 }
Andy Greenb3a614a2013-01-19 13:08:17 +08001759 lwsl_notice(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001760 "serving unencrypted\n");
1761#endif
Andy Greena17c6922013-01-20 20:21:54 +08001762
Andy Green16ab3182013-02-10 18:02:31 +08001763 lwsl_notice(" per-connection allocation: %u + %u headers (only during handshake) + frame buffer set by protocol\n", sizeof(struct libwebsocket), sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001764 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001765#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001766
1767 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001768#ifdef WIN32
1769#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001770 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001771#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001772
1773
1774#ifdef LWS_OPENSSL_SUPPORT
1775
1776 /* basic openssl init */
1777
1778 SSL_library_init();
1779
1780 OpenSSL_add_all_algorithms();
1781 SSL_load_error_strings();
1782
Andy Green2e24da02011-03-05 16:12:04 +00001783 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001784 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1785
Andy Green90c7cbc2011-01-27 06:26:52 +00001786 /*
1787 * Firefox insists on SSLv23 not SSLv3
1788 * Konq disables SSLv2 by default now, SSLv23 works
1789 */
1790
1791 method = (SSL_METHOD *)SSLv23_server_method();
1792 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001793 lwsl_err("problem creating ssl method: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001794 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001795 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001796 }
Peter Hinz56885f32011-03-02 22:03:47 +00001797 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1798 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001799 lwsl_err("problem creating ssl context: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001800 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001801 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001802 }
1803
David Galeanocc148e42013-01-10 10:18:59 +08001804#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001805 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001806#endif
David Galeano77a677c2013-01-10 10:14:12 +08001807 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001808 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001809
Andy Greena1ce6be2013-01-18 11:43:21 +08001810#ifndef LWS_NO_CLIENT
1811
Andy Green90c7cbc2011-01-27 06:26:52 +00001812 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001813
Andy Green1b265272013-02-09 14:01:09 +08001814 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001815 method = (SSL_METHOD *)SSLv23_client_method();
1816 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001817 lwsl_err("problem creating ssl method: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001818 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001819 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001820 }
1821 /* create context */
1822 context->ssl_client_ctx = SSL_CTX_new(method);
1823 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001824 lwsl_err("problem creating ssl context: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001825 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001826 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001827 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001828
David Galeanocc148e42013-01-10 10:18:59 +08001829#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001830 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001831#endif
David Galeano77a677c2013-01-10 10:14:12 +08001832 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001833 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001834
Peter Hinz56885f32011-03-02 22:03:47 +00001835 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001836 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001837 if (!SSL_CTX_load_verify_locations(
1838 context->ssl_client_ctx, NULL,
1839 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001840 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001841 "Unable to load SSL Client certs from %s "
1842 "(set by --with-client-cert-dir= in configure) -- "
1843 " client ssl isn't going to work",
1844 LWS_OPENSSL_CLIENT_CERTS);
1845 } else
1846 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001847 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001848 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001849 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001850 "Unable to load SSL Client certs "
1851 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001852 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001853
1854 /*
1855 * callback allowing user code to load extra verification certs
1856 * helping the client to verify server identity
1857 */
1858
1859 context->protocols[0].callback(context, NULL,
1860 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1861 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001862 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001863#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001864
Andy Greenc6bf2c22011-02-20 11:10:47 +00001865 /* as a server, are we requiring clients to identify themselves? */
1866
Andy Green1b265272013-02-09 14:01:09 +08001867 if (info->options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001868
1869 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001870
Peter Hinz56885f32011-03-02 22:03:47 +00001871 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001872 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1873 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001874
1875 /*
1876 * give user code a chance to load certs into the server
1877 * allowing it to verify incoming client certs
1878 */
1879
Peter Hinz56885f32011-03-02 22:03:47 +00001880 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001881 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001882 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001883 }
1884
Peter Hinz56885f32011-03-02 22:03:47 +00001885 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001886
1887 /* openssl init for server sockets */
1888
Andy Green3faa9c72010-11-08 17:03:03 +00001889 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001890 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001891 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001892 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001893 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001894 info->ssl_cert_filepath,
Andy Greene310b0c2013-02-10 15:10:10 +08001895 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001896 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001897 }
1898 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001899 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001900 info->ssl_private_key_filepath,
1901 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001902 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001903 info->ssl_private_key_filepath,
Andy Greene310b0c2013-02-10 15:10:10 +08001904 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001905 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001906 }
1907 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001908 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001909 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001910 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001911 }
1912
1913 /* SSL is happy and has a cert it's content with */
1914 }
1915#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001916
Andy Greendf736162011-01-18 15:39:02 +00001917 /* selftest */
1918
1919 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001920 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00001921
Andy Greena1ce6be2013-01-18 11:43:21 +08001922#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001923 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001924
Andy Green1b265272013-02-09 14:01:09 +08001925 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001926 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1927 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001928
Andy Green4739e5c2011-01-22 12:51:57 +00001929 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1930 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001931 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001932 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001933 }
Andy Green775c0dd2010-10-29 14:15:22 +01001934
Joakim Soderberg4c531232013-02-06 15:26:58 +09001935#ifndef WIN32
1936 /* allow us to restart even if old sockets in TIME_WAIT
1937 * (REUSEADDR on Unix means, "don't hang on to this address after the
1938 * listener is closed." On Windows, though, it means "don't keep other
1939 * processes from binding to this address while we're using it) */
Andy Green6ee372f2012-04-09 15:09:01 +08001940 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1941 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09001942#endif
Andy Green6c939552011-03-08 08:56:57 +00001943
1944 /* Disable Nagle */
1945 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001946 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1947 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001948
Joakim Soderberg4c531232013-02-06 15:26:58 +09001949 #ifdef WIN32
1950 opt = 0;
1951 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt );
1952 #else
Andy Greene2160712013-01-28 12:19:10 +08001953 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09001954 #endif
Andy Greene2160712013-01-28 12:19:10 +08001955
Andy Green4739e5c2011-01-22 12:51:57 +00001956 bzero((char *) &serv_addr, sizeof(serv_addr));
1957 serv_addr.sin_family = AF_INET;
Andy Green1b265272013-02-09 14:01:09 +08001958 if (info->interface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001959 serv_addr.sin_addr.s_addr = INADDR_ANY;
1960 else
Andy Green1b265272013-02-09 14:01:09 +08001961 interface_to_sa(info->interface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001962 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08001963 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00001964
1965 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1966 sizeof(serv_addr));
1967 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001968 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08001969 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001970 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001971 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001972 }
Andy Green0d338332011-02-12 11:57:43 +00001973
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001974 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001975 if (wsi == NULL) {
1976 lwsl_err("Out of mem\n");
1977 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001978 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08001979 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001980 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001981 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08001982#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001983 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001984#endif
Andy Green0d338332011-02-12 11:57:43 +00001985 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08001986
1987 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001988
Andy Green65b0e912013-01-16 07:59:47 +08001989 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
1990 context->listen_service_count = 0;
1991 context->listen_service_fd = sockfd;
1992
Andy Greena824d182013-01-15 20:52:29 +08001993 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08001994 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00001995 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001996#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001997
Andy Green6ee372f2012-04-09 15:09:01 +08001998 /*
1999 * drop any root privs for this process
2000 * to listen on port < 1023 we would have needed root, but now we are
2001 * listening, we don't want the power for anything else
2002 */
Peter Hinz56885f32011-03-02 22:03:47 +00002003#ifdef WIN32
2004#else
Andy Green1b265272013-02-09 14:01:09 +08002005 if (info->gid != -1)
2006 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002007 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002008 if (info->uid != -1)
2009 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002010 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002011#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002012
Andy Green6f520a52013-01-29 17:57:39 +08002013 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002014
Peter Hinz56885f32011-03-02 22:03:47 +00002015 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002016 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002017 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002018
Andy Green43db0452013-01-10 19:50:35 +08002019 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002020 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002021
Andy Green1b265272013-02-09 14:01:09 +08002022 info->protocols[context->count_protocols].owning_server =
2023 context;
2024 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002025 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00002026 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002027
Andy Green3182ece2013-01-20 17:08:31 +08002028#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002029 /*
2030 * give all extensions a chance to create any per-context
2031 * allocations they need
2032 */
2033
2034 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002035 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002036 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08002037
Andy Green1b265272013-02-09 14:01:09 +08002038 if (info->extensions) {
2039 ext = info->extensions;
2040 while (ext->callback) {
2041 lwsl_ext(" Extension: %s\n", ext->name);
2042 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002043 (enum libwebsocket_extension_callback_reasons)m,
2044 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002045 ext++;
2046 }
Andy Greena41314f2011-05-23 10:00:03 +01002047 }
Andy Green3182ece2013-01-20 17:08:31 +08002048#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002049 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002050
2051bail:
2052 libwebsocket_context_destroy(context);
2053 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002054}
Andy Greenb45993c2010-12-18 15:13:50 +00002055
Andy Greenb45993c2010-12-18 15:13:50 +00002056/**
2057 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002058 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002059 * @wsi: pointer to struct websocket you want to know the protocol of
2060 *
Andy Green8f037e42010-12-19 22:13:26 +00002061 *
Andy Green6f520a52013-01-29 17:57:39 +08002062 * Some apis can act on all live connections of a given protocol,
2063 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002064 */
Andy Greenab990e42010-10-31 12:42:52 +00002065
Andy Greenb45993c2010-12-18 15:13:50 +00002066const struct libwebsocket_protocols *
2067libwebsockets_get_protocol(struct libwebsocket *wsi)
2068{
2069 return wsi->protocol;
2070}
2071
Andy Green82c3d542011-03-07 21:16:31 +00002072int
2073libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2074{
Andy Green623a98d2013-01-21 11:04:23 +08002075 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002076}
Alex Bligh49146db2011-11-07 17:19:25 +08002077
David Galeanoe2cf9922013-01-09 18:06:55 +08002078unsigned char
2079libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2080{
Andy Green623a98d2013-01-21 11:04:23 +08002081 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002082}
2083
Alex Bligh49146db2011-11-07 17:19:25 +08002084void *
2085libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2086{
2087 /* allocate the per-connection user memory (if any) */
2088
2089 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2090 wsi->user_space = malloc(
2091 wsi->protocol->per_session_data_size);
2092 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002093 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002094 return NULL;
2095 }
Andy Green6ee372f2012-04-09 15:09:01 +08002096 memset(wsi->user_space, 0,
2097 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002098 }
2099 return wsi->user_space;
2100}
Andy Green43db0452013-01-10 19:50:35 +08002101
Andy Green0b319092013-01-19 11:17:56 +08002102static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002103{
Andy Green0b319092013-01-19 11:17:56 +08002104 char buf[300];
2105 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002106 int n;
2107
2108 gettimeofday(&tv, NULL);
2109
2110 buf[0] = '\0';
2111 for (n = 0; n < LLL_COUNT; n++)
2112 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002113 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Green0b319092013-01-19 11:17:56 +08002114 (int)(tv.tv_usec / 100), log_level_names[n]);
2115 break;
2116 }
2117
2118 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002119}
2120
Joakim Soderberg4c531232013-02-06 15:26:58 +09002121#ifdef WIN32
2122void lwsl_emit_syslog(int level, const char *line)
2123{
2124 lwsl_emit_stderr(level, line);
2125}
2126#else
Andy Greenc11db202013-01-19 11:12:16 +08002127void lwsl_emit_syslog(int level, const char *line)
2128{
2129 int syslog_level = LOG_DEBUG;
2130
2131 switch (level) {
2132 case LLL_ERR:
2133 syslog_level = LOG_ERR;
2134 break;
2135 case LLL_WARN:
2136 syslog_level = LOG_WARNING;
2137 break;
2138 case LLL_NOTICE:
2139 syslog_level = LOG_NOTICE;
2140 break;
2141 case LLL_INFO:
2142 syslog_level = LOG_INFO;
2143 break;
2144 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002145 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002146}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002147#endif
Andy Greenc11db202013-01-19 11:12:16 +08002148
Andy Green43db0452013-01-10 19:50:35 +08002149void _lws_log(int filter, const char *format, ...)
2150{
Andy Greende8f27a2013-01-12 09:17:42 +08002151 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002152 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002153
2154 if (!(log_level & filter))
2155 return;
2156
Andy Green43db0452013-01-10 19:50:35 +08002157 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08002158 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08002159 buf[(sizeof buf) - 1] = '\0';
2160 va_end(ap);
2161
Andy Green0b319092013-01-19 11:17:56 +08002162 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002163}
2164
2165/**
2166 * lws_set_log_level() - Set the logging bitfield
2167 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002168 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2169 * function to perform log string emission instead of
2170 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002171 *
Andy Greende8f27a2013-01-12 09:17:42 +08002172 * log level defaults to "err" and "warn" contexts enabled only and
2173 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002174 */
2175
Andy Green058ba812013-01-19 11:32:18 +08002176void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002177{
2178 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002179 if (log_emit_function)
2180 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002181}