blob: 48ea51a4885780b4454f39cc21d39653e2562bb9 [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 Greenfdd305a2013-02-11 14:32:48 +0800275 /* make valgrind happy */
276 memset(buf, 0, sizeof buf);
277 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000278 0, LWS_WRITE_CLOSE);
279 if (!n) {
280 /*
281 * we have sent a nice protocol level indication we
282 * now wish to close, we should not send anything more
283 */
284
285 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
286
Andy Green0303db42013-01-17 14:46:43 +0800287 /* and we should wait for a reply for a bit out of politeness */
Andy Greenda527df2011-03-07 07:08:12 +0000288
289 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800290 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000291
Andy Green43db0452013-01-10 19:50:35 +0800292 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000293
294 return;
295 }
296
Andy Green0303db42013-01-17 14:46:43 +0800297 lwsl_info("close: sending the close packet failed, hanging up\n");
298
Andy Greenda527df2011-03-07 07:08:12 +0000299 /* else, the send failed and we should just hang up */
300 }
301
Andy Green3182ece2013-01-20 17:08:31 +0800302#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000303just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800304#endif
Andy Green66a16f32011-05-24 22:07:45 +0100305
Andy Green43db0452013-01-10 19:50:35 +0800306 lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100307
Andy Greenda527df2011-03-07 07:08:12 +0000308 /*
309 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800310 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000311 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000312
Andy Greendfb23042013-01-17 12:26:48 +0800313 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000314
Andy Green251f6fa2010-11-03 11:13:06 +0000315 wsi->state = WSI_STATE_DEAD_SOCKET;
316
Andy Green1ae1b1f2013-02-11 14:12:32 +0800317 if ((old_state == WSI_STATE_ESTABLISHED || wsi->mode == LWS_CONNMODE_WS_SERVING || wsi->mode == LWS_CONNMODE_WS_CLIENT) && wsi->u.ws.rx_user_buffer) {
Andy Green54495112013-02-06 21:10:16 +0900318 free(wsi->u.ws.rx_user_buffer);
319 wsi->u.ws.rx_user_buffer = NULL;
320 }
321
Andy Green4b6fbe12011-02-14 08:03:48 +0000322 /* tell the user it's all over for this guy */
323
Andy Greend4302732011-02-28 07:45:29 +0000324 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800325 ((old_state == WSI_STATE_ESTABLISHED) ||
326 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
327 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800328 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000329 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000330 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800331 } else
Andy Green43db0452013-01-10 19:50:35 +0800332 lwsl_debug("not calling back closed, old_state=%d\n", old_state);
Andy Green251f6fa2010-11-03 11:13:06 +0000333
Andy Green3182ece2013-01-20 17:08:31 +0800334#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000335 /* deallocate any active extension contexts */
336
337 for (n = 0; n < wsi->count_active_extensions; n++) {
338 if (!wsi->active_extensions[n]->callback)
339 continue;
340
Andy Green46c2ea02011-03-22 09:04:01 +0000341 wsi->active_extensions[n]->callback(context,
342 wsi->active_extensions[n], wsi,
343 LWS_EXT_CALLBACK_DESTROY,
344 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000345
346 free(wsi->active_extensions_user[n]);
347 }
348
Andy Greena41314f2011-05-23 10:00:03 +0100349 /*
350 * inform all extensions in case they tracked this guy out of band
351 * even though not active on him specifically
352 */
353
354 ext = context->extensions;
355 while (ext && ext->callback) {
356 ext->callback(context, ext, wsi,
357 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
358 NULL, NULL, 0);
359 ext++;
360 }
Andy Green3182ece2013-01-20 17:08:31 +0800361#endif
Andy Greena41314f2011-05-23 10:00:03 +0100362
Andy Green623a98d2013-01-21 11:04:23 +0800363 if (wsi->u.ws.rxflow_buffer)
364 free(wsi->u.ws.rxflow_buffer);
Andy Green706961d2013-01-17 16:50:35 +0800365
Andy Green43db0452013-01-10 19:50:35 +0800366/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000367
Andy Green3faa9c72010-11-08 17:03:03 +0000368#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000369 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000370 n = SSL_get_fd(wsi->ssl);
371 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800372 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000373 SSL_free(wsi->ssl);
374 } else {
375#endif
Andy Green0303db42013-01-17 14:46:43 +0800376 if (wsi->sock) {
377 n = shutdown(wsi->sock, SHUT_RDWR);
378 if (n)
379 lwsl_debug("closing: shutdown returned %d\n", errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800380
Andy Green0303db42013-01-17 14:46:43 +0800381 n = compatible_close(wsi->sock);
382 if (n)
383 lwsl_debug("closing: close returned %d\n", errno);
384 }
Andy Green3faa9c72010-11-08 17:03:03 +0000385#ifdef LWS_OPENSSL_SUPPORT
386 }
387#endif
David Brooks2c60d952012-04-20 12:19:01 +0800388 if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000389 free(wsi->user_space);
390
Andy Green251f6fa2010-11-03 11:13:06 +0000391 free(wsi);
392}
393
Andy Green07034092011-02-13 08:37:12 +0000394/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000395 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800396 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000397 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000398 * @fd: Connection socket descriptor
399 */
400
401void
Peter Hinz56885f32011-03-02 22:03:47 +0000402libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000403{
Andy Greendfb23042013-01-17 12:26:48 +0800404 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000405
Andy Greendfb23042013-01-17 12:26:48 +0800406 if (wsi) {
Andy Green467c7ef2013-01-30 12:28:34 +0800407 lwsl_info("closing connection at libwebsockets_hangup_on_client:\n");
Andy Greendfb23042013-01-17 12:26:48 +0800408 libwebsocket_close_and_free_session(context,
409 wsi, LWS_CLOSE_STATUS_NOSTATUS);
410 } else
411 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000412}
413
414
415/**
Andy Green07034092011-02-13 08:37:12 +0000416 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800417 * @context: Libwebsockets context
418 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000419 * @fd: Connection socket descriptor
420 * @name: Buffer to take client address name
421 * @name_len: Length of client address name buffer
422 * @rip: Buffer to take client address IP qotted quad
423 * @rip_len: Length of client address IP buffer
424 *
425 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800426 * the client connected with socket descriptor @fd. Names may be
427 * truncated if there is not enough room. If either cannot be
428 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000429 */
430
431void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800432libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
433 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000434 char *rip, int rip_len)
435{
436 unsigned int len;
437 struct sockaddr_in sin;
438 struct hostent *host;
439 struct hostent *host1;
440 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000441 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000442 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800443 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800444#ifdef AF_LOCAL
445 struct sockaddr_un *un;
446#endif
Andy Green07034092011-02-13 08:37:12 +0000447
448 rip[0] = '\0';
449 name[0] = '\0';
450
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800451 lws_latency_pre(context, wsi);
452
Andy Green07034092011-02-13 08:37:12 +0000453 len = sizeof sin;
454 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
455 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800456 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000457 }
Andy Green6ee372f2012-04-09 15:09:01 +0800458
Andy Green07034092011-02-13 08:37:12 +0000459 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
460 AF_INET);
461 if (host == NULL) {
462 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800463 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000464 }
465
466 strncpy(name, host->h_name, name_len);
467 name[name_len - 1] = '\0';
468
469 host1 = gethostbyname(host->h_name);
470 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800471 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000472 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000473 n = 0;
474 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000475 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000476 if (p == NULL)
477 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000478 if ((host1->h_addrtype != AF_INET)
479#ifdef AF_LOCAL
480 && (host1->h_addrtype != AF_LOCAL)
481#endif
482 )
Andy Green07034092011-02-13 08:37:12 +0000483 continue;
484
Andy Green7627af52011-03-09 15:13:52 +0000485 if (host1->h_addrtype == AF_INET)
486 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000487#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000488 else {
489 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800490 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000491 ip[sizeof(ip) - 1] = '\0';
492 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000493#endif
Andy Green07034092011-02-13 08:37:12 +0000494 p = NULL;
495 strncpy(rip, ip, rip_len);
496 rip[rip_len - 1] = '\0';
497 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800498
499 ret = 0;
500bail:
501 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000502}
Andy Green9f990342011-02-12 11:57:45 +0000503
Peter Hinz56885f32011-03-02 22:03:47 +0000504int libwebsockets_get_random(struct libwebsocket_context *context,
505 void *buf, int len)
506{
507 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800508 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000509
510#ifdef WIN32
511 for (n = 0; n < len; n++)
512 p[n] = (unsigned char)rand();
513#else
514 n = read(context->fd_random, p, len);
515#endif
516
517 return n;
518}
519
Andy Greena690cd02013-02-09 12:25:31 +0800520int lws_set_socket_options(struct libwebsocket_context *context, int fd)
521{
522 int optval = 1;
523 socklen_t optlen = sizeof(optval);
524#ifdef WIN32
525 unsigned long optl = 0;
526#endif
527#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
528 struct protoent *tcp_proto;
529#endif
530
531 if (context->ka_time) {
532 /* enable keepalive on this socket */
533 optval = 1;
534 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
535 (const void *)&optval, optlen) < 0)
536 return 1;
537
Andy Greena47865f2013-02-10 09:39:47 +0800538#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
539
540 /*
541 * didn't find a way to set these per-socket, need to
542 * tune kernel systemwide values
543 */
544
545#else
Andy Greena690cd02013-02-09 12:25:31 +0800546 /* set the keepalive conditions we want on it too */
547 optval = context->ka_time;
548 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
549 (const void *)&optval, optlen) < 0)
550 return 1;
551
552 optval = context->ka_probes;
553 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
554 (const void *)&optval, optlen) < 0)
555 return 1;
556
557 optval = context->ka_interval;
558 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
559 (const void *)&optval, optlen) < 0)
560 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800561#endif
Andy Greena690cd02013-02-09 12:25:31 +0800562 }
563
564 /* Disable Nagle */
565 optval = 1;
566#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
567 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
568#else
569 tcp_proto = getprotobyname("TCP");
570 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
571#endif
572
573 /* We are nonblocking... */
574#ifdef WIN32
575 ioctlsocket(fd, FIONBIO, &optl);
576#else
577 fcntl(fd, F_SETFL, O_NONBLOCK);
578#endif
579
580 return 0;
581}
582
Andy Green95a7b5d2011-03-06 10:29:39 +0000583int lws_send_pipe_choked(struct libwebsocket *wsi)
584{
585 struct pollfd fds;
586
587 fds.fd = wsi->sock;
588 fds.events = POLLOUT;
589 fds.revents = 0;
590
591 if (poll(&fds, 1, 0) != 1)
592 return 1;
593
594 if ((fds.revents & POLLOUT) == 0)
595 return 1;
596
597 /* okay to send another packet without blocking */
598
599 return 0;
600}
601
Andy Greena41314f2011-05-23 10:00:03 +0100602int
Andy Green3b84c002011-03-06 13:14:42 +0000603lws_handle_POLLOUT_event(struct libwebsocket_context *context,
604 struct libwebsocket *wsi, struct pollfd *pollfd)
605{
Andy Green3b84c002011-03-06 13:14:42 +0000606 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800607
Andy Green3182ece2013-01-20 17:08:31 +0800608#ifndef LWS_NO_EXTENSIONS
609 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000610 int ret;
611 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100612 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000613
Andy Greena41314f2011-05-23 10:00:03 +0100614 for (n = 0; n < wsi->count_active_extensions; n++) {
615 if (!wsi->active_extensions[n]->callback)
616 continue;
617
618 m = wsi->active_extensions[n]->callback(context,
619 wsi->active_extensions[n], wsi,
620 LWS_EXT_CALLBACK_IS_WRITEABLE,
621 wsi->active_extensions_user[n], NULL, 0);
622 if (m > handled)
623 handled = m;
624 }
625
626 if (handled == 1)
627 goto notify_action;
628
629 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000630 goto user_service;
631
632 /*
633 * check in on the active extensions, see if they
634 * had pending stuff to spill... they need to get the
635 * first look-in otherwise sequence will be disordered
636 *
637 * NULL, zero-length eff_buf means just spill pending
638 */
639
640 ret = 1;
641 while (ret == 1) {
642
643 /* default to nobody has more to spill */
644
645 ret = 0;
646 eff_buf.token = NULL;
647 eff_buf.token_len = 0;
648
649 /* give every extension a chance to spill */
650
651 for (n = 0; n < wsi->count_active_extensions; n++) {
652 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000653 wsi->protocol->owning_server,
654 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000655 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
656 wsi->active_extensions_user[n], &eff_buf, 0);
657 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800658 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000659 return -1;
660 }
661 if (m)
662 /*
663 * at least one extension told us he has more
664 * to spill, so we will go around again after
665 */
666 ret = 1;
667 }
668
669 /* assuming they gave us something to send, send it */
670
671 if (eff_buf.token_len) {
672 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
673 eff_buf.token_len))
674 return -1;
675 } else
676 continue;
677
678 /* no extension has more to spill */
679
680 if (!ret)
681 continue;
682
683 /*
684 * There's more to spill from an extension, but we just sent
685 * something... did that leave the pipe choked?
686 */
687
688 if (!lws_send_pipe_choked(wsi))
689 /* no we could add more */
690 continue;
691
Andy Green43db0452013-01-10 19:50:35 +0800692 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000693
694 /*
695 * Yes, he's choked. Leave the POLLOUT masked on so we will
696 * come back here when he is unchoked. Don't call the user
697 * callback to enforce ordering of spilling, he'll get called
698 * when we come back here and there's nothing more to spill.
699 */
700
701 return 0;
702 }
703
704 wsi->extension_data_pending = 0;
705
706user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800707#endif
Andy Green3b84c002011-03-06 13:14:42 +0000708 /* one shot */
709
Andy Greena41314f2011-05-23 10:00:03 +0100710 if (pollfd) {
711 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000712
Andy Greena41314f2011-05-23 10:00:03 +0100713 /* external POLL support via protocol 0 */
714 context->protocols[0].callback(context, wsi,
715 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
716 (void *)(long)wsi->sock, NULL, POLLOUT);
717 }
Andy Green3182ece2013-01-20 17:08:31 +0800718#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100719notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800720#endif
Andy Green3b84c002011-03-06 13:14:42 +0000721
Andy Green9e4c2b62011-03-07 20:47:39 +0000722 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
723 n = LWS_CALLBACK_CLIENT_WRITEABLE;
724 else
725 n = LWS_CALLBACK_SERVER_WRITEABLE;
726
Andy Greenb8b247d2013-01-22 07:20:08 +0800727 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Green706961d2013-01-17 16:50:35 +0800728 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000729}
730
731
732
Andy Greena41314f2011-05-23 10:00:03 +0100733void
734libwebsocket_service_timeout_check(struct libwebsocket_context *context,
735 struct libwebsocket *wsi, unsigned int sec)
736{
Andy Green3182ece2013-01-20 17:08:31 +0800737#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100738 int n;
739
740 /*
741 * if extensions want in on it (eg, we are a mux parent)
742 * give them a chance to service child timeouts
743 */
744
745 for (n = 0; n < wsi->count_active_extensions; n++)
746 wsi->active_extensions[n]->callback(
747 context, wsi->active_extensions[n],
748 wsi, LWS_EXT_CALLBACK_1HZ,
749 wsi->active_extensions_user[n], NULL, sec);
750
Andy Green3182ece2013-01-20 17:08:31 +0800751#endif
Andy Greena41314f2011-05-23 10:00:03 +0100752 if (!wsi->pending_timeout)
753 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800754
Andy Greena41314f2011-05-23 10:00:03 +0100755 /*
756 * if we went beyond the allowed time, kill the
757 * connection
758 */
759
760 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800761 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100762 libwebsocket_close_and_free_session(context,
763 wsi, LWS_CLOSE_STATUS_NOSTATUS);
764 }
765}
766
Andy Green9f990342011-02-12 11:57:45 +0000767/**
768 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000769 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000770 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800771 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000772 *
Andy Green75006172013-01-22 12:32:11 +0800773 * This function takes a pollfd that has POLLIN or POLLOUT activity and
774 * services it according to the state of the associated struct libwebsocket.
775 *
776 * The one call deals with all "service" that might happen on a socket
777 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000778 */
779
780int
Peter Hinz56885f32011-03-02 22:03:47 +0000781libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000782 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000783{
Andy Greena1ce6be2013-01-18 11:43:21 +0800784 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000785 int n;
Andy Green0d338332011-02-12 11:57:43 +0000786 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800787 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000788 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800789
Andy Green3182ece2013-01-20 17:08:31 +0800790#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000791 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800792#endif
Andy Green98a717c2011-03-06 13:14:15 +0000793 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800794#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800795 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800796#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800797#ifndef LWS_NO_SERVER
798 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
799#endif
Andy Greenf0b79e22013-02-10 10:46:45 +0800800
801 if (context->listen_service_fd)
802 listen_socket_fds_index = context->lws_lookup[
803 context->listen_service_fd]->position_in_fds_table;
804
Andy Greena71eafc2011-02-14 17:59:43 +0000805 /*
806 * you can call us with pollfd = NULL to just allow the once-per-second
807 * global timeout checks; if less than a second since the last check
808 * it returns immediately then.
809 */
810
811 gettimeofday(&tv, NULL);
812
Peter Hinz56885f32011-03-02 22:03:47 +0000813 if (context->last_timeout_check_s != tv.tv_sec) {
814 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000815
Joakim Soderberg4c531232013-02-06 15:26:58 +0900816 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800817 /* if our parent went down, don't linger around */
818 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
819 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900820 #endif
Andy Green24cba922013-01-19 13:56:10 +0800821
Andy Greena71eafc2011-02-14 17:59:43 +0000822 /* global timeout check once per second */
823
Peter Hinz56885f32011-03-02 22:03:47 +0000824 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800825 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
826 if (!new_wsi)
827 continue;
828 libwebsocket_service_timeout_check(context,
829 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000830 }
831 }
832
833 /* just here for timeout management? */
834
835 if (pollfd == NULL)
836 return 0;
837
838 /* no, here to service a socket descriptor */
839
Andy Green65b0e912013-01-16 07:59:47 +0800840 /*
841 * deal with listen service piggybacking
842 * every listen_service_modulo services of other fds, we
843 * sneak one in to service the listen socket if there's anything waiting
844 *
845 * To handle connection storms, as found in ab, if we previously saw a
846 * pending connection here, it causes us to check again next time.
847 */
848
Andy Green9e4c9172013-02-10 09:06:38 +0800849 if (context->listen_service_fd && pollfd != &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800850 context->listen_service_count++;
851 if (context->listen_service_extraseen ||
852 context->listen_service_count == context->listen_service_modulo) {
853 context->listen_service_count = 0;
854 m = 1;
855 if (context->listen_service_extraseen > 5)
856 m = 2;
857 while (m--) {
858 /* even with extpoll, we prepared this internal fds for listen */
Andy Green9e4c9172013-02-10 09:06:38 +0800859 n = poll(&context->fds[listen_socket_fds_index], 1, 0);
Andy Green65b0e912013-01-16 07:59:47 +0800860 if (n > 0) { /* there's a connection waiting for us */
Andy Green9e4c9172013-02-10 09:06:38 +0800861 libwebsocket_service_fd(context, &context->fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800862 context->listen_service_extraseen++;
863 } else {
864 if (context->listen_service_extraseen)
865 context->listen_service_extraseen--;
866 break;
867 }
868 }
869 }
870
871 }
872
873 /* okay, what we came here to do... */
874
Andy Greendfb23042013-01-17 12:26:48 +0800875 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800876 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800877 if (pollfd->fd > 11)
878 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800879 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800880 }
Andy Green8f037e42010-12-19 22:13:26 +0000881
Andy Green0d338332011-02-12 11:57:43 +0000882 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800883
Andy Greena1ce6be2013-01-18 11:43:21 +0800884#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800885 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000886 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800887 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800888 return lws_server_socket_service(context, wsi, pollfd);
889#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000890
Andy Green0d338332011-02-12 11:57:43 +0000891 case LWS_CONNMODE_WS_SERVING:
892 case LWS_CONNMODE_WS_CLIENT:
893
894 /* handle session socket closed */
895
896 if (pollfd->revents & (POLLERR | POLLHUP)) {
897
Andy Green43db0452013-01-10 19:50:35 +0800898 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000899 (void *)wsi, pollfd->fd);
900
Peter Hinz56885f32011-03-02 22:03:47 +0000901 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000902 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800903 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000904 }
905
Andy Green0d338332011-02-12 11:57:43 +0000906 /* the guy requested a callback when it was OK to write */
907
Andy Greenda527df2011-03-07 07:08:12 +0000908 if ((pollfd->revents & POLLOUT) &&
909 wsi->state == WSI_STATE_ESTABLISHED)
910 if (lws_handle_POLLOUT_event(context, wsi,
911 pollfd) < 0) {
Andy Green16ab3182013-02-10 18:02:31 +0800912 lwsl_info("libwebsocket_service_fd: closing");
Andy Greenda527df2011-03-07 07:08:12 +0000913 libwebsocket_close_and_free_session(
914 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800915 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000916 }
Andy Green0d338332011-02-12 11:57:43 +0000917
Andy Green0d338332011-02-12 11:57:43 +0000918
919 /* any incoming data ready? */
920
921 if (!(pollfd->revents & POLLIN))
922 break;
923
Andy Greenb45993c2010-12-18 15:13:50 +0000924#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800925read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800926 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800927 eff_buf.token_len = SSL_read(wsi->ssl,
928 context->service_buffer,
929 sizeof context->service_buffer);
Andy Green467c7ef2013-01-30 12:28:34 +0800930 if (!eff_buf.token_len) {
931 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800932 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +0800933 ERR_error_string(n, (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800934 }
935 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000936#endif
Andy Greene84652c2013-02-08 13:01:02 +0800937 eff_buf.token_len = recv(pollfd->fd,
938 context->service_buffer,
939 sizeof context->service_buffer, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000940
Andy Green98a717c2011-03-06 13:14:15 +0000941 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800942 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000943 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200944 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800945 libwebsocket_close_and_free_session(context,
946 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800947 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000948 }
Andy Green98a717c2011-03-06 13:14:15 +0000949 if (!eff_buf.token_len) {
Andy Green467c7ef2013-01-30 12:28:34 +0800950 lwsl_info("closing connection due to zero length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000951 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800952 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800953 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000954 }
955
Andy Green98a717c2011-03-06 13:14:15 +0000956 /*
957 * give any active extensions a chance to munge the buffer
958 * before parse. We pass in a pointer to an lws_tokens struct
959 * prepared with the default buffer and content length that's in
960 * there. Rather than rewrite the default buffer, extensions
961 * that expect to grow the buffer can adapt .token to
962 * point to their own per-connection buffer in the extension
963 * user allocation. By default with no extensions or no
964 * extension callback handling, just the normal input buffer is
965 * used then so it is efficient.
966 */
Andy Greenb45993c2010-12-18 15:13:50 +0000967
Andy Greene84652c2013-02-08 13:01:02 +0800968 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +0800969#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000970 more = 1;
971 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000972
Andy Green98a717c2011-03-06 13:14:15 +0000973 more = 0;
974
975 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000976 m = wsi->active_extensions[n]->callback(context,
977 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000978 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000979 wsi->active_extensions_user[n],
980 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000981 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800982 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800983 "Extension reports fatal error\n");
984 libwebsocket_close_and_free_session(
985 context, wsi,
986 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800987 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000988 }
989 if (m)
990 more = 1;
991 }
Andy Green3182ece2013-01-20 17:08:31 +0800992#endif
Andy Green98a717c2011-03-06 13:14:15 +0000993 /* service incoming data */
994
995 if (eff_buf.token_len) {
996 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800997 (unsigned char *)eff_buf.token,
998 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +0000999 if (n < 0)
1000 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001001 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001002 }
Andy Green3182ece2013-01-20 17:08:31 +08001003#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001004 eff_buf.token = NULL;
1005 eff_buf.token_len = 0;
1006 }
Andy Green3182ece2013-01-20 17:08:31 +08001007#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001008
1009#ifdef LWS_OPENSSL_SUPPORT
1010 if (wsi->ssl && SSL_pending(wsi->ssl))
1011 goto read_pending;
1012#endif
Andy Green98a717c2011-03-06 13:14:15 +00001013 break;
Andy Green76f61e72013-01-16 11:53:05 +08001014
1015 default:
Andy Green03674a62013-01-16 11:47:40 +08001016#ifdef LWS_NO_CLIENT
1017 break;
1018#else
Andy Green76f61e72013-01-16 11:53:05 +08001019 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001020#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001021 }
1022
1023 return 0;
1024}
1025
Andy Green0d338332011-02-12 11:57:43 +00001026
Andy Green6964bb52011-01-23 16:50:33 +00001027/**
1028 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001029 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001030 *
1031 * This function closes any active connections and then frees the
1032 * context. After calling this, any further use of the context is
1033 * undefined.
1034 */
1035void
Peter Hinz56885f32011-03-02 22:03:47 +00001036libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001037{
Andy Green3182ece2013-01-20 17:08:31 +08001038#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001039 int n;
1040 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001041 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001042 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001043
Andy Greend636e352013-01-29 12:36:17 +08001044#ifdef LWS_LATENCY
1045 if (context->worst_latency_info[0])
1046 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1047#endif
1048
Andy Greendfb23042013-01-17 12:26:48 +08001049 for (n = 0; n < context->fds_count; n++) {
1050 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
1051 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001052 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1053 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001054 }
Andy Green6964bb52011-01-23 16:50:33 +00001055
Andy Greena41314f2011-05-23 10:00:03 +01001056 /*
1057 * give all extensions a chance to clean up any per-context
1058 * allocations they might have made
1059 */
1060
1061 ext = context->extensions;
1062 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1063 if (context->listen_port)
1064 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001065 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001066 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001067 ext++;
1068 }
Andy Greena7109e62013-02-11 12:05:54 +08001069
1070 /*
1071 * inform all the protocols that they are done and will have no more
1072 * callbacks
1073 */
1074
1075 while (protocol->callback) {
1076 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1077 NULL, NULL, 0);
1078 protocol++;
1079 }
1080
Andy Green3182ece2013-01-20 17:08:31 +08001081#endif
Andy Greena41314f2011-05-23 10:00:03 +01001082
Peter Hinz56885f32011-03-02 22:03:47 +00001083#ifdef WIN32
1084#else
1085 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001086#endif
1087
Peter Hinz56885f32011-03-02 22:03:47 +00001088#ifdef LWS_OPENSSL_SUPPORT
1089 if (context->ssl_ctx)
1090 SSL_CTX_free(context->ssl_ctx);
1091 if (context->ssl_client_ctx)
1092 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001093
1094 ERR_remove_state(0);
1095 ERR_free_strings();
1096 EVP_cleanup();
1097 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001098#endif
1099
Andy Green46596482013-02-11 11:04:01 +08001100 if (context->fds)
1101 free(context->fds);
1102 if (context->lws_lookup)
1103 free(context->lws_lookup);
1104
Peter Hinz56885f32011-03-02 22:03:47 +00001105 free(context);
1106
1107#ifdef WIN32
1108 WSACleanup();
1109#endif
Andy Green6964bb52011-01-23 16:50:33 +00001110}
1111
Andy Greend88146d2013-01-22 12:40:35 +08001112/**
1113 * libwebsocket_context_user() - get the user data associated with the whole context
1114 * @context: Websocket context
1115 *
1116 * This returns the optional user allocation that can be attached to
1117 * the context the sockets live in at context_create time. It's a way
1118 * to let all sockets serviced in the same context share data without
1119 * using globals statics in the user code.
1120 */
1121
1122
Alon Levy0291eb32012-10-19 11:21:56 +02001123LWS_EXTERN void *
1124libwebsocket_context_user(struct libwebsocket_context *context)
1125{
1126 return context->user_space;
1127}
1128
Andy Green6964bb52011-01-23 16:50:33 +00001129/**
1130 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001131 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001132 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1133 * service otherwise block and service immediately, returning
1134 * after the timeout if nothing needed service.
1135 *
1136 * This function deals with any pending websocket traffic, for three
1137 * kinds of event. It handles these events on both server and client
1138 * types of connection the same.
1139 *
1140 * 1) Accept new connections to our context's server
1141 *
Andy Green6f520a52013-01-29 17:57:39 +08001142 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001143 * server or client connections.
1144 *
1145 * You need to call this service function periodically to all the above
1146 * functions to happen; if your application is single-threaded you can
1147 * just call it in your main event loop.
1148 *
1149 * Alternatively you can fork a new process that asynchronously handles
1150 * calling this service in a loop. In that case you are happy if this
1151 * call blocks your thread until it needs to take care of something and
1152 * would call it with a large nonzero timeout. Your loop then takes no
1153 * CPU while there is nothing happening.
1154 *
1155 * If you are calling it in a single-threaded app, you don't want it to
1156 * wait around blocking other things in your loop from happening, so you
1157 * would call it with a timeout_ms of 0, so it returns immediately if
1158 * nothing is pending, or as soon as it services whatever was pending.
1159 */
1160
Andy Greenb45993c2010-12-18 15:13:50 +00001161
Andy Greene92cd172011-01-19 13:11:55 +00001162int
Peter Hinz56885f32011-03-02 22:03:47 +00001163libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001164{
1165 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001166
1167 /* stay dead once we are dead */
1168
Peter Hinz56885f32011-03-02 22:03:47 +00001169 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001170 return 1;
1171
Andy Green0d338332011-02-12 11:57:43 +00001172 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001173
Peter Hinz56885f32011-03-02 22:03:47 +00001174 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001175 if (n == 0) /* poll timeout */
1176 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001177
Andy Greendfb23042013-01-17 12:26:48 +08001178 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001179 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001180
Andy Greendfb23042013-01-17 12:26:48 +08001181 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001182
Peter Hinz56885f32011-03-02 22:03:47 +00001183 for (n = 0; n < context->fds_count; n++)
1184 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001185 if (libwebsocket_service_fd(context,
1186 &context->fds[n]) < 0)
1187 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001188 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001189}
1190
Andy Green3182ece2013-01-20 17:08:31 +08001191#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001192int
1193lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001194 struct libwebsocket *wsi,
1195 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001196 void *v, size_t len)
1197{
1198 int n;
1199 int handled = 0;
1200
1201 /* maybe an extension will take care of it for us */
1202
1203 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1204 if (!wsi->active_extensions[n]->callback)
1205 continue;
1206
1207 handled |= wsi->active_extensions[n]->callback(context,
1208 wsi->active_extensions[n], wsi,
1209 r, wsi->active_extensions_user[n], v, len);
1210 }
1211
1212 return handled;
1213}
1214
1215
1216void *
1217lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001218 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001219{
1220 int n = 0;
1221
Andy Green68b45042011-05-25 21:41:57 +01001222 if (wsi == NULL)
1223 return NULL;
1224
Andy Greena41314f2011-05-23 10:00:03 +01001225 while (n < wsi->count_active_extensions) {
1226 if (wsi->active_extensions[n] != ext) {
1227 n++;
1228 continue;
1229 }
1230 return wsi->active_extensions_user[n];
1231 }
1232
1233 return NULL;
1234}
Andy Green3182ece2013-01-20 17:08:31 +08001235#endif
Andy Greena41314f2011-05-23 10:00:03 +01001236
Andy Green90c7cbc2011-01-27 06:26:52 +00001237/**
1238 * libwebsocket_callback_on_writable() - Request a callback when this socket
1239 * becomes able to be written to without
1240 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001241 *
Peter Hinz56885f32011-03-02 22:03:47 +00001242 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001243 * @wsi: Websocket connection instance to get callback for
1244 */
1245
1246int
Peter Hinz56885f32011-03-02 22:03:47 +00001247libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001248 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001249{
Andy Green3182ece2013-01-20 17:08:31 +08001250#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001251 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001252 int handled = 0;
1253
1254 /* maybe an extension will take care of it for us */
1255
1256 for (n = 0; n < wsi->count_active_extensions; n++) {
1257 if (!wsi->active_extensions[n]->callback)
1258 continue;
1259
1260 handled |= wsi->active_extensions[n]->callback(context,
1261 wsi->active_extensions[n], wsi,
1262 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1263 wsi->active_extensions_user[n], NULL, 0);
1264 }
1265
1266 if (handled)
1267 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001268#endif
Andy Greendfb23042013-01-17 12:26:48 +08001269 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001270 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001271 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001272 return -1;
1273 }
1274
1275 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001276
Andy Green3221f922011-02-12 13:14:11 +00001277 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001278 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001279 LWS_CALLBACK_SET_MODE_POLL_FD,
1280 (void *)(long)wsi->sock, NULL, POLLOUT);
1281
Andy Green90c7cbc2011-01-27 06:26:52 +00001282 return 1;
1283}
1284
1285/**
1286 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1287 * all connections using the given protocol when it
1288 * becomes possible to write to each socket without
1289 * blocking in turn.
1290 *
1291 * @protocol: Protocol whose connections will get callbacks
1292 */
1293
1294int
1295libwebsocket_callback_on_writable_all_protocol(
1296 const struct libwebsocket_protocols *protocol)
1297{
Peter Hinz56885f32011-03-02 22:03:47 +00001298 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001299 int n;
Andy Green0d338332011-02-12 11:57:43 +00001300 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001301
Andy Greendfb23042013-01-17 12:26:48 +08001302 for (n = 0; n < context->fds_count; n++) {
1303 wsi = context->lws_lookup[context->fds[n].fd];
1304 if (!wsi)
1305 continue;
1306 if (wsi->protocol == protocol)
1307 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001308 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001309
1310 return 0;
1311}
1312
Andy Greenbe93fef2011-02-14 20:25:43 +00001313/**
1314 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1315 *
1316 * You will not need this unless you are doing something special
1317 *
1318 * @wsi: Websocket connection instance
1319 * @reason: timeout reason
1320 * @secs: how many seconds
1321 */
1322
1323void
1324libwebsocket_set_timeout(struct libwebsocket *wsi,
1325 enum pending_timeout reason, int secs)
1326{
1327 struct timeval tv;
1328
1329 gettimeofday(&tv, NULL);
1330
1331 wsi->pending_timeout_limit = tv.tv_sec + secs;
1332 wsi->pending_timeout = reason;
1333}
1334
Andy Greena6cbece2011-01-27 20:06:03 +00001335
1336/**
1337 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1338 *
1339 * You will not need this unless you are doing something special
1340 *
1341 * @wsi: Websocket connection instance
1342 */
1343
1344int
1345libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1346{
1347 return wsi->sock;
1348}
1349
Andy Greend636e352013-01-29 12:36:17 +08001350#ifdef LWS_LATENCY
1351void
1352lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completed)
1353{
1354 struct timeval tv;
1355 unsigned long u;
1356 char buf[256];
1357
1358 gettimeofday(&tv, NULL);
1359
1360 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1361
1362 if (action) {
1363 if (completed) {
1364 if (wsi->action_start == wsi->latency_start)
1365 sprintf(buf, "Completion first try lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1366 else
1367 sprintf(buf, "Completion %luus: lat %luus: %p: ret %d: %s\n", u - wsi->action_start, u - wsi->latency_start, (void *)wsi, ret, action);
1368 wsi->action_start = 0;
1369 } else
1370 sprintf(buf, "lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1371 if (u - wsi->latency_start > context->worst_latency) {
1372 context->worst_latency = u - wsi->latency_start;
1373 strcpy(context->worst_latency_info, buf);
1374 }
1375 lwsl_latency("%s", buf);
1376 } else {
1377 wsi->latency_start = u;
1378 if (!wsi->action_start)
1379 wsi->action_start = u;
1380 }
1381}
1382#endif
1383
Andy Greena1ce6be2013-01-18 11:43:21 +08001384#ifdef LWS_NO_SERVER
1385int
1386_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1387{
1388 return 0;
1389}
1390#else
Andy Green706961d2013-01-17 16:50:35 +08001391int
1392_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1393{
1394 struct libwebsocket_context *context = wsi->protocol->owning_server;
1395 int n;
1396
Andy Green623a98d2013-01-21 11:04:23 +08001397 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001398 return 0;
1399
Andy Green623a98d2013-01-21 11:04:23 +08001400 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001401
Andy Green623a98d2013-01-21 11:04:23 +08001402 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001403
1404 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001405 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001406 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1407 if (n < 0) {
Andy Greenaedc9532013-02-10 21:21:24 +08001408 lwsl_info("returning that we want to close connection at libwebsocket_rx_flow_control:\n");
Andy Green706961d2013-01-17 16:50:35 +08001409 return -1;
1410 }
1411 if (n)
1412 /* oh he stuck again, do nothing */
1413 return 0;
1414 }
1415
Andy Green623a98d2013-01-21 11:04:23 +08001416 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001417 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1418 else
1419 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1420
Andy Green623a98d2013-01-21 11:04:23 +08001421 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001422 /* external POLL support via protocol 0 */
1423 context->protocols[0].callback(context, wsi,
1424 LWS_CALLBACK_SET_MODE_POLL_FD,
1425 (void *)(long)wsi->sock, NULL, POLLIN);
1426 else
1427 /* external POLL support via protocol 0 */
1428 context->protocols[0].callback(context, wsi,
1429 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1430 (void *)(long)wsi->sock, NULL, POLLIN);
1431
1432 return 1;
1433}
Andy Greena1ce6be2013-01-18 11:43:21 +08001434#endif
Andy Green706961d2013-01-17 16:50:35 +08001435
Andy Green90c7cbc2011-01-27 06:26:52 +00001436/**
1437 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1438 * receieved packets.
1439 *
1440 * If the output side of a server process becomes choked, this allows flow
1441 * control for the input side.
1442 *
1443 * @wsi: Websocket connection instance to get callback for
1444 * @enable: 0 = disable read servicing for this connection, 1 = enable
1445 */
1446
1447int
1448libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1449{
Andy Green623a98d2013-01-21 11:04:23 +08001450 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001451
Andy Green706961d2013-01-17 16:50:35 +08001452 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001453}
1454
Andy Green706961d2013-01-17 16:50:35 +08001455
Andy Green2ac5a6f2011-01-28 10:00:18 +00001456/**
1457 * libwebsocket_canonical_hostname() - returns this host's hostname
1458 *
1459 * This is typically used by client code to fill in the host parameter
1460 * when making a client connection. You can only call it after the context
1461 * has been created.
1462 *
Peter Hinz56885f32011-03-02 22:03:47 +00001463 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001464 */
1465
1466
1467extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001468libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001469{
Peter Hinz56885f32011-03-02 22:03:47 +00001470 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001471}
1472
1473
Andy Green90c7cbc2011-01-27 06:26:52 +00001474static void sigpipe_handler(int x)
1475{
1476}
1477
Andy Green6901cb32011-02-21 08:06:47 +00001478#ifdef LWS_OPENSSL_SUPPORT
1479static int
1480OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1481{
1482
1483 SSL *ssl;
1484 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001485 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001486
1487 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1488 SSL_get_ex_data_X509_STORE_CTX_idx());
1489
1490 /*
Andy Green2e24da02011-03-05 16:12:04 +00001491 * !!! nasty openssl requires the index to come as a library-scope
1492 * static
Andy Green6901cb32011-02-21 08:06:47 +00001493 */
Andy Green2e24da02011-03-05 16:12:04 +00001494 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001495
Peter Hinz56885f32011-03-02 22:03:47 +00001496 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001497 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1498 x509_ctx, ssl, preverify_ok);
1499
1500 /* convert return code from 0 = OK to 1 = OK */
1501
1502 if (!n)
1503 n = 1;
1504 else
1505 n = 0;
1506
1507 return n;
1508}
1509#endif
1510
Andy Green706961d2013-01-17 16:50:35 +08001511int user_callback_handle_rxflow(callback_function callback_function,
1512 struct libwebsocket_context * context,
1513 struct libwebsocket *wsi,
1514 enum libwebsocket_callback_reasons reason, void *user,
1515 void *in, size_t len)
1516{
1517 int n;
1518
1519 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001520 if (!n)
1521 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001522
Andy Greenaedc9532013-02-10 21:21:24 +08001523 return n;
Andy Green706961d2013-01-17 16:50:35 +08001524}
1525
Andy Greenb45993c2010-12-18 15:13:50 +00001526
Andy Greenab990e42010-10-31 12:42:52 +00001527/**
Andy Green4739e5c2011-01-22 12:51:57 +00001528 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001529 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001530 *
Andy Green1b265272013-02-09 14:01:09 +08001531 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001532 * of all initialization in one step.
1533 *
Andy Greene92cd172011-01-19 13:11:55 +00001534 * After initialization, it returns a struct libwebsocket_context * that
1535 * represents this server. After calling, user code needs to take care
1536 * of calling libwebsocket_service() with the context pointer to get the
1537 * server's sockets serviced. This can be done in the same process context
1538 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001539 *
Andy Green8f037e42010-12-19 22:13:26 +00001540 * The protocol callback functions are called for a handful of events
1541 * including http requests coming in, websocket connections becoming
1542 * established, and data arriving; it's also called periodically to allow
1543 * async transmission.
1544 *
1545 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1546 * at that time websocket protocol has not been negotiated. Other
1547 * protocols after the first one never see any HTTP callack activity.
1548 *
1549 * The server created is a simple http server by default; part of the
1550 * websocket standard is upgrading this http connection to a websocket one.
1551 *
1552 * This allows the same server to provide files like scripts and favicon /
1553 * images or whatever over http and dynamic data over websockets all in
1554 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001555 */
Andy Green4ea60062010-10-30 12:15:07 +01001556
Andy Greene92cd172011-01-19 13:11:55 +00001557struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001558libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001559{
1560 int n;
Peter Hinz56885f32011-03-02 22:03:47 +00001561 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001562 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001563#ifndef LWS_NO_SERVER
1564 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001565 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001566 struct sockaddr_in serv_addr;
1567#endif
Andy Green3182ece2013-01-20 17:08:31 +08001568#ifndef LWS_NO_EXTENSIONS
1569 int m;
Andy Green1b265272013-02-09 14:01:09 +08001570 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001571#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001572
Andy Green3faa9c72010-11-08 17:03:03 +00001573#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001574 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001575#endif
1576
Joakim Soderberg4c531232013-02-06 15:26:58 +09001577#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001578 extern int get_daemonize_pid();
1579 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001580#endif
1581
Andy Greenb3a614a2013-01-19 13:08:17 +08001582 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001583 lwsl_notice("Library version: %s\n", library_version);
Andy Greenc0d6b632013-01-12 23:42:17 +08001584 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1585 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001586 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001587#ifndef LWS_NO_EXTENSIONS
Andy Greenc0d6b632013-01-12 23:42:17 +08001588 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001589#else
1590 lwsl_notice(" Configured without extension support\n");
1591#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001592 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1593 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1594 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1595 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1596 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001597
Peter Hinz56885f32011-03-02 22:03:47 +00001598#ifdef _WIN32
1599 {
1600 WORD wVersionRequested;
1601 WSADATA wsaData;
1602 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001603 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001604
1605 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1606 wVersionRequested = MAKEWORD(2, 2);
1607
1608 err = WSAStartup(wVersionRequested, &wsaData);
1609 if (err != 0) {
1610 /* Tell the user that we could not find a usable */
1611 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001612 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001613 return NULL;
1614 }
David Galeano7b11fec2011-10-04 19:55:18 +08001615
Andy Green6ee372f2012-04-09 15:09:01 +08001616 /* default to a poll() made out of select() */
1617 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001618
Andy Green6ee372f2012-04-09 15:09:01 +08001619 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001620 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001621 if (wsdll)
1622 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001623
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001624 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001625 if (!poll)
1626 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001627 }
1628#endif
1629
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001630 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001631 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001632 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001633 return NULL;
1634 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001635 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001636#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001637 context->started_with_parent = pid_daemon;
1638 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1639#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +09001640
1641 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001642 context->protocols = info->protocols;
1643 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001644 context->http_proxy_port = 0;
1645 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001646 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001647 /* to reduce this allocation, */
1648 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001649 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1650 sizeof(struct libwebsocket_context),
1651 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1652 context->max_fds,
1653 sizeof(struct libwebsocket_context) + ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) * context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001654
1655 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1656 if (context->fds == NULL) {
1657 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1658 free(context);
1659 return NULL;
1660 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08001661 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001662 if (context->lws_lookup == NULL) {
1663 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1664 free(context->fds);
1665 free(context);
1666 return NULL;
1667 }
Andy Greena17c6922013-01-20 20:21:54 +08001668
Peter Hinz56885f32011-03-02 22:03:47 +00001669 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001670#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001671 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001672#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001673 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001674 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001675
Peter Hinz56885f32011-03-02 22:03:47 +00001676#ifdef WIN32
1677 context->fd_random = 0;
1678#else
1679 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1680 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001681 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001682 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001683 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001684 }
Peter Hinz56885f32011-03-02 22:03:47 +00001685#endif
Andy Green44eee682011-02-10 09:32:24 +00001686
Peter Hinz56885f32011-03-02 22:03:47 +00001687#ifdef LWS_OPENSSL_SUPPORT
1688 context->use_ssl = 0;
1689 context->ssl_ctx = NULL;
1690 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001691 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001692#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001693
Andy Greena1ce6be2013-01-18 11:43:21 +08001694 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001695
Andy Greena1ce6be2013-01-18 11:43:21 +08001696#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001697 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001698 struct sockaddr sa;
Andy Greene310b0c2013-02-10 15:10:10 +08001699 context->service_buffer[0] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001700
1701 /* find canonical hostname */
1702
Andy Greene310b0c2013-02-10 15:10:10 +08001703 context->service_buffer[(sizeof context->service_buffer) - 1] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001704 memset(&sa, 0, sizeof(sa));
1705 sa.sa_family = AF_INET;
1706 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
Andy Greene310b0c2013-02-10 15:10:10 +08001707 gethostname((char *)context->service_buffer, (sizeof context->service_buffer) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001708
1709 n = 0;
1710
Andy Greene310b0c2013-02-10 15:10:10 +08001711 if (strlen((char *)context->service_buffer) < sizeof(sa.sa_data) - 1) {
1712 strcpy(sa.sa_data, (char *)context->service_buffer);
Andy Greend09d7d42013-01-31 10:05:43 +08001713 lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Greene310b0c2013-02-10 15:10:10 +08001714 n = getnameinfo(&sa, sizeof(sa), (char *)context->service_buffer,
1715 (sizeof context->service_buffer) - 1, NULL, 0, NI_NAMEREQD);
Andy Green788c4a82012-10-22 12:29:57 +01001716 }
1717
1718 if (!n) {
Andy Greene310b0c2013-02-10 15:10:10 +08001719 strncpy(context->canonical_hostname, (char *)context->service_buffer,
Andy Green788c4a82012-10-22 12:29:57 +01001720 sizeof context->canonical_hostname - 1);
1721 context->canonical_hostname[
1722 sizeof context->canonical_hostname - 1] = '\0';
1723 } else
Andy Greene310b0c2013-02-10 15:10:10 +08001724 strncpy(context->canonical_hostname, (char *)context->service_buffer,
Andy Green788c4a82012-10-22 12:29:57 +01001725 sizeof context->canonical_hostname - 1);
1726
Andy Greenb3a614a2013-01-19 13:08:17 +08001727 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001728 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001729#endif
Andy Greena69f0512012-05-03 12:32:38 +08001730
Andy Green9659f372011-01-27 22:01:43 +00001731 /* split the proxy ads:port if given */
1732
1733 p = getenv("http_proxy");
1734 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001735 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001736 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001737 context->http_proxy_address[
1738 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001739
Peter Hinz56885f32011-03-02 22:03:47 +00001740 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001741 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001742 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001743 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001744 }
1745 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001746 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001747
Andy Greenb3a614a2013-01-19 13:08:17 +08001748 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001749 context->http_proxy_address,
1750 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001751 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001752
Andy Greena1ce6be2013-01-18 11:43:21 +08001753#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001754 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001755
Andy Green3faa9c72010-11-08 17:03:03 +00001756#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001757 context->use_ssl = info->ssl_cert_filepath != NULL &&
1758 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001759#ifdef USE_CYASSL
1760 lwsl_notice(" Compiled with CYASSL support\n");
1761#else
1762 lwsl_notice(" Compiled with OpenSSL support\n");
1763#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001764 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001765 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001766 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001767 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001768
Andy Green90c7cbc2011-01-27 06:26:52 +00001769#else
Andy Green2b40b792013-02-10 22:22:01 +08001770 if (info->ssl_cert_filepath != NULL &&
1771 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001772 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001773 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001774 }
Andy Greenb3a614a2013-01-19 13:08:17 +08001775 lwsl_notice(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001776 "serving unencrypted\n");
1777#endif
Andy Greena17c6922013-01-20 20:21:54 +08001778
Andy Green16ab3182013-02-10 18:02:31 +08001779 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 +00001780 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001781#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001782
1783 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001784#ifdef WIN32
1785#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001786 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001787#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001788
1789
1790#ifdef LWS_OPENSSL_SUPPORT
1791
1792 /* basic openssl init */
1793
1794 SSL_library_init();
1795
1796 OpenSSL_add_all_algorithms();
1797 SSL_load_error_strings();
1798
Andy Green2e24da02011-03-05 16:12:04 +00001799 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001800 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1801
Andy Green90c7cbc2011-01-27 06:26:52 +00001802 /*
1803 * Firefox insists on SSLv23 not SSLv3
1804 * Konq disables SSLv2 by default now, SSLv23 works
1805 */
1806
1807 method = (SSL_METHOD *)SSLv23_server_method();
1808 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001809 lwsl_err("problem creating ssl method: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001810 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001811 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001812 }
Peter Hinz56885f32011-03-02 22:03:47 +00001813 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1814 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001815 lwsl_err("problem creating ssl context: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001816 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001817 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001818 }
1819
David Galeanocc148e42013-01-10 10:18:59 +08001820#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001821 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001822#endif
David Galeano77a677c2013-01-10 10:14:12 +08001823 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001824 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001825
Andy Greena1ce6be2013-01-18 11:43:21 +08001826#ifndef LWS_NO_CLIENT
1827
Andy Green90c7cbc2011-01-27 06:26:52 +00001828 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001829
Andy Green1b265272013-02-09 14:01:09 +08001830 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001831 method = (SSL_METHOD *)SSLv23_client_method();
1832 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001833 lwsl_err("problem creating ssl method: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001834 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001835 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001836 }
1837 /* create context */
1838 context->ssl_client_ctx = SSL_CTX_new(method);
1839 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001840 lwsl_err("problem creating ssl context: %s\n",
Andy Greene310b0c2013-02-10 15:10:10 +08001841 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001842 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001843 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001844
David Galeanocc148e42013-01-10 10:18:59 +08001845#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001846 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001847#endif
David Galeano77a677c2013-01-10 10:14:12 +08001848 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001849 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001850
Peter Hinz56885f32011-03-02 22:03:47 +00001851 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001852 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001853 if (!SSL_CTX_load_verify_locations(
1854 context->ssl_client_ctx, NULL,
1855 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001856 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001857 "Unable to load SSL Client certs from %s "
1858 "(set by --with-client-cert-dir= in configure) -- "
1859 " client ssl isn't going to work",
1860 LWS_OPENSSL_CLIENT_CERTS);
1861 } else
1862 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001863 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001864 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001865 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001866 "Unable to load SSL Client certs "
1867 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001868 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001869
1870 /*
1871 * callback allowing user code to load extra verification certs
1872 * helping the client to verify server identity
1873 */
1874
1875 context->protocols[0].callback(context, NULL,
1876 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1877 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001878 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001879#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001880
Andy Greenc6bf2c22011-02-20 11:10:47 +00001881 /* as a server, are we requiring clients to identify themselves? */
1882
Andy Green1b265272013-02-09 14:01:09 +08001883 if (info->options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001884
1885 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001886
Peter Hinz56885f32011-03-02 22:03:47 +00001887 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001888 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1889 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001890
1891 /*
1892 * give user code a chance to load certs into the server
1893 * allowing it to verify incoming client certs
1894 */
1895
Peter Hinz56885f32011-03-02 22:03:47 +00001896 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001897 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001898 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001899 }
1900
Peter Hinz56885f32011-03-02 22:03:47 +00001901 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001902
1903 /* openssl init for server sockets */
1904
Andy Green3faa9c72010-11-08 17:03:03 +00001905 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001906 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001907 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001908 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001909 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001910 info->ssl_cert_filepath,
Andy Greene310b0c2013-02-10 15:10:10 +08001911 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001912 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001913 }
1914 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001915 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001916 info->ssl_private_key_filepath,
1917 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001918 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001919 info->ssl_private_key_filepath,
Andy Greene310b0c2013-02-10 15:10:10 +08001920 ERR_error_string(ERR_get_error(), (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001921 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001922 }
1923 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001924 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001925 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001926 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001927 }
1928
1929 /* SSL is happy and has a cert it's content with */
1930 }
1931#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001932
Andy Greendf736162011-01-18 15:39:02 +00001933 /* selftest */
1934
1935 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001936 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00001937
Andy Greena1ce6be2013-01-18 11:43:21 +08001938#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001939 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001940
Andy Green1b265272013-02-09 14:01:09 +08001941 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001942 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1943 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001944
Andy Green4739e5c2011-01-22 12:51:57 +00001945 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1946 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001947 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001948 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001949 }
Andy Green775c0dd2010-10-29 14:15:22 +01001950
Joakim Soderberg4c531232013-02-06 15:26:58 +09001951#ifndef WIN32
1952 /* allow us to restart even if old sockets in TIME_WAIT
1953 * (REUSEADDR on Unix means, "don't hang on to this address after the
1954 * listener is closed." On Windows, though, it means "don't keep other
1955 * processes from binding to this address while we're using it) */
Andy Green6ee372f2012-04-09 15:09:01 +08001956 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1957 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09001958#endif
Andy Green6c939552011-03-08 08:56:57 +00001959
1960 /* Disable Nagle */
1961 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001962 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1963 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001964
Joakim Soderberg4c531232013-02-06 15:26:58 +09001965 #ifdef WIN32
1966 opt = 0;
1967 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt );
1968 #else
Andy Greene2160712013-01-28 12:19:10 +08001969 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09001970 #endif
Andy Greene2160712013-01-28 12:19:10 +08001971
Andy Green4739e5c2011-01-22 12:51:57 +00001972 bzero((char *) &serv_addr, sizeof(serv_addr));
1973 serv_addr.sin_family = AF_INET;
Andy Green1b265272013-02-09 14:01:09 +08001974 if (info->interface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001975 serv_addr.sin_addr.s_addr = INADDR_ANY;
1976 else
Andy Green1b265272013-02-09 14:01:09 +08001977 interface_to_sa(info->interface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001978 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08001979 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00001980
1981 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1982 sizeof(serv_addr));
1983 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001984 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08001985 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001986 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001987 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001988 }
Andy Green0d338332011-02-12 11:57:43 +00001989
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001990 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001991 if (wsi == NULL) {
1992 lwsl_err("Out of mem\n");
1993 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001994 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08001995 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001996 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001997 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08001998#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001999 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002000#endif
Andy Green0d338332011-02-12 11:57:43 +00002001 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002002
2003 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002004
Andy Green65b0e912013-01-16 07:59:47 +08002005 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2006 context->listen_service_count = 0;
2007 context->listen_service_fd = sockfd;
2008
Andy Greena824d182013-01-15 20:52:29 +08002009 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002010 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002011 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002012#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002013
Andy Green6ee372f2012-04-09 15:09:01 +08002014 /*
2015 * drop any root privs for this process
2016 * to listen on port < 1023 we would have needed root, but now we are
2017 * listening, we don't want the power for anything else
2018 */
Peter Hinz56885f32011-03-02 22:03:47 +00002019#ifdef WIN32
2020#else
Andy Green1b265272013-02-09 14:01:09 +08002021 if (info->gid != -1)
2022 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002023 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002024 if (info->uid != -1)
2025 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002026 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002027#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002028
Andy Green6f520a52013-01-29 17:57:39 +08002029 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002030
Peter Hinz56885f32011-03-02 22:03:47 +00002031 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002032 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002033 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002034
Andy Green43db0452013-01-10 19:50:35 +08002035 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002036 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002037
Andy Green1b265272013-02-09 14:01:09 +08002038 info->protocols[context->count_protocols].owning_server =
2039 context;
2040 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002041 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002042
2043 /*
2044 * inform all the protocols that they are doing their one-time
2045 * initialization if they want to
2046 */
2047 info->protocols[context->count_protocols].callback(context,
2048 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002049 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002050
Andy Green3182ece2013-01-20 17:08:31 +08002051#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002052 /*
2053 * give all extensions a chance to create any per-context
2054 * allocations they need
2055 */
2056
2057 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002058 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002059 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08002060
Andy Green1b265272013-02-09 14:01:09 +08002061 if (info->extensions) {
2062 ext = info->extensions;
2063 while (ext->callback) {
2064 lwsl_ext(" Extension: %s\n", ext->name);
2065 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002066 (enum libwebsocket_extension_callback_reasons)m,
2067 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002068 ext++;
2069 }
Andy Greena41314f2011-05-23 10:00:03 +01002070 }
Andy Green3182ece2013-01-20 17:08:31 +08002071#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002072 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002073
2074bail:
2075 libwebsocket_context_destroy(context);
2076 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002077}
Andy Greenb45993c2010-12-18 15:13:50 +00002078
Andy Greenb45993c2010-12-18 15:13:50 +00002079/**
2080 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002081 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002082 * @wsi: pointer to struct websocket you want to know the protocol of
2083 *
Andy Green8f037e42010-12-19 22:13:26 +00002084 *
Andy Green6f520a52013-01-29 17:57:39 +08002085 * Some apis can act on all live connections of a given protocol,
2086 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002087 */
Andy Greenab990e42010-10-31 12:42:52 +00002088
Andy Greenb45993c2010-12-18 15:13:50 +00002089const struct libwebsocket_protocols *
2090libwebsockets_get_protocol(struct libwebsocket *wsi)
2091{
2092 return wsi->protocol;
2093}
2094
Andy Green82c3d542011-03-07 21:16:31 +00002095int
2096libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2097{
Andy Green623a98d2013-01-21 11:04:23 +08002098 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002099}
Alex Bligh49146db2011-11-07 17:19:25 +08002100
David Galeanoe2cf9922013-01-09 18:06:55 +08002101unsigned char
2102libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2103{
Andy Green623a98d2013-01-21 11:04:23 +08002104 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002105}
2106
Alex Bligh49146db2011-11-07 17:19:25 +08002107void *
2108libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2109{
2110 /* allocate the per-connection user memory (if any) */
2111
2112 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2113 wsi->user_space = malloc(
2114 wsi->protocol->per_session_data_size);
2115 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002116 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002117 return NULL;
2118 }
Andy Green6ee372f2012-04-09 15:09:01 +08002119 memset(wsi->user_space, 0,
2120 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002121 }
2122 return wsi->user_space;
2123}
Andy Green43db0452013-01-10 19:50:35 +08002124
Andy Green0b319092013-01-19 11:17:56 +08002125static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002126{
Andy Green0b319092013-01-19 11:17:56 +08002127 char buf[300];
2128 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002129 int n;
2130
2131 gettimeofday(&tv, NULL);
2132
2133 buf[0] = '\0';
2134 for (n = 0; n < LLL_COUNT; n++)
2135 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002136 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Green0b319092013-01-19 11:17:56 +08002137 (int)(tv.tv_usec / 100), log_level_names[n]);
2138 break;
2139 }
2140
2141 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002142}
2143
Joakim Soderberg4c531232013-02-06 15:26:58 +09002144#ifdef WIN32
2145void lwsl_emit_syslog(int level, const char *line)
2146{
2147 lwsl_emit_stderr(level, line);
2148}
2149#else
Andy Greenc11db202013-01-19 11:12:16 +08002150void lwsl_emit_syslog(int level, const char *line)
2151{
2152 int syslog_level = LOG_DEBUG;
2153
2154 switch (level) {
2155 case LLL_ERR:
2156 syslog_level = LOG_ERR;
2157 break;
2158 case LLL_WARN:
2159 syslog_level = LOG_WARNING;
2160 break;
2161 case LLL_NOTICE:
2162 syslog_level = LOG_NOTICE;
2163 break;
2164 case LLL_INFO:
2165 syslog_level = LOG_INFO;
2166 break;
2167 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002168 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002169}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002170#endif
Andy Greenc11db202013-01-19 11:12:16 +08002171
Andy Green43db0452013-01-10 19:50:35 +08002172void _lws_log(int filter, const char *format, ...)
2173{
Andy Greende8f27a2013-01-12 09:17:42 +08002174 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002175 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002176
2177 if (!(log_level & filter))
2178 return;
2179
Andy Green43db0452013-01-10 19:50:35 +08002180 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08002181 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08002182 buf[(sizeof buf) - 1] = '\0';
2183 va_end(ap);
2184
Andy Green0b319092013-01-19 11:17:56 +08002185 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002186}
2187
2188/**
2189 * lws_set_log_level() - Set the logging bitfield
2190 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002191 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2192 * function to perform log string emission instead of
2193 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002194 *
Andy Greende8f27a2013-01-12 09:17:42 +08002195 * log level defaults to "err" and "warn" contexts enabled only and
2196 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002197 */
2198
Andy Green058ba812013-01-19 11:32:18 +08002199void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002200{
2201 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002202 if (log_emit_function)
2203 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002204}