blob: 0291acbbffa79451ae327025f1016cfd42795308 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Peter Hinz56885f32011-03-02 22:03:47 +000024#ifdef WIN32
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010027#include <mstcpip.h>
Peter Hinz56885f32011-03-02 22:03:47 +000028#else
Davidc4ef7b12013-01-12 20:39:47 +080029#ifdef LWS_BUILTIN_GETIFADDRS
30#include <getifaddrs.h>
31#else
Peter Hinz56885f32011-03-02 22:03:47 +000032#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080033#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090034#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000035#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080036#include <sys/socket.h>
37#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000038#endif
Andy Green2e24da02011-03-05 16:12:04 +000039
40#ifdef LWS_OPENSSL_SUPPORT
41int openssl_websocket_private_data_index;
42#endif
43
Andy Greenaa6fc442012-04-12 13:26:49 +080044#ifdef __MINGW32__
45#include "../win32port/win32helpers/websock-w32.c"
46#else
47#ifdef __MINGW64__
48#include "../win32port/win32helpers/websock-w32.c"
49#endif
50#endif
51
Andy Green7b405452013-02-01 10:50:15 +080052#ifndef LWS_BUILD_HASH
53#define LWS_BUILD_HASH "unknown-build-hash"
54#endif
Andy Green3182ece2013-01-20 17:08:31 +080055
Andy Green7c19c342013-01-19 12:18:07 +080056static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080057static void lwsl_emit_stderr(int level, const char *line);
58static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
59
Andy Green7b405452013-02-01 10:50:15 +080060static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
61
Andy Greenb5b23192013-02-11 17:13:32 +080062static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080063 "ERR",
64 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080065 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080066 "INFO",
67 "DEBUG",
68 "PARSER",
69 "HEADER",
70 "EXTENSION",
71 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080072 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080073};
74
Andy Greenb5b23192013-02-11 17:13:32 +080075#ifndef LWS_NO_CLIENT
76 extern int lws_client_socket_service(
77 struct libwebsocket_context *context,
78 struct libwebsocket *wsi, struct pollfd *pollfd);
79#endif
80#ifndef LWS_NO_SERVER
81 extern int lws_server_socket_service(
82 struct libwebsocket_context *context,
83 struct libwebsocket *wsi, struct pollfd *pollfd);
84#endif
85
Andy Green7b405452013-02-01 10:50:15 +080086/**
87 * lws_get_library_version: get version and git hash library built from
88 *
89 * returns a const char * to a string like "1.1 178d78c"
90 * representing the library version followed by the git head hash it
91 * was built from
92 */
93
94const char *
95lws_get_library_version(void)
96{
97 return library_version;
98}
99
Andy Green0d338332011-02-12 11:57:43 +0000100int
Andy Greenb5b23192013-02-11 17:13:32 +0800101insert_wsi_socket_into_fds(struct libwebsocket_context *context,
102 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000103{
Andy Greendfb23042013-01-17 12:26:48 +0800104 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800105 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000106 return 1;
107 }
108
Andy Greendfb23042013-01-17 12:26:48 +0800109 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800110 lwsl_err("Socket fd %d is too high (%d)\n",
111 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800112 return 1;
113 }
114
115 assert(wsi);
116 assert(wsi->sock);
117
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
119 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800120
121 context->lws_lookup[wsi->sock] = wsi;
122 wsi->position_in_fds_table = context->fds_count;
123 context->fds[context->fds_count].fd = wsi->sock;
124 context->fds[context->fds_count].events = POLLIN;
125 context->fds[context->fds_count++].revents = 0;
126
127 /* external POLL support via protocol 0 */
128 context->protocols[0].callback(context, wsi,
129 LWS_CALLBACK_ADD_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800130 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000131
132 return 0;
133}
134
Andy Greendfb23042013-01-17 12:26:48 +0800135static int
Andy Greenb5b23192013-02-11 17:13:32 +0800136remove_wsi_socket_from_fds(struct libwebsocket_context *context,
137 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000138{
Andy Greendfb23042013-01-17 12:26:48 +0800139 int m;
Andy Green0d338332011-02-12 11:57:43 +0000140
Andy Greendfb23042013-01-17 12:26:48 +0800141 if (!--context->fds_count)
142 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000143
Andy Greendfb23042013-01-17 12:26:48 +0800144 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800145 lwsl_err("Socket fd %d too high (%d)\n",
146 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800147 return 1;
148 }
Andy Green0d338332011-02-12 11:57:43 +0000149
Andy Greenb5b23192013-02-11 17:13:32 +0800150 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
151 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800152
153 m = wsi->position_in_fds_table; /* replace the contents for this */
154
155 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800156 context->fds[m] = context->fds[context->fds_count];
157 /*
158 * end guy's fds_lookup entry remains unchanged
159 * (still same fd pointing to same wsi)
160 */
Andy Greendfb23042013-01-17 12:26:48 +0800161 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800162 context->lws_lookup[context->fds[context->fds_count].fd]->
163 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800164 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800165 context->lws_lookup[wsi->sock] = NULL;
166 /* removed wsi has no position any more */
167 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800168
169do_ext:
170 /* remove also from external POLL support via protocol 0 */
171 if (wsi->sock)
172 context->protocols[0].callback(context, wsi,
Andy Green50097dd2013-02-15 22:36:30 +0800173 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
174 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800175
176 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000177}
178
Andy Green32375b72011-02-19 08:32:53 +0000179
Andy Green8f037e42010-12-19 22:13:26 +0000180void
Peter Hinz56885f32011-03-02 22:03:47 +0000181libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000182 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000183{
Andy Greenb45993c2010-12-18 15:13:50 +0000184 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000185 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000186 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
187 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800188#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000189 int ret;
190 int m;
191 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100192 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800193#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000194
Andy Green4b6fbe12011-02-14 08:03:48 +0000195 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000196 return;
197
Andy Green62c54d22011-02-14 09:14:25 +0000198 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000199
Andy Green62c54d22011-02-14 09:14:25 +0000200 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000201 return;
202
Andy Green22524a62013-02-15 22:48:58 +0800203 /* we tried the polite way... */
204 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
205 goto just_kill_connection;
206
Andy Green623a98d2013-01-21 11:04:23 +0800207 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000208
Andy Greenb8b247d2013-01-22 07:20:08 +0800209 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
210 close(wsi->u.http.fd);
211 wsi->u.http.fd = 0;
212 }
213
Andy Green3182ece2013-01-20 17:08:31 +0800214#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000215 /*
Andy Green68b45042011-05-25 21:41:57 +0100216 * are his extensions okay with him closing? Eg he might be a mux
217 * parent and just his ch1 aspect is closing?
218 */
219
Andy Green68b45042011-05-25 21:41:57 +0100220 for (n = 0; n < wsi->count_active_extensions; n++) {
221 if (!wsi->active_extensions[n]->callback)
222 continue;
223
224 m = wsi->active_extensions[n]->callback(context,
225 wsi->active_extensions[n], wsi,
226 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
227 wsi->active_extensions_user[n], NULL, 0);
228
229 /*
230 * if somebody vetoed actually closing him at this time....
231 * up to the extension to track the attempted close, let's
232 * just bail
233 */
234
235 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800236 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100237 return;
238 }
239 }
240
Andy Green68b45042011-05-25 21:41:57 +0100241 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000242 * flush any tx pending from extensions, since we may send close packet
243 * if there are problems with send, just nuke the connection
244 */
245
246 ret = 1;
247 while (ret == 1) {
248
249 /* default to nobody has more to spill */
250
251 ret = 0;
252 eff_buf.token = NULL;
253 eff_buf.token_len = 0;
254
255 /* show every extension the new incoming data */
256
257 for (n = 0; n < wsi->count_active_extensions; n++) {
258 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000259 wsi->protocol->owning_server,
260 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000261 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
262 wsi->active_extensions_user[n], &eff_buf, 0);
263 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800264 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000265 goto just_kill_connection;
266 }
267 if (m)
268 /*
269 * at least one extension told us he has more
270 * to spill, so we will go around again after
271 */
272 ret = 1;
273 }
274
275 /* assuming they left us something to send, send it */
276
277 if (eff_buf.token_len)
278 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenb5b23192013-02-11 17:13:32 +0800279 eff_buf.token_len)) {
280 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000281 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800282 }
Andy Greenc44159f2011-03-07 07:08:18 +0000283 }
Andy Green3182ece2013-01-20 17:08:31 +0800284#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000285
286 /*
Andy Greenda527df2011-03-07 07:08:12 +0000287 * signal we are closing, libsocket_write will
288 * add any necessary version-specific stuff. If the write fails,
289 * no worries we are closing anyway. If we didn't initiate this
290 * close, then our state has been changed to
291 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
292 *
293 * Likewise if it's a second call to close this connection after we
294 * sent the close indication to the peer already, we are in state
295 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
296 */
297
298 if (old_state == WSI_STATE_ESTABLISHED &&
299 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100300
Andy Green43db0452013-01-10 19:50:35 +0800301 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100302
Andy Greenfdd305a2013-02-11 14:32:48 +0800303 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800304 memset(buf, 0, sizeof(buf));
305 n = libwebsocket_write(wsi,
306 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000307 0, LWS_WRITE_CLOSE);
308 if (!n) {
309 /*
310 * we have sent a nice protocol level indication we
311 * now wish to close, we should not send anything more
312 */
313
314 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
315
Andy Greenb5b23192013-02-11 17:13:32 +0800316 /*
317 * ...and we should wait for a reply for a bit
318 * out of politeness
319 */
Andy Greenda527df2011-03-07 07:08:12 +0000320
321 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800322 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000323
Andy Green43db0452013-01-10 19:50:35 +0800324 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000325
326 return;
327 }
328
Andy Greenb5b23192013-02-11 17:13:32 +0800329 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800330
Andy Greenda527df2011-03-07 07:08:12 +0000331 /* else, the send failed and we should just hang up */
332 }
333
Andy Greenc44159f2011-03-07 07:08:18 +0000334just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100335
Andy Greenb5b23192013-02-11 17:13:32 +0800336 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100337
Andy Greenda527df2011-03-07 07:08:12 +0000338 /*
339 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800340 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000341 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000342
Andy Greendfb23042013-01-17 12:26:48 +0800343 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000344
Andy Green251f6fa2010-11-03 11:13:06 +0000345 wsi->state = WSI_STATE_DEAD_SOCKET;
346
Andy Greenb5b23192013-02-11 17:13:32 +0800347 if ((old_state == WSI_STATE_ESTABLISHED ||
348 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800349 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
350
351 if (wsi->u.ws.rx_user_buffer) {
352 free(wsi->u.ws.rx_user_buffer);
353 wsi->u.ws.rx_user_buffer = NULL;
354 }
355 if (wsi->u.ws.rxflow_buffer) {
356 free(wsi->u.ws.rxflow_buffer);
357 wsi->u.ws.rxflow_buffer = NULL;
358 }
Andy Green54495112013-02-06 21:10:16 +0900359 }
360
Andy Green4b6fbe12011-02-14 08:03:48 +0000361 /* tell the user it's all over for this guy */
362
Andy Greend4302732011-02-28 07:45:29 +0000363 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800364 ((old_state == WSI_STATE_ESTABLISHED) ||
365 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
366 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800367 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000368 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000369 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800370 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800371 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000372
Andy Green3182ece2013-01-20 17:08:31 +0800373#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000374 /* deallocate any active extension contexts */
375
376 for (n = 0; n < wsi->count_active_extensions; n++) {
377 if (!wsi->active_extensions[n]->callback)
378 continue;
379
Andy Green46c2ea02011-03-22 09:04:01 +0000380 wsi->active_extensions[n]->callback(context,
381 wsi->active_extensions[n], wsi,
382 LWS_EXT_CALLBACK_DESTROY,
383 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000384
385 free(wsi->active_extensions_user[n]);
386 }
387
Andy Greena41314f2011-05-23 10:00:03 +0100388 /*
389 * inform all extensions in case they tracked this guy out of band
390 * even though not active on him specifically
391 */
392
393 ext = context->extensions;
394 while (ext && ext->callback) {
395 ext->callback(context, ext, wsi,
396 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
397 NULL, NULL, 0);
398 ext++;
399 }
Andy Green3182ece2013-01-20 17:08:31 +0800400#endif
Andy Greena41314f2011-05-23 10:00:03 +0100401
Andy Green43db0452013-01-10 19:50:35 +0800402/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000403
Andy Green3faa9c72010-11-08 17:03:03 +0000404#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000405 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000406 n = SSL_get_fd(wsi->ssl);
407 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800408 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000409 SSL_free(wsi->ssl);
410 } else {
411#endif
Andy Green0303db42013-01-17 14:46:43 +0800412 if (wsi->sock) {
413 n = shutdown(wsi->sock, SHUT_RDWR);
414 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800415 lwsl_debug("closing: shutdown returned %d\n",
416 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800417
Andy Green0303db42013-01-17 14:46:43 +0800418 n = compatible_close(wsi->sock);
419 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800420 lwsl_debug("closing: close returned %d\n",
421 errno);
Andy Green0303db42013-01-17 14:46:43 +0800422 }
Andy Green3faa9c72010-11-08 17:03:03 +0000423#ifdef LWS_OPENSSL_SUPPORT
424 }
425#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800426 if (wsi->protocol && wsi->protocol->per_session_data_size &&
427 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000428 free(wsi->user_space);
429
Andy Green251f6fa2010-11-03 11:13:06 +0000430 free(wsi);
431}
432
Andy Green07034092011-02-13 08:37:12 +0000433/**
434 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800435 * @context: Libwebsockets context
436 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000437 * @fd: Connection socket descriptor
438 * @name: Buffer to take client address name
439 * @name_len: Length of client address name buffer
440 * @rip: Buffer to take client address IP qotted quad
441 * @rip_len: Length of client address IP buffer
442 *
443 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800444 * the client connected with socket descriptor @fd. Names may be
445 * truncated if there is not enough room. If either cannot be
446 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000447 */
448
449void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800450libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
451 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000452 char *rip, int rip_len)
453{
454 unsigned int len;
455 struct sockaddr_in sin;
456 struct hostent *host;
457 struct hostent *host1;
458 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000459 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000460 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800461 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800462#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800463 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800464#endif
Andy Green07034092011-02-13 08:37:12 +0000465
466 rip[0] = '\0';
467 name[0] = '\0';
468
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800469 lws_latency_pre(context, wsi);
470
Andy Greenb5b23192013-02-11 17:13:32 +0800471 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000472 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
473 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800474 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000475 }
Andy Green6ee372f2012-04-09 15:09:01 +0800476
Andy Greenb5b23192013-02-11 17:13:32 +0800477 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000478 AF_INET);
479 if (host == NULL) {
480 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800481 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000482 }
483
484 strncpy(name, host->h_name, name_len);
485 name[name_len - 1] = '\0';
486
487 host1 = gethostbyname(host->h_name);
488 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800489 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000490 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000491 n = 0;
492 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000493 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000494 if (p == NULL)
495 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000496 if ((host1->h_addrtype != AF_INET)
497#ifdef AF_LOCAL
498 && (host1->h_addrtype != AF_LOCAL)
499#endif
500 )
Andy Green07034092011-02-13 08:37:12 +0000501 continue;
502
Andy Green7627af52011-03-09 15:13:52 +0000503 if (host1->h_addrtype == AF_INET)
504 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000505#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000506 else {
507 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800508 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000509 ip[sizeof(ip) - 1] = '\0';
510 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000511#endif
Andy Green07034092011-02-13 08:37:12 +0000512 p = NULL;
513 strncpy(rip, ip, rip_len);
514 rip[rip_len - 1] = '\0';
515 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800516
517 ret = 0;
518bail:
519 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000520}
Andy Green9f990342011-02-12 11:57:45 +0000521
Peter Hinz56885f32011-03-02 22:03:47 +0000522int libwebsockets_get_random(struct libwebsocket_context *context,
523 void *buf, int len)
524{
525 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800526 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000527
528#ifdef WIN32
529 for (n = 0; n < len; n++)
530 p[n] = (unsigned char)rand();
531#else
532 n = read(context->fd_random, p, len);
533#endif
534
535 return n;
536}
537
Andy Greena690cd02013-02-09 12:25:31 +0800538int lws_set_socket_options(struct libwebsocket_context *context, int fd)
539{
540 int optval = 1;
541 socklen_t optlen = sizeof(optval);
542#ifdef WIN32
543 unsigned long optl = 0;
544#endif
545#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
546 struct protoent *tcp_proto;
547#endif
548
549 if (context->ka_time) {
550 /* enable keepalive on this socket */
551 optval = 1;
552 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
553 (const void *)&optval, optlen) < 0)
554 return 1;
555
Andy Greena47865f2013-02-10 09:39:47 +0800556#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
557
558 /*
559 * didn't find a way to set these per-socket, need to
560 * tune kernel systemwide values
561 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100562#elif WIN32
563 {
564 DWORD dwBytesRet;
565 struct tcp_keepalive alive;
566 alive.onoff = TRUE;
567 alive.keepalivetime = context->ka_time;
568 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800569
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100570 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
571 NULL, 0, &dwBytesRet, NULL, NULL))
572 return 1;
573 }
Andy Greena47865f2013-02-10 09:39:47 +0800574#else
Andy Greena690cd02013-02-09 12:25:31 +0800575 /* set the keepalive conditions we want on it too */
576 optval = context->ka_time;
577 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
578 (const void *)&optval, optlen) < 0)
579 return 1;
580
581 optval = context->ka_probes;
582 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
583 (const void *)&optval, optlen) < 0)
584 return 1;
585
586 optval = context->ka_interval;
587 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
588 (const void *)&optval, optlen) < 0)
589 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800590#endif
Andy Greena690cd02013-02-09 12:25:31 +0800591 }
592
593 /* Disable Nagle */
594 optval = 1;
595#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
596 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
597#else
598 tcp_proto = getprotobyname("TCP");
599 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
600#endif
601
602 /* We are nonblocking... */
603#ifdef WIN32
604 ioctlsocket(fd, FIONBIO, &optl);
605#else
606 fcntl(fd, F_SETFL, O_NONBLOCK);
607#endif
608
609 return 0;
610}
611
Andy Green95a7b5d2011-03-06 10:29:39 +0000612int lws_send_pipe_choked(struct libwebsocket *wsi)
613{
614 struct pollfd fds;
615
616 fds.fd = wsi->sock;
617 fds.events = POLLOUT;
618 fds.revents = 0;
619
620 if (poll(&fds, 1, 0) != 1)
621 return 1;
622
623 if ((fds.revents & POLLOUT) == 0)
624 return 1;
625
626 /* okay to send another packet without blocking */
627
628 return 0;
629}
630
Andy Greena41314f2011-05-23 10:00:03 +0100631int
Andy Green3b84c002011-03-06 13:14:42 +0000632lws_handle_POLLOUT_event(struct libwebsocket_context *context,
633 struct libwebsocket *wsi, struct pollfd *pollfd)
634{
Andy Green3b84c002011-03-06 13:14:42 +0000635 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800636
Andy Green3182ece2013-01-20 17:08:31 +0800637#ifndef LWS_NO_EXTENSIONS
638 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000639 int ret;
640 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100641 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000642
Andy Greena41314f2011-05-23 10:00:03 +0100643 for (n = 0; n < wsi->count_active_extensions; n++) {
644 if (!wsi->active_extensions[n]->callback)
645 continue;
646
647 m = wsi->active_extensions[n]->callback(context,
648 wsi->active_extensions[n], wsi,
649 LWS_EXT_CALLBACK_IS_WRITEABLE,
650 wsi->active_extensions_user[n], NULL, 0);
651 if (m > handled)
652 handled = m;
653 }
654
655 if (handled == 1)
656 goto notify_action;
657
658 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000659 goto user_service;
660
661 /*
662 * check in on the active extensions, see if they
663 * had pending stuff to spill... they need to get the
664 * first look-in otherwise sequence will be disordered
665 *
666 * NULL, zero-length eff_buf means just spill pending
667 */
668
669 ret = 1;
670 while (ret == 1) {
671
672 /* default to nobody has more to spill */
673
674 ret = 0;
675 eff_buf.token = NULL;
676 eff_buf.token_len = 0;
677
678 /* give every extension a chance to spill */
679
680 for (n = 0; n < wsi->count_active_extensions; n++) {
681 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000682 wsi->protocol->owning_server,
683 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000684 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
685 wsi->active_extensions_user[n], &eff_buf, 0);
686 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800687 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000688 return -1;
689 }
690 if (m)
691 /*
692 * at least one extension told us he has more
693 * to spill, so we will go around again after
694 */
695 ret = 1;
696 }
697
698 /* assuming they gave us something to send, send it */
699
700 if (eff_buf.token_len) {
701 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
702 eff_buf.token_len))
703 return -1;
704 } else
705 continue;
706
707 /* no extension has more to spill */
708
709 if (!ret)
710 continue;
711
712 /*
713 * There's more to spill from an extension, but we just sent
714 * something... did that leave the pipe choked?
715 */
716
717 if (!lws_send_pipe_choked(wsi))
718 /* no we could add more */
719 continue;
720
Andy Green43db0452013-01-10 19:50:35 +0800721 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000722
723 /*
724 * Yes, he's choked. Leave the POLLOUT masked on so we will
725 * come back here when he is unchoked. Don't call the user
726 * callback to enforce ordering of spilling, he'll get called
727 * when we come back here and there's nothing more to spill.
728 */
729
730 return 0;
731 }
732
733 wsi->extension_data_pending = 0;
734
735user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800736#endif
Andy Green3b84c002011-03-06 13:14:42 +0000737 /* one shot */
738
Andy Greena41314f2011-05-23 10:00:03 +0100739 if (pollfd) {
740 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000741
Andy Greena41314f2011-05-23 10:00:03 +0100742 /* external POLL support via protocol 0 */
743 context->protocols[0].callback(context, wsi,
744 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800745 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100746 }
Andy Green3182ece2013-01-20 17:08:31 +0800747#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100748notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800749#endif
Andy Green3b84c002011-03-06 13:14:42 +0000750
Andy Green9e4c2b62011-03-07 20:47:39 +0000751 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
752 n = LWS_CALLBACK_CLIENT_WRITEABLE;
753 else
754 n = LWS_CALLBACK_SERVER_WRITEABLE;
755
Andy Greenb8b247d2013-01-22 07:20:08 +0800756 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800757 wsi, (enum libwebsocket_callback_reasons) n,
758 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000759}
760
761
762
Andy Greena41314f2011-05-23 10:00:03 +0100763void
764libwebsocket_service_timeout_check(struct libwebsocket_context *context,
765 struct libwebsocket *wsi, unsigned int sec)
766{
Andy Green3182ece2013-01-20 17:08:31 +0800767#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100768 int n;
769
770 /*
771 * if extensions want in on it (eg, we are a mux parent)
772 * give them a chance to service child timeouts
773 */
774
775 for (n = 0; n < wsi->count_active_extensions; n++)
776 wsi->active_extensions[n]->callback(
777 context, wsi->active_extensions[n],
778 wsi, LWS_EXT_CALLBACK_1HZ,
779 wsi->active_extensions_user[n], NULL, sec);
780
Andy Green3182ece2013-01-20 17:08:31 +0800781#endif
Andy Greena41314f2011-05-23 10:00:03 +0100782 if (!wsi->pending_timeout)
783 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800784
Andy Greena41314f2011-05-23 10:00:03 +0100785 /*
786 * if we went beyond the allowed time, kill the
787 * connection
788 */
789
790 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800791 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100792 libwebsocket_close_and_free_session(context,
793 wsi, LWS_CLOSE_STATUS_NOSTATUS);
794 }
795}
796
Andy Green9f990342011-02-12 11:57:45 +0000797/**
798 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000799 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000800 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800801 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000802 *
Andy Green75006172013-01-22 12:32:11 +0800803 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800804 * services it according to the state of the associated
805 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800806 *
807 * The one call deals with all "service" that might happen on a socket
808 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000809 */
810
811int
Peter Hinz56885f32011-03-02 22:03:47 +0000812libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000813 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000814{
Andy Greena1ce6be2013-01-18 11:43:21 +0800815 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000816 int n;
Andy Green0d338332011-02-12 11:57:43 +0000817 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800818 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000819 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800820
Andy Green3182ece2013-01-20 17:08:31 +0800821#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000822 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800823#endif
Andy Green98a717c2011-03-06 13:14:15 +0000824 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800825
826 if (context->listen_service_fd)
827 listen_socket_fds_index = context->lws_lookup[
828 context->listen_service_fd]->position_in_fds_table;
829
Andy Greena71eafc2011-02-14 17:59:43 +0000830 /*
831 * you can call us with pollfd = NULL to just allow the once-per-second
832 * global timeout checks; if less than a second since the last check
833 * it returns immediately then.
834 */
835
836 gettimeofday(&tv, NULL);
837
Peter Hinz56885f32011-03-02 22:03:47 +0000838 if (context->last_timeout_check_s != tv.tv_sec) {
839 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000840
Joakim Soderberg4c531232013-02-06 15:26:58 +0900841 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800842 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800843 if (context->started_with_parent &&
844 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800845 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900846 #endif
Andy Green24cba922013-01-19 13:56:10 +0800847
Andy Greena71eafc2011-02-14 17:59:43 +0000848 /* global timeout check once per second */
849
Peter Hinz56885f32011-03-02 22:03:47 +0000850 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +0800851 struct libwebsocket *new_wsi =
852 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +0800853 if (!new_wsi)
854 continue;
855 libwebsocket_service_timeout_check(context,
856 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000857 }
858 }
859
860 /* just here for timeout management? */
861
862 if (pollfd == NULL)
863 return 0;
864
865 /* no, here to service a socket descriptor */
866
Andy Green65b0e912013-01-16 07:59:47 +0800867 /*
868 * deal with listen service piggybacking
869 * every listen_service_modulo services of other fds, we
870 * sneak one in to service the listen socket if there's anything waiting
871 *
872 * To handle connection storms, as found in ab, if we previously saw a
873 * pending connection here, it causes us to check again next time.
874 */
875
Andy Greenb5b23192013-02-11 17:13:32 +0800876 if (context->listen_service_fd && pollfd !=
877 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800878 context->listen_service_count++;
879 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800880 context->listen_service_count ==
881 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800882 context->listen_service_count = 0;
883 m = 1;
884 if (context->listen_service_extraseen > 5)
885 m = 2;
886 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800887 /*
888 * even with extpoll, we prepared this
889 * internal fds for listen
890 */
891 n = poll(&context->fds[listen_socket_fds_index],
892 1, 0);
893 if (n > 0) { /* there's a conn waiting for us */
894 libwebsocket_service_fd(context,
895 &context->
896 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800897 context->listen_service_extraseen++;
898 } else {
899 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800900 context->
901 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800902 break;
903 }
904 }
905 }
906
907 }
908
909 /* okay, what we came here to do... */
910
Andy Greendfb23042013-01-17 12:26:48 +0800911 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800912 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800913 if (pollfd->fd > 11)
Andy Greenb5b23192013-02-11 17:13:32 +0800914 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n",
915 pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800916 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800917 }
Andy Green8f037e42010-12-19 22:13:26 +0000918
Andy Green0d338332011-02-12 11:57:43 +0000919 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800920
Andy Greena1ce6be2013-01-18 11:43:21 +0800921#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800922 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000923 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800924 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800925 return lws_server_socket_service(context, wsi, pollfd);
926#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000927
Andy Green0d338332011-02-12 11:57:43 +0000928 case LWS_CONNMODE_WS_SERVING:
929 case LWS_CONNMODE_WS_CLIENT:
930
931 /* handle session socket closed */
932
Andy Greenc9ac31e2013-02-16 10:17:52 +0800933 if ((!pollfd->revents & POLLIN) &&
934 (pollfd->revents & (POLLERR | POLLHUP))) {
Andy Green0d338332011-02-12 11:57:43 +0000935
Andy Green43db0452013-01-10 19:50:35 +0800936 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000937 (void *)wsi, pollfd->fd);
938
Peter Hinz56885f32011-03-02 22:03:47 +0000939 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000940 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800941 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000942 }
943
Andy Green0d338332011-02-12 11:57:43 +0000944 /* the guy requested a callback when it was OK to write */
945
Andy Greenda527df2011-03-07 07:08:12 +0000946 if ((pollfd->revents & POLLOUT) &&
947 wsi->state == WSI_STATE_ESTABLISHED)
948 if (lws_handle_POLLOUT_event(context, wsi,
949 pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800950 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenda527df2011-03-07 07:08:12 +0000951 libwebsocket_close_and_free_session(
Andy Greenb0593712013-02-15 22:32:53 +0800952 context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800953 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000954 }
Andy Green0d338332011-02-12 11:57:43 +0000955
Andy Green0d338332011-02-12 11:57:43 +0000956
957 /* any incoming data ready? */
958
959 if (!(pollfd->revents & POLLIN))
960 break;
961
Andy Greenb45993c2010-12-18 15:13:50 +0000962#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800963read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800964 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800965 eff_buf.token_len = SSL_read(wsi->ssl,
966 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800967 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800968 if (!eff_buf.token_len) {
969 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800970 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +0800971 ERR_error_string(n,
972 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800973 }
974 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000975#endif
Andy Greene84652c2013-02-08 13:01:02 +0800976 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +0800977 context->service_buffer,
978 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000979
Andy Green98a717c2011-03-06 13:14:15 +0000980 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800981 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000982 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200983 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800984 libwebsocket_close_and_free_session(context,
985 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800986 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000987 }
Andy Green98a717c2011-03-06 13:14:15 +0000988 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800989 lwsl_info("closing connection due to 0 length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000990 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800991 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800992 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000993 }
994
Andy Green98a717c2011-03-06 13:14:15 +0000995 /*
996 * give any active extensions a chance to munge the buffer
997 * before parse. We pass in a pointer to an lws_tokens struct
998 * prepared with the default buffer and content length that's in
999 * there. Rather than rewrite the default buffer, extensions
1000 * that expect to grow the buffer can adapt .token to
1001 * point to their own per-connection buffer in the extension
1002 * user allocation. By default with no extensions or no
1003 * extension callback handling, just the normal input buffer is
1004 * used then so it is efficient.
1005 */
Andy Greenb45993c2010-12-18 15:13:50 +00001006
Andy Greene84652c2013-02-08 13:01:02 +08001007 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001008#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001009 more = 1;
1010 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001011
Andy Green98a717c2011-03-06 13:14:15 +00001012 more = 0;
1013
1014 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001015 m = wsi->active_extensions[n]->callback(context,
1016 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001017 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001018 wsi->active_extensions_user[n],
1019 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001020 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001021 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001022 "Extension reports fatal error\n");
1023 libwebsocket_close_and_free_session(
1024 context, wsi,
1025 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001026 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001027 }
1028 if (m)
1029 more = 1;
1030 }
Andy Green3182ece2013-01-20 17:08:31 +08001031#endif
Andy Green98a717c2011-03-06 13:14:15 +00001032 /* service incoming data */
1033
1034 if (eff_buf.token_len) {
1035 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001036 (unsigned char *)eff_buf.token,
1037 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001038 if (n < 0)
1039 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001040 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001041 }
Andy Green3182ece2013-01-20 17:08:31 +08001042#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001043 eff_buf.token = NULL;
1044 eff_buf.token_len = 0;
1045 }
Andy Green3182ece2013-01-20 17:08:31 +08001046#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001047
1048#ifdef LWS_OPENSSL_SUPPORT
1049 if (wsi->ssl && SSL_pending(wsi->ssl))
1050 goto read_pending;
1051#endif
Andy Green98a717c2011-03-06 13:14:15 +00001052 break;
Andy Green76f61e72013-01-16 11:53:05 +08001053
1054 default:
Andy Green03674a62013-01-16 11:47:40 +08001055#ifdef LWS_NO_CLIENT
1056 break;
1057#else
Andy Green76f61e72013-01-16 11:53:05 +08001058 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001059#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001060 }
1061
1062 return 0;
1063}
1064
Andy Green0d338332011-02-12 11:57:43 +00001065
Andy Green6964bb52011-01-23 16:50:33 +00001066/**
1067 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001068 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001069 *
1070 * This function closes any active connections and then frees the
1071 * context. After calling this, any further use of the context is
1072 * undefined.
1073 */
1074void
Peter Hinz56885f32011-03-02 22:03:47 +00001075libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001076{
Andy Green3182ece2013-01-20 17:08:31 +08001077#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001078 int n;
1079 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001080 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001081 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001082
Andy Greend636e352013-01-29 12:36:17 +08001083#ifdef LWS_LATENCY
1084 if (context->worst_latency_info[0])
1085 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1086#endif
1087
Andy Greendfb23042013-01-17 12:26:48 +08001088 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001089 struct libwebsocket *wsi =
1090 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001091 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001092 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1093 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001094 }
Andy Green6964bb52011-01-23 16:50:33 +00001095
Andy Greena41314f2011-05-23 10:00:03 +01001096 /*
1097 * give all extensions a chance to clean up any per-context
1098 * allocations they might have made
1099 */
1100
1101 ext = context->extensions;
1102 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1103 if (context->listen_port)
1104 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001105 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001106 ext->callback(context, ext, NULL,
1107 (enum libwebsocket_extension_callback_reasons)m,
1108 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001109 ext++;
1110 }
Andy Greena7109e62013-02-11 12:05:54 +08001111
1112 /*
1113 * inform all the protocols that they are done and will have no more
1114 * callbacks
1115 */
1116
1117 while (protocol->callback) {
1118 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1119 NULL, NULL, 0);
1120 protocol++;
1121 }
1122
Andy Green3182ece2013-01-20 17:08:31 +08001123#endif
Andy Greena41314f2011-05-23 10:00:03 +01001124
Peter Hinz56885f32011-03-02 22:03:47 +00001125#ifdef WIN32
1126#else
1127 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001128#endif
1129
Peter Hinz56885f32011-03-02 22:03:47 +00001130#ifdef LWS_OPENSSL_SUPPORT
1131 if (context->ssl_ctx)
1132 SSL_CTX_free(context->ssl_ctx);
1133 if (context->ssl_client_ctx)
1134 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001135
1136 ERR_remove_state(0);
1137 ERR_free_strings();
1138 EVP_cleanup();
1139 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001140#endif
1141
Andy Green46596482013-02-11 11:04:01 +08001142 if (context->fds)
1143 free(context->fds);
1144 if (context->lws_lookup)
1145 free(context->lws_lookup);
1146
Peter Hinz56885f32011-03-02 22:03:47 +00001147 free(context);
1148
1149#ifdef WIN32
1150 WSACleanup();
1151#endif
Andy Green6964bb52011-01-23 16:50:33 +00001152}
1153
Andy Greend88146d2013-01-22 12:40:35 +08001154/**
Andy Greenb5b23192013-02-11 17:13:32 +08001155 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001156 * @context: Websocket context
1157 *
1158 * This returns the optional user allocation that can be attached to
1159 * the context the sockets live in at context_create time. It's a way
1160 * to let all sockets serviced in the same context share data without
1161 * using globals statics in the user code.
1162 */
Alon Levy0291eb32012-10-19 11:21:56 +02001163LWS_EXTERN void *
1164libwebsocket_context_user(struct libwebsocket_context *context)
1165{
Andy Greenb5b23192013-02-11 17:13:32 +08001166 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001167}
1168
Andy Green6964bb52011-01-23 16:50:33 +00001169/**
1170 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001171 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001172 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1173 * service otherwise block and service immediately, returning
1174 * after the timeout if nothing needed service.
1175 *
1176 * This function deals with any pending websocket traffic, for three
1177 * kinds of event. It handles these events on both server and client
1178 * types of connection the same.
1179 *
1180 * 1) Accept new connections to our context's server
1181 *
Andy Green6f520a52013-01-29 17:57:39 +08001182 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001183 * server or client connections.
1184 *
1185 * You need to call this service function periodically to all the above
1186 * functions to happen; if your application is single-threaded you can
1187 * just call it in your main event loop.
1188 *
1189 * Alternatively you can fork a new process that asynchronously handles
1190 * calling this service in a loop. In that case you are happy if this
1191 * call blocks your thread until it needs to take care of something and
1192 * would call it with a large nonzero timeout. Your loop then takes no
1193 * CPU while there is nothing happening.
1194 *
1195 * If you are calling it in a single-threaded app, you don't want it to
1196 * wait around blocking other things in your loop from happening, so you
1197 * would call it with a timeout_ms of 0, so it returns immediately if
1198 * nothing is pending, or as soon as it services whatever was pending.
1199 */
1200
Andy Greene92cd172011-01-19 13:11:55 +00001201int
Peter Hinz56885f32011-03-02 22:03:47 +00001202libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001203{
1204 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001205
1206 /* stay dead once we are dead */
1207
Peter Hinz56885f32011-03-02 22:03:47 +00001208 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001209 return 1;
1210
Andy Green0d338332011-02-12 11:57:43 +00001211 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001212
Peter Hinz56885f32011-03-02 22:03:47 +00001213 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001214 if (n == 0) /* poll timeout */
1215 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001216
Andy Greendfb23042013-01-17 12:26:48 +08001217 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001218 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001219
Andy Greendfb23042013-01-17 12:26:48 +08001220 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001221
Peter Hinz56885f32011-03-02 22:03:47 +00001222 for (n = 0; n < context->fds_count; n++)
1223 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001224 if (libwebsocket_service_fd(context,
1225 &context->fds[n]) < 0)
1226 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001227 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001228}
1229
Andy Green3182ece2013-01-20 17:08:31 +08001230#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001231int
1232lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001233 struct libwebsocket *wsi,
1234 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001235 void *v, size_t len)
1236{
1237 int n;
1238 int handled = 0;
1239
1240 /* maybe an extension will take care of it for us */
1241
1242 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1243 if (!wsi->active_extensions[n]->callback)
1244 continue;
1245
1246 handled |= wsi->active_extensions[n]->callback(context,
1247 wsi->active_extensions[n], wsi,
1248 r, wsi->active_extensions_user[n], v, len);
1249 }
1250
1251 return handled;
1252}
1253
1254
1255void *
1256lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001257 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001258{
1259 int n = 0;
1260
Andy Green68b45042011-05-25 21:41:57 +01001261 if (wsi == NULL)
1262 return NULL;
1263
Andy Greena41314f2011-05-23 10:00:03 +01001264 while (n < wsi->count_active_extensions) {
1265 if (wsi->active_extensions[n] != ext) {
1266 n++;
1267 continue;
1268 }
1269 return wsi->active_extensions_user[n];
1270 }
1271
1272 return NULL;
1273}
Andy Green3182ece2013-01-20 17:08:31 +08001274#endif
Andy Greena41314f2011-05-23 10:00:03 +01001275
Andy Green90c7cbc2011-01-27 06:26:52 +00001276/**
1277 * libwebsocket_callback_on_writable() - Request a callback when this socket
1278 * becomes able to be written to without
1279 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001280 *
Peter Hinz56885f32011-03-02 22:03:47 +00001281 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001282 * @wsi: Websocket connection instance to get callback for
1283 */
1284
1285int
Peter Hinz56885f32011-03-02 22:03:47 +00001286libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001287 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001288{
Andy Green3182ece2013-01-20 17:08:31 +08001289#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001290 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001291 int handled = 0;
1292
1293 /* maybe an extension will take care of it for us */
1294
1295 for (n = 0; n < wsi->count_active_extensions; n++) {
1296 if (!wsi->active_extensions[n]->callback)
1297 continue;
1298
1299 handled |= wsi->active_extensions[n]->callback(context,
1300 wsi->active_extensions[n], wsi,
1301 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1302 wsi->active_extensions_user[n], NULL, 0);
1303 }
1304
1305 if (handled)
1306 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001307#endif
Andy Greendfb23042013-01-17 12:26:48 +08001308 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001309 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1310 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001311 return -1;
1312 }
1313
1314 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001315
Andy Green3221f922011-02-12 13:14:11 +00001316 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001317 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001318 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001319 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001320
Andy Green90c7cbc2011-01-27 06:26:52 +00001321 return 1;
1322}
1323
1324/**
1325 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1326 * all connections using the given protocol when it
1327 * becomes possible to write to each socket without
1328 * blocking in turn.
1329 *
1330 * @protocol: Protocol whose connections will get callbacks
1331 */
1332
1333int
1334libwebsocket_callback_on_writable_all_protocol(
1335 const struct libwebsocket_protocols *protocol)
1336{
Peter Hinz56885f32011-03-02 22:03:47 +00001337 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001338 int n;
Andy Green0d338332011-02-12 11:57:43 +00001339 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001340
Andy Greendfb23042013-01-17 12:26:48 +08001341 for (n = 0; n < context->fds_count; n++) {
1342 wsi = context->lws_lookup[context->fds[n].fd];
1343 if (!wsi)
1344 continue;
1345 if (wsi->protocol == protocol)
1346 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001347 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001348
1349 return 0;
1350}
1351
Andy Greenbe93fef2011-02-14 20:25:43 +00001352/**
1353 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1354 *
1355 * You will not need this unless you are doing something special
1356 *
1357 * @wsi: Websocket connection instance
1358 * @reason: timeout reason
1359 * @secs: how many seconds
1360 */
1361
1362void
1363libwebsocket_set_timeout(struct libwebsocket *wsi,
1364 enum pending_timeout reason, int secs)
1365{
1366 struct timeval tv;
1367
1368 gettimeofday(&tv, NULL);
1369
1370 wsi->pending_timeout_limit = tv.tv_sec + secs;
1371 wsi->pending_timeout = reason;
1372}
1373
Andy Greena6cbece2011-01-27 20:06:03 +00001374
1375/**
1376 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1377 *
1378 * You will not need this unless you are doing something special
1379 *
1380 * @wsi: Websocket connection instance
1381 */
1382
1383int
1384libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1385{
1386 return wsi->sock;
1387}
1388
Andy Greend636e352013-01-29 12:36:17 +08001389#ifdef LWS_LATENCY
1390void
Andy Greenb5b23192013-02-11 17:13:32 +08001391lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1392 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001393{
1394 struct timeval tv;
1395 unsigned long u;
1396 char buf[256];
1397
1398 gettimeofday(&tv, NULL);
1399
1400 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1401
1402 if (action) {
1403 if (completed) {
1404 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001405 sprintf(buf,
1406 "Completion first try lat %luus: %p: ret %d: %s\n",
1407 u - wsi->latency_start,
1408 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001409 else
Andy Greenb5b23192013-02-11 17:13:32 +08001410 sprintf(buf,
1411 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1412 u - wsi->action_start,
1413 u - wsi->latency_start,
1414 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001415 wsi->action_start = 0;
1416 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001417 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1418 u - wsi->latency_start,
1419 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001420 if (u - wsi->latency_start > context->worst_latency) {
1421 context->worst_latency = u - wsi->latency_start;
1422 strcpy(context->worst_latency_info, buf);
1423 }
1424 lwsl_latency("%s", buf);
1425 } else {
1426 wsi->latency_start = u;
1427 if (!wsi->action_start)
1428 wsi->action_start = u;
1429 }
1430}
1431#endif
1432
Andy Greena1ce6be2013-01-18 11:43:21 +08001433#ifdef LWS_NO_SERVER
1434int
1435_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1436{
1437 return 0;
1438}
1439#else
Andy Green706961d2013-01-17 16:50:35 +08001440int
1441_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1442{
1443 struct libwebsocket_context *context = wsi->protocol->owning_server;
1444 int n;
1445
Andy Green623a98d2013-01-21 11:04:23 +08001446 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001447 return 0;
1448
Andy Green623a98d2013-01-21 11:04:23 +08001449 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001450
Andy Greenb5b23192013-02-11 17:13:32 +08001451 lwsl_info("rxflow: wsi %p change_to %d\n",
1452 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001453
1454 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001455 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001456 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1457 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001458 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001459 return -1;
1460 }
1461 if (n)
1462 /* oh he stuck again, do nothing */
1463 return 0;
1464 }
1465
Andy Green623a98d2013-01-21 11:04:23 +08001466 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001467 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1468 else
1469 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1470
Andy Green623a98d2013-01-21 11:04:23 +08001471 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001472 /* external POLL support via protocol 0 */
1473 context->protocols[0].callback(context, wsi,
1474 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001475 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001476 else
1477 /* external POLL support via protocol 0 */
1478 context->protocols[0].callback(context, wsi,
1479 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001480 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001481
1482 return 1;
1483}
Andy Greena1ce6be2013-01-18 11:43:21 +08001484#endif
Andy Green706961d2013-01-17 16:50:35 +08001485
Andy Green90c7cbc2011-01-27 06:26:52 +00001486/**
1487 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1488 * receieved packets.
1489 *
1490 * If the output side of a server process becomes choked, this allows flow
1491 * control for the input side.
1492 *
1493 * @wsi: Websocket connection instance to get callback for
1494 * @enable: 0 = disable read servicing for this connection, 1 = enable
1495 */
1496
1497int
1498libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1499{
Andy Green623a98d2013-01-21 11:04:23 +08001500 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001501
Andy Green706961d2013-01-17 16:50:35 +08001502 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001503}
1504
Andy Green706961d2013-01-17 16:50:35 +08001505
Andy Green2ac5a6f2011-01-28 10:00:18 +00001506/**
1507 * libwebsocket_canonical_hostname() - returns this host's hostname
1508 *
1509 * This is typically used by client code to fill in the host parameter
1510 * when making a client connection. You can only call it after the context
1511 * has been created.
1512 *
Peter Hinz56885f32011-03-02 22:03:47 +00001513 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001514 */
1515
1516
1517extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001518libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001519{
Peter Hinz56885f32011-03-02 22:03:47 +00001520 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001521}
1522
1523
Andy Green90c7cbc2011-01-27 06:26:52 +00001524static void sigpipe_handler(int x)
1525{
1526}
1527
Andy Green6901cb32011-02-21 08:06:47 +00001528#ifdef LWS_OPENSSL_SUPPORT
1529static int
1530OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1531{
1532
1533 SSL *ssl;
1534 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001535 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001536
1537 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1538 SSL_get_ex_data_X509_STORE_CTX_idx());
1539
1540 /*
Andy Green2e24da02011-03-05 16:12:04 +00001541 * !!! nasty openssl requires the index to come as a library-scope
1542 * static
Andy Green6901cb32011-02-21 08:06:47 +00001543 */
Andy Green2e24da02011-03-05 16:12:04 +00001544 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001545
Peter Hinz56885f32011-03-02 22:03:47 +00001546 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001547 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1548 x509_ctx, ssl, preverify_ok);
1549
1550 /* convert return code from 0 = OK to 1 = OK */
1551
1552 if (!n)
1553 n = 1;
1554 else
1555 n = 0;
1556
1557 return n;
1558}
1559#endif
1560
Andy Green706961d2013-01-17 16:50:35 +08001561int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001562 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001563 struct libwebsocket *wsi,
1564 enum libwebsocket_callback_reasons reason, void *user,
1565 void *in, size_t len)
1566{
1567 int n;
1568
1569 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001570 if (!n)
1571 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001572
Andy Greenaedc9532013-02-10 21:21:24 +08001573 return n;
Andy Green706961d2013-01-17 16:50:35 +08001574}
1575
Andy Greenb45993c2010-12-18 15:13:50 +00001576
Andy Greenab990e42010-10-31 12:42:52 +00001577/**
Andy Green4739e5c2011-01-22 12:51:57 +00001578 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001579 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001580 *
Andy Green1b265272013-02-09 14:01:09 +08001581 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001582 * of all initialization in one step.
1583 *
Andy Greene92cd172011-01-19 13:11:55 +00001584 * After initialization, it returns a struct libwebsocket_context * that
1585 * represents this server. After calling, user code needs to take care
1586 * of calling libwebsocket_service() with the context pointer to get the
1587 * server's sockets serviced. This can be done in the same process context
1588 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001589 *
Andy Green8f037e42010-12-19 22:13:26 +00001590 * The protocol callback functions are called for a handful of events
1591 * including http requests coming in, websocket connections becoming
1592 * established, and data arriving; it's also called periodically to allow
1593 * async transmission.
1594 *
1595 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1596 * at that time websocket protocol has not been negotiated. Other
1597 * protocols after the first one never see any HTTP callack activity.
1598 *
1599 * The server created is a simple http server by default; part of the
1600 * websocket standard is upgrading this http connection to a websocket one.
1601 *
1602 * This allows the same server to provide files like scripts and favicon /
1603 * images or whatever over http and dynamic data over websockets all in
1604 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001605 */
Andy Green4ea60062010-10-30 12:15:07 +01001606
Andy Greene92cd172011-01-19 13:11:55 +00001607struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001608libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001609{
Peter Hinz56885f32011-03-02 22:03:47 +00001610 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001611 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001612#ifndef LWS_NO_SERVER
Andy Green14f47292013-02-11 19:36:15 +08001613 int n;
Andy Greencbb31222013-01-31 09:57:05 +08001614 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001615 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001616 struct sockaddr_in serv_addr;
1617#endif
Andy Green3182ece2013-01-20 17:08:31 +08001618#ifndef LWS_NO_EXTENSIONS
1619 int m;
Andy Green1b265272013-02-09 14:01:09 +08001620 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001621#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001622
Andy Green3faa9c72010-11-08 17:03:03 +00001623#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001624 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001625#endif
1626
Joakim Soderberg4c531232013-02-06 15:26:58 +09001627#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001628 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001629#endif
1630
Andy Greenb3a614a2013-01-19 13:08:17 +08001631 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001632 lwsl_notice("Library version: %s\n", library_version);
Andy Greenb5b23192013-02-11 17:13:32 +08001633 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n",
1634 LWS_MAX_HEADER_NAME_LENGTH);
Andy Greenc0d6b632013-01-12 23:42:17 +08001635 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001636 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001637#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001638 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1639 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001640#else
1641 lwsl_notice(" Configured without extension support\n");
1642#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001643 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1644 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1645 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1646 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1647 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001648
Peter Hinz56885f32011-03-02 22:03:47 +00001649#ifdef _WIN32
1650 {
1651 WORD wVersionRequested;
1652 WSADATA wsaData;
1653 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001654 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001655
1656 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1657 wVersionRequested = MAKEWORD(2, 2);
1658
1659 err = WSAStartup(wVersionRequested, &wsaData);
1660 if (err != 0) {
1661 /* Tell the user that we could not find a usable */
1662 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001663 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001664 return NULL;
1665 }
David Galeano7b11fec2011-10-04 19:55:18 +08001666
Andy Green6ee372f2012-04-09 15:09:01 +08001667 /* default to a poll() made out of select() */
1668 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001669
Andy Green6ee372f2012-04-09 15:09:01 +08001670 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001671 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001672 if (wsdll)
1673 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001674
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001675 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001676 if (!poll)
1677 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001678 }
1679#endif
1680
Andy Greenb5b23192013-02-11 17:13:32 +08001681 context = (struct libwebsocket_context *)
1682 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001683 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001684 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001685 return NULL;
1686 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001687 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001688#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001689 context->started_with_parent = pid_daemon;
1690 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1691#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001692
Joakim Soderberg4c531232013-02-06 15:26:58 +09001693 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001694 context->protocols = info->protocols;
1695 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001696 context->http_proxy_port = 0;
1697 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001698 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001699 /* to reduce this allocation, */
1700 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001701 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1702 sizeof(struct libwebsocket_context),
1703 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1704 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001705 sizeof(struct libwebsocket_context) +
1706 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1707 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001708
Andy Greenb5b23192013-02-11 17:13:32 +08001709 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1710 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001711 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001712 lwsl_err("Unable to allocate fds array for %d connections\n",
1713 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001714 free(context);
1715 return NULL;
1716 }
Andy Greenb5b23192013-02-11 17:13:32 +08001717 context->lws_lookup = (struct libwebsocket **)
1718 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001719 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001720 lwsl_err(
1721 "Unable to allocate lws_lookup array for %d connections\n",
1722 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001723 free(context->fds);
1724 free(context);
1725 return NULL;
1726 }
Andy Greena17c6922013-01-20 20:21:54 +08001727
Peter Hinz56885f32011-03-02 22:03:47 +00001728 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001729#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001730 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001731#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001732 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001733 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001734
Peter Hinz56885f32011-03-02 22:03:47 +00001735#ifdef WIN32
1736 context->fd_random = 0;
1737#else
1738 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1739 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001740 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001741 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001742 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001743 }
Peter Hinz56885f32011-03-02 22:03:47 +00001744#endif
Andy Green44eee682011-02-10 09:32:24 +00001745
Peter Hinz56885f32011-03-02 22:03:47 +00001746#ifdef LWS_OPENSSL_SUPPORT
1747 context->use_ssl = 0;
1748 context->ssl_ctx = NULL;
1749 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001750 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001751#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001752
Andy Greena1ce6be2013-01-18 11:43:21 +08001753 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001754
Andy Greena1ce6be2013-01-18 11:43:21 +08001755#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001756 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001757 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001758 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001759 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001760
Andy Greenb5b23192013-02-11 17:13:32 +08001761 lwsl_notice(" canonical_hostname = %s\n",
1762 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001763 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001764#endif
Andy Greena69f0512012-05-03 12:32:38 +08001765
Andy Green9659f372011-01-27 22:01:43 +00001766 /* split the proxy ads:port if given */
1767
1768 p = getenv("http_proxy");
1769 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001770 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001771 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001772 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001773 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001774
Peter Hinz56885f32011-03-02 22:03:47 +00001775 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001776 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001777 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001778 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001779 }
1780 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001781 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001782
Andy Greenb3a614a2013-01-19 13:08:17 +08001783 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001784 context->http_proxy_address,
1785 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001786 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001787
Andy Greena1ce6be2013-01-18 11:43:21 +08001788#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001789 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001790
Andy Green3faa9c72010-11-08 17:03:03 +00001791#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001792 context->use_ssl = info->ssl_cert_filepath != NULL &&
1793 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001794#ifdef USE_CYASSL
1795 lwsl_notice(" Compiled with CYASSL support\n");
1796#else
1797 lwsl_notice(" Compiled with OpenSSL support\n");
1798#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001799 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001800 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001801 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001802 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001803
Andy Green90c7cbc2011-01-27 06:26:52 +00001804#else
Andy Green2b40b792013-02-10 22:22:01 +08001805 if (info->ssl_cert_filepath != NULL &&
1806 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001807 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001808 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001809 }
Andy Greenb5b23192013-02-11 17:13:32 +08001810 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001811#endif
Andy Greena17c6922013-01-20 20:21:54 +08001812
Andy Greenb5b23192013-02-11 17:13:32 +08001813 lwsl_notice(
1814 " per-conn mem: %u + %u headers + protocol rx buf\n",
1815 sizeof(struct libwebsocket),
1816 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001817 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001818#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001819
1820 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001821#ifdef WIN32
1822#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001823 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001824#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001825
1826
1827#ifdef LWS_OPENSSL_SUPPORT
1828
1829 /* basic openssl init */
1830
1831 SSL_library_init();
1832
1833 OpenSSL_add_all_algorithms();
1834 SSL_load_error_strings();
1835
Andy Green2e24da02011-03-05 16:12:04 +00001836 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001837 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1838
Andy Green90c7cbc2011-01-27 06:26:52 +00001839 /*
1840 * Firefox insists on SSLv23 not SSLv3
1841 * Konq disables SSLv2 by default now, SSLv23 works
1842 */
1843
1844 method = (SSL_METHOD *)SSLv23_server_method();
1845 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001846 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001847 ERR_error_string(ERR_get_error(),
1848 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001849 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001850 }
Peter Hinz56885f32011-03-02 22:03:47 +00001851 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1852 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001853 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001854 ERR_error_string(ERR_get_error(),
1855 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001856 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001857 }
1858
David Galeanocc148e42013-01-10 10:18:59 +08001859#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001860 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001861#endif
David Galeano77a677c2013-01-10 10:14:12 +08001862 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001863 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001864
Andy Greena1ce6be2013-01-18 11:43:21 +08001865#ifndef LWS_NO_CLIENT
1866
Andy Green90c7cbc2011-01-27 06:26:52 +00001867 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001868
Andy Green1b265272013-02-09 14:01:09 +08001869 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001870 method = (SSL_METHOD *)SSLv23_client_method();
1871 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001872 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001873 ERR_error_string(ERR_get_error(),
1874 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001875 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001876 }
1877 /* create context */
1878 context->ssl_client_ctx = SSL_CTX_new(method);
1879 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001880 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001881 ERR_error_string(ERR_get_error(),
1882 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001883 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001884 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001885
David Galeanocc148e42013-01-10 10:18:59 +08001886#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001887 SSL_CTX_set_options(context->ssl_client_ctx,
1888 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001889#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001890 SSL_CTX_set_options(context->ssl_client_ctx,
1891 SSL_OP_CIPHER_SERVER_PREFERENCE);
1892 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1893 CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001894
Peter Hinz56885f32011-03-02 22:03:47 +00001895 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001896 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001897 if (!SSL_CTX_load_verify_locations(
1898 context->ssl_client_ctx, NULL,
1899 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001900 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001901 "Unable to load SSL Client certs from %s "
1902 "(set by --with-client-cert-dir= "
1903 "in configure) -- client ssl isn't "
1904 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001905 } else
1906 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001907 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001908 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001909 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001910 "Unable to load SSL Client certs "
1911 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001912 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001913
1914 /*
1915 * callback allowing user code to load extra verification certs
1916 * helping the client to verify server identity
1917 */
1918
1919 context->protocols[0].callback(context, NULL,
1920 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1921 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001922 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001923#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001924
Andy Greenc6bf2c22011-02-20 11:10:47 +00001925 /* as a server, are we requiring clients to identify themselves? */
1926
Andy Greenb5b23192013-02-11 17:13:32 +08001927 if (info->options &
1928 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001929
1930 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001931
Peter Hinz56885f32011-03-02 22:03:47 +00001932 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001933 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1934 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001935
1936 /*
1937 * give user code a chance to load certs into the server
1938 * allowing it to verify incoming client certs
1939 */
1940
Peter Hinz56885f32011-03-02 22:03:47 +00001941 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001942 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001943 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001944 }
1945
Peter Hinz56885f32011-03-02 22:03:47 +00001946 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001947
1948 /* openssl init for server sockets */
1949
Andy Green3faa9c72010-11-08 17:03:03 +00001950 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001951 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001952 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001953 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001954 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001955 info->ssl_cert_filepath,
Andy Greenb5b23192013-02-11 17:13:32 +08001956 ERR_error_string(ERR_get_error(),
1957 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001958 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001959 }
1960 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001961 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001962 info->ssl_private_key_filepath,
1963 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001964 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001965 info->ssl_private_key_filepath,
1966 ERR_error_string(ERR_get_error(),
1967 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001968 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001969 }
1970 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001971 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001972 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001973 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001974 }
1975
1976 /* SSL is happy and has a cert it's content with */
1977 }
1978#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001979
Andy Greendf736162011-01-18 15:39:02 +00001980 /* selftest */
1981
1982 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001983 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00001984
Andy Greena1ce6be2013-01-18 11:43:21 +08001985#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001986 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001987
Andy Green1b265272013-02-09 14:01:09 +08001988 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001989 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001990
Andy Green4739e5c2011-01-22 12:51:57 +00001991 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1992 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001993 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001994 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001995 }
Andy Green775c0dd2010-10-29 14:15:22 +01001996
Joakim Soderberg4c531232013-02-06 15:26:58 +09001997#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08001998 /*
1999 * allow us to restart even if old sockets in TIME_WAIT
2000 * (REUSEADDR on Unix means, "don't hang on to this
2001 * address after the listener is closed." On Windows, though,
2002 * it means "don't keep other processes from binding to
2003 * this address while we're using it)
2004 */
Andy Green6ee372f2012-04-09 15:09:01 +08002005 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2006 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002007#endif
Andy Green6c939552011-03-08 08:56:57 +00002008
2009 /* Disable Nagle */
2010 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002011 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2012 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002013
Joakim Soderberg4c531232013-02-06 15:26:58 +09002014 #ifdef WIN32
2015 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002016 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002017 #else
Andy Greene2160712013-01-28 12:19:10 +08002018 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002019 #endif
Andy Greene2160712013-01-28 12:19:10 +08002020
Andy Green4739e5c2011-01-22 12:51:57 +00002021 bzero((char *) &serv_addr, sizeof(serv_addr));
2022 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002023 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002024 serv_addr.sin_addr.s_addr = INADDR_ANY;
2025 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002026 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002027 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002028 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002029
2030 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2031 sizeof(serv_addr));
2032 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002033 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002034 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002035 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002036 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002037 }
Andy Green0d338332011-02-12 11:57:43 +00002038
Andy Greenb5b23192013-02-11 17:13:32 +08002039 wsi = (struct libwebsocket *)malloc(
2040 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002041 if (wsi == NULL) {
2042 lwsl_err("Out of mem\n");
2043 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002044 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002045 }
Andy Greenb5b23192013-02-11 17:13:32 +08002046 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002047 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002048#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002049 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002050#endif
Andy Green0d338332011-02-12 11:57:43 +00002051 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002052
2053 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002054
Andy Green65b0e912013-01-16 07:59:47 +08002055 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2056 context->listen_service_count = 0;
2057 context->listen_service_fd = sockfd;
2058
Andy Greena824d182013-01-15 20:52:29 +08002059 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002060 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002061 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002062#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002063
Andy Green6ee372f2012-04-09 15:09:01 +08002064 /*
2065 * drop any root privs for this process
2066 * to listen on port < 1023 we would have needed root, but now we are
2067 * listening, we don't want the power for anything else
2068 */
Peter Hinz56885f32011-03-02 22:03:47 +00002069#ifdef WIN32
2070#else
Andy Green1b265272013-02-09 14:01:09 +08002071 if (info->gid != -1)
2072 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002073 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002074 if (info->uid != -1)
2075 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002076 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002077#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002078
Andy Green6f520a52013-01-29 17:57:39 +08002079 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002080
Peter Hinz56885f32011-03-02 22:03:47 +00002081 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002082 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002083 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002084
Andy Green43db0452013-01-10 19:50:35 +08002085 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002086 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002087
Andy Green1b265272013-02-09 14:01:09 +08002088 info->protocols[context->count_protocols].owning_server =
2089 context;
2090 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002091 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002092
2093 /*
2094 * inform all the protocols that they are doing their one-time
2095 * initialization if they want to
2096 */
2097 info->protocols[context->count_protocols].callback(context,
2098 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002099 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002100
Andy Green3182ece2013-01-20 17:08:31 +08002101#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002102 /*
2103 * give all extensions a chance to create any per-context
2104 * allocations they need
2105 */
2106
2107 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002108 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002109 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002110
Andy Green1b265272013-02-09 14:01:09 +08002111 if (info->extensions) {
2112 ext = info->extensions;
2113 while (ext->callback) {
2114 lwsl_ext(" Extension: %s\n", ext->name);
2115 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002116 (enum libwebsocket_extension_callback_reasons)m,
2117 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002118 ext++;
2119 }
Andy Greena41314f2011-05-23 10:00:03 +01002120 }
Andy Green3182ece2013-01-20 17:08:31 +08002121#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002122 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002123
2124bail:
2125 libwebsocket_context_destroy(context);
2126 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002127}
Andy Greenb45993c2010-12-18 15:13:50 +00002128
Andy Greenb45993c2010-12-18 15:13:50 +00002129/**
2130 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002131 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002132 * @wsi: pointer to struct websocket you want to know the protocol of
2133 *
Andy Green8f037e42010-12-19 22:13:26 +00002134 *
Andy Green6f520a52013-01-29 17:57:39 +08002135 * Some apis can act on all live connections of a given protocol,
2136 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002137 */
Andy Greenab990e42010-10-31 12:42:52 +00002138
Andy Greenb45993c2010-12-18 15:13:50 +00002139const struct libwebsocket_protocols *
2140libwebsockets_get_protocol(struct libwebsocket *wsi)
2141{
2142 return wsi->protocol;
2143}
2144
Andy Green82c3d542011-03-07 21:16:31 +00002145int
2146libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2147{
Andy Green623a98d2013-01-21 11:04:23 +08002148 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002149}
Alex Bligh49146db2011-11-07 17:19:25 +08002150
David Galeanoe2cf9922013-01-09 18:06:55 +08002151unsigned char
2152libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2153{
Andy Green623a98d2013-01-21 11:04:23 +08002154 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002155}
2156
Andy Green2af4d5b2013-02-18 16:30:10 +08002157int
Alex Bligh49146db2011-11-07 17:19:25 +08002158libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2159{
Andy Green67d556c2013-02-15 22:31:55 +08002160 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002161 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002162
Alex Bligh49146db2011-11-07 17:19:25 +08002163 /* allocate the per-connection user memory (if any) */
2164
2165 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2166 wsi->user_space = malloc(
2167 wsi->protocol->per_session_data_size);
2168 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002169 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002170 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002171 }
Andy Green6ee372f2012-04-09 15:09:01 +08002172 memset(wsi->user_space, 0,
2173 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002174 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002175 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002176}
Andy Green43db0452013-01-10 19:50:35 +08002177
Andy Green0b319092013-01-19 11:17:56 +08002178static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002179{
Andy Green0b319092013-01-19 11:17:56 +08002180 char buf[300];
2181 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002182 int n;
2183
2184 gettimeofday(&tv, NULL);
2185
2186 buf[0] = '\0';
2187 for (n = 0; n < LLL_COUNT; n++)
2188 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002189 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002190 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002191 break;
2192 }
Andy Greenb5b23192013-02-11 17:13:32 +08002193
Andy Green0b319092013-01-19 11:17:56 +08002194 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002195}
2196
Joakim Soderberg4c531232013-02-06 15:26:58 +09002197#ifdef WIN32
2198void lwsl_emit_syslog(int level, const char *line)
2199{
2200 lwsl_emit_stderr(level, line);
2201}
2202#else
Andy Greenc11db202013-01-19 11:12:16 +08002203void lwsl_emit_syslog(int level, const char *line)
2204{
2205 int syslog_level = LOG_DEBUG;
2206
2207 switch (level) {
2208 case LLL_ERR:
2209 syslog_level = LOG_ERR;
2210 break;
2211 case LLL_WARN:
2212 syslog_level = LOG_WARNING;
2213 break;
2214 case LLL_NOTICE:
2215 syslog_level = LOG_NOTICE;
2216 break;
2217 case LLL_INFO:
2218 syslog_level = LOG_INFO;
2219 break;
2220 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002221 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002222}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002223#endif
Andy Greenc11db202013-01-19 11:12:16 +08002224
Andy Green43db0452013-01-10 19:50:35 +08002225void _lws_log(int filter, const char *format, ...)
2226{
Andy Greende8f27a2013-01-12 09:17:42 +08002227 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002228 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002229
2230 if (!(log_level & filter))
2231 return;
2232
Andy Green43db0452013-01-10 19:50:35 +08002233 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002234 vsnprintf(buf, sizeof(buf), format, ap);
2235 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002236 va_end(ap);
2237
Andy Green0b319092013-01-19 11:17:56 +08002238 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002239}
2240
2241/**
2242 * lws_set_log_level() - Set the logging bitfield
2243 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002244 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2245 * function to perform log string emission instead of
2246 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002247 *
Andy Greenc6511a02013-02-19 10:01:48 +08002248 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002249 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002250 */
2251
Andy Greenb5b23192013-02-11 17:13:32 +08002252void lws_set_log_level(int level, void (*log_emit_function)(int level,
2253 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002254{
2255 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002256 if (log_emit_function)
2257 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002258}